为什么这个 prolog 查询既是真的又是假的? [英] Why is this prolog query both true and false?
问题描述
我的 SWI-Prolog 知识库包含以下两个事实:
My SWI-Prolog knowledge base contains the following two facts:
f(a,b).
f(a,c).
现在如果我提出查询
?- f(a,c).
true.
但是
?- f(a,b).
true ;
false.
为什么 f(a,b) 既为真又为假?当知识库中有三个事实时,也会发生这种情况.如果我附加 f(a,d).到 KB,则 f(a,d) 为真(仅),但 f(a,b) 和 f(a,c) 既为真又为假.发生了什么,我该怎么做才能使 Prolog(仅)对这些查询做出正确的回答?
Why is f(a,b) both true and false? This also happens when there are three facts in the KB. If I append f(a,d). to the KB, then f(a,d) is true (only), but f(a,b) and f(a,c) are both true and false. What's going on, and what can I do so that Prolog answers (only) true to these queries?
推荐答案
(注:这个答案有点猜测)
(Note: this answer is somewhat of a guess)
考虑 Prolog 如何确定 f(a,c)
是否为真.它检查第一条规则 f(a,b)
,但没有找到匹配项,但第二条规则 f(a,c)
匹配.因此,f(a,c)
为真.此外,由于 f
没有更多规则,因此没有必要允许回溯发生——没有其他可能的解决方案.
Consider how Prolog determines whether f(a,c)
is true or not. It checks the first rule, f(a,b)
, and doesn't find a match, but the second rule, f(a,c)
matches. Therefore, f(a,c)
is true. Furthermore, since there are no more rules for f
, there is no point in allowing a backtrack to occur -- there are no other possible solutions.
现在考虑f(a,b)
.Prolog 将检查第一条规则,并找到匹配项.因此,f(a,b)
为真.但是,并非所有规则都已用尽.因此,Prolog 将允许继续搜索(如果您点击 ;
).当你继续搜索和回溯时,它会发现剩余的规则,特别是 f(a,c)
,与 f(a,b)
不匹配.因此,结果为假.
Now consider f(a,b)
. Prolog will check the first rule, and find a match. Therefore, f(a,b)
is true. However, not all rules have been exhausted. Therefore, Prolog will allow the search to continue (if you hit ;
). When you do continue the search and backtrack, it will discover that the remaining rules, specifically f(a,c)
, do not match f(a,b)
. Therefore, the result is false.
这篇关于为什么这个 prolog 查询既是真的又是假的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!