0
votes

I was having some problem trying to trace the prolog. The trace log as follow:

parent_of(X,Y).
Call: (6) parent_of(_G2780, _G2781) ? creep
Exit: (6) parent_of(warren, jerry) ? creep
X = warren
Y = jerry ;
Redo: (6) parent_of(_G2780, _G2781) ? creep
Exit: (6) parent_of(maryalice, jerry) ? creep
X = maryalice
Y = jerry ;
Redo: (6) parent_of(_G2780, _G2781) ? creep
Call: (7) brother(_G2865, _G2781) ? creep
Exit: (7) brother(jerry, kather) ? creep
Call: (7) father(_G2780, jerry) ? creep
Call: (8) parent_of(_G2780, jerry) ? creep
Exit: (8) parent_of(warren, jerry) ? creep
Call: (8) male(warren) ? creep
Exit: (8) male(warren) ? creep
Exit: (7) father(warren, jerry) ? creep
Exit: (6) parent_of(warren, kather) ? creep

However, I declared my facts and rules as follow:

male(jerry).
male(stuart).
male(warren).
male(peter).
female(kather).
female(maryalice).
female(ann)
brother(jerry,stuart).
brother(jerry,kather).
brother(peter, warren).
sister(ann, maryalice).
sister(kather,jerry).
parent_of(warren,jerry).
parent_of(maryalice,jerry).

parent_of(X,Z):- brother(Y,Z),(father(X,Y);mother(X,Y)).
father(X,Y) :-  parent_of(X,Y), male(X).
mother(X,Y) :-  parent_of(X,Y), female(X).

I thought the first Call by right should execute brother and (father and mother)? How come it straight away gave me the Exit which is Warren is the parent of Jerry?

Thanks in advance!

1
You have a syntax error. You need a period after female(ann). - lurker

1 Answers

1
votes

Prolog finds facts and rules in the order they are asserted. Since you have some facts parent_of/2 asserted before your rule parent_of/2, those are found first. You didn't show your full trace, but after it finds the facts, it traces into the rule:

[trace]  ?- parent_of(X,Y).
   Call: (7) parent_of(_G1353, _G1354) ? creep
   Exit: (7) parent_of(warren, jerry) ? creep
X = warren,
Y = jerry ;
   Redo: (7) parent_of(_G1353, _G1354) ? creep
   Exit: (7) parent_of(maryalice, jerry) ? creep
X = maryalice,
Y = jerry ;
   Redo: (7) parent_of(_G1353, _G1354) ? creep
   Call: (8) brother(_G1444, _G1354) ? creep
   Exit: (8) brother(jerry, stuart) ? creep
   Call: (8) father(_G1353, jerry) ? creep
   Call: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(warren, jerry) ? creep
   Call: (9) male(warren) ? creep
   Exit: (9) male(warren) ? creep
   Exit: (8) father(warren, jerry) ? creep
   Exit: (7) parent_of(warren, stuart) ? creep
X = warren,
Y = stuart ;
   Redo: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(maryalice, jerry) ? creep
   Call: (9) male(maryalice) ? creep
   Fail: (9) male(maryalice) ? creep
   Redo: (9) parent_of(_G1353, jerry) ? creep
   Call: (10) brother(_G1444, jerry) ? creep
   Fail: (10) brother(_G1444, jerry) ? creep
   Fail: (9) parent_of(_G1353, jerry) ? creep
   Fail: (8) father(_G1353, jerry) ? creep
   Redo: (7) parent_of(_G1353, stuart) ? creep
   Call: (8) mother(_G1353, jerry) ? creep
   Call: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(warren, jerry) ? creep
   Call: (9) female(warren) ? creep
   Fail: (9) female(warren) ? creep
   Redo: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(maryalice, jerry) ? creep
   Call: (9) female(maryalice) ? creep
   Exit: (9) female(maryalice) ? creep
   Exit: (8) mother(maryalice, jerry) ? creep
   Exit: (7) parent_of(maryalice, stuart) ? creep
X = maryalice,
Y = stuart
...

I would recommend, however, that you make your rule names different than fact names. So I would do something like:

parent(warren,jerry).
parent(maryalice,jerry).

Then:

parent_of(X,Z) :-
    parent(X, Z).
parent_of(X,Z) :-
    brother(Y,Z),
    ( father(X,Y); mother(X,Y) ).

But also be careful of redundant facts. It looks like your parent relationships might already be defined by facts involving father/2 and mother/2. So it's unclear why you need the parent/2 facts at all.

NOTE that your code has a syntax error here:

female(ann)
brother(jerry,stuart).

You probably ignored the error. But without the period, Prolog will ignore the above two facts, and then give you the results you are seeing.


parent_of/2father/2mother/2father/2mother/2parent_of/2