Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Tracing



Now let us see some more relationships that we can make from the previous relationships of a family. So if we want to make a grandparent relationship, that can be formed as follows −

Grandparent

We can also create some other relationships like wife, uncle, etc. We can write the relationships as given below −

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).
grandmother(X,Z) :- mother(X,Y), parent(Y,Z).
grandfather(X,Z) :- father(X,Y), parent(Y,Z).
wife(X,Y) :- parent(X,Z),parent(Y,Z), female(X),male(Y).
uncle(X,Z) :- brother(X,Y), parent(Y,Z).

So let us write a prolog program to see this in action. Here we will also see the trace to trace-out the execution.

Knowledge Base (family_ext.pl)

female(pam).
female(liz).
female(pat).
female(ann).
male(jim).
male(bob).
male(tom).
male(peter).
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
parent(bob,peter).
parent(peter,jim).
mother(X,Y):- parent(X,Y),female(X).
father(X,Y):- parent(X,Y),male(X).
haschild(X):- parent(X,_).
sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y.
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
grandmother(X,Z):-mother(X,Y),parent(Y,Z).
grandfather(X,Z):-father(X,Y),parent(Y,Z).
wife(X,Y):-parent(X,Z),parent(Y,Z),female(X),male(Y).
uncle(X,Z):-brother(X,Y),parent(Y,Z).

Output

| ?- consult('D:/TP Prolog/Sample Codes/family_ext.pl').
compiling D:/TP Prolog/Sample Codes/family_ext.pl for byte code...
D:/TP Prolog/Sample Codes/family_ext.pl compiled, 25 lines read - 4789 bytes written, 4 ms

yes
| ?- uncle(X,Y).

X = peter
Y = jim ? ;

(16 ms) no
| ?- 
grandparent(X,Y).

X = pam
Y = ann ? ;

X = pam
Y = pat ? ;

X = pam
Y = peter ? ;

X = tom
Y = ann ? ;

X = tom
Y = pat ? ;

X = tom
Y = peter ? ;

X = bob
Y = jim ? ;

X = bob
Y = jim ? ;

(78 ms) no
| ?- wife(X,Y).

X = pam
Y = tom ? ;

X = pat
Y = peter ? ;

no
| ?- 

Tracing the output

In Prolog we can trace the execution. To trace the output, you have to enter into the trace mode by typing trace.. Then from the output we can see that we are just tracing pam is mother of whom?. See the tracing output by taking X = pam, and Y as variable, there Y will be bob as answer. To come out from the tracing mode press notrace.

Program

| ?- consult('D:/TP Prolog/Sample Codes/family_ext.pl').
compiling D:/TP Prolog/Sample Codes/family_ext.pl for byte code...
D:/TP Prolog/Sample Codes/family_ext.pl compiled, 25 lines read - 4789 bytes written, 4 ms

yes
| ?- mother(X,Y).

X = pam
Y = bob ? ;

X = pat
Y = jim ? ;

(31 ms) no
| ?- trace.
The debugger will first creep -- showing everything (trace)

yes
{trace}
| ?- mother(pam,Y).
      1    1  Call: mother(pam,_23) ? 
      2    2  Call: parent(pam,_23) ? 
      2    2  Exit: parent(pam,bob) ? 
      3    2  Call: female(pam) ? 
      3    2  Exit: female(pam) ? 
      1    1  Exit: mother(pam,bob) ? 

Y = bob

(47 ms) yes
{trace}
| ?- notrace.
The debugger is switched off

yes
| ?- 
Advertisements