简体   繁体   English

元编程序言,基础事实列表

[英]meta-Programming Prolog ,List of ground facts

I must generate a list that contains all the ground terms that pass through a meta-programming in Prolog. 我必须生成一个列表,其中包含通过Prolog中的元编程传递的所有基本术语。 I did it , but the last element I don't know why is duplicate, here my code : 我做到了,但是我不知道为什么的最后一个元素是重复的,这里是我的代码:

KB: KB:

parent(aa,vv).
parent(bb,aa). 
parent(bb,cc).
parent(dd,bb).

ancestor(X,Y):- parent(X,Y).
ancestor(X,Y):- ancestor(X,Z),parent(Z,Y).

Here is the meta-interpreter : 这是元解释器:

 solve(true,true,_):-!.
 solve((A,B),(ProofA,ProofB),Rest):- !, solve(A,ProofA,Rest), solve(B,ProofB,Rest).
 solve(A,(A:-Proof), [A:-Proof|Rest]):- ground(A),clause(A,B),solve(B,Proof,Rest).
 solve(A,(A:-Proof), List):- clause(A,B),solve(B,Proof,List).

My query is: solve(ancestor(dd,vv),H,N). 我的查询是: solve(ancestor(dd,vv),H,N).

As I said previously, the interpreter inserts all ground facts in the list but it duplicates the last element. 正如我之前说的,解释器将所有地面事实插入列表中,但它会复制最后一个元素。 You can see it compiling my code. 您可以看到它正在编译我的代码。

Could you help me? 你可以帮帮我吗?

Your last clause of the solve/3 predicate overlaps with the previous one. 您的solve/3谓词最后一个子句与上一个重叠。 Ie it doesn't verify that the current goal that you're trying to prove is not ground. 也就是说,这并不能验证您要证明的当前目标是否未实现。 You can either add that test, \\+ ground(A) or combine the two last clauses in a single one using an if-then-else control construct: 您可以添加测试\\+ ground(A)或使用if-then-else控制构造将最后两个子句合并为一个:

solve(A, (A:-Proof), List) :-
    (   ground(A) ->
        List = [A:-Proof| Rest],
        clause(A, B),
        solve(B, Proof, Rest)
    ;   clause(A, B),
        solve(B, Proof, List)
    ).

But your definition of this predicate is odd. 但是您对该谓词的定义很奇怪。 Shouldn't the ground/1 test be done after calling clause? 调用子句不应该进行ground/1测试吗? And shouldn't you verify that B == true to ensure that you're using facts from the KB? 并且不应该验证B == true以确保您使用的是KB中的事实吗?

try this: instead of your 试试这个:代替你的

solve(true,true,_)

put this 把这个

solve(true,true,[])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM