You are on page 1of 15

Introduction to Prolog

Page 1 of 15

Prolog is a language (originally) based on (a subset of first order predicate) logic.


A program written in PROLOG is simply a collection of
Facts (assertions or assumptions) and
Rules of inference.
each of which are called clauses.
We can describe a simplified syntax of a Prolog program as follows
<program> ::= <clause>. | <clause> . <program>
<clause> ::= <fact> | <rule>
<fact> ::= <goal>
<rule> ::= <goal> :- <goallist>
<goallist> ::= <goal> | <goal> , <goallist>
<goal> ::= <predicate>(<argumentlist>)
<argumentlist> := <argument> | <argument> , <argumentlist>
<argument> ::= <constant> | <variable>
Note that PROLOG is case sensitive - all variables are written in upper case, constants, functions and
predicates1 (relations) are written in lower case. All clauses terminate with a period (.).
Example 1:

Blocks World
a

c
table

We introduce the predicate on(X,Y) read as X is directly on top of Y to represent the above configuration of
blocks as
on(a,b).
/*
on.1
*/
on(b,t).
/*
on.2
*/
on(c,t).
/*
on.3
*/
These assertions correspond to the facts of the Prolog program. The predicate on has arity2 2 and is often
described in Prolog as on/2. I will use the method above when such predicates are introduced as it reflects the
intended meaning that we wish to capture by the facts and rules.
We may now introduce further predicates defined in terms of on. For example, suppose we want to specify the
above relation. This corresponds to two clauses as follows.
above(X,Y) :- on(X,Y).
above(X,Y) :- on(X,Z),above(Z,Y).

/*
/*

above.1 */
above.2 */

These assertions written in terms of the variables X,Y and Z are rules of the Prolog program.
Notice that the rules as we shall see enable us to make logical inferences. The clauses on.1 on.3 and above.1
above.4 constitute a Prolog program.
PROLOG adopts a goal directed proof using the closed world assumption3 (cwa). In general, PROLOG
constructs an and/or graph (hypergraph4) in the order that the clauses are declared. In the case of a failure
1

A predicate is a Boolean valued function.


Number of arguments.
3
Only the facts asserted in the program are true, all other facts are assumed to be false.
4
A graph is like a tree but may have loops I will often refer to hypergraphs as Proof Trees.
2

Introduction to Prolog

Page 2 of 15

PROLOG backtracks to test more clauses. If there are no more clauses to test then PROLOG will return no
(failure).
For example, in the case of the goal clause5 on(a,a), PROLOG attempts to unify on(a,a) with each of the
clauses for on (clauses 1 - 3), in each case it fails, exhausts all possibilities and terminates. Thus the closed
world principle dictates that a is not on itself since it cannot be proved to be so! The proof tree corresponding
to this goal clause can be simply drawn as

on(a,a)

failure

The root not is the goal clause on(a,a), the arrows correspond to an attempt to unify (or match) the goal clause
with each of the facts given by the on clauses in the program each of which end in failure.
As in all programming languages, Prolog has a built in clause not, such that not(P) is true in the case that the
(sub)goal P fails. In our example, the clause not( on(a,a)). would return true or success. We will return to
negation later.
PROLOG is sensitive to both the order that the clauses are declared and the order that the subgoals are declared.
As we shall see, if we are not careful, when formalising recursive definitions in PROLOG, computations may
never terminate (in theory that is). This is no different to many other languages, but unfortunately logically
equivalent sentences may give rise to completely different computations (and outputs) in PROLOG.
Running the Prolog interpreter.
First of all you should install the Prolog interpreter from the SWI website (www.SWI-Prolog.org). Installation
does not take long and use all the default options6. In the bin directory contains the file plwin (you may make
a shortcut to this file) selecting this file will start the Prolog interpreter. Once started, select File and use the
consult option to open your Prolog program (see below).
Step 1. Using a suitable text editor to write the Prolog clauses.
on(a,b).
on(b,t).
on(c,t).
above(X,Y) :- on(X,Y).
above(X,Y) :- on(Z,Y),above(X,Z).

