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:
The extension operation avoids nested applications of which tends to improve ergonomics.
The extension operation simultaneously encodes both multiplication and its underlying functorial action, which reduces the amount of data that needs to be given explicitly.
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:
A mapping of objects, and
A family of morphisms called the unit of the extension system; and
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
- For every we must have
- For every we must have and
- 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:
- For every we have and
- 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π
module _ {o β} {C : Precategory o β} {E : Extension-system C} where open Cat.Reasoning C open Extension-system E open Extension-algebra-on
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) β©β‘ Ξ±.Ξ½ β)