module Cat.Diagram.Monad.Extension where

Extension systemsπŸ”—

An extension system on a category is an alternative way of presenting a monad on As the name suggests, extension systems are built around an extension operation, of type in place of the multiplication operation that characterises a monad. This has a couple of immediate benefits:

  1. The extension operation avoids nested applications of which tends to improve ergonomics.

  2. The extension operation simultaneously encodes both multiplication and its underlying functorial action, which reduces the amount of data that needs to be given explicitly.

  3. It is not immediately clear how to generalize monads beyond endofunctors. In constrast, extension systems can be readily generalized to relative extension systems1.

With that bit of motivation out of the way, we shall proceed to define extension systems. An extension system consists of:

  1. A mapping of objects, and

  2. A family of morphisms called the unit of the extension system; and

  3. The extension operation, Gesturing towards the β€œmonads” found in functional programming, we will call this operation bind.

Note that we do not require the mapping of objects to be functorial, nor the do we require the unit to be natural. Instead, we impose 3 equations on this structure:2

  1. For every we must have
  2. For every we must have and
  3. For every and we must have

For reasons of generality, we shall define extension systems as relative extension systems, along the identity functor. Even though we use a more general definition, the data contained in an extension system is precisely the data listed above.

Extension-system : βˆ€ {o β„“} β†’ Precategory o β„“ β†’ Type (o βŠ” β„“)
Extension-system C = Relative-extension-system (Id {C = C})

module Extension-system {o β„“} {C : Precategory o β„“} (E : Extension-system C) where
  open Cat.Reasoning C
  open Relative-extension-system E public

We can recover the monad’s multiplication by extending the identity morphism

  join : βˆ€ {x} β†’ Hom (Mβ‚€ (Mβ‚€ x)) (Mβ‚€ x)
  join {x} = bind (id {Mβ‚€ x})

Naturality of join also follows, though is a bit more involved.

  join-natural
    : βˆ€ {x y} (f : Hom x y)
    β†’ join ∘ M₁ (M₁ f) ≑ M₁ f ∘ join
  join-natural f =
    bind id ∘ bind (unit ∘ bind (unit ∘ f)) β‰‘βŸ¨ bind-∘ id (unit ∘ bind (unit ∘ f)) βŸ©β‰‘
    bind (bind id ∘ unit ∘ bind (unit ∘ f)) β‰‘βŸ¨ ap bind (cancell (bind-unit-∘ id) βˆ™ sym (idr _)) βŸ©β‰‘
    bind (bind (unit ∘ f) ∘ id)             β‰‘Λ˜βŸ¨ bind-∘ (unit ∘ f) id βŸ©β‰‘Λ˜
    bind (unit ∘ f) ∘ bind id               ∎

Equivalence with monadsπŸ”—

It remains to show that monads on are equivalent to extension systems on We’ll start with the forward direction. Throughout, let be a fixed monad on

  Monad→Extension-system : Monad C → Extension-system C
  Monad→Extension-system M = system where
    module M = Monad M
    open Extension-system

The mapping of objects, and the unit, are directly given by (the object part of) and by the unit natural transformation.

    system : Extension-system C
    system .Mβ‚€ = M.Mβ‚€
    system .unit = M.Ξ· _

Defining the extension operation is slightly trickier, but not by much. If we have a morphism then its extension is defined to be composite

    system .bind f = M.ΞΌ _ ∘ M.M₁ f

