-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lambda.agda
222 lines (176 loc) · 6.15 KB
/
Lambda.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
214
215
216
217
218
219
220
221
222
module Lambda where
open import Prelude renaming (subst to ≡-subst)
open import Data.List hiding (_!_)
open import Data.Nat.Order
open import Data.Nat.Properties
data `Type : Type where
_`→_ : `Type → `Type → `Type
`ℕ : `Type
infixr 7 _`→_
Context : Type
Context = List `Type
private
variable
Γ Δ : Context
`A `B `C : `Type
Fin : ℕ → Type
Fin n = ∃ m × (m < n)
index : (xs : List A) → (n : ℕ) → n < length xs → A
index (x ∷ _ ) zero _ = x
index (_ ∷ xs) (suc n) p = index xs n p
infixr 5 _∈_
record _∈_ (`A : `Type) (Γ : Context) : Type where
constructor member
field
position : ℕ
bounded : position < length Γ
present : index Γ position bounded ≡ `A
open _∈_
infix 4 _⊢_
infix 5 ƛ_
infix 5 μ_
infixl 7 _·_
infix 8 `suc_
infix 9 `_
infix 9 #_
data _⊢_ (Γ : Context) : `Type → Type where
`_ : `A ∈ Γ
-------
→ Γ ⊢ `A
ƛ_ : `A ∷ Γ ⊢ `B
---------------
→ Γ ⊢ `A `→ `B
_·_ : Γ ⊢ `A `→ `B
→ Γ ⊢ `A
--------------
→ Γ ⊢ `B
`zero :
---------
Γ ⊢ `ℕ
`suc_ : Γ ⊢ `ℕ
---------
→ Γ ⊢ `ℕ
case : Γ ⊢ `ℕ
→ Γ ⊢ `A
→ `ℕ ∷ Γ ⊢ `A
------------
→ Γ ⊢ `A
μ_ : `A ∷ Γ ⊢ `A
--------------
→ Γ ⊢ `A
#_ : ∀ n → {p : n < length Γ} → Γ ⊢ index Γ n p
# n = ` member n _ refl
plus : Γ ⊢ `ℕ `→ `ℕ `→ `ℕ
plus = μ ƛ ƛ (case (# 1) (# 0) (`suc (# 3 · # 0 · # 1)))
two : Γ ⊢ `ℕ
two = `suc `suc `zero
2+2 : Γ ⊢ `ℕ
2+2 = plus · two · two
mul : Γ ⊢ `ℕ `→ `ℕ `→ `ℕ
mul = μ ƛ ƛ case (# 0) `zero (plus · # 1 · (# 3 · # 0 · # 1))
S : `A ∈ Γ → `A ∈ `B ∷ Γ
S (member i p m) = member (suc i) p m
ext : (∀ {A} → A ∈ Γ → A ∈ Δ) →
(∀ {A B} → A ∈ B ∷ Γ → A ∈ B ∷ Δ)
ext ρ (member zero p m) = member zero tt m
ext ρ (member (suc i) p m) = S (ρ (member i p m))
rename : (∀ {A} → A ∈ Γ → A ∈ Δ)
→ (∀ {A} → Γ ⊢ A → Δ ⊢ A)
rename ρ (` x) = ` ρ x
rename ρ (ƛ x) = ƛ rename (ext ρ) x
rename ρ (f · x) = rename ρ f · rename ρ x
rename ρ `zero = `zero
rename ρ (`suc x) = `suc rename ρ x
rename ρ (case x l r) = case (rename ρ x) (rename ρ l) (rename (ext ρ) r)
rename ρ (μ x) = μ rename (ext ρ) x
exts : (∀ {A} → A ∈ Γ → Δ ⊢ A)
→ (∀ {A B} → A ∈ B ∷ Γ → B ∷ Δ ⊢ A)
exts σ (member zero b p) = ` member zero tt p
exts σ (member (suc i) b p) = rename S (σ (member i b p))
subst : (∀ {A} → A ∈ Γ → Δ ⊢ A)
→ (∀ {A} → Γ ⊢ A → Δ ⊢ A)
subst σ (` x) = σ x
subst σ (ƛ x) = ƛ subst (exts σ) x
subst σ (f · x) = subst σ f · subst σ x
subst σ `zero = `zero
subst σ (`suc x) = `suc subst σ x
subst σ (case x l r) = case (subst σ x) (subst σ l) (subst (exts σ) r)
subst σ (μ x) = μ subst (exts σ) x
_[_] : `B ∷ Γ ⊢ `A
→ Γ ⊢ `B
--------
→ Γ ⊢ `A
N [ M ] = subst (λ { (member zero b m) → ≡-subst (_ ⊢_) m M ; (member (suc i) b m) → ` member i b m }) N
data Value : Γ ⊢ `A → Type where
V-ƛ : {N : `A ∷ Γ ⊢ `B} → Value (ƛ N)
V-zero : Value (`zero {Γ})
V-suc : ∀ {V : Γ ⊢ `ℕ} → Value V → Value (`suc V)
variable
M N W L V : Γ ⊢ `A
infix 2 _⟶_
data _⟶_ : Γ ⊢ `A → Γ ⊢ `A → Type where
ξ-·₁ : L ⟶ M
→ L · N ⟶ M · N
ξ-·₂ : Value V
→ M ⟶ N
------------
→ V · M ⟶ V · N
β-ƛ : Value W
→ (ƛ N) · W ⟶ N [ W ]
ξ-suc : M ⟶ N
→ `suc M ⟶ `suc N
ξ-case : L ⟶ M
→ case L V W ⟶ case M V W
β-zero : case `zero M N ⟶ M
β-suc : Value V
→ case (`suc V) M N ⟶ N [ V ]
β-μ : μ N ⟶ N [ μ N ]
infix 2 _—↠_
infix 1 begin_
infixr 2 _—→⟨_⟩_
infix 3 _∎
data _—↠_ {Γ A} : (Γ ⊢ A) → (Γ ⊢ A) → Type where
_∎ : (M : Γ ⊢ A)
------
→ M —↠ M
_—→⟨_⟩_ : (L : Γ ⊢ A) {M N : Γ ⊢ A}
→ L ⟶ M
→ M —↠ N
------
→ L —↠ N
begin_ : ∀ {Γ A} {M N : Γ ⊢ A}
→ M —↠ N
------
→ M —↠ N
begin M—↠N = M—↠N
-- _ : plus {[]} · two · two —↠ `suc `suc `suc `suc `zero
-- _ =
-- plus · two · two
-- —→⟨ ξ-·₁ (ξ-·₁ β-μ) ⟩
-- (ƛ (ƛ case (# 1) (# 0) (`suc {!plus · {!!} · {!!}!}) )) · two · two
-- —→⟨ {!!} ⟩
-- -- —→⟨ ξ-·₁ (β-ƛ (V-suc (V-suc V-zero))) ⟩
-- -- (ƛ case two (` member 0 tt refl) (`suc (plus · ` member 0 tt refl · ` member 1 tt refl))) · two
-- -- —→⟨ β-ƛ (V-suc (V-suc V-zero)) ⟩
-- -- case two two (`suc (plus · ` member 0 tt refl · two))
-- -- —→⟨ β-suc (V-suc V-zero) ⟩
-- -- `suc (plus · `suc `zero · two)
-- -- —→⟨ ξ-suc (ξ-·₁ (ξ-·₁ β-μ)) ⟩
-- -- `suc ((ƛ ƛ case (` member 1 tt refl) (` member 0 tt refl) (`suc (plus · ` member 0 tt refl · ` member 1 tt refl)))
-- -- · `suc `zero · two)
-- -- —→⟨ ξ-suc (ξ-·₁ (β-ƛ (V-suc V-zero))) ⟩
-- -- `suc ((ƛ case (`suc `zero) (` member 0 tt refl) (`suc (plus · ` member 0 tt refl · ` member 1 tt refl))) · two)
-- -- —→⟨ ξ-suc (β-ƛ (V-suc (V-suc V-zero))) ⟩
-- -- `suc (case (`suc `zero) (two) (`suc (plus · ` member 0 tt refl · two)))
-- -- —→⟨ ξ-suc (β-suc V-zero) ⟩
-- -- `suc (`suc (plus · `zero · two))
-- -- —→⟨ ξ-suc (ξ-suc (ξ-·₁ (ξ-·₁ β-μ))) ⟩
-- -- `suc (`suc ((ƛ ƛ case (` member 1 tt refl) (` member 0 tt refl) (`suc (plus · ` member 0 tt refl · ` member 1 tt refl)))
-- -- · `zero · two))
-- -- —→⟨ ξ-suc (ξ-suc (ξ-·₁ (β-ƛ V-zero))) ⟩
-- -- `suc (`suc ((ƛ case `zero (` member 0 tt refl) (`suc (plus · ` member 0 tt refl · ` member 1 tt refl))) · two))
-- -- —→⟨ ξ-suc (ξ-suc (β-ƛ (V-suc (V-suc V-zero)))) ⟩
-- -- `suc (`suc (case `zero (two) (`suc (plus · ` member 0 tt refl · two))))
-- -- —→⟨ ξ-suc (ξ-suc β-zero) ⟩
-- `suc (`suc (`suc (`suc `zero)))
-- ∎