StrategyLib-4.0.0.0ContentsIndex
Control.Monad.Run
Contents
Monad algebras
Running monads
Unlifting monad transformers
Monadic choice combinators that confine the partiality effect
Monadic choice
Monadic function choice
Implementation variants
Description

Author : Ralf Laemmel, Joost Visser Stability : experimental Portability : portable

This module is part of StrategyLib, a library of functional strategy combinators, including combinators for generic traversal. This module provides non-strategic functionality for running monads and unlifting monad transformers. In a sense, this is dual to the return and lift functionality of the Monad and MonadTrans classes.

Synopsis
data MaybeAlg a b = MaybeAlg {
nothing :: b
just :: (a -> b)
}
data ErrorAlg e a b = ErrorAlg {
left :: (e -> b)
right :: (a -> b)
}
data ListAlg a b = ListAlg {
nil :: b
cons :: (a -> b -> b)
}
data StateAlg s a b = StateAlg {
first :: s
next :: ((a, s) -> b)
}
class MonadRun s m | m -> s where
run :: s a b -> m a -> b
mrun :: (MonadRun s m, Monad m') => s a b -> m a -> m' b
class MonadUnTrans s t | t -> s where
unlift :: Monad m => s a b -> t m a -> m b
mplus' :: (Monad m, MonadUnTrans MaybeAlg t) => t m b -> m b -> m b
mswitch :: (Monad m, MonadUnTrans MaybeAlg t) => [t m b] -> m b -> m b
mayswitch :: Monad m => [MaybeT m b] -> m b -> m b
mchoice' :: (Monad m, MonadUnTrans MaybeAlg t) => (a -> t m b) -> (a -> m b) -> a -> m b
mchoices :: (Monad m, MonadUnTrans MaybeAlg t, MonadPlus (t m)) => [a -> t m b] -> (a -> m b) -> a -> m b
mswitch0 :: (Monad m, MonadUnTrans MaybeAlg t) => [t m b] -> m b -> m b
mswitch1 :: (Monad m, MonadUnTrans MaybeAlg t) => [t m b] -> m b -> m b
mswitch' :: (Monad m, MonadUnTrans MaybeAlg t, MonadPlus (t m)) => [t m b] -> m b -> m b
Monad algebras
data MaybeAlg a b
The algebra for the partiality effect of Maybe and MaybeT.
Constructors
MaybeAlg
nothing :: b
just :: (a -> b)
show/hide Instances
data ErrorAlg e a b
The algebra for the error effect of Either and ErrorT.
Constructors
ErrorAlg
left :: (e -> b)
right :: (a -> b)
show/hide Instances
data ListAlg a b
The algebra for the non-determinacy effect of '[]' and ListT.
Constructors
ListAlg
nil :: b
cons :: (a -> b -> b)
show/hide Instances
data StateAlg s a b
The algebra for the state effect of State and StateT.
Constructors
StateAlg
first :: sinitial state
next :: ((a, s) -> b)state transformer
show/hide Instances
Running monads
class MonadRun s m | m -> s where
The class of monads for which a run function is defined that executes the computation of the monad.
Methods
run :: s a b -> m a -> b
The overloaded function run takes as first argument an algebra which captures the ingredients necessary to run the particular monad at hand. This algebra is parameterized with the domain and co-domain of run.
show/hide Instances
mrun :: (MonadRun s m, Monad m') => s a b -> m a -> m' b
Exchange one monad by another. This function runs one monad, and puts its value in another. This is basically a monadic version of the run function itself. Note that the two monads are unrelated, so none of the effects of the incoming monad are transferred to the result monad.
Unlifting monad transformers
class MonadUnTrans s t | t -> s where
Just as a base monad can be run to remove the monad, so can a transformed monad be unlifted to remove the transformer and obtain the original monad.
Methods
unlift :: Monad m => s a b -> t m a -> m b
The overloaded function unlift for monad transformers takes as first argument an algebra just like the run function for base monads. For each monad transformer, the same algebra is used as for the base monad of which the transformer is the parameterized variant.
show/hide Instances
Monadic choice combinators that confine the partiality effect
Monadic choice
mplus' :: (Monad m, MonadUnTrans MaybeAlg t) => t m b -> m b -> m b
Monadic choice combinator that confines the partiality effect to the first argument. This is a variation on mplus which allows the partiality effect to spread to both arguments and to the result.
mswitch
:: (Monad m, MonadUnTrans MaybeAlg t)
=> [t m b]choice branches
-> m botherwise
-> m bresult
Monadic choice combinator. Generalization of mplus' that takes a list of choice arguments rather than a single one.
mayswitch :: Monad m => [MaybeT m b] -> m b -> m b
Specialization of mswitch for MaybeT.
Monadic function choice
mchoice' :: (Monad m, MonadUnTrans MaybeAlg t) => (a -> t m b) -> (a -> m b) -> a -> m b
Monadic function choice combinator that confines the partiality effect to the first argument. This is a variation on mchoice which allows the partiality effect to spread to both arguments and to the result.
mchoices :: (Monad m, MonadUnTrans MaybeAlg t, MonadPlus (t m)) => [a -> t m b] -> (a -> m b) -> a -> m b
Monadic function choice combinator. Generalization of mchoice' that takes a list of choice arguments rather than a single one.
Implementation variants
mswitch0 :: (Monad m, MonadUnTrans MaybeAlg t) => [t m b] -> m b -> m b
Implementation variant of mswitch in terms of foldr.
mswitch1 :: (Monad m, MonadUnTrans MaybeAlg t) => [t m b] -> m b -> m b
Implementation variant of mswitch with mplus' expanded:
mswitch' :: (Monad m, MonadUnTrans MaybeAlg t, MonadPlus (t m)) => [t m b] -> m b -> m b
Implementation variant of mswitch where the unlift is postponed to the very end.
Produced by Haddock version 0.8