Execution tree meta interpreting

末鹿安然 提交于 2019-12-12 16:15:27

问题


I have tracing meta-interpreter made from my previous questions here and I would like to make similar meta-interpreter but this time for making execution trees. I've made something like this below using similar to same code found on the web and techniques from my previous questions.

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

How can I alter this interpreter that it displays branches that fails and that tree is only one with all solutions? Consider that trees are done only for similar programs like example program written in code if that matters.

I am using swi-prolog.

Edit : I am trying to achieve something like this but in textual form.

来源:https://stackoverflow.com/questions/27390236/execution-tree-meta-interpreting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!