Prolog search tree

825 views
Skip to first unread message

Karel Prchal

unread,
Feb 24, 2015, 7:23:04 AM2/24/15
to swi-p...@googlegroups.com
Good day,

Has anybody ever done visualisation of prolog search tree? I'm looking for the way to draw search trees (something like http://www.learnprolognow.org/html/chap2-pspic5.ps.png) automatically using prolog or another programming language (my main language is C#). I found algorithm how to achieve that in this book (chapter 4.2.5) http://goo.gl/A9V0OC but I'd love to be more general and visualise for example programs with prolog lists. I thought I could have done some program in prolog since I don't want to write my own implementation of prolog. I wrote the code below using various sources on the internet. The idea is to meta interpret the given prolog code and with clause why/1 to draw the search tree. I have two problems with this - it does not draw whole tree at once and second - it does not draw branches that are false. Do you think I am on good way to solve this or is there another, maybe better way to draw search trees?

Thank you in advance!

clause_tree(true,_,true) :- !, true. 
clause_tree((G,R),Trail,(TG,TR)) :-
   !, 
   clause_tree(G,Trail,TG),
   clause_tree(R,Trail,TR). 
clause_tree(G,_,prolog(G)) :- 
   (predicate_property(G,built_in) ;  
     predicate_property(G,compiled) ), 
    call(G).
clause_tree(G,Trail,tree(G,T)) :- 
   clause(G,Body),
   clause_tree(Body,[G|Trail],T).

why(G) :-
    call_with_depth_limit( 
        catch(
            clause_tree(G,[],T),
            cut,
            fail),
        30,
        _Message),
    nl,
    draw_tree(T,0).

draw_tree(tree(Root,Branches),Tab) :- !,
   tab(Tab),
   write(Tab),
   write(': '),
   write(Root),
   nl,
   Tab1 is Tab + 1,
   draw_tree(Branches,Tab1).
draw_tree((B,Bs),Tab) :- !,
   draw_tree(B,Tab),
   draw_tree(Bs,Tab).
draw_tree(Node,Tab) :-
   tab(Tab),
   write(Tab),
   write(': '),
   write(Node),
   nl.

%example program for testing
%?-p(X).
p(X) :- a(X). 
p(X) :- b(X),c(X), d(X),e(X). 
p(X) :- f(X).

b(Y) :- g(Y), h(Y).
b(1). 
b(2). 

a(1).

c(1). 
c(2).

d(1). 
d(2). 

e(2). 

f(3). 

g(2).
g(1).

h(2).

Marco Gavanelli

unread,
Feb 24, 2015, 10:54:40 AM2/24/15
to swi-p...@googlegroups.com
Dear Karel,

time ago I implemented a Prolog meta-interpreter that visualizes the SLDNF tree in LaTeX format.
Then, it is very easy to convert LaTeX into EPS, PDF, PNG, etc.

Please, have a look at this page:

http://www.ing.unife.it/it/ricerca-1/aree-di-ricerca/informazione/ingegneria-informatica/software/sldnf-draw/

Best,
Marco
--
http://docente.unife.it/marco.gavanelli/

Jonathon Doran

unread,
Mar 6, 2015, 9:44:53 PM3/6/15
to swi-p...@googlegroups.com
I have generated DOT code from within Prolog.  This is a graph language that goes with Graphviz.  In my case I used it to draw trees I was generating.

Wouter Beek

unread,
Mar 7, 2015, 2:52:46 AM3/7/15
to Jonathon Doran, swi-p...@googlegroups.com
Hi Karel, others,

Paulo Moura's Logtalk can draw rather complex diagrams for Prolog-based structures; for example: http://logtalk.org/diagrams/csv_entity_diagram.pdf Prolog search trees may already be part of that, and otherwise it's likely that you can reuse quite a bit of his code.

In line with Jonathon's suggestion, there is a generic library for generating GraphViz (including DOT) files from within SWI-Prolog. It's over at https://github.com/wouterbeek/plGraphViz​ Over time this library has come to supports even some of the more obscure GraphViz features, e.g. HTML-like labels.

---
​Cheers!,
Wouter.

E-mail: w.g.j...@vu.nl
WWW: www.wouterbeek.com
Tel.: 0647674624

On Sat, Mar 7, 2015 at 3:44 AM, Jonathon Doran <jhd...@fsmail.bradley.edu> wrote:
I have generated DOT code from within Prolog.  This is a graph language that goes with Graphviz.  In my case I used it to draw trees I was generating.

--
You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
Visit this group at http://groups.google.com/group/swi-prolog.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages