这是我的代码,只关注最后 3 条规则:
father(costac,anne).
mother(mary,mike).
husband(costac,mary).
sex_male(costac).
sex_male(mike).
sex_female(mary).
sex_female(anne).
mother(X,Y):-
father(Z,Y),
wife(X,Z).
father(X,Y):-
mother(Z,Y),
husband(X,Z).
husband(X,Y):-
wife(Y,X).
wife(X,Y):-
husband(Y,X).
husband(X,Y):-
father(X,Z),
mother(Y,Z).
wife(X,Y):-
father(Y,Z),
mother(X,Z).
brother(X,Y):-
father(Z,Y),
father(Z,X),
sex_male(X),
\+ X = Y.
brother(X,Y):-
mother(Z,Y),
mother(Z,X),
sex_male(X),
\+ X = Y.
sister(X,Y):-
father(Z,X),
sex_female(X),
father(Z,Y),
\+ X = Y.
sister(X,Y):-
mother(Z,X),
sex_female(X),
mother(Z,Y),
\+ X = Y.
find_rel(Ans):-
write('Please give first name: '), read(X),
find(X,Ans).
find(X,Ans):-
write('Would you like to give second name? -yes/no-'), nl, read(Ask), find_ans(Ask,X,Ans).
find_ans(yes,X,Ans):-
write('Please give second name: '), read(Y),nl,
(father(X,Y),!, Ans = father(X,Y));
(mother(X,Y), !,Ans = mother(X,Y));
(husband(X,Y), !,Ans = husband(X,Y));
(wife(X,Y), !,Ans = wife(X,Y));
(brother(X,Y), !,Ans = brother(X,Y));
(sister(X,Y), Ans = sister(X,Y)).
/*find_ans(no,X,Ans):-....*/
我不关心任何其他答案,只关心这个问题的答案:
?-find_rel(Ans).
给出名字 costac 和 anne,我确实得到了我想要的,因为这是我规则的第一行。我尝试的其他一切都失败了,并给出了标题的错误!我尝试使用 cut,但没有任何效果。当我追踪时,例如我给玛丽和迈克,我看到的是,即使它进入步骤(mother(X,Y), !,Ans = mother(X,Y));
并产生mother(mary,anne)
忽略它,并进入接下来的两个并继续循环那个东西:
Call: (10) father(mary, mike) ? creep
Call: (11) mother(_G1479, mike) ? creep
Exit: (11) mother(mary, mike) ? creep
Call: (11) husband(mary, mary) ? creep
Call: (12) wife(mary, mary) ? creep
Call: (13) husband(mary, mary) ? creep
Call: (14) wife(mary, mary) ? creep
Call: (15) husband(mary, mary) ? creep
Call: (16) wife(mary, mary) ? creep
Call: (17) husband(mary, mary) ? creep
Call: (18) wife(mary, mary) ? creep
Call: (19) husband(mary, mary) ? creep
Call: (20) wife(mary, mary) ? creep
Call: (21) husband(mary, mary) ?