forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICSafeConversion.v
3317 lines (3124 loc) · 115 KB
/
PCUICSafeConversion.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
Classes.RelationClasses Omega.
From MetaCoq.Template Require Import config Universes monad_utils utils BasicAst
AstUtils UnivSubst.
From MetaCoq.Checker Require Import uGraph.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICReflect PCUICLiftSubst PCUICUnivSubst PCUICTyping
PCUICCumulativity PCUICSR PCUICEquality PCUICNameless PCUICConversion
PCUICSafeLemmata PCUICNormal PCUICInversion PCUICReduction PCUICPosition
PCUICContextConversion PCUICConfluence PCUICSN PCUICAlpha.
From MetaCoq.SafeChecker Require Import PCUICSafeReduce.
From Equations Require Import Equations.
Require Import Equations.Prop.DepElim.
Local Set Keyed Unification.
Import MonadNotation.
Module PSR := PCUICSafeReduce.
(** * Conversion for PCUIC without fuel
Following PCUICSafereduce, we derive a fuel-free implementation of
conversion (and cumulativity) checking for PCUIC.
*)
Definition global_uctx (Σ : global_env) : ContextSet.t
:= (global_levels Σ, global_constraints Σ).
Definition global_ext_uctx (Σ : global_env_ext) : ContextSet.t
:= (global_ext_levels Σ, global_ext_constraints Σ).
Definition wf_global_uctx_invariants {cf:checker_flags} Σ
: ∥ wf Σ ∥ -> global_uctx_invariants (global_uctx Σ).
Proof.
intros [HΣ]. split.
- cbn. unfold global_levels.
cut (LevelSet.In lSet (LevelSet_pair Level.lSet Level.lProp)).
+ generalize (LevelSet_pair Level.lSet Level.lProp).
clear HΣ. induction Σ; simpl. easy.
intros X H. apply LevelSet.union_spec. now right.
+ apply LevelSet.add_spec. right. now apply LevelSet.singleton_spec.
- unfold global_uctx.
simpl. intros [[l ct] l'] Hctr. simpl in *.
induction Σ in HΣ, l, ct, l', Hctr |- *.
* apply ConstraintSetFact.empty_iff in Hctr; contradiction.
* simpl in *. apply ConstraintSet.union_spec in Hctr;
destruct Hctr as [Hctr|Hctr].
-- split.
inversion HΣ; subst.
destruct H2 as [HH1 [HH HH3]].
subst udecl. destruct a as [kn decl|kn decl]; simpl in *.
destruct decl; simpl in *.
destruct cst_universes;
[eapply (HH (l, ct, l') Hctr)|
apply ConstraintSetFact.empty_iff in Hctr; contradiction|
apply ConstraintSetFact.empty_iff in Hctr; contradiction].
destruct decl; simpl in *.
destruct ind_universes;
[eapply (HH (l, ct, l') Hctr)|
apply ConstraintSetFact.empty_iff in Hctr; contradiction|
apply ConstraintSetFact.empty_iff in Hctr; contradiction].
inversion HΣ; subst.
destruct H2 as [HH1 [HH HH3]].
subst udecl. destruct a as [kn decl|kn decl]; simpl in *.
destruct decl; simpl in *.
destruct cst_universes;
[eapply (HH (l, ct, l') Hctr)|
apply ConstraintSetFact.empty_iff in Hctr; contradiction|
apply ConstraintSetFact.empty_iff in Hctr; contradiction].
destruct decl; simpl in *.
destruct ind_universes;
[eapply (HH (l, ct, l') Hctr)|
apply ConstraintSetFact.empty_iff in Hctr; contradiction|
apply ConstraintSetFact.empty_iff in Hctr; contradiction].
-- inversion HΣ; subst.
split; apply LevelSet.union_spec; right;
unshelve eapply (IHΣ _ _ _ _ Hctr); tea.
Qed.
Definition wf_ext_global_uctx_invariants {cf:checker_flags} Σ
: ∥ wf_ext Σ ∥ -> global_uctx_invariants (global_ext_uctx Σ).
Proof.
intros [HΣ]. split.
- apply LevelSet.union_spec. right. unfold global_levels.
cut (LevelSet.In lSet (LevelSet_pair Level.lSet Level.lProp)).
+ generalize (LevelSet_pair Level.lSet Level.lProp).
induction Σ.1; simpl. easy.
intros X H. apply LevelSet.union_spec. now right.
+ apply LevelSet.add_spec. right. now apply LevelSet.singleton_spec.
- destruct Σ as [Σ φ]. destruct HΣ as [HΣ Hφ].
destruct (wf_global_uctx_invariants _ (sq HΣ)) as [_ XX].
unfold global_ext_uctx, global_ext_levels, global_ext_constraints.
simpl. intros [[l ct] l'] Hctr. simpl in *. apply ConstraintSet.union_spec in Hctr.
destruct Hctr as [Hctr|Hctr].
+ destruct Hφ as [_ [HH _]]. apply (HH _ Hctr).
+ specialize (XX _ Hctr).
split; apply LevelSet.union_spec; right; apply XX.
Qed.
Definition global_ext_uctx_consistent {cf:checker_flags} Σ
: ∥ wf_ext Σ ∥ -> consistent (global_ext_uctx Σ).2.
intros [HΣ]. cbn. unfold global_ext_constraints.
unfold wf_ext, on_global_env_ext in HΣ.
destruct HΣ as [_ [_ [_ HH]]]. apply HH.
Qed.
Section Conversion.
Context {cf : checker_flags}.
Context (flags : RedFlags.t).
Context (Σ : global_env_ext).
Context (hΣ : ∥ wf Σ ∥) (Hφ : ∥ on_udecl Σ.1 Σ.2 ∥).
Context (G : universes_graph) (HG : is_graph_of_uctx G (global_ext_uctx Σ)).
Local Definition hΣ' : ∥ wf_ext Σ ∥.
Proof.
destruct hΣ, Hφ; now constructor.
Defined.
Set Equations With UIP.
Inductive state :=
| Reduction (t : term)
| Term (t : term)
| Args
| Fallback (t : term).
Inductive stateR : state -> state -> Prop :=
| stateR_Term_Reduction : forall u v, stateR (Term u) (Reduction v)
| stateR_Args_Term : forall u, stateR Args (Term u)
| stateR_Fallback_Term : forall u v, stateR (Fallback u) (Term v)
| stateR_Args_Fallback : forall u, stateR Args (Fallback u).
Derive Signature for stateR.
Lemma stateR_Acc :
forall s, Acc stateR s.
Proof.
assert (Acc stateR Args) as hArgs.
{ constructor. intros s h.
dependent induction h.
all: discriminate.
}
assert (forall t, Acc stateR (Fallback t)) as hFall.
{ intros t. constructor. intros s h.
dependent induction h.
all: try discriminate.
apply hArgs.
}
assert (forall t, Acc stateR (Term t)) as hTerm.
{ intros t. constructor. intros s h.
dependent induction h.
all: try discriminate.
- apply hArgs.
- apply hFall.
}
assert (forall t, Acc stateR (Reduction t)) as hRed.
{ intros t. constructor. intros s h.
dependent induction h.
all: try discriminate.
apply hTerm.
}
intro s. destruct s ; eauto.
Qed.
Notation wtp Γ t π :=
(wellformed Σ Γ (zipc t π)) (only parsing).
Definition wts Γ s t π :=
match s with
| Reduction t'
| Fallback t'
| Term t' => wtp Γ t' π
| Args => wtp Γ t π
end.
Set Primitive Projections.
Record pack (Γ : context) := mkpack {
st : state ;
tm : term ;
stk1 : stack ;
stk2 : stack ;
tm' := match st with
| Reduction t | Fallback t | Term t => t
| Args => tm
end ;
wth : wellformed Σ Γ (zipc tm' stk2)
}.
Arguments st {_} _.
Arguments tm {_} _.
Arguments stk1 {_} _.
Arguments stk2 {_} _.
Arguments tm' {_} _.
Arguments wth {_} _.
Definition nlstate (s : state) :=
match s with
| Reduction t => Reduction (nl t)
| Term t => Term (nl t)
| Args => Args
| Fallback t => Fallback (nl t)
end.
Definition nl_pack {Γ : context} (p : pack Γ) : pack (nlctx Γ).
Proof.
destruct p as [s t π1 π2 t' h].
unshelve eexists.
- exact (nlstate s).
- exact (nl t).
- exact (nlstack π2).
- exact (nlstack π1).
- eapply wellformed_nlctx; tas.
eapply wellformed_alpha ; try eassumption.
destruct s.
all: cbn.
all: rewrite <- nl_zipc.
all: eapply eq_term_upto_univ_tm_nl.
all: auto.
Defined.
Definition wterm Γ := { t : term | wellformed Σ Γ t }.
Definition wcored Γ (u v : wterm Γ) :=
cored' Σ Γ (` u) (` v).
Lemma wcored_wf :
forall Γ, well_founded (wcored Γ).
Proof.
intros Γ [u hu].
destruct hΣ as [hΣ'].
apply normalisation_upto in hu as h.
dependent induction h.
constructor. intros [y hy] r.
unfold wcored in r. cbn in r.
eapply H0. all: assumption.
Qed.
Definition eqt u v :=
∥ eq_term Σ u v ∥.
Lemma eq_term_valid_pos :
forall {u v p},
validpos u p ->
eqt u v ->
validpos v p.
Proof.
intros u v p vp [e].
eapply eq_term_valid_pos. all: eauto.
Qed.
(* Can be generalised *)
(* Definition eq_term_pos u v (e : eqt u v) (p : pos u) : pos v :=
exist (` p) (eq_term_valid_pos (proj2_sig p) e). *)
Definition weqt {Γ} (u v : wterm Γ) :=
eqt (` u) (` v).
(* Definition weq_term_pos {Γ} (u v : wterm Γ) (e : weqt u v) (p : pos u) *)
Equations R_aux (Γ : context) :
(∑ t : term, pos t × (∑ w : wterm Γ, pos (` w) × state)) ->
(∑ t : term, pos t × (∑ w : wterm Γ, pos (` w) × state)) -> Prop :=
R_aux Γ :=
t ⊨ eqt \ cored' Σ Γ by _ ⨷
@posR t ⊗
w ⊨ weqt \ wcored Γ by _ ⨷
@posR (` w) ⊗
stateR.
Next Obligation.
split. 2: intuition eauto.
exists (` p).
destruct p as [p hp].
eapply eq_term_valid_pos. all: eauto.
Defined.
Next Obligation.
split. 2: assumption.
exists (` p).
destruct x as [u hu], x' as [v hv].
destruct p as [p hp].
simpl in *.
eapply eq_term_valid_pos. all: eauto.
Defined.
(* Transparent R_aux. *)
Derive Signature for Subterm.lexprod.
Lemma R_aux_Acc :
forall Γ t p w q s,
wellformed Σ Γ t ->
Acc (R_aux Γ) (t ; (p, (w ; (q, s)))).
Proof.
intros Γ t p w q s ht.
rewrite R_aux_equation_1.
unshelve eapply dlexmod_Acc.
- intros x y [e]. constructor. eapply eq_term_sym. assumption.
- intros x y z [e1] [e2]. constructor. eapply eq_term_trans. all: eauto.
- intro u. eapply Subterm.wf_lexprod.
+ intro. eapply posR_Acc.
+ intros [w' q'].
unshelve eapply dlexmod_Acc.
* intros x y [e]. constructor. eapply eq_term_sym. assumption.
* intros x y z [e1] [e2]. constructor. eapply eq_term_trans. all: eauto.
* intros [t' h']. eapply Subterm.wf_lexprod.
-- intro. eapply posR_Acc.
-- intro. eapply stateR_Acc.
* intros x x' y [e] [y' [x'' [r [[e1] [e2]]]]].
eexists _,_. intuition eauto.
-- constructor. assumption.
-- constructor. eapply eq_term_trans. all: eauto.
* intros x. exists (sq (eq_term_refl _ _)). intros [[q'' h] ?].
unfold R_aux_obligations_obligation_2.
simpl. f_equal. f_equal.
eapply uip.
* intros x x' [[q'' h] ?] [e].
unfold R_aux_obligations_obligation_2.
simpl. f_equal. f_equal.
eapply uip.
* intros x y z e1 e2 [[q'' h] ?].
unfold R_aux_obligations_obligation_2.
simpl. f_equal. f_equal.
eapply uip.
* intros [t1 ht1] [t2 ht2] [e] [[q1 hq1] s1] [[q2 hq2] s2] h.
simpl in *.
dependent destruction h.
-- left. unfold posR in *. simpl in *. assumption.
-- match goal with
| |- context [ exist q1 ?hq1 ] =>
assert (ee : hq1 = hq2) by eapply uip
end.
rewrite ee. right. clear ee. assumption.
* eapply wcored_wf.
- intros x x' y [e] [y' [x'' [r [[e1] [e2]]]]].
eexists _,_. intuition eauto.
+ constructor. assumption.
+ constructor. eapply eq_term_trans. all: eauto.
- intros x. exists (sq (eq_term_refl _ _)). intros [[q' h] [? [? ?]]].
unfold R_aux_obligations_obligation_1.
simpl. f_equal. f_equal.
eapply uip.
- intros x x' [[q' h] [? [? ?]]] [e].
unfold R_aux_obligations_obligation_1.
simpl. f_equal. f_equal.
eapply uip.
- intros x y z e1 e2 [[q' h] [? [? ?]]].
unfold R_aux_obligations_obligation_1.
simpl. f_equal. f_equal.
eapply uip.
- intros x x' [e]
[[p1 hp1] [[u hu] [[q1 hq1] s1]]]
[[p2 hp2] [[v hv] [[q2 hq2] s2]]] hl.
simpl in *.
dependent destruction hl.
+ left. unfold posR in *.
simpl in *.
assumption.
+ match goal with
| |- context [ exist p1 ?hp1 ] =>
assert (ee : hp1 = hp2) by eapply uip
end.
rewrite ee. right. clear ee.
dependent destruction H.
* left. assumption.
* unshelve econstructor 2. 1: assumption.
dependent destruction H.
-- left. unfold posR in *.
simpl in *. assumption.
-- right. assumption.
- eapply normalisation_upto. all: assumption.
Qed.
Notation pzt u := (zipc (tm u) (stk1 u)) (only parsing).
Notation pps1 u := (stack_pos (tm u) (stk1 u)) (only parsing).
Notation pwt u := (exist _ (wth u)) (only parsing).
Notation pps2 u := (stack_pos (tm' u) (stk2 u)) (only parsing).
Notation obpack u :=
(pzt u ; (pps1 u, (pwt u ; (pps2 u, st u)))) (only parsing).
Definition R Γ (u v : pack Γ) :=
R_aux Γ (obpack u) (obpack v).
Lemma R_Acc :
forall Γ u,
wellformed Σ Γ (zipc (tm u) (stk1 u)) ->
Acc (R Γ) u.
Proof.
intros Γ u h.
eapply Acc_fun with (f := fun x => obpack x).
apply R_aux_Acc. assumption.
Qed.
Lemma R_cored :
forall Γ p1 p2,
cored Σ Γ (pzt p1) (pzt p2) ->
R Γ p1 p2.
Proof.
intros Γ p1 p2 h.
left. eapply cored_cored'. assumption.
Qed.
Lemma R_aux_positionR :
forall Γ t1 t2 (p1 : pos t1) (p2 : pos t2) s1 s2,
eq_term Σ t1 t2 ->
positionR (` p1) (` p2) ->
R_aux Γ (t1 ; (p1, s1)) (t2 ; (p2, s2)).
Proof.
intros Γ t1 t2 p1 p2 [? [? ?]] s2 e h.
unshelve eright.
- constructor. assumption.
- left. unfold posR. simpl. assumption.
Qed.
Lemma R_positionR :
forall Γ p1 p2,
eq_term Σ (pzt p1) (pzt p2) ->
positionR (` (pps1 p1)) (` (pps1 p2)) ->
R Γ p1 p2.
Proof.
intros Γ [s1 t1 π1 ρ1 t1' h1] [s2 t2 π2 ρ2 t2' h2] e h. simpl in *.
eapply R_aux_positionR ; simpl.
- assumption.
- assumption.
Qed.
Lemma R_aux_cored2 :
forall Γ t1 t2 (p1 : pos t1) (p2 : pos t2) w1 w2 q1 q2 s1 s2,
eq_term Σ t1 t2 ->
` p1 = ` p2 ->
cored' Σ Γ (` w1) (` w2) ->
R_aux Γ (t1 ; (p1, (w1 ; (q1, s1)))) (t2 ; (p2, (w2 ; (q2, s2)))).
Proof.
intros Γ t1 t2 [p1 hp1] [p2 hp2] [t1' h1'] [t2' h2'] q1 q2 s1 s2 e1 e2 h.
cbn in e2. cbn in h. subst.
unshelve eright.
- constructor. assumption.
- unfold R_aux_obligations_obligation_1. simpl.
match goal with
| |- context [ exist p2 ?hp1 ] =>
assert (e : hp1 = hp2) by eapply uip
end.
rewrite e.
right.
left. assumption.
Qed.
Lemma R_cored2 :
forall Γ p1 p2,
eq_term Σ (pzt p1) (pzt p2) ->
` (pps1 p1) = ` (pps1 p2) ->
cored Σ Γ (` (pwt p1)) (` (pwt p2)) ->
R Γ p1 p2.
Proof.
intros Γ [s1 t1 π1 ρ1 t1' h1] [s2 t2 π2 ρ2 t2' h2] e1 e2 h. simpl in *.
eapply R_aux_cored2. all: simpl. all: auto.
destruct s1, s2.
all: eapply cored_cored'.
all: assumption.
Qed.
Lemma R_aux_positionR2 :
forall Γ t1 t2 (p1 : pos t1) (p2 : pos t2) w1 w2 q1 q2 s1 s2,
eq_term Σ t1 t2 ->
` p1 = ` p2 ->
eq_term Σ (` w1) (` w2) ->
positionR (` q1) (` q2) ->
R_aux Γ (t1 ; (p1, (w1 ; (q1, s1)))) (t2 ; (p2, (w2 ; (q2, s2)))).
Proof.
intros Γ t1 t2 [p1 hp1] [p2 hp2] [t1' h1'] [t2' h2'] q1 q2 s1 s2 e1 e2 e3 h.
cbn in e2. cbn in e3. subst.
unshelve eright.
- constructor. assumption.
- unfold R_aux_obligations_obligation_1. simpl.
match goal with
| |- context [ exist p2 ?hp1 ] =>
assert (e : hp1 = hp2) by eapply uip
end.
rewrite e.
right.
unshelve eright.
+ constructor. assumption.
+ left. unfold posR. simpl. assumption.
Qed.
Lemma R_positionR2 :
forall Γ p1 p2,
eq_term Σ (pzt p1) (pzt p2) ->
` (pps1 p1) = ` (pps1 p2) ->
eq_term Σ (` (pwt p1)) (` (pwt p2)) ->
positionR (` (pps2 p1)) (` (pps2 p2)) ->
R Γ p1 p2.
Proof.
intros Γ [s1 t1 π1 ρ1 t1' h1] [s2 t2 π2 ρ2 t2' h2] e1 e2 e3 h.
simpl in *.
eapply R_aux_positionR2. all: simpl. all: auto.
Qed.
Lemma R_aux_stateR :
forall Γ t1 t2 (p1 : pos t1) (p2 : pos t2) w1 w2 q1 q2 s1 s2 ,
eq_term Σ t1 t2 ->
` p1 = ` p2 ->
eq_term Σ (` w1) (` w2) ->
` q1 = ` q2 ->
stateR s1 s2 ->
R_aux Γ (t1 ; (p1, (w1 ; (q1, s1)))) (t2 ; (p2, (w2 ; (q2, s2)))).
Proof.
intros Γ t1 t2 [p1 hp1] [p2 hp2] [t1' h1'] [t2' h2'] [q1 hq1] [q2 hq2] s1 s2
e1 e2 e3 e4 h.
cbn in e2. cbn in e3. cbn in e4. subst.
unshelve eright.
- constructor. assumption.
- unfold R_aux_obligations_obligation_1. simpl.
match goal with
| |- context [ exist p2 ?hp1 ] =>
assert (e : hp1 = hp2) by eapply uip
end.
rewrite e.
right.
unshelve eright.
+ constructor. assumption.
+ unfold R_aux_obligations_obligation_2. simpl.
match goal with
| |- context [ exist q2 ?hq1 ] =>
assert (e' : hq1 = hq2) by eapply uip
end.
rewrite e'.
right. assumption.
Qed.
Lemma R_stateR :
forall Γ p1 p2,
eq_term Σ (pzt p1) (pzt p2) ->
` (pps1 p1) = ` (pps1 p2) ->
eq_term Σ (` (pwt p1)) (` (pwt p2)) ->
` (pps2 p1) = ` (pps2 p2) ->
stateR (st p1) (st p2) ->
R Γ p1 p2.
Proof.
intros Γ [s1 t1 π1 ρ1 t1' h1] [s2 t2 π2 ρ2 t2' h2] e1 e2 e3 e4 h.
simpl in *.
eapply R_aux_stateR. all: simpl. all: auto.
Qed.
(* TODO MOVE *)
Lemma eqb_term_upto_univ_refl :
forall (eqb leqb : universe -> universe -> bool) t,
(forall u, eqb u u) ->
(forall u, leqb u u) ->
eqb_term_upto_univ eqb leqb t t.
Admitted.
Definition leqb_term :=
eqb_term_upto_univ (try_eqb_universe G) (try_leqb_universe G).
Definition eqb_term :=
eqb_term_upto_univ (try_eqb_universe G) (try_eqb_universe G).
Lemma leqb_term_spec t u :
leqb_term t u ->
leq_term (global_ext_constraints Σ) t u.
Proof.
pose proof hΣ'.
apply eqb_term_upto_univ_impl.
intros u1 u2; eapply (try_eqb_universe_spec G (global_ext_uctx Σ)); tas.
now eapply wf_ext_global_uctx_invariants.
now eapply global_ext_uctx_consistent.
intros u1 u2; eapply (try_leqb_universe_spec G (global_ext_uctx Σ)); tas.
now eapply wf_ext_global_uctx_invariants.
now eapply global_ext_uctx_consistent.
Qed.
Lemma eqb_term_spec t u :
eqb_term t u ->
eq_term (global_ext_constraints Σ) t u.
Proof.
pose proof hΣ'.
apply eqb_term_upto_univ_impl.
intros u1 u2; eapply (try_eqb_universe_spec G (global_ext_uctx Σ)); tas.
now eapply wf_ext_global_uctx_invariants.
now eapply global_ext_uctx_consistent.
intros u1 u2; eapply (try_eqb_universe_spec G (global_ext_uctx Σ)); tas.
now eapply wf_ext_global_uctx_invariants.
now eapply global_ext_uctx_consistent.
Qed.
Lemma leqb_term_refl :
forall t, leqb_term t t.
Proof.
intro t. eapply eqb_term_upto_univ_refl.
Admitted.
Lemma eqb_term_refl :
forall t, eqb_term t t.
Admitted.
Fixpoint eqb_ctx (Γ Δ : context) : bool :=
match Γ, Δ with
| [], [] => true
| {| decl_name := na1 ; decl_body := None ; decl_type := t1 |} :: Γ,
{| decl_name := na2 ; decl_body := None ; decl_type := t2 |} :: Δ =>
eqb_term t1 t2 && eqb_ctx Γ Δ
| {| decl_name := na1 ; decl_body := Some b1 ; decl_type := t1 |} :: Γ,
{| decl_name := na2 ; decl_body := Some b2 ; decl_type := t2 |} :: Δ =>
eqb_term b1 b2 && eqb_term t1 t2 && eqb_ctx Γ Δ
| _, _ => false
end.
Lemma eqb_ctx_spec :
forall Γ Δ,
eqb_ctx Γ Δ ->
eq_context_upto (eq_universe (global_ext_constraints Σ)) Γ Δ.
Proof.
intros Γ Δ h.
induction Γ as [| [na [b|] A] Γ ih ] in Δ, h |- *.
all: destruct Δ as [| [na' [b'|] A'] Δ].
all: try discriminate.
- constructor.
- simpl in h. apply andP in h as [h h3]. apply andP in h as [h1 h2].
constructor.
+ eapply eqb_term_spec. assumption.
+ eapply eqb_term_spec. assumption.
+ eapply ih. assumption.
- simpl in h. apply andP in h as [h1 h2].
constructor.
+ eapply eqb_term_spec. assumption.
+ eapply ih. assumption.
Qed.
Definition eqb_term_stack t1 π1 t2 π2 :=
eqb_ctx (stack_context π1) (stack_context π2) &&
eqb_term (zipp t1 π1) (zipp t2 π2).
Lemma eqb_term_stack_spec :
forall Γ t1 π1 t2 π2,
eqb_term_stack t1 π1 t2 π2 ->
eq_context_upto (eq_universe (global_ext_constraints Σ))
(Γ ,,, stack_context π1)
(Γ ,,, stack_context π2) ×
eq_term (global_ext_constraints Σ) (zipp t1 π1) (zipp t2 π2).
Proof.
intros Γ t1 π1 t2 π2 h.
apply andP in h as [h1 h2].
split.
- eapply eq_context_upto_cat.
+ eapply eq_context_upto_refl. intro. apply eq_universe_refl.
+ eapply eqb_ctx_spec. assumption.
- eapply eqb_term_spec. assumption.
Qed.
Definition leqb_term_stack t1 π1 t2 π2 :=
eqb_ctx (stack_context π1) (stack_context π2) &&
leqb_term (zipp t1 π1) (zipp t2 π2).
Definition eqb_termp leq u v :=
match leq with
| Conv => eqb_term u v
| Cumul => leqb_term u v
end.
Lemma eqb_termp_spec :
forall leq u v Γ,
eqb_termp leq u v ->
conv leq Σ Γ u v.
Proof.
intros leq u v Γ e.
destruct leq.
- simpl. constructor. constructor. eapply eqb_term_spec. assumption.
- simpl. constructor. constructor. eapply leqb_term_spec. assumption.
Qed.
Lemma leqb_term_stack_spec :
forall Γ t1 π1 t2 π2,
leqb_term_stack t1 π1 t2 π2 ->
eq_context_upto (eq_universe (global_ext_constraints Σ))
(Γ ,,, stack_context π1)
(Γ ,,, stack_context π2) ×
leq_term (global_ext_constraints Σ) (zipp t1 π1) (zipp t2 π2).
Proof.
intros Γ t1 π1 t2 π2 h.
apply andP in h as [h1 h2].
split.
- eapply eq_context_upto_cat.
+ eapply eq_context_upto_refl. intro. apply eq_universe_refl.
+ eapply eqb_ctx_spec. assumption.
- eapply leqb_term_spec. assumption.
Qed.
Lemma zwts :
forall {Γ s t π},
wts Γ s t π ->
wellformed Σ Γ (zipc match s with Reduction u | Fallback u | Term u => u | Args => t end π).
Proof.
intros Γ s t π h.
destruct s ; assumption.
Defined.
Notation conv_stack_ctx Γ π1 π2 :=
(∥ conv_context Σ (Γ ,,, stack_context π1) (Γ ,,, stack_context π2) ∥).
Notation conv_term leq Γ t π t' π' :=
(conv leq Σ (Γ ,,, stack_context π) (zipp t π) (zipp t' π'))
(only parsing).
Notation alt_conv_term Γ t π π' :=
(∥ Σ ;;; Γ ,,, stack_context π |- zipp t π == zipp t π' ∥)
(only parsing).
(* Definition Ret s Γ t π π' := *)
(* match s with *)
(* | Reduction t' => *)
(* forall leq, *)
(* { b : bool | if b then conv_term leq Γ t π t' π' else True } *)
(* | Fallback t' *)
(* | Term t' => *)
(* forall leq, *)
(* isred (t, π) -> *)
(* isred (t', π') -> *)
(* { b : bool | if b then conv_term leq Γ t π t' π' else True } *)
(* | Args => *)
(* { b : bool | if b then alt_conv_term Γ t π π' else True } *)
(* end. *)
Definition Ret s Γ t π π' :=
forall (leq : match s with Args => unit | _ => conv_pb end),
conv_stack_ctx Γ π π' ->
(match s with Fallback t' | Term t' => isred (t, π) | _ => True end) ->
(match s with Fallback t' | Term t' => isred (t', π') | _ => True end) ->
{ b : bool | match s
return forall (leq : match s with Args => unit | _ => conv_pb end), Prop
with
| Reduction t' => fun leq =>
if b then conv_term leq Γ t π t' π' else True
| Fallback t'
| Term t' => fun leq =>
if b then conv_term leq Γ t π t' π' else True
| Args => fun _ =>
if b then alt_conv_term Γ t π π' else True
end leq
}.
Definition Aux s Γ t π1 π2 h2 :=
forall s' t' π1' π2'
(h1' : wtp Γ t' π1')
(h2' : wts Γ s' t' π2'),
conv_stack_ctx Γ π1 π2 ->
R Γ
(mkpack Γ s' t' π1' π2' (zwts h2'))
(mkpack Γ s t π1 π2 (zwts h2)) ->
Ret s' Γ t' π1' π2'.
Notation no := (exist false I) (only parsing).
Notation yes := (exist true _) (only parsing).
Notation repack e := (let '(exist b h) := e in exist b _) (only parsing).
Notation isconv_red_raw leq t1 π1 t2 π2 aux :=
(aux (Reduction t2) t1 π1 π2 _ _ _ _ leq _ I I) (only parsing).
Notation isconv_prog_raw leq t1 π1 t2 π2 aux :=
(aux (Term t2) t1 π1 π2 _ _ _ _ leq _ _ _) (only parsing).
Notation isconv_args_raw t π1 π2 aux :=
(aux Args t π1 π2 _ _ _ _ tt _ I I) (only parsing).
Notation isconv_fallback_raw leq t1 π1 t2 π2 aux :=
(aux (Fallback t2) t1 π1 π2 _ _ _ _ leq _ _ _) (only parsing).
Notation isconv_red leq t1 π1 t2 π2 aux :=
(repack (isconv_red_raw leq t1 π1 t2 π2 aux)) (only parsing).
Notation isconv_prog leq t1 π1 t2 π2 aux :=
(repack (isconv_prog_raw leq t1 π1 t2 π2 aux)) (only parsing).
Notation isconv_args t π1 π2 aux :=
(repack (isconv_args_raw t π1 π2 aux)) (only parsing).
Notation isconv_fallback leq t1 π1 t2 π2 aux :=
(repack (isconv_fallback_raw leq t1 π1 t2 π2 aux)) (only parsing).
Ltac tas := try assumption.
Equations(noeqns) _isconv_red (Γ : context) (leq : conv_pb)
(t1 : term) (π1 : stack) (h1 : wtp Γ t1 π1)
(t2 : term) (π2 : stack) (h2 : wtp Γ t2 π2)
(hx : conv_stack_ctx Γ π1 π2)
(aux : Aux (Reduction t2) Γ t1 π1 π2 h2)
: { b : bool | if b then conv_term leq Γ t1 π1 t2 π2 else True } :=
_isconv_red Γ leq t1 π1 h1 t2 π2 h2 hx aux
with inspect (decompose_stack π1) := {
| @exist (args1, ρ1) e1 with inspect (decompose_stack π2) := {
| @exist (args2, ρ2) e2
with inspect (reduce_stack nodelta_flags Σ hΣ (Γ ,,, stack_context π1) t1 (appstack args1 ε) _) := {
| @exist (t1',π1') eq1
with inspect (reduce_stack nodelta_flags Σ hΣ (Γ ,,, stack_context π2) t2 (appstack args2 ε) _) := {
| @exist (t2',π2') eq2 => isconv_prog leq t1' (π1' +++ ρ1) t2' (π2' +++ ρ2) aux
}
}
}
}.
Next Obligation.
symmetry in e1.
eapply wellformed_zipc_stack_context. all: eassumption.
Qed.
Next Obligation.
clear aux eq1.
symmetry in e2.
eapply wellformed_zipc_stack_context. all: eassumption.
Qed.
Next Obligation.
pose proof hΣ as hΣ'.
destruct hΣ' as [wΣ].
match type of eq1 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
destruct (reduce_stack_sound f Σ hΣ Γ t π h) as [r1] ;
pose proof (reduce_stack_decompose nodelta_flags _ hΣ _ _ _ h) as d1 ;
pose proof (reduce_stack_context f Σ hΣ Γ t π h) as c1
end.
rewrite <- eq1 in r1.
rewrite <- eq1 in d1. cbn in d1.
rewrite <- eq1 in c1. cbn in c1.
rewrite stack_context_appstack in c1. cbn in c1.
pose proof (decompose_stack_eq _ _ _ (eq_sym e1)). subst.
clear eq1 eq2.
rewrite zipc_appstack in h1.
case_eq (decompose_stack π1'). intros args1' ρ1' e1'.
rewrite e1' in d1. cbn in d1.
rewrite decompose_stack_appstack in d1. cbn in d1. subst.
pose proof (decompose_stack_eq _ _ _ e1'). subst.
rewrite stack_cat_appstack.
rewrite zipc_appstack.
rewrite stack_context_appstack in r1. cbn in r1.
rewrite 2!zipc_appstack in r1. cbn in r1.
eapply red_wellformed ; try assumption ; revgoals.
- constructor. zip fold. eapply PCUICPosition.red_context. eassumption.
- cbn. assumption.
Qed.
Next Obligation.
pose proof hΣ as hΣ'.
destruct hΣ' as [wΣ].
match type of eq2 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
destruct (reduce_stack_sound f Σ hΣ Γ t π h) as [r2] ;
pose proof (reduce_stack_decompose nodelta_flags _ hΣ _ _ _ h) as d2 ;
pose proof (reduce_stack_context f Σ hΣ Γ t π h) as c2
end.
rewrite <- eq2 in r2.
rewrite <- eq2 in d2. cbn in d2.
rewrite <- eq2 in c2. cbn in c2.
rewrite stack_context_appstack in c2. cbn in c2.
pose proof (decompose_stack_eq _ _ _ (eq_sym e2)). subst.
clear eq1 eq2 aux.
rewrite zipc_appstack in h2.
case_eq (decompose_stack π2'). intros args2' ρ2' e2'.
rewrite e2' in d2. cbn in d2.
rewrite decompose_stack_appstack in d2. cbn in d2. subst.
pose proof (decompose_stack_eq _ _ _ e2'). subst.
rewrite stack_cat_appstack.
rewrite zipc_appstack.
rewrite stack_context_appstack in r2. cbn in r2.
rewrite 2!zipc_appstack in r2. cbn in r2.
eapply red_wellformed ; try assumption ; revgoals.
- constructor. zip fold. eapply PCUICPosition.red_context. eassumption.
- cbn. assumption.
Qed.
Next Obligation.
match type of eq1 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
pose proof (reduce_stack_decompose nodelta_flags _ hΣ _ _ _ h) as d1 ;
pose proof (reduce_stack_context f Σ hΣ Γ t π h) as c1
end.
rewrite <- eq1 in d1. cbn in d1.
rewrite <- eq1 in c1. cbn in c1.
rewrite stack_context_appstack in c1. cbn in c1.
pose proof (decompose_stack_eq _ _ _ (eq_sym e1)). subst.
match type of eq1 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?hh =>
pose proof (reduce_stack_Req f _ hΣ _ _ _ hh) as [ e | h ]
end.
- assert (ee1 := eq1). rewrite e in ee1. inversion ee1. subst.
match type of eq2 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
pose proof (reduce_stack_decompose nodelta_flags _ hΣ _ _ _ h) as d2 ;
pose proof (reduce_stack_context f Σ hΣ Γ t π h) as c2
end.
rewrite <- eq2 in d2. cbn in d2.
rewrite <- eq2 in c2. cbn in c2.
rewrite stack_context_appstack in c2. cbn in c2.
pose proof (decompose_stack_eq _ _ _ (eq_sym e2)). subst.
match type of eq2 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?hh =>
pose proof (reduce_stack_Req f _ hΣ _ _ _ hh) as [ ee | h ]
end.
+ assert (ee2 := eq2). rewrite ee in ee2. inversion ee2. subst.
unshelve eapply R_stateR.
* simpl. rewrite stack_cat_appstack. reflexivity.
* simpl. rewrite stack_cat_appstack. reflexivity.
* simpl. rewrite stack_cat_appstack. reflexivity.
* simpl. rewrite stack_cat_appstack. reflexivity.
* simpl. constructor.
+ rewrite <- eq2 in h.
rewrite stack_context_appstack in h.
dependent destruction h.
* cbn in H. rewrite zipc_appstack in H. cbn in H.
unshelve eapply R_cored2.
-- simpl. rewrite stack_cat_appstack. reflexivity.
-- simpl. rewrite stack_cat_appstack. reflexivity.
-- simpl.
rewrite zipc_appstack. rewrite zipc_stack_cat.
repeat zip fold. eapply cored_context.
assumption.
* destruct y' as [q hq].
cbn in H0. inversion H0. subst.
unshelve eapply R_positionR2.
-- simpl. rewrite stack_cat_appstack. reflexivity.
-- simpl. rewrite stack_cat_appstack. reflexivity.
-- simpl. f_equal.
rewrite zipc_stack_cat.
rewrite <- H2.
rewrite 2!zipc_appstack. cbn. reflexivity.
-- simpl.
unfold posR in H. cbn in H.
rewrite stack_position_appstack in H. cbn in H.
rewrite stack_position_stack_cat.
rewrite stack_position_appstack.
eapply positionR_poscat.
assumption.
- rewrite <- eq1 in h.
rewrite stack_context_appstack in h.
dependent destruction h.
+ cbn in H. rewrite zipc_appstack in H. cbn in H.
eapply R_cored. simpl.
rewrite zipc_appstack. rewrite zipc_stack_cat.
repeat zip fold. eapply cored_context.
assumption.
+ destruct y' as [q hq].
cbn in H0. inversion H0. (* Why is noconf failing at this point? *)
subst.
unshelve eapply R_positionR ; simpl.
* f_equal.
rewrite zipc_stack_cat.
rewrite <- H2.
rewrite 2!zipc_appstack. cbn. reflexivity.
* unfold posR in H. cbn in H.
rewrite stack_position_appstack in H. cbn in H.
rewrite stack_position_stack_cat.
rewrite stack_position_appstack.
eapply positionR_poscat.
assumption.
Qed.
Next Obligation.
match type of eq1 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
pose proof (reduce_stack_decompose nodelta_flags _ hΣ _ _ _ h) as d1 ;
pose proof (reduce_stack_context f Σ hΣ Γ t π h) as c1
end.
rewrite <- eq1 in d1. cbn in d1.
rewrite <- eq1 in c1. cbn in c1.
rewrite stack_context_appstack in c1. cbn in c1.
pose proof (decompose_stack_eq _ _ _ (eq_sym e1)). subst.
match type of eq2 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
pose proof (reduce_stack_decompose nodelta_flags _ hΣ _ _ _ h) as d2 ;
pose proof (reduce_stack_context f Σ hΣ Γ t π h) as c2
end.
rewrite <- eq2 in d2. cbn in d2.
rewrite <- eq2 in c2. cbn in c2.
rewrite stack_context_appstack in c2. cbn in c2.
pose proof (decompose_stack_eq _ _ _ (eq_sym e2)). subst.
rewrite 2!stack_context_stack_cat.
rewrite c1. rewrite c2. simpl.
rewrite 2!stack_context_appstack in hx.
assumption.
Qed.
Next Obligation.
match type of eq1 with
| _ = reduce_stack ?f ?Σ ?hΣ ?Γ ?t ?π ?h =>
pose proof (reduce_stack_isred f Σ hΣ Γ t π h eq_refl) as r1
end.
rewrite <- eq1 in r1. destruct r1 as [ha hl].
split.
- assumption.
- cbn in hl. cbn. intro h.
specialize (hl h).
destruct π1'.
all: try reflexivity.
+ cbn. destruct ρ1.
all: try reflexivity.
exfalso.
apply (decompose_stack_not_app _ _ _ _ (eq_sym e1)).
+ discriminate hl.