min_member/2的违反直觉的行为 [英] Counter-intuitive behavior of min_member/2

查看:67
本文介绍了min_member/2的违反直觉的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最小成员(-最小,+ List )

当Min是标准术语中的最小成员时为真.如果列表为空,则失败.

True when Min is the smallest member in the standard order of terms. Fails if List is empty.

?- min_member(3, [1,2,X]).
X = 3.

当然,这是因为变量以标准的术语顺序排在所有其他术语之前,并且使用了统一.但是,报告的解决方案感觉有点不对.

The explanation is of course that variables come before all other terms in the standard order of terms, and unification is used. However, the reported solution feels somehow wrong.

如何证明其合理性?我应该如何解释这种解决方案?

How can it be justified? How should I interpret this solution?

防止此解决方案成功的一种方法是更改​​标准库(SWI-Prolog)

One way to prevent min_member/2 from succeeding with this solution is to change the standard library (SWI-Prolog) implementation as follows:

xmin_member(Min, [H|T]) :-
    xmin_member_(T, H, Min).

xmin_member_([], Min0, Min) :-
    (   var(Min0), nonvar(Min)
    ->  fail
    ;   Min = Min0
    ).
xmin_member_([H|T], Min0, Min) :-
    (   H @>= Min0 
    ->  xmin_member_(T, Min0, Min)
    ;   xmin_member_(T, H, Min)
    ).

失败而不是引发实例化错误(如果我正确理解的话,@ mat在他的答案中提出的建议)的原因是,这是一个明确的问题:

The rationale behind failing instead of throwing an instantiation error (what @mat suggests in his answer, if I understood correctly) is that this is a clear question:

当X是自由变量时,3是[1,2,X]的最小成员吗?"

"Is 3 the minimum member of [1,2,X], when X is a free variable?"

对此的答案(至少对我来说)是明确的否",而不是我不能真正说出来."

and the answer to this is (to me at least) a clear "No", rather than "I can't really tell."

这是与sort/2相同的行为类别:

This is the same class of behavior as sort/2:

?- sort([A,B,C], [3,1,2]).
A = 3,
B = 1,
C = 2.

同样的技巧也适用:

?- min_member(3, [1,2,A,B]).
A = 3.

?- var(B), min_member(3, [1,2,A,B]).
B = 3.

推荐答案

这是许多(全部?)谓词的共同属性,取决于条件的标准顺序,而两个条件之间的顺序在统一后可以更改.基线是下面的连接词,也不能还原:

This is a common property of many (all?) predicates that depend on the standard order of terms, while the order between two terms can change after unification. Baseline is the conjunction below, which cannot be reverted either:

?- X @< 2, X = 3.
X = 3.

大多数谓词使用-Value注释的谓词都说pred(Value)是相同的 如pred(Var), Value = Var.这是另一个示例:

Most predicates using a -Value annotation for an argument say that pred(Value) is the same as pred(Var), Value = Var. Here is another example:

?- sort([2,X], [3,2]).
X = 3.

仅当输入为 ground 时,这些谓词才表示干净关系.尽管由于用户可以意识到自己不应该进一步实例化任何排序的术语,但是因为可以将它们与变量有效地结合使用,所以要求将输入置于地面是太多了.从这个意义上说,我不同意@mat.我确实同意约束确实可以使其中的某些关系听起来很合理.

These predicates only represent clean relations if the input is ground. It is too much to demand the input to be ground though because they can be meaningfully used with variables, as long as the user is aware that s/he should not further instantiate any of the ordered terms. In that sense, I disagree with @mat. I do agree that constraints can surely make some of these relations sound.

这篇关于min_member/2的违反直觉的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