Haskell pattern matching too strict?

Robert F. Pointon rpointon at cee.hw.ac.uk
Tue Jan 9 11:02:48 EST 2001


On Tue, 9 Jan 2001, Malcolm Wallace wrote:

> > -- broken fragment
> > instance (Typeable a, Typeable b) => Typeable (a,b) where
> >   typeOf (x,y) = "("++(typeOf x)++ ","++(typeOf y)++")"
> > --
> 
> > Is this too strict? It never uses the tuple or its arguments yet
> > tries to construct the tuple anyway.
> 
> Yes, this is too strict, but no, Haskell is correctly doing what you
> asked for here.  When you write
> 
>     f (x,y) = ...
> 
> the pattern must be matched before the system can decide which clause
> of the function definition to commit to.  Hence, it must evaluate
> the tuple as far as the (,) constructor, to be sure that it actually
> has one.

I agree in general, however in this case there is only one clause so it
does not need to evaluate to decide which clause to commit to. Ideally
the compiler should detect this specific situation where there is one
pattern that always matches.

cheers,
___                 
][_)              | +44 (0)131 451 3328
]| \obert Pointon | http://www.cee.hw.ac.uk/~rpointon
------------------+------------------------------
Dept of Computing & Electrical Eng.
Heriot-Watt University
Riccarton
Edinburgh, EH14 4AS






More information about the Glasgow-haskell-bugs mailing list