Control.Monad.Trans.Except

transformers-0.5.1.0: Concrete functor and monad transformers

Copyright | (C) 2013 Ross Paterson
License | BSD-style (see the file LICENSE)
Maintainer | R.Paterson@city.ac.uk
Stability | experimental
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Control.Monad.Trans.Except

Contents

- The Except monad
- The ExceptT monad transformer
- Exception operations
- Lifting other operations

Description

This monad transformer extends a monad with the ability throw exceptions.

A sequence of actions terminates normally, producing a value, only if none of the actions in the sequence throws an exception. If one throws an exception, the rest of the sequence is skipped and the composite action exits with that exception.

If the value of the exception is not required, the variant in Control.Monad.Trans.Maybe may be used instead.

Synopsis

- type Except e = ExceptT e Identity
- except :: Either e a -> Except e a
- runExcept :: Except e a -> Either e a
- mapExcept :: (Either e a -> Either e' b) -> Except e a -> Except e' b
- withExcept :: (e -> e') -> Except e a -> Except e' a
- newtype ExceptT e m a = ExceptT (m (Either e a))
- runExceptT :: ExceptT e m a -> m (Either e a)
- mapExceptT :: (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n b
- withExceptT :: Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a
- throwE :: Monad m => e -> ExceptT e m a
- catchE :: Monad m => ExceptT e m a -> (e -> ExceptT e' m a) -> ExceptT e' m a
- liftCallCC :: CallCC m (Either e a) (Either e b) -> CallCC (ExceptT e m) a b
- liftListen :: Monad m => Listen w m (Either e a) -> Listen w (ExceptT e m) a
- liftPass :: Monad m => Pass w m (Either e a) -> Pass w (ExceptT e m) a

# The Except monad

type Except e = ExceptT e Identity Source

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`.

except :: Either e a -> Except e a Source

Constructor for computations in the exception monad. (The inverse of `runExcept`).

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

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

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

Map the unwrapped computation using the given function.

-     runExcept (mapExcept f m) = f (runExcept m)

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

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

# The ExceptT monad transformer

newtype ExceptT e m a Source

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

MonadTrans (ExceptT e) |
Methods lift :: Monad m => m a -> ExceptT e m a Source
Monad m => Monad (ExceptT e m) |
Methods (>>=) :: ExceptT e m a -> (a -> ExceptT e m b) -> ExceptT e m b Source (>>) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b Source return :: a -> ExceptT e m a Source fail :: String -> ExceptT e m a Source
Functor m => Functor (ExceptT e m) |
Methods fmap :: (a -> b) -> ExceptT e m a -> ExceptT e m b Source (<$) :: a -> ExceptT e m b -> ExceptT e m a Source
MonadFix m => MonadFix (ExceptT e m) |
Methods mfix :: (a -> ExceptT e m a) -> ExceptT e m a Source
MonadFail m => MonadFail (ExceptT e m) |
Methods fail :: String -> ExceptT e m a Source
(Functor m, Monad m) => Applicative (ExceptT e m) |
Methods pure :: a -> ExceptT e m a Source (<*>) :: ExceptT e m (a -> b) -> ExceptT e m a -> ExceptT e m b Source (*>) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b Source (<*) :: ExceptT e m a -> ExceptT e m b -> ExceptT e m a Source
Foldable f => Foldable (ExceptT e f) |
Methods fold :: Monoid m => ExceptT e f m -> m Source foldMap :: Monoid m => (a -> m) -> ExceptT e f a -> m Source foldr :: (a -> b -> b) -> b -> ExceptT e f a -> b Source foldr' :: (a -> b -> b) -> b -> ExceptT e f a -> b Source foldl :: (b -> a -> b) -> b -> ExceptT e f a -> b Source foldl' :: (b -> a -> b) -> b -> ExceptT e f a -> b Source foldr1 :: (a -> a -> a) -> ExceptT e f a -> a Source foldl1 :: (a -> a -> a) -> ExceptT e f a -> a Source toList :: ExceptT e f a -> [a] Source null :: ExceptT e f a -> Bool Source length :: ExceptT e f a -> Int Source elem :: Eq a => a -> ExceptT e f a -> Bool Source maximum :: Ord a => ExceptT e f a -> a Source minimum :: Ord a => ExceptT e f a -> a Source sum :: Num a => ExceptT e f a -> a Source product :: Num a => ExceptT e f a -> a Source
Traversable f => Traversable (ExceptT e f) |
Methods traverse :: Applicative f => (a -> f b) -> ExceptT e f a -> f (ExceptT e f b) Source sequenceA :: Applicative f => ExceptT e f (f a) -> f (ExceptT e f a) Source mapM :: Monad m => (a -> m b) -> ExceptT e f a -> m (ExceptT e f b) Source sequence :: Monad m => ExceptT e f (m a) -> m (ExceptT e f a) Source
(Eq e, Eq1 m) => Eq1 (ExceptT e m) |
Methods liftEq :: (a -> b -> Bool) -> ExceptT e m a -> ExceptT e m b -> Bool Source
(Ord e, Ord1 m) => Ord1 (ExceptT e m) |
Methods liftCompare :: (a -> b -> Ordering) -> ExceptT e m a -> ExceptT e m b -> Ordering Source
(Read e, Read1 m) => Read1 (ExceptT e m) |
Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (ExceptT e m a) Source liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [ExceptT e m a] Source
(Show e, Show1 m) => Show1 (ExceptT e m) |
Methods liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> ExceptT e m a -> ShowS Source liftShowList :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> [ExceptT e m a] -> ShowS Source
MonadIO m => MonadIO (ExceptT e m) |
Methods liftIO :: IO a -> ExceptT e m a Source
MonadZip m => MonadZip (ExceptT e m) |
Methods mzip :: ExceptT e m a -> ExceptT e m b -> ExceptT e m (a, b) Source mzipWith :: (a -> b -> c) -> ExceptT e m a -> ExceptT e m b -> ExceptT e m c Source munzip :: ExceptT e m (a, b) -> (ExceptT e m a, ExceptT e m b) Source
(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m) |
Methods empty :: ExceptT e m a Source (<|>) :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a Source some :: ExceptT e m a -> ExceptT e m [a] Source many :: ExceptT e m a -> ExceptT e m [a] Source
(Monad m, Monoid e) => MonadPlus (ExceptT e m) |
Methods mzero :: ExceptT e m a Source mplus :: ExceptT e m a -> ExceptT e m a -> ExceptT e m a Source
(Eq e, Eq1 m, Eq a) => Eq (ExceptT e m a) |
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) |
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) |
Methods readsPrec :: Int -> ReadS (ExceptT e m a) Source readList :: ReadS [ExceptT e m a] Source readPrec :: ReadPrec (ExceptT e m a) Source readListPrec :: ReadPrec [ExceptT e m a] Source
(Show e, Show1 m, Show a) => Show (ExceptT e m a) |
Methods showsPrec :: Int -> ExceptT e m a -> ShowS Source show :: ExceptT e m a -> String Source showList :: [ExceptT e m a] -> ShowS Source

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

The inverse of `ExceptT`.

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

Map the unwrapped computation using the given function.

-     runExceptT (mapExceptT f m) = f (runExceptT m)

withExceptT :: Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a Source

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

# Exception operations

throwE :: Monad m => e -> ExceptT e m a Source

Signal an exception value `e`.

-     runExceptT (throwE e) = return (Left e)

-     throwE e >>= m = throwE e

catchE Source

Arguments

:: Monad m |
=> ExceptT e m a | the inner computation
-> (e -> ExceptT e' m a) | a handler for exceptions in the inner computation
-> ExceptT e' m a |

Handle an exception.

-     catchE h (lift m) = lift m

-     catchE h (throwE e) = h e

# Lifting other operations

liftCallCC :: CallCC m (Either e a) (Either e b) -> CallCC (ExceptT e m) a b Source

Lift a `callCC` operation to the new monad.

liftListen :: Monad m => Listen w m (Either e a) -> Listen w (ExceptT e m) a Source

Lift a `listen` operation to the new monad.

liftPass :: Monad m => Pass w m (Either e a) -> Pass w (ExceptT e m) a Source

Lift a `pass` operation to the new monad.