a trap for the unwary

Jan-Willem Maessen jmaessen@mit.edu
Fri, 1 Dec 2000 10:41:32 -0500


Malcolm Wallace writes:
> Quick quiz:  without running this through a compiler, who can spot
> the mistake?  :-)
> 
> > module Main where
> > import Char
> > f x = y
> >   where
> >     y | isSpace x = True
> >     y | otherwise = False    --  ** The problem line?
> > main = print (f 'x')

Without running this through the compiler, but based on similar
problems I've had recently, I'd assume the problem is the marked
line.  Two outer-level patterns are each presented with guards.  This
would be correct for a function definition:

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


This is a tricky issue.  I'd like the original program to be all
right.  We end up sowing confusion with erroneous programs like this
one:

> f x = y
>   where
>     y | otherwise = False    --  ** Now this pattern overlaps!
>     y | isSpace x = True

But of course an analogous problem occurs in the function definition,
and I think can be caught by turning on warnings in ghc.

-Jan-Willem Maessen
jmaessen@mit.edu