forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICConfluence.v
2743 lines (2547 loc) · 97.5 KB
/
PCUICConfluence.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. *)
Set Warnings "-notation-overridden".
Require Import ssreflect ssrbool.
From MetaCoq Require Import LibHypsNaming.
From Equations Require Import Equations.
From Coq Require Import Bool String List Program BinPos Compare_dec Omega Utf8 String Lia.
From MetaCoq.Template Require Import config utils.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICLiftSubst PCUICUnivSubst PCUICTyping PCUICReduction PCUICWeakening
PCUICSubstitution PCUICEquality PCUICReflect PCUICClosed
PCUICParallelReduction PCUICParallelReductionConfluence
PCUICCumulativity.
(* Type-valued relations. *)
Require Import CRelationClasses.
Require Import Equations.Prop.DepElim.
Require Import Equations.Type.Relation Equations.Type.Relation_Properties.
Ltac tc := try typeclasses eauto 10.
Set Asymmetric Patterns.
Lemma red1_eq_context_upto_l Σ Re Γ Δ u v :
RelationClasses.Reflexive Re ->
SubstUnivPreserving Re ->
red1 Σ Γ u v ->
eq_context_upto Re Γ Δ ->
∑ v', red1 Σ Δ u v' *
eq_term_upto_univ Re Re v v'.
Proof.
intros he he' h e.
induction h in Δ, e |- * using red1_ind_all.
all: try solve [
eexists ; split ; [
solve [ econstructor ; eauto ]
| eapply eq_term_upto_univ_refl ; eauto
]
].
all: try solve [
destruct (IHh _ e) as [? [? ?]] ;
eexists ; split ; [
solve [ econstructor ; eauto ]
| constructor; eauto ;
eapply eq_term_upto_univ_refl ; eauto
]
].
all: try solve [
match goal with
| r : red1 _ (?Γ ,, ?d) _ _ |- _ =>
assert (e' : eq_context_upto Re (Γ,, d) (Δ,, d)) ; [
constructor ; eauto ;
eapply eq_term_upto_univ_refl ; eauto
|
]
end ;
destruct (IHh _ e') as [? [? ?]] ;
eexists ; split ; [
solve [ econstructor ; eauto ]
| constructor ; eauto ;
eapply eq_term_upto_univ_refl ; eauto
]
].
- assert (h : ∑ b',
(option_map decl_body (nth_error Δ i) = Some (Some b')) *
eq_term_upto_univ Re Re body b').
{ induction i in Γ, Δ, H, e |- *.
- destruct e.
+ cbn in *. discriminate.
+ simpl in *. discriminate.
+ simpl in *. inversion H. subst. clear H.
eexists. split ; try constructor; eauto.
- destruct e.
+ cbn in *. discriminate.
+ simpl in *. eapply IHi in H ; eauto.
+ simpl in *. eapply IHi in H ; eauto.
}
destruct h as [b' [e1 e2]].
eexists. split.
+ constructor. eassumption.
+ eapply eq_term_upto_univ_lift ; eauto.
- destruct (IHh _ e) as [? [? ?]].
eexists. split.
+ solve [ econstructor ; eauto ].
+ destruct ind.
econstructor ; eauto.
* eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_same.
intros. split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
- destruct (IHh _ e) as [? [? ?]].
eexists. split.
+ solve [ econstructor ; eauto ].
+ destruct ind.
econstructor ; eauto.
* eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_same.
intros. split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
- destruct ind.
assert (h : ∑ brs0,
OnOne2 (on_Trel_eq (red1 Σ Δ) snd fst) brs brs0 *
All2 (fun x y =>
(fst x = fst y) *
eq_term_upto_univ Re Re (snd x) (snd y))%type
brs' brs0
).
{ induction X.
- destruct p0 as [[p1 p2] p3].
eapply p2 in e as hh.
destruct hh as [? [? ?]].
eexists. split.
+ constructor.
instantiate (1 := (_,_)).
split ; eauto.
+ constructor.
* split ; eauto.
* eapply All2_same.
intros. split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
- destruct IHX as [brs0 [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor.
* split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
* eassumption.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply case_red_brs. eassumption.
+ econstructor. all: try eapply eq_term_upto_univ_refl ; eauto.
- assert (h : ∑ ll,
OnOne2 (red1 Σ Δ) l ll *
All2 (eq_term_upto_univ Re Re) l' ll
).
{ induction X.
- destruct p as [p1 p2].
eapply p2 in e as hh. destruct hh as [? [? ?]].
eexists. split.
+ constructor. eassumption.
+ constructor.
* assumption.
* eapply All2_same.
intros.
eapply eq_term_upto_univ_refl ; eauto.
- destruct IHX as [ll [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply evar_red. eassumption.
+ constructor. assumption.
- assert (h : ∑ mfix',
OnOne2 (fun d d' =>
red1 Σ Δ d.(dtype) d'.(dtype) ×
(d.(dname), d.(dbody), d.(rarg)) =
(d'.(dname), d'.(dbody), d'.(rarg))
) mfix0 mfix'
*
All2 (fun x y =>
eq_term_upto_univ Re Re (dtype x) (dtype y) *
eq_term_upto_univ Re Re (dbody x) (dbody y) *
(rarg x = rarg y))%type mfix1 mfix').
{ induction X.
- destruct p as [[p1 p2] p3].
eapply p2 in e as hh. destruct hh as [? [? ?]].
eexists. split.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
split ; eauto.
+ constructor.
* simpl. repeat split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_same.
intros. repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
- destruct IHX as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor ; eauto.
repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply fix_red_ty. eassumption.
+ constructor. assumption.
- assert (h : ∑ mfix',
OnOne2 (fun d d' =>
red1 Σ (Δ ,,, fix_context mfix0) d.(dbody) d'.(dbody) ×
(d.(dname), d.(dtype), d.(rarg)) =
(d'.(dname), d'.(dtype), d'.(rarg))
) mfix0 mfix' *
All2 (fun x y =>
eq_term_upto_univ Re Re (dtype x) (dtype y) *
eq_term_upto_univ Re Re (dbody x) (dbody y) *
(rarg x = rarg y))%type mfix1 mfix').
{ (* Maybe we should use a lemma using firstn or skipn to keep
fix_context intact. Anything general?
*)
Fail induction X using OnOne2_ind_l.
(* This FAILs because it reduces the type of X before unifying
unfortunately...
*)
change (
OnOne2
((fun L (x y : def term) =>
(red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (forall Δ : context,
eq_context_upto Re (Γ ,,, fix_context L) Δ ->
∑ v' : term,
red1 Σ Δ (dbody x) v' × eq_term_upto_univ Re Re (dbody y) v'))
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) mfix0) mfix0 mfix1
) in X.
Fail induction X using OnOne2_ind_l.
revert mfix0 mfix1 X.
refine (OnOne2_ind_l _ (fun (L : mfixpoint term) (x y : def term) =>
((red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (forall Δ0 : context,
eq_context_upto Re (Γ ,,, fix_context L) Δ0 ->
∑ v' : term,
red1 Σ Δ0 (dbody x) v' × eq_term_upto_univ Re Re (dbody y) v'))
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)))
(fun L mfix0 mfix1 o => ∑ mfix' : list (def term),
OnOne2
(fun d d' : def term =>
red1 Σ (Δ ,,, fix_context L) (dbody d) (dbody d')
× (dname d, dtype d, rarg d) = (dname d', dtype d', rarg d')) mfix0 mfix'
× All2
(fun x y : def term =>
(eq_term_upto_univ Re Re (dtype x) (dtype y)
× eq_term_upto_univ Re Re (dbody x) (dbody y)) ×
rarg x = rarg y) mfix1 mfix') _ _).
- intros L x y l [[p1 p2] p3].
assert (
e' : eq_context_upto Re (Γ ,,, fix_context L) (Δ ,,, fix_context L)
).
{ eapply eq_context_upto_cat ; eauto.
eapply eq_context_upto_refl. assumption.
}
eapply p2 in e' as hh. destruct hh as [? [? ?]].
eexists. constructor.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
split ; eauto.
+ constructor.
* simpl. repeat split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_same. intros.
repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
- intros L x l l' h [? [? ?]].
eexists. constructor.
+ eapply OnOne2_tl. eassumption.
+ constructor ; eauto.
repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply fix_red_body. eassumption.
+ constructor. assumption.
- assert (h : ∑ mfix',
OnOne2 (fun d d' =>
red1 Σ Δ d.(dtype) d'.(dtype) ×
(d.(dname), d.(dbody), d.(rarg)) =
(d'.(dname), d'.(dbody), d'.(rarg))
) mfix0 mfix' *
All2 (fun x y =>
eq_term_upto_univ Re Re (dtype x) (dtype y) *
eq_term_upto_univ Re Re (dbody x) (dbody y) *
(rarg x = rarg y))%type mfix1 mfix'
).
{ induction X.
- destruct p as [[p1 p2] p3].
eapply p2 in e as hh. destruct hh as [? [? ?]].
eexists. split.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
split ; eauto.
+ constructor.
* simpl. repeat split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_same.
intros. repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
- destruct IHX as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor ; eauto.
repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply cofix_red_ty. eassumption.
+ constructor. assumption.
- assert (h : ∑ mfix',
OnOne2 (fun d d' =>
red1 Σ (Δ ,,, fix_context mfix0) d.(dbody) d'.(dbody) ×
(d.(dname), d.(dtype), d.(rarg)) =
(d'.(dname), d'.(dtype), d'.(rarg))
) mfix0 mfix' *
All2 (fun x y =>
eq_term_upto_univ Re Re (dtype x) (dtype y) *
eq_term_upto_univ Re Re (dbody x) (dbody y) *
(rarg x = rarg y))%type mfix1 mfix').
{ (* Maybe we should use a lemma using firstn or skipn to keep
fix_context intact. Anything general?
*)
Fail induction X using OnOne2_ind_l.
(* This FAILs because it reduces the type of X before unifying
unfortunately...
*)
change (
OnOne2
((fun L (x y : def term) =>
(red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (forall Δ : context,
eq_context_upto Re (Γ ,,, fix_context L) Δ ->
∑ v' : term,
red1 Σ Δ (dbody x) v' × eq_term_upto_univ Re Re (dbody y) v' ))
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) mfix0) mfix0 mfix1
) in X.
Fail induction X using OnOne2_ind_l.
revert mfix0 mfix1 X.
refine (OnOne2_ind_l _ (fun (L : mfixpoint term) (x y : def term) =>
(red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (forall Δ0 : context,
eq_context_upto Re (Γ ,,, fix_context L) Δ0 ->
∑ v' : term,
red1 Σ Δ0 (dbody x) v' × eq_term_upto_univ Re Re (dbody y) v' ))
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) (fun L mfix0 mfix1 o => ∑ mfix' : list (def term),
(OnOne2
(fun d d' : def term =>
red1 Σ (Δ ,,, fix_context L) (dbody d) (dbody d')
× (dname d, dtype d, rarg d) = (dname d', dtype d', rarg d')) mfix0 mfix'
× All2
(fun x y : def term =>
(eq_term_upto_univ Re Re (dtype x) (dtype y)
× eq_term_upto_univ Re Re (dbody x) (dbody y)) ×
rarg x = rarg y) mfix1 mfix')) _ _).
- intros L x y l [[p1 p2] p3].
assert (
e' : eq_context_upto Re (Γ ,,, fix_context L) (Δ ,,, fix_context L)
).
{ eapply eq_context_upto_cat ; eauto.
eapply eq_context_upto_refl. assumption.
}
eapply p2 in e' as hh. destruct hh as [? [? ?]].
eexists. constructor.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
split ; eauto.
+ constructor.
* simpl. repeat split ; eauto.
eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_same. intros.
repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
- intros L x l l' h [? [? ?]].
eexists. constructor.
+ eapply OnOne2_tl. eassumption.
+ constructor ; eauto.
repeat split ; eauto.
all: eapply eq_term_upto_univ_refl ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply cofix_red_body. eassumption.
+ constructor. assumption.
Qed.
Lemma red1_eq_term_upto_univ_l Σ Re Rle Γ u v u' :
RelationClasses.Reflexive Re ->
SubstUnivPreserving Re ->
RelationClasses.Reflexive Rle ->
RelationClasses.Transitive Re ->
RelationClasses.Transitive Rle ->
RelationClasses.subrelation Re Rle ->
eq_term_upto_univ Re Rle u u' ->
red1 Σ Γ u v ->
∑ v', red1 Σ Γ u' v' *
eq_term_upto_univ Re Rle v v'.
Proof.
unfold RelationClasses.subrelation.
intros he he' hle tRe tRle hR e h.
induction h in u', e, tRle, Rle, hle, hR |- * using red1_ind_all.
all: try solve [
dependent destruction e ;
edestruct IHh as [? [? ?]] ; [ .. | eassumption | ] ; eauto ;
eexists ; split ; [
solve [ econstructor ; eauto ]
| constructor ; eauto
]
].
(* tLambda and tProd *)
10,13: solve [
dependent destruction e ;
edestruct IHh as [? [? ?]] ; [ .. | eassumption | ] ; eauto ;
clear h ;
lazymatch goal with
| r : red1 _ (?Γ,, vass ?na ?A) ?u ?v,
e : eq_term_upto_univ _ _ ?A ?B
|- _ =>
let hh := fresh "hh" in
eapply red1_eq_context_upto_l in r as hh ; revgoals ; [
eapply eq_context_vass (* with (nb := na) *) ; [
eapply e
| eapply eq_context_upto_refl ; eauto
]
| assumption
| assumption
| destruct hh as [? [? ?]]
]
end ;
eexists ; split ; [
solve [ econstructor ; eauto ]
| constructor ; eauto ;
eapply eq_term_upto_univ_trans ; eauto ;
eapply eq_term_upto_univ_leq ; eauto
]
].
- dependent destruction e. dependent destruction e1.
eexists. split.
+ constructor.
+ eapply eq_term_upto_univ_subst ; eauto.
- dependent destruction e.
eexists. split.
+ constructor.
+ eapply eq_term_upto_univ_subst ; assumption.
- dependent destruction e.
eexists. split.
+ constructor. eassumption.
+ eapply eq_term_upto_univ_refl ; assumption.
- dependent destruction e.
apply eq_term_upto_univ_mkApps_l_inv in e2 as [? [? [[h1 h2] h3]]]. subst.
dependent destruction h1.
eexists. split.
+ constructor.
+ eapply eq_term_upto_univ_mkApps.
* eapply All2_nth
with (P := fun x y => eq_term_upto_univ Re Rle (snd x) (snd y)).
-- solve_all.
eapply eq_term_upto_univ_leq ; eauto.
-- cbn. eapply eq_term_upto_univ_refl ; eauto.
* eapply All2_skipn. assumption.
- apply eq_term_upto_univ_mkApps_l_inv in e as [? [? [[h1 h2] h3]]]. subst.
dependent destruction h1.
unfold unfold_fix in H.
case_eq (nth_error mfix idx) ;
try (intros e ; rewrite e in H ; discriminate H).
intros d e. rewrite e in H. inversion H. subst. clear H.
eapply All2_nth_error_Some in a as hh ; try eassumption.
destruct hh as [d' [e' [[? ?] erarg]]].
unfold is_constructor in H0.
destruct (isLambda (dbody d)) eqn:isl; noconf H2.
case_eq (nth_error args (rarg d)) ;
try (intros bot ; rewrite bot in H0 ; discriminate H0).
intros a' ea.
rewrite ea in H0.
eapply All2_nth_error_Some in ea as hh ; try eassumption.
destruct hh as [a'' [ea' ?]].
eexists. split.
+ eapply red_fix.
* unfold unfold_fix. rewrite e'.
erewrite isLambda_eq_term_l; eauto.
* unfold is_constructor. rewrite <- erarg. rewrite ea'.
eapply isConstruct_app_eq_term_l ; eassumption.
+ eapply eq_term_upto_univ_mkApps.
* eapply eq_term_upto_univ_substs ; eauto.
-- eapply eq_term_upto_univ_leq ; eauto.
-- unfold fix_subst.
apply All2_length in a as el. rewrite <- el.
generalize #|mfix|. intro n.
induction n.
++ constructor.
++ constructor ; eauto.
constructor. assumption.
* assumption.
- dependent destruction e.
apply eq_term_upto_univ_mkApps_l_inv in e2 as [? [? [[h1 h2] h3]]]. subst.
dependent destruction h1.
unfold unfold_cofix in H.
case_eq (nth_error mfix idx) ;
try (intros e ; rewrite e in H ; discriminate H).
intros d e. rewrite e in H. inversion H. subst. clear H.
eapply All2_nth_error_Some in e as hh ; try eassumption.
destruct hh as [d' [e' [[? ?] erarg]]].
eexists. split.
+ eapply red_cofix_case.
unfold unfold_cofix. rewrite e'. reflexivity.
+ constructor. all: eauto.
eapply eq_term_upto_univ_mkApps. all: eauto.
eapply eq_term_upto_univ_substs ; eauto; try exact _.
unfold cofix_subst.
apply All2_length in a0 as el. rewrite <- el.
generalize #|mfix|. intro n.
induction n.
* constructor.
* constructor ; eauto.
constructor. assumption.
- dependent destruction e.
apply eq_term_upto_univ_mkApps_l_inv in e as [? [? [[h1 h2] h3]]]. subst.
dependent destruction h1.
unfold unfold_cofix in H.
case_eq (nth_error mfix idx) ;
try (intros e ; rewrite e in H ; discriminate H).
intros d e. rewrite e in H. inversion H. subst. clear H.
eapply All2_nth_error_Some in e as hh ; try eassumption.
destruct hh as [d' [e' [[? ?] erarg]]].
eexists. split.
+ eapply red_cofix_proj.
unfold unfold_cofix. rewrite e'. reflexivity.
+ constructor.
eapply eq_term_upto_univ_mkApps. all: eauto.
eapply eq_term_upto_univ_substs ; eauto; try exact _.
unfold cofix_subst.
apply All2_length in a as el. rewrite <- el.
generalize #|mfix|. intro n.
induction n.
* constructor.
* constructor ; eauto.
constructor. assumption.
- dependent destruction e.
eexists. split.
+ econstructor. all: eauto.
+ eapply eq_term_upto_univ_leq; tas.
now apply eq_term_upto_univ_subst_instance_constr.
- dependent destruction e.
apply eq_term_upto_univ_mkApps_l_inv in e as [? [? [[h1 h2] h3]]]. subst.
dependent destruction h1.
eapply All2_nth_error_Some in h2 as hh ; try eassumption.
destruct hh as [arg' [e' ?]].
eexists. split.
+ constructor. eassumption.
+ eapply eq_term_upto_univ_leq ; eauto.
- dependent destruction e.
edestruct IHh as [? [? ?]] ; [ .. | eassumption | ] ; eauto.
clear h.
lazymatch goal with
| r : red1 _ (?Γ,, vdef ?na ?a ?A) ?u ?v,
e1 : eq_term_upto_univ _ _ ?A ?B,
e2 : eq_term_upto_univ _ _ ?a ?b
|- _ =>
let hh := fresh "hh" in
eapply red1_eq_context_upto_l in r as hh ; revgoals ; [
eapply eq_context_vdef (* with (nb := na) *) ; [
eapply e2
| eapply e1
| eapply eq_context_upto_refl ; eauto
]
| assumption
| assumption
| destruct hh as [? [? ?]]
]
end.
eexists. split.
+ eapply letin_red_body ; eauto.
+ constructor ; eauto.
eapply eq_term_upto_univ_trans ; eauto.
eapply eq_term_upto_univ_leq ; eauto.
- dependent destruction e.
assert (h : ∑ brs0,
OnOne2 (on_Trel_eq (red1 Σ Γ) snd fst) brs'0 brs0 *
All2 (fun x y =>
(fst x = fst y) *
(eq_term_upto_univ Re Re (snd x) (snd y))
)%type brs' brs0
).
{ induction X in a, brs'0 |- *.
- destruct p0 as [[p1 p2] p3].
dependent destruction a. destruct p0 as [h1 h2].
eapply p2 in h2 as hh ; eauto.
destruct hh as [? [? ?]].
eexists. split.
+ constructor.
instantiate (1 := (_, _)). cbn. split ; eauto.
+ constructor. all: eauto.
split ; eauto. cbn. transitivity (fst hd) ; eauto.
- dependent destruction a.
destruct (IHX _ a) as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor. all: eauto.
}
destruct h as [brs0 [? ?]].
eexists. split.
+ eapply case_red_brs. eassumption.
+ constructor. all: eauto.
- dependent destruction e.
assert (h : ∑ args,
OnOne2 (red1 Σ Γ) args' args *
All2 (eq_term_upto_univ Re Re) l' args
).
{ induction X in a, args' |- *.
- destruct p as [p1 p2].
dependent destruction a.
eapply p2 in e as hh ; eauto.
destruct hh as [? [? ?]].
eexists. split.
+ constructor. eassumption.
+ constructor. all: eauto.
- dependent destruction a.
destruct (IHX _ a) as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor. all: eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply evar_red. eassumption.
+ constructor. all: eauto.
- dependent destruction e.
assert (h : ∑ mfix,
OnOne2 (fun d0 d1 =>
red1 Σ Γ d0.(dtype) d1.(dtype) ×
(d0.(dname), d0.(dbody), d0.(rarg)) =
(d1.(dname), d1.(dbody), d1.(rarg))
) mfix' mfix *
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)))%type mfix1 mfix
).
{ induction X in a, mfix' |- *.
- destruct p as [[p1 p2] p3].
dependent destruction a.
destruct p as [[h1 h2] h3].
eapply p2 in h1 as hh ; eauto.
destruct hh as [? [? ?]].
eexists. split.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
simpl. eauto.
+ constructor. all: eauto.
simpl. inversion p3.
repeat split ; eauto.
- dependent destruction a. destruct p as [[h1 h2] h3].
destruct (IHX _ a) as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor. all: eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply fix_red_ty. eassumption.
+ constructor. all: eauto.
- dependent destruction e.
assert (h : ∑ mfix,
OnOne2 (fun x y =>
red1 Σ (Γ ,,, fix_context mfix0) x.(dbody) y.(dbody) ×
(dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)
) mfix' mfix *
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))
) mfix1 mfix
).
{ revert mfix' a.
refine (OnOne2_ind_l _ (fun L x y => (red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (forall Rle (u' : term),
RelationClasses.Reflexive Rle ->
RelationClasses.Transitive Rle ->
(forall u u'0 : universe, Re u u'0 -> Rle u u'0) ->
eq_term_upto_univ Re Rle (dbody x) u' ->
∑ v' : term,
red1 Σ (Γ ,,, fix_context L) u' v'
× eq_term_upto_univ Re Rle (dbody y) v' ))
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) (fun L mfix0 mfix1 o => forall mfix', All2
(fun x y : def term =>
(eq_term_upto_univ Re Re (dtype x) (dtype y)
× eq_term_upto_univ Re Re (dbody x) (dbody y)) ×
rarg x = rarg y) mfix0 mfix' -> ∑ mfix : list (def term),
( OnOne2
(fun x y : def term =>
red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) mfix' mfix ) *
( All2
(fun x y : def term =>
(eq_term_upto_univ Re Re (dtype x) (dtype y)
× eq_term_upto_univ Re Re (dbody x) (dbody y)) ×
rarg x = rarg y) mfix1 mfix )) _ _ _ _ X).
- clear X. intros L x y l [[p1 p2] p3] mfix' h.
dependent destruction h. destruct p as [[h1 h2] h3].
eapply p2 in h2 as hh ; eauto.
destruct hh as [? [? ?]].
eexists. split.
+ constructor. constructor.
instantiate (1 := mkdef _ _ _ _ _).
simpl. eauto. reflexivity.
+ constructor. constructor; simpl. all: eauto.
inversion p3.
simpl. repeat split ; eauto. destruct y0. simpl in *.
congruence.
- clear X. intros L x l l' h ih mfix' ha.
dependent destruction ha. destruct p as [[h1 h2] h3].
destruct (ih _ ha) as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eauto.
+ constructor. constructor. all: eauto.
}
destruct h as [mfix [? ?]].
assert (h : ∑ mfix,
OnOne2 (fun x y =>
red1 Σ (Γ ,,, fix_context mfix') x.(dbody) y.(dbody) ×
(dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)
) mfix' mfix ×
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))
) mfix1 mfix %type
).
{ clear X.
assert (hc : eq_context_upto
Re
(Γ ,,, fix_context mfix0)
(Γ ,,, fix_context mfix')
).
{ eapply eq_context_upto_cat.
- eapply eq_context_upto_refl. assumption.
- clear - a. induction a.
+ constructor.
+ destruct r as [[? ?] ?].
eapply All2_eq_context_upto.
eapply All2_rev.
eapply All2_mapi.
constructor.
* intros i. split.
-- cbn. constructor.
-- cbn. eapply eq_term_upto_univ_lift. eauto.
* eapply All2_impl ; eauto.
intros ? ? [[? ?] ?] i. split.
-- cbn. constructor.
-- cbn. eapply eq_term_upto_univ_lift. eauto.
}
clear a.
eapply OnOne2_impl_exist_and_All ; try eassumption.
clear o a0.
intros x x' y [r e] [[? ?] ?].
inversion e. clear e.
eapply red1_eq_context_upto_l in r as [? [? ?]].
3: eassumption. all: tea.
eexists. constructor.
instantiate (1 := mkdef _ _ _ _ _). simpl.
intuition eauto.
intuition eauto.
- rewrite H1. eauto.
- eapply eq_term_upto_univ_trans ; eassumption.
- etransitivity ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply fix_red_body. eassumption.
+ constructor. all: eauto.
- dependent destruction e.
assert (h : ∑ mfix,
OnOne2 (fun d0 d1 =>
red1 Σ Γ d0.(dtype) d1.(dtype) ×
(d0.(dname), d0.(dbody), d0.(rarg)) =
(d1.(dname), d1.(dbody), d1.(rarg))
) mfix' mfix *
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)))%type mfix1 mfix
).
{ induction X in a, mfix' |- *.
- destruct p as [[p1 p2] p3].
dependent destruction a.
destruct p as [[h1 h2] h3].
eapply p2 in h1 as hh ; eauto.
destruct hh as [? [? ?]].
eexists. split.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
simpl. eauto.
+ constructor. all: eauto.
simpl. inversion p3.
repeat split ; eauto.
- dependent destruction a. destruct p as [[h1 h2] h3].
destruct (IHX _ a) as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eassumption.
+ constructor. all: eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply cofix_red_ty. eassumption.
+ constructor. all: eauto.
- dependent destruction e.
assert (h : ∑ mfix,
OnOne2 (fun x y =>
red1 Σ (Γ ,,, fix_context mfix0) x.(dbody) y.(dbody) ×
(dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)
) mfix' mfix *
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))
) mfix1 mfix
).
{ revert mfix' a.
refine (OnOne2_ind_l _ (fun L x y => (red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (forall Rle (u' : term),
RelationClasses.Reflexive Rle ->
RelationClasses.Transitive Rle ->
(forall u u'0 : universe, Re u u'0 -> Rle u u'0) ->
eq_term_upto_univ Re Rle (dbody x) u' ->
∑ v' : term,
red1 Σ (Γ ,,, fix_context L) u' v'
× eq_term_upto_univ Re Rle (dbody y) v'))
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) (fun L mfix0 mfix1 o => forall mfix', All2
(fun x y : def term =>
(eq_term_upto_univ Re Re (dtype x) (dtype y)
× eq_term_upto_univ Re Re (dbody x) (dbody y)) ×
rarg x = rarg y) mfix0 mfix' -> ∑ mfix : list (def term),
( OnOne2
(fun x y : def term =>
red1 Σ (Γ ,,, fix_context L) (dbody x) (dbody y)
× (dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)) mfix' mfix ) *
( All2
(fun x y : def term =>
(eq_term_upto_univ Re Re (dtype x) (dtype y)
× eq_term_upto_univ Re Re (dbody x) (dbody y)) ×
rarg x = rarg y) mfix1 mfix )) _ _ _ _ X).
- clear X. intros L x y l [[p1 p2] p3] mfix' h.
dependent destruction h. destruct p as [[h1 h2] h3].
eapply p2 in h2 as hh ; eauto.
destruct hh as [? [? ?]].
noconf p3. hnf in H. noconf H.
eexists. split; simpl.
+ constructor.
instantiate (1 := mkdef _ _ _ _ _).
simpl. eauto.
+ constructor. all: eauto.
simpl. repeat split ; eauto; congruence.
- clear X. intros L x l l' h ih mfix' ha.
dependent destruction ha. destruct p as [[h1 h2] h3].
destruct (ih _ ha) as [? [? ?]].
eexists. split.
+ eapply OnOne2_tl. eauto.
+ constructor. all: eauto.
}
destruct h as [mfix [? ?]].
assert (h : ∑ mfix,
OnOne2 (fun x y =>
red1 Σ (Γ ,,, fix_context mfix') x.(dbody) y.(dbody) ×
(dname x, dtype x, rarg x) = (dname y, dtype y, rarg y)
) mfix' mfix ×
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))
) mfix1 mfix
).
{ clear X.
assert (hc : eq_context_upto
Re
(Γ ,,, fix_context mfix0)
(Γ ,,, fix_context mfix')
).
{ eapply eq_context_upto_cat.
- eapply eq_context_upto_refl. assumption.
- clear - a. induction a.
+ constructor.
+ destruct r as [[? ?] ?].
eapply All2_eq_context_upto.
eapply All2_rev.
eapply All2_mapi.
constructor.
* intros i. split.
-- cbn. constructor.
-- cbn. eapply eq_term_upto_univ_lift. eauto.
* eapply All2_impl ; eauto.
intros ? ? [[? ?] ?] i. split.
-- cbn. constructor.
-- cbn. eapply eq_term_upto_univ_lift. eauto.
}
clear a.
eapply OnOne2_impl_exist_and_All ; try eassumption.
clear o a0.
intros x x' y [r e] [[? ?] ?].
inversion e. clear e.
eapply red1_eq_context_upto_l in r as [? [? ?]].
3: eassumption. all: tea.
eexists.
instantiate (1 := mkdef _ _ _ _ _). simpl.
intuition eauto.
- rewrite H1. eauto.
- eapply eq_term_upto_univ_trans ; eassumption.
- etransitivity ; eauto.
}
destruct h as [? [? ?]].
eexists. split.
+ eapply cofix_red_body. eassumption.
+ constructor. all: eauto.
Qed.
Lemma red1_eq_context_upto_r Σ Re Γ Δ u v :
RelationClasses.Reflexive Re ->
RelationClasses.Symmetric Re ->
SubstUnivPreserving Re ->
red1 Σ Γ u v ->
eq_context_upto Re Δ Γ ->
∑ v', red1 Σ Δ u v' *
eq_term_upto_univ Re Re v' v.
Proof.
intros.
destruct (red1_eq_context_upto_l Σ Re Γ Δ u v); auto.
now apply eq_context_upto_sym.
exists x. intuition auto.
now eapply eq_term_upto_univ_sym.
Qed.
Lemma red1_eq_term_upto_univ_r Σ Re Rle Γ u v u' :
RelationClasses.Reflexive Re ->
SubstUnivPreserving Re ->
RelationClasses.Reflexive Rle ->
RelationClasses.Symmetric Re ->
RelationClasses.Transitive Re ->
RelationClasses.Transitive Rle ->
RelationClasses.subrelation Re Rle ->
eq_term_upto_univ Re Rle u' u ->
red1 Σ Γ u v ->
∑ v', red1 Σ Γ u' v' ×
eq_term_upto_univ Re Rle v' v.
Proof.
intros he he' hle tRe tRle hR e h uv.
destruct (red1_eq_term_upto_univ_l Σ Re (flip Rle) Γ u v u'); auto.
- now eapply flip_Transitive.
- intros x y X. symmetry in X. apply e. auto.
- eapply eq_term_upto_univ_flip; eauto.
- exists x. intuition auto.
eapply (eq_term_upto_univ_flip Re (flip Rle) Rle); eauto.
+ now eapply flip_Transitive.
+ unfold flip. intros ? ? H. symmetry in H. eauto.
Qed.
Section RedEq.
Context (Σ : global_env_ext).
Context {Re Rle : universe -> universe -> Prop}
{refl : RelationClasses.Reflexive Re}
{refl': RelationClasses.Reflexive Rle}
{pres : SubstUnivPreserving Re}
{sym : RelationClasses.Symmetric Re}
{trre : RelationClasses.Transitive Re}
{trle : RelationClasses.Transitive Rle}.
Context (inclre : RelationClasses.subrelation Re Rle).
Lemma red_eq_term_upto_univ_r {Γ T V U} :
eq_term_upto_univ Re Rle T U -> red Σ Γ U V ->
∑ T', red Σ Γ T T' * eq_term_upto_univ Re Rle T' V.
Proof.
intros eq r.
apply red_alt in r.
induction r in T, eq |- *.
- eapply red1_eq_term_upto_univ_r in eq as [v' [r' eq']]; eauto.
- exists T; split; eauto.
- case: (IHr1 _ eq) => T' [r' eq'].
case: (IHr2 _ eq') => T'' [r'' eq''].
exists T''. split=>//.
now transitivity T'.
Qed.
Lemma red_eq_term_upto_univ_l {Γ u v u'} :
eq_term_upto_univ Re Rle u u' ->
red Σ Γ u v ->
∑ v',
red Σ Γ u' v' *
eq_term_upto_univ Re Rle v v'.
Proof.
intros eq r.
eapply red_alt in r.
induction r in u', eq |- *.
- eapply red1_eq_term_upto_univ_l in eq as [v' [r' eq']]; eauto.
- exists u'. split; auto.
- case: (IHr1 _ eq) => T' [r' eq'].
case: (IHr2 _ eq') => T'' [r'' eq''].
exists T''. split=>//.
now transitivity T'.
Qed.
End RedEq.
(* Using Derive makes Qed break?? *)
(** FIXME Equations Derive Bug *)
(* Polymorphic Derive Signature for Relation.clos_refl_trans. *)
Set Universe Polymorphism.
Definition clos_refl_trans_sig@{i j} (A : Type@{i}) (R : Relation.relation A)
(index : sigma (fun _ : A => A)) : Type@{j} :=
Relation.clos_refl_trans@{i j} R (pr1 index) (pr2 index).