-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTLC.lagda
370 lines (284 loc) Β· 9.77 KB
/
STLC.lagda
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
\chapter{Source Language}
The source language closely follows PCF formulation from PLFA. The only difference is that rather than having distinct lambda abstraction and fixpoint operator,
the lambda abstraction makes a variable containing itself available to its body, thus enabling recursion and subsuming the role of the fixpoint operator.
This was done to facilitate closure conversion, but I would be interested in seeing how the fixpoint operator could be closure converted.
\section{Imports}
\begin{code}
module STLC where
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_β‘_; refl; congβ)
open import Data.Empty using (β₯; β₯-elim)
open import Data.Nat using (β; zero; suc)
open import Relation.Nullary using (Β¬_)
open import Data.List using ([] ; _β·_)
open import Function using (id)
open import Common
\end{code}
\section{Syntax}
\begin{code}
infix 4 _β’_
infix 5 Ζ_
infixl 7 _Β·_
infix 9 `_
infix 9 #_
\end{code}
\section{Terms and the typing judgment}
\begin{code}
data _β’_ : Context β Type β Set where
-- variables
`_ : β {Ξ A}
β Ξ β A
-----
β Ξ β’ A
-- functions
Ζ_ : β {Ξ A B}
β A β· Ξ β’ B
---------
β Ξ β’ A β B
_Β·_ : β {Ξ A B}
β Ξ β’ A β B
β Ξ β’ A
---------
β Ξ β’ B
\end{code}
\section {Abbreviating de Bruijn indices}
\begin{code}
look-up : Context β β β Type --
look-up (A β· Ξ) zero = A
look-up (_ β· Ξ) (suc n) = look-up Ξ n
look-up [] _ = β₯-elim impossible
where postulate impossible : β₯
count : β {Ξ} β (n : β) β Ξ β look-up Ξ n
count {_ β· Ξ} zero = Z
count {_ β· Ξ} (suc n) = S (count n)
count {[]} _ = β₯-elim impossible
where postulate impossible : β₯
#_ : β {Ξ} β (n : β) β Ξ β’ look-up Ξ n
# n = ` count n
\end{code}
\section{Semantics}
\begin{code}
Model : Setβ
Model = Context β Type β Set
infix 4 _βEnv
record _βEnv (Ξ : Context) (π₯ : Model) (Ξ : Context) : Set where
constructor pack
field lookup : β {Ο} β Ξ β Ο β π₯ Ξ Ο
open _βEnv public
Thinning : Context β Context β Set
Thinning Ξ Ξ = (Ξ βEnv) _β_ Ξ
Substitution'' : Context β Context β Set
Substitution'' Ξ Ξ = (Ξ βEnv) _β’_ Ξ
infixr 5 _<$>_
_<$>_ : β {Ξ Ξ Ξ π₯β π₯β}
β (β {Ο} β π₯β Ξ Ο β π₯β Ξ Ο) β (Ξ βEnv) π₯β Ξ β (Ξ βEnv) π₯β Ξ
lookup (f <$> Ο) x = f (lookup Ο x)
Ξ΅ : β {π₯ Ξ} β ([] βEnv) π₯ Ξ
lookup Ξ΅ ()
infixl 4 _β_
_β_ : β {Ξ Ξ Ο π₯} β (Ξ βEnv) π₯ Ξ β π₯ Ξ Ο β (Ο β· Ξ βEnv) π₯ Ξ
lookup (Ο β v) Z = v
lookup (Ο β v) (S x) = lookup Ο x
-- extend : β {Ξ Ο} β Thinning Ξ (Ο β· Ξ)
-- lookup extend x = S x
record Sem (π₯ π : Model) : Set where
field th^π₯ : β {Ξ Ξ Ο} β Thinning Ξ Ξ β π₯ Ξ Ο β π₯ Ξ Ο
β¦Vβ§ : β {Ξ Ο} β π₯ Ξ Ο β π Ξ Ο
β¦Aβ§ : β {Ξ Ο Ο} β π Ξ (Ο β Ο) β π Ξ Ο β π Ξ Ο
β¦Lβ§ : β {Ξ} β (Ο : Type) β {Ο : Type} β (Thinning Ξ (Ο β· Ξ) β π₯ (Ο β· Ξ) Ο β π (Ο β· Ξ) Ο) β π Ξ (Ο β Ο) -- can we and should we generalise Ο β· Ξ to Ξ ?
sem : β {Ξ Ξ Ο} β (Ξ βEnv) π₯ Ξ β Ξ β’ Ο β π Ξ Ο
sem Ο (` x) = β¦Vβ§ (lookup Ο x)
sem Ο (L Β· M) = β¦Aβ§ (sem Ο L) (sem Ο M)
sem Ο (Ζ_ N) = β¦Lβ§ _ (Ξ» Ξ³ v β sem (extend' Ο Ξ³ v) N)
where
extend' : β {Ξ Ξ Ξ Ο} β (Ξ βEnv) π₯ Ξ β Thinning Ξ Ξ β π₯ Ξ Ο β (Ο β· Ξ βEnv) π₯ Ξ
extend' Ο Ξ³ v = th^π₯ Ξ³ <$> Ο β v
Renaming' : Sem _β_ _β’_
Renaming' = record
{ th^π₯ = Ξ» Ο v β lookup Ο v
; β¦Vβ§ = `_
; β¦Aβ§ = _Β·_
; β¦Lβ§ = Ξ» _ b β Ζ b (pack S_) Z }
ren : β {Ξ Ξ Ο} β Thinning Ξ Ξ β Ξ β’ Ο β Ξ β’ Ο
ren = Sem.sem Renaming'
Substitution' : Sem _β’_ _β’_
Substitution' = record
{ th^π₯ = Ξ» Ο v β Sem.sem Renaming' Ο v
; β¦Vβ§ = id
; β¦Aβ§ = _Β·_
; β¦Lβ§ = Ξ» _ b β Ζ (b (pack S_) (` Z)) }
sub : β {Ξ Ξ Ο} β (Ξ βEnv) _β’_ Ξ β Ξ β’ Ο β Ξ β’ Ο
sub = Sem.sem Substitution'
Kripke : Model β Model β Context β Type β Type β Set
Kripke π₯ π Ξ Ο Ο = Thinning Ξ (Ο β· Ξ) β π₯ (Ο β· Ξ) Ο β π (Ο β· Ξ) Ο
Applicative : Model β Set
Applicative π = {Ξ : Context} {Ο Ο : Type} β π Ξ (Ο β Ο) β π Ξ Ο β π Ξ Ο
\end{code}
Now suppose that we could reduce under abstractions.
Then we'd need a proof of SN for all vars in the env.
\section{Renaming}
\begin{code}
ext : β {Ξ Ξ A}
β Renaming Ξ Ξ
-----------------------------------
β Renaming (A β· Ξ) (A β· Ξ)
ext Ο Z = Z
ext Ο (S x) = S (Ο x)
rename : β {Ξ Ξ}
β Renaming Ξ Ξ
---------------------------
β Rebasing _β’_ Ξ Ξ
rename Ο (` x) = ` (Ο x)
rename Ο (Ζ N) = Ζ rename (ext Ο) N
rename Ο (L Β· M) = (rename Ο L) Β· (rename Ο M)
\end{code}
\section{Simultaneous substitution}
\begin{code}
exts : β {Ξ Ξ A}
β Substitution _β’_ Ξ Ξ
----------------------------
β Substitution _β’_ (A β· Ξ) (A β· Ξ)
exts Ο Z = ` Z
exts Ο (S x) = rename S_ (Ο x)
subst : β {Ξ Ξ}
β Substitution _β’_ Ξ Ξ
----------------
β Rebasing _β’_ Ξ Ξ
subst Ο (` k) = Ο k
subst Ο (Ζ N) = Ζ (subst (exts Ο) N)
subst Ο (L Β· M) = (subst Ο L) Β· (subst Ο M)
\end{code}
\section{Single substitution}
\begin{code}
_[_] : β {Ξ A B}
β A β· Ξ β’ B
β Ξ β’ A
------------
β Ξ β’ B
_[_] {Ξ} {A} N V = subst {A β· Ξ} {Ξ} Ο N
where
Ο : β {B} β A β· Ξ β B β Ξ β’ B
Ο Z = V
Ο (S x) = ` x
\end{code}
\section{Values}
\begin{code}
data Value : β {Ξ A} β Ξ β’ A β Set where
-- functions
V-Ζ : β {Ξ A B} {N : A β· Ξ β’ B}
---------------------------
β Value (Ζ N)
\end{code}
\section{Reduction}
\begin{code}
infix 2 _ββ_
data _ββ_ : β {Ξ A} β (Ξ β’ A) β (Ξ β’ A) β Set where
-- functions
ΞΎ-Β·β : β {Ξ A B} {L Lβ² : Ξ β’ A β B} {M : Ξ β’ A}
β L ββ Lβ²
---------------
β L Β· M ββ Lβ² Β· M
ΞΎ-Β·β : β {Ξ A B} {V : Ξ β’ A β B} {M Mβ² : Ξ β’ A}
β Value V
β M ββ Mβ²
---------------
β V Β· M ββ V Β· Mβ²
Ξ²-Ζ : β {Ξ A B} {N : A β· Ξ β’ B} {V : Ξ β’ A} -- TODO
β Value V
--------------------
β (Ζ N) Β· V ββ N [ V ]
VΒ¬ββ : β {Ξ Ο} {M N : Ξ β’ Ο}
β Value M
-------------
β Β¬ (M ββ N)
VΒ¬ββ () (ΞΎ-Β·β MββN)
VΒ¬ββ () (ΞΎ-Β·β x MββN)
VΒ¬ββ () (Ξ²-Ζ V)
det : β {Ξ Ο} {e eβ eβ : Ξ β’ Ο}
β e ββ eβ
β e ββ eβ
---------
β eβ β‘ eβ
det (ΞΎ-Β·β eββeβ) (ΞΎ-Β·β eββeβ) = congβ _Β·_ (det eββeβ eββeβ) refl
det (ΞΎ-Β·β eββeβ) (ΞΎ-Β·β V-L eββeβ) = β₯-elim (VΒ¬ββ V-L eββeβ)
det (ΞΎ-Β·β eββeβ) (Ξ²-Ζ _) = β₯-elim (VΒ¬ββ V-Ζ eββeβ)
det (ΞΎ-Β·β V-L eββeβ) (ΞΎ-Β·β eββeβ) = β₯-elim (VΒ¬ββ V-L eββeβ)
det (ΞΎ-Β·β _ eββeβ) (ΞΎ-Β·β _ eββeβ) = congβ _Β·_ refl (det eββeβ eββeβ)
det (ΞΎ-Β·β _ eββeβ) (Ξ²-Ζ V) = β₯-elim (VΒ¬ββ V eββeβ)
det (Ξ²-Ζ _) (ΞΎ-Β·β eββeβ) = β₯-elim (VΒ¬ββ V-Ζ eββeβ)
det (Ξ²-Ζ V) (ΞΎ-Β·β _ eββeβ) = β₯-elim (VΒ¬ββ V eββeβ)
det (Ξ²-Ζ V) (Ξ²-Ζ V') = refl
\end{code}
\section{Reflexive and transitive closure}
\begin{code}
infix 2 _ββ _
infix 1 begin_
infixr 2 _βββ¨_β©_
infix 3 _β
data _ββ _ : β {Ξ A} β (Ξ β’ A) β (Ξ β’ A) β Set where
_β : β {Ξ A} (M : Ξ β’ A)
--------
β M ββ M
_βββ¨_β©_ : β {Ξ A} (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
\end{code}
\section{Progress}
\begin{code}
data Progress {A} (M : [] β’ A) : Set where
step : β {N : [] β’ A}
β M ββ N
----------
β Progress M
done :
Value M
----------
β Progress M
progress : β {A}
β (M : [] β’ A)
-----------
β Progress M
progress (` ())
progress (Ζ N) = done V-Ζ
progress (L Β· M) with progress L
... | step LββLβ² = step (ΞΎ-Β·β LββLβ²)
... | done V-Ζ with progress M
... | step MββMβ² = step (ΞΎ-Β·β V-Ζ MββMβ²)
... | done VM = step (Ξ²-Ζ VM)
\end{code}
\section{Evaluation}
\begin{code}
data Gas : Set where
gas : β β Gas
data Finished {Ξ A} (N : Ξ β’ A) : Set where
done :
Value N
----------
β Finished N
out-of-gas :
----------
Finished N
data Steps : β {A} β [] β’ A β Set where
steps : β {A} {L N : [] β’ A}
β L ββ N
β Finished N
----------
β Steps L
eval : β {A}
β Gas
β (L : [] β’ A)
-----------
β Steps L
eval (gas zero) L = steps (L β) out-of-gas
eval (gas (suc m)) L with progress L
... | done VL = steps (L β) (done VL)
... | step {M} LββM with eval (gas m) M
... | steps Mββ N fin = steps (L βββ¨ LββM β© Mββ N) fin
\end{code}