-
Notifications
You must be signed in to change notification settings - Fork 0
/
FundamentalLemma.v
1232 lines (1127 loc) · 37.9 KB
/
FundamentalLemma.v
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
From Coq Require Import Bool String List BinPos Compare_dec Lia Arith.
Require Import Equations.Prop.DepElim.
From Equations Require Import Equations.
From Translation
Require Import util Sorts SAst SLiftSubst Equality SCommon XTyping ITyping
ITypingInversions ITypingLemmata ITypingAdmissible Optim
Uniqueness PackLifts.
Import ListNotations.
Open Scope type_scope.
Open Scope x_scope.
Open Scope i_scope.
(*! Relation for translated expressions *)
Section Sim.
Context `{Sort_notion : Sorts.notion}.
Reserved Notation " t1 ∼ t2 " (at level 20).
Inductive trel : sterm -> sterm -> Type :=
| trel_Rel x :
sRel x ∼ sRel x
| trel_Transport_l t1 t2 T1 T2 p :
t1 ∼ t2 ->
sTransport T1 T2 p t1 ∼ t2
| trel_Transport_r t1 t2 T1 T2 p :
t1 ∼ t2 ->
t1 ∼ sTransport T1 T2 p t2
| trel_Prod n1 n2 A1 A2 B1 B2 :
A1 ∼ A2 ->
B1 ∼ B2 ->
sProd n1 A1 B1 ∼ sProd n2 A2 B2
| trel_Sum n1 n2 A1 A2 B1 B2 :
A1 ∼ A2 ->
B1 ∼ B2 ->
sSum n1 A1 B1 ∼ sSum n2 A2 B2
| trel_Eq A1 A2 u1 u2 v1 v2 :
A1 ∼ A2 ->
u1 ∼ u2 ->
v1 ∼ v2 ->
sEq A1 u1 v1 ∼ sEq A2 u2 v2
| trel_Sort s :
sSort s ∼ sSort s
| trel_Lambda n1 n2 A1 A2 B1 B2 u1 u2 :
A1 ∼ A2 ->
B1 ∼ B2 ->
u1 ∼ u2 ->
sLambda n1 A1 B1 u1 ∼ sLambda n2 A2 B2 u2
| trel_App u1 u2 A1 A2 B1 B2 v1 v2 :
u1 ∼ u2 ->
A1 ∼ A2 ->
B1 ∼ B2 ->
v1 ∼ v2 ->
sApp u1 A1 B1 v1 ∼ sApp u2 A2 B2 v2
| trel_Pair A1 A2 B1 B2 u1 u2 v1 v2 :
A1 ∼ A2 ->
B1 ∼ B2 ->
u1 ∼ u2 ->
v1 ∼ v2 ->
sPair A1 B1 u1 v1 ∼ sPair A2 B2 u2 v2
| trel_Pi1 A1 A2 B1 B2 p1 p2 :
A1 ∼ A2 ->
B1 ∼ B2 ->
p1 ∼ p2 ->
sPi1 A1 B1 p1 ∼ sPi1 A2 B2 p2
| trel_Pi2 A1 A2 B1 B2 p1 p2 :
A1 ∼ A2 ->
B1 ∼ B2 ->
p1 ∼ p2 ->
sPi2 A1 B1 p1 ∼ sPi2 A2 B2 p2
| trel_Refl A1 A2 u1 u2 :
A1 ∼ A2 ->
u1 ∼ u2 ->
sRefl A1 u1 ∼ sRefl A2 u2
| trel_Ax id :
sAx id ∼ sAx id
where " t1 ∼ t2 " := (trel t1 t2).
End Sim.
Notation " t1 ∼ t2 " := (trel t1 t2) (at level 20).
Derive Signature for trel.
Section In.
Context `{Sort_notion : Sorts.notion}.
(* We also define a biased relation that only allows transports on one side,
the idea being that the term on the other side belongs to the source.
This might be unnecessary as transport isn't typable in the source but
hopefully this is more straightforward.
*)
Reserved Notation " t ⊏ t' " (at level 20).
(* The first is in the source, the second in the target. *)
Inductive inrel : sterm -> sterm -> Type :=
| inrel_Rel x :
sRel x ⊏ sRel x
| inrel_Transport t t' T1 T2 p :
t ⊏ t' ->
t ⊏ sTransport T1 T2 p t'
| inrel_Prod n n' A A' B B' :
A ⊏ A' ->
B ⊏ B' ->
sProd n A B ⊏ sProd n' A' B'
| inrel_Sum n n' A A' B B' :
A ⊏ A' ->
B ⊏ B' ->
sSum n A B ⊏ sSum n' A' B'
| inrel_Eq A A' u u' v v' :
A ⊏ A' ->
u ⊏ u' ->
v ⊏ v' ->
sEq A u v ⊏ sEq A' u' v'
| inrel_Sort s :
sSort s ⊏ sSort s
| inrel_Lambda n n' A A' B B' u u' :
A ⊏ A' ->
B ⊏ B' ->
u ⊏ u' ->
sLambda n A B u ⊏ sLambda n' A' B' u'
| inrel_App u u' A A' B B' v v' :
u ⊏ u' ->
A ⊏ A' ->
B ⊏ B' ->
v ⊏ v' ->
sApp u A B v ⊏ sApp u' A' B' v'
| inrel_Pair A A' B B' u u' v v' :
A ⊏ A' ->
B ⊏ B' ->
u ⊏ u' ->
v ⊏ v' ->
sPair A B u v ⊏ sPair A' B' u' v'
| inrel_Pi1 A A' B B' p p' :
A ⊏ A' ->
B ⊏ B' ->
p ⊏ p' ->
sPi1 A B p ⊏ sPi1 A' B' p'
| inrel_Pi2 A A' B B' p p' :
A ⊏ A' ->
B ⊏ B' ->
p ⊏ p' ->
sPi2 A B p ⊏ sPi2 A' B' p'
| inrel_Refl A A' u u' :
A ⊏ A' ->
u ⊏ u' ->
sRefl A u ⊏ sRefl A' u'
| inrel_Ax id :
sAx id ⊏ sAx id
where " t ⊏ t' " := (inrel t t').
Lemma inrel_trel :
forall {t t'}, t ⊏ t' -> t ∼ t'.
Proof.
intros t t' h.
induction h ; now constructor.
Defined.
Lemma inrel_optTransport :
forall {A B p t t'},
t ⊏ t' ->
t ⊏ optTransport A B p t'.
Proof.
intros A B p t t' h.
unfold optTransport.
destruct (Equality.eq_term A B).
- assumption.
- constructor. assumption.
Defined.
End In.
Notation " t ⊏ t' " := (inrel t t') (at level 20).
Ltac lift_sort :=
match goal with
| |- _ ;;; _ |-i lift ?n ?k ?t : ?S => change S with (lift n k S)
| |- _ ;;; _ |-i llift ?n ?k ?t : ?S => change S with (llift n k S)
| |- _ ;;; _ |-i rlift ?n ?k ?t : ?S => change S with (rlift n k S)
| |- _ ;;; _ |-i ?t { ?n := ?u } : ?S => change S with (S {n := u})
| |- sSort ?s = lift ?n ?k ?t =>
change (sSort s) with (lift n k (sSort s))
| |- sSort ?s = llift ?n ?k ?t =>
change (sSort s) with (llift n k (sSort s))
| |- sSort ?s = rlift ?n ?k ?t =>
change (sSort s) with (rlift n k (sSort s))
| |- sSort ?s = ?t{ ?n := ?u } =>
change (sSort s) with ((sSort s){ n := u })
| |- lift ?n ?k ?t = sSort ?s =>
change (sSort s) with (lift n k (sSort s))
| |- llift ?n ?k ?t = sSort ?s =>
change (sSort s) with (llift n k (sSort s))
| |- rlift ?n ?k ?t = sSort ?s =>
change (sSort s) with (rlift n k (sSort s))
| |- ?t{ ?n := ?u } = sSort ?s =>
change (sSort s) with ((sSort s){ n := u })
end.
Section Fundamental.
Context `{Sort_notion : Sorts.notion}.
Ltac cleannl :=
let inv h :=
inversion h ; subst ; clear h
in
let aux t h :=
destruct t ; cbn in h ; try discriminate h ; inv h
in
repeat match goal with
| h : context [ nl (sSort _) ] |- _ => cbn in h
| h : context [ nl (sProd _ _ _) ] |- _ => cbn in h
| h : context [ nl (sSum _ _ _) ] |- _ => cbn in h
| h : nlSort _ = nlSort _ |- _ => inv h
| h : nlProd _ _ = nlProd _ _ |- _ => inv h
| h : nlSum _ _ = nlSum _ _ |- _ => inv h
| h : nl ?t = nlSort _ |- _ => aux t h
| h : nlSort _ = nl ?t |- _ => aux t h
| h : nlProd _ _ = nl ?t |- _ => aux t h
| h : nl ?t = nlProd _ _ |- _ => aux t h
| h : nlSum _ _ = nl ?t |- _ => aux t h
| h : nl ?t = nlSum _ _ |- _ => aux t h
end.
Lemma trel_to_heq' :
forall {Σ t1 t2},
type_glob Σ ->
t1 ∼ t2 ->
forall Γ Γ1 Γ2,
∑ p,
forall Γm T1 T2,
ismix Σ Γ Γ1 Γ2 Γm ->
Σ ;;; Γ ,,, Γ1 |-i t1 : T1 ->
Σ ;;; Γ ,,, Γ2 |-i t2 : T2 ->
Σ ;;; Γ ,,, Γm |-i p : sHeq (llift0 #|Γm| T1)
(llift0 #|Γm| t1)
(rlift0 #|Γm| T2)
(rlift0 #|Γm| t2).
Proof.
intros Σ t1 t2 hg sim.
induction sim ; intros Γ Γ1 Γ2.
(* Variable *)
- case_eq (x <? #|Γ1|) ; intro e0 ; bprop e0.
+ (* The variable is located in the mixed part.
We use the available equality.
*)
exists (sProjTe (sRel x)).
intros Γm U1 U2 hm h1 h2.
unfold llift at 2. unfold rlift at 2.
case_eq (x <? 0) ; intro e ; bprop e ; try mylia. clear e2.
pose proof (mix_length1 hm) as ml. rewrite <- ml in e0, e1.
change (0 + #|Γm|)%nat with #|Γm|.
rewrite e0.
(* Now for the specifics *)
apply type_ProjTe' ; try assumption.
ttinv h1. ttinv h2.
rename H into en1, H1 into en2, H0 into hx1, H2 into hx2.
assert (is1' : x < #|Γ1|) by (erewrite mix_length1 in e1 ; eassumption).
assert (is2' : x < #|Γ2|) by (erewrite mix_length2 in e1 ; eassumption).
destruct (istype_type hg h1) as [s1 ?].
destruct (istype_type hg h2) as [s2 ?].
unfold ",,," in en1. rewrite nth_error_app1 in en1 by auto.
unfold ",,," in en2. rewrite nth_error_app1 in en2 by auto.
eapply ismix_nth_sort in hm as hm'. 2-4: eassumption.
destruct hm' as [ss [? ?]].
eapply type_rename.
* eapply type_Rel.
-- eapply (@wf_llift Sort_notion) with (Δ := []) ; try eassumption.
eapply typing_wf ; eassumption.
-- unfold ",,,". rewrite nth_error_app1 by auto.
eapply nth_error_mix. all: eassumption.
* cbn. f_equal.
-- rewrite lift_llift.
replace (S x + (#|Γm| - S x))%nat with #|Γm| by mylia.
eapply nl_llift. eassumption.
-- rewrite lift_rlift.
replace (S x + (#|Γm| - S x))%nat with #|Γm| by mylia.
eapply nl_rlift. eassumption.
+ (* Unless it is ill-typed, the variable is in Γ, reflexivity will do.
To type reflexivity properly we still need a proof that
x - #|Γ1| < #|Γ|. We have to consider both cases.
*)
case_eq ((x - #|Γ1|) <? #|Γ|) ; intro isdecl ; bprop isdecl.
* (* The variable is indeed in the context. *)
set (y := x - #|Γ1|) in *.
case_eq (nth_error Γ y).
2:{ intros e. apply nth_error_None in e. mylia. }
intros B en.
set (A := lift0 (S x) B).
exists (sHeqRefl A (sRel x)).
intros Γm U1 U2 hm h1 h2.
unfold llift at 2. unfold rlift at 2.
case_eq (x <? 0) ; intro e ; bprop e ; try mylia. clear e2.
pose proof (mix_length1 hm) as ml. rewrite <- ml in e0, e1.
change (0 + #|Γm|)%nat with #|Γm|.
rewrite e0.
(* Now for the specifics *)
assert (h1' : Σ ;;; Γ ,,, Γm |-i sRel x : llift0 #|Γm| U1).
{ replace (sRel x) with (llift0 #|Γm| (sRel x)).
2:{
unfold llift. rewrite e.
change (0 + #|Γm|)%nat with #|Γm|. rewrite e0.
reflexivity.
}
eapply type_llift0 ; eassumption.
}
assert (h2' : Σ ;;; Γ ,,, Γm |-i sRel x : rlift0 #|Γm| U2).
{ replace (sRel x) with (rlift0 #|Γm| (sRel x)).
2:{
unfold rlift. rewrite e.
change (0 + #|Γm|)%nat with #|Γm|. rewrite e0.
reflexivity.
}
eapply type_rlift0 ; eassumption.
}
pose proof (uniqueness hg h1' h2').
destruct (istype_type hg h1').
destruct (istype_type hg h2').
eapply type_rename.
-- eapply type_HeqRefl' ; try eassumption.
subst y A.
eapply type_Rel.
++ eapply typing_wf. eassumption.
++ unfold ",,,". rewrite nth_error_app2 by mylia.
rewrite ml. assumption.
-- cbn. ttinv h1'. ttinv h2'. f_equal.
++ subst y A. rewrite <- H3.
unfold ",,," in H2. rewrite nth_error_app2 in H2 by mylia.
rewrite ml in H2. rewrite en in H2. inversion H2. reflexivity.
++ subst y A. rewrite <- H5.
unfold ",,," in H4. rewrite nth_error_app2 in H4 by mylia.
rewrite ml in H4. rewrite en in H4. inversion H4. reflexivity.
* (* In case the variable isn't in the context at all,
it is bound to be ill-typed and we can return garbage.
*)
exists (sRel 0).
intros Γm U1 U2 hm h1 h2.
exfalso. ttinv h1. apply nth_error_Some_length in H.
unfold ",,," in H. rewrite length_cat in H. mylia.
(* Left transport *)
- destruct (IHsim Γ Γ1 Γ2) as [q hq].
exists (optHeqTrans (optHeqSym (optHeqTransport (llift0 #|Γ1| p) (llift0 #|Γ1| t1))) q).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1.
specialize (hq _ _ _ hm h h2).
destruct (istype_type hg hq) as [s' h'].
ttinv h'. cbn in h11. inversion h11. subst. clear h11.
eapply opt_HeqTrans ; try assumption.
+ eapply opt_HeqSym ; try assumption.
eapply type_rename.
* eapply opt_HeqTransport ; try assumption.
-- eapply type_llift0 ; eassumption.
-- instantiate (2 := s). instantiate (1 := llift0 #|Γm| U1).
change (sEq (sSort s) (llift0 #|Γm| T1) (llift0 #|Γm| U1))
with (llift0 #|Γm| (sEq (sSort s) T1 U1)).
eapply type_llift0 ; try eassumption.
eapply type_rename ; try eassumption.
cbn. f_equal. eauto.
* cbn. f_equal. f_equal. eapply nl_llift. eauto.
+ assumption.
(* Right transport *)
- destruct (IHsim Γ Γ1 Γ2) as [q hq].
exists (optHeqTrans q (optHeqTransport (rlift0 #|Γ1| p) (rlift0 #|Γ1| t2))).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h2.
specialize (hq _ _ _ hm h1 h).
destruct (istype_type hg hq) as [s' h'].
ttinv h'. cbn in h11. inversion h11. subst. clear h11.
cbn.
eapply opt_HeqTrans ; try assumption.
+ eassumption.
+ eapply type_rename.
* eapply opt_HeqTransport ; try eassumption.
instantiate (2 := s). instantiate (1 := rlift0 #|Γm| T2).
change (sEq (sSort s) (rlift0 #|Γm| T1) (rlift0 #|Γm| T2))
with (rlift0 #|Γm| (sEq (sSort s) T1 T2)).
eapply type_rlift0 ; eassumption.
* cbn. f_equal. eapply nl_rlift. eauto.
(* Prod *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
exists (optCongProd (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2) pA pB).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h0 h5).
destruct (istype_type hg hpA) as [s iA].
ttinv iA. cbn in h12. inversion h12. subst. clear h12.
cleannl.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h h3).
destruct (istype_type hg hpB) as [? iB]. ttinv iB.
cbn in h14. inversion h14. subst. clear h14.
assert (s3 = s2).
{ eapply sorts_in_sort ; eassumption. }
subst.
destruct (istype_type hg h1).
destruct (istype_type hg h2).
eapply type_rename.
+ eapply opt_CongProd ; try assumption.
* eassumption.
* rewrite llift_substProj, rlift_substProj.
apply hpB.
* lift_sort.
eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort.
eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. reflexivity.
(* Sum *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
exists (sCongSum (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2) pA pB).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h0 h5).
destruct (istype_type hg hpA) as [s iA].
ttinv iA. cbn in h12. inversion h12. subst. clear h12.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h h3).
destruct (istype_type hg hpB) as [? iB]. ttinv iB.
cbn in h16. inversion h16. subst. clear h16.
cleannl.
assert (s3 = s2).
{ eapply sorts_in_sort ; eassumption. }
subst.
destruct (istype_type hg h1).
destruct (istype_type hg h2).
eapply type_rename.
+ eapply type_CongSum' ; try assumption.
* eassumption.
* rewrite llift_substProj, rlift_substProj.
apply hpB.
* lift_sort.
eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort.
eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. reflexivity.
(* Eq *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ Γ1 Γ2) as [pu hpu].
destruct (IHsim3 Γ Γ1 Γ2) as [pv hpv].
exists (optCongEq pA pu pv).
intros Γm U1 U2 hm h1 h2.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h0 h6).
specialize (hpu _ _ _ hm h h4).
specialize (hpv _ _ _ hm h3 h7).
destruct (istype_type hg hpA) as [? ipA]. ttinv ipA.
cbn in h14. inversion h14. subst. clear h14.
cleannl.
assert (s0 = s).
{ eapply sorts_in_sort ; eassumption. }
subst.
eapply type_rename.
+ eapply opt_CongEq ; eassumption.
+ cbn. reflexivity.
(* Sort *)
- exists (sHeqRefl (sSort (Sorts.succ s)) (sSort s)).
intros Γm U1 U2 hm h1 h2.
ttinv h1. ttinv h2.
cleannl.
assert (hwf : wf Σ (Γ ,,, Γm)).
{ eapply (@wf_llift Sort_notion) with (Δ := []) ; try eassumption.
eapply typing_wf ; eassumption.
}
eapply type_rename.
+ eapply type_HeqRefl' ; try assumption.
apply type_Sort. eassumption.
+ reflexivity.
(* Lambda *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
destruct (IHsim3 Γ (Γ1,, A1) (Γ2,, A2)) as [pu hpu].
exists (optCongLambda (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2)
(llift #|Γ1| 1 u1) (rlift #|Γ1| 1 u2) pA pB pu).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h0 h6).
destruct (istype_type hg hpA) as [? iA]. ttinv iA.
cleannl.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h h4).
specialize (hpu _ _ _ hm' h3 h7).
assert (s3 = s2).
{ destruct (istype_type hg hpB) as [? ipB]. ttinv ipB.
cleannl. eapply sorts_in_sort ; eassumption.
} subst.
eapply type_rename.
+ eapply opt_CongLambda ; try assumption.
* eassumption.
* rewrite llift_substProj, rlift_substProj. apply hpB.
* rewrite !llift_substProj, !rlift_substProj. apply hpu.
* lift_sort.
eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort.
eapply (@type_rlift1 Sort_notion) ; eassumption.
* eapply (@type_llift1 Sort_notion) ; eassumption.
* eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. f_equal. all: f_equal.
all: try eapply nl_llift.
all: try eapply nl_rlift.
all: eauto.
(* App *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pu hpu].
destruct (IHsim2 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim3 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
destruct (IHsim4 Γ Γ1 Γ2) as [pv hpv].
exists (optCongApp (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2) pu pA pB pv).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpu _ _ _ hm h3 h8).
specialize (hpA _ _ _ hm h0 h7).
destruct (istype_type hg hpA) as [? iA].
ttinv iA. cleannl.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h h5).
specialize (hpv _ _ _ hm h4 h9).
assert (s3 = s2).
{ destruct (istype_type hg hpB) as [? ipB]. ttinv ipB.
cleannl. eapply sorts_in_sort ; eassumption.
} subst.
eapply type_rename.
+ eapply opt_CongApp ; try assumption.
* apply hpA.
* rewrite llift_substProj, rlift_substProj.
apply hpB.
* apply hpu.
* apply hpv.
* lift_sort.
eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort.
eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. rewrite <- llift_subst, <- rlift_subst. f_equal.
* cbn. eapply nl_llift. assumption.
* cbn. eapply nl_rlift. assumption.
(* Pair *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
destruct (IHsim3 Γ Γ1 Γ2) as [pu hpu].
destruct (IHsim4 Γ Γ1 Γ2) as [pv hpv].
clear IHsim1 IHsim2 IHsim3 IHsim4.
exists (sCongPair (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2) pA pB pu pv).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h0 h7).
destruct (istype_type hg hpA) as [? iA].
ttinv iA. cleannl.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h h5).
assert (s3 = s2).
{ destruct (istype_type hg hpB) as [? ipB]. ttinv ipB.
cleannl. eapply sorts_in_sort ; eassumption.
} subst.
specialize (hpu _ _ _ hm h3 h8).
specialize (hpv _ _ _ hm h4 h9).
eapply type_rename.
+ eapply type_CongPair' ; try assumption.
* apply hpA.
* rewrite llift_substProj, rlift_substProj.
apply hpB.
* apply hpu.
* replace 0 with (0 + 0)%nat in hpv by mylia.
rewrite llift_subst, rlift_subst in hpv.
apply hpv.
* lift_sort. eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort. eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. f_equal. all: f_equal.
all: try eapply nl_llift.
all: try eapply nl_rlift.
all: eauto.
(* Pi1 *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
destruct (IHsim3 Γ Γ1 Γ2) as [pp hpp].
exists (sCongPi1 (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2) pA pB pp).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h h4).
destruct (istype_type hg hpA) as [? iA].
ttinv iA. cleannl.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h3 h7).
assert (s3 = s2).
{ destruct (istype_type hg hpB) as [? ipB]. ttinv ipB.
cleannl. eapply sorts_in_sort ; eassumption.
} subst.
specialize (hpp _ _ _ hm h0 h6).
eapply type_rename.
+ eapply type_CongPi1' ; try assumption.
* apply hpA.
* rewrite llift_substProj, rlift_substProj.
apply hpB.
* apply hpp.
* lift_sort. eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort. eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. f_equal.
all: try eapply nl_llift.
all: try eapply nl_rlift.
all: eauto.
(* Pi2 *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ (Γ1,, A1) (Γ2,, A2)) as [pB hpB].
destruct (IHsim3 Γ Γ1 Γ2) as [pp hpp].
exists (sCongPi2 (llift #|Γ1| 1 B1) (rlift #|Γ1| 1 B2) pA pB pp).
intros Γm U1 U2 hm h1 h2.
pose proof (mix_length1 hm) as ml. rewrite <- ml.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h h4).
destruct (istype_type hg hpA) as [? iA].
ttinv iA. cleannl.
assert (s1 = s0).
{ eapply sorts_in_sort ; eassumption. }
subst.
assert (hm' :
ismix Σ Γ
(Γ1 ,, A1)
(Γ2 ,, A2)
(Γm ,, (sPack (llift0 #|Γm| A1) (rlift0 #|Γm| A2)))
).
{ econstructor ; eassumption. }
specialize (hpB _ _ _ hm' h3 h7).
assert (s3 = s2).
{ destruct (istype_type hg hpB) as [? ipB]. ttinv ipB.
cleannl. eapply sorts_in_sort ; eassumption.
} subst.
specialize (hpp _ _ _ hm h0 h6).
eapply type_rename.
+ eapply type_CongPi2' ; try assumption.
* apply hpA.
* rewrite llift_substProj, rlift_substProj.
apply hpB.
* apply hpp.
* lift_sort. eapply (@type_llift1 Sort_notion) ; eassumption.
* lift_sort. eapply (@type_rlift1 Sort_notion) ; eassumption.
+ cbn. f_equal.
* change (sPi1 (llift0 #|Γm| A1) (llift #|Γm| 1 B1) (llift0 #|Γm| p1))
with (llift0 #|Γm| (sPi1 A1 B1 p1)).
rewrite <- llift_subst.
eapply nl_llift. assumption.
* change (sPi1 (rlift0 #|Γm| A2) (rlift #|Γm| 1 B2) (rlift0 #|Γm| p2))
with (rlift0 #|Γm| (sPi1 A2 B2 p2)).
rewrite <- rlift_subst.
eapply nl_rlift. assumption.
(* Refl *)
- destruct (IHsim1 Γ Γ1 Γ2) as [pA hpA].
destruct (IHsim2 Γ Γ1 Γ2) as [pu hpu].
exists (optCongRefl pA pu).
intros Γm U1 U2 hm h1 h2.
ttinv h1. ttinv h2.
specialize (hpA _ _ _ hm h0 h5).
specialize (hpu _ _ _ hm h h3).
assert (s0 = s).
{ destruct (istype_type hg hpA) as [? ipA]. ttinv ipA.
cleannl. eapply sorts_in_sort ; eassumption.
} subst.
eapply type_rename.
+ eapply opt_CongRefl ; eassumption.
+ cbn. f_equal.
* change (nlEq (nl (llift0 #|Γm| A1)) (nl (llift0 #|Γm| u1)) (nl (llift0 #|Γm| u1)))
with (nl (llift0 #|Γm| (sEq A1 u1 u1))).
eapply nl_llift. assumption.
* change (nlEq (nl (rlift0 #|Γm| A2)) (nl (rlift0 #|Γm| u2)) (nl (rlift0 #|Γm| u2)))
with (nl (rlift0 #|Γm| (sEq A2 u2 u2))).
eapply nl_rlift. assumption.
(* Ax *)
- case_eq (lookup_glob Σ id).
+ (* The axiom is in the global context *)
intros ty eq.
exists (sHeqRefl ty (sAx id)).
intros Γm U1 U2 hm h1 h2.
assert (h1' : Σ ;;; Γ ,,, Γm |-i sAx id : llift0 #|Γm| U1).
{ change (sAx id) with (llift0 #|Γm| (sAx id)).
eapply type_llift0 ; eassumption.
}
assert (h2' : Σ ;;; Γ ,,, Γm |-i sAx id : rlift0 #|Γm| U2).
{ change (sAx id) with (rlift0 #|Γm| (sAx id)).
eapply type_rlift0 ; eassumption.
}
pose proof (uniqueness hg h1' h2').
destruct (istype_type hg h1).
destruct (istype_type hg h2).
eapply type_rename.
* eapply type_HeqRefl' ; try assumption.
econstructor ; try eassumption.
eapply typing_wf. eassumption.
* cbn. ttinv h1'. ttinv h2'. subst.
f_equal.
-- rewrite h0 in eq. inversion eq. subst. assumption.
-- rewrite h4 in eq. inversion eq. subst. assumption.
+ (* The axiom isn't declared. We return garbage. *)
intro neq.
exists (sRel 0).
intros Γm U1 U2 hm h1 h2.
exfalso. ttinv h1.
rewrite h0 in neq. discriminate neq.
Unshelve.
all: cbn ; try rewrite !length_cat ; try exact nAnon ; mylia.
Defined.
Corollary trel_to_heq :
forall {Σ} Γ {t1 t2 : sterm},
type_glob Σ ->
t1 ∼ t2 ->
∑ p,
forall T1 T2,
Σ ;;; Γ |-i t1 : T1 ->
Σ ;;; Γ |-i t2 : T2 ->
Σ ;;; Γ |-i p : sHeq T1 t1 T2 t2.
Proof.
intros Σ Γ t1 t2 hg h.
destruct (trel_to_heq' hg h Γ [] []) as [p hp].
exists p. intros T1 T2 h1 h2. specialize (hp _ _ _ (mixnil _ _) h1 h2).
cbn in hp. rewrite !llift00, !rlift00 in hp.
apply hp.
Defined.
Lemma inrel_lift :
forall {t t'},
t ⊏ t' ->
forall n k, lift n k t ⊏ lift n k t'.
Proof.
intros t t'. induction 1 ; intros m k.
all: try (cbn ; now constructor).
cbn. destruct (k <=? x) ; now constructor.
Defined.
Lemma inrel_subst :
forall {t t'},
t ⊏ t' ->
forall {u u'},
u ⊏ u' ->
forall n, t{ n := u } ⊏ t'{ n := u' }.
Proof.
intros t t'. induction 1 ; intros v1 v2 hu m.
all: try (cbn ; constructor ; easy).
cbn. destruct (m ?= x).
- now apply inrel_lift.
- constructor.
- constructor.
Defined.
Lemma inrel_nl :
forall {u u' v},
u ⊏ u' ->
nl u = nl v ->
v ⊏ u'.
Proof.
intros u u' v h eq.
revert v eq. induction h.
all: intros w eq.
2:{ constructor. eapply IHh. assumption. }
all: destruct w ; try discriminate eq.
all: cbn in eq ; inversion eq ; now constructor.
Defined.
Lemma trel_lift :
forall {t1 t2},
t1 ∼ t2 ->
forall n k, lift n k t1 ∼ lift n k t2.
Proof.
intros t1 t2. induction 1 ; intros n k.
all: try (cbn ; now constructor).
cbn. destruct (k <=? x) ; now constructor.
Defined.
Lemma trel_subst :
forall {t1 t2},
t1 ∼ t2 ->
forall {u1 u2},
u1 ∼ u2 ->
forall n, t1{ n := u1 } ∼ t2{ n := u2 }.
Proof.
intros t1 t2. induction 1 ; intros m1 m2 hu n.
all: try (cbn ; constructor ; easy).
unfold subst. destruct (n ?= x).
- now apply trel_lift.
- apply trel_Rel.
- apply trel_Rel.
Defined.
(* Reflexivity is restricted to the syntax that makes sense in ETT. *)
Lemma trel_refl :
forall {t},
Xcomp t ->
t ∼ t.
Proof.
intros t h. dependent induction h.
all: constructor. all: assumption.
Defined.
Lemma inrel_refl :
forall {t},
Xcomp t ->
t ⊏ t.
Proof.
intros t h. dependent induction h.
all: constructor. all: assumption.
Defined.
Lemma trel_sym : forall {t1 t2}, t1 ∼ t2 -> t2 ∼ t1.
Proof.
intros t1 t2. induction 1 ; (now constructor).
Defined.
Lemma inversion_trel_transport :
forall {A B p t1 t2},
sTransport A B p t1 ∼ t2 ->
t1 ∼ t2.
Proof.
intros A B p t1 t2 h.
dependent induction h.
- assumption.
- constructor. eapply IHh.
Defined.
Lemma trel_trans :
forall {t1 t2},
t1 ∼ t2 ->
forall {t3},
t2 ∼ t3 ->
t1 ∼ t3.
Proof.
intros t1 t2. induction 1 ; intros t3 h.
all: try (
dependent induction h ; [
constructor ; eapply IHh ; assumption
| now constructor
]
).
- constructor. now apply IHX.
- apply IHX. eapply inversion_trel_transport. eassumption.
Defined.
Reserved Notation " Γ ≈ Δ " (at level 19).
Inductive crel : scontext -> scontext -> Type :=
| crel_empty : nil ≈ nil
| crel_snoc Γ Δ t u : Γ ≈ Δ -> t ∼ u -> (Γ ,, t) ≈ (Δ ,, u)
where " Γ ≈ Δ " := (crel Γ Δ).
Reserved Notation " Γ ⊂ Γ' " (at level 19).
Inductive increl : scontext -> scontext -> Type :=
| increl_empty : nil ⊂ nil
| increl_snoc Γ Γ' T T' :
Γ ⊂ Γ' -> T ⊏ T' -> (Γ ,, T) ⊂ (Γ' ,, T')
where " Γ ⊂ Γ' " := (increl Γ Γ').
(*! Notion of translation *)
Definition trans Σ Γ A t Γ' A' t' :=
Γ ⊂ Γ' *
A ⊏ A' *
t ⊏ t' *
(Σ ;;; Γ' |-i t' : A').
End Fundamental.
Notation " Γ ≈ Δ " := (crel Γ Δ) (at level 19).
Notation " Γ ⊂ Γ' " := (increl Γ Γ') (at level 19).
Notation " Σ ;;;; Γ' ⊢ [ t' ] : A' ∈ ⟦ Γ ⊢ [ t ] : A ⟧ " :=
(trans Σ Γ A t Γ' A' t')
(at level 7) : i_scope.
Definition ctxtrans `{Sort_notion : Sorts.notion} Σ Γ Γ' :=
Γ ⊂ Γ' * (wf Σ Γ').
Notation " Σ |--i Γ' ∈ ⟦ Γ ⟧ " := (ctxtrans Σ Γ Γ') (at level 7) : i_scope.
Section Head.
Context `{Sort_notion : Sorts.notion}.
(* Notion of head *)
Inductive head_kind :=
| headRel
| headSort (s : sort)
| headProd
| headLambda
| headApp
| headSum
| headPi1
| headPi2
| headEq
| headRefl
| headJ
| headTransport
| headHeq
| headOther
.
Definition head (t : sterm) : head_kind :=
match t with