What does '{1}' mean in: Patterns not matched: p where p is not one of {1}

For the function

``````chain1 :: Integral a => a -> [a]
chain1 1 = [1]
chain1 n
| even n = n:chain1 (n `div` 2)
| odd n  = n:chain1 (n * 3 + 1)
``````

I’m getting the error

``````Pattern match(es) are non-exhaustive
In an equation for ‘chain1’:
Patterns not matched: p where p is not one of {1} compile(-Wincomplete-patterns)
``````

I haven’t been able to discover what `{1}` means.

I think it just means the singleton set with the number 1 in it, but I agree that it is confusing. See how the messages changes if you add `chain1 2 = [2,1]`:

``````Patterns not matched: p where p is not one of {1, 2}
``````

The problem here is that GHC doesn’t know that `even n` and `odd n` exhaustively cover all possible `n`. To fix it you can use `otherwise` instead of `odd n`:

``````chain1 n
| even n    = ...
| otherwise = ...
``````

Or you could use `quotRem` and a case expression:

``````chain1 n =
case quotRem n 2 of
(n', 0) -> n : chain1 n'
_       -> n : chain1 (n * 3 + 1)
``````
5 Likes