Control.Monad.Co

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

Copyright | (C) 2011-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 | Safe-Inferred
Language | Haskell2010

Control.Monad.Co

Contents

- Monads from Comonads
- Monad Transformers from Comonads
- Klesili from CoKleisli

Description

Monads from Comonads

http://comonad.com/reader/2011/monads-from-comonads/

`Co` can be viewed as a right Kan lift along a `Comonad`.

In general you can "sandwich" a monad in between two halves of an adjunction. That is to say, if you have an adjunction `F -| G : C -> D` then not only does `GF` form a monad, but `GMF` forms a monad for `M` a monad in `D`. Therefore if we have an adjunction `F -| G : Hask -> Hask^op` then we can lift a `Comonad` in `Hask` which is a `Monad` in `Hask^op` to a `Monad` in `Hask`.

For any `r`, the `Contravariant` functor / presheaf `(-> r)` :: Hask^op -> Hask is adjoint to the "same" `Contravariant` functor `(-> r) :: Hask -> Hask^op`. So we can sandwich a Monad in Hask^op in the middle to obtain `w (a -> r-) -> r+`, and then take a coend over `r` to obtain `forall r. w (a -> r) -> r`. This gives rise to `Co`. If we observe that we didn't care what the choices we made for `r` were to finish this construction, we can upgrade to `forall r. w (a -> m r) -> m r` in a manner similar to how `ContT` is constructed yielding `CoT`.

We could consider unifying the definition of `Co` and `Rift`, but there are many other arguments for which `Rift` can form a `Monad`, and this wouldn't give rise to `CoT`.

Synopsis

- type Co (w :: Type -> Type) = CoT w Identity
- co :: Functor w => (forall r. w (a -> r) -> r) -> Co w a
- runCo :: Functor w => Co w a -> w (a -> r) -> r
- newtype CoT (w :: Type -> Type) (m :: k -> Type) a = CoT {
  - runCoT :: forall (r :: k). w (a -> m r) -> m r
  }
- liftCoT0 :: forall {k} w s (m :: k -> Type). Comonad w => (forall a. w a -> s) -> CoT w m s
- liftCoT0M :: (Comonad w, Monad m) => (forall a. w a -> m s) -> CoT w m s
- lowerCoT0 :: (Functor w, Monad m) => CoT w m s -> w a -> m s
- lowerCo0 :: Functor w => Co w s -> w a -> s
- liftCoT1 :: forall {k} w (m :: k -> Type). (forall a. w a -> a) -> CoT w m ()
- liftCoT1M :: Monad m => (forall a. w a -> m a) -> CoT w m ()
- lowerCoT1 :: (Functor w, Monad m) => CoT w m () -> w a -> m a
- lowerCo1 :: Functor w => Co w () -> w a -> a
- diter :: Functor f => a -> (a -> f a) -> Density (Cofree f) a
- dctrlM :: forall {k} m w. Monad m => (forall (a :: k). w a -> m (w a)) -> CoT (Density w) m ()
- posW :: forall {k} s (w :: Type -> Type) (m :: k -> Type). ComonadStore s w => CoT w m s
- peekW :: forall {k} s (w :: Type -> Type) (m :: k -> Type). ComonadStore s w => s -> CoT w m ()
- peeksW :: forall {k} s (w :: Type -> Type) (m :: k -> Type). ComonadStore s w => (s -> s) -> CoT w m ()
- askW :: forall {k} e (w :: Type -> Type) (m :: k -> Type). ComonadEnv e w => CoT w m e
- asksW :: forall {k} e (w :: Type -> Type) a (m :: k -> Type). ComonadEnv e w => (e -> a) -> CoT w m a
- traceW :: forall {k} e (w :: Type -> Type) (m :: k -> Type). ComonadTraced e w => e -> CoT w m ()

# Monads from Comonads

type Co (w :: Type -> Type) = CoT w Identity #

co :: Functor w => (forall r. w (a -> r) -> r) -> Co w a #

runCo :: Functor w => Co w a -> w (a -> r) -> r #

# Monad Transformers from Comonads

newtype CoT (w :: Type -> Type) (m :: k -> Type) a #

    Co w a ~ Rift w Identity a

Constructors

CoT |
Fields runCoT :: forall (r :: k). w (a -> m r) -> m r

#### Instances

Instances details

