-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallelSubstitutionTyped.agda
213 lines (167 loc) · 6.89 KB
/
ParallelSubstitutionTyped.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
-- Parallel substitution for simply-typed lambda terms.
--
-- This is directly adapted from Andreas Abel's code for parallel
-- substitution for untyped de Bruijn terms:
--
-- "Instead of distinguishing renamings and substitutions as in
--
-- Chung-Kil Hur et al.,
-- Strongly Typed Term Representations in Coq
--
-- which leads to 4 different composition operations,
-- we define a generic substitution/renaming operation.
-- This is inspired by
--
-- Conor McBride
-- Type Preserving Renaming and Substitution
--
-- but, being not restricted to structural recursion, we can utilize
-- the lexicographically terminating definition of
--
-- Thorsten Altenkirch and Bernhard Reus
-- Monadic Presentations of Lambda Terms Using Generalized Inductive Types
--
-- This way, we have a single composition operation and lemma, which is mutual
-- with a lemma for composition of liftings."
--
module ParallelSubstitutionTyped where
open import Data.Nat
open import Relation.Binary.PropositionalEquality renaming (subst to coerce)
open ≡-Reasoning
open import TypedLambda renaming (suc to succ)
-- since we model substitutions as functions over natural numbers
-- (instead of lists), functional extensionality is very convenient to have!
postulate
funExt : forall {i j}{X : Set i}{Y : X -> Set j}
{f g : (x : X) -> Y x} ->
((x : X) -> f x ≡ g x) ->
f ≡ g
implFunExt :
forall {i j}{X : Set i}{Y : X -> Set j} ->
{f g : {x : X} -> Y x} ->
((x : X) -> f {x} ≡ g {x}) ->
(\{x} -> f {x}) ≡ g
-- A structurally ordered two-element set
data VarTrm : ℕ → Set where
Var : VarTrm 0 -- we are dealing with variables (natural numbers)
Trm : VarTrm 1 -- we are dealing with terms
max01 : ℕ → ℕ → ℕ
max01 0 m = m
max01 n m = n
compVT : ∀ {n m} (vt : VarTrm n) (vt' : VarTrm m) → VarTrm (max01 n m)
compVT Var vt' = vt'
compVT Trm vt = Trm
-- A set of variables or terms
VT : (Γ : Cx Ty) (τ : Ty) -> ∀ {n} → (vt : VarTrm n) → Set
VT Γ τ Var = τ <: Γ
VT Γ τ Trm = Γ !- τ
-- A mapping which is either a renaming (Var) or substitution (Trm)
RenSub : (Γ Δ : Cx Ty) → ∀ {n} → (vt : VarTrm n) → Set
RenSub Γ Δ vt = {τ : Ty} -> τ <: Γ → VT Δ τ vt
Ren : (Γ Δ : Cx Ty) → Set
Ren Γ Δ = {τ : Ty} → τ <: Γ → τ <: Δ -- Renamings
Sub : (Γ Δ : Cx Ty) → Set
Sub Γ Δ = {τ : Ty} → τ <: Γ → Δ !- τ -- Substitutions
mutual
lift : ∀ {Γ Δ σ m} {vt : VarTrm m} (θ : RenSub Γ Δ vt) → RenSub (Γ :: σ) (Δ :: σ) vt
-- lifting a renaming
lift {vt = Var} θ zero = zero
lift {vt = Var} θ (succ x) = succ (θ x)
-- lifting a substituion
lift {vt = Trm} θ zero = var zero
lift {vt = Trm} θ (succ x) = subst succ (θ x) -- shift
subst : ∀ {Γ Δ τ m} {vt : VarTrm m} (θ : RenSub Γ Δ vt) → Γ !- τ → Δ !- τ
subst {vt = vt} θ (lam{σ}{τ} t) = lam (subst (lift θ) t)
subst {vt = vt} θ (app t u) = app (subst θ t) (subst θ u)
-- PUT THESE LAST BECAUSE OF AGDA SPLIT HEURISTICS:
subst {vt = Var} θ (var x) = var (θ x) -- lookup in renaming
subst {vt = Trm} θ (var x) = θ x -- lookup in substitution
-- substitution composition
comp : ∀ {Γ Δ Φ}{n}{vt2 : VarTrm n}(θ : RenSub Δ Φ vt2)
{m}{vt1 : VarTrm m}(π : RenSub Γ Δ vt1) → RenSub Γ Φ (compVT vt1 vt2)
comp θ {vt1 = Var} π x = θ (π x)
comp θ {vt1 = Trm} π x = subst θ (π x)
-- Composition lemma
mutual
comp_lift :
∀ {Γ Δ Φ σ}
{n}{vt2 : VarTrm n}(θ : RenSub Δ Φ vt2)
{m}{vt1 : VarTrm m}(π : RenSub Γ Δ vt1) →
(λ {τ} → comp{Γ :: σ}{Δ :: σ}{Φ :: σ} (lift θ) (lift π) {τ}) ≡ (λ {τ} → lift (comp θ π))
comp_lift θ π = implFunExt (λ τ → funExt (λ x -> comp_lift' θ π x))
comp_lift' :
∀ {Γ Δ Φ σ}
{n}{vt2 : VarTrm n}(θ : RenSub Δ Φ vt2)
{m}{vt1 : VarTrm m}(π : RenSub Γ Δ vt1){τ : Ty}(x : τ <: Γ :: σ) →
comp (lift θ) (lift π) x ≡ lift (comp θ π) x
comp_lift' {vt2 = Var} θ {vt1 = Var} π zero = refl
comp_lift' {vt2 = Trm} θ {vt1 = Var} π zero = refl
comp_lift' {vt2 = Var} θ {vt1 = Trm} π zero = refl
comp_lift' {vt2 = Trm} θ {vt1 = Trm} π zero = refl
comp_lift' {vt2 = Var} θ {vt1 = Var} π (succ x') = refl
comp_lift' {vt2 = Trm} θ {vt1 = Var} π (succ x') = refl
comp_lift' {vt2 = Var} θ {vt1 = Trm} π (succ x') = begin
subst (lift θ) (subst succ (π x'))
≡⟨ comp_subst (lift θ) succ (π x') ⟩
subst (comp (lift θ) succ) (π x')
≡⟨ refl ⟩
subst (λ x → comp (lift θ) succ x) (π x')
≡⟨ refl ⟩
subst (λ x → succ (θ x)) (π x')
≡⟨ refl ⟩
subst (λ x → comp succ θ x) (π x')
≡⟨ refl ⟩
subst (comp succ θ) (π x')
≡⟨ sym (comp_subst succ θ (π x')) ⟩
subst succ (subst θ (π x'))
∎
comp_lift' {vt2 = Trm} θ {vt1 = Trm} π (succ x') = begin
subst (lift θ) (subst succ (π x'))
≡⟨ comp_subst (lift θ) succ (π x') ⟩
subst (comp (lift θ) succ) (π x')
≡⟨ refl ⟩
subst (λ x → comp (lift θ) succ x) (π x')
≡⟨ refl ⟩
subst (λ x → subst succ (θ x)) (π x') -- distinct line!
≡⟨ refl ⟩
subst (λ x → comp succ θ x) (π x')
≡⟨ refl ⟩
subst (comp succ θ) (π x')
≡⟨ sym (comp_subst succ θ (π x')) ⟩
subst succ (subst θ (π x'))
∎
comp_subst :
∀ {Γ Δ Φ τ}
{n}{vt2 : VarTrm n}(θ : RenSub Δ Φ vt2)
{m}{vt1 : VarTrm m}(π : RenSub Γ Δ vt1)(t : Γ !- τ) →
subst θ (subst π t) ≡ subst (comp θ π) t
comp_subst {vt2 = Var} θ {vt1 = Var} π (var x) = refl
comp_subst {vt2 = Var} θ {vt1 = Trm} π (var x) = refl
comp_subst {vt2 = Trm} θ {vt1 = Var} π (var x) = refl
comp_subst {vt2 = Trm} θ {vt1 = Trm} π (var x) = refl
comp_subst {vt2 = vt2} θ {vt1 = vt1} π (lam t) = begin
subst θ (subst π (lam t))
≡⟨ refl ⟩
subst θ (lam (subst (lift π) t))
≡⟨ refl ⟩
lam (subst (lift θ) (subst (lift π) t))
≡⟨ cong lam (comp_subst (lift θ) (lift π) t) ⟩
lam (subst (comp (lift θ) (lift π)) t)
≡⟨ cong (λ π' → lam (subst (λ{τ} → π'{τ}) t)) (comp_lift θ π) ⟩
lam (subst (lift (comp θ π)) t)
≡⟨ refl ⟩
subst (comp θ π) (lam t)
∎
comp_subst {vt2 = vt2} θ {vt1 = vt1} π (app t u) = begin
subst θ (subst π (app t u))
≡⟨ refl ⟩
app (subst θ (subst π t)) (subst θ (subst π u))
≡⟨ cong (λ t' → app t' (subst θ (subst π u)))
(comp_subst θ π t) ⟩
app (subst (comp θ π) t) (subst θ (subst π u))
≡⟨ cong (λ u' → app (subst (comp θ π) t) u')
(comp_subst θ π u) ⟩
app (subst (comp θ π) t) (subst (comp θ π) u)
≡⟨ refl ⟩
subst (comp θ π) (app t u)
∎