Skip to content

Commit 3354847

Browse files
committed
Adapt erasure for primitive arrays
1 parent 8ecbd0e commit 3354847

30 files changed

+401
-219
lines changed

.vscode/metacoq.code-workspace

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,28 @@
1111

1212

1313

14-
"-R", "safechecker-plugin/theories,MetaCoq.SafeCheckerPlugin",
1514

16-
"-R", "utils/theories,MetaCoq.Utils",
17-
"-R", "common/theories,MetaCoq.Common",
18-
"-R", "template-pcuic/theories,MetaCoq.TemplatePCUIC",
15+
"-R", "safechecker-plugin/theories","MetaCoq.SafeCheckerPlugin",
1916

20-
"-I template-coq",
17+
"-R", "utils/theories","MetaCoq.Utils",
18+
"-R", "common/theories","MetaCoq.Common",
19+
"-R", "template-pcuic/theories","MetaCoq.TemplatePCUIC",
20+
21+
"-I", "template-coq",
2122
// "-bt", get backtraces from Coq on errors
22-
"-I template-coq/build",
23-
"-R", "template-coq/theories,MetaCoq.Template",
24-
"-R", "examples,MetaCoq.Examples",
25-
"-R", "checker/theories,MetaCoq.Checker",
26-
"-R", "pcuic/theories,MetaCoq.PCUIC",
27-
"-I safechecker/src",
28-
"-R", "safechecker/theories,MetaCoq.SafeChecker",
29-
"-I erasure/src",
30-
"-R", "erasure/theories,MetaCoq.Erasure",
31-
"-R", "erasure-plugin/theories,MetaCoq.ErasurePlugin",
32-
"-R", "translations,MetaCoq.Translations",
33-
"-R", "test-suite/plugin-demo/theories,MetaCoq.ExtractedPluginDemo",
34-
"-R", "test-suite,MetaCoq.TestSuite"
23+
"-I", "template-coq/build",
24+
"-R", "template-coq/theories", "MetaCoq.Template",
25+
"-R", "examples", "MetaCoq.Examples",
26+
"-R", "checker/theories" "MetaCoq.Checker",
27+
"-R", "pcuic/theories","MetaCoq.PCUIC",
28+
"-I", "safechecker/src",
29+
"-R", "safechecker/theories","MetaCoq.SafeChecker",
30+
"-I", "erasure/src",
31+
"-R", "erasure/theories","MetaCoq.Erasure",
32+
"-R", "erasure-plugin/theories","MetaCoq.ErasurePlugin",
33+
"-R", "translations","MetaCoq.Translations",
34+
"-R", "test-suite/plugin-demo/theories","MetaCoq.ExtractedPluginDemo",
35+
"-R", "test-suite","MetaCoq.TestSuite"
3536
],
3637

3738
// When enabled, will trim trailing whitespace when saving a file.

erasure-plugin/_PluginProject.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ src/pCUICSafeReduce.ml
5050
src/pCUICSafeRetyping.ml
5151
src/pCUICSafeRetyping.mli
5252

53+
src/ePrimitive.mli
54+
src/ePrimitive.ml
5355
src/eAst.ml
5456
src/eAst.mli
5557
src/eAstUtils.ml

erasure-plugin/src/metacoq_erasure_plugin.mlpack

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PCUICUnivSubst
3939
PCUICSafeReduce
4040
PCUICSafeRetyping
4141

42+
EPrimitive
4243
EAst
4344
EAstUtils
4445
ELiftSubst

erasure/theories/EAstUtils.v

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
From Equations Require Import Equations.
33
From MetaCoq.Utils Require Import utils.
44
From MetaCoq.Common Require Import BasicAst Kernames.
5-
From MetaCoq.Erasure Require Import EAst.
5+
From MetaCoq.Erasure Require Import EPrimitive EAst.
66
Require Import ssreflect ssrbool.
77

