Even the most experienced and able programmers need help in finding bugs and shortcomings in their programs. Debugging environments were pioneered by users of artificial intelligence languages and their use has spread to the procedural languages. SICStus Prolog uses a fairly standard Prolog debugging environment.
You will need the debugger to follow the working of your programs when they behave wrongly. You can also use debugging tools to understand how example programs work.
The simplest command is trace. This will allow you to see how every step in your program is executed. For instance, I traced append/3.
To switch off the tracer, use the command notrace, ie:
| ?- notrace. {The debugger is switched off} yes
You may suspect that a problem with your program is located in a particular procedure. In such cases, it is more convenient just to look at how that procedure works, rather than looking at every step of each procedure used in the program. In the following example, I have a program for which I use the query pp. I want to look only at the procedure append/3. This is done by the command spy/1 which has the general form:
spy <procedure_name>/<arity>
For instance:
| ?- spy append/3. Turn spy on {The debugger will first leap -- showing spypoints (debug)} {Spypoint placed on user:append/3} yes {debug} | ?- pp. This is my query Pure Prolog interpreter: version 1.0 ... This is the output of spy + 1 1 Call: append([1,2],[a,b],_325) ? + 2 2 Call: append([2],[a,b],_789) ? + 3 3 Call: append([],[a,b],_1186) ? + 3 3 Exit: append([],[a,b],[a,b]) ? + 2 2 Exit: append([2],[a,b],[2,a,b]) ? + 1 1 Exit: append([1,2],[a,b],[1,2,a,b]) ? ... yes {debug} | ?- nospy append/3. This turns spy off {Spypoint removed from user:append/3} yes
Sometimes you may lose track of what you tracing settings you have on. The predicate debugging/0 will give you a report of the current settings. This is the state of the Prolog interpreter I was using before turning the last spy point off.
| ?- debugging. The debugger will first leap -- showing spypoints (debug) Using leashing stopping at [call,exit,redo,fail,exception] ports Undefined predicates will raise an exception (error) Spypoints: user:append/3 yes {debug}
The debugger is described in more detail in the SICStus Prolog user's manual: release 3 #0. Kista: Swedish Institute of Computer Science, 1995. Chapter 6: pp 68-78.
The model used in many Prolog debuggers is described by Clocksin, W F & Mellish, C S. Programming in Prolog. 3rd ed. Berlin: Springer, 1987. pp 182-200. - Note: Univ of Bham Lib: QA 76.7.P76.
© P.J.Hancox@bham.ac.uk