以下是相关代码:
married(X,Y) :- wife(X,Y);husband(X,Y).
parent(X,Y) :- father(X,Y) ;mother(X,Y).
brother(X,Y) :-
man(X),
parent(Z,X),
parent(Z,Y),
X \= Y.
brother_in_law(X,Y) :-
brother(X,Z),married(Z,Y).
我已经在Google上搜索了,看来其他人一直在使用兄弟the谓词的确切代码,所以应该没问题吗?我检查了其他谓词,它们看起来也不错。.不确定发生了什么。
另外,由于不工作,我的意思是当我检查时它没有承认相关的关系。
答案 0 :(得分:2)
查看跟踪,您将看到问题:
?- trace, brother_in_law(prins-daniel, Y).
Call: (9) brother_in_law(prins-daniel, _11346) ? creep
Call: (10) brother(prins-daniel, _11680) ? creep
Call: (11) man(prins-daniel) ? creep
Exit: (11) man(prins-daniel) ? creep
Call: (11) parent(_11678, prins-daniel) ? creep
Call: (12) father(_11678, prins-daniel) ? creep
Fail: (12) father(_11678, prins-daniel) ? creep
Redo: (11) parent(_11678, prins-daniel) ? creep
Call: (12) mother(_11678, prins-daniel) ? creep
Fail: (12) mother(_11678, prins-daniel) ? creep
Fail: (11) parent(_11678, prins-daniel) ? creep
Redo: (11) man(prins-daniel) ? creep
Fail: (11) man(prins-daniel) ? creep
Fail: (10) brother(prins-daniel, _11680) ? creep
Fail: (9) brother_in_law(prins-daniel, _11346) ? creep
false.
prins-daniel
的父亲是谁?你没有事实。 prins-daniel'
的母亲是谁?您也没有事实。结果,您找不到任何兄弟,因此查询失败。
这是否意味着您缺少事实或代码?代码中说,如果X有一个与Y结婚的兄弟Z,那么X和Y就是brother子。这是拥有a子的唯一方法吗?
旁注:prins-daniel
不是Prolog中的原子,就像Lisp中的原子一样。这是一个术语:
?- write_canonical(prins-daniel).
-(prins,daniel)
情况长期恶化:
?- write_canonical(johann-georg-av-hohenzollern).
-(-(-(johann,georg),av),hohenzollern)
需要注意的事情。