It’s All About Logic, Not Instructions
The core reason Prolog looks so different is that it’s a declarative language, rooted in formal logic. Most mainstream languages are imperative; you provide a step-by-step recipe of how to achieve a goal. In contrast, Prolog asks you to describe what
is true about your world. You define a universe of facts and rules, and the language itself figures out how to answer questions within that universe. The name “Prolog,” coined in 1972, is short for “Programmation en logique” (Programming in logic). It was created by a team in Marseille, France, led by Alain Colmerauer, who wasn't trying to invent a new language but to process natural human language.
The Building Blocks: Facts and Rules
A Prolog program is essentially a knowledge base made of two simple things: facts and rules. A fact is a simple, unconditionally true statement, like `cat(tom).` or `likes(jerry, cheese).`. These statements establish the ground truth. A rule, however, defines a relationship that is true if certain conditions are met. Rules are where the famous `:-` syntax comes in, which can be read as “if.” For example, `friend(X, Y) :- likes(X, Y), likes(Y, X).` translates to: X is a friend of Y if X likes Y and Y likes X. This structure is derived from a concept in formal logic called a Horn clause, which provides a clean way to express logical implications.
Asking Questions with Queries
Once you've populated your world with facts and rules, you don't “run” the program in a traditional sense. Instead, you ask it questions, known as queries. A query looks like a fact but is entered at a prompt, often `?-`. For instance, you might ask `?- cat(tom).` and Prolog would respond `true.`. More powerfully, you can use variables (which always start with an uppercase letter) to ask open-ended questions. A query like `?- likes(jerry, What).` prompts Prolog to search its knowledge base for anything Jerry likes. The engine will find `cheese` and present it as the answer.
The Magic of Unification
The real power behind Prolog's simple syntax is a process called unification. When you issue a query with variables, the Prolog engine doesn't just search for exact matches; it tries to make the query and the facts or rule heads equivalent by binding values to the variables. It’s a sophisticated form of pattern matching. For a query like `friend(tom, X).`, the engine would look at the rule `friend(A, B) :- ...` and unify `tom` with `A` and the variable `X` with `B`. It then proceeds to solve the body of the rule with those bindings. This mechanism of pattern matching and variable binding is what allows Prolog to deduce new information and find complex solutions based on the simple truths you’ve provided.
A Design Born of Practicality and Theory
The syntax we see today is the result of a marriage between the practical goal of natural language processing and the theoretical work of logicians like Robert Kowalski. The French team built the first interpreter, and later, researchers at the University of Edinburgh, particularly David H.D. Warren, refined the syntax and created a highly efficient implementation that became the standard. While it may seem unusual, every piece of Prolog syntax, from the period at the end of every statement to the `:-` symbol and the capitalization of variables, is designed to serve its core purpose: to represent knowledge as clearly and logically as possible.











