Copyright | (c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001 (c) Jeff Newbern 2003-2006 (c) Andriy Palamarchuk 2006 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | ross@soi.city.ac.uk |
Stability | experimental |
Portability | non-portable (type families) |
Safe Haskell | None |
Language | GHC2021 |
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
- class Monad m => MonadError (m :: Type -> Type) where
- type ErrorType (m :: Type -> Type)
- throwError :: ErrorType m -> m a
- catchError :: m a -> (ErrorType m -> m a) -> m a
- tryError :: MonadError m => m a -> m (Either (ErrorType m) a)
- withError :: MonadError m => (ErrorType m -> ErrorType m) -> m a -> m a
- newtype ExceptT e (m :: Type -> Type) 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 :: forall (m :: Type -> Type) e e' a. Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a
- class Applicative m => Monad (m :: Type -> Type) where
- class Monad m => MonadFail (m :: Type -> Type) where
- fail :: String -> m a
- mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
- sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
- 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 Functor (f :: Type -> Type) where
- (=<<) :: Monad m => (a -> m b) -> m a -> m b
- join :: Monad m => m (m a) -> m a
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
- ap :: Monad m => m (a -> b) -> m a -> m b
- liftM :: Monad m => (a1 -> r) -> m a1 -> m r
- liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
- liftM3 :: Monad m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
- liftM4 :: Monad m => (a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
- liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r
- when :: Applicative f => Bool -> f () -> f ()
- forM_ :: (Foldable t, Monad m) => t a -> (a -> m b) -> m ()
- msum :: (Foldable t, MonadPlus m) => t (m a) -> m a
- void :: Functor f => f a -> f ()
- forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
- (<$!>) :: Monad m => (a -> b) -> m a -> m b
- (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
- foldM :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m b
- foldM_ :: (Foldable t, Monad m) => (b -> a -> m b) -> b -> t a -> m ()
- replicateM :: Applicative m => Int -> m a -> m [a]
- replicateM_ :: Applicative m => Int -> m a -> m ()
- unless :: Applicative f => Bool -> f () -> f ()
- filterM :: Applicative m => (a -> m Bool) -> [a] -> m [a]
- forever :: Applicative f => f a -> f b
- mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c])
- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
- zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]
- zipWithM_ :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m ()
- guard :: Alternative f => Bool -> f ()
- class Monad m => MonadFix (m :: Type -> Type) where
- mfix :: (a -> m a) -> m a
- fix :: (a -> a) -> a
- module Control.Monad.Trans
Monads with error handling
class Monad m => MonadError (m :: Type -> Type) 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
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 a monad type constructor
other than
, in which case you will have to explicitly define
an instance of the Either
eMonadError
class.
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
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 #
Constructors
ExceptT (m (Either e a)) |
Instances
Functor m => Generic1 (ExceptT e m :: Type -> Type) | |||||
Defined in Control.Monad.Trans.Except Associated Types
| |||||
MonadTrans (ExceptT e) | |||||
Defined in Control.Monad.Trans.Except | |||||
MonadFail m => MonadFail (ExceptT e m) | |||||
Defined in Control.Monad.Trans.Except | |||||
MonadFix m => MonadFix (ExceptT e m) | |||||
Defined in Control.Monad.Trans.Except | |||||
MonadIO m => MonadIO (ExceptT e m) | |||||
Defined in Control.Monad.Trans.Except | |||||
MonadZip m => MonadZip (ExceptT e m) | |||||
Foldable f => Foldable (ExceptT e f) | |||||
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] 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 | |||||
(Eq e, Eq1 m) => Eq1 (ExceptT e m) | |||||
Defined in Control.Monad.Trans.Except | |||||
(Ord e, Ord1 m) => Ord1 (ExceptT e m) | |||||
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) | |||||
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) | |||||
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) | |||||
Traversable f => Traversable (ExceptT e f) | |||||
Defined in Control.Monad.Trans.Except | |||||
(Functor m, Monad m, Monoid e) => Alternative (ExceptT e m) | |||||
(Functor m, Monad m) => Applicative (ExceptT e m) | |||||
Defined in Control.Monad.Trans.Except | |||||
Functor m => Functor (ExceptT e m) | |||||
Monad m => Monad (ExceptT e m) | |||||
(Monad m, Monoid e) => MonadPlus (ExceptT e m) | |||||
MonadCont m => MonadCont (ExceptT e m) Source # | |||||
Monad m => MonadError (ExceptT e m) Source # | |||||
Defined in Control.Monad.Except.Class Associated Types
| |||||
MonadRWS m => MonadRWS (ExceptT e m) Source # | |||||
Defined in Control.Monad.RWS.Class | |||||
MonadReader m => MonadReader (ExceptT e m) Source # | |||||
Defined in Control.Monad.Reader.Class Associated Types
| |||||
MonadState m => MonadState (ExceptT e m) Source # | |||||
MonadWriter m => MonadWriter (ExceptT e m) Source # | |||||
Defined in Control.Monad.Writer.Class Associated Types
| |||||
Generic (ExceptT e m a) | |||||
Defined in Control.Monad.Trans.Except Associated Types
| |||||
(Read e, Read1 m, Read a) => Read (ExceptT e m a) | |||||
Defined in Control.Monad.Trans.Except | |||||
(Show e, Show1 m, Show a) => Show (ExceptT e m a) | |||||
(Eq e, Eq1 m, Eq a) => Eq (ExceptT e m a) | |||||
(Ord e, Ord1 m, Ord a) => Ord (ExceptT e m a) | |||||
Defined in Control.Monad.Trans.Except | |||||
type Rep1 (ExceptT e m :: Type -> Type) | |||||
Defined in Control.Monad.Trans.Except type Rep1 (ExceptT e m :: Type -> Type) = D1 ('MetaData "ExceptT" "Control.Monad.Trans.Except" "transformers-0.6.1.0-a97a" 'True) (C1 ('MetaCons "ExceptT" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (m :.: Rec1 (Either e)))) | |||||
type ErrorType (ExceptT e m) Source # | |||||
Defined in Control.Monad.Except.Class | |||||
type EnvType (ExceptT e m) Source # | |||||
Defined in Control.Monad.Reader.Class | |||||
type StateType (ExceptT e m) Source # | |||||
Defined in Control.Monad.State.Class | |||||
type WriterType (ExceptT e m) Source # | |||||
Defined in Control.Monad.Writer.Class | |||||
type Rep (ExceptT e m a) | |||||
Defined in Control.Monad.Trans.Except type Rep (ExceptT e m a) = D1 ('MetaData "ExceptT" "Control.Monad.Trans.Except" "transformers-0.6.1.0-a97a" 'True) (C1 ('MetaCons "ExceptT" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (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 :: forall (m :: Type -> Type) e e' a. Functor m => (e -> e') -> ExceptT e m a -> ExceptT e' m a #
class Applicative m => Monad (m :: Type -> Type) where #
Minimal complete definition
Instances
Monad Complex | |
Monad Identity | |
Monad First | |
Monad Last | |
Monad Down | |
Monad First | |
Monad Last | |
Monad Max | |
Monad Min | |
Monad Dual | |
Monad Product | |
Monad Sum | |
Monad NonEmpty | |
Monad STM | |
Monad NoIO | |
Monad Par1 | |
Monad P | |
Monad ReadP | |
Monad ReadPrec | |
Monad IO | |
Monad Maybe | |
Monad Solo | |
Monad [] | |
Monad m => Monad (WrappedMonad m) | |
ArrowApply a => Monad (ArrowMonad a) | |
Monad (Either e) | |
Monad (Proxy :: Type -> Type) | |
Monad (U1 :: Type -> Type) | |
Monad (ST s) | |
Monad m => Monad (MaybeT m) | |
Monoid a => Monad ((,) a) | |
Monad m => Monad (Kleisli m a) | |
Monad f => Monad (Ap f) | |
Monad f => Monad (Alt f) | |
Monad f => Monad (Rec1 f) | |
(Monoid w, Functor m, Monad m) => Monad (AccumT w m) | |
Monad m => Monad (ExceptT e m) | |
Monad m => Monad (IdentityT m) | |
Monad m => Monad (ReaderT r m) | |
Monad m => Monad (SelectT r m) | |
Monad m => Monad (StateT s m) | |
Monad m => Monad (StateT s m) | |
Monad m => Monad (WriterT w m) | |
(Monoid w, Monad m) => Monad (WriterT w m) | |
(Monoid w, Monad m) => Monad (WriterT w m) | |
Monad m => Monad (Reverse m) | |
(Monoid a, Monoid b) => Monad ((,,) a b) | |
(Monad f, Monad g) => Monad (Product f g) | |
(Monad f, Monad g) => Monad (f :*: g) | |
Monad (ContT r m) | |
(Monoid a, Monoid b, Monoid c) => Monad ((,,,) a b c) | |
Monad ((->) r) | |
Monad f => Monad (M1 i c f) | |
Monad m => Monad (RWST r w s m) | |
(Monoid w, Monad m) => Monad (RWST r w s m) | |
(Monoid w, Monad m) => Monad (RWST r w s m) | |
class Monad m => MonadFail (m :: Type -> Type) where #
Instances
MonadFail P | |
Defined in Text.ParserCombinators.ReadP | |
MonadFail ReadP | |
Defined in Text.ParserCombinators.ReadP | |
MonadFail ReadPrec | |
Defined in Text.ParserCombinators.ReadPrec | |
MonadFail IO | |
Defined in Control.Monad.Fail | |
MonadFail Maybe | |
Defined in Control.Monad.Fail | |
MonadFail [] | |
Defined in Control.Monad.Fail | |
Monad m => MonadFail (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
MonadFail f => MonadFail (Ap f) | |
Defined in Data.Monoid | |
(Monoid w, MonadFail m) => MonadFail (AccumT w m) | |
Defined in Control.Monad.Trans.Accum | |
MonadFail m => MonadFail (ExceptT e m) | |
Defined in Control.Monad.Trans.Except | |
MonadFail m => MonadFail (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
MonadFail m => MonadFail (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
MonadFail m => MonadFail (SelectT r m) | |
Defined in Control.Monad.Trans.Select | |
MonadFail m => MonadFail (StateT s m) | |
Defined in Control.Monad.Trans.State.Lazy | |
MonadFail m => MonadFail (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
MonadFail m => MonadFail (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.CPS | |
(Monoid w, MonadFail m) => MonadFail (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Lazy | |
(Monoid w, MonadFail m) => MonadFail (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Strict | |
MonadFail m => MonadFail (Reverse m) | |
Defined in Data.Functor.Reverse | |
MonadFail m => MonadFail (ContT r m) | |
Defined in Control.Monad.Trans.Cont | |
MonadFail m => MonadFail (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.CPS | |
(Monoid w, MonadFail m) => MonadFail (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Lazy | |
(Monoid w, MonadFail m) => MonadFail (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Strict |
class Functor (f :: Type -> Type) where #
Minimal complete definition
Instances
Functor ZipList | |
Defined in Control.Applicative | |
Functor Handler | |
Defined in Control.Exception | |
Functor Complex | |
Defined in Data.Complex | |
Functor Identity | |
Functor First | |
Functor Last | |
Functor Down | |
Functor First | |
Defined in Data.Semigroup | |
Functor Last | |
Defined in Data.Semigroup | |
Functor Max | |
Defined in Data.Semigroup | |
Functor Min | |
Defined in Data.Semigroup | |
Functor Dual | |
Functor Product | |
Functor Sum | |
Functor NonEmpty | |
Functor STM | |
Defined in GHC.Conc.Sync | |
Functor NoIO | |
Functor Par1 | |
Defined in GHC.Generics | |
Functor ArgDescr | |
Defined in System.Console.GetOpt | |
Functor ArgOrder | |
Defined in System.Console.GetOpt | |
Functor OptDescr | |
Defined in System.Console.GetOpt | |
Functor P | |
Defined in Text.ParserCombinators.ReadP | |
Functor ReadP | |
Defined in Text.ParserCombinators.ReadP | |
Functor ReadPrec | |
Defined in Text.ParserCombinators.ReadPrec | |
Functor IO | |
Functor Maybe | |
Functor Solo | |
Functor [] | |
Monad m => Functor (WrappedMonad m) | |
Defined in Control.Applicative | |
Arrow a => Functor (ArrowMonad a) | |
Defined in Control.Arrow | |
Functor (Either a) | |
Defined in Data.Either | |
Functor (Proxy :: Type -> Type) | |
Defined in Data.Proxy | |
Functor (Arg a) | |
Defined in Data.Semigroup | |
Functor (Array i) | |
Functor (U1 :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (V1 :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (ST s) | |
Functor f => Functor (Lift f) | |
Defined in Control.Applicative.Lift | |
Functor m => Functor (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
Functor ((,) a) | |
Arrow a => Functor (WrappedArrow a b) | |
Defined in Control.Applicative | |
Functor m => Functor (Kleisli m a) | |
Defined in Control.Arrow | |
Functor (Const m :: Type -> Type) | |
Defined in Data.Functor.Const | |
Functor f => Functor (Ap f) | |
Functor f => Functor (Alt f) | |
(Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f) | |
Defined in GHC.Generics | |
Functor f => Functor (Rec1 f) | |
Defined in GHC.Generics | |
Functor (URec (Ptr ()) :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Char :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Double :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Float :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Int :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (URec Word :: Type -> Type) | |
Defined in GHC.Generics | |
Functor f => Functor (Backwards f) | |
Defined in Control.Applicative.Backwards | |
Functor m => Functor (AccumT w m) | |
Defined in Control.Monad.Trans.Accum | |
Functor m => Functor (ExceptT e m) | |
Functor m => Functor (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
Functor m => Functor (ReaderT r m) | |
Functor m => Functor (SelectT r m) | |
Defined in Control.Monad.Trans.Select | |
Functor m => Functor (StateT s m) | |
Functor m => Functor (StateT s m) | |
Functor m => Functor (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.CPS | |
Functor m => Functor (WriterT w m) | |
Functor m => Functor (WriterT w m) | |
Functor (Constant a :: Type -> Type) | |
Defined in Data.Functor.Constant | |
Functor f => Functor (Reverse f) | |
Defined in Data.Functor.Reverse | |
Functor ((,,) a b) | |
(Functor f, Functor g) => Functor (Product f g) | |
Defined in Data.Functor.Product | |
(Functor f, Functor g) => Functor (Sum f g) | |
Defined in Data.Functor.Sum | |
(Functor f, Functor g) => Functor (f :*: g) | |
Defined in GHC.Generics | |
(Functor f, Functor g) => Functor (f :+: g) | |
Defined in GHC.Generics | |
Functor (K1 i c :: Type -> Type) | |
Defined in GHC.Generics | |
Functor (ContT r m) | |
Functor ((,,,) a b c) | |
Functor ((->) r) | |
(Functor f, Functor g) => Functor (Compose f g) | |
Defined in Data.Functor.Compose | |
(Functor f, Functor g) => Functor (f :.: g) | |
Defined in GHC.Generics | |
Functor f => Functor (M1 i c f) | |
Defined in GHC.Generics | |
Functor m => Functor (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.CPS | |
Functor m => Functor (RWST r w s m) | |
Functor m => Functor (RWST r w s m) | |
Functor ((,,,,) a b c d) | |
Functor ((,,,,,) a b c d e) | |
Functor ((,,,,,,) a b c d e f) | |
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #
Minimal complete definition
Nothing
Instances
MonadPlus STM | |
Defined in GHC.Conc.Sync | |
MonadPlus P | |
Defined in Text.ParserCombinators.ReadP | |
MonadPlus ReadP | |
Defined in Text.ParserCombinators.ReadP | |
MonadPlus ReadPrec | |
Defined in Text.ParserCombinators.ReadPrec | |
MonadPlus IO | |
MonadPlus Maybe | |
MonadPlus [] | |
(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a) | |
Defined in Control.Arrow | |
MonadPlus (Proxy :: Type -> Type) | |
Defined in Data.Proxy | |
MonadPlus (U1 :: Type -> Type) | |
Defined in GHC.Generics | |
Monad m => MonadPlus (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
MonadPlus m => MonadPlus (Kleisli m a) | |
Defined in Control.Arrow | |
MonadPlus f => MonadPlus (Ap f) | |
MonadPlus f => MonadPlus (Alt f) | |
MonadPlus f => MonadPlus (Rec1 f) | |
Defined in GHC.Generics | |
(Monoid w, Functor m, MonadPlus m) => MonadPlus (AccumT w m) | |
Defined in Control.Monad.Trans.Accum | |
(Monad m, Monoid e) => MonadPlus (ExceptT e m) | |
MonadPlus m => MonadPlus (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
MonadPlus m => MonadPlus (ReaderT r m) | |
MonadPlus m => MonadPlus (SelectT r m) | |
Defined in Control.Monad.Trans.Select | |
MonadPlus m => MonadPlus (StateT s m) | |
MonadPlus m => MonadPlus (StateT s m) | |
(Functor m, MonadPlus m) => MonadPlus (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.CPS | |
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) | |
MonadPlus m => MonadPlus (Reverse m) | |
Defined in Data.Functor.Reverse | |
(MonadPlus f, MonadPlus g) => MonadPlus (Product f g) | |
Defined in Data.Functor.Product | |
(MonadPlus f, MonadPlus g) => MonadPlus (f :*: g) | |
Defined in GHC.Generics | |
MonadPlus f => MonadPlus (M1 i c f) | |
Defined in GHC.Generics | |
(Functor m, MonadPlus m) => MonadPlus (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.CPS | |
(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) | |
(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) | |
liftM5 :: Monad m => (a1 -> a2 -> a3 -> a4 -> a5 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m a5 -> m r #
replicateM :: Applicative m => Int -> m a -> m [a] #
replicateM_ :: Applicative m => Int -> m a -> m () #
mapAndUnzipM :: Applicative m => (a -> m (b, c)) -> [a] -> m ([b], [c]) #
class Monad m => MonadFix (m :: Type -> Type) where #
Instances
MonadFix Complex | |
Defined in Data.Complex | |
MonadFix Identity | |
Defined in Data.Functor.Identity | |
MonadFix First | |
Defined in Control.Monad.Fix | |
MonadFix Last | |
Defined in Control.Monad.Fix | |
MonadFix Down | |
Defined in Control.Monad.Fix | |
MonadFix First | |
Defined in Data.Semigroup | |
MonadFix Last | |
Defined in Data.Semigroup | |
MonadFix Max | |
Defined in Data.Semigroup | |
MonadFix Min | |
Defined in Data.Semigroup | |
MonadFix Dual | |
Defined in Control.Monad.Fix | |
MonadFix Product | |
Defined in Control.Monad.Fix | |
MonadFix Sum | |
Defined in Control.Monad.Fix | |
MonadFix NonEmpty | |
Defined in Control.Monad.Fix | |
MonadFix Par1 | |
Defined in Control.Monad.Fix | |
MonadFix IO | |
Defined in Control.Monad.Fix | |
MonadFix Maybe | |
Defined in Control.Monad.Fix | |
MonadFix Solo | |
Defined in Control.Monad.Fix | |
MonadFix [] | |
Defined in Control.Monad.Fix | |
MonadFix (Either e) | |
Defined in Control.Monad.Fix | |
MonadFix (ST s) | |
Defined in Control.Monad.Fix | |
MonadFix m => MonadFix (MaybeT m) | |
Defined in Control.Monad.Trans.Maybe | |
MonadFix f => MonadFix (Ap f) | |
Defined in Control.Monad.Fix | |
MonadFix f => MonadFix (Alt f) | |
Defined in Control.Monad.Fix | |
MonadFix f => MonadFix (Rec1 f) | |
Defined in Control.Monad.Fix | |
(Monoid w, Functor m, MonadFix m) => MonadFix (AccumT w m) | |
Defined in Control.Monad.Trans.Accum | |
MonadFix m => MonadFix (ExceptT e m) | |
Defined in Control.Monad.Trans.Except | |
MonadFix m => MonadFix (IdentityT m) | |
Defined in Control.Monad.Trans.Identity | |
MonadFix m => MonadFix (ReaderT r m) | |
Defined in Control.Monad.Trans.Reader | |
MonadFix m => MonadFix (StateT s m) | |
Defined in Control.Monad.Trans.State.Lazy | |
MonadFix m => MonadFix (StateT s m) | |
Defined in Control.Monad.Trans.State.Strict | |
MonadFix m => MonadFix (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.CPS | |
(Monoid w, MonadFix m) => MonadFix (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Lazy | |
(Monoid w, MonadFix m) => MonadFix (WriterT w m) | |
Defined in Control.Monad.Trans.Writer.Strict | |
(MonadFix f, MonadFix g) => MonadFix (Product f g) | |
Defined in Data.Functor.Product | |
(MonadFix f, MonadFix g) => MonadFix (f :*: g) | |
Defined in Control.Monad.Fix | |
MonadFix ((->) r) | |
Defined in Control.Monad.Fix | |
MonadFix f => MonadFix (M1 i c f) | |
Defined in Control.Monad.Fix | |
MonadFix m => MonadFix (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.CPS | |
(Monoid w, MonadFix m) => MonadFix (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Lazy | |
(Monoid w, MonadFix m) => MonadFix (RWST r w s m) | |
Defined in Control.Monad.Trans.RWS.Strict |
module Control.Monad.Trans
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
monad transformer can be used to add error handling to another monad.
Here is an example how to combine it with an ErrorT
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))