Skip to content

Commit 9ee34f6

Browse files
committed
Unsafe inlining and beta-reduction transfomations
1 parent 3be71de commit 9ee34f6

File tree

12 files changed

+357
-32
lines changed

12 files changed

+357
-32
lines changed

erasure-plugin/_PluginProject.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ src/eOptimizePropDiscr.mli
112112
src/eOptimizePropDiscr.ml
113113
src/optimizePropDiscr.mli
114114
src/optimizePropDiscr.ml
115+
src/eInlining.mli
116+
src/eInlining.ml
117+
src/eBeta.mli
118+
src/eBeta.ml
115119
src/eProgram.mli
116120
src/eProgram.ml
117121
src/eInlineProjections.mli

erasure-plugin/src/g_metacoq_erasure.mlg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ let default_config =
5959

6060
let make_erasure_config config =
6161
let open Erasure0 in
62-
{ enable_cofix_to_fix = config.unsafe;
62+
{ enable_unsafe = config.unsafe;
6363
enable_typed_erasure = config.typed;
6464
enable_fast_remove_params = config.fast;
6565
dearging_config = default_dearging_config;
66-
inductives_mapping = [] }
66+
inductives_mapping = [];
67+
inlining = Kernames.KernameSet.empty }
6768

6869
let time_opt config str fn arg =
6970
if config.time then

erasure-plugin/src/metacoq_erasure_plugin.mlpack

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ ESpineView
5858
EPretty
5959
Extract
6060
EEtaExpanded
61+
EInlining
62+
EBeta
6163
ERemoveParams
6264
ErasureFunction
6365
ErasureFunctionProperties

erasure-plugin/theories/ETransform.v

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Definition build_wf_env_from_env {cf : checker_flags} (Σ : global_env_map) (wf
2323
wf_env_map_repr := Σ.(trans_env_repr);
2424
|}.
2525

26-
2726
Notation NormalizationIn_erase_pcuic_program_1 p
2827
:= (@PCUICTyping.wf_ext config.extraction_checker_flags p -> PCUICSN.NormalizationIn (cf:=config.extraction_checker_flags) (no:=PCUICSN.extraction_normalizing) p)
2928
(only parsing).
@@ -601,11 +600,6 @@ Section Dearging.
601600

602601
End Dearging.
603602

