Information Technology Reference
In-Depth Information
Figure 3.6.
A Prolog trace for a query
?- parent(X,john), \+ female(X).
Call: (8) parent(_G312,john)
Call: (9) child(john,_G312)
Exit: (9) child(john,sue)
Exit: (8) parent(sue,john)
Call: (8) female(sue)
Exit: (8) female(sue)
Redo: (9) child(john,_G312)
Exit: (9) child(john,sam)
Exit: (8) parent(sam,john)
Call: (8) female(sam)
Fail: (8) female(sam)
X = sam
Yes
5.
Reconsider the atom child(john,_G312) (from step 3). Find the next matching
clause, child(john,sam) with no body in the program. Tentatively, this part of
the query is finished. This leaves
\+ female(sam).
6.
Work on \+ female(sam) . Since this is a negated query, remove the \+ and
consider the unnegated version.
a. Work on the query female(sam) . There is no clause in the program whose
head matches this query. Return failure .
Since the unnegated version fails, the negated query succeeds .
7.
Since there is nothing left in the conjunctive query, return success, noting that
the value of X is sam .
One very nice feature of most Prolog systems is that one can ask Prolog itself to
do the tracing. This is very helpful when there is a Prolog program that is legal, but
where some queries do not yield hoped-for results.
Figure 3.6 shows an example of a Prolog trace for the same query. (How to turn
on and turn off this tracing behavior varies from system to system. See appendix C.)
This trace goes through roughly the same steps as the previous detailed listing. Four
types of lines printed:
A line that begins Call: is printed when Prolog starts to work on an atomic
query.
 
Search WWH ::




Custom Search