Prolog Family Tree, cousins issues - prolog

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.

Here is a picture of your database
that shows there are no cousins, just because there are no relations among parents. Let's add an unknown, but shared, grandparent:
?- 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).

Related

SWI prolog return true instead of variable

here is my code
/*facts*/
male(James).
male(Charles).
male(William).
female(Megan).
male(George).
female(Catherine).
female(Diana).
female(Elizabith).
parent(James,Charles).
parent(James,Elizabeth).
parent(Charles,Catherine).
parent(Charles,William).
parent(Charles,Megan).
parent(Elizabeth,Diana).
parent(Diana,George).
/*rules*/
different(X,Y):- X\=Y.
father(X, Y) :- parent(X,Y), male(X).
mother(X, Y) :- parent(X,Y), female(X).
grandparent(X,Y):- parent(X,F), parent(F,Y).
grandma(X,Y):- parent(X,F), parent(F,Y),female(X).
sister(X,Y):-female(X), parent(F,X), parent(F,Y),different(X,Y) .
brother(X,Y):-male(X), parent(F,X), parent(F,Y),different(X,Y).
aunt(X,Y):-parent(F,Y), sister(X,F), female(X).
uncle(X,Y):-parent(F,Y), brother(X,F), male(X).
i write mother(X,Y). expecting return name not true or false .
same with all statements
Your facts use variables and not constants. Constants and functors start with a lowercase, so james, charles, megan and diana, not James, Charles, Megan and Diana:
male(james).
male(charles).
male(william).
female(megan).
male(george).
female(catherine).
female(diana).
female(elizabith).
parent(james, charles).
parent(james, elizabeth).
parent(charles, catherine).
parent(charles, william).
parent(charles, megan).
parent(elizabeth, diana).
parent(diana, george).

Prolog query satisfiable but returns false

I have a simple example below of a prolog program I have written, which contains a satisfiable query that always returns false for searches.
male(william).
male(harry).
% parent(x,y) - the parent of x is y
parent(william, diana).
parent(william, charles).
parent(harry, diana).
parent(harry, charles).
% Brother - the brother of X is Y
brother(X,Y) :- X\=Y, parent(X,A), parent(Y,A), male(Y).
When I ask if two constants are brothers, that works fine, but if I try to find a brother of a constant prolog returns false.
?- brother(william,harry).
true
?- brother(william,X).
false.
What am I doing wrong?
The problem is here X\=Y this part lucks logical purity since \=/2 is non-monotonic. Simply to make it work change the order:
brother(X,Y) :- X\=Y, parent(X,A), parent(Y,A), male(Y).
to:
brother(X,Y) :- parent(X,A), parent(Y,A), male(Y), X\=Y.
But a much better solution would be to use dif/2 which is preserves purity:
brother(X,Y) :- dif(X,Y), parent(X,A), parent(Y,A), male(Y).

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

Error in Family Tree using Prolog

I have some issues in my family tree. As required, I need to create predicates on father, mother, son, daughter, grandfather, sibling, aunt, uncle, cousin, spouse, parent_of based on the facts of Male, Female, parent_of.
male(jerry).
male(stuart).
male(warren).
male(peter).
female(kather).
female(maryalice).
female(ann).
brother(jerry,stuart).
brother(jerry,kather).
brother(peter, warren).
sister(ann, maryalice).
sister(kather,jerry).
parent_of(warren,jerry).
parent_of(maryalice,jerry).
father(X,Y) :- male(X), parent_of(X,Y).
mother(X,Y) :- female(X), parent_of(X,Y).
son(X,Y) :- male(X), parent_of(Y,X).
daughter(X,Y) :- female(X), parent_of(Y,X).
grandfather(X,Y) :- father(X,P), parent_of(P,Y).
sibling(X,Y):- parent_of(P,X), parent_of(P,Y), X\=Y.
aunt(X,Y) :- sister(X,P), parent_of(P,Y).
uncle(X,Y) :- brother(X,P), parent_of(P,Y).
cousin(X,Y):- sibling(P,Q), parent_of(P,X), parent_of(Q,Y).
spouse(X,Y) :- parent_of(X,P), parent_of(Y,P).
parent_of(X,Y) :- male(X), father(X,Y); female(X), mother(X,Y).
The parent_of predicate gives me an error. Clasues of parent_of/2 are not together in the source file. When I ignore the error and run a query ?- sibling(jerry, stuart), it gives me an error out of local stack. Anyone know how to solve this problem. Your help will be appreicated. Thanks.
Problem 1
The parent_of predicate gives me an error. Clasues of parent_of/2 are not together in the source file.
Simply place definitions of the predicate right next to eachother
Change
parent_of(warren,jerry).
parent_of(maryalice,jerry).
...
spouse(X,Y) :- parent_of(X,P), parent_of(Y,P).
parent_of(X,Y) :- male(X), father(X,Y); female(X), mother(X,Y).
to
parent_of(warren,jerry).
parent_of(maryalice,jerry).
parent_of(X,Y) :- male(X), father(X,Y); female(X), mother(X,Y).
...
spouse(X,Y) :- parent_of(X,P), parent_of(Y,P).
Problem 2
?- sibling(jerry, stuart), it gives me an error out of local stack.
You have a circular logic error. You define parent_of\2 in terms of father\2 and father\2 in terms of parent_of\2. This will cause each to circle off into Prolog never-never-land.

