monads-tf-0.3.0.1: Monad classes, using type families
Copyright(c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001
(c) Jeff Newbern 2003-2006
(c) Andriy Palamarchuk 2006
LicenseBSD-style (see the file LICENSE)
Maintainerross@soi.city.ac.uk
Stabilityexperimental
Portabilitynon-portable (type families)
Safe HaskellSafe-Inferred
LanguageGHC2021

Control.Monad.Except

Description

Computation type:
Computations which may fail or throw exceptions.
Binding strategy:
Failure records information about the cause/location of the failure. Failure values bypass the bound function, other values are used as inputs to the bound function.
Useful for:
Building computations from sequences of functions that may fail or using exception handling to structure error handling.
Zero and plus:
Zero is represented by an empty error and the plus operation executes its second argument if the first fails.
Example type:
Either String a

The Error monad (also called the Exception monad).

Synopsis

Monads with error handling

class Monad m => MonadError m where Source #

The strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.

Is parameterized over the type of error information and the monad type constructor. It is common to use Either String as the monad type constructor for an error monad in which error descriptions take the form of strings. In that case and many other common cases the resulting monad is already defined as an instance of the MonadError class. You can also define your own a monad type constructor other than Either e, in which case you will have to explicitly define an instance of the MonadError class.

Associated Types

type ErrorType m Source #

Methods

throwError :: ErrorType m -> m a Source #

Is used within a monadic computation to begin exception processing.

catchError :: m a -> (ErrorType m -> m a) -> m a Source #

A handler function to handle previous errors and return to normal execution. A common idiom is:

do { action1; action2; action3 } `catchError` handler

where the action functions can call throwError. Note that handler and the do-block must have the same return type.

Instances

Instances details
MonadError IO Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType IO Source #

Methods

throwError :: ErrorType IO -> IO a Source #

catchError :: IO a -> (ErrorType IO -> IO a) -> IO a Source #

MonadError (Either e) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (Either e) Source #

Methods

throwError :: ErrorType (Either e) -> Either e a Source #

catchError :: Either e a -> (ErrorType (Either e) -> Either e a) -> Either e a Source #

MonadError m => MonadError (MaybeT m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (MaybeT m) Source #

Methods

throwError :: ErrorType (MaybeT m) -> MaybeT m a Source #

catchError :: MaybeT m a -> (ErrorType (MaybeT m) -> MaybeT m a) -> MaybeT m a Source #

Monad m => MonadError (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (ExceptT e m) Source #

Methods

throwError :: ErrorType (ExceptT e m) -> ExceptT e m a Source #

catchError :: ExceptT e m a -> (ErrorType (ExceptT e m) -> ExceptT e m a) -> ExceptT e m a Source #

MonadError m => MonadError (IdentityT m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (IdentityT m) Source #

MonadError m => MonadError (ReaderT r m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (ReaderT r m) Source #

Methods

throwError :: ErrorType (ReaderT r m) -> ReaderT r m a Source #

catchError :: ReaderT r m a -> (ErrorType (ReaderT r m) -> ReaderT r m a) -> ReaderT r m a Source #

MonadError m => MonadError (StateT s m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (StateT s m) Source #

Methods

throwError :: ErrorType (StateT s m) -> StateT s m a Source #

catchError :: StateT s m a -> (ErrorType (StateT s m) -> StateT s m a) -> StateT s m a Source #

MonadError m => MonadError (StateT s m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (StateT s m) Source #

Methods

throwError :: ErrorType (StateT s m) -> StateT s m a Source #

catchError :: StateT s m a -> (ErrorType (StateT s m) -> StateT s m a) -> StateT s m a Source #

(Monoid w, MonadError m) => MonadError (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (WriterT w m) Source #

Methods

throwError :: ErrorType (WriterT w m) -> WriterT w m a Source #

catchError :: WriterT w m a -> (ErrorType (WriterT w m) -> WriterT w m a) -> WriterT w m a Source #

(Monoid w, MonadError m) => MonadError (WriterT w m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (WriterT w m) Source #

Methods

throwError :: ErrorType (WriterT w m) -> WriterT w m a Source #

catchError :: WriterT w m a -> (ErrorType (WriterT w m) -> WriterT w m a) -> WriterT w m a Source #

(Monoid w, MonadError m) => MonadError (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (RWST r w s m) Source #

Methods

throwError :: ErrorType (RWST r w s m) -> RWST r w s m a Source #

catchError :: RWST r w s m a -> (ErrorType (RWST r w s m) -> RWST r w s m a) -> RWST r w s m a Source #

(Monoid w, MonadError m) => MonadError (RWST r w s m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (RWST r w s m) Source #

Methods

throwError :: ErrorType (RWST r w s m) -> RWST r w s m a Source #

catchError :: RWST r w s m a -> (ErrorType (RWST r w s m) -> RWST r w s m a) -> RWST r w s m a Source #

tryError :: MonadError m => m a -> m (Either (ErrorType m) a) Source #

MonadError analogue to the try function.

withError :: MonadError m => (ErrorType m -> ErrorType m) -> m a -> m a Source #

MonadError analogue to the withExceptT function. Modify the value (but not the type) of an error. The type is fixed because of the ErrorType type family.

The ExceptT monad transformer

newtype ExceptT e (m :: Type -> Type) a #

A monad transformer that adds exceptions to other monads.

ExceptT constructs a monad parameterized over two things:

  • e - The exception type.
  • m - The inner monad.

The return function yields a computation that produces the given value, while >>= sequences two subcomputations, exiting on the first exception.

Constructors

ExceptT (m (Either e a)) 

Instances

Instances details
MonadTrans (ExceptT e) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

lift :: Monad m => m a -> ExceptT e m a #

MonadFail m => MonadFail (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

fail :: String -> ExceptT e m a #

MonadFix m => MonadFix (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

mfix :: (a -> ExceptT e m a) -> ExceptT e m a #

MonadIO m => MonadIO (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftIO :: IO a -> ExceptT e m a #

MonadZip m => MonadZip (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

mzip :: ExceptT e m a -> ExceptT e m b -> ExceptT e m (a, b) #

mzipWith :: (a -> b -> c) -> ExceptT e m a -> ExceptT e m b -> ExceptT e m c #

munzip :: ExceptT e m (a, b) -> (ExceptT e m a, ExceptT e m b) #

Foldable f => Foldable (ExceptT e f) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

fold :: Monoid m => ExceptT e f m -> m #

foldMap :: Monoid m => (a -> m) -> ExceptT e f a -> m #

foldMap' :: Monoid m => (a -> m) -> ExceptT e f a -> m #

foldr :: (a -> b -> b) -> b -> ExceptT e f a -> b #

foldr' :: (a -> b -> b) -> b -> ExceptT e f a -> b #

foldl :: (b -> a -> b) -> b -> ExceptT e f a -> b #

foldl' :: (b -> a -> b) -> b -> ExceptT e f a -> b #

foldr1 :: (a -> a -> a) -> ExceptT e f a -> a #

foldl1 :: (a -> a -> a) -> ExceptT e f a -> a #

toList :: ExceptT e f a -> [a] #

null :: ExceptT e f a -> Bool #

length :: ExceptT e f a -> Int #

elem :: Eq a => a -> ExceptT e f a -> Bool #

maximum :: Ord a => ExceptT e f a -> a #

minimum :: Ord a => ExceptT e f a -> a #

sum :: Num a => ExceptT e f a -> a #

product :: Num a => ExceptT e f a -> a #

(Eq e, Eq1 m) => Eq1 (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftEq :: (a -> b -> Bool) -> ExceptT e m a -> ExceptT e m b -> Bool #

(Ord e, Ord1 m) => Ord1 (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftCompare :: (a -> b -> Ordering) -> ExceptT e m a -> ExceptT e m b -> Ordering #

(Read e, Read1 m) => Read1 (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (ExceptT e m a) #

liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [ExceptT e m a] #

liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (ExceptT e m a) #

liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [ExceptT e m a] #

(Show e, Show1 m) => Show1 (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> ExceptT e m a -> ShowS #

liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [ExceptT e m a] -> ShowS #

Contravariant m => Contravariant (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

contramap :: (a' -> a) -> ExceptT e m a -> ExceptT e m a' #

(>$) :: b -> ExceptT e m b -> ExceptT e m a #

Traversable f => Traversable (ExceptT e f) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

traverse :: Applicative f0 => (a -> f0 b) -> ExceptT e f a -> f0 (ExceptT e f b) #

sequenceA :: Applicative f0 => ExceptT e f (f0 a) -> f0 (ExceptT e f a) #

mapM :: Monad m => (a -> m b) -> ExceptT e f a -> m (ExceptT e f b) #

sequence :: Monad m => ExceptT e f (m a) -> m (ExceptT e f a) #

(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

empty :: ExceptT e m a #

(<|>) :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a #

some :: ExceptT e m a -> ExceptT e m [a] #

many :: ExceptT e m a -> ExceptT e m [a] #

(Functor m, Monad m) => Applicative (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

pure :: a -> ExceptT e m a #

(<*>) :: ExceptT e m (a -> b) -> ExceptT e m a -> ExceptT e m b #

liftA2 :: (a -> b -> c) -> ExceptT e m a -> ExceptT e m b -> ExceptT e m c #

(*>) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b #

(<*) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m a #

Functor m => Functor (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

fmap :: (a -> b) -> ExceptT e m a -> ExceptT e m b #

(<$) :: a -> ExceptT e m b -> ExceptT e m a #

Monad m => Monad (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

(>>=) :: ExceptT e m a -> (a -> ExceptT e m b) -> ExceptT e m b #

(>>) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b #

return :: a -> ExceptT e m a #

(Monad m, Monoid e) => MonadPlus (ExceptT e m) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

mzero :: ExceptT e m a #

mplus :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a #

MonadCont m => MonadCont (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Cont.Class

Methods

callCC :: ((a -> ExceptT e m b) -> ExceptT e m a) -> ExceptT e m a Source #

Monad m => MonadError (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Except.Class

Associated Types

type ErrorType (ExceptT e m) Source #

Methods

throwError :: ErrorType (ExceptT e m) -> ExceptT e m a Source #

catchError :: ExceptT e m a -> (ErrorType (ExceptT e m) -> ExceptT e m a) -> ExceptT e m a Source #

MonadRWS m => MonadRWS (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.RWS.Class

MonadReader m => MonadReader (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Reader.Class

Associated Types

type EnvType (ExceptT e m) Source #

Methods

ask :: ExceptT e m (EnvType (ExceptT e m)) Source #

local :: (EnvType (ExceptT e m) -> EnvType (ExceptT e m)) -> ExceptT e m a -> ExceptT e m a Source #

MonadState m => MonadState (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.State.Class

Associated Types

type StateType (ExceptT e m) Source #

Methods

get :: ExceptT e m (StateType (ExceptT e m)) Source #

put :: StateType (ExceptT e m) -> ExceptT e m () Source #

MonadWriter m => MonadWriter (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Writer.Class

Associated Types

type WriterType (ExceptT e m) Source #

Methods

tell :: WriterType (ExceptT e m) -> ExceptT e m () Source #

listen :: ExceptT e m a -> ExceptT e m (a, WriterType (ExceptT e m)) Source #

pass :: ExceptT e m (a, WriterType (ExceptT e m) -> WriterType (ExceptT e m)) -> ExceptT e m a Source #

(Read e, Read1 m, Read a) => Read (ExceptT e m a) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

readsPrec :: Int -> ReadS (ExceptT e m a) #

readList :: ReadS [ExceptT e m a] #

readPrec :: ReadPrec (ExceptT e m a) #

readListPrec :: ReadPrec [ExceptT e m a] #

(Show e, Show1 m, Show a) => Show (ExceptT e m a) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

showsPrec :: Int -> ExceptT e m a -> ShowS #

show :: ExceptT e m a -> String #

showList :: [ExceptT e m a] -> ShowS #

(Eq e, Eq1 m, Eq a) => Eq (ExceptT e m a) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

(==) :: ExceptT e m a -> ExceptT e m a -> Bool #

(/=) :: ExceptT e m a -> ExceptT e m a -> Bool #

(Ord e, Ord1 m, Ord a) => Ord (ExceptT e m a) 
Instance details

Defined in Control.Monad.Trans.Except

Methods

compare :: ExceptT e m a -> ExceptT e m a -> Ordering #

(<) :: ExceptT e m a -> ExceptT e m a -> Bool #

(<=) :: ExceptT e m a -> ExceptT e m a -> Bool #

(>) :: ExceptT e m a -> ExceptT e m a -> Bool #

(>=) :: ExceptT e m a -> ExceptT e m a -> Bool #

max :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a #

min :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a #

type ErrorType (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Except.Class

type ErrorType (ExceptT e m) = e
type EnvType (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Reader.Class

type EnvType (ExceptT e m) = EnvType m
type StateType (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.State.Class

type StateType (ExceptT e m) = StateType m
type WriterType (ExceptT e m) Source # 
Instance details

Defined in Control.Monad.Writer.Class

runExceptT :: ExceptT e m a -> m (Either e a) #

The inverse of ExceptT.

mapExceptT :: (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n b #

Map the unwrapped computation using the given function.

withExceptT :: forall (m :: Type -> Type) e e' a. Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a #

Transform any exceptions thrown by the computation using the given function.

Example 1: Custom Error Data Type

Here is an example that demonstrates the use of a custom Error data type with the throwError and catchError exception mechanism from MonadError. The example throws an exception if the user enters an empty string or a string longer than 5 characters. Otherwise it prints length of the string.

import Control.Monad.Except

-- This is the type to represent length calculation error.
data LengthError = EmptyString  -- Entered string was empty.
          | StringTooLong Int   -- A string is longer than 5 characters.
                                -- Records a length of the string.
          | OtherError String   -- Other error, stores the problem description.

-- Converts LengthError to a readable message.
instance Show LengthError where
  show EmptyString = "The string was empty!"
  show (StringTooLong len) =
      "The length of the string (" ++ (show len) ++ ") is bigger than 5!"
  show (OtherError msg) = msg

-- For our monad type constructor, we use Either LengthError
-- which represents failure using Left LengthError
-- or a successful result of type a using Right a.
type LengthMonad = Either LengthError

main = do
  putStrLn "Please enter a string:"
  s <- getLine
  reportResult (calculateLengthOrFail s)

-- Attempts to calculate length and throws an error if the provided string is
-- empty or longer than 5 characters.
-- The processing is done in Either monad.
calculateLengthOrFail :: String -> LengthMonad Int
calculateLengthOrFail [] = throwError EmptyString
calculateLengthOrFail s | len > 5 = throwError (StringTooLong len)
                        | otherwise = return len
  where len = length s

-- Prints result of the string length calculation.
reportResult :: LengthMonad Int -> IO ()
reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))
reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))

Example 2: Using ExceptT Monad Transformer

ErrorT monad transformer can be used to add error handling to another monad. Here is an example how to combine it with an IO monad:

import Control.Monad.Except

-- An IO monad which can return String failure.
-- It is convenient to define the monad type of the combined monad,
-- especially if we combine more monad transformers.
type LengthMonad = ExceptT String IO

main = do
  -- runExceptT removes the ExceptT wrapper
  r <- runExceptT calculateLength
  reportResult r

-- Asks user for a non-empty string and returns its length.
-- Throws an error if user enters an empty string.
calculateLength :: LengthMonad Int
calculateLength = do
  -- all the IO operations have to be lifted to the IO monad in the monad stack
  liftIO $ putStrLn "Please enter a non-empty string: "
  s <- liftIO getLine
  if null s
    then throwError "The string was empty!"
    else return $ length s

-- Prints result of the string length calculation.
reportResult :: Either String Int -> IO ()
reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))
reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))