-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-iu.ott
More file actions
496 lines (358 loc) · 11.9 KB
/
lambda-iu.ott
File metadata and controls
496 lines (358 loc) · 11.9 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
embed
{{ tex-preamble
\usepackage{bbold}
\DeclareSymbolFont{bbsymbol}{U}{bbold}{m}{n}
\DeclareMathSymbol{\bbcomma}{\mathbin}{bbsymbol}{"2C}
}}
embed
{{ coq
Set Implicit Arguments.
Definition one (C : Type) (item : C) : list C := cons item nil.
Notation "x ~ a" := (one (x, a)) (at level 50) : list_scope.
Definition binds
(A : Type) (x : nat) (a : A) (E : list (nat*A)) : Prop :=
In (x, a) E.
Unset Implicit Arguments.
}}
metavar var, x, y, z, args, l {{ tex \ell }} ::= {{ coq nat }} {{ com variable/label }}
grammar
%%%%% target calculus %%%%%
typ, A, B, C :: 't_' ::= {{ com types }}
| Top :: :: top {{ com top type }} {{ tex \top }}
| Bot :: :: bot {{ com bottom type }} {{ tex \bot }}
| Null :: :: null {{ com null type }}
| Int :: :: int {{ com integer type }} {{ tex \mathbb{Z} }}
| A -> B :: :: arrow {{ com function type }}
| { l : A } :: :: rcd {{ com record type }}
| A & B :: :: and {{ com intersection type }}
| A | B :: :: or {{ com union type }}
| ( A ) :: S:: paren {{ coq ([[A]]) }}
exp, e :: 'e_' ::= {{ com expressions }}
| {} :: :: top {{ com top value }}
| null :: :: null {{ com null value }}
| int :: :: int {{ com integer literal }} {{ tex n }}
| x :: :: var {{ com variable }}
| \ x : A . e : B :: :: abs {{ com abstraction }} {{ tex [[\]][[x]]\!:\![[A]].\;[[e]]\!:\![[B]] }}
| e1 e2 :: :: app {{ com application }}
| { l : A = e } :: :: rcd {{ com record }}
| e . l :: :: prj {{ com projection }}
| e1 ,, e2 :: :: merge {{ com merging }}
| switch e0 as x case A => e1 case B => e2
:: :: switch {{ com type switch }}
| letin e :: :: letin
| ( e ) :: S:: paren {{ coq ([[e]]) }}
| 0 :: S:: zero {{ coq }}
| 1 :: S:: one {{ coq }}
| 2 :: S:: two {{ coq }}
| e1 + e2 :: S:: plus {{ coq }}
val, v :: 'v_' ::= {{ com values }}
| {} :: :: top
| null :: :: null
| int :: :: int
| \ x : A . e : B :: :: abs
| { l : A = v } :: :: rcd
| v1 ,, v2 :: :: merge
letin :: 'letin_' ::= {{ com let-in bindings }}
| id :: :: identity
| letin letin' :: :: composition {{ tex [[letin]]\circ[[letin']] }}
| let x = e in :: :: bind
ctx, G {{ tex \Gamma }} :: 'ctx_' ::= {{ com typing context }} {{ coq list (nat * typ) }}
| [] :: :: nil {{ tex \cdot }} {{ coq nil }}
| G , x : A :: :: cons {{ coq (([[x]],[[A]])::[[G]]) }}
%%%%% source calculus %%%%%
styp, As {{ tex \mathcal{A} }}, Bs {{ tex \mathcal{B} }} :: 'st_' ::= {{ com source types }}
| Int :: :: int {{ com integer type }} {{ tex \mathbb{Z} }}
| As -> Bs :: :: arrow {{ com function type }} {{ tex ([[As]])[[->]][[Bs]] }}
| { P } -> Bs :: :: narrow {{ com function type with named parameters }}
| { T } :: :: narg {{ com named argument type }}
| ( As ) :: S:: paren {{ coq ([[As]]) }}
nptyp, P {{ tex \mathcal{P} }} :: 'pt_' ::= {{ com named parameter types }}
| [] :: :: empty {{ com empty }} {{ tex \cdot }}
| P ; l : As :: :: required {{ com required parameter }}
| P ; l ? : As :: :: optional {{ com optional parameter }}
| ( P ) :: S:: paren {{ coq ([[P]]) }}
natyp, T {{ tex \mathcal{K} }} :: 'at_' ::= {{ com named argument types }}
| [] :: :: empty {{ com empty }} {{ tex \cdot }}
| T ; l : As :: :: field {{ com field }}
| ( T ) :: S:: paren {{ coq ([[T]]) }}
sexp, es {{ tex \epsilon }} :: 'se_' ::= {{ com source expressions }}
| int :: :: int {{ com integer literal }} {{ tex n }}
| x :: :: var {{ com variable }}
| \ ( x : As ) . es :: :: abs {{ com abstraction }}
{{ tex [[\]]([[x]]\!:\![[As]]).\;[[es]] }}
| \ { p } . es :: :: nabs {{ com abstraction with named parameters }}
{{ tex [[\]]\{[[p]]\}.\;[[es]] }}
| es1 es2 :: :: app {{ com application }}
| { a } :: :: narg {{ com named arguments }}
| ( es ) :: S:: paren {{ coq ([[es]]) }}
| 0 :: S:: zero {{ coq }}
| 1 :: S:: one {{ coq }}
| 2 :: S:: two {{ coq }}
| es1 + es2 :: S:: plus {{ coq }}
npexp, p {{ tex \rho }} :: 'par_' ::= {{ com named parameters }}
| [] :: :: empty {{ com empty }} {{ tex \cdot }}
| p ; l : As :: :: required {{ com required parameter }}
| p ; l = es :: :: optional {{ com optional parameter }}
| ( p ) :: S:: paren {{ coq ([[p]]) }}
naexp, a {{ tex \kappa }} :: 'arg_' ::= {{ com named arguments }}
| [] :: :: empty {{ com empty }} {{ tex \cdot }}
| a ; l = es :: :: field {{ com field }}
| ( a ) :: S:: paren {{ coq ([[a]]) }}
sctx, Gs {{ tex \Delta }} :: 'sctx_' ::= {{ com typing context }} {{ coq list (nat * styp) }}
| [] :: :: nil {{ tex \cdot }} {{ coq nil }}
| Gs , x : As :: :: cons {{ coq (([[x]],[[As]])::[[Gs]]) }}
terminals :: 'terminals_' ::=
| \ :: :: lambda {{ tex \lambda }}
| -> :: :: arrow {{ tex \rightarrow }}
| --> :: :: larrow {{ tex \longrightarrow }}
| ~~> :: :: sarrow {{ tex \,\rightsquigarrow\, }}
| => :: :: farrow {{ tex \Rightarrow }}
| =/> :: :: nfarrow {{ tex \nRightarrow }}
| |- :: :: turnstile {{ tex \,\vdash\, }}
| -| :: :: rturnstile {{ tex \,\dashv\, }}
| ~ :: :: consistent {{ tex \approx }}
| & :: :: and {{ tex \land }}
| | :: :: or {{ tex \lor }}
| || :: :: pipe {{ tex | }}
| <> :: :: diamond {{ tex \,\diamond\, }}
| /= :: :: neq {{ tex \neq }}
| ,, :: :: merge {{ tex \bbcomma }}
| , :: :: comma {{ tex ,\, }}
| ; :: :: semicomma {{ tex ;\, }}
formula :: 'formula_' ::=
| judgement :: :: judgement
| x : A in G :: M:: inG {{ tex [[x]][[:]][[A]]\in[[G]] }}
{{ coq (binds[[x]][[A]][[G]]) }}
| x : As in Gs :: M:: inGs {{ tex [[x]][[:]][[As]]\in[[Gs]] }}
{{ coq (binds[[x]][[As]][[Gs]]) }}
| A = B :: M:: eqTyp {{ coq ([[A]]=[[B]]) }}
| l /= l' :: M:: neqLabel {{ coq [[l]]<>[[l']] }}
parsing
t_arrow <= t_and
t_arrow right t_arrow
e_merge left e_merge
se_abs <= se_plus
se_nabs <= se_plus
subrules
val <:: exp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
funs
Trans ::=
fun
|| As || :: A :: trans
{{ com Type translation }}
by
||Int|| === Int
||As -> Bs|| === ||As|| -> ||Bs||
||{P} -> Bs|| === ||P|| -> ||Bs||
||{T}|| === |T|
fun
|| P || :: A :: ptrans
{{ com Parameter type translation }}
by
||[]|| === Top
||P; l:As|| === ||P|| & { l: ||As|| }
||P; l?:As|| === ||P|| & { l: ||As|| | Null }
fun
| T | :: A :: atrans
{{ com Argument type translation }}
{{ tex |[[T]]| }}
by
|[]| === Top
|T; l:As| === |T| & { l: ||As|| }
funs
CtxTrans ::=
fun
|| Gs || :: G :: ctxtrans
{{ com Typing context translation }}
by
||[]|| === []
||Gs, x:As|| === ||Gs||, x:||As||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
defns
Target :: '' ::=
defn
A <: B :: :: sub :: Sub_
{{ com Subtyping }}
by
--------------- :: Top
A <: Top
--------------- :: Bot
Bot <: A
--------------- :: Null
Null <: Null
--------------- :: Int
Int <: Int
A2 <: A1
B1 <: B2
------------------------ :: Arrow
A1 -> B1 <: A2 -> B2
A <: B
------------------------ :: Rcd
{l:A} <: {l:B}
A <: B
A <: C
------------------------ :: And
A <: B & C
A <: C
------------------------ :: AndL
A & B <: C
B <: C
------------------------ :: AndR
A & B <: C
A <: C
B <: C
------------------------ :: Or
A | B <: C
A <: B
------------------------ :: OrL
A <: B | C
A <: C
------------------------ :: OrR
A <: B | C
defn
G |- e : A :: :: typing :: Typ_
{{ com Typing }}
by
----------------- :: Top
G |- {} : Top
----------------- :: Null
G |- null : Null
----------------- :: Int
G |- int : Int
x : A in G
----------------- :: Var
G |- x : A
G, x : A |- e : B
----------------------------- :: Abs
G |- (\x : A . e : B) : A->B
G |- e1 : A -> B
G |- e2 : A
----------------------- :: App
G |- e1 e2 : B
G |- e : A
----------------------- :: Rcd
G |- {l:A=e} : {l:A}
G |- e : {l:A}
----------------------- :: Prj
G |- e.l : A
G |- e1 : A
G |- e2 : B
----------------------- :: Merge
G |- e1 ,, e2 : A & B
G |- e0 : A | B
G, x : A |- e1 : C
G, x : B |- e2 : C
-------------------------------------------------- :: Switch
G |- switch e0 as x case A => e1 case B => e2 : C
G |- letin -| G'
G' |- e : A
----------------- :: Let
G |- letin e : A
G |- e : A
A <: B
----------------- :: Sub
G |- e : B
defn
G |- letin -| G' :: :: letbind :: LB_
{{ com Let-in binding }}
by
G |- e : A
--------------------------- :: Let
G |- let x = e in -| G, x:A
G |- letin1 -| G'
G' |- letin2 -| G''
------------------------- :: Comp
G |- letin1 letin2 -| G''
------------ :: Id
G |- id -| G
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
defns
Source :: '' ::=
defn
Gs |- es : As ~~> e :: :: elab :: Ela_
{{ com Elaboration }}
by
----------------------- :: Int
Gs |- int : Int ~~> int
x : As in Gs
------------------ :: Var
Gs |- x : As ~~> x
Gs,x:As |- es : Bs ~~> e
------------------------------------------------ :: Abs
Gs |- \(x:As).es : As->Bs ~~> \x:||As||.e:||Bs||
Gs; x |- p : P ~~> letin -| Gs'
Gs' |- es : Bs ~~> e
-------------------------------------------------- :: NAbs
Gs |- \{p}.es : {P}->Bs ~~> \x:||P||.letin e:||Bs||
Gs |- es1 : As -> Bs ~~> e1
Gs |- es2 : As ~~> e2
---------------------------- :: App
Gs |- es1 es2 : Bs ~~> e1 e2
Gs |- es1 : {P} -> Bs ~~> e1
Gs |- es2 : {T} ~~> e2
Gs; e2 |- P <> T ~~> e2'
----------------------------- :: NApp
Gs |- es1 es2 : Bs ~~> e1 e2'
------------------------ :: NEmpty
Gs |- {[]} : {[]} ~~> {}
Gs |- {a} : {T} ~~> e'
Gs |- es : As ~~> e
-------------------------------------------------- :: NField
Gs |- {a; l = es} : {T; l:As} ~~> e',,{l:||As||=e}
defn
Gs ; x |- p : P ~~> letin -| Gs' :: :: pelab :: PEla_
{{ com Named parameter elaboration }}
{{ tex [[Gs]][[|-]]_{\!\!\!\![[x]]}\:\:[[p]][[:]][[P]][[~~>]][[letin]][[-|]][[Gs']] }}
by
----------------------------- :: Empty
Gs; x |- [] : [] ~~> id -| Gs
Gs; x |- p : P ~~> letin -| Gs'
-------------------------------------------------------------------- :: Required
Gs; x |- (p; l:As) : (P; l:As) ~~> letin let l = x.l in -| Gs', l:As
Gs; x |- p : P ~~> letin -| Gs'
Gs' |- es : As ~~> e
----------------------------------------------------------------------------------------------------------------- :: Optional
Gs; x |- (p; l=es) : (P; l?:As) ~~> letin let l = switch x.l as y case ||As|| => y case Null => e in -| Gs', l:As
defn
Gs ; e |- P <> T ~~> e' :: :: pmatch :: PMat_
{{ com Call site rewriting }}
{{ tex [[Gs]][[|-]]_{\!\!\!\![[e]]}\:\:[[P]][[<>]][[T]][[~~>]][[e']] }}
by
----------------------- :: Empty
Gs; e |- [] <> T ~~> {}
T.l => As
Gs; e |- P <> T ~~> e'
---------------------------------------------- :: Required
Gs; e |- (P; l:As) <> T ~~> e',,{l:||As||=e.l}
T.l => As
Gs; e |- P <> T ~~> e'
---------------------------------------------------- :: Present
Gs; e |- (P; l?:As) <> T ~~> e',,{l:||As|||Null=e.l}
T.l =/>
Gs; e |- P <> T ~~> e'
----------------------------------------------------- :: Absent
Gs; e |- (P; l?:As) <> T ~~> e',,{l:||As|||Null=null}
defn
T . l => As :: :: lookup :: LU_
{{ com Successful lookup }}
{{ tex [[T]]::[[l]][[=>]][[As]] }}
by
T.l =/>
------------------- :: Present
(T; l : As).l => As
l' /= l
T.l => As
--------------------- :: Absent
(T; l' : Bs).l => As
defn
T . l =/> :: :: lookdown :: LD_
{{ com Failed lookup }}
{{ tex [[T]]::[[l]][[=/>]] }}
by
-------- :: Empty
[].l =/>
l' /= l
T.l =/>
------------------ :: Absent
(T; l' : As).l =/>