Prolog Tutor
Prolog Tutor
Dave Saunders
(modified by Graem Ringwood)
Department of Computer Science
Queen Mary and Westfield College
0.
The notes are based on four weeks' of practical. Most statements in these notes hold for all
ersions of !rolog. They use "S# !rolog.
0.1 References
There are many books on !rolog and $". The following are the better ones%
&lach ! '())*+ Simply ,ogicial- Wiley
#'.eefe '())/+ The Craft of !rolog- M"T !ress
Shoham 0 '())*+ $rtificial "ntelligence Techni1ues in !rolog- Morgan .aufmann
2Sterling ,S and Shapiro 30 '())*+ The $rt of !rolog 4nd ed- M"T !ress
The one marked with a 2 is introductory.
0.2 Accessing Prolog
5nder 5ni6- !rolog may be accessed by typing%
prolog
in an 7term or telnet session. "f there is a prolog system running on the machine you will get
a prompt something like%
SICStus 2.1 #8: Wed Jan 19 12:23:17 GMT 1994
| ?-
There is an online manual for Sicstus !rolog which can be accessed on the Web with the
58,% http% 9 9 www. dcs.1mw.ac.uk%:/:/9 manuals 9 S"CStus;man9 sicstus;toc.html
Sicstus !rolog is both compiled and interpreted . !rograms are usually loaded from files. $
number of procedures are built-in- that is they are present before any user program is loaded.
#ne of these is consult- which loads a program from a file Type%
consult(file).
in response to the prompt to load the file called file.pl. Note the full-stop at the end. <y
conention- !rolog filenames end in .pl. "t is not necessary to type this e6tension. "f the
filename starts with a capital letter- or contains non= alphanumeric characters- you hae to
enclose it within 1uotes%
consult('file.pl').
To e6it the !rolog system type%
halt.
or
^D.
$ summary of all the built=ins is gien in the manual.
(
1. Week 1
1.1. Introduction
!rolog is a relational language. "t is a based on a subset of &#,. There are some non= logical
parts for finer control of program e6ecution.
1.2. The usual example
The following is a !rolog program representing a family database. This is the usual first
!rolog program.
mother'fred->ill+.
mother'flo->erri+.
mother'terry->erri+.
mother'>ames-rowena+.
mother'>ill-roberta+.
mother'rob-henrietta+.
mother'harry-petra+.
father'fred->ames+.
father'flo->ames+.
father'terry->ack+.
father'>ames-rob+.
father'>ill-re6+.
father'rob-harry+.
father're6-harry+.
father'harry-peter+.
parent'7-0+ %= mother'7-0+.
parent'7-0+ %= father'7-0+.
grandparent'7-0+ %= parent'7-?+-parent'?-0+.
ancestor'7-0+ %= parent'7-0+.
ancestor'7-0+ %= parent'7-?+-ancestor'?-0+.
$ll predicates are to be read in similar fashion eg father'fred->ames+ is read 'fred is the father
of >ames'. The family tree aboe looks like%
4
fred flo terry
jack jerri
jill james
roberta rex rowena rob
henrietta harry
petra peter
1.3. Prolog syntax
$ program is made up of such facts and rules. <oth are called definite clauses. Within these
are predicate expressions such as mother'>ames-rowena+ and ancestor'7-?+. $ predicate
e6pression consists of a predicate name followed by the arguments in parentheses- separated
by commas. "f there are no arguments then the parentheses are omitted. $ll the predicates
e6pressions in the aboe e6ample hae two arguments but there may be any number.
@ames of ob>ects may contain the underline symbol ;- letters and digits- but cannot start
with a digit. @ames starting with a lower case letter are constants and functions. @ames
starting with capital letters or underline represent ariables and are assumed to be
uniersally 1uantified. The scope of a ariable is the definite clause. Strings like (4A or =*A4
are integers in the usual way. Definite clauses are terminated with a period and a carriage
return- as can be seen aboe.
$ fuller account of the synta6 is gien in the manual.
1.4. Logical interpretation
$ !rolog rule- of the form%
p'7(-...7n+ %= 1'7(-...7n-0(-...0m+
has the logical reading
'7(-...7n+'p'7(-...7n+ '0(-...0m+'1'7(-...7n-0(-...-0m+++
or alternatiely
'7(-...7n-0(-...0m+'p'7(-...7n+ 1'7(-...7n-0(-...-0m++
Thus the last statement of the program has the logical reading
'7-0-?+'ancestor'7-0+ parent'7-?+ ancestor'?-0++
@ote therefore that a !rolog statement implicitly has uniersal 1uantification oer all
ariables in it. ,ogical or can be written using two !rolog statements.
To say that B7 is the parent of 0 if 7 is the mother of 0 #8 7 is the father of 0B
'7-0+'parent'7-0+ mother'7-0+ father'7-0++
would appear in !rolog as the two rules
parent'7-0+ %= mother'7-0+.
A
parent'7-0+ %= father'7-0+.
@ote that a fact such as father'fred->ames+ can be thought of as a rule 'father'fred->ames+ %= .+-
ie- fred is the father of >ames if ...- ie you don't need any condition to proe that the father of
fred is >ames. $ rule such as ' %= p'7+.+ is e1uialent to nothing can be concluded from p'7+-
ie p'7+ is false. This is more easily seen if we note the following e1uialences%
"S# !rolog logic
1 %= p. 1p
and hence
1 %= p. 1 Cp
1 %= . 1
%= p. C p
1.. The Prolog interpreter.
1..1. !etting "tarted
Copy the file 9i mport 9 t eaching9 $dMSc9 M$S9 family.pl to your own directory. Start up
the !rolog interpreter as described aboe and consult the file family. This is done by
responding
consult 'family+.
to the prompt. $ shorter way is simply DfamilyE.. #ou $%"T put a period &.& at the end.
The interpreter then checks for synta6 errors etc- but if there are none it replies with a
message indicating the consultation followed by%
yes
F G=
The yes means that it has satisfied the consultation. "f there were errors it would hae
indicated the position of the first error and then said no. $nything you respond to the
prompt is regarded as a 1uery which it tries to satisfy. The last line is then >ust another
prompt.
$ fuller account of how to run !rolog is gien in the maual.
1..2. 'uerying facts
0ou can now 1uery the fact and rule base.
Thus
F G= father'flo->ames+.
yes
'83M3M<38 T# !5T TH3 '. &$T TH3 3@D.+
F G= father'fred-flo+.
no
F G=
*
"f you gie a 1uery with ariables in it- the system tries to find all ways of satisfying the
1uery = after each success it asks whether you wish it to continue. "f you want any other
answers type ( if not type .. Thus%
F G= mother'fred-7+.
7 I >ill J
no
F G= mother'7->erri+.
7 I flo J
7 I terry J
no
F G= father'7->ames+.
7 I fred .
yes
F G=
We could also hae tried father'7-0+ which would hae yielded all pairs 7-0 where the
father of 7 is 0- eg
F G= father'7-0+.
7 I fred
0 I >ames J
7 I flo
0 I >ames J
7 I terry
0 I >ack .
yes
F G=
1..3. 'uerying facts deri)ed from rules.
This is essentially the same- thus%
F G= parent'fred-M+.
M I >ill J
K
M I >ames J
no
F G= parent'M->ames+.
M I fred J
M I flo J
no
F G=
The system tries to satisfy parent'fred-M+. $ccordingly it runs through the facts and rules
until it finds parent in a head of a rule. The first such is parent'7-0+ %= mother'7-0+.. "t unifies
fred with 7 'more on unification later+ and tries to satisfy the body of the rule with this
unification ie it tries to satisfy mother'fred-M+. To do this it runs through the knowledge
base till it comes to mother'fred->ill+ which gies us the first solution M I >ill. #n receiing
the semicolon from us it tries again- but can't find any other occurences of mother with fred
as first argument. "t then looks again for a rule head containing parent. The ne6t such is
parent'7-0+ %= father'7-0+. so it tries to satisfy father'fred-M+ which gies another solution M
I >ames. #n receiing another semicolon it tries again- but can't satisfy parent'fred-M+ in
any other way. Trying to satisfy parent'M->ames+ is similar but this time mother neer can
be satisfied with >ames as second argument- but father can be satisfied twice.
@ote that the 1uery parent'terry->ack+ would first try to satisfy mother'terry->ack+ which fails-
and then father'terry->ack+ which succeeds- thus%
F G= parent'terry->ack+.
yes
F G= parent'>ames-fred+.
no
F G=
5sing the other predicates% grandparent- ancestor is similar.
F G= grandparent'fred-rowena+.
yes
F G= grandparent'7->ames+.
no
F G= ancestor'7-fred+.
no
F G= grandparent'>ames-<+.
< I henrietta J
L
< I harry J
no
F G= grandparent'7-rob+.
7 I fred J
7 I flo J
no
F G=
#f course- >ames probably has * grandparents- but only two of them seem to be in the
knowledge base. To satisfy grandparent'>ames-<+ it finds the rule grandparent etc and tries
to satisfy parent'>ames-?+ which it first does by trying to satisfy mother*>ames-?+ which
succeeds with ?Irowena. "t then tries to satisfy parent'rowena-<+ but fails. "t then backtracks
to the preious predicate ie parent'>ames-?+ and tries to resatisfy this. @eedless to say >ames
doesn't hae another mother so it tries using the other parent rule ie tries to satisfy
father'>ames-?+. "t can do this by setting ?Irob. "t then tries parent'rob-<+. "t can do this by
satisfying mother'rob-<+ with < I henrietta. $fter the semicolon it then satisfies
parent'rob-<+ again using father'rob-<+ with < I harry. "t tries again after the ne6t
semicolon- but there are no more solutions.
!randparent'7-rob+ is done similarly. "t tries to satisfy parent'7-?+ and hence mother'7-?+.
This can be done with 7Ifred-?I>ill. Howeer- parent'>ill-rob+ then fails. "t then tries
7Iflo-?I>erri but then parent'>erri-rob+ fails. This continues until it runs out of ways of
satisfying mother'7-?+- and tries father'7-?+ instead. "t satisfies this with 7Ifred-?I>ames-
and it tries to satisfy parent'>ames-rob+. This succeeds as father'>ames-rob+ succeeds.
grandparent'7-rob+ therefore succeeds with 7I fred. The ne6t attempt obtains the other
solution- and any further attempt fails.
0ou can watch all these contortions by doing
F G= trace.
Ma message about creepingN
yes.
F G= grandparent'7-rob+.
@ote the recursie definition of ancestor. ,ogically the interpretation is
'7-0+'ancestor'7-0+ parent'7-0++