How to find blood relatives only in prolog

Using recursion i need to find all blood relatives of any person in the family tree.
My attempt so far has failed.
Here is my code, with my attempt at the bottom
female(helen).
female(debbie).
female(louise).
female(yvonne).
female(belinda).
female(heather).
male(john).
male(andrew).
male(barry).
male(daniel).
male(charles).
parent(helen, debbie).
parent(helen, barry).
parent(helen, louise).
parent(john, debbie).
parent(john, barry).
parent(andrew, louise).
parent(debbie, yvonne).
parent(debbie, daniel).
parent(barry, charles).
parent(barry, belinda).
parent(louise, heather).
mother(X, Y) :-
female(X),
parent(X, Y).
father(X, Y) :-
male(X),
parent(X,Y).
child(X, Y) :-
parent(Y, X).
daughter(X, Y) :-
parent(Y, X),
female(X).
son(X, Y) :-
parent(Y,X),
male(X).
sister(X, Y) :-
female(X),
parent(Q,X),
parent(Q,Y).
brother(X, Y) :-
male(X),
parent(Q,X),
parent(Q,Y).
sibling(X, Y) :-
parent(Q,X),
parent(Q,Y),
X\=Y.
uncle(X, Y) :-
parent(P,Y),
brother(X,P).
aunt(X, Y) :-
parent(P,Y),
sister(X,P).
cousin(C, Cousin):-
parent(Parent,C),
sibling(Parent,AU),
child(Cousin,AU).
%Here is Relative
relative(An, Re):-
An\=Re,
parent(An, Re);
sibling(An, Re).
relative(An, Rela):-
parent(An, Child);
sibling(An, Rela),
relative(Child, Rela),
An\=Rela, C\=Rela.
Sort of works, but gets stuck in an infinite loop at the end.
Thanks.
not sure about 'relatives' (any person bound reachable in a parent/child relation ?), but your definition seems more complex than needed ( do you know what ; does ?).
I tried
relative(An, Re):-
parent(An, Re).
relative(An, Rela):-
parent(An, C),
relative(C, Rela).
that yields
16 ?- forall(relative(X,Y),writeln(X:Y)).
helen:debbie
helen:barry
helen:louise
john:debbie
john:barry
andrew:louise
debbie:yvonne
debbie:daniel
barry:charles
barry:belinda
louise:heather
helen:yvonne
helen:daniel
helen:charles
helen:belinda
helen:heather
john:yvonne
john:daniel
john:charles
john:belinda
andrew:heather
true.
edit I tried another relation, using a generalized parent/2, but still too permissive.
relative(Pers, Re):-
ancestor(Re, Pers) ; sibling(Pers, Re) ; cousin(Pers, Re) ; uncle(Re, Pers) ; aunt(Re, Pers).
ancestor(Anc, Pers) :- parent(Anc, Pers).
ancestor(Anc, Pers) :- parent(Anc, P), ancestor(P, Pers).
Maybe cousin/2 is too permissive also. Here is the graph
I guess that heather should have only luise,helen,andrew as relatives. It's this true ?
edit given latest comment, seems that the definition could be right. I get
24 ?- setln(X,relative(heather,X)).
andrew
barry
belinda
charles
daniel
debbie
helen
louise
yvonne
true.
that is everyone is related to heather apart john.
Here's one way that works, but it will sometimes produce duplicates. Using setof will give the unique collection. I avoided the miscellaneous relations and stuck with descendent or parent.
descendent(A, B) :-
parent(B, A).
descendent(A, B) :-
parent(C, A),
descendent(C, B).
relative(A, B) :-
descendent(B, A).
relative(A, B) :-
descendent(A, B).
relative(A, B) :-
descendent(A, C),
descendent(B, C),
A \= B.
setof(A, relative(heather, A), Relatives).
Relatives = [andrew,barry,belinda,charles,daniel,debbie,helen,louise,yvonne]
If you don't have setof, you can use the findall/3 and sort/2 ISO predicates:
findall(A, relative(heather, A), R), sort(R, Relatives).
Note that the solutions presented so far assume that all of the relatives have unique names. A general case of dealing with relatives with the same first name (and possibly the same last name) you would need to track and compare lineages for differences.

Resources