1 Prolog
1 Prolog
1 Prolog
Books
• Text Book: PROLOG programming for AI By Ivan Bratko,
P.Ed. Publication, 3rd Edition
Optional books for further reading:
• The Art of Prolog, Leon Sterling and Ehud Shapiro, 2nd
Edition, MIT Press, 1994.
• W. F. Clocksin, C. S. Mellish, Programming in
Prolog:Using the Iso Standard, 5th edition, Springer-
Verlag,2003.
• Prolog : A Relational language and its Applications, John
Malpas, Prentice-Hall, Inc. 1987.
• “Programming in Prolog”, by Clocksin & Mellish, Narosa
Publishing House.
Prolog Interpreter/Compiler
• Most common free Prolog implementation is
– You can download freely EasyProlog compiler by Strawberry also.
– SWI Prolog is Very nice,
– though faster ones are for sale (e.g., SICSTUS Prolog).
– SICStus Prolog is a commercial s/w used in a wide range of
domains from medicine research to data mining of financial data.
6. The entire content of your file is then stored in the memory of the Prolog
interpreter.
– You can see what is consulted by typing | ?- listing.
7. Then you can ask questions of your database.
Using Prolog (2)
• If you edit your program file (e.g. to correct something), be sure to
consult it again afterwards!
• To exit from Prolog, type
|?- halt.
or press
Control/D
• The Prolog comment characters:
5 ?- go(saarbruecken, auckland).
saarbruecken to_frankfurt by train
frankfurt to_bangkok by plane
bangkok to_auckland by plane
saarbruecken to_paris by train
paris to_losAngeles by plane
losAngeles to_auckland by plane
false.
6 ?- go(auckland,craglan).
false.
7 ?- go(auckland,raglan).
auckland to_hamilton by car
hamilton to_raglan by car
false. */
Imperative vs Declarative
• Programming in imperative programming (e.g.
Java, Pascal, C++), you tell the computer HOW to
solve the problem, i.e. do this, then do that.
numbers atoms
Prolog Data Objects (Terms)
Simple objects Structured Objects
Symbols Signs
a Strings <--->
bob ‘a’ ==>
l8r_2day ‘Bob’ …
‘L8r 2day’
THREE SYNTACIC FORMS
FOR ATOMS
(1) Strings of letters, digits, and “_”, starting with
lowercase letter:
alpha_beta_algorithm taxi_35
1 1313 0 -55
somerelationship(a,b,c,[1,2,3])
|?- X = date(04,10,04).
X = date(04,10,04)?
yes
TREE REPRESENTATION
OF STRUCTURES
date
17 june 2006
TREE REPRESENTATION
OF STRUCTURES
• Therefore all structured objects in Prolog can be viewed
as trees
P2 = (2,3) (6,4)
S T
(4,2)
P1=(1,1) (7,1)
P1 = point( 1, 1) P2 = point( 2, 3)
S = seg( P1, P2) = seg( point(1,1), point(2,3))
T = triangle( point(4,2), point(5,4), point(7,1))
LINE SEGMENT
S = seg
point point
1 1 2 3
ARITHMETIC EXPRESSIONS
ARE ALSO STRUCTURES
• For example: (a + b) * (c - 5)
*( +( a, b), -( c, 5))
*
+ -
a b c 5
A Particular Structure
• How can we represent the courses a student takes?
courses(csi2111, csi2114, csi2165)
courses(csi2114, csi2115, csi2165, mat2343)
courses(adm2302, csi2111, csi2114, csi2115, csi2165)
• Three different structures.
• In general, how do we represent a variable number of
arguments with a single structure?
• LISTS
Naming tips
• Use real English when naming predicates,
constants, and variables.
e.g. “John wants to help Somebody.”
Could be: wants(john,to_help,Somebody).
Not: x87g(j,_789).
• Use a Verb Subject Object structure:
wants(john,to_help).
• BUT do not assume Prolog Understands the
meaning of your chosen names!
• You create meaning by specifying the body (i.e.
preconditions) of a clause.
Clauses/Rules
• In Prolog, rules (and facts) are called clauses.
• A clause always ends with ‘.’
– Clause: <head> :- <body>.
• you can conclude that <head> is true, if you canprove that
<body> is true
– Facts - clauses with an empty body: <head>.
• you can conclude that <head> is true
– Rules - normal clauses (or more clauses)
– Queries - clauses with an empty head: ?- <body>.
• Try to prove that <body> is true
Queries
• The goal represented as a question.
• Once we have a Prolog program we can use it to answer a
query.
?- parent(philip, anne).
?- border(wales, scotland).
• The Prolog interpreter responds ‘yes’ or ‘no’.
• Note that all queries must end with a dot.
• A query may contain variables:
• ?- parent(philip, Who).
• The Prolog interpreter will respond with the values for the
variables which make the query true (if any).
• Otherwise it will respond with a ‘no’.
Questions/Queries
• Questions based on facts
• Answered by matching
Two facts match if their predicates are same
(spelt the same way) and the arguments
each are same.
• Procedural readings:
• To solve problem P, first solve subproblem Q and then R.
• To satisfy P, first satisfy Q and then R.
Conjunctions
• Use ‘,’ and pronounce it as and.
• Example
• Facts:
• likes(mary,food).
• likes(mary,tea).
• likes(john,tea).
• likes(john,mary)
• ?- likes(mary,X),likes(john,X).
• Meaning is anything liked by Mary also liked by John?
• X = tea
Backtracking (an inherent property of prolog
programming)
?- likes(mary,X) , likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
• For example:
?- date( D1, M1, 2006) = date( D2, june, Y2),
date( D1, M1, 2006) = date( 17, M3, Y3).
?- X = f(X).
• Rules:
predecessor(X,Y) :- parent(X,Y).
predecessor(X,Y) :- parent(X, Z), predecessor (Z, Y).
Recursive Definition
• Rules:
predecessor(X,Y) :- parent(X,Y).
predecessor(X,Y) :- parent(X, Z), predecessor (Z, Y).
greetings:- ?- greetings.
write(‘What is your name?’), What is your name?
nl, |: tim.
Hello tim
read(X),
X = tim ?
write(‘Hello ‘), yes
write(X).
Prolog Code Terminal
Arity
• greetings is a predicate with no arguments.
• The number of arguments a predicate has is called its
arity.
– The arity of greetings is zero = greetings/0
• The behaviour of predicates can be made more specific by
including more arguments.
– greetings(hamish) = greetings/1
greet(amelia):-
write('Awfully nice to see you!').
| ?- greet(hamish).
Hullo hamish?
yes
greet(amelia):-
write('Awfully nice to see you!').
greet(Anybody):-
write('Hullo '), write(Anybody).
• ?- X is 3*4.
X = 12
yes
Logical Operators
• a :- b. % a if b
• a :- b,c. % a if b and c.
• a :- b;c. % a if b or c.
• a :- \++ b. % a if b is not provable
• a :- not b. % a if b fails
• not(S) :- S, ! , fail ; true.
• a :- b -> c ; d. % a if (if b then c else d)
4 ?- consult('G:/AI lecture ppts july 2009/lecture
%SWI program programs/OR_demo.pl').
not(S) :- S, ! , fail ; true. % G:/AI lecture ppts july 2009/lecture
programs/OR_demo.pl compiled 0.00 sec, 0 bytes
b. true.
5 ?- a.
c. true.
6 ?- p.
d. true.
e. 7 ?- q.
true ;
a :- b. % a if b ;
true.
p :- b,c. % p if b and c. 8 ?- | r.
q :- b;d. % q if b or d. false.
9 ?- m.
%k :- \++ b. % k if b is not true.
provable 10 ?- n.
true ;
r :- not(b). % a if b fails ;
r :- \+ b. % r if b is not true true ;
;
m :- b -> c ; d. % m if (if b then c true.
11 ?- j.
else d) true ;
n:-b; c, d; e. ;
true ;
j:-b; c; d; e. ;
true ;
;
true.
Input/output
built-in predicates for input and output work as follows:
No backtracking!
=> take into account procedural behavior
equivalent:
fact(a,b,c).
fact(a,b,c) :- true.