У меня есть простая база знаний, которая кодирует генеалогическое древо. Некоторые важные правила в этом представлении заключаются в следующем:
% fathers
father(michael,cathy).
father(michael,sharon).
father(charles_gordon,michael).
father(charles_gordon,julie).
father(charles,charles_gordon).
father(jim,melody).
father(jim,crystal).
father(elmo,jim).
father(greg,stephanie).
father(greg,danielle).
% mothers
mother(melody,cathy).
mother(melody,sharon).
mother(hazel,michael).
mother(hazel,julie).
mother(eleanor,melody).
mother(eleanor,crystal).
mother(crystal,stephanie).
mother(crystal,danielle).
% parents
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
% men
male(michael).
male(charles_gordon).
male(charles).
male(jim).
male(elmo).
male(greg).
% women
female(cathy).
female(sharon).
female(julie).
female(hazel).
female(eleanor).
female(melody).
female(crystal).
female(stephanie).
female(danielle).
person(X) :- male(X) ; female(X).
parent(X,Y) :- father(X,Y) ; mother(X,Y). % X is parent of Y
child(X,Y) :- parent(Y,X).
elder(X,Y) :- parent(X,Y). % X is an elder of Y, meaning X is a parent or an ancestor of Y
elder(X,Y) :- parent(X,Z), elder(Z,Y).
junior(X,Y) :- child(X,Y). % X is a junior of Y, meaning X is a child or some descendant of Y
junior(X,Y) :- child(X,Z), junior(Z,Y).
Я пытаюсь найти ближайшего старшего между двумя людьми (предикат ne(X,Y,Z)
). Этот человек Z
является старшим из X
и Y
, и никто из младших Z
не является также старшим из ОБОИХ X
и Y
.
Моя попытка выглядит так:
ne(X,Y,Z) :- person(X),
person(Y),
X \= Y,
elder(Z,X),
elder(Z,Y),
junior(A,Z),
not(elder(A,X)),
not(elder(A,Y)).
но это как-то неправильно, потому что всякий раз, когда я запускаю ?- ne(stephanie,cathy,Z).
, я получаю
Z = jim ;
Z = jim ;
Z = jim ;
Z = jim ;
Z = elmo ;
Z = elmo ;
Z = elmo ;
Z = elmo ;
Z = eleanor ;
Z = eleanor ;
Z = eleanor ;
Z = eleanor ;
но я должен получить только один ответ, и я не могу понять, что не так. Спасибо!
parent/2
дважды. Кроме того, человек может быть мужчиной или женщиной, но тогда почему тогда у вас естьfather/2
иmother/2
, а не толькоparent/2
? Или один и тот же человек может быть мужчиной и матерью? В любом случае, проследите свою программу, и вы можете понять, что не так. И не игнорируйте предупреждения времени компиляции. - person   schedule 09.09.2014