Maintainer | CS 131, Programming Languages (Melissa O'Neill, Chris Stone, Ben Wiedermann) This module provides a simple monad for handling computations that can fail with an error. For example: Fallible a is either - a thing (of type a) or - a failure with an error message Unlike Haskell's error function, for computation to continue, Haskell must *know* whether the previous computation failed or not. Thus let err = error "Oops" in 42 will produce 42 without ever showing an error, in contrast do err <- fail "Oops" return 42 will fail and never even try to compute 42. On the negative side, it does impose *some* sequencing, in that let a = foo "Wow" b = bar 42 in [b,a] will produce a list, then compute b if it is needed, then a if it is needed. The *same* would occur if we wrote: do a <- return (foo "Wow") b <- return (bar 42) return [b,a] but if foo and bar return Fallible, if we write do a <- foo "Wow" b <- bar 42 return [b,a] Haskell must first evaluate foo *enough* to know whether or not it failed, and then, if it succeeded, evaluate bar enough to know whether it failed, and then if that succeeded, return a and b. a and b themselves may not yet have been computed (due to laziness) but we at least know that we have a "successful" result. |
---|---|
Safe Haskell | Safe |
Documentation
A Fallible can succeed with a result or fail with a message
fail2error :: Fallible a -> a #
Pull the result out of the Fallible or display an error message.