Metaphorizing metadata

I have a meta interpreter made from my previous questions here , and I would like to make a similar meta interpreter, but this time for generating execution trees. I did something like this below, using similar code I found on the internet and the methods 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 change this interpreter so that it displays branches that fail and this tree is just one with all solutions? Note that trees are only executed for similar programs, such as a sample program written in code, if that matters.

I am using swi-prolog.

Edit: I'm trying to achieve something like this , but in text form.

+3


source to share


1 answer


A tree is just a list of paths. So if you are using a regular vanilla interpreter:

solve(true) :- !.
solve((A,B)) :- !, solve(A), solve(B).
solve(H) :- clause(H,B), solve(B).

      

You can force it to return a path including incomplete paths to clause / 2 calls:



solve(true) --> !.
solve((A,B)) --> !, solve(A), solve(B).
solve(H) --> [H].
solve(H) --> [H], {clause(H,B)}, solve(B).

      

Then you can invoke solution // 1 with findall / 3 and restore the tree from it.

Some side effect of Gensym / 1 may be required to improve technique.

0


source







All Articles