-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.agda
592 lines (540 loc) · 25.6 KB
/
core.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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
open import Nat
open import Prelude
open import contexts
module core where
-- types
data htyp : Set where
b : htyp
⦇-⦈ : htyp
_==>_ : htyp → htyp → htyp
-- arrow type constructors bind very tightly
infixr 25 _==>_
-- external expressions
data hexp : Set where
c : hexp
_·:_ : hexp → htyp → hexp
X : Nat → hexp
·λ : Nat → hexp → hexp
·λ_[_]_ : Nat → htyp → hexp → hexp
⦇-⦈[_] : Nat → hexp
⦇⌜_⌟⦈[_] : hexp → Nat → hexp
_∘_ : hexp → hexp → hexp
-- the type of type contexts, i.e. Γs in the judegments below
tctx : Set
tctx = htyp ctx
mutual
-- identity substitution, substitition environments
data env : Set where
Id : (Γ : tctx) → env
Subst : (d : ihexp) → (y : Nat) → env → env
-- internal expressions
data ihexp : Set where
c : ihexp
X : Nat → ihexp
·λ_[_]_ : Nat → htyp → ihexp → ihexp
⦇-⦈⟨_⟩ : (Nat × env) → ihexp
⦇⌜_⌟⦈⟨_⟩ : ihexp → (Nat × env) → ihexp
_∘_ : ihexp → ihexp → ihexp
_⟨_⇒_⟩ : ihexp → htyp → htyp → ihexp
_⟨_⇒⦇-⦈⇏_⟩ : ihexp → htyp → htyp → ihexp
-- convenient notation for chaining together two agreeable casts
_⟨_⇒_⇒_⟩ : ihexp → htyp → htyp → htyp → ihexp
d ⟨ t1 ⇒ t2 ⇒ t3 ⟩ = d ⟨ t1 ⇒ t2 ⟩ ⟨ t2 ⇒ t3 ⟩
-- type consistency
data _~_ : (t1 t2 : htyp) → Set where
TCRefl : {τ : htyp} → τ ~ τ
TCHole1 : {τ : htyp} → τ ~ ⦇-⦈
TCHole2 : {τ : htyp} → ⦇-⦈ ~ τ
TCArr : {τ1 τ2 τ1' τ2' : htyp} →
τ1 ~ τ1' →
τ2 ~ τ2' →
τ1 ==> τ2 ~ τ1' ==> τ2'
-- type inconsistency
data _~̸_ : (τ1 τ2 : htyp) → Set where
ICBaseArr1 : {τ1 τ2 : htyp} → b ~̸ τ1 ==> τ2
ICBaseArr2 : {τ1 τ2 : htyp} → τ1 ==> τ2 ~̸ b
ICArr1 : {τ1 τ2 τ3 τ4 : htyp} →
τ1 ~̸ τ3 →
τ1 ==> τ2 ~̸ τ3 ==> τ4
ICArr2 : {τ1 τ2 τ3 τ4 : htyp} →
τ2 ~̸ τ4 →
τ1 ==> τ2 ~̸ τ3 ==> τ4
--- matching for arrows
data _▸arr_ : htyp → htyp → Set where
MAHole : ⦇-⦈ ▸arr ⦇-⦈ ==> ⦇-⦈
MAArr : {τ1 τ2 : htyp} → τ1 ==> τ2 ▸arr τ1 ==> τ2
-- the type of hole contexts, i.e. Δs in the judgements
hctx : Set
hctx = (htyp ctx × htyp) ctx
-- notation for a triple to match the CMTT syntax
_::_[_] : Nat → htyp → tctx → (Nat × (tctx × htyp))
u :: τ [ Γ ] = u , (Γ , τ)
-- the hole name u does not appear in the term e
data hole-name-new : (e : hexp) (u : Nat) → Set where
HNConst : ∀{u} → hole-name-new c u
HNAsc : ∀{e τ u} →
hole-name-new e u →
hole-name-new (e ·: τ) u
HNVar : ∀{x u} → hole-name-new (X x) u
HNLam1 : ∀{x e u} →
hole-name-new e u →
hole-name-new (·λ x e) u
HNLam2 : ∀{x e u τ} →
hole-name-new e u →
hole-name-new (·λ x [ τ ] e) u
HNHole : ∀{u u'} →
u' ≠ u →
hole-name-new (⦇-⦈[ u' ]) u
HNNEHole : ∀{u u' e} →
u' ≠ u →
hole-name-new e u →
hole-name-new (⦇⌜ e ⌟⦈[ u' ]) u
HNAp : ∀{ u e1 e2 } →
hole-name-new e1 u →
hole-name-new e2 u →
hole-name-new (e1 ∘ e2) u
-- two terms that do not share any hole names
data holes-disjoint : (e1 : hexp) → (e2 : hexp) → Set where
HDConst : ∀{e} → holes-disjoint c e
HDAsc : ∀{e1 e2 τ} → holes-disjoint e1 e2 → holes-disjoint (e1 ·: τ) e2
HDVar : ∀{x e} → holes-disjoint (X x) e
HDLam1 : ∀{x e1 e2} → holes-disjoint e1 e2 → holes-disjoint (·λ x e1) e2
HDLam2 : ∀{x e1 e2 τ} → holes-disjoint e1 e2 → holes-disjoint (·λ x [ τ ] e1) e2
HDHole : ∀{u e2} → hole-name-new e2 u → holes-disjoint (⦇-⦈[ u ]) e2
HDNEHole : ∀{u e1 e2} → hole-name-new e2 u → holes-disjoint e1 e2 → holes-disjoint (⦇⌜ e1 ⌟⦈[ u ]) e2
HDAp : ∀{e1 e2 e3} → holes-disjoint e1 e3 → holes-disjoint e2 e3 → holes-disjoint (e1 ∘ e2) e3
-- bidirectional type checking judgements for hexp
mutual
-- synthesis
data _⊢_=>_ : (Γ : tctx) (e : hexp) (τ : htyp) → Set where
SConst : {Γ : tctx} → Γ ⊢ c => b
SAsc : {Γ : tctx} {e : hexp} {τ : htyp} →
Γ ⊢ e <= τ →
Γ ⊢ (e ·: τ) => τ
SVar : {Γ : tctx} {τ : htyp} {x : Nat} →
(x , τ) ∈ Γ →
Γ ⊢ X x => τ
SAp : {Γ : tctx} {e1 e2 : hexp} {τ τ1 τ2 : htyp} →
holes-disjoint e1 e2 →
Γ ⊢ e1 => τ1 →
τ1 ▸arr τ2 ==> τ →
Γ ⊢ e2 <= τ2 →
Γ ⊢ (e1 ∘ e2) => τ
SEHole : {Γ : tctx} {u : Nat} → Γ ⊢ ⦇-⦈[ u ] => ⦇-⦈
SNEHole : {Γ : tctx} {e : hexp} {τ : htyp} {u : Nat} →
hole-name-new e u →
Γ ⊢ e => τ →
Γ ⊢ ⦇⌜ e ⌟⦈[ u ] => ⦇-⦈
SLam : {Γ : tctx} {e : hexp} {τ1 τ2 : htyp} {x : Nat} →
x # Γ →
(Γ ,, (x , τ1)) ⊢ e => τ2 →
Γ ⊢ ·λ x [ τ1 ] e => τ1 ==> τ2
-- analysis
data _⊢_<=_ : (Γ : htyp ctx) (e : hexp) (τ : htyp) → Set where
ASubsume : {Γ : tctx} {e : hexp} {τ τ' : htyp} →
Γ ⊢ e => τ' →
τ ~ τ' →
Γ ⊢ e <= τ
ALam : {Γ : tctx} {e : hexp} {τ τ1 τ2 : htyp} {x : Nat} →
x # Γ →
τ ▸arr τ1 ==> τ2 →
(Γ ,, (x , τ1)) ⊢ e <= τ2 →
Γ ⊢ (·λ x e) <= τ
-- those types without holes
data _tcomplete : htyp → Set where
TCBase : b tcomplete
TCArr : ∀{τ1 τ2} → τ1 tcomplete → τ2 tcomplete → (τ1 ==> τ2) tcomplete
-- those external expressions without holes
data _ecomplete : hexp → Set where
ECConst : c ecomplete
ECAsc : ∀{τ e} → τ tcomplete → e ecomplete → (e ·: τ) ecomplete
ECVar : ∀{x} → (X x) ecomplete
ECLam1 : ∀{x e} → e ecomplete → (·λ x e) ecomplete
ECLam2 : ∀{x e τ} → e ecomplete → τ tcomplete → (·λ x [ τ ] e) ecomplete
ECAp : ∀{e1 e2} → e1 ecomplete → e2 ecomplete → (e1 ∘ e2) ecomplete
-- those internal expressions without holes
data _dcomplete : ihexp → Set where
DCVar : ∀{x} → (X x) dcomplete
DCConst : c dcomplete
DCLam : ∀{x τ d} → d dcomplete → τ tcomplete → (·λ x [ τ ] d) dcomplete
DCAp : ∀{d1 d2} → d1 dcomplete → d2 dcomplete → (d1 ∘ d2) dcomplete
DCCast : ∀{d τ1 τ2} → d dcomplete → τ1 tcomplete → τ2 tcomplete → (d ⟨ τ1 ⇒ τ2 ⟩) dcomplete
-- contexts that only produce complete types
_gcomplete : tctx → Set
Γ gcomplete = (x : Nat) (τ : htyp) → (x , τ) ∈ Γ → τ tcomplete
-- those internal expressions where every cast is the identity cast and
-- there are no failed casts
data cast-id : ihexp → Set where
CIConst : cast-id c
CIVar : ∀{x} → cast-id (X x)
CILam : ∀{x τ d} → cast-id d → cast-id (·λ x [ τ ] d)
CIHole : ∀{u} → cast-id (⦇-⦈⟨ u ⟩)
CINEHole : ∀{d u} → cast-id d → cast-id (⦇⌜ d ⌟⦈⟨ u ⟩)
CIAp : ∀{d1 d2} → cast-id d1 → cast-id d2 → cast-id (d1 ∘ d2)
CICast : ∀{d τ} → cast-id d → cast-id (d ⟨ τ ⇒ τ ⟩)
-- expansion
mutual
-- synthesis
data _⊢_⇒_~>_⊣_ : (Γ : tctx) (e : hexp) (τ : htyp) (d : ihexp) (Δ : hctx) → Set where
ESConst : ∀{Γ} → Γ ⊢ c ⇒ b ~> c ⊣ ∅
ESVar : ∀{Γ x τ} → (x , τ) ∈ Γ →
Γ ⊢ X x ⇒ τ ~> X x ⊣ ∅
ESLam : ∀{Γ x τ1 τ2 e d Δ } →
(x # Γ) →
(Γ ,, (x , τ1)) ⊢ e ⇒ τ2 ~> d ⊣ Δ →
Γ ⊢ ·λ x [ τ1 ] e ⇒ (τ1 ==> τ2) ~> ·λ x [ τ1 ] d ⊣ Δ
ESAp : ∀{Γ e1 τ τ1 τ1' τ2 τ2' d1 Δ1 e2 d2 Δ2 } →
holes-disjoint e1 e2 →
Δ1 ## Δ2 →
Γ ⊢ e1 => τ1 →
τ1 ▸arr τ2 ==> τ →
Γ ⊢ e1 ⇐ (τ2 ==> τ) ~> d1 :: τ1' ⊣ Δ1 →
Γ ⊢ e2 ⇐ τ2 ~> d2 :: τ2' ⊣ Δ2 →
Γ ⊢ e1 ∘ e2 ⇒ τ ~> (d1 ⟨ τ1' ⇒ τ2 ==> τ ⟩) ∘ (d2 ⟨ τ2' ⇒ τ2 ⟩) ⊣ (Δ1 ∪ Δ2)
ESEHole : ∀{ Γ u } →
Γ ⊢ ⦇-⦈[ u ] ⇒ ⦇-⦈ ~> ⦇-⦈⟨ u , Id Γ ⟩ ⊣ ■ (u :: ⦇-⦈ [ Γ ])
ESNEHole : ∀{ Γ e τ d u Δ } →
Δ ## (■ (u , Γ , ⦇-⦈)) →
Γ ⊢ e ⇒ τ ~> d ⊣ Δ →
Γ ⊢ ⦇⌜ e ⌟⦈[ u ] ⇒ ⦇-⦈ ~> ⦇⌜ d ⌟⦈⟨ u , Id Γ ⟩ ⊣ (Δ ,, u :: ⦇-⦈ [ Γ ])
ESAsc : ∀ {Γ e τ d τ' Δ} →
Γ ⊢ e ⇐ τ ~> d :: τ' ⊣ Δ →
Γ ⊢ (e ·: τ) ⇒ τ ~> d ⟨ τ' ⇒ τ ⟩ ⊣ Δ
-- analysis
data _⊢_⇐_~>_::_⊣_ : (Γ : tctx) (e : hexp) (τ : htyp) (d : ihexp) (τ' : htyp) (Δ : hctx) → Set where
EALam : ∀{Γ x τ τ1 τ2 e d τ2' Δ } →
(x # Γ) →
τ ▸arr τ1 ==> τ2 →
(Γ ,, (x , τ1)) ⊢ e ⇐ τ2 ~> d :: τ2' ⊣ Δ →
Γ ⊢ ·λ x e ⇐ τ ~> ·λ x [ τ1 ] d :: τ1 ==> τ2' ⊣ Δ
EASubsume : ∀{e Γ τ' d Δ τ} →
((u : Nat) → e ≠ ⦇-⦈[ u ]) →
((e' : hexp) (u : Nat) → e ≠ ⦇⌜ e' ⌟⦈[ u ]) →
Γ ⊢ e ⇒ τ' ~> d ⊣ Δ →
τ ~ τ' →
Γ ⊢ e ⇐ τ ~> d :: τ' ⊣ Δ
EAEHole : ∀{ Γ u τ } →
Γ ⊢ ⦇-⦈[ u ] ⇐ τ ~> ⦇-⦈⟨ u , Id Γ ⟩ :: τ ⊣ ■ (u :: τ [ Γ ])
EANEHole : ∀{ Γ e u τ d τ' Δ } →
Δ ## (■ (u , Γ , τ)) →
Γ ⊢ e ⇒ τ' ~> d ⊣ Δ →
Γ ⊢ ⦇⌜ e ⌟⦈[ u ] ⇐ τ ~> ⦇⌜ d ⌟⦈⟨ u , Id Γ ⟩ :: τ ⊣ (Δ ,, u :: τ [ Γ ])
-- ground types
data _ground : (τ : htyp) → Set where
GBase : b ground
GHole : ⦇-⦈ ==> ⦇-⦈ ground
mutual
-- substitution typing
data _,_⊢_:s:_ : hctx → tctx → env → tctx → Set where
STAId : ∀{Γ Γ' Δ} →
((x : Nat) (τ : htyp) → (x , τ) ∈ Γ' → (x , τ) ∈ Γ) →
Δ , Γ ⊢ Id Γ' :s: Γ'
STASubst : ∀{Γ Δ σ y Γ' d τ } →
Δ , Γ ,, (y , τ) ⊢ σ :s: Γ' →
Δ , Γ ⊢ d :: τ →
Δ , Γ ⊢ Subst d y σ :s: Γ'
-- type assignment
data _,_⊢_::_ : (Δ : hctx) (Γ : tctx) (d : ihexp) (τ : htyp) → Set where
TAConst : ∀{Δ Γ} → Δ , Γ ⊢ c :: b
TAVar : ∀{Δ Γ x τ} → (x , τ) ∈ Γ → Δ , Γ ⊢ X x :: τ
TALam : ∀{ Δ Γ x τ1 d τ2} →
x # Γ →
Δ , (Γ ,, (x , τ1)) ⊢ d :: τ2 →
Δ , Γ ⊢ ·λ x [ τ1 ] d :: (τ1 ==> τ2)
TAAp : ∀{ Δ Γ d1 d2 τ1 τ} →
Δ , Γ ⊢ d1 :: τ1 ==> τ →
Δ , Γ ⊢ d2 :: τ1 →
Δ , Γ ⊢ d1 ∘ d2 :: τ
TAEHole : ∀{ Δ Γ σ u Γ' τ} →
(u , (Γ' , τ)) ∈ Δ →
Δ , Γ ⊢ σ :s: Γ' →
Δ , Γ ⊢ ⦇-⦈⟨ u , σ ⟩ :: τ
TANEHole : ∀ { Δ Γ d τ' Γ' u σ τ } →
(u , (Γ' , τ)) ∈ Δ →
Δ , Γ ⊢ d :: τ' →
Δ , Γ ⊢ σ :s: Γ' →
Δ , Γ ⊢ ⦇⌜ d ⌟⦈⟨ u , σ ⟩ :: τ
TACast : ∀{ Δ Γ d τ1 τ2} →
Δ , Γ ⊢ d :: τ1 →
τ1 ~ τ2 →
Δ , Γ ⊢ d ⟨ τ1 ⇒ τ2 ⟩ :: τ2
TAFailedCast : ∀{Δ Γ d τ1 τ2} →
Δ , Γ ⊢ d :: τ1 →
τ1 ground →
τ2 ground →
τ1 ≠ τ2 →
Δ , Γ ⊢ d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩ :: τ2
-- substitution
[_/_]_ : ihexp → Nat → ihexp → ihexp
[ d / y ] c = c
[ d / y ] X x
with natEQ x y
[ d / y ] X .y | Inl refl = d
[ d / y ] X x | Inr neq = X x
[ d / y ] (·λ x [ x₁ ] d')
with natEQ x y
[ d / y ] (·λ .y [ τ ] d') | Inl refl = ·λ y [ τ ] d'
[ d / y ] (·λ x [ τ ] d') | Inr x₁ = ·λ x [ τ ] ( [ d / y ] d')
[ d / y ] ⦇-⦈⟨ u , σ ⟩ = ⦇-⦈⟨ u , Subst d y σ ⟩
[ d / y ] ⦇⌜ d' ⌟⦈⟨ u , σ ⟩ = ⦇⌜ [ d / y ] d' ⌟⦈⟨ u , Subst d y σ ⟩
[ d / y ] (d1 ∘ d2) = ([ d / y ] d1) ∘ ([ d / y ] d2)
[ d / y ] (d' ⟨ τ1 ⇒ τ2 ⟩ ) = ([ d / y ] d') ⟨ τ1 ⇒ τ2 ⟩
[ d / y ] (d' ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩ ) = ([ d / y ] d') ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩
-- applying an environment to an expression
apply-env : env → ihexp → ihexp
apply-env (Id Γ) d = d
apply-env (Subst d y σ) d' = [ d / y ] ( apply-env σ d')
-- values
data _val : (d : ihexp) → Set where
VConst : c val
VLam : ∀{x τ d} → (·λ x [ τ ] d) val
-- boxed values
data _boxedval : (d : ihexp) → Set where
BVVal : ∀{d} → d val → d boxedval
BVArrCast : ∀{ d τ1 τ2 τ3 τ4 } →
τ1 ==> τ2 ≠ τ3 ==> τ4 →
d boxedval →
d ⟨ (τ1 ==> τ2) ⇒ (τ3 ==> τ4) ⟩ boxedval
BVHoleCast : ∀{ τ d } → τ ground → d boxedval → d ⟨ τ ⇒ ⦇-⦈ ⟩ boxedval
mutual
-- indeterminate forms
data _indet : (d : ihexp) → Set where
IEHole : ∀{u σ} → ⦇-⦈⟨ u , σ ⟩ indet
INEHole : ∀{d u σ} → d final → ⦇⌜ d ⌟⦈⟨ u , σ ⟩ indet
IAp : ∀{d1 d2} → ((τ1 τ2 τ3 τ4 : htyp) (d1' : ihexp) →
d1 ≠ (d1' ⟨(τ1 ==> τ2) ⇒ (τ3 ==> τ4)⟩)) →
d1 indet →
d2 final →
(d1 ∘ d2) indet
ICastArr : ∀{d τ1 τ2 τ3 τ4} →
τ1 ==> τ2 ≠ τ3 ==> τ4 →
d indet →
d ⟨ (τ1 ==> τ2) ⇒ (τ3 ==> τ4) ⟩ indet
ICastGroundHole : ∀{ τ d } →
τ ground →
d indet →
d ⟨ τ ⇒ ⦇-⦈ ⟩ indet
ICastHoleGround : ∀ { d τ } →
((d' : ihexp) (τ' : htyp) → d ≠ (d' ⟨ τ' ⇒ ⦇-⦈ ⟩)) →
d indet →
τ ground →
d ⟨ ⦇-⦈ ⇒ τ ⟩ indet
IFailedCast : ∀{ d τ1 τ2 } →
d final →
τ1 ground →
τ2 ground →
τ1 ≠ τ2 →
d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩ indet
-- final expressions
data _final : (d : ihexp) → Set where
FBoxedVal : ∀{d} → d boxedval → d final
FIndet : ∀{d} → d indet → d final
-- contextual dynamics
-- evaluation contexts
data ectx : Set where
⊙ : ectx
_∘₁_ : ectx → ihexp → ectx
_∘₂_ : ihexp → ectx → ectx
⦇⌜_⌟⦈⟨_⟩ : ectx → (Nat × env ) → ectx
_⟨_⇒_⟩ : ectx → htyp → htyp → ectx
_⟨_⇒⦇-⦈⇏_⟩ : ectx → htyp → htyp → ectx
-- note: this judgement is redundant: in the absence of the premises in
-- the red brackets, all syntactically well formed ectxs are valid. with
-- finality premises, that's not true, and that would propagate through
-- additions to the calculus. so we leave it here for clarity but note
-- that, as written, in any use case its either trival to prove or
-- provides no additional information
--ε is an evaluation context
data _evalctx : (ε : ectx) → Set where
ECDot : ⊙ evalctx
ECAp1 : ∀{d ε} →
ε evalctx →
(ε ∘₁ d) evalctx
ECAp2 : ∀{d ε} →
-- d final → -- red brackets
ε evalctx →
(d ∘₂ ε) evalctx
ECNEHole : ∀{ε u σ} →
ε evalctx →
⦇⌜ ε ⌟⦈⟨ u , σ ⟩ evalctx
ECCast : ∀{ ε τ1 τ2} →
ε evalctx →
(ε ⟨ τ1 ⇒ τ2 ⟩) evalctx
ECFailedCast : ∀{ ε τ1 τ2 } →
ε evalctx →
ε ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩ evalctx
-- d is the result of filling the hole in ε with d'
data _==_⟦_⟧ : (d : ihexp) (ε : ectx) (d' : ihexp) → Set where
FHOuter : ∀{d} → d == ⊙ ⟦ d ⟧
FHAp1 : ∀{d1 d1' d2 ε} →
d1 == ε ⟦ d1' ⟧ →
(d1 ∘ d2) == (ε ∘₁ d2) ⟦ d1' ⟧
FHAp2 : ∀{d1 d2 d2' ε} →
-- d1 final → -- red brackets
d2 == ε ⟦ d2' ⟧ →
(d1 ∘ d2) == (d1 ∘₂ ε) ⟦ d2' ⟧
FHNEHole : ∀{ d d' ε u σ} →
d == ε ⟦ d' ⟧ →
⦇⌜ d ⌟⦈⟨ (u , σ ) ⟩ == ⦇⌜ ε ⌟⦈⟨ (u , σ ) ⟩ ⟦ d' ⟧
FHCast : ∀{ d d' ε τ1 τ2 } →
d == ε ⟦ d' ⟧ →
d ⟨ τ1 ⇒ τ2 ⟩ == ε ⟨ τ1 ⇒ τ2 ⟩ ⟦ d' ⟧
FHFailedCast : ∀{ d d' ε τ1 τ2} →
d == ε ⟦ d' ⟧ →
(d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩) == (ε ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩) ⟦ d' ⟧
-- matched ground types
data _▸gnd_ : htyp → htyp → Set where
MGArr : ∀{τ1 τ2} →
(τ1 ==> τ2) ≠ (⦇-⦈ ==> ⦇-⦈) →
(τ1 ==> τ2) ▸gnd (⦇-⦈ ==> ⦇-⦈)
-- instruction transition judgement
data _→>_ : (d d' : ihexp) → Set where
ITLam : ∀{ x τ d1 d2 } →
-- d2 final → -- red brackets
((·λ x [ τ ] d1) ∘ d2) →> ([ d2 / x ] d1)
ITCastID : ∀{d τ } →
-- d final → -- red brackets
(d ⟨ τ ⇒ τ ⟩) →> d
ITCastSucceed : ∀{d τ } →
-- d final → -- red brackets
τ ground →
(d ⟨ τ ⇒ ⦇-⦈ ⇒ τ ⟩) →> d
ITCastFail : ∀{ d τ1 τ2} →
-- d final → -- red brackets
τ1 ground →
τ2 ground →
τ1 ≠ τ2 →
(d ⟨ τ1 ⇒ ⦇-⦈ ⇒ τ2 ⟩) →> (d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩)
ITApCast : ∀{d1 d2 τ1 τ2 τ1' τ2' } →
-- d1 final → -- red brackets
-- d2 final → -- red brackets
((d1 ⟨ (τ1 ==> τ2) ⇒ (τ1' ==> τ2')⟩) ∘ d2) →> ((d1 ∘ (d2 ⟨ τ1' ⇒ τ1 ⟩)) ⟨ τ2 ⇒ τ2' ⟩)
ITGround : ∀{ d τ τ'} →
-- d final → -- red brackets
τ ▸gnd τ' →
(d ⟨ τ ⇒ ⦇-⦈ ⟩) →> (d ⟨ τ ⇒ τ' ⇒ ⦇-⦈ ⟩)
ITExpand : ∀{d τ τ' } →
-- d final → -- red brackets
τ ▸gnd τ' →
(d ⟨ ⦇-⦈ ⇒ τ ⟩) →> (d ⟨ ⦇-⦈ ⇒ τ' ⇒ τ ⟩)
-- single step (in contextual evaluation sense)
data _↦_ : (d d' : ihexp) → Set where
Step : ∀{ d d0 d' d0' ε} →
d == ε ⟦ d0 ⟧ →
d0 →> d0' →
d' == ε ⟦ d0' ⟧ →
d ↦ d'
-- reflexive transitive closure of single steps into multi steps
data _↦*_ : (d d' : ihexp) → Set where
MSRefl : ∀{d} → d ↦* d
MSStep : ∀{d d' d''} →
d ↦ d' →
d' ↦* d'' →
d ↦* d''
-- freshness
mutual
-- ... with respect to a hole context
data envfresh : Nat → env → Set where
EFId : ∀{x Γ} → x # Γ → envfresh x (Id Γ)
EFSubst : ∀{x d σ y} → fresh x d
→ envfresh x σ
→ x ≠ y
→ envfresh x (Subst d y σ)
-- ... for inernal expressions
data fresh : Nat → ihexp → Set where
FConst : ∀{x} → fresh x c
FVar : ∀{x y} → x ≠ y → fresh x (X y)
FLam : ∀{x y τ d} → x ≠ y → fresh x d → fresh x (·λ y [ τ ] d)
FHole : ∀{x u σ} → envfresh x σ → fresh x (⦇-⦈⟨ u , σ ⟩)
FNEHole : ∀{x d u σ} → envfresh x σ → fresh x d → fresh x (⦇⌜ d ⌟⦈⟨ u , σ ⟩)
FAp : ∀{x d1 d2} → fresh x d1 → fresh x d2 → fresh x (d1 ∘ d2)
FCast : ∀{x d τ1 τ2} → fresh x d → fresh x (d ⟨ τ1 ⇒ τ2 ⟩)
FFailedCast : ∀{x d τ1 τ2} → fresh x d → fresh x (d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩)
-- ... for external expressions
data freshh : Nat → hexp → Set where
FRHConst : ∀{x} → freshh x c
FRHAsc : ∀{x e τ} → freshh x e → freshh x (e ·: τ)
FRHVar : ∀{x y} → x ≠ y → freshh x (X y)
FRHLam1 : ∀{x y e} → x ≠ y → freshh x e → freshh x (·λ y e)
FRHLam2 : ∀{x τ e y} → x ≠ y → freshh x e → freshh x (·λ y [ τ ] e)
FRHEHole : ∀{x u} → freshh x (⦇-⦈[ u ])
FRHNEHole : ∀{x u e} → freshh x e → freshh x (⦇⌜ e ⌟⦈[ u ])
FRHAp : ∀{x e1 e2} → freshh x e1 → freshh x e2 → freshh x (e1 ∘ e2)
-- x is not used in a binding site in d
mutual
data unbound-in-σ : Nat → env → Set where
UBσId : ∀{x Γ} → unbound-in-σ x (Id Γ)
UBσSubst : ∀{x d y σ} → unbound-in x d
→ unbound-in-σ x σ
→ x ≠ y
→ unbound-in-σ x (Subst d y σ)
data unbound-in : (x : Nat) (d : ihexp) → Set where
UBConst : ∀{x} → unbound-in x c
UBVar : ∀{x y} → unbound-in x (X y)
UBLam2 : ∀{x d y τ} → x ≠ y
→ unbound-in x d
→ unbound-in x (·λ_[_]_ y τ d)
UBHole : ∀{x u σ} → unbound-in-σ x σ
→ unbound-in x (⦇-⦈⟨ u , σ ⟩)
UBNEHole : ∀{x u σ d }
→ unbound-in-σ x σ
→ unbound-in x d
→ unbound-in x (⦇⌜ d ⌟⦈⟨ u , σ ⟩)
UBAp : ∀{ x d1 d2 } →
unbound-in x d1 →
unbound-in x d2 →
unbound-in x (d1 ∘ d2)
UBCast : ∀{x d τ1 τ2} → unbound-in x d → unbound-in x (d ⟨ τ1 ⇒ τ2 ⟩)
UBFailedCast : ∀{x d τ1 τ2} → unbound-in x d → unbound-in x (d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩)
mutual
data binders-disjoint-σ : env → ihexp → Set where
BDσId : ∀{Γ d} → binders-disjoint-σ (Id Γ) d
BDσSubst : ∀{d1 d2 y σ} → binders-disjoint d1 d2
→ binders-disjoint-σ σ d2
→ binders-disjoint-σ (Subst d1 y σ) d2
-- two terms that do not share any binders
data binders-disjoint : (d1 : ihexp) → (d2 : ihexp) → Set where
BDConst : ∀{d} → binders-disjoint c d
BDVar : ∀{x d} → binders-disjoint (X x) d
BDLam : ∀{x τ d1 d2} → binders-disjoint d1 d2
→ unbound-in x d2
→ binders-disjoint (·λ_[_]_ x τ d1) d2
BDHole : ∀{u σ d2} → binders-disjoint-σ σ d2
→ binders-disjoint (⦇-⦈⟨ u , σ ⟩) d2
BDNEHole : ∀{u σ d1 d2} → binders-disjoint-σ σ d2
→ binders-disjoint d1 d2
→ binders-disjoint (⦇⌜ d1 ⌟⦈⟨ u , σ ⟩) d2
BDAp : ∀{d1 d2 d3} → binders-disjoint d1 d3
→ binders-disjoint d2 d3
→ binders-disjoint (d1 ∘ d2) d3
BDCast : ∀{d1 d2 τ1 τ2} → binders-disjoint d1 d2 → binders-disjoint (d1 ⟨ τ1 ⇒ τ2 ⟩) d2
BDFailedCast : ∀{d1 d2 τ1 τ2} → binders-disjoint d1 d2 → binders-disjoint (d1 ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩) d2
mutual
-- each term has to be binders unique, and they have to be pairwise
-- disjoint with the collection of bound vars
data binders-unique-σ : env → Set where
BUσId : ∀{Γ} → binders-unique-σ (Id Γ)
BUσSubst : ∀{d y σ} → binders-unique d
→ binders-unique-σ σ
→ binders-disjoint-σ σ d
→ binders-unique-σ (Subst d y σ)
-- all the variable names in the term are unique
data binders-unique : ihexp → Set where
BUHole : binders-unique c
BUVar : ∀{x} → binders-unique (X x)
BULam : {x : Nat} {τ : htyp} {d : ihexp} → binders-unique d
→ unbound-in x d
→ binders-unique (·λ_[_]_ x τ d)
BUEHole : ∀{u σ} → binders-unique-σ σ
→ binders-unique (⦇-⦈⟨ u , σ ⟩)
BUNEHole : ∀{u σ d} → binders-unique d
→ binders-unique-σ σ
→ binders-unique (⦇⌜ d ⌟⦈⟨ u , σ ⟩)
BUAp : ∀{d1 d2} → binders-unique d1
→ binders-unique d2
→ binders-disjoint d1 d2
→ binders-unique (d1 ∘ d2)
BUCast : ∀{d τ1 τ2} → binders-unique d
→ binders-unique (d ⟨ τ1 ⇒ τ2 ⟩)
BUFailedCast : ∀{d τ1 τ2} → binders-unique d
→ binders-unique (d ⟨ τ1 ⇒⦇-⦈⇏ τ2 ⟩)