Prolog Family Tree,表兄弟问题

时间:2017-02-21 03:44:43

标签: prolog

我试图在我的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. 

我想在查询cousins(X, william).时返回2个表兄弟,但我只是作为回报得到假。我做错了什么?

编辑:继续我现在所拥有的,但只能得到一个堂兄来展示

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.

1 个答案:

答案 0 :(得分:7)

以下是数据库的图片

enter image description here

表明没有堂兄弟,因为父母之间没有关系。让我们添加一个未知但共享的祖父母:

enter image description here

?- forall(cousins(X,william),writeln(X)).
johnny
peter
betty

如果您有兴趣,可以从此github repo获取图形表示,并使用此最小的粘合剂'代码:

parent_child(P,C) :- parent(P,C).