Finally, a few short computations show that this definition is lawful.

    system .bind-unit-id =
      M.ΞΌ _ ∘ M.M₁ (M.Ξ· _) β‰‘βŸ¨ M.left-ident βŸ©β‰‘
      id                             ∎
    system .bind-unit-∘ f =
      (M.ΞΌ _ ∘ M.M₁ f) ∘ M.Ξ· _ β‰‘βŸ¨ pullr (sym $ M.unit.is-natural _ _ _) βŸ©β‰‘
      M.ΞΌ _ ∘ M.Ξ· _ ∘ f        β‰‘βŸ¨ cancell M.right-ident βŸ©β‰‘
      f                        ∎
    system .bind-∘ f g =
      (M.ΞΌ _ ∘ M.M₁ f) ∘ (M.ΞΌ _ ∘ M.M₁ g)             β‰‘βŸ¨ pullr (extendl (sym $ M.mult.is-natural _ _ _)) βŸ©β‰‘
      M.ΞΌ _ ∘ M.ΞΌ _ ∘ (M.M₁ (M.M₁ f) ∘ M.M₁ g)        β‰‘βŸ¨ extendl (sym M.mult-assoc) βŸ©β‰‘
      M.ΞΌ _ ∘ M.M₁ (M.ΞΌ _) ∘ (M.M₁ (M.M₁ f) ∘ M.M₁ g) β‰‘βŸ¨ apβ‚‚ _∘_ refl (pulll (sym (M.M-∘ _ _)) βˆ™ sym (M.M-∘ _ _)) βŸ©β‰‘
      M.ΞΌ _ ∘ M.M₁ ((M.ΞΌ _ ∘ M.M₁ f) ∘ g)             ∎

Constructing a monad from an extension system is simply a matter of bolting together our results from the previous section.

  Extension-system→Monad : Extension-system C → Monad C
  Extension-system→Monad E = monad where
    module E = Extension-system E
    open Monad

    monad : Monad C
    monad .M = E.M
    monad .unit .Ξ· x = E.unit
    monad .unit .is-natural _ _ f = E.unit-natural f
    monad .mult .Ξ· x = E.join
    monad .mult .is-natural _ _ f = E.join-natural f

The monad laws follow from another short series of computations.

    monad .left-ident =
      E.bind id ∘ E.bind (E.unit ∘ E.unit) β‰‘βŸ¨ E.bind-∘ _ _ βŸ©β‰‘
      E.bind (E.bind id ∘ E.unit ∘ E.unit) β‰‘βŸ¨ ap E.bind (cancell (E.bind-unit-∘ id)) βŸ©β‰‘
      E.bind E.unit                        β‰‘βŸ¨ E.bind-unit-id βŸ©β‰‘
      id                                   ∎
    monad .right-ident =
      E.bind id ∘ E.unit β‰‘βŸ¨ E.bind-unit-∘ id βŸ©β‰‘
      id                 ∎
    monad .mult-assoc =
      E.bind id ∘ E.bind (E.unit ∘ E.bind id) β‰‘βŸ¨ E.bind-∘ _ _ βŸ©β‰‘
      E.bind (E.bind id ∘ E.unit ∘ E.bind id) β‰‘βŸ¨ ap E.bind (cancell (E.bind-unit-∘ id) βˆ™ sym (idr _)) βŸ©β‰‘
      E.bind (E.bind id ∘ id)                 β‰‘Λ˜βŸ¨ E.bind-∘ _ _ βŸ©β‰‘Λ˜
      E.bind id ∘ E.bind id                   ∎

Moreover, these two functions constitute an equivalence between monads on and extension systems on In light of this fact, we will silently identify monads and extension systems, whenever it is convenient.

  Monad≃Extension-system : Monad C ≃ Extension-system C
  Monad≃Extension-system = Isoβ†’Equiv $
    Monad→Extension-system ,
    iso Extension-system→Monad
      (Ξ» E β†’
        let module E = Extension-system E in
        Relative-extension-system-path
          (Ξ» _ β†’ refl)
          (Ξ» _ β†’ refl)
          (Ξ» f β†’
            E.bind id ∘ E.bind (E.unit ∘ f i0) β‰‘βŸ¨ E.bind-∘ id (E.unit ∘ f i0) βŸ©β‰‘
            E.bind (E.bind id ∘ E.unit ∘ f i0) β‰‘βŸ¨ ap E.bind (cancell (E.bind-unit-∘ id)) βŸ©β‰‘
            E.bind (f i0)                      β‰‘βŸ¨ ap E.bind (coe0β†’1 (Ξ» i β†’ f i0 ≑ f i) refl) βŸ©β‰‘
            E.bind (f i1)                      ∎))
      (Ξ» M β†’
        let module M = Monad M in
        Monad-path
          (Ξ» _ β†’ refl)
          (Ξ» f β†’
            M.ΞΌ _ ∘ M.M₁ (M.Ξ· _ ∘ f)        β‰‘βŸ¨ pushr (M.M-∘ _ _) βŸ©β‰‘
            (M.ΞΌ _ ∘ M.M₁ (M.Ξ· _)) ∘ M.M₁ f β‰‘βŸ¨ eliml M.left-ident βŸ©β‰‘
            M.M₁ f ∎)
          (Ξ» _ β†’ refl)
          (Ξ» _ β†’ elimr M.M-id))

