2018-03-12 13:20:49 +00:00
|
|
|
|
{---
|
|
|
|
|
The Kleisli formulation of monads
|
|
|
|
|
---}
|
2018-05-08 16:34:12 +00:00
|
|
|
|
{-# OPTIONS --cubical #-}
|
2018-03-12 13:20:49 +00:00
|
|
|
|
open import Agda.Primitive
|
|
|
|
|
|
2018-03-21 13:56:43 +00:00
|
|
|
|
open import Cat.Prelude
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
open import Cat.Category
|
|
|
|
|
open import Cat.Category.Functor as F
|
|
|
|
|
open import Cat.Categories.Fun
|
|
|
|
|
|
|
|
|
|
-- "A monad in the Kleisli form" [voe]
|
|
|
|
|
module Cat.Category.Monad.Kleisli {ℓa ℓb : Level} (ℂ : Category ℓa ℓb) where
|
2018-04-03 09:36:09 +00:00
|
|
|
|
open import Cat.Category.NaturalTransformation ℂ ℂ
|
|
|
|
|
using (NaturalTransformation ; Transformation ; Natural)
|
|
|
|
|
|
2018-03-12 13:20:49 +00:00
|
|
|
|
private
|
|
|
|
|
ℓ = ℓa ⊔ ℓb
|
|
|
|
|
module ℂ = Category ℂ
|
2018-04-11 08:58:50 +00:00
|
|
|
|
open ℂ using (Arrow ; identity ; Object ; _<<<_ ; _>>>_)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
-- | Data for a monad.
|
|
|
|
|
--
|
|
|
|
|
-- Note that (>>=) is not expressible in a general category because objects
|
|
|
|
|
-- are not generally types.
|
|
|
|
|
record RawMonad : Set ℓ where
|
|
|
|
|
field
|
|
|
|
|
omap : Object → Object
|
|
|
|
|
pure : {X : Object} → ℂ [ X , omap X ]
|
|
|
|
|
bind : {X Y : Object} → ℂ [ X , omap Y ] → ℂ [ omap X , omap Y ]
|
|
|
|
|
|
|
|
|
|
-- | functor map
|
|
|
|
|
--
|
|
|
|
|
-- This should perhaps be defined in a "Klesli-version" of functors as well?
|
|
|
|
|
fmap : ∀ {A B} → ℂ [ A , B ] → ℂ [ omap A , omap B ]
|
2018-04-11 08:58:50 +00:00
|
|
|
|
fmap f = bind (pure <<< f)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
-- | Composition of monads aka. the kleisli-arrow.
|
|
|
|
|
_>=>_ : {A B C : Object} → ℂ [ A , omap B ] → ℂ [ B , omap C ] → ℂ [ A , omap C ]
|
|
|
|
|
f >=> g = f >>> (bind g)
|
|
|
|
|
|
|
|
|
|
-- | Flattening nested monads.
|
|
|
|
|
join : {A : Object} → ℂ [ omap (omap A) , omap A ]
|
2018-04-03 09:36:09 +00:00
|
|
|
|
join = bind identity
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
------------------
|
|
|
|
|
-- * Monad laws --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
-- There may be better names than what I've chosen here.
|
|
|
|
|
|
2018-05-01 09:33:12 +00:00
|
|
|
|
-- `pure` is the neutral element for `bind`
|
2018-03-12 13:20:49 +00:00
|
|
|
|
IsIdentity = {X : Object}
|
2018-04-03 09:36:09 +00:00
|
|
|
|
→ bind pure ≡ identity {omap X}
|
2018-05-01 09:33:12 +00:00
|
|
|
|
-- pure is the left-identity for the kleisli arrow.
|
2018-03-12 13:20:49 +00:00
|
|
|
|
IsNatural = {X Y : Object} (f : ℂ [ X , omap Y ])
|
2018-05-01 09:33:12 +00:00
|
|
|
|
→ pure >=> f ≡ f
|
|
|
|
|
-- Composition interacts with bind in the following way.
|
2018-05-01 16:54:08 +00:00
|
|
|
|
IsDistributive = {X Y Z : Object}
|
|
|
|
|
(g : ℂ [ Y , omap Z ]) (f : ℂ [ X , omap Y ])
|
2018-03-12 13:20:49 +00:00
|
|
|
|
→ (bind f) >>> (bind g) ≡ bind (f >=> g)
|
|
|
|
|
|
2018-05-01 09:33:12 +00:00
|
|
|
|
RightIdentity = {A B : Object} {m : ℂ [ A , omap B ]}
|
|
|
|
|
→ m >=> pure ≡ m
|
|
|
|
|
|
2018-03-12 13:20:49 +00:00
|
|
|
|
-- | Functor map fusion.
|
|
|
|
|
--
|
|
|
|
|
-- This is really a functor law. Should we have a kleisli-representation of
|
|
|
|
|
-- functors as well and make them a super-class?
|
|
|
|
|
Fusion = {X Y Z : Object} {g : ℂ [ Y , Z ]} {f : ℂ [ X , Y ]}
|
2018-04-11 08:58:50 +00:00
|
|
|
|
→ fmap (g <<< f) ≡ fmap g <<< fmap f
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
-- In the ("foreign") formulation of a monad `IsNatural`'s analogue here would be:
|
|
|
|
|
IsNaturalForeign : Set _
|
2018-04-11 08:58:50 +00:00
|
|
|
|
IsNaturalForeign = {X : Object} → join {X} <<< fmap join ≡ join <<< join
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
IsInverse : Set _
|
2018-04-11 08:58:50 +00:00
|
|
|
|
IsInverse = {X : Object} → join {X} <<< pure ≡ identity × join {X} <<< fmap pure ≡ identity
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
record IsMonad (raw : RawMonad) : Set ℓ where
|
|
|
|
|
open RawMonad raw public
|
|
|
|
|
field
|
|
|
|
|
isIdentity : IsIdentity
|
|
|
|
|
isNatural : IsNatural
|
|
|
|
|
isDistributive : IsDistributive
|
|
|
|
|
|
|
|
|
|
-- | Map fusion is admissable.
|
|
|
|
|
fusion : Fusion
|
|
|
|
|
fusion {g = g} {f} = begin
|
2018-04-11 08:58:50 +00:00
|
|
|
|
fmap (g <<< f) ≡⟨⟩
|
|
|
|
|
bind ((f >>> g) >>> pure) ≡⟨ cong bind ℂ.isAssociative ⟩
|
|
|
|
|
bind (f >>> (g >>> pure))
|
|
|
|
|
≡⟨ cong (λ φ → bind (f >>> φ)) (sym (isNatural _)) ⟩
|
|
|
|
|
bind (f >>> (pure >>> (bind (g >>> pure))))
|
|
|
|
|
≡⟨⟩
|
2018-03-12 13:20:49 +00:00
|
|
|
|
bind (f >>> (pure >>> fmap g)) ≡⟨⟩
|
2018-04-11 08:58:50 +00:00
|
|
|
|
bind ((fmap g <<< pure) <<< f) ≡⟨ cong bind (sym ℂ.isAssociative) ⟩
|
|
|
|
|
bind (fmap g <<< (pure <<< f)) ≡⟨ sym distrib ⟩
|
|
|
|
|
bind (pure <<< g) <<< bind (pure <<< f)
|
|
|
|
|
≡⟨⟩
|
|
|
|
|
fmap g <<< fmap f ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
where
|
2018-04-11 08:58:50 +00:00
|
|
|
|
distrib : fmap g <<< fmap f ≡ bind (fmap g <<< (pure <<< f))
|
|
|
|
|
distrib = isDistributive (pure <<< g) (pure <<< f)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
-- | This formulation gives rise to the following endo-functor.
|
|
|
|
|
private
|
|
|
|
|
rawR : RawFunctor ℂ ℂ
|
|
|
|
|
RawFunctor.omap rawR = omap
|
|
|
|
|
RawFunctor.fmap rawR = fmap
|
|
|
|
|
|
|
|
|
|
isFunctorR : IsFunctor ℂ ℂ rawR
|
|
|
|
|
IsFunctor.isIdentity isFunctorR = begin
|
2018-04-11 08:58:50 +00:00
|
|
|
|
bind (pure <<< identity) ≡⟨ cong bind (ℂ.rightIdentity) ⟩
|
|
|
|
|
bind pure ≡⟨ isIdentity ⟩
|
|
|
|
|
identity ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
IsFunctor.isDistributive isFunctorR {f = f} {g} = begin
|
2018-04-11 08:58:50 +00:00
|
|
|
|
bind (pure <<< (g <<< f)) ≡⟨⟩
|
|
|
|
|
fmap (g <<< f) ≡⟨ fusion ⟩
|
|
|
|
|
fmap g <<< fmap f ≡⟨⟩
|
|
|
|
|
bind (pure <<< g) <<< bind (pure <<< f) ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
-- FIXME Naming!
|
|
|
|
|
R : EndoFunctor ℂ
|
|
|
|
|
Functor.raw R = rawR
|
|
|
|
|
Functor.isFunctor R = isFunctorR
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
R⁰ : EndoFunctor ℂ
|
2018-03-23 12:55:03 +00:00
|
|
|
|
R⁰ = Functors.identity
|
2018-03-12 13:20:49 +00:00
|
|
|
|
R² : EndoFunctor ℂ
|
|
|
|
|
R² = F[ R ∘ R ]
|
|
|
|
|
module R = Functor R
|
|
|
|
|
module R⁰ = Functor R⁰
|
|
|
|
|
module R² = Functor R²
|
|
|
|
|
pureT : Transformation R⁰ R
|
|
|
|
|
pureT A = pure
|
|
|
|
|
pureN : Natural R⁰ R pureT
|
|
|
|
|
pureN {A} {B} f = begin
|
2018-04-11 08:58:50 +00:00
|
|
|
|
pureT B <<< R⁰.fmap f ≡⟨⟩
|
|
|
|
|
pure <<< f ≡⟨ sym (isNatural _) ⟩
|
|
|
|
|
bind (pure <<< f) <<< pure ≡⟨⟩
|
|
|
|
|
fmap f <<< pure ≡⟨⟩
|
|
|
|
|
R.fmap f <<< pureT A ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
joinT : Transformation R² R
|
|
|
|
|
joinT C = join
|
|
|
|
|
joinN : Natural R² R joinT
|
|
|
|
|
joinN f = begin
|
2018-04-11 08:58:50 +00:00
|
|
|
|
join <<< R².fmap f ≡⟨⟩
|
|
|
|
|
bind identity <<< R².fmap f ≡⟨⟩
|
|
|
|
|
R².fmap f >>> bind identity ≡⟨⟩
|
|
|
|
|
fmap (fmap f) >>> bind identity ≡⟨⟩
|
|
|
|
|
fmap (bind (f >>> pure)) >>> bind identity ≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (bind (f >>> pure) >>> pure) >>> bind identity
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind ((bind (f >>> pure) >>> pure) >=> identity)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind ((bind (f >>> pure) >>> pure) >>> bind identity)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (bind (f >>> pure) >>> (pure >>> bind identity))
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ cong (λ φ → bind (bind (f >>> pure) >>> φ)) (isNatural _) ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (bind (f >>> pure) >>> identity)
|
2018-03-21 10:46:36 +00:00
|
|
|
|
≡⟨ cong bind ℂ.leftIdentity ⟩
|
2018-03-12 13:20:49 +00:00
|
|
|
|
bind (bind (f >>> pure))
|
2018-03-21 10:46:36 +00:00
|
|
|
|
≡⟨ cong bind (sym ℂ.rightIdentity) ⟩
|
2018-04-11 08:58:50 +00:00
|
|
|
|
bind (identity >>> bind (f >>> pure)) ≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (identity >=> (f >>> pure))
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ sym (isDistributive _ _) ⟩
|
2018-04-11 08:58:50 +00:00
|
|
|
|
bind identity >>> bind (f >>> pure) ≡⟨⟩
|
|
|
|
|
bind identity >>> fmap f ≡⟨⟩
|
|
|
|
|
bind identity >>> R.fmap f ≡⟨⟩
|
|
|
|
|
R.fmap f <<< bind identity ≡⟨⟩
|
|
|
|
|
R.fmap f <<< join ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
pureNT : NaturalTransformation R⁰ R
|
2018-04-05 08:41:56 +00:00
|
|
|
|
fst pureNT = pureT
|
|
|
|
|
snd pureNT = pureN
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
joinNT : NaturalTransformation R² R
|
2018-04-05 08:41:56 +00:00
|
|
|
|
fst joinNT = joinT
|
|
|
|
|
snd joinNT = joinN
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
|
|
|
|
isNaturalForeign : IsNaturalForeign
|
|
|
|
|
isNaturalForeign = begin
|
|
|
|
|
fmap join >>> join ≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (join >>> pure) >>> bind identity
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind ((join >>> pure) >>> bind identity)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (join >>> (pure >>> bind identity))
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ cong (λ φ → bind (join >>> φ)) (isNatural _) ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (join >>> identity)
|
2018-03-21 10:46:36 +00:00
|
|
|
|
≡⟨ cong bind ℂ.leftIdentity ⟩
|
2018-03-12 13:20:49 +00:00
|
|
|
|
bind join ≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (bind identity)
|
2018-03-21 10:46:36 +00:00
|
|
|
|
≡⟨ cong bind (sym ℂ.rightIdentity) ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (identity >>> bind identity) ≡⟨⟩
|
|
|
|
|
bind (identity >=> identity) ≡⟨ sym (isDistributive _ _) ⟩
|
|
|
|
|
bind identity >>> bind identity ≡⟨⟩
|
2018-03-12 13:20:49 +00:00
|
|
|
|
join >>> join ∎
|
|
|
|
|
|
|
|
|
|
isInverse : IsInverse
|
|
|
|
|
isInverse = inv-l , inv-r
|
|
|
|
|
where
|
|
|
|
|
inv-l = begin
|
|
|
|
|
pure >>> join ≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
pure >>> bind identity ≡⟨ isNatural _ ⟩
|
|
|
|
|
identity ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
inv-r = begin
|
|
|
|
|
fmap pure >>> join ≡⟨⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (pure >>> pure) >>> bind identity
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind ((pure >>> pure) >=> identity) ≡⟨⟩
|
|
|
|
|
bind ((pure >>> pure) >>> bind identity)
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (pure >>> (pure >>> bind identity))
|
2018-03-12 13:20:49 +00:00
|
|
|
|
≡⟨ cong (λ φ → bind (pure >>> φ)) (isNatural _) ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
bind (pure >>> identity)
|
2018-03-21 10:46:36 +00:00
|
|
|
|
≡⟨ cong bind ℂ.leftIdentity ⟩
|
2018-03-12 13:20:49 +00:00
|
|
|
|
bind pure ≡⟨ isIdentity ⟩
|
2018-04-03 09:36:09 +00:00
|
|
|
|
identity ∎
|
2018-03-12 13:20:49 +00:00
|
|
|
|
|
2018-05-01 09:33:12 +00:00
|
|
|
|
rightIdentity : RightIdentity
|
|
|
|
|
rightIdentity {m = m} = begin
|
|
|
|
|
m >=> pure ≡⟨⟩
|
|
|
|
|
m >>> bind pure ≡⟨ cong (m >>>_) isIdentity ⟩
|
|
|
|
|
m >>> identity ≡⟨ ℂ.leftIdentity ⟩
|
|
|
|
|
m ∎
|
|
|
|
|
|
2018-03-12 13:20:49 +00:00
|
|
|
|
record Monad : Set ℓ where
|
|
|
|
|
field
|
|
|
|
|
raw : RawMonad
|
|
|
|
|
isMonad : IsMonad raw
|
|
|
|
|
open IsMonad isMonad public
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
module _ (raw : RawMonad) where
|
|
|
|
|
open RawMonad raw
|
|
|
|
|
propIsIdentity : isProp IsIdentity
|
|
|
|
|
propIsIdentity x y i = ℂ.arrowsAreSets _ _ x y i
|
|
|
|
|
propIsNatural : isProp IsNatural
|
|
|
|
|
propIsNatural x y i = λ f
|
|
|
|
|
→ ℂ.arrowsAreSets _ _ (x f) (y f) i
|
|
|
|
|
propIsDistributive : isProp IsDistributive
|
|
|
|
|
propIsDistributive x y i = λ g f
|
|
|
|
|
→ ℂ.arrowsAreSets _ _ (x g f) (y g f) i
|
|
|
|
|
|
|
|
|
|
open IsMonad
|
|
|
|
|
propIsMonad : (raw : _) → isProp (IsMonad raw)
|
|
|
|
|
IsMonad.isIdentity (propIsMonad raw x y i)
|
|
|
|
|
= propIsIdentity raw (isIdentity x) (isIdentity y) i
|
|
|
|
|
IsMonad.isNatural (propIsMonad raw x y i)
|
|
|
|
|
= propIsNatural raw (isNatural x) (isNatural y) i
|
|
|
|
|
IsMonad.isDistributive (propIsMonad raw x y i)
|
|
|
|
|
= propIsDistributive raw (isDistributive x) (isDistributive y) i
|
|
|
|
|
|
|
|
|
|
module _ {m n : Monad} (eq : Monad.raw m ≡ Monad.raw n) where
|
|
|
|
|
private
|
|
|
|
|
eqIsMonad : (λ i → IsMonad (eq i)) [ Monad.isMonad m ≡ Monad.isMonad n ]
|
|
|
|
|
eqIsMonad = lemPropF propIsMonad eq
|
|
|
|
|
|
|
|
|
|
Monad≡ : m ≡ n
|
|
|
|
|
Monad.raw (Monad≡ i) = eq i
|
|
|
|
|
Monad.isMonad (Monad≡ i) = eqIsMonad i
|