Skip to content

Commit 260ce0d

Browse files
authored
Merge pull request #1061 from MetaCoq/unsafe-inline-beta
Unsafe inline beta and unboxing transforms
2 parents f9c62c5 + 9ee34f6 commit 260ce0d

File tree

13 files changed

+2652
-36
lines changed

13 files changed

+2652
-36
lines changed

erasure-plugin/_PluginProject.in

Lines changed: 6 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
@@ -122,6 +126,8 @@ src/eCoInductiveToInductive.mli
122126
src/eCoInductiveToInductive.ml
123127
src/eReorderCstrs.mli
124128
src/eReorderCstrs.ml
129+
src/eUnboxing.mli
130+
src/eUnboxing.ml
125131
src/eTransform.mli
126132
src/eTransform.ml
127133
src/erasure0.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: 3 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
@@ -66,6 +68,7 @@ EInlineProjections
6668
EConstructorsAsBlocks
6769
ECoInductiveToInductive
6870
EReorderCstrs
71+
EUnboxing
6972
EProgram
7073
OptimizePropDiscr
7174

erasure-plugin/theories/ETransform.v

Lines changed: 41 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.
@@ -1063,6 +1057,47 @@ Axiom trust_reorder_cstrs_transformation_ext' : forall (efl : EEnvFlags) (wfl :
10631057
TransformExt.t (reorder_cstrs_transformation efl wfl m)
10641058
extends_eprogram extends_eprogram.
10651059

1060+
From MetaCoq.Erasure Require Import EUnboxing.
1061+
1062+
Axiom trust_unboxing_wf :
1063+
forall efl : EEnvFlags,
1064+
WcbvFlags ->
1065+
forall (input : Transform.program _ term),
1066+
wf_eprogram_env efl input -> wf_eprogram efl (unbox_program input).
1067+
Axiom trust_unboxing_pres :
1068+
forall (efl : EEnvFlags) (wfl : WcbvFlags) (p : Transform.program _ term)
1069+
(v : term),
1070+
wf_eprogram_env efl p ->
1071+
eval_eprogram_env wfl p v -> exists v' : term, eval_eprogram wfl (unbox_program p) v' /\ v' = unbox p.1 v.
1072+
1073+
Program Definition unbox_transformation (efl : EEnvFlags) (wfl : WcbvFlags) :
1074+
Transform.t _ _ EAst.term EAst.term _ _
1075+
(eval_eprogram_env wfl) (eval_eprogram wfl) :=
1076+
{| name := "unbox singleton constructors ";
1077+
transform p _ := EUnboxing.unbox_program p ;
1078+
pre p := wf_eprogram_env efl p ;
1079+
post p := wf_eprogram efl p ;
1080+
obseq p hp p' v v' := v' = unbox p.1 v |}.
1081+
1082+
Next Obligation.
1083+
move=> efl wfl m. cbn. now apply trust_unboxing_wf.
1084+
Qed.
1085+
Next Obligation.
1086+
red. eapply trust_unboxing_pres.
1087+
Qed.
1088+
1089+
#[global]
1090+
Axiom trust_unbox_transformation_ext :
1091+
forall (efl : EEnvFlags) (wfl : WcbvFlags),
1092+
TransformExt.t (unbox_transformation efl wfl)
1093+
(fun p p' => extends p.1 p'.1) (fun p p' => extends p.1 p'.1).
1094+
1095+
#[global]
1096+
Axiom trust_unbox_transformation_ext' :
1097+
forall (efl : EEnvFlags) (wfl : WcbvFlags),
1098+
TransformExt.t (unbox_transformation efl wfl)
1099+
extends_eprogram_env extends_eprogram.
1100+
10661101
Program Definition optional_transform {env env' term term' value value' eval eval'} (activate : bool)
10671102
(tr : Transform.t env env' term term' value value' eval eval') :
10681103
if activate then Transform.t env env' term term' value value' eval eval'

erasure-plugin/theories/Erasure.v

Lines changed: 29 additions & 21 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 ∥),
@@ -93,16 +96,20 @@ Definition final_wcbv_flags := {|
9396
with_constructor_as_block := true |}.
9497

