-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamics-core.agda
1165 lines (1125 loc) · 45.6 KB
/
dynamics-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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open import Nat
open import Prelude
open import contexts
open import core
module dynamics-core where
open core public
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
N : Nat → ihexp
_·+_ : ihexp → ihexp → ihexp
X : Nat → ihexp
·λ_·[_]_ : Nat → htyp → ihexp → ihexp
_∘_ : ihexp → ihexp → ihexp
inl : htyp → ihexp → ihexp
inr : htyp → ihexp → ihexp
case : ihexp → Nat → ihexp → Nat → ihexp → ihexp
⟨_,_⟩ : ihexp → ihexp → ihexp
fst : ihexp → ihexp
snd : ihexp → ihexp
⦇-⦈⟨_⟩ : (Nat × env) → ihexp
⦇⌜_⌟⦈⟨_⟩ : ihexp → (Nat × env) → 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 ⟩
-- 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 : hexp → Nat → Set where
HNNum : ∀{n u} → hole-name-new (N n) u
HNPlus : ∀{e1 e2 u} →
hole-name-new e1 u →
hole-name-new e2 u →
hole-name-new (e1 ·+ e2) 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
HNAp : ∀{e1 e2 u} →
hole-name-new e1 u →
hole-name-new e2 u →
hole-name-new (e1 ∘ e2) u
HNInl : ∀{e u} →
hole-name-new e u →
hole-name-new (inl e) u
HNInr : ∀{e u} →
hole-name-new e u →
hole-name-new (inr e) u
HNCase : ∀{e x e1 y e2 u} →
hole-name-new e u →
hole-name-new e1 u →
hole-name-new e2 u →
hole-name-new (case e x e1 y e2) u
HNPair : ∀{e1 e2 u} →
hole-name-new e1 u →
hole-name-new e2 u →
hole-name-new ⟨ e1 , e2 ⟩ u
HNFst : ∀{e u} →
hole-name-new e u →
hole-name-new (fst e) u
HNSnd : ∀{e u} →
hole-name-new e u →
hole-name-new (snd 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
-- two terms that do not share any hole names
data holes-disjoint : hexp → hexp → Set where
HDNum : ∀{n e} → holes-disjoint (N n) e
HDPlus : ∀{e1 e2 e3} →
holes-disjoint e1 e3 →
holes-disjoint e2 e3 →
holes-disjoint (e1 ·+ e2) e3
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
HDAp : ∀{e1 e2 e3} →
holes-disjoint e1 e3 →
holes-disjoint e2 e3 →
holes-disjoint (e1 ∘ e2) e3
HDInl : ∀{e1 e2} →
holes-disjoint e1 e2 →
holes-disjoint (inl e1) e2
HDInr : ∀{e1 e2} →
holes-disjoint e1 e2 →
holes-disjoint (inr e1) e2
HDCase : ∀{e x e1 y e2 e3} →
holes-disjoint e e3 →
holes-disjoint e1 e3 →
holes-disjoint e2 e3 →
holes-disjoint (case e x e1 y e2) e3
HDPair : ∀{e1 e2 e3} →
holes-disjoint e1 e3 →
holes-disjoint e2 e3 →
holes-disjoint ⟨ e1 , e2 ⟩ e3
HDFst : ∀{e1 e2} →
holes-disjoint e1 e2 →
holes-disjoint (fst e1) e2
HDSnd : ∀{e1 e2} →
holes-disjoint e1 e2 →
holes-disjoint (snd 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
-- all hole names in the term are unique
data holes-unique : hexp → Set where
HUNum : ∀{n} →
holes-unique (N n)
HUPlus : ∀{e1 e2} →
holes-unique e1 →
holes-unique e2 →
holes-disjoint e1 e2 →
holes-unique (e1 ·+ e2)
HUAsc : ∀{e τ} →
holes-unique e →
holes-unique (e ·: τ)
HUVar : ∀{x} →
holes-unique (X x)
HULam1 : ∀{x e} →
holes-unique e →
holes-unique (·λ x e)
HULam2 : ∀{x e τ} →
holes-unique e →
holes-unique (·λ x ·[ τ ] e)
HUAp : ∀{e1 e2} →
holes-unique e1 →
holes-unique e2 →
holes-disjoint e1 e2 →
holes-unique (e1 ∘ e2)
HUInl : ∀{e} →
holes-unique e →
holes-unique (inl e)
HUInr : ∀{e} →
holes-unique e →
holes-unique (inr e)
HUCase : ∀{e x e1 y e2} →
holes-unique e →
holes-unique e1 →
holes-unique e2 →
holes-disjoint e e1 →
holes-disjoint e e2 →
holes-disjoint e1 e2 →
holes-unique (case e x e1 y e2)
HUPair : ∀{e1 e2} →
holes-unique e1 →
holes-unique e2 →
holes-disjoint e1 e2 →
holes-unique ⟨ e1 , e2 ⟩
HUFst : ∀{e} →
holes-unique e →
holes-unique (fst e)
HUSnd : ∀{e} →
holes-unique e →
holes-unique (snd e)
HUHole : ∀{u} →
holes-unique (⦇-⦈[ u ])
HUNEHole : ∀{u e} →
holes-unique e →
hole-name-new e u →
holes-unique (⦇⌜ e ⌟⦈[ u ])
-- freshness for external expressions
data freshh : Nat → hexp → Set where
FRHNum : ∀{x n} →
freshh x (N n)
FRHPlus : ∀{x d1 d2} →
freshh x d1 →
freshh x d2 →
freshh x (d1 ·+ d2)
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)
FRHAp : ∀{x e1 e2} →
freshh x e1 →
freshh x e2 →
freshh x (e1 ∘ e2)
FRHInl : ∀{x d} →
freshh x d →
freshh x (inl d)
FRHInr : ∀{x d} →
freshh x d →
freshh x (inr d)
FRHCase : ∀{x d y d1 z d2} →
freshh x d →
x ≠ y →
freshh x d1 →
x ≠ z →
freshh x d2 →
freshh x (case d y d1 z d2)
FRHPair : ∀{x d1 d2} →
freshh x d1 →
freshh x d2 →
freshh x ⟨ d1 , d2 ⟩
FRHFst : ∀{x d} →
freshh x d →
freshh x (fst d)
FRHSnd : ∀{x d} →
freshh x d →
freshh x (snd d)
FRHEHole : ∀{x u} →
freshh x (⦇-⦈[ u ])
FRHNEHole : ∀{x u e} →
freshh x e →
freshh x (⦇⌜ e ⌟⦈[ u ])
-- those internal expressions without holes
data _dcomplete : ihexp → Set where
DCNum : ∀{n} →
(N n) dcomplete
DCPlus : ∀{d1 d2} →
d1 dcomplete →
d2 dcomplete →
(d1 ·+ d2) dcomplete
DCVar : ∀{x} →
(X x) dcomplete
DCLam : ∀{x τ d} →
d dcomplete →
τ tcomplete →
(·λ x ·[ τ ] d) dcomplete
DCAp : ∀{d1 d2} →
d1 dcomplete →
d2 dcomplete →
(d1 ∘ d2) dcomplete
DCInl : ∀{τ d} →
τ tcomplete →
d dcomplete →
(inl τ d) dcomplete
DCInr : ∀{τ d} →
τ tcomplete →
d dcomplete →
(inr τ d) dcomplete
DCCase : ∀{d x d1 y d2} →
d dcomplete →
d1 dcomplete →
d2 dcomplete →
(case d x d1 y d2) dcomplete
DCPair : ∀{d1 d2} →
d1 dcomplete →
d2 dcomplete →
⟨ d1 , d2 ⟩ dcomplete
DCFst : ∀{d} →
d dcomplete →
(fst d) dcomplete
DCSnd : ∀{d} →
d dcomplete →
(snd d) dcomplete
DCCast : ∀{d τ1 τ2} →
d dcomplete →
τ1 tcomplete →
τ2 tcomplete →
(d ⟨ τ1 ⇒ τ2 ⟩) dcomplete
-- expansion
mutual
-- synthesis
data _⊢_⇒_~>_⊣_ : (Γ : tctx) (e : hexp) (τ : htyp) (d : ihexp) (Δ : hctx) → Set where
ESNum : ∀{Γ n} →
Γ ⊢ (N n) ⇒ num ~> (N n) ⊣ ∅
ESPlus : ∀{Γ e1 e2 d1 d2 Δ1 Δ2 τ1 τ2} →
holes-disjoint e1 e2 →
Δ1 ## Δ2 →
Γ ⊢ e1 ⇐ num ~> d1 :: τ1 ⊣ Δ1 →
Γ ⊢ e2 ⇐ num ~> d2 :: τ2 ⊣ Δ2 →
Γ ⊢ e1 ·+ e2 ⇒ num ~> (d1 ⟨ τ1 ⇒ num ⟩) ·+ (d2 ⟨ τ2 ⇒ num ⟩) ⊣ (Δ1 ∪ Δ2)
ESAsc : ∀{Γ e τ d τ' Δ} →
Γ ⊢ e ⇐ τ ~> d :: τ' ⊣ Δ →
Γ ⊢ (e ·: τ) ⇒ τ ~> d ⟨ τ' ⇒ τ ⟩ ⊣ Δ
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)
ESPair : ∀{Γ e1 τ1 d1 Δ1 e2 τ2 d2 Δ2} →
holes-disjoint e1 e2 →
Δ1 ## Δ2 →
Γ ⊢ e1 ⇒ τ1 ~> d1 ⊣ Δ1 →
Γ ⊢ e2 ⇒ τ2 ~> d2 ⊣ Δ2 →
Γ ⊢ ⟨ e1 , e2 ⟩ ⇒ τ1 ⊠ τ2 ~> ⟨ d1 , d2 ⟩ ⊣ (Δ1 ∪ Δ2)
ESFst : ∀{Γ e τ τ' d τ1 τ2 Δ} →
Γ ⊢ e => τ →
τ ▸prod τ1 ⊠ τ2 →
Γ ⊢ e ⇐ τ1 ⊠ τ2 ~> d :: τ' ⊣ Δ →
Γ ⊢ fst e ⇒ τ1 ~> fst (d ⟨ τ' ⇒ τ1 ⊠ τ2 ⟩) ⊣ Δ
ESSnd : ∀{Γ e τ τ' d τ1 τ2 Δ} →
Γ ⊢ e => τ →
τ ▸prod τ1 ⊠ τ2 →
Γ ⊢ e ⇐ τ1 ⊠ τ2 ~> d :: τ' ⊣ Δ →
Γ ⊢ snd e ⇒ τ2 ~> snd (d ⟨ τ' ⇒ τ1 ⊠ τ2 ⟩) ⊣ Δ
ESEHole : ∀{Γ u} →
Γ ⊢ ⦇-⦈[ u ] ⇒ ⦇-⦈ ~> ⦇-⦈⟨ u , Id Γ ⟩ ⊣ ■ (u :: ⦇-⦈ [ Γ ])
ESNEHole : ∀{Γ e τ d u Δ} →
Δ ## (■ (u , Γ , ⦇-⦈)) →
Γ ⊢ e ⇒ τ ~> d ⊣ Δ →
Γ ⊢ ⦇⌜ e ⌟⦈[ u ] ⇒ ⦇-⦈ ~> ⦇⌜ d ⌟⦈⟨ u , Id Γ ⟩ ⊣ (Δ ,, u :: ⦇-⦈ [ Γ ])
-- analysis
data _⊢_⇐_~>_::_⊣_ : (Γ : tctx) (e : hexp) (τ : htyp)
(d : ihexp) (τ' : htyp) (Δ : hctx) → Set where
EASubsume : ∀{e Γ τ' d Δ τ} →
((u : Nat) → e ≠ ⦇-⦈[ u ]) →
((e' : hexp) (u : Nat) → e ≠ ⦇⌜ e' ⌟⦈[ u ]) →
Γ ⊢ e ⇒ τ' ~> d ⊣ Δ →
τ ~ τ' →
Γ ⊢ e ⇐ τ ~> d :: τ' ⊣ Δ
EALam : ∀{Γ x τ τ1 τ2 e d τ2' Δ} →
x # Γ →
τ ▸arr τ1 ==> τ2 →
(Γ ,, (x , τ1)) ⊢ e ⇐ τ2 ~> d :: τ2' ⊣ Δ →
Γ ⊢ ·λ x e ⇐ τ ~> ·λ x ·[ τ1 ] d :: τ1 ==> τ2' ⊣ Δ
EAInl : ∀{Γ τ τ1 τ2 e d τ1' Δ} →
τ ▸sum τ1 ⊕ τ2 →
Γ ⊢ e ⇐ τ1 ~> d :: τ1' ⊣ Δ →
Γ ⊢ inl e ⇐ τ ~> inl τ2 d :: τ1' ⊕ τ2 ⊣ Δ
EAInr : ∀{Γ τ τ1 τ2 e d τ2' Δ} →
τ ▸sum τ1 ⊕ τ2 →
Γ ⊢ e ⇐ τ2 ~> d :: τ2' ⊣ Δ →
Γ ⊢ inr e ⇐ τ ~> inr τ1 d :: τ1 ⊕ τ2' ⊣ Δ
EACase : ∀{Γ e τ+ d Δ τ1 τ2 τ x e1 d1 τr1 Δ1 y e2 d2 τr2 Δ2} →
holes-disjoint e e1 →
holes-disjoint e e2 →
holes-disjoint e1 e2 →
Δ ## Δ1 →
Δ ## Δ2 →
Δ1 ## Δ2 →
x # Γ →
y # Γ →
Γ ⊢ e ⇒ τ+ ~> d ⊣ Δ →
τ+ ▸sum τ1 ⊕ τ2 →
(Γ ,, (x , τ1)) ⊢ e1 ⇐ τ ~> d1 :: τr1 ⊣ Δ1 →
(Γ ,, (y , τ2)) ⊢ e2 ⇐ τ ~> d2 :: τr2 ⊣ Δ2 →
Γ ⊢ case e x e1 y e2 ⇐ τ ~>
case (d ⟨ τ+ ⇒ τ1 ⊕ τ2 ⟩) x (d1 ⟨ τr1 ⇒ τ ⟩) y (d2 ⟨ τr2 ⇒ τ ⟩) :: τ
⊣ (Δ ∪ (Δ1 ∪ Δ2))
EAEHole : ∀{Γ u τ} →
Γ ⊢ ⦇-⦈[ u ] ⇐ τ ~> ⦇-⦈⟨ u , Id Γ ⟩ :: τ ⊣ ■ (u :: τ [ Γ ])
EANEHole : ∀{Γ e u τ d τ' Δ} →
Δ ## (■ (u , Γ , τ)) →
Γ ⊢ e ⇒ τ' ~> d ⊣ Δ →
Γ ⊢ ⦇⌜ e ⌟⦈[ u ] ⇐ τ ~> ⦇⌜ d ⌟⦈⟨ u , Id Γ ⟩ :: τ ⊣ (Δ ,, u :: τ [ Γ ])
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
TANum : ∀{Δ Γ n} →
Δ , Γ ⊢ (N n) :: num
TAPlus : ∀{Δ Γ d1 d2} →
Δ , Γ ⊢ d1 :: num →
Δ , Γ ⊢ d2 :: num →
Δ , Γ ⊢ (d1 ·+ d2) :: num
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 :: τ
TAInl : ∀{Δ Γ d τ1 τ2} →
Δ , Γ ⊢ d :: τ1 →
Δ , Γ ⊢ inl τ2 d :: τ1 ⊕ τ2
TAInr : ∀{Δ Γ d τ1 τ2} →
Δ , Γ ⊢ d :: τ2 →
Δ , Γ ⊢ inr τ1 d :: τ1 ⊕ τ2
TACase : ∀{Δ Γ d τ1 τ2 τ x d1 y d2} →
Δ , Γ ⊢ d :: τ1 ⊕ τ2 →
x # Γ →
Δ , (Γ ,, (x , τ1)) ⊢ d1 :: τ →
y # Γ →
Δ , (Γ ,, (y , τ2)) ⊢ d2 :: τ →
Δ , Γ ⊢ case d x d1 y d2 :: τ
TAPair : ∀{Δ Γ d1 d2 τ1 τ2} →
Δ , Γ ⊢ d1 :: τ1 →
Δ , Γ ⊢ d2 :: τ2 →
Δ , Γ ⊢ ⟨ d1 , d2 ⟩ :: τ1 ⊠ τ2
TAFst : ∀{Δ Γ d τ1 τ2} →
Δ , Γ ⊢ d :: τ1 ⊠ τ2 →
Δ , Γ ⊢ fst d :: τ1
TASnd : ∀{Δ Γ d τ1 τ2} →
Δ , Γ ⊢ d :: τ1 ⊠ τ2 →
Δ , Γ ⊢ snd d :: τ2
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 ] (N n) = N n
[ d / y ] (d1 ·+ d2) = ([ d / y ] d1) ·+ ([ d / y ] d2)
[ 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 ] (d1 ∘ d2) = ([ d / y ] d1) ∘ ([ d / y ] d2)
[ d / y ] (inl τ d') = inl τ ([ d / y ] d')
[ d / y ] (inr τ d') = inr τ ([ d / y ] d')
[ d / y ] (case d' x d1 z d2)
with natEQ x y | natEQ z y
... | Inl refl | Inl refl = case ([ d / y ] d') x d1 z d2
... | Inl refl | Inr neq = case ([ d / y ] d') x d1 z ([ d / y ] d2)
... | Inr neq | Inl refl = case ([ d / y ] d') x ([ d / y ] d1) z d2
... | Inr neq1 | Inr neq2 = case ([ d / y ] d') x ([ d / y ] d1) z ([ d / y ] d2)
[ d / y ] ⟨ d1 , d2 ⟩ = ⟨ [ d / y ] d1 , [ d / y ] d2 ⟩
[ d / y ] (fst d') = fst ([ d / y ] d')
[ d / y ] (snd d') = snd ([ d / y ] d')
[ d / y ] ⦇-⦈⟨ u , σ ⟩ = ⦇-⦈⟨ u , Subst d y σ ⟩
[ d / y ] ⦇⌜ d' ⌟⦈⟨ u , σ ⟩ = ⦇⌜ [ d / y ] d' ⌟⦈⟨ u , Subst d y σ ⟩
[ 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
VNum : ∀{n} → (N n) val
VLam : ∀{x τ d} → (·λ x ·[ τ ] d) val
VInl : ∀{d τ} → d val → (inl τ d) val
VInr : ∀{d τ} → d val → (inr τ d) val
VPair : ∀{d1 d2} → d1 val → d2 val → ⟨ d1 , d2 ⟩ val
-- boxed values
data _boxedval : (d : ihexp) → Set where
BVVal : ∀{d} →
d val →
d boxedval
BVInl : ∀{d τ} →
d boxedval →
(inl τ d) boxedval
BVInr : ∀{d τ} →
d boxedval →
(inr τ d) boxedval
BVPair : ∀{d1 d2} →
d1 boxedval →
d2 boxedval →
⟨ d1 , d2 ⟩ boxedval
BVArrCast : ∀{d τ1 τ2 τ3 τ4} →
τ1 ==> τ2 ≠ τ3 ==> τ4 →
d boxedval →
d ⟨ (τ1 ==> τ2) ⇒ (τ3 ==> τ4) ⟩ boxedval
BVSumCast : ∀{d τ1 τ2 τ3 τ4} →
τ1 ⊕ τ2 ≠ τ3 ⊕ τ4 →
d boxedval →
d ⟨ (τ1 ⊕ τ2) ⇒ (τ3 ⊕ τ4) ⟩ boxedval
BVProdCast : ∀{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
IPlus1 : ∀{d1 d2} →
d1 indet →
d2 final →
(d1 ·+ d2) indet
IPlus2 : ∀{d1 d2} →
d1 final →
d2 indet →
(d1 ·+ d2) indet
IAp : ∀{d1 d2} →
((τ1 τ2 τ3 τ4 : htyp) (d1' : ihexp) →
d1 ≠ (d1' ⟨(τ1 ==> τ2) ⇒ (τ3 ==> τ4)⟩)) →
d1 indet →
d2 final →
(d1 ∘ d2) indet
IInl : ∀{d τ} →
d indet →
(inl τ d) indet
IInr : ∀{d τ} →
d indet →
(inr τ d) indet
ICase : ∀{d x d1 y d2} →
((τ : htyp) (d' : ihexp) →
d ≠ inl τ d') →
((τ : htyp) (d' : ihexp) →
d ≠ inr τ d') →
((τ1 τ2 τ1' τ2' : htyp) (d' : ihexp) →
d ≠ (d' ⟨(τ1 ⊕ τ2) ⇒ (τ1' ⊕ τ2')⟩)) →
d indet →
(case d x d1 y d2) indet
IPair1 : ∀{d1 d2} →
d1 indet →
d2 final →
⟨ d1 , d2 ⟩ indet
IPair2 : ∀{d1 d2} →
d1 final →
d2 indet →
⟨ d1 , d2 ⟩ indet
IFst : ∀{d} →
((d1 d2 : ihexp) →
d ≠ ⟨ d1 , d2 ⟩) →
((τ1 τ2 τ1' τ2' : htyp) (d' : ihexp) →
d ≠ (d' ⟨(τ1 ⊠ τ2) ⇒ (τ1' ⊠ τ2')⟩)) →
d indet →
(fst d) indet
ISnd : ∀{d} →
((d1 d2 : ihexp) →
d ≠ ⟨ d1 , d2 ⟩) →
((τ1 τ2 τ1' τ2' : htyp) (d' : ihexp) →
d ≠ (d' ⟨(τ1 ⊠ τ2) ⇒ (τ1' ⊠ τ2')⟩)) →
d indet →
(snd d) indet
IEHole : ∀{u σ} →
⦇-⦈⟨ u , σ ⟩ indet
INEHole : ∀{d u σ} →
d final →
⦇⌜ d ⌟⦈⟨ u , σ ⟩ indet
ICastArr : ∀{d τ1 τ2 τ3 τ4} →
τ1 ==> τ2 ≠ τ3 ==> τ4 →
d indet →
d ⟨ (τ1 ==> τ2) ⇒ (τ3 ==> τ4) ⟩ indet
ICastSum : ∀{d τ1 τ2 τ3 τ4} →
τ1 ⊕ τ2 ≠ τ3 ⊕ τ4 →
d indet →
d ⟨ (τ1 ⊕ τ2) ⇒ (τ3 ⊕ τ4) ⟩ indet
ICastProd : ∀{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 → ihexp → ectx
_∘₂_ : ihexp → ectx → ectx
inl : htyp → ectx → ectx
inr : htyp → ectx → ectx
case : ectx → Nat → ihexp → Nat → ihexp → ectx
⟨_,_⟩₁ : ectx → ihexp → ectx
⟨_,_⟩₂ : ihexp → ectx → ectx
fst : ectx → ectx
snd : 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 it's either trival to prove or
-- provides no additional information
--ε is an evaluation context
data _evalctx : (ε : ectx) → Set where
ECDot : ⊙ evalctx
ECPlus1 : ∀{d ε} →
ε evalctx →
(ε ·+₁ d) evalctx
ECPlus2 : ∀{d ε} →
-- d final → -- red brackets
ε evalctx →
(d ·+₂ ε) evalctx
ECAp1 : ∀{d ε} →
ε evalctx →
(ε ∘₁ d) evalctx
ECAp2 : ∀{d ε} →
-- d final → -- red brackets
ε evalctx →
(d ∘₂ ε) evalctx
ECInl : ∀{τ ε} →
ε evalctx →
(inl τ ε) evalctx
ECInr : ∀{τ ε} →
ε evalctx →
(inr τ ε) evalctx
ECCase : ∀{ε x d1 y d2} →
ε evalctx →
(case ε x d1 y d2) evalctx
ECPair1 : ∀{d ε} →
ε evalctx →
⟨ ε , d ⟩₁ evalctx
ECPair2 : ∀{d ε} →
-- d final → -- red brackets
ε evalctx →
⟨ d , ε ⟩₂ evalctx
ECFst : ∀{ε} →
(fst ε) evalctx
ECSnd : ∀{ε} →
(snd ε) 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 ⟧
FHPlus1 : ∀{d1 d1' d2 ε} →
d1 == ε ⟦ d1' ⟧ →
(d1 ·+ d2) == (ε ·+₁ d2) ⟦ d1' ⟧
FHPlus2 : ∀{d1 d2 d2' ε} →
-- d1 final → -- red brackets
d2 == ε ⟦ d2' ⟧ →
(d1 ·+ d2) == (d1 ·+₂ ε) ⟦ d2' ⟧
FHAp1 : ∀{d1 d1' d2 ε} →
d1 == ε ⟦ d1' ⟧ →
(d1 ∘ d2) == (ε ∘₁ d2) ⟦ d1' ⟧
FHAp2 : ∀{d1 d2 d2' ε} →
-- d1 final → -- red brackets
d2 == ε ⟦ d2' ⟧ →
(d1 ∘ d2) == (d1 ∘₂ ε) ⟦ d2' ⟧
FHInl : ∀{d d' ε τ} →
d == ε ⟦ d' ⟧ →
(inl τ d) == (inl τ ε) ⟦ d' ⟧
FHInr : ∀{d d' ε τ} →
d == ε ⟦ d' ⟧ →
(inr τ d) == (inr τ ε) ⟦ d' ⟧
FHCase : ∀{d d' x d1 y d2 ε} →
d == ε ⟦ d' ⟧ →
(case d x d1 y d2) == (case ε x d1 y d2) ⟦ d' ⟧
FHPair1 : ∀{d1 d1' d2 ε} →
d1 == ε ⟦ d1' ⟧ →
⟨ d1 , d2 ⟩ == ⟨ ε , d2 ⟩₁ ⟦ d1' ⟧
FHPair2 : ∀{d1 d2 d2' ε} →
d2 == ε ⟦ d2' ⟧ →
⟨ d1 , d2 ⟩ == ⟨ d1 , ε ⟩₂ ⟦ d2' ⟧
FHFst : ∀{d d' ε} →
d == ε ⟦ d' ⟧ →
fst d == (fst ε) ⟦ d' ⟧
FHSnd : ∀{d d' ε} →
d == ε ⟦ d' ⟧ →
snd d == (snd ε) ⟦ d' ⟧
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 (⦇-⦈ ==> ⦇-⦈)
MGSum : ∀{τ1 τ2} →
(τ1 ⊕ τ2) ≠ (⦇-⦈ ⊕ ⦇-⦈) →
(τ1 ⊕ τ2) ▸gnd (⦇-⦈ ⊕ ⦇-⦈)
MGProd : ∀{τ1 τ2} →
(τ1 ⊠ τ2) ≠ (⦇-⦈ ⊠ ⦇-⦈) →
(τ1 ⊠ τ2) ▸gnd (⦇-⦈ ⊠ ⦇-⦈)
-- instruction transition judgement
data _→>_ : (d d' : ihexp) → Set where
ITPlus : ∀{n1 n2 n3} →
(n1 nat+ n2) == n3 →
((N n1) ·+ (N n2)) →> (N n3)
ITLam : ∀{x τ d1 d2} →
-- d2 final → -- red brackets
((·λ x ·[ τ ] d1) ∘ d2) →> ([ d2 / x ] d1)
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' ⟩)
ITCaseInl : ∀{d τ x d1 y d2} →
-- d final → -- red brackets,
(case (inl τ d) x d1 y d2) →> ([ d / x ] d1)
ITCaseInr : ∀{d τ x d1 y d2} →
-- d final → -- red brackets,
(case (inr τ d) x d1 y d2) →> ([ d / y ] d2)
ITCaseCast : ∀{d τ1 τ2 τ1' τ2' x d1 y d2} →
-- d final → -- red brackets
(case (d ⟨ τ1 ⊕ τ2 ⇒ τ1' ⊕ τ2' ⟩) x d1 y d2) →>
(case d x ([ (X x) ⟨ τ1 ⇒ τ1' ⟩ / x ] d1) y ([ (X y) ⟨ τ2 ⇒ τ2' ⟩ / y ] d2))
ITFstPair : ∀{d1 d2} →
-- d1 final → -- red brackets
-- d2 final → -- red brackets
fst ⟨ d1 , d2 ⟩ →> d1
ITFstCast : ∀{d τ1 τ2 τ1' τ2' } →
-- d final → -- red brackets
fst (d ⟨ τ1 ⊠ τ2 ⇒ τ1' ⊠ τ2' ⟩) →> ((fst d) ⟨ τ1 ⇒ τ1' ⟩)
ITSndPair : ∀{d1 d2} →
-- d1 final → -- red brackets
-- d2 final → -- red brackets
snd ⟨ d1 , d2 ⟩ →> d2
ITSndCast : ∀{d τ1 τ2 τ1' τ2' } →
-- d final → -- red brackets
snd (d ⟨ τ1 ⊠ τ2 ⇒ τ1' ⊠ τ2' ⟩) →> ((snd d) ⟨ τ2 ⇒ τ2' ⟩)
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 ⟩)
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''
-- those internal expressions where every cast is the identity cast and
-- there are no failed casts
data cast-id : ihexp → Set where
CINum : ∀{n} →
cast-id (N n)
CIPlus : ∀{d1 d2} →
cast-id d1 →
cast-id d2 →
cast-id (d1 ·+ d2)
CIVar : ∀{x} →
cast-id (X x)
CILam : ∀{x τ d} →
cast-id d →
cast-id (·λ x ·[ τ ] d)
CIAp : ∀{d1 d2} →
cast-id d1 →
cast-id d2 →
cast-id (d1 ∘ d2)
CIInl : ∀{τ d} →
cast-id d →
cast-id (inl τ d)
CIInr : ∀{τ d} →
cast-id d →
cast-id (inr τ d)
CICase : ∀{d x d1 y d2} →
cast-id d →
cast-id d1 →
cast-id d2 →
cast-id (case d x d1 y d2)
CIPair : ∀{d1 d2} →
cast-id d1 →
cast-id d2 →
cast-id ⟨ d1 , d2 ⟩
CIFst : ∀{d} →
cast-id d →
cast-id (fst d)
CISnd : ∀{d} →
cast-id d →
cast-id (snd d)
CIHole : ∀{u} →
cast-id (⦇-⦈⟨ u ⟩)
CINEHole : ∀{d u} →
cast-id d →
cast-id (⦇⌜ d ⌟⦈⟨ u ⟩)
CICast : ∀{d τ} →
cast-id d →
cast-id (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 internal expressions
data fresh : Nat → ihexp → Set where
FNum : ∀{x n} →
fresh x (N n)
FPlus : ∀{x d1 d2} →
fresh x d1 →
fresh x d2 →
fresh x (d1 ·+ d2)
FVar : ∀{x y} →
x ≠ y →
fresh x (X y)
FLam : ∀{x y τ d} →
x ≠ y →
fresh x d →
fresh x (·λ y ·[ τ ] d)
FAp : ∀{x d1 d2} →
fresh x d1 →
fresh x d2 →
fresh x (d1 ∘ d2)
FInl : ∀{x d τ} →
fresh x d →
fresh x (inl τ d)
FInr : ∀{x d τ} →
fresh x d →
fresh x (inr τ d)
FCase : ∀{x d y d1 z d2} →
fresh x d →
x ≠ y →
fresh x d1 →
x ≠ z →
fresh x d2 →
fresh x (case d y d1 z d2)
FPair : ∀{x d1 d2} →
fresh x d1 →
fresh x d2 →
fresh x ⟨ d1 , d2 ⟩
FFst : ∀{x d} →
fresh x d →
fresh x (fst d)
FSnd : ∀{x d} →
fresh x d →
fresh x (snd d)
FHole : ∀{x u σ} →
envfresh x σ →
fresh x (⦇-⦈⟨ u , σ ⟩)
FNEHole : ∀{x d u σ} →
envfresh x σ →
fresh x d →
fresh x (⦇⌜ d ⌟⦈⟨ u , σ ⟩)
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 ⟩)
-- 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
UBNum : ∀{x n} → unbound-in x (N n)
UBPlus : ∀{x d1 d2} →
unbound-in x d1 →
unbound-in x d2 →
unbound-in x (d1 ·+ d2)
UBVar : ∀{x y} → unbound-in x (X y)
UBLam2 : ∀{x d y τ} →
x ≠ y →
unbound-in x d →
unbound-in x (·λ y ·[ τ ] d)
UBAp : ∀{x d1 d2} →
unbound-in x d1 →
unbound-in x d2 →
unbound-in x (d1 ∘ d2)
UBInl : ∀{x d τ} →
unbound-in x d →
unbound-in x (inl τ d)
UBInr : ∀{x d τ} →
unbound-in x d →
unbound-in x (inr τ d)
UBCase : ∀{x d y d1 z d2} →
unbound-in x d →
x ≠ y →