Debian Codegraph @

src/Data/Container/FreeMonad.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The free monad construction on containers
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Container.FreeMonad where
10
11 open import Level using (Level; _⊔_)
12 open import Data.Sum.Base using (inj₁; inj₂ ; [_,_]′)
13 open import Data.Product.Base using (_,_; -,_)
14 open import Data.Container using (Container; ⟦_⟧; μ)
15 open import Data.Container.Relation.Unary.All using (□; all)
16 open import Data.Container.Combinator using (const; _⊎_)
17 open import Data.W as W using (sup)
18 open import Effect.Functor using (RawFunctor)
19 open import Effect.Applicative using (RawApplicative)
20 open import Effect.Monad using (RawMonad)
21 open import Function.Base as Function using (_$_; _∘_)
22
23 private
24 variable
25 x y s p ℓ : Level
26 C : Container s p
27 X : Set x
28 Y : Set y
29
30 infixl 1 _⋆C_
31 infix 1 _⋆_
32
33 ------------------------------------------------------------------------
34 -- The free monad construction over a container and a set is, in
35 -- universal algebra terminology, also known as the term algebra over a
36 -- signature (a container) and a set (of variable symbols). The return
37 -- of the free monad corresponds to variables and the bind operator
38 -- corresponds to (parallel) substitution.
39
40 -- A useful intuition is to think of containers describing single
41 -- operations and the free monad construction over a container and a set
42 -- describing a tree of operations as nodes and elements of the set as
43 -- leaves. If one starts at the root, then any path will pass finitely
44 -- many nodes (operations described by the container) and eventually end
45 -- up in a leaf (element of the set) -- hence the Kleene star notation
46 -- (the type can be read as a regular expression).
47
48 ------------------------------------------------------------------------
49 -- Type definition
50
51 -- The free monad can be defined as the least fixpoint `μ (C ⋆C X)`
52
53 _⋆C_ : ∀ {x s p} → Container s p → Set x → Container (s ⊔ x) p
54 C ⋆C X = const X ⊎ C
55
56 -- However Agda's positivity checker is currently too weak to observe
57 -- that `X` is used in a strictly positive manner in `μ (C ⋆C X)` as
58 -- demonstrated in #693.
59 -- So we provide instead an inductive definition that we prove to be
60 -- equivalent to the μ-based one.
61
62 data _⋆_ (C : Container s p) (X : Set x) : Set (x ⊔ s ⊔ p) where
63 pure : X → C ⋆ X
64 impure : ⟦ C ⟧ (C ⋆ X) → C ⋆ X
65
66 ------------------------------------------------------------------------
67 -- Equivalent types
68
69 -- We can prove that `C ⋆ X` is equivalent to one layer of `C ⋆C X` with
70 -- subterms of tyep `C ⋆ X`.
71
72 inj : {X : Set x} → ⟦ C ⋆C X ⟧ (C ⋆ X) → C ⋆ X
73 inj (inj₁ x , _) = pure x
74 inj (inj₂ c , r) = impure (c , r)
75
76 out : {X : Set x} → C ⋆ X → ⟦ C ⋆C X ⟧ (C ⋆ X)
77 out (pure x) = inj₁ x , λ ()
78 out (impure (c , r)) = inj₂ c , r
79
80 -- We can fully convert back and forth between `C ⋆ X` and `μ (C ⋆C X)`.
81
82 toμ : C ⋆ X → μ (C ⋆C X)
83 toμ (pure x) = sup (inj₁ x , λ ())
84 toμ (impure (c , r)) = sup (inj₂ c , toμ ∘ r)
85
86 fromμ : μ (C ⋆C X) → C ⋆ X
87 fromμ = W.foldr inj
88
89 -- We can recover an induction principle similar to the one given in `Data.W`.
90 -- We curry these ones by distinguishing the pure vs. impure case
91
92 module _ (P : C ⋆ X → Set ℓ)
93 (algP : ∀ x → P (pure x))
94 (algI : ∀ {t} → □ C P t → P (impure t)) where
95
96 induction : (t : C ⋆ X) → P t
97 induction (pure x) = algP x
98 induction (impure (c , r)) = algI $ all (induction ∘ r)
99
100 module _ {P : Set ℓ}
101 (algP : X → P)
102 (algI : ⟦ C ⟧ P → P) where
103
104 foldr : C ⋆ X → P
105 foldr = induction (Function.const P) algP (algI ∘ -,_ ∘ □.proof)
106
107 infixr -1 _<$>_ _<*>_
108 infixl 1 _>>=_
109
110 _<$>_ : (X → Y) → C ⋆ X → C ⋆ Y
111 f <$> t = foldr (pure ∘ f) impure t
112
113 _<*>_ : C ⋆ (X → Y) → C ⋆ X → C ⋆ Y
114 pure f <*> t = f <$> t
115 impure (c , r) <*> t = impure (c , λ v → r v <*> t)
116
117 _>>=_ : C ⋆ X → (X → C ⋆ Y) → C ⋆ Y
118 pure x >>= f = f x
119 impure (c , r) >>= f = impure (c , λ v → r v >>= f)
120
121 ------------------------------------------------------------------------
122 -- Structure
123
124 functor : RawFunctor (_⋆_ {x = x} C)
125 functor = record { _<$>_ = _<$>_ }
126
127 applicative : {C : Container s p} → RawApplicative (_⋆_ {x = x ⊔ s ⊔ p} C)
128 applicative = record
129 { rawFunctor = functor
130 ; pure = pure
131 ; _<*>_ = _<*>_ }
132
133 monad : {C : Container s p} → RawMonad (_⋆_ {x = x ⊔ s ⊔ p} C)
134 monad {x = x} = record
135 { rawApplicative = applicative {x = x}
136 ; _>>=_ = _>>=_
137 }
138
139 ------------------------------------------------------------------------
140 -- DEPRECATIONS
141
142 rawFunctor = functor
143 {-# WARNING_ON_USAGE rawFunctor
144 "Warning: all rawFunctor deprecated in v2.0.
145 Please use functor instead."
146 #-}
147
148 rawApplicative = applicative
149 {-# WARNING_ON_USAGE rawApplicative
150 "Warning: rawApplicative was deprecated in v2.0.
151 Please use applicative instead."
152 #-}
153
154 rawMonad = monad
155 {-# WARNING_ON_USAGE rawMonad
156 "Warning: rawMonad was deprecated in v2.0.
157 Please use monad instead."
158 #-}