# Tool for drawing SLD trees in Prolog

**URL:** <https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737>\
**Category:** Help!\
**Created:** [December 9, 2021, 3:11pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737 "2021-12-09T15:11:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lbarqueira](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/lbarqueira/32/3875_2.png) [@lbarqueira](https://swi-prolog.discourse.group/u/lbarqueira)\
**Post date:** [December 9, 2021, 3:11pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/1 "2021-12-09T15:11:33Z")

</div>

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

```prolog
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).`

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/d/d60d778d7c4a14ec2661b8e5439e2fc519b52ca5.png)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [December 9, 2021, 3:24pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/2 "2021-12-09T15:24:07Z")

</div>

As I noted in the SO comment for the [question](https://stackoverflow.com/q/70249621/1243762), 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](https://www.metalevel.at/acomip/) 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](https://swi-prolog.discourse.group/t/example-of-prolog-trace-interception-4/4458).

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.

---

<div class="post-metadata">

**Author:** ![lbarqueira](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/lbarqueira/32/3875_2.png) [@lbarqueira](https://swi-prolog.discourse.group/u/lbarqueira)\
**Post date:** [December 9, 2021, 3:43pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/3 "2021-12-09T15:43:52Z")

</div>

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/](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](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.156.8281&rep=rep1&type=pdf)

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [December 9, 2021, 3:51pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/4 "2021-12-09T15:51:01Z")

</div>

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

Best  
Fabrizio

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [December 9, 2021, 3:56pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/5 "2021-12-09T15:56:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lbarqueira](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/lbarqueira/32/3875_2.png) [@lbarqueira](https://swi-prolog.discourse.group/u/lbarqueira)\
**Post date:** [December 9, 2021, 4:23pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/6 "2021-12-09T16:23:14Z")

</div>

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

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

```

The query:

```prolog
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?

---

<div class="post-metadata">

**Author:** ![friguzzi](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/friguzzi/32/7045_2.png) [@friguzzi](https://swi-prolog.discourse.group/u/friguzzi)\
**Post date:** [December 9, 2021, 4:42pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/7 "2021-12-09T16:42:23Z")

</div>

> > | [lbarqueira](https://swi-prolog.discourse.group/u/lbarqueira)  
> > December 9 |
> > 
> > - | - |
> 
> Thank you very much for the tool you have just shared.  
> I have tried the online tool with the following program:
> 
> ```prolog
> ad(marge, bart).
> ad(srB, marge).
> ant(X, Y) :- ad(X, Y).
> ant(X, Z) :- ant(X, Y), ad(Y, Z). 
> 
> ```
> 
> The query:
> 
> ```prolog
> 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

---

<div class="post-metadata">

**Author:** ![lbarqueira](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/lbarqueira/32/3875_2.png) [@lbarqueira](https://swi-prolog.discourse.group/u/lbarqueira)\
**Post date:** [December 10, 2021, 6:58pm UTC](https://swi-prolog.discourse.group/t/tool-for-drawing-sld-trees-in-prolog/4737/8 "2021-12-10T18:58:41Z")

</div>

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

```prolog
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?
