Debian Codegraph @

src/Data/List/Relation/Binary/Sublist/Propositional/Example/UniqueBoundVariables.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A larger example for sublists (propositional case):
5 -- Simply-typed lambda terms with globally unique variables
6 -- (both bound and free ones).
7 ------------------------------------------------------------------------
8
9 {-# OPTIONS --cubical-compatible --safe #-}
10
11 module Data.List.Relation.Binary.Sublist.Propositional.Example.UniqueBoundVariables (Base : Set) where
12
13 open import Relation.Binary.PropositionalEquality.Core
14 using (_≡_; refl; sym; cong; subst)
15 open import Relation.Binary.PropositionalEquality.Properties
16 using (module ≡-Reasoning)
17 open ≡-Reasoning
18
19 open import Data.List.Base using (List; []; _∷_; [_])
20 open import Data.List.Membership.Propositional using (_∈_)
21 open import Data.List.Relation.Unary.All using (Null; [])
22 open import Data.List.Relation.Binary.Sublist.Propositional using
23 ( _⊆_; []; _∷_; _∷ʳ_
24 ; ⊆-refl; ⊆-trans; minimum
25 ; from∈; to∈; lookup
26 ; ⊆-pushoutˡ; RawPushout
27 ; Disjoint; DisjointUnion
28 ; separateˡ; Separation
29 )
30 open import Data.List.Relation.Binary.Sublist.Propositional.Properties using
31 ( ∷ˡ⁻
32 ; ⊆-trans-assoc
33 ; from∈∘to∈; from∈∘lookup; lookup-⊆-trans
34 ; ⊆-pushoutˡ-is-wpo
35 ; Disjoint→DisjointUnion; DisjointUnion→Disjoint
36 ; Disjoint-sym; DisjointUnion-inj₁; DisjointUnion-inj₂; DisjointUnion-[]ʳ
37 ; weakenDisjoint; weakenDisjointUnion; shrinkDisjointˡ
38 ; disjoint⇒disjoint-to-union; DisjointUnion-fromAny∘toAny-∷ˡ⁻
39 ; equalize-separators
40 )
41
42 open import Data.Product.Base using (_,_; proj₁; proj₂)
43
44 infixr 8 _⇒_
45 infix 1 _⊢_~_▷_
46
47 -- Simple types over a set Base of base types.
48
49 data Ty : Set where
50 base : (o : Base) → Ty
51 _⇒_ : (a b : Ty) → Ty
52
53 -- Typing contexts are lists of types.
54
55 Cxt = List Ty
56
57 variable
58 a b : Ty
59 Γ Δ : Cxt
60 x y : a ∈ Γ
61
62 -- The familiar intrinsically well-typed formulation of STLC
63 -- where a de Bruijn index x is a pointer into the context.
64
65 module DeBruijn where
66
67 data Tm (Δ : Cxt) : (a : Ty) → Set where
68 var : (x : a ∈ Δ) → Tm Δ a
69 abs : (t : Tm (a ∷ Δ) b) → Tm Δ (a ⇒ b)
70 app : (t : Tm Δ (a ⇒ b)) (u : Tm Δ a) → Tm Δ b
71
72 -- We formalize now intrinsically well-typed STLC with
73 -- named variables that are globally unique, i.e.,
74 -- each variable can be bound at most once.
75
76 -- List of bound variables of a term.
77
78 BVars = List Ty
79
80 variable
81 B : BVars
82 noBV : Null B
83
84 -- There is a single global context Γ of all variables used in the terms.
85 -- Each list of bound variables B is a sublist of Γ.
86
87 variable
88 β βₜ βᵤ yβ β\y : B ⊆ Γ
89
90 -- Named terms are parameterized by a sublist β : B ⊆ Γ of bound variables.
91 -- Variables outside B can occur as free variables in a term.
92 --
93 -- * Variables x do not contain any bound variables (Null B).
94 --
95 -- * The bound variables of an application (t u) is the disjoint union
96 -- of the bound variables βₜ of t and βᵤ of u.
97 --
98 -- * The bound variables β of an abstraction λyt is the disjoint union
99 -- of the single variable y and the bound variables β\y of t.
100
101 module UniquelyNamed where
102
103 data Tm (β : B ⊆ Γ) : (a : Ty) → Set where
104
105 var : (noBV : Null B)
106 (x : a ∈ Γ)
107 → Tm β a
108
109 abs : (y : a ∈ Γ)
110 (y# : DisjointUnion (from∈ y) β\y β)
111 (t : Tm β\y b)
112 → Tm β (a ⇒ b)
113
114 app : (t : Tm βₜ (a ⇒ b))
115 (u : Tm βᵤ a)
116 (t#u : DisjointUnion βₜ βᵤ β)
117 → Tm β b
118
119 pattern var! x = var [] x
120
121 -- Bound variables β : B ⊆ Γ can be considered in a larger context Γ′
122 -- obtained by γ : Γ ⊆ Γ′. The embedding β′ : B ⊆ Γ′ is simply the
123 -- composition of β and γ, and terms can be coerced recursively:
124
125 weakenBV : ∀ {Γ B Γ′} {β : B ⊆ Γ} (γ : Γ ⊆ Γ′) →
126 Tm β a → Tm (⊆-trans β γ) a
127
128 weakenBV γ (var noBV x) = var noBV (lookup γ x)
129 weakenBV γ (app t u t#u) = app (weakenBV γ t) (weakenBV γ u) (weakenDisjointUnion γ t#u)
130 weakenBV γ (abs y y# t) = abs y′ y′# (weakenBV γ t)
131 where
132 y′ = lookup γ y
133 -- Typing: y′# : DisjointUnion (from∈ y′) (⊆-trans β\y γ) (⊆-trans β γ)
134 y′# = subst (λ □ → DisjointUnion □ _ _) (sym (from∈∘lookup _ _)) (weakenDisjointUnion γ y#)
135
136 -- We bring de Bruijn terms into scope as Exp.
137
138 open DeBruijn renaming (Tm to Exp)
139 open UniquelyNamed
140
141 variable
142 t u : Tm β a
143 f e : Exp Δ a
144
145 -- Relating de Bruijn terms and uniquely named terms.
146 --
147 -- The judgement δ ⊢ e ~ β ▷ t relates a de Bruijn term e with
148 -- potentially free variables δ : Δ ⊆ Γ to a named term t with exact
149 -- bound variables β : B ⊆ Γ. The intention is to relate exactly the
150 -- terms with the same meaning.
151 --
152 -- The judgement will imply the disjointness of Δ and B.
153
154 variable
155 δ yδ : Δ ⊆ Γ
156
157 data _⊢_~_▷_ {Γ Δ : Cxt} (δ : Δ ⊆ Γ) : ∀{a} (e : Exp Δ a) {B} (β : B ⊆ Γ) (t : Tm β a) → Set where
158
159 -- Free de Bruijn index x : a ∈ Δ is related to free variable
160 -- y : a ∈ Γ if δ : Δ ⊆ Γ maps x to y.
161
162 var : ∀{y} (δx≡y : lookup δ x ≡ y) (δ#β : Disjoint δ β)
163 → δ ⊢ var x ~ β ▷ var! y
164
165 -- Unnamed lambda δ ⊢ λ.e is related to named lambda y,β ▷ λy.t
166 -- if body y,δ ⊢ e is related to body β ▷ t.
167
168 abs : (y#δ : DisjointUnion (from∈ y) δ yδ)
169 → (y#β : DisjointUnion (from∈ y) β yβ)
170 → yδ ⊢ e ~ β ▷ t
171 → δ ⊢ abs e ~ yβ ▷ abs y y#β t
172
173 -- Application δ ⊢ f e is related to application βₜ,βᵤ ▷ t u
174 -- if function δ ⊢ f is related to βₜ ▷ t
175 -- and argument δ ⊢ e is related to βᵤ ▷ u.
176
177 app : δ ⊢ f ~ βₜ ▷ t
178 → δ ⊢ e ~ βᵤ ▷ u
179 → (t#u : DisjointUnion βₜ βᵤ β)
180 → δ ⊢ app f e ~ β ▷ app t u t#u
181
182 -- A dependent substitution lemma for ~.
183 -- Trivial, but needed because term equality t : Tm β a ≡ t′ : Tm β′ a is heterogeneous,
184 -- or, more precisely, indexed by a sublist equality β ≡ β′.
185
186 subst~ : ∀ {a Δ Γ B} {δ δ′ : Δ ⊆ Γ} {β β′ : B ⊆ Γ}
187 {e : Exp Δ a} {t : Tm β a} {t′ : Tm β′ a}
188 (δ≡δ′ : δ ≡ δ′)
189 (β≡β′ : β ≡ β′)
190 (t≡t′ : subst (λ □ → Tm □ a) β≡β′ t ≡ t′) →
191 δ ⊢ e ~ β ▷ t →
192 δ′ ⊢ e ~ β′ ▷ t′
193 subst~ refl refl refl d = d
194
195 -- The judgement δ ⊢ e ~ β ▷ t relative to Γ
196 -- can be transported to a bigger context γ : Γ ⊆ Γ′.
197
198 weaken~ : ∀{a Δ B Γ Γ′} {δ : Δ ⊆ Γ} {β : B ⊆ Γ} {e : Exp Δ a} {t : Tm β a} (γ : Γ ⊆ Γ′)
199 (let δ′ = ⊆-trans δ γ)
200 (let β′ = ⊆-trans β γ)
201 (let t′ = weakenBV γ t) →
202 δ ⊢ e ~ β ▷ t →
203 δ′ ⊢ e ~ β′ ▷ t′
204 weaken~ γ (var refl δ#β) = var (lookup-⊆-trans γ _) (weakenDisjoint γ δ#β)
205 weaken~ γ (abs y#δ y#β d) = abs y′#δ′ y′#β′ (weaken~ γ d)
206 where
207 y′#δ′ = subst (λ □ → DisjointUnion □ _ _) (sym (from∈∘lookup _ _)) (weakenDisjointUnion γ y#δ)
208 y′#β′ = subst (λ □ → DisjointUnion □ _ _) (sym (from∈∘lookup _ _)) (weakenDisjointUnion γ y#β)
209 weaken~ γ (app dₜ dᵤ t#u) = app (weaken~ γ dₜ) (weaken~ γ dᵤ) (weakenDisjointUnion γ t#u)
210
211 -- Lemma: If δ ⊢ e ~ β ▷ t, then
212 -- the (potentially) free variables δ of the de Bruijn term e
213 -- are disjoint from the bound variables β of the named term t.
214
215 disjoint-fv-bv : δ ⊢ e ~ β ▷ t → Disjoint δ β
216 disjoint-fv-bv (var _ δ#β) = δ#β
217
218 disjoint-fv-bv {β = yβ} (abs y⊎δ y⊎β d) = δ#yβ
219 where
220 δ#y = Disjoint-sym (DisjointUnion→Disjoint y⊎δ)
221 yδ#β = disjoint-fv-bv d
222 δ⊆yδ,eq = DisjointUnion-inj₂ y⊎δ
223 δ⊆yδ = proj₁ δ⊆yδ,eq
224 eq = proj₂ δ⊆yδ,eq
225 δ#β = subst (λ □ → Disjoint □ _) eq (shrinkDisjointˡ δ⊆yδ yδ#β)
226 δ#yβ = disjoint⇒disjoint-to-union δ#y δ#β y⊎β
227
228 disjoint-fv-bv (app dₜ dᵤ βₜ⊎βᵤ) = disjoint⇒disjoint-to-union δ#βₜ δ#βᵤ βₜ⊎βᵤ
229 where
230 δ#βₜ = disjoint-fv-bv dₜ
231 δ#βᵤ = disjoint-fv-bv dᵤ
232
233
234 -- Translating de Bruijn terms to uniquely named terms.
235 --
236 -- Given a de Bruijn term Δ ⊢ e : a, we seek to produce a named term
237 -- β ▷ t : a that is related to the de Bruijn term. On the way, we have
238 -- to compute the global context Γ that hosts all free and bound
239 -- variables of t.
240
241 -- Record (NamedOf e) collects all the outputs of the translation of e.
242
243 record NamedOf (e : Exp Δ a) : Set where
244 constructor mkNamedOf
245 field
246 {glob} : Cxt -- Γ
247 emb : Δ ⊆ glob -- δ : Δ ⊆ Γ
248 {bv} : BVars -- B
249 bound : bv ⊆ glob -- β : B ⊆ Γ
250 {tm} : Tm bound a -- t : Tm β a
251 relate : emb ⊢ e ~ bound ▷ tm -- δ ⊢ e ~ β ▷ t
252
253 -- The translation.
254
255 dB→Named : (e : Exp Δ a) → NamedOf e
256
257 -- For the translation of a variable x : a ∈ Δ, we can pick Γ := Δ and B := [].
258 -- Δ and B are obviously disjoint subsets of Γ.
259
260 dB→Named (var x) = record
261 { emb = ⊆-refl -- Γ := Δ
262 ; bound = minimum _ -- no bound variables
263 ; relate = var refl (DisjointUnion→Disjoint DisjointUnion-[]ʳ)
264 }
265
266 -- For the translation of an abstraction
267 --
268 -- abs (t : Exp (a ∷ Δ) b) : Exp Δ (a ⇒ b)
269 --
270 -- we recursively have Γ, B and β : B ⊆ Γ with z,δ : (a ∷ Δ) ⊆ Γ
271 -- and know that B # a∷Δ.
272 --
273 -- We keep Γ and produce embedding δ : Δ ⊆ Γ and bound variables z ⊎ β.
274
275 dB→Named {Δ = Δ} {a = a ⇒ b} (abs e) with dB→Named e
276 ... | record{ glob = Γ; emb = zδ; bound = β; relate = d } =
277 record
278 { glob = Γ
279 ; emb = δ̇
280 ; bound = proj₁ (proj₂ z⊎β)
281 ; relate = abs [a]⊆Γ⊎δ (proj₂ (proj₂ z⊎β)) d
282 }
283 where
284 -- Typings:
285 -- zδ : a ∷ Δ ⊆ Γ
286 -- β : bv ⊆ Γ
287 zδ#β = disjoint-fv-bv d
288 z : a ∈ Γ
289 z = to∈ zδ
290 [a]⊆Γ = from∈ z
291 δ̇ = ∷ˡ⁻ zδ
292 [a]⊆Γ⊎δ = DisjointUnion-fromAny∘toAny-∷ˡ⁻ zδ
293 [a]⊆aΔ : [ a ] ⊆ (a ∷ Δ)
294 [a]⊆aΔ = refl ∷ minimum _
295 eq : ⊆-trans [a]⊆aΔ zδ ≡ [a]⊆Γ
296 eq = sym (from∈∘to∈ _)
297 z#β : Disjoint [a]⊆Γ β
298 z#β = subst (λ □ → Disjoint □ β) eq (shrinkDisjointˡ [a]⊆aΔ zδ#β)
299 z⊎β = Disjoint→DisjointUnion z#β
300
301 -- For the translation of an application (f e) we have by induction
302 -- hypothesis two independent extensions δ₁ : Δ ⊆ Γ₁ and δ₂ : Δ ⊆ Γ₂
303 -- and two bound variable lists β₁ : B₁ ⊆ Γ₁ and β₂ : B₂ ⊆ Γ₂.
304 -- We need to find a common global context Γ such that
305 --
306 -- (a) δ : Δ ⊆ Γ and
307 -- (b) the bound variables embed disjointly as β₁″ : B₁ ⊆ Γ and β₂″ : B₂ ⊆ Γ.
308 --
309 -- (a) δ is (eventually) found via a weak pushout of δ₁ and δ₂, giving
310 -- ϕ₁ : Γ₁ ⊆ Γ₁₂ and ϕ₂ : Γ₂ ⊆ Γ₁₂.
311 --
312 -- (b) The bound variable embeddings
313 --
314 -- β₁′ = β₁ϕ₁ : B₁ ⊆ Γ₁₂ and
315 -- β₂′ = β₂ϕ₂ : B₂ ⊆ Γ₁₂ and
316 --
317 -- may be overlapping, but we can separate them by enlarging the global
318 -- context to Γ with two embeddings
319 --
320 -- γ₁ : Γ₁₂ ⊆ Γ
321 -- γ₂ : Γ₁₂ ⊆ Γ
322 --
323 -- such that
324 --
325 -- β₁″ = β₁′γ₁ : B₁ ⊆ Γ
326 -- β₂″ = β₂′γ₂ : B₂ ⊆ Γ
327 --
328 -- are disjoint. Since Δ is disjoint to both B₁ and B₂ we have equality of
329 --
330 -- δ₁ϕ₁γ₁ : Δ ⊆ Γ
331 -- δ₂ϕ₂γ₂ : Δ ⊆ Γ
332 --
333 -- Thus, we can return either of them as δ.
334
335 dB→Named (app f e) with dB→Named f | dB→Named e
336 ... | mkNamedOf {Γ₁} δ₁ β₁ {t} d₁ | mkNamedOf {Γ₂} δ₂ β₂ {u} d₂ =
337 mkNamedOf δ̇ β̇ (app d₁″ d₂″ β₁″⊎β₂″)
338 where
339 -- Disjointness of δᵢ and βᵢ from induction hypotheses.
340 δ₁#β₁ = disjoint-fv-bv d₁
341 δ₂#β₂ = disjoint-fv-bv d₂
342
343 -- join δ₁ and δ₂ via weak pushout
344 po = ⊆-pushoutˡ δ₁ δ₂
345 Γ₁₂ = RawPushout.upperBound po
346 ϕ₁ = RawPushout.leg₁ po
347 ϕ₂ = RawPushout.leg₂ po
348 δ₁′ = ⊆-trans δ₁ ϕ₁
349 δ₂′ = ⊆-trans δ₂ ϕ₂
350 β₁′ = ⊆-trans β₁ ϕ₁
351 β₂′ = ⊆-trans β₂ ϕ₂
352 δ₁′#β₁′ : Disjoint δ₁′ β₁′
353 δ₁′#β₁′ = weakenDisjoint ϕ₁ δ₁#β₁
354 δ₂′#β₂′ : Disjoint δ₂′ β₂′
355 δ₂′#β₂′ = weakenDisjoint ϕ₂ δ₂#β₂
356 δ₁′≡δ₂′ : δ₁′ ≡ δ₂′
357 δ₁′≡δ₂′ = ⊆-pushoutˡ-is-wpo δ₁ δ₂
358 δ₂′#β₁′ : Disjoint δ₂′ β₁′
359 δ₂′#β₁′ = subst (λ □ → Disjoint □ β₁′) δ₁′≡δ₂′ δ₁′#β₁′
360
361 -- separate β₁ and β₂
362 sep : Separation β₁′ β₂′
363 sep = separateˡ β₁′ β₂′
364 γ₁ = Separation.separator₁ sep
365 γ₂ = Separation.separator₂ sep
366 β₁″ = Separation.separated₁ sep
367 β₂″ = Separation.separated₂ sep
368
369 -- produce their disjoint union
370 uni = Disjoint→DisjointUnion (Separation.disjoint sep)
371 β̇ = proj₁ (proj₂ uni)
372 β₁″⊎β₂″ : DisjointUnion β₁″ β₂″ β̇
373 β₁″⊎β₂″ = proj₂ (proj₂ uni)
374 ι₁ = DisjointUnion-inj₁ β₁″⊎β₂″
375 ι₂ = DisjointUnion-inj₂ β₁″⊎β₂″
376
377 -- after separation, the FVs are still disjoint from the BVs.
378 δ₁″ = ⊆-trans δ₂′ γ₁
379 δ₂″ = ⊆-trans δ₂′ γ₂
380 δ₁″≡δ₂″ : δ₁″ ≡ δ₂″
381 δ₁″≡δ₂″ = equalize-separators δ₂′#β₁′ δ₂′#β₂′
382 δ₁″#β₁″ : Disjoint δ₁″ β₁″
383 δ₁″#β₁″ = weakenDisjoint γ₁ δ₂′#β₁′
384 δ₂″#β₂″ : Disjoint δ₂″ β₂″
385 δ₂″#β₂″ = weakenDisjoint γ₂ δ₂′#β₂′
386 δ̇ = δ₂″
387 δ₂″#β₁″ : Disjoint δ₂″ β₁″
388 δ₂″#β₁″ = subst (λ □ → Disjoint □ β₁″) δ₁″≡δ₂″ δ₁″#β₁″
389 δ̇#β̇ : Disjoint δ̇ β̇
390 δ̇#β̇ = disjoint⇒disjoint-to-union δ₂″#β₁″ δ₂″#β₂″ β₁″⊎β₂″
391
392 -- Combined weakening from Γᵢ to Γ
393 γ₁′ = ⊆-trans ϕ₁ γ₁
394 γ₂′ = ⊆-trans ϕ₂ γ₂
395
396 -- Weakening and converting the first derivation.
397 d₁′ : ⊆-trans δ₁ γ₁′ ⊢ f ~ ⊆-trans β₁ γ₁′ ▷ weakenBV γ₁′ t
398 d₁′ = weaken~ γ₁′ d₁
399 δ₁≤δ̇ : ⊆-trans δ₁ γ₁′ ≡ ⊆-trans δ₂′ γ₂
400 δ₁≤δ̇ = begin
401 ⊆-trans δ₁ γ₁′ ≡⟨ ⊆-trans-assoc ⟩
402 ⊆-trans δ₁′ γ₁ ≡⟨ cong (λ □ → ⊆-trans □ γ₁) δ₁′≡δ₂′ ⟩
403 ⊆-trans δ₂′ γ₁ ≡⟨⟩
404 δ₁″ ≡⟨ δ₁″≡δ₂″ ⟩
405 δ₂″ ≡⟨⟩
406 δ̇ ∎
407 β₁≤β₁″ : ⊆-trans β₁ γ₁′ ≡ β₁″
408 β₁≤β₁″ = ⊆-trans-assoc
409 d₁″ : δ̇ ⊢ f ~ β₁″ ▷ subst (λ □ → Tm □ _) β₁≤β₁″ (weakenBV γ₁′ t)
410 d₁″ = subst~ δ₁≤δ̇ β₁≤β₁″ refl d₁′
411
412 -- Weakening and converting the second derivation.
413 d₂′ : ⊆-trans δ₂ γ₂′ ⊢ e ~ ⊆-trans β₂ γ₂′ ▷ weakenBV γ₂′ u
414 d₂′ = weaken~ γ₂′ d₂
415 β₂≤β₂″ : ⊆-trans β₂ γ₂′ ≡ β₂″
416 β₂≤β₂″ = ⊆-trans-assoc
417 δ₂≤δ̇ : ⊆-trans δ₂ γ₂′ ≡ δ̇
418 δ₂≤δ̇ = ⊆-trans-assoc
419 d₂″ : δ̇ ⊢ e ~ β₂″ ▷ subst (λ □ → Tm □ _) β₂≤β₂″ (weakenBV γ₂′ u)
420 d₂″ = subst~ δ₂≤δ̇ β₂≤β₂″ refl d₂′