604-
Definition extends_eprogram (p p' : eprogram) :=
605-
extends p.1 p'.1 /\ p.2 = p'.2.
606-
607-
Definition extends_eprogram_env (p p' : eprogram_env) :=
608-
extends p.1 p'.1 /\ p.2 = p'.2.
609603

610604
Section PCUICEnv. (* Locally reuse the short names for PCUIC environment handling *)
611605
Import PCUICAst.PCUICEnvironment.

erasure-plugin/theories/Erasure.v

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ From MetaCoq.Template Require Import EtaExpand TemplateProgram.
66
From MetaCoq.PCUIC Require PCUICAst PCUICAstUtils PCUICProgram.
77
From MetaCoq.SafeChecker Require Import PCUICErrors PCUICWfEnvImpl.
88
From MetaCoq.Erasure Require EAstUtils ErasureFunction ErasureCorrectness EPretty Extract.
9-
From MetaCoq.Erasure Require Import EProgram.
9+
From MetaCoq.Erasure Require Import EProgram EInlining EBeta.
1010
From MetaCoq.ErasurePlugin Require Import ETransform.
1111

1212
Import PCUICProgram.
@@ -31,11 +31,12 @@ Import EWcbvEval.
3131
Local Obligation Tactic := program_simpl.
3232

3333
Record erasure_configuration := {
34-
enable_cofix_to_fix : bool;
34+
enable_unsafe : bool;
3535
enable_typed_erasure : bool;
3636
enable_fast_remove_params : bool;
3737
dearging_config : dearging_config;
38-
inductives_mapping : EReorderCstrs.inductives_mapping
38+
inductives_mapping : EReorderCstrs.inductives_mapping;
39+
inlining : KernameSet.t
3940
}.
4041

4142
Definition default_dearging_config :=
@@ -45,19 +46,21 @@ Definition default_dearging_config :=
4546

4647
(* This runs the cofix -> fix translation which is not entirely verified yet *)
4748
Definition default_erasure_config :=
48-
{| enable_cofix_to_fix := true;
49+
{| enable_unsafe := true;
4950
dearging_config := default_dearging_config;
5051
enable_typed_erasure := true;
5152
enable_fast_remove_params := true;
52-
inductives_mapping := [] |}.
53+
inductives_mapping := [];
54+
inlining := KernameSet.empty |}.
5355

5456
(* This runs only the verified phases without the typed erasure and "fast" remove params *)
5557
Definition safe_erasure_config :=
56-
{| enable_cofix_to_fix := false;
58+
{| enable_unsafe := false;
5759
enable_typed_erasure := false;
5860
enable_fast_remove_params := false;
5961
dearging_config := default_dearging_config;
60-
inductives_mapping := [] |}.
62+
inductives_mapping := [];
63+
inlining := KernameSet.empty |}.
6164

6265
Axiom assume_welltyped_template_program_expansion :
6366
forall p (wtp : ∥ wt_template_program_env p ∥),
@@ -95,15 +98,18 @@ Definition final_wcbv_flags := {|
9598
Program Definition optional_unsafe_transforms econf :=
9699
let efl := EConstructorsAsBlocks.switch_cstr_as_blocks
97100
(EInlineProjections.disable_projections_env_flag (ERemoveParams.switch_no_params EWellformed.all_env_flags)) in
98-
ETransform.optional_self_transform econf.(enable_cofix_to_fix)
101+
ETransform.optional_self_transform econf.(enable_unsafe)
99102
((* Rebuild the efficient lookup table *)
100103
rebuild_wf_env_transform (efl := efl) false false ▷
101104
(* Coinductives & cofixpoints are translated to inductive types and thunked fixpoints *)
102105
coinductive_to_inductive_transformation efl
103106
(has_app := eq_refl) (has_box := eq_refl) (has_rel := eq_refl) (has_pars := eq_refl) (has_cstrblocks := eq_refl) ▷
104107
reorder_cstrs_transformation efl final_wcbv_flags econf.(inductives_mapping) ▷
105108
rebuild_wf_env_transform (efl := efl) false false ▷
106-
unbox_transformation efl final_wcbv_flags).
109+
unbox_transformation efl final_wcbv_flags ▷
110+
inline_transformation efl final_wcbv_flags econf.(inlining) ▷
111+
forget_inlining_info_transformation efl final_wcbv_flags ▷
112+
betared_transformation efl final_wcbv_flags).
107113

108114
Program Definition verified_lambdabox_pipeline {guard : abstract_guard_impl}
109115
(efl := EWellformed.all_env_flags)
@@ -258,8 +264,7 @@ Qed.
258264

259265
Next Obligation.
260266
unfold optional_unsafe_transforms. cbn.
261-
unfold optional_self_transform. cbn.
262-
destruct enable_cofix_to_fix => //.
267+
destruct enable_unsafe => //.
263268
Qed.
264269

265270
Local Obligation Tactic := intros; eauto.
@@ -358,7 +363,7 @@ Next Obligation.
358363
cbn in H. split; cbn; intuition eauto.
359364
Qed.
360365
Next Obligation.
361-
cbn in H. unfold optional_unsafe_transforms. destruct enable_cofix_to_fix => //.
366+
cbn in H. unfold optional_unsafe_transforms. destruct enable_unsafe => //.
362367
Qed.
363368
Next Obligation.
364369
cbn in H. split; cbn; intuition eauto.
@@ -403,7 +408,7 @@ Program Definition run_erase_program {guard : abstract_guard_impl} econf :=
403408
Next Obligation.
404409
Proof.
405410
unfold optional_unsafe_transforms.
406-
destruct enable_cofix_to_fix => //.
411+
destruct enable_unsafe => //.
407412
Qed.
408413

409414
Program Definition erase_and_print_template_program econf (p : Ast.Env.program) : string :=
@@ -418,21 +423,23 @@ Next Obligation.
418423
Qed.
419424

420425
Definition erasure_fast_config :=
421-
{| enable_cofix_to_fix := false;
426+
{| enable_unsafe := false;
422427
dearging_config := default_dearging_config;
423428
enable_typed_erasure := false;
424429
enable_fast_remove_params := true;
425-
inductives_mapping := [] |}.
430+
inductives_mapping := [];
431+
inlining := KernameSet.empty |}.
426432

427433
Program Definition erase_fast_and_print_template_program (p : Ast.Env.program) : string :=
428434
erase_and_print_template_program erasure_fast_config p.
429435

430436
Definition typed_erasure_config :=
431-
{| enable_cofix_to_fix := false;
437+
{| enable_unsafe := false;
432438
dearging_config := default_dearging_config;
433439
enable_typed_erasure := true;
434440
enable_fast_remove_params := true;
435-
inductives_mapping := [] |}.
441+
inductives_mapping := [];
442+
inlining := KernameSet.empty |}.
436443

437444
(* Parameterized by a configuration for dearging, allowing to, e.g., override masks. *)
438445
Program Definition typed_erase_and_print_template_program (p : Ast.Env.program)

erasure-plugin/theories/ErasureCorrectness.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ From MetaCoq.Utils Require Import bytestring utils.
55
From MetaCoq.PCUIC Require PCUICAst PCUICAstUtils PCUICProgram.
66
From MetaCoq.PCUIC Require Import PCUICNormal.
77
From MetaCoq.SafeChecker Require Import PCUICErrors PCUICWfEnvImpl.
8-
From MetaCoq.Erasure Require EAstUtils ErasureCorrectness EPretty Extract EConstructorsAsBlocks.
8+
From MetaCoq.Erasure Require EAstUtils ErasureCorrectness EPretty Extract EProgram EConstructorsAsBlocks.
99
From MetaCoq.Erasure Require Import EWcbvEvalNamed ErasureFunction ErasureFunctionProperties.
1010
From MetaCoq.ErasurePlugin Require Import ETransform Erasure.
11-
Import PCUICProgram.
11+
Import EProgram PCUICProgram.
1212
Import PCUICTransform (template_to_pcuic_transform, pcuic_expand_lets_transform).
1313

1414
(* This is the total erasure function +
@@ -1742,7 +1742,7 @@ Section PCUICErase.
17421742
match goal with
17431743
[ |- context [ @erase ?X_type ?X ?nin ?G (tApp _ _) ?wt ] ] =>
17441744
unshelve epose proof (@erase_mkApps X_type X nin G t [u] wt (wt'_erase_pcuic_program (Σ, t) prf3 prf0))
1745-
end.
1745+
end.
17461746
assert (hargs : forall Σ : global_env_ext, Σ ∼_ext env -> ∥ All (welltyped Σ []) [u] ∥).
17471747
{ cbn; intros ? ->. do 2 constructor; auto. destruct prf4 as [[T HT]]. eexists; eapply HT. }
17481748
specialize (H hargs).
@@ -2120,7 +2120,7 @@ Section pipeline_cond.
21202120

21212121
Opaque compose.
21222122

2123-
Lemma verified_erasure_pipeline_lookup_env_in kn decl (efl := EInlineProjections.switch_no_params all_env_flags)
2123+
Lemma verified_erasure_pipeline_lookup_env_in kn decl (efl := EInlineProjections.switch_no_params all_env_flags)
21242124
{has_rel : has_tRel} {has_box : has_tBox} :
21252125
EGlobalEnv.lookup_env Σ_t kn = Some decl ->
21262126
exists decl',
@@ -2216,7 +2216,7 @@ Section pipeline_theorem.
22162216
Let Σ_v := (transform verified_erasure_pipeline (Σ, v) (precond2 _ _ _ _ expΣ expt typing _ _ Heval)).1.
22172217
Let v_t := compile_value_box (PCUICExpandLets.trans_global_env Σ) v [].
22182218

2219-
Lemma verified_erasure_pipeline_extends (efl := EInlineProjections.switch_no_params all_env_flags)
2219+
Lemma verified_erasure_pipeline_extends (efl := EInlineProjections.switch_no_params all_env_flags)
22202220
{has_rel : has_tRel} {has_box : has_tBox} :
22212221
EGlobalEnv.extends Σ_v Σ_t.
22222222
Proof.

erasure/_CoqProject.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ theories/ErasureFunction.v
3030
theories/ErasureFunctionProperties.v
3131
theories/EExtends.v
3232
theories/EOptimizePropDiscr.v
33+
theories/EInlining.v
34+
theories/EBeta.v
3335
theories/EEtaExpandedFix.v
3436
theories/EEtaExpanded.v
3537
theories/EProgram.v

erasure/theories/EBeta.v

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
From Coq Require Import List.
2+
From Coq Require Import String.
3+
From MetaCoq.Utils Require Import utils.
4+
From MetaCoq.Common Require Import BasicAst.
5+
From MetaCoq.Erasure Require Import EPrimitive EAst EAstUtils ELiftSubst EProgram.
6+
7+
Definition map_subterms (f : term -> term) (t : term) : term :=
8+
match t with
9+
| tEvar n ts => tEvar n (map f ts)
10+
| tLambda na body => tLambda na (f body)
11+
| tLetIn na val body => tLetIn na (f val) (f body)
12+
| tApp hd arg => tApp (f hd) (f arg)
13+
| tCase p disc brs =>
14+
tCase p (f disc) (map (on_snd f) brs)
15+
| tProj p t => tProj p (f t)
16+
| tFix def i => tFix (map (map_def f) def) i
17+
| tCoFix def i => tCoFix (map (map_def f) def) i
18+
| tPrim p => tPrim (map_prim f p)
19+
| tLazy t => tLazy (f t)
20+
| tForce t => tForce (f t)
21+
| tConstruct ind n args => tConstruct ind n (map f args)
22+
| t => t
23+
end.
24+
25+
Section betared.
26+
Fixpoint decompose_lam (t : term) {struct t} : list name × term :=
27+
match t with
28+
| tLambda na B =>
29+
let (ns, B0) := decompose_lam B in
30+
(na :: ns, B0)
31+
| _ => ([], t)
32+
end.
33+
34+
Fixpoint beta_body (body : term) (args : list term) {struct args} : term :=
35+
match args with
36+
| [] => body
37+
| a :: args =>
38+
match body with
39+
| tLambda na body => beta_body (body{0 := a}) args
40+
| _ => mkApps body (a :: args)
41+
end
42+
end.
43+
44+
Fixpoint betared_aux (args : list term) (t : term) : term :=
45+
match t with
46+
| tApp hd arg => betared_aux (betared_aux [] arg :: args) hd
47+
| tLambda na body =>
48+
let b := betared_aux [] body in
49+
beta_body (tLambda na b) args
50+
| t => mkApps (map_subterms (betared_aux []) t) args
51+
end.
52+
53+
Definition betared : term -> term := betared_aux [].
54+
55+
Definition betared_in_constant_body cst :=
56+
{| cst_body := option_map betared (cst_body cst); |}.
57+
58+
Definition betared_in_decl d :=
59+
match d with
60+
| ConstantDecl cst => ConstantDecl (betared_in_constant_body cst)
61+
| _ => d
62+
end.
63+
64+
End betared.
65+
66+
Definition betared_env (Σ : global_declarations) : global_declarations :=
67+
map (fun '(kn, decl) => (kn, betared_in_decl decl)) Σ.
68+
69+
Definition betared_program (p : program) : program :=
70+
(betared_env p.1, betared p.2).
71+
72+
From MetaCoq.Erasure Require Import EProgram EWellformed EWcbvEval.
73+
From MetaCoq.Common Require Import Transform.
74+
75+
Axiom trust_betared_wf :
76+
forall efl : EEnvFlags,
77+
WcbvFlags ->
78+
forall (input : Transform.program _ term),
79+
wf_eprogram efl input -> wf_eprogram efl (betared_program input).
80+
Axiom trust_betared_pres :
81+
forall (efl : EEnvFlags) (wfl : WcbvFlags) (p : Transform.program _ term)
82+
(v : term),
83+
wf_eprogram efl p ->
84+
eval_eprogram wfl p v ->
85+
exists v' : term,
86+
eval_eprogram wfl (betared_program p) v' /\ v' = betared v.
87+
88+
Import Transform.
89+
90+
Program Definition betared_transformation (efl : EEnvFlags) (wfl : WcbvFlags) :
91+
Transform.t _ _ EAst.term EAst.term _ _
92+
(eval_eprogram wfl) (eval_eprogram wfl) :=
93+
{| name := "betared ";
94+
transform p _ := betared_program p ;
95+
pre p := wf_eprogram efl p ;
96+
post p := wf_eprogram efl p ;
97+
obseq p hp p' v v' := v' = betared v |}.
98+
99+
Next Obligation.
100+
now apply trust_betared_wf.
101+
Qed.
102+
Next Obligation.
103+
now eapply trust_betared_pres.
104+
Qed.
105+
106+
Import EProgram EGlobalEnv.
107+
108+
#[global]
109+
Axiom betared_transformation_ext :
110+
forall (efl : EEnvFlags) (wfl : WcbvFlags),
111+
TransformExt.t (betared_transformation efl wfl)
112+
(fun p p' => extends p.1 p'.1) (fun p p' => extends p.1 p'.1).
113+
114+
#[global]
115+
Axiom betared_transformation_ext' :
116+
forall (efl : EEnvFlags) (wfl : WcbvFlags),
117+
TransformExt.t (betared_transformation efl wfl)
118+
extends_eprogram extends_eprogram.
119+
120+

erasure/theories/EEnvMap.v

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ Module GlobalContextMap.
6969
rewrite (EnvMap.lookup_spec Σ.(global_decls)) //; apply Σ.
7070
Qed.
7171

72+
Definition lookup_constant Σ kn : option constant_body :=
73+
decl <- lookup_env Σ kn;;
74+
match decl with
75+
| ConstantDecl cb => ret cb
76+
| InductiveDecl _ => None
77+
end.
78+
79+
Lemma lookup_constant_spec Σ kn : lookup_constant Σ kn = EGlobalEnv.lookup_constant Σ kn.
80+
Proof.
81+
rewrite /lookup_constant /EGlobalEnv.lookup_constant.
82+
rewrite lookup_env_spec //.
83+
Qed.
84+
7285
Definition lookup_minductive Σ kn : option mutual_inductive_body :=
7386
decl <- lookup_env Σ kn;;
7487
match decl with

0 commit comments

Comments
 (0)