Introduction To Prolog
Tracing Prolog Executions

SWI Prolog allows you to trace the route that Prolog takes through a program when looking for a solution to a particular query.

There are 2 ways to do this, the graphical and the non-graphical trace. Both yield up the same information but do so in different ways.

Example Program

Type in the following program and save it as office.pl.

/*office program */
adminWorker(black).
admnWorker(white).

officeJunior(green).

manager(brown).
manager(grey).
supervises(X,Y) :- manager(X), adminWorker(Y).
supervises(X,Y) :- adminWorker(X), officeJunior(Y).
supervises(X,Y) :- manager(X), officeJunior(Y).

Non-Graphical Debug

Launch Prolog and Click on File, Consult to load up the example program. At the prompt, type

trace.

and on the next line, type

supervises(Who, green).

Press enter to start your query and you will see the text below. You will need to press enter after each line to make the debugger move to the next instruction.

[trace] 1 ?- supervises(Who, green).User Input
Call: (7) supervises(_G501, green) ? creepFinds the first rule for supervises(X, Y) and instantiates Y to green to match the query. The word creep appears where you press enter. It means that Prolog has been told to move to the next instruction.
Call: (8) manager(_G501) ? creepProlog tries to satisfy the first subgoal of the rule. It tries manager(x).
Exit: (8) manager(brown) ? creepBrown is found as a manager. Prolog will test whether this leads to a solution. The word Exit on the left reflects the fact that Prolog has found a solution to its last call and will set X to brown.
Call: (8) adminWorker(green) ? creepIf Brown manages green, green must be an adminworker.
Fail: (8) adminWorker(green) ? creepSince green is not an admin worker, the second subgoal of the rule cannot be satisfied.
Redo: (8) manager(_G501) ? creepProlog goes back to the first subgoal and picks up where it left off with manager(x).
Exit: (8) manager(grey) ? creepIt finds grey and sets now instantiates X set to this value.
Call: (8) adminWorker(green) ? creepIt again tests whether green is an adminworker.
Fail: (8) adminWorker(green) ? creepAnd again fails. This means that the rule cannot provide a solution.
Redo: (7) supervises(_G501, green) ? creepProlog returns to point A to continue processing the top-level goal.
Call: (8) adminWorker(_G501) ? creepThis time it looks for an admin worker as the supervisor (the second rule).
Exit: (8) adminWorker(black) ? creepIt finds black and sets X to this value.
Call: (8) officeJunior(green) ? creepIt checks whether, the second subgoal is satisfied.
Exit: (8) officeJunior(green) ? creepGreen is an office junior and therefore this subgoal is satisfied.
Exit: (7) supervises(black, green) ? creepThe top level goal is satisfied.

You may have noticed that Prolog took no account of the fact that Brown also supervises Green. It followed the logic of the program one-step at a time until it found the solution. There are 3 types of supervisor in the scenario. Prolog exhausted the possibilities of the first type of supervisor before looking at the rule for the second type of supervisor. If you press the semicolon key when you get the result, you will find that White, Brown and Grey also supervise Green. Prolog finds them in that order when it follows this program's logic.

Graphical Debugger

To activate the graphical debugger, click on Debug, Graphical Debugger on the menu. Then type the trace command followed by the query. Press the spacebar to follow the pretty colours and the logic.

Which Tool Is Best?

There are advantages to each of the tools. Both allow the programmer to see exactly what happens when their code is executed. The non-graphical debugger has all of the same information but arrives a bit more concisely. You can view all of the information at once and scan through large amounts at a time. The graphical debugger shows you what is happening in your code. You can isolate problems in your rules and see exactly what Prolog is doing.