monad-ran-0.1.0: Fast monads and monad transformersSource codeContentsIndex
Control.Monad.Ran
Portabilitynon-portable (type families, GHC internals)
Stabilityexperimental
Maintainerekmett@gmail.com
Contents
A right Kan extension monad transformer
Representing monads as right Kan extensions
Ran Monad Transformers
Default definitions for common extensions
IO, ST s, STM
The Yoneda Lemma is a right-Kan extension
The codensity monad of a functor is a right-Kan extension
Description

A fast right Kan extension based Monad Transformer that can be used to generate an efficient CPS representation from any combination of monads from the Monad Transformer Library.

To use, just wrap the type of your monad in Ran: i.e. Ran (StateT MyState ReaderT MyEnv IO) Bool and use liftRan :: RanFunctor m => m a -> Ran m a and and lowerRan :: RanFunctor m => Ran m a -> m a to extract your original monad.

This is really just a fancy way of saying that m a is isomorphic to forall o. (a -> f o) -> g o for some definition of f and g that is chosen by m. In practice f and g are built up out of newtypes.

Ran m a is often more efficient than the straightforward monad m because CPS transforming can yield additional optimization opportunities. There are a few caveats to be aware of however. If you inspect the result multiple times then 'Ran m a' may have to recompute its result for each usage. To prevent this, either, use 'Ran m a' once, as in most straight-line monadic code, or explicitly call lowerRan on it and perform your repeated tests against the unlifted monad.

Since Ran m is a data type that depends on type families, Ran cannot be made an instance of MonadTrans, use liftRanT or inRan in place of lift as needed.