/* on.1 */
/* on.2 */
/* on.3 */
/* above.1 */
/* above.2 */

Step 2. Save as blocks.pl


Step 3. You may now invoke the Prolog interpreter by opening the file blocks.pl. You should see something
like (depending on the version)
Welcome to SWI-Prolog (Version 3.2.8)
Copyright (c) 1993-1998 University of Amsterdam. All rights reserved.
For help, use ?- help(Topic). or ?- apropos(Word).
d:/prolog/blocks1.pl compiled, 0.00 sec, 856 bytes.
?- on(X,t).

(here I have entered the goal clause on(X,t))

X=b;

(here I have typed ; to force prolog to search for more solutions)

X=c;
5
6

A goal clause is a clause that we are trying to prove.


You do this only once.

Introduction to Prolog

Page 3 of 15

No
?- above(a,Y).

(here I have entered the goal clause above(a,Y))

Y=b;
Y=t;
No
?(here I have entered ctrl C)
Action (h for help) ? (enter t) for trace
above(a,Y).
(here I have entered the goal clause above(a,Y))
Call:
Call:
Exit:
Exit:

(
(
(
(

7) above(a, _G160) ? creep


8) on(a, _G160) ? creep
8) on(a, b) ? creep
7) above(a, b) ? creep

Y=b;
Redo: ( 7) above(a, _G160) ? creep
Call: ( 8) on(a, _L146) ? creep
Exit: ( 8) on(a, b) ? creep
Call: ( 8) above(b, _G160) ? creep
Call: ( 9) on(b, _G160) ? creep
Exit: ( 9) on(b, t) ? creep
Exit: ( 8) above(b, t) ? creep
Exit: ( 7) above(a, t) ? creep
Y=t
Yes
[debug] ?-

(Here I terminated the session)

PROLOG's Deductive Apparatus


PROLOG adopts a goal directed proof strategy. We will demonstrate Prolog's search strategy by the way of a
another example drawn from a blocks world.
Example 2
on(d,a).
on(c,b).
on(e,d).
on(e,c).
above(X,Y) :- on(X,Y).
above(X,Y) :- on(X,Z),above(Z,Y).
This configuration is depicted as
e
c
b

d
a

/*
/*
/*
/*
/*
/*

on.1
on.2
on.3
on.4
above.1
above.2

Introduction to Prolog

Page 4 of 15

The following diagram is the Prolog search tree for the goal clause above(d,Y).

above(d,Y)

on(d,Y)

on(d,Z1),above(Z1,Y)

Y=a

Z1 = a

Success

above(a,Y)

on(a,Y)

Fail

Fail

on(a,Z2),above(Z2,Y)

Fail

We can relate such a search tree to a trace of the Prolog program given below.
Welcome to SWI-Prolog (Version 3.2.8)
Copyright (c) 1993-1998 University of Amsterdam. All rights reserved.
For help, use ?- help(Topic). or ?- apropos(Word).
d:/prolog/blocks.pl compiled, 0.00 sec, 932 bytes.
?Action (h for help) ? trace
above(d,Y).
Call: ( 7) above(d, _G160) ? creep
Call: ( 8) on(d, _G160) ? creep
Exit: ( 8) on(d, a) ? creep
Exit: ( 7) above(d, a) ? creep
Y=a;
Redo: ( 7) above(d, _G160) ? creep
Call: ( 8) on(d, _L146) ? creep
Exit: ( 8) on(d, a) ? creep
Call: ( 8) above(a, _G160) ? creep
Call: ( 9) on(a, _G160) ? creep
Fail: ( 9) on(a, _G160) ? creep
Redo: ( 8) above(a, _G160) ? creep
Call: ( 9) on(a, _L158) ? creep
Fail: ( 9) on(a, _L158) ? creep
Fail: ( 8) above(a, _G160) ? creep
Fail: ( 7) above(d, _G160) ? creep
No
[debug] ?The names _G160, _L146, _L158 are internally generated names for the variables Y,Z1 and Z2 respectively.

Introduction to Prolog

Page 5 of 15

Call says that prolog is attempting a (sub)goal. Exit says that the call to the subgoal has ended in success. Fail
says that the call has ended in failure and Redo says that prolog has backtracked and is attempting an alternative
clause for the subgoal.
The programming language Prolog as outlined above lacks expression, in particular in its expression of
negation and equality. The following is a short discussion of some additional features supported by Prolog.
However the simple semantics of the language is severely compromised by the use of these features (see
Appendix - Peyton Palace).
negation and the cut (!).
The cut is a procedural feature of Prolog used to delimit a search for solutions. Consider the following clauses
likes(mary,X) :- mouse(X),!,fail.
likes(mary,X) :- animal(X).

/* likes.1 */
/* likes.2 */

