Stability | experimental |
---|---|
Maintainer | Ertugrul Soeylemez <es@ertes.de> |
This module implements a number of monad transformers using a CPS approach internally.
- newtype Id a = Id {
- getId :: a
- newtype IdT m a = IdT {
- getIdT :: m a
- type Cont r a = ContT r Id a
- runCont :: (a -> r) -> Cont r a -> r
- evalCont :: Cont r r -> r
- modifyCont :: (r -> r) -> Cont r ()
- newtype ContT r m a = ContT {
- getContT :: (a -> m r) -> m r
- runContT :: (a -> m r) -> ContT r m a -> m r
- evalContT :: Applicative m => ContT r m r -> m r
- modifyContT :: Functor m => (r -> r) -> ContT r m ()
- newtype ChoiceT r i m a = ChoiceT {
- getChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> m r
- runChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> ChoiceT r i m a -> m r
- findFirst :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)
- findAll :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)
- listChoiceT :: Applicative m => ChoiceT [a] [a] m a -> m [a]
- listA :: Alternative f => [a] -> f a
- type State r s a = StateT r s Id a
- runState :: s -> (s -> a -> r) -> State r s a -> r
- evalState :: s -> State r s r -> r
- execState :: s -> State s s a -> s
- newtype StateT r s m a = StateT {
- getStateT :: s -> (s -> a -> m r) -> m r
- runStateT :: s -> (s -> a -> m r) -> StateT r s m a -> m r
- evalStateT :: Applicative m => s -> StateT r s m r -> m r
- execStateT :: Applicative m => s -> StateT s s m a -> m s
- type OldWriter r w a = ContT (r, w) Id a
- runOldWriter :: Monoid w => OldWriter r w r -> (r, w)
- evalOldWriter :: Monoid w => OldWriter r w r -> r
- execOldWriter :: Monoid w => OldWriter r w r -> w
- type OldWriterT r w m a = ContT (r, w) m a
- runOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m (r, w)
- evalOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m r
- execOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m w
- type WriterT = ContT
- runWriterT :: Alternative m => WriterT r m a -> m r
- class Abortable m where
- class CallCC m where
- callCC :: ((a -> m b) -> m a) -> m a
- data Label m a
- labelCC :: (Applicative m, CallCC m) => a -> m (a, Label m a)
- goto :: Applicative m => Label m a -> a -> m b
- class LiftBase m a where
- io :: (LiftBase m a, Base m a ~ IO a) => Base m a -> m a
- class Runnable t r m a where
- class Stateful m where
- getField :: (Functor m, Stateful m) => (StateOf m -> a) -> m a
- modify :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()
- modifyField :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()
- modifyFieldLazy :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()
- modifyLazy :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()
- class Transformer t where
- class Writable m w where
- module Control.Applicative
- module Control.Monad
The identity monad
The identity monad. This monad represents values themselves, i.e. computations without effects.
Monad transformers
Identity transformer
The identity monad transformer. This monad transformer represents computations themselves without further side effects. Unlike most other monad transformers in this module it is not implemented in terms of continuation passing style.
Transformer IdT | |
Runnable IdT r m r | |
Monad m => Monad (IdT m) | |
Functor m => Functor (IdT m) | |
MonadFix m => MonadFix (IdT m) | |
Applicative m => Applicative (IdT m) | |
Alternative m => Alternative (IdT m) | |
(LiftBase m a, Monad m) => LiftBase (IdT m) a |
Basic CPS monads
modifyCont :: (r -> r) -> Cont r ()Source
Modify the result of a CPS computation along the way.
The continuation passing style monad transformer. This monad transformer models the most basic form of CPS.
Transformer (ContT r) | |
Runnable (ContT r) r m a | |
Monad (ContT r m) | |
Functor (ContT r m) | |
Applicative (ContT r m) | |
(Alternative m, Monad m) => Alternative (ContT r m) | |
(Monad m, Stateful m) => Stateful (ContT r m) | |
CallCC (ContT r m) | |
Applicative m => Abortable (ContT r m) | |
(Functor m, Monoid w) => Writable (ContT (r, w) m) w | |
Alternative m => Writable (ContT r m) r | |
(LiftBase m a, Monad m) => LiftBase (ContT r m) a |
runContT :: (a -> m r) -> ContT r m a -> m rSource
Run a CPS-style computation given the supplied final continuation.
evalContT :: Applicative m => ContT r m r -> m rSource
Evaluate a CPS-style computation to its final result.
modifyContT :: Functor m => (r -> r) -> ContT r m ()Source
Transform the final result along the way.
Advanced CPS monads
Choice/nondeterminism
newtype ChoiceT r i m a Source
The choice monad transformer, which models, as the most common interpretation, nondeterminism. Internally a list of choices is represented as a CPS-based left-fold function.
ChoiceT | |
|
Transformer (ChoiceT r i) | |
Monad (ChoiceT r i m) | |
Functor (ChoiceT r i m) | |
Applicative (ChoiceT r i m) | |
Alternative (ChoiceT r i m) | |
(LiftBase m a, Monad m) => LiftBase (ChoiceT r i m) a |
runChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> ChoiceT r i m a -> m rSource
Run a choice computation.
findFirst :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)Source
Find the first solution.
findAll :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)Source
Find all solutions.
listChoiceT :: Applicative m => ChoiceT [a] [a] m a -> m [a]Source
Get list of solutions (faster than findAll
, but returns solutions
in reversed order).
listA :: Alternative f => [a] -> f aSource
Turn a list into a computation with alternatives.
State
Transformer (StateT r s) | |
Runnable (StateT r s) r m a | |
Monad (StateT r s m) | |
Functor (StateT r s m) | |
Applicative (StateT r s m) | |
Alternative m => Alternative (StateT r s m) | |
Stateful (StateT r s m) | |
CallCC (StateT r s m) | |
Applicative m => Abortable (StateT r s m) | |
(Functor m, Monoid w) => Writable (StateT (r, w) s m) w | |
Alternative m => Writable (StateT r s m) r | |
(LiftBase m a, Monad m) => LiftBase (StateT r s m) a |
evalStateT :: Applicative m => s -> StateT r s m r -> m rSource
Run a state transformer returning its result.
execStateT :: Applicative m => s -> StateT s s m a -> m sSource
Run a state transformer returning its final state.
Writer monads
runOldWriter :: Monoid w => OldWriter r w r -> (r, w)Source
Run a traditional writer computation.
evalOldWriter :: Monoid w => OldWriter r w r -> rSource
Run a traditional writer computation and return its result.
execOldWriter :: Monoid w => OldWriter r w r -> wSource
Run a traditional writer computation and return its log.
type OldWriterT r w m a = ContT (r, w) m aSource
The traditional writer monad transformer.
runOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m (r, w)Source
Run a traditional writer transformer.
evalOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m rSource
Run a traditional writer transformer and return its result.
execOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m wSource
Run a traditional writer transformer and return its log.
runWriterT :: Alternative m => WriterT r m a -> m rSource
Run a writer transformer.
Effect classes
Monads supporting abortion.
Applicative m => Abortable (ContT r m) | |
Applicative m => Abortable (StateT r s m) |
Monads supporting *call-with-current-continuation* (aka callCC).
labelCC :: (Applicative m, CallCC m) => a -> m (a, Label m a)Source
Capture the current continuation for later use.
goto :: Applicative m => Label m a -> a -> m bSource
Jump to a label.
io :: (LiftBase m a, Base m a ~ IO a) => Base m a -> m aSource
Handy alias for lifting IO computations.
class Runnable t r m a whereSource
Every monad transformer t
that supports transforming t m a
to
m a
can be an instance of this class.
Stateful monads.
modify :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()Source
Apply a function to the current state.
modifyField :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()Source
Get a field and modify the state.
modifyFieldLazy :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()Source
Get a field and modify the state. Lazy version.
modifyLazy :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()Source
Apply a function to the current state. Lazy version.
class Transformer t whereSource
The monad transformer class. Lifting computations one level down the monad stack, or stated differently promoting a computation of the underlying monad to the transformer.
Transformer IdT | |
Transformer (ContT r) | |
Transformer (StateT r s) | |
Transformer (ChoiceT r i) |
class Writable m w whereSource
Monads with support for logging. Traditionally these are called *writer monads*.
Module reexports
module Control.Applicative
module Control.Monad