Prolog is a logic programming language. The name Prolog is taken from programmation en logique (French for "programming in logic"). It was created by Alain Colmerauer and Robert Kowalski around 1972 as an alternative to the American-dominated Lisp programming languages. It is an attempt to make a programming language that enables the expression of logic instead of carefully specified instructions on the computer. In some ways Prolog is a subset of Planner, e.g., see Kowalski's early history of logic programming. The ideas in Planner were later further developed in the Scientific Community Metaphor.
Prolog is used in many artificial intelligence programs and in computational linguistics (especially natural language processing, which it was originally designed for). Its syntax and semantics are considered very simple and clear. (The original goal was to provide a tool for computer-illiterate linguists.) A lot of the research leading up to modern implementations of Prolog came from spin-off effects caused by the fifth generation computer systems project (FGCS) which chose to use a variant of Prolog named Kernel Language for their operating system (but this area of research is now actually almost defunct).
Prolog is based on first-order predicate calculus; however it is restricted to allow only Horn clauses. See Logic programming for a discussion of the relationship of Prolog to mathematical logic. Execution of a Prolog program is effectively an application of theorem proving by first-order resolution. Fundamental concepts are unification, tail recursion, and backtracking.
The so called anonymous variable (explained below), a wildcard which means 'any variable', is written as a single underscore (_).
For programmer's convenience, the lists can be constructed and deconstructed in a variety of ways.
cat(tom).
This enters into the database the fact that 'tom' is a 'cat'. More formally, 'cat' is the head, and 'tom' is the single argument. Here are some sample queries you could ask a Prolog interpreter basing on this fact:
is tom a cat?
?- cat(tom). yes.
what things are cats?
?- cat(X). X = tom; yes.
Predicates are usually defined to express some fact the program knows about the world. In most of the cases, the usage of predicates requires a certain convention. Thus, which version of the two below would signify that Bob is the father of Sally?
father(sally,bob). father(bob,sally).
In both cases 'father' is the head and 'sally' and 'bob' are arguments. However in the first case, Sally comes first in the argument list, and in the second, Bob comes first (the order in the argument list matters). The first case is an example of a definition in Verb Subject Object order, and the second of Verb Object Subject order. Since Prolog does not understand English, both versions are fine so far as it is concerned; however it is good programming style to stick to either convention during the writing of a single program, in order to avoid writing something like
father(bob,sally). father(jessica,james).
Some predicates are built into the language, and allow a Prolog program to perform routine activities (such as input/output, using graphics and otherwise communicating with the operating system). For example, the predicate write can be used for output to the screen. Thus,
write('Hello').
will display the word 'Hello' on the screen.
light(on) :- switch(on).
The ":-" means "if"; this rule means light(on) is true if switch(on) is true. Rules can also make use of variables; variables begin with capital letters while constants begin with lower case letters. For example,
father(X,Y) :- parent(X,Y),male(X).
This means "if someone is a parent of someone and he's male, he is a father". The antecedent and consequent are in reverse order to that normally found in logic: the consequent is written first and called the head of the rule, the antecedent is called the body. Conjunction (and) is written as ",", while disjunction (or) is written as ";". It is also possible to place multiple predicates in a body which are joined with disjunction, for example:
a :- b;c;d.
which is simply equivalent to three separate rules:
a :- b. a :- c. a :- d.
What is not allowed are rules like:
a;b :- c.
that is "if c then a or b". This is because of the restriction to Horn clauses.
sibling(X,Y) :- parent(Z,X), parent(Z,Y). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). mother(trude, sally). father(tom, sally). father(tom, erica). father(mike, tom).
This results in the following query being evaluated as true:
?- sibling(sally, erica). yes.
The interpreter arrives at this result at matching the rule sibling(X,Y) by binding sally to X and erica to Y. This means the query can be expanded to parent(Z,sally), parent(Z,erica). Matching this conjunction is done by looking at all possible parents of sally. However, parent(trude,sally) doesn't lead to a viable solution, because if trude is substituted for Z, parent(trude,erica) would have to be true, and no such fact (or any rule that can satisfy this) is present. So instead, tom is substituted for Z, and erica and sally turn out to be siblings none the less. The code parent(X,Y) :- father(X,Y). might seem suspicious. After all, not every parent is a father. But actually this code means that any father is a parent. To infer that all fathers are male, you'd need to code male(X) :- father(X,_). which simply doesn't care whoever the child is (the underscore is an anonymous variable).
A rule such as
legal(X) :- \+ illegal(X).
can only be evaluated by exhaustively searching for all things illegal, comparing them to X, and if no illegal fact can be found to be the same as X, X is legal. This is called negation as failure. The \+/1 prefix operator used above implements negation as failure in ISO Prolog compilers.
For example, we can write code to count the number of elements in a list.
elems(*,0). elems(*, X) :- elems(T, Y), X is Y + 1.
This simply says: If the list is empty, the number of elements is zero. If the list is non-empty, then X is one higher than Y, which is the number of elements in the remainder of the list without the first element.
In this case, there is a clear distinction between the cases in the rules' antecedent. But consider the case where you need to decide whether to keep gambling in a casino:
gamble(X) :- gotmoney(X). gamble(X) :- gotcredit(X), \+ gotmoney(X).
If you have money, you keep gambling. If you've lost it all, you'll need to borrow money, or else no more gambling. gotmoney(X) might be a very costly function - for example, it might access your internet banking account to check your balance, which takes time. However, the same goes for gotcredit(X).
In theory, Prolog implementations might evaluate these rules out of order, so you might as well have written:
gamble(X) :- gotcredit(X), \+ gotmoney(X). gamble(X) :- gotmoney(X).
This is fine, because the two options exclude each other. However, checking whether you can get a loan is not necessary if you know you have money. So in practice, Prolog implementations will check the uppermost rule first (in fact, most Prologs will always try the rules in order from the uppermost to the lowermost). You can use the cut operator to tell the interpreter to skip the second option if the first suffices. For example:
gamble(X) :- gotmoney(X),!. gamble(X) :- gotcredit(X), \+ gotmoney(X).
This is called a green cut operator. The ! simply tells the interpreter to stop looking for alternatives. But you'll notice that if you need money it will need to check the second rule, and it will. Checking for gotmoney in the second rule is pretty useless since you already know you don't have any, otherwise the second rule wouldn't be evaluated in the first place. So you can change the code to
gamble(X) :- gotmoney(X),!. gamble(X) :- gotcredit(X).
This is called a red cut operator, because it is dangerous to do this. You now depend on the proper placement of the cut operator and the order of the rules to determine their logical meaning. Cut-and-paste accidents lurk in dark corners. If the rules got mixed up, you might now max out your credit card before spending your cash.
x = 2 =, 2
To evaluate the list we could use ordinary Prolog list manipulation. We use a binary predicate which takes the input list as its first argument and what ever rest we get after the parse is complete (the rest should be * if parse was successful). The following example shows how it can be done:
statement(A,B) :- id(A,C), assign_op(C,D), digit(D,B). id(*,X). assign_op(*,X). digit(*,X).
The nestling of list tails can be automated using the built in parsing mechanism, also known as arrow notation.
statement --> id, assign_op, digit.
Given the sentence expressed in BNF:
This can be written in Prolog using the arrow notation (assuming the input like =, 3, *, b, ;, b, =, 0, ; etc.):
sentence --> stat_part. stat_part --> statement ; stat_part, statement. statement --> id, expression, [;. expression --> operand ; operand, operator, expression. operand --> id ; digit. id --> ; [b. digit --> *, {D>=0, D=<9}. operator --> ; *.
/* Derivation Definition */ d(X,X,1) :- !. /* d x dx = 1 */ d(C,X,0) :- atomic(C). /* d c dx = 0 */ d(-U,X,-A) :- d(U,X,A). /* d -u dx = - d u dx */ d(U+V,X,A+B) :- d(U,X,A), d(V,X,B). /* d u+v dx = d u dx + d v dx */ d(U-V,X,A-B) :- d(U,X,A), d(V,X,B). /* d u-v dx = d u dx - d v dx */ d(C*U,X,C*A) :- atomic(C), C \= X, d(U,X,A), !. /* d c*u dx = c*d u dx */ d(U*V,X,B*U+A*V) :- d(U,X,A), d(V,X,B). /* d u*v dx = u*d v dx + v*d u dx */ d(U/V,X,A) :- d(U*V^(-1),X,A). /* d u/v dx = d (u*v)^-1 dx */ d(U^C,X,C*U^(C-1)*W) :- atomic(C), C \= X, d(U,X,W). /* d u^c dx = c*u^(c-1)*d u dx */ d(log(U),X,A*U^(-1)) :- d(U,X,A). /* d ln(u) dx = u^-1 * d u dx */
/* Integral Definition */ i(0,X,0) :- !. /* Int 0 dx = 0 */ i(X,X,(X*X)/2) :- !. /* Int X dx = (X^2)/2 */ i(C,X,C*X) :- atomic(C). /* Int c dx = c*x */ i(-U,X,-A) :- i(U,X,A). /* Int -U dx = - Int U dx */ i(U+V,X,A+B) :- i(U,X,A), i(V,X,B). /* Int U+V dx = Int U dx + Int V dx */ i(U-V,X,A-B) :- i(U,X,A), i(V,X,B). /* Int U-V dx = Int U dx - Int V dx */ i(C*U,X,C*A) :- atomic(C), C \= X, i(U,X,A), !. /* Int cU dx = c (Int U dx) */ i(X^C,X,(X^(C+1))/(C+1)) :- atomic(C), !. /* Int x^c dx = x^(c+1)/(c+1) */ i(U,V,U*V-A) :- d(V,U,A), !. /* Int u dv = u*v - Int v du */
/* Simplification Rules */ s(+,X,0,X). /* x + 0 = x */ s(+,0,X,X). /* 0 + x = x */ s(+,X,Y,X+Y). /* x + y = x + y */ s(+,X,Y,Z) :- integer(X), integer(Y), Z is X+Y. /* x + y = z <- Calculate */ s(*,_,0,0). /* anything * 0 = 0 */ s(*,0,_,0). /* 0 * anything = 0 */ s(*,1,X,X). /* 1 * x = x */ s(*,X,1,X). /* x * 1 = x */ s(*,X,Y,X*Y). /* x * y = x * y */ s(*,X*Y,W,X*Z) :- integer(Y), integer(W), Z is Y*W. s(*,X,Y,Z) :- integer(X), integer(Y), Z is X*Y. /* x * y = z <- Calculate */
/* Simplification Definition */ simp(E,E) :- atomic(E), !. simp(E,F) :- E =.. La, Ra, simp(La,X), simp(Ra,Y), s(Op,X,Y,F).
| Platform | Features | Toolkit | Prolog Mechanics | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Name | OS | Licence | Native Graphics | Unicode | Object Oriented | Native OS Control | Stand Alone Executable | C Interface* | Java Interface* | Interactive Interpreter | Debugger | Code Profiler | Syntax |
| DOS-PROLOG | MS-DOS | Shareware | TRUE | TRUE | TRUE | TRUE | TRUE | Edinburgh Prolog | |||||
| Open Prolog | Mac OS | Freeware | TRUE | ||||||||||
| Ciao Prolog | Unix, Windows | GPL | TRUE | TRUE | TRUE | TRUE | TRUE | ISO-Prolog | |||||
| GNU Prolog | Unix, Windows | GPL | TRUE | TRUE | TRUE | TRUE | TRUE | ISO-Prolog | |||||
| Visual Prolog | Windows | Freeware, Commercial | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | ||||
| SWI-Prolog | Unix, Windows, Mac OS X | LGPL | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | TRUE | ISO-Prolog, Edinburgh Prolog | ||
*C/Java interface can also be used for graphics and OS control.
Logic programming languages | ISO standards
برولوغ | Prolog | Prolog | Prolog (programovací jazyk) | Prolog (programmeringssprog) | Prolog (Programmiersprache) | Prolog | Prolog | Prolog | Prolog | 프롤로그 | Prolog | Prolog | Prolog | פרולוג (שפת תכנות) | პროლოგი | prolog | Prolog programozási nyelv | Prolog | Prolog | Prolog | Пролог (язык программирования) | Prolog | Prolog | ภาษาโปรล็อก | Prolog | Prolog