Handling Error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
data Uncertain a = Fail String | Result a
    
instance Monad Uncertain where
    return = Result
    Fail s >>= _ = Fail s
    Result r >>= k = k r
    fail s = Fail s

    
main :: IO ()
main = do 
    putStrLn $ case eval testExpr of
            Fail msg -> "Error: " ++ msg
            Result result -> show result
 
7:5: Error: Eta reduce
Found:
fail s = Fail s
Why not:
fail = Fail
11:8: Error: Redundant do
Found:
do putStrLn $
case eval testExpr of
Fail msg -> "Error: " ++ msg
Result result -> show result
Why not:
putStrLn $
case eval testExpr of
Fail msg -> "Error: " ++ msg
Result result -> show result