non-exhaustive pattern match(es)?

Dear Haskell Cafe, In the following program f :: Int -> [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)] f _ cs [] = cs f _ [] _ = [] f i (c:cs) ((i',t):os) | i < i' = [] | i == i' = [] | i > i' = [] -- | otherwise = [] 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

Hi, to the compiler, (<), (==) and (>) are just functions. It doesn't know that they have anything to do with each other. Its totality checking works solely on all alternatives given in data type declarations (separated by `|`s). Have a look at http://community.haskell.org/~ndm/catch/ for a try to go beyond that. Niklas On 11/06/14 00:17, Semen Trygubenko / Семен Тригубенко wrote:
Could someone help me uncover which cases that are not… erm… covered by the above patterns?

To propose an actual solution to your problem: You can use the `compare` function from Data.Ord. It returns a `data Ordering = LT | EQ | GT`, which you can pattern match on with a totality check.

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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Niklas and Chris: On Wed, Jun 11, 2014 at 11:26:57AM +1200, Chris Wong wrote:
The problem is in these lines:
| i < i' = [] | i == i' = [] | i > i' = []
Thank you for your replies — it all makes sense now! I arrived at the solution (addition of otherwise) by trial and error; the pattern in the last definition, namely, i (c :cs) ((i',t) :os) looks pretty exhaustive, so when the compiler issued a warning, proposing the addition of _ (_ : _) ((_, _) : _) , I scratched my head a bit before realizing the problem was not with the pattern, but with the comparisons further down, in the guards. And, of course, "patterns not matched" included many patterns that _were_ matched (in this case, everything :)). Wouldn't it be great if it was something like Patterns not matched: _ (_ : _) ((_, _) : _) | otherwise = instead? On Wed, Jun 11, 2014 at 12:17:49AM +0100, Semen Trygubenko / Семен Тригубенко wrote:
example.hs:2:1: Warning: Pattern match(es) are non-exhaustive In an equation for `f': Patterns not matched: _ (_ : _) ((_, _) : _)
-- Семен Тригубенко http://trygub.com
participants (3)
-
Chris Wong
-
Niklas Hambüchen
-
Semen Trygubenko / Семен Тригубен ко