Prolog 序言:简单的家庭关系

Prolog 序言:简单的家庭关系,prolog,logic,family-tree,Prolog,Logic,Family Tree,我正在学习Prolog,我想做的是对一个simle家庭的成员做一个简单的“计算”(我不知道它在Prolog中叫什么) 例如,我有: 1)father of steve is petter 2)brother steve is john 3)A person is a son to a Father when the brother of the person has as father the Father (这似乎很有趣,而且完全不合逻辑,但我却忘了:) 我的问题是谁是约翰的父亲(正

我正在学习Prolog,我想做的是对一个simle家庭的成员做一个简单的“计算”(我不知道它在Prolog中叫什么)

例如,我有:

 1)father of steve is petter
 2)brother steve is john
 3)A person is a son to a Father when the brother of the person has  as father the Father
(这似乎很有趣,而且完全不合逻辑,但我却忘了:)

我的问题是谁是约翰的父亲(正确的父亲应该是彼得)


但它总是给我假。

当你输入
父亲(约翰,X)。
时,它首先试图找到一个
Z
,这样
兄弟(约翰,Z)
就是真的。不存在这样的
Z
,因此返回false


请注意,
兄弟(steve,john)
并不意味着兄弟(john,steve),除非您告诉Prolog应该这样做。

解决:

father(steve,petter).
brother(john,steve).
whoisfather(X,Y):-brother(X,Z),father(Z,Y).
?- whoisfather(john,X).
X = petter.
而不是

father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).
?- father(john,X).
false.

请参阅注释。在教程中,我读到了(1,2)所说的可以解释为(2,1)的东西,但我想这只是程序员的意思,不是序言…我的错。。。但是,即使我改变了它们,我也会再次出错,即使我把这两种情况都放进去。正确的方法是什么?@SteveL我想你读到的意思是,你可以将诸如“迈克与狗玩耍”之类的短语编码为“迈克与狗玩耍”或者“迈克与狗玩耍”或者“迈克与狗玩耍”或者“迈克与狗玩耍”。但是一旦你选择了一种解释,你就必须坚持下去。不,实际上,你的原始代码是有效的。你只是多了一个括号,一个拼写错误。当我运行它时,我得到
2?-父亲(约翰,X)。
==>
X=peter(这里我按
),然后按
。lol。天哪,我怎么会错过那个让你发疯的小错误。谢谢
father(steve,petter).
brother(john,steve).
whoisfather(X,Y):-brother(X,Z),father(Z,Y).
?- whoisfather(john,X).
X = petter.
father(steve,petter).
brother(john,steve).
father(X,Y):-brother(X,Z),father(Z,Y)).
?- father(john,X).
false.