animal(X) :- mouse(X).
mouse(jerry).

/* animal.1 */
/* mouse.1 */

Given that jerry is a mouse then without the cut, the goal clause likes(mary,jerry) would hold by virtue of
of the clause likes.2. However, the presence of the cut will not allow Prolog to backtrack (across the cut) in an
attempt to find a solution.
a)

b)

for the Prolog program


likes(mary,X) :- mouse(X),!,fail.
likes(mary,X) :- rabbit(X).

/* likes.1 */
/* likes.2 */

rabbit(peter).
mouse(jerry).

/* rabbitl.1 */
/* mouse.1 */

and for the Prolog program


likes(mary,X) :- mouse(X),fail.
likes(mary,X) :- rabbit(X).

/* likes.1 */
/* likes.2 */

rabbit(peter).
mouse(jerry).

/* rabbit.1 */
/* mouse.1 */

We can write the "likes" clauses in part b) above more succinctly as


likes(mary,X) :- mouse(X),fail
;
rabbit(X).
negation (not)
not is defined in prolog as
not(P) :- P,!,fail
;
true.
where true is the goal that always succeeds. It is provided as a built in predicate as part of the language.
It should be noted that not implements negation by failure and uses the closed world assumption (cwa).
We could write mary's disliking for mice in Prolog as
likes(mary,X) :- animal(X),not(mouse(X)).

Introduction to Prolog

Page 6 of 15

WARNING:
We have to be careful when using not. Consider the following example.
likes(mary,X) :- mouse(X),fail.
likes(mary,X) :- rabbit(X).

/* likes.1 */
/* likes.2 */

rabbit(peter).
mouse(jerry).

/* rabbit.1 */
/* mouse.1 */

the goal clause not(rabbit(jerry)) returns Yes which we would expect and the goal clause not(mouse(peter))
also returns Yes (as we might expect).
However care has to be taken for clauses involving not and variables, for example the goal clause
not(rabbit(X)). does not find all those animals that are not rabbits, it gives No. Why????
Equality
Prolog supports a number of equalities, perhaps the most useful in the context of these notes is the binary
predicate =, such that T1 = T2 holds provided T1 and T2 match.
Suppose we wish to express that "mary likes all dogs except buster". This may be written in Prolog as
likes(mary,X) :- dog(X), not(X = buster).

Two websites of interest are


http://www.swi-prolog.org
http://cblslca.leeds.ac.uk/%7Epaul/prologbook/

Introduction to Prolog

Page 7 of 15

Exercise 1
Represent the following scenario in Prolog.
buster is a terrier, benji is a spaniel, all spaniels are dogs and all terriers are dogs. All terriers chase all dogs
Run your Prolog code for the goal clauses
?-dog(X).
?-chases(X,benji).
?-chases(X,buster).
Exercise 2
Represent the following scenario in Prolog.
tom loves sue. jane loves anyone who loves tom. brenda and mary love tom.
Run your Prolog code for the goal clauses
?-loves(X,tom).
?-loves(tom,Y).
?-loves(jane,Y).
Exercise 3
The following exercise is quite demanding. Enter the clauses in a piecemeal way
a) Create a program in PROLOG to express the following facts.
You should use the predicates
parentof(X,Y)
male(X)
female(X)

