2018-02-24 14:13:25 +00:00
|
|
|
|
{-# OPTIONS --cubical --allow-unsolved-metas #-}
|
2018-02-23 16:33:09 +00:00
|
|
|
|
module Cat.Category.Monad where
|
|
|
|
|
|
2018-02-24 11:52:16 +00:00
|
|
|
|
open import Agda.Primitive
|
|
|
|
|
|
|
|
|
|
open import Data.Product
|
|
|
|
|
|
2018-02-23 16:33:09 +00:00
|
|
|
|
open import Cubical
|
2018-03-05 16:10:41 +00:00
|
|
|
|
open import Cubical.NType.Properties using (lemPropF ; lemSig)
|
2018-03-06 09:16:42 +00:00
|
|
|
|
open import Cubical.GradLemma using (gradLemma)
|
2018-02-23 16:33:09 +00:00
|
|
|
|
|
2018-03-02 12:31:46 +00:00
|
|
|
|
open import Cat.Category
|
2018-02-24 11:52:16 +00:00
|
|
|
|
open import Cat.Category.Functor as F
|
|
|
|
|
open import Cat.Category.NaturalTransformation
|
2018-02-23 16:33:09 +00:00
|
|
|
|
open import Cat.Categories.Fun
|
2018-02-24 11:52:16 +00:00
|
|
|
|
|
2018-02-24 14:13:25 +00:00
|
|
|
|
-- "A monad in the monoidal form" [voe]
|
2018-02-24 11:52:16 +00:00
|
|
|
|
module Monoidal {ℓa ℓb : Level} (ℂ : Category ℓa ℓb) where
|
|
|
|
|
private
|
|
|
|
|
ℓ = ℓa ⊔ ℓb
|
|
|
|
|
|
2018-02-25 00:27:20 +00:00
|
|
|
|
open Category ℂ using (Object ; Arrow ; 𝟙 ; _∘_)
|
2018-02-24 11:52:16 +00:00
|
|
|
|
open NaturalTransformation ℂ ℂ
|
|
|
|
|
record RawMonad : Set ℓ where
|
|
|
|
|
field
|
2018-03-06 08:56:44 +00:00
|
|
|
|
R : EndoFunctor ℂ
|
2018-03-06 08:30:41 +00:00
|
|
|
|
pureNT : NaturalTransformation F.identity R
|
|
|
|
|
joinNT : NaturalTransformation F[ R ∘ R ] R
|
2018-02-24 11:52:16 +00:00
|
|
|
|
|
2018-03-06 08:41:29 +00:00
|
|
|
|
-- Note that `pureT` and `joinT` differs from their definition in the
|
|
|
|
|
-- kleisli formulation only by having an explicit parameter.
|
2018-03-06 08:30:41 +00:00
|
|
|
|
pureT : Transformation F.identity R
|
|
|
|
|
pureT = proj₁ pureNT
|
|
|
|
|
pureN : Natural F.identity R pureT
|
|
|
|
|
pureN = proj₂ pureNT
|
2018-02-26 19:31:47 +00:00
|
|
|
|
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT : Transformation F[ R ∘ R ] R
|
|
|
|
|
joinT = proj₁ joinNT
|
|
|
|
|
joinN : Natural F[ R ∘ R ] R joinT
|
|
|
|
|
joinN = proj₂ joinNT
|
2018-02-24 13:00:52 +00:00
|
|
|
|
|
2018-03-06 08:52:01 +00:00
|
|
|
|
Romap = Functor.func* R
|
|
|
|
|
Rfmap = Functor.func→ R
|
|
|
|
|
|
|
|
|
|
bind : {X Y : Object} → ℂ [ X , Romap Y ] → ℂ [ Romap X , Romap Y ]
|
|
|
|
|
bind {X} {Y} f = joinT Y ∘ Rfmap f
|
|
|
|
|
|
2018-02-25 18:03:30 +00:00
|
|
|
|
IsAssociative : Set _
|
|
|
|
|
IsAssociative = {X : Object}
|
2018-03-06 08:56:44 +00:00
|
|
|
|
→ joinT X ∘ Rfmap (joinT X) ≡ joinT X ∘ joinT (Romap X)
|
2018-02-25 18:03:30 +00:00
|
|
|
|
IsInverse : Set _
|
|
|
|
|
IsInverse = {X : Object}
|
2018-03-06 08:56:44 +00:00
|
|
|
|
→ joinT X ∘ pureT (Romap X) ≡ 𝟙
|
|
|
|
|
× joinT X ∘ Rfmap (pureT X) ≡ 𝟙
|
|
|
|
|
IsNatural = ∀ {X Y} f → joinT Y ∘ Rfmap f ∘ pureT X ≡ f
|
|
|
|
|
IsDistributive = ∀ {X Y Z} (g : Arrow Y (Romap Z)) (f : Arrow X (Romap Y))
|
|
|
|
|
→ joinT Z ∘ Rfmap g ∘ (joinT Y ∘ Rfmap f)
|
|
|
|
|
≡ joinT Z ∘ Rfmap (joinT Z ∘ Rfmap g ∘ f)
|
2018-02-24 11:52:16 +00:00
|
|
|
|
|
|
|
|
|
record IsMonad (raw : RawMonad) : Set ℓ where
|
|
|
|
|
open RawMonad raw public
|
|
|
|
|
field
|
|
|
|
|
isAssociative : IsAssociative
|
2018-02-26 19:36:39 +00:00
|
|
|
|
isInverse : IsInverse
|
2018-02-24 13:00:52 +00:00
|
|
|
|
|
2018-02-26 19:31:47 +00:00
|
|
|
|
private
|
|
|
|
|
module R = Functor R
|
|
|
|
|
module ℂ = Category ℂ
|
|
|
|
|
|
2018-02-28 17:55:32 +00:00
|
|
|
|
isNatural : IsNatural
|
|
|
|
|
isNatural {X} {Y} f = begin
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Y ∘ R.func→ f ∘ pureT X ≡⟨ sym ℂ.isAssociative ⟩
|
|
|
|
|
joinT Y ∘ (R.func→ f ∘ pureT X) ≡⟨ cong (λ φ → joinT Y ∘ φ) (sym (pureN f)) ⟩
|
|
|
|
|
joinT Y ∘ (pureT (R.func* Y) ∘ f) ≡⟨ ℂ.isAssociative ⟩
|
|
|
|
|
joinT Y ∘ pureT (R.func* Y) ∘ f ≡⟨ cong (λ φ → φ ∘ f) (proj₁ isInverse) ⟩
|
2018-02-26 19:31:47 +00:00
|
|
|
|
𝟙 ∘ f ≡⟨ proj₂ ℂ.isIdentity ⟩
|
|
|
|
|
f ∎
|
|
|
|
|
|
2018-02-28 17:55:32 +00:00
|
|
|
|
isDistributive : IsDistributive
|
2018-03-06 08:39:48 +00:00
|
|
|
|
isDistributive {X} {Y} {Z} g f = sym aux
|
2018-02-26 19:36:39 +00:00
|
|
|
|
where
|
2018-02-28 17:55:32 +00:00
|
|
|
|
module R² = Functor F[ R ∘ R ]
|
2018-03-06 08:39:48 +00:00
|
|
|
|
distrib3 : ∀ {A B C D} {a : Arrow C D} {b : Arrow B C} {c : Arrow A B}
|
2018-03-01 19:47:36 +00:00
|
|
|
|
→ R.func→ (a ∘ b ∘ c)
|
|
|
|
|
≡ R.func→ a ∘ R.func→ b ∘ R.func→ c
|
2018-03-06 08:39:48 +00:00
|
|
|
|
distrib3 {a = a} {b} {c} = begin
|
|
|
|
|
R.func→ (a ∘ b ∘ c) ≡⟨ R.isDistributive ⟩
|
|
|
|
|
R.func→ (a ∘ b) ∘ R.func→ c ≡⟨ cong (_∘ _) R.isDistributive ⟩
|
2018-03-01 19:47:36 +00:00
|
|
|
|
R.func→ a ∘ R.func→ b ∘ R.func→ c ∎
|
2018-03-06 08:39:48 +00:00
|
|
|
|
aux = begin
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ R.func→ (joinT Z ∘ R.func→ g ∘ f)
|
2018-03-06 08:39:48 +00:00
|
|
|
|
≡⟨ cong (λ φ → joinT Z ∘ φ) distrib3 ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ (R.func→ (joinT Z) ∘ R.func→ (R.func→ g) ∘ R.func→ f)
|
2018-03-01 19:47:36 +00:00
|
|
|
|
≡⟨⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ (R.func→ (joinT Z) ∘ R².func→ g ∘ R.func→ f)
|
2018-03-06 08:39:48 +00:00
|
|
|
|
≡⟨ cong (_∘_ (joinT Z)) (sym ℂ.isAssociative) ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ (R.func→ (joinT Z) ∘ (R².func→ g ∘ R.func→ f))
|
2018-03-01 19:47:36 +00:00
|
|
|
|
≡⟨ ℂ.isAssociative ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
(joinT Z ∘ R.func→ (joinT Z)) ∘ (R².func→ g ∘ R.func→ f)
|
2018-03-01 19:47:36 +00:00
|
|
|
|
≡⟨ cong (λ φ → φ ∘ (R².func→ g ∘ R.func→ f)) isAssociative ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
(joinT Z ∘ joinT (R.func* Z)) ∘ (R².func→ g ∘ R.func→ f)
|
2018-03-06 08:39:48 +00:00
|
|
|
|
≡⟨ ℂ.isAssociative ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ joinT (R.func* Z) ∘ R².func→ g ∘ R.func→ f
|
2018-03-06 08:39:48 +00:00
|
|
|
|
≡⟨⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
((joinT Z ∘ joinT (R.func* Z)) ∘ R².func→ g) ∘ R.func→ f
|
2018-03-01 19:47:36 +00:00
|
|
|
|
≡⟨ cong (_∘ R.func→ f) (sym ℂ.isAssociative) ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
(joinT Z ∘ (joinT (R.func* Z) ∘ R².func→ g)) ∘ R.func→ f
|
2018-03-06 08:39:48 +00:00
|
|
|
|
≡⟨ cong (λ φ → φ ∘ R.func→ f) (cong (_∘_ (joinT Z)) (joinN g)) ⟩
|
|
|
|
|
(joinT Z ∘ (R.func→ g ∘ joinT Y)) ∘ R.func→ f
|
|
|
|
|
≡⟨ cong (_∘ R.func→ f) ℂ.isAssociative ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ R.func→ g ∘ joinT Y ∘ R.func→ f
|
2018-03-01 19:47:36 +00:00
|
|
|
|
≡⟨ sym (Category.isAssociative ℂ) ⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT Z ∘ R.func→ g ∘ (joinT Y ∘ R.func→ f)
|
2018-03-01 19:47:36 +00:00
|
|
|
|
∎
|
2018-02-26 19:36:39 +00:00
|
|
|
|
|
2018-02-24 13:01:57 +00:00
|
|
|
|
record Monad : Set ℓ where
|
|
|
|
|
field
|
2018-03-06 09:05:35 +00:00
|
|
|
|
raw : RawMonad
|
2018-02-24 13:01:57 +00:00
|
|
|
|
isMonad : IsMonad raw
|
|
|
|
|
open IsMonad isMonad public
|
|
|
|
|
|
2018-03-01 19:12:49 +00:00
|
|
|
|
private
|
|
|
|
|
module _ {m : RawMonad} where
|
|
|
|
|
open RawMonad m
|
|
|
|
|
propIsAssociative : isProp IsAssociative
|
|
|
|
|
propIsAssociative x y i {X}
|
|
|
|
|
= Category.arrowsAreSets ℂ _ _ (x {X}) (y {X}) i
|
|
|
|
|
propIsInverse : isProp IsInverse
|
|
|
|
|
propIsInverse x y i {X} = e1 i , e2 i
|
|
|
|
|
where
|
|
|
|
|
xX = x {X}
|
|
|
|
|
yX = y {X}
|
|
|
|
|
e1 = Category.arrowsAreSets ℂ _ _ (proj₁ xX) (proj₁ yX)
|
|
|
|
|
e2 = Category.arrowsAreSets ℂ _ _ (proj₂ xX) (proj₂ yX)
|
2018-03-06 09:05:35 +00:00
|
|
|
|
|
2018-03-01 19:12:49 +00:00
|
|
|
|
open IsMonad
|
|
|
|
|
propIsMonad : (raw : _) → isProp (IsMonad raw)
|
|
|
|
|
IsMonad.isAssociative (propIsMonad raw a b i) j
|
|
|
|
|
= propIsAssociative {raw}
|
|
|
|
|
(isAssociative a) (isAssociative b) i j
|
|
|
|
|
IsMonad.isInverse (propIsMonad raw a b i)
|
|
|
|
|
= propIsInverse {raw}
|
|
|
|
|
(isInverse a) (isInverse b) i
|
|
|
|
|
|
|
|
|
|
module _ {m n : Monad} (eq : Monad.raw m ≡ Monad.raw n) where
|
2018-03-06 09:05:35 +00:00
|
|
|
|
private
|
|
|
|
|
eqIsMonad : (λ i → IsMonad (eq i)) [ Monad.isMonad m ≡ Monad.isMonad n ]
|
|
|
|
|
eqIsMonad = lemPropF propIsMonad eq
|
2018-03-01 19:12:49 +00:00
|
|
|
|
|
|
|
|
|
Monad≡ : m ≡ n
|
|
|
|
|
Monad.raw (Monad≡ i) = eq i
|
|
|
|
|
Monad.isMonad (Monad≡ i) = eqIsMonad i
|
2018-02-25 02:09:25 +00:00
|
|
|
|
|
2018-02-24 14:13:25 +00:00
|
|
|
|
-- "A monad in the Kleisli form" [voe]
|
2018-02-24 13:00:52 +00:00
|
|
|
|
module Kleisli {ℓa ℓb : Level} (ℂ : Category ℓa ℓb) where
|
|
|
|
|
private
|
|
|
|
|
ℓ = ℓa ⊔ ℓb
|
2018-03-01 13:58:01 +00:00
|
|
|
|
module ℂ = Category ℂ
|
|
|
|
|
open ℂ using (Arrow ; 𝟙 ; Object ; _∘_ ; _>>>_)
|
2018-02-26 19:08:48 +00:00
|
|
|
|
|
|
|
|
|
-- | Data for a monad.
|
|
|
|
|
--
|
|
|
|
|
-- Note that (>>=) is not expressible in a general category because objects
|
|
|
|
|
-- are not generally types.
|
2018-02-24 13:00:52 +00:00
|
|
|
|
record RawMonad : Set ℓ where
|
|
|
|
|
field
|
2018-03-06 09:05:35 +00:00
|
|
|
|
omap : Object → Object
|
|
|
|
|
pure : {X : Object} → ℂ [ X , omap X ]
|
|
|
|
|
bind : {X Y : Object} → ℂ [ X , omap Y ] → ℂ [ omap X , omap Y ]
|
2018-02-26 19:08:48 +00:00
|
|
|
|
|
|
|
|
|
-- | functor map
|
|
|
|
|
--
|
|
|
|
|
-- This should perhaps be defined in a "Klesli-version" of functors as well?
|
2018-03-06 09:05:35 +00:00
|
|
|
|
fmap : ∀ {A B} → ℂ [ A , B ] → ℂ [ omap A , omap B ]
|
2018-02-26 18:58:27 +00:00
|
|
|
|
fmap f = bind (pure ∘ f)
|
2018-02-26 19:08:48 +00:00
|
|
|
|
|
|
|
|
|
-- | Composition of monads aka. the kleisli-arrow.
|
2018-03-06 09:05:35 +00:00
|
|
|
|
_>=>_ : {A B C : Object} → ℂ [ A , omap B ] → ℂ [ B , omap C ] → ℂ [ A , omap C ]
|
2018-02-26 18:57:05 +00:00
|
|
|
|
f >=> g = f >>> (bind g)
|
2018-02-26 19:08:48 +00:00
|
|
|
|
|
|
|
|
|
-- | Flattening nested monads.
|
2018-03-06 09:05:35 +00:00
|
|
|
|
join : {A : Object} → ℂ [ omap (omap A) , omap A ]
|
2018-02-26 18:57:05 +00:00
|
|
|
|
join = bind 𝟙
|
2018-02-24 18:08:20 +00:00
|
|
|
|
|
2018-02-26 19:08:48 +00:00
|
|
|
|
------------------
|
|
|
|
|
-- * Monad laws --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
-- There may be better names than what I've chosen here.
|
|
|
|
|
|
2018-02-24 13:00:52 +00:00
|
|
|
|
IsIdentity = {X : Object}
|
2018-03-06 09:05:35 +00:00
|
|
|
|
→ bind pure ≡ 𝟙 {omap X}
|
|
|
|
|
IsNatural = {X Y : Object} (f : ℂ [ X , omap Y ])
|
2018-02-26 18:57:05 +00:00
|
|
|
|
→ pure >>> (bind f) ≡ f
|
2018-03-06 09:05:35 +00:00
|
|
|
|
IsDistributive = {X Y Z : Object} (g : ℂ [ Y , omap Z ]) (f : ℂ [ X , omap Y ])
|
2018-02-26 18:57:05 +00:00
|
|
|
|
→ (bind f) >>> (bind g) ≡ bind (f >=> g)
|
2018-02-26 19:08:48 +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?
|
2018-02-25 02:09:25 +00:00
|
|
|
|
Fusion = {X Y Z : Object} {g : ℂ [ Y , Z ]} {f : ℂ [ X , Y ]}
|
|
|
|
|
→ fmap (g ∘ f) ≡ fmap g ∘ fmap f
|
2018-02-24 13:00:52 +00:00
|
|
|
|
|
2018-03-01 13:58:01 +00:00
|
|
|
|
-- In the ("foreign") formulation of a monad `IsNatural`'s analogue here would be:
|
|
|
|
|
IsNaturalForeign : Set _
|
|
|
|
|
IsNaturalForeign = {X : Object} → join {X} ∘ fmap join ≡ join ∘ join
|
|
|
|
|
|
|
|
|
|
IsInverse : Set _
|
|
|
|
|
IsInverse = {X : Object} → join {X} ∘ pure ≡ 𝟙 × join {X} ∘ fmap pure ≡ 𝟙
|
|
|
|
|
|
2018-02-24 13:00:52 +00:00
|
|
|
|
record IsMonad (raw : RawMonad) : Set ℓ where
|
|
|
|
|
open RawMonad raw public
|
|
|
|
|
field
|
|
|
|
|
isIdentity : IsIdentity
|
|
|
|
|
isNatural : IsNatural
|
|
|
|
|
isDistributive : IsDistributive
|
2018-02-26 19:23:31 +00:00
|
|
|
|
|
|
|
|
|
-- | Map fusion is admissable.
|
2018-02-25 02:09:25 +00:00
|
|
|
|
fusion : Fusion
|
|
|
|
|
fusion {g = g} {f} = begin
|
2018-02-26 19:23:31 +00:00
|
|
|
|
fmap (g ∘ f) ≡⟨⟩
|
2018-03-06 09:05:35 +00:00
|
|
|
|
bind ((f >>> g) >>> pure) ≡⟨ cong bind ℂ.isAssociative ⟩
|
2018-02-26 18:57:05 +00:00
|
|
|
|
bind (f >>> (g >>> pure)) ≡⟨ cong (λ φ → bind (f >>> φ)) (sym (isNatural _)) ⟩
|
|
|
|
|
bind (f >>> (pure >>> (bind (g >>> pure)))) ≡⟨⟩
|
|
|
|
|
bind (f >>> (pure >>> fmap g)) ≡⟨⟩
|
2018-03-06 09:05:35 +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-02-25 02:09:25 +00:00
|
|
|
|
where
|
2018-03-06 09:05:35 +00:00
|
|
|
|
distrib : fmap g ∘ fmap f ≡ bind (fmap g ∘ (pure ∘ f))
|
|
|
|
|
distrib = isDistributive (pure ∘ g) (pure ∘ f)
|
2018-02-24 13:00:52 +00:00
|
|
|
|
|
2018-02-28 18:00:21 +00:00
|
|
|
|
-- | This formulation gives rise to the following endo-functor.
|
|
|
|
|
private
|
|
|
|
|
rawR : RawFunctor ℂ ℂ
|
2018-03-06 09:05:35 +00:00
|
|
|
|
RawFunctor.func* rawR = omap
|
2018-02-28 18:31:53 +00:00
|
|
|
|
RawFunctor.func→ rawR = fmap
|
2018-02-28 18:00:21 +00:00
|
|
|
|
|
|
|
|
|
isFunctorR : IsFunctor ℂ ℂ rawR
|
|
|
|
|
IsFunctor.isIdentity isFunctorR = begin
|
|
|
|
|
bind (pure ∘ 𝟙) ≡⟨ cong bind (proj₁ ℂ.isIdentity) ⟩
|
|
|
|
|
bind pure ≡⟨ isIdentity ⟩
|
|
|
|
|
𝟙 ∎
|
|
|
|
|
|
|
|
|
|
IsFunctor.isDistributive isFunctorR {f = f} {g} = begin
|
|
|
|
|
bind (pure ∘ (g ∘ f)) ≡⟨⟩
|
|
|
|
|
fmap (g ∘ f) ≡⟨ fusion ⟩
|
|
|
|
|
fmap g ∘ fmap f ≡⟨⟩
|
|
|
|
|
bind (pure ∘ g) ∘ bind (pure ∘ f) ∎
|
|
|
|
|
|
|
|
|
|
-- TODO: Naming!
|
2018-02-28 18:03:11 +00:00
|
|
|
|
R : EndoFunctor ℂ
|
2018-02-28 18:00:21 +00:00
|
|
|
|
Functor.raw R = rawR
|
|
|
|
|
Functor.isFunctor R = isFunctorR
|
|
|
|
|
|
2018-02-28 18:31:53 +00:00
|
|
|
|
private
|
|
|
|
|
open NaturalTransformation ℂ ℂ
|
|
|
|
|
|
|
|
|
|
R⁰ : EndoFunctor ℂ
|
|
|
|
|
R⁰ = F.identity
|
|
|
|
|
R² : EndoFunctor ℂ
|
|
|
|
|
R² = F[ R ∘ R ]
|
|
|
|
|
module R = Functor R
|
|
|
|
|
module R⁰ = Functor R⁰
|
|
|
|
|
module R² = Functor R²
|
2018-03-06 08:30:41 +00:00
|
|
|
|
pureT : Transformation R⁰ R
|
|
|
|
|
pureT A = pure
|
2018-03-06 08:45:04 +00:00
|
|
|
|
pureN : Natural R⁰ R pureT
|
|
|
|
|
pureN {A} {B} f = begin
|
2018-03-06 08:30:41 +00:00
|
|
|
|
pureT B ∘ R⁰.func→ f ≡⟨⟩
|
2018-02-28 18:31:53 +00:00
|
|
|
|
pure ∘ f ≡⟨ sym (isNatural _) ⟩
|
|
|
|
|
bind (pure ∘ f) ∘ pure ≡⟨⟩
|
|
|
|
|
fmap f ∘ pure ≡⟨⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
R.func→ f ∘ pureT A ∎
|
|
|
|
|
joinT : Transformation R² R
|
|
|
|
|
joinT C = join
|
2018-03-06 08:45:04 +00:00
|
|
|
|
joinN : Natural R² R joinT
|
|
|
|
|
joinN f = begin
|
2018-02-28 22:41:59 +00:00
|
|
|
|
join ∘ R².func→ f ≡⟨⟩
|
|
|
|
|
bind 𝟙 ∘ R².func→ f ≡⟨⟩
|
|
|
|
|
R².func→ f >>> bind 𝟙 ≡⟨⟩
|
|
|
|
|
fmap (fmap f) >>> bind 𝟙 ≡⟨⟩
|
|
|
|
|
fmap (bind (f >>> pure)) >>> bind 𝟙 ≡⟨⟩
|
|
|
|
|
bind (bind (f >>> pure) >>> pure) >>> bind 𝟙
|
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
|
|
|
|
bind ((bind (f >>> pure) >>> pure) >=> 𝟙)
|
|
|
|
|
≡⟨⟩
|
|
|
|
|
bind ((bind (f >>> pure) >>> pure) >>> bind 𝟙)
|
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
|
|
|
|
bind (bind (f >>> pure) >>> (pure >>> bind 𝟙))
|
|
|
|
|
≡⟨ cong (λ φ → bind (bind (f >>> pure) >>> φ)) (isNatural _) ⟩
|
|
|
|
|
bind (bind (f >>> pure) >>> 𝟙)
|
|
|
|
|
≡⟨ cong bind (proj₂ ℂ.isIdentity) ⟩
|
|
|
|
|
bind (bind (f >>> pure))
|
|
|
|
|
≡⟨ cong bind (sym (proj₁ ℂ.isIdentity)) ⟩
|
|
|
|
|
bind (𝟙 >>> bind (f >>> pure)) ≡⟨⟩
|
|
|
|
|
bind (𝟙 >=> (f >>> pure))
|
|
|
|
|
≡⟨ sym (isDistributive _ _) ⟩
|
|
|
|
|
bind 𝟙 >>> bind (f >>> pure) ≡⟨⟩
|
|
|
|
|
bind 𝟙 >>> fmap f ≡⟨⟩
|
|
|
|
|
bind 𝟙 >>> R.func→ f ≡⟨⟩
|
|
|
|
|
R.func→ f ∘ bind 𝟙 ≡⟨⟩
|
|
|
|
|
R.func→ f ∘ join ∎
|
2018-02-28 18:31:53 +00:00
|
|
|
|
|
2018-03-06 08:30:41 +00:00
|
|
|
|
pureNT : NaturalTransformation R⁰ R
|
|
|
|
|
proj₁ pureNT = pureT
|
2018-03-06 08:45:04 +00:00
|
|
|
|
proj₂ pureNT = pureN
|
2018-02-28 18:31:53 +00:00
|
|
|
|
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinNT : NaturalTransformation R² R
|
|
|
|
|
proj₁ joinNT = joinT
|
2018-03-06 08:45:04 +00:00
|
|
|
|
proj₂ joinNT = joinN
|
2018-02-28 18:31:53 +00:00
|
|
|
|
|
2018-03-01 13:58:01 +00:00
|
|
|
|
isNaturalForeign : IsNaturalForeign
|
|
|
|
|
isNaturalForeign = begin
|
2018-03-01 17:00:51 +00:00
|
|
|
|
fmap join >>> join ≡⟨⟩
|
|
|
|
|
bind (join >>> pure) >>> bind 𝟙
|
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
|
|
|
|
bind ((join >>> pure) >>> bind 𝟙)
|
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
|
|
|
|
bind (join >>> (pure >>> bind 𝟙))
|
|
|
|
|
≡⟨ cong (λ φ → bind (join >>> φ)) (isNatural _) ⟩
|
|
|
|
|
bind (join >>> 𝟙)
|
|
|
|
|
≡⟨ cong bind (proj₂ ℂ.isIdentity) ⟩
|
|
|
|
|
bind join ≡⟨⟩
|
|
|
|
|
bind (bind 𝟙)
|
|
|
|
|
≡⟨ cong bind (sym (proj₁ ℂ.isIdentity)) ⟩
|
|
|
|
|
bind (𝟙 >>> bind 𝟙) ≡⟨⟩
|
|
|
|
|
bind (𝟙 >=> 𝟙) ≡⟨ sym (isDistributive _ _) ⟩
|
|
|
|
|
bind 𝟙 >>> bind 𝟙 ≡⟨⟩
|
|
|
|
|
join >>> join ∎
|
2018-03-01 13:58:01 +00:00
|
|
|
|
|
|
|
|
|
isInverse : IsInverse
|
|
|
|
|
isInverse = inv-l , inv-r
|
|
|
|
|
where
|
|
|
|
|
inv-l = begin
|
2018-03-01 16:50:06 +00:00
|
|
|
|
pure >>> join ≡⟨⟩
|
|
|
|
|
pure >>> bind 𝟙 ≡⟨ isNatural _ ⟩
|
2018-03-01 13:58:01 +00:00
|
|
|
|
𝟙 ∎
|
|
|
|
|
inv-r = begin
|
2018-03-01 16:50:06 +00:00
|
|
|
|
fmap pure >>> join ≡⟨⟩
|
|
|
|
|
bind (pure >>> pure) >>> bind 𝟙
|
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
|
|
|
|
bind ((pure >>> pure) >=> 𝟙) ≡⟨⟩
|
|
|
|
|
bind ((pure >>> pure) >>> bind 𝟙)
|
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
|
|
|
|
bind (pure >>> (pure >>> bind 𝟙))
|
|
|
|
|
≡⟨ cong (λ φ → bind (pure >>> φ)) (isNatural _) ⟩
|
|
|
|
|
bind (pure >>> 𝟙)
|
|
|
|
|
≡⟨ cong bind (proj₂ ℂ.isIdentity) ⟩
|
|
|
|
|
bind pure ≡⟨ isIdentity ⟩
|
2018-03-01 13:58:01 +00:00
|
|
|
|
𝟙 ∎
|
|
|
|
|
|
2018-02-24 13:00:52 +00:00
|
|
|
|
record Monad : Set ℓ where
|
|
|
|
|
field
|
|
|
|
|
raw : RawMonad
|
|
|
|
|
isMonad : IsMonad raw
|
|
|
|
|
open IsMonad isMonad public
|
2018-02-24 14:13:25 +00:00
|
|
|
|
|
2018-03-06 09:16:42 +00:00
|
|
|
|
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
|
|
|
|
|
|
2018-03-01 19:23:34 +00:00
|
|
|
|
module _ {m n : Monad} (eq : Monad.raw m ≡ Monad.raw n) where
|
2018-03-06 09:06:45 +00:00
|
|
|
|
private
|
|
|
|
|
eqIsMonad : (λ i → IsMonad (eq i)) [ Monad.isMonad m ≡ Monad.isMonad n ]
|
|
|
|
|
eqIsMonad = lemPropF propIsMonad eq
|
2018-03-01 19:23:34 +00:00
|
|
|
|
|
|
|
|
|
Monad≡ : m ≡ n
|
|
|
|
|
Monad.raw (Monad≡ i) = eq i
|
|
|
|
|
Monad.isMonad (Monad≡ i) = eqIsMonad i
|
2018-02-25 02:09:25 +00:00
|
|
|
|
|
2018-02-26 19:23:31 +00:00
|
|
|
|
-- | The monoidal- and kleisli presentation of monads are equivalent.
|
|
|
|
|
--
|
|
|
|
|
-- This is problem 2.3 in [voe].
|
2018-02-24 14:13:25 +00:00
|
|
|
|
module _ {ℓa ℓb : Level} {ℂ : Category ℓa ℓb} where
|
|
|
|
|
private
|
2018-03-05 09:28:16 +00:00
|
|
|
|
module ℂ = Category ℂ
|
|
|
|
|
open ℂ using (Object ; Arrow ; 𝟙 ; _∘_ ; _>>>_)
|
2018-02-24 14:13:25 +00:00
|
|
|
|
open Functor using (func* ; func→)
|
|
|
|
|
module M = Monoidal ℂ
|
2018-03-06 08:55:18 +00:00
|
|
|
|
module K = Kleisli ℂ
|
2018-02-24 14:13:25 +00:00
|
|
|
|
|
|
|
|
|
module _ (m : M.RawMonad) where
|
2018-03-06 08:52:01 +00:00
|
|
|
|
open M.RawMonad m
|
2018-02-24 14:13:25 +00:00
|
|
|
|
|
|
|
|
|
forthRaw : K.RawMonad
|
2018-03-06 09:16:42 +00:00
|
|
|
|
K.RawMonad.omap forthRaw = Romap
|
2018-03-06 08:52:01 +00:00
|
|
|
|
K.RawMonad.pure forthRaw = pureT _
|
|
|
|
|
K.RawMonad.bind forthRaw = bind
|
2018-02-24 14:13:25 +00:00
|
|
|
|
|
|
|
|
|
module _ {raw : M.RawMonad} (m : M.IsMonad raw) where
|
2018-03-01 13:19:46 +00:00
|
|
|
|
private
|
|
|
|
|
module MI = M.IsMonad m
|
2018-02-24 14:13:25 +00:00
|
|
|
|
forthIsMonad : K.IsMonad (forthRaw raw)
|
2018-03-06 08:55:18 +00:00
|
|
|
|
K.IsMonad.isIdentity forthIsMonad = proj₂ MI.isInverse
|
|
|
|
|
K.IsMonad.isNatural forthIsMonad = MI.isNatural
|
|
|
|
|
K.IsMonad.isDistributive forthIsMonad = MI.isDistributive
|
2018-02-24 14:13:25 +00:00
|
|
|
|
|
|
|
|
|
forth : M.Monad → K.Monad
|
2018-03-06 09:16:42 +00:00
|
|
|
|
Kleisli.Monad.raw (forth m) = forthRaw (M.Monad.raw m)
|
2018-02-24 14:13:25 +00:00
|
|
|
|
Kleisli.Monad.isMonad (forth m) = forthIsMonad (M.Monad.isMonad m)
|
|
|
|
|
|
2018-02-25 02:09:25 +00:00
|
|
|
|
module _ (m : K.Monad) where
|
2018-03-06 09:16:42 +00:00
|
|
|
|
open K.Monad m
|
2018-02-25 02:09:25 +00:00
|
|
|
|
|
|
|
|
|
backRaw : M.RawMonad
|
2018-03-06 09:16:42 +00:00
|
|
|
|
M.RawMonad.R backRaw = R
|
|
|
|
|
M.RawMonad.pureNT backRaw = pureNT
|
|
|
|
|
M.RawMonad.joinNT backRaw = joinNT
|
2018-02-25 02:09:25 +00:00
|
|
|
|
|
2018-03-01 13:58:01 +00:00
|
|
|
|
private
|
2018-03-06 09:16:42 +00:00
|
|
|
|
open M.RawMonad backRaw
|
|
|
|
|
module R = Functor (M.RawMonad.R backRaw)
|
2018-03-01 13:58:01 +00:00
|
|
|
|
|
2018-03-01 13:19:46 +00:00
|
|
|
|
backIsMonad : M.IsMonad backRaw
|
2018-03-06 09:16:42 +00:00
|
|
|
|
M.IsMonad.isAssociative backIsMonad {X} = begin
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT X ∘ R.func→ (joinT X) ≡⟨⟩
|
2018-03-06 09:16:42 +00:00
|
|
|
|
join ∘ fmap (joinT X) ≡⟨⟩
|
|
|
|
|
join ∘ fmap join ≡⟨ isNaturalForeign ⟩
|
|
|
|
|
join ∘ join ≡⟨⟩
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT X ∘ joinT (R.func* X) ∎
|
2018-03-06 09:16:42 +00:00
|
|
|
|
M.IsMonad.isInverse backIsMonad {X} = inv-l , inv-r
|
2018-03-01 13:58:01 +00:00
|
|
|
|
where
|
|
|
|
|
inv-l = begin
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT X ∘ pureT (R.func* X) ≡⟨⟩
|
2018-03-06 09:16:42 +00:00
|
|
|
|
join ∘ pure ≡⟨ proj₁ isInverse ⟩
|
|
|
|
|
𝟙 ∎
|
2018-03-01 13:58:01 +00:00
|
|
|
|
inv-r = begin
|
2018-03-06 08:30:41 +00:00
|
|
|
|
joinT X ∘ R.func→ (pureT X) ≡⟨⟩
|
2018-03-06 09:16:42 +00:00
|
|
|
|
join ∘ fmap pure ≡⟨ proj₂ isInverse ⟩
|
|
|
|
|
𝟙 ∎
|
2018-02-25 02:09:25 +00:00
|
|
|
|
|
2018-02-24 18:07:58 +00:00
|
|
|
|
back : K.Monad → M.Monad
|
2018-02-25 02:09:25 +00:00
|
|
|
|
Monoidal.Monad.raw (back m) = backRaw m
|
|
|
|
|
Monoidal.Monad.isMonad (back m) = backIsMonad m
|
|
|
|
|
|
2018-02-25 02:12:23 +00:00
|
|
|
|
module _ (m : K.Monad) where
|
2018-03-06 14:52:22 +00:00
|
|
|
|
private
|
|
|
|
|
open K.Monad m
|
|
|
|
|
bindEq : ∀ {X Y}
|
|
|
|
|
→ K.RawMonad.bind (forthRaw (backRaw m)) {X} {Y}
|
|
|
|
|
≡ K.RawMonad.bind (K.Monad.raw m)
|
|
|
|
|
bindEq {X} {Y} = begin
|
|
|
|
|
K.RawMonad.bind (forthRaw (backRaw m)) ≡⟨⟩
|
|
|
|
|
(λ f → join ∘ fmap f) ≡⟨⟩
|
|
|
|
|
(λ f → bind (f >>> pure) >>> bind 𝟙) ≡⟨ funExt lem ⟩
|
|
|
|
|
(λ f → bind f) ≡⟨⟩
|
|
|
|
|
bind ∎
|
|
|
|
|
where
|
|
|
|
|
lem : (f : Arrow X (omap Y))
|
|
|
|
|
→ bind (f >>> pure) >>> bind 𝟙
|
|
|
|
|
≡ bind f
|
|
|
|
|
lem f = begin
|
|
|
|
|
bind (f >>> pure) >>> bind 𝟙
|
|
|
|
|
≡⟨ isDistributive _ _ ⟩
|
|
|
|
|
bind ((f >>> pure) >>> bind 𝟙)
|
|
|
|
|
≡⟨ cong bind ℂ.isAssociative ⟩
|
|
|
|
|
bind (f >>> (pure >>> bind 𝟙))
|
|
|
|
|
≡⟨ cong (λ φ → bind (f >>> φ)) (isNatural _) ⟩
|
|
|
|
|
bind (f >>> 𝟙)
|
|
|
|
|
≡⟨ cong bind (proj₂ ℂ.isIdentity) ⟩
|
|
|
|
|
bind f ∎
|
2018-03-05 09:28:16 +00:00
|
|
|
|
|
2018-02-25 02:12:23 +00:00
|
|
|
|
forthRawEq : forthRaw (backRaw m) ≡ K.Monad.raw m
|
2018-03-06 09:05:35 +00:00
|
|
|
|
K.RawMonad.omap (forthRawEq _) = omap
|
2018-02-26 18:58:27 +00:00
|
|
|
|
K.RawMonad.pure (forthRawEq _) = pure
|
2018-03-05 09:28:16 +00:00
|
|
|
|
K.RawMonad.bind (forthRawEq i) = bindEq i
|
2018-02-24 18:07:58 +00:00
|
|
|
|
|
|
|
|
|
fortheq : (m : K.Monad) → forth (back m) ≡ m
|
2018-02-25 02:09:25 +00:00
|
|
|
|
fortheq m = K.Monad≡ (forthRawEq m)
|
|
|
|
|
|
2018-02-25 02:12:23 +00:00
|
|
|
|
module _ (m : M.Monad) where
|
2018-03-06 14:52:22 +00:00
|
|
|
|
private
|
|
|
|
|
open M.Monad m
|
|
|
|
|
module KM = K.Monad (forth m)
|
|
|
|
|
module R = Functor R
|
|
|
|
|
omapEq : KM.omap ≡ Romap
|
|
|
|
|
omapEq = refl
|
|
|
|
|
|
|
|
|
|
bindEq : ∀ {X Y} {f : Arrow X (Romap Y)} → KM.bind f ≡ bind f
|
|
|
|
|
bindEq {X} {Y} {f} = begin
|
|
|
|
|
KM.bind f ≡⟨⟩
|
|
|
|
|
joinT Y ∘ Rfmap f ≡⟨⟩
|
|
|
|
|
bind f ∎
|
|
|
|
|
|
|
|
|
|
joinEq : ∀ {X} → KM.join ≡ joinT X
|
|
|
|
|
joinEq {X} = begin
|
|
|
|
|
KM.join ≡⟨⟩
|
|
|
|
|
KM.bind 𝟙 ≡⟨⟩
|
|
|
|
|
bind 𝟙 ≡⟨⟩
|
|
|
|
|
joinT X ∘ Rfmap 𝟙 ≡⟨ cong (λ φ → _ ∘ φ) R.isIdentity ⟩
|
|
|
|
|
joinT X ∘ 𝟙 ≡⟨ proj₁ ℂ.isIdentity ⟩
|
|
|
|
|
joinT X ∎
|
|
|
|
|
|
|
|
|
|
fmapEq : ∀ {A B} → KM.fmap {A} {B} ≡ Rfmap
|
|
|
|
|
fmapEq {A} {B} = funExt (λ f → begin
|
|
|
|
|
KM.fmap f ≡⟨⟩
|
|
|
|
|
KM.bind (f >>> KM.pure) ≡⟨⟩
|
|
|
|
|
bind (f >>> pureT _) ≡⟨⟩
|
|
|
|
|
Rfmap (f >>> pureT B) >>> joinT B ≡⟨⟩
|
|
|
|
|
Rfmap (f >>> pureT B) >>> joinT B ≡⟨ cong (λ φ → φ >>> joinT B) R.isDistributive ⟩
|
|
|
|
|
Rfmap f >>> Rfmap (pureT B) >>> joinT B ≡⟨ ℂ.isAssociative ⟩
|
|
|
|
|
joinT B ∘ Rfmap (pureT B) ∘ Rfmap f ≡⟨ cong (λ φ → φ ∘ Rfmap f) (proj₂ isInverse) ⟩
|
|
|
|
|
𝟙 ∘ Rfmap f ≡⟨ proj₂ ℂ.isIdentity ⟩
|
|
|
|
|
Rfmap f ∎
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
rawEq : Functor.raw KM.R ≡ Functor.raw R
|
|
|
|
|
RawFunctor.func* (rawEq i) = omapEq i
|
|
|
|
|
RawFunctor.func→ (rawEq i) = fmapEq i
|
2018-03-05 16:31:13 +00:00
|
|
|
|
|
2018-03-05 09:28:16 +00:00
|
|
|
|
Req : M.RawMonad.R (backRaw (forth m)) ≡ R
|
2018-03-05 16:10:41 +00:00
|
|
|
|
Req = Functor≡ rawEq
|
2018-03-05 09:28:16 +00:00
|
|
|
|
|
|
|
|
|
open NaturalTransformation ℂ ℂ
|
2018-03-05 16:10:41 +00:00
|
|
|
|
postulate
|
2018-03-06 08:30:41 +00:00
|
|
|
|
pureNTEq : (λ i → NaturalTransformation F.identity (Req i))
|
|
|
|
|
[ M.RawMonad.pureNT (backRaw (forth m)) ≡ pureNT ]
|
2018-03-06 14:55:03 +00:00
|
|
|
|
joinNTEq : (λ i → NaturalTransformation F[ Req i ∘ Req i ] (Req i))
|
|
|
|
|
[ M.RawMonad.joinNT (backRaw (forth m)) ≡ joinNT ]
|
2018-02-25 02:12:23 +00:00
|
|
|
|
backRawEq : backRaw (forth m) ≡ M.Monad.raw m
|
|
|
|
|
-- stuck
|
2018-03-06 09:16:42 +00:00
|
|
|
|
M.RawMonad.R (backRawEq i) = Req i
|
2018-03-06 14:55:03 +00:00
|
|
|
|
M.RawMonad.pureNT (backRawEq i) = pureNTEq i -- pureNTEq i
|
|
|
|
|
M.RawMonad.joinNT (backRawEq i) = joinNTEq i
|
2018-02-24 18:07:58 +00:00
|
|
|
|
|
|
|
|
|
backeq : (m : M.Monad) → back (forth m) ≡ m
|
2018-02-25 02:09:25 +00:00
|
|
|
|
backeq m = M.Monad≡ (backRawEq m)
|
2018-02-24 18:07:58 +00:00
|
|
|
|
|
2018-02-24 14:13:25 +00:00
|
|
|
|
eqv : isEquiv M.Monad K.Monad forth
|
2018-02-24 18:07:58 +00:00
|
|
|
|
eqv = gradLemma forth back fortheq backeq
|
2018-02-24 14:13:25 +00:00
|
|
|
|
|
|
|
|
|
Monoidal≃Kleisli : M.Monad ≃ K.Monad
|
|
|
|
|
Monoidal≃Kleisli = forth , eqv
|