MaintainerCS 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 HaskellSafe

Fallible

Description

 

Synopsis

Documentation

data Fallible a #

A Fallible can succeed with a result or fail with a message

Constructors

Result a 
Failure String 

Instances

Monad Fallible # 

Methods

(>>=) :: Fallible a -> (a -> Fallible b) -> Fallible b

(>>) :: Fallible a -> Fallible b -> Fallible b

return :: a -> Fallible a

fail :: String -> Fallible a

Functor Fallible # 

Methods

fmap :: (a -> b) -> Fallible a -> Fallible b

(<$) :: a -> Fallible b -> Fallible a

MonadFail Fallible # 

Methods

fail :: String -> Fallible a

Applicative Fallible # 

Methods

pure :: a -> Fallible a

(<*>) :: Fallible (a -> b) -> Fallible a -> Fallible b

(*>) :: Fallible a -> Fallible b -> Fallible b

(<*) :: Fallible a -> Fallible b -> Fallible a

MonadPlus Fallible # 

Methods

mzero :: Fallible a

mplus :: Fallible a -> Fallible a -> Fallible a

Alternative Fallible # 

Methods

empty :: Fallible a

(<|>) :: Fallible a -> Fallible a -> Fallible a #

some :: Fallible a -> Fallible [a] #

many :: Fallible a -> Fallible [a] #

Eq a => Eq (Fallible a) # 

Methods

(==) :: Fallible a -> Fallible a -> Bool

(/=) :: Fallible a -> Fallible a -> Bool

Ord a => Ord (Fallible a) # 

Methods

compare :: Fallible a -> Fallible a -> Ordering

(<) :: Fallible a -> Fallible a -> Bool

(<=) :: Fallible a -> Fallible a -> Bool

(>) :: Fallible a -> Fallible a -> Bool

(>=) :: Fallible a -> Fallible a -> Bool

max :: Fallible a -> Fallible a -> Fallible a

min :: Fallible a -> Fallible a -> Fallible a

Show a => Show (Fallible a) # 

Methods

showsPrec :: Int -> Fallible a -> ShowS

show :: Fallible a -> String

showList :: [Fallible a] -> ShowS

fail2error :: Fallible a -> a #

Pull the result out of the Fallible or display an error message.

catchfail :: (String -> a) -> Fallible a -> a #

Handle a failure (or produce the result if the computation succeeded).