我试图在我的prolog程序中列出特定人的所有表兄弟,但似乎无法让它工作.我检查了我的代码,看起来是正确的,但我没有得到我想要的输出.
father(john, johnny).
father(john, peter).
father(josh, william).
father(simone, betty).
mother(mary, johnny).
mother(mary, peter).
mother(catherine, william).
mother(kate, betty).
parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).
siblings(B,G) :- parent(P,B), parent(P,G), B\=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y.
Run Code Online (Sandbox Code Playgroud)
我希望当我查询cousins(X, william).
返回2个表兄弟但我只是作为回报得到假.我究竟做错了什么?
编辑:继承人我现在拥有的,但只能得到1表兄弟
father(grandpa1, mary).
father(grandpa1, catherine).
father(grandpa2, john).
father(grandpa2, simone).
father(john, johnny).
father(john, peter).
father(josh, william).
father(simone, betty).
mother(grandma1, mary).
mother(grandma1, catherine).
mother(grandma2, john).
mother(grandma2, simone).
mother(mary, johnny).
mother(mary, peter).
mother(catherine, william).
mother(kate, betty).
parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).
siblings(B,G) :- parent(P,B), parent(P,G), B\=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y.
Run Code Online (Sandbox Code Playgroud)
这是您的数据库的图片
这表明没有堂兄弟,只因为父母之间没有关系.让我们添加一个未知但共享的祖父母:
?- forall(cousins(X,william),writeln(X)).
johnny
peter
betty
Run Code Online (Sandbox Code Playgroud)
如果您有兴趣,可以从这个github仓库获取图形表示,使用这个最小的"胶水"代码:
parent_child(P,C) :- parent(P,C).
Run Code Online (Sandbox Code Playgroud)