Prolog家谱,表亲问题

标签 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 个表亲,但我只得到 false 作为返回。我做错了什么?

编辑:这是我现在拥有的,但只能显示 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.

最佳答案

这是你的数据库的图片

enter image description here

这说明没有表亲,只是因为 parent 之间没有关系。让我们添加一个未知但共享的祖 parent :

enter image description here

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

如果您有兴趣,可以从这个 github repo 获得图形表示。 ,使用这个最小的“胶水”代码:

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

关于Prolog家谱,表亲问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358271/

相关文章:

prolog - 在 Prolog 中构建表达式树

c++ - 使用 C++ 接口(interface)在 Prolog 中进行列表处理

list - 检查列表是否有序

prolog - 如何卡住变量列表的目标?

Prolog - 查找列表列表(列表)的所有组合(产品)

prolog - 编写一个接受包装常量的函数,但只有一层深

prolog - Prolog 中的无损通用量化

prolog - 在 Prolog 中求解方程组

Prolog 回溯寻找解决方案并返回错误

PROLOG 规则仅返回第一个匹配项