-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfluenceTakahashi.agda
196 lines (154 loc) · 8.03 KB
/
ConfluenceTakahashi.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
open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym; cong; cong₂)
open import Data.Nat using (ℕ; zero; suc; _≤_; z≤n; s≤s)
open import Data.Nat.Properties using (≤-total)
open import Data.Fin using (Fin; zero; suc)
open import Data.Product using (∃; ∃-syntax; _×_; _,_)
open import Data.Sum using ([_,_])
open import DeBruijn
open import Substitution using (rename-subst-commute; subst-commute; extensionality)
open import Beta
open import BetaSubstitutivity using (sub-betas)
open import Takahashi
infix 8 _*⁽_⁾
_*⁽_⁾ : ∀ {n} → Term n → ℕ → Term n
M *⁽ zero ⁾ = M
M *⁽ suc k ⁾ = (M *) *⁽ k ⁾
data _—↠⁽_⁾_ : ∀ {n} → Term n → ℕ → Term n → Set where
_∎ : ∀ {n} (M : Term n)
--------------
→ M —↠⁽ zero ⁾ M
_—→⁽⁾⟨_⟩_ : ∀ {n} {N L : Term n} {k : ℕ} (M : Term n)
→ M —→ L
→ L —↠⁽ k ⁾ N
---------------
→ M —↠⁽ suc k ⁾ N
lemma3-2 : ∀ {n} {M : Term n}
→ M —↠ M *
lemma3-2 {M = # x} = # x ∎
lemma3-2 {M = ƛ _} = —↠-cong-ƛ lemma3-2
lemma3-2 {M = # _ · _} = —↠-congᵣ lemma3-2
lemma3-2 {M = _ · _ · _} = —↠-cong lemma3-2 lemma3-2
lemma3-2 {M = (ƛ M) · N} = (ƛ M) · N —→⟨ —→-β ⟩ sub-betas {M = M} lemma3-2 lemma3-2
lemma3-3 : ∀ {n} {M N : Term n}
→ M —→ N
--------
→ N —↠ M *
lemma3-3 {M = # _} ()
lemma3-3 {M = ƛ M} (—→-ƛ M—→M′) = —↠-cong-ƛ (lemma3-3 M—→M′)
lemma3-3 {M = # _ · N} (—→-ξᵣ N—→N′) = —↠-congᵣ (lemma3-3 N—→N′)
lemma3-3 {M = _ · _ · N} (—→-ξᵣ N—→N′) = —↠-cong lemma3-2 (lemma3-3 N—→N′)
lemma3-3 {M = M₁ · M₂ · _} (—→-ξₗ M—↠M′) = —↠-cong (lemma3-3 M—↠M′) lemma3-2
lemma3-3 {M = (ƛ M) · N} —→-β = sub-betas {M = M} lemma3-2 lemma3-2
lemma3-3 {M = (ƛ M) · N} (—→-ξᵣ {N′ = N′} N—→N′) = (ƛ M ) · N′ —→⟨ —→-β ⟩ sub-betas {M = M} lemma3-2 (lemma3-3 N—→N′)
lemma3-3 {M = (ƛ M) · N} (—→-ξₗ (—→-ƛ {M′ = M′} M—→M′)) = (ƛ M′) · N —→⟨ —→-β ⟩ sub-betas {N = N} (lemma3-3 M—→M′) lemma3-2
_*ˢ : ∀ {n m} → Subst n m → Subst n m
σ *ˢ = λ x → (σ x) *
rename-* : ∀ {n m} (ρ : Rename n m) (M : Term n)
→ rename ρ (M *) ≡ (rename ρ M) *
rename-* ρ (# _) = refl
rename-* ρ (ƛ M) = cong ƛ_ (rename-* (ext ρ) M)
rename-* ρ (# _ · N) = cong₂ _·_ refl (rename-* ρ N)
rename-* ρ (M₁ · M₂ · N) = cong₂ _·_ (rename-* ρ (M₁ · M₂)) (rename-* ρ N)
rename-* ρ ((ƛ M) · N) rewrite sym (rename-subst-commute {N = M *}{M = N *}{ρ = ρ})
| rename-* (ext ρ) M
| rename-* ρ N
= refl
exts-ts-commute : ∀ {n m} (σ : Subst n m)
→ exts (σ *ˢ) ≡ (exts σ) *ˢ
exts-ts-commute {n} σ = extensionality exts-ts-commute′
where
exts-ts-commute′ : (x : Fin (suc n))
→ (exts (σ *ˢ)) x ≡ ((exts σ) *ˢ) x
exts-ts-commute′ zero = refl
exts-ts-commute′ (suc x) = rename-* suc (σ x)
app-*-join : ∀ {n} (M N : Term n)
→ M * · N * —↠ (M · N) *
app-*-join (# x) N = # x · N * ∎
app-*-join (ƛ M) N = (ƛ M *) · N * —→⟨ —→-β ⟩ subst (subst-zero (N *)) (M *) ∎
app-*-join (M₁ · M₂) N = (M₁ · M₂) * · N * ∎
subst-ts : ∀ {n m} (σ : Subst n m) (M : Term n)
→ subst (σ *ˢ) (M *) —↠ (subst σ M) *
subst-ts σ (# x) = σ x * ∎
subst-ts σ (ƛ M) rewrite exts-ts-commute σ = —↠-cong-ƛ (subst-ts (exts σ) M)
subst-ts σ (# x · N) = —↠-trans (—↠-congᵣ (subst-ts σ N))
(app-*-join (σ x) (subst σ N))
subst-ts σ (M₁ · M₂ · N) = —↠-cong (subst-ts σ (M₁ · M₂)) (subst-ts σ N)
subst-ts σ ((ƛ M) · N) rewrite sym (subst-commute {N = M *}{M = N *}{σ = σ *ˢ})
| exts-ts-commute σ
= sub-betas (subst-ts (exts σ) M) (subst-ts σ N)
subst-zero-ts : ∀ {n} {N : Term n}
→ subst-zero (N *) ≡ (subst-zero N) *ˢ
subst-zero-ts = extensionality (λ { zero → refl ; (suc x) → refl })
lemma3-4 : ∀ {n} (M : Term (suc n)) (N : Term n)
→ M * [ N * ] —↠ (M [ N ]) *
lemma3-4 M N rewrite subst-zero-ts {N = N} = subst-ts (subst-zero N) M
lemma3-5 : ∀ {n} {M N : Term n}
→ M —→ N
----------
→ M * —↠ N *
lemma3-5 {M = # x} ()
lemma3-5 {M = ƛ M} (—→-ƛ M—→M′) = —↠-cong-ƛ (lemma3-5 M—→M′)
lemma3-5 {M = # _ · N} (—→-ξᵣ M₂—→M′) = —↠-congᵣ (lemma3-5 M₂—→M′)
lemma3-5 {M = (ƛ M) · N} —→-β = lemma3-4 M N
lemma3-5 {M = (ƛ M) · N} (—→-ξₗ (—→-ƛ M—→M′)) = sub-betas (lemma3-5 M—→M′) (N * ∎)
lemma3-5 {M = (ƛ M) · N} (—→-ξᵣ N—→N′) = sub-betas (M * ∎) (lemma3-5 N—→N′)
lemma3-5 {M = M₁ · M₂ · _} (—→-ξᵣ N—→N′) = —↠-congᵣ (lemma3-5 N—→N′)
lemma3-5 {M = M₁ · M₂ · _} (—→-ξₗ {M′ = # x} M₁M₂—→#x) = —↠-congₗ (lemma3-5 M₁M₂—→#x)
lemma3-5 {M = M₁ · M₂ · _} (—→-ξₗ {M′ = M′₁ · M′₂} M₁M₂—→M′₁M′₂) = —↠-congₗ (lemma3-5 M₁M₂—→M′₁M′₂)
lemma3-5 {M = M₁ · M₂ · N} (—→-ξₗ {M′ = ƛ M′} M₁M₂—→ƛM′) =
—↠-trans (—↠-congₗ (lemma3-5 M₁M₂—→ƛM′))
((ƛ M′ *) · N * —→⟨ —→-β ⟩ subst (subst-zero (N *)) (M′ *) ∎)
corollary3-6 : ∀ {n} {M N : Term n}
→ M —↠ N
----------
→ M * —↠ N *
corollary3-6 (M ∎) = M * ∎
corollary3-6 (M —→⟨ M—→L ⟩ L—↠N) = —↠-trans (lemma3-5 M—→L) (corollary3-6 L—↠N)
corollary3-7 : ∀ {n} {M N : Term n} (m : ℕ)
→ M —↠ N
--------------------
→ M *⁽ m ⁾ —↠ N *⁽ m ⁾
corollary3-7 zero M—↠N = M—↠N
corollary3-7 (suc m) M—↠N = corollary3-7 m (corollary3-6 M—↠N)
theorem3-8 : ∀ {n} {M N : Term n} {m : ℕ}
→ M —↠⁽ m ⁾ N
-------------
→ N —↠ M *⁽ m ⁾
theorem3-8 {m = zero} (M ∎) = M ∎
theorem3-8 {m = suc m} (M —→⁽⁾⟨ M—→L ⟩ L—↠ᵐN) =
—↠-trans (theorem3-8 L—↠ᵐN) (corollary3-7 m (lemma3-3 M—→L))
unnamed-named : ∀ {n} {M N : Term n}
→ M —↠ N
--------------------
→ ∃[ m ] (M —↠⁽ m ⁾ N)
unnamed-named (M ∎) = zero , (M ∎)
unnamed-named (M —→⟨ M—→L ⟩ L—↠N) with unnamed-named L—↠N
... | m′ , L—↠ᵐ′N = suc m′ , (M —→⁽⁾⟨ M—→L ⟩ L—↠ᵐ′N)
lift-* : ∀ {n} (M : Term n) (m : ℕ)
→ M —↠ M *⁽ m ⁾
lift-* M zero = M ∎
lift-* M (suc m) = —↠-trans lemma3-2 (lift-* (M *) m)
complete-* : ∀ {k} (M : Term k) {n m : ℕ}
→ n ≤ m
--------------------
→ M *⁽ n ⁾ —↠ M *⁽ m ⁾
complete-* M {m = m} z≤n = lift-* M m
complete-* M (s≤s k) = complete-* (M *) k
theorem3-9 : ∀ {n} {M A B : Term n}
→ M —↠ A → M —↠ B
------------------------
→ ∃[ N ] (A —↠ N × B —↠ N)
theorem3-9 {M = M} M—↠A M—↠B =
let n , M—↠ⁿA = unnamed-named M—↠A
m , M—↠ᵐB = unnamed-named M—↠B
A—↠M*ⁿ = theorem3-8 M—↠ⁿA
B—↠M*ᵐ = theorem3-8 M—↠ᵐB
in [ (λ n≤m →
let M*ⁿ—↠M*ᵐ : M *⁽ n ⁾ —↠ M *⁽ m ⁾
M*ⁿ—↠M*ᵐ = complete-* M n≤m
in M *⁽ m ⁾ , —↠-trans A—↠M*ⁿ M*ⁿ—↠M*ᵐ , B—↠M*ᵐ)
, (λ m≤n →
let M*ᵐ—↠M*ⁿ : M *⁽ m ⁾ —↠ M *⁽ n ⁾
M*ᵐ—↠M*ⁿ = complete-* M m≤n
in M *⁽ n ⁾ , A—↠M*ⁿ , —↠-trans B—↠M*ᵐ M*ᵐ—↠M*ⁿ)
] (≤-total n m)