Afonso Pinto
Afonso Pinto

Reputation: 9

Family tree in Prolog

When I try to see who is brother to who and same for sister it gives me the sons and daughter, I cannot find the mistake...

    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)).

Any other opinion I appreciate that

    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).

Upvotes: 0

Views: 1780

Answers (1)

Topological Sort
Topological Sort

Reputation: 2787

Your clause brother(X,Y) :- ancestor(Z,Y),X\==Y,if_then_else(X,Y,male). requires Y to have an ancestor, but X also needs to have an ancestor -- the same ancestor:

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

You also need to eliminate the requirement at the end that X be the father of Y.

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

male needs to depend simply on the individual (you don't need to be a father to be a male.) male (fernando-i)., etc.

Upvotes: 1

Related Questions