I am just learning prolog and I ran into the problem where I get the same answer twice when asking for niece and nephew. I just cannot wrap my head around it. Does anyone know what I am doing wrong? Perhaps it is something having two rules for each cousin? But it works fine when asking for brother or sister..
UPDATE: I tried out some things, and after returning to the code I wrote down here, the rule of a niece being only a female doesn't work anymore. and the nephew returns false.
So I have the code:
male( ted ).
male( john ).
male( aron ).
male( nico ).
male( gio ).
female( liza ).
female( sophie ).
female( lily ).
female( elin ).
female( daisy ).
parent_of( lily , aron ).
parent_of( john , aron ).
parent_of( john , elin ).
parent_of( lily , elin ).
parent_of( sophie , gio ).
parent_of( sophie , nico ).
parent_of( daisy , liza ).
parent_of( ted , liza ).
father_of(X,Y):- male(X),
parent_of(X,Y).
mother_of(X,Y):- female(X),
parent_of(X,Y).
sister_of( X , Y ) :- % (X,Y or Y,X) %
female(X),
father_of(F,Y),
father_of(F,X),
X \= Y.
sister_of( X , Y ) :-
female(X),
mother_of(M,Y),
mother_of(M,X),
X \= Y.
brother_of(X,Y):- %(X,Y or Y,X)%
male(X),
father_of(F,Y),
father_of(F,X),
X \= Y.
brother_of(X,Y):- male(X),
mother_of(M,Y),
mother_of(M,X),
X \= Y.
niece_of(X,Y):-
dif(X,Y),
female(X),
parent_of(W,Y),
parent_of(P,X),
brother_of(P,W).
niece_of(X,Y) :-
dif(X,Y),female(X),
parent_of(W,Y),
parent_of(P,X),
sister_of(P,W).
nephew_of(X,Y) :-
dif(X,Y),
male(X),
parent_of(W,Y),
parent_of(P,X),
brother_of(P,W).
nephew_of(X,Y):-
dif(X,Y),
male(X),
parent_of(W,Y),
parent_of(D,X),
sister_of(D,W).
The output I get is
?- niece_of(X,liza).
X = aron ;
X = aron ;
X = nico ;
X = nico ;
X = gio ;
X = gio ;
false.
?- nephew_of(X,liza).
false.


