lucyb
lucyb

Reputation: 353

Prolog Family Tree, cousins issues

I am trying to list all the cousins of a particular person in my prolog program but can't seem to get it to work. I've checked my code and it seems correct but I am not getting the output I want.

father(john, johnny).
father(john, peter).
father(josh, william).
father(simone, betty).

mother(mary, johnny).
mother(mary, peter).
mother(catherine, william).
mother(kate, betty).

parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).

siblings(B,G) :- parent(P,B), parent(P,G), B\=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y. 

I want when I query cousins(X, william). to return the 2 cousins but I am only getting false as a return. What am I doing wrong?

EDIT: heres what I have now, but can only get 1 cousin to display

father(grandpa1, mary).
father(grandpa1, catherine).
father(grandpa2, john).
father(grandpa2, simone).
father(john, johnny).
father(john, peter).
father(josh, william).
father(simone, betty).

mother(grandma1, mary).
mother(grandma1, catherine).
mother(grandma2, john).
mother(grandma2, simone).
mother(mary, johnny).
mother(mary, peter).
mother(catherine, william).
mother(kate, betty).

parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).

siblings(B,G) :- parent(P,B), parent(P,G), B\=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y.

Upvotes: 2

Views: 3650

Answers (1)

CapelliC
CapelliC

Reputation: 60014

Here is a picture of your database

enter image description here

that shows there are no cousins, just because there are no relations among parents. Let's add an unknown, but shared, grandparent:

enter image description here

?- forall(cousins(X,william),writeln(X)).
johnny
peter
betty

If you're interested, the graphical representation can be obtained from this github repo, with this minimal 'glue' code:

parent_child(P,C) :- parent(P,C).

Upvotes: 7

Related Questions