在Prolog的家谱

Family tree in Prolog

当我想知道谁是谁的兄弟,谁是姐妹,这给了我儿子和女儿,我找不到错误…

1
2
3
4
5
6
7
8
9
    father(pedro-i,fernando-i).
    father(pedro-i,beatriz(1347)).
    father(pedro-i,jo?o(1349)).
    father(pedro-i,dinis(1354)).
    father(pedro-i,jo?o_gr?o_mestre_da_ordem_de_avis).
    mother(constan?a(1320),luis).
    mother(constan?a(1320),maria(1342)).
    mother(constan?a(1320),fernando-i).
    mother(inês_de_castro,beatriz(1347)).

有其他意见吗?我很感激

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    ancestor(X,Y) :- mother(X,Y).
    ancestor(X,Y) :- father(X,Y).

    if_then_else(X,Y,male) :- father(X,Y).
    if_then_else(X,Y,female) :- mother(X,Y).

    son(X,Y) :- father(X,Y).
    sister(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,female).
    brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male).
    descendent (X,Y) :- filho(X,Y).
    descendent (X,Y) :- filho(X,Z),descendent (Z,Y).
    grandfather(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
    grandmother(X,Y) :- ancestor(X,Z),ancestor(Z,Y).
    grandchildren(X,Y) :- ancestor(Z,X),ancestor(Y,Z).

    uncle(X,Y) :- brother(X,Z),ancestor(Z,Y).


你的条款brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male).要求y有一个祖先,但x也需要有一个祖先——同一个祖先:

1
brother(X,Y) :- ancestor(Z,Y),ancestor(Z,X), X\==Y,if_then_else(X,Y,male).

最后你还需要消除X是Y之父的要求。

1
brother(X,Y) :- ancestor(Z,Y),ancestor(Z,X), X\==Y,male(X).

male只需要依靠个人(你不需要做父亲就可以成为男性)male (fernando-i).等。