forked from MetaCoq/metacoq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCUICSafeChecker.v
1536 lines (1372 loc) · 57.6 KB
/
PCUICSafeChecker.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 monad_utils utils BasicAst AstUtils
UnivSubst.
From MetaCoq.Template Require Pretty.
From MetaCoq.Checker Require Import uGraph.
From MetaCoq.PCUIC Require Import PCUICAst PCUICAstUtils PCUICInduction
PCUICLiftSubst PCUICUnivSubst PCUICTyping PCUICNormal PCUICSR
PCUICGeneration PCUICReflect PCUICEquality PCUICInversion PCUICValidity
PCUICWeakening PCUICPosition PCUICCumulativity PCUICSafeLemmata PCUICSN.
From MetaCoq.SafeChecker Require Import PCUICSafeReduce PCUICSafeConversion.
Import MonadNotation.
Open Scope type_scope.
Open Scope list_scope.
Local Set Keyed Unification.
(* todo: move this *)
Arguments All_nil {_ _}.
Arguments All_cons {_ _ _ _}.
Lemma All_sq {A} {P : A -> Type} {l}
: All (fun x => ∥ P x ∥) l -> ∥ All P l ∥.
Proof.
induction 1.
- repeat constructor.
- sq; now constructor.
Qed.
Lemma All2_sq {A B} {R : A -> B -> Type} {l1 l2}
: All2 (fun x y => ∥ R x y ∥) l1 l2 -> ∥ All2 R l1 l2 ∥.
Proof.
induction 1.
- repeat constructor.
- sq; now constructor.
Qed.
Lemma weakening_sq `{cf : checker_flags} {Σ Γ} Γ' {t T} :
∥ wf Σ.1 ∥ -> ∥ wf_local Σ (Γ ,,, Γ') ∥ ->
∥ Σ ;;; Γ |- t : T ∥ ->
∥ Σ ;;; Γ ,,, Γ' |- lift0 #|Γ'| t : lift0 #|Γ'| T ∥.
Proof.
intros; sq; now eapply weakening.
Defined.
Definition wf_local_rel_abs_sq `{cf : checker_flags} {Σ Γ Γ' A na} :
∥ wf_local_rel Σ Γ Γ' ∥ -> {u & ∥ Σ ;;; Γ ,,, Γ' |- A : tSort u ∥ }
-> ∥ wf_local_rel Σ Γ (Γ',, vass na A) ∥.
Proof.
intros H [u Hu]; sq. now eapply wf_local_rel_abs.
Qed.
Definition wf_local_rel_def_sq `{cf : checker_flags} {Σ Γ Γ' t A na} :
∥ wf_local_rel Σ Γ Γ' ∥ -> ∥ isType Σ (Γ ,,, Γ') A ∥ -> ∥ Σ ;;; Γ ,,, Γ' |- t : A ∥
-> ∥ wf_local_rel Σ Γ (Γ',, vdef na t A) ∥.
Proof.
intros; sq. now eapply wf_local_rel_def.
Qed.
Definition wf_local_rel_app_inv_sq `{cf : checker_flags} {Σ Γ1 Γ2 Γ3} :
∥ wf_local_rel Σ Γ1 Γ2 ∥ -> ∥ wf_local_rel Σ (Γ1 ,,, Γ2) Γ3 ∥
-> ∥ wf_local_rel Σ Γ1 (Γ2 ,,, Γ3) ∥.
Proof.
intros; sq; now eapply wf_local_rel_app_inv.
Qed.
Fixpoint monad_All {T} {M : Monad T} {A} {P} (f : forall x, T (P x)) l : T (@All A P l)
:= match l with
| [] => ret All_nil
| a :: l => X <- f a ;;
Y <- monad_All f l ;;
ret (All_cons X Y)
end.
Fixpoint monad_All2 {T E} {M : Monad T} {M' : MonadExc E T} wrong_sizes
{A B R} (f : forall x y, T (R x y)) l1 l2
: T (@All2 A B R l1 l2)
:= match l1, l2 with
| [], [] => ret (All2_nil R)
| a :: l1, b :: l2 => X <- f a b ;;
Y <- monad_All2 wrong_sizes f l1 l2 ;;
ret (All2_cons R a b l1 l2 X Y)
| _, _ => raise wrong_sizes
end.
Definition monad_prod {T} {M : Monad T} {A B} (x : T A) (y : T B): T (A * B)
:= X <- x ;; Y <- y ;; ret (X, Y).
Definition check_dec {T E} {M : Monad T} {M' : MonadExc E T} (e : E) {P}
(H : {P} + {~ P})
: T P
:= match H with
| left x => ret x
| right _ => raise e
end.
Definition check_eq_true {T E} {M : Monad T} {M' : MonadExc E T} b (e : E)
: T (b = true)
:= if b return T (b = true) then ret eq_refl else raise e.
Program Definition check_eq_nat {T E} {M : Monad T} {M' : MonadExc E T} n m (e : E)
: T (n = m)
:= match Nat.eq_dec n m with
| left p => ret p
| right p => raise e
end.
Definition mkApps_decompose_app_rec t l :
mkApps t l = mkApps (fst (decompose_app_rec t l)) (snd (decompose_app_rec t l)).
Proof.
revert l; induction t; try reflexivity.
intro l; cbn in *.
transitivity (mkApps t1 ((t2 ::l))). reflexivity.
now rewrite IHt1.
Qed.
Definition mkApps_decompose_app t :
t = mkApps (fst (decompose_app t)) (snd (decompose_app t))
:= mkApps_decompose_app_rec t [].
Derive EqDec for sort_family.
Next Obligation.
unfold Classes.dec_eq. decide equality.
Defined.
Inductive type_error :=
| UnboundRel (n : nat)
| UnboundVar (id : string)
| UnboundEvar (ev : nat)
| UndeclaredConstant (c : string)
| UndeclaredInductive (c : inductive)
| UndeclaredConstructor (c : inductive) (i : nat)
| NotCumulSmaller (Γ : context) (t u t' u' : term)
| NotConvertible (Γ : context) (t u : term)
| NotASort (t : term)
| NotAProduct (t t' : term)
| NotAnInductive (t : term)
| IllFormedFix (m : mfixpoint term) (i : nat)
| UnsatisfiedConstraints (c : ConstraintSet.t)
| Msg (s : string).
Lemma lookup_env_id Σ id decl
: lookup_env Σ id = Some decl -> id = global_decl_ident decl.
Proof.
induction Σ; cbn. inversion 1.
destruct (ident_eq_spec id (global_decl_ident a)).
- inversion 1. now destruct H1.
- easy.
Qed.
Local Open Scope string_scope.
Definition string_of_list_aux {A} (f : A -> string) (l : list A) : string :=
let fix aux l :=
match l return string with
| nil => ""
| cons a nil => f a
| cons a l => f a ++ "," ++ aux l
end
in aux l.
Definition string_of_list {A} (f : A -> string) (l : list A) : string :=
"[" ++ string_of_list_aux f l ++ "]".
Definition string_of_level (l : Level.t) : string :=
match l with
| Level.lProp => "Prop"
| Level.lSet => "Set"
| Level.Level s => s
| Level.Var n => "Var " ++ string_of_nat n
end.
Definition string_of_level_expr (l : Level.t * bool) : string :=
let '(l, b) := l in
string_of_level l ++ (if b then "+1" else "").
Definition string_of_sort (u : universe) :=
string_of_list string_of_level_expr u.
Definition string_of_name (na : name) :=
match na with
| nAnon => "_"
| nNamed n => n
end.
Definition string_of_universe_instance u :=
string_of_list string_of_level u.
Definition string_of_def {A : Set} (f : A -> string) (def : def A) :=
"(" ++ string_of_name (dname def) ++ "," ++ f (dtype def) ++ "," ++ f (dbody def) ++ ","
++ string_of_nat (rarg def) ++ ")".
Definition string_of_inductive (i : inductive) :=
(inductive_mind i) ++ "," ++ string_of_nat (inductive_ind i).
Fixpoint string_of_term (t : term) :=
match t with
| tRel n => "#" ++ string_of_nat n
| tVar n => "Var(" ++ n ++ ")"
| tEvar ev args => "Evar(" ++ string_of_nat ev ++ "TODO" ++ ")"
| tSort s => "Sort(" ++ string_of_sort s ++ ")"
| tProd na b t => "Pi (" ++ string_of_name na ++ " : " ++
string_of_term b ++ ") (" ++ string_of_term t ++ ")"
| tLambda na b t => "Lam (" ++ string_of_name na ++ " : " ++ string_of_term b
++ ") (" ++ string_of_term t ++ ")"
| tLetIn na b t' t => "LetIn(" ++ string_of_name na ++ "," ++ string_of_term b
++ "," ++ string_of_term t' ++ "," ++ string_of_term t ++ ")"
| tApp f l => string_of_term f ++ " @ (" ++ string_of_term l ++ ")"
| tConst c u => "Const(" ++ c ++ "," ++ string_of_universe_instance u ++ ")"
| tInd i u => "Ind(" ++ string_of_inductive i ++ "," ++ string_of_universe_instance u ++ ")"
| tConstruct i n u => "Construct(" ++ string_of_inductive i ++ "," ++ string_of_nat n ++ ","
++ string_of_universe_instance u ++ ")"
| tCase (ind, i) t p brs =>
"Case(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ "," ++ string_of_term t ++ ","
++ string_of_term p ++ "," ++ string_of_list (fun b => string_of_term (snd b)) brs ++ ")"
| tProj (ind, i, k) c =>
"Proj(" ++ string_of_inductive ind ++ "," ++ string_of_nat i ++ "," ++ string_of_nat k ++ ","
++ string_of_term c ++ ")"
| tFix l n => "Fix(" ++ (string_of_list (string_of_def string_of_term) l) ++ "," ++ string_of_nat n ++ ")"
| tCoFix l n => "CoFix(" ++ (string_of_list (string_of_def string_of_term) l) ++ "," ++ string_of_nat n ++ ")"
end.
Definition string_of_type_error (e : type_error) : string :=
match e with
| UnboundRel n => "Unboound rel " ++ string_of_nat n
| UnboundVar id => "Unbound var " ++ id
| UnboundEvar ev => "Unbound evar " ++ string_of_nat ev
| UndeclaredConstant c => "Undeclared constant " ++ c
| UndeclaredInductive c => "Undeclared inductive " ++ (inductive_mind c)
| UndeclaredConstructor c i => "Undeclared inductive " ++ (inductive_mind c)
| NotCumulSmaller Γ t u t' u' => "Terms are not <= for cumulativity: " ++
string_of_term t ++ " and " ++ string_of_term u ++ " after reduction: " ++
string_of_term t' ++ " and " ++ string_of_term u'
| NotConvertible Γ t u => "Terms are not convertible: " ++
string_of_term t ++ " and " ++ string_of_term u
| NotASort t => "Not a sort"
| NotAProduct t t' => "Not a product"
| NotAnInductive t => "Not an inductive"
| IllFormedFix m i => "Ill-formed recursive definition"
| UnsatisfiedConstraints c => "Unsatisfied constraints"
| Msg s => "Msg: " ++ s
end.
Inductive typing_result (A : Type) :=
| Checked (a : A)
| TypeError (t : type_error).
Global Arguments Checked {A} a.
Global Arguments TypeError {A} t.
Instance typing_monad : Monad typing_result :=
{| ret A a := Checked a ;
bind A B m f :=
match m with
| Checked a => f a
| TypeError t => TypeError t
end
|}.
Instance monad_exc : MonadExc type_error typing_result :=
{ raise A e := TypeError e;
catch A m f :=
match m with
| Checked a => m
| TypeError t => f t
end
}.
Lemma wf_local_rel_inv `{checker_flags} {Σ Γ1 Γ2} (w : wf_local_rel Σ Γ1 Γ2) :
forall d Γ2', Γ2 = Γ2' ,, d ->
wf_local_rel Σ Γ1 Γ2' × (∑ u, Σ ;;; Γ1 ,,, Γ2' |- d.(decl_type) : tSort u) ×
match d.(decl_body) with
| Some b => Σ ;;; Γ1 ,,, Γ2' |- b : d.(decl_type)
| None => True
end.
Proof.
intros d Γ.
destruct w; inversion 1; subst; cbn in *.
split; [assumption|]. split; [assumption| trivial].
split; [assumption|]. split; [assumption| trivial].
Defined.
Definition strengthening_wf_local_rel `{cf : checker_flags} Σ Γ1 Γ2 Γ3 Γ4 :
wf Σ.1 -> wf_local_rel Σ Γ1 (Γ2 ,,, Γ3 ,,, lift_context #|Γ3| 0 Γ4)
-> wf_local_rel Σ Γ1 (Γ2 ,,, Γ4).
Proof.
intros HΣ H.
eapply wf_local_rel_app in H; destruct H as [H1 H2].
eapply wf_local_rel_app in H1; destruct H1 as [H1 H1'].
eapply wf_local_rel_app_inv. assumption.
revert H2. clear -HΣ. induction Γ4. intros; constructor.
intro H. rewrite lift_context_snoc0 in H.
destruct a as [na [b|] ty].
- unfold lift_decl, map_decl in H; cbn in H.
destruct (wf_local_rel_inv H _ _ eq_refl) as [H1 [H2 H3]]; cbn in *.
rewrite Nat.add_0_r in *.
rewrite app_context_assoc in *.
econstructor.
+ easy.
+ cbn. exists H2.π1. eapply strengthening. assumption. exact H2.π2.
+ cbn. eapply strengthening; eassumption.
- unfold lift_decl, map_decl in H; cbn in H.
destruct (wf_local_rel_inv H _ _ eq_refl) as [H1 [[u H2] _]]; cbn in *.
rewrite Nat.add_0_r in H2.
rewrite app_context_assoc in H2.
econstructor. easy.
exists u. eapply strengthening; eassumption.
Qed.
Definition wf_local_app_inv `{cf : checker_flags} {Σ Γ1 Γ2} :
wf_local Σ Γ1 -> wf_local_rel Σ Γ1 Γ2
-> wf_local Σ (Γ1 ,,, Γ2).
Proof.
intros H1 H2.
apply wf_local_local_rel, wf_local_rel_app_inv.
now apply wf_local_rel_local.
now rewrite app_context_nil_l.
Defined.
Definition fix_context_i i mfix :=
List.rev (mapi_rec (fun i (d : BasicAst.def term)
=> vass (dname d) ((lift0 i) (dtype d))) mfix i).
Lemma lift_fix_context mfix : forall k k' j, j <= k ->
fix_context_i (k + k') mfix = lift_context k' j (fix_context_i k mfix).
Proof.
induction mfix. reflexivity.
intros k k' j Hj.
change ([vass (dname a) ((lift0 (k + k')) (dtype a))] ,,, fix_context_i (S (k + k')) mfix = lift_context k' j ([vass (dname a) ((lift0 k) (dtype a))] ,,, fix_context_i (S k) mfix)).
erewrite lift_context_app, (IHmfix (S k) k' (S j)); cbn.
unfold map_decl, vass; cbn. now rewrite simpl_lift.
now apply le_n_S.
Qed.
Lemma wf_ext_gc_of_uctx {cf:checker_flags} {Σ : global_env_ext} (HΣ : ∥ wf_ext Σ ∥)
: ∑ uctx', gc_of_uctx (global_ext_uctx Σ) = Some uctx'.
Proof.
pose proof (global_ext_uctx_consistent _ HΣ) as HC.
destruct Σ as [Σ φ].
simpl in HC.
unfold gc_of_uctx; simpl in *.
apply gc_consistent_iff in HC.
destruct (gc_of_constraints (global_ext_constraints (Σ, φ))).
eexists; reflexivity.
contradiction HC.
Qed.
Lemma wf_ext_is_graph {cf:checker_flags} {Σ : global_env_ext} (HΣ : ∥ wf_ext Σ ∥)
: ∑ G, is_graph_of_uctx G (global_ext_uctx Σ).
Proof.
destruct (wf_ext_gc_of_uctx HΣ) as [uctx Huctx].
exists (make_graph uctx). unfold is_graph_of_uctx. now rewrite Huctx.
Defined.
Lemma map_squash {A B} (f : A -> B) : ∥ A ∥ -> ∥ B ∥.
Proof.
intros []; constructor; auto.
Qed.
Section Typecheck.
Context {cf : checker_flags} {Σ : global_env_ext} (HΣ : ∥ wf Σ ∥)
(Hφ : ∥ on_udecl Σ.1 Σ.2 ∥)
(G : universes_graph) (HG : is_graph_of_uctx G (global_ext_uctx Σ)).
Local Definition HΣ' : ∥ wf_ext Σ ∥.
Proof.
destruct HΣ, Hφ; now constructor.
Defined.
Definition isType_wellformed {Γ t}
: isType Σ Γ t -> wellformed Σ Γ t.
Proof.
intros []. left; now econstructor.
Qed.
(* Definition typ_to_wf {Γ t} *)
(* : { A : term | ∥ Σ ;;; Γ |- t : A ∥ } -> wellformed Σ Γ t. *)
(* Proof. *)
(* intros [A h]. sq. left. now exists A. *)
(* Defined. *)
Lemma validity_wf {Γ t T} :
∥ wf_local Σ Γ ∥ -> ∥ Σ ;;; Γ |- t : T ∥ -> wellformed Σ Γ T.
Proof.
destruct HΣ as [wΣ]. intros [wΓ] [X].
intros. eapply validity_term in X; try assumption.
destruct X. now right.
left; destruct i. now exists (tSort x).
Defined.
Definition hnf := reduce_term RedFlags.default Σ HΣ.
Theorem hnf_sound {Γ t h} : ∥ red (fst Σ) Γ t (hnf Γ t h) ∥.
Proof.
apply reduce_term_sound.
Defined.
Program Definition reduce_to_sort Γ t (h : wellformed Σ Γ t)
: typing_result (∑ u, ∥ red (fst Σ) Γ t (tSort u) ∥) :=
match t with
| tSort s => ret (s; sq (refl_red _ _ _))
| _ =>
match hnf Γ t h with
| tSort s => ret (s; _)
| _ => raise (NotASort t)
end
end.
Next Obligation.
pose proof (hnf_sound (h:=h)).
now rewrite <- Heq_anonymous in H0.
Defined.
Program Definition reduce_to_prod Γ t (h : wellformed Σ Γ t)
: typing_result (∑ na a b, ∥ red (fst Σ) Γ t (tProd na a b) ∥) :=
match t with
| tProd na a b => ret (na; a; b; sq (refl_red _ _ _))
| _ =>
match hnf Γ t h with
| tProd na a b => ret (na; a; b; _)
| t' => raise (NotAProduct t t')
end
end.
Next Obligation.
pose proof (hnf_sound (h:=h)).
now rewrite <- Heq_anonymous in H0.
Defined.
Fixpoint stack_to_apps π : typing_result (list term) :=
match π with
| Empty => ret []
| App t π => l <- stack_to_apps π ;; ret (t :: l)
| _ => raise (Msg "not some applications")
end.
Lemma zip_stack_to_apps t π l :
stack_to_apps π = Checked l -> zipc t π = mkApps t l.
Proof.
revert t l. induction π; cbn; try inversion 1.
reflexivity.
destruct (stack_to_apps π); inversion H1.
erewrite IHπ; reflexivity.
Qed.
Program Definition reduce_to_ind Γ t (h : wellformed Σ Γ t)
: typing_result (∑ i u l, ∥ red (fst Σ) Γ t (mkApps (tInd i u) l) ∥) :=
match decompose_app t with
| (tInd i u, l) => ret (i; u; l; sq _)
| _ =>
match reduce_stack RedFlags.default Σ HΣ Γ t Empty h with
| (tInd i u, π) => match stack_to_apps π with
| Checked l => ret (i; u; l; _)
| TypeError e => raise e
end
| _ => raise (NotAnInductive t)
end
end.
Next Obligation.
assert (X : mkApps (tInd i u) l = t); [|rewrite X; apply refl_red].
etransitivity. 2: symmetry; eapply mkApps_decompose_app.
pose proof (f_equal fst Heq_anonymous) as X; cbn in X; rewrite X; clear X.
pose proof (f_equal snd Heq_anonymous) as X; cbn in X; rewrite X; clear X.
reflexivity.
Defined.
Next Obligation.
pose proof (reduce_stack_sound RedFlags.default Σ HΣ _ _ Empty h).
rewrite <- Heq_anonymous1 in H0.
now erewrite <- zip_stack_to_apps.
Defined.
Definition iscumul Γ := isconv_term RedFlags.default Σ HΣ Hφ G HG Γ Cumul.
Program Definition convert_leq Γ t u
(ht : wellformed Σ Γ t) (hu : wellformed Σ Γ u)
: typing_result (∥ Σ ;;; Γ |- t <= u ∥) :=
match leqb_term G t u with true => ret _ | false =>
match iscumul Γ t ht u hu with
| true => ret _
| false => (* fallback *) (* nico *)
let t' := hnf Γ t ht in
let u' := hnf Γ u hu in
(* match leq_term (snd Σ) t' u' with true => ret _ | false => *)
raise (NotCumulSmaller Γ t u t' u')
(* end *)
end end.
Next Obligation.
symmetry in Heq_anonymous; eapply leqb_term_spec in Heq_anonymous; tea.
constructor. now constructor.
Qed.
Next Obligation.
symmetry in Heq_anonymous; apply isconv_term_sound in Heq_anonymous.
assumption.
Qed.
Section InferAux.
Variable (infer : forall Γ (HΓ : ∥ wf_local Σ Γ ∥) (t : term),
typing_result ({ A : term & ∥ Σ ;;; Γ |- t : A ∥ })).
Program Definition infer_type Γ HΓ t
: typing_result ({u : universe & ∥ Σ ;;; Γ |- t : tSort u ∥}) :=
tx <- infer Γ HΓ t ;;
u <- reduce_to_sort Γ tx.π1 _ ;;
ret (u.π1; _).
Next Obligation.
eapply validity_wf; eassumption.
Defined.
Next Obligation.
destruct HΣ, HΓ, X, X0.
now constructor; eapply type_reduction.
Defined.
Program Definition infer_cumul Γ HΓ t A (hA : ∥ isType Σ Γ A ∥)
: typing_result (∥ Σ ;;; Γ |- t : A ∥) :=
A' <- infer Γ HΓ t ;;
X <- convert_leq Γ A'.π1 A _ _ ;;
ret _.
Next Obligation. now eapply validity_wf. Qed.
Next Obligation. destruct hA; now apply isType_wellformed. Qed.
Next Obligation.
destruct hA as [[s hA]].
- destruct HΣ, HΓ, X, X0. constructor. econstructor; try eassumption.
right; eexists; eassumption.
Qed.
End InferAux.
Program Definition lookup_ind_decl ind
: typing_result
({decl & {body & declared_inductive (fst Σ) decl ind body}}) :=
match lookup_env (fst Σ) ind.(inductive_mind) with
| Some (InductiveDecl _ decl) =>
match nth_error decl.(ind_bodies) ind.(inductive_ind) with
| Some body => ret (decl; body; _)
| None => raise (UndeclaredInductive ind)
end
| _ => raise (UndeclaredInductive ind)
end.
Next Obligation.
split.
- symmetry in Heq_anonymous0.
etransitivity. eassumption.
erewrite (lookup_env_id _ _ _ Heq_anonymous0); reflexivity.
- now symmetry.
Defined.
Lemma check_constraints_spec ctrs
: check_constraints G ctrs -> valid_constraints (global_ext_constraints Σ) ctrs.
Proof.
pose proof HΣ'.
intros HH.
refine (check_constraints_spec G (global_ext_uctx Σ) _ _ HG _ HH).
now apply wf_ext_global_uctx_invariants.
now apply global_ext_uctx_consistent.
Qed.
Lemma is_graph_of_uctx_levels l :
wGraph.VSet.mem l (uGraph.wGraph.V G) ->
LevelSet.mem l (global_ext_levels Σ).
Proof.
unfold is_graph_of_uctx in HG.
case_eq (gc_of_uctx (global_ext_uctx Σ)); [intros [lvs cts] XX|intro XX];
rewrite XX in *; simpl in *; [|contradiction].
unfold gc_of_uctx in XX; simpl in XX.
destruct (gc_of_constraints Σ); [|discriminate].
inversion XX; subst. generalize (global_ext_levels Σ); intros lvs; cbn.
clear. intro H. apply LevelSet.mem_spec. apply wGraph.VSet.mem_spec in H.
apply LevelSetProp.FM.elements_2.
unfold no_prop_levels in H.
rewrite LevelSet.fold_spec in H.
cut (SetoidList.InA eq (level_of_no_prop l) (LevelSet.elements lvs)
\/ wGraph.VSet.In l wGraph.VSet.empty). {
intros [|H0]; [trivial|].
now apply wGraph.VSet.empty_spec in H0. }
revert H. generalize (LevelSet.elements lvs), wGraph.VSet.empty; clear.
intros lvs; induction lvs; cbn; [intuition|].
intros S H. apply IHlvs in H. destruct H as [H|H].
- left. now constructor 2.
- destruct a; cbn in *. now right.
all: apply wGraph.VSet.add_spec in H; destruct H as [H|H]; [left|now right].
all: rewrite H; now constructor.
Qed.
Program Definition check_consistent_instance uctx u
: typing_result (consistent_instance_ext Σ uctx u)
:= match uctx with
| Monomorphic_ctx _ =>
check_eq_nat #|u| 0 (Msg "monomorphic instance should be of length 0")
| Polymorphic_ctx (inst, cstrs)
| Cumulative_ctx ((inst, cstrs), _) =>
let '(inst, cstrs) := AUContext.repr (inst, cstrs) in
check_eq_true (forallb (fun l => match no_prop_of_level l with
| Some l => wGraph.VSet.mem l (uGraph.wGraph.V G)
| None => false
end) u)
(Msg "instance does not have the right length") ;;
(* check_eq_true (forallb (fun l => LevelSet.mem l lvs) u) ;; *)
X <- check_eq_nat #|u| #|inst|
(Msg "instance does not have the right length");;
match check_constraints G (subst_instance_cstrs u cstrs) with
| true => ret (conj _ _)
| false => raise (Msg "ctrs not satisfiable")
end
(* #|u| = #|inst| /\ valid_constraints φ (subst_instance_cstrs u cstrs) *)
end.
Next Obligation.
eapply forallb_All in H. eapply All_forallb'; tea.
clear. intros []; cbnr; trivial.
Qed.
Next Obligation.
repeat split.
- eapply forallb_All in H. eapply All_forallb'; tea.
intros []; simpl. discriminate.
all: apply is_graph_of_uctx_levels.
- now rewrite mapi_length in X.
- eapply check_constraints_spec; eauto.
Qed.
Next Obligation.
eapply forallb_All in H. eapply All_forallb'; tea.
clear. intros []; cbnr; trivial.
Qed.
Next Obligation.
repeat split.
- eapply forallb_All in H. eapply All_forallb'; tea.
intros []; simpl. discriminate.
all: apply is_graph_of_uctx_levels.
- now rewrite mapi_length in X.
- eapply check_constraints_spec; eauto.
Qed.
Definition eqb_opt_term (t u : option term) :=
match t, u with
| Some t, Some u => eqb_term G t u
| None, None => true
| _, _ => false
end.
Lemma eqb_opt_term_spec t u
: eqb_opt_term t u -> eq_opt_term (global_ext_constraints Σ) t u.
Proof.
destruct t, u; try discriminate; cbn.
apply eqb_term_spec; tea. trivial.
Qed.
Definition eqb_decl (d d' : context_decl) :=
eqb_opt_term d.(decl_body) d'.(decl_body) && eqb_term G d.(decl_type) d'.(decl_type).
Lemma eqb_decl_spec d d'
: eqb_decl d d' -> eq_decl (global_ext_constraints Σ) d d'.
Proof.
unfold eqb_decl, eq_decl.
intro H. utils.toProp. apply eqb_opt_term_spec in H.
eapply eqb_term_spec in H0; tea. now split.
Qed.
Definition eqb_context (Γ Δ : context) := forallb2 eqb_decl Γ Δ.
Lemma eqb_context_spec Γ Δ
: eqb_context Γ Δ -> eq_context (global_ext_constraints Σ) Γ Δ.
Proof.
unfold eqb_context, eq_context.
intro HH. apply forallb2_All2 in HH.
eapply All2_impl; try eassumption.
cbn. apply eqb_decl_spec.
Qed.
Definition check_correct_arity 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 eqb_context (inddecl :: ctx) pctx.
Lemma check_correct_arity_spec decl ind u ctx pars pctx
: check_correct_arity decl ind u ctx pars pctx
-> PCUICTyping.check_correct_arity (global_ext_constraints Σ) decl ind u ctx pars pctx.
Proof.
apply eqb_context_spec.
Qed.
Ltac sq :=
repeat match goal with
| H : ∥ _ ∥ |- _ => case H; clear H; intro H
end; try eapply sq.
(* Definition sq_type_CoFix {Γ mfix n decl} : *)
(* allow_cofix -> *)
(* let types := fix_context mfix in *)
(* nth_error mfix n = Some decl -> *)
(* ∥ wf_local Σ (Γ ,,, types) ∥ -> *)
(* All (fun d => ∥ Σ;;; Γ ,,, types |- dbody d : (lift0 #|types|) (dtype d) ∥) mfix -> *)
(* ∥ Σ;;; Γ |- tCoFix mfix n : dtype decl ∥. *)
(* Proof. *)
(* intros H types H0 H1 H2. *)
(* apply All_sq in H2. sq. *)
(* eapply type_CoFix; eauto. *)
(* Defined. *)
Program Fixpoint infer (Γ : context) (HΓ : ∥ wf_local Σ Γ ∥) (t : term) {struct t}
: typing_result ({ A : term & ∥ Σ ;;; Γ |- t : A ∥ }) :=
match t with
| tRel n =>
match nth_error Γ n with
| Some c => ret ((lift0 (S n)) (decl_type c); _)
| None => raise (UnboundRel n)
end
| tVar n => raise (UnboundVar n)
| tEvar ev args => raise (UnboundEvar ev)
| tSort u =>
match u with
| NEL.sing (l, false) =>
check_eq_true (LevelSet.mem l (global_ext_levels Σ)) (Msg ("undeclared level " ++ string_of_level l));;
ret (tSort (Universe.super l); _)
| _ => raise (Msg (string_of_sort u ++ " is not a level"))
end
| tProd na A B =>
s1 <- infer_type infer Γ HΓ A ;;
s2 <- infer_type infer (Γ ,, vass na A) _ B ;;
ret (tSort (Universe.sort_of_product s1.π1 s2.π1); _)
| tLambda na A t =>
s1 <- infer_type infer Γ HΓ A ;;
B <- infer (Γ ,, vass na A) _ t ;;
ret (tProd na A B.π1; _)
| tLetIn n b b_ty b' =>
infer_type infer Γ HΓ b_ty ;;
infer_cumul infer Γ HΓ b b_ty _ ;;
b'_ty <- infer (Γ ,, vdef n b b_ty) _ b' ;;
ret (tLetIn n b b_ty b'_ty.π1; _)
| tApp t u =>
ty <- infer Γ HΓ t ;;
pi <- reduce_to_prod Γ ty.π1 _ ;;
infer_cumul infer Γ HΓ u pi.π2.π1 _ ;;
ret (subst10 u pi.π2.π2.π1; _)
| tConst cst u =>
match lookup_env (fst Σ) cst with
| Some (ConstantDecl _ d) =>
check_consistent_instance d.(cst_universes) u ;;
let ty := subst_instance_constr u d.(cst_type) in
ret (ty; _)
| _ => raise (UndeclaredConstant cst)
end
| tInd ind u =>
d <- lookup_ind_decl ind ;;
check_consistent_instance d.π1.(ind_universes) u ;;
let ty := subst_instance_constr u d.π2.π1.(ind_type) in
ret (ty; _)
| tConstruct ind k u =>
d <- lookup_ind_decl ind ;;
match nth_error d.π2.π1.(ind_ctors) k with
| Some cdecl =>
check_consistent_instance d.π1.(ind_universes) u ;;
ret (type_of_constructor d.π1 cdecl (ind, k) u; _)
| None => raise (UndeclaredConstructor ind k)
end
| tCase (ind, par) p c brs =>
cty <- infer Γ HΓ c ;;
I <- reduce_to_ind Γ cty.π1 _ ;;
let '(ind'; I') := I in let '(u; I'') := I' in let '(args; H) := I'' in
check_eq_true (eqb ind ind')
(NotConvertible Γ (tInd ind u) (tInd ind' u)) ;;
d <- lookup_ind_decl ind' ;;
let '(decl; d') := d in let '(body; HH) := d' in
check_eq_true (ind_npars decl =? par)
(Msg "not the right number of parameters") ;;
pty <- infer Γ HΓ p ;;
match types_of_case ind decl body (firstn par args) u p pty.π1 with
| None => raise (Msg "not the type of a case")
| Some (indctx, pctx, ps, btys) =>
check_eq_true
(check_correct_arity body ind u indctx (firstn par args) pctx)
(Msg "not correct arity") ;;
check_eq_true
(existsb (leb_sort_family (universe_family ps)) (ind_kelim body))
(Msg "cannot eliminate over this sort") ;;
(fix check_branches (brs btys : list (nat * term))
(HH : Forall (squash ∘ (isType Σ Γ) ∘ snd) btys) {struct brs}
: typing_result
(All2 (fun x y => fst x = fst y /\ ∥ Σ ;;; Γ |- snd x : snd y ∥) brs btys)
:= match brs, btys with
| [], [] => ret (All2_nil _)
| (n, t) :: brs , (m, A) :: btys =>
W <- check_dec (Msg "not nat eq")
(EqDecInstances.nat_eqdec n m) ;;
Z <- infer_cumul infer Γ HΓ t A _ ;;
X <- check_branches brs btys _ ;;
ret (All2_cons _ _ _ _ _ (conj _ _) X)
| [], _ :: _
| _ :: _, [] => raise (Msg "wrong number of branches")
end) brs btys _ ;;
ret (mkApps p (List.skipn par args ++ [c]); _)
end
| tProj (ind, n, k) c =>
d <- lookup_ind_decl ind ;;
match nth_error d.π2.π1.(ind_projs) k with
| Some pdecl =>
c_ty <- infer Γ HΓ c ;;
I <- reduce_to_ind Γ c_ty.π1 _ ;;
let '(ind'; I') := I in let '(u; I'') := I' in let '(args; H) := I'' in
check_eq_true (eqb ind ind')
(NotConvertible Γ (tInd ind u) (tInd ind' u)) ;;
check_eq_true (ind_npars d.π1 =? n)
(Msg "not the right number of parameters") ;;
check_eq_true (#|args| =? ind_npars d.π1) (* probably redundant *)
(Msg "not the right number of parameters") ;;
let ty := snd pdecl in
ret (subst0 (c :: List.rev args) (subst_instance_constr u ty);
_)
| None => raise (Msg "projection not found")
end
| tFix mfix n =>
match nth_error mfix n with
| None => raise (IllFormedFix mfix n)
| Some decl =>
XX <- (fix check_types (mfix : mfixpoint term) acc (Hacc : ∥ wf_local_rel Σ Γ acc ∥) {struct mfix}
: typing_result (∥ wf_local_rel Σ (Γ ,,, acc) (fix_context_i #|acc| mfix) ∥)
:= match mfix with
| [] => ret (sq wf_local_rel_nil)
| def :: mfix =>
(* probably not tail recursive but needed so that next line terminates *)
W <- infer_type infer Γ HΓ (dtype def) ;;
let W' := weakening_sq acc _ _ W.π2 in
Z <- check_types mfix
(acc ,, vass (dname def) ((lift0 #|acc|) (dtype def)))
(wf_local_rel_abs_sq Hacc (W.π1; W')) ;;
ret (wf_local_rel_app_inv_sq
(wf_local_rel_abs_sq (sq wf_local_rel_nil) (W.π1; W')) Z)
end)
mfix [] (sq wf_local_rel_nil);;
YY <- (fix check_bodies (mfix' : mfixpoint term)
(XX' : ∥ wf_local_rel Σ Γ (fix_context mfix') ∥) {struct mfix'}
: typing_result (All (fun d =>
∥ Σ ;;; Γ ,,, fix_context mfix |- dbody d : (lift0 #|fix_context mfix|) (dtype d) ∥
/\ isLambda (dbody d) = true) mfix')
:= match mfix' with
| [] => ret All_nil
| def :: mfix' =>
W1 <- infer_cumul infer (Γ ,,, fix_context mfix) _ (dbody def)
(lift0 #|fix_context mfix| (dtype def)) _ ;;
W2 <- check_eq_true (isLambda (dbody def))
(Msg "not a lambda") ;;
Z <- check_bodies mfix' _ ;;
ret (All_cons (conj W1 W2) Z)
end) mfix _ ;;
guarded <- check_eq_true (fix_guard mfix) (Msg "Unguarded fixpoint") ;;
ret (dtype decl; _)
end
| tCoFix mfix n =>
(* to add when generalizing to all flags *)
allowcofix <- check_eq_true allow_cofix (Msg "cofix not allowed") ;;
match nth_error mfix n with
| None => raise (IllFormedFix mfix n)
| Some decl =>
XX <- (fix check_types (mfix : mfixpoint term) acc (Hacc : ∥ wf_local_rel Σ Γ acc ∥) {struct mfix}
: typing_result (∥ wf_local_rel Σ (Γ ,,, acc) (fix_context_i #|acc| mfix) ∥)
:= match mfix with
| [] => ret (sq wf_local_rel_nil)
| def :: mfix =>
(* probably not tail recursive but needed so that next line terminates *)
W <- infer_type infer Γ HΓ (dtype def) ;;
let W' := weakening_sq acc _ _ W.π2 in
Z <- check_types mfix
(acc ,, vass (dname def) ((lift0 #|acc|) (dtype def)))
(wf_local_rel_abs_sq Hacc (W.π1; W')) ;;
ret (wf_local_rel_app_inv_sq
(wf_local_rel_abs_sq (sq wf_local_rel_nil) (W.π1; W')) Z)
end)
mfix [] (sq wf_local_rel_nil);;
YY <- (fix check_bodies (mfix' : mfixpoint term) (XX' : ∥ wf_local_rel Σ Γ (fix_context mfix') ∥) {struct mfix'}
: typing_result (All (fun d =>
∥ Σ ;;; Γ ,,, fix_context mfix |- dbody d : (lift0 #|fix_context mfix|) (dtype d) ∥
) mfix')
:= match mfix' with
| [] => ret All_nil
| def :: mfix' =>
W1 <- infer_cumul infer (Γ ,,, fix_context mfix) _ (dbody def)
(lift0 #|fix_context mfix| (dtype def)) _ ;;
Z <- check_bodies mfix' _ ;;
ret (All_cons W1 Z)
end) mfix _ ;;
ret (dtype decl; _)
end
end.
Next Obligation. sq; now econstructor. Defined.
Next Obligation. sq; econstructor; tas.
now apply LevelSetFact.mem_2. Defined.
(* tProd *)
Next Obligation. sq; econstructor; cbn; easy. Defined.
Next Obligation. sq; econstructor; eassumption. Defined.
(* tLambda *)
Next Obligation. sq; econstructor; cbn; easy. Defined.
Next Obligation. sq; econstructor; eassumption. Defined.
(* tLetIn *)
Next Obligation. sq; econstructor; eauto. Defined.
Next Obligation. sq; econstructor; cbn; eauto. Defined.
Next Obligation. sq; now econstructor. Defined.
(* tApp *)
Next Obligation. now eapply validity_wf. Defined.
Next Obligation.
sq. eapply type_reduction in X1 ; try eassumption.
eapply validity_term in X1 ; try assumption. destruct X1.
- destruct i as [ctx [s [H1 H2]]]. cbn in H1.
apply destArity_app_Some in H1. destruct H1 as [ctx' [e1 e2]] ; subst.
rewrite app_context_assoc in H2. cbn in H2.
apply wf_local_app in H2.
inversion H2; subst. assumption.
- destruct i as [s HH].
eapply inversion_Prod in HH ; try assumption.
destruct HH as [s1 [_ [HH _]]].
eexists. eassumption.
Defined.
Next Obligation.
sq; econstructor. 2: eassumption.
eapply type_reduction; eassumption.
Defined.
(* tConst *)
Next Obligation.
sq; constructor; try assumption.
symmetry in Heq_anonymous.
etransitivity. eassumption.
now rewrite (lookup_env_id _ _ _ Heq_anonymous).
Defined.
(* tInd *)
Next Obligation. sq; econstructor; eassumption. Defined.
(* tConstruct *)
Next Obligation. sq; econstructor; try eassumption. now split. Defined.
(* tCase *)
Next Obligation. now eapply validity_wf. Defined.
Next Obligation. inversion HH; assumption. Qed.
Next Obligation. inversion HH; assumption. Qed.
Next Obligation.
destruct HΣ, HΓ, X.
change (eqb ind I = true) in H0.
destruct (eqb_spec ind I) as [e|e]; [destruct e|discriminate H0].
change (eqb (ind_npars d) par = true) in H1.
destruct (eqb_spec (ind_npars d) par) as [e|e]; [|discriminate].
rename Heq_anonymous into HH. symmetry in HH.
eapply (type_Case_valid_btys Σ Γ) in HH; tea.
eapply All_Forall, All_impl; tea. clear.
intros x X; constructor; now exists ps.
Defined.
Next Obligation.
change (eqb ind I = true) in H0.
destruct (eqb_spec ind I) as [e|e]; [destruct e|discriminate H0].
assert (∥ All2 (fun x y => ((fst x = fst y) *
(Σ;;; Γ |- snd x : snd y))%type) brs btys ∥). {
eapply All2_sq. eapply All2_impl. eassumption.
cbn; intros ? ? []. now sq. }
destruct HΣ, HΓ, X9, X8, X5, X, H.
constructor. eapply type_Case'; tea.
- apply beq_nat_true; assumption.
- symmetry; eassumption.
- apply check_correct_arity_spec; assumption.
- eapply type_reduction; eassumption.
Defined.
(* tProj *)
Next Obligation. now eapply validity_wf. Defined.
Next Obligation.
sq; eapply type_Proj with (pdecl := (i, t0)).
- split. eassumption. split. symmetry; eassumption. cbn in *.
now apply beq_nat_true.
- cbn. destruct (ssrbool.elimT (eqb_spec ind I)); [assumption|].
eapply type_reduction; eassumption.
- now apply beq_nat_true.
Defined.
(* tFix *)