You can use the built-in predicate trace
to turn on
tracing for any predicate you like. It has this signature:
trace(
+Pred)
(+Pred,
+Ports)
trace
Put a trace point on the given predicate. Ports is a single tracing port or a list of ports; the available ports are (call, redo, exit, fail). If you omit the port list, all ports are traced.
As a first level of tracing, I recommend enabling just the 'call' port to see only calls to the predicate. For example, consider this program:
% length of a list len([], 0). len([_ | L], N) :- len(L, N1), N is N1 + 1. % last element of a list last([X], X). last([_ | L], X) :- last(L, X).
You can debug like this:
?- trace(len, call). % len/2: [call] true. [debug] ?- len([a, b, c], N). T Call: (8) len([a, b, c], _7160) T Call: (9) len([b, c], _7466) T Call: (10) len([c], _7466) T Call: (11) len([], _7466) N = 3. [debug] ?- trace(last, call). % last/2: [call] true. [debug] ?- last([a, b, c], X). T Call: (8) last([a, b, c], _7172) T Call: (9) last([b, c], _7172) T Call: (10) last([c], _7172) X = c ; T Call: (11) last([], _7172) false.
If you want more detail, turn on all ports:
[debug] ?- trace(len). % len/2: [call,redo,exit,fail] true. [debug] ?- len([a, b, c], N). T Call: (8) len([a, b, c], _7160) T Call: (9) len([b, c], _7464) T Call: (10) len([c], _7464) T Call: (11) len([], _7464) T Exit: (11) len([], 0) T Exit: (10) len([c], 1) T Exit: (9) len([b, c], 2) T Exit: (8) len([a, b, c], 3) N = 3. [debug] ?- trace(last). % last/2: [call,redo,exit,fail] true. [debug] ?- last([a, b, c], X). T Call: (8) last([a, b, c], _7380) T Call: (9) last([b, c], _7380) T Call: (10) last([c], _7380) T Exit: (10) last([c], c) T Exit: (9) last([b, c], c) T Exit: (8) last([a, b, c], c) X = c ; T Redo: (10) last([c], _7380) T Call: (11) last([], _7380) T Fail: (11) last([], _7380) T Fail: (10) last([c], _7380) T Fail: (9) last([b, c], _7380) T Fail: (8) last([a, b, c], _7380) false.
When you are finished debugging, type nodebug
so that
Prolog will stop producing trace output:
[debug] ?- nodebug. true. ?-