Counter-intuitive behavior of min_member/2

后端 未结 6 1438
一个人的身影
一个人的身影 2021-01-04 16:40

min_member(-Min, +List)

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         


        
6条回答
  •  醉酒成梦
    2021-01-04 17:04

    Clearly min_member/2 is not a true relation:

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

    yet, after simply exchanging the two goals by (highly desirable) commutativity of conjunction, we get:

    ?- X = 1, min_member(X, [X,0]).
    false.
    

    This is clearly quite bad, as you correctly observe.

    Constraints are a declarative solution for such problems. In the case of integers, finite domain constraints are a completely declarative solution for such problems.

    Without constraints, it is best to throw an instantiation error when we know too little to give a sound answer.

提交回复
热议问题