forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICEquality.v
1515 lines (1338 loc) · 47.3 KB
/
PCUICEquality.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
(* Distributed under the terms of the MIT license. *)
From Coq Require Import Bool String List Program BinPos Compare_dec Arith Lia
RelationClasses CRelationClasses CMorphisms Omega.
From MetaCoq.Template Require Import config utils Universes UnivSubst.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICReflect PCUICLiftSubst PCUICUnivSubst.
Require Import ssreflect.
From Equations Require Import Equations.
Require Import Equations.Prop.DepElim.
Set Equations With UIP.
Local Open Scope type_scope.
Section CRelationLemmas.
Local Set Universe Polymorphism.
Context {A : Type} (R : crelation A).
Lemma flip_Reflexive : Reflexive R -> Reflexive (flip R).
Proof.
intros HR x. unfold flip. apply reflexivity.
Qed.
Lemma flip_Symmetric : Symmetric R -> Symmetric (flip R).
Proof.
intros HR x y. unfold flip. apply symmetry.
Qed.
Lemma flip_Transitive : Transitive R -> Transitive (flip R).
Proof.
intros HR x y z xy yz.
unfold flip in *. now transitivity y.
Qed.
End CRelationLemmas.
Definition R_universe_instance R
:= fun u u' => Forall2 R (List.map Universe.make u) (List.map Universe.make u').
Lemma R_universe_instance_impl R R' :
RelationClasses.subrelation R R' ->
RelationClasses.subrelation (R_universe_instance R) (R_universe_instance R').
Proof.
intros H x y xy. eapply Forall2_impl ; tea.
Qed.
Lemma R_universe_instance_impl' R R' :
RelationClasses.subrelation R R' ->
forall u u', R_universe_instance R u u' -> R_universe_instance R' u u'.
Proof.
intros H x y xy. eapply Forall2_impl ; tea.
Qed.
(** ** Syntactic equality up-to universes
We don't look at printing annotations *)
Inductive eq_term_upto_univ (Re Rle : universe -> universe -> Prop) : term -> term -> Type :=
| eq_Rel n :
eq_term_upto_univ Re Rle (tRel n) (tRel n)
| eq_Evar e args args' :
All2 (eq_term_upto_univ Re Re) args args' ->
eq_term_upto_univ Re Rle (tEvar e args) (tEvar e args')
| eq_Var id :
eq_term_upto_univ Re Rle (tVar id) (tVar id)
| eq_Sort s s' :
Rle s s' ->
eq_term_upto_univ Re Rle (tSort s) (tSort s')
| eq_App t t' u u' :
eq_term_upto_univ Re Rle t t' ->
eq_term_upto_univ Re Re u u' ->
eq_term_upto_univ Re Rle (tApp t u) (tApp t' u')
| eq_Const c u u' :
R_universe_instance Re u u' ->
eq_term_upto_univ Re Rle (tConst c u) (tConst c u')
| eq_Ind i u u' :
R_universe_instance Re u u' ->
eq_term_upto_univ Re Rle (tInd i u) (tInd i u')
| eq_Construct i k u u' :
R_universe_instance Re u u' ->
eq_term_upto_univ Re Rle (tConstruct i k u) (tConstruct i k u')
| eq_Lambda na na' ty ty' t t' :
eq_term_upto_univ Re Re ty ty' ->
eq_term_upto_univ Re Rle t t' ->
eq_term_upto_univ Re Rle (tLambda na ty t) (tLambda na' ty' t')
| eq_Prod na na' a a' b b' :
eq_term_upto_univ Re Re a a' ->
eq_term_upto_univ Re Rle b b' ->
eq_term_upto_univ Re Rle (tProd na a b) (tProd na' a' b')
| eq_LetIn na na' t t' ty ty' u u' :
eq_term_upto_univ Re Re t t' ->
eq_term_upto_univ Re Re ty ty' ->
eq_term_upto_univ Re Rle u u' ->
eq_term_upto_univ Re Rle (tLetIn na t ty u) (tLetIn na' t' ty' u')
| eq_Case indn p p' c c' brs brs' :
eq_term_upto_univ Re Re p p' ->
eq_term_upto_univ Re Re c c' ->
All2 (fun x y =>
(fst x = fst y) *
eq_term_upto_univ Re Re (snd x) (snd y)
) brs brs' ->
eq_term_upto_univ Re Rle (tCase indn p c brs) (tCase indn p' c' brs')
| eq_Proj p c c' :
eq_term_upto_univ Re Re c c' ->
eq_term_upto_univ Re Rle (tProj p c) (tProj p c')
| eq_Fix mfix mfix' idx :
All2 (fun x y =>
eq_term_upto_univ Re Re x.(dtype) y.(dtype) *
eq_term_upto_univ Re Re x.(dbody) y.(dbody) *
(x.(rarg) = y.(rarg))
) mfix mfix' ->
eq_term_upto_univ Re Rle (tFix mfix idx) (tFix mfix' idx)
| eq_CoFix mfix mfix' idx :
All2 (fun x y =>
eq_term_upto_univ Re Re x.(dtype) y.(dtype) *
eq_term_upto_univ Re Re x.(dbody) y.(dbody) *
(x.(rarg) = y.(rarg))
) mfix mfix' ->
eq_term_upto_univ Re Rle (tCoFix mfix idx) (tCoFix mfix' idx).
(* ** Syntactic conversion up-to universes *)
Definition eq_term `{checker_flags} φ :=
eq_term_upto_univ (eq_universe φ) (eq_universe φ).
(* ** Syntactic cumulativity up-to universes *)
Definition leq_term `{checker_flags} φ :=
eq_term_upto_univ (eq_universe φ) (leq_universe φ).
(* TODO MOVE *)
Lemma Forall2_same :
forall A (P : A -> A -> Prop) l,
(forall x, P x x) ->
Forall2 P l l.
Proof.
intros A P l h.
induction l.
- constructor.
- constructor.
+ eapply h.
+ assumption.
Qed.
Instance eq_term_upto_univ_refl Re Rle :
RelationClasses.Reflexive Re ->
RelationClasses.Reflexive Rle ->
Reflexive (eq_term_upto_univ Re Rle).
Proof.
intros hRe hRle t.
induction t in Rle, hRle |- * using term_forall_list_ind.
all: simpl.
all: try constructor. all: eauto.
all: try solve [eapply All_All2 ; eauto].
all: try solve [eapply Forall2_same ; eauto].
- eapply All_All2; eauto. simpl. intuition eauto.
- eapply All_All2; eauto. simpl. intuition eauto.
Qed.
Instance eq_term_refl `{checker_flags} φ : Reflexive (eq_term φ).
Proof.
apply eq_term_upto_univ_refl. all: exact _.
Qed.
Instance leq_term_refl `{checker_flags} φ : Reflexive (leq_term φ).
Proof.
apply eq_term_upto_univ_refl; exact _.
Qed.
(* TODO MOVE *)
Existing Instance All2_symP.
(* TODO MOVE *)
Instance Forall2_symP :
forall A (P : A -> A -> Prop),
RelationClasses.Symmetric P ->
Symmetric (Forall2 P).
Proof.
intros A P h l l' hl.
induction hl. all: auto.
Qed.
Derive Signature for eq_term_upto_univ.
Instance eq_term_upto_univ_sym Re Rle :
RelationClasses.Symmetric Re ->
RelationClasses.Symmetric Rle ->
Symmetric (eq_term_upto_univ Re Rle).
Proof.
intros he hle u v e.
induction u in Rle, hle, v, e |- * using term_forall_list_ind.
all: dependent destruction e.
all: try solve [
econstructor ; eauto ;
try eapply Forall2_symP ; eauto
].
- econstructor.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h.
+ constructor.
+ destruct r as [h1 h2]. eapply h1 in h2 ; auto.
- econstructor; eauto.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h.
+ constructor.
+ destruct r as [h1 [h2 h3]]. eapply h1 in h3 ; auto.
- econstructor.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h.
+ constructor.
+ destruct r as [[h1 h2] [[h3 h4] h5]].
eapply h1 in h3 ; auto.
- econstructor.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h.
+ constructor.
+ destruct r as [[h1 h2] [[h3 h4] h5]]. eapply h1 in h3 ; auto.
Qed.
Instance eq_term_sym `{checker_flags} φ : Symmetric (eq_term φ).
Proof.
eapply eq_term_upto_univ_sym. all: exact _.
Qed.
Instance eq_term_upto_univ_trans Re Rle :
RelationClasses.Transitive Re ->
RelationClasses.Transitive Rle ->
Transitive (eq_term_upto_univ Re Rle).
Proof.
intros he hle u v w e1 e2.
induction u in Rle, hle, v, w, e1, e2 |- * using term_forall_list_ind.
all: dependent destruction e1.
all: try solve [ eauto ].
all: try solve [
dependent destruction e2 ; econstructor ; eauto ;
try eapply Forall2_trans ; eauto
].
- dependent destruction e2.
econstructor.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h in a0, args'0 |- *.
+ assumption.
+ dependent destruction a0. constructor ; eauto.
destruct r as [h1 h2]. eauto.
- dependent destruction e2.
econstructor; eauto.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h in a0, brs'0 |- *.
+ assumption.
+ dependent destruction a0. constructor ; eauto.
destruct r as [h1 [h2 h3]].
destruct p0 as [? ?]. split; eauto.
transitivity (fst y); auto.
- dependent destruction e2.
econstructor.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h in a0, mfix'0 |- *.
+ assumption.
+ dependent destruction a0. constructor ; eauto.
intuition eauto.
transitivity (rarg y); auto.
- dependent destruction e2.
econstructor.
eapply All2_All_mix_left in X as h; eauto.
clear a X.
induction h in a0, mfix'0 |- *.
+ assumption.
+ dependent destruction a0. constructor ; eauto.
intuition eauto.
transitivity (rarg y); auto.
Qed.
Instance eq_term_trans {cf:checker_flags} φ : Transitive (eq_term φ).
Proof.
eapply eq_term_upto_univ_trans. all: exact _.
Qed.
Instance leq_term_trans {cf:checker_flags} φ : Transitive (leq_term φ).
Proof.
eapply eq_term_upto_univ_trans ; exact _.
Qed.
Instance eq_term_upto_univ_equiv Re (hRe : RelationClasses.Equivalence Re)
: Equivalence (eq_term_upto_univ Re Re).
Proof.
constructor. all: exact _.
Defined.
Instance eq_term_equiv {cf:checker_flags} φ : Equivalence (eq_term φ) :=
{| Equivalence_Reflexive := eq_term_refl _;
Equivalence_Symmetric := eq_term_sym _;
Equivalence_Transitive := eq_term_trans _ |}.
Instance leq_term_preorder {cf:checker_flags} φ : PreOrder (leq_term φ) :=
{| PreOrder_Reflexive := leq_term_refl _;
PreOrder_Transitive := leq_term_trans _ |}.
(* TODO MOVE *)
Lemma Forall2_sym :
forall A (P : A -> A -> Prop) l l',
Forall2 P l l' ->
Forall2 (fun x y => P y x) l' l.
Proof.
intros A P l l' h.
induction h.
- constructor.
- constructor. all: auto.
Qed.
Instance R_universe_instance_equiv R (hR : RelationClasses.Equivalence R)
: RelationClasses.Equivalence (R_universe_instance R).
Proof.
split.
- intro. apply Forall2_same. reflexivity.
- intros x y xy. eapply Forall2_sym, Forall2_impl; tea. now symmetry.
- intros x y z xy yz. eapply Forall2_trans; tea. apply hR.
Qed.
Lemma R_universe_instance_antisym Re Rle (hRe : RelationClasses.Equivalence Re) :
RelationClasses.Antisymmetric _ Re Rle ->
RelationClasses.Antisymmetric _ (R_universe_instance Re) (R_universe_instance Rle).
Proof.
intros H x y H1 H2.
eapply Forall2_sym in H2.
eapply Forall2_impl; [eapply Forall2_and|]; [exact H1|exact H2|].
cbn; intros ? ? [? ?]. eapply H; assumption.
Qed.
Lemma eq_term_upto_univ_antisym Re Rle (hRe : RelationClasses.Equivalence Re) :
RelationClasses.Antisymmetric _ Re Rle ->
Antisymmetric (eq_term_upto_univ Re Re) (eq_term_upto_univ Re Rle).
Proof.
intros hR u v h h'.
induction u in v, h, h' |- * using term_forall_list_ind.
all: simpl ; inversion h ; subst; inversion h' ;
subst ; try constructor ; auto.
Qed.
Instance leq_term_antisym {cf:checker_flags} φ
: Antisymmetric (eq_term φ) (leq_term φ).
Proof.
eapply eq_term_upto_univ_antisym; exact _.
Qed.
Instance eq_term_upto_univ_impl Re Re' Rle Rle' :
RelationClasses.subrelation Re Re' ->
RelationClasses.subrelation Rle Rle' ->
subrelation (eq_term_upto_univ Re Rle) (eq_term_upto_univ Re' Rle').
Proof.
intros he hle t t'.
induction t in t', Rle, Rle', hle |- * using term_forall_list_ind;
try (inversion 1; subst; constructor;
eauto using R_universe_instance_impl'; fail).
- inversion 1; subst; constructor.
eapply All2_impl'; tea.
eapply All_impl; eauto.
- inversion 1; subst; constructor; eauto.
eapply All2_impl'; tea.
eapply All_impl; eauto.
cbn. intros x ? y [? ?]. split; eauto.
- inversion 1; subst; constructor.
eapply All2_impl'; tea.
eapply All_impl; eauto.
cbn. intros x [? ?] y [[? ?] ?]. repeat split; eauto.
- inversion 1; subst; constructor.
eapply All2_impl'; tea.
eapply All_impl; eauto.
cbn. intros x [? ?] y [[? ?] ?]. repeat split; eauto.
Qed.
Instance eq_term_upto_univ_leq Re Rle :
RelationClasses.subrelation Re Rle ->
subrelation (eq_term_upto_univ Re Re) (eq_term_upto_univ Re Rle).
Proof.
intros H. eapply eq_term_upto_univ_impl; exact _.
Qed.
Instance eq_term_leq_term {cf:checker_flags} φ
: subrelation (eq_term φ) (leq_term φ).
Proof.
eapply eq_term_upto_univ_leq; exact _.
Qed.
Instance leq_term_partial_order {cf:checker_flags} φ
: PartialOrder (eq_term φ) (leq_term φ).
Proof.
split. intros eqxy; split; now eapply eq_term_leq_term.
intros [xy yx].
now eapply leq_term_antisym.
Qed.
Local Ltac lih :=
lazymatch goal with
| ih : forall Rle v n k, eq_term_upto_univ _ _ ?u _ -> _
|- eq_term_upto_univ _ _ (lift _ _ ?u) _ =>
eapply ih
end.
Lemma eq_term_upto_univ_lift Re Rle n k :
Proper (eq_term_upto_univ Re Rle ==> eq_term_upto_univ Re Rle) (lift n k).
Proof.
intros u v e.
induction u in v, n, k, e, Rle |- * using term_forall_list_ind.
all: dependent destruction e.
all: try (cbn ; constructor ; try lih ; assumption).
- cbn. destruct (Nat.leb_spec0 k n0).
+ constructor.
+ constructor.
- cbn. constructor. solve_all.
- cbn. constructor ; try lih ; try assumption. solve_all.
- cbn. constructor.
pose proof (All2_length _ _ a).
solve_all. rewrite H. eauto.
- cbn. constructor.
pose proof (All2_length _ _ a).
solve_all. rewrite H. eauto.
Qed.
Lemma lift_eq_term {cf:checker_flags} φ n k T U :
eq_term φ T U -> eq_term φ (lift n k T) (lift n k U).
Proof.
apply eq_term_upto_univ_lift.
Qed.
Lemma lift_leq_term {cf:checker_flags} φ n k T U :
leq_term φ T U -> leq_term φ (lift n k T) (lift n k U).
Proof.
apply eq_term_upto_univ_lift.
Qed.
Local Ltac sih :=
lazymatch goal with
| ih : forall Rle v n x y, _ -> eq_term_upto_univ _ _ ?u _ -> _ -> _
|- eq_term_upto_univ _ _ (subst _ _ ?u) _ => eapply ih
end.
Lemma eq_term_upto_univ_substs Re Rle :
RelationClasses.subrelation Re Rle ->
forall u v n l l',
eq_term_upto_univ Re Rle u v ->
All2 (eq_term_upto_univ Re Re) l l' ->
eq_term_upto_univ Re Rle (subst l n u) (subst l' n v).
Proof.
unfold RelationClasses.subrelation; intros hR u v n l l' hu hl.
induction u in v, n, l, l', hu, hl, Rle, hR |- * using term_forall_list_ind.
all: dependent destruction hu.
all: try solve [ cbn ; constructor ; try sih ; eauto ].
- cbn. destruct (Nat.leb_spec0 n n0).
+ case_eq (nth_error l (n0 - n)).
* intros t e. eapply All2_nth_error_Some in e as h ; eauto.
destruct h as [t' [e' h]].
rewrite e'.
eapply eq_term_upto_univ_lift.
eapply eq_term_upto_univ_leq ; eauto.
* intros h. eapply All2_nth_error_None in h as hh ; eauto.
rewrite hh.
apply All2_length in hl as e. rewrite <- e.
constructor.
+ constructor.
- cbn. constructor. solve_all.
- cbn. constructor ; try sih ; eauto. solve_all.
- cbn. constructor ; try sih ; eauto.
pose proof (All2_length _ _ a).
solve_all. now rewrite H.
- cbn. constructor ; try sih ; eauto.
pose proof (All2_length _ _ a).
solve_all. now rewrite H.
Qed.
Lemma eq_term_upto_univ_subst Re Rle :
RelationClasses.subrelation Re Rle ->
forall u v n x y,
eq_term_upto_univ Re Rle u v ->
eq_term_upto_univ Re Re x y ->
eq_term_upto_univ Re Rle (u{n := x}) (v{n := y}).
Proof.
intros hR u v n x y e1 e2.
eapply eq_term_upto_univ_substs; eauto.
Qed.
Lemma subst_eq_term {cf:checker_flags} φ l k T U :
eq_term φ T U ->
eq_term φ (subst l k T) (subst l k U).
Proof.
intros Hleq.
eapply eq_term_upto_univ_substs; try easy.
now eapply All2_same.
Qed.
Lemma subst_leq_term {cf:checker_flags} φ l k T U :
leq_term φ T U ->
leq_term φ (subst l k T) (subst l k U).
Proof.
intros Hleq.
eapply eq_term_upto_univ_substs; try easy.
intro; apply eq_universe_leq_universe.
now eapply All2_same.
Qed.
(** ** Boolean version ** *)
Fixpoint eqb_term_upto_univ (equ lequ : universe -> universe -> bool) (u v : term) : bool :=
match u, v with
| tRel n, tRel m =>
eqb n m
| tEvar e args, tEvar e' args' =>
eqb e e' &&
forallb2 (eqb_term_upto_univ equ equ) args args'
| tVar id, tVar id' =>
eqb id id'
| tSort u, tSort u' =>
lequ u u'
| tApp u v, tApp u' v' =>
eqb_term_upto_univ equ lequ u u' &&
eqb_term_upto_univ equ equ v v'
| tConst c u, tConst c' u' =>
eqb c c' &&
forallb2 equ (map Universe.make u) (map Universe.make u')
| tInd i u, tInd i' u' =>
eqb i i' &&
forallb2 equ (map Universe.make u) (map Universe.make u')
| tConstruct i k u, tConstruct i' k' u' =>
eqb i i' &&
eqb k k' &&
forallb2 equ (map Universe.make u) (map Universe.make u')
| tLambda na A t, tLambda na' A' t' =>
eqb_term_upto_univ equ equ A A' &&
eqb_term_upto_univ equ lequ t t'
| tProd na A B, tProd na' A' B' =>
eqb_term_upto_univ equ equ A A' &&
eqb_term_upto_univ equ lequ B B'
| tLetIn na B b u, tLetIn na' B' b' u' =>
eqb_term_upto_univ equ equ B B' &&
eqb_term_upto_univ equ equ b b' &&
eqb_term_upto_univ equ lequ u u'
| tCase indp p c brs, tCase indp' p' c' brs' =>
eqb indp indp' &&
eqb_term_upto_univ equ equ p p' &&
eqb_term_upto_univ equ equ c c' &&
forallb2 (fun x y =>
eqb (fst x) (fst y) &&
eqb_term_upto_univ equ equ (snd x) (snd y)
) brs brs'
| tProj p c, tProj p' c' =>
eqb p p' &&
eqb_term_upto_univ equ equ c c'
| tFix mfix idx, tFix mfix' idx' =>
eqb idx idx' &&
forallb2 (fun x y =>
eqb_term_upto_univ equ equ x.(dtype) y.(dtype) &&
eqb_term_upto_univ equ equ x.(dbody) y.(dbody) &&
eqb x.(rarg) y.(rarg)
) mfix mfix'
| tCoFix mfix idx, tCoFix mfix' idx' =>
eqb idx idx' &&
forallb2 (fun x y =>
eqb_term_upto_univ equ equ x.(dtype) y.(dtype) &&
eqb_term_upto_univ equ equ x.(dbody) y.(dbody) &&
eqb x.(rarg) y.(rarg)
) mfix mfix'
| _, _ => false
end.
Ltac eqspec :=
lazymatch goal with
| |- context [ eqb ?u ?v ] =>
destruct (eqb_spec u v) ; nodec ; subst
end.
Ltac eqspecs :=
repeat eqspec.
Local Ltac equspec equ h :=
repeat lazymatch goal with
| |- context [ equ ?x ?y ] =>
destruct (h x y) ; nodec ; subst
end.
Local Ltac ih :=
repeat lazymatch goal with
| ih : forall lequ Rle hle t', reflectT (eq_term_upto_univ _ _ ?t _) _,
hle : forall u u', reflectT (?Rle u u') (?lequ u u')
|- context [ eqb_term_upto_univ _ ?lequ ?t ?t' ] =>
destruct (ih lequ Rle hle t') ; nodec ; subst
end.
(* TODO MOVE *)
Lemma forallb2_Forall2 :
forall A (p : A -> A -> bool) l l',
forallb2 p l l' ->
Forall2 (fun x y => p x y) l l'.
Proof.
intros A p l l' h.
induction l in l', h |- *.
- destruct l'. 2: discriminate.
constructor.
- destruct l'. 1: discriminate.
simpl in h. apply andP in h as [? ?].
constructor. all: auto.
Qed.
Lemma eqb_term_upto_univ_impl (equ lequ : _ -> _ -> bool) Re Rle :
RelationClasses.subrelation equ Re ->
RelationClasses.subrelation lequ Rle ->
subrelation (eqb_term_upto_univ equ lequ) (eq_term_upto_univ Re Rle).
Proof.
intros he hle t t'.
induction t in t', lequ, Rle, hle |- * using term_forall_list_ind.
all: destruct t'; try discriminate 1. all: cbn -[eqb].
- eqspec; [intros _|discriminate]. constructor.
- eqspec; [intros _|discriminate]. constructor.
- eqspec; [|discriminate]. constructor.
cbn in H. apply forallb2_All2 in H.
eapply All2_impl'; tea.
eapply All_impl; tea. eauto.
- constructor; eauto.
- intro. toProp. constructor; eauto.
- intro. toProp. constructor; eauto.
- intro. toProp. constructor; eauto.
- intro. toProp. constructor; eauto.
- unfold kername in *. eqspec; [|discriminate].
intro. toProp. constructor; eauto.
apply forallb2_Forall2 in H0.
eapply Forall2_impl; tea; eauto.
- unfold kername in *. eqspec; [|discriminate].
intro. toProp. constructor; eauto.
apply forallb2_Forall2 in H0.
eapply Forall2_impl; tea; eauto.
- unfold kername in *. eqspec; [|discriminate].
eqspec; [|discriminate].
intro. toProp. constructor; eauto.
apply forallb2_Forall2 in H0.
eapply Forall2_impl; tea; eauto.
- eqspec; [|discriminate]. intro. toProp.
destruct indn. econstructor; eauto.
apply forallb2_All2 in H0.
eapply All2_impl'; tea.
red in X. eapply All_impl; tea.
cbn -[eqb]. intros x X0 y. eqspec; [|discriminate].
intro. split; eauto.
- eqspec; [|discriminate]. intro. constructor; eauto.
- eqspec; [|discriminate].
econstructor; eauto.
cbn -[eqb] in H; apply forallb2_All2 in H.
eapply All2_impl'; tea.
red in X. eapply All_impl; tea.
cbn -[eqb]. intros x X0 y. eqspec; [|rewrite andb_false_r; discriminate].
intro. toProp. split; tas. split; eapply X0; tea.
- eqspec; [|discriminate].
econstructor; eauto.
cbn -[eqb] in H; apply forallb2_All2 in H.
eapply All2_impl'; tea.
red in X. eapply All_impl; tea.
cbn -[eqb]. intros x X0 y. eqspec; [|rewrite andb_false_r; discriminate].
intro. toProp. split; tas. split; eapply X0; tea.
Qed.
Lemma reflect_eq_term_upto_univ equ lequ (Re Rle : universe -> universe -> Prop) :
(forall u u', reflectT (Re u u') (equ u u')) ->
(forall u u', reflectT (Rle u u') (lequ u u')) ->
forall t t', reflectT (eq_term_upto_univ Re Rle t t')
(eqb_term_upto_univ equ lequ t t').
Proof.
intros he hle t t'.
induction t in t', lequ, Rle, hle |- * using term_forall_list_ind.
all: destruct t' ; nodec.
(* all: try solve [ *)
(* cbn - [eqb] ; eqspecs ; equspec equ h ; ih ; *)
(* constructor ; constructor ; assumption *)
(* ]. *)
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
cbn.
induction X in l0 |- *.
+ destruct l0.
* constructor. constructor. constructor.
* constructor. intro bot. inversion bot. subst.
inversion H0.
+ destruct l0.
* constructor. intro bot. inversion bot. subst.
inversion H0.
* cbn. destruct (p _ _ he t).
-- destruct (IHX l0).
++ constructor. constructor. constructor ; try assumption.
inversion e0. subst. assumption.
++ constructor. intro bot. inversion bot. subst.
inversion H0. subst.
apply f. constructor. assumption.
-- constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst. assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor. assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb].
pose proof (eqb_spec s k) as H.
match goal with
| |- context G[ eqb ?x ?y ] =>
set (toto := eqb x y) in * ;
let G' := context G[toto] in
change G'
end.
destruct H ; nodec. subst.
equspec equ he. equspec lequ hle. ih.
cbn. induction u in ui |- *.
+ destruct ui.
* constructor. constructor. constructor.
* constructor. intro bot. inversion bot. subst. inversion H0.
+ destruct ui.
* constructor. intro bot. inversion bot. subst. inversion H0.
* cbn. equspec equ he. equspec lequ hle.
-- cbn. destruct (IHu ui).
++ constructor. constructor.
inversion e. subst.
constructor ; assumption.
++ constructor. intro bot. apply f.
inversion bot. subst. constructor. inversion H0.
subst. assumption.
-- constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst.
assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
simpl. induction u in ui |- *.
+ destruct ui.
* constructor. constructor. constructor.
* constructor. intro bot. inversion bot. subst. inversion H0.
+ destruct ui.
* constructor. intro bot. inversion bot. subst. inversion H0.
* cbn. equspec equ he. equspec lequ hle.
-- cbn. destruct (IHu ui).
++ constructor. constructor.
inversion e. subst.
constructor ; assumption.
++ constructor. intro bot. apply f.
inversion bot. subst. constructor. inversion H0.
subst. assumption.
-- constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst.
assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
simpl. induction u in ui |- *.
+ destruct ui.
* constructor. constructor. constructor.
* constructor. intro bot. inversion bot. subst. inversion H0.
+ destruct ui.
* constructor. intro bot. inversion bot. subst. inversion H0.
* cbn. equspec equ he. equspec lequ hle.
-- cbn. destruct (IHu ui).
++ constructor. constructor.
inversion e. subst.
constructor ; assumption.
++ constructor. intro bot. apply f.
inversion bot. subst. constructor. inversion H0.
subst. assumption.
-- constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst.
assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
cbn - [eqb].
destruct indn as [i n].
induction l in brs, X |- *.
+ destruct brs.
* constructor. constructor ; try assumption.
constructor.
* constructor. intro bot. inversion bot. subst. inversion H8.
+ destruct brs.
* constructor. intro bot. inversion bot. subst. inversion H8.
* cbn - [eqb]. inversion X. subst.
destruct a, p. cbn - [eqb]. eqspecs.
-- cbn - [eqb]. pose proof (X0 equ Re he t0) as hh. cbn in hh.
destruct hh.
++ cbn - [eqb].
destruct (IHl X1 brs).
** constructor. constructor ; try assumption.
constructor ; try easy.
inversion e2. subst. assumption.
** constructor. intro bot. apply f. inversion bot. subst.
constructor ; try assumption.
inversion H8. subst. assumption.
++ constructor. intro bot. apply f. inversion bot. subst.
inversion H8. subst. destruct H3. assumption.
-- constructor. intro bot. inversion bot. subst.
inversion H8. subst. destruct H3. cbn in e1. subst.
apply n2. reflexivity.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
constructor. constructor ; assumption.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
cbn - [eqb]. induction m in X, mfix |- *.
+ destruct mfix.
* constructor. constructor. constructor.
* constructor. intro bot. inversion bot. subst. inversion H0.
+ destruct mfix.
* constructor. intro bot. inversion bot. subst. inversion H0.
* cbn - [eqb]. inversion X. subst.
destruct X0 as [h1 h2].
destruct (h1 equ Re he (dtype d)).
-- destruct (h2 equ Re he (dbody d)).
++ cbn - [eqb]. eqspecs.
** cbn - [eqb]. destruct (IHm X1 mfix).
--- constructor. constructor. constructor ; try easy.
inversion e2. assumption.
--- constructor. intro bot. apply f.
inversion bot. subst. constructor.
inversion H0. subst. assumption.
** constructor. intro bot. inversion bot. subst.
apply n. inversion H0. subst. destruct H3 as [[? ?] ?].
assumption.
++ constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst.
apply H3.
-- constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst. apply H3.
- cbn - [eqb]. eqspecs. equspec equ he. equspec lequ hle. ih.
cbn - [eqb]. induction m in X, mfix |- *.
+ destruct mfix.
* constructor. constructor. constructor.
* constructor. intro bot. inversion bot. subst. inversion H0.
+ destruct mfix.
* constructor. intro bot. inversion bot. subst. inversion H0.
* cbn - [eqb]. inversion X. subst.
destruct X0 as [h1 h2].
destruct (h1 equ Re he (dtype d)).
-- destruct (h2 equ Re he (dbody d)).
++ cbn - [eqb]. eqspecs.
** cbn - [eqb]. destruct (IHm X1 mfix).
--- constructor. constructor. constructor ; try easy.
inversion e2. assumption.
--- constructor. intro bot. apply f.
inversion bot. subst. constructor.
inversion H0. subst. assumption.
** constructor. intro bot. inversion bot. subst.
apply n. inversion H0. subst. destruct H3 as [[? ?] ?].
assumption.
++ constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst.
apply H3.
-- constructor. intro bot. apply f.
inversion bot. subst. inversion H0. subst. apply H3.
Qed.
(** ** Behavior on mkApps and it_mkLambda_or_LetIn ** *)
Lemma eq_term_upto_univ_mkApps Re Rle u1 l1 u2 l2 :
eq_term_upto_univ Re Rle u1 u2 ->
All2 (eq_term_upto_univ Re Re) l1 l2 ->
eq_term_upto_univ Re Rle (mkApps u1 l1) (mkApps u2 l2).
Proof.
intros hu hl. induction l1 in u1, u2, l2, hu, hl |- *.
- inversion hl. subst. assumption.
- inversion hl. subst. simpl.
eapply IHl1.
+ constructor. all: assumption.
+ assumption.
Qed.
Lemma eq_term_upto_univ_mkApps_l_inv Re Rle u l t :
eq_term_upto_univ Re Rle (mkApps u l) t ->
∑ u' l',
eq_term_upto_univ Re Rle u u' *
All2 (eq_term_upto_univ Re Re) l l' *
(t = mkApps u' l').
Proof.
intros h. induction l in u, t, h, Rle |- *.
- cbn in h. exists t, []. split ; auto.
- cbn in h. apply IHl in h as [u' [l' [[h1 h2] h3]]].
dependent destruction h1. subst.
eexists. eexists. split; [ split | ].
+ eassumption.
+ constructor.
* eassumption.
* eassumption.
+ cbn. reflexivity.
Qed.
Lemma eq_term_upto_univ_mkApps_r_inv Re Rle u l t :
eq_term_upto_univ Re Rle t (mkApps u l) ->
∑ u' l',
eq_term_upto_univ Re Rle u' u *
All2 (eq_term_upto_univ Re Re) l' l *
(t = mkApps u' l').
Proof.
intros h. induction l in u, t, h, Rle |- *.
- cbn in h. exists t, []. split ; auto.
- cbn in h. apply IHl in h as [u' [l' [[h1 h2] h3]]].
dependent destruction h1. subst.
eexists. eexists. split; [ split | ].
+ eassumption.
+ constructor.
* eassumption.
* eassumption.
+ cbn. reflexivity.
Qed.
Lemma eq_term_upto_univ_it_mkLambda_or_LetIn Re Rle Γ :
RelationClasses.Reflexive Re ->
respectful (eq_term_upto_univ Re Rle) (eq_term_upto_univ Re Rle)
(it_mkLambda_or_LetIn Γ) (it_mkLambda_or_LetIn Γ).
Proof.
intros he u v h.
induction Γ as [| [na [b|] A] Γ ih ] in u, v, h |- *.
- assumption.
- simpl. cbn. apply ih. constructor ; try apply eq_term_upto_univ_refl.
all: auto.
- simpl. cbn. apply ih. constructor ; try apply eq_term_upto_univ_refl.
all: auto.
Qed.
Lemma eq_term_it_mkLambda_or_LetIn {cf:checker_flags} φ Γ :
respectful (eq_term φ) (eq_term φ)
(it_mkLambda_or_LetIn Γ) (it_mkLambda_or_LetIn Γ).
Proof.
apply eq_term_upto_univ_it_mkLambda_or_LetIn; exact _.
Qed.
Lemma eq_term_upto_univ_it_mkProd_or_LetIn Re Rle Γ :
RelationClasses.Reflexive Re ->
respectful (eq_term_upto_univ Re Rle) (eq_term_upto_univ Re Rle)
(it_mkProd_or_LetIn Γ) (it_mkProd_or_LetIn Γ).
Proof.
intros he u v h.
induction Γ as [| [na [b|] A] Γ ih ] in u, v, h |- *.
- assumption.
- simpl. cbn. apply ih. constructor ; try apply eq_term_upto_univ_refl.
all: auto.
- simpl. cbn. apply ih. constructor ; try apply eq_term_upto_univ_refl.
all: auto.
Qed.
Lemma eq_term_it_mkProd_or_LetIn {cf:checker_flags} φ Γ:
respectful (eq_term φ) (eq_term φ)
(it_mkProd_or_LetIn Γ) (it_mkProd_or_LetIn Γ).
Proof.
eapply eq_term_upto_univ_it_mkProd_or_LetIn ; exact _.
Qed.
Lemma eq_term_it_mkLambda_or_LetIn_inv {cf:checker_flags} φ Γ u v :
eq_term φ (it_mkLambda_or_LetIn Γ u) (it_mkLambda_or_LetIn Γ v) ->
eq_term φ u v.
Proof.
revert u v. induction Γ as [| [na [b|] A] Γ ih ] ; intros u v h.
- assumption.
- simpl in h. cbn in h. apply ih in h. inversion h. subst.
assumption.
- simpl in h. cbn in h. apply ih in h. inversion h. subst.