When I compile the following code with GHC (using the -Wall flag):
module Main where
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
insert :: (Ord a) => a -> Tree a -> Tree a
insert x EmptyTree = Node x EmptyTree EmptyTree
insert x (Node a left right)
| x == a = Node a left right
| x < a = Node a (insert x left) right
| x > a = Node a left (insert x right)
main :: IO()
main = do
let nums = [1..10]::[Int]
print . foldr insert EmptyTree $ nums
GHC complains that pattern matching in insert is non-exhaustive:
test.hs|6| 1:
|| Warning: Pattern match(es) are non-exhaustive
|| In an equation for `insert': Patterns not matched: _ (Node _ _ _)
Why is GHC issuing this warning? It is pretty obvious that the pattern GHC complains about is handled in insert x (Node a left right).
Riccardo is correct, GHC doesn't infer that your guards can't possibly all be false. So accept his answer please.
I'm going to digress and talk about coding style.
Your motivation for not using
otherwisemay have been that it looks unsightly:Looking at this code, a human reader must confirm to themselves that the final guard accepts precisely those cases where
x > a.We could instead write it like this:
The
Orderingtype returned bycomparehas only the three valuesEQ,LT, andGT, so GHC can confirm that you've covered all possibilities, and a human reader can easily see that you've covered them correctly.This is also more efficient code: we call
compareonce, instead of calling==and then probably calling<as well.Now I'm going to digress some more and talk about laziness.
You've probably also written a function similar to this:
When
x == a, you need to know that the tree uses theNodeconstructor, and that its first argument is equal tox. You don't need to know what either of the subtrees are.But now look back at my definition of
insertabove. When the tree it's given is aNode, it always returns aNodewhose first argument is alwaysa. But it doesn't state that up front: instead it evaluatesx `compare` a.We can rewrite
insertto perform the comparison as late as possible:Now we return the
Node abit as soon as possible --- even if the comparison throws an error! --- and we still perform the comparison once at most.