- read "X is a parentof Y"


- read as "X is male"
- read as "X is female"

WARNING: Prolog programming is not always a happy experience. Try to develop you programs using a
piecemeal approach and write simple goal clauses to test each part of your program as you go along.
Facts
david and janet are the parents of john and emma. (4 clauses)
ron and eva are the parents of david. (2 clauses)
jack is a parent of ron. (1 clause)
john, david, ron and jack are all males. (4 clauses)
emma, janet and eva are female. (3 clauses)
By using the predicates parentof, male and female write Rules for the following predicates (do one at a time
and test them as you progress)
fatherof(X,Y)
motherof(X,Y)
grandparent(X,Y)
ancestor(X,Y)

- read as "X is the father of Y"


- read as "X is the mother of Y"
- read as "X is a grandparent of Y"
- read as X is an ancestor of Y.

b)
Replace my family tree with one of your own. Can you write a clause to determine who is your
grandmother?

Introduction to Prolog

Page 8 of 15

Exercise 4:
With the aid of the following trace, Construct the search tree for the goal clause above(e,b). (see Example 2)
above(e,b).
Call: ( 7) above(e, b) ? creep
Call: ( 8) on(e, b) ? creep
Fail: ( 8) on(e, b) ? creep
Redo: ( 7) above(e, b) ? creep
Call: ( 8) on(e, _L145) ? creep
Exit: ( 8) on(e, d) ? creep
Call: ( 8) above(d, b) ? creep
Call: ( 9) on(d, b) ? creep
Fail: ( 9) on(d, b) ? creep
Redo: ( 8) above(d, b) ? creep
Call: ( 9) on(d, _L168) ? creep
Exit: ( 9) on(d, a) ? creep
Call: ( 9) above(a, b) ? creep
Call: ( 10) on(a, b) ? creep
Fail: ( 10) on(a, b) ? creep
Redo: ( 9) above(a, b) ? creep
Call: ( 10) on(a, _L180) ? creep
Fail: ( 10) on(a, _L180) ? creep
Fail: ( 9) above(a, b) ? creep
Fail: ( 8) above(d, b) ? creep
Redo: ( 8) on(e, _L145) ? creep
Exit: ( 8) on(e, c) ? creep
Call: ( 8) above(c, b) ? creep
Call: ( 9) on(c, b) ? creep
Exit: ( 9) on(c, b) ? creep
Exit: ( 8) above(c, b) ? creep
Exit: ( 7) above(e, b) ? creep
Yes
Assignment 4 (part 2)
Question
a) By choosing a suitable domain of discourse, constants and predicates represent the following scenario in
Prolog.
Tom who is a man loves Mary, moreover Jane loves all men. Charles loves only himself
b) Construct the search tree for the goal clause that returns everybody that Jane loves.
c) Define the clause female in terms of the clause male.
d) Write down the outcome of the goal clauses.
i.
female(charles).
ii
loves(X,X).

Introduction to Prolog

Page 9 of 15

Answers to the Exercises


