
190 Logic Programming With Prolog
?-ancestor(louise,Desc).
[A1] ancestor(louise,Y):-parent(louise,Y).
X is bound to louise. Variables Desc and Y are bound to each other.
The goal parent(louise,Y) is now matched with clause [P3], which is first
rewritten to replace X and Y by X1 and Y1, i.e.
parent(X1,Y1):-mother(X1,Y1).
?-ancestor(louise,Desc).
[A1] ancestor(louise,Y):-parent(louise,Y).
[P3] parent(louise,Y1):-mother(louise,Y1).
X is bound to louise. Variables Desc, Y and Y1 are bound to each other. X1 is
bound to louise.
The system now tries to satisfy the goal mother(louise,Y1). It matches it with
clause [M9].
?-ancestor(louise,Desc).
[A1] ancestor(louise,Y):-parent(louise,Y).
[P3] parent(louise,Y1):-mother(louise,Y1).
[M9] mother(louise,caroline).
X is bound to louise. Variables Desc, Y and Y1 are bound to each other and to
caroline. X1 is bound to louise.
This gives a solution to the user's goal, with Desc bound to caroline.
?- ancestor(louise,Desc).
Desc = caroline
If the user now forces the system to backtrack, the system will try to resatisfy the
goal mother(louise,Y1) and fail. This will cause the rule [P3] to be rejected.
Attempts to resatisfy parent(louise,Y) in the body of [A1] will also fail, so clause
[A1] will be rejected. This brings the system back to the original goal
ancestor(louise,Desc).
The system tries to resatisfy it by matching it with the second clause for
ancestor/2, i.e. [A2].