(Comonad w, MonadError e m) => MonadError e (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods throwError :: e -> CoT w m a # catchError :: CoT w m a -> (e -> CoT w m a) -> CoT w m a #
(Comonad w, MonadReader e m) => MonadReader e (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods ask :: CoT w m e # local :: (e -> e) -> CoT w m a -> CoT w m a # reader :: (e -> a) -> CoT w m a #
(Comonad w, MonadState s m) => MonadState s (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods get :: CoT w m s # put :: s -> CoT w m () # state :: (s -> (a, s)) -> CoT w m a #
(Comonad w, MonadWriter e m) => MonadWriter e (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods writer :: (a, e) -> CoT w m a # tell :: e -> CoT w m () # listen :: CoT w m a -> CoT w m (a, e) # pass :: CoT w m (a, e -> e) -> CoT w m a #
Comonad w => MonadTrans (CoT w :: (Type -> Type) -> Type -> Type) # |
Instance details Defined in Control.Monad.Co Methods lift :: Monad m => m a -> CoT w m a #
(Comonad w, MonadIO m) => MonadIO (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods liftIO :: IO a -> CoT w m a #
Comonad w => Applicative (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods pure :: a -> CoT w m a # (<*>) :: CoT w m (a -> b) -> CoT w m a -> CoT w m b # liftA2 :: (a -> b -> c) -> CoT w m a -> CoT w m b -> CoT w m c # (*>) :: CoT w m a -> CoT w m b -> CoT w m b # (<*) :: CoT w m a -> CoT w m b -> CoT w m a #
Functor w => Functor (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods fmap :: (a -> b) -> CoT w m a -> CoT w m b # (<$) :: a -> CoT w m b -> CoT w m a #
Comonad w => Monad (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods (>>=) :: CoT w m a -> (a -> CoT w m b) -> CoT w m b # (>>) :: CoT w m a -> CoT w m b -> CoT w m b # return :: a -> CoT w m a #
(Comonad w, MonadFail m) => MonadFail (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods fail :: String -> CoT w m a #
Extend w => Apply (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods (<.>) :: CoT w m (a -> b) -> CoT w m a -> CoT w m b # (.>) :: CoT w m a -> CoT w m b -> CoT w m b # (<.) :: CoT w m a -> CoT w m b -> CoT w m a # liftF2 :: (a -> b -> c) -> CoT w m a -> CoT w m b -> CoT w m c #
Extend w => Bind (CoT w m) # |
Instance details Defined in Control.Monad.Co Methods (>>-) :: CoT w m a -> (a -> CoT w m b) -> CoT w m b # join :: CoT w m (CoT w m a) -> CoT w m a #

# Klesili from CoKleisli

liftCoT0 :: forall {k} w s (m :: k -> Type). Comonad w => (forall a. w a -> s) -> CoT w m s #

liftCoT0M :: (Comonad w, Monad m) => (forall a. w a -> m s) -> CoT w m s #

lowerCoT0 :: (Functor w, Monad m) => CoT w m s -> w a -> m s #

lowerCo0 :: Functor w => Co w s -> w a -> s #

liftCoT1 :: forall {k} w (m :: k -> Type). (forall a. w a -> a) -> CoT w m () #

liftCoT1M :: Monad m => (forall a. w a -> m a) -> CoT w m () #

lowerCoT1 :: (Functor w, Monad m) => CoT w m () -> w a -> m a #

lowerCo1 :: Functor w => Co w () -> w a -> a #

diter :: Functor f => a -> (a -> f a) -> Density (Cofree f) a #

dctrlM :: forall {k} m w. Monad m => (forall (a :: k). w a -> m (w a)) -> CoT (Density w) m () #

posW :: forall {k} s (w :: Type -> Type) (m :: k -> Type). ComonadStore s w => CoT w m s #

peekW :: forall {k} s (w :: Type -> Type) (m :: k -> Type). ComonadStore s w => s -> CoT w m () #

peeksW :: forall {k} s (w :: Type -> Type) (m :: k -> Type). ComonadStore s w => (s -> s) -> CoT w m () #

askW :: forall {k} e (w :: Type -> Type) (m :: k -> Type). ComonadEnv e w => CoT w m e #

asksW :: forall {k} e (w :: Type -> Type) a (m :: k -> Type). ComonadEnv e w => (e -> a) -> CoT w m a #

traceW :: forall {k} e (w :: Type -> Type) (m :: k -> Type). ComonadTraced e w => e -> CoT w m () #