Prolog Family tree

2019-03-19 15:25发布

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.

标签: prolog
6条回答
该账号已被封号
2楼-- · 2019-03-19 15:58

You have a strong database of facts and a very important predicate called parent(X,Y). Think logically about the approach.

  • Who is a brother/sister:
    a) A male/female, thus male(X) or female(X) must be inside the predicate
    b) X and Y have the same, but be careful to use either mother or father 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).

  • Who is uncle/aunt(a bit tricky, but not a lot):
    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 ;)

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-19 16:03

Your rule of brother already verifies that brother(bob, bob) will fail because it calls sibling(X, Y), which does the check to make sure X \= Y already.

Also, it looks as though everything is working on my machine, but I had to change the dashes on these lines:

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

to:

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

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.

查看更多
孤傲高冷的网名
4楼-- · 2019-03-19 16:07

first:

put your facts at the beginning of your code!

and for brother:

equal(X,Y) :-
   X=Y.

different(X,Y):-
   not(equal(X,Y)).

brother(X, Y) :-
  sibling(X, Y),
  male(X),
  different(X,Y).
查看更多
来,给爷笑一个
5楼-- · 2019-03-19 16:15

Your aunt/2 predicates are not placed together, therefore Prolog assumes grandparent/2 is aunt/2. Place them together like below or use

:- discontiguous(aunt/2).

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

parent(Z,Y) :- father(Z,Y) ; mother(Z,Y).

sibling(bob,bill).
sibling(sue,bill).
sibling(nancy,jeff).
sibling(nancy,ron).
sibling(jell,ron).

sibling(X,Y) :-
    parent(A,X),
    parent(A,Y),
    not(X = Y).


sister(X, Y) :-
      sibling(X, Y),
      female(X),
      not(X = Y).

brother(X, Y) :-
      sibling(X, Y),
      male(X),
      not(X = Y).

grandparent(C,D) :- parent(C,E), parent(E,D).

aunt(X,Y) :-
  parent(Z,Y), sister(X,Z). 
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). 

uncle(X,Y) :-
  parent(Z,Y), brother(X,Z). 


male(john).
male(bob).
male(bill).
male(ron).
male(jeff).

female(mary).
female(sue).
female(nancy).
female(jane).

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).
查看更多
我只想做你的唯一
6楼-- · 2019-03-19 16:16

It looks like some of your axioms are wrong or missing, if you are trying to replicate the diagram.

missing:

female(jane).

father(john, bob).

wrong:

father(john, sue).

This might cause the sibling rule to conflict. But what do I know about prolog

查看更多
叼着烟拽天下
7楼-- · 2019-03-19 16:16

to optimize the rule of the brother for X is not brother to itself.

You should do:

brother(X, Y) :-
      sibling(X, Y),
      male(X),
      not X=Y.
查看更多
登录 后发表回答