I did it but its not showing answers When i ask about the brothers,sisters,uncles,aunts
This is what I wrote, what's wrong ?
/*uncle(X, Y) :– male(X), sibling(X, Z), parent(Z, Y).*/
/*uncle(X, Y) :– male(X), spouse(X, W), sibling(W, Z), parent(Z, Y).*/
uncle(X,Y) :-
parent(Z,Y), brother(X,Z).
aunt(X,Y) :-
parent(Z,Y), sister(X,Z).
sibling(X, Y) :-
parent(Z, X),
parent(Z, Y),
X \= Y.
sister(X, Y) :-
sibling(X, Y),
female(X).
brother(X, Y) :-
sibling(X, Y),
male(X).
parent(Z,Y) :- father(Z,Y).
parent(Z,Y) :- mother(Z,Y).
grandparent(C,D) :- parent(C,E), parent(E,D).
aunt(X, Y) :– female(X), sibling(X, Z), parent(Z, Y).
aunt(X, Y) :– female(X), spouse(X, W), sibling(W, Z), parent(Z, Y).
male(john).
male(bob).
male(bill).
male(ron).
male(jeff).
female(mary).
female(sue).
female(nancy).
mother(mary, sue).
mother(mary, bill).
mother(sue, nancy).
mother(sue, jeff).
mother(jane, ron).
father(john, sue).
father(john, bill).
father(bob, nancy).
father(bob, jeff).
father(bill, ron).
sibling(bob,bill).
sibling(sue,bill).
sibling(nancy,jeff).
sibling(nancy,ron).
sibling(jell,ron).
And one more thing, how do I optimize the rule of the brother so that X is not brother to itself.
You have a strong database of facts and a very important predicate called
parent(X,Y)
. Think logically about the approach.a) A male/female, thus
male(X)
orfemale(X)
must be inside the predicateb) X and Y have the same, but be careful to use either
mother
orfather
function, because otherwise the result will be shown twice.PS: make sure
X/=Y
. =)Ex.:
brother(X,Y):- X/=Y, male(X), father(Father,X), father(Father,Y).
a) Is a male/female.
b) Aunt/uncle is sister/brother of the sibling's mom or dad.
Ex.:
aunt(X,Y):- female(X), parent(Parent,Y),sister(X,Parent).
PS: Aunt may also be considered the wife of the brother of sibling's mom or dad(Uncle's wife). But in this case you need to introduce a new fact that expresses a
married_couple
.aunt(X,Y):- female(X),
parent(Parent,Y),
(sister(X,Parent); (brother(Parent,Uncle),married_couple(Uncle,X))).
Hope this works ;)
Your rule of brother already verifies that
brother(bob, bob)
will fail because it callssibling(X, Y)
, which does the check to make sureX \= Y
already.Also, it looks as though everything is working on my machine, but I had to change the dashes on these lines:
to:
Yeah, they look identical, but the dashes in the top version are slightly longer... and seemed to cause problems when I "consulted" the file.
I only caught that because I created a Prolog color scheme for Notepad++, if anyone is interested I can post it online.
first:
put your facts at the beginning of your code!
and for brother:
Your aunt/2 predicates are not placed together, therefore Prolog assumes grandparent/2 is aunt/2. Place them together like below or use
You use spouse/2, but do not define it. Furthermore, Prolog assumes there should be a sibling/2 predicate somewhere and uses father/2. It does this because you define list of siblings on the bottom of your KB. Again, place them together like below.
Like stated in other answers, you can use not(X = Y).
It looks like some of your axioms are wrong or missing, if you are trying to replicate the diagram.
missing:
wrong:
This might cause the sibling rule to conflict. But what do I know about prolog
to optimize the rule of the brother for X is not brother to itself.
You should do: