Tool for drawing SLD trees in Prolog

Just as an example, I have the following SWI-Prolog Program (inverts a list):

inverte(L,LI) :- 
   inverte(L,[],LI).

inverte([], Aux, Aux).
inverte([P | R], Aux, LI) :- 
   inverte(R, [P | Aux], LI).

Are there any tools for Ubuntu 20.04 in order to view the SLD tree?
Like the following one, that solves the query:
inverte([a,b,c], X).

1 Like

As I noted in the SO comment for the question, I will help guide you to a possible solution but you will be writing the code so expect do some leg work.

Since the goal is to translate the execution of the Prolog code into a graph with the edges being calls to goals and the nodes being goals (statements) you will need a way to capture the steps as they occur. One common way is to use a meta interpreter but trying to create one that works for all of the SWI-Prolog code is not easy. What I find easier is to use prolog_trace_interception/4.

See: Example of prolog_trace_interception/4.

Get the example working and ask questions if necessary.


Some questions I have about your diagram.

Did you make this or is it from somewhere? I ask because if it is from somewhere then there may be code that can be reused.

Can you explain the meaning of the edge labels. I have an idea but it is more of a guess than knowing.


What means do you plan to use to represent the graph?

Some I know are GraphViz, Cytoscape.js and Visio.

It does not need to be know at this moment but it does have a big impact on about half of the code that needs to be written.

The image that I share in the post has been obtained using the sldDraw software. You can find it at this link:
http://www.lcc.uma.es/~pacog/sldDraw/
Although it draws a sld tree in Windows platform, it is not working for Ubuntu 20.04 (or I can not figure out how it is done). There is also a Java version, that works in Ubuntu but with some limitations.
The nodes are the selected predicates with unification.
Another limitation of this specific app has to do with a query that returns ERROR: Out of local stack. In fact, the obtained graph does not explicitly shows that an “infinite” recursion is happening, although we can figure out. I have not tried it extensively, but you may find a comparison with sldnfDraw in Chapter 4 of this article: https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.156.8281&rep=rep1&type=pdf

You can use this online tool to draw SLDNF trees
https://cplint.eu/example/member.swinb

Best
Fabrizio

1 Like

I inserted your problem in the online tool, here is the result
https://cplint.eu/p/inverte.swinb

Thank you very much for the tool you have just shared.
I have tried the online tool with the following program:

ad(marge, bart).
ad(srB, marge).
ant(X, Y) :- ad(X, Y).
ant(X, Z) :- ant(X, Y), ad(Y, Z). 

The query:

ant(eva, bart).

It does not show anything. Although it is an infinite recursion it would be nice to have some input from the tool. Or, am I doing something wrong with the tool?

| lbarqueira
December 9 |

  • | - |

Thank you very much for the tool you have just shared.
I have tried the online tool with the following program:

ad(marge, bart).
ad(srB, marge).
ant(X, Y) :- ad(X, Y).
ant(X, Z) :- ant(X, Y), ad(Y, Z). 

The query:

ant(eva, bart).

It does not show anything. Although it is an infinite recursion it would be nice to have some input from the tool. Or, am I doing something wrong with the tool?

the problem is that the tree is too big. If you run the second query, you will get the latex code that you can insert into a document and compile

@friguzzi Thanks. I have tried the following program with the tool:

junta([],L,L). 
junta([P | R], L2, [P | J_R_L2]) :- junta(R, L2, J_R_L2).
inverte([],[]).
inverte([P | R], LI) :- inverte(R,RI), junta(RI,[P],LI).

The above program inverts a list.
The query is inverte(X,[a,b,c]).
From the output of the tool, it does not support dependencies. I think this means that inverte/2
depends on junta/2. Is it correct? This is a limitation of the sldnfdraw library?