Exhaustive Pattern-Matching

Ketil Z. Malde ketil@ii.uib.no
29 Aug 2003 15:58:08 +0200


Christian Maeder <maeder@tzi.de> writes:

> Indeed, I always try to avoid all warnings in my sources by using the
> flag "-Wall", because I consider this to be good programming
> style. (In particular warnings about unused and shadowed variables
> prevented a lot of errors.) However some warnings are difficult to
> avoid. So how difficult would it be to implement non-exhaustive
> pattern warnings for nested patterns?

> data Color = Red | Green | Blue

> f :: Color -> String
> f x = case x of
>        Red -> "r"
>        _   -> " " ++ case x of
> 	            Green -> "g"
> 	            Blue  -> "b"

One way to do it, is to add

                    _ -> error "This can never happen"

I do this occasionally, and it catches some errors or mistaken
assumptions on my part every now and then.

(Perhaps one could even have a special funcition, "impossible", say,
that would tell the compiler that a particular branch would never be
taken.  In case the compiler is smart enough to figure it out, and
issue a warning for it -- it would know not to bother.)

Or one might wish for some kind of pragma to turn off this warning
selectively for a block of code.

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants