[Haskell-cafe] non-exhaustive pattern match(es)?

Chris Wong lambda.fairy at gmail.com
Tue Jun 10 23:26:57 UTC 2014


Hi there,

The problem is in these lines:

>     | i <  i'   = []
>     | i == i'   = []
>     | i >  i'   = []

Comparisons are defined by the library, not the language. There is no
way for the compiler to know they're mutually exclusive, short of
solving the halting problem. In fact, it is possible to write this:

instance Ord MyType where
    _ < _ = False
    _ == _ = False
    _ > _ = False

and your f will fail at run time.

The solution is to either replace your last comparison with
"otherwise" (since it must be True anyway), or use `compare`:

    f i (c:cs) ((i',t):os) = case i `compare` i' of
        LT -> ...
        EQ -> ...
        GT -> ...

Since these cases *are* mutually exclusive, it will pass the warning.

>
> main :: IO ()
> main = do print $ f 0 [] []
>
>
> without the "otherwise" clause (commented) I get the warning
>
> example.hs:2:1: Warning:
>     Pattern match(es) are non-exhaustive
>     In an equation for `f':
>         Patterns not matched: _ (_ : _) ((_, _) : _)
>
> Could someone help me uncover which cases that are not… erm… covered
> by the above patterns?
>
> Many thanks,
> Semen
>
> PS This definition, on the other hand,
>
> f _ cs     []          = cs
> f _ []     _           = []
> f i (c:cs) ((i',t):os) = []
>
> is exhaustive…
>
>
> --
> Семен Тригубенко http://trygub.com
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list