a trap for the unwary

Malcolm Wallace Malcolm.Wallace@cs.york.ac.uk
Fri, 1 Dec 2000 15:44:16 +0000


> > > f x = y
> > >   where
> > >     y | isSpace x = True
> > >     y | otherwise = False    --  ** The problem line?

Correct.  Here y is a pattern binding, and multiple pattern bindings of
the same variable are not permitted.

> f x = y ()
>   where
>     y _ | isSpace x = True
>     y _ | otherwise = False    --  ** Does this work?

Correct. Here y is a function binding instead, and multiple clauses
*are* permitted.

> I'd like the original program to be all right. 

Me too.  I wrote 'y' as a 0-arity function, knowing that because
it used a free variable bound at an outer scope, it would probably
be lambda-lifted to a greater arity by the compiler.  But only one
compiler saw it in the same way as I did. :-)

Of course, if the pattern binding is more complex than a single
variable name, I still want the no-multiple-bindings rule to apply
as usual:

> f x = y ()
>   where
>     (y:_) | isSpace x = [True]
>     (y:_) | otherwise = [False]    --  ** Definitely wrong

and indeed all compilers reject this, as they should.

Regards,
    Malcolm