88
Global Hint Resolve app_tip_nil : core.
@@ -378,24 +378,36 @@ Fixpoint string_of_term (t : term) : string :=
378378

379379
(** Compute all the global environment dependencies of the term *)
380380

381-
Fixpoint term_global_deps (t : EAst.term) :=
381+
Section PrimDeps.
382+
Context (deps : term -> KernameSet.t).
383+
384+
Equations prim_global_deps (p : prim_val term) : KernameSet.t :=
385+
| (primInt; primIntModel i) => KernameSet.empty
386+
| (primFloat; primFloatModel f) => KernameSet.empty
387+
| (primArray; primArrayModel a) =>
388+
List.fold_left (fun acc x => KernameSet.union (deps x) acc) a.(array_value) (deps a.(array_default)).
389+
390+
End PrimDeps.
391+
392+
Fixpoint term_global_deps (t : term) :=
382393
match t with
383-
| EAst.tConst kn => KernameSet.singleton kn
384-
| EAst.tConstruct {| inductive_mind := kn |} _ args =>
394+
| tConst kn => KernameSet.singleton kn
395+
| tConstruct {| inductive_mind := kn |} _ args =>
385396
List.fold_left (fun acc x => KernameSet.union (term_global_deps x) acc) args
386397
(KernameSet.singleton kn)
387-
| EAst.tLambda _ x => term_global_deps x
388-
| EAst.tApp x y
389-
| EAst.tLetIn _ x y => KernameSet.union (term_global_deps x) (term_global_deps y)
390-
| EAst.tCase (ind, _) x brs =>
398+
| tLambda _ x => term_global_deps x
399+
| tApp x y
400+
| tLetIn _ x y => KernameSet.union (term_global_deps x) (term_global_deps y)
401+
| tCase (ind, _) x brs =>
391402
KernameSet.union (KernameSet.singleton (inductive_mind ind))
392403
(List.fold_left (fun acc x => KernameSet.union (term_global_deps (snd x)) acc) brs
393404
(term_global_deps x))
394-
| EAst.tFix mfix _ | EAst.tCoFix mfix _ =>
395-
List.fold_left (fun acc x => KernameSet.union (term_global_deps (EAst.dbody x)) acc) mfix
405+
| tFix mfix _ | tCoFix mfix _ =>
406+
List.fold_left (fun acc x => KernameSet.union (term_global_deps (dbody x)) acc) mfix
396407
KernameSet.empty
397-
| EAst.tProj p c =>
408+
| tProj p c =>
398409
KernameSet.union (KernameSet.singleton (inductive_mind p.(proj_ind)))
399410
(term_global_deps c)
411+
| tPrim p => prim_global_deps term_global_deps p
400412
| _ => KernameSet.empty
401413
end.

erasure/theories/EConstructorsAsBlocks.v

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
From Coq Require Import Utf8 Program.
33
From MetaCoq.Utils Require Import utils.
44
From MetaCoq.Common Require Import config Kernames BasicAst EnvMap.
5-
From MetaCoq.Erasure Require Import EAst EAstUtils EInduction EArities
5+
From MetaCoq.Erasure Require Import EPrimitive EAst EAstUtils EInduction EArities
66
ELiftSubst ESpineView EGlobalEnv EWellformed EEnvMap
77
EWcbvEval EEtaExpanded ECSubst EWcbvEvalEtaInd EProgram.
88

@@ -62,7 +62,7 @@ Section transform_blocks.
6262
| tVar n => EAst.tVar n
6363
| tConst n => EAst.tConst n
6464
| tConstruct ind i block_args => EAst.tConstruct ind i []
65-
| tPrim p => EAst.tPrim p }.
65+
| tPrim p => EAst.tPrim (map_primIn p (fun x H => transform_blocks x)) }.
6666
Proof.
6767
all:try lia.
6868
all:try apply (In_size); tea.
@@ -82,6 +82,7 @@ Section transform_blocks.
8282
change (fun x => size x) with size in H.
8383
pose proof (size_mkApps_l napp nnil). lia.
8484
- eapply (In_size snd size) in H. cbn in *. lia.
85+
- now eapply InPrim_size in H.
8586
Qed.
8687

