PROLOG
Brief History
• Kowalski: late 60’s Logician who showed logical
proof can support computation.
• Colmerauer: early 70’s Developed early version
of Prolog for natural language processing, mainly
multiple parses.
• Warren: mid 70’s First version of Prolog that was
efficient.
1
Basic Characteristics
• Prolog approximates first-order logic.
• Every program is a set of Horn clauses.
• Inference is by resolution.
• Search is by backtracking with unification.
• Basic data structure is term or tree.
• Variables are unknowns not locations.
• Prolog does not distinguish between inputs and outputs.
It solves relations/predicates.
Example
• Facts: ()
– likes(john,mary).
– likes(john,X). % Variables begin with capital
• Queries
– ?- likes(X,Y).
– X=john, y=Mary. % hit “;” for more
– ?- likes(X,X).
– X=john.
2
Example
• Rules
– likes(john,X) :- likes(X,wine). % :- = if
– likes(john,X):- female(X), likes(X,john).
• Note: variables are dummy. Standarized apart
• Some Facts:
– likes(bill,wine). female(mary). female(sue).
• Query: ? - likes(john,Y).
– Y = bill ;
– no.
Structure of Programs
• Programs consist of procedures.
• Procedures consist of clauses.
• Each clause is a fact or a rule.
• Programs are executed by posing queries.
3
Exercises
Simple Exercise 1a using SWI-Prolog
tasty(cheese).
Input facts & rules
tasty(bread).
made_from(cheese, milk).
made_from(bread, flour).
has(milk,calcium).
has(flour,cabohidrate).
contains(X,Y) :- made_from(X, Z), has(Z, Y).
?- tasty(A).
Make queries
?- has(milk,B).
?- made_from(bread,X).
?- contains(cheese,Y).
?- contains(X,Y).
4
Simple Exercise 1b using SWI-Prolog
tasty(chocolate).
Input facts & rules
tasty(cake).
made_from(chocolate, milk).
made_from(cake, flour).
has(milk,calcium).
has(flour,carbohidrate).
contains(X,Y) :- made_from(X, Z), has(Z, Y).
?- tasty(A).
Make queries
?- has(milk,B).
?- made_from(cake,X).
?- contains(chocolate,Y).
?- contains(X,Y).
◼ The law says that it is a crime for an
American to sell weapons to hostile
nations. The country Nono, an enemy of
America, has some missiles, and all of
its missiles were sold to it by Col. West,
who is an American.
◼ Prove that Col. West is a criminal.
10
5
Simple Exercise 2 using SWI-Prolog
mother(aminah, ahmed).
father(abdullah, ahmed). Input facts & rules
mother(aminah, ilham).
father(abdullah, ilham).
mother(aisha, aminah).
father(aisha, umar).
decendent(X,Y) :- parent(X,Y).
decendent(X,Y) :- parent(Y,Z), parent(Z,X).
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
?- parent(A, ahmed). Make queries
?- mother(X,Y).
?- descendent(X,Y).
11
Next…Exercises
12