forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICNameless.v
1445 lines (1356 loc) · 46.9 KB
/
PCUICNameless.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.
From MetaCoq.Template
Require Import config monad_utils utils AstUtils UnivSubst.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICLiftSubst PCUICEquality PCUICTyping PCUICPosition PCUICUnivSubst
PCUICCumulativity.
From Equations Require Import Equations.
Local Set Keyed Unification.
Require Import Equations.Prop.DepElim.
Definition anon (na : name) : bool :=
match na with
| nAnon => true
| nNamed s => false
end.
Fixpoint nameless (t : term) : bool :=
match t with
| tRel n => true
| tVar n => true
| tEvar n l => forallb nameless l
| tSort s => true
| tProd na A B => anon na && nameless A && nameless B
| tLambda na A b => anon na && nameless A && nameless b
| tLetIn na b B t => anon na && nameless b && nameless B && nameless t
| tApp u v => nameless u && nameless v
| tConst c u => true
| tInd i u => true
| tConstruct i n u => true
| tCase indn p c brs =>
nameless p && nameless c && forallb (test_snd nameless) brs
| tProj p c => nameless c
| tFix mfix idx =>
forallb (fun d => anon d.(dname)) mfix &&
forallb (test_def nameless nameless) mfix
| tCoFix mfix idx =>
forallb (fun d => anon d.(dname)) mfix &&
forallb (test_def nameless nameless) mfix
end.
Definition map_def_anon {A B : Set} (tyf bodyf : A -> B) (d : def A) := {|
dname := nAnon ;
dtype := tyf d.(dtype) ;
dbody := bodyf d.(dbody) ;
rarg := d.(rarg)
|}.
Fixpoint nl (t : term) : term :=
match t with
| tRel n => tRel n
| tVar n => tVar n
| tEvar n l => tEvar n (map nl l)
| tSort s => tSort s
| tProd na A B => tProd nAnon (nl A) (nl B)
| tLambda na A b => tLambda nAnon (nl A) (nl b)
| tLetIn na b B t => tLetIn nAnon (nl b) (nl B) (nl t)
| tApp u v => tApp (nl u) (nl v)
| tConst c u => tConst c u
| tInd i u => tInd i u
| tConstruct i n u => tConstruct i n u
| tCase indn p c brs => tCase indn (nl p) (nl c) (map (on_snd nl) brs)
| tProj p c => tProj p (nl c)
| tFix mfix idx => tFix (map (map_def_anon nl nl) mfix) idx
| tCoFix mfix idx => tCoFix (map (map_def_anon nl nl) mfix) idx
end.
Ltac destruct_one_andb :=
lazymatch goal with
| h : is_true (_ && _) |- _ =>
apply andP in h ; destruct h as [? ?]
end.
Ltac destruct_andb :=
repeat destruct_one_andb.
Local Ltac anonify :=
repeat lazymatch goal with
| h : is_true (anon ?na) |- _ =>
destruct na ; [clear h | discriminate h]
end.
Local Ltac ih :=
lazymatch goal with
| ih : forall v : term, _ -> _ -> eq_term_upto_univ _ _ _ _ -> ?u = _
|- ?u = ?v =>
eapply ih ; assumption
end.
Lemma eq_univ_make :
forall u u',
Forall2 eq (map Universe.make u) (map Universe.make u') ->
u = u'.
Proof.
intros u u' h.
revert u' h.
induction u ; intros u' h.
- destruct u' ; inversion h. reflexivity.
- destruct u' ; inversion h. subst.
f_equal.
+ inversion H2. reflexivity.
+ eapply IHu. assumption.
Qed.
Lemma nameless_eq_term_spec :
forall u v,
nameless u ->
nameless v ->
eq_term_upto_univ eq eq u v ->
u = v.
Proof.
intros u v hu hv e.
revert v hu hv e.
induction u using term_forall_list_ind ; intros v hu hv e.
all: dependent destruction e.
all: cbn in hu, hv ; destruct_andb ; anonify.
all: try reflexivity.
all: try solve [ f_equal ; try ih ; try assumption ].
- f_equal. cbn in hu, hv.
revert args' hu hv a. induction l ; intros args' hu hv h.
+ destruct args' ; try solve [ inversion h ].
reflexivity.
+ destruct args' ; try solve [ inversion h ].
inversion h. subst.
inversion H. subst.
cbn in hu, hv. destruct_andb.
f_equal.
* eapply H2 ; assumption.
* eapply IHl ; assumption.
- f_equal ; try solve [ ih ].
eapply eq_univ_make. assumption.
- f_equal ; try solve [ ih ].
eapply eq_univ_make. assumption.
- f_equal ; try solve [ ih ].
eapply eq_univ_make. assumption.
- f_equal ; try solve [ ih ].
revert brs' H3 H0 a.
induction l ; intros brs' h1 h2 h.
+ destruct brs' ; inversion h. reflexivity.
+ destruct brs' ; inversion h. subst.
cbn in h1, h2. destruct_andb.
inversion X. subst.
f_equal.
* destruct a, p0. cbn in *. destruct H6. subst.
f_equal. eapply H11 ; assumption.
* eapply IHl ; assumption.
- f_equal ; try solve [ ih ].
revert mfix' H1 H2 H H0 a.
induction m ; intros m' h1 h2 h3 h4 h.
+ destruct m' ; inversion h. reflexivity.
+ destruct m' ; inversion h. subst.
inversion X. subst.
cbn in h1, h2, h3, h4. destruct_andb.
f_equal.
* destruct a, d. cbn in *. destruct H2 as [[? ?] ?].
destruct H1 as [Hty Hbod].
unfold test_def in H7, H. cbn in H7, H.
destruct_andb. anonify.
f_equal.
-- eapply Hty; assumption.
-- eapply Hbod ; assumption.
-- assumption.
* eapply IHm ; assumption.
- f_equal ; try solve [ ih ].
revert mfix' H1 H2 H H0 a.
induction m ; intros m' h1 h2 h3 h4 h.
+ destruct m' ; inversion h. reflexivity.
+ destruct m' ; inversion h. subst.
inversion X. subst.
cbn in h1, h2, h3, h4. destruct_andb.
f_equal.
* destruct a, d. cbn in *. destruct H2 as [[? ?] ?].
destruct H1 as [Hty Hbod].
unfold test_def in H7, H. cbn in H7, H.
destruct_andb. anonify.
f_equal.
-- eapply Hty; assumption.
-- eapply Hbod ; assumption.
-- assumption.
* eapply IHm ; assumption.
Qed.
Lemma nl_spec :
forall u, nameless (nl u).
Proof.
intros u. induction u using term_forall_list_ind.
all: try reflexivity.
all: try (simpl ; repeat (eapply andb_true_intro ; split) ; assumption).
- cbn. eapply All_forallb. eapply All_map. assumption.
- simpl ; repeat (eapply andb_true_intro ; split) ; try assumption.
induction l.
+ reflexivity.
+ cbn. inversion X. subst.
repeat (eapply andb_true_intro ; split) ; try assumption.
eapply IHl. assumption.
- simpl ; repeat (eapply andb_true_intro ; split) ; try assumption.
+ induction m.
* reflexivity.
* cbn. eapply IHm. inversion X. subst. assumption.
+ induction m.
* reflexivity.
* cbn. inversion X. subst. destruct H1.
repeat (eapply andb_true_intro ; split).
all: try assumption.
eapply IHm. assumption.
- simpl ; repeat (eapply andb_true_intro ; split) ; try assumption.
+ induction m.
* reflexivity.
* cbn. eapply IHm. inversion X. subst. assumption.
+ induction m.
* reflexivity.
* cbn. inversion X. subst. destruct H1.
repeat (eapply andb_true_intro ; split).
all: try assumption.
eapply IHm. assumption.
Qed.
Lemma nl_eq_term_upto_univ :
forall Re Rle t t',
eq_term_upto_univ Re Rle t t' ->
eq_term_upto_univ Re Rle (nl t) (nl t').
Proof.
intros Re Rle t t' h.
revert t t' Rle h. fix aux 4.
intros t t' Rle h.
destruct h.
all: simpl.
all: try solve [ econstructor ; eauto ].
- econstructor.
induction a.
+ constructor.
+ simpl. econstructor. all: eauto.
- econstructor. all: try solve [ eauto ].
induction a.
+ constructor.
+ simpl. econstructor. 2: solve [ eauto ].
destruct x, y, r. simpl in *.
split. 1: assumption.
eauto.
- econstructor.
induction a. 1: constructor.
simpl. constructor.
+ destruct x, y, r as [[? ?] ?].
simpl in *. eauto.
+ eauto.
- econstructor.
induction a. 1: constructor.
simpl. constructor.
+ destruct x, y, r as [[? ?] ?].
simpl in *. eauto.
+ eauto.
Qed.
Lemma nl_leq_term {cf:checker_flags} :
forall φ t t',
leq_term φ t t' ->
leq_term φ (nl t) (nl t').
Proof.
intros. apply nl_eq_term_upto_univ. assumption.
Qed.
Lemma nl_eq_term {cf:checker_flags} :
forall φ t t',
eq_term φ t t' ->
eq_term φ (nl t) (nl t').
Proof.
intros. apply nl_eq_term_upto_univ. assumption.
Qed.
Corollary eq_term_nl_eq :
forall u v,
eq_term_upto_univ eq eq u v ->
nl u = nl v.
Proof.
intros u v h.
eapply nameless_eq_term_spec.
- eapply nl_spec.
- eapply nl_spec.
- now eapply nl_eq_term_upto_univ.
Qed.
Local Ltac ih3 :=
lazymatch goal with
| ih : forall Rle v, eq_term_upto_univ _ _ (nl ?u) _ -> _
|- eq_term_upto_univ _ _ ?u _ =>
eapply ih ; assumption
end.
Lemma eq_term_upto_univ_nl_inv :
forall Re Rle u v,
eq_term_upto_univ Re Rle (nl u) (nl v) ->
eq_term_upto_univ Re Rle u v.
Proof.
intros Re Rle u v h.
induction u in v, h, Rle |- * using term_forall_list_ind.
all: dependent destruction h.
all: destruct v ; try discriminate.
all: try solve [
try lazymatch goal with
| h : nl _ = _ |- _ =>
simpl in h ; inversion h ; subst
end ;
constructor ;
try ih3 ;
assumption
].
- cbn in H. inversion H. subst. constructor.
apply All2_map_inv in a. solve_all.
- cbn in H. inversion H. subst. constructor ; try ih3.
apply All2_map_inv in a. solve_all.
- cbn in H. inversion H. subst. constructor ; try ih3.
apply All2_map_inv in a. solve_all.
- cbn in H. inversion H. subst. constructor ; try ih3.
apply All2_map_inv in a. solve_all.
Qed.
Definition map_decl_anon f (d : context_decl) := {|
decl_name := nAnon ;
decl_body := option_map f d.(decl_body) ;
decl_type := f d.(decl_type)
|}.
Definition nlctx (Γ : context) : context :=
map (map_decl_anon nl) Γ.
(* TODO MOVE *)
Definition test_option {A} f (o : option A) : bool :=
match o with
| None => true
| Some x => f x
end.
Definition nameless_ctx (Γ : context) : bool :=
forallb (fun d =>
anon d.(decl_name) &&
test_option nameless d.(decl_body) &&
nameless d.(decl_type)
) Γ.
Lemma nlctx_spec :
forall Γ, nameless_ctx (nlctx Γ).
Proof.
intros Γ. induction Γ as [| [na [b|] B] Γ ih].
- reflexivity.
- simpl. rewrite 2!nl_spec, ih. reflexivity.
- simpl. rewrite nl_spec, ih. reflexivity.
Qed.
Lemma eq_term_upto_univ_tm_nl :
forall Re Rle u,
Reflexive Re ->
Reflexive Rle ->
eq_term_upto_univ Re Rle u (nl u).
Proof.
intros Re Rle u hRe hRle.
induction u in Rle, hRle |- * using term_forall_list_ind.
all: try solve [
simpl ; try apply eq_term_upto_univ_refl ; auto ; constructor ; eauto
].
- simpl. constructor.
induction l.
+ constructor.
+ simpl. inversion X. subst. constructor ; eauto.
- simpl. destruct p. constructor ; eauto.
induction l.
+ constructor.
+ simpl. inversion X. subst. constructor.
* split ; auto.
* eapply IHl. assumption.
- simpl. constructor. induction m.
+ constructor.
+ simpl. inversion X. subst. constructor ; auto.
repeat split ; auto.
all: apply X0 ; eauto.
- simpl. constructor. induction m.
+ constructor.
+ simpl. inversion X. subst. constructor ; auto.
repeat split ; auto.
all: apply X0 ; eauto.
Qed.
Corollary eq_term_tm_nl :
forall `{checker_flags} G u, eq_term G u (nl u).
Proof.
intros flags G u.
eapply eq_term_upto_univ_tm_nl.
- intro. eapply eq_universe_refl.
- intro. eapply eq_universe_refl.
Qed.
Definition nl_constant_body c :=
Build_constant_body
(nl c.(cst_type)) (option_map nl c.(cst_body)) c.(cst_universes).
Definition nl_one_inductive_body o :=
Build_one_inductive_body
o.(ind_name)
(nl o.(ind_type))
o.(ind_kelim)
(map (fun '((x,y),n) => ((x, nl y), n)) o.(ind_ctors))
(map (fun '(x,y) => (x, nl y)) o.(ind_projs)).
Definition nl_mutual_inductive_body m :=
Build_mutual_inductive_body
m.(ind_finite)
m.(ind_npars)
(nlctx m.(ind_params))
(map nl_one_inductive_body m.(ind_bodies))
m.(ind_universes).
Definition nl_global_decl (d : global_decl) : global_decl :=
match d with
| ConstantDecl kn cb => ConstantDecl kn (nl_constant_body cb)
| InductiveDecl kn mib => InductiveDecl kn (nl_mutual_inductive_body mib)
end.
Definition nlg (Σ : global_env_ext) : global_env_ext :=
let '(Σ, φ) := Σ in
(map nl_global_decl Σ, φ).
Fixpoint nlstack (π : stack) : stack :=
match π with
| ε => ε
| App u ρ =>
App (nl u) (nlstack ρ)
| Fix f n args ρ =>
Fix (map (map_def_anon nl nl) f) n (map nl args) (nlstack ρ)
| CoFix f n args ρ =>
CoFix (map (map_def_anon nl nl) f) n (map nl args) (nlstack ρ)
| Case indn p brs ρ =>
Case indn (nl p) (map (on_snd nl) brs) (nlstack ρ)
| Proj p ρ =>
Proj p (nlstack ρ)
| Prod_l na B ρ =>
Prod_l nAnon (nl B) (nlstack ρ)
| Prod_r na A ρ =>
Prod_r nAnon (nl A) (nlstack ρ)
| Lambda_ty na b ρ =>
Lambda_ty nAnon (nl b) (nlstack ρ)
| Lambda_tm na A ρ =>
Lambda_tm nAnon (nl A) (nlstack ρ)
| coApp t ρ =>
coApp (nl t) (nlstack ρ)
end.
Lemma nlstack_appstack :
forall args ρ,
nlstack (appstack args ρ) = appstack (map nl args) (nlstack ρ).
Proof.
intros args ρ.
induction args in ρ |- *.
- reflexivity.
- simpl. f_equal. eapply IHargs.
Qed.
Lemma nlstack_cat :
forall ρ θ,
nlstack (ρ +++ θ) = nlstack ρ +++ nlstack θ.
Proof.
intros ρ θ.
induction ρ in θ |- *.
all: solve [ simpl ; rewrite ?IHρ ; reflexivity ].
Qed.
Lemma stack_position_nlstack :
forall ρ,
stack_position (nlstack ρ) = stack_position ρ.
Proof.
intros ρ.
induction ρ.
all: (simpl ; rewrite ?IHρ ; reflexivity).
Qed.
Lemma nl_it_mkLambda_or_LetIn :
forall Γ t,
nl (it_mkLambda_or_LetIn Γ t) =
it_mkLambda_or_LetIn (nlctx Γ) (nl t).
Proof.
intros Γ t.
induction Γ as [| [na [b|] B] Γ ih] in t |- *.
- reflexivity.
- simpl. cbn. rewrite ih. reflexivity.
- simpl. cbn. rewrite ih. reflexivity.
Qed.
Lemma nl_mkApps :
forall t l,
nl (mkApps t l) = mkApps (nl t) (map nl l).
Proof.
intros t l.
induction l in t |- *.
- reflexivity.
- simpl. rewrite IHl. reflexivity.
Qed.
Lemma nlctx_app_context :
forall Γ Δ,
nlctx (Γ ,,, Δ) = nlctx Γ ,,, nlctx Δ.
Proof.
intros Γ Δ.
induction Δ as [| [na [b|] B] Δ ih] in Γ |- *.
- reflexivity.
- simpl. f_equal. apply ih.
- simpl. f_equal. apply ih.
Qed.
Lemma nlctx_stack_context :
forall ρ,
nlctx (stack_context ρ) = stack_context (nlstack ρ).
Proof.
intro ρ. induction ρ.
all: (simpl ; rewrite ?IHρ ; reflexivity).
Qed.
Lemma nl_subst_instance_constr :
forall u b,
nl (subst_instance_constr u b) = subst_instance_constr u (nl b).
Proof.
intros u b.
induction b using term_forall_list_ind.
all: try (simpl ; rewrite ?IHb, ?IHb1, ?IHb2, ?IHb3 ; reflexivity).
- simpl. f_equal. induction H.
+ reflexivity.
+ simpl. rewrite p, IHAll. reflexivity.
- simpl. rewrite IHb1, IHb2. f_equal.
induction X.
+ reflexivity.
+ simpl. f_equal.
* unfold on_snd. destruct p, x. simpl in *.
rewrite p0. reflexivity.
* apply IHX.
- simpl. f_equal. induction X ; try reflexivity.
simpl. rewrite IHX. f_equal.
destruct p as [h1 h2].
destruct x. simpl in *.
unfold map_def, map_def_anon. cbn.
rewrite h1, h2. reflexivity.
- simpl. f_equal. induction X ; try reflexivity.
simpl. rewrite IHX. f_equal.
destruct p as [h1 h2].
destruct x. simpl in *.
unfold map_def, map_def_anon. cbn.
rewrite h1, h2. reflexivity.
Qed.
Lemma context_position_nlctx :
forall Γ,
context_position (nlctx Γ) = context_position Γ.
Proof.
intros Γ. induction Γ as [| [na [b|] A] Γ ih ].
- reflexivity.
- simpl. rewrite ih. reflexivity.
- simpl. rewrite ih. reflexivity.
Qed.
Lemma xposition_nlctx :
forall Γ π,
xposition (nlctx Γ) π = xposition Γ π.
Proof.
intros Γ π.
unfold xposition.
rewrite context_position_nlctx.
reflexivity.
Qed.
Lemma xposition_nlstack :
forall Γ π,
xposition Γ (nlstack π) = xposition Γ π.
Proof.
intros Γ π.
unfold xposition.
rewrite stack_position_nlstack.
reflexivity.
Qed.
Lemma nl_zipc :
forall t π,
nl (zipc t π) = zipc (nl t) (nlstack π).
Proof.
intros t π.
induction π in t |- *.
all: try solve [ simpl ; rewrite ?IHπ ; reflexivity ].
all: solve [
simpl ; rewrite IHπ ; cbn ; f_equal ;
rewrite nl_mkApps ; reflexivity
].
Qed.
Lemma nl_zipx :
forall Γ t π,
nl (zipx Γ t π) = zipx (nlctx Γ) (nl t) (nlstack π).
Proof.
intros Γ t π.
unfold zipx. rewrite nl_it_mkLambda_or_LetIn. f_equal.
apply nl_zipc.
Qed.
Lemma global_ext_levels_nlg :
forall Σ,
global_ext_levels (nlg Σ) = global_ext_levels Σ.
Proof.
intros [g ?].
cbn. unfold global_ext_levels. f_equal.
simpl. clear - g.
induction g. 1: reflexivity.
simpl. f_equal. 2: assumption.
destruct a. all: reflexivity.
Qed.
Lemma global_ext_constraints_nlg :
forall Σ,
global_ext_constraints (nlg Σ) = global_ext_constraints Σ.
Proof.
intros [g ?].
cbn. unfold global_ext_constraints.
f_equal. simpl. clear - g.
induction g. 1: reflexivity.
simpl. f_equal. 2: assumption.
destruct a. all: reflexivity.
Qed.
Lemma nl_lookup_env :
forall Σ c,
lookup_env (map nl_global_decl Σ) c
= option_map nl_global_decl (lookup_env Σ c).
Proof.
intros Σ c.
induction Σ. 1: reflexivity.
simpl.
replace (global_decl_ident (nl_global_decl a))
with (global_decl_ident a)
by (destruct a ; reflexivity).
destruct (ident_eq c (global_decl_ident a)).
- reflexivity.
- assumption.
Qed.
Lemma lookup_env_nlg :
forall Σ c decl,
lookup_env Σ.1 c = Some decl ->
lookup_env (nlg Σ) c = Some (nl_global_decl decl).
Proof.
intros [g ?] c decl h.
cbn. rewrite nl_lookup_env.
rewrite h. reflexivity.
Qed.
Lemma nlg_wf_local {cf : checker_flags} :
forall Σ Γ (hΓ : wf_local Σ Γ),
All_local_env_over
typing
(fun Σ Γ (_ : wf_local Σ Γ) (t T : term) (_ : Σ ;;; Γ |- t : T) =>
nlg Σ ;;; nlctx Γ |- nl t : nl T)
Σ Γ hΓ ->
wf_local (nlg Σ) (nlctx Γ).
Proof.
intros Σ Γ hΓ h.
induction h.
- constructor.
- simpl. unfold map_decl_anon. cbn. constructor. 1: assumption.
eexists. eassumption.
- simpl. unfold map_decl_anon. cbn. constructor.
+ assumption.
+ eexists. eassumption.
+ assumption.
Qed.
Lemma nl_lift :
forall n k t,
nl (lift n k t) = lift n k (nl t).
Proof.
intros n k t.
induction t in n, k |- * using term_forall_list_ind.
all: simpl.
all: try congruence.
- destruct (_ <=? _). all: reflexivity.
- f_equal. induction H.
+ reflexivity.
+ simpl. f_equal.
* eapply p.
* eapply IHAll.
- f_equal. 1-2: solve [ auto ].
induction X. 1: reflexivity.
simpl. f_equal. 2: assumption.
unfold on_snd. cbn. f_equal. auto.
- f_equal. rewrite map_length.
generalize (#|m| + k). intro l.
induction X.
+ reflexivity.
+ simpl. f_equal.
* unfold map_def_anon, map_def. simpl.
f_equal. all: eapply p.
* assumption.
- f_equal. rewrite map_length.
generalize (#|m| + k). intro l.
induction X.
+ reflexivity.
+ simpl. f_equal.
* unfold map_def_anon, map_def. simpl.
f_equal. all: eapply p.
* assumption.
Qed.
Lemma nl_subst :
forall s k u,
nl (subst s k u) = subst (map nl s) k (nl u).
Proof.
intros s k u.
induction u in s, k |- * using term_forall_list_ind.
all: simpl.
all: try congruence.
- destruct (_ <=? _). 2: reflexivity.
rewrite nth_error_map. destruct (nth_error _ _).
+ simpl. apply nl_lift.
+ rewrite map_length. reflexivity.
- f_equal. induction H.
+ reflexivity.
+ simpl. f_equal.
* eapply p.
* eapply IHAll.
- f_equal. 1-2: solve [ auto ].
induction X. 1: reflexivity.
simpl. f_equal. 2: assumption.
unfold on_snd. cbn. f_equal. auto.
- f_equal. rewrite map_length.
generalize (#|m| + k). intro l.
induction X.
+ reflexivity.
+ simpl. f_equal.
* unfold map_def_anon, map_def. simpl.
f_equal. all: eapply p.
* assumption.
- f_equal. rewrite map_length.
generalize (#|m| + k). intro l.
induction X.
+ reflexivity.
+ simpl. f_equal.
* unfold map_def_anon, map_def. simpl.
f_equal. all: eapply p.
* assumption.
Qed.
Lemma nl_eq_decl {cf:checker_flags} :
forall φ d d',
eq_decl φ d d' ->
eq_decl φ (map_decl nl d) (map_decl nl d').
Proof.
intros φ d d' [h1 h2].
split.
- simpl. destruct d as [? [?|] ?], d' as [? [?|] ?].
all: cbn in *.
all: trivial.
apply nl_eq_term. assumption.
- apply nl_eq_term. assumption.
Qed.
Lemma nl_eq_decl' {cf:checker_flags} :
forall φ d d',
eq_decl φ d d' ->
eq_decl φ (map_decl_anon nl d) (map_decl_anon nl d').
Proof.
intros φ d d' [h1 h2].
split.
- simpl. destruct d as [? [?|] ?], d' as [? [?|] ?].
all: cbn in *.
all: trivial.
apply nl_eq_term. assumption.
- apply nl_eq_term. assumption.
Qed.
Lemma nl_eq_context {cf:checker_flags} :
forall φ Γ Γ',
eq_context φ Γ Γ' ->
eq_context φ (nlctx Γ) (nlctx Γ').
Proof.
intros φ Γ Γ' h.
unfold eq_context, nlctx.
eapply All2_map, All2_impl.
- eassumption.
- apply nl_eq_decl'.
Qed.
Lemma nl_decompose_app :
forall t,
decompose_app (nl t)
= let '(u, vs) := decompose_app t in (nl u, map nl vs).
Proof.
intro t.
unfold decompose_app.
change [] with (map nl []) at 1. generalize (@nil term).
induction t. all: try reflexivity.
intro l. cbn. change (nl t2 :: map nl l) with (map nl (t2 :: l)).
apply IHt1.
Qed.
Lemma nl_fix_context :
forall mfix,
nlctx (fix_context mfix) = fix_context (map (map_def_anon nl nl) mfix).
Proof.
intro mfix.
unfold nlctx, fix_context, mapi.
generalize 0 at 2 4.
induction mfix.
- reflexivity.
- intro n. simpl. rewrite map_app. cbn. f_equal.
+ apply IHmfix.
+ unfold map_decl_anon. cbn. rewrite nl_lift. reflexivity.
Qed.
Lemma nl_red1 :
forall Σ Γ M N,
red1 Σ Γ M N ->
red1 (map nl_global_decl Σ) (nlctx Γ) (nl M) (nl N).
Proof.
intros Σ Γ M N h.
induction h using red1_ind_all.
all: simpl.
all: rewrite ?nl_lift, ?nl_subst, ?nl_subst_instance_constr.
all: try solve [ econstructor ; eauto ].
- constructor. unfold nlctx. rewrite nth_error_map.
destruct (nth_error Γ i). 2: discriminate.
cbn in *. apply some_inj in H. rewrite H. reflexivity.
- rewrite nl_mkApps. cbn.
replace (nl (iota_red pars c args brs))
with (iota_red pars c (map nl args) (map (on_snd nl) brs)).
+ eapply red_iota.
+ unfold iota_red. rewrite nl_mkApps.
rewrite map_skipn. rewrite nth_map. all: reflexivity.
- rewrite !nl_mkApps. cbn. eapply red_fix with (narg:=narg).
+ unfold unfold_fix in *. rewrite nth_error_map.
destruct (nth_error mfix idx). 2: discriminate.
cbn.
replace (isLambda (nl (dbody d))) with (isLambda (dbody d))
by (destruct (dbody d) ; reflexivity).
destruct (isLambda (dbody d)). 2: discriminate.
inversion H. subst. rewrite nl_subst.
repeat f_equal. clear.
unfold fix_subst. rewrite map_length.
induction #|mfix|.
* reflexivity.
* cbn. rewrite IHn. reflexivity.
+ unfold is_constructor in *.
rewrite nth_error_map. destruct (nth_error args narg) ; [| discriminate ].
cbn. unfold isConstruct_app in *. rewrite nl_decompose_app.
destruct (decompose_app t) as [u ?].
destruct u. all: try discriminate.
reflexivity.
- rewrite !nl_mkApps. simpl. eapply red_cofix_case with (narg := narg).
unfold unfold_cofix in *. rewrite nth_error_map.
destruct (nth_error mfix idx). 2: discriminate.
cbn.
inversion H. subst. rewrite nl_subst.
repeat f_equal. clear.
unfold cofix_subst. rewrite map_length.
induction #|mfix|.
* reflexivity.
* cbn. rewrite IHn. reflexivity.
- rewrite !nl_mkApps. simpl. eapply red_cofix_proj with (narg := narg).
unfold unfold_cofix in *. rewrite nth_error_map.
destruct (nth_error mfix idx). 2: discriminate.
cbn.
inversion H. subst. rewrite nl_subst.
repeat f_equal. clear.
unfold cofix_subst. rewrite map_length.
induction #|mfix|.
* reflexivity.
* cbn. rewrite IHn. reflexivity.
- econstructor.
+ unfold declared_constant in *.
rewrite nl_lookup_env, H. reflexivity.
+ destruct decl as [? [?|] ?].
all: cbn in *.
all: congruence.
- rewrite nl_mkApps. cbn. constructor.
rewrite nth_error_map, H. reflexivity.
- constructor. eapply OnOne2_map, OnOne2_impl. 1: eassumption.
cbn. intros x y [[? ?] ?]. split. all: assumption.
- constructor. eapply OnOne2_map, OnOne2_impl. 1: eassumption.
cbn. intros x y [? ?]. all: assumption.
- constructor. eapply OnOne2_map, OnOne2_impl. 1: eassumption.
cbn. intros x y [[? ?] ?]. split. 1: assumption.
cbn. congruence.
- apply fix_red_body. eapply OnOne2_map, OnOne2_impl. 1: eassumption.
cbn. intros x y [[? ?] ?]. split.
+ rewrite nlctx_app_context, nl_fix_context in r0. assumption.
+ cbn. congruence.
- constructor. eapply OnOne2_map, OnOne2_impl. 1: eassumption.
cbn. intros x y [[? ?] ?]. split. 1: assumption.
cbn. congruence.
- apply cofix_red_body. eapply OnOne2_map, OnOne2_impl. 1: eassumption.
cbn. intros x y [[? ?] ?]. split.
+ rewrite nlctx_app_context, nl_fix_context in r0. assumption.
+ cbn. congruence.
Qed.
Lemma nl_cumul {cf:checker_flags} :
forall Σ Γ A B,
Σ ;;; Γ |- A <= B ->
nlg Σ ;;; nlctx Γ |- nl A <= nl B.
Proof.
intros Σ Γ A B h.
induction h.
- constructor. rewrite global_ext_constraints_nlg. apply nl_leq_term.
assumption.
- eapply cumul_red_l. 2: eassumption.
destruct Σ. apply nl_red1. assumption.
- eapply cumul_red_r. 1: eassumption.
destruct Σ. apply nl_red1. assumption.
Qed.
Lemma nl_destArity :
forall Γ A Δ s,
destArity Γ A = Some (Δ, s) ->
destArity (nlctx Γ) (nl A) = Some (nlctx Δ, s).
Proof.
intros Γ A Δ s h.
induction A in Γ, h |- *.
all: simpl in *. all: try discriminate.
- inversion h. reflexivity.
- apply (IHA2 (Γ ,, vass na A1) h).
- apply (IHA3 (Γ ,, vdef na A1 A2) h).
Qed.
Lemma nl_instantiate_params :
forall params args ty,
option_map nl (instantiate_params params args ty) =
instantiate_params (nlctx params) (map nl args) (nl ty).
Proof.
intros params args ty.
unfold instantiate_params.
assert (e :
option_map (fun '(s, ty0) => (map nl s, nl ty0))
(instantiate_params_subst (List.rev params) args [] ty)
= instantiate_params_subst (List.rev (nlctx params))
(map nl args) [] (nl ty)
).
{ replace (List.rev (nlctx params)) with (nlctx (List.rev params))
by (unfold nlctx ; rewrite map_rev ; reflexivity).
change [] with (map nl []) at 2.
generalize (List.rev params), (@nil term). clear.
intros params l.
induction params in ty, args, l |- *.
- destruct args. all: reflexivity.
- simpl. destruct a as [? [?|] ?].
+ simpl. destruct ty. all: try reflexivity.
simpl. rewrite IHparams. simpl.
rewrite nl_subst. reflexivity.
+ destruct ty. all: try reflexivity.
destruct args.
* reflexivity.
* rewrite IHparams. reflexivity.
}
rewrite <- e.
destruct (instantiate_params_subst _ _ _) as [[? ?]|].
- simpl. f_equal. apply nl_subst.
- reflexivity.
Qed.
Lemma nl_inds :
forall kn u bodies,
map nl (inds kn u bodies) = inds kn u (map nl_one_inductive_body bodies).
Proof.
intros kn u bodies.
unfold inds. rewrite map_length.
induction #|bodies|.
- reflexivity.
- simpl. rewrite IHn. reflexivity.
Qed.
Lemma nl_decompose_prod_assum :
forall Γ t,
decompose_prod_assum (nlctx Γ) (nl t)
= let '(Γ, t) := decompose_prod_assum Γ t in (nlctx Γ, nl t).
Proof.
intros Γ t.
induction t in Γ |- *. all: try reflexivity.
- apply (IHt2 (Γ ,, vass na t1)).
- apply (IHt3 (Γ ,, vdef na t1 t2)).
Qed.
Lemma nl_it_mkProd_or_LetIn :
forall Γ A,
nl (it_mkProd_or_LetIn Γ A) = it_mkProd_or_LetIn (nlctx Γ) (nl A).
Proof.
intros Γ A.
induction Γ in A |- *.
- reflexivity.
- simpl. rewrite IHΓ. f_equal.
destruct a as [? [?|] ?].
all: reflexivity.
Qed.
Lemma nl_to_extended_list:
forall indctx : list context_decl,
map nl (to_extended_list indctx) = to_extended_list (nlctx indctx).
Proof.
intros indctx. unfold to_extended_list, to_extended_list_k.