kan-extensions-5.2.5: Kan extensions, Kan lifts, the Yoneda lemma, and (co)density (co)monads
Copyright(C) 2008-2016 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
Portabilitynon-portable (rank-2 polymorphism)
Safe HaskellTrustworthy
LanguageHaskell2010

Control.Monad.Codensity

Description

 
Synopsis

Documentation

newtype Codensity (m :: k -> TYPE rep) a Source #

Codensity f is the Monad generated by taking the right Kan extension of any Functor f along itself (Ran f f).

This can often be more "efficient" to construct than f itself using repeated applications of (>>=).

See "Asymptotic Improvement of Computations over Free Monads" by Janis Voigtländer for more information about this type.

https://www.janis-voigtlaender.eu/papers/AsymptoticImprovementOfComputationsOverFreeMonads.pdf

Constructors

Codensity 

Fields

Instances

Instances details
(Functor f, MonadFree f m) => MonadFree f (Codensity m) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

wrap :: f (Codensity m a) -> Codensity m a Source #

MonadReader r m => MonadReader r (Codensity m) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

ask :: Codensity m r Source #

local :: (r -> r) -> Codensity m a -> Codensity m a Source #

reader :: (r -> a) -> Codensity m a Source #

MonadReader r m => MonadState r (Codensity m) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

get :: Codensity m r Source #

put :: r -> Codensity m () Source #

state :: (r -> (a, r)) -> Codensity m a Source #

MonadTrans (Codensity :: (Type -> Type) -> Type -> Type) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

lift :: Monad m => m a -> Codensity m a Source #

MonadFail f => MonadFail (Codensity f) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

fail :: String -> Codensity f a Source #

MonadIO m => MonadIO (Codensity m) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

liftIO :: IO a -> Codensity m a Source #

Alternative v => Alternative (Codensity v) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

empty :: Codensity v a Source #

(<|>) :: Codensity v a -> Codensity v a -> Codensity v a Source #

some :: Codensity v a -> Codensity v [a] Source #

many :: Codensity v a -> Codensity v [a] Source #

Applicative (Codensity f) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

pure :: a -> Codensity f a Source #

(<*>) :: Codensity f (a -> b) -> Codensity f a -> Codensity f b Source #

liftA2 :: (a -> b -> c) -> Codensity f a -> Codensity f b -> Codensity f c Source #

(*>) :: Codensity f a -> Codensity f b -> Codensity f b Source #

(<*) :: Codensity f a -> Codensity f b -> Codensity f a Source #

Functor (Codensity k) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

fmap :: (a -> b) -> Codensity k a -> Codensity k b Source #

(<$) :: a -> Codensity k b -> Codensity k a Source #

Monad (Codensity f) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

(>>=) :: Codensity f a -> (a -> Codensity f b) -> Codensity f b Source #

(>>) :: Codensity f a -> Codensity f b -> Codensity f b Source #

return :: a -> Codensity f a Source #

Alternative v => MonadPlus (Codensity v) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

mzero :: Codensity v a Source #

mplus :: Codensity v a -> Codensity v a -> Codensity v a Source #

Alt v => Alt (Codensity v) Source # 
Instance details

Defined in Control.Monad.Codensity

Apply (Codensity f) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

(<.>) :: Codensity f (a -> b) -> Codensity f a -> Codensity f b Source #

(.>) :: Codensity f a -> Codensity f b -> Codensity f b Source #

(<.) :: Codensity f a -> Codensity f b -> Codensity f a Source #

liftF2 :: (a -> b -> c) -> Codensity f a -> Codensity f b -> Codensity f c Source #

Plus v => Plus (Codensity v) Source # 
Instance details

Defined in Control.Monad.Codensity

Methods

zero :: Codensity v a Source #

lowerCodensity :: Applicative f => Codensity f a -> f a Source #

This serves as the *left*-inverse (retraction) of lift.

lowerCodensity . liftid

In general this is not a full 2-sided inverse, merely a retraction, as Codensity m is often considerably "larger" than m.

e.g. Codensity ((->) s)) a ~ forall r. (a -> s -> r) -> s -> r could support a full complement of MonadState s actions, while (->) s is limited to MonadReader s actions.

codensityToAdjunction :: Adjunction f g => Codensity g a -> g (f a) Source #

The Codensity monad of a right adjoint is isomorphic to the monad obtained from the Adjunction.

codensityToAdjunction . adjunctionToCodensityid
adjunctionToCodensity . codensityToAdjunctionid

codensityToRan :: Codensity g a -> Ran g g a Source #

The Codensity Monad of a Functor g is the right Kan extension (Ran) of g along itself.

codensityToRan . ranToCodensityid
ranToCodensity . codensityToRanid

codensityToComposedRep :: Representable u => Codensity u a -> u (Rep u, a) Source #

The Codensity monad of a representable Functor is isomorphic to the monad obtained from the Adjunction for which that Functor is the right adjoint.

codensityToComposedRep . composedRepToCodensityid
composedRepToCodensity . codensityToComposedRepid
codensityToComposedRep = ranToComposedRep . codensityToRan

wrapCodensity :: (forall a. m a -> m a) -> Codensity m () Source #

Wrap the remainder of the Codensity action using the given function.

This function can be used to register cleanup actions that will be executed at the end. Example:

wrapCodensity (`finally` putStrLn "Done.")

improve :: Functor f => (forall m. MonadFree f m => m a) -> Free f a Source #

Right associate all binds in a computation that generates a free monad

This can improve the asymptotic efficiency of the result, while preserving semantics.

See "Asymptotic Improvement of Computations over Free Monads" by Janis Voightländer for more information about this combinator.

http://www.iai.uni-bonn.de/~jv/mpc08.pdf

Delimited continuations

reset :: Monad m => Codensity m a -> Codensity m a Source #

reset m delimits the continuation of any shift inside m.

shift :: Applicative m => (forall b. (a -> m b) -> Codensity m b) -> Codensity m a Source #

shift f captures the continuation up to the nearest enclosing reset and passes it to f: