tracing
Tracing the execution of a Prolog query allows you to see all of the goals that are executed as part of the query, in sequence, along with whether or not they succeed. Tracing also allows you to see what steps occur as Prolog backtracks.

To turn on tracing in Prolog, execute the "goal"

```?- trace.

true.
```

When you are finished with tracing, turn it off using the "goal"

```?- notrace.
```

Here is an example of `trace` in action. First some code, which computes factorial N - the product of the numbers from 1 to N. By convention, factorial 0 is 1. Factorial N is often written N!, where the exclamation mark signifies the "factorial operator". Here is the code:

 ``` factorial(0, 1). factorial(N, NFact) :-     N > 0,     Nminus1 is N - 1,     factorial(Nminus1, Nminus1Fact),     NFact is Nminus1Fact * N.``` ``` rule 1 rule 2 rule 2, goal 1 rule 2, goal 2 rule 2, goal 3 rule 2, goal 4``` ``` factorial(0) = 1   if N > 0, then compute N - 1, recursively work out factorial(N-1), then multiply that by N ```

The tables above and below may not display correctly if viewed in a narrow window. If the commentary doesn't seem to line up correctly with the text to the left of it, try making your browser window wider.

Prolog dialog/trace outputCommentary
```prolog -s factorial.pl
blah blah blah …
?- trace.
true.
[trace]  ?- factorial(3, X).
Call: (7) factorial(3, _G284) ? creep*
^  Call: (8) 3>0 ? creep
^  Exit: (8) 3>0 ? creep
^  Call: (8) _L205 is 3-1 ? creep
^  Exit: (8) 2 is 3-1 ? creep
Call: (8) factorial(2, _L206) ? creep
^  Call: (9) 2>0 ? creep
^  Exit: (9) 2>0 ? creep
^  Call: (9) _L224 is 2-1 ? creep
^  Exit: (9) 1 is 2-1 ? creep
Call: (9) factorial(1, _L225) ? creep
^  Call: (10) 1>0 ? creep
^  Exit: (10) 1>0 ? creep
^  Call: (10) _L243 is 1-1 ? creep
^  Exit: (10) 0 is 1-1 ? creep
Call: (10) factorial(0, _L244) ? creep
Exit: (10) factorial(0, 1) ? creep
^  Call: (10) _L225 is 1*1 ? creep
^  Exit: (10) 1 is 1*1 ? creep
Exit: (9) factorial(1, 1) ? creep
^  Call: (9) _L206 is 1*2 ? creep
^  Exit: (9) 2 is 1*2 ? creep
Exit: (8) factorial(2, 2) ? creep
^  Call: (8) _G284 is 2*3 ? creep
^  Exit: (8) 6 is 2*3 ? creep
Exit: (7) factorial(3, 6) ? creep

X = 6 ;
Redo: (10) factorial(0, _L244) ? creep
^  Call: (11) 0>0 ? creep
^  Fail: (11) 0>0 ? creep
Fail: (9) factorial(1, _L225) ? creep
Fail: (8) factorial(2, _L206) ? creep
Fail: (7) factorial(3, _G284) ? creep

false.
[debug]  ?- notrace. % turn off tracing

true.
[debug]  ?-
```
``````Invoke prolog, loading code for factorial
Greeting from Prolog
Turn on tracing

Call factorial
Trace echoes query, replacing X with a unique variable
Rule 2, Goal 1 (N > 0) is invoked
Goal 1 succeeds immediately
Rule 2, Goal 2 invoked to compute 3 - 1
and succeeds
Rule 2, Goal 3 is invoked: level 2 call to factorial(2, …)
Goal 1 again for new call to factorial
Goal 1 succeeds
New version of goal 2
succeeds
Rule 2, Goal 3 is invoked: level 3 call to factorial(1, …)
Goal 1 again
succeeds
Goal 2 again
successfully
Rule 2, Goal 3 is invoked: recursive call to factorial(0, …)
This time, Rule 1 succeeds at once.
This is Goal 4 of level 3
Compute 1 * 1 without trouble
so the level 3 factorial call succeeds
This is goal 4 of level 2
Compute 1 * 2 without trouble
so the level 2 factorial call succeeds
This is goal 4 of level 1
Compute 2 * 3 without trouble
so the level 1 factorial call succeeds

… with this binding of X - type ";" to find more solutions
Prolog backtracks looking for another solution

without success
Turn off tracing.

``````
``` ```
* What is this "creep" business? In SWI Prolog, the implementation of Prolog which this dictionary uses for the syntax of its examples, when you press return at the end of a line of tracing, Prolog prints "creep" on the same line, and then prints the next line of trace output on the next line. Pressing return again produces "creep" again and another line of tracing, and so on.

There are further tracing facilities in SWI Prolog. Do

```?- help(trace).
```
to start to find out about them.

Built-in Prolog functions are not traced - that is, the internals of calls to things like `member` are not further explained by tracing them.