8788
End Def.
@@ -116,6 +117,7 @@ Section transform_blocks.
116117
unfold test_def in *;
117118
simpl closed in *;
118119
try solve [simpl; subst; simpl closed; f_equal; auto; rtoProp; solve_all; solve_all]; try easy.
120+
- solve_all_k 6.
119121
- rewrite !closedn_mkApps in H1 *.
120122
rtoProp; intuition auto. solve_all.
121123
- destruct (chop nargs v) eqn:E.
@@ -714,6 +716,7 @@ Proof.
714716
cbn -[transform_blocks isEtaExp] in *. rtoProp. eauto.
715717
- unfold wf_fix in *. len. solve_all. rtoProp; intuition auto.
716718
solve_all.
719+
- solve_all_k 7.
717720
- rewrite !wellformed_mkApps in Hw |- * => //. rtoProp.
718721
eapply isEtaExp_mkApps in H1. rewrite decompose_app_mkApps in H1; eauto.
719722
destruct construct_viewc; eauto. cbn in d. eauto.

erasure/theories/EDeps.v

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
From Coq Require Import Arith List.
22
From Equations Require Import Equations.
33
From MetaCoq.PCUIC Require Import
4-
PCUICAst PCUICAstUtils PCUICTyping PCUICInversion PCUICWeakeningEnv PCUICWeakeningEnvTyp.
4+
PCUICPrimitive PCUICAst PCUICAstUtils PCUICTyping PCUICInversion PCUICWeakeningEnv PCUICWeakeningEnvTyp.
55
Set Warnings "-notation-overridden".
6-
From MetaCoq.Erasure Require Import EAst EAstUtils ECSubst EInduction
6+
From MetaCoq.Erasure Require Import EPrimitive EAst EAstUtils ECSubst EInduction
77
ELiftSubst EGlobalEnv EWcbvEval Extract ESubstitution.
88
From MetaCoq.Erasure Require EExtends.
99
Set Warnings "+notation-overridden".
@@ -84,6 +84,8 @@ Proof.
8484
depelim X.
8585
constructor; [|easy].
8686
now apply e.
87+
- depelim X; depelim er; constructor; cbn. solve_all.
88+
destruct p. solve_all.
8789
Qed.
8890

8991
Lemma erases_deps_subst Σ Σ' s k t :
@@ -134,6 +136,7 @@ Proof.
134136
depelim X.
135137
constructor; [|easy].
136138
now apply e.
139+
- depelim X; depelim er; constructor; cbn; intuition auto; solve_all.
137140
Qed.
138141

139142
Lemma erases_deps_subst1 Σ Σ' t k u :
@@ -191,6 +194,7 @@ Proof.
191194
depelim X.
192195
constructor; [|easy].
193196
now apply e.
197+
- depelim X; depelim er; constructor; cbn; intuition auto; solve_all.
194198
Qed.
195199

