monadLib-3.3.0: A collection of monad transformers.Source codeContentsIndex
MonadLib
Contents
Types
Lifting
Effect Classes
Execution
Eliminating Effects
Nested Execution
Deriving functions
Miscellaneous
Description
This library provides a collection of monad transformers that can be combined to produce various monads.
Synopsis
data Id a
data Lift a
data IdT m a
data ReaderT i m a
data WriterT i m a
data StateT i m a
data ExceptionT i m a
data ContT i m a
class MonadT t where
lift :: Monad m => m a -> t m a
class (Monad m, Monad n) => BaseM m n | m -> n where
inBase :: n a -> m a
class Monad m => ReaderM m i | m -> i where
ask :: m i
class Monad m => WriterM m i | m -> i where
put :: i -> m ()
class Monad m => StateM m i | m -> i where
get :: m i
set :: i -> m ()
class Monad m => ExceptionM m i | m -> i where
raise :: i -> m a
class Monad m => ContM m where
callCC :: ((a -> m b) -> m a) -> m a
data Label m a
labelCC :: ContM m => a -> m (a, Label m a)
jump :: ContM m => a -> Label m a -> m b
runId :: Id a -> a
runLift :: Lift a -> a
runIdT :: IdT m a -> m a
runReaderT :: i -> ReaderT i m a -> m a
runWriterT :: WriterT i m a -> m (a, i)
runStateT :: i -> StateT i m a -> m (a, i)
runExceptionT :: ExceptionT i m a -> m (Either i a)
runContT :: (a -> m i) -> ContT i m a -> m i
class ReaderM m i => RunReaderM m i | m -> i where
local :: i -> m a -> m a
class WriterM m i => RunWriterM m i | m -> i where
collect :: m a -> m (a, i)
class ExceptionM m i => RunExceptionM m i | m -> i where
try :: m a -> m (Either i a)
data Iso m n = Iso {
close :: forall a. m a -> n a
open :: forall a. n a -> m a
}
derive_fmap :: Functor m => Iso m n -> (a -> b) -> n a -> n b
derive_return :: Monad m => Iso m n -> a -> n a
derive_bind :: Monad m => Iso m n -> n a -> (a -> n b) -> n b
derive_fail :: Monad m => Iso m n -> String -> n a
derive_mfix :: MonadFix m => Iso m n -> (a -> n a) -> n a
derive_ask :: ReaderM m i => Iso m n -> n i
derive_put :: WriterM m i => Iso m n -> i -> n ()
derive_get :: StateM m i => Iso m n -> n i
derive_set :: StateM m i => Iso m n -> i -> n ()
derive_raise :: ExceptionM m i => Iso m n -> i -> n a
derive_callCC :: ContM m => Iso m n -> ((a -> n b) -> n a) -> n a
derive_local :: RunReaderM m i => Iso m n -> i -> n a -> n a
derive_collect :: RunWriterM m i => Iso m n -> n a -> n (a, i)
derive_try :: RunExceptionM m i => Iso m n -> n a -> n (Either i a)
derive_lift :: (MonadT t, Monad m) => Iso (t m) n -> m a -> n a
derive_inBase :: BaseM m x => Iso m n -> x a -> n a
version :: (Int, Int, Int)
Types
The following types define the representations of the computation types supported by the library. Each type adds support for a different effect.
data Id a Source
Computations with no effects.
show/hide Instances
data Lift a Source
Computation with no effects (strict).
show/hide Instances
data IdT m a Source
Add nothing. Useful as a placeholder.
show/hide Instances
MonadT IdT
Monad m => Monad (IdT m)
Monad m => Functor (IdT m)
MonadFix m => MonadFix (IdT m)
MonadPlus m => MonadPlus (IdT m)
ContM m => ContM (IdT m)
RunExceptionM m i => RunExceptionM (IdT m) i
RunWriterM m j => RunWriterM (IdT m) j
RunReaderM m j => RunReaderM (IdT m) j
ExceptionM m j => ExceptionM (IdT m) j
StateM m j => StateM (IdT m) j
WriterM m j => WriterM (IdT m) j
ReaderM m j => ReaderM (IdT m) j
BaseM m n => BaseM (IdT m) n
data ReaderT i m a Source
Add support for propagating a context.
show/hide Instances
MonadT (ReaderT i)
Monad m => Monad (ReaderT i m)
Monad m => Functor (ReaderT i m)
MonadFix m => MonadFix (ReaderT i m)
MonadPlus m => MonadPlus (ReaderT i m)
ContM m => ContM (ReaderT i m)
RunExceptionM m i => RunExceptionM (ReaderT j m) i
RunWriterM m j => RunWriterM (ReaderT i m) j
Monad m => RunReaderM (ReaderT i m) i
ExceptionM m j => ExceptionM (ReaderT i m) j
StateM m j => StateM (ReaderT i m) j
WriterM m j => WriterM (ReaderT i m) j
Monad m => ReaderM (ReaderT i m) i
BaseM m n => BaseM (ReaderT i m) n
data WriterT i m a Source
Add support for collecting values.
show/hide Instances
Monoid i => MonadT (WriterT i)
(Monad m, Monoid i) => Monad (WriterT i m)
(Monad m, Monoid i) => Functor (WriterT i m)
(MonadFix m, Monoid i) => MonadFix (WriterT i m)
(MonadPlus m, Monoid i) => MonadPlus (WriterT i m)
(ContM m, Monoid i) => ContM (WriterT i m)
(RunExceptionM m i, Monoid j) => RunExceptionM (WriterT j m) i
(Monad m, Monoid i) => RunWriterM (WriterT i m) i
(RunReaderM m j, Monoid i) => RunReaderM (WriterT i m) j
(ExceptionM m j, Monoid i) => ExceptionM (WriterT i m) j
(StateM m j, Monoid i) => StateM (WriterT i m) j
(Monad m, Monoid i) => WriterM (WriterT i m) i
(ReaderM m j, Monoid i) => ReaderM (WriterT i m) j
(BaseM m n, Monoid i) => BaseM (WriterT i m) n
data StateT i m a Source
Add support for threading state.
show/hide Instances
MonadT (StateT i)
Monad m => Monad (StateT i m)
Monad m => Functor (StateT i m)
MonadFix m => MonadFix (StateT i m)
MonadPlus m => MonadPlus (StateT i m)
ContM m => ContM (StateT i m)
RunExceptionM m i => RunExceptionM (StateT j m) i
RunWriterM m j => RunWriterM (StateT i m) j
RunReaderM m j => RunReaderM (StateT i m) j
ExceptionM m j => ExceptionM (StateT i m) j
Monad m => StateM (StateT i m) i
WriterM m j => WriterM (StateT i m) j
ReaderM m j => ReaderM (StateT i m) j
BaseM m n => BaseM (StateT i m) n
data ExceptionT i m a Source
Add support for exceptions.
show/hide Instances
data ContT i m a Source
Add support for jumps.
show/hide Instances
MonadT (ContT i)
Monad m => Monad (ContT i m)
Monad m => Functor (ContT i m)
Monad m => ContM (ContT i m)
ExceptionM m j => ExceptionM (ContT i m) j
StateM m j => StateM (ContT i m) j
WriterM m j => WriterM (ContT i m) j
ReaderM m j => ReaderM (ContT i m) j
BaseM m n => BaseM (ContT i m) n
Lifting
The following operations allow us to promote computations in the underlying monad to computations that support an extra effect. Computations defined in this way do not make use of the new effect but can be combined with other operations that utilize the effect.
class MonadT t whereSource
Methods
lift :: Monad m => m a -> t m aSource
Promote a computation from the underlying monad.
show/hide Instances
class (Monad m, Monad n) => BaseM m n | m -> n whereSource
Methods
inBase :: n a -> m aSource
Promote a computation from the base monad.
show/hide Instances
BaseM [] []
BaseM IO IO
BaseM Maybe Maybe
BaseM Lift Lift
BaseM Id Id
BaseM m n => BaseM (IdT m) n
BaseM (Cont i) (Cont i)
BaseM (Exception i) (Exception i)
BaseM (State i) (State i)
Monoid i => BaseM (Writer i) (Writer i)
BaseM (Reader i) (Reader i)
BaseM m n => BaseM (ContT i m) n
BaseM m n => BaseM (ExceptionT i m) n
BaseM m n => BaseM (StateT i m) n
(BaseM m n, Monoid i) => BaseM (WriterT i m) n
BaseM m n => BaseM (ReaderT i m) n
Effect Classes
The following classes define overloaded operations that can be used to define effectful computations.
class Monad m => ReaderM m i | m -> i whereSource
Classifies monads that provide access to a context of type i.
Methods
ask :: m iSource
Get the context.
show/hide Instances
ReaderM m j => ReaderM (IdT m) j
ReaderM (Reader i) i
ReaderM m j => ReaderM (ContT i m) j
ReaderM m j => ReaderM (ExceptionT i m) j
ReaderM m j => ReaderM (StateT i m) j
(ReaderM m j, Monoid i) => ReaderM (WriterT i m) j
Monad m => ReaderM (ReaderT i m) i
class Monad m => WriterM m i | m -> i whereSource
Classifies monads that can collect values of type i.
Methods
put :: i -> m ()Source
Add a value to the collection.
show/hide Instances
WriterM m j => WriterM (IdT m) j
Monoid i => WriterM (Writer i) i
WriterM m j => WriterM (ContT i m) j
WriterM m j => WriterM (ExceptionT i m) j
WriterM m j => WriterM (StateT i m) j
(Monad m, Monoid i) => WriterM (WriterT i m) i
WriterM m j => WriterM (ReaderT i m) j
class Monad m => StateM m i | m -> i whereSource
Classifies monads that propagate a state component of type i.
Methods
get :: m iSource
Get the state.
set :: i -> m ()Source
Set the state.
show/hide Instances
StateM m j => StateM (IdT m) j
StateM (State i) i
StateM m j => StateM (ContT i m) j
StateM m j => StateM (ExceptionT i m) j
Monad m => StateM (StateT i m) i
(StateM m j, Monoid i) => StateM (WriterT i m) j
StateM m j => StateM (ReaderT i m) j
class Monad m => ExceptionM m i | m -> i whereSource
Classifies monads that support raising exceptions of type i.
Methods
raise :: i -> m aSource
Raise an exception.
show/hide Instances
class Monad m => ContM m whereSource
Classifies monads that provide access to a computation's continuation.
Methods
callCC :: ((a -> m b) -> m a) -> m aSource
Capture the current continuation.
show/hide Instances
ContM m => ContM (IdT m)
ContM (Cont i)
Monad m => ContM (ContT i m)
ContM m => ContM (ExceptionT i m)
ContM m => ContM (StateT i m)
(ContM m, Monoid i) => ContM (WriterT i m)
ContM m => ContM (ReaderT i m)
data Label m a Source
An explicit representation for continuations that store a value.
labelCC :: ContM m => a -> m (a, Label m a)Source
Capture the current continuation This function is like return, except that it also captures the current continuation. Later we can use jump to go back to the continuation with a possibly different value.
jump :: ContM m => a -> Label m a -> m bSource
Change the value passed to a previously captured continuation.
Execution
Eliminating Effects
The following functions eliminate the outermost effect of a computation by translating a computation into an equivalent computation in the underlying monad. (The exceptions are Id and Lift which are not transformers but ordinary monas and so, their run operations simply eliminate the monad.)
runId :: Id a -> aSource
Get the result of a pure computation.
runLift :: Lift a -> aSource
Get the result of a pure strict computation.
runIdT :: IdT m a -> m aSource
Remove an identity layer.
runReaderT :: i -> ReaderT i m a -> m aSource
Execute a reader computation in the given context.
runWriterT :: WriterT i m a -> m (a, i)Source
Execute a writer computation. Returns the result and the collected output.
runStateT :: i -> StateT i m a -> m (a, i)Source
Execute a stateful computation in the given initial state. The second component of the result is the final state.
runExceptionT :: ExceptionT i m a -> m (Either i a)Source
Execute a computation with exceptions. Successful results are tagged with Right, exceptional results are tagged with Left.
runContT :: (a -> m i) -> ContT i m a -> m iSource
Execute a computation with the given continuation.
Nested Execution
The following classes define operations that are overloaded versions of the run operations. Unlike the run operations, these functions do not change the type of the computation (i.e, they do not remove a layer). Instead, they perform the effects in a ``separate effect thread''.
class ReaderM m i => RunReaderM m i | m -> i whereSource
Classifies monads that support changing the context for a sub-computation.
Methods
local :: i -> m a -> m aSource
Change the context for the duration of a computation.
show/hide Instances
class WriterM m i => RunWriterM m i | m -> i whereSource
Classifies monads that support collecting the output of a sub-computation.
Methods
collect :: m a -> m (a, i)Source
Collect the output from a computation.
show/hide Instances
class ExceptionM m i => RunExceptionM m i | m -> i whereSource
Classifies monads that support handling of exceptions.
Methods
try :: m a -> m (Either i a)Source
Exceptions are explicit in the result.
show/hide Instances
Deriving functions
data Iso m n Source
A isomorphism between (usually) monads. Typically the constructor and selector of a newtype delcaration.
Constructors
Iso
close :: forall a. m a -> n a
open :: forall a. n a -> m a
derive_fmap :: Functor m => Iso m n -> (a -> b) -> n a -> n bSource
Derive the implementation of fmap from Functor.
derive_return :: Monad m => Iso m n -> a -> n aSource
Derive the implementation of return from Monad.
derive_bind :: Monad m => Iso m n -> n a -> (a -> n b) -> n bSource
Derive the implementation of >>= from Monad.
derive_fail :: Monad m => Iso m n -> String -> n aSource
derive_mfix :: MonadFix m => Iso m n -> (a -> n a) -> n aSource
Derive the implementation of mfix from MonadFix.
derive_ask :: ReaderM m i => Iso m n -> n iSource
Derive the implementation of ask from ReaderM.
derive_put :: WriterM m i => Iso m n -> i -> n ()Source
Derive the implementation of put from WriterM.
derive_get :: StateM m i => Iso m n -> n iSource
Derive the implementation of get from StateM.
derive_set :: StateM m i => Iso m n -> i -> n ()Source
Derive the implementation of set from StateM.
derive_raise :: ExceptionM m i => Iso m n -> i -> n aSource
Derive the implementation of raise from ExceptionM.
derive_callCC :: ContM m => Iso m n -> ((a -> n b) -> n a) -> n aSource
Derive the implementation of callCC from ContM.
derive_local :: RunReaderM m i => Iso m n -> i -> n a -> n aSource
Derive the implementation of local from RunReaderM.
derive_collect :: RunWriterM m i => Iso m n -> n a -> n (a, i)Source
Derive the implementation of collect from RunWriterM.
derive_try :: RunExceptionM m i => Iso m n -> n a -> n (Either i a)Source
Derive the implementation of try from RunExceptionM.
derive_lift :: (MonadT t, Monad m) => Iso (t m) n -> m a -> n aSource
derive_inBase :: BaseM m x => Iso m n -> x a -> n aSource
Miscellaneous
version :: (Int, Int, Int)Source
The current version of the library.
Produced by Haddock version 2.3.0