You are on page 1of 5

1.

Use Prolog to create a knowledge base for the family tree of Figure 1 and then ask queries about

the family tree. Assume the intended interpretation of all predicates of the form p(x,y) is that “x is

the p of y”.

a) Here are the facts representing the family tree:

wife(philip, mum).

wife(charles, diana).

wife(mark, anne).

wife(andrew, sarah).

wife(edward, sophie).

wife(peter, autumn).

wife(mike, zara).

son(charles, mum).

son(andrew, mum).

son(edward, mum).

son(william, diana).

son(harry, diana).

son(peter, anne).

son(zara, anne).

daughter(anne, mum).

daughter(elizabeth, mum).

daughter(margaret, mum).

daughter(beatrice, andrew).

daughter(eugenie, andrew).
b) Here are the rules added to infer information about the family tree:

husband(X, Y) :- wife(Y, X).

spouse(X, Y) :- husband(X, Y).

spouse(X, Y) :- wife(X, Y).

child(X, Y) :- son(X, Y).

child(X, Y) :- daughter(X, Y).

parent(X, Y) :- child(Y, X).

grandChild(X, Y) :- child(X, Z), child(Z, Y).

greatGrandParent(X, Y) :- grandChild(Y, Z), child(Z, X).

brother(X, Y) :- child(X, Z), child(Y, Z), male(X), X \= Y.

sister(X, Y) :- child(X, Z), child(Y, Z), female(X), X \= Y.

aunt(X, Y) :- sister(X, Z), parent(Z, Y).

aunt(X, Y) :- sisterInLaw(X, Z), spouse(Z, W), parent(W, Y).

uncle(X, Y) :- brother(X, Z), parent(Z, Y).

uncle(X, Y) :- brotherInLaw(X, Z), spouse(Z, W), parent(W, Y).

brotherInLaw(X, Y) :- brother(X, Z), spouse(Y, Z).

sisterInLaw(X, Y) :- sister(X, Z), spouse(Y, Z).

firstCousin(X, Y) :- child(X, Z), child(Y, W), sibling(Z, W).

male(X) :- son(X, _).

male(X) :- husband(X, _).


female(X) :- daughter(X, _).

female(X) :- wife(X, _).

sibling(X, Y) :- child(X, Z), child(Y, Z), X \= Y.

c) Here are the queries and their results:

?- husband(X, sarah).

X = andrew.

?- grandChild(X, elizabeth).

X = william ;

X = harry ;

X = peter ;

X = zara.

?- greatGrandParent(X, zara).

X = philip ;

X = mum ;

X = elizabeth.

?- sisterInLaw(X, diana).

X = anne ;

X = sarah.

?- uncle(X, beatrice).
X = charles ;

X = edward.

Question 2

a) Here's the recursive rule degree_lower/2 in Prolog:

degree_lower(hs,_).

degree_lower(bs,hs).

degree_lower(ms,bs).

degree_lower(phd,ms).

b) Here's the dateable/2 predicate that encodes the dating rules:

dateable(Female, Male) :-

person(Female, female, FemaleHeight, FemaleAge, FemaleDegree),

person(Male, male, MaleHeight, MaleAge, MaleDegree),

MaleHeight >= FemaleHeight,

degree_lower(FemaleDegree, MaleDegree),

MaleAge =< FemaleAge + 10,

MaleAge >= FemaleAge.


Here are the queries and their responses:

?- dateable(lily, richard).

false.

?- dateable(lily, bob).

true.

?- dateable(jenny, tom).

true.

You might also like