forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICAlpha.v
869 lines (836 loc) · 32.5 KB
/
PCUICAlpha.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
(* 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 PCUICPosition PCUICEquality PCUICNameless
PCUICContextConversion PCUICValidity.
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.
Section Alpha.
Context {cf:checker_flags}.
Lemma build_branches_type_eq_term :
forall p p' ind mdecl idecl pars u brtys,
eq_term_upto_univ eq eq p p' ->
map_option_out
(build_branches_type ind mdecl idecl pars u p) =
Some brtys ->
∑ brtys',
map_option_out
(build_branches_type ind mdecl idecl pars u p') =
Some brtys' ×
All2 (on_Trel_eq (eq_term_upto_univ eq eq) snd fst) brtys brtys'.
Proof.
intros p p' ind mdecl idecl pars u brtys e hb.
unfold build_branches_type in *.
destruct idecl as [ina ity ike ict ipr]. simpl in *.
unfold mapi in *. revert hb.
generalize 0 at 3 6.
intros n hb.
induction ict in brtys, n, hb |- *.
- cbn in *. eexists. split.
+ eassumption.
+ apply All2_same. intros [m t]. simpl. split ; now auto.
- cbn. cbn in hb.
lazymatch type of hb with
| match ?t with _ => _ end = _ =>
case_eq (t) ;
try (intro bot ; rewrite bot in hb ; discriminate hb)
end.
intros [m t] e'. rewrite e' in hb.
destruct a as [[na ta] ar].
lazymatch type of e' with
| match ?expr with _ => _ end = _ =>
case_eq (expr) ;
try (intro bot ; rewrite bot in e' ; discriminate e')
end.
intros ty ety. rewrite ety in e'.
case_eq (decompose_prod_assum [] ty). intros sign ccl edty.
rewrite edty in e'.
case_eq (chop (ind_npars mdecl) (snd (decompose_app ccl))).
intros paramrels args ech. rewrite ech in e'.
inversion e'. subst. clear e'.
lazymatch type of hb with
| match ?t with _ => _ end = _ =>
case_eq (t) ;
try (intro bot ; rewrite bot in hb ; discriminate hb)
end.
intros tl etl. rewrite etl in hb.
inversion hb. subst. clear hb.
edestruct IHict as [brtys' [eq' he]].
+ eauto.
+ eexists. rewrite eq'. split.
* reflexivity.
* constructor ; auto.
simpl. split ; auto.
eapply eq_term_upto_univ_it_mkProd_or_LetIn ; auto.
eapply eq_term_upto_univ_mkApps.
-- eapply eq_term_upto_univ_lift. assumption.
-- apply All2_same. intro. apply eq_term_upto_univ_refl ; auto.
Qed.
Lemma types_of_case_eq_term :
forall ind mdecl idecl npar args u p p' pty indctx pctx ps btys,
types_of_case ind mdecl idecl (firstn npar args) u p pty =
Some (indctx, pctx, ps, btys) ->
eq_term_upto_univ eq eq p p' ->
∑ btys',
types_of_case ind mdecl idecl (firstn npar args) u p' pty =
Some (indctx, pctx, ps, btys') ×
All2 (on_Trel_eq (eq_term_upto_univ eq eq) snd fst) btys btys'.
Proof.
intros ind mdecl idecl npar args u p p' pty indctx pctx ps btys htc e.
unfold types_of_case in *.
case_eq (instantiate_params (subst_instance_context u (ind_params mdecl)) (firstn npar args) (subst_instance_constr u (ind_type idecl))) ;
try solve [ intro bot ; rewrite bot in htc ; discriminate htc ].
intros ity eity. rewrite eity in htc.
case_eq (destArity [] ity) ;
try solve [ intro bot ; rewrite bot in htc ; discriminate htc ].
intros [args0 ?] ear. rewrite ear in htc.
case_eq (destArity [] pty) ;
try solve [ intro bot ; rewrite bot in htc ; discriminate htc ].
intros [args' s'] ear'. rewrite ear' in htc.
case_eq (map_option_out (build_branches_type ind mdecl idecl (firstn npar args) u p)) ;
try solve [ intro bot ; rewrite bot in htc ; discriminate htc ].
intros brtys ebrtys. rewrite ebrtys in htc.
eapply build_branches_type_eq_term in ebrtys as [brtys' [ebrtys' he]] ; eauto.
inversion htc. subst. clear htc.
rewrite ebrtys'. intuition eauto.
Qed.
(* TODO MOVE *)
Lemma wf_local_nth_error_vass :
forall Σ Γ i na ty,
wf Σ.1 ->
wf_local Σ Γ ->
nth_error Γ i = Some (vass na ty) ->
lift_typing typing Σ Γ (lift0 (S i) ty) None.
Proof.
intros Σ Γ i na ty hΣ hΓ e. simpl.
induction i in Γ, hΓ, e |- *.
- destruct Γ. 1: discriminate.
simpl in e. apply some_inj in e. subst.
inversion hΓ. subst. simpl in X0.
destruct X0 as [s h].
exists s. change (tSort s) with (lift0 1 (tSort s)).
eapply PCUICWeakening.weakening with (Γ' := [ vass na ty ]).
all: assumption.
- destruct Γ. 1: discriminate.
simpl in e.
specialize IHi with (2 := e).
destruct IHi as [s h].
+ inversion hΓ. all: auto.
+ exists s. change (tSort s) with (lift0 1 (lift0 (S i) (tSort s))).
rewrite simpl_lift0.
eapply PCUICWeakening.weakening with (Γ' := [ c ]).
all: assumption.
Qed.
(* TODO MOVE *)
Lemma fix_context_nth_error :
forall m i d,
nth_error m i = Some d ->
nth_error (fix_context m) (#|m| - S i) =
Some (vass (dname d) (lift0 i (dtype d))).
Proof.
intros m i d e.
rewrite <- fix_context_length.
unfold fix_context. rewrite List.rev_length.
rewrite <- nth_error_rev.
- rewrite nth_error_mapi. rewrite e. simpl. reflexivity.
- rewrite mapi_length.
eauto using nth_error_Some_length.
Qed.
(* TODO MOVE *)
Lemma nth_error_weak_context :
forall Γ Δ i d,
nth_error Δ i = Some d ->
nth_error (Γ ,,, Δ) i = Some d.
Proof.
intros Γ Δ i d h.
rewrite nth_error_app_lt.
- assumption.
- apply nth_error_Some_length in h. assumption.
Qed.
(* TODO MOVE *)
Lemma Forall2_eq :
forall A (l l' : list A),
Forall2 eq l l' ->
l = l'.
Proof.
intros A l l' h.
induction h.
- reflexivity.
- f_equal. all: auto.
Qed.
Lemma typing_alpha :
forall Σ Γ u v A,
wf Σ.1 ->
Σ ;;; Γ |- u : A ->
eq_term_upto_univ eq eq u v ->
Σ ;;; Γ |- v : A.
Proof.
assert (tm :
env_prop (fun Σ Γ u A =>
forall v,
eq_term_upto_univ eq eq u v ->
Σ ;;; Γ |- v : A)
).
eapply typing_ind_env.
all: intros Σ wfΣ Γ wfΓ.
- intros n decl hnth ih v e.
dependent destruction e.
eapply type_Rel ; eassumption.
- intros l ih hl v e.
dependent destruction e. subst.
eapply type_Sort; assumption.
- intros na A B s1 s2 ih hA ihA hB ihB v e.
dependent destruction e.
econstructor.
+ eapply ihA. assumption.
+ eapply context_conversion'.
* assumption.
* constructor. 1: assumption.
simpl. eexists. eapply ihA. assumption.
* eapply ihB. assumption.
* constructor.
-- apply conv_ctx_refl ; auto.
-- constructor. constructor.
eapply upto_names_impl_eq_term. assumption.
- intros na A t s1 B ih hA ihA hB ihB v e.
dependent destruction e.
econstructor.
+ econstructor.
* eapply ihA. assumption.
* eapply context_conversion'.
-- assumption.
-- constructor. 1: assumption.
simpl. eexists. eapply ihA. assumption.
-- eapply ihB. assumption.
-- constructor.
++ apply conv_ctx_refl ; auto.
++ do 2 constructor.
eapply upto_names_impl_eq_term. assumption.
+ eapply validity_term ; eauto.
econstructor ; eauto.
+ constructor.
eapply eq_term_leq_term.
apply eq_term_sym.
constructor.
all: try (eapply upto_names_impl_eq_term ; assumption).
all: eapply eq_term_refl.
- intros na b B t s1 A ih hB ihB hb ihb hA ihA v e.
dependent destruction e.
econstructor.
+ econstructor.
* eapply ihB. assumption.
* econstructor.
-- eapply ihb. assumption.
-- right. eexists. eapply ihB. assumption.
-- constructor. eapply eq_term_leq_term.
eapply upto_names_impl_eq_term. assumption.
* eapply context_conversion'.
-- assumption.
-- constructor.
++ assumption.
++ simpl. eexists. eapply ihB. assumption.
++ simpl. eapply type_Cumul.
** eapply ihb. assumption.
** right. eexists. eapply ihB. assumption.
** eapply cumul_refl.
eapply eq_term_upto_univ_impl. 3: eassumption.
all: intros x ? [].
--- eapply eq_universe_refl.
--- eapply leq_universe_refl.
-- eapply ihA. assumption.
-- constructor.
++ apply conv_ctx_refl ; auto.
++ econstructor. constructor.
now apply upto_names_impl_eq_term.
constructor.
now apply upto_names_impl_eq_term.
+ eapply validity_term ; eauto.
econstructor ; eauto.
+ constructor.
eapply eq_term_leq_term.
apply eq_term_sym.
constructor.
all: try (eapply upto_names_impl_eq_term ; assumption).
all: eapply eq_term_refl.
- intros t na A B u ih ht iht hu ihu v e.
dependent destruction e.
econstructor.
+ econstructor.
* eapply iht. assumption.
* eapply ihu. assumption.
+ eapply validity_term ; eauto.
econstructor ; eauto.
+ constructor.
eapply eq_term_leq_term.
apply eq_term_sym.
eapply upto_names_impl_eq_term.
eapply eq_term_upto_univ_subst ; now auto.
- intros cst u decl ? ? hdecl hcons v e.
dependent destruction e.
apply Forall2_eq in r. apply map_inj in r ; revgoals.
{ intros x y h. inversion h. reflexivity. }
subst.
constructor ; auto.
- intros ind u mdecl idecl isdecl ? ? hcons v e.
dependent destruction e.
apply Forall2_eq in r. apply map_inj in r ; revgoals.
{ intros x y h. inversion h. reflexivity. }
subst.
econstructor ; eauto.
- intros ind i u mdecl idecl cdecl isdecl ? ? ? v e.
dependent destruction e.
apply Forall2_eq in r. apply map_inj in r ; revgoals.
{ intros x y h. inversion h. reflexivity. }
subst.
econstructor ; eauto.
- intros ind u npar p c brs args mdecl idecl isdecl X X0 H pars pty X1
indctx pctx ps btys htc H1 H2 ihp hc ihc ihbrs v e.
dependent destruction e.
eapply types_of_case_eq_term in htc as htc' ; eauto.
destruct htc' as [btys' [ebtys' he]].
econstructor.
+ econstructor. all: try eassumption.
* eapply ihp. assumption.
* eapply ihc. assumption.
* assert (All2 (fun x y => (fst x = fst y × Σ ;;; Γ |- snd x : snd y) × (Σ ;;; Γ |- y.2 : tSort ps)) brs' btys)
as hty.
{ clear - ihbrs a.
induction ihbrs in brs', a |- *.
- dependent destruction a. constructor.
- dependent destruction a.
constructor. all: auto.
destruct p, r as [[[? ?] ?] ?]. intuition eauto.
transitivity (fst x) ; eauto.
}
clear - he hty ihbrs.
induction hty in brs, ihbrs, btys', he |- *.
-- dependent destruction he. constructor.
-- dependent destruction he.
dependent destruction ihbrs.
destruct r. destruct p1.
destruct p.
destruct p0 as [[[? ?] ?] ihy].
constructor ; eauto. intuition eauto.
++ solve [ etransitivity ; eauto ].
++ econstructor.
** eassumption.
** right. eexists. eapply ihy. assumption.
** constructor.
eapply eq_term_leq_term.
eapply upto_names_impl_eq_term. assumption.
+ eapply validity_term ; eauto.
instantiate (1 := tCase (ind, npar) p c brs).
econstructor ; eauto.
apply All2_prod_inv in ihbrs as [a1 a4].
apply All2_prod_inv in a1 as [a1 a3].
apply All2_prod_inv in a1 as [a1 a2].
apply All2_prod. all: assumption.
+ constructor.
eapply eq_term_leq_term.
apply eq_term_sym.
eapply eq_term_mkApps.
all: try (eapply upto_names_impl_eq_term ; assumption).
eapply All2_app.
* eapply All2_same. intro. eapply eq_term_refl.
* constructor ; eauto.
eapply upto_names_impl_eq_term. assumption.
- intros p c u mdecl idecl pdecl isdecl args X X0 hc ihc H ty v e.
dependent destruction e.
econstructor.
+ econstructor. all: try eassumption.
eapply ihc. assumption.
+ eapply validity_term ; eauto.
econstructor ; eauto.
+ constructor.
eapply eq_term_leq_term.
apply eq_term_sym.
eapply upto_names_impl_eq_term.
eapply eq_term_upto_univ_substs ; auto; try reflexivity.
* constructor ; auto.
eapply All2_same.
intro. eapply eq_term_upto_univ_refl ; auto.
- intros mfix n decl types hnth hguard hwf ihmfix v e.
dependent destruction e.
eapply All2_nth_error_Some in hnth as hnth' ; eauto.
destruct hnth' as [decl' [hnth' hh]].
destruct hh as [[ety ebo] era].
assert (hwf' : wf_local Σ (Γ ,,, fix_context mfix')).
{ rename types into Δ. set (Ξ := fix_context mfix').
assert (e : eq_context_upto eq Δ Ξ).
{ eapply eq_context_upto_rev'.
clear - a.
unfold mapi. generalize 0 at 2 4. intro n.
induction a in n |- *.
- constructor.
- simpl. constructor.
+ eapply eq_term_upto_univ_lift. apply r.
+ eapply IHa.
}
clearbody Δ Ξ.
clear - e hwf wfΣ wfΓ.
induction e.
- assumption.
- simpl in *. inversion hwf. subst.
constructor.
+ eapply IHe. assumption.
+ simpl. destruct X0 as [? [? ih]].
eexists.
eapply context_conversion'.
* assumption.
* eapply IHe. assumption.
* eapply ih. assumption.
* eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
-- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
-- intros ? ? []. eapply eq_universe_refl.
- simpl in *. inversion hwf. subst.
constructor.
+ eapply IHe. assumption.
+ simpl. destruct X0 as [? [? ih]].
eexists.
eapply context_conversion'.
* assumption.
* eapply IHe. assumption.
* eapply ih. assumption.
* eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
-- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
-- intros ? ? []. eapply eq_universe_refl.
+ simpl in *. destruct X0 as [? [? ih1]]. destruct X1 as [? ih2].
eapply context_conversion'.
* assumption.
* eapply IHe. assumption.
* eapply type_Cumul.
-- eapply ih2. assumption.
-- right. eexists. eapply ih1. assumption.
-- eapply cumul_refl.
eapply eq_term_upto_univ_impl. 3: eassumption.
all: intros ? ? [].
++ eapply eq_universe_refl.
++ eapply leq_universe_refl.
* eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
-- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
-- intros ? ? []. eapply eq_universe_refl.
}
eapply type_Cumul.
+ econstructor.
* eapply fix_guard_eq_term ; eauto.
constructor. assumption.
* eassumption.
* assumption.
* assert (val :
All (fun d =>
lift_typing typing Σ (Γ ,,, fix_context mfix')
((lift0 #|fix_context mfix'|) (dtype d))
None
) mfix'
).
{ eapply forall_nth_error_All.
intros i d e.
apply fix_context_nth_error in e as e'.
apply nth_error_weak_context with (Γ := Γ) in e'.
eapply wf_local_nth_error_vass in e'. all: try eassumption.
rewrite simpl_lift in e' by lia.
apply nth_error_Some_length in e as hl.
replace (S (#|mfix'| - S i) + i)
with (#|mfix'|)
in e'
by lia.
rewrite fix_context_length. assumption.
}
rename types into Δ. set (Ξ := fix_context mfix') in *.
assert (e : eq_context_upto eq Δ Ξ).
{ eapply eq_context_upto_rev'.
clear - a.
unfold mapi. generalize 0 at 2 4. intro n.
induction a in n |- *.
- constructor.
- simpl. constructor.
+ eapply eq_term_upto_univ_lift. apply r.
+ eapply IHa.
}
clearbody Δ Ξ.
assert (el : #|Δ| = #|Ξ|).
{ eapply eq_context_upto_length. eassumption. }
clear - e a ihmfix wfΣ hwf' el val.
induction a.
-- constructor.
-- inversion ihmfix. subst.
destruct X as [[? ?] ih].
constructor.
++ split.
** eapply context_conversion'.
--- assumption.
--- assumption.
--- eapply type_Cumul.
+++ eapply ih. intuition eauto.
+++ right. simpl in val. inversion val. subst.
destruct X.
eexists. eapply context_conversion'.
*** assumption.
*** eauto using typing_wf_local.
*** eassumption.
*** eapply eq_context_upto_univ_conv_context.
eapply eq_context_upto_sym.
---- intros ? ? ?. eapply eq_universe_sym.
assumption.
---- eapply eq_context_impl ; revgoals.
++++ eapply eq_context_upto_cat.
2: eassumption.
eapply eq_context_upto_refl. auto.
++++ intros ? ? []. eapply eq_universe_refl.
+++ eapply cumul_refl. rewrite <- el.
eapply eq_term_upto_univ_lift.
eapply eq_term_upto_univ_impl.
3: intuition eauto.
all: intros ? ? [].
*** eapply eq_universe_refl.
*** eapply leq_universe_refl.
--- eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
+++ eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
+++ intros ? ? []. eapply eq_universe_refl.
** eapply isLambda_eq_term_l.
--- eassumption.
--- intuition eauto.
++ eapply IHa.
** assumption.
** inversion val. assumption.
+ eapply validity_term ; eauto.
instantiate (1 := tFix mfix n).
econstructor ; eauto.
* apply All_local_env_lift_prod_inv in hwf as [? ?]. assumption.
* apply All_prod_inv in ihmfix as [? ?]. assumption.
+ constructor. eapply eq_term_leq_term.
apply eq_term_sym.
eapply upto_names_impl_eq_term. assumption.
- intros mfix n decl types hnth hwf ihmfix hac v e. subst types.
dependent destruction e.
eapply All2_nth_error_Some in hnth as hnth' ; eauto.
destruct hnth' as [decl' [hnth' hh]].
destruct hh as [[ety ebo] era].
assert (hwf' : wf_local Σ (Γ ,,, fix_context mfix')).
{ set (Δ := fix_context mfix) in *.
set (Ξ := fix_context mfix').
assert (e : eq_context_upto eq Δ Ξ).
{ eapply eq_context_upto_rev'.
clear - a.
unfold mapi. generalize 0 at 2 4. intro n.
induction a in n |- *.
- constructor.
- simpl. constructor.
+ eapply eq_term_upto_univ_lift. apply r.
+ eapply IHa.
}
clearbody Δ Ξ.
clear - e hwf wfΣ wfΓ.
induction e.
- assumption.
- simpl in *. inversion hwf. subst.
constructor.
+ eapply IHe. assumption.
+ simpl. destruct X0 as [? [? ih]].
eexists.
eapply context_conversion'.
* assumption.
* eapply IHe. assumption.
* eapply ih. assumption.
* eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
-- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
-- intros ? ? []. eapply eq_universe_refl.
- simpl in *. inversion hwf. subst.
constructor.
+ eapply IHe. assumption.
+ simpl. destruct X0 as [? [? ih]].
eexists.
eapply context_conversion'.
* assumption.
* eapply IHe. assumption.
* eapply ih. assumption.
* eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
-- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
-- intros ? ? []. eapply eq_universe_refl.
+ simpl in *. destruct X0 as [? [? ih1]]. destruct X1 as [? ih2].
eapply context_conversion'.
* assumption.
* eapply IHe. assumption.
* eapply type_Cumul.
-- eapply ih2. assumption.
-- right. eexists. eapply ih1. assumption.
-- eapply cumul_refl.
eapply eq_term_upto_univ_impl. 3: eassumption.
all: intros ? ? [].
++ eapply eq_universe_refl.
++ eapply leq_universe_refl.
* eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
-- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
-- intros ? ? []. eapply eq_universe_refl.
}
eapply type_Cumul.
+ econstructor.
* assumption.
* eassumption.
* assumption.
* assert (val :
All (fun d =>
lift_typing typing Σ (Γ ,,, fix_context mfix')
((lift0 #|fix_context mfix'|) (dtype d))
None
) mfix'
).
{ eapply forall_nth_error_All.
intros i d e.
apply fix_context_nth_error in e as e'.
apply nth_error_weak_context with (Γ := Γ) in e'.
eapply wf_local_nth_error_vass in e'. all: try eassumption.
rewrite simpl_lift in e' by lia.
apply nth_error_Some_length in e as hl.
replace (S (#|mfix'| - S i) + i)
with (#|mfix'|)
in e'
by lia.
rewrite fix_context_length. assumption.
}
set (Δ := fix_context mfix) in *.
set (Ξ := fix_context mfix') in *.
assert (e : eq_context_upto eq Δ Ξ).
{ eapply eq_context_upto_rev'.
clear - a.
unfold mapi. generalize 0 at 2 4. intro n.
induction a in n |- *.
- constructor.
- simpl. constructor.
+ eapply eq_term_upto_univ_lift. apply r.
+ eapply IHa.
}
clearbody Δ Ξ.
assert (el : #|Δ| = #|Ξ|).
{ eapply eq_context_upto_length. eassumption. }
clear - e a ihmfix wfΣ hwf' el val.
induction a.
-- constructor.
-- inversion ihmfix. subst.
destruct X as [? ih].
constructor.
++ eapply context_conversion'.
** assumption.
** assumption.
** eapply type_Cumul.
--- eapply ih. intuition eauto.
--- right. simpl in val. inversion val. subst.
destruct X.
eexists. eapply context_conversion'.
+++ assumption.
+++ eauto using typing_wf_local.
+++ eassumption.
+++ eapply eq_context_upto_univ_conv_context.
eapply eq_context_upto_sym.
*** intros ? ? ?. eapply eq_universe_sym.
assumption.
*** eapply eq_context_impl ; revgoals.
---- eapply eq_context_upto_cat.
2: eassumption.
eapply eq_context_upto_refl. auto.
---- intros ? ? []. eapply eq_universe_refl.
--- eapply cumul_refl. rewrite <- el.
eapply eq_term_upto_univ_lift.
eapply eq_term_upto_univ_impl.
3: intuition eauto.
all: intros ? ? [].
+++ eapply eq_universe_refl.
+++ eapply leq_universe_refl.
** eapply eq_context_upto_univ_conv_context.
eapply eq_context_impl ; revgoals.
--- eapply eq_context_upto_cat. 2: eassumption.
eapply eq_context_upto_refl. auto.
--- intros ? ? []. eapply eq_universe_refl.
++ eapply IHa.
** assumption.
** inversion val. assumption.
+ eapply validity_term ; eauto.
instantiate (1 := tCoFix mfix n).
econstructor ; eauto.
* apply All_local_env_lift_prod_inv in hwf as [? ?]. assumption.
* apply All_prod_inv in ihmfix as [? ?]. assumption.
+ constructor. eapply eq_term_leq_term.
apply eq_term_sym.
eapply upto_names_impl_eq_term. assumption.
- intros t A B X ht iht har hcu v e.
eapply type_Cumul.
+ eapply iht. assumption.
+ destruct har as [[? ?] | [? [? ?]]].
* left. assumption.
* right. eexists. eassumption.
+ assumption.
- rename wfΣ into Γ, wfΓ into v, Γ into u.
intros A hΣ hu e.
eapply tm ; eauto.
eapply typing_wf_local. eassumption.
Unshelve. exact 0.
Qed.
Corollary type_nameless :
forall Σ Γ u A,
wf Σ.1 ->
Σ ;;; Γ |- u : A ->
Σ ;;; Γ |- nl u : A.
Proof.
intros Σ Γ u A hΣ h.
eapply typing_alpha ; eauto.
eapply eq_term_upto_univ_tm_nl. all: auto.
Qed.
Local Ltac inv H := inversion H; subst; clear H.
Lemma upto_names_eq_term_upto_univ Re Rle t u
: eq_term_upto_univ Re Rle t u ->
forall t' u', t ≡ t' -> u ≡ u' ->
eq_term_upto_univ Re Rle t' u'.
Proof.
revert t u Rle. fix aux 4.
destruct 1; cbn; intros t'' u'' H' H0';
inv H'; inv H0'; try econstructor; eauto.
- revert args'0 args'1 H2 H3.
induction a; simpl; intros args0 args'0 H1 H2.
+ inv H1; inv H2; constructor; eauto.
+ inv H1; inv H2. constructor; eauto.
- apply Forall2_eq, map_inj in H2.
apply Forall2_eq, map_inj in H3.
congruence.
all: intros x y H; now inv H.
- apply Forall2_eq, map_inj in H2.
apply Forall2_eq, map_inj in H3.
congruence.
all: intros x y H; now inv H.
- apply Forall2_eq, map_inj in H3.
apply Forall2_eq, map_inj in H4.
congruence.
all: intros x y H; now inv H.
- revert brs'0 brs'1 H8 H11.
induction a; simpl; intros args0 args'0 H1 H2.
+ inv H1; inv H2; constructor; eauto.
+ inv H1; inv H2. constructor; eauto.
destruct H5, H4, r. split; eauto. congruence.
- revert mfix'0 mfix'1 H2 H3.
induction a; simpl; intros args0 args'0 H1 H2.
+ inv H1; inv H2; constructor; eauto.
+ inv H1; inv H2. constructor; eauto.
destruct H3 as [[? ?] ?], H1 as [[? ?] ?], r as [[? ?] ?].
repeat split; eauto. congruence.
- revert mfix'0 mfix'1 H2 H3.
induction a; simpl; intros args0 args'0 H1 H2.
+ inv H1; inv H2; constructor; eauto.
+ inv H1; inv H2. constructor; eauto.
destruct H3 as [[? ?] ?], H1 as [[? ?] ?], r as [[? ?] ?].
repeat split; eauto. congruence.
Qed.
Lemma upto_names_leq_term φ t u t' u'
: t ≡ t' -> u ≡ u' -> leq_term φ t u -> leq_term φ t' u'.
Proof.
intros; eapply upto_names_eq_term_upto_univ; eassumption.
Qed.
Lemma upto_names_eq_term φ t u t' u'
: t ≡ t' -> u ≡ u' -> eq_term φ t u -> eq_term φ t' u'.
Proof.
intros; eapply upto_names_eq_term_upto_univ; eassumption.
Qed.
Definition upto_names_decl := eq_decl_upto eq.
Definition upto_names_ctx := eq_context_upto eq.
Infix "≡Γ" := upto_names_ctx (at level 60).
Lemma destArity_alpha Γ u v ctx s :
destArity Γ u = Some (ctx, s) ->
u ≡ v ->
∑ ctx', destArity Γ v = Some (ctx', s) × ctx ≡Γ ctx'.
Proof.
induction u in v, Γ, ctx, s |- *; cbn; try discriminate.
- intros X Y. destruct v; inv Y. inv X.
eexists. split; reflexivity.
- intros X Y. rewrite destArity_app in X.
case_eq (destArity [] u2); [|intro e; rewrite e in X; discriminate].
intros [ctx' s'] e; rewrite e in X; cbn in X; inv X.
destruct v; inv Y.
eapply IHu2 in e; tea. destruct e as [ctx'' [e1 e2]].
eexists; split. cbn. rewrite destArity_app, e1; reflexivity.
apply eq_context_upto_cat; tas. constructor; tas. reflexivity.
- intros X Y. rewrite destArity_app in X.
case_eq (destArity [] u3); [|intro e; rewrite e in X; discriminate].
intros [ctx' s'] e; rewrite e in X; cbn in X; inv X.
destruct v; inv Y.
eapply IHu3 in e; tea. destruct e as [ctx'' [e1 e2]].
eexists; split. cbn. rewrite destArity_app, e1; reflexivity.
apply eq_context_upto_cat; tas. constructor; tas. reflexivity.
Qed.
Lemma upto_names_conv_context (Σ : global_env_ext) Γ Δ :
Γ ≡Γ Δ -> conv_context Σ Γ Δ.
Proof.
eapply eq_context_upto_conv_context.
intros x y []. eapply eq_universe_refl.
Qed.
Lemma wf_local_alpha Σ Γ Γ' :
wf Σ.1 -> wf_local Σ Γ -> Γ ≡Γ Γ' -> wf_local Σ Γ'.
Proof.
intro hΣ. induction 1 in Γ' |- *.
- intro Y; inv Y; constructor.
- intro Y; inv Y. constructor. auto.
destruct t0 as [s Ht]. exists s. eapply typing_alpha; tea.
eapply context_conversion'; tea. auto.
now apply upto_names_conv_context.
- intro Y; inv Y. constructor; auto.
+ destruct t0 as [s Ht]. exists s. eapply typing_alpha; tea.
eapply context_conversion'; tea. auto.
now apply upto_names_conv_context.
+ cbn in *.
eapply type_Cumul with t.
* eapply typing_alpha; tea.
eapply context_conversion'; tea. auto.
apply upto_names_conv_context; tea.
* right. destruct t0 as [s Ht]. exists s.
eapply typing_alpha; tea.
eapply context_conversion'; tea. auto.
now apply upto_names_conv_context.
* constructor; now apply upto_names_impl_leq_term.
Qed.
Lemma isWfArity_alpha Σ Γ u v :
wf Σ.1 ->
isWfArity typing Σ Γ u ->
u ≡ v ->
isWfArity typing Σ Γ v.
Proof.
intros hΣ [ctx [s [X1 X2]]] e.
eapply destArity_alpha in X1; tea.
destruct X1 as [ctx' [X1 X1']].
exists ctx', s. split; tas.
eapply wf_local_alpha; tea.
now eapply eq_context_upto_cat.
Qed.
Lemma upto_names_nl t
: t ≡ nl t.
Proof.
eapply eq_term_upto_univ_tm_nl; exact _.
Qed.
Lemma upto_names_nlctx Γ
: Γ ≡Γ nlctx Γ.
Proof.
induction Γ as [|a Γ]; try constructor.
destruct a as [na [bo|] ty]; simpl; constructor; cbn; tas.
all: apply upto_names_nl.
Qed.
End Alpha.
Infix "≡Γ" := upto_names_ctx (at level 60).