| Stability | experimental |
|---|---|
| Safe Haskell | Safe |
| Language | Haskell2010 |
Control.Monad.Except.CoHas
Description
This module defines a class CoHas intended to be used with the MonadError class
(and similar ones) or Except / ExceptT types.
The problem
Assume there are several types representing the possible errors in different parts of an application:
data DbError = ... data WebUIError = ...
as well as a single sum type containing all of those:
data AppError = AppDbError DbError | AppWebUIError WebUIError
What should be the MonadError constraint of the DB module and web module respectively?
- It could be
MonadError AppError mfor both, introducing unnecessary coupling. - Or it could be
MonadError DbError mfor the DB module andMonadError WebError mfor the web module respectively, but combining them becomes a pain.
Or, it could be MonadError e m, CoHas AppError e for the DB module (and similarly for the web module),
where some appropriately defined CoHas option sum class allows injecting option
creating a value of the sum type.
This approach keeps both modules decoupled, while allowing using them in the same monad stack.
The only downside is that now one has to define the CoHas class
and write tedious instances for the AppError type (and potentially other types in case of, for example, tests).
But why bother doing the work that the machine will happily do for you?
The solution
This module defines the generic CoHas class as well as hides all the boilerplate behind GHC.Generics,
so all you have to do is to add the corresponding deriving-clause:
data AppError = AppDbError DbError | AppWebUIError WebUIError deriving (Generic, CoHas DbError, CoHas WebUIError)
and use throwError . inject instead of throwError (but this is something you'd have to do anyway).
Type safety
What should happen if sum does not have any way to construct it from option at all?
Of course, this means that we cannot inject option into sum, and no CoHas instance can be derived at all.
Indeed, this library will refuse to generate an instance in this case.
On the other hand, what should happen if sum contains multiple values of type option
(like Either option option), perhaps on different levels of nesting?
While technically we could make an arbitrary choice, like taking the first one in breadth-first or depth-first order,
we instead decide that such a choice is inherently ambiguous,
so this library will refuse to generate an instance in this case as well.
Exports
This module also reexports Except along with some functions like throwError or liftEither
with types adjusted for the intended usage of the CoHas class.
Synopsis
- class CoHas option sum where
- inject :: option -> sum
- type SuccessfulSearch option sum path = (Search option (Rep sum) ~ Found path, GCoHas path option (Rep sum))
- guard :: Alternative f => Bool -> f ()
- join :: Monad m => m (m a) -> m a
- class Applicative m => Monad (m :: Type -> Type) where
- class Functor (f :: Type -> Type) where
- fmap :: (a -> b) -> f a -> f b
- class Monad m => MonadFix (m :: Type -> Type) where
- mfix :: (a -> m a) -> m a
- mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
- sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
- class Monad m => MonadIO (m :: Type -> Type) where
- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
- unless :: Applicative f => Bool -> f () -> f ()
- replicateM_ :: Applicative m => Int -> m a -> m ()
- replicateM :: Applicative m => Int -> m a -> m [a]
- foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m ()
- foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m ()
- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- forever :: Applicative f => f a -> f b
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
- forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
- mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
- fix :: (a -> a) -> a
- void :: Functor f => f a -> f ()
- ap :: Monad m => m (a -> b) -> m a -> m b
- liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
- liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
- liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
- liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- when :: Applicative f => Bool -> f () -> f ()
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
- class MonadTrans (t :: (Type -> Type) -> Type -> Type) where
- class Monad m => MonadError e (m :: Type -> Type) | m -> e where
- catchError :: m a -> (e -> m a) -> m a
- newtype ExceptT e (m :: Type -> Type) a = ExceptT (m (Either e a))
- type Except e = ExceptT e Identity
- 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
- 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
- throwError :: (MonadError error m, CoHas option error) => option -> m a
- liftEither :: (MonadError error m, CoHas option error) => Either option a -> m a
- liftMaybe :: (MonadError error m, CoHas option error) => option -> Maybe a -> m a
Documentation
class CoHas option sum where Source #
The CoHas option sum class is used for sum types that could be created from a value of type option.
Minimal complete definition
Nothing
Methods
inject :: option -> sum Source #
Inject an option into the sum type.
The default implementation searches sum for some constructor
that's compatible with option and creates sum using that constructor.
The default implementation typechecks iff there is a single matching constructor.
inject :: forall path. (Generic sum, SuccessfulSearch option sum path) => option -> sum Source #
Inject an option into the sum type.
The default implementation searches sum for some constructor
that's compatible with option and creates sum using that constructor.
The default implementation typechecks iff there is a single matching constructor.
Instances
| CoHas sum sum Source # | Each type can be injected into itself (and that is an |
Defined in Control.Monad.Except.CoHas | |
| SuccessfulSearch a (Either l r) path => CoHas a (Either l r) Source # | |
Defined in Control.Monad.Except.CoHas | |
type SuccessfulSearch option sum path = (Search option (Rep sum) ~ Found path, GCoHas path option (Rep sum)) Source #
Type alias representing that the search of option in sum has been successful.
The path is used to guide the default generic implementation of CoHas.
guard :: Alternative f => Bool -> f () #
Conditional failure of Alternative computations. Defined by
guard True =pure() guard False =empty
Examples
Common uses of guard include conditionally signaling an error in
an error monad and conditionally rejecting the current choice in an
Alternative-based parser.
As an example of signaling an error in the error monad Maybe,
consider a safe division function safeDiv x y that returns
Nothing when the denominator y is zero and otherwise. For example:Just (x `div`
y)
>>> safeDiv 4 0 Nothing >>> safeDiv 4 2 Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0 = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y = do guard (y /= 0) return (x `div` y)
join :: Monad m => m (m a) -> m a #
The join function is the conventional monad join operator. It
is used to remove one level of monadic structure, projecting its
bound argument into the outer level.
Examples
A common use of join is to run an IO computation returned from
an STM transaction, since STM transactions
can't perform IO directly. Recall that
atomically :: STM a -> IO a
is used to run STM transactions atomically. So, by
specializing the types of atomically and join to
atomically:: STM (IO b) -> IO (IO b)join:: IO (IO b) -> IO b
we can compose them as
join.atomically:: STM (IO b) -> IO b
class Applicative m => Monad (m :: Type -> Type) where #
The Monad class defines the basic operations over a monad,
a concept from a branch of mathematics known as category theory.
From the perspective of a Haskell programmer, however, it is best to
think of a monad as an abstract datatype of actions.
Haskell's do expressions provide a convenient syntax for writing
monadic expressions.
Instances of Monad should satisfy the following laws:
Furthermore, the Monad and Applicative operations should relate as follows:
The above laws imply:
and that pure and (<*>) satisfy the applicative functor laws.
The instances of Monad for lists, Maybe and IO
defined in the Prelude satisfy these laws.
Minimal complete definition
Methods
(>>=) :: m a -> (a -> m b) -> m b infixl 1 #
Sequentially compose two actions, passing any value produced by the first as an argument to the second.
(>>) :: m a -> m b -> m b infixl 1 #
Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
Inject a value into the monadic type.
Fail with a message. This operation is not part of the
mathematical definition of a monad, but is invoked on pattern-match
failure in a do expression.
As part of the MonadFail proposal (MFP), this function is moved
to its own class MonadFail (see Control.Monad.Fail for more
details). The definition here will be removed in a future
release.
Instances
| Monad [] | Since: base-2.1 |
| Monad Maybe | Since: base-2.1 |
| Monad IO | Since: base-2.1 |
| Monad Par1 | Since: base-4.9.0.0 |
| Monad Down | Since: base-4.11.0.0 |
| Monad ReadP | Since: base-2.1 |
| Monad NonEmpty | Since: base-4.9.0.0 |
| Monad P | Since: base-2.1 |
| Monad (Either e) | Since: base-4.4.0.0 |
| Monad (U1 :: Type -> Type) | Since: base-4.9.0.0 |
| Monoid a => Monad ((,) a) | Since: base-4.9.0.0 |
| Monad (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
| Monad m => Monad (ListT m) | |
| Monad m => Monad (MaybeT m) | |
| Monad f => Monad (Rec1 f) | Since: base-4.9.0.0 |
| Monad m => Monad (IdentityT m) | |
| (Monad m, Error e) => Monad (ErrorT e m) | |
| Monad m => Monad (ExceptT e m) | |
| Monad m => Monad (StateT s m) | |
| Monad m => Monad (StateT s m) | |
| (Monoid w, Monad m) => Monad (WriterT w m) | |
| (Monoid w, Monad m) => Monad (WriterT w m) | |
| Monad ((->) r :: Type -> Type) | Since: base-2.1 |
| (Monad f, Monad g) => Monad (f :*: g) | Since: base-4.9.0.0 |
| Monad (ContT r m) | |
| Monad m => Monad (ReaderT r m) | |
| Monad f => Monad (M1 i c f) | Since: base-4.9.0.0 |
| (Monoid w, Monad m) => Monad (RWST r w s m) | |
| (Monoid w, Monad m) => Monad (RWST r w s m) | |
class Functor (f :: Type -> Type) where #
The Functor class is used for types that can be mapped over.
Instances of Functor should satisfy the following laws:
fmap id == id fmap (f . g) == fmap f . fmap g
The instances of Functor for lists, Maybe and IO
satisfy these laws.
Instances
| Functor [] | Since: base-2.1 |
| Functor Maybe | Since: base-2.1 |
| Functor IO | Since: base-2.1 |
| Functor Par1 | Since: base-4.9.0.0 |
| Functor Handler | Since: base-4.6.0.0 |
| Functor Down | Since: base-4.11.0.0 |
| Functor ReadP | Since: base-2.1 |
| Functor NonEmpty | Since: base-4.9.0.0 |
| Functor P | Since: base-4.8.0.0 |
Defined in Text.ParserCombinators.ReadP | |
| Functor (Either a) | Since: base-3.0 |
| Functor (V1 :: Type -> Type) | Since: base-4.9.0.0 |
| Functor (U1 :: Type -> Type) | Since: base-4.9.0.0 |
| Functor ((,) a) | Since: base-2.1 |
| Functor (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
| Functor m => Functor (ListT m) | |
| Functor m => Functor (MaybeT m) | |
| Functor f => Functor (Rec1 f) | Since: base-4.9.0.0 |
| Functor (URec Char :: Type -> Type) | Since: base-4.9.0.0 |
| Functor (URec Double :: Type -> Type) | Since: base-4.9.0.0 |
| Functor (URec Float :: Type -> Type) | Since: base-4.9.0.0 |
| Functor (URec Int :: Type -> Type) | Since: base-4.9.0.0 |
| Functor (URec Word :: Type -> Type) | Since: base-4.9.0.0 |
| Functor (URec (Ptr ()) :: Type -> Type) | Since: base-4.9.0.0 |
| Functor m => Functor (IdentityT m) | |
| Functor m => Functor (ErrorT e m) | |
| Functor m => Functor (ExceptT e m) | |
| Functor m => Functor (StateT s m) | |
| Functor m => Functor (StateT s m) | |
| Functor m => Functor (WriterT w m) | |
| Functor m => Functor (WriterT w m) | |
| Functor ((->) r :: Type -> Type) | Since: base-2.1 |
| Functor (K1 i c :: Type -> Type) | Since: base-4.9.0.0 |
| (Functor f, Functor g) => Functor (f :+: g) | Since: base-4.9.0.0 |
| (Functor f, Functor g) => Functor (f :*: g) | Since: base-4.9.0.0 |
| Functor (ContT r m) | |
| Functor m => Functor (ReaderT r m) | |
| Functor f => Functor (M1 i c f) | Since: base-4.9.0.0 |
| (Functor f, Functor g) => Functor (f :.: g) | Since: base-4.9.0.0 |
| Functor m => Functor (RWST r w s m) | |
| Functor m => Functor (RWST r w s m) | |
class Monad m => MonadFix (m :: Type -> Type) where #
Monads having fixed points with a 'knot-tying' semantics.
Instances of MonadFix should satisfy the following laws:
- purity
mfix(return. h) =return(fixh)- left shrinking (or tightening)
mfix(\x -> a >>= \y -> f x y) = a >>= \y ->mfix(\x -> f x y)- sliding
, for strictmfix(liftMh . f) =liftMh (mfix(f . h))h.- nesting
mfix(\x ->mfix(\y -> f x y)) =mfix(\x -> f x x)
This class is used in the translation of the recursive do notation
supported by GHC and Hugs.
Methods
Instances
mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b) #
Map each element of a structure to a monadic action, evaluate
these actions from left to right, and collect the results. For
a version that ignores the results see mapM_.
sequence :: (Traversable t, Monad m) => t (m a) -> m (t a) #
Evaluate each monadic action in the structure from left to
right, and collect the results. For a version that ignores the
results see sequence_.
class Monad m => MonadIO (m :: Type -> Type) where #
Monads in which IO computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
Instances
unless :: Applicative f => Bool -> f () -> f () #
The reverse of when.
replicateM_ :: Applicative m => Int -> m a -> m () #
Like replicateM, but discards the result.
replicateM :: Applicative m => Int -> m a -> m [a] #
performs the action replicateM n actn times,
gathering the results.
foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m () #
Like foldM, but discards the result.
foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b #
The foldM function is analogous to foldl, except that its result is
encapsulated in a monad. Note that foldM works from left-to-right over
the list arguments. This could be an issue where ( and the `folded
function' are not commutative.>>)
foldM f a1 [x1, x2, ..., xm] == do a2 <- f a1 x1 a3 <- f a2 x2 ... f am xm
If right-to-left evaluation is required, the input list should be reversed.
zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m () #
zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c] #
mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c]) #
The mapAndUnzipM function maps its first argument over a list, returning
the result as a pair of lists. This function is mainly used with complicated
data structures or a state-transforming monad.
forever :: Applicative f => f a -> f b #
Repeat an action indefinitely.
Examples
A common use of forever is to process input from network sockets,
Handles, and channels
(e.g. MVar and
Chan).
For example, here is how we might implement an echo
server, using
forever both to listen for client connections on a network socket
and to echo client input on client connection handles:
echoServer :: Socket -> IO () echoServer socket =forever$ do client <- accept socketforkFinally(echo client) (\_ -> hClose client) where echo :: Handle -> IO () echo client =forever$ hGetLine client >>= hPutStrLn client
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c infixr 1 #
Left-to-right composition of Kleisli arrows.
filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a] #
This generalizes the list-based filter function.
forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b) #
sequence_ :: (Foldable t, Monad m) => t (m a) -> m () #
Evaluate each monadic action in the structure from left to right,
and ignore the results. For a version that doesn't ignore the
results see sequence.
As of base 4.8.0.0, sequence_ is just sequenceA_, specialized
to Monad.
is the least fixed point of the function fix ff,
i.e. the least defined x such that f x = x.
For example, we can write the factorial function using direct recursion as
>>>let fac n = if n <= 1 then 1 else n * fac (n-1) in fac 5120
This uses the fact that Haskell’s let introduces recursive bindings. We can
rewrite this definition using fix,
>>>fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5120
Instead of making a recursive call, we introduce a dummy parameter rec;
when used within fix, this parameter then refers to fix' argument, hence
the recursion is reintroduced.
void :: Functor f => f a -> f () #
discards or ignores the result of evaluation, such
as the return value of an void valueIO action.
Examples
Replace the contents of a with unit:Maybe Int
>>>void NothingNothing>>>void (Just 3)Just ()
Replace the contents of an with unit,
resulting in an Either Int Int:Either Int '()'
>>>void (Left 8675309)Left 8675309>>>void (Right 8675309)Right ()
Replace every element of a list with unit:
>>>void [1,2,3][(),(),()]
Replace the second element of a pair with unit:
>>>void (1,2)(1,())
Discard the result of an IO action:
>>>mapM print [1,2]1 2 [(),()]>>>void $ mapM print [1,2]1 2
liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r #
Promote a function to a monad, scanning the monadic arguments from
left to right (cf. liftM2).
liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r #
Promote a function to a monad, scanning the monadic arguments from
left to right (cf. liftM2).
liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r #
Promote a function to a monad, scanning the monadic arguments from
left to right (cf. liftM2).
liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r #
Promote a function to a monad, scanning the monadic arguments from left to right. For example,
liftM2 (+) [0,1] [0,2] = [0,2,1,3] liftM2 (+) (Just 1) Nothing = Nothing
when :: Applicative f => Bool -> f () -> f () #
Conditional execution of Applicative expressions. For example,
when debug (putStrLn "Debugging")
will output the string Debugging if the Boolean value debug
is True, and otherwise do nothing.
(=<<) :: Monad m => (a -> m b) -> m a -> m b infixr 1 #
Same as >>=, but with the arguments interchanged.
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #
Monads that also support choice and failure.
Minimal complete definition
Nothing
Methods
The identity of mplus. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
The default definition is
mzero = empty
An associative operation. The default definition is
mplus = (<|>)
Instances
| MonadPlus [] | Since: base-2.1 |
| MonadPlus Maybe | Since: base-2.1 |
| MonadPlus IO | Since: base-4.9.0.0 |
| MonadPlus ReadP | Since: base-2.1 |
| MonadPlus P | Since: base-2.1 |
Defined in Text.ParserCombinators.ReadP | |
| MonadPlus (U1 :: Type -> Type) | Since: base-4.9.0.0 |
| MonadPlus (Proxy :: Type -> Type) | Since: base-4.9.0.0 |
| Monad m => MonadPlus (ListT m) | |
| Monad m => MonadPlus (MaybeT m) | |
| MonadPlus f => MonadPlus (Rec1 f) | Since: base-4.9.0.0 |
| MonadPlus m => MonadPlus (IdentityT m) | |
| (Monad m, Error e) => MonadPlus (ErrorT e m) | |
| (Monad m, Monoid e) => MonadPlus (ExceptT e m) | |
| MonadPlus m => MonadPlus (StateT s m) | |
| MonadPlus m => MonadPlus (StateT s m) | |
| (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
| (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
| (MonadPlus f, MonadPlus g) => MonadPlus (f :*: g) | Since: base-4.9.0.0 |
| MonadPlus m => MonadPlus (ReaderT r m) | |
| MonadPlus f => MonadPlus (M1 i c f) | Since: base-4.9.0.0 |
| (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) | |
| (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) | |
class MonadTrans (t :: (Type -> Type) -> Type -> Type) where #
The class of monad transformers. Instances should satisfy the
following laws, which state that lift is a monad transformation:
Methods
lift :: Monad m => m a -> t m a #
Lift a computation from the argument monad to the constructed monad.
Instances
| MonadTrans ListT | |
Defined in Control.Monad.Trans.List | |
| MonadTrans MaybeT | |
Defined in Control.Monad.Trans.Maybe | |
| MonadTrans (IdentityT :: (Type -> Type) -> Type -> Type) | |
Defined in Control.Monad.Trans.Identity | |
| MonadTrans (ErrorT e) | |
Defined in Control.Monad.Trans.Error | |
| MonadTrans (ExceptT e) | |
Defined in Control.Monad.Trans.Except | |
| MonadTrans (StateT s) | |
Defined in Control.Monad.Trans.State.Lazy | |
| MonadTrans (StateT s) | |
Defined in Control.Monad.Trans.State.Strict | |
| Monoid w => MonadTrans (WriterT w) | |
Defined in Control.Monad.Trans.Writer.Lazy | |
| Monoid w => MonadTrans (WriterT w) | |
Defined in Control.Monad.Trans.Writer.Strict | |
| MonadTrans (ContT r) | |
Defined in Control.Monad.Trans.Cont | |
| MonadTrans (ReaderT r :: (Type -> Type) -> Type -> Type) | |
Defined in Control.Monad.Trans.Reader | |
| Monoid w => MonadTrans (RWST r w s) | |
Defined in Control.Monad.Trans.RWS.Lazy | |
| Monoid w => MonadTrans (RWST r w s) | |
Defined in Control.Monad.Trans.RWS.Strict | |
class Monad m => MonadError e (m :: Type -> Type) | m -> e where #
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 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 Either StringMonadError class.
You can also define your own error type and/or use a monad type constructor
other than or Either String.
In these cases you will have to explicitly define instances of the Either IOErrorMonadError
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
Methods
catchError :: m a -> (e -> m a) -> m a #
A handler function to handle previous errors and return to normal execution. A common idiom is:
do { action1; action2; action3 } `catchError` handlerwhere the action functions can call throwError.
Note that handler and the do-block must have the same return type.
Instances
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.
Instances
runExcept :: Except e a -> Either e a #
Extractor for computations in the exception monad.
(The inverse of except).
withExcept :: (e -> e') -> Except e a -> Except e' a #
Transform any exceptions thrown by the computation using the given
function (a specialization of withExceptT).
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.
runExceptT(mapExceptTf m) = f (runExceptTm)
withExceptT :: Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a #
Transform any exceptions thrown by the computation using the given function.
throwError :: (MonadError error m, CoHas option error) => option -> m a Source #
Begin error processing for the error of type option.
This is Control.Monad.Except's throwError
with the type adjusted for better compatibility with CoHas.
liftEither :: (MonadError error m, CoHas option error) => Either option a -> m a Source #
Lifts an Either option into any MonadError error where option can be injected into error.
This is Control.Monad.Except's liftEither
with the type adjusted for better compatibility with CoHas.