SWI prolog return true instead of variable - prolog

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

Related

Do not know how to say male(X) not equal to female(X)

I have the following code in prolog.
male(X).
female(X).
child(X,Y).
mother(X,Y) :- female(X), child(Y,X).
this code outputs yes even when I test it as
male(X), mother(X,Y).
I don't how should I code male(X) is not equal to female(X) in this?
I have tried mother(X,Y) :- female(X), !, +male(X), child(Y,X). But this doesn't work.

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

Family Database in Prolog

I have written a program in Prolog for the database of a family. I have defined a few predicates like mother_of, father_of, son_of , grandparent(G,X). I was supposed to define the above-mentioned rules. I am new to Prolog. Can someone please let me know if my code is right or if I am on the right path?
man(dan).
man(joe).
man(paul).
woman(marry).
woman(susie).
parent(joe,susie). % means adam is parent of peter
parent(joe,dan) :- child(dan,joe).
parent(marry,susie).
parent(giuseppe,margaret).
parent(giuseppe,evelyn).
parent(giuseppe,robert).
parent(giuseppe,grace).
parent(giuseppe,fred).
parent(giuseppe,mary).
parent(giuseppe,estelle).
parent(albina,margaret).
parent(albina,evelyn).
parent(albina,robert).
parent(albina,grace).
parent(albina,fred).
parent(albina,mary).
parent(albina,estelle).
parent(ida,catherine).
parent(ida,juanita).
parent(ida,edythe).
parent(ida,george).
parent(johnMiddleton,catherine).
parent(georgeAlexandar,juanita).
parent(georgeAlexandar,edythe).
parent(georgeAlexandar,george).
parent(estelle,steve).
parent(estelle,judy).
parent(estelle,david).
parent(estelle,marilyn).
parent(estelle,john).
parent(george,steve).
parent(george,judy).
parent(george,david).
parent(george,marilyn).
parent(george,john).
parent(marry,dan).
father_of(F,C):-man(F),parent(F,C).
mother_of(M,C):-woman(M),parent(M,C).
son_of(C,F) :-man(C),child(C,F).
brother(B,P) :- man(B),
mother(M,B),
mother(M,P),
father(F,B),
father(F,P).
sister(S,P) :- woman(S),
mother(M,S),
mother(M,P),
father(F,S),
father(F,P).
grandparent(G,X) :- parent(G,P),
parent(P,X).
grandparent(G,john).
I have added
child(C, P) :-
parent(P, C).
and commented out
% parent(joe,dan) :- child(dan,joe).
and
% grandparent(G, john).
Now you can try
?- child(evelyn, giuseppe).
true .
as expected.
All the code for your reference
man(dan).
man(joe).
man(paul).
woman(marry).
woman(susie).
parent(joe, susie).
% Do not use this as it cause stack overflow due to child/2 definition later on
parent(joe, dan).
% :- child(dan, joe).
parent(marry, susie).
parent(giuseppe, margaret).
parent(giuseppe, evelyn).
parent(giuseppe, robert).
parent(giuseppe, grace).
parent(giuseppe, fred).
parent(giuseppe, mary).
parent(giuseppe, estelle).
parent(albina, margaret).
parent(albina, evelyn).
parent(albina, robert).
parent(albina, grace).
parent(albina, fred).
parent(albina, mary).
parent(albina, estelle).
parent(ida, catherine).
parent(ida, juanita).
parent(ida, edythe).
parent(ida, george).
parent(johnMiddleton, catherine).
parent(georgeAlexandar, juanita).
parent(georgeAlexandar, edythe).
parent(georgeAlexandar, george).
parent(estelle, steve).
parent(estelle, judy).
parent(estelle, david).
parent(estelle, marilyn).
parent(estelle, john).
parent(george, steve).
parent(george, judy).
parent(george, david).
parent(george, marilyn).
parent(george, john).
parent(marry, dan).
father_of(F, C) :-
man(F),
parent(F, C).
mother_of(M, C) :-
woman(M),
parent(M, C).
son_of(C, F) :-
man(C),
child(C, F).
brother(B, P) :-
man(B),
mother(M, B),
mother(M, P),
father(F, B),
father(F, P).
sister(S, P) :-
woman(S),
mother(M, S),
mother(M, P),
father(F, S),
father(F, P).
grandparent(G, X) :-
parent(G, P),
parent(P, X).
% ???
% grandparent(G, john).
child(A, B) :-
parent(B, A).

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.

grandfather predicate in Prolog

I have written the following program in SWI-Prolog:
male(reza).
male(aliakbar).
male(behrooz).
male(said).
male(aliasghar).
male(taghi).
male(gholamreza).
male(hadi).
female(fatema).
female(tahere).
female(olya).
female(fatema).
mother(fateme,reza).
mother(olya,hasan).
mother(x,y) :-
wife(x,z),
father(z,y).
brother(said,reza).
brother(x,y) :-
father(z,x),
father(z,y),
x\==y,
male(x).
sister(tahere,fateme).
sister(x,y) :-
father(z,x),
father(z,y),
x\==y,
female(x).
sister(olya,aliakbar).
wife(tahere,gholamreza).
father(gholamreza,hadi).
father(gholamreza, nastaran).
father(abdollah,hasan).
father(aliakbar,reza).
father(taghi,olya).
father(taghi,aliakbar).
father(taghi,aliasghar).
father(aliakbar,said).
grandfather(x,z) :-
father(x,y),
father(y,z).
grandfather(x,z) :-
father(x,y),
mother(y,z).
uncle(y,x) :-
father(z,x),
brother(z,y),
male(y).
aunt(y,x) :-
mother(z,x),
sister(z,y),
female(y).
cousin(y,x) :-
aunt(z,x),
mother(z,y),
female(y).
cousin(y,x) :-
aunt(z,x),
mother(z,y),
male(y).
When I consult "father(X,Y),father(Y,Z)." it returns the correct answer (names are in persian), i.e. it returns
X = taghi,
Y = aliakbar,
Z = reza ;
X = taghi,
Y = aliakbar,
Z = said ;
false.
But I could not find the correct answer of grandfather(X,Y). Please help me why.
Thanks.
Be careful: case is crucial in Prolog. Your rules cannot be interpreted right because you didn't use capitals for variables.

Resources