EilenbergMooreAlgebra.lagda 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. %include agda.fmt
  2. \subsection{Eilenberg-Moore algebras}
  3. \label{em-algs}
  4. \AgdaHide{
  5. \begin{code}
  6. {-# OPTIONS --sized-types #-}
  7. module Issue854.EilenbergMooreAlgebra where
  8. open import Function
  9. open import Data.Sum
  10. open import Data.Product as Prod
  11. open import Data.Container renaming (⟦_⟧ to ⟦_⟧^C)
  12. open import Data.Container.Combinator using ()
  13. renaming (_×_ to _×^C_)
  14. open import Data.Container.FreeMonad
  15. renaming (_⋆_ to _⋆^C_; _⋆C_ to _⋆^CC_)
  16. open import Data.W
  17. open import Category.Monad
  18. \end{code}
  19. }
  20. \begin{code}
  21. iter : ∀ {a b c} {A : Set a} {B : A → Set b} {C : Set c} →
  22. (Σ[ x ∈ A ] (B x → C) → C) → W (A ▷ B) → C
  23. iter g (sup (x , f)) = g (x , λ b → iter g (f b))
  24. module T-algebra
  25. (T : Set → Set)
  26. (M : RawMonad T)
  27. where
  28. open RawMonad M
  29. -- Eilenberg-Moore algebra for a monad T on Set
  30. record T-Alg : Set₁ where
  31. constructor alg
  32. field
  33. Carrier : Set
  34. structure : T Carrier → Carrier
  35. open T-Alg public
  36. -- Generalised bind operator.
  37. bind : ∀ {X}(A : T-Alg) → T X → (X → Carrier A) → Carrier A
  38. bind A tx f = structure A (f <$> tx)
  39. -- Free algebra
  40. ⋆-alg : Set → T-Alg
  41. ⋆-alg X = alg (T X) join
  42. -- Exponential algebras
  43. _⇒-alg_ : Set → T-Alg → T-Alg
  44. X ⇒-alg alg C φ = alg (X → C) (λ a x → φ (a ⊛ return x))
  45. -- Product algebras
  46. _×-alg_ : T-Alg → T-Alg → T-Alg
  47. alg C φ ×-alg alg C′ φ′ = alg (C × C′)
  48. < φ ∘ _<$>_ proj₁ , φ′ ∘ _<$>_ proj₂ >
  49. -- Eilenberg-Moore algebras for free monads.
  50. module _ {Σ : Container _ _} where
  51. open T-algebra (_⋆^C_ Σ) rawMonad public
  52. _-Alg : Container _ _ → Set₁
  53. Σ -Alg = T-Alg {Σ}
  54. -- A more general product algebra.
  55. _⊙-alg_ : ∀ {Σ Σ′} → Σ -Alg → Σ′ -Alg → (Σ ×^C Σ′) -Alg
  56. alg C φ ⊙-alg alg C′ φ′ = alg (C × C′) (Prod.map φ φ′ ∘ split-⋆×)
  57. where
  58. split-⋆× : ∀ {Σ Σ′}{X Y : Set} → (Σ ×^C Σ′) ⋆^C (X × Y) →
  59. (Σ ⋆^C X) × (Σ′ ⋆^C Y)
  60. split-⋆× {Σ}{Σ′}{X}{Y} = iter α
  61. where
  62. α : let C = (Σ ⋆^C X) × (Σ′ ⋆^C Y) in
  63. ⟦ (Σ ×^C Σ′) ⋆^CC X × Y ⟧^C C → C
  64. α (inj₁ (x , y) , _) = RawMonad.return rawMonad x ,
  65. RawMonad.return rawMonad y
  66. α (inj₂ (s , s′) , k) = inn (s , proj₁ ∘ k ∘ inj₁) ,
  67. inn (s′ , proj₂ ∘ k ∘ inj₂)
  68. \end{code}
  69. \begin{code}
  70. -- Using container exponents can we construct:
  71. -- _⇒′-alg_ : Σ -Alg → Σ′ -Alg → Σ ⟶ Σ′ -Alg?
  72. -- Which could be used to denote the function space between two
  73. -- computation types?!
  74. \end{code}