我是Prolog的新手,我不知道该如何解决。我必须写一个兄弟姐妹规则。一直到代码底部,我都有兄弟姐妹规则。规则的问题在于它会返回兄弟姐妹两次。例如,如果我想得到茱莉亚的兄弟姐妹,我会两次获得“罗莎”。我认为正在发生的事情是该程序对每个父级(alex和lina)进行评估。那么,我该如何修正自己的规则,使我只得到朱莉娅(或任何人)的兄弟姐妹一次?
parent(alex,julia).
parent(alex,rosa).
parent(lina,julia).
parent(lina,rosa).
parent(romeo,peter).
parent(julia,peter).
parent(rosa,silvia).
parent(oscar,ida).
parent(eva,ida).
parent(eva,bruno).
parent(peter,bruno).
parent(peter,georg).
parent(peter,irma).
parent(ruth,georg).
parent(ruth,irma).
parent(silvia,otto).
parent(silvia,pascal).
parent(irma,olga).
parent(irma,jean).
parent(otto,olga).
parent(otto,jean).
parent(jean,tina).
parent(marie,tina).
male(alex).
male(romeo).
male(oscar).
male(peter).
male(bruno).
male(georg).
male(otto).
male(pascal).
male(jean).
husband(alex,lina).
husband(romeo,julia).
husband(oscar,eva).
husband(peter,ruth).
husband(otto,irma).
husband(jean,marie).
% father(X,Y) :- X is the father of Y.
father(X,Y) :- parent(X,Y), male(X).
% grandfather(X,Y) :- X is the grandfather of Y.
grandfather(X,Y) :- father(X,P), parent(P,Y).
% brother(X,Y) :- X is the brother of Y.
brother(X,Y) :- parent(P,X), parent(P,Y), male(X), X \= Y.
% uncle(X,Y) :- X is the uncle of Y.
uncle(X,Y) :- brother(X,P), parent(P,Y).
% female(X) :- X is a female person.
female(X) :- \+ male(X).
% sister(X,Y) :- X is the sister of Y.
sister(X,Y) :- parent(P,X), parent(P,Y), female(X), X \= Y.
% has_son(X) :- the person X has a son.
has_son(X) :- parent(X,Y), male(Y).
% married(X,Y) :- X and Y are married to each other.
married(X,Y) :- husband(X,Y).
married(X,Y) :- husband(Y,X).
% brother_in_law(X,Y) :- X is the brother-in-law of Y.
brother_in_law(X,Y) :- brother(X,P), married(P,Y).
brother_in_law(X,Y) :- husband(X,W), sister(W,Y).
% ancestor(X,Y) :- X is an ancestor of Y.
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(X,P), ancestor(P,Y).
% relatives(X,Y) :- X and Y are relatives (related by blood to each other).
relatives(X,X).
relatives(X,Y) :- ancestor(X,Y).
relatives(X,Y) :- ancestor(Y,X).
relatives(X,Y) :- ancestor(A,X), ancestor(A,Y).
% ancestors(As,X) :- As is the set of all (known) ancestors of X.
ancestors(As,X) :- setof(A,ancestor(A,X),As).
% descendants(Ds,X) :- Ds is the set of all (known) descendants of X.
descendants(Ds,X) :-setof(D,ancestor(X,D),Ds).
% ancestor(X,Y,L) :- X is an ancestor of Y, and L is the list of names
% leading from X to Y.
ancestor(X,Y,[X,Y]) :- parent(X,Y).
ancestor(X,Y,[X|L]) :- parent(X,P), ancestor(P,Y,L).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Exercise 1
% Write a rule that determines if mother (M) is mother of (C) child.
% mother(M, C) :-
mother(M,C) :- female(M), parent(M,C).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Exercise 2
% Write a rule that infers the siblings of certain (X) person
% siblings. Ss is the siblings list.
% siblings(Ss, X) :-
sibling(X,Y) :- dif(X,Y),parent(Z,X),parent(Z,Y).
答案 0 :(得分:1)
您的怀疑是正确。您的谓词存在的问题是,一个人(通常)有两个父母:mother/2
和father/2
。
这意味着,如果rosa
和julia
是( real )兄弟姐妹(则它们共享相同的mother/2
和father/2
)。 / p>
如果我们trace.
看到重复出现了这个原因:
?- trace.
true.
[trace] ?- sibling(rosa, julia).
Call: (8) sibling(rosa, julia) ? creep
Call: (9) dif:dif(rosa, julia) ? creep
Exit: (9) dif:dif(rosa, julia) ? creep
Call: (9) parent(_5278, rosa) ? creep
Exit: (9) parent(alex, rosa) ? creep
Call: (9) parent(alex, julia) ? creep
Exit: (9) parent(alex, julia) ? creep
Exit: (8) sibling(rosa, julia) ? creep
true ;
Redo: (9) parent(alex, julia) ? creep
Fail: (9) parent(alex, julia) ? creep
Redo: (9) parent(_5278, rosa) ? creep
Exit: (9) parent(lina, rosa) ? creep
Call: (9) parent(lina, julia) ? creep
Exit: (9) parent(lina, julia) ? creep
Exit: (8) sibling(rosa, julia) ? creep
true.
因此,首先我们通过sibling/2
(两者中的alex
)找到father/2
关系,然后我们通过sibling/2
找到lina
关系(两者中的mother/2
。
如果这里没有半兄弟姐妹(两个人共享相同的father/2
或mother/2
,但不共享相同的mother/2
或{{1} }),那么我们可以通过仅选择两个父级之一来“消除重复”,例如father/2
:
mother/2
根据注释,您的sibling(A, B) :-
dif(A, B),
mother(M, A),
mother(M, B).
谓词似乎无法正常工作。因此,我建议您尝试解决此问题。
或通过female/1
:
father/2
如果我们考虑一个有半兄弟姐妹的家庭,我们可以通过同时检查sibling(A, B) :-
dif(A, B),
father(F, A),
father(F, B).
和sibling/2
来实现一个考虑mother/2
的谓词。