Algebras over an extension systemπŸ”—

An extension algebra over is the extension system analog of a algebra over a monad. Following the general theme of extension operations, an extension algebra on is given by an operation Intuitively, this operation lets us β€œevaluate” any so long as the codomain of the evaluation is

This operation must satisfy a pair of equations:

  1. For every we have and
  2. For every and we have

As with extension systems, we define extension algebras in terms of relative extension algebras.

Extension-algebra-on
  : βˆ€ {o β„“} {C : Precategory o β„“}
  β†’ (open Precategory C)
  β†’ Extension-system C
  β†’ Ob
  β†’ Type (o βŠ” β„“)
Extension-algebra-on = Relative-algebra-on

module Extension-algebra-on
  {o β„“} {C : Precategory o β„“} (open Cat.Reasoning C)
  {E : Extension-system C} {x : Ob} (Ξ± : Extension-algebra-on E x)
  where
  open Extension-system E
  open Relative-algebra-on Ξ± public

The evaluation map also interacts well with the multiplication.

  Ξ½-join : βˆ€ {a} (f : Hom a x) β†’ Ξ½ f ∘ join ≑ Ξ½ (Ξ½ f)
  Ξ½-join f =
    Ξ½ f ∘ bind id β‰‘βŸ¨ Ξ½-bind f id βŸ©β‰‘
    Ξ½ (Ξ½ f ∘ id)  β‰‘βŸ¨ ap Ξ½ (idr _) βŸ©β‰‘
    ν (ν f)       ∎

Equivalence with monad algebrasπŸ”—

As the name suggests, extension algebras over are equivalent to monad algebras over the canonical monad associated with

For the forward direction, let be a monad algebra over We can obtain the required extension operation by sending each to the composite

  Algebra-on→Extension-algebra-on
    : βˆ€ {x}
    → Algebra-on C (Extension-system→Monad E) x
    β†’ Extension-algebra-on E x
  Algebra-on→Extension-algebra-on {x = x} α = ext-alg where
    module Ξ± = Algebra-on Ξ±
    open Extension-algebra-on

    ext-alg : Extension-algebra-on E x
    ext-alg .Ξ½ f = Ξ±.Ξ½ ∘ M₁ f
The monad algebra laws follow from some tedious calculations that we shall omit.
    ext-alg .Ξ½-unit f =
      (Ξ±.Ξ½ ∘ M₁ f) ∘ unit β‰‘βŸ¨ pullr (sym $ unit-natural f) βŸ©β‰‘
      Ξ±.Ξ½ ∘ unit ∘ f      β‰‘βŸ¨ cancell Ξ±.Ξ½-unit βŸ©β‰‘
      f ∎
    ext-alg .Ξ½-bind f g =
      (Ξ±.Ξ½ ∘ M₁ f) ∘ bind g                    β‰‘βŸ¨ pullr (bind-naturall _ _) βŸ©β‰‘
      Ξ±.Ξ½ ∘ bind ⌜ M₁ f ∘ g ⌝                  β‰‘βŸ¨ ap! (insertl (bind-unit-∘ id)) βŸ©β‰‘
      Ξ±.Ξ½ ∘ bind (join ∘ unit ∘ M₁ f ∘ g)      β‰‘βŸ¨ pushr (sym (bind-∘ _ _)) βŸ©β‰‘
      (Ξ±.Ξ½ ∘ join) ∘ bind (unit ∘ M₁ f ∘ g)    β‰‘βŸ¨ pushl (sym $ Ξ±.Ξ½-mult) βŸ©β‰‘
      Ξ±.Ξ½ ∘ M₁ Ξ±.Ξ½ ∘ bind (unit ∘ M₁ f ∘ g)    β‰‘βŸ¨ ap (Ξ±.Ξ½ ∘_) (bind-naturall _ _) βŸ©β‰‘
      Ξ±.Ξ½ ∘ bind ⌜ M₁ Ξ±.Ξ½ ∘ unit ∘ M₁ f ∘ g ⌝  β‰‘βŸ¨ ap! (centralizel (sym $ unit-natural _)) βŸ©β‰‘
      Ξ±.Ξ½ ∘ bind (unit ∘ (Ξ±.Ξ½ ∘ M₁ f) ∘ g)     ∎

