Control.Monad.Codensity

kan-extensions-5.2.6: Kan extensions, Kan lifts, the Yoneda lemma, and (co)density (co)monads

Copyright | (C) 2008-2016 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | non-portable (rank-2 polymorphism)
Safe Haskell | Trustworthy
Language | Haskell2010

Control.Monad.Codensity

Contents

- Delimited continuations

Description

Synopsis

- newtype Codensity (m :: k -> TYPE rep) a = Codensity {
  - runCodensity :: forall (b :: k). (a -> m b) -> m b
  }
- lowerCodensity :: Applicative f => Codensity f a -> f a
- codensityToAdjunction :: Adjunction f g => Codensity g a -> g (f a)
- adjunctionToCodensity :: Adjunction f g => g (f a) -> Codensity g a
- codensityToRan :: forall {k} (g :: k -> Type) a. Codensity g a -> Ran g g a
- ranToCodensity :: forall {k} (g :: k -> Type) a. Ran g g a -> Codensity g a
- codensityToComposedRep :: Representable u => Codensity u a -> u (Rep u, a)
- composedRepToCodensity :: Representable u => u (Rep u, a) -> Codensity u a
- wrapCodensity :: (forall (a :: k). m a -> m a) -> Codensity m ()
- improve :: forall (f :: Type -> Type) a. Functor f => (forall (m :: Type -> Type). MonadFree f m => m a) -> Free f a
- reset :: forall (m :: Type -> Type) a. Monad m => Codensity m a -> Codensity m a
- shift :: Applicative m => (forall b. (a -> m b) -> Codensity m b) -> Codensity m a

# Documentation

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

``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 runCodensity :: forall (b :: k). (a -> m b) -> m b

#### Instances

Instances details

(m ~~ m', Functor f, MonadFree f m') => MonadFree f (Codensity m) # |
Instance details Defined in Control.Monad.Codensity Methods wrap :: f (Codensity m a) -> Codensity m a #
(m ~~ m', MonadReader r m') => MonadReader r (Codensity m) # |
Instance details Defined in Control.Monad.Codensity Methods ask :: Codensity m r # local :: (r -> r) -> Codensity m a -> Codensity m a # reader :: (r -> a) -> Codensity m a #
(m ~~ m', MonadReader r m') => MonadState r (Codensity m) # |
Instance details Defined in Control.Monad.Codensity Methods get :: Codensity m r # put :: r -> Codensity m () # state :: (r -> (a, r)) -> Codensity m a #
MonadTrans (Codensity :: (Type -> Type) -> Type -> Type) # |
Instance details Defined in Control.Monad.Codensity Methods lift :: Monad m => m a -> Codensity m a #
(m ~~ m', MonadIO m') => MonadIO (Codensity m) # |
Instance details Defined in Control.Monad.Codensity Methods liftIO :: IO a -> Codensity m a #
(v ~~ v', Alternative v') => Alternative (Codensity v) # |
Instance details Defined in Control.Monad.Codensity Methods empty :: Codensity v a # (<|>) :: Codensity v a -> Codensity v a -> Codensity v a # some :: Codensity v a -> Codensity v [a] # many :: Codensity v a -> Codensity v [a] #
Applicative (Codensity f) # |
Instance details Defined in Control.Monad.Codensity Methods pure :: a -> Codensity f a # (<*>) :: Codensity f (a -> b) -> Codensity f a -> Codensity f b # liftA2 :: (a -> b -> c) -> Codensity f a -> Codensity f b -> Codensity f c # (*>) :: Codensity f a -> Codensity f b -> Codensity f b # (<*) :: Codensity f a -> Codensity f b -> Codensity f a #
Functor (Codensity k) # |
Instance details Defined in Control.Monad.Codensity Methods fmap :: (a -> b) -> Codensity k a -> Codensity k b # (<$) :: a -> Codensity k b -> Codensity k a #
Monad (Codensity f) # |
Instance details Defined in Control.Monad.Codensity Methods (>>=) :: Codensity f a -> (a -> Codensity f b) -> Codensity f b # (>>) :: Codensity f a -> Codensity f b -> Codensity f b # return :: a -> Codensity f a #
(v ~~ v', Alternative v') => MonadPlus (Codensity v) # |
Instance details Defined in Control.Monad.Codensity Methods mzero :: Codensity v a # mplus :: Codensity v a -> Codensity v a -> Codensity v a #
(f ~~ f', MonadFail f') => MonadFail (Codensity f) # |
Instance details Defined in Control.Monad.Codensity Methods fail :: String -> Codensity f a #
(v ~~ v', Alt v') => Alt (Codensity v) # |
Instance details Defined in Control.Monad.Codensity Methods (<!>) :: Codensity v a -> Codensity v a -> Codensity v a # some :: Applicative (Codensity v) => Codensity v a -> Codensity v [a] # many :: Applicative (Codensity v) => Codensity v a -> Codensity v [a] #
Apply (Codensity f) # |
Instance details Defined in Control.Monad.Codensity Methods (<.>) :: Codensity f (a -> b) -> Codensity f a -> Codensity f b # (.>) :: Codensity f a -> Codensity f b -> Codensity f b # (<.) :: Codensity f a -> Codensity f b -> Codensity f a # liftF2 :: (a -> b -> c) -> Codensity f a -> Codensity f b -> Codensity f c #
(v ~~ v', Plus v') => Plus (Codensity v) # |
Instance details Defined in Control.Monad.Codensity Methods zero :: Codensity v a #

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

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

    lowerCodensity . lift ≡ id

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) #

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

    codensityToAdjunction . adjunctionToCodensity ≡ id
    adjunctionToCodensity . codensityToAdjunction ≡ id

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

codensityToRan :: forall {k} (g :: k -> Type) a. Codensity g a -> Ran g g a #

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

    codensityToRan . ranToCodensity ≡ id
    ranToCodensity . codensityToRan ≡ id

ranToCodensity :: forall {k} (g :: k -> Type) a. Ran g g a -> Codensity g a #

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

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 . composedRepToCodensity ≡ id
    composedRepToCodensity . codensityToComposedRep ≡ id

    codensityToComposedRep = ranToComposedRep . codensityToRan

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

    composedRepToCodensity = ranToCodensity . composedRepToRan

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

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 :: forall (f :: Type -> Type) a. Functor f => (forall (m :: Type -> Type). MonadFree f m => m a) -> Free f a #

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 :: forall (m :: Type -> Type) a. Monad m => Codensity m a -> Codensity m a #

``reset` m` delimits the continuation of any `shift` inside `m`.

-     reset (return m) = return m

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

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

-     reset (shift f >>= k) = reset (f (lowerCodensity . k))