HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulekan-extensions-5.2.7Haskell2010

Control.Monad.Co

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.

  • 2 types
  • 18 values

Monads from Comonads

3 declarations
valueco :: Functor w => (forall r. w (a -> r) -> r) -> Co w a
#

Monad Transformers from Comonads

1 declaration
newtypenewtype CoT (w :: Type -> Type) (m :: k -> Type) a
#
Co w a ~ Data.Functor.Kan.Rift.Rift w Identity a

Constructors

  • CoT
    • runCoT :: forall (r :: k). w (a -> m r) -> m r
Instances12MonadError, MonadReader, MonadState, MonadWriter, MonadTrans, Monad, …

Klesili from CoKleisli

16 declarations