-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemF.lean
2246 lines (2017 loc) · 85.7 KB
/
SystemF.lean
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
import Lott
import Lott.Data.List
import Lott.DSL.Elab.UniversalJudgement
namespace LottExamples.SystemF
open Lean
locally_nameless
metavar TypeVar, a, b
locally_nameless
metavar TermVar, x, y
nonterminal Type', A, B :=
| a : var
| A " → " B : arr
| "∀ " a ". " A : forall' (bind a in A)
| "(" A ")" : paren (desugar := return A)
namespace Type'
@[simp]
theorem TypeVar_open_sizeOf (A : Type') : sizeOf (A.TypeVar_open a n) = sizeOf A := by
match A with
| var (.bound _) =>
rw [TypeVar_open]
split
· case isTrue h' =>
dsimp only [sizeOf]
rw [← h', _sizeOf_1, _sizeOf_1]
dsimp only [sizeOf]
rw [default.sizeOf, default.sizeOf]
· case isFalse => rfl
| var (.free _) =>
rw [TypeVar_open]
split
· case isTrue => rw [if_neg id]
· case isFalse => rfl
| arr A' B =>
dsimp only [sizeOf]
rw [TypeVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1, ← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Type', a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this (A'.TypeVar_open _ _), this (B.TypeVar_open _ _), this A', this B]
rw [A'.TypeVar_open_sizeOf, B.TypeVar_open_sizeOf]
| forall' A' =>
dsimp only [sizeOf]
rw [TypeVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Type', a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this (A'.TypeVar_open _ _), this A', A'.TypeVar_open_sizeOf]
theorem TypeVar_open_comm (A : Type') (anea' : a ≠ a') (mnen : m ≠ n)
: (A.TypeVar_open a m).TypeVar_open a' n = (A.TypeVar_open a' n).TypeVar_open a m := by
match A with
| var (.free _) =>
rw [TypeVar_open, if_neg (nomatch ·), TypeVar_open, if_neg (nomatch ·), TypeVar_open,
if_neg (nomatch ·)]
| var (.bound _) =>
rw [TypeVar_open]
split
· case isTrue h =>
rw [← h, TypeVar_open, if_neg (nomatch ·), TypeVar_open,
if_neg (mnen <| TypeVar.bound.inj ·.symm), TypeVar_open, if_pos rfl]
· case isFalse h =>
rw [TypeVar_open]
split
· case isTrue h' => rw [TypeVar_open, if_neg (nomatch ·)]
· case isFalse h' => rw [TypeVar_open, if_neg h]
| arr A' B =>
rw [TypeVar_open, TypeVar_open, A'.TypeVar_open_comm anea' mnen, B.TypeVar_open_comm anea' mnen,
TypeVar_open, TypeVar_open]
| forall' A' =>
rw [TypeVar_open, TypeVar_open]
simp only
rw [A'.TypeVar_open_comm anea' (mnen <| Nat.succ_inj'.mp ·), ← TypeVar_open, ← TypeVar_open]
namespace TypeVarLocallyClosed
theorem weaken {A : Type'} : A.TypeVarLocallyClosed m → A.TypeVarLocallyClosed (m + n)
| var_free => var_free
| var_bound h => var_bound <| Nat.lt_add_right _ h
| arr A'lc Blc => arr A'lc.weaken Blc.weaken
| forall' A'lc => by
apply forall'
rw [Nat.add_assoc, Nat.add_comm _ 1, ← Nat.add_assoc]
exact A'lc.weaken
theorem TypeVar_open_drop {A : Type'} (h : m < n)
: (A.TypeVar_open a m).TypeVarLocallyClosed n → A.TypeVarLocallyClosed n := fun Alc => by
match A with
| .var _ =>
rw [TypeVar_open] at Alc
split at Alc
· case isTrue h' =>
rw [← h']
exact var_bound h
· case isFalse h' => exact Alc
| .arr A' B =>
rw [TypeVar_open] at Alc
let .arr A'lc Blc := Alc
exact arr (A'lc.TypeVar_open_drop h) (Blc.TypeVar_open_drop h)
| .forall' A' =>
rw [TypeVar_open] at Alc
let .forall' A'lc := Alc
exact forall' <| A'lc.TypeVar_open_drop <| Nat.add_lt_add_iff_right.mpr h
theorem TypeVar_open_eq {A : Type'} (Alc : A.TypeVarLocallyClosed m) (mlen : m ≤ n)
: A.TypeVar_open a n = A := by match A, Alc with
| var (.free _), var_free => rw [TypeVar_open, if_neg (nomatch ·)]
| var (.bound _), var_bound lt =>
have := Nat.ne_of_lt <| Nat.lt_of_lt_of_le lt mlen
rw [TypeVar_open, if_neg (this.symm <| TypeVar.bound.inj ·)]
| .arr A' B, arr A'lc Blc =>
rw [TypeVar_open, A'lc.TypeVar_open_eq mlen, Blc.TypeVar_open_eq mlen]
| .forall' A, forall' A'lc =>
rw [TypeVar_open]
simp only
rw [A'lc.TypeVar_open_eq (Nat.add_le_add_iff_right.mpr mlen)]
theorem Type'_open_eq {A : Type'} (Alc : A.TypeVarLocallyClosed m) (mlen : m ≤ n)
: A.Type'_open B n = A := by match A, Alc with
| var (.free _), var_free => rw [Type'_open, if_neg (nomatch ·)]
| var (.bound _), var_bound lt =>
have := Nat.ne_of_lt <| Nat.lt_of_lt_of_le lt mlen
rw [Type'_open, if_neg fun x => this.symm <| TypeVar.bound.inj x]
| .arr A' B, arr A'lc Blc =>
rw [Type'_open, A'lc.Type'_open_eq mlen, Blc.Type'_open_eq mlen]
| .forall' A, forall' A'lc =>
rw [Type'_open]
simp only
rw [A'lc.Type'_open_eq (Nat.add_le_add_iff_right.mpr mlen)]
theorem TypeVar_open_TypeVar_close {A : Type'} (Alc : A.TypeVarLocallyClosed n)
: (A.TypeVar_close a n).TypeVar_open a n = A := by match A with
| var (.free _) =>
rw [TypeVar_close]
split
· case isTrue h => rw [TypeVar_open, if_pos rfl, h]
· case isFalse h => rw [TypeVar_open, if_neg (nomatch ·)]
| var (.bound _) => rw [TypeVar_close, if_neg (nomatch ·), Alc.TypeVar_open_eq (Nat.le_refl _)]
| .arr A' B =>
let .arr A'lc Blc := Alc
rw [TypeVar_close, TypeVar_open, A'lc.TypeVar_open_TypeVar_close,
Blc.TypeVar_open_TypeVar_close]
| .forall' A' =>
let .forall' A'lc := Alc
rw [TypeVar_close, TypeVar_open]
simp only
rw [A'lc.TypeVar_open_TypeVar_close]
theorem TypeVar_close_Type'_open_eq_TypeVar_subst {A B : Type'} (Alc : A.TypeVarLocallyClosed n)
: (A.TypeVar_close a n).Type'_open B n = A.TypeVar_subst a B := by match A with
| .var (.free _) =>
rw [TypeVar_close]
split
· case isTrue h =>
cases h
rw [Type'_open, if_pos rfl, TypeVar_subst, if_pos rfl]
· case isFalse h => rw [Type'_open, if_neg (nomatch ·), TypeVar_subst, if_neg h]
| .var (.bound _) =>
rw [TypeVar_close, if_neg (nomatch ·), Type'_open]
split
· case isTrue h =>
cases h
let .var_bound nltn := Alc
nomatch Nat.ne_of_lt nltn
· case isFalse h => rw [TypeVar_subst, if_neg (nomatch ·)]
| .arr A' B' =>
let .arr A'lc B'lc := Alc
rw [TypeVar_close, Type'_open, A'lc.TypeVar_close_Type'_open_eq_TypeVar_subst,
B'lc.TypeVar_close_Type'_open_eq_TypeVar_subst, TypeVar_subst]
| .forall' A' =>
let .forall' A'lc := Alc
rw [TypeVar_close, Type'_open]
simp only
rw [A'lc.TypeVar_close_Type'_open_eq_TypeVar_subst, TypeVar_subst]
end TypeVarLocallyClosed
theorem Type'_open_comm (A : Type') {B A': Type'} (mnen : m ≠ n) (Blc : B.TypeVarLocallyClosed n)
(A'lc : A'.TypeVarLocallyClosed m)
: (A.Type'_open B m).Type'_open A' n = (A.Type'_open A' n).Type'_open B m := by match A with
| var (.free _) =>
rw [Type'_open, if_neg (nomatch ·), Type'_open, if_neg (nomatch ·), Type'_open,
if_neg (nomatch ·)]
| var (.bound _) =>
rw [Type'_open]
split
· case isTrue h =>
rw [← h, Blc.Type'_open_eq (Nat.le_refl _), Type'_open,
if_neg (mnen <| TypeVar.bound.inj ·.symm), Type'_open, if_pos rfl]
· case isFalse h =>
rw [Type'_open]
split
· case isTrue h' => rw [A'lc.Type'_open_eq (Nat.le_refl _)]
· case isFalse h' => rw [Type'_open, if_neg h]
| arr A' B =>
rw [Type'_open, Type'_open, A'.Type'_open_comm mnen Blc A'lc, B.Type'_open_comm mnen Blc A'lc,
← Type'_open, ← Type'_open]
| forall' A' =>
rw [Type'_open, Type'_open]
simp only
rw [A'.Type'_open_comm (mnen <| Nat.succ_inj'.mp ·) (Blc.weaken (n := 1))
(A'lc.weaken (n := 1)), ← Type'_open, ← Type'_open]
theorem TypeVar_open_Type'_open_eq {A B : Type'} (Blc : B.TypeVarLocallyClosed m) (mnen : m ≠ n)
: (A.TypeVar_open a m).Type'_open B n = (A.Type'_open B n).TypeVar_open a m := by match A with
| var (.free _) =>
rw [TypeVar_open, if_neg (nomatch ·), Type'_open, if_neg (nomatch ·), TypeVar_open,
if_neg (nomatch ·)]
| var (.bound _) =>
rw [TypeVar_open]
split
· case isTrue h =>
rw [Type'_open, if_neg (nomatch ·), Type'_open, ← h,
if_neg (mnen <| TypeVar.bound.inj ·.symm), TypeVar_open, if_pos rfl]
· case isFalse h =>
rw [Type'_open]
split
· case isTrue h' => rw [Blc.TypeVar_open_eq (Nat.le_refl _)]
· case isFalse h' => rw [TypeVar_open, if_neg h]
| arr A' B' =>
rw [TypeVar_open, Type'_open, TypeVar_open_Type'_open_eq Blc mnen,
TypeVar_open_Type'_open_eq Blc mnen, ← TypeVar_open, ← Type'_open]
| forall' A' =>
rw [TypeVar_open, Type'_open]
simp only
rw [TypeVar_open_Type'_open_eq Blc.weaken
(by exact mnen <| Nat.succ_inj'.mp ·), ← TypeVar_open, ← Type'_open]
theorem TypeVar_open_Type'_open_eq' {A B : Type'} (Blc : B.TypeVarLocallyClosed n)
: (A.TypeVar_open a n).Type'_open B n = A.TypeVar_open a n := by match A with
| var (.free _) =>
rw [TypeVar_open, if_neg (nomatch ·), Type'_open, if_neg (nomatch ·)]
| var (.bound _) =>
rw [TypeVar_open]
split
· case isTrue h => rw [Type'_open, if_neg (nomatch ·)]
· case isFalse h => rw [Type'_open, if_neg h]
| arr A' B' =>
rw [TypeVar_open, Type'_open, TypeVar_open_Type'_open_eq' Blc, TypeVar_open_Type'_open_eq' Blc,
← TypeVar_open]
| forall' A' =>
rw [TypeVar_open, Type'_open]
simp only
rw [TypeVar_open_Type'_open_eq' Blc.weaken, ← TypeVar_open]
theorem TypeVar_subst_TypeVar_open_comm {A B : Type'} (Blc : B.TypeVarLocallyClosed n)
(anea' : a ≠ a')
: (A.TypeVar_subst a B).TypeVar_open a' n = (A.TypeVar_open a' n).TypeVar_subst a B := by
match A with
| var (.free _) =>
rw [TypeVar_subst]
split
· case isTrue h =>
rw [TypeVar_open, if_neg (nomatch ·), TypeVar_subst, if_pos h,
Blc.TypeVar_open_eq <| Nat.le_refl _]
· case isFalse h => rw [TypeVar_open, if_neg (nomatch ·), TypeVar_subst, if_neg h]
| var (.bound _) =>
rw [TypeVar_subst, if_neg (nomatch ·), TypeVar_open]
split
· case isTrue h =>
rw [TypeVar_subst, if_neg fun freeaeqfreea' => anea' <| TypeVar.free.inj freeaeqfreea']
· case isFalse h => rw [TypeVar_subst, if_neg (nomatch ·)]
| arr A' B' =>
rw [TypeVar_subst, TypeVar_open, TypeVar_subst_TypeVar_open_comm Blc anea',
TypeVar_subst_TypeVar_open_comm Blc anea', ← TypeVar_subst, ← TypeVar_open]
| forall' A' =>
rw [TypeVar_subst, TypeVar_open]
simp only
rw [TypeVar_subst_TypeVar_open_comm Blc.weaken anea', ← TypeVar_subst, ← TypeVar_open]
end Type'
nonterminal Term, E, F :=
| x : var
| "λ " x " : " A ". " E : lam (bind x in E)
| E F : app
| "Λ " a ". " E : typeGen (bind a in E)
| E " [" A "]" : typeApp
| "(" E ")" : paren (desugar := return E)
namespace Term
@[simp]
theorem TermVar_open_sizeOf (E : Term) : sizeOf (E.TermVar_open x n) = sizeOf E := by match E with
| var (.bound _) =>
rw [TermVar_open]
split
· case isTrue h' =>
dsimp only [sizeOf]
rw [← h', _sizeOf_1, _sizeOf_1]
dsimp only [sizeOf]
rw [default.sizeOf, default.sizeOf]
· case isFalse => rfl
| var (.free _) =>
rw [TermVar_open]
split
· case isTrue => rw [if_neg id]
· case isFalse => rfl
| lam A E' =>
dsimp [sizeOf]
rw [TermVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _]
have : ∀ a : Type', a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, E'.TermVar_open_sizeOf]
| app E' F =>
dsimp [sizeOf]
rw [TermVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _, this _, this _, E'.TermVar_open_sizeOf,
F.TermVar_open_sizeOf]
| typeGen E' =>
dsimp [sizeOf]
rw [TermVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _, E'.TermVar_open_sizeOf]
| typeApp E' A =>
dsimp [sizeOf]
rw [TermVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _]
have : ∀ a : Type', a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, E'.TermVar_open_sizeOf]
@[simp]
theorem TypeVar_open_sizeOf (E : Term) : sizeOf (E.TypeVar_open x n) = sizeOf E := by match E with
| var _ => rw [TypeVar_open]
| lam A E' =>
dsimp [sizeOf]
rw [TypeVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _]
have : ∀ a : Type', a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, A.TypeVar_open_sizeOf, E'.TypeVar_open_sizeOf]
| app E' F =>
dsimp [sizeOf]
rw [TypeVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _, this _, this _, E'.TypeVar_open_sizeOf, F.TypeVar_open_sizeOf]
| typeGen E' =>
dsimp [sizeOf]
rw [TypeVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _, E'.TypeVar_open_sizeOf]
| typeApp E' A =>
dsimp [sizeOf]
rw [TypeVar_open, _sizeOf_1, _sizeOf_1]
simp only
rw [← _sizeOf_1, ← _sizeOf_1]
have : ∀ a : Term, a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, this _]
have : ∀ a : Type', a._sizeOf_1 = sizeOf a := fun _ => by dsimp only [sizeOf]
rw [this _, E'.TypeVar_open_sizeOf, A.TypeVar_open_sizeOf]
theorem TermVar_open_comm (E : Term) (xnex' : x ≠ x') (mnen : m ≠ n)
: (E.TermVar_open x m).TermVar_open x' n = (E.TermVar_open x' n).TermVar_open x m := by
match E with
| var (.free _) =>
rw [TermVar_open, if_neg (nomatch ·), TermVar_open, if_neg (nomatch ·), TermVar_open,
if_neg (nomatch ·)]
| var (.bound _) =>
rw [TermVar_open]
split
· case isTrue h =>
rw [← h, TermVar_open, if_neg (nomatch ·), TermVar_open,
if_neg (mnen <| TermVar.bound.inj ·.symm), TermVar_open, if_pos rfl]
· case isFalse h =>
rw [TermVar_open]
split
· case isTrue h' => rw [TermVar_open, if_neg (nomatch ·)]
· case isFalse h' => rw [TermVar_open, if_neg h]
| lam A E' =>
rw [TermVar_open, TermVar_open]
simp only
rw [E'.TermVar_open_comm xnex' (mnen <| Nat.succ_inj'.mp ·), ← TermVar_open, ← TermVar_open]
| app E' F =>
rw [TermVar_open, TermVar_open, E'.TermVar_open_comm xnex' mnen, F.TermVar_open_comm xnex' mnen,
← TermVar_open, ← TermVar_open]
| typeGen E' =>
rw [TermVar_open, TermVar_open, E'.TermVar_open_comm xnex' mnen, ← TermVar_open, ← TermVar_open]
| typeApp E' A =>
rw [TermVar_open, TermVar_open, E'.TermVar_open_comm xnex' mnen, ← TermVar_open, ← TermVar_open]
theorem TypeVar_open_comm (E : Term) (anea' : a ≠ a') (mnen : m ≠ n)
: (E.TypeVar_open a m).TypeVar_open a' n = (E.TypeVar_open a' n).TypeVar_open a m := by
match E with
| var _ => rw [TypeVar_open, TypeVar_open, TypeVar_open]
| lam A E' =>
rw [TypeVar_open, TypeVar_open, A.TypeVar_open_comm anea' mnen, E'.TypeVar_open_comm anea' mnen,
← TypeVar_open, ← TypeVar_open]
| app E' F =>
rw [TypeVar_open, TypeVar_open, E'.TypeVar_open_comm anea' mnen, F.TypeVar_open_comm anea' mnen,
← TypeVar_open, ← TypeVar_open]
| typeGen E' =>
rw [TypeVar_open, TypeVar_open]
simp only
rw [E'.TypeVar_open_comm anea' (mnen <| Nat.succ_inj'.mp ·), ← TypeVar_open, ← TypeVar_open]
| typeApp E' A =>
rw [TypeVar_open, TypeVar_open, E'.TypeVar_open_comm anea' mnen, A.TypeVar_open_comm anea' mnen,
← TypeVar_open, ← TypeVar_open]
theorem TermVar_open_Type'_open_comm (E : Term)
: (E.TermVar_open x m).Type'_open A n = (E.Type'_open A n).TermVar_open x m := by match E with
| var _ =>
rw [TermVar_open]
split
· case isTrue h => rw [Type'_open, Type'_open, TermVar_open, if_pos h]
· case isFalse h => rw [Type'_open, TermVar_open, if_neg h]
| lam A' E' =>
rw [TermVar_open, Type'_open, E'.TermVar_open_Type'_open_comm, ← TermVar_open, ← Type'_open]
| app E' F =>
rw [TermVar_open, Type'_open, E'.TermVar_open_Type'_open_comm, F.TermVar_open_Type'_open_comm,
← TermVar_open, ← Type'_open]
| typeGen E' =>
rw [TermVar_open, Type'_open]
simp only
rw [E'.TermVar_open_Type'_open_comm, ← TermVar_open, ← Type'_open]
| typeApp E' A' =>
rw [TermVar_open, Type'_open, E'.TermVar_open_Type'_open_comm, ← TermVar_open, ← Type'_open]
namespace TermVarLocallyClosed
theorem weaken {E : Term} : E.TermVarLocallyClosed m → E.TermVarLocallyClosed (m + n)
| var_free => var_free
| var_bound h => var_bound <| Nat.lt_add_right _ h
| lam E'lc => by
apply lam
rw [Nat.add_assoc, Nat.add_comm _ 1, ← Nat.add_assoc]
exact E'lc.weaken
| app E'lc Flc => app E'lc.weaken Flc.weaken
| typeGen E'lc => typeGen E'lc.weaken
| typeApp E'lc => typeApp E'lc.weaken
theorem TermVar_open_drop {E : Term} (h : m < n)
: (E.TermVar_open x m).TermVarLocallyClosed n → E.TermVarLocallyClosed n := fun Elc => by
match E with
| .var _ =>
rw [TermVar_open] at Elc
split at Elc
· case isTrue h' =>
rw [← h']
exact var_bound h
· case isFalse h' => exact Elc
| .lam _ E' =>
rw [TermVar_open] at Elc
let .lam E'lc := Elc
exact lam <| E'lc.TermVar_open_drop <| Nat.add_lt_add_iff_right.mpr h
| .app E' F =>
rw [TermVar_open] at Elc
let .app E'lc Flc := Elc
exact app (E'lc.TermVar_open_drop h) (Flc.TermVar_open_drop h)
| .typeGen E' =>
rw [TermVar_open] at Elc
let .typeGen E'lc := Elc
exact typeGen <| E'lc.TermVar_open_drop h
| .typeApp E' A =>
rw [TermVar_open] at Elc
let .typeApp E'lc := Elc
exact typeApp <| E'lc.TermVar_open_drop h
theorem TypeVar_open_drop {E : Term}
: (E.TypeVar_open a m).TermVarLocallyClosed n → E.TermVarLocallyClosed n := fun Elc => by
match E with
| .var _ =>
rw [TypeVar_open] at Elc
exact Elc
| .lam _ E' =>
rw [TypeVar_open] at Elc
let .lam E'lc := Elc
exact lam E'lc.TypeVar_open_drop
| .app E' F =>
rw [TypeVar_open] at Elc
let .app E'lc Flc := Elc
exact app E'lc.TypeVar_open_drop Flc.TypeVar_open_drop
| .typeGen E' =>
rw [TypeVar_open] at Elc
let .typeGen E'lc := Elc
exact typeGen E'lc.TypeVar_open_drop
| .typeApp E' A =>
rw [TypeVar_open] at Elc
let .typeApp E'lc := Elc
exact typeApp E'lc.TypeVar_open_drop
theorem TermVar_open_eq {E : Term} (h : E.TermVarLocallyClosed m) (mlen : m ≤ n)
: E.TermVar_open x n = E := by match E, h with
| var (.free _), var_free => rw [TermVar_open, if_neg (nomatch ·)]
| var (.bound _), var_bound lt =>
have := Nat.ne_of_lt <| Nat.lt_of_lt_of_le lt mlen
rw [TermVar_open, if_neg fun x => this.symm <| TermVar.bound.inj x]
| .lam A E', lam E'lc =>
rw [TermVar_open]
simp only
rw [E'lc.TermVar_open_eq (Nat.add_le_add_iff_right.mpr mlen)]
| .app E' F, app E'lc Flc =>
rw [TermVar_open, E'lc.TermVar_open_eq mlen, Flc.TermVar_open_eq mlen]
| .typeGen E', typeGen E'lc => rw [TermVar_open, E'lc.TermVar_open_eq mlen]
| .typeApp E' A, typeApp E'lc => rw [TermVar_open, E'lc.TermVar_open_eq mlen]
end TermVarLocallyClosed
theorem TermVar_open_Term_open_eq {E F : Term} (Flc : F.TermVarLocallyClosed m) (mnen : m ≠ n)
: (E.TermVar_open x m).Term_open F n = (E.Term_open F n).TermVar_open x m := by match E with
| var (.free _) =>
rw [TermVar_open, if_neg (nomatch ·), Term_open, if_neg (nomatch ·), TermVar_open,
if_neg (nomatch ·)]
| var (.bound _) =>
rw [TermVar_open]
split
· case isTrue h =>
rw [Term_open, if_neg (nomatch ·), Term_open, ← h, if_neg (mnen <| TermVar.bound.inj ·.symm),
TermVar_open, if_pos rfl]
· case isFalse h =>
rw [Term_open]
split
· case isTrue h' => rw [Flc.TermVar_open_eq (Nat.le_refl _)]
· case isFalse h' => rw [TermVar_open, if_neg h]
| lam A E' =>
rw [TermVar_open, Term_open]
simp only
rw [TermVar_open_Term_open_eq Flc.weaken (by exact mnen <| Nat.succ_inj'.mp ·), ← TermVar_open,
← Term_open]
| app E' F =>
rw [TermVar_open, Term_open, TermVar_open_Term_open_eq Flc mnen,
TermVar_open_Term_open_eq Flc mnen, ← TermVar_open, ← Term_open]
| typeGen E' =>
rw [TermVar_open, Term_open, TermVar_open_Term_open_eq Flc mnen, ← TermVar_open, ← Term_open]
| typeApp E' A =>
rw [TermVar_open, Term_open, TermVar_open_Term_open_eq Flc mnen, ← TermVar_open, ← Term_open]
theorem TermVar_open_TypeVar_open_eq (E : Term)
: (E.TermVar_open x m).TypeVar_open a n = (E.TypeVar_open a n).TermVar_open x m := by match E with
| var (.free _) =>
rw [TermVar_open, if_neg (nomatch ·), TypeVar_open, TermVar_open, if_neg (nomatch ·)]
| var (.bound _) =>
rw [TermVar_open]
split
· case isTrue h => rw [← h, TypeVar_open, TypeVar_open, TermVar_open, if_pos rfl]
· case isFalse h => rw [TypeVar_open, TermVar_open, if_neg h]
| lam A E' =>
rw [TermVar_open, TypeVar_open, TermVar_open_TypeVar_open_eq, ← TermVar_open, ← TypeVar_open]
| app E' F =>
rw [TermVar_open, TypeVar_open, TermVar_open_TypeVar_open_eq, TermVar_open_TypeVar_open_eq,
← TermVar_open, ← TypeVar_open]
| typeGen E' =>
rw [TermVar_open, TypeVar_open]
simp only
rw [TermVar_open_TypeVar_open_eq, ← TermVar_open, ← TypeVar_open]
| typeApp E' A =>
rw [TermVar_open, TypeVar_open, TermVar_open_TypeVar_open_eq, ← TermVar_open, ← TypeVar_open]
theorem TypeVar_open_Type'_open_eq {E : Term} {A : Type'} (Alc : A.TypeVarLocallyClosed m)
(mnen : m ≠ n) : (E.TypeVar_open a m).Type'_open A n = (E.Type'_open A n).TypeVar_open a m := by
match E with
| var _ => rw [TypeVar_open, Type'_open, TypeVar_open]
| lam A E' =>
rw [TypeVar_open, Type'_open, TypeVar_open_Type'_open_eq Alc mnen,
Type'.TypeVar_open_Type'_open_eq Alc mnen, ← TypeVar_open, ← Type'_open]
| app E' F =>
rw [TypeVar_open, Type'_open, TypeVar_open_Type'_open_eq Alc mnen,
TypeVar_open_Type'_open_eq Alc mnen, ← TypeVar_open, ← Type'_open]
| typeGen E' =>
rw [TypeVar_open, Type'_open]
simp only
rw [TypeVar_open_Type'_open_eq Alc.weaken (by exact mnen <| Nat.succ_inj'.mp ·), ← TypeVar_open,
← Type'_open]
| typeApp E' A =>
rw [TypeVar_open, Type'_open, TypeVar_open_Type'_open_eq Alc mnen,
Type'.TypeVar_open_Type'_open_eq Alc mnen, ← TypeVar_open, ← Type'_open]
namespace TypeVarLocallyClosed
theorem weaken {E : Term} : E.TypeVarLocallyClosed m → E.TypeVarLocallyClosed (m + n)
| var => var
| lam Alc E'lc => lam Alc.weaken E'lc.weaken
| app E'lc Flc => app E'lc.weaken Flc.weaken
| typeGen E'lc => by
apply typeGen
rw [Nat.add_assoc, Nat.add_comm _ 1, ← Nat.add_assoc]
exact E'lc.weaken
| typeApp E'lc Alc => .typeApp E'lc.weaken Alc.weaken
theorem TermVar_open_drop {E : Term}
: (E.TermVar_open x m).TypeVarLocallyClosed n → E.TypeVarLocallyClosed n := fun Elc => by
match E with
| .var _ => exact var
| .lam A' E' =>
rw [TermVar_open] at Elc
let .lam A'lc E'lc := Elc
exact lam A'lc E'lc.TermVar_open_drop
| .app E' F =>
rw [TermVar_open] at Elc
let .app E'lc Flc := Elc
exact app E'lc.TermVar_open_drop Flc.TermVar_open_drop
| .typeGen E' =>
rw [TermVar_open] at Elc
let .typeGen E'lc := Elc
exact typeGen E'lc.TermVar_open_drop
| .typeApp E' A =>
rw [TermVar_open] at Elc
let .typeApp E'lc A'lc := Elc
exact typeApp E'lc.TermVar_open_drop A'lc
theorem TypeVar_open_drop {E : Term} (h : m < n)
: (E.TypeVar_open a m).TypeVarLocallyClosed n → E.TypeVarLocallyClosed n := fun Elc => by
match E with
| .var _ => exact var
| .lam A' E' =>
rw [TypeVar_open] at Elc
let .lam A'lc E'lc := Elc
exact lam (A'lc.TypeVar_open_drop h) (E'lc.TypeVar_open_drop h)
| .app E' F =>
rw [TypeVar_open] at Elc
let .app E'lc Flc := Elc
exact app (E'lc.TypeVar_open_drop h) (Flc.TypeVar_open_drop h)
| .typeGen E' =>
rw [TypeVar_open] at Elc
let .typeGen E'lc := Elc
exact typeGen <| E'lc.TypeVar_open_drop <| Nat.add_lt_add_iff_right.mpr h
| .typeApp E' A =>
rw [TypeVar_open] at Elc
let .typeApp E'lc A'lc := Elc
exact typeApp (E'lc.TypeVar_open_drop h) (A'lc.TypeVar_open_drop h)
theorem TypeVar_open_eq {E : Term} (h : E.TypeVarLocallyClosed m) (mlen : m ≤ n)
: E.TypeVar_open x n = E := by match E, h with
| .var _, var => rw [TypeVar_open]
| .lam A E', lam Alc E'lc =>
rw [TypeVar_open, Alc.TypeVar_open_eq mlen, E'lc.TypeVar_open_eq mlen]
| .app E' F, app E'lc Flc =>
rw [TypeVar_open, E'lc.TypeVar_open_eq mlen, Flc.TypeVar_open_eq mlen]
| .typeGen E', typeGen E'lc =>
rw [TypeVar_open]
simp only
rw [E'lc.TypeVar_open_eq (Nat.add_le_add_iff_right.mpr mlen)]
| .typeApp E' A, typeApp E'lc Alc =>
rw [TypeVar_open, E'lc.TypeVar_open_eq mlen, Alc.TypeVar_open_eq mlen]
end TypeVarLocallyClosed
theorem TypeVar_open_Term_open_eq {E F : Term} (Flc : F.TypeVarLocallyClosed n)
: (E.TypeVar_open x n).Term_open F m = (E.Term_open F m).TypeVar_open x n := by match E with
| var (.free _) => rw [TypeVar_open, Term_open, if_neg (nomatch ·), TypeVar_open]
| var (.bound _) =>
rw [TypeVar_open, Term_open]
split
· case isTrue h => rw [Flc.TypeVar_open_eq (Nat.le_refl _)]
· case isFalse h => rw [TypeVar_open]
| lam A E' =>
rw [TypeVar_open, Term_open]
simp only
rw [TypeVar_open_Term_open_eq Flc, Term_open, TypeVar_open]
| app E' F =>
rw [TypeVar_open, Term_open, TypeVar_open_Term_open_eq Flc, TypeVar_open_Term_open_eq Flc,
← TypeVar_open, ← Term_open]
| typeGen E' =>
rw [TypeVar_open, Term_open, TypeVar_open_Term_open_eq Flc.weaken, ← TypeVar_open, Term_open]
| typeApp E' A =>
rw [TypeVar_open, Term_open, TypeVar_open_Term_open_eq Flc, ← TypeVar_open, Term_open]
end Term
private
def Environment.appendExpr : Expr := .const `LottExamples.SystemF.Environment.append []
private
def Environment.TypeVar_substExpr : Expr :=
.const `LottExamples.SystemF.Environment.TypeVar_subst []
nosubst
nonterminal Environment, G, D :=
| "ε" : empty
| G ", " x " : " A : termVarExt (id x)
| G ", " a : typeVarExt (id a)
| G ", " D : append (elab := return mkApp2 Environment.appendExpr G D)
| "(" G ")" : paren (desugar := return G)
| G " [" A " / " a "]" : subst (id a) (elab := return mkApp3 Environment.TypeVar_substExpr G a A)
namespace Environment
def append (G : Environment) : Environment → Environment
| empty => G
| termVarExt G' x A => G.append G' |>.termVarExt x A
| typeVarExt G' a => G.append G' |>.typeVarExt a
theorem append_termVarExt : [[(G, G', x : A)]] = [[((G, G'), x : A)]] := rfl
theorem append_typeVarExt : [[(G, G', a)]] = [[((G, G'), a)]] := rfl
theorem empty_append (G : Environment) : empty.append G = G := match G with
| empty => rfl
| termVarExt G' x A => by rw [append_termVarExt, empty_append G']
| typeVarExt G' a => by rw [append_typeVarExt, empty_append G']
theorem append_empty (G : Environment) : G.append empty = G := by match G with
| empty => rfl
| termVarExt G' x A => rw [append]
| typeVarExt G' a => rw [append]
theorem append_assoc : [[(G, G', G'')]] = [[((G, G'), G'')]] := match G'' with
| empty => rfl
| termVarExt G''' x A => by
rw [append_termVarExt, append_termVarExt, G.append_assoc, append_termVarExt]
| typeVarExt G''' a => by
rw [append_typeVarExt, append_typeVarExt, G.append_assoc, append_typeVarExt]
def TypeVar_subst (G : Environment) (a : TypeVarId) (A : Type') := match G with
| empty => empty
| termVarExt G' x A' => G'.TypeVar_subst a A |>.termVarExt x <| A'.TypeVar_subst a A
| typeVarExt G' a' => G'.TypeVar_subst a A |>.typeVarExt a'
def termVar_count : Environment → Nat
| empty => 0
| termVarExt G _ _ => 1 + G.termVar_count
| typeVarExt G _ => G.termVar_count
def typeVar_count : Environment → Nat
| empty => 0
| termVarExt G _ _ => G.typeVar_count
| typeVarExt G _ => 1 + G.typeVar_count
def termVar_domain : Environment → List TermVarId
| empty => []
| termVarExt G x _ => x :: G.termVar_domain
| typeVarExt G _ => G.termVar_domain
theorem termVar_domain_append
: [[(G, G')]].termVar_domain = G'.termVar_domain ++ G.termVar_domain := by match G' with
| empty => rw [termVar_domain, append_empty, List.nil_append]
| termVarExt G'' x A =>
rw [append_termVarExt, termVar_domain, termVar_domain, termVar_domain_append, List.cons_append]
| typeVarExt G'' a =>
rw [append_typeVarExt, termVar_domain, termVar_domain, termVar_domain_append]
def typeVar_domain : Environment → List TypeVarId
| empty => []
| termVarExt G _ _ => G.typeVar_domain
| typeVarExt G a => a :: G.typeVar_domain
theorem typeVar_domain_append
: [[(G, G')]].typeVar_domain = G'.typeVar_domain ++ G.typeVar_domain := by match G' with
| empty => rw [typeVar_domain, append_empty, List.nil_append]
| termVarExt G'' x A =>
rw [append_termVarExt, typeVar_domain, typeVar_domain, typeVar_domain_append]
| typeVarExt G'' a =>
rw [append_typeVarExt, typeVar_domain, typeVar_domain, typeVar_domain_append, List.cons_append]
end Environment
nonterminal (parent := Term) Value, V :=
| "λ " x " : " A ". " E : lam (bind x in E)
| "Λ " a ". " E : typeGen (bind a in E)
judgement_syntax a " ≠ " b : TypeVarNe (id a, b)
def TypeVarNe := Ne (α := TypeVarId)
judgement_syntax a " ∈ " G : TypeVarInEnvironment (id a)
judgement TypeVarInEnvironment :=
──────── head
a ∈ G, a
a ∈ G
──────────── termVarExt
a ∈ G, x : A
a ∈ G
a ≠ a'
───────── typeVarExt
a ∈ G, a'
judgement_syntax a " ∉ " G : TypeVarNotInEnvironment (id a)
def TypeVarNotInEnvironment a G := ¬[[a ∈ G]]
namespace TypeVarInEnvironment
theorem append_elim (ainGappGG : [[a ∈ G, GG]]) : [[a ∈ G]] ∨ [[a ∈ GG]] := by
by_cases [[a ∈ GG]]
· case pos ainGG => exact .inr ainGG
· case neg aninGG =>
left
induction GG
· case empty => exact ainGappGG
· case termVarExt GG' x A ih =>
apply ih
· cases ainGappGG
case ainGappGG.termVarExt => assumption
· intro ainGG'
exact aninGG ainGG'.termVarExt
· case typeVarExt GG' a' ih =>
by_cases a' = a
· case pos a'eqa =>
apply False.elim
apply aninGG
rw [a'eqa]
exact head
· case neg a'nea =>
apply ih
· cases ainGappGG
· case ainGappGG.head => contradiction
· case ainGappGG.typeVarExt => assumption
· intro ainGG'
apply aninGG
have a'nea : a' ≠ a := a'nea
exact ainGG'.typeVarExt a'nea.symm
theorem append_inl (ainG : [[a ∈ G]]) : [[a ∈ G, GG]] := by
match GG with
| .empty => exact ainG
| .termVarExt GG' x A => exact ainG.append_inl |>.termVarExt
| .typeVarExt GG' a' =>
by_cases a' = a
· case pos a'eqa =>
rw [a'eqa]
exact .head
· case neg a'nea =>
have a'nea : a' ≠ a := a'nea
exact .typeVarExt ainG.append_inl a'nea.symm
theorem append_inr : [[a ∈ GG]] → [[a ∈ G, GG]]
| head => head
| termVarExt ainGG' => ainGG'.append_inr |>.termVarExt
| typeVarExt ainGG' anea' => ainGG'.append_inr |>.typeVarExt anea'
theorem TypeVar_subst : [[a ∈ G]] → [[a ∈ G [A / a'] ]]
| termVarExt ainG' => by
rw [Environment.TypeVar_subst]
exact termVarExt <| ainG'.TypeVar_subst
| typeVarExt ainG' anea'' => by
rw [Environment.TypeVar_subst]
exact typeVarExt (ainG'.TypeVar_subst) anea''
| head => by
rw [Environment.TypeVar_subst]
exact head
end TypeVarInEnvironment
namespace TypeVarNotInEnvironment
theorem termVarExt : [[a ∉ G]] → [[a ∉ G, x : A]]
| aninG, .termVarExt ainG => aninG ainG
theorem typeVarExt (h : a ≠ a') : [[a ∉ G]] → [[a ∉ G, a']]
| _, .head => h rfl
| aninG, .typeVarExt ainG _ => aninG ainG
end TypeVarNotInEnvironment
judgement_syntax x " ≠ " y : TermVarNe (id x, y)
def TermVarNe := Ne (α := TermVarId)
judgement_syntax x " : " A " ∈ " G : TermVarInEnvironment (id x)
judgement TermVarInEnvironment :=
──────────────── head
x : A ∈ G, x : A
x : A ∈ G
x ≠ x'
───────────────── termVarExt
x : A ∈ G, x' : B
x : A ∈ G
──────────── typeVarExt
x : A ∈ G, a
judgement_syntax x " ∉ " G : TermVarNotInEnvironment (id x)
def TermVarNotInEnvironment x G := ∀ A : Type', ¬[[x : A ∈ G]]
namespace TermVarInEnvironment
theorem append_elim (xAinGappGG : [[x : A ∈ G, GG]])
: [[x : A ∈ G]] ∧ [[x ∉ GG]] ∨ [[x : A ∈ GG]] := by
by_cases [[x : A ∈ GG]]
· case pos xAinGG => exact .inr xAinGG
· case neg xAninGG =>
left
match GG with
| .empty =>
constructor
· exact xAinGappGG
· intro A'
intro xA'inε
nomatch xA'inε
| .termVarExt GG' x' A' =>
by_cases x' = x
· case pos x'eqx =>
by_cases A' = A
· case pos A'eqA =>
rw [x'eqx, A'eqA] at xAinGappGG xAninGG
exact xAninGG head |>.elim
· case neg A'neA =>
cases xAinGappGG
· case head => contradiction
· case termVarExt xAinGappGG' xnex' =>
exact xnex' x'eqx.symm |>.elim
· case neg x'nex =>
cases xAinGappGG
· case head => contradiction
· case termVarExt xAinGappGG' xnex' =>
rcases xAinGappGG'.append_elim with ⟨xAinG, xninGG'⟩ | xAinG''
· constructor
· exact xAinG
· intro A''
intro xA''inGG'x'A'
match xA''inGG'x'A' with
| head => contradiction
| termVarExt xA''inG' _ => exact xninGG' A'' xA''inG'
· have x'nex : x' ≠ x := x'nex
exact False.elim <| xAninGG <| xAinG''.termVarExt x'nex.symm
| .typeVarExt GG' a =>
cases xAinGappGG
case typeVarExt xAinGappG'' =>
match xAinGappG''.append_elim with
| .inl ⟨xAinG, xninGG'⟩ =>
constructor
· exact xAinG
· intro A' xA'inGG'a
apply xninGG' A'
cases xA'inGG'a
case right.typeVarExt xA'inGG' =>
exact xA'inGG'
| .inr xAinGG' =>
exact xAninGG xAinGG'.typeVarExt |>.elim
theorem append_inr : [[x : A ∈ GG]] → [[x : A ∈ G, GG]]
| head => head
| termVarExt xAinGG' xnex' => xAinGG'.append_inr |>.termVarExt xnex'
| typeVarExt xAinGG' => xAinGG'.append_inr |>.typeVarExt
end TermVarInEnvironment
namespace TermVarNotInEnvironment
theorem termVarExt (xnex' : x ≠ x') : [[x ∉ G]] → [[x ∉ G, x' : A]] := fun xnin A xAinGx'A => by
apply xnin A
cases xAinGx'A
· case head => contradiction
· case termVarExt h _ => exact h
theorem typeVarExt : [[x ∉ G]] → [[x ∉ G, a]] := fun xnin A xAinGa => by
apply xnin A
cases xAinGa
case typeVarExt h =>
exact h
end TermVarNotInEnvironment
judgement_syntax a " ∈ " "ftv" "(" A ")" : Type'.InFreeTypeVars (id a)
namespace Type'.InFreeTypeVars
theorem of_TypeVar_open {A : Type'} (h : a ≠ a')
: InFreeTypeVars a (A.TypeVar_open a' n) → [[a ∈ ftv(A)]] := fun ainAop => by
match A with
| .var (.free _) =>
rw [Type'.TypeVar_open] at ainAop
let .var := ainAop
exact .var
| .var (.bound _) =>
rw [Type'.TypeVar_open] at ainAop
split at ainAop
· case isTrue h =>
cases ainAop
contradiction
· case isFalse h =>
nomatch ainAop
| .arr A' B =>
rw [Type'.TypeVar_open] at ainAop
match ainAop with
| .arr₀ ainA'op => exact .arr₀ <| ainA'op.of_TypeVar_open h
| .arr₁ ainA'op => exact .arr₁ <| ainA'op.of_TypeVar_open h
| .forall' A' =>
rw [Type'.TypeVar_open] at ainAop
let .forall' ainA'op := ainAop