Uzaku
Uzaku

Reputation: 541

Prolog SLD-Tree generator

I was given the task to write a tool that visualizes the SLD-Tree for a given Prolog-program and query. So since I'd rather not implement a whole Prolog-parser and interpreter myself, I am looking for a library or program which generates that tree for me, so that I only need to do the visualization part. The best case would be a C++ library, but something in any common language would do (or a program which outputs the tree as xml document, or anything alike) So far I could not find anything, so I place my hopes on you guys.

Best regards Uzaku

Upvotes: 2

Views: 2748

Answers (1)

migfilg
migfilg

Reputation: 542

You may write a Prolog meta-interpreter in Prolog that produces a representation of the tree to a file and then use it as input for you tool. The simplest of meta-interpreters is this one, that only deals with conjunction (the comma operator)

prolog(true) :- !.
prolog((X,Y)) :- !, prolog(X), prolog(Y).
prolog(H) :- clause(H,Body), prolog(Body).

You should add more clauses for other language constructs, like the disjunction (the semicolon), and support for the cut if needed. For producing the tree you also need to have one or more arguments in the predicate. And you should take care with infinite trees, a limit on the tree depth being a good idea. Finally the use of clause/2 in some Prolog systems require some kind of previous declaration of the predicates whose clauses you want to access.

UPDATE: no need for a nonvar/1 in 3rd clause.

Upvotes: 4

Related Questions