When you do not understand how a certain query is answered by prolog or you when you are debugging a program, you might want to ask prolog to print an execution trace. Below we illustrate this using the program for computing fibonacci numbers from session 2.
First load fibonacci.pl. This contains the program for computing fibonacci numbers (to do so, use 'consult' as explained in session 1).
To trace all calls to the fib/2 predicate, type trace(fib).
?- trace(fib). % fib/2: [call, redo, exit, fail] Yes [debug] ?- fib(5,F). T Call: (7) fib(5, _G416) T Call: (8) fib(4, _L188) T Call: (9) fib(3, _L209) T Call: (10) fib(2, _L230) T Exit: (10) fib(2, 1) T Call: (10) fib(1, _L231) T Exit: (10) fib(1, 1) T Exit: (9) fib(3, 2) T Call: (9) fib(2, _L210) T Exit: (9) fib(2, 1) T Exit: (8) fib(4, 3) T Call: (8) fib(3, _L189) T Call: (9) fib(2, _L302) T Exit: (9) fib(2, 1) T Call: (9) fib(1, _L303) T Exit: (9) fib(1, 1) T Exit: (8) fib(3, 2) T Exit: (7) fib(5, 5) F = 5 YesTo stop the tracing, type nodebug:
[debug] ?- nodebug. YesYou will notice that you do not get the trace anymore:
?- fib(5,F). F = 5 Yes
?- guitracer. % The graphical front-end will be used for subsequent tracing Yes ?- trace. Yes [trace] ?- fib(5,F). T Call: (6) fib(5, _G278) T Call: (7) fib(4, _G361) T Call: (8) fib(3, _G367) T Call: (9) fib(2, _G373) T Exit: (9) fib(2, 1) T Call: (9) fib(1, _G373) T Exit: (9) fib(1, 1) T Exit: (8) fib(3, 2) T Call: (8) fib(2, _G376) T Exit: (8) fib(2, 1) T Exit: (7) fib(4, 3) T Call: (7) fib(3, _G379) T Call: (8) fib(2, _G385) T Exit: (8) fib(2, 1) T Call: (8) fib(1, _G385) T Exit: (8) fib(1, 1) T Exit: (7) fib(3, 2) T Exit: (6) fib(5, 5) F = 5 ; T Redo: (8) fib(1, _G385) T Fail: (8) fib(1, _G385) T Redo: (8) fib(2, _G385) T Fail: (8) fib(2, _G385) T Fail: (7) fib(3, _G379) T Redo: (8) fib(2, _G376) T Fail: (8) fib(2, _G376) T Redo: (9) fib(1, _G373) T Fail: (9) fib(1, _G373) T Redo: (9) fib(2, _G373) T Fail: (9) fib(2, _G373) T Fail: (8) fib(3, _G367) T Fail: (7) fib(4, _G361) T Fail: (6) fib(5, _G278) NoTo stop the tracing, again type nodebug:
[debug] ?- nodebug. YesNote that the next time you start tracing again, the graphical tracer will again be used (because you have never said not to use the graphical tracer anymore).
?- noguitracer. % Subsequent tracing uses the commandline tracer Yes