mtl-2.2.2: Monad classes, using functional dependencies

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)
Maintainerlibraries@haskell.org
Stabilityexperimental
Portabilitynon-portable (multi-parameter type classes)
Safe HaskellSafe
LanguageHaskell2010

Control.Monad.Except

Contents

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.
Example type:
Either String a

The Error monad (also called the Exception monad).

Since: 2.2.1

Synopsis

Warning

Please do not confuse ExceptT and throwError with Exception / SomeException and catch, respectively. The latter are for exceptions built into GHC, by default, and are mostly used from within the IO monad. They do not interact with the "exceptions" in this package at all. This package allows you to define a new kind of exception control mechanism which does not necessarily need your code to be placed in the IO monad.

In short, all "catching" mechanisms in this library will be unable to catch exceptions thrown by functions in the Control.Exception module, and vice-versa.

class Monad m => MonadError e m | m -> e 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 error type and/or use a monad type constructor other than Either String or Either IOError. In these cases you will have to explicitly define instances of the MonadError class. (If you are using the deprecated Control.Monad.Error or Control.Monad.Trans.Error, you may also have to define an Error instance.)

Minimal complete definition

throwError, catchError

Methods

throwError :: e -> m a Source #

Is used within a monadic computation to begin exception processing.

catchError :: m a -> (e -> 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
MonadError () Maybe Source #

Since: 2.2.2

Instance details

Methods

throwError :: () -> Maybe a Source #

catchError :: Maybe a -> (() -> Maybe a) -> Maybe a Source #

MonadError IOException IO Source # 
Instance details

Methods

throwError :: IOException -> IO a Source #

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

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

Methods

throwError :: e -> MaybeT m a Source #

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

MonadError e m => MonadError e (ListT m) Source # 
Instance details

Methods

throwError :: e -> ListT m a Source #

catchError :: ListT m a -> (e -> ListT m a) -> ListT m a Source #

MonadError e (Either e) Source # 
Instance details

Methods

throwError :: e -> Either e a Source #

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

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

Methods

throwError :: e -> WriterT w m a Source #

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

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

Methods

throwError :: e -> WriterT w m a Source #

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

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

Methods

throwError :: e -> StateT s m a Source #

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

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

Methods

throwError :: e -> StateT s m a Source #

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

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

Methods

throwError :: e -> IdentityT m a Source #

catchError :: IdentityT m a -> (e -> IdentityT m a) -> IdentityT m a Source #

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

Since: 2.2

Instance details

Methods

throwError :: e -> ExceptT e m a Source #

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

(Monad m, Error e) => MonadError e (ErrorT e m) Source # 
Instance details

Methods

throwError :: e -> ErrorT e m a Source #

catchError :: ErrorT e m a -> (e -> ErrorT e m a) -> ErrorT e m a Source #

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

Methods

throwError :: e -> ReaderT r m a Source #

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

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

Methods

throwError :: e -> RWST r w s m a Source #

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

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

Methods

throwError :: e -> RWST r w s m a Source #

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

liftEither :: MonadError e m => Either e a -> m a Source #

Lifts an Either e into any MonadError e.

do { val <- liftEither =<< action1; action2 }

where action1 returns an Either to represent errors.

Since: 2.2.2

The ExceptT monad transformer

newtype ExceptT e (m :: * -> *) 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
MonadRWS r w s m => MonadRWS r w s (ExceptT e m) Source #

Since: 2.2

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

Since: 2.2

Instance details

Methods

throwError :: e -> ExceptT e m a Source #

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

MonadReader r m => MonadReader r (ExceptT e m) Source #

Since: 2.2

Instance details

Methods

ask :: ExceptT e m r Source #

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

reader :: (r -> a) -> ExceptT e m a Source #

MonadState s m => MonadState s (ExceptT e m) Source #

Since: 2.2

Instance details

Methods

get :: ExceptT e m s Source #

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

state :: (s -> (a, s)) -> ExceptT e m a Source #

MonadWriter w m => MonadWriter w (ExceptT e m) Source #

Since: 2.2

Instance details

Methods

writer :: (a, w) -> ExceptT e m a Source #

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

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

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

MonadTrans (ExceptT e) 
Instance details

Methods

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

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

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 #

fail :: String -> ExceptT e m a #

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

Methods

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

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

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

Methods

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

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

Methods

fail :: String -> ExceptT e m a #

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

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 #

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

Methods

fold :: Monoid m => ExceptT e f m -> 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 #

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

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) #

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

Methods

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

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

Methods

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

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

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

Methods

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

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

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

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) #

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

Methods

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

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

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] #

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

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 #

Since: 2.2

Instance details

Methods

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

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

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

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 #

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

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

Methods

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

show :: ExceptT e m a -> String #

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

type Except e = ExceptT e Identity #

The parameterizable exception monad.

Computations are either exceptions or normal values.

The return function returns a normal value, while >>= exits on the first exception. For a variant that continues after an error and collects all the errors, see Errors.

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 :: Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a #

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

runExcept :: Except e a -> Either e a #

Extractor for computations in the exception monad. (The inverse of except).

mapExcept :: (Either e a -> Either e' b) -> Except e a -> Except e' b #

Map the unwrapped computation using the given function.

withExcept :: (e -> e') -> Except e a -> Except e' a #

Transform any exceptions thrown by the computation using the given function (a specialization of withExceptT).

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.

-- 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 (calculateLength s)

-- Attempts to calculate length and throws an error if the provided string is
-- empty or longer than 5 characters.
-- (Throwing an error in this monad means returning a 'Left'.)
calculateLength :: String -> LengthMonad Int
calculateLength [] = throwError EmptyString
calculateLength 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

ExceptT 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))