-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemF.lean
1477 lines (1313 loc) · 65.1 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
inductive Tp.Ctx
| nil
| cons (Δ : Ctx)
inductive Tp.Var : (Δ : Ctx) → Type
| here : Var (.cons Δ)
| there (α : Var Δ) : Var Δ.cons
deriving DecidableEq
@[elab_as_elim]
def Tp.Var.cases {motive : (α : Var (.cons Δ)) → Sort u} (here : motive here) (there : ∀ α, motive (there α)) : ∀ α, motive α
| .here => here
| .there α => there α
@[simp] theorem Tp.Var.cases_here {here there} : @cases Δ motive here there .here = here := rfl
@[simp] theorem Tp.Var.cases_there {here there α} : @cases Δ motive here there (.there α) = there α := rfl
@[simp]
theorem Tp.Var.cases_commute {here there} (f : (b : β) → γ) : (α : Var (.cons Δ)) → f (cases here there α) = cases (f here) (fun α => f (there α)) α
| .here | .there _ => rfl
inductive Tp : (Δ : Tp.Ctx) → Type
| var (α : Tp.Var Δ) : Tp Δ
| arr (τ₁ τ₂ : Tp Δ) : Tp Δ
| all (τ : Tp Δ.cons) : Tp Δ
deriving DecidableEq
def Tp.Rename (Δ Δ' : Ctx) : Type :=
(α : Var Δ) → Var Δ'
@[simp]
def Tp.Rename.weaken (δ : Rename Δ Δ') : Rename Δ.cons Δ'.cons :=
Var.cases .here fun α => (δ α).there
@[simp]
def Tp.rename (δ : Rename Δ Δ') : (τ : Tp Δ) → Tp Δ'
| var α => var (δ α)
| arr τ₁ τ₂ => arr (τ₁.rename δ) (τ₂.rename δ)
| all τ => all (τ.rename δ.weaken)
def Tp.Subst (Δ Δ' : Ctx) : Type :=
(α : Var Δ) → Tp Δ'
@[simp]
def Tp.Subst.mk (τ : Tp Δ) : Subst Δ.cons Δ :=
Var.cases τ var
@[simp]
def Tp.Subst.weaken (δ : Subst Δ Δ') : Subst Δ.cons Δ'.cons :=
Var.cases (var .here) fun α => (δ α).rename .there
@[simp]
def Tp.subst (δ : Subst Δ Δ') : (τ : Tp Δ) → Tp Δ'
| var α => δ α
| arr τ₁ τ₂ => arr (τ₁.subst δ) (τ₂.subst δ)
| all τ => all (τ.subst δ.weaken)
@[simp]
theorem Tp.rename_id : rename (Δ := Δ) (·) = (·) := by
funext τ
generalize h : (·) = δ
replace h α := congrFun h.symm α
induction τ with simp [*]
| all _ ih => apply ih; apply Var.cases <;> simp [h]
@[simp]
theorem Tp.subst_var : subst (Δ := Δ) var = (·) := by
funext τ
generalize h : var = δ
replace h α := congrFun h.symm α
induction τ with simp [*]
| all _ ih => apply ih; apply Var.cases <;> simp [h]
@[simp]
theorem Tp.rename_rename {δ₁ : Rename Δ Δ'} {δ₂ : Rename Δ' Δ''} {τ} : rename δ₂ (rename δ₁ τ) = rename (fun α => δ₂ (δ₁ α)) τ := by
generalize h : (fun α => δ₂ (δ₁ α)) = δ
replace h := congrFun h
induction τ generalizing Δ' Δ'' with simp [*]
| all _ ih => apply ih; apply Var.cases <;> simp [h]
@[simp]
theorem Tp.subst_rename {δ₁ : Rename Δ Δ'} {δ₂ : Subst Δ' Δ''} {τ} : subst δ₂ (rename δ₁ τ) = subst (fun α => δ₂ (δ₁ α)) τ := by
generalize h : (fun α => δ₂ (δ₁ α)) = δ
replace h := congrFun h
induction τ generalizing Δ' Δ'' with simp [*]
| all _ ih => apply ih; apply Var.cases <;> simp [h]
@[simp]
theorem Tp.rename_subst {δ₁ : Subst Δ Δ'} {δ₂ : Rename Δ' Δ''} {τ} : rename δ₂ (subst δ₁ τ) = subst (fun α => (δ₁ α).rename δ₂) τ := by
generalize h : (fun α => (δ₁ α).rename δ₂) = δ
replace h := congrFun h
induction τ generalizing Δ' Δ'' with simp [*]
| all _ ih => apply ih; apply Var.cases <;> simp [← h]
@[simp]
theorem Tp.subst_subst {δ₁ : Subst Δ Δ'} {δ₂ : Subst Δ' Δ''} {τ} : subst δ₂ (subst δ₁ τ) = subst (fun α => (δ₁ α).subst δ₂) τ := by
generalize h : (fun α => (δ₁ α).subst δ₂) = δ
replace h := congrFun h
induction τ generalizing Δ' Δ'' with simp [*]
| all _ ih => apply ih; apply Var.cases <;> simp [← h]
example (τ : Tp Δ) : Tp Δ.cons := τ.rename .there
example (τ : Tp Δ.cons) (τ' : Tp Δ) : Tp Δ := τ.subst (.mk τ')
inductive Tm.Ctx (Δ : Tp.Ctx)
| nil
| cons (Γ : Ctx Δ) (τ : Tp Δ)
@[simp]
def Tm.Ctx.map (δ : (τ : Tp Δ) → Tp Δ') : (Γ : Ctx Δ) → Ctx Δ'
| nil => nil
| cons Γ τ => cons (Γ.map δ) (δ τ)
@[simp]
theorem Tm.Ctx.map_id (h : ∀ τ, δ τ = τ) : map δ Γ = Γ :=
by induction Γ with simp [*]
@[simp]
theorem Tm.Ctx.map_map : map δ₂ (map δ₁ Γ) = map (fun τ => δ₂ (δ₁ τ)) Γ :=
by induction Γ with simp [*]
inductive Tm.Var Δ (τ : Tp Δ) : (Γ : Ctx Δ) → Type
| here : Var Δ τ (.cons Γ τ)
| there (x : Var Δ τ Γ) : Var Δ τ (Γ.cons τ')
deriving DecidableEq
@[elab_as_elim]
def Tm.Var.cases {motive : ∀ τ, (x : Var Δ τ (.cons Γ τ')) → Sort u} (here : motive τ' here) (there : ∀ {τ} x, motive τ (there x)) : ∀ {τ} x, motive τ x
| _, .here => here
| _, .there x => there x
@[simp] theorem Tm.Var.cases_here {here there} : @cases Δ Γ τ' motive here there τ' .here = here := rfl
@[simp] theorem Tm.Var.cases_there {here there x} : @cases Δ Γ τ' motive here there τ (.there x) = there x := rfl
@[simp]
def Tm.Var.map (δ : (τ : Tp Δ) → Tp Δ') {τ Γ} : (x : Var Δ τ Γ) → Var Δ' (δ τ) (Γ.map δ)
| here => here
| there x => there (x.map δ)
@[elab_as_elim, simp]
def Tm.Var.casesMap {motive : ∀ τ, Var Δ' τ (Γ.map δ) → Sort u} (k : ∀ {τ} (x : Var Δ τ Γ), motive (δ τ) (x.map δ)) {τ} x : motive τ x :=
match Γ, τ, x with
| .cons .., _, .here => k here
| .cons .., _, .there x => x.casesMap (motive := fun τ x => motive τ x.there) fun x => k x.there
@[simp]
theorem Tm.Var.casesMap_map : @casesMap Δ' Δ Γ δ motive k (δ τ) (x.map δ) = k x :=
by induction x with simp [*]
inductive Tm : ∀ Δ, (Γ : Tm.Ctx Δ) → (τ : Tp Δ) → Type
| var (x : Tm.Var Δ τ Γ) : Tm Δ Γ τ
| lam (e : Tm Δ (Γ.cons τ₁) τ₂) : Tm Δ Γ (τ₁.arr τ₂)
| app (e : Tm Δ Γ (τ₁.arr τ₂)) (e₁ : Tm Δ Γ τ₁) : Tm Δ Γ τ₂
| gen (e : Tm (.cons Δ) (Γ.map (.rename .there)) τ) : Tm Δ Γ (.all τ)
| inst (e : Tm Δ Γ (.all τ)) τ' : Tm Δ Γ (τ.subst (.mk τ'))
theorem hcongrArg₂ {α : Sort u} {β : α → Sort v} {γ : ∀ a, β a → Sort w} (f : ∀ a b, γ a b) {a₁ a₂ : α} (ha : a₁ = a₂) {b₁ : β a₁} {b₂ : β a₂} (hb : HEq b₁ b₂) : HEq (f a₁ b₁) (f a₂ b₂) :=
by cases ha; cases hb; rfl
theorem Tm.rec_cast {τ τ' : Tp Δ} (h : τ = τ') {var lam app gen inst} {t : Tm Δ Γ τ} : @rec motive @var @lam @app @gen @inst Δ Γ τ' (h ▸ t) = (h.rec (motive := fun τ' _ => _ = motive Δ Γ τ' _) rfl).mp (@rec motive @var @lam @app @gen @inst Δ Γ τ t) :=
eq_of_heq (.trans (hcongrArg₂ _ h.symm (h.rec (.refl _))) (have := h.rec (motive := fun τ' _ => _ = motive Δ Γ τ' _) rfl; (this.rec (.refl _) : HEq _ (this.mp _))))
def Tm.decEq {τ τ'} : ∀ (h : τ = τ') (e : Tm Δ Γ τ) (e' : Tm Δ Γ τ'), Decidable (h ▸ e = e')
| h, var x, var x' => by cases h; exact if h : x = x'
then isTrue (h ▸ rfl)
else isFalse (by simp [h])
| h, lam e, lam e' => by cases h; exact match decEq rfl e e' with
| isTrue h => isTrue (h ▸ rfl)
| isFalse h => isFalse (by simp [h])
| h, app (τ₁ := τ₁) e e₁, app (τ₁ := τ₁') e' e₁' => have ih := @e.decEq
by cases h; exact if h : τ₁ = τ₁'
then by cases h; exact
match ih _ rfl e', decEq rfl e₁ e₁' with
| isTrue h, isTrue h' => isTrue (h ▸ h' ▸ rfl)
| isFalse h, _ => isFalse (by simp [h])
| _, isFalse h => isFalse (by simp [h])
else isFalse (by simp [h])
| h, gen e, gen e' => by cases h; exact match decEq rfl e e' with
| isTrue h => isTrue (h ▸ rfl)
| isFalse h => isFalse (by simp [h])
| _, inst (τ := τ₁) e τ, inst (τ := τ₁') e' τ' => if h : τ = τ'
then if h₁ : τ₁ = τ₁'
then by cases h; cases h₁; exact match decEq rfl e e' with
| isTrue h => isTrue (h ▸ rfl)
| isFalse h => isFalse (by simp [h])
else isFalse fun h' => (rec_cast _).mp (Tm.noConfusion h') fun | _, _, .refl _, _, _ => h₁ rfl
else isFalse fun h' => (rec_cast _).mp (Tm.noConfusion h') fun | _, _, _, _, .refl _ => h rfl
| h, var _, lam _
| h, var _, app ..
| h, var _, gen _
| h, var _, inst ..
| h, lam _, var _
| h, lam _, app ..
| h, app .., var _
| h, app .., lam _
| h, app .., gen _
| h, app .., inst ..
| h, gen _, var _
| h, gen _, app ..
| h, inst .., var _
| h, inst .., app .. => isFalse (by cases h; simp)
| h, lam _, inst ..
| h, gen _, inst ..
| h, inst .., lam _
| h, inst .., gen _ => isFalse fun h' => (rec_cast h).mp (Tm.noConfusion h')
instance : DecidableEq (Tm Δ Γ τ)
| e, e' => e.decEq rfl e'
def Tm.Var.cast {τ τ' Γ Γ'} (hτ : τ = τ') (hΓ : Γ = Γ') (x : Var Δ τ Γ) : Var Δ τ' Γ' :=
hτ ▸ hΓ ▸ x
@[simp]
theorem Tm.Var.cast_there : @cast Δ τ τ' (.cons Γ τ'') (.cons Γ' τ''') hτ hΓ (.there x) = there (x.cast hτ (Ctx.cons.inj hΓ).left) :=
by cases hτ; cases hΓ; rfl
@[simp]
theorem Tm.Var.cast_rfl : cast rfl rfl x = x :=
rfl
@[simp]
theorem Tm.Var.cast_cast : cast hτ₂ hΓ₂ (cast hτ₁ hΓ₁ x) = cast (hτ₁.trans hτ₂) (hΓ₁.trans hΓ₂) x :=
by cases hτ₁; cases hΓ₁; rfl
theorem Tm.Var.cases_cast {motive : ∀ τ, (x : Var Δ τ (.cons Γ τ')) → Sort u} {here there} (hτ : τ = τ'') (hΓ : .cons Γ' τ = Γ.cons τ') : HEq (@cases Δ Γ τ' motive here there τ'' (cast hτ hΓ .here)) here :=
by cases hτ; cases hΓ; rfl
def Tm.cast {Γ Γ' τ τ'} (hΓ : Γ = Γ') (hτ : τ = τ') (e : Tm Δ Γ τ) : Tm Δ Γ' τ' :=
hΓ ▸ hτ ▸ e
@[simp]
theorem Tm.cast_var : @cast Δ Γ Γ' τ τ' hΓ hτ (var x) = var (x.cast hτ hΓ) :=
by cases hΓ; cases hτ; rfl
@[simp]
theorem Tm.cast_lam : @cast Δ Γ Γ' (.arr τ₁ τ₂) (.arr τ₁' τ₂') hΓ hτ (lam e) = lam (e.cast (congr (congrArg Ctx.cons hΓ) (Tp.arr.inj hτ).left) (Tp.arr.inj hτ).right) :=
by cases hΓ; cases hτ; rfl
@[simp]
theorem Tm.cast_app : @cast Δ Γ Γ' τ τ' hΓ hτ (app e e₁) = app (e.cast hΓ (congrArg _ hτ)) (e₁.cast hΓ rfl) :=
by cases hΓ; cases hτ; rfl
@[simp]
theorem Tm.cast_gen : @cast Δ Γ Γ' (.all τ) (.all τ') hΓ hτ (gen e) = gen (e.cast (congrArg _ hΓ) (Tp.all.inj hτ)) :=
by cases hΓ; cases hτ; rfl
@[simp]
theorem Tm.cast_rfl : cast rfl rfl e = e :=
rfl
@[simp]
theorem Tm.cast_cast : cast hΓ₂ hτ₂ (cast hΓ₁ hτ₁ e) = cast (hΓ₁.trans hΓ₂) (hτ₁.trans hτ₂) e :=
by cases hΓ₁; cases hτ₁; rfl
@[simp]
def Tm.renameTp (δ : Tp.Rename Δ Δ') {Γ τ} : (e : Tm Δ Γ τ) → Tm Δ' (Γ.map (.rename δ)) (τ.rename δ)
| var x => var (x.map (.rename δ))
| lam e => lam (e.renameTp δ)
| app e e₁ => app (e.renameTp δ) (e₁.renameTp δ)
| gen e => gen (e.renameTp δ.weaken |>.cast (by simp) rfl)
| inst e τ => inst (e.renameTp δ) (τ.rename δ) |>.cast rfl (by simp)
@[simp]
def Tm.substTp (δ : Tp.Subst Δ Δ') {Γ τ} : (e : Tm Δ Γ τ) → Tm Δ' (Γ.map (.subst δ)) (τ.subst δ)
| var x => var (x.map (.subst δ))
| lam e => lam (e.substTp δ)
| app e e₁ => app (e.substTp δ) (e₁.substTp δ)
| gen e => gen (e.substTp δ.weaken |>.cast (by simp) rfl)
| inst e τ => inst (e.substTp δ) (τ.subst δ) |>.cast rfl (by simp)
def Tm.Rename Δ (Γ Γ' : Ctx Δ) : Type :=
∀ {τ}, (x : Var Δ τ Γ) → Var Δ τ Γ'
def Tm.Rename.map (δ : (τ : Tp Δ) → Tp Δ') {Γ Γ'} (γ : Rename Δ Γ Γ') : Rename Δ' (Γ.map δ) (Γ'.map δ) :=
Var.casesMap fun x => (γ x).map δ
@[simp]
def Tm.Rename.weaken (γ : Rename Δ Γ Γ') {τ} : Rename Δ (Γ.cons τ) (Γ'.cons τ) :=
Var.cases .here fun x => (γ x).there
@[simp]
def Tm.rename (γ : Rename Δ Γ Γ') {τ} : (e : Tm Δ Γ τ) → Tm Δ Γ' τ
| var x => var (γ x)
| lam e => lam (e.rename γ.weaken)
| app e e₁ => app (e.rename @γ) (e₁.rename @γ)
| gen e => gen (e.rename (γ.map (.rename .there)))
| inst e τ => inst (e.rename @γ) τ
def Tm.Subst Δ (Γ Γ' : Ctx Δ) : Type :=
∀ {τ}, (x : Var Δ τ Γ) → Tm Δ Γ' τ
@[simp]
def Tm.Subst.mk (e : Tm Δ Γ τ) : Subst Δ (Γ.cons τ) Γ :=
Var.cases e .var
def Tm.Subst.rename (δ : Tp.Rename Δ Δ') {Γ Γ'} (γ : Subst Δ Γ Γ') : Subst Δ' (Γ.map (.rename δ)) (Γ'.map (.rename δ)) :=
Var.casesMap fun x => (γ x).renameTp δ
@[simp]
def Tm.Subst.weaken (γ : Subst Δ Γ Γ') {τ} : Subst Δ (Γ.cons τ) (Γ'.cons τ) :=
Var.cases (var .here) fun x => (γ x).rename .there
@[simp]
def Tm.subst (γ : Subst Δ Γ Γ') {τ} : (e : Tm Δ Γ τ) → Tm Δ Γ' τ
| var x => γ x
| lam e => lam (e.subst γ.weaken)
| app e e₁ => app (e.subst @γ) (e₁.subst @γ)
| gen e => gen (e.subst (γ.rename .there))
| inst e τ => inst (e.subst @γ) τ
@[simp]
theorem Tm.Var.heq_cast : HEq (cast hτ hΓ x) x' = HEq x x' :=
by subst_eqs; simp
@[simp]
theorem Tm.Var.heq_cast' : HEq x (cast hτ hΓ x') = HEq x x' :=
by subst_eqs; simp
@[simp]
theorem Tm.heq_cast : HEq (cast hΓ hτ e) e' = HEq e e' :=
by subst_eqs; simp
@[simp]
theorem Tm.heq_cast' : HEq e (cast hΓ hτ e') = HEq e e' :=
by subst_eqs; simp
@[simp]
theorem Tm.Rename.map_map : map δ γ (x.map δ) = (γ x).map δ := by
induction x with
| here => rfl
| there x ih => exact ih
@[simp]
theorem Tm.rename_id : rename (·) e = e := by
generalize h : (fun {τ} x => x) = γ
replace h {τ} x := congrFun (congrFun h.symm τ) x
induction e with simp [*]
| lam e ih => exact ih _ (Var.cases rfl fun x => congrArg _ (h x))
| gen e ih => exact ih _ (Var.casesMap fun x => by simp; exact congrArg _ (h x))
@[simp]
theorem Tm.Subst.rename_rename' : rename δ γ (x.map (.rename δ)) = (γ x).renameTp δ := by
induction x with
| here => rfl
| there x ih => exact ih
@[simp]
theorem Tm.Var.heq_here {τ τ' Γ Γ'} (hτ : τ = τ') (hΓ : Γ = Γ') : HEq (@here Δ τ Γ) (@here Δ τ' Γ') :=
by cases hτ; cases hΓ; rfl
@[simp]
theorem Tm.Var.map_id (h : ∀ τ, δ τ = τ) : map δ x = x.cast (by simp [h]) (by simp [h]) := by
induction x with
| here => simp [← heq_eq_eq, h]
| there x ih => simp [ih]
@[simp]
theorem Tm.Var.map_map (h : ∀ α, δ₂ (δ₁ α) = δ₂' (δ₁' α)) : cast rfl (by simp [h]) (map δ₂ (map δ₁ x)) = cast (by simp [h]) rfl (map δ₂' (map δ₁' x)) := by
induction x with
| here =>
have {Δ} {τ τ' τ'' : Tp Δ} {Γ Γ' Γ'' : Ctx Δ} (hτ : τ = τ'') (hτ' : τ' = τ'') (hΓ : Γ.cons τ = Γ'') (hΓ'' : Γ'.cons τ' = Γ'') : cast hτ hΓ here = cast hτ' hΓ'' here := by
subst_eqs
rfl
exact this ..
| there x ih => simp [ih]
theorem Tm.Var.map_map' (h : ∀ α, δ₂ (δ₁ α) = δ₂' (δ₁' α)) : HEq (map δ₂ (map δ₁ x)) (map δ₂' (map δ₁' x)) := by
have := heq_of_eq (map_map h (x := x))
simp [-heq_eq_eq] at this
exact this
@[simp]
theorem Tm.Var.map_map'' (h : ∀ α, δ₂ (δ₁ α) = δ α) : map δ₂ (map δ₁ x) = cast (by simp [h]) (by simp [h]) (map δ x) := by
induction x with
| here =>
have {Δ} {τ τ' : Tp Δ} {Γ Γ' : Ctx Δ} {hτ : τ = τ'} {hΓ : Γ.cons τ = Γ'.cons τ'} : here = cast hτ hΓ here := by
subst_eqs
rfl
exact this
| there x ih => simp [ih]
@[simp]
theorem Tm.renameTp_cast : renameTp δ (cast hΓ hτ e) = cast (congrArg _ hΓ) (congrArg _ hτ) (e.renameTp δ) :=
by subst_eqs; simp
@[simp]
theorem Tm.substTp_cast : substTp δ (cast hΓ hτ e) = cast (congrArg _ hΓ) (congrArg _ hτ) (e.substTp δ) :=
by subst_eqs; simp
theorem Tm.substTp_cong (eq : δ = δ') : substTp δ e = cast (by simp [eq]) (by simp [eq]) (substTp δ' e) :=
by cases eq; simp
theorem Tm.inst_cong {Γ₁ Γ₂ τ₁ τ₂ e₁ e₂ τ'₁ τ'₂} (hΓ : Γ₁ = Γ₂) (hτ : τ₁ = τ₂) (he : HEq e₁ e₂) (hτ : τ'₁ = τ'₂) : HEq (@inst Δ Γ₁ τ₁ e₁ τ'₁) (@inst Δ Γ₂ τ₂ e₂ τ'₂) := by
subst_eqs
rfl
@[simp]
theorem Eq.rec_heq {refl} : HEq (@rec α a motive refl b h) rhs = HEq refl rhs :=
by cases h; rfl
@[simp]
theorem Eq.rec_heq' {refl} : HEq lhs (@rec α a motive refl b h) = HEq lhs refl :=
by cases h; rfl
@[simp]
theorem Tm.renameTp_renameTp {δ₁ : Tp.Rename Δ Δ'} {δ₂ : Tp.Rename Δ' Δ''} : renameTp δ₂ (renameTp δ₁ e) = (renameTp (fun α => δ₂ (δ₁ α)) e).cast (by simp) (by simp) := by
induction e generalizing Δ' Δ'' with simp [*]
| gen =>
simp [← heq_eq_eq]
congr
funext α
cases α <;> rfl
| inst =>
simp [← heq_eq_eq]
apply inst_cong <;> simp
rfl
@[simp]
theorem Tm.substTp_substTp {δ₁ : Tp.Subst Δ Δ'} {δ₂ : Tp.Subst Δ' Δ''} {e : Tm Δ Γ τ} : substTp δ₂ (substTp δ₁ e) = (substTp (fun α => (δ₁ α).subst δ₂) e).cast (by simp) (by simp) := by
induction e generalizing Δ' Δ'' with simp [*]
| gen =>
simp [← heq_eq_eq]
congr
funext α
cases α <;> simp
| inst =>
simp [← heq_eq_eq]
apply inst_cong <;> simp
congr
funext α
cases α <;> simp
@[simp]
theorem Tm.Subst.rename_cast : rename δ γ (.cast hτ rfl x) = (rename δ γ x).cast rfl hτ :=
by cases hτ; simp
theorem Tm.Subst.rename_rename : rename δ₂ (rename δ₁ γ) x = cast (by simp) rfl (rename (fun α => δ₂ (δ₁ α)) γ (x.cast rfl (by simp))) := by
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp [-Var.map_map'']
simp
@[simp]
theorem Tm.Rename.map_map' {γ : Rename Δ Γ Γ'} {δ₁ : (τ : Tp Δ) → Tp Δ'} {δ₂ : (τ : Tp Δ') → Tp Δ''} {x : Var _ τ _} : map δ₂ (map δ₁ @γ) x = x.casesMap (Var.casesMap fun x => (γ x).map δ₁ |>.map δ₂) := by
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp [-Var.map_map'']
@[simp]
theorem Tm.Rename.there_rename {γ : Rename Δ Γ Γ'} : (map δ γ x).there = map δ (fun x => (γ x).there (τ' := τ)) x := by
refine x.casesMap fun x => ?_
simp
@[simp]
theorem Tm.subst_var : subst var e = e := by
generalize h : (fun {τ} x => var x) = γ
replace h {τ} x := congrFun (congrFun h.symm τ) x
induction e with simp [*]
| lam e ih => exact ih _ (Var.cases rfl (by simp [h]))
| gen e ih => exact ih _ (Var.casesMap (by simp [h]))
@[simp]
theorem Tp.Subst.weaken_var : @weaken Δ _ var = var := by
funext α
cases α <;> rfl
theorem Tm.rename_rename' (h : ∀ {τ} (x : Var _ τ _), γ₂ (γ₁ x) = γ x) : Rename.map δ γ₂ (Rename.map δ γ₁ x) = Rename.map δ γ x := by
refine x.casesMap fun x => ?_
simp [h]
theorem Tm.Var.cast_flip : (eq : cast hτ.symm hΓ.symm x = x') → x = cast hτ hΓ x' :=
by cases hΓ; cases hτ; simp
theorem Tm.cast_flip : (eq : cast hΓ.symm hτ.symm e = e') → e = cast hΓ hτ e' :=
by cases hΓ; cases hτ; simp
theorem Tm.Var.cast_flip' : (eq : cast hτ hΓ x = x') → x = cast hτ.symm hΓ.symm x' :=
by cases hΓ; cases hτ; simp
theorem Tm.cast_flip' : (eq : cast hΓ hτ e = e') → e = cast hΓ.symm hτ.symm e' :=
by cases hΓ; cases hτ; simp
@[simp]
theorem Tm.rename_cast {γ : Rename Δ Γ Γ'} : rename γ (cast hΓ rfl e) = rename (fun x => γ (x.cast rfl hΓ)) e :=
by cases hΓ; simp
@[simp]
theorem Tm.rename_cast' {γ : Rename Δ Γ Γ'} : rename γ (cast rfl hτ e) = cast rfl hτ (rename γ e) :=
by cases hτ; simp
@[simp]
theorem Tm.cast_rename {γ : Rename Δ Γ Γ'} : cast hΓ rfl (rename γ e) = rename (fun x => (γ x).cast rfl hΓ) e :=
by cases hΓ; simp
@[simp]
theorem Tm.Var.casesMap_rename_cast : @casesMap Δ' Δ Γ (.rename δ) motive @k τ' (@cast Δ' (.rename δ τ) τ' (Γ.map (.rename δ)) (Γ.map (.rename δ)) hτ rfl (.map (.rename δ) x)) = hτ.rec (motive := fun τ' hτ => motive τ' (cast hτ rfl (x.map (.rename δ)))) (cast_rfl (x := x.map (.rename δ)) ▸ k x :) := by
cases hτ
induction x with
| here => rfl
| there x ih =>
apply ih.trans
rfl
@[simp]
theorem Tm.Var.casesMap_subst_cast : @casesMap Δ' Δ Γ (.subst δ) motive @k τ' (@cast Δ' (.subst δ τ) τ' (Γ.map (.subst δ)) (Γ.map (.subst δ)) hτ rfl (.map (.subst δ) x)) = hτ.rec (motive := fun τ' hτ => motive τ' (cast hτ rfl (x.map (.subst δ)))) (cast_rfl (x := x.map (.subst δ)) ▸ k x :) := by
cases hτ
induction x with
| here => rfl
| there x ih =>
apply ih.trans
rfl
theorem Tm.Var.heq_of_eq_cast (eq : cast hτ hΓ x = x') : HEq x x' :=
by cases eq; simp
theorem Tm.heq_of_eq_cast (eq : cast hΓ hτ e = e') : HEq e e' :=
by cases eq; simp
@[simp]
theorem Tm.substTp_var {e : Tm Δ Γ τ} : substTp .var e = e.cast (by simp) (by simp) := by
have {Δ} : Tp.Subst.weaken (Δ := Δ) .var = .var := by
funext α
cases α <;> rfl
induction e with simp [*]
| gen e ih => simp [← heq_eq_eq]; rewrite [this]; simp [ih]
| inst e τ ih => simp [← heq_eq_eq]; apply inst_cong <;> simp [this]
@[simp]
theorem Tm.substTp_renameTp {δ₁ : Tp.Rename Δ Δ'} {δ₂ : Tp.Subst Δ' Δ''} : substTp δ₂ (renameTp δ₁ e) = (substTp (fun α => δ₂ (δ₁ α)) e).cast (by simp) (by simp) := by
induction e generalizing Δ' Δ'' with simp [*]
| gen e ih =>
have : @Eq (Tp.Subst ..) (fun α => Tp.Var.cases (.var .here) (fun α => (δ₂ α).rename .there) (Tp.Var.cases .here (fun α => (δ₁ α).there) α)) (.weaken fun α => δ₂ (δ₁ α)) := by
funext α
cases α
. rfl
. rfl
rewrite [substTp_cong this]
simp
| inst e τ ih =>
simp [← heq_eq_eq]
apply inst_cong <;> simp
rfl
@[simp]
theorem Tm.renameTp_substTp {δ₁ : Tp.Subst Δ Δ'} {δ₂ : Tp.Rename Δ' Δ''} : renameTp δ₂ (substTp δ₁ e) = (substTp (fun α => (δ₁ α).rename δ₂) e).cast (by simp) (by simp) := by
induction e generalizing Δ' Δ'' with simp [*]
| gen e ih =>
have : @Eq (Tp.Subst ..) (fun α => Tp.rename δ₂.weaken (Tp.Var.cases (.var .here) (fun α => (δ₁ α).rename .there) α)) (.weaken fun α => (δ₁ α).rename δ₂) := by
funext α
cases α <;> simp
rewrite [substTp_cong this]
simp
| inst e τ ih =>
simp [← heq_eq_eq]
apply inst_cong <;> simp
congr
funext α
cases α <;> simp
theorem Tm.rename_rename'' {γ₁ : Subst Δ Γ Γ'} {γ₂ : Rename Δ Γ' Γ''} {γ : Subst Δ Γ Γ''} (h : ∀ {τ} (x : Var _ τ _), (γ₁ x).rename @γ₂ = γ x) : rename (γ₂.map (.rename δ)) (γ₁.rename δ x) = γ.rename δ x := by
refine x.casesMap fun x => ?_
simp [← h]
generalize γ₁ x = e
clear h γ₁
rename_i Δ' τ x' _
clear Γ τ x' γ x
induction e generalizing Δ' with simp [*]
| lam e ih =>
simp [← ih]
congr
funext τ x
cases x
. rfl
next x =>
refine x.casesMap fun x => ?_
simp [Rename.map]
| gen e ih =>
simp [← ih]
congr
funext τ x
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp [← heq_eq_eq]
rfl
@[simp]
theorem Tm.rename_renameTp : rename (Rename.map (.rename δ) @γ) (renameTp δ e) = renameTp δ (rename @γ e) := by
rename_i Δ Δ' _ _ _
induction e generalizing Δ' with simp [*]
| lam e ih =>
refine .trans ?_ (ih (γ := γ.weaken))
congr
funext τ x
cases x
. rfl
next x =>
refine x.casesMap fun x => ?_
simp [Rename.map]
| gen e ih =>
apply cast_flip
refine .trans ?_ ih
simp
congr
funext τ x
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp [← heq_eq_eq]
simp
theorem Tm.cast_subst {γ : Subst Δ Γ Γ'} : cast hΓ hτ (subst γ e) = subst (fun x => (γ x).cast hΓ rfl) (e.cast rfl hτ) :=
by subst_eqs; simp
@[simp]
theorem Tm.subst_cast {γ : Subst Δ Γ Γ'} : subst γ (cast hΓ hτ e) = cast rfl hτ (subst (fun x => γ (x.cast rfl hΓ)) e) :=
by subst_vars; simp
theorem Tm.subst_rename'' {γ₁ : Subst Δ Γ Γ'} {γ₂ : Subst Δ Γ' Γ''} {γ : Subst Δ Γ Γ''} (h : ∀ {τ} (x : Var _ τ _), (γ₁ x).subst @γ₂ = γ x) : subst (γ₂.rename δ) (γ₁.rename δ x) = γ.rename δ x := by
refine x.casesMap fun x => ?_
simp [← h]
generalize γ₁ x = e
clear h γ₁
rename_i Δ' τ x' _
clear Γ τ x' γ x
induction e generalizing Δ' with simp [*]
| lam e ih =>
simp [← ih]
congr
funext τ x
cases x
. rfl
next x =>
refine x.casesMap fun x => ?_
simp [Subst.rename]
simp [← rename_renameTp]
congr
funext τ x
refine x.casesMap fun x => ?_
simp
| gen e ih =>
simp [← ih]
simp [cast_subst]
congr
funext τ x
simp [Subst.rename_rename]
theorem Tm.subst_rename' (h : ∀ {τ} (x : Var _ τ _), γ₂ (γ₁ x) = γ x) : Subst.rename δ γ₂ (Rename.map (.rename δ) γ₁ x) = Subst.rename δ γ x := by
refine x.casesMap fun x => ?_
simp [h]
@[simp]
theorem Tm.rename_rename : rename γ₂ (rename γ₁ e) = rename (fun x => γ₂ (γ₁ x)) e := by
generalize h : (fun {τ} x => γ₂ (γ₁ x)) = γ
replace h {τ} x := congrFun (congrFun h τ) x
induction e with
| var x => exact congrArg var (h x)
| lam e ih => exact congrArg lam (ih _ <| Var.cases rfl fun x => congrArg Var.there (h x))
| app e e₁ ih ih₁ => exact congr (congrArg app (ih _ h)) (ih₁ _ h)
| gen e ih => exact congrArg gen (ih _ fun _ => rename_rename' h)
| inst e τ ih => exact congrArg (inst · τ) (ih _ h)
@[simp]
theorem Tm.subst_rename : subst γ₂ (rename γ₁ e) = subst (fun x => γ₂ (γ₁ x)) e := by
generalize h : (fun {τ} x => γ₂ (γ₁ x)) = γ
replace h {τ} x := congrFun (congrFun h τ) x
induction e with
| var x => exact h x
| lam e ih => exact congrArg lam (ih _ <| Var.cases rfl fun x => congrArg (rename .there) (h x))
| app e e₁ ih ih₁ => exact congr (congrArg app (ih _ h)) (ih₁ _ h)
| gen e ih => exact congrArg gen (ih _ fun _ => subst_rename' h)
| inst e τ ih => exact congrArg (inst · τ) (ih _ h)
@[simp]
theorem Tm.rename_subst : rename @γ₂ (subst @γ₁ e) = subst (fun x => (γ₁ x).rename @γ₂) e := by
generalize h : (fun {τ} x => (γ₁ x).rename @γ₂) = γ
replace h {τ} x := congrFun (congrFun h τ) x
induction e with
| var x => exact h x
| lam e ih => exact congrArg lam (ih _ <| Var.cases rfl fun x => by simp [Rename.weaken, Subst.weaken, Var.cases, ← h x])
| app e e₁ ih ih₁ => exact congr (congrArg app (ih _ h)) (ih₁ _ h)
| gen e ih => exact congrArg gen (ih _ fun _ => rename_rename'' h)
| inst e τ ih => exact congrArg (inst · τ) (ih _ h)
@[simp]
theorem Tm.subst_subst : subst @γ₂ (subst @γ₁ e) = subst (fun x => (γ₁ x).subst @γ₂) e := by
generalize h : (fun {τ} x => (γ₁ x).subst @γ₂) = γ
replace h {τ} x := congrFun (congrFun h τ) x
induction e with
| var x => exact h x
| lam e ih => exact congrArg lam (ih _ <| Var.cases rfl fun x => by simp [Rename.weaken, Subst.weaken, Var.cases, ← h x])
| app e e₁ ih ih₁ => exact congr (congrArg app (ih _ h)) (ih₁ _ h)
| gen e ih => exact congrArg gen (ih _ fun _ => subst_rename'' h)
| inst e τ ih => exact congrArg (inst · τ) (ih _ h)
@[simp]
theorem Tm.substTp_rename {δ : Tp.Subst Δ Δ'} {γ : Rename Δ Γ Γ'} {e : Tm Δ Γ τ} : substTp δ (rename γ e) = (e.substTp δ).rename (Var.casesMap fun x => (γ x).map (.subst δ)) := by
induction e generalizing Δ' with simp [*]
| lam e ih =>
congr
funext τ x
cases x
. rfl
next x =>
refine x.casesMap fun x => ?_
simp
| gen e ih =>
congr
funext τ x
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp [Rename.map]
have : x.map (.subst (fun α => (δ α).rename .there)) = ((x.map (.subst δ)).map (.rename .there)).cast (by simp) (by simp) := by
simp
simp [this, ← heq_eq_eq]
have : ((x.map (.subst δ)).map (.rename .there)).cast (by simp) (by simp) = (x.map (.rename .there)).map (.subst δ.weaken) := by
simp
rewrite [this]
simp only [Var.casesMap_map]
simp
@[simp]
theorem Tm.substTp_subst {γ : Subst Δ Γ Γ'} {δ : Tp.Subst Δ Δ'} : substTp δ (subst γ e) = subst (Var.casesMap fun x => (γ x).substTp δ) (substTp δ e) := by
induction e generalizing Δ' with simp [*]
| lam e ih =>
congr
funext τ x
cases x
. rfl
next x =>
refine x.casesMap fun x => ?_
simp
congr
funext τ x
refine x.casesMap fun x => ?_
simp
| gen e ih =>
simp [cast_subst]
congr
funext τ x
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp [Rename.map]
have : x.map (.subst (fun α => (δ α).rename .there)) = ((x.map (.subst δ)).map (.rename .there)).cast (by simp) (by simp) := by
simp
simp [this, ← heq_eq_eq]
have : ((x.map (.subst δ)).map (.rename .there)).cast (by simp) (by simp) = (x.map (.rename .there)).map (.subst δ.weaken) := by
simp
rewrite [this]
simp only [Var.casesMap_map]
simp
example (e : Tm Δ Γ τ) : Tm Δ.cons (Γ.map (.rename .there)) (τ.rename .there) := e.renameTp .there
example (e : Tm Δ Γ τ) : Tm Δ (Γ.cons τ') τ := e.rename .there
example (e : Tm (.cons Δ) Γ τ) (τ' : Tp Δ) : Tm Δ (Γ.map (.subst (.mk τ'))) (τ.subst (.mk τ')) := e.substTp (.mk τ')
example (e : Tm Δ (Γ.cons τ') τ) (e' : Tm Δ Γ τ') : Tm Δ Γ τ := e.subst (Tm.Subst.mk e')
@[simp]
theorem Tm.subst_renameTp : subst (Subst.rename δ @γ) (renameTp δ e) = renameTp δ (subst @γ e) := by
rename_i Δ Δ' _ _ _
induction e generalizing Δ' with simp [*]
| lam e ih =>
refine .trans ?_ (ih (γ := γ.weaken))
congr
funext τ x
cases x
. rfl
next x =>
refine x.casesMap fun x => ?_
change _ = Subst.rename δ γ.weaken (.map (.rename δ) x.there)
simp only [Subst.rename_rename']
simp
generalize γ x = e
simp [← rename_renameTp]
congr
funext τ x
refine x.casesMap fun x => ?_
simp
| gen e ih =>
apply cast_flip
refine .trans ?_ ih
simp [cast_subst]
congr
funext τ x
refine x.casesMap fun x => ?_
refine x.casesMap fun x => ?_
simp only [Subst.rename_rename']
simp only [Var.map_map (δ₁ := .rename .there) (δ₂ := .rename δ.weaken) (δ₁' := .rename δ) (δ₂' := .rename .there) (by simp) (x := x)]
simp only [Subst.rename_cast, cast_cast, Subst.rename_rename']
simp
theorem Tm.rename_cong {Γ₁ Γ₂ γ₁ γ₂} (hΓ : Γ₁ = Γ₂) (hγ : ∀ {τ} (x : Var Δ τ Γ), HEq (γ₁ x) (γ₂ x)) : HEq (@rename Δ Γ Γ₁ γ₁ τ e) (@rename Δ Γ Γ₂ γ₂ τ e) :=
by cases hΓ; cases funext fun τ => funext fun x => eq_of_heq (@hγ τ x); rfl
@[simp]
theorem Tp.subst_nil : @subst .nil .nil δ = (·) := by
cases show δ = var from funext nofun
exact subst_var
@[simp]
theorem Tm.subst_nil : @subst .nil .nil .nil γ τ e = e := by
cases show @γ = fun {τ} x => var x from funext fun τ => funext nofun
simp
theorem Tm.Var.cases_casesMap {here there} : @casesMap Δ' Δ (.cons Γ τ') δ motive (cases here there) τ x = cases here (casesMap there) x :=
x.casesMap (cases rfl fun _ => rfl)
theorem Tm.inst_inj_cast {τ₂' : Tp Δ} (hτ : Tp.subst (.mk τ₁') τ₁ = Tp.subst (.mk τ₂') τ₂) (he : hτ ▸ @inst Δ Γ τ₁ e₁ τ₁' = @inst Δ Γ τ₂ e₂ τ₂') : τ₁ = τ₂ ∧ τ₁' = τ₂' :=
(rec_cast hτ).mp (Tm.noConfusion he) fun _ _ hτ _ hτ' => ⟨eq_of_heq hτ, eq_of_heq hτ'⟩
@[simp]
def Tm.Subst.weakenTp (γ : Subst Δ (.map (.subst δ) Γ) Γ') : Subst Δ ((Γ.map (.rename .there)).map (.subst (Tp.Var.cases τ δ))) Γ'
| τ, x => γ (x.cast rfl (by simp))
@[simp]
theorem Tm.Subst.apply_cast {γ : Subst Δ Γ Γ'} : γ (x.cast hτ rfl) = (γ x).cast rfl hτ :=
by cases hτ; simp
@[simp]
theorem Tm.subst_cases {γ : Subst Δ Γ Γ'} {x : Var Δ τ (.cons Γ τ')} : (subst γ' (x.cases e γ) : Tm Δ Γ'' τ) = x.cases (e.subst γ') fun x => (γ x).subst γ' :=
by cases x <;> rfl
@[simp]
theorem Tm.substTp_subst_cases {δ : Tp.Subst Δ Δ'} : substTp (fun α => Tp.subst (.mk τ) (α.cases τ' (fun α => (δ α).rename .there))) e = (substTp (fun α => α.cases (τ'.subst (.mk τ)) δ) e).cast (by simp) (by simp) :=
by simp [← heq_eq_eq]; congr; funext α; cases α <;> simp
@[simp]
theorem Tm.casesMap_cast
{Γ : Ctx Δ}
{δ : Tp.Subst Δ .nil}
{γ : Subst .nil (Γ.map (.subst δ)) .nil}
{x : Var .nil τ ((Γ.map (.rename .there)).map (.subst (Tp.Var.cases τ₂ δ)))}
: Var.casesMap (Γ := (Γ.map (.rename .there)).map (.subst δ.weaken)) (motive := fun τ _ => Tm _ _ τ) (fun x => substTp (.mk τ₂) (Subst.rename .there γ (x.cast rfl (by simp)))) (x.cast rfl (by simp)) = γ.weakenTp x := by
refine x.casesMap fun x => ?_
refine x.casesMap fun {τ} x => ?_
simp
have : (x.map (.subst δ)).cast (by simp : _ = (τ.rename .there).subst (Tp.Var.cases τ₂ δ)) (by simp) = (x.map (.rename .there) |>.map (.subst δ.weaken) |>.map (.subst (.mk τ₂))).cast (by simp) rfl := by
rewrite [Var.map_map'' fun _ => rfl]
simp
simp [this]
have : (x.map (.subst (fun α => (δ α).rename .there))).cast (by simp : _ = (τ.rename .there).subst δ.weaken) (by simp) = (x.map (.subst δ) |>.map (.rename .there)).cast (by simp) rfl := by
simp
simp [← heq_eq_eq]
rewrite [this, Subst.rename, Var.casesMap_rename_cast]
refine .trans (b := (γ (x.map (.subst δ))).renameTp .there |>.substTp (.mk τ₂)) ?_ ?_
. congr
. simp
simp [← heq_eq_eq]
rfl
. simp
inductive Tm.Steps : (e e' : Tm .nil .nil τ) → Type
| app (s : e.Steps e') : (app e e₁).Steps (app e' e₁)
| app_lam : Steps (app (lam e) e₁) (e.subst (Subst.mk e₁))
| inst (s : e.Steps e') : (inst e τ).Steps (inst e' τ)
| inst_gen : Steps (inst (gen e) τ) (e.substTp (.mk τ))
inductive Tm.Val : (e : Tm .nil .nil τ) → Type
| lam : Val (.lam e)
| gen : Val (.gen e)
theorem Tm.Val.not_steps : (v : Val e) → (s : Steps e e') → False
| @lam τ₁ τ₂ e, s =>
have {τ} {e₁ e' : Tm .nil .nil τ} (h : τ = τ₁.arr τ₂) (h' : h ▸ e₁ = e.lam) (s : Steps e₁ e') : False := by
cases s with
| app => nomatch h
| app_lam => nomatch h
| inst => rename_i τ _ _ _ _ ; cases τ; rename_i α _ _ _; cases α; cases h; nomatch h'; nomatch h; cases h; nomatch h'; nomatch h
| inst_gen => rename_i τ _ _ ; cases τ; rename_i α _; cases α; cases h; nomatch h'; nomatch h; cases h; nomatch h'; nomatch h
this rfl rfl s
| @gen τ e, s =>
have {τ₁} {e₁ e' : Tm .nil .nil τ₁} (h : τ₁ = τ.all) (h' : h ▸ e₁ = e.gen) (s : Steps e₁ e') : False := by
cases s with
| app => nomatch h
| app_lam => nomatch h
| inst => rename_i τ _ _ _ _ ; cases τ; rename_i α _ _ _; cases α; cases h; nomatch h'; nomatch h; nomatch h; nomatch h
| inst_gen => rename_i τ _ _ ; cases τ; rename_i α _; cases α; cases h; nomatch h'; nomatch h; nomatch h; nomatch h
this rfl rfl s
theorem Tm.Steps.deterministic : (s₁ : Steps e e₁) → (s₂ : Steps e e₂) → e₁ = e₂
| app s₁, app s₂ => s₁.deterministic s₂ ▸ rfl
| app s₁, app_lam => nomatch Val.lam.not_steps s₁
| app_lam, app s₂ => nomatch Val.lam.not_steps s₂
| app_lam, app_lam => rfl
| @inst τ e' τ' e₁' s₁, s₂ =>
have {τ₂} (h : τ₂ = τ.subst (.mk τ')) {e e₂} (h' : h ▸ e = e'.inst τ') (s₂ : Steps e e₂) : e₁'.inst τ' = h ▸ e₂ := by
cases s₂ with
| app => nomatch h
| app_lam => nomatch h
| inst s₂ =>
let ⟨_, _⟩ := inst_inj_cast h h'
subst_eqs
cases s₁.deterministic s₂
rfl
| inst_gen =>
let ⟨_, _⟩ := inst_inj_cast h h'
subst_eqs
nomatch Val.gen.not_steps s₁
this rfl rfl s₂
| @inst_gen τ e' τ', s₂ =>
have {τ₂} (h : τ₂ = τ.subst (.mk τ')) {e e₂} (h' : h ▸ e = e'.gen.inst τ') (s₂ : Steps e e₂) : e'.substTp (.mk τ') = h ▸ e₂ := by
cases s₂ with
| app => nomatch h
| app_lam => nomatch h
| inst s₂ =>
let ⟨_, _⟩ := inst_inj_cast h h'
subst_eqs
nomatch Val.gen.not_steps s₂
| inst_gen =>
let ⟨_, _⟩ := inst_inj_cast h h'
subst_eqs
rfl
this rfl rfl s₂
def Tm.progress : (e : Tm .nil .nil τ) → Val e ⊕ Σ e', Steps e e'
| lam e => .inl .lam
| app e e₁ => .inr <|
have := e.progress
match this with
| .inl .lam => ⟨_, .app_lam⟩
| .inr ⟨_, s⟩ => ⟨_, .app s⟩
| gen e => .inl .gen
| inst e τ => .inr <|
have := e.progress
match this with
| .inl .gen => ⟨_, .inst_gen⟩
| .inr ⟨_, s⟩ => ⟨_, .inst s⟩
def Tm.Steps.irrelevant' (h : Nonempty (Σ e', Steps e e')) : Σ e', Steps e e' :=
match progress e with
| .inl v => (h.elim fun ⟨_, s⟩ => v.not_steps s).elim
| .inr s => s
def Tm.Steps.irrelevant (h : Nonempty (Steps e e')) : Steps e e' :=
let ⟨_, s⟩ := irrelevant' (h.elim (⟨e', ·⟩))
h.elim (deterministic s) ▸ s
inductive Tm.Reduces : (e e' : Tm .nil .nil τ) → Type
| refl : Reduces e e
| step (s : Steps e e') (r : Reduces e' e'') : Reduces e e''
def Tm.Reduces.trans : (r : Reduces e e') → (r' : Reduces e' e'') → Reduces e e''
| refl, r' => r'
| step s r, r' => step s (r.trans r')
def Tm.Reduces.lift {F : (e : Tm .nil .nil τ) → Tm .nil .nil τ'} (f : ∀ {e e'}, (s : Steps e e') → Steps (F e) (F e')) : (r : Reduces e e') → Reduces (F e) (F e')
| refl => refl
| step s r => step (f s) (r.lift f)
def Tm.Reduces.app : (r : Reduces e e') → Reduces (e.app e₁) (e'.app e₁) :=
lift .app
def Tm.Reduces.inst : (r : Reduces e e') → Reduces (e.inst τ) (e'.inst τ) :=
lift .inst
def Tm.Reduces.cast (r : Reduces e e') : Reduces (e.cast hΓ hτ) (e'.cast hΓ hτ) :=
by subst_eqs; simp; exact r
theorem Tm.Reduces.deterministic (r₁ : Reduces e e₁) (r₂ : Reduces e e₂) (v₁ : Val e₁) (v₂ : Val e₂) : e₁ = e₂ := by
induction r₁ with
| refl =>
cases r₂ with
| refl => rfl
| step s₂ r₂ => nomatch v₁.not_steps s₂
| step s₁ _ ih =>
cases r₂ with
| refl => nomatch v₂.not_steps s₁
| step s₂ r₂ =>
cases s₁.deterministic s₂
exact ih r₂ v₁
instance : WellFoundedRelation { x // Acc r x } where
rel := InvImage r (·.1)
wf := ⟨fun ac => InvImage.accessible _ ac.2⟩
def Tm.Reduces.irrelevant'.go (h : Acc (fun e e' => Nonempty (Steps e' e)) e) : Σ e', Val e' × Reduces e e' :=
match progress e with
| .inl v => ⟨_, v, refl⟩
| .inr ⟨e', s⟩ => have := ⟨s⟩; let ⟨_, v, r⟩ := go (h.inv this); ⟨_, v, step s r⟩
termination_by Subtype.mk e h
def Tm.Reduces.irrelevant' (h : Nonempty (Σ e', Val e' × Reduces e e')) : Σ e', Val e' × Reduces e e' :=
irrelevant'.go <| by
revert h
intro ⟨e', v, r⟩
induction r with
| refl => exact ⟨_, fun _ h => nomatch h.elim v.not_steps⟩
| step s r ih => exact ⟨_, fun _ h => h.elim s.deterministic ▸ ih v⟩
def Tm.Reduces.irrelevant.go (h : Nonempty (Reduces e e')) (ha : Acc (fun e e'' => e'' ≠ e' ∧ Nonempty (Steps e'' e)) e) : Reduces e e' :=
if h' : e = e' then
h' ▸ refl
else
match progress e with
| .inl v => False.elim <| match h with | ⟨refl⟩ => h' rfl | ⟨step s _⟩ => v.not_steps s
| .inr ⟨e'', s⟩ => have := ⟨h', ⟨s⟩⟩; step s (go (e := e'') (e' := e') (by match h with | ⟨refl⟩ => exact (h' rfl).elim | ⟨step s' r⟩ => exact s.deterministic s' ▸ ⟨r⟩) (ha.inv this))
termination_by Subtype.mk e ha
def Tm.Reduces.irrelevant (h : Nonempty (Reduces e e')) : Reduces e e' :=
irrelevant.go h <| by
revert h
intro ⟨r⟩