KB-Bab-01
KB-Bab-01
of Prolog
1.1 An exampleprogram:definingfamilyrelations
Prolog is a programming language for symbolic, non-numeric computation. It
is specially well suited for solving problems that involve objects and relations
between objects.Figure 1.1 showsan example: a family relation. The fact that
Tom is a parent of Bob can be written in Prolog as:
Here we choose parent as the name of a relation; tom and bob are its argu-
yes
Prolog answers
no
?- parent(X, liz).
answerls:
X: tom
?- parent(bob, X).
X:ann
;g - pat
in Prologby:
This is expressed
?- parent{ X, Y).
X-pam
Y : bob;
X:tom
Y : bob;
X:tom
Y : liz;
X:bob
Y-pat
Prolog'sanswersare:
X:bob
Y : ann;
X:bob
Y-pat
The answeris:
X:bob
Exercises
1.1 Assumingthe parentrelationasdefinedin thissection(seeFigure1.1),
what will be Prolog'sanswersto the folrowingquestions?
(a) parent(jim, X).
(b) parent( X, jim).
(') parent( paffi, X), parent( X, pat).
(d) parent(paffi, X), parent( X, Y), parent( Y, jim).
female( pam).
male( tom).
male( bob).
female( liz).
female( pat).
female( ann).
male( jim).
The relations introduced here are male and female. These relations are unary
(or one-place) relations. A binary relation like parent defines a relation
between pairs of objects; on the other hand, unary relations can be used to
declare simple yes/no properties of objects. The first unary clauseabove can be
read: Pam is a female. We could convey the same information declared in the
two unary relations with one binary relation, sex, instead. An alternative piece
AN OVERVIEW OF PROLOG
offspring(liz, tom).
However, the offspring relation can be defined much more elegantly by making
use of the fact that it is the inverse of parent, and that parent has already been
defined. This alternative way can be based on the following logical statement:
Prolog clausessuch as
are called rules. There is an important difference between facts and rules. A
fact like
head body
If the condition parent(X, Y) is true then a logical consequence of this is
offspring( Y, X).
How rules are actually used by Prolog is illustratedby the following
example.Let us ask our programwhetherLiz is an offspringbt rom:
?- offspring(liz, tom).
offspring(liz, tom)
This (new)goalhappensto be trivial asit canbe found asa factin our program.
This meansthat the conclusionpart of the rule is also true, and Prolog will
answerthe questionwith yes.
Let us now add more family relationsto our exampleprogram. The
AN OVERVIEW OF PROLOG 11
9.:'"' o.*"8'
parent I mother
I
€r -.-rB grandparent
grandparent(X, Z) :-
parent( X, Y),
parent(Y, Z).
female
sister( X, Y)
parent( Z,
parent( Z,
female( X).
sister(ann, pat).
?- sister(X, pat).
X : ann;
X-pat U:/BR
So, Pat is a sisterto herself?!This is probablynot what we had in mind when
defining the sister relation. However, accordingto our rule about sisters
Prolog's answeris perfectly logical. Our rule about sistersdoesnot mention
that X and Y must not be the sameif X is to be a sisterof Y. As this is not
requiredProlog(rightfully)assumes that X andY canbe the same,andwill asa
consequence find that any femalewho hasa parentis a sisterof herself.
To correctour rule about sisterswe haveto add that X and Y must be
different.We will seein laterchaptershow thiscanbe donein severalways,but
for the moment we will assumethat a relation different is alreadyknown to
Prolog, and that
different( X, Y)
is satisfiedif and only if X and Y are not equal. An improvedrule for the sister
relationcan then be:
sister(X, Y) :-
parent( Z, X),
parent( Z, Y),
female( X),
different( X, Y).
Exercises
1.3 Translatethe followingstatementsinto prolog rules:
(a) Everybody who has a child is happy (introduce a one-argument
relationhappy).
(b) For all X, if X has a child who hasa sisterthen X hastwo children
(introduce new relation hastwochildren).