Synopsis
data Ran m a = Ran {
getRan :: forall b. (a -> G m b) -> H m b
}
class (Applicative (Ran f), Applicative f, RanFunctor f) => RanApplicative f
class (Monad (Ran f), Monad f, RanFunctor f) => RanMonad f
class RanFunctor f where
type G f :: * -> *
type H f :: * -> *
liftRan :: f a -> Ran f a
lowerRan :: Ran f a -> f a
class RanTrans t where
liftRanT :: (RanFunctor m, RanFunctor (t m)) => Ran m a -> Ran (t m) a
outRan :: (RanFunctor m, RanFunctor (t m)) => Ran (t m) a -> t (Ran m) a
inRan :: (RanFunctor m, RanFunctor (t m)) => t (Ran m) a -> Ran (t m) a
returnRanCodensity :: (RanFunctor m, G m ~ H m) => a -> Ran m a
bindRanCodensity :: (RanFunctor m, G m ~ H m) => Ran m a -> (a -> Ran m b) -> Ran m b
apRanCodensity :: (RanFunctor m, G m ~ H m) => Ran m (a -> b) -> Ran m a -> Ran m b
ranCodensity :: Ran (Codensity f) a -> Codensity f a
codensityRan :: Codensity f a -> Ran (Codensity f) a
liftRanCodensity :: (RanFunctor m, G m ~ H m, Monad (G m)) => G m a -> Ran m a
lowerRanCodensity :: (RanFunctor m, G m ~ H m, Monad (G m)) => Ran m a -> G m a
liftRanWorld :: (G m ~ World w, H m ~ World w) => (State# w -> (#State# w, a#)) -> Ran m a
lowerRanWorld :: (G m ~ World w, H m ~ World w) => Ran m a -> State# w -> (#State# w, a#)
data Yoneda f a = Yoneda {
getYoneda :: forall b. (a -> b) -> f b
}
lowerYoneda :: Yoneda f a -> f a
data Codensity f a = Codensity {
getCodensity :: forall b. (a -> f b) -> f b
}
lowerCodensity :: Monad m => Codensity m a -> m a
lowerCodensityApp :: Applicative f => Codensity f a -> f a
A right Kan extension monad transformer
data Ran m a Source
A right Kan extension transformer for a monad
Constructors
Ran
getRan :: forall b. (a -> G m b) -> H m b
show/hide Instances
MonadRWS r w s m => MonadRWS r w s (Ran (Codensity m))
(RanMonad m, Error e, MonadRWS r w s (Ran m)) => MonadRWS r w s (Ran (ErrorT e m))
MonadRWS r w s f => MonadRWS r w s (Ran (Yoneda f))
MonadError e m => MonadError e (Ran (Codensity m))
Error e => MonadError e (Ran (Either e))
MonadError e f => MonadError e (Ran (Yoneda f))
MonadError IOError (Ran IO)
MonadReader r m => MonadReader r (Ran (Codensity m))
(RanMonad m, Monoid w) => MonadReader r (Ran (RWST r w s m))
(RanMonad m, Monoid w) => MonadReader r (Ran (RWST r w s m))
(RanMonad m, MonadReader e (Ran m)) => MonadReader e (Ran (StateT s m))
(RanMonad m, MonadReader e (Ran m)) => MonadReader e (Ran (StateT s m))
(Monoid w, RanMonad m, MonadReader e (Ran m)) => MonadReader e (Ran (WriterT w m))
(Monoid w, RanMonad m, MonadReader e (Ran m)) => MonadReader e (Ran (WriterT w m))
(RanMonad m, Error e, MonadReader r (Ran m)) => MonadReader r (Ran (ErrorT e m))
RanMonad m => MonadReader r (Ran (ReaderT r m))
MonadReader e (Ran (Reader e))
MonadReader e (Ran ((->) e))
MonadReader r f => MonadReader r (Ran (Yoneda f))
MonadState s m => MonadState s (Ran (Codensity m))
(RanMonad m, Monoid w) => MonadState s (Ran (RWST r w s m))
(RanMonad m, Monoid w) => MonadState s (Ran (RWST r w s m))
RanMonad m => MonadState s (Ran (StateT s m))
RanMonad m => MonadState s (Ran (StateT s m))
(Monoid w, RanMonad m, MonadState s (Ran m)) => MonadState s (Ran (WriterT w m))
(Monoid w, RanMonad m, MonadState s (Ran m)) => MonadState s (Ran (WriterT w m))
(RanMonad m, Error e, MonadState s (Ran m)) => MonadState s (Ran (ErrorT e m))
(RanMonad m, MonadState s (Ran m)) => MonadState s (Ran (ReaderT e m))
MonadState s f => MonadState s (Ran (Yoneda f))
MonadState s (Ran (State s))
MonadWriter w m => MonadWriter w (Ran (Codensity m))
(RanMonad m, Monoid w) => MonadWriter w (Ran (RWST r w s m))
(RanMonad m, Monoid w) => MonadWriter w (Ran (RWST r w s m))
(RanMonad m, MonadWriter w (Ran m)) => MonadWriter w (Ran (StateT s m))
(RanMonad m, MonadWriter w (Ran m)) => MonadWriter w (Ran (StateT s m))
(Monoid w, RanMonad m) => MonadWriter w (Ran (WriterT w m))
(Monoid w, RanMonad m) => MonadWriter w (Ran (WriterT w m))
(RanMonad m, Error e, MonadWriter w (Ran m)) => MonadWriter w (Ran (ErrorT e m))
(RanMonad m, MonadWriter w (Ran m)) => MonadWriter w (Ran (ReaderT e m))
MonadWriter w f => MonadWriter w (Ran (Yoneda f))
Monoid w => MonadWriter w (Ran (Writer w))
Monad (Ran ((->) e))
Monad (Ran IO)
Error e => Monad (Ran (Either e))
Monad (Ran STM)
Monad (Ran (ST s))
Monad (Ran Maybe)
Monad (Ran (Cont r))
Monad (Ran (ContT r m))
(Monoid w, RanMonad m) => Monad (Ran (WriterT w m))
Monoid w => Monad (Ran (Writer w))
(Monoid w, RanMonad m) => Monad (Ran (WriterT w m))
RanMonad m => Monad (Ran (StateT s m))
Monad (Ran (State s))
RanMonad m => Monad (Ran (StateT s m))
Monad (Ran (Reader e))
RanMonad m => Monad (Ran (ReaderT e m))
(RanMonad m, Monoid w) => Monad (Ran (RWST r w s m))
(RanMonad m, Monoid w) => Monad (Ran (RWST r w s m))
(RanMonad m, Error e) => Monad (Ran (ErrorT e m))
Monad (Ran Identity)
Monad f => Monad (Ran (Yoneda f))
Monad (Ran (Codensity f))
RanFunctor f => Functor (Ran f)
MonadFix (Ran IO)
Error e => MonadFix (Ran (Either e))
MonadFix (Ran (ST s))
MonadFix (Ran Maybe)
(Monoid w, RanMonad m, MonadFix (Ran m)) => MonadFix (Ran (WriterT w m))
(Monoid w, RanMonad m, MonadFix (Ran m)) => MonadFix (Ran (WriterT w m))
(RanMonad m, MonadFix (Ran m)) => MonadFix (Ran (StateT s m))
(RanMonad m, MonadFix (Ran m)) => MonadFix (Ran (StateT s m))
(RanMonad m, MonadFix (Ran m)) => MonadFix (Ran (ReaderT e m))
(RanMonad m, Monoid w, MonadFix (Ran m)) => MonadFix (Ran (RWST r w s m))
(RanMonad m, Monoid w, MonadFix (Ran m)) => MonadFix (Ran (RWST r w s m))
(RanMonad m, Error e, MonadFix (Ran m)) => MonadFix (Ran (ErrorT e m))
MonadFix m => MonadFix (Ran (Yoneda m))
MonadFix m => MonadFix (Ran (Codensity m))
MonadPlus (Ran IO)
Error e => MonadPlus (Ran (Either e))
MonadPlus (Ran Maybe)
(Monoid w, RanMonad m, MonadPlus (Ran m)) => MonadPlus (Ran (WriterT w m))
(Monoid w, RanMonad m, MonadPlus (Ran m)) => MonadPlus (Ran (WriterT w m))
(RanMonad m, MonadPlus (Ran m)) => MonadPlus (Ran (StateT s m))
(RanMonad m, MonadPlus (Ran m)) => MonadPlus (Ran (StateT s m))
(RanMonad m, MonadPlus (Ran m)) => MonadPlus (Ran (ReaderT e m))
(RanMonad m, Monoid w, MonadPlus (Ran m)) => MonadPlus (Ran (RWST r w s m))
(RanMonad m, Monoid w, MonadPlus (Ran m)) => MonadPlus (Ran (RWST r w s m))
MonadPlus f => MonadPlus (Ran (Yoneda f))
MonadPlus f => MonadPlus (Ran (Codensity f))
Applicative (Ran ((->) e))
Applicative (Ran IO)
Applicative (Ran STM)
Applicative (Ran (ST s))
Applicative (Ran Maybe)
Applicative (Ran (Cont r))
(Monoid w, RanMonad m) => Applicative (Ran (WriterT w m))
Monoid w => Applicative (Ran (Writer w))
(Monoid w, RanMonad m) => Applicative (Ran (WriterT w m))
RanMonad m => Applicative (Ran (StateT e m))
Applicative (Ran (State s))
RanMonad m => Applicative (Ran (StateT e m))
Applicative (Ran (Reader e))
RanMonad m => Applicative (Ran (ReaderT e m))
(RanMonad m, Monoid w) => Applicative (Ran (RWST r w s m))
(RanMonad m, Monoid w) => Applicative (Ran (RWST r w s m))
(RanMonad m, Error e) => Applicative (Ran (ErrorT e m))
Applicative (Ran Identity)
Applicative f => Applicative (Ran (Yoneda f))
Applicative (Ran (Codensity f))
(Monoid w, RanMonad m, MonadPlus (Ran m)) => Alternative (Ran (WriterT w m))
(Monoid w, RanMonad m, MonadPlus (Ran m)) => Alternative (Ran (WriterT w m))
(RanMonad m, MonadPlus (Ran m)) => Alternative (Ran (StateT s m))
(RanMonad m, MonadPlus (Ran m)) => Alternative (Ran (StateT s m))
(RanMonad m, MonadPlus (Ran m)) => Alternative (Ran (ReaderT e m))
(RanMonad m, MonadPlus (Ran m), Monoid w) => Alternative (Ran (RWST r w s m))
(RanMonad m, MonadPlus (Ran m), Monoid w) => Alternative (Ran (RWST r w s m))
(RanMonad m, Error e, MonadPlus (Ran m)) => Alternative (Ran (ErrorT e m))
Alternative f => Alternative (Ran (Yoneda f))
Alternative (Codensity f) => Alternative (Ran (Codensity f))
MonadCont (Ran (Cont r))
MonadCont (Ran (ContT r m))
MonadIO (Ran IO)
(Monoid w, RanMonad m, MonadIO (Ran m)) => MonadIO (Ran (WriterT w m))
(Monoid w, RanMonad m, MonadIO (Ran m)) => MonadIO (Ran (WriterT w m))
(RanMonad m, MonadIO (Ran m)) => MonadIO (Ran (StateT s m))
(RanMonad m, MonadIO (Ran m)) => MonadIO (Ran (StateT s m))
(RanMonad m, MonadIO (Ran m)) => MonadIO (Ran (ReaderT e m))
(RanMonad m, Monoid w, MonadIO (Ran m)) => MonadIO (Ran (RWST r w s m))
(RanMonad m, Monoid w, MonadIO (Ran m)) => MonadIO (Ran (RWST r w s m))
(RanMonad m, Error e, MonadIO (Ran m)) => MonadIO (Ran (ErrorT e m))
MonadIO f => MonadIO (Ran (Yoneda f))
MonadIO f => MonadIO (Ran (Codensity f))
(Eq a, Eq b) => Eq (Ran (Either a) b)
Eq a => Eq (Ran Maybe a)
(RanFunctor m, Eq (Ran m (Either a b))) => Eq (Ran (ErrorT a m) b)
Eq a => Eq (Ran Identity a)
(Ord a, Ord b) => Ord (Ran (Either a) b)
Ord a => Ord (Ran Maybe a)
(RanFunctor m, Ord (Ran m (Either a b))) => Ord (Ran (ErrorT a m) b)
Ord a => Ord (Ran Identity a)
(Read a, Read b) => Read (Ran (Either a) b)
Read a => Read (Ran Maybe a)
(RanFunctor m, Read (Ran m (Either a b))) => Read (Ran (ErrorT a m) b)
Read a => Read (Ran Identity a)
(Show a, Show b) => Show (Ran (Either a) b)
Show a => Show (Ran Maybe a)
(RanFunctor m, Show (Ran m (Either a b))) => Show (Ran (ErrorT a m) b)
Show a => Show (Ran Identity a)
Monoid m => Monoid (Ran ((->) e) m)
Monoid a => Monoid (Ran Maybe a)
Monoid m => Monoid (Ran (Reader e) m)
Representing monads as right Kan extensions
class (Applicative (Ran f), Applicative f, RanFunctor f) => RanApplicative f Source
class (Monad (Ran f), Monad f, RanFunctor f) => RanMonad f Source
class RanFunctor f whereSource
Associated Types
type G f :: * -> *Source
type H f :: * -> *Source
Methods
liftRan :: f a -> Ran f aSource
lowerRan :: Ran f a -> f aSource
show/hide Instances
Ran Monad Transformers
class RanTrans t whereSource
Methods
liftRanT :: (RanFunctor m, RanFunctor (t m)) => Ran m a -> Ran (t m) aSource
outRan :: (RanFunctor m, RanFunctor (t m)) => Ran (t m) a -> t (Ran m) aSource
inRan :: (RanFunctor m, RanFunctor (t m)) => t (Ran m) a -> Ran (t m) aSource
show/hide Instances
Default definitions for common extensions
returnRanCodensity :: (RanFunctor m, G m ~ H m) => a -> Ran m aSource
bindRanCodensity :: (RanFunctor m, G m ~ H m) => Ran m a -> (a -> Ran m b) -> Ran m bSource
apRanCodensity :: (RanFunctor m, G m ~ H m) => Ran m (a -> b) -> Ran m a -> Ran m bSource
ranCodensity :: Ran (Codensity f) a -> Codensity f aSource
codensityRan :: Codensity f a -> Ran (Codensity f) aSource
liftRanCodensity :: (RanFunctor m, G m ~ H m, Monad (G m)) => G m a -> Ran m aSource
lowerRanCodensity :: (RanFunctor m, G m ~ H m, Monad (G m)) => Ran m a -> G m aSource
IO, ST s, STM
liftRanWorld :: (G m ~ World w, H m ~ World w) => (State# w -> (#State# w, a#)) -> Ran m aSource
lowerRanWorld :: (G m ~ World w, H m ~ World w) => Ran m a -> State# w -> (#State# w, a#)Source
The Yoneda Lemma is a right-Kan extension
data Yoneda f a Source
The Covariant Yoneda lemma applied to a functor. Note that f need not be a Hask Functor!
Constructors
Yoneda
getYoneda :: forall b. (a -> b) -> f b
show/hide Instances
lowerYoneda :: Yoneda f a -> f aSource
The codensity monad of a functor is a right-Kan extension
data Codensity f a Source
The Codensity monad of a functor/monad generated by a functor
Constructors
Codensity
getCodensity :: forall b. (a -> f b) -> f b
show/hide Instances
lowerCodensity :: Monad m => Codensity m a -> m aSource
lowerCodensityApp :: Applicative f => Codensity f a -> f aSource
Produced by Haddock version 2.4.2