SWI Prolog Debugger
First of all, I will caution you that you should generally avoid using
the debugger when writing declarative code, since it really encourages
you to think non-declaratively (i.e., how the program is executed as a
search through the state space tree generated by the declarative
spec/code). However, it is sometimes unavoidable.
As with all languages/IDEs, it takes a little time to get used to using
the debugger; however, Prolog has a unique execution mechanism that you
will need to get familiar with.
I've summarized the features I find most useful on the Linux
version below (although everything I've mentioned below is
platform-independent, I can't vouch for how the Windows or Mac versions
work).
Debugging Methodology
When you first enter prolog and after you consult your input file
(using
"[filename]" or "consult(filename)"), type "debug.".
To trace a program, type "trace." at the prompt. The next
query
will be run in trace mode (try it). Once you are tracing a
query,
you can hit carriage return for normal 'single-step' operation, or you
can type 'h' (for help) to get a list of what you can do. The
choices I use the most are s (skip), l (leap), and a (abort).
The
most important concept to learn is Prolog's Call-Fail-Exit-Redo
mechanism, and you should get a good feel for how this works.
Try
tracing through something simple that includes backtracking (e.g.,
member(X,[a,b,c]) using our code from class), making sure to understand
all four components of Prolog's mechanism.
To set a spy-point (the Prolog analog of a breakpoint) at a
particular predicate p with arity 2, type "spy(p/2).", and then proceed
as normal. The program will run until p is
called, at which point you will be prompted. Type "nospyall."
to remove all spypoints.
NOTE: In some recent versions of swi-prolog, trace mode is not properly
handled (or at least, I haven't been able to figure it out). If your
version does not seem to be doing a trace, set a spy-point instead, and
you can trace after hitting the spy point. ...or you can just use
an older version of swi.
Graphical Mode
The SWI debugger includes both a graphical and textual mode.
I
personally find that the graphical mode is nicer for smaller programs
but the text mode is needed for debugging larger programs especially
when calling library predicates.
After
entering Prolog, type "gdebug." (instead of debug) and all future
traces/etc. will run in graphical mode. If you get an error
message upon typing gdebug, it means you need to install the XPCE
package (on Linux systems). Now, you can use commands like gtrace
to do effectively the same as with the textual debugger. You
should still understand the Call-Fail-Exit-Redo mechanism, though the
debugger uses colors to convey the same information.