开发者

Rule dependency graph in Prolog

Let's say I want to make a rule dependency graph for a Prolog program, in Prolog. For instance, the following program

foo(X) :- bar(X, 0).
bar(A, B) :- quux(A), coox(B).
baz.

would resul开发者_JAVA技巧t in the following being true (by using assert):

depends(foo, [bar]).
depends(bar, [quux, coox]).
depends(baz, []).

Or something like the above that I could easily use to make a graph. The approach I've been thinking about is reading the lines of input file as strings and performing some simple search-and-replace on them, but that looks like an ugly, un-Prologlike hack. Any other options that make use of Prolog's metalogical capabilities?


There's no need to read the file in as (unstructured) string: A Prolog source file is a sequence of valid Prolog terms, you can read the clauses with (for example) read/1 and reason about them just as you would about any other terms. (:-)/2 is already a predefined operator, and you can then easily inspect the clauses with Prolog's built-in metapredicates (arg/3, functor/3, =../2 etc.). This automatically takes care of source-level comments etc. just as you would expect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