Portability | non-portable (rank-2 polymorphism) |
---|---|

Stability | provisional |

Maintainer | Edward Kmett <ekmett@gmail.com> |

Safe Haskell | Trustworthy |

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 sandwhich 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`

.

- type Co w = 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 m a = CoT {
- runCoT :: forall r. w (a -> m r) -> m r

- liftCoT0 :: Comonad w => (forall a. w a -> 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 a. w a -> a) -> CoT w m ()
- lowerCoT1 :: (Functor w, Monad m) => CoT w m () -> w a -> m a
- lowerCo1 :: Functor w => Co w () -> w a -> a
- posW :: (ComonadStore s w, Monad m) => CoT w m s
- peekW :: (ComonadStore s w, Monad m) => s -> CoT w m ()
- peeksW :: (ComonadStore s w, Monad m) => (s -> s) -> CoT w m ()
- askW :: (ComonadEnv e w, Monad m) => CoT w m e
- asksW :: (ComonadEnv e w, Monad m) => (e -> a) -> CoT w m a
- traceW :: (ComonadTraced e w, Monad m) => e -> CoT w m ()

# Monads from Comonads

# Monad Transformers from Comonads

(Comonad w, MonadState s m) => MonadState s (CoT w m) | |

(Comonad w, MonadReader e m) => MonadReader e (CoT w m) | |

(Comonad w, MonadError e m) => MonadError e (CoT w m) | |

(Comonad w, MonadWriter e m) => MonadWriter e (CoT w m) | |

Comonad w => MonadTrans (CoT w) | |

Comonad w => Monad (CoT w m) | |

Functor w => Functor (CoT w m) | |

Comonad w => Applicative (CoT w m) | |

(Comonad w, MonadIO m) => MonadIO (CoT w m) | |

Extend w => Apply (CoT w m) | |

Extend w => Bind (CoT w m) | |

Comonad w => MonadSpec (CoT w m) |

# Klesili from CoKleisli

posW :: (ComonadStore s w, Monad m) => CoT w m sSource

peekW :: (ComonadStore s w, Monad m) => s -> CoT w m ()Source

peeksW :: (ComonadStore s w, Monad m) => (s -> s) -> CoT w m ()Source

askW :: (ComonadEnv e w, Monad m) => CoT w m eSource

asksW :: (ComonadEnv e w, Monad m) => (e -> a) -> CoT w m aSource

traceW :: (ComonadTraced e w, Monad m) => e -> CoT w m ()Source