如何确定孩子是否是PROLOG家谱中的单个孩子



我目前正在编写一个代表家庭关系的PROLOG程序。

现在我已经实现了以下有效的功能。

male(X). Returns true if X is male.
female(X). Returns true if X is female.
mother_of(X,Y). Returns true if X is the mother of Y.
father_of(X,Y). Returns true if X is the father of Y.
sister_of(X,Y).  Returns true if X is the sister of Y.
brother_of(X,Y). Returns true if X is the brother of Y.

现在我想实现一个功能来检查某人是否是独生子女/没有姐妹/兄弟。我尝试了以下功能,但没有一个有效:

single_child(X) :- (+ sister_of(X,Y)),(+ brother_of(X,Y)).
single_child(X) :- not(sister_of(X,Y)), not(brother_of(X,Y)).
single_child(X) :- + (sister_of(X,Y),+ brother_of(X,Y)).
single_child(X) :- not(sister_of(X,Y),brother_of(X,Y)).

有谁知道我如何正确实现这样的功能?

亲切问候大卫·

我自己找到了一个解决方案:

single_child(X) :- + has_sibling(X),(parent(P,X)).
parent(X,Y) :- father_of(X,Y);mother_of(X,Y).
sibling(X,Y):- parent(Z,X), parent(Z,Y), X=Y.
has_sibling(X) :- sibling(X,_).

最新更新