-
Notifications
You must be signed in to change notification settings - Fork 83
/
Environment.v
1254 lines (1041 loc) · 48.8 KB
/
Environment.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 ssreflect ssrbool ssrfun Morphisms Setoid.
From MetaCoq.Utils Require Import utils.
From MetaCoq.Common Require Import BasicAst Primitive Universes.
From Equations.Prop Require Import Classes EqDecInstances.
Ltac Tauto.intuition_solver ::= auto with *.
Module Type Term.
Parameter Inline term : Type.
Parameter Inline tRel : nat -> term.
Parameter Inline tSort : Sort.t -> term.
Parameter Inline tProd : aname -> term -> term -> term.
Parameter Inline tLambda : aname -> term -> term -> term.
Parameter Inline tLetIn : aname -> term -> term -> term -> term.
Parameter Inline tInd : inductive -> Instance.t -> term.
Parameter Inline tProj : projection -> term -> term.
Parameter Inline mkApps : term -> list term -> term.
Parameter Inline lift : nat -> nat -> term -> term.
Parameter Inline subst : list term -> nat -> term -> term.
Parameter Inline closedn : nat -> term -> bool.
Parameter Inline noccur_between : nat -> nat -> term -> bool.
Parameter Inline subst_instance_constr : UnivSubst term.
Notation lift0 n := (lift n 0).
End Term.
Module Type TermDecide (Import T : Term).
#[export] Declare Instance term_eq_dec : EqDec term.
#[export] Hint Extern 0 (ReflectEq term) => exact (@EqDec_ReflectEq term term_eq_dec) : typeclass_instances.
End TermDecide.
Module TermDecideReflectInstances (Import T : Term) (Import TDec : TermDecide T).
#[export] Hint Extern 0 (ReflectEq term) => exact (@EqDec_ReflectEq term term_eq_dec) : typeclass_instances.
End TermDecideReflectInstances.
Module Retroknowledge.
Record t := mk_retroknowledge {
retro_int63 : option kername;
retro_float64 : option kername;
retro_string : option kername;
retro_array : option kername;
}.
Definition empty := {| retro_int63 := None; retro_float64 := None; retro_string := None; retro_array := None |}.
Definition extends (x y : t) :=
option_extends x.(retro_int63) y.(retro_int63) /\
option_extends x.(retro_float64) y.(retro_float64) /\
option_extends x.(retro_string) y.(retro_string) /\
option_extends x.(retro_array) y.(retro_array).
Existing Class extends.
Definition extendsb (x y : t) :=
option_extendsb x.(retro_int63) y.(retro_int63) &&
option_extendsb x.(retro_float64) y.(retro_float64) &&
option_extendsb x.(retro_string) y.(retro_string) &&
option_extendsb x.(retro_array) y.(retro_array).
Lemma extendsT x y : reflect (extends x y) (extendsb x y).
Proof.
rewrite /extends/extendsb; do 4 case: option_extendsT; cbn; constructor; intuition auto.
Qed.
Lemma extends_spec x y : extendsb x y <-> extends x y.
Proof.
rewrite /extends/extendsb -!option_extends_spec /is_true !Bool.andb_true_iff //=.
intuition auto.
Qed.
#[global] Instance extends_refl x : extends x x.
Proof.
repeat split; apply option_extends_refl.
Qed.
#[global] Instance extends_trans : RelationClasses.Transitive Retroknowledge.extends.
Proof.
intros x y z [? [? []]] [? [? []]]; repeat split; cbn; now etransitivity; tea.
Qed.
#[export,program] Instance reflect_t : ReflectEq t := {
eqb x y := (x.(retro_int63) == y.(retro_int63)) &&
(x.(retro_float64) == y.(retro_float64)) &&
(x.(retro_string) == y.(retro_string)) &&
(x.(retro_array) == y.(retro_array))
}.
Next Obligation.
do 4 case: eqb_spec; destruct x, y; cbn; intros; subst; constructor; congruence.
Qed.
(** This operation is asymmetric; it perfers the first argument when the arguments are incompatible, but otherwise takes the join *)
Definition merge (r1 r2 : t) : t
:= {| retro_int63 := match r1.(retro_int63) with Some v => Some v | None => r2.(retro_int63) end
; retro_float64 := match r1.(retro_float64) with Some v => Some v | None => r2.(retro_float64) end
; retro_string := match r1.(retro_string) with Some v => Some v | None => r2.(retro_string) end
; retro_array := match r1.(retro_array) with Some v => Some v | None => r2.(retro_array) end
|}.
Lemma extends_l_merge r1 r2
: extends r1 (merge r1 r2).
Proof.
rewrite /extends/merge; destruct r1, r2; cbn; repeat destruct ?; subst;
repeat constructor; clear; destruct_head' option; constructor.
Qed.
Lemma extends_merge_idempotent r1 r2
: extends r1 r2 -> merge r1 r2 = r2.
Proof.
rewrite /extends/merge; destruct r1, r2; cbn.
intro; rdest; destruct_head' (@option_extends); reflexivity.
Qed.
Definition compatible (x y : t) : bool
:= match x.(retro_int63), y.(retro_int63) with Some x, Some y => x == y | _, _ => true end
&& match x.(retro_float64), y.(retro_float64) with Some x, Some y => x == y | _, _ => true end
&& match x.(retro_string), y.(retro_string) with Some x, Some y => x == y | _, _ => true end
&& match x.(retro_array), y.(retro_array) with Some x, Some y => x == y | _, _ => true end.
Lemma extends_r_merge r1 r2
: compatible r1 r2 -> extends r2 (merge r1 r2).
Proof.
rewrite /extends/merge/compatible; destruct r1, r2; cbn; repeat destruct ?; subst.
all: repeat case: eqb_spec => //=.
all: intros; subst.
all: repeat constructor; clear; destruct_head' option; constructor.
Qed.
End Retroknowledge.
Export (hints) Retroknowledge.
Module Environment (T : Term).
Import T.
#[global] Existing Instance subst_instance_constr.
Definition judgment := judgment_ Sort.t term.
(** ** Declarations *)
Notation context_decl := (context_decl term).
(** Local (de Bruijn) variable binding *)
Definition vass x A : context_decl :=
{| decl_name := x ; decl_body := None ; decl_type := A |}.
(** Local (de Bruijn) let-binding *)
Definition vdef x t A : context_decl :=
{| decl_name := x ; decl_body := Some t ; decl_type := A |}.
(** Local (de Bruijn) context *)
Definition context := list context_decl.
(** Last declaration first *)
Definition lift_decl n k d := (map_decl (lift n k) d).
Definition lift_context n k (Γ : context) : context :=
fold_context_k (fun k' => lift n (k' + k)) Γ.
Lemma lift_context_alt n k Γ :
lift_context n k Γ =
mapi (fun k' d => lift_decl n (Nat.pred #|Γ| - k' + k) d) Γ.
Proof.
unfold lift_context. apply: fold_context_k_alt.
Qed.
Lemma lift_context_length n k Γ : #|lift_context n k Γ| = #|Γ|.
Proof. now rewrite /lift_context; len. Qed.
#[global] Hint Rewrite lift_context_length : len.
Definition subst_context s k (Γ : context) : context :=
fold_context_k (fun k' => subst s (k' + k)) Γ.
Definition subst_decl s k (d : context_decl) := map_decl (subst s k) d.
Lemma subst_context_length s n Γ : #|subst_context s n Γ| = #|Γ|.
Proof. now rewrite /subst_context; len. Qed.
#[global] Hint Rewrite subst_context_length : len.
Lemma subst_context_nil s n : subst_context s n [] = [].
Proof. reflexivity. Qed.
Lemma subst_context_alt s k Γ :
subst_context s k Γ =
mapi (fun k' d => subst_decl s (Nat.pred #|Γ| - k' + k) d) Γ.
Proof.
unfold subst_context, fold_context_k. rewrite rev_mapi. rewrite List.rev_involutive.
apply mapi_ext. intros. f_equal. now rewrite List.length_rev.
Qed.
Lemma subst_context_snoc s k Γ d : subst_context s k (d :: Γ) = subst_context s k Γ ,, subst_decl s (#|Γ| + k) d.
Proof.
now rewrite /subst_context fold_context_k_snoc0.
Qed.
Definition subst_telescope s k (Γ : context) : context :=
mapi (fun k' decl => map_decl (subst s (k' + k)) decl) Γ.
Global Instance subst_instance_decl : UnivSubst context_decl
:= map_decl ∘ subst_instance.
Global Instance subst_instance_context : UnivSubst context
:= map_context ∘ subst_instance.
Lemma subst_instance_length u (ctx : context)
: #|subst_instance u ctx| = #|ctx|.
Proof. unfold subst_instance, subst_instance_context, map_context. now rewrite length_map. Qed.
#[global] Hint Rewrite subst_instance_length : len.
Definition set_binder_name (na : aname) (x : context_decl) : context_decl :=
{| decl_name := na;
decl_body := decl_body x;
decl_type := decl_type x |}.
Fixpoint context_assumptions (Γ : context) :=
match Γ with
| [] => 0
| d :: Γ =>
match d.(decl_body) with
| Some _ => context_assumptions Γ
| None => S (context_assumptions Γ)
end
end.
Fixpoint is_assumption_context (Γ : context) :=
match Γ with
| [] => true
| d :: Γ =>
match d.(decl_body) with
| Some _ => false
| None => is_assumption_context Γ
end
end.
(** Smashing a context produces an assumption context. *)
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]) Γ'
| [] => Γ
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 length_app. simpl. lia.
Qed.
#[global] Hint Rewrite smash_context_length : len.
(* Smashing a context Γ with Δ depending on it is the same as smashing Γ
and substituting all references to Γ in Δ by the expansions of let bindings. *)
Lemma smash_context_app Δ Γ Γ' :
smash_context Δ (Γ ++ Γ') = smash_context (smash_context Δ Γ) Γ'.
Proof.
revert Δ; induction Γ as [|[na [b|] ty]]; intros Δ; simpl; auto.
Qed.
Fixpoint extended_subst (Γ : context) (n : nat)
(* Δ, smash_context Γ, n |- extended_subst Γ n : Γ *) :=
match Γ with
| nil => nil
| cons d vs =>
match decl_body d with
| Some b =>
(* Δ , vs |- b *)
let s := extended_subst vs n in
(* Δ , smash_context vs , n |- s : vs *)
let b' := lift (context_assumptions vs + n) #|s| b in
(* Δ, smash_context vs, n , vs |- b' *)
let b' := subst s 0 b' in
(* Δ, smash_context vs , n |- b' *)
b' :: s
| None => tRel n :: extended_subst vs (S n)
end
end.
Lemma extended_subst_length Γ n : #|extended_subst Γ n| = #|Γ|.
Proof.
induction Γ in n |- *; simpl; auto.
now destruct a as [? [?|] ?] => /=; simpl; rewrite IHΓ.
Qed.
#[global] Hint Rewrite extended_subst_length : len.
Definition expand_lets_k Γ k t :=
(subst (extended_subst Γ 0) k (lift (context_assumptions Γ) (k + #|Γ|) t)).
Definition expand_lets Γ t := expand_lets_k Γ 0 t.
Definition expand_lets_k_ctx Γ k Δ :=
(subst_context (extended_subst Γ 0) k (lift_context (context_assumptions Γ) (k + #|Γ|) Δ)).
Definition expand_lets_ctx Γ Δ := expand_lets_k_ctx Γ 0 Δ.
Lemma expand_lets_k_ctx_length Γ k Δ : #|expand_lets_k_ctx Γ k Δ| = #|Δ|.
Proof. now rewrite /expand_lets_k_ctx; len. Qed.
#[global] Hint Rewrite expand_lets_k_ctx_length : len.
Lemma expand_lets_ctx_length Γ Δ : #|expand_lets_ctx Γ Δ| = #|Δ|.
Proof. now rewrite /expand_lets_ctx; len. Qed.
#[global] Hint Rewrite expand_lets_ctx_length : len.
Definition fix_context (m : mfixpoint term) : context :=
List.rev (mapi (fun i d => vass d.(dname) (lift i 0 d.(dtype))) m).
(** *** Environments *)
Record constructor_body := {
cstr_name : ident;
(* The arguments and indices are typeable under the context of
arities of the mutual inductive + parameters *)
cstr_args : context;
cstr_indices : list term;
cstr_type : term;
(* Closed type: on well-formed constructors: forall params, cstr_args, I params cstr_indices *)
cstr_arity : nat; (* arity, w/o lets, w/o parameters *)
}.
Record projection_body := {
proj_name : ident;
(* The arguments and indices are typeable under the context of
arities of the mutual inductive + parameters *)
proj_relevance : relevance;
proj_type : term; (* Type under context of params and inductive object *)
}.
Definition map_constructor_body npars arities f c :=
{| cstr_name := c.(cstr_name);
cstr_args := fold_context_k (fun x => f (x + npars + arities)) c.(cstr_args);
cstr_indices := map (f (npars + arities + #|c.(cstr_args)|)) c.(cstr_indices);
(* Note only after positivity checking we can ensure that the indices do not mention the
inductive type.. beware of lets! *)
cstr_type := f arities c.(cstr_type);
cstr_arity := c.(cstr_arity) |}.
(* Here npars should be the [context_assumptions] of the parameters context. *)
Definition map_projection_body npars f c :=
{| proj_name := c.(proj_name);
proj_relevance := c.(proj_relevance);
proj_type := f (S npars) c.(proj_type)
|}.
(** See [one_inductive_body] from [declarations.ml]. *)
Record one_inductive_body := {
ind_name : ident;
ind_indices : context; (* Indices of the inductive types, under params *)
ind_sort : Sort.t; (* Sort of the inductive. *)
ind_type : term; (* Closed arity = forall mind_params, ind_indices, tSort ind_sort *)
ind_kelim : allowed_eliminations; (* Allowed eliminations *)
ind_ctors : list constructor_body;
ind_projs : list projection_body; (* names and types of projections, if any. *)
ind_relevance : relevance (* relevance of the inductive definition *) }.
Definition map_one_inductive_body npars arities f m :=
match m with
| Build_one_inductive_body ind_name ind_indices ind_sort
ind_type ind_kelim ind_ctors ind_projs ind_relevance =>
Build_one_inductive_body
ind_name (fold_context_k (fun x => f (npars + x)) ind_indices) ind_sort
(f 0 ind_type) ind_kelim (map (map_constructor_body npars arities f) ind_ctors)
(map (map_projection_body npars f) ind_projs) ind_relevance
end.
(** See [mutual_inductive_body] from [declarations.ml]. *)
Record mutual_inductive_body := {
ind_finite : recursivity_kind;
ind_npars : nat;
ind_params : context;
ind_bodies : list one_inductive_body ;
ind_universes : universes_decl;
ind_variance : option (list Universes.Variance.t) }.
(** See [constant_body] from [declarations.ml] *)
Record constant_body := {
cst_type : term;
cst_body : option term;
cst_universes : universes_decl;
cst_relevance : relevance }.
Definition map_constant_body f decl :=
{| cst_type := f decl.(cst_type);
cst_body := option_map f decl.(cst_body);
cst_universes := decl.(cst_universes);
cst_relevance := decl.(cst_relevance) |}.
Lemma map_cst_type f decl :
f (cst_type decl) = cst_type (map_constant_body f decl).
Proof. destruct decl; reflexivity. Qed.
Lemma map_cst_body f decl :
option_map f (cst_body decl) = cst_body (map_constant_body f decl).
Proof. destruct decl; reflexivity. Qed.
Inductive global_decl :=
| ConstantDecl : constant_body -> global_decl
| InductiveDecl : mutual_inductive_body -> global_decl.
Derive NoConfusion for global_decl.
Definition global_declarations := list (kername * global_decl).
Record global_env := mk_global_env
{ universes : ContextSet.t;
declarations : global_declarations;
retroknowledge : Retroknowledge.t }.
Coercion universes : global_env >-> ContextSet.t.
Definition empty_global_env :=
{| universes := ContextSet.empty;
declarations := [];
retroknowledge := Retroknowledge.empty |}.
Definition add_global_decl Σ decl :=
{| universes := Σ.(universes);
declarations := decl :: Σ.(declarations);
retroknowledge := Σ.(retroknowledge) |}.
Lemma eta_global_env Σ : Σ = {| universes := Σ.(universes); declarations := Σ.(declarations);
retroknowledge := Σ.(retroknowledge) |}.
Proof. now destruct Σ. Qed.
Definition set_declarations Σ decls :=
{| universes := Σ.(universes);
declarations := decls;
retroknowledge := Σ.(retroknowledge) |}.
Fixpoint lookup_global (Σ : global_declarations) (kn : kername) : option global_decl :=
match Σ with
| nil => None
| d :: tl =>
if kn == d.1 then Some d.2
else lookup_global tl kn
end.
Definition lookup_env (Σ : global_env) (kn : kername) := lookup_global Σ.(declarations) kn.
(* version for possibly duplicative environments *)
Fixpoint lookup_globals (Σ : global_declarations) (kn : kername) : list global_decl :=
match Σ with
| nil => nil
| d :: tl =>
let tl := lookup_globals tl kn in
if kn == d.1 then d.2 :: tl else tl
end.
Definition lookup_envs (Σ : global_env) (kn : kername) := lookup_globals Σ.(declarations) kn.
(** We define four notions of environment extension. The two
configurable bits are: is universe and retroknowledge extension
strict ([Logic.eq]) or loose ([⊂_cs] / [Retroknowledge.extends]);
and is declaration extension strict (fully order-preserving,
i.e., new declarations are added only at the front) or lax
(declarations may be reordered and added freely, as long as all
new declarations with existing names come before the old ones
with the same names, and the relative order of declarations with
identical names is preserved).
In most cases, we are actually interested only in duplicate-free
environments, where the lax construction is equivalent to merely
requiring that the lookup function agrees on all existing
delcarations. However, we formulate the property in a way that
makes sense for duplicative environments so that strict
declaration extension will always imply lax declaration
extension. (The lookup function prefers earlier / newer
declarations over older ones.)
We thus have the following implication structure:
<<<
┌-----------------------------┬------------------------┬---------------------------┐
| univ/retro extension is: | strict | lax |
├ declaration exension is ----┼------------------------┼---------------------------┤
| lax | extends_decls → extends |
| | ↑ ↗ ↑ |
| strict | strictly_extends_decls → extends_strictly_on_decls |
└-----------------------------┴------------------------┴---------------------------┘
>>>
*)
Notation extends_decls_part_globals Σ Σ'
:= (forall c, ∑ decls, lookup_globals Σ' c = decls ++ lookup_globals Σ c)
(only parsing).
Notation strictly_extends_decls_part_globals Σ Σ'
:= (∑ Σ'', Σ' = Σ'' ++ Σ)
(only parsing).
Notation extends_decls_part Σ Σ'
:= (forall c, ∑ decls, lookup_envs Σ' c = decls ++ lookup_envs Σ c)
(only parsing).
Notation strictly_extends_decls_part Σ Σ'
:= (strictly_extends_decls_part_globals Σ.(declarations) Σ'.(declarations))
(only parsing).
Definition extends (Σ Σ' : global_env) :=
[× Σ.(universes) ⊂_cs Σ'.(universes),
extends_decls_part Σ Σ' &
Retroknowledge.extends Σ.(retroknowledge) Σ'.(retroknowledge)].
Definition extends_decls (Σ Σ' : global_env) :=
[× Σ.(universes) = Σ'.(universes),
extends_decls_part Σ Σ' &
Σ.(retroknowledge) = Σ'.(retroknowledge)].
Definition extends_strictly_on_decls (Σ Σ' : global_env) :=
[× Σ.(universes) ⊂_cs Σ'.(universes),
strictly_extends_decls_part Σ Σ' &
Retroknowledge.extends Σ.(retroknowledge) Σ'.(retroknowledge)].
Definition strictly_extends_decls (Σ Σ' : global_env) :=
[× Σ.(universes) = Σ'.(universes),
strictly_extends_decls_part Σ Σ' &
Σ.(retroknowledge) = Σ'.(retroknowledge)].
Existing Class extends.
Existing Class extends_decls.
Existing Class extends_strictly_on_decls.
Existing Class strictly_extends_decls.
Lemma lookup_global_None Σ kn : ~In kn (List.map fst Σ) <-> lookup_global Σ kn = None.
Proof.
move: Σ; elim => //=; try tauto.
move => ??; case: eqb_spec; intuition congruence.
Qed.
Lemma hd_error_lookup_globals Σ kn : hd_error (lookup_globals Σ kn) = lookup_global Σ kn.
Proof.
move: Σ; elim => //= ?? <-.
case: eqb_spec => //=.
Qed.
Lemma lookup_globals_nil Σ kn : ~In kn (List.map fst Σ) <-> lookup_globals Σ kn = nil.
Proof.
rewrite lookup_global_None-hd_error_lookup_globals.
case: lookup_globals => //.
Qed.
Lemma NoDup_length_lookup_globals Σ
: NoDup (List.map fst Σ)
-> forall kn, List.length (lookup_globals Σ kn) = match lookup_global Σ kn with
| Some _ => 1
| None => 0
end.
Proof.
move => H kn.
move: Σ H; elim => //=; try lia.
move => ?? H. inversion 1; subst.
move: (H ltac:(assumption)).
case: eqb_spec => //= ->.
rewrite (proj1 (@lookup_global_None _ _)) => //= -> //=.
Qed.
Lemma NoDup_lookup_globals_eq Σ
: NoDup (List.map fst Σ)
-> forall kn, lookup_globals Σ kn = match lookup_global Σ kn with
| Some v => [v]
| None => []
end.
Proof.
move => H kn.
move: (NoDup_length_lookup_globals Σ H kn) (hd_error_lookup_globals Σ kn).
repeat destruct ?; subst.
all: case: lookup_globals; cbn; try congruence.
move => ? [|]; cbn; congruence.
Qed.
Lemma lookup_globals_In Σ kn decl
: In (kn, decl) Σ <-> In decl (lookup_globals Σ kn).
Proof.
move: Σ; elim => //=; try tauto.
move => [??]?; case: eqb_spec => ? //=; subst => <-; cbn in *; firstorder (subst; auto).
all: (idtac + constructor); congruence.
Qed.
Lemma lookup_global_Some_if_In Σ kn decl
: lookup_global Σ kn = Some decl -> In (kn, decl) Σ.
Proof.
move: Σ; elim => //=; try tauto.
move => [??]?; case: eqb_spec => ? IH; inversion 1; subst; try rewrite <- IH by assumption.
all: intuition try congruence; subst.
Qed.
Lemma lookup_global_Some_iff_In_NoDup Σ kn decl (H : NoDup (List.map fst Σ))
: In (kn, decl) Σ <-> lookup_global Σ kn = Some decl.
Proof.
rewrite -hd_error_lookup_globals lookup_globals_In.
apply NoDup_length_lookup_globals with (kn:=kn) in H; move: H.
case: lookup_global; case: lookup_globals => [|?[]]; cbn.
all: try lia.
all: intuition congruence.
Qed.
Lemma lookup_global_extends_NoDup Σ Σ' k d :
NoDup (List.map fst Σ') ->
lookup_global Σ k = Some d ->
extends_decls_part_globals Σ Σ' -> lookup_global Σ' k = Some d.
Proof.
rewrite /= -!hd_error_lookup_globals => Hnd.
move: (@NoDup_length_lookup_globals _ Hnd k); clear Hnd.
rewrite -hd_error_lookup_globals.
move=> H Hd eq.
move: (eq k); clear eq.
case => ls eq.
move: eq Hd H => ->.
case: ls => //= ?.
case => //=.
case: lookup_globals => //=.
Qed.
Lemma lookup_env_extends_NoDup Σ Σ' k d :
NoDup (List.map fst Σ'.(declarations)) ->
lookup_env Σ k = Some d ->
extends Σ Σ' -> lookup_env Σ' k = Some d.
Proof.
move => Hnd Hd; case => *.
eapply lookup_global_extends_NoDup; tea.
Qed.
Lemma lookup_globals_app Σ Σ' kn :
lookup_globals (Σ ++ Σ') kn = lookup_globals Σ kn ++ lookup_globals Σ' kn.
Proof.
move: Σ.
elim => //= ??.
case: eqb_spec => //= -> -> //=.
Qed.
Lemma strictly_extends_decls_extends_part_globals Σ Σ'
: strictly_extends_decls_part_globals Σ Σ' -> extends_decls_part_globals Σ Σ'.
Proof.
case => //= ? -> c.
rewrite lookup_globals_app.
eexists; reflexivity.
Qed.
Lemma strictly_extends_decls_extends_part Σ Σ'
: strictly_extends_decls_part Σ Σ' -> extends_decls_part Σ Σ'.
Proof. apply strictly_extends_decls_extends_part_globals. Qed.
#[global] Instance strictly_extends_decls_extends_decls Σ Σ' : strictly_extends_decls Σ Σ' -> extends_decls Σ Σ'.
Proof.
destruct Σ, Σ'; case => //= -> ? ->.
rewrite /extends_decls; split; try reflexivity.
now apply strictly_extends_decls_extends_part.
Qed.
#[global] Instance strictly_extends_decls_extends_strictly_on_decls Σ Σ' : strictly_extends_decls Σ Σ' -> extends_strictly_on_decls Σ Σ'.
Proof.
destruct Σ, Σ'; intros []. cbn in *; subst. split => //=.
split; [lsets|csets]. apply Retroknowledge.extends_refl.
Qed.
#[global] Instance extends_decls_extends Σ Σ' : extends_decls Σ Σ' -> extends Σ Σ'.
Proof.
destruct Σ, Σ'; intros []. cbn in *; subst. split => //=.
split; [lsets|csets]. apply Retroknowledge.extends_refl.
Qed.
#[global] Instance extends_strictly_on_decls_extends Σ Σ' : extends_strictly_on_decls Σ Σ' -> extends Σ Σ'.
Proof.
destruct Σ, Σ'; case => //= ? ? ?.
rewrite /extends; split => //=.
now apply strictly_extends_decls_extends_part.
Qed.
#[global] Instance strictly_extends_decls_extends_decls_subrel : CRelationClasses.subrelation strictly_extends_decls extends_decls := strictly_extends_decls_extends_decls.
#[global] Instance strictly_extends_decls_extends_strictly_on_decls_subrel : CRelationClasses.subrelation strictly_extends_decls extends_strictly_on_decls := strictly_extends_decls_extends_strictly_on_decls.
#[global] Instance extends_decls_extends_subrel : CRelationClasses.subrelation extends_decls extends := extends_decls_extends.
#[global] Instance extends_strictly_on_decls_extends_subrel : CRelationClasses.subrelation extends_strictly_on_decls extends := extends_strictly_on_decls_extends.
#[global] Instance strictly_extends_decls_extends_subrel : CRelationClasses.subrelation strictly_extends_decls extends := fun _ => _.
#[global] Instance strictly_extends_decls_refl : CRelationClasses.Reflexive strictly_extends_decls.
Proof. red. intros x. split => //; try exists [] => //. Qed.
#[global] Instance extends_decls_refl : CRelationClasses.Reflexive extends_decls.
Proof. red. intros x. split => //; try exists [] => //. Qed.
Lemma extends_strictly_on_decls_refl : CRelationClasses.Reflexive extends_strictly_on_decls.
Proof. red. intros x. split; [apply incl_cs_refl | try exists [] => // | apply Retroknowledge.extends_refl]. Qed.
Lemma extends_refl : CRelationClasses.Reflexive extends.
Proof. red. intros x. split; [apply incl_cs_refl | try exists [] => // | apply Retroknowledge.extends_refl]. Qed.
(* easy prefers this to the local hypotheses, which is annoying
#[global] Instance extends_refl : CRelationClasses.Reflexive extends.
Proof. apply extends_refl. Qed.
*)
Lemma extends_decls_part_globals_refl Σ : extends_decls_part_globals Σ Σ.
Proof. now exists [] => //. Qed.
Lemma extends_decls_part_refl Σ : extends_decls_part Σ Σ.
Proof. apply extends_decls_part_globals_refl. Qed.
Lemma strictly_extends_decls_part_globals_refl (Σ : global_declarations)
: strictly_extends_decls_part_globals Σ Σ.
Proof. now exists [] => //. Qed.
Lemma strictly_extends_decls_part_refl Σ : strictly_extends_decls_part Σ Σ.
Proof. apply strictly_extends_decls_part_globals_refl. Qed.
Lemma extends_decls_part_globals_trans Σ Σ' Σ''
: extends_decls_part_globals Σ Σ' -> extends_decls_part_globals Σ' Σ'' -> extends_decls_part_globals Σ Σ''.
Proof.
move => H1 H2 c; move: (H1 c) (H2 c) => [? ->] [? ->].
now eexists; rewrite app_assoc.
Qed.
Lemma extends_decls_part_trans Σ Σ' Σ''
: extends_decls_part Σ Σ' -> extends_decls_part Σ' Σ'' -> extends_decls_part Σ Σ''.
Proof. apply extends_decls_part_globals_trans. Qed.
Lemma strictly_extends_decls_part_globals_trans (Σ Σ' Σ'' : global_declarations)
: strictly_extends_decls_part_globals Σ Σ' -> strictly_extends_decls_part_globals Σ' Σ'' -> strictly_extends_decls_part_globals Σ Σ''.
Proof.
move => [? ->] [? ->].
now eexists; rewrite app_assoc.
Qed.
Lemma strictly_extends_decls_part_trans Σ Σ' Σ''
: strictly_extends_decls_part Σ Σ' -> strictly_extends_decls_part Σ' Σ'' -> strictly_extends_decls_part Σ Σ''.
Proof. apply strictly_extends_decls_part_globals_trans. Qed.
Local Ltac extends_trans_t :=
intros [?] [?] [?] [?] [?]; red; cbn in *; split;
try solve [ etransitivity; eassumption
| eapply incl_cs_trans; eassumption
| eapply strictly_extends_decls_part_globals_trans; eassumption
| eapply extends_decls_part_globals_trans; eassumption ].
#[global] Instance strictly_extends_decls_trans : CRelationClasses.Transitive strictly_extends_decls.
Proof. extends_trans_t. Qed.
#[global] Instance extends_decls_trans : CRelationClasses.Transitive extends_decls.
Proof. extends_trans_t. Qed.
#[global] Instance extends_strictly_on_decls_trans : CRelationClasses.Transitive extends_strictly_on_decls.
Proof. extends_trans_t. Qed.
#[global] Instance extends_trans : CRelationClasses.Transitive extends.
Proof. extends_trans_t. Qed.
(** Merge two lists of global_declarations, assuming that any globals sharing a name are identical *)
Definition declared_kername_set (Σ : global_declarations) : KernameSet.t
:= List.fold_right KernameSet.add KernameSet.empty (List.map fst Σ).
Definition merge_globals (Σ Σ' : global_declarations) : global_declarations
:= let known_kns := declared_kername_set Σ in
List.filter (fun '(kn, _) => negb (KernameSet.mem kn known_kns)) Σ' ++ Σ.
Definition merge_global_envs (Σ Σ' : global_env) : global_env
:= {| universes := ContextSet.union Σ.(universes) Σ'.(universes)
; declarations := merge_globals Σ.(declarations) Σ'.(declarations)
; retroknowledge := Retroknowledge.merge Σ.(retroknowledge) Σ'.(retroknowledge) |}.
Definition compatible_globals (Σ Σ' : global_declarations) : Prop
:= forall c, lookup_globals Σ c <> [] -> lookup_globals Σ' c <> [] -> lookup_globals Σ c = lookup_globals Σ' c.
Definition compatible (Σ Σ' : global_env)
:= Retroknowledge.compatible Σ.(retroknowledge) Σ'.(retroknowledge)
/\ compatible_globals Σ.(declarations) Σ'.(declarations).
Lemma lookup_globals_filter p Σ c
: lookup_globals (filter (fun '(kn, _) => p kn) Σ) c = if p c then lookup_globals Σ c else [].
Proof.
induction Σ as [|?? IH]; cbn; rdest; cbn; try now repeat destruct ?.
case: eqb_spec => ?; repeat destruct ?; subst => //=.
all: rewrite ?eqb_refl.
all: try case: eqb_spec => ?; subst.
all: rewrite IH //=.
all: try congruence.
Qed.
Lemma strictly_extends_decls_l_merge_globals Σ Σ'
: strictly_extends_decls_part_globals Σ (merge_globals Σ Σ').
Proof. now eexists. Qed.
Lemma extends_l_merge_globals Σ Σ'
: extends_decls_part_globals Σ (merge_globals Σ Σ').
Proof.
rewrite /merge_globals.
intro c.
rewrite lookup_globals_app lookup_globals_filter.
eexists; reflexivity.
Qed.
Lemma extends_strictly_on_decls_l_merge Σ Σ'
: extends_strictly_on_decls Σ (merge_global_envs Σ Σ').
Proof.
rewrite /extends_strictly_on_decls/merge_global_envs/merge_globals; cbn.
split;
try first [ apply ContextSet.union_spec
| apply Retroknowledge.extends_l_merge
| apply strictly_extends_decls_l_merge_globals ].
Qed.
#[export] Hint Extern 0 (extends_strictly_on_decls _ (merge_global_envs ?Σ ?Σ')) => simple apply (@extends_strictly_on_decls_l_merge Σ Σ') : typeclass_instances.
Lemma extends_l_merge Σ Σ'
: extends Σ (merge_global_envs Σ Σ').
Proof. exact _. Qed.
Lemma declared_kername_set_spec
: forall Σ c, KernameSet.In c (declared_kername_set Σ) <-> List.In c (map fst Σ).
Proof.
elim => //=; try setoid_rewrite KernameSetFact.empty_iff => //=.
move => [? ?] ? IH c //=.
rewrite KernameSet.add_spec.
intuition auto with *.
Qed.
Lemma declared_kername_set_mem_iff Σ c
: KernameSet.mem c (declared_kername_set Σ) <-> List.In c (map fst Σ).
Proof.
setoid_rewrite <- KernameSetFact.mem_iff.
apply declared_kername_set_spec.
Qed.
Lemma extends_r_merge_globals Σ Σ'
: compatible_globals Σ Σ' ->
extends_decls_part_globals Σ' (merge_globals Σ Σ').
Proof.
rewrite /merge_globals.
intro H2; cbn.
cbv [compatible_globals] in *.
intro c.
specialize (H2 c).
rewrite lookup_globals_app lookup_globals_filter.
destruct (in_dec eq_dec c (map fst Σ')) as [H'|H'];
[ exists nil | exists (lookup_globals Σ c) ].
2: apply lookup_globals_nil in H'; rewrite H'; clear H'.
2: now destruct ?; cbn; rewrite app_nil_r.
pose proof (lookup_globals_nil Σ c) as Hc.
rewrite <- !lookup_globals_nil in H2.
rewrite <- (declared_kername_set_mem_iff Σ) in *.
destruct KernameSet.mem; cbn in *.
{ intuition auto. }
{ destruct Hc as [Hc _].
rewrite Hc ?app_nil_r //=. }
Qed.
Lemma extends_r_merge Σ Σ'
: compatible Σ Σ' -> extends Σ' (merge_global_envs Σ Σ').
Proof.
rewrite /extends/compatible/merge_global_envs/lookup_envs.
intros [H1 H2].
split;
try first [ apply ContextSet.union_spec
| now apply Retroknowledge.extends_r_merge
| now apply extends_r_merge_globals ].
Qed.
Definition primitive_constant (Σ : global_env) (p : prim_tag) : option kername :=
match p with
| primInt => Σ.(retroknowledge).(Retroknowledge.retro_int63)
| primFloat => Σ.(retroknowledge).(Retroknowledge.retro_float64)
| primString => Σ.(retroknowledge).(Retroknowledge.retro_string)
| primArray => Σ.(retroknowledge).(Retroknowledge.retro_array)
end.
Definition tImpl (dom codom : term) : term :=
tProd {| binder_name := nAnon; binder_relevance := Relevant |}
dom (lift 1 0 codom).
Definition array_uctx := ([nAnon], ConstraintSet.empty).
Definition primitive_invariants (p : prim_tag) (cdecl : constant_body) :=
match p with
| primInt | primFloat | primString =>
[/\ cdecl.(cst_type) = tSort Sort.type0, cdecl.(cst_body) = None &
cdecl.(cst_universes) = Monomorphic_ctx]
| primArray =>
let s := sType (Universe.make' (Level.lvar 0)) in
[/\ cdecl.(cst_type) = tImpl (tSort s) (tSort s), cdecl.(cst_body) = None &
cdecl.(cst_universes) = Polymorphic_ctx array_uctx]
end.
(** A context of global declarations + global universe constraints,
i.e. a global environment *)
Definition global_env_ext : Type := global_env * universes_decl.
(** Use a coercion for this common projection of the global context. *)
Definition fst_ctx : global_env_ext -> global_env := fst.
Coercion fst_ctx : global_env_ext >-> global_env.
Definition empty_ext (Σ : global_env) : global_env_ext
:= (Σ, Monomorphic_ctx).
(** *** Programs
A set of declarations and a term, as produced by [MetaCoq Quote Recursively]. *)
Definition program : Type := global_env * term.
(** Make a lambda/let-in string of abstractions from a context [Γ], ending with term [t]. *)
Definition mkLambda_or_LetIn d t :=
match d.(decl_body) with
| None => tLambda d.(decl_name) d.(decl_type) t
| Some b => tLetIn d.(decl_name) b d.(decl_type) t
end.
Definition it_mkLambda_or_LetIn (l : context) (t : term) :=
List.fold_left (fun acc d => mkLambda_or_LetIn d acc) l t.
(** Make a prod/let-in string of abstractions from a context [Γ], ending with term [t]. *)
Definition mkProd_or_LetIn d t :=
match d.(decl_body) with
| None => tProd d.(decl_name) d.(decl_type) t
| Some b => tLetIn d.(decl_name) b d.(decl_type) t
end.
Definition it_mkProd_or_LetIn (l : context) (t : term) :=
List.fold_left (fun acc d => mkProd_or_LetIn d acc) l t.
Lemma it_mkProd_or_LetIn_app l l' t :
it_mkProd_or_LetIn (l ++ l') t = it_mkProd_or_LetIn l' (it_mkProd_or_LetIn l t).
Proof. induction l in l', t |- *; simpl; auto. Qed.
Fixpoint reln (l : list term) (p : nat) (Γ0 : list context_decl) {struct Γ0} : list term :=
match Γ0 with
| [] => l
| {| decl_body := Some _ |} :: hyps => reln l (p + 1) hyps
| {| decl_body := None |} :: hyps => reln (tRel p :: l) (p + 1) hyps
end.
Definition to_extended_list_k Γ k := reln [] k Γ.
Definition to_extended_list Γ := to_extended_list_k Γ 0.
Lemma reln_fold f ctx n acc :
reln acc n (fold_context_k f ctx) =
reln acc n ctx.
Proof.
induction ctx as [|[na [b|] ty] ctx] in n, acc |- *; simpl; auto;
rewrite fold_context_k_snoc0 /=; apply IHctx.
Qed.
Lemma reln_list_lift_above l p Γ :
Forall (fun x => exists n, x = tRel n /\ p <= n /\ n < p + length Γ) l ->
Forall (fun x => exists n, x = tRel n /\ p <= n /\ n < p + length Γ) (reln l p Γ).
Proof.
generalize (Nat.le_refl p).
generalize p at 1 3 5.
induction Γ in p, l |- *. simpl. auto.
intros. destruct a. destruct decl_body. simpl.
assert(p0 <= S p) by lia.
specialize (IHΓ l (S p) p0 H1). rewrite <- Nat.add_succ_comm, Nat.add_1_r.
simpl in *. rewrite <- Nat.add_succ_comm in H0. eauto.
simpl in *.
specialize (IHΓ (tRel p :: l) (S p) p0 ltac:(lia)).
rewrite <- Nat.add_succ_comm, Nat.add_1_r.
eapply IHΓ. simpl in *. rewrite <- Nat.add_succ_comm in H0. auto.
simpl in *.
constructor. exists p. intuition lia. auto.
Qed.
Lemma to_extended_list_k_spec Γ k :
Forall (fun x => exists n, x = tRel n /\ k <= n /\ n < k + length Γ)
(to_extended_list_k Γ k).
Proof.
pose (reln_list_lift_above [] k Γ).
unfold to_extended_list_k.
forward f. constructor. apply f.
Qed.
Lemma to_extended_list_lift_above Γ :
Forall (fun x => exists n, x = tRel n /\ n < length Γ) (to_extended_list Γ).
Proof.
pose (reln_list_lift_above [] 0 Γ).
unfold to_extended_list.
forward f. constructor. eapply Forall_impl; eauto. intros.
destruct H; eexists; intuition eauto.
Qed.
Fixpoint reln_alt p (Γ : context) :=
match Γ with
| [] => []
| {| decl_body := Some _ |} :: Γ => reln_alt (p + 1) Γ
| {| decl_body := None |} :: Γ => tRel p :: reln_alt (p + 1) Γ
end.
Lemma reln_alt_eq l Γ k : reln l k Γ = List.rev (reln_alt k Γ) ++ l.
Proof.
induction Γ in l, k |- *; simpl; auto.
destruct a as [na [body|] ty]; simpl.
now rewrite IHΓ.
now rewrite IHΓ -app_assoc.
Qed.
Lemma to_extended_list_k_cons d Γ k :
to_extended_list_k (d :: Γ) k =
match d.(decl_body) with
| None => to_extended_list_k Γ (S k) ++ [tRel k]
| Some b => to_extended_list_k Γ (S k)
end.
Proof.
unfold to_extended_list_k.
rewrite reln_alt_eq. simpl.
destruct d as [na [body|] ty]. simpl.
now rewrite reln_alt_eq Nat.add_1_r.