Deterministic Tracing

We demonstrate deterministic tracing of the default debugger for a simple example. The execution of a predicate works in that it determines a substitution for the argument variables. This execution might or might not succeed and it then might or might not leave choice points. A deterministic execution is a successful execution without choice points.

Whether a predicate executes deterministically or not depends on pattern of arguments. Some pattern of arguments can execute deterministically other pattern of arguments might execute non-deterministically. We will use the following Prolog text, which defines a reverse predicate, as the working example to demonstrate deterministic tracing:

rev(X, Y) :- rev(X, [], Y).
rev([], X, X).
rev([X|Y], Z, T) :- rev(Y, [X|Z], T).

We can build trust that the predicate is correctly implemented by running a few test cases. This will be a black-box test, since we will only see the input argument and the output argument. The top-level will also immediately return to the prompt if an execution succeeded deterministically, so that we see that as well:

?- rev([1,2,3], X).
X = [3,2,1]
?-

The default debugger now allows performing a more white-box testing, in that it will halt on the call port and the exit port of each execution of a sub predicate. We could also archive this by placing write statements in the source code. The development environment does that instrumentation for us, it is a matter of the trace/0 debugger directive to start tracing:

?- trace.
Yes
?- rev([1,2,3], X).
    0 Call rev([1,2,3], X) ?
    1 Call rev([1,2,3], [], X) ?
    2 Call rev([2,3], [1], X) ?
    3 Call rev([3], [2,1], X) ?
    4 Call rev([], [3,2,1], X) ?
    4 Exit rev([], [3,2,1], [3,2,1]) ?
    3 Exit rev([3], [2,1], [3,2,1]) ?
    2 Exit rev([2,3], [1], [3,2,1]) ?
    1 Exit rev([1,2,3], [], [3,2,1]) ?
    0 Exit rev([1,2,3], [3,2,1]) ?
X = [3,2,1]
?-

In the above, we have just pressed the return button after each debugger prompt "?". Since the mode was trace mode, this is known as creeping among Prolog interpreters. When the prompt "?" appears the end-user can either use the mouse or the keyboard to issue debugger directives. For more information, see the next chapter.

Kommentare