Search code examples
haskellghcinon-exhaustive-patterns

Disable "Non-exhaustive patterns in case" in GHCI


I am reading the paper "Monad Transformers Step by Step" and making my way through the examples.

In the eval0 example, there is an intentional non-exhaustive pattern in a case expression:

eval0 :: Env -> Exp -> Value
eval0 env (Lit i) = IntVal i
eval0 env (Var n) = fromJust (Map.lookup n env)
eval0 env (Plus e1 e2) = let IntVal i1 = eval0 env e1
                             IntVal i2 = eval0 env e2
                         in IntVal (i1 + i2)
eval0 env (Abs n e) = FunVal env n e
eval0 env (App e1 e2) = let val1 = eval0 env e1
                            val2 = eval0 env e2
                        in case val1 of
                          FunVal env' n body ->
                            eval0 (Map.insert n val2 env') body

Is there any way to suppress the exception "Non-exhaustive patterns in case" without adding a

_ -> error "Error"

to the end of the case? Later examples in the paper show how to handle this situation using the ErrorT monad transformer.


Solution

  • Yes indeed! Just add the appropriate pragma at the top of your file to suppress the undesired warnings.

    {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
    

    You can find a complete list (I think) of how to turn off individual warnings like so

    bash> man ghc | grep no-warn
    

    Obviously, you can also pass -fno-warn-incomplete-patterns when you start up ghci

    bash> ghci -Wall -fno-warn-incomplete-patterns myfile.hs