forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICTyping.v
2143 lines (1804 loc) · 84.1 KB
/
PCUICTyping.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.
From MetaCoq.Template Require Import config utils Universes BasicAst AstUtils UnivSubst.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction PCUICReflect
PCUICLiftSubst PCUICUnivSubst PCUICEquality.
From MetaCoq Require Export LibHypsNaming.
Require Import String.
Require Import Wf Wellfounded Relation_Definitions.
Require Import Relation_Operators Lexicographic_Product Wf_nat.
Require Import ssreflect.
Local Open Scope string_scope.
Set Asymmetric Patterns.
(** * Typing derivations
Inductive relations for reduction, conversion and typing of PCUIC terms.
These come with many additional functions, to define the reduction operations,
deal with arities, declarations in the environment etc...
*)
Definition isSort T :=
match T with
| tSort u => True
| _ => False
end.
Fixpoint isArity T :=
match T with
| tSort u => True
| tProd _ _ codom => isArity codom
| tLetIn _ _ _ codom => isArity codom
| _ => False
end.
Definition subst_context s k (Γ : context) : context :=
fold_context (fun k' => subst s (k' + k)) Γ.
Lemma subst_context_length s n Γ : #|subst_context s n Γ| = #|Γ|.
Proof.
induction Γ as [|[na [body|] ty] tl] in Γ |- *; cbn; eauto.
- rewrite !List.rev_length !mapi_length !app_length !List.rev_length. simpl.
lia.
- rewrite !List.rev_length !mapi_length !app_length !List.rev_length. simpl.
lia.
Qed.
Fixpoint smash_context (Γ Γ' : context) : context :=
match Γ' with
| {| decl_body := Some b |} :: Γ' => smash_context (subst_context [b] 0 Γ) Γ'
| {| decl_body := None |} as d :: Γ' => smash_context (Γ ++ [d])%list Γ'
| [] => Γ
end.
Lemma smash_context_length Γ Γ' : #|smash_context Γ Γ'| = #|Γ| + context_assumptions Γ'.
Proof.
induction Γ' as [|[na [body|] ty] tl] in Γ |- *; cbn; eauto.
- now rewrite IHtl subst_context_length.
- rewrite IHtl app_length. simpl. lia.
Qed.
(** ** Environment lookup *)
Definition global_decl_ident d :=
match d with
| ConstantDecl id _ => id
| InductiveDecl id _ => id
end.
Fixpoint lookup_env (Σ : global_env) (id : ident) : option global_decl :=
match Σ with
| nil => None
| hd :: tl =>
if ident_eq id (global_decl_ident hd) then Some hd
else lookup_env tl id
end.
Definition declared_constant (Σ : global_env) (id : ident) decl : Prop :=
lookup_env Σ id = Some (ConstantDecl id decl).
Definition declared_minductive Σ mind decl :=
lookup_env Σ mind = Some (InductiveDecl mind decl).
Definition declared_inductive Σ mdecl ind decl :=
declared_minductive Σ (inductive_mind ind) mdecl /\
List.nth_error mdecl.(ind_bodies) (inductive_ind ind) = Some decl.
Definition declared_constructor Σ mdecl idecl cstr cdecl : Prop :=
declared_inductive Σ mdecl (fst cstr) idecl /\
List.nth_error idecl.(ind_ctors) (snd cstr) = Some cdecl.
Definition declared_projection Σ mdecl idecl (proj : projection) pdecl : Prop :=
declared_inductive Σ mdecl (fst (fst proj)) idecl /\
List.nth_error idecl.(ind_projs) (snd proj) = Some pdecl /\
mdecl.(ind_npars) = snd (fst proj).
(* TODO fix lookup env *)
Lemma lookup_env_cst_inv {Σ c k cst} :
lookup_env Σ c = Some (ConstantDecl k cst) -> c = k.
Proof.
induction Σ. simpl. discriminate.
simpl. destruct AstUtils.ident_eq eqn:Heq. intros [= ->]. simpl in Heq.
now destruct (AstUtils.ident_eq_spec c k). auto.
Qed.
(** Inductive substitution, to produce a constructors' type *)
Definition inds ind u (l : list one_inductive_body) :=
let fix aux n :=
match n with
| 0 => []
| S n => tInd (mkInd ind n) u :: aux n
end
in aux (List.length l).
Lemma inds_length ind u l : #|inds ind u l| = #|l|.
Proof.
unfold inds. induction l; simpl; congruence.
Qed.
Lemma inds_spec ind u l :
inds ind u l = List.rev (mapi (fun i _ => tInd {| inductive_mind := ind; inductive_ind := i |} u) l).
Proof.
unfold inds, mapi. induction l using rev_ind. simpl. reflexivity.
now rewrite app_length /= Nat.add_1_r IHl mapi_rec_app /= rev_app_distr /= Nat.add_0_r.
Qed.
Definition type_of_constructor mdecl (cdecl : ident * term * nat) (c : inductive * nat) (u : list Level.t) :=
let mind := inductive_mind (fst c) in
subst0 (inds mind u mdecl.(ind_bodies)) (subst_instance_constr u (snd (fst cdecl))).
(** ** Reduction *)
(** *** Helper functions for reduction *)
Definition fix_subst (l : mfixpoint term) :=
let fix aux n :=
match n with
| 0 => []
| S n => tFix l n :: aux n
end
in aux (List.length l).
Definition unfold_fix (mfix : mfixpoint term) (idx : nat) :=
match List.nth_error mfix idx with
| Some d =>
if isLambda d.(dbody) then
Some (d.(rarg), subst0 (fix_subst mfix) d.(dbody))
else None (* We don't unfold ill-formed fixpoints, which would
render confluence unprovable, creating an infinite
number of critical pairs between unfoldings of fixpoints.
e.g. [fix f := f] is not allowed. *)
| None => None
end.
Definition cofix_subst (l : mfixpoint term) :=
let fix aux n :=
match n with
| 0 => []
| S n => tCoFix l n :: aux n
end
in aux (List.length l).
Definition unfold_cofix (mfix : mfixpoint term) (idx : nat) :=
match List.nth_error mfix idx with
| Some d => Some (d.(rarg), subst0 (cofix_subst mfix) d.(dbody))
| None => None
end.
Definition is_constructor n ts :=
match List.nth_error ts n with
| Some a => isConstruct_app a
| None => false
end.
Lemma fix_subst_length mfix : #|fix_subst mfix| = #|mfix|.
Proof.
unfold fix_subst. generalize (tFix mfix). intros.
induction mfix; simpl; auto.
Qed.
Lemma cofix_subst_length mfix : #|cofix_subst mfix| = #|mfix|.
Proof.
unfold cofix_subst. generalize (tCoFix mfix). intros.
induction mfix; simpl; auto.
Qed.
Lemma fix_context_length mfix : #|fix_context mfix| = #|mfix|.
Proof. unfold fix_context. now rewrite List.rev_length mapi_length. Qed.
Definition tDummy := tVar "".
Definition iota_red npar c args brs :=
(mkApps (snd (List.nth c brs (0, tDummy))) (List.skipn npar args)).
(** *** One step strong beta-zeta-iota-fix-delta reduction
Inspired by the reduction relation from Coq in Coq [Barras'99].
*)
Local Open Scope type_scope.
Arguments OnOne2 {A} P%type l l'.
Notation on_Trel_eq R f g :=
(fun x y => (R (f x) (f y) * (g x = g y)))%type.
Inductive red1 (Σ : global_env) (Γ : context) : term -> term -> Type :=
(** Reductions *)
(** Beta *)
| red_beta na t b a :
red1 Σ Γ (tApp (tLambda na t b) a) (subst10 a b)
(** Let *)
| red_zeta na b t b' :
red1 Σ Γ (tLetIn na b t b') (subst10 b b')
| red_rel i body :
option_map decl_body (nth_error Γ i) = Some (Some body) ->
red1 Σ Γ (tRel i) (lift0 (S i) body)
(** Case *)
| red_iota ind pars c u args p brs :
red1 Σ Γ (tCase (ind, pars) p (mkApps (tConstruct ind c u) args) brs)
(iota_red pars c args brs)
(** Fix unfolding, with guard *)
| red_fix mfix idx args narg fn :
unfold_fix mfix idx = Some (narg, fn) ->
is_constructor narg args = true ->
red1 Σ Γ (mkApps (tFix mfix idx) args) (mkApps fn args)
(** CoFix-case unfolding *)
| red_cofix_case ip p mfix idx args narg fn brs :
unfold_cofix mfix idx = Some (narg, fn) ->
red1 Σ Γ (tCase ip p (mkApps (tCoFix mfix idx) args) brs)
(tCase ip p (mkApps fn args) brs)
(** CoFix-proj unfolding *)
| red_cofix_proj p mfix idx args narg fn :
unfold_cofix mfix idx = Some (narg, fn) ->
red1 Σ Γ (tProj p (mkApps (tCoFix mfix idx) args))
(tProj p (mkApps fn args))
(** Constant unfolding *)
| red_delta c decl body (isdecl : declared_constant Σ c decl) u :
decl.(cst_body) = Some body ->
red1 Σ Γ (tConst c u) (subst_instance_constr u body)
(** Proj *)
| red_proj i pars narg args k u arg:
nth_error args (pars + narg) = Some arg ->
red1 Σ Γ (tProj (i, pars, narg) (mkApps (tConstruct i k u) args)) arg
| abs_red_l na M M' N : red1 Σ Γ M M' -> red1 Σ Γ (tLambda na M N) (tLambda na M' N)
| abs_red_r na M M' N : red1 Σ (Γ ,, vass na N) M M' -> red1 Σ Γ (tLambda na N M) (tLambda na N M')
| letin_red_def na b t b' r : red1 Σ Γ b r -> red1 Σ Γ (tLetIn na b t b') (tLetIn na r t b')
| letin_red_ty na b t b' r : red1 Σ Γ t r -> red1 Σ Γ (tLetIn na b t b') (tLetIn na b r b')
| letin_red_body na b t b' r : red1 Σ (Γ ,, vdef na b t) b' r -> red1 Σ Γ (tLetIn na b t b') (tLetIn na b t r)
| case_red_pred ind p p' c brs : red1 Σ Γ p p' -> red1 Σ Γ (tCase ind p c brs) (tCase ind p' c brs)
| case_red_discr ind p c c' brs : red1 Σ Γ c c' -> red1 Σ Γ (tCase ind p c brs) (tCase ind p c' brs)
| case_red_brs ind p c brs brs' : OnOne2 (on_Trel_eq (red1 Σ Γ) snd fst) brs brs' -> red1 Σ Γ (tCase ind p c brs) (tCase ind p c brs')
| proj_red p c c' : red1 Σ Γ c c' -> red1 Σ Γ (tProj p c) (tProj p c')
| app_red_l M1 N1 M2 : red1 Σ Γ M1 N1 -> red1 Σ Γ (tApp M1 M2) (tApp N1 M2)
| app_red_r M2 N2 M1 : red1 Σ Γ M2 N2 -> red1 Σ Γ (tApp M1 M2) (tApp M1 N2)
| prod_red_l na M1 M2 N1 : red1 Σ Γ M1 N1 -> red1 Σ Γ (tProd na M1 M2) (tProd na N1 M2)
| prod_red_r na M2 N2 M1 : red1 Σ (Γ ,, vass na M1) M2 N2 ->
red1 Σ Γ (tProd na M1 M2) (tProd na M1 N2)
| evar_red ev l l' : OnOne2 (red1 Σ Γ) l l' -> red1 Σ Γ (tEvar ev l) (tEvar ev l')
| fix_red_ty mfix0 mfix1 idx :
OnOne2 (on_Trel_eq (red1 Σ Γ) dtype (fun x => (dname x, dbody x, rarg x))) mfix0 mfix1 ->
red1 Σ Γ (tFix mfix0 idx) (tFix mfix1 idx)
| fix_red_body mfix0 mfix1 idx :
OnOne2 (on_Trel_eq (red1 Σ (Γ ,,, fix_context mfix0)) dbody (fun x => (dname x, dtype x, rarg x)))
mfix0 mfix1 ->
red1 Σ Γ (tFix mfix0 idx) (tFix mfix1 idx)
| cofix_red_ty mfix0 mfix1 idx :
OnOne2 (on_Trel_eq (red1 Σ Γ) dtype (fun x => (dname x, dbody x, rarg x))) mfix0 mfix1 ->
red1 Σ Γ (tCoFix mfix0 idx) (tCoFix mfix1 idx)
| cofix_red_body mfix0 mfix1 idx :
OnOne2 (on_Trel_eq (red1 Σ (Γ ,,, fix_context mfix0)) dbody (fun x => (dname x, dtype x, rarg x))) mfix0 mfix1 ->
red1 Σ Γ (tCoFix mfix0 idx) (tCoFix mfix1 idx).
Lemma red1_ind_all :
forall (Σ : global_env) (P : context -> term -> term -> Type),
(forall (Γ : context) (na : name) (t b a : term),
P Γ (tApp (tLambda na t b) a) (b {0 := a})) ->
(forall (Γ : context) (na : name) (b t b' : term), P Γ (tLetIn na b t b') (b' {0 := b})) ->
(forall (Γ : context) (i : nat) (body : term),
option_map decl_body (nth_error Γ i) = Some (Some body) -> P Γ (tRel i) ((lift0 (S i)) body)) ->
(forall (Γ : context) (ind : inductive) (pars c : nat) (u : universe_instance) (args : list term)
(p : term) (brs : list (nat * term)),
P Γ (tCase (ind, pars) p (mkApps (tConstruct ind c u) args) brs) (iota_red pars c args brs)) ->
(forall (Γ : context) (mfix : mfixpoint term) (idx : nat) (args : list term) (narg : nat) (fn : term),
unfold_fix mfix idx = Some (narg, fn) ->
is_constructor narg args = true -> P Γ (mkApps (tFix mfix idx) args) (mkApps fn args)) ->
(forall (Γ : context) (ip : inductive * nat) (p : term) (mfix : mfixpoint term) (idx : nat)
(args : list term) (narg : nat) (fn : term) (brs : list (nat * term)),
unfold_cofix mfix idx = Some (narg, fn) ->
P Γ (tCase ip p (mkApps (tCoFix mfix idx) args) brs) (tCase ip p (mkApps fn args) brs)) ->
(forall (Γ : context) (p : projection) (mfix : mfixpoint term) (idx : nat) (args : list term)
(narg : nat) (fn : term),
unfold_cofix mfix idx = Some (narg, fn) -> P Γ (tProj p (mkApps (tCoFix mfix idx) args)) (tProj p (mkApps fn args))) ->
(forall (Γ : context) (c : ident) (decl : constant_body) (body : term),
declared_constant Σ c decl ->
forall u : universe_instance, cst_body decl = Some body -> P Γ (tConst c u) (subst_instance_constr u body)) ->
(forall (Γ : context) (i : inductive) (pars narg : nat) (args : list term) (k : nat) (u : universe_instance)
(arg : term),
nth_error args (pars + narg) = Some arg ->
P Γ (tProj (i, pars, narg) (mkApps (tConstruct i k u) args)) arg) ->
(forall (Γ : context) (na : name) (M M' N : term),
red1 Σ Γ M M' -> P Γ M M' -> P Γ (tLambda na M N) (tLambda na M' N)) ->
(forall (Γ : context) (na : name) (M M' N : term),
red1 Σ (Γ,, vass na N) M M' -> P (Γ,, vass na N) M M' -> P Γ (tLambda na N M) (tLambda na N M')) ->
(forall (Γ : context) (na : name) (b t b' r : term),
red1 Σ Γ b r -> P Γ b r -> P Γ (tLetIn na b t b') (tLetIn na r t b')) ->
(forall (Γ : context) (na : name) (b t b' r : term),
red1 Σ Γ t r -> P Γ t r -> P Γ (tLetIn na b t b') (tLetIn na b r b')) ->
(forall (Γ : context) (na : name) (b t b' r : term),
red1 Σ (Γ,, vdef na b t) b' r -> P (Γ,, vdef na b t) b' r -> P Γ (tLetIn na b t b') (tLetIn na b t r)) ->
(forall (Γ : context) (ind : inductive * nat) (p p' c : term) (brs : list (nat * term)),
red1 Σ Γ p p' -> P Γ p p' -> P Γ (tCase ind p c brs) (tCase ind p' c brs)) ->
(forall (Γ : context) (ind : inductive * nat) (p c c' : term) (brs : list (nat * term)),
red1 Σ Γ c c' -> P Γ c c' -> P Γ (tCase ind p c brs) (tCase ind p c' brs)) ->
(forall (Γ : context) (ind : inductive * nat) (p c : term) (brs brs' : list (nat * term)),
OnOne2 (on_Trel_eq (Trel_conj (red1 Σ Γ) (P Γ)) snd fst) brs brs' ->
P Γ (tCase ind p c brs) (tCase ind p c brs')) ->
(forall (Γ : context) (p : projection) (c c' : term), red1 Σ Γ c c' -> P Γ c c' ->
P Γ (tProj p c) (tProj p c')) ->
(forall (Γ : context) (M1 N1 : term) (M2 : term), red1 Σ Γ M1 N1 -> P Γ M1 N1 ->
P Γ (tApp M1 M2) (tApp N1 M2)) ->
(forall (Γ : context) (M2 N2 : term) (M1 : term), red1 Σ Γ M2 N2 -> P Γ M2 N2 ->
P Γ (tApp M1 M2) (tApp M1 N2)) ->
(forall (Γ : context) (na : name) (M1 M2 N1 : term),
red1 Σ Γ M1 N1 -> P Γ M1 N1 -> P Γ (tProd na M1 M2) (tProd na N1 M2)) ->
(forall (Γ : context) (na : name) (M2 N2 M1 : term),
red1 Σ (Γ,, vass na M1) M2 N2 -> P (Γ,, vass na M1) M2 N2 -> P Γ (tProd na M1 M2) (tProd na M1 N2)) ->
(forall (Γ : context) (ev : nat) (l l' : list term),
OnOne2 (Trel_conj (red1 Σ Γ) (P Γ)) l l' -> P Γ (tEvar ev l) (tEvar ev l')) ->
(forall (Γ : context) (mfix0 mfix1 : list (def term)) (idx : nat),
OnOne2 (on_Trel_eq (Trel_conj (red1 Σ Γ) (P Γ)) dtype (fun x => (dname x, dbody x, rarg x))) mfix0 mfix1 ->
P Γ (tFix mfix0 idx) (tFix mfix1 idx)) ->
(forall (Γ : context) (mfix0 mfix1 : list (def term)) (idx : nat),
OnOne2 (on_Trel_eq (Trel_conj (red1 Σ (Γ ,,, fix_context mfix0))
(P (Γ ,,, fix_context mfix0))) dbody
(fun x => (dname x, dtype x, rarg x))) mfix0 mfix1 ->
P Γ (tFix mfix0 idx) (tFix mfix1 idx)) ->
(forall (Γ : context) (mfix0 mfix1 : list (def term)) (idx : nat),
OnOne2 (on_Trel_eq (Trel_conj (red1 Σ Γ) (P Γ)) dtype (fun x => (dname x, dbody x, rarg x))) mfix0 mfix1 ->
P Γ (tCoFix mfix0 idx) (tCoFix mfix1 idx)) ->
(forall (Γ : context) (mfix0 mfix1 : list (def term)) (idx : nat),
OnOne2 (on_Trel_eq (Trel_conj (red1 Σ (Γ ,,, fix_context mfix0))
(P (Γ ,,, fix_context mfix0))) dbody
(fun x => (dname x, dtype x, rarg x))) mfix0 mfix1 ->
P Γ (tCoFix mfix0 idx) (tCoFix mfix1 idx)) ->
forall (Γ : context) (t t0 : term), red1 Σ Γ t t0 -> P Γ t t0.
Proof.
intros. rename X26 into Xlast. revert Γ t t0 Xlast.
fix aux 4. intros Γ t T.
move aux at top.
destruct 1; match goal with
| |- P _ (tFix _ _) (tFix _ _) => idtac
| |- P _ (tCoFix _ _) (tCoFix _ _) => idtac
| |- P _ (mkApps (tFix _ _) _) _ => idtac
| |- P _ (tCase _ _ (mkApps (tCoFix _ _) _) _) _ => idtac
| |- P _ (tProj _ (mkApps (tCoFix _ _) _)) _ => idtac
| H : _ |- _ => eapply H; eauto
end.
- eapply X3; eauto.
- eapply X4; eauto.
- eapply X5; eauto.
- revert brs brs' o.
fix auxl 3.
intros l l' Hl. destruct Hl.
constructor. intuition auto. constructor. intuition auto.
- revert l l' o.
fix auxl 3.
intros l l' Hl. destruct Hl.
constructor. split; auto.
constructor. auto.
- eapply X22.
revert mfix0 mfix1 o; fix auxl 3; intros l l' Hl; destruct Hl;
constructor; try split; auto; intuition.
- eapply X23.
revert o. generalize (fix_context mfix0). intros c Xnew.
revert mfix0 mfix1 Xnew; fix auxl 3; intros l l' Hl;
destruct Hl; constructor; try split; auto; intuition.
- eapply X24.
revert mfix0 mfix1 o.
fix auxl 3; intros l l' Hl; destruct Hl;
constructor; try split; auto; intuition.
- eapply X25.
revert o. generalize (fix_context mfix0). intros c new.
revert mfix0 mfix1 new; fix auxl 3; intros l l' Hl; destruct Hl;
constructor; try split; auto; intuition.
Defined.
(** *** Reduction
The reflexive-transitive closure of 1-step reduction. *)
Inductive red Σ Γ M : term -> Type :=
| refl_red : red Σ Γ M M
| trans_red : forall (P : term) N, red Σ Γ M P -> red1 Σ Γ P N -> red Σ Γ M N.
Fixpoint subst_app (t : term) (us : list term) : term :=
match t, us with
| tLambda _ A t, u :: us => subst_app (t {0 := u}) us
| _, [] => t
| _, _ => mkApps t us
end.
(** ** Utilities for typing *)
(** Decompose an arity into a context and a sort *)
Fixpoint destArity Γ (t : term) :=
match t with
| tProd na t b => destArity (Γ ,, vass na t) b
| tLetIn na b b_ty b' => destArity (Γ ,, vdef na b b_ty) b'
| tSort s => Some (Γ, s)
| _ => None
end.
Lemma destArity_app_aux {Γ Γ' t}
: destArity (Γ ,,, Γ') t = option_map (fun '(ctx, s) => (Γ ,,, ctx, s))
(destArity Γ' t).
Proof.
revert Γ'.
induction t; cbn; intro Γ'; try reflexivity.
- rewrite <- app_context_cons. now eapply IHt2.
- rewrite <- app_context_cons. now eapply IHt3.
Qed.
Lemma destArity_app {Γ t}
: destArity Γ t = option_map (fun '(ctx, s) => (Γ ,,, ctx, s))
(destArity [] t).
Proof.
exact (@destArity_app_aux Γ [] t).
Qed.
Lemma destArity_app_Some {Γ t ctx s}
: destArity Γ t = Some (ctx, s)
-> ∑ ctx', destArity [] t = Some (ctx', s) /\ ctx = Γ ,,, ctx'.
Proof.
intros H. rewrite destArity_app in H.
destruct (destArity [] t) as [[ctx' s']|]; cbn in *.
exists ctx'. inversion H. now subst.
discriminate H.
Qed.
Lemma mkApps_nonempty f l :
l <> [] -> mkApps f l = tApp (mkApps f (removelast l)) (last l f).
Proof.
destruct l using rev_ind. intros; congruence.
intros. rewrite <- mkApps_nested. simpl. f_equal.
rewrite removelast_app. congruence. simpl. now rewrite app_nil_r.
rewrite last_app. congruence.
reflexivity.
Qed.
Lemma destArity_tFix {mfix idx args} :
destArity [] (mkApps (tFix mfix idx) args) = None.
Proof.
induction args. reflexivity.
rewrite mkApps_nonempty.
intros e; discriminate e.
reflexivity.
Qed.
Lemma destArity_tApp {t u l} :
destArity [] (mkApps (tApp t u) l) = None.
Proof.
induction l. reflexivity.
rewrite mkApps_nonempty.
intros e; discriminate e.
reflexivity.
Qed.
(** Compute the type of a case from the predicate [p], actual parameters [pars] and
an inductive declaration. *)
Fixpoint instantiate_params_subst params pars s ty :=
match params with
| [] => match pars with
| [] => Some (s, ty)
| _ :: _ => None (* Too many arguments to substitute *)
end
| d :: params =>
match d.(decl_body), ty with
| None, tProd _ _ B =>
match pars with
| hd :: tl => instantiate_params_subst params tl (hd :: s) B
| [] => None (* Not enough arguments to substitute *)
end
| Some b, tLetIn _ _ _ b' => instantiate_params_subst params pars (subst0 s b :: s) b'
| _, _ => None (* Not enough products in the type *)
end
end.
Definition instantiate_params params pars ty :=
match instantiate_params_subst (List.rev params) pars [] ty with
| Some (s, ty) => Some (subst0 s ty)
| None => None
end.
Lemma instantiate_params_ params pars ty :
instantiate_params params pars ty
= option_map (fun '(s, ty) => subst0 s ty)
(instantiate_params_subst (List.rev params) pars [] ty).
Proof.
unfold instantiate_params.
repeat (destruct ?; cbnr).
Qed.
(* [params], [p] and output are already instanciated by [u] *)
Definition build_branches_type ind mdecl idecl params u p :=
let inds := inds (inductive_mind ind) u mdecl.(ind_bodies) in
let branch_type i '(id, t, ar) :=
let ty := subst0 inds (subst_instance_constr u t) in
match instantiate_params (subst_instance_context u mdecl.(ind_params)) params ty with
| Some ty =>
let '(sign, ccl) := decompose_prod_assum [] ty in
let nargs := List.length sign in
let allargs := snd (decompose_app ccl) in
let '(paramrels, args) := chop mdecl.(ind_npars) allargs in
let cstr := tConstruct ind i u in
let args := (args ++ [mkApps cstr (paramrels ++ to_extended_list sign)])%list in
Some (ar, it_mkProd_or_LetIn sign (mkApps (lift0 nargs p) args))
| None => None
end
in mapi branch_type idecl.(ind_ctors).
Lemma build_branches_type_ ind mdecl idecl params u p :
build_branches_type ind mdecl idecl params u p
= let inds := inds (inductive_mind ind) u mdecl.(ind_bodies) in
let branch_type i '(id, t, ar) :=
let ty := subst0 inds (subst_instance_constr u t) in
option_map (fun ty =>
let '(sign, ccl) := decompose_prod_assum [] ty in
let nargs := List.length sign in
let allargs := snd (decompose_app ccl) in
let '(paramrels, args) := chop mdecl.(ind_npars) allargs in
let cstr := tConstruct ind i u in
let args := (args ++ [mkApps cstr (paramrels ++ to_extended_list sign)])%list in
(ar, it_mkProd_or_LetIn sign (mkApps (lift0 nargs p) args)))
(instantiate_params (subst_instance_context u mdecl.(ind_params))
params ty)
in mapi branch_type idecl.(ind_ctors).
Proof.
apply mapi_ext. intros ? [[? ?] ?]; cbnr.
repeat (destruct ?; cbnr).
Qed.
(* [params], [p], [pty] and output already instanciated by [u] *)
Definition types_of_case ind mdecl idecl params u p pty :=
let brtys := build_branches_type ind mdecl idecl params u p in
match instantiate_params (subst_instance_context u mdecl.(ind_params)) params (subst_instance_constr u idecl.(ind_type)) with
| Some ity =>
match
destArity [] ity,
destArity [] pty,
map_option_out brtys
with
| Some (args, s), Some (args', s'), Some brtys =>
Some (args, args', s', brtys)
| _, _, _ => None
end
| None => None
end.
Lemma types_of_case_spec ind mdecl idecl pars u p pty indctx pctx ps btys :
types_of_case ind mdecl idecl pars u p pty
= Some (indctx, pctx, ps, btys)
<~> ∑ s', option_map (destArity [])
(instantiate_params (subst_instance_context u (ind_params mdecl)) pars (subst_instance_constr u (ind_type idecl)))
= Some (Some (indctx, s'))
/\ destArity [] pty = Some (pctx, ps)
/\ map_option_out (build_branches_type ind mdecl idecl pars u p)
= Some btys.
Proof.
unfold types_of_case.
repeat (destruct ?; cbn).
all: split; [try discriminate; inversion 1; subst; eexists; repeat split|].
all: intros [s' [HH1 [HH2 HH3]]]; inversion HH1; inversion HH2; now inversion HH3.
Qed.
Definition on_udecl_decl {A} (F : universes_decl -> A) d : A :=
match d with
| ConstantDecl _ cb => F cb.(cst_universes)
| InductiveDecl _ mb => F mb.(ind_universes)
end.
Definition monomorphic_udecl_decl := on_udecl_decl monomorphic_udecl.
Definition monomorphic_levels_decl := fst ∘ monomorphic_udecl_decl.
Definition monomorphic_constraints_decl := snd ∘ monomorphic_udecl_decl.
Definition universes_decl_of_decl := on_udecl_decl (fun x => x).
(* Definition LevelSet_add_list l := LevelSet.union (LevelSetProp.of_list l). *)
Definition LevelSet_pair x y
:= LevelSet.add y (LevelSet.singleton x).
Lemma LevelSet_pair_In x y z :
LevelSet.In x (LevelSet_pair y z) -> x = y \/ x = z.
Proof.
intro H. apply LevelSetFact.add_iff in H.
destruct H; [intuition|].
apply LevelSetFact.singleton_1 in H; intuition.
Qed.
Definition global_levels (Σ : global_env) : LevelSet.t
:= fold_right (fun decl lvls => LevelSet.union (monomorphic_levels_decl decl) lvls)
(LevelSet_pair Level.lSet Level.lProp) Σ.
Lemma global_levels_Set Σ :
LevelSet.mem Level.lSet (global_levels Σ) = true.
Proof.
induction Σ; simpl. reflexivity.
apply LevelSet.mem_spec, LevelSet.union_spec; right.
now apply LevelSet.mem_spec in IHΣ.
Qed.
Lemma global_levels_Prop Σ :
LevelSet.mem Level.lProp (global_levels Σ) = true.
Proof.
induction Σ; simpl. reflexivity.
apply LevelSet.mem_spec, LevelSet.union_spec; right.
now apply LevelSet.mem_spec in IHΣ.
Qed.
(** One can compute the constraints associated to a global environment or its
extension by folding over its constituent definitions.
We make *only* the second of these computations an implicit coercion
for more readability. Note that [fst_ctx] is also a coercion which goes
from a [global_env_ext] to a [global_env]: coercion coherence would *not*
be ensured if we added [global_constraints] as well as a coercion, as it
would forget the extension's constraints. *)
Definition global_constraints (Σ : global_env) : constraints
:= fold_right (fun decl ctrs => ConstraintSet.union
(monomorphic_constraints_decl decl) ctrs)
ConstraintSet.empty Σ.
Definition global_ext_levels (Σ : global_env_ext) : LevelSet.t
:= LevelSet.union (levels_of_udecl (snd Σ)) (global_levels Σ.1).
Definition global_ext_constraints (Σ : global_env_ext) : constraints
:= ConstraintSet.union (constraints_of_udecl (snd Σ))
(global_constraints Σ.1).
Coercion global_ext_constraints : global_env_ext >-> constraints.
Lemma prop_global_ext_levels Σ : LevelSet.In Level.prop (global_ext_levels Σ).
Proof.
destruct Σ as [Σ φ]; cbn.
apply LevelSetFact.union_3. cbn -[global_levels]; clear φ.
induction Σ.
- cbn. now apply LevelSetFact.add_1.
- simpl. now apply LevelSetFact.union_3.
Qed.
(** Check that [uctx] instantiated at [u] is consistent with
the current universe graph. *)
Definition consistent_instance `{checker_flags} (lvs : LevelSet.t) (φ : constraints) uctx (u : universe_instance) :=
match uctx with
| Monomorphic_ctx c => List.length u = 0
| Polymorphic_ctx c
| Cumulative_ctx (c, _) => (* FIXME Cumulative *)
(* no prop levels in instances *)
forallb (negb ∘ Level.is_prop) u /\
(* levels of the instance already declared *)
forallb (fun l => LevelSet.mem l lvs) u /\
List.length u = List.length c.1 /\
valid_constraints φ (subst_instance_cstrs u c.2)
end.
Definition consistent_instance_ext `{checker_flags} Σ
:= consistent_instance (global_ext_levels Σ) (global_ext_constraints Σ).
Reserved Notation " Σ ;;; Γ |- t : T " (at level 50, Γ, t, T at next level).
Reserved Notation " Σ ;;; Γ |- t <= u " (at level 50, Γ, t, u at next level).
(** ** Cumulativity *)
Inductive cumul `{checker_flags} (Σ : global_env_ext) (Γ : context) : term -> term -> Type :=
| cumul_refl t u : leq_term (global_ext_constraints Σ) t u -> Σ ;;; Γ |- t <= u
| cumul_red_l t u v : red1 Σ.1 Γ t v -> Σ ;;; Γ |- v <= u -> Σ ;;; Γ |- t <= u
| cumul_red_r t u v : Σ ;;; Γ |- t <= v -> red1 Σ.1 Γ u v -> Σ ;;; Γ |- t <= u
where " Σ ;;; Γ |- t <= u " := (cumul Σ Γ t u) : type_scope.
(** *** Conversion
Defined as cumulativity in both directions.
*)
Definition conv `{checker_flags} Σ Γ T U : Type :=
(Σ ;;; Γ |- T <= U) * (Σ ;;; Γ |- U <= T).
Notation " Σ ;;; Γ |- t = u " := (conv Σ Γ t u) (at level 50, Γ, t, u at next level) : type_scope.
Definition check_correct_arity `{checker_flags} φ decl ind u ctx pars pctx :=
let inddecl :=
{| decl_name := nNamed decl.(ind_name);
decl_body := None;
decl_type := mkApps (tInd ind u) (map (lift0 #|ctx|) pars ++ to_extended_list ctx) |}
in eq_context φ (inddecl :: ctx) pctx.
(** ** Typing relation *)
Section TypeLocal.
Context (typing : forall (Γ : context), term -> option term -> Type).
Inductive All_local_env : context -> Type :=
| localenv_nil :
All_local_env []
| localenv_cons_abs Γ na t :
All_local_env Γ ->
typing Γ t None ->
All_local_env (Γ ,, vass na t)
| localenv_cons_def Γ na b t :
All_local_env Γ ->
typing Γ t None ->
typing Γ b (Some t) ->
All_local_env (Γ ,, vdef na b t).
End TypeLocal.
Arguments localenv_nil {_}.
Arguments localenv_cons_def {_ _ _ _ _} _ _.
Arguments localenv_cons_abs {_ _ _ _} _ _.
(** Well-formedness of local environments embeds a sorting for each variable *)
Definition lift_typing (P : global_env_ext -> context -> term -> term -> Type) :
(global_env_ext -> context -> term -> option term -> Type) :=
fun Σ Γ t T =>
match T with
| Some T => P Σ Γ t T
| None => { s : universe & P Σ Γ t (tSort s) }
end.
Definition on_local_decl (P : context -> term -> option term -> Type) Γ d :=
match d.(decl_body) with
| Some b => P Γ b (Some d.(decl_type))
| None => P Γ d.(decl_type) None
end.
Section TypeLocalOver.
Context (typing : forall (Σ : global_env_ext) (Γ : context), term -> term -> Type).
Context (property : forall (Σ : global_env_ext) (Γ : context),
All_local_env (lift_typing typing Σ) Γ ->
forall (t T : term), typing Σ Γ t T -> Type).
Inductive All_local_env_over (Σ : global_env_ext) :
forall (Γ : context), All_local_env (lift_typing typing Σ) Γ -> Type :=
| localenv_over_nil :
All_local_env_over Σ [] localenv_nil
| localenv_over_cons_abs Γ na t
(all : All_local_env (lift_typing typing Σ) Γ) :
All_local_env_over Σ Γ all ->
forall (tu : lift_typing typing Σ Γ t None),
property Σ Γ all _ _ (projT2 tu) ->
All_local_env_over Σ (Γ ,, vass na t)
(localenv_cons_abs all tu)
| localenv_over_cons_def Γ na b t
(all : All_local_env (lift_typing typing Σ) Γ) (tb : typing Σ Γ b t) :
All_local_env_over Σ Γ all ->
property Σ Γ all _ _ tb ->
forall (tu : lift_typing typing Σ Γ t None),
property Σ Γ all _ _ (projT2 tu) ->
All_local_env_over Σ (Γ ,, vdef na b t)
(localenv_cons_def all tu tb).
End TypeLocalOver.
Section WfArity.
Context (typing : forall (Σ : global_env_ext) (Γ : context), term -> term -> Type).
Definition isWfArity Σ (Γ : context) T :=
{ ctx & { s & (destArity [] T = Some (ctx, s)) * All_local_env (lift_typing typing Σ) (Γ ,,, ctx) } }.
Context (property : forall (Σ : global_env_ext) (Γ : context),
All_local_env (lift_typing typing Σ) Γ ->
forall (t T : term), typing Σ Γ t T -> Type).
Definition isWfArity_prop Σ (Γ : context) T :=
{ wfa : isWfArity Σ Γ T & All_local_env_over typing property Σ _ (snd (projT2 (projT2 wfa))) }.
End WfArity.
(* AXIOM GUARD CONDITION *)
Axiom fix_guard : mfixpoint term -> bool.
Axiom fix_guard_red1 :
forall Σ Γ mfix mfix' idx,
fix_guard mfix ->
red1 Σ Γ (tFix mfix idx) (tFix mfix' idx) ->
fix_guard mfix'.
Axiom fix_guard_eq_term :
forall mfix mfix' idx,
fix_guard mfix ->
upto_names (tFix mfix idx) (tFix mfix' idx) ->
fix_guard mfix'.
Axiom fix_guard_rename :
forall mfix f,
let mfix' :=
map (map_def (rename f) (rename (shiftn (List.length mfix) f))) mfix
in
fix_guard mfix ->
fix_guard mfix'.
Axiom fix_guard_lift :
forall mfix n k,
let k' := (#|mfix| + k)%nat in
let mfix' := map (map_def (lift n k) (lift n k')) mfix in
fix_guard mfix ->
fix_guard mfix'.
Axiom fix_guard_subst :
forall mfix s k,
let k' := (#|mfix| + k)%nat in
let mfix' := map (map_def (subst s k) (subst s k')) mfix in
fix_guard mfix ->
fix_guard mfix'.
Axiom ind_guard : mutual_inductive_body -> bool.
Inductive typing `{checker_flags} (Σ : global_env_ext) (Γ : context) : term -> term -> Type :=
| type_Rel n decl :
All_local_env (lift_typing typing Σ) Γ ->
nth_error Γ n = Some decl ->
Σ ;;; Γ |- tRel n : lift0 (S n) decl.(decl_type)
| type_Sort l :
All_local_env (lift_typing typing Σ) Γ ->
LevelSet.In l (global_ext_levels Σ) ->
Σ ;;; Γ |- tSort (Universe.make l) : tSort (Universe.super l)
| type_Prod na A B s1 s2 :
Σ ;;; Γ |- A : tSort s1 ->
Σ ;;; Γ ,, vass na A |- B : tSort s2 ->
Σ ;;; Γ |- tProd na A B : tSort (Universe.sort_of_product s1 s2)
| type_Lambda na A t s1 B :
Σ ;;; Γ |- A : tSort s1 ->
Σ ;;; Γ ,, vass na A |- t : B ->
Σ ;;; Γ |- (tLambda na A t) : tProd na A B
| type_LetIn na b B t s1 A :
Σ ;;; Γ |- B : tSort s1 ->
Σ ;;; Γ |- b : B ->
Σ ;;; Γ ,, vdef na b B |- t : A ->
Σ ;;; Γ |- tLetIn na b B t : tLetIn na b B A
| type_App t na A B u :
Σ ;;; Γ |- t : tProd na A B ->
Σ ;;; Γ |- u : A ->
Σ ;;; Γ |- tApp t u : B{0 := u}
| type_Const cst u :
All_local_env (lift_typing typing Σ) Γ ->
forall decl (isdecl : declared_constant Σ.1 cst decl),
consistent_instance_ext Σ decl.(cst_universes) u ->
Σ ;;; Γ |- (tConst cst u) : subst_instance_constr u decl.(cst_type)
| type_Ind ind u :
All_local_env (lift_typing typing Σ) Γ ->
forall mdecl idecl (isdecl : declared_inductive Σ.1 mdecl ind idecl),
consistent_instance_ext Σ mdecl.(ind_universes) u ->
Σ ;;; Γ |- (tInd ind u) : subst_instance_constr u idecl.(ind_type)
| type_Construct ind i u :
All_local_env (lift_typing typing Σ) Γ ->
forall mdecl idecl cdecl (isdecl : declared_constructor Σ.1 mdecl idecl (ind, i) cdecl),
consistent_instance_ext Σ mdecl.(ind_universes) u ->
Σ ;;; Γ |- (tConstruct ind i u) : type_of_constructor mdecl cdecl (ind, i) u
| type_Case ind u npar p c brs args :
forall mdecl idecl (isdecl : declared_inductive Σ.1 mdecl ind idecl),
mdecl.(ind_npars) = npar ->
let pars := List.firstn npar args in
forall pty, Σ ;;; Γ |- p : pty ->
forall indctx pctx ps btys, types_of_case ind mdecl idecl pars u p pty = Some (indctx, pctx, ps, btys) ->
check_correct_arity (global_ext_constraints Σ) idecl ind u indctx pars pctx ->
existsb (leb_sort_family (universe_family ps)) idecl.(ind_kelim) ->
Σ ;;; Γ |- c : mkApps (tInd ind u) args ->
All2 (fun x y => (fst x = fst y) * (Σ ;;; Γ |- snd x : snd y) * (Σ ;;; Γ |- snd y : tSort ps)) brs btys ->
Σ ;;; Γ |- tCase (ind, npar) p c brs : mkApps p (List.skipn npar args ++ [c])
| type_Proj p c u :
forall mdecl idecl pdecl (isdecl : declared_projection Σ.1 mdecl idecl p pdecl) args,
Σ ;;; Γ |- c : mkApps (tInd (fst (fst p)) u) args ->
#|args| = ind_npars mdecl ->
let ty := snd pdecl in
Σ ;;; Γ |- tProj p c : subst0 (c :: List.rev args) (subst_instance_constr u ty)
| type_Fix mfix n decl :
let types := fix_context mfix in
fix_guard mfix ->
nth_error mfix n = Some decl ->
All_local_env (lift_typing typing Σ) (Γ ,,, types) ->
All (fun d => (Σ ;;; Γ ,,, types |- d.(dbody) : lift0 #|types| d.(dtype))
* (isLambda d.(dbody) = true)%type) mfix ->
Σ ;;; Γ |- tFix mfix n : decl.(dtype)
| type_CoFix mfix n decl :
allow_cofix ->
let types := fix_context mfix in
nth_error mfix n = Some decl ->
All_local_env (lift_typing typing Σ) (Γ ,,, types) ->
All (fun d => Σ ;;; Γ ,,, types |- d.(dbody) : lift0 #|types| d.(dtype)) mfix ->
Σ ;;; Γ |- tCoFix mfix n : decl.(dtype)
| type_Cumul t A B :
Σ ;;; Γ |- t : A ->
(isWfArity typing Σ Γ B + {s & Σ ;;; Γ |- B : tSort s})%type ->
Σ ;;; Γ |- A <= B -> Σ ;;; Γ |- t : B
where " Σ ;;; Γ |- t : T " := (typing Σ Γ t T) : type_scope.
Notation wf_local Σ Γ := (All_local_env (lift_typing typing Σ) Γ).
Lemma meta_conv {cf : checker_flags} Σ Γ t A B :
Σ ;;; Γ |- t : A ->
A = B ->
Σ ;;; Γ |- t : B.
Proof.
intros h []; assumption.
Qed.
(** ** Typechecking of global environments *)
Definition isType `{checker_flags} (Σ : global_env_ext) (Γ : context) (t : term) :=
{ s : _ & Σ ;;; Γ |- t : tSort s }.
Definition has_nparams npars ty :=
decompose_prod_n_assum [] npars ty <> None.