我如何为序言创建兄弟谓词?

How do i create a brother predicate for prolog?

有人告诉我创建一个兄弟谓词,用于查明兄弟是否有兄弟姐妹。 Brother(B, S) :-。我知道您需要查明它们是否具有相同的 parents,但我不确定该怎么做。

father(dan,phil).
father(dan,jack).
father(dan,christine).
father(phil,kenton).
father(phil,shula).
father(phil,david).
father(phil,elizabeth).
father(jack,lillian).
father(jack,tony).
father(jack,jenny).
father(tony,tommy).
father(tony,helen).
father(tony,john).
father(roger,adam).
father(roger,debbie).
father(brian,kate).
father(david,pip).
father(david,josh).
father(mark,daniel).

mother(doris,phil).
mother(doris,jack).
mother(doris,christine).
mother(jill,kenton).
mother(jill,shula).
mother(jill,david).
mother(jill,elizabeth).
mother(peggy,lillian).
mother(peggy,tony).
mother(peggy,jenny).
mother(pat,tommy).
mother(pat,helen).
mother(pat,john).
mother(jenny,adam).
mother(jenny,debbie).
mother(jenny,kate).
mother(ruth,pip).
mother(ruth,josh).
mother(shula,daniel).

male(dan).
male(phil).
male(jack).
male(kenton).
male(david).
male(tony).
male(brian).
male(roger).
male(mark).
male(john).
male(tommy).
male(adam).
male(daniel).
male(josh).

female(doris).
female(jill).
female(peggy).
female(christine).
female(shula).
female(ruth).
female(elizabeth).
female(lillian).
female(pat).
female(jenny).
female(helen).
female(debbie).
female(kate).
female(pip).

dead(dan).
dead(doris).
dead(jack).
dead(mark).
dead(john).
dead(fred).


parent(X,Y) :-
        father(X,Y); mother(X,Y).

grandfather(X,Y) :-
        father(X,Z), mother(Z,Y).
grandfather(X,Y) :-
        father(X,Z), father(Z,Y).

ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

archer(dan).
archer(X) :- father(P,X), archer(P).

这是我的文档,其中定义了 parents、祖父等。 我需要创建一个兄弟谓词,作为兄弟(B 是兄弟,S 是兄弟姐妹)。例如兄弟(利亚姆,佐治亚州)。 Liam是Georgia的弟弟应该是真的

您可以通过首先提供 brother(B,S) 是什么的正式定义来解决您的问题:

A brother B of a slibing S is a male/1 which has the same father/2 F and mother/2 M.

现在我们知道了这一点,我们为每个条件写一行或多行。


第一个只是子句的头部:

brother(B,S) :-

既然兄弟是male/1,我们检查的第一个条件是:

    male(B),

接下来我们要检查他们是否有相同的father/2。因此我们定义一个父亲F,而F必须是B的父亲,所以我们写:

    father(F,B),

因为那个父亲也必须是 S 的父亲,所以我们写:

    father(F,S),

同样适用于mother:我们定义一个母亲M并检查这个母亲是BS的母亲:

    mother(M,B),
    mother(M,S).

现在把它们放在一起我们得到:

brother(B,S) :-
    male(B),
    father(F,B),
    father(F,S),
    mother(M,B),
    mother(M,S).

现在有个问题是,有了这个谓词,男的就是自己的兄弟了。如果你不想要这种行为,我们添加一个约束:

And a person is not a brother/2 of himself.

因此您必须使用 不等于 谓词:

    B \= S.

或完整谓词:

brother(B,S) :-
    male(B),
    father(F,B),
    father(F,S),
    mother(M,B),
    mother(M,S),
    B \= S.

该谓词生成以下答案:

?- brother(B,S).
B = phil,
S = jack ;
B = phil,
S = christine ;
B = jack,
S = phil ;
B = jack,
S = christine ;
B = kenton,
S = shula ;
B = kenton,
S = david ;
B = kenton,
S = elizabeth ;
B = david,
S = kenton
...