1.
terrier(buster).
spaniel(benji).
dog(X) :- terrier(X);spaniel(X).
chases(X,Y) :- terrier(X),dog(Y).
2.
loves(tom,sue).
loves(jane,X) :- loves(X,tom).
loves(brenda,tom).
loves(mary,tom).
3.
/*

A family Tree

david and janet are the parents of john and emma. (4 clauses)
ron and eva are the parents of david. (2 clauses)
jack is a parent of ron. (1 clause)
john, david, ron and jack are all males. (4 clauses)
emma, janet and eva are female. (3 clauses)
By using the predicates parentof, male and female write Rules for the following predicates (do one at a time and
test them as you progress)
fatherof(X,Y)
motherof(X,Y)
grandparent(X,Y)
ancestor(X,Y)

- read as "X is the father of Y"


- read as "X is the mother of Y"
- read as "X is a grandparent of Y"
- read as X is an ancestor of Y.

*/
/*
parentof/2 clauses (facts) */
parentof(david,john).
parentof(david,emma).
parentof(janet,john).
parentof(janet,emma).
parentof(ron,david).
parentof(eva,david).
parentof(jack,ron).
/*
gender facts
male(john).
male(david).
male(ron).
male(jack).
female(emma).
female(janet).
female(eva).

*/

/*

*/

Some rules

fatherof(X,Y) :- male(X),parentof(X,Y).
motherof(X,Y) :- female(X),parentof(X,Y).
grandparentof(X,Y) :- parentof(X,Z),parentof(Z,Y).
ancestorof(X,Y) :- parentof(X,Y).
ancestorof(X,Y) :- parentof(X,Z),ancestorof(Z,Y).
4.

Introduction to Prolog

Page 10 of 15

on(d,a).
on(c,b).
on(e,d).
on(e,c).
above(X,Y) :- on(X,Y).
above(X,Y) :- on(X,Z),above(Z,Y).

/*
/*
/*
/*
/*
/*

on.1
on.2
on.3
on.4
above.1
above.2

above(e,b)

on(e,b)
fail
Z1=d

on(e,Z1),above(Z1,b)
Z1=c

above(d,b)

above(c,b)

on(d,b) on(d,Z2),above(Z2,b)
fail
Z2=a

on(c,b)
success

above(a,b)

on(a,b) on(a,Z3),above(Z3,b)
fail
fail

Introduction to Prolog

Page 11 of 15

Appendix I like this example so I have included it for those that are interested in Prolog and its
semantics.
The following example is an illustration of the way the non logical features of the cut changes the simple
meaning of the semantics of Prolog.
Example 3:
Peyton Palace
The Prolog program given below is an attempt to represent the following scenario
Diana loves everyone apart from herself
Charles loves Camilla but does not love Diana
James does not love anyone but himself
No-one (apart from Diana and James) loves James
Everyone (apart from James) loves their mother.
Everyone is loved by someone.
Charles and Philip do not love the same persons
Ann loves horses
Highhat is a horse.

3.1
By showing the output to the Prolog program (below), determine the outcome of the following goal clauses
a)
Who does James love
b)
Does Philip love Charles' mother

3.2
By constructing suitable and/or graphs (hypergraphs) explain why the goal clauses
loves(X,diana).
loves(lover(diana),Y).
fail and yet the goal clause
loves(lover(diana),diana).
succeeds

3.3
Construct a suitable and/or graph for the goal clause
loves(philip,ann).
Explain the significance of the outcome of the above goal clause.

Introduction to Prolog

Page 12 of 15

/* Peyton Palace */
/* diana loves everyone apart from herself */
loves(diana,diana) :- !,fail.
loves(diana,_).

/* look no further */

/* charles loves camilla but does not love diana */


loves(charles,camilla).
loves(charles,diana) :- !,fail.

/* we may assert negation */

/* james does not love anyone but himself */


loves(james,james).
loves(james,_) :- !,fail.

/* look no further */

/* no one but diana and james love james */


loves(_,james) :- !,fail.

/* loves(Diana,james) by virtue of above */

/* everyone (apart from james) loves their mother */


loves(X,mother(X)).

/* loves(james,mother(james)) fails earlier */

/* everyone is loved by someone */


loves(lover(X),X).

/* skolem function */

/* ann loves horses */


loves(ann,Y) :- horse(Y).
/* charles and philip do not love the same persons */
loves(philip,Y) :- loves(charles,Y),!,fail.
loves(charles,Y) :- loves(philip,Y),!,fail.
horse(highhat).

