# The monad K
module Monad.Instance.K {o  e} (ambient : Ambient o  e) where
  open Ambient ambient
  open import Category.Construction.ElgotAlgebras cocartesian
  open import Algebra.Elgot cocartesian using (Elgot-Algebra)
  open import Algebra.Elgot.Free cocartesian using (FreeElgotAlgebra; elgotForgetfulF)
  open import Algebra.Elgot.Stable distributive using (IsStableFreeElgotAlgebra)

  open Equiv
  open MR C
  open M C
  open HomReasoning
## Definition The monad is defined by existence of free uniform-iteration algebras. Since free objects yield and adjunctions, this yields a monad.
  record MonadK : Set (suc o  suc   suc e) where
    field
      freealgebras :  X  FreeElgotAlgebra X
      stable :  X  IsStableFreeElgotAlgebra (freealgebras X)

    -- helper for accessing elgot algebras
    algebras :  (X : Obj)  Elgot-Algebra
    algebras X = FreeObject.FX (freealgebras X)

    freeF : Functor C Elgot-Algebras
    freeF = FO⇒Functor elgotForgetfulF freealgebras
    
    adjoint : freeF  elgotForgetfulF
    adjoint = FO⇒LAdj elgotForgetfulF freealgebras

    monadK : Monad C
    monadK = adjoint⇒monad adjoint
    module monadK = Monad monadK

    kleisliK : KleisliTriple C
    kleisliK = Monad⇒Kleisli C monadK
    module kleisliK = RMonad kleisliK

    module K = Functor monadK.F