Conversely, a monad algebra over can be derived from an extension algebra over by restricting to

  Extension-algebra-on→Algebra-on
    : βˆ€ {x}
    β†’ Extension-algebra-on E x
    → Algebra-on C (Extension-system→Monad E) x
  Extension-algebra-on→Algebra-on {x = x} α = alg where
    module Ξ± = Extension-algebra-on Ξ±
    open Algebra-on

    alg : Algebra-on C (Extension-system→Monad E) x
    alg .Ξ½ = Ξ±.Ξ½ id

The proofs of the monad algebra laws are mercifully short.

    alg .Ξ½-unit = Ξ±.Ξ½-unit id
    alg .Ξ½-mult =
      Ξ±.Ξ½ id ∘ M₁ (Ξ±.Ξ½ id) β‰‘βŸ¨ Ξ±.Ξ½-natural _ _ βŸ©β‰‘
      Ξ±.Ξ½ (id ∘ Ξ±.Ξ½ id)    β‰‘βŸ¨ ap Ξ±.Ξ½ (idl _) βŸ©β‰‘
      Ξ±.Ξ½ (Ξ±.Ξ½ id)         β‰‘Λ˜βŸ¨ Ξ±.Ξ½-join id βŸ©β‰‘Λ˜
      α.ν id ∘ join ∎

As expected, these two functions constitute an equivalence between monad algebras and extension algebras.

  Algebra-on≃Extension-algebra-on
    : βˆ€ {x}
    β†’ Algebra-on C (Extension-systemβ†’Monad E) x ≃ Extension-algebra-on E x
  Algebra-on≃Extension-algebra-on {x} = Isoβ†’Equiv $
    Algebra-on→Extension-algebra-on ,
    iso Extension-algebra-on→Algebra-on
      (Ξ» Ξ± β†’
        let module Ξ± = Extension-algebra-on Ξ± in
        Relative-algebra-on-pathp refl Ξ» f β†’
          Ξ±.Ξ½ id ∘ M₁ (f i0) β‰‘βŸ¨ Ξ±.Ξ½-natural _ _ βŸ©β‰‘
          Ξ±.Ξ½ (id ∘ f i0)    β‰‘βŸ¨ ap Ξ±.Ξ½ (idl _) βŸ©β‰‘
          Ξ±.Ξ½ (f i0)         β‰‘βŸ¨ ap Ξ±.Ξ½ (coe0β†’1 (Ξ» i β†’ f i0 ≑ f i) refl) βŸ©β‰‘
          α.ν (f i1)         ∎)
      (Ξ» Ξ± β†’
        let module Ξ± = Algebra-on Ξ± in
        Algebra-on-pathp C refl $
          Ξ±.Ξ½ ∘ bind (unit ∘ id) β‰‘βŸ¨ elimr (ap bind (idr _) βˆ™ bind-unit-id) βŸ©β‰‘
          α.ν                    ∎)

  1. In fact, we will define extension systems as a special case of relative extension systems!β†©οΈŽ

  2. contrast this with the 7 equations required for a monad: 2 for functoriality, 2 for naturality, and 3 for unitality/associativityβ†©οΈŽ