196200
Lemma erases_deps_substl Σ Σ' s t :
@@ -402,7 +406,7 @@ Lemma erases_deps_forall_ind Σ Σ'
402406
Forall (fun d : Extract.E.def Extract.E.term => erases_deps Σ Σ' (Extract.E.dbody d)) defs ->
403407
Forall (fun d => P (E.dbody d)) defs ->
404408
P (Extract.E.tCoFix defs i))
405-
(Hprim : forall p, P (Extract.E.tPrim p)):
409+
(Hprim : forall p, primProp (erases_deps Σ Σ') p -> primProp P p -> P (Extract.E.tPrim p)):
406410
forall t, erases_deps Σ Σ' t -> P t.
407411
Proof.
408412
fix f 2.
@@ -441,6 +445,15 @@ Proof.
441445
fix f' 2.
442446
intros defs []; [now constructor|].
443447
constructor; [now apply f|now apply f'].
448+
- eapply Hprim; tea; constructor.
449+
- eapply Hprim; tea; constructor.
450+
- eapply Hprim; tea; constructor.
451+
intuition auto; solve_all.
452+
split. auto. destruct a as [d v]. cbn in *.
453+
eapply Forall_All.
454+
revert v H.
455+
fix aux 2.
456+
intros ? [];constructor; auto.
444457
Defined.
445458

446459
(* Lemma fresh_global_erase {cf : checker_flags} Σ Σ' kn :
@@ -559,6 +572,7 @@ Proof.
559572
rewrite H in declm.
560573
eapply PCUICWeakeningEnv.lookup_env_Some_fresh in kn_fresh.
561574
eauto. eauto.
575+
- depelim X0; intuition auto; constructor; auto.
562576
Qed.
563577

564578
Derive Signature for erases_global_decls.
@@ -681,6 +695,8 @@ Proof.
681695
depelim all_deps.
682696
destruct p as (?&?&?).
683697
now constructor; eauto.
698+
- eapply inversion_Prim in wt as [prim_ty [decl []]]; eauto.
699+
depelim H0; depelim p1; depelim X; cbn in *; try constructor; cbn; intuition eauto. solve_all.
684700
Qed.
685701

686702
Lemma Forall2_nth_error_left {A B} {P} {l : list A} {l' : list B} : Forall2 P l l' ->

erasure/theories/EEtaExpanded.v

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
From Coq Require Import Utf8 Program.
66
From MetaCoq.Utils Require Import utils.
77
From MetaCoq.Common Require Import config Kernames EnvMap BasicAst.
8-
From MetaCoq.Erasure Require Import EAst EAstUtils EInduction EGlobalEnv EExtends EWellformed ELiftSubst ESpineView ECSubst EWcbvEval EWcbvEvalInd EProgram.
8+
From MetaCoq.Erasure Require Import EPrimitive EAst EAstUtils EInduction EGlobalEnv EExtends EWellformed ELiftSubst ESpineView ECSubst EWcbvEval EWcbvEvalInd EProgram.
99

1010
Local Open Scope string_scope.
1111
Set Asymmetric Patterns.
@@ -75,7 +75,7 @@ Section isEtaExp.
7575
| tBox => true
7676
| tVar _ => true
7777
| tConst _ => true
78-
| tPrim _ => true
78+
| tPrim p => test_primIn p (fun x H => isEtaExp x)
7979
| tConstruct ind i block_args => isEtaExp_app ind i 0 && is_nil block_args }.
8080
Proof.
8181
all:try lia.
@@ -92,11 +92,15 @@ Section isEtaExp.
9292
change (fun x => size x) with size in H.
9393
pose proof (size_mkApps_l napp nnil). lia.
9494
- eapply (In_size snd size) in H. cbn in H; lia.
95+
- destruct p as [? []]; cbn in *; intuition eauto.
96+
subst. lia.
97+
eapply (In_size id size) in H0. unfold id in H0.
98+
change (fun x => size x) with size in H0. lia.
9599
Qed.
96100

97101
End isEtaExp.
98102

99-
Global Hint Rewrite @forallb_InP_spec : isEtaExp.
103+
Global Hint Rewrite @test_primIn_spec @forallb_InP_spec : isEtaExp.
100104
Tactic Notation "simp_eta" "in" hyp(H) := simp isEtaExp in H; rewrite -?isEtaExp_equation_1 in H.
101105
Ltac simp_eta := simp isEtaExp; rewrite -?isEtaExp_equation_1.
102106

@@ -276,8 +280,8 @@ Section WeakEtaExp.
276280
eapply a0 => //.
277281
- move/andP: H0 => [] etaexp h.
278282
rewrite csubst_mkApps /=.
279-
rewrite isEtaExp_Constructor. solve_all.
280-
rewrite map_length. rtoProp; solve_all. solve_all.
283+
rewrite isEtaExp_Constructor; solve_all.
284+
rtoProp; solve_all. solve_all.
281285
now destruct block_args.
282286
- rewrite csubst_mkApps /=.
283287
move/andP: H1 => [] eu ev.
@@ -417,6 +421,7 @@ Proof.
417421
- eapply In_All in H; solve_all.
418422
move/andP: b => [] -> /=. eauto.
419423
- eapply In_All in H; solve_all.
424+
- solve_all.
420425
- eapply In_All in H; solve_all.
421426
rewrite isEtaExp_Constructor //. rtoProp; intuition auto.
422427
eapply isEtaExp_app_extends; tea.
@@ -471,7 +476,7 @@ Inductive expanded : term -> Prop :=
471476
#|args| >= cstr_arity mind cdecl ->
472477
Forall expanded args ->
473478
expanded (mkApps (tConstruct ind idx []) args)
474-
| expanded_tPrim p : expanded (tPrim p)
479+
| expanded_tPrim p : primProp expanded p -> expanded (tPrim p)
475480
| expanded_tBox : expanded tBox.
476481

477482
End expanded.
@@ -510,7 +515,7 @@ forall (Σ : global_declarations) (P : term -> Prop),
510515
(args : list term),
511516
declared_constructor Σ (ind, idx) mind idecl cdecl ->
512517
#|args| >= cstr_arity mind cdecl -> Forall (expanded Σ) args -> Forall P args -> P (mkApps (tConstruct ind idx []) args)) ->
513-
(forall p, P (tPrim p)) ->
518+
(forall p, primProp (expanded Σ) p -> primProp P p -> P (tPrim p)) ->
514519
(P tBox) ->
515520
forall t : term, expanded Σ t -> P t.
516521
Proof.
@@ -523,6 +528,9 @@ Proof.
523528
- eapply H8; eauto. induction H13; econstructor; cbn in *; intuition eauto.
524529
- eapply H9; eauto. induction H13; econstructor; cbn in *; eauto.
525530
- eapply H10; eauto. clear - H15 f. induction H15; econstructor; cbn in *; eauto.
531+
- eapply H11; eauto.
532+
depelim X; constructor. destruct p; split; eauto.
533+
eapply (make_All_All f a0).
526534
Qed.
527535

528536
Local Hint Constructors expanded : core.
@@ -595,6 +603,7 @@ Proof.
595603
intuition auto.
596604
- econstructor. rewrite forallb_InP_spec in H0. eapply forallb_Forall in H0.
597605
eapply In_All in H. solve_all.
606+
- econstructor. rewrite test_primIn_spec in H0. solve_all.
598607
- rtoProp. eapply In_All in H.
599608
rewrite forallb_InP_spec in H2. eapply forallb_Forall in H2.
600609
eapply isEtaExp_app_expanded in H0 as (? & ? & ? & ? & ?).
@@ -621,6 +630,7 @@ Proof.
621630
- rewrite isEtaExp_Constructor. rtoProp; repeat split.
622631
2: eapply forallb_Forall.
623632
2: solve_all. eapply expanded_isEtaExp_app_; eauto.
633+
- solve_all.
624634
Qed.
625635

626636
Lemma expanded_global_env_isEtaExp_env {Σ} : expanded_global_env Σ -> isEtaExp_env Σ.
@@ -695,6 +705,7 @@ Proof.
695705
- simp_eta. rtoProp; intuition auto.
696706
eapply In_All in H0; solve_all.
697707
- eapply In_All in H. simp_eta; rtoProp; intuition auto. solve_all.
708+
- simp_eta. solve_all_k 7. primProp. solve_all.
698709
- eapply In_All in H. simp_eta; rtoProp; intuition auto.
699710
rewrite EEtaExpanded.isEtaExp_Constructor. rtoProp; repeat split. eauto.
700711
solve_all. destruct block_args; cbn in *; eauto.

0 commit comments

Comments
 (0)