Information Technology Reference
In-Depth Information
Sometimes, the best way to find out what is wrong with a predicate is to trace the
execution of queries that use it.
C.2.1 Tracing the execution
In the simplest case, simply add trace, leash(-all) to the front of a query:
?- trace, leash(-all), mother(hanna,henry).
Call: (8) mother(hanna, henry)
Call: (9) child(henry, hanna)
Exit: (9) child(henry, hanna)
Call: (9) female(henry)
Fail: (9) female(henry)
Fail: (8) mother(hanna, henry)
No
With this trace, you can see the problem: you are checking that henry is female instead
of hanna . It is now easy to fix the program (change the argument of female from Y to
X in the mother clause), and then reload the saved file using make .
As programs get larger, you often want more control on how the tracing should
appear. If you put trace, leash(+all) at the front of the query (that is, use +all
instead of -all ), you get the trace presented line by line, after which you enter a
single-character command. There are a number of options (typing a ? will show
them), but here are the main ones:
space or c (for creep) tells Prolog to go to the next line as usual;
a (for abort) tells Prolog to give up on the entire query;
s (for skip) tells Prolog to jump ahead without stopping or printing anything
until the current atomic query either succeeds or fails.
With s you can skip over parts of the query that you know are working properly.
Finally, it is possible to restrict a trace to a given list of predicates. For example, by
inserting trace([mother,female]) at the front of a query, only the parts of the trace
involving those two predicates will be printed. (The leash predicate is not used in
this case.)
C.2.2
Interrupting the execution
Another common problem is that instead of getting the wrong answer to a query, you
get no answer at all! This can happen when the program goes into a loop (because of
a recursive predicate) or just takes too long (because of a very large search space).
 
 
Search WWH ::




Custom Search