contstuff-1.2.4: Fast, easy to use CPS-based monad transformers

Stabilityexperimental
MaintainerErtugrul Soeylemez <es@ertes.de>

Control.ContStuff.Trans

Contents

Description

This module implements a number of monad transformers using a CPS approach internally.

Synopsis

Monad transformers

Identity transformer

newtype IdentityT m a

The trivial monad transformer, which maps a monad to an equivalent monad.

Constructors

IdentityT 

Fields

runIdentityT :: m a
 

ContT

newtype ContT r m a Source

The continuation passing style monad transformer. This monad transformer models the most basic form of CPS.

Constructors

ContT 

Fields

getContT :: (a -> m r) -> m r
 

Instances

MonadTrans (ContT r) 
Monad (ContT r m) 
Functor (ContT r m) 
Alternative m => MonadPlus (ContT r m) 
Applicative (ContT r m) 
Alternative m => Alternative (ContT r m) 
MonadIO m => MonadIO (ContT r m) 
(Monad m, Stateful m) => Stateful (ContT r m) 
(Monad m, Readable m) => Readable (ContT r m) 
Forkable m => Forkable (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 

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.

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.

Constructors

ChoiceT 

Fields

getChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> m r
 

Instances

MonadTrans (ChoiceT r i) 
LiftFunctor (ChoiceT r i) 
Monad (ChoiceT r i m) 
Functor (ChoiceT r i m) 
MonadPlus (ChoiceT r i m) 
Applicative (ChoiceT r i m) 
Alternative (ChoiceT r i m) 
MonadIO m => MonadIO (ChoiceT r i m) 
(Monad m, Stateful m) => Stateful (ChoiceT r i m) 
(Monad m, Readable m) => Readable (ChoiceT r i m) 
Forkable m => Forkable (ChoiceT r i m) 
Applicative m => Abortable (ChoiceT r i m) 

runChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> ChoiceT r i m a -> m rSource

Run a choice computation.

choice :: [a] -> ChoiceT r i m aSource

Turn a list into a ChoiceT computation efficiently.

findAll :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)Source

Find all solutions.

findAll_ :: Applicative m => ChoiceT r i m a -> m ()Source

Find all solutions and ignore them.

findFirst :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)Source

Find the first solution.

findFirst_ :: Applicative m => ChoiceT r i m a -> m ()Source

Find the first solution and ignore it.

listA :: Alternative f => [a] -> f aSource

Turn a list into a computation with alternatives.

listChoiceT :: Applicative m => ChoiceT [a] [a] m a -> m [a]Source

Get list of solutions (faster than findAll, but returns solutions in reversed order).

maybeChoiceT :: Applicative m => ChoiceT (Maybe a) (Maybe a) m a -> m (Maybe a)Source

Get one solution (faster than findFirst).

Exceptions

newtype EitherT r e m a Source

Monad transformer for CPS computations with an additional exception continuation.

Constructors

EitherT 

Fields

getEitherT :: (a -> m r) -> (e -> m r) -> m r
 

Instances

MonadTrans (EitherT r e) 
LiftFunctor (EitherT r e) 
Monad (EitherT r e m) 
Functor (EitherT r e m) 
Alternative m => MonadPlus (EitherT r e m) 
Applicative (EitherT r e m) 
Alternative m => Alternative (EitherT r e m) 
MonadIO m => MonadIO (EitherT r e m) 
(Monad m, Stateful m) => Stateful (EitherT r e m) 
(Monad m, Readable m) => Readable (EitherT r e m) 
HasExceptions (EitherT r e m) 
Forkable m => Forkable (EitherT r e m) 
CallCC (EitherT r e m) 
Applicative m => Abortable (EitherT r e m) 
(Functor m, Monoid w) => Writable (EitherT (r, w) e m) w 
Alternative m => Writable (EitherT r e m) r 

runEitherT :: (a -> m r) -> (e -> m r) -> EitherT r e m a -> m rSource

Run an EitherT transformer.

evalEitherT :: Applicative m => EitherT (Either e a) e m a -> m (Either e a)Source

Run an EitherT transformer returning an Either result.

modifyEitherT :: Functor m => (r -> r) -> EitherT r e m ()Source

Modify the result of an EitherT computation along the way.

testEitherT :: Applicative m => EitherT Bool e m a -> m BoolSource

Run the EitherT computation and return True, if it results in a right value, False otherwise.

newtype MaybeT r m a Source

Monad transformer for CPS computations with an additional exception continuation with no argument.

Constructors

MaybeT 

Fields

getMaybeT :: (a -> m r) -> m r -> m r
 

Instances

MonadTrans (MaybeT r) 
LiftFunctor (MaybeT r) 
Monad (MaybeT r m) 
Functor (MaybeT r m) 
Alternative m => MonadPlus (MaybeT r m) 
Applicative (MaybeT r m) 
Alternative (MaybeT r m) 
MonadIO m => MonadIO (MaybeT r m) 
(Monad m, Stateful m) => Stateful (MaybeT r m) 
(Monad m, Readable m) => Readable (MaybeT r m) 
HasExceptions (MaybeT r m) 
Forkable m => Forkable (MaybeT r m) 
CallCC (MaybeT r m) 
Applicative m => Abortable (MaybeT r m) 
(Functor m, Monoid w) => Writable (MaybeT (r, w) m) w 
Alternative m => Writable (MaybeT r m) r 

runMaybeT :: (a -> m r) -> m r -> MaybeT r m a -> m rSource

Run a MaybeT transformer.

evalMaybeT :: Applicative m => MaybeT (Maybe a) m a -> m (Maybe a)Source

Run a MaybeT transformer returning a Maybe result.

modifyMaybeT :: Functor m => (r -> r) -> MaybeT r m ()Source

Modify the result of a MaybeT computation along the way.

testMaybeT :: Applicative m => MaybeT Bool m a -> m BoolSource

Run the MaybeT computation and return True, if it results in a Just value, False otherwise.

State

data ReaderT e m a Source

Monad transformer for computations with readable environment. Unlike the other monad transformers this one allows no CPS effects and also hides its constructors, which makes it commutative.

If you need CPS effects, consider using StateT.

Instances

runReaderT :: Applicative m => e -> ReaderT e m a -> m aSource

Run a computation with environment.

newtype StateT r s m a Source

Monad transformer for stateful computations.

Constructors

StateT 

Fields

getStateT :: (a -> s -> m r) -> s -> m r
 

Instances

MonadTrans (StateT r s) 
Monad (StateT r s m) 
Functor (StateT r s m) 
Alternative m => MonadPlus (StateT r s m) 
Applicative (StateT r s m) 
Alternative m => Alternative (StateT r s m) 
MonadIO m => MonadIO (StateT r s m) 
Stateful (StateT r s m) 
Readable (StateT r s m) 
Forkable m => Forkable (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 

runStateT :: s -> (a -> s -> m r) -> StateT r s m a -> m rSource

Run a state transformer.

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

type WriterT = ContTSource

The writer monad transformer. Supports logging effects.

runWriterT :: Alternative m => WriterT r m a -> m rSource

Run a writer transformer.

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.