Non-exhaustive pattern match(es) warning in lambda functions

Christian Maeder Christian.Maeder at dfki.de
Thu Sep 23 08:19:20 EDT 2010


Am 23.09.2010 10:40, schrieb Simon Peyton-Jones:
> | For patterns in left hand sides (lhs) of let or where also no warnings
> | are issued.
> | 
> | data Foo = Bar Int | Baz
> | 
> | test3 :: Foo -> Int
> | test3 x = let Bar i = x in i
> | 
> | Will or should these cases be covered, too?
> 
> No, I don't plan to warn about these, which is the case at present.  It's quite common to have let-bindings that are only used sometimes, eg
> 
> f xs | null xs = blah
>      | otherwise = x+1
>      where
>        (x:_) = xs
> 
> I use this a lot.

There are two things that compete:
 1. Get warnings (or information) about possible pattern match errors
 2. Writing -Wall clean code

I suppose I can write:

  test2 = \ ~Bar -> return ()

to avoid the warning. But this style should not be encouraged.

I tend to introduce artificial error cases for "the Impossible", to
avoid warnings, although the compiler generated error messages are
better to locate the problem.

Clearly calling "head" or "tail" should not issue warnings, because you
have been warned when defining them (ignoring that someone else compiled
the Prelude for you).

You are also not warned when introducing partial field selectors,
though. (But for a data type with more than one variant it would more
interesting to now, if a selector happens to be total.)

C.

> | 
> | Cheers Christian
> | 
> | >
> | > S
> | >
> | > | -----Original Message-----
> | > | From: glasgow-haskell-users-bounces at haskell.org [mailto:glasgow-haskell-
> | > | users-bounces at haskell.org] On Behalf Of Mitar
> | > | Sent: 21 September 2010 07:44
> | > | To: glasgow-haskell-users
> | > | Subject: Non-exhaustive pattern match(es) warning in lambda functions
> | > |
> | > | Hi!
> | > |
> | > | Why GHC does not warn (with -Wall) about non-exhaustive pattern
> | > | match(es) warning in lambda functions? For example, this code:
> | > |
> | > | data Foo = Bar | Baz
> | > |
> | > | test1 :: Foo -> IO ()
> | > | test1 Bar = return () -- Pattern match(es) are non-exhaustive warning, OK
> | > |
> | > | test2 :: Foo -> IO ()
> | > | test2 = \Bar -> return () -- No pattern match(es) are non-exhaustive, BAD
> | > |
> | > | I think it would be quite useful to also catch such situations and
> | > | issue a warning.
> | > |
> | > |
> | > | Mitar


More information about the Glasgow-haskell-users mailing list