0

这是我第一次使用 Prolog,我想知道是否有人可以就我的逻辑给我一些建议:

male(jerry).
male(stuart).
male(warren).
male(peter).
female(kather).
female(maryalice).
female(ann).
brother(jerry,stuart).
brother(jerry,kather).
brother(peter, warren).
sister(ann, maryalice).
sister(kather,jerry).
parent_of(warren,jerry).
parent_of(maryalice,jerry).

这是家庭作业的一部分,我们只能使用上述事实。为了知道沃伦和玛丽爱丽丝也是斯图尔特和凯瑟的父母,需要执行一些规则。我所做的是:

parent_of(X,Y) :- brother(Z,Y), parent_of(X,Z).
parent_of(X,Y) :- brother(Y,Z), parent_of(X,Z).
parent_of(X,Y) :- sister(Z,Y), parent_of(X,Z).
parent_of(X,Y) :- sister(Y,Z), parent_of(X,Z).

使用上述规则和事实在 prolog 中查询 parent_of(X,Y) 使我陷入了一个无限循环,其递归值为 X=warren、Y=stuart 和 X=maryalice、Y=stuart。

任何建议将不胜感激。谢谢你!

4

1 回答 1

0

如果您尝试从交互式提示中查询,您会发现它有效。我可能会将其重写为:

?- parent_of(P,C),
   ( ( sister(C,OC) ; sister(OC,C) )
   ; ( brother(C,OC) ; brother(OC,C) )
   ).

但这真的没关系。

The easiest solution to defining a predicate with the exact definition as this query is not calling it parent_of, as you are introducing a recursive call that you don't terminate in any way. So call it maybe also_parent_of?

Furthermore, you have a redundant fact in your database: brother(jerry,kather) and sister(kather,jerry). I don't know if this is on purpose, but using this query, you will get the answer P=warren, C=jerry, OC=kather twice.

于 2013-03-30T08:47:38.943 回答