Prolog:成员谓词的真/假输出? [英] Prolog: true / false output of member predicate?
问题描述
我想知道为什么 Prolog 的 member/2
谓词提供了多种选择(通过回溯?!),如果 true 已经统一用于输出.
I am wondering why the member/2
predicate of Prolog delivers multiple alternatives (via backtracking?!), if true was already unified for the output.
例如 member(1, [1,2,3]).
提供以下输出:
For example member(1, [1,2,3]).
delivers the following output:
true ;
false.
为什么member
在已经发现原子1
确实是列表[1,2,3]
的成员后返回false代码>?
Why does member
return false after it already found out that the atom 1
is indeed a member of the list [1,2,3]
?
更令我困惑的是以下输出:
Even more confusing to me is the following output:
?- member(1, [1,2,3,1]).
true ;
true.
推荐答案
在你的第一个例子中,你要求它证明 member(1,[1,2,3]).
;因为它可以,所以它报告 true
.当您输入 ;
时,您要求找到另一种方法来证明该查询;因为它不能,所以它报告 false
.
In your first example, you ask it to prove member(1,[1,2,3]).
; since it can, it reports true
. When you enter ;
, you are asking to find another way to prove that query; since it cannot, it reports false
.
在第二种情况下,第一个 true
是因为它在列表中找到了 1
之一;第二个是因为它找到了第二个.如果您再次点击 ;
,它会返回 false
,因为它没有其他方法来证明查询.(注意:正如@WillNess 指出的那样,您实际上没有机会再次点击 ;
;这可能是由于 member
的实现使得 Prolog 知道没有其他选择.如果列表没有以 1 结尾,您将能够再次点击 ;
.)
In the second case, the first true
is because it found one of the 1
's in the list; the second is because it found the second. If you had hit ;
again, it would have come back with false
, as it had no other ways to prove the query. (Note: As @WillNess points out, you don't actually get the chance to hit ;
again; this is probably due to the implementation of member
being such that Prolog knows there are no remaining alternatives. If the list did not end with a 1, you would be able to hit ;
again.)
这篇关于Prolog:成员谓词的真/假输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!