/* see Day at the Races */

Introduction to Prolog

Page 13 of 15

Answer
3.1
Welcome to SWI-Prolog (Version 3.3.7)
Copyright (c) 1990-2000 University of Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?- apropos(Word).
% d:/prolog programs/pp.pl compiled 0.00 sec, 1,652 bytes
?- loves(james,X).
X = james ;
No
?- loves(philip,mother(charles)).
No
?3.2
loves(X,diana)

loves(X,diana)
X = diana
cut
fail

loves(lover(diana),Y)

loves(lover(diana),Y)
Y = james

cut

fail

loves(lover(diana),diana)
loves(lover(diana),diana)
X = diana

success

Observe that in the presence of variables in a goal clause, if such a clause unifies with a program clause
containing the cut (!) followed by fail then this may change the semantics of the goal clause.

Introduction to Prolog

Page 14 of 15

In particular the goal clause loves(X,diana) is normally interpreted in Prolog as find all those instances of X
who love diana. However loves(X,diana) unifies with the program clause
loves(diana,diana) :- !,fail
with X taking the value diana. The subgoal fails with no backtracking. Thus the goal clause loves(X,diana) is
interpreted by Prolog in this context as does everyone love diana which of course fails.
A similar argument applies to the goal clause loves(lover(diana),Y).
However loves(lover(diana),diana) only unifies with the program clause loves(lover(X),X) thus avoiding any
such problems.
The cut (!) and fail are often termed non logical terms and may have a dramatic affect on the simple semantics
of Prolog. They should be used with great care.
This problem is not solved by simply reordering the clauses.
3.3
loves(philip,ann)

loves(charles,ann),!,fail

loves(philip,ann),!,fail,!,fail

etc.

The clauses imply that neither loves(philip,ann) nor its negation are provable from the axioms .
Note: The behaviour of the program is not a feature of Prologs inability to adequately reflect the
denotational semantics of predicate logic as is sometimes the case but an example of a proposition that is not
provable from the axioms.
Some Comments on this Prolog program and testing.
First you should identify the people in the universe of discourse. These include the named people diana,
charles, camilla, james, philip, ann and highhat. However in view of the constructors (functors) mother and
lover, for each person X, lover(X) is a member of the universe of discourse, in particular lover(diana) and
lover(lover(james)) and for each person X, mother(X) is a member of the universe of discourse, in particular,
mother(james) and mother(mother(charles)) etc.

Introduction to Prolog

Page 15 of 15

A consequence of including the constructors (called functors in Prolog) lover and mother is the inclusion of
terms such as mother(lover(mother(philip))) as you can see there are a lot of people in this world. The
Collection of all such people is called the Herbrand Universe7.
You should run the Prolog program to demonstrate the facts that now hold (anything provable must hold in any
model of the axioms (rules and facts) Prolog is sound) you should express the facts as relations between
ground terms.
You obviously cannot write down all such pairings loves(a,b) for ground terms a and b so a general description
should suffice supported by demonstrating its truth using Prolog.
For example: diana loves everyone other than herself can be demonstrated as
Welcome to SWI-Prolog (Version 3.3.7) etc
?- loves(diana,james).
Yes
?- loves(diana,charles).
Yes
?- loves(diana,philip).
Yes
?- loves(diana,camilla).
Yes
?- loves(diana,mother(diana)).
Yes
?- loves(diana,mother(mother(charles))).
Yes
?- loves(diana,diana).
No
?- loves(diana,lover(lover(mother(ann)))).
Yes
james does not love anyone but himself can be demonstrated as
?- loves(james,james).
Yes
?- loves(james,mother(james)).
No
?- loves(james,lover(mother(ann))).
No
?similarly for goal clauses such as loves(mother(ann),ann), loves(lover(philip),mother(lover(philip)),
loves(charles,highhat) etc.

lover(highhat) also belongs to the Herbrand Universe Annes rival!

You might also like