9598
Program Definition optional_unsafe_transforms econf :=
96-
ETransform.optional_self_transform econf.(enable_cofix_to_fix)
99+
let efl := EConstructorsAsBlocks.switch_cstr_as_blocks
100+
(EInlineProjections.disable_projections_env_flag (ERemoveParams.switch_no_params EWellformed.all_env_flags)) in
101+
ETransform.optional_self_transform econf.(enable_unsafe)
97102
((* Rebuild the efficient lookup table *)
98-
rebuild_wf_env_transform (efl := EConstructorsAsBlocks.switch_cstr_as_blocks
99-
(EInlineProjections.disable_projections_env_flag (ERemoveParams.switch_no_params EWellformed.all_env_flags))) false false ▷
103+
rebuild_wf_env_transform (efl := efl) false false ▷
100104
(* Coinductives & cofixpoints are translated to inductive types and thunked fixpoints *)
101-
let efl := EConstructorsAsBlocks.switch_cstr_as_blocks
102-
(EInlineProjections.disable_projections_env_flag (ERemoveParams.switch_no_params EWellformed.all_env_flags)) in
103105
coinductive_to_inductive_transformation efl
104106
(has_app := eq_refl) (has_box := eq_refl) (has_rel := eq_refl) (has_pars := eq_refl) (has_cstrblocks := eq_refl) ▷
105-
reorder_cstrs_transformation efl final_wcbv_flags econf.(inductives_mapping)).
107+
reorder_cstrs_transformation efl final_wcbv_flags econf.(inductives_mapping) ▷
108+
rebuild_wf_env_transform (efl := efl) false false ▷
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).
106113

107114
Program Definition verified_lambdabox_pipeline {guard : abstract_guard_impl}
108115
(efl := EWellformed.all_env_flags)
@@ -257,8 +264,7 @@ Qed.
257264

258265
Next Obligation.
259266
unfold optional_unsafe_transforms. cbn.
260-
unfold optional_self_transform. cbn.
261-
destruct enable_cofix_to_fix => //.
267+
destruct enable_unsafe => //.
262268
Qed.
263269

264270
Local Obligation Tactic := intros; eauto.
@@ -357,7 +363,7 @@ Next Obligation.
357363
cbn in H. split; cbn; intuition eauto.
358364
Qed.
359365
Next Obligation.
360-
cbn in H. unfold optional_unsafe_transforms. destruct enable_cofix_to_fix => //.
366+
cbn in H. unfold optional_unsafe_transforms. destruct enable_unsafe => //.
361367
Qed.
362368
Next Obligation.
363369
cbn in H. split; cbn; intuition eauto.
@@ -402,7 +408,7 @@ Program Definition run_erase_program {guard : abstract_guard_impl} econf :=
402408
Next Obligation.
403409
Proof.
404410
unfold optional_unsafe_transforms.
405-
destruct enable_cofix_to_fix => //.
411+
destruct enable_unsafe => //.
406412
Qed.
407413

408414
Program Definition erase_and_print_template_program econf (p : Ast.Env.program) : string :=
@@ -417,21 +423,23 @@ Next Obligation.
417423
Qed.
418424

419425
Definition erasure_fast_config :=
420-
{| enable_cofix_to_fix := false;
426+
{| enable_unsafe := false;
421427
dearging_config := default_dearging_config;
422428
enable_typed_erasure := false;
423429
enable_fast_remove_params := true;
424-
inductives_mapping := [] |}.
430+
inductives_mapping := [];
431+
inlining := KernameSet.empty |}.
425432

426433
Program Definition erase_fast_and_print_template_program (p : Ast.Env.program) : string :=
427434
erase_and_print_template_program erasure_fast_config p.
428435

429436
Definition typed_erasure_config :=
430-
{| enable_cofix_to_fix := false;
437+
{| enable_unsafe := false;
431438
dearging_config := default_dearging_config;
432439
enable_typed_erasure := true;
433440
enable_fast_remove_params := true;
434-
inductives_mapping := [] |}.
441+
inductives_mapping := [];
442+
inlining := KernameSet.empty |}.
435443

436444
(* Parameterized by a configuration for dearging, allowing to, e.g., override masks. *)
437445
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: 3 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
@@ -39,6 +41,7 @@ theories/EConstructorsAsBlocks.v
3941
theories/EWcbvEvalCstrsAsBlocksInd.v
4042
theories/EWcbvEvalCstrsAsBlocksFixLambdaInd.v
4143
theories/ECoInductiveToInductive.v
44+
theories/EUnboxing.v
4245
theories/EReorderCstrs.v
4346
theories/EImplementBox.v
4447

0 commit comments

Comments
 (0)