forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICSafeLemmata.v
1718 lines (1574 loc) · 50.1 KB
/
PCUICSafeLemmata.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.CRelationClasses Omega ProofIrrelevance.
From MetaCoq.Template Require Import config Universes monad_utils utils BasicAst
AstUtils UnivSubst.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICReflect PCUICLiftSubst PCUICUnivSubst PCUICTyping
PCUICCumulativity PCUICSR PCUICPosition PCUICEquality PCUICNameless
PCUICAlpha PCUICNormal PCUICInversion PCUICCumulativity PCUICReduction
PCUICConfluence PCUICConversion PCUICContextConversion PCUICValidity
PCUICParallelReductionConfluence PCUICWeakeningEnv
PCUICClosed PCUICPrincipality PCUICSubstitution
PCUICWeakening PCUICGeneration.
From Equations Require Import Equations.
Require Import Equations.Prop.DepElim.
Require Import Equations.Type.Relation_Properties.
Derive Signature for red.
Import MonadNotation.
Local Set Keyed Unification.
Set Equations With UIP.
Arguments sq {_} _.
Notation "( x ; y )" := (existT _ x y).
Ltac rdestruct H :=
match type of H with
| _ /\ _ => let H' := fresh H in
destruct H as [H H']; rdestruct H; rdestruct H'
| _ × _ => let H' := fresh H in
destruct H as [H H']; rdestruct H; rdestruct H'
| sigT _ => let H' := fresh H in
destruct H as [H H']; rdestruct H; rdestruct H'
| _ => idtac
end.
Definition nodelta_flags := RedFlags.mk true true true false true true.
(* Dependent lexicographic order *)
Inductive dlexprod {A} {B : A -> Type}
(leA : A -> A -> Prop) (leB : forall x, B x -> B x -> Prop)
: sigT B -> sigT B -> Prop :=
| left_lex :
forall x x' y y',
leA x x' ->
dlexprod leA leB (x;y) (x';y')
| right_lex :
forall x y y',
leB x y y' ->
dlexprod leA leB (x;y) (x;y').
Derive Signature for dlexprod.
Definition lexprod := Subterm.lexprod.
Arguments lexprod {_ _} _ _ _ _.
(* Dependent lexicographic order modulo another relation *)
Inductive dlexmod {A} {B : A -> Type}
(leA : A -> A -> Prop)
(eA : A -> A -> Prop)
(coe : forall x x', eA x x' -> B x -> B x')
(leB : forall x, B x -> B x -> Prop)
: sigT B -> sigT B -> Prop :=
| left_dlexmod :
forall x x' y y',
leA x x' ->
dlexmod leA eA coe leB (x;y) (x';y')
| right_dlexmod :
forall x x' y y' (e : eA x x'),
leB x' (coe _ _ e y) y' ->
dlexmod leA eA coe leB (x;y) (x';y').
Notation "x ⊩ R1 ⨶ R2" :=
(dlexprod R1 (fun x => R2)) (at level 20, right associativity).
Notation "R1 ⊗ R2" :=
(lexprod R1 R2) (at level 20, right associativity).
Notation "x ⊨ e \ R1 'by' coe ⨷ R2" :=
(dlexmod R1 e coe (fun x => R2)) (at level 20, right associativity).
Lemma acc_dlexprod :
forall A B leA leB,
(forall x, well_founded (leB x)) ->
forall x,
Acc leA x ->
forall y,
Acc (leB x) y ->
Acc (@dlexprod A B leA leB) (x;y).
Proof.
intros A B leA leB hw.
induction 1 as [x hx ih1].
intros y.
induction 1 as [y hy ih2].
constructor.
intros [x' y'] h. simple inversion h.
- intro hA. inversion H0. inversion H1. subst.
eapply ih1.
+ assumption.
+ apply hw.
- intro hB. rewrite <- H0.
pose proof (projT2_eq H1) as p2.
set (projT1_eq H1) as p1 in *; cbn in p1.
destruct p1; cbn in p2; destruct p2.
eapply ih2. assumption.
Qed.
Lemma dlexprod_Acc :
forall A B leA leB,
(forall x, well_founded (leB x)) ->
forall x y,
Acc leA x ->
Acc (@dlexprod A B leA leB) (x;y).
Proof.
intros A B leA leB hB x y hA.
eapply acc_dlexprod ; try assumption.
apply hB.
Qed.
Lemma dlexprod_trans :
forall A B RA RB,
transitive RA ->
(forall x, transitive (RB x)) ->
transitive (@dlexprod A B RA RB).
Proof.
intros A B RA RB hA hB [u1 u2] [v1 v2] [w1 w2] h1 h2.
revert w1 w2 h2. induction h1 ; intros w1 w2 h2.
- dependent induction h2.
+ left. eapply hA ; eassumption.
+ left. assumption.
- dependent induction h2.
+ left. assumption.
+ right. eapply hB ; eassumption.
Qed.
Derive Signature for dlexmod.
Lemma acc_dlexmod :
forall A B (leA : A -> A -> Prop) (eA : A -> A -> Prop)
(coe : forall x x', eA x x' -> B x -> B x')
(leB : forall x : A, B x -> B x -> Prop)
(sym : forall x y, eA x y -> eA y x)
(trans : forall x y z, eA x y -> eA y z -> eA x z),
(forall x, well_founded (leB x)) ->
(forall x x' y, eA x x' -> leA y x' -> leA y x) ->
(forall x, exists e : eA x x, forall y, coe _ _ e y = y) ->
(forall x x' y e, coe x x' (sym _ _ e) (coe _ _ e y) = y) ->
(forall x0 x1 x2 e1 e2 y,
coe _ _ (trans x0 x1 x2 e1 e2) y =
coe _ _ e2 (coe _ _ e1 y)
) ->
(forall x x' e y y',
leB _ y (coe x x' e y') ->
leB _ (coe _ _ (sym _ _ e) y) y'
) ->
forall x,
Acc leA x ->
forall y,
Acc (leB x) y ->
forall x' (e : eA x x'),
Acc (@dlexmod A B leA eA coe leB) (x'; coe _ _ e y).
Proof.
intros A B leA eA coe leB sym trans hw hA hcoe coesym coetrans lesym.
induction 1 as [x hx ih1].
induction 1 as [y hy ih2].
intros x' e.
constructor.
intros [x'' y''] h. dependent destruction h.
- specialize (hcoe x'') as [e' he'].
rewrite <- (he' y'').
eapply ih1.
+ eapply hA. all: eauto.
+ apply hw.
- simpl in *.
specialize ih2 with (x' := x'').
set (e2 := trans _ _ _ e0 (sym _ _ e)).
set (e1 := sym _ _ e2).
replace y'' with (coe _ _ e1 (coe _ _ e2 y''))
by eauto using coesym.
eapply ih2.
rewrite coetrans.
eapply lesym.
assumption.
Qed.
Lemma dlexmod_Acc :
forall A B (leA : A -> A -> Prop) (eA : A -> A -> Prop)
(coe : forall x x', eA x x' -> B x -> B x')
(leB : forall x : A, B x -> B x -> Prop)
(sym : forall x y, eA x y -> eA y x)
(trans : forall x y z, eA x y -> eA y z -> eA x z),
(forall x, well_founded (leB x)) ->
(forall x x' y, eA x x' -> leA y x' -> leA y x) ->
(forall x, exists e : eA x x, forall y, coe _ _ e y = y) ->
(forall x x' y e, coe x x' (sym _ _ e) (coe _ _ e y) = y) ->
(forall x0 x1 x2 e1 e2 y,
coe _ _ (trans x0 x1 x2 e1 e2) y =
coe _ _ e2 (coe _ _ e1 y)
) ->
(forall x x' e y y',
leB _ y (coe x x' e y') ->
leB _ (coe _ _ (sym _ _ e) y) y'
) ->
forall x y,
Acc leA x ->
Acc (@dlexmod A B leA eA coe leB) (x ; y).
Proof.
intros A B leA eA coe leB sym trans hB ? hcoe ? ? ? x y h.
specialize (hcoe x) as h'. destruct h' as [e he].
rewrite <- (he y).
eapply acc_dlexmod. all: eauto.
apply hB.
Qed.
Section Lemmata.
Context {cf : checker_flags}.
Context (flags : RedFlags.t).
Lemma eq_term_zipc_inv :
forall φ u v π,
eq_term φ (zipc u π) (zipc v π) ->
eq_term φ u v.
Proof.
intros Σ u v π h.
revert u v h. induction π ; intros u v h.
all: solve [
simpl in h ; try apply IHπ in h ;
cbn in h ; inversion h ; subst ; assumption
].
Qed.
Lemma eq_term_zipx_inv :
forall φ Γ u v π,
eq_term φ (zipx Γ u π) (zipx Γ v π) ->
eq_term φ u v.
Proof.
intros Σ Γ u v π h.
eapply eq_term_zipc_inv.
eapply eq_term_it_mkLambda_or_LetIn_inv.
eassumption.
Qed.
Lemma eq_term_upto_univ_zipc :
forall Re u v π,
RelationClasses.Reflexive Re ->
eq_term_upto_univ Re Re u v ->
eq_term_upto_univ Re Re (zipc u π) (zipc v π).
Proof.
intros Re u v π he h.
induction π in u, v, h |- *.
all: try solve [
simpl ; try apply IHπ ;
cbn ; constructor ; try apply eq_term_upto_univ_refl ; assumption
].
- assumption.
- simpl. apply IHπ. destruct indn as [i n].
constructor.
+ apply eq_term_upto_univ_refl. all: assumption.
+ assumption.
+ eapply All2_same.
intros. split ; auto. apply eq_term_upto_univ_refl. all: assumption.
Qed.
Lemma eq_term_zipc :
forall (Σ : global_env_ext) u v π,
eq_term (global_ext_constraints Σ) u v ->
eq_term (global_ext_constraints Σ) (zipc u π) (zipc v π).
Proof.
intros Σ u v π h.
eapply eq_term_upto_univ_zipc.
- intro. eapply eq_universe_refl.
- assumption.
Qed.
Lemma eq_term_upto_univ_zipp :
forall Re u v π,
RelationClasses.Reflexive Re ->
eq_term_upto_univ Re Re u v ->
eq_term_upto_univ Re Re (zipp u π) (zipp v π).
Proof.
intros Re u v π he h.
unfold zipp.
case_eq (decompose_stack π). intros l ρ e.
eapply eq_term_upto_univ_mkApps.
- assumption.
- apply All2_same. intro. reflexivity.
Qed.
Lemma eq_term_zipp :
forall (Σ : global_env_ext) u v π,
eq_term (global_ext_constraints Σ) u v ->
eq_term (global_ext_constraints Σ) (zipp u π) (zipp v π).
Proof.
intros Σ u v π h.
eapply eq_term_upto_univ_zipp.
- intro. eapply eq_universe_refl.
- assumption.
Qed.
Lemma eq_term_upto_univ_zipx :
forall Re Γ u v π,
RelationClasses.Reflexive Re ->
eq_term_upto_univ Re Re u v ->
eq_term_upto_univ Re Re (zipx Γ u π) (zipx Γ v π).
Proof.
intros Re Γ u v π he h.
eapply eq_term_upto_univ_it_mkLambda_or_LetIn ; auto.
eapply eq_term_upto_univ_zipc ; auto.
Qed.
Lemma eq_term_zipx :
forall φ Γ u v π,
eq_term φ u v ->
eq_term φ (zipx Γ u π) (zipx Γ v π).
Proof.
intros Σ Γ u v π h.
eapply eq_term_upto_univ_zipx ; auto.
intro. eapply eq_universe_refl.
Qed.
(* red is the reflexive transitive closure of one-step reduction and thus
can't be used as well order. We thus define the transitive closure,
but we take the symmetric version.
*)
Inductive cored Σ Γ: term -> term -> Prop :=
| cored1 : forall u v, red1 Σ Γ u v -> cored Σ Γ v u
| cored_trans : forall u v w, cored Σ Γ v u -> red1 Σ Γ v w -> cored Σ Γ w u.
Derive Signature for cored.
Hint Resolve eq_term_upto_univ_refl : core.
Lemma lookup_env_ConstantDecl_inv :
forall Σ k k' ty bo uni,
Some (ConstantDecl k' {| cst_type := ty ; cst_body := bo; cst_universes := uni |})
= lookup_env Σ k ->
k = k'.
Proof.
intros Σ k k' ty bo uni h.
induction Σ in h |- *.
- cbn in h. discriminate.
- cbn in h. destruct (ident_eq_spec k (global_decl_ident a)).
+ subst. inversion h. reflexivity.
+ apply IHΣ in h. assumption.
Qed.
Lemma fresh_global_nl :
forall Σ k,
fresh_global k Σ ->
fresh_global k (map nl_global_decl Σ).
Proof.
intros Σ k h. eapply Forall_map.
eapply Forall_impl ; try eassumption.
intros x hh. cbn in hh.
destruct x ; assumption.
Qed.
(* Lemma conv_context : *)
(* forall Σ Γ u v ρ, *)
(* wf Σ.1 -> *)
(* Σ ;;; Γ ,,, stack_context ρ |- u == v -> *)
(* Σ ;;; Γ |- zipc u ρ == zipc v ρ. *)
(* Proof. *)
(* intros Σ Γ u v ρ hΣ h. *)
(* induction ρ in u, v, h |- *. *)
(* - assumption. *)
(* - simpl. eapply IHρ. eapply conv_App_l ; auto. *)
(* - simpl. eapply IHρ. eapply conv_App_r ; auto. *)
(* - simpl. eapply IHρ. eapply conv_App_r ; auto. *)
(* - simpl. eapply IHρ. eapply conv_Case_c ; auto. *)
(* - simpl. eapply IHρ. eapply conv_Proj_c ; auto. *)
(* - simpl. eapply IHρ. eapply conv_Prod_l ; auto. *)
(* - simpl. eapply IHρ. eapply conv_Prod_r ; auto. *)
(* - simpl. eapply IHρ. eapply conv_Lambda_l ; auto. *)
(* - simpl. eapply IHρ. eapply conv_Lambda_r ; auto. *)
(* - simpl. eapply IHρ. eapply conv_App_r ; auto. *)
(* Qed. *)
Context (Σ : global_env_ext).
Inductive welltyped Σ Γ t : Prop :=
| iswelltyped A : Σ ;;; Γ |- t : A -> welltyped Σ Γ t.
Arguments iswelltyped {Σ Γ t A} h.
Definition wellformed Σ Γ t :=
welltyped Σ Γ t \/ ∥ isWfArity typing Σ Γ t ∥.
(* Here we use use the proof irrelevance axiom to show that wellformed is really squashed.
Using SProp would avoid this.
*)
Lemma wellformed_irr :
forall {Σ Γ t} (h1 h2 : wellformed Σ Γ t), h1 = h2.
Proof. intros. apply proof_irrelevance. Qed.
Context (hΣ : ∥ wf Σ ∥).
Lemma welltyped_alpha Γ u v :
welltyped Σ Γ u ->
eq_term_upto_univ eq eq u v ->
welltyped Σ Γ v.
Proof.
intros [A h] e.
destruct hΣ.
exists A. eapply typing_alpha ; eauto.
Qed.
Lemma wellformed_alpha Γ u v :
wellformed Σ Γ u ->
eq_term_upto_univ eq eq u v ->
wellformed Σ Γ v.
Proof.
destruct hΣ as [hΣ'].
intros [X|X] e; [left|right].
- destruct X as [A Hu]. eexists. eapply typing_alpha; tea.
- destruct X. constructor.
now eapply isWfArity_alpha.
Qed.
Lemma wellformed_nlctx Γ u :
wellformed Σ Γ u ->
wellformed Σ (nlctx Γ) u.
Proof.
destruct hΣ as [hΣ'].
assert (Γ ≡Γ nlctx Γ) by apply upto_names_nlctx.
intros [[A hu]|[[ctx [s [X1 X2]]]]]; [left|right].
- exists A. eapply context_conversion'; tea.
eapply wf_local_alpha with Γ; tea.
now eapply typing_wf_local.
now eapply upto_names_conv_context.
- constructor. exists ctx, s. split; tas.
eapply wf_local_alpha; tea.
now eapply eq_context_upto_cat.
Qed.
Lemma red_cored_or_eq :
forall Γ u v,
red Σ Γ u v ->
cored Σ Γ v u \/ u = v.
Proof.
intros Γ u v h.
induction h.
- right. reflexivity.
- destruct IHh.
+ left. eapply cored_trans ; eassumption.
+ subst. left. constructor. assumption.
Qed.
Lemma cored_it_mkLambda_or_LetIn :
forall Γ Δ u v,
cored Σ (Γ ,,, Δ) u v ->
cored Σ Γ (it_mkLambda_or_LetIn Δ u)
(it_mkLambda_or_LetIn Δ v).
Proof.
intros Γ Δ u v h.
induction h.
- constructor. apply red1_it_mkLambda_or_LetIn. assumption.
- eapply cored_trans.
+ eapply IHh.
+ apply red1_it_mkLambda_or_LetIn. assumption.
Qed.
Lemma cored_welltyped :
forall {Γ u v},
welltyped Σ Γ u ->
cored (fst Σ) Γ v u ->
welltyped Σ Γ v.
Proof.
destruct hΣ as [wΣ]; clear hΣ.
intros Γ u v h r.
revert h. induction r ; intros h.
- destruct h as [A h]. exists A.
eapply sr_red1 ; eauto with wf.
- specialize IHr with (1 := ltac:(eassumption)).
destruct IHr as [A ?]. exists A.
eapply sr_red1 ; eauto with wf.
Qed.
Lemma cored_trans' :
forall {Γ u v w},
cored Σ Γ u v ->
cored Σ Γ v w ->
cored Σ Γ u w.
Proof.
intros Γ u v w h1 h2. revert w h2.
induction h1 ; intros z h2.
- eapply cored_trans ; eassumption.
- eapply cored_trans.
+ eapply IHh1. assumption.
+ assumption.
Qed.
(* This suggests that this should be the actual definition.
->+ = ->*.->
*)
Lemma cored_red_trans :
forall Γ u v w,
red Σ Γ u v ->
red1 Σ Γ v w ->
cored Σ Γ w u.
Proof.
intros Γ u v w h1 h2.
revert w h2. induction h1 ; intros w h2.
- constructor. assumption.
- eapply cored_trans.
+ eapply IHh1. eassumption.
+ assumption.
Qed.
Lemma cored_case :
forall Γ ind p c c' brs,
cored Σ Γ c c' ->
cored Σ Γ (tCase ind p c brs) (tCase ind p c' brs).
Proof.
intros Γ ind p c c' brs h.
revert ind p brs. induction h ; intros ind p brs.
- constructor. constructor. assumption.
- eapply cored_trans.
+ eapply IHh.
+ econstructor. assumption.
Qed.
Lemma welltyped_context :
forall Γ t,
welltyped Σ Γ (zip t) ->
welltyped Σ (Γ ,,, stack_context (snd t)) (fst t).
Proof.
destruct hΣ as [wΣ].
intros Γ [t π] h. simpl.
destruct h as [T h].
induction π in Γ, t, T, h |- *.
- cbn. cbn in h. eexists. eassumption.
- simpl. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [B h].
apply inversion_App in h as hh ; auto.
destruct hh as [na [A' [B' [? [? ?]]]]].
eexists. eassumption.
- simpl. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [B h].
apply inversion_App in h as hh ; auto.
destruct hh as [na [A' [B' [? [? ?]]]]].
eexists. eassumption.
- simpl. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [B h].
apply inversion_App in h as hh ; auto.
destruct hh as [na [A' [B' [? [? ?]]]]].
eexists. eassumption.
- simpl. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [B h].
destruct indn.
apply inversion_Case in h as hh ; auto.
destruct hh
as [uni [args [mdecl [idecl [pty [indctx [pctx [ps [btys [? [? [? [? [? [? [ht0 [? ?]]]]]]]]]]]]]]]]].
eexists. eassumption.
- simpl. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [T' h].
apply inversion_Proj in h
as [uni [mdecl [idecl [pdecl [args [? [? [? ?]]]]]]]] ; auto.
eexists. eassumption.
- simpl. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [T' h].
apply inversion_Prod in h as hh ; auto.
destruct hh as [s1 [s2 [? [? ?]]]].
eexists. eassumption.
- cbn. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [T' h].
apply inversion_Prod in h as hh ; auto.
destruct hh as [s1 [s2 [? [? ?]]]].
eexists. eassumption.
- cbn. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [T' h].
apply inversion_Lambda in h as hh ; auto.
destruct hh as [s1 [B [? [? ?]]]].
eexists. eassumption.
- cbn. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [T' h].
apply inversion_Lambda in h as hh ; auto.
destruct hh as [s1 [B [? [? ?]]]].
eexists. eassumption.
- cbn. cbn in h. cbn in IHπ. apply IHπ in h.
destruct h as [B h].
apply inversion_App in h as hh ; auto.
destruct hh as [na [A' [B' [? [? ?]]]]].
eexists. eassumption.
Qed.
Lemma wellformed_context :
forall Γ t,
wellformed Σ Γ (zip t) ->
wellformed Σ (Γ ,,, stack_context (snd t)) (fst t).
Proof.
destruct hΣ as [wΣ].
intros Γ [t π] [[A h]|h].
- destruct (welltyped_context Γ (t, π) (iswelltyped h)) as [? ?].
left. econstructor. eassumption.
- induction π in t, h |- *.
all: try (specialize (IHπ _ h)).
all: simpl in *.
1: right ; assumption.
all: destruct IHπ as [[A' h'] | [[Δ [s [h1 h2]]]]] ; [| try discriminate].
all: try solve [
apply inversion_App in h' ; auto ;
rdestruct h' ;
left ; econstructor ; eassumption
].
+ destruct indn.
apply inversion_Case in h' ; auto. cbn in h'. rdestruct h'.
left. econstructor. eassumption.
+ apply inversion_Proj in h' ; auto.
cbn in h'. rdestruct h'.
left. eexists. eassumption.
+ apply inversion_Prod in h' ; auto. rdestruct h'.
left. eexists. eassumption.
+ cbn in h1. apply destArity_app_Some in h1 as [Δ' [h1 h1']].
subst. left. rewrite app_context_assoc in h2. cbn in *.
apply wf_local_app in h2. inversion h2. subst. cbn in *.
destruct X0. eexists. eassumption.
+ apply inversion_Prod in h' ; auto. rdestruct h'.
left. eexists. eassumption.
+ cbn in h1. apply destArity_app_Some in h1 as [Δ' [h1 h1']].
subst. right. constructor. exists Δ', s.
rewrite app_context_assoc in h2. cbn in h2.
split ; eauto.
+ apply inversion_Lambda in h' ; auto. rdestruct h'.
left. eexists. eassumption.
+ apply inversion_Lambda in h' ; auto. rdestruct h'.
left. eexists. eassumption.
Qed.
Lemma cored_red :
forall Γ u v,
cored Σ Γ v u ->
∥ red Σ Γ u v ∥.
Proof.
intros Γ u v h.
induction h.
- constructor. econstructor.
+ constructor.
+ assumption.
- destruct IHh as [r].
constructor. econstructor ; eassumption.
Qed.
Lemma cored_context :
forall Γ t u π,
cored Σ (Γ ,,, stack_context π) t u ->
cored Σ Γ (zip (t, π)) (zip (u, π)).
Proof.
intros Γ t u π h. induction h.
- constructor. eapply red1_context. assumption.
- eapply cored_trans.
+ eapply IHh.
+ eapply red1_context. assumption.
Qed.
Lemma cored_zipx :
forall Γ u v π,
cored Σ (Γ ,,, stack_context π) u v ->
cored Σ [] (zipx Γ u π) (zipx Γ v π).
Proof.
intros Γ u v π h.
eapply cored_it_mkLambda_or_LetIn.
eapply cored_context.
rewrite app_context_nil_l.
assumption.
Qed.
Lemma red_zipx :
forall Γ u v π,
red Σ (Γ ,,, stack_context π) u v ->
red Σ [] (zipx Γ u π) (zipx Γ v π).
Proof.
intros Γ u v π h.
eapply red_it_mkLambda_or_LetIn.
eapply red_context.
rewrite app_context_nil_l.
assumption.
Qed.
Lemma cumul_zippx :
forall Γ u v ρ,
Σ ;;; (Γ ,,, stack_context ρ) |- u <= v ->
Σ ;;; Γ |- zippx u ρ <= zippx v ρ.
Proof.
intros Γ u v ρ h.
revert u v h. induction ρ ; intros u v h.
- cbn. assumption.
- unfold zippx. simpl.
case_eq (decompose_stack ρ). intros l π e.
unfold zippx in IHρ. rewrite e in IHρ.
apply IHρ.
eapply cumul_App_l. assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn. cbn.
eapply cumul_Lambda_r.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn. cbn.
eapply cumul_Lambda_r.
assumption.
- unfold zippx. simpl.
eapply cumul_it_mkLambda_or_LetIn. assumption.
Qed.
Lemma conv_LetIn_bo :
forall Γ na ty t u u',
Σ ;;; Γ ,, vdef na ty t |- u == u' ->
Σ ;;; Γ |- tLetIn na ty t u == tLetIn na ty t u'.
Proof.
intros Γ na ty t u u' h.
induction h.
- eapply conv_alt_refl. constructor.
all: try eapply eq_term_refl.
assumption.
- eapply conv_alt_red_l ; try eassumption.
econstructor. assumption.
- eapply conv_alt_red_r ; try eassumption.
econstructor. assumption.
Qed.
Lemma conv_alt_it_mkLambda_or_LetIn :
forall Δ Γ u v,
Σ ;;; (Δ ,,, Γ) |- u == v ->
Σ ;;; Δ |- it_mkLambda_or_LetIn Γ u == it_mkLambda_or_LetIn Γ v.
Proof.
intros Δ Γ u v h. revert Δ u v h.
induction Γ as [| [na [b|] A] Γ ih ] ; intros Δ u v h.
- assumption.
- simpl. cbn. eapply ih.
eapply conv_LetIn_bo. assumption.
- simpl. cbn. eapply ih.
eapply conv_Lambda_r. assumption.
Qed.
Lemma conv_alt_it_mkProd_or_LetIn :
forall Δ Γ B B',
Σ ;;; (Δ ,,, Γ) |- B == B' ->
Σ ;;; Δ |- it_mkProd_or_LetIn Γ B == it_mkProd_or_LetIn Γ B'.
Proof.
intros Δ Γ B B' h.
induction Γ as [| [na [b|] A] Γ ih ] in Δ, B, B', h |- *.
- assumption.
- simpl. cbn. eapply ih.
eapply conv_LetIn_bo. assumption.
- simpl. cbn. eapply ih.
eapply conv_Prod_r. assumption.
Qed.
Lemma conv_zipp :
forall Γ u v ρ,
Σ ;;; Γ |- u == v ->
Σ ;;; Γ |- zipp u ρ == zipp v ρ.
Proof.
intros Γ u v ρ h.
unfold zipp.
destruct decompose_stack.
induction l in u, v, h |- *.
- assumption.
- simpl. eapply IHl. eapply conv_App_l. assumption.
Qed.
Lemma cumul_zipp :
forall Γ u v π,
Σ ;;; Γ |- u <= v ->
Σ ;;; Γ |- zipp u π <= zipp v π.
Proof.
intros Γ u v π h.
unfold zipp.
destruct decompose_stack as [l ρ].
induction l in u, v, h |- *.
- assumption.
- simpl. eapply IHl. eapply cumul_App_l. assumption.
Qed.
Lemma conv_zipp' :
forall leq Γ u v π,
conv leq Σ Γ u v ->
conv leq Σ Γ (zipp u π) (zipp v π).
Proof.
intros leq Γ u v π h.
destruct leq.
- destruct h. constructor. eapply conv_zipp. assumption.
- destruct h. constructor. eapply cumul_zipp. assumption.
Qed.
Lemma conv_alt_zippx :
forall Γ u v ρ,
Σ ;;; (Γ ,,, stack_context ρ) |- u == v ->
Σ ;;; Γ |- zippx u ρ == zippx v ρ.
Proof.
intros Γ u v ρ h.
revert u v h. induction ρ ; intros u v h.
- cbn. assumption.
- unfold zippx. simpl.
case_eq (decompose_stack ρ). intros l π e.
unfold zippx in IHρ. rewrite e in IHρ.
apply IHρ.
eapply conv_App_l. assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn. cbn.
eapply conv_Lambda_r.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn. cbn.
eapply conv_Lambda_r.
assumption.
- unfold zippx. simpl.
eapply conv_alt_it_mkLambda_or_LetIn. assumption.
Qed.
Lemma conv_zippx :
forall Γ u v ρ,
Σ ;;; Γ ,,, stack_context ρ |- u == v ->
Σ ;;; Γ |- zippx u ρ == zippx v ρ.
Proof.
intros Γ u v ρ uv. eapply conv_alt_zippx ; assumption.
Qed.
Lemma conv_zippx' :
forall Γ leq u v ρ,
conv leq Σ (Γ ,,, stack_context ρ) u v ->
conv leq Σ Γ (zippx u ρ) (zippx v ρ).
Proof.
intros Γ leq u v ρ h.
destruct leq.
- cbn in *. destruct h as [h]. constructor.
eapply conv_alt_zippx ; assumption.
- cbn in *. destruct h. constructor.
eapply cumul_zippx. assumption.
Qed.
Lemma cored_nl :
forall Γ u v,
cored Σ Γ u v ->
cored Σ (nlctx Γ) (nl u) (nl v).
Proof.
intros Γ u v H. induction H.
- constructor 1. admit.
- econstructor 2; tea. admit.
Admitted.
Derive Signature for Acc.
Lemma wf_fun :
forall A (R : A -> A -> Prop) B (f : B -> A),
well_founded R ->
well_founded (fun x y => R (f x) (f y)).
Proof.
intros A R B f h x.
specialize (h (f x)).
dependent induction h.
constructor. intros y h.
eapply H0 ; try reflexivity. assumption.
Qed.
Lemma Acc_fun :
forall A (R : A -> A -> Prop) B (f : B -> A) x,
Acc R (f x) ->
Acc (fun x y => R (f x) (f y)) x.
Proof.
intros A R B f x h.
dependent induction h.
constructor. intros y h.
eapply H0 ; try reflexivity. assumption.
Qed.
(* TODO Put more general lemma in Inversion *)
Lemma welltyped_it_mkLambda_or_LetIn :
forall Γ Δ t,
welltyped Σ Γ (it_mkLambda_or_LetIn Δ t) ->
welltyped Σ (Γ ,,, Δ) t.
Proof.
destruct hΣ as [wΣ].
intros Γ Δ t h.
induction Δ as [| [na [b|] A] Δ ih ] in Γ, t, h |- *.
- assumption.
- simpl. apply ih in h. cbn in h.
destruct h as [T h].
apply inversion_LetIn in h as hh ; auto.
destruct hh as [s1 [A' [? [? [? ?]]]]].
exists A'. assumption.
- simpl. apply ih in h. cbn in h.
destruct h as [T h].
apply inversion_Lambda in h as hh ; auto.
pose proof hh as [s1 [B [? [? ?]]]].
exists B. assumption.
Qed.
Lemma it_mkLambda_or_LetIn_welltyped :
forall Γ Δ t,
welltyped Σ (Γ ,,, Δ) t ->
welltyped Σ Γ (it_mkLambda_or_LetIn Δ t).
Proof.
intros Γ Δ t [T h].
eexists. eapply PCUICGeneration.type_it_mkLambda_or_LetIn.
eassumption.
Qed.
Lemma welltyped_it_mkLambda_or_LetIn_iff :
forall Γ Δ t,
welltyped Σ Γ (it_mkLambda_or_LetIn Δ t) <->
welltyped Σ (Γ ,,, Δ) t.
Proof.
intros; split.
apply welltyped_it_mkLambda_or_LetIn.
apply it_mkLambda_or_LetIn_welltyped.
Qed.
Lemma isWfArity_it_mkLambda_or_LetIn :
forall Γ Δ T,
isWfArity typing Σ Γ (it_mkLambda_or_LetIn Δ T) ->
isWfArity typing Σ (Γ ,,, Δ) T.
Proof.
intro Γ; induction Δ in Γ |- *; intro T; [easy|].
destruct a as [na [bd|] ty].
- simpl. cbn. intro HH. apply IHΔ in HH.
destruct HH as [Δ' [s [HH HH']]].
cbn in HH; apply destArity_app_Some in HH.
destruct HH as [Δ'' [HH1 HH2]].
exists Δ'', s. split; tas.
refine (eq_rect _ (fun Γ => wf_local Σ Γ) HH' _ _).
rewrite HH2. rewrite app_context_assoc. reflexivity.
- simpl. cbn. intro HH. apply IHΔ in HH.
destruct HH as [Δ' [s [HH HH']]]. discriminate.
Qed.
Lemma wellformed_it_mkLambda_or_LetIn :
forall Γ Δ t,
wellformed Σ Γ (it_mkLambda_or_LetIn Δ t) ->
wellformed Σ (Γ ,,, Δ) t.
Proof.
intros Γ Δ t [Hwf|Hwf];
[left; now apply welltyped_it_mkLambda_or_LetIn |
right; destruct Hwf; constructor].
now apply isWfArity_it_mkLambda_or_LetIn.
Qed.
Lemma wellformed_zipp :
forall Γ t ρ,
wellformed Σ Γ (zipp t ρ) ->
wellformed Σ Γ t.
Proof.
destruct hΣ as [wΣ].
intros Γ t ρ h.
unfold zipp in h.
case_eq (decompose_stack ρ). intros l π e.
rewrite e in h. clear - h wΣ.
destruct h as [[A h]|[h]].
- left.
induction l in t, A, h |- *.
+ eexists. eassumption.
+ apply IHl in h.