A quick tutorial on how to write debuggers for Prolog and Prolog like languages.

This is a little tutorial on how to write debuggers for Prolog and Prolog like languages, e.g. the DSL in http://www.reddit.com/r/prolog/comments/303txw/problem_solving_with_layered_design_domain/ I'll give the examples for Prolog as we all know the language.

I first read about the key to tracing Prolog like languages in Tracing Requirements for Multi-Layered Meta-Programming by Bowles and Wilk, in Meta-Programming in Logic Programming by Abramson and Rogers, MIT Press.

Consider the following program:

:- set_prolog_flag(unknown, fail).

a :- b, c ; d.
b :- true.
d :- true.

In Prolog the following query succeeds:

| ?- a.
% yes

Now consider our own little Prolog like language implemented as follows:

demo(true) :- !.
demo((G1, G2)) :- !, demo(G1), demo(G2).
demo((G1; G2)) :- !, (demo(G1); demo(G2)).
demo(G) :- clause(G, C), demo(C).

Just to test it works as we want it to we can give it the same query as before:

| ?- demo(a).
% yes

Now imagine if we wanted to build a debugger for our demo/1 language with tracing, leashing, and spypoints. The easiest way to build such a thing would be to start with simple tracer like this:

trace(true) :- !.
trace((G1, G2)) :- !, trace(G1), trace(G2).
trace((G1; G2)) :- !, (trace(G1); trace(G2)).
trace(G) :- pre(G), clause(G, C), trace(C), post(G).

pre(G) :- write(call), tab(1), write(G), nl.
pre(G) :- write(fail), tab(1), write(G), nl, fail.

post(G) :- write(exit), tab(1), write(G), nl.
post(G) :- write(redo), tab(1), write(G), nl, fail.

A quick test gives us:

| ?- trace(a).
call a
call b
exit b
call c
fail c
redo b
fail b
call d
exit d
exit a
% yes

If you can figure out how the pre/1 and post/1 procedures manage to print the correct messages then you'll see it's quite easy to add leash/1 and spy/1.

Posted Mar 27, 2015, 10:25 AM UTC · edited

100

No comments yet

Nobody commented on this post, or the comments weren't captured.