[mercury-users] Starting to understand some/all

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Mar 2 22:40:55 AEDT 2003


On 02-Mar-2003, Douglas Auclair <dauclair at msn.com> wrote:
> Now I finally understand why Mercury doesn't have the list predicates of 
> any/2 and every/2!  I ran across a compilation error, however, and I need 
> some help.  The compiler complained that for:
> 
> some [X] (member(X, List) => p(X))
> 
> X was free and should have been bound under the negation.  I don't 
> understand the error (what negation?).

The goal

	some [X] (member(X, List) => p(X))

is treated the same as

	some [X] (not (member(X, List), not p(X)))

The negation which the compiler is referring to is the first of these
two negations.  This is a mode error because X occurs outside the
negation (in the `some'), but it gets bound by a goal inside the negation
(the call to member/2).

> My intention is to see of any 
> element of List passes p (which I think I'm doing by binding X for each of 
> the elements of List and passing that value to p).

In that case, you should use conjunction rather than
implication, e.g. `some [X] (member(X, List), p(X))'.

If you use implication, you are asking if there is some X for which
the implication "if X is a member of the list then p(X) is true" is true.
This implication will be true for any X which is not in the list.
For example, the statement `member(X, [1,2,3]) => X > 1' is true for
X = 1, but it is also true for X = -42.

Fortunately Mercury will report a mode error, rather than returning
that unintended answer!

> Am I using some incorrectly?

Well, you are using `some' correctly, but you are using implication
incorrectly.  If you are using `some', then generally you should use
conjunction rather than implication.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list