Debian Codegraph @

doc/README/Data/Container/Indexed/MultiSortedAlgebraExample.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Example of multi-sorted algebras as indexed containers
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --safe --cubical-compatible #-}
8
9 module README.Data.Container.Indexed.MultiSortedAlgebraExample where
10
11 ------------------------------------------------------------------------
12 -- Preliminaries
13 ------------------------------------------------------------------------
14 -- We import library content for indexed containers, standard types,
15 -- and setoids.
16
17 open import Level
18
19 open import Data.Container.Indexed.Core using (Container; ⟦_⟧; _◃_/_)
20 open import Data.Container.Indexed.FreeMonad using (_⋆C_)
21 open import Data.W.Indexed using (W; sup)
22
23 open import Data.Product using (Σ; _×_; _,_; Σ-syntax)
24 open import Data.Sum using (_⊎_; inj₁; inj₂; [_,_])
25 open import Data.Empty.Polymorphic using (⊥; ⊥-elim)
26
27 open import Function using (_∘_)
28 open import Function.Bundles using (Func)
29
30 open import Relation.Binary using (Setoid; IsEquivalence)
31 open import Relation.Binary.PropositionalEquality using (_≡_; refl)
32
33 import Data.Container.Indexed.Relation.Binary.Equality.Setoid as ICSetoid
34 import Relation.Binary.Reasoning.Setoid as SetoidReasoning
35
36 open Setoid using (Carrier; _≈_; isEquivalence)
37 open Func renaming (to to apply)
38
39 -- Letter ℓ denotes universe levels.
40
41 variable
42 ℓ ℓ' ℓˢ ℓᵒ ℓᵃ ℓᵐ ℓᵉ ℓⁱ : Level
43 I : Set ℓⁱ
44 S : Set ℓˢ
45
46 ------------------------------------------------------------------------
47 -- The interpretation of a container (Op ◃ Ar / sort) is
48 --
49 -- ⟦ Op ◃ Ar / sort ⟧ X s = Σ[ o ∈ Op s ] ((i : Ar o) → X (sort o i))
50 --
51 -- which contains pairs consisting of an operator $o$ and its collection
52 -- of arguments. The least fixed point of (X ↦ ⟦ C ⟧ X) is the indexed
53 -- W-type given by C, and it contains closed first-order terms of the
54 -- multi-sorted algebra C.
55
56 -- We need to interpret indexed containers on Setoids.
57 -- This definition is missing from the standard library v1.7.
58
59 ⟦_⟧s : (C : Container I S ℓᵒ ℓᵃ) (ξ : I → Setoid ℓᵐ ℓᵉ) → S → Setoid _ _
60 ⟦ C ⟧s ξ = ICSetoid.setoid ξ C
61
62 ------------------------------------------------------------------------
63 -- Multi-sorted algebras
64 --------------------------------------------------------------------------
65 -- A multi-sorted algebra is an indexed container.
66 --
67 -- * Sorts are indexes.
68 --
69 -- * Operators are commands/shapes.
70 --
71 -- * Arities/argument are responses/positions.
72 --
73 -- Closed terms (initial model) are given by the W type for a container,
74 -- renamed to μ here (for least fixed-point).
75
76 -- We assume a fixed signature (Sort, Ops).
77
78 module _ (Sort : Set ℓˢ) (Ops : Container Sort Sort ℓᵒ ℓᵃ) where
79 open Container Ops renaming
80 ( Command to Op
81 ; Response to Arity
82 ; next to sort
83 )
84
85 -- We let letter $s$ range over sorts and $\mathit{op}$ over operators.
86
87 variable
88 s s' : Sort
89 op op' : Op s
90
91 ------------------------------------------------------------------------
92 -- Models
93
94 -- A model is given by an interpretation (Den $s$) for each sort $s$
95 -- plus an interpretation (den $o$) for each operator $o$.
96
97 record SetModel ℓᵐ : Set (ℓˢ ⊔ ℓᵒ ⊔ ℓᵃ ⊔ suc ℓᵐ) where
98 field
99 Den : Sort → Set ℓᵐ
100 den : {s : Sort} → ⟦ Ops ⟧ Den s → Den s
101
102 -- The setoid model requires operators to respect equality.
103 -- The Func record packs a function (apply) with a proof (cong)
104 -- that the function maps equals to equals.
105
106 record SetoidModel ℓᵐ ℓᵉ : Set (ℓˢ ⊔ ℓᵒ ⊔ ℓᵃ ⊔ suc (ℓᵐ ⊔ ℓᵉ)) where
107 field
108 Den : Sort → Setoid ℓᵐ ℓᵉ
109 den : {s : Sort} → Func (⟦ Ops ⟧s Den s) (Den s)
110
111
112 ------------------------------------------------------------------------
113 -- Terms
114
115 -- To obtain terms with free variables, we add additional nullary
116 -- operators, each representing a variable.
117 --
118 -- These are covered in the standard library FreeMonad module,
119 -- albeit with the restriction that the operator and variable sets
120 -- have the same size.
121
122 Cxt = Sort → Set ℓᵒ
123
124 variable
125 Γ Δ : Cxt
126
127 -- Terms with free variables in Var.
128
129 module _ (Var : Cxt) where
130
131 -- We keep the same sorts, but add a nullary operator for each variable.
132
133 Ops⁺ : Container Sort Sort ℓᵒ ℓᵃ
134 Ops⁺ = Ops ⋆C Var
135
136 -- Terms with variables are then given by the W-type for the extended container.
137
138 Tm = W Ops⁺
139
140 -- We define nice constructors for variables and operator application
141 -- via pattern synonyms.
142 -- Note that the $f$ in constructor var' is a function from the empty set,
143 -- so it should be uniquely determined. However, Agda's equality is
144 -- more intensional and will not identify all functions from the empty set.
145 -- Since we do not make use of the axiom of function extensionality,
146 -- we sometimes have to consult the extensional equality of the
147 -- function setoid.
148
149 pattern _∙_ op args = sup (inj₂ op , args)
150 pattern var' x f = sup (inj₁ x , f )
151 pattern var x = var' x _
152
153 -- Letter $t$ ranges over terms, and $\mathit{ts}$ over argument vectors.
154
155 variable
156 t t' t₁ t₂ t₃ : Tm Γ s
157 ts ts' : (i : Arity op) → Tm Γ (sort _ i)
158
159 ------------------------------------------------------------------------
160 -- Parallel substitutions
161
162 -- A substitution from Δ to Γ holds a term in Γ for each variable in Δ.
163
164 Sub : (Γ Δ : Cxt) → Set _
165 Sub Γ Δ = ∀{s} (x : Δ s) → Tm Γ s
166
167 -- Application of a substitution.
168
169 _[_] : (t : Tm Δ s) (σ : Sub Γ Δ) → Tm Γ s
170 (var x ) [ σ ] = σ x
171 (op ∙ ts) [ σ ] = op ∙ λ i → ts i [ σ ]
172
173 -- Letter $σ$ ranges over substitutions.
174
175 variable
176 σ σ' : Sub Γ Δ
177
178 ------------------------------------------------------------------------
179 -- Interpretation of terms in a model
180 ------------------------------------------------------------------------
181
182 -- Given an algebra $M$ of set-size $ℓ^m$ and equality-size $ℓ^e$,
183 -- we define the interpretation of an $s$-sorted term $t$ as element
184 -- of $M(s)$ according to an environment $ρ$ that maps each variable
185 -- of sort $s'$ to an element of $M(s')$.
186
187 module _ {M : SetoidModel ℓᵐ ℓᵉ} where
188 open SetoidModel M
189
190 -- Equality in $M$'s interpretation of sort $s$.
191
192 _≃_ : Den s .Carrier → Den s .Carrier → Set _
193 _≃_ {s = s} = Den s ._≈_
194
195 -- An environment for Γ maps each variable $x : Γ(s)$ to an element of $M(s)$.
196 -- Equality of environments is defined pointwise.
197
198 Env : Cxt → Setoid _ _
199 Env Γ .Carrier = {s : Sort} (x : Γ s) → Den s .Carrier
200 Env Γ ._≈_ ρ ρ' = {s : Sort} (x : Γ s) → ρ x ≃ ρ' x
201 Env Γ .isEquivalence .IsEquivalence.refl {s = s} x = Den s .Setoid.refl
202 Env Γ .isEquivalence .IsEquivalence.sym h {s} x = Den s .Setoid.sym (h x)
203 Env Γ .isEquivalence .IsEquivalence.trans g h {s} x = Den s .Setoid.trans (g x) (h x)
204
205 -- Interpretation of terms is iteration on the W-type.
206 -- The standard library offers `iter' (on sets), but we need this to be a Func (on setoids).
207
208 ⦅_⦆ : ∀{s} (t : Tm Γ s) → Func (Env Γ) (Den s)
209 ⦅ var x ⦆ .apply ρ = ρ x
210 ⦅ var x ⦆ .cong ρ=ρ' = ρ=ρ' x
211 ⦅ op ∙ args ⦆ .apply ρ = den .apply (op , λ i → ⦅ args i ⦆ .apply ρ)
212 ⦅ op ∙ args ⦆ .cong ρ=ρ' = den .cong (refl , λ i → ⦅ args i ⦆ .cong ρ=ρ')
213
214 -- An equality between two terms holds in a model
215 -- if the two terms are equal under all valuations of their free variables.
216
217 Equal : ∀ {Γ s} (t t' : Tm Γ s) → Set _
218 Equal {Γ} {s} t t' = ∀ (ρ : Env Γ .Carrier) → ⦅ t ⦆ .apply ρ ≃ ⦅ t' ⦆ .apply ρ
219
220 -- This notion is an equivalence relation.
221
222 isEquiv : IsEquivalence (Equal {Γ = Γ} {s = s})
223 isEquiv {s = s} .IsEquivalence.refl ρ = Den s .Setoid.refl
224 isEquiv {s = s} .IsEquivalence.sym e ρ = Den s .Setoid.sym (e ρ)
225 isEquiv {s = s} .IsEquivalence.trans e e' ρ = Den s .Setoid.trans (e ρ) (e' ρ)
226
227 ------------------------------------------------------------------------
228 -- Substitution lemma
229
230 -- Evaluation of a substitution gives an environment.
231
232 ⦅_⦆s : Sub Γ Δ → Env Γ .Carrier → Env Δ .Carrier
233 ⦅ σ ⦆s ρ x = ⦅ σ x ⦆ .apply ρ
234
235 -- Substitution lemma: ⦅t[σ]⦆ρ ≃ ⦅t⦆⦅σ⦆ρ
236
237 substitution : (t : Tm Δ s) (σ : Sub Γ Δ) (ρ : Env Γ .Carrier) →
238 ⦅ t [ σ ] ⦆ .apply ρ ≃ ⦅ t ⦆ .apply (⦅ σ ⦆s ρ)
239 substitution (var x) σ ρ = Den _ .Setoid.refl
240 substitution (op ∙ ts) σ ρ = den .cong (refl , λ i → substitution (ts i) σ ρ)
241
242 ------------------------------------------------------------------------
243 -- Equations
244
245 -- An equation is a pair $t ≐ t'$ of terms of the same sort in the same context.
246
247 record Eq : Set (ℓˢ ⊔ suc ℓᵒ ⊔ ℓᵃ) where
248 constructor _≐_
249 field
250 {cxt} : Sort → Set ℓᵒ
251 {srt} : Sort
252 lhs : Tm cxt srt
253 rhs : Tm cxt srt
254
255 -- Equation $t ≐ t'$ holding in model $M$.
256
257 _⊧_ : (M : SetoidModel ℓᵐ ℓᵉ) (eq : Eq) → Set _
258 M ⊧ (t ≐ t') = Equal {M = M} t t'
259
260 -- Sets of equations are presented as collection E : I → Eq
261 -- for some index set I : Set ℓⁱ.
262
263 -- An entailment/consequence $E ⊃ t ≐ t'$ is valid
264 -- if $t ≐ t'$ holds in all models that satify equations $E$.
265
266 module _ {ℓᵐ ℓᵉ} where
267
268 _⊃_ : (E : I → Eq) (eq : Eq) → Set _
269 E ⊃ eq = ∀ (M : SetoidModel ℓᵐ ℓᵉ) → (∀ i → M ⊧ E i) → M ⊧ eq
270
271 -- Derivations
272 --------------
273
274 -- Equalitional logic allows us to prove entailments via the
275 -- inference rules for the judgment $E ⊢ Γ ▹ t ≡ t'$.
276 -- This could be coined as equational theory over a given
277 -- set of equations $E$.
278 -- Relation $E ⊢ Γ ▹ \_ ≡ \_$ is the least congruence over the equations $E$.
279
280 data _⊢_▹_≡_ {I : Set ℓⁱ}
281 (E : I → Eq) : (Γ : Cxt) (t t' : Tm Γ s) → Set (ℓˢ ⊔ suc ℓᵒ ⊔ ℓᵃ ⊔ ℓⁱ) where
282
283 hyp : ∀ i → let t ≐ t' = E i in
284 E ⊢ _ ▹ t ≡ t'
285
286 base : ∀ (x : Γ s) {f f' : (i : ⊥) → Tm _ (⊥-elim i)} →
287 E ⊢ Γ ▹ var' x f ≡ var' x f'
288
289 app : (∀ i → E ⊢ Γ ▹ ts i ≡ ts' i) →
290 E ⊢ Γ ▹ (op ∙ ts) ≡ (op ∙ ts')
291
292 sub : E ⊢ Δ ▹ t ≡ t' →
293 ∀ (σ : Sub Γ Δ) →
294 E ⊢ Γ ▹ (t [ σ ]) ≡ (t' [ σ ])
295
296 refl : ∀ (t : Tm Γ s) →
297 E ⊢ Γ ▹ t ≡ t
298
299 sym : E ⊢ Γ ▹ t ≡ t' →
300 E ⊢ Γ ▹ t' ≡ t
301
302 trans : E ⊢ Γ ▹ t₁ ≡ t₂ →
303 E ⊢ Γ ▹ t₂ ≡ t₃ →
304 E ⊢ Γ ▹ t₁ ≡ t₃
305
306 ------------------------------------------------------------------------
307 -- Soundness of the inference rules
308
309 -- We assume a model $M$ that validates all equations in $E$.
310
311 module Soundness {I : Set ℓⁱ} (E : I → Eq) (M : SetoidModel ℓᵐ ℓᵉ)
312 (V : ∀ i → M ⊧ E i) where
313 open SetoidModel M
314
315 -- In any model $M$ that satisfies the equations $E$,
316 -- derived equality is actual equality.
317
318 sound : E ⊢ Γ ▹ t ≡ t' → M ⊧ (t ≐ t')
319
320 sound (hyp i) = V i
321 sound (app {op = op} es) ρ = den .cong (refl , λ i → sound (es i) ρ)
322 sound (sub {t = t} {t' = t'} e σ) ρ = begin
323 ⦅ t [ σ ] ⦆ .apply ρ ≈⟨ substitution {M = M} t σ ρ ⟩
324 ⦅ t ⦆ .apply ρ' ≈⟨ sound e ρ' ⟩
325 ⦅ t' ⦆ .apply ρ' ≈⟨ substitution {M = M} t' σ ρ ⟨
326 ⦅ t' [ σ ] ⦆ .apply ρ ∎
327 where
328 open SetoidReasoning (Den _)
329 ρ' = ⦅ σ ⦆s ρ
330
331 sound (base x {f} {f'}) = isEquiv {M = M} .IsEquivalence.refl {var' x λ()}
332
333 sound (refl t) = isEquiv {M = M} .IsEquivalence.refl {t}
334 sound (sym {t = t} {t' = t'} e) = isEquiv {M = M} .IsEquivalence.sym
335 {x = t} {y = t'} (sound e)
336 sound (trans {t₁ = t₁} {t₂ = t₂}
337 {t₃ = t₃} e e') = isEquiv {M = M} .IsEquivalence.trans
338 {i = t₁} {j = t₂} {k = t₃} (sound e) (sound e')
339
340
341 ------------------------------------------------------------------------
342 -- Birkhoff's completeness theorem
343 ------------------------------------------------------------------------
344
345 -- Birkhoff proved that any equation $t ≐ t'$ is derivable from $E$
346 -- when it is valid in all models satisfying $E$. His proof (for
347 -- single-sorted algebras) is a blue print for many more
348 -- completeness proofs. They all proceed by constructing a
349 -- universal model aka term model. In our case, it is terms
350 -- quotiented by derivable equality $E ⊢ Γ ▹ \_ ≡ \_$. It then
351 -- suffices to prove that this model satisfies all equations in $E$.
352
353 ------------------------------------------------------------------------
354 -- Universal model
355
356 -- A term model for $E$ and $Γ$ interprets sort $s$ by (Tm Γ s) quotiented by $E ⊢ Γ ▹ \_ ≡ \_$.
357
358 module TermModel {I : Set ℓⁱ} (E : I → Eq) where
359 open SetoidModel
360
361 -- Tm Γ s quotiented by E⊢Γ▹·≡·.
362
363 TmSetoid : Cxt → Sort → Setoid _ _
364 TmSetoid Γ s .Carrier = Tm Γ s
365 TmSetoid Γ s ._≈_ = E ⊢ Γ ▹_≡_
366 TmSetoid Γ s .isEquivalence .IsEquivalence.refl = refl _
367 TmSetoid Γ s .isEquivalence .IsEquivalence.sym = sym
368 TmSetoid Γ s .isEquivalence .IsEquivalence.trans = trans
369
370 -- The interpretation of an operator is simply the operator.
371 -- This works because $E⊢Γ▹\_≡\_$ is a congruence.
372
373 tmInterp : ∀ {Γ s} → Func (⟦ Ops ⟧s (TmSetoid Γ) s) (TmSetoid Γ s)
374 tmInterp .apply (op , ts) = op ∙ ts
375 tmInterp .cong (refl , h) = app h
376
377 -- The term model per context Γ.
378
379 M : Cxt → SetoidModel _ _
380 M Γ .Den = TmSetoid Γ
381 M Γ .den = tmInterp
382
383 -- The identity substitution σ₀ maps variables to themselves.
384
385 σ₀ : {Γ : Cxt} → Sub Γ Γ
386 σ₀ x = var' x λ()
387
388 -- σ₀ acts indeed as identity.
389
390 identity : (t : Tm Γ s) → E ⊢ Γ ▹ t [ σ₀ ] ≡ t
391 identity (var x) = base x
392 identity (op ∙ ts) = app λ i → identity (ts i)
393
394 -- Evaluation in the term model is substitution $E ⊢ Γ ▹ ⦅t⦆σ ≡ t[σ]$.
395 -- This would even hold "up to the nose" if we had function extensionality.
396
397 evaluation : (t : Tm Δ s) (σ : Sub Γ Δ) → E ⊢ Γ ▹ (⦅_⦆ {M = M Γ} t .apply σ) ≡ (t [ σ ])
398 evaluation (var x) σ = refl (σ x)
399 evaluation (op ∙ ts) σ = app (λ i → evaluation (ts i) σ)
400
401 -- The term model satisfies all the equations it started out with.
402
403 satisfies : ∀ i → M Γ ⊧ E i
404 satisfies i σ = begin
405 ⦅ tₗ ⦆ .apply σ ≈⟨ evaluation tₗ σ ⟩
406 tₗ [ σ ] ≈⟨ sub (hyp i) σ ⟩
407 tᵣ [ σ ] ≈⟨ evaluation tᵣ σ ⟨
408 ⦅ tᵣ ⦆ .apply σ ∎
409 where
410 open SetoidReasoning (TmSetoid _ _)
411 tₗ = E i .Eq.lhs
412 tᵣ = E i .Eq.rhs
413
414 ------------------------------------------------------------------------
415 -- Completeness
416
417 -- Birkhoff's completeness theorem \citeyearpar{birkhoff:1935}:
418 -- Any valid consequence is derivable in the equational theory.
419
420 module Completeness {I : Set ℓⁱ} (E : I → Eq) {Γ s} {t t' : Tm Γ s} where
421 open TermModel E
422
423 completeness : E ⊃ (t ≐ t') → E ⊢ Γ ▹ t ≡ t'
424 completeness V = begin
425 t ≈˘⟨ identity t ⟩
426 t [ σ₀ ] ≈˘⟨ evaluation t σ₀ ⟩
427 ⦅ t ⦆ .apply σ₀ ≈⟨ V (M Γ) satisfies σ₀ ⟩
428 ⦅ t' ⦆ .apply σ₀ ≈⟨ evaluation t' σ₀ ⟩
429 t' [ σ₀ ] ≈⟨ identity t' ⟩
430 t' ∎
431 where open SetoidReasoning (TmSetoid Γ s)