Skip to content

Commit 462f3ab

Browse files
authored
Proof that reordering of constructors is correct (#1095)
* WIP on reordering constructors * New/old tag reasoning * WIP correctness of constructor reordering. main lemma proven * Reordering preserves well-formedness * Reorderin * Admit free proofs of preservation for wf and substitution * WIP adapting to an additional mapping argument for transforms and stronger wellformedness property on extracted terms * Full composed pipeline with mapping of constructors * Fixes due to removal of useless -fast flag and change for reordering of constructors, now verified * Remove option to reorder constructors, now safely done always * Fix metacoq_tour * Fix a remaining todo * Remove generated files
1 parent 13dc7ce commit 462f3ab

31 files changed

+2456
-552
lines changed

.vscode/metacoq.code-workspace

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@
106106
"**/.DS_Store": true,
107107
"**/Thumbs.db": true
108108
},
109+
"coq-lsp.check_only_on_request": true,
109110
}
110111
}

erasure-plugin/clean_extraction.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fi
99

1010
shopt -s nullglob # make the for loop do nothnig when there is no *.ml* files
1111

12-
files=`cat ../template-coq/_PluginProject | grep "^[^#].*mli\?$" | sed -e s/gen-src/src/`
12+
files=`cat ../template-coq/_PluginProject.in | grep "^[^#].*mli\?$" | sed -e s/gen-src/src/`
1313

1414
if [[ ! -f "src/metacoq_erasure_plugin.cmxs" ||
1515
"src/metacoq_erasure_plugin.cmxs" -ot "theories/Extraction.vo" ]]

erasure-plugin/src/g_metacoq_erasure.mlg

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ type erasure_command_args =
2525
| Time
2626
| Typed
2727
| BypassQeds
28-
| Fast
2928

3029
let pr_char c = str (Char.escaped c)
3130

@@ -46,24 +45,19 @@ type erasure_config =
4645
{ unsafe : bool;
4746
time : bool;
4847
typed : bool;
49-
bypass_qeds : bool;
50-
fast : bool;
51-
}
48+
bypass_qeds : bool }
5249

5350
let default_config =
5451
{ unsafe = false;
5552
time = false;
5653
typed = false;
57-
bypass_qeds = false;
58-
fast = false }
54+
bypass_qeds = false }
5955

6056
let make_erasure_config config =
6157
let open Erasure0 in
6258
{ enable_unsafe = if config.unsafe then all_unsafe_passes else no_unsafe_passes ;
6359
enable_typed_erasure = config.typed;
64-
enable_fast_remove_params = config.fast;
6560
dearging_config = default_dearging_config;
66-
inductives_mapping = [];
6761
inlined_constants = Kernames.KernameSet.empty }
6862

6963
let time_opt config str fn arg =
@@ -76,8 +70,9 @@ let check config env evm c =
7670
let time str f arg = time_opt config str f arg in
7771
let term = time (str"Quoting") (Ast_quoter.quote_term_rec ~bypass:config.bypass_qeds env evm) (EConstr.to_constr evm c) in
7872
let erasure_config = make_erasure_config config in
73+
let mapping = [] in
7974
let erase =
80-
time (str"Erasure") (Erasure0.erase_and_print_template_program erasure_config) term
75+
time (str"Erasure") (Erasure0.erase_and_print_template_program erasure_config mapping) term
8176
in
8277
Feedback.msg_notice (pr_char_list erase)
8378

@@ -92,7 +87,6 @@ let interp_erase args env evm c =
9287
| Time -> aux {config with time = true} args
9388
| Typed -> aux {config with typed = true} args
9489
| BypassQeds -> aux {config with bypass_qeds = true} args
95-
| Fast -> aux {config with fast = true} args
9690
in aux default_config args
9791
in
9892
check config env evm c
@@ -123,7 +117,6 @@ ARGUMENT EXTEND erase_args
123117
| [ "-time" ] -> { Time }
124118
| [ "-typed" ] -> { Typed }
125119
| [ "-bypass-qeds" ] -> { BypassQeds }
126-
| [ "-fast" ] -> { Fast }
127120
END
128121

129122
VERNAC COMMAND EXTEND MetaCoqErase CLASSIFIED AS QUERY

erasure-plugin/theories/ETransform.v

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,42 +1020,59 @@ Qed.
10201020

10211021
From MetaCoq.Erasure Require Import EReorderCstrs.
10221022

1023-
Axiom trust_reorder_cstrs_wf :
1024-
forall efl : EEnvFlags,
1025-
WcbvFlags ->
1026-
forall (m : inductives_mapping) (input : Transform.program E.global_context term),
1027-
wf_eprogram efl input -> wf_eprogram efl (reorder_program m input).
1028-
Axiom trust_reorder_cstrs_pres :
1029-
forall (efl : EEnvFlags) (wfl : WcbvFlags) (m : inductives_mapping) (p : Transform.program E.global_context term)
1030-
(v : term),
1031-
wf_eprogram efl p ->
1032-
eval_eprogram wfl p v -> exists v' : term, eval_eprogram wfl (reorder_program m p) v' /\ v' = reorder m v.
1023+
Definition eval_eprogram_mapping (wfl : WcbvFlags) (p : inductives_mapping * eprogram) t :=
1024+
eval_eprogram wfl p.2 t.
10331025

1034-
Program Definition reorder_cstrs_transformation (efl : EEnvFlags) (wfl : WcbvFlags) (m : inductives_mapping) :
1035-
Transform.t _ _ EAst.term EAst.term _ _
1036-
(eval_eprogram wfl) (eval_eprogram wfl) :=
1037-
{| name := "reoder inductive constructors ";
1038-
transform p _ := EReorderCstrs.reorder_program m p ;
1039-
pre p := wf_eprogram efl p ;
1040-
post p := wf_eprogram efl p ;
1041-
obseq p hp p' v v' := v' = EReorderCstrs.reorder m v |}.
1026+
Definition eval_eprogram_env_mapping (wfl : WcbvFlags) (p : inductives_mapping * eprogram_env) t :=
1027+
eval_eprogram_env wfl p.2 t.
1028+
1029+
Definition to_program (e : eprogram_env) : eprogram :=
1030+
(e.1, e.2).
1031+
1032+
Program Definition reorder_cstrs_transformation {efl : EEnvFlags} {wca : cstr_as_blocks = false} {has_app : has_tApp}
1033+
(wfl : WcbvFlags) {wcon : with_constructor_as_block = false} :
1034+
Transform.t _ _ _ EAst.term _ _
1035+
(eval_eprogram_env_mapping wfl) (eval_eprogram wfl) :=
1036+
{| name := "reorder inductive constructors ";
1037+
transform p _ := EReorderCstrs.reorder_program p.1 (to_program p.2) ;
1038+
pre p := [/\ wf_eprogram_env efl p.2, EEtaExpandedFix.expanded_eprogram_env p.2 & wf_inductives_mapping p.2.1 p.1] ;
1039+
post p := wf_eprogram efl p /\ EEtaExpandedFix.expanded_eprogram p;
1040+
obseq p hp p' v v' := v' = EReorderCstrs.reorder p.1 v |}.
10421041

10431042
Next Obligation.
1044-
move=> efl wfl m. cbn. now apply trust_reorder_cstrs_wf.
1043+
move=> efl wca hasapp wfl wcb [m p] [wfp exp wfm]. split => //.
1044+
now unshelve eapply reorder_program_wf. cbn.
1045+
now eapply reorder_program_expanded_fix.
10451046
Qed.
10461047
Next Obligation.
1047-
red. eapply trust_reorder_cstrs_pres.
1048+
red. intros efl wca hasapp wfl wcb [m p] v [wfp wfm] evp.
1049+
destruct evp as [ev].
1050+
unshelve eapply EReorderCstrs.optimize_correct in ev; trea.
1051+
2,3:apply wfp.
1052+
exists (reorder m v). split => //.
10481053
Qed.
10491054

10501055
#[global]
1051-
Axiom trust_reorder_cstrs_transformation_ext : forall (efl : EEnvFlags) (wfl : WcbvFlags) (m : inductives_mapping),
1052-
TransformExt.t (reorder_cstrs_transformation efl wfl m)
1053-
(fun p p' => extends p.1 p'.1) (fun p p' => extends p.1 p'.1).
1056+
Instance reorder_cstrs_transformation_ext {efl : EEnvFlags} (wca : cstr_as_blocks = false) (has_app : has_tApp) (wfl : WcbvFlags)
1057+
{wcon : with_constructor_as_block = false} :
1058+
TransformExt.t (reorder_cstrs_transformation (wca := wca) (has_app := has_app) wfl (wcon:=wcon))
1059+
(fun p p' => p.1 = p'.1 /\ extends p.2.1 p'.2.1) (fun p p' => extends p.1 p'.1).
1060+
Proof.
1061+
red. intros p p' pr pr' [eq ext].
1062+
red; cbn. rewrite -eq. eapply EReorderCstrs.optimize_extends_env; eauto.
1063+
move: pr'; cbn. now intros []. apply pr. apply pr'.
1064+
Qed.
10541065

10551066
#[global]
1056-
Axiom trust_reorder_cstrs_transformation_ext' : forall (efl : EEnvFlags) (wfl : WcbvFlags) (m : inductives_mapping),
1057-
TransformExt.t (reorder_cstrs_transformation efl wfl m)
1058-
extends_eprogram extends_eprogram.
1067+
Instance reorder_cstrs_transformation_ext' {efl : EEnvFlags} (wca : cstr_as_blocks = false) (has_app : has_tApp) (wfl : WcbvFlags)
1068+
{wcon : with_constructor_as_block = false} :
1069+
TransformExt.t (reorder_cstrs_transformation (wca := wca) (has_app := has_app) wfl (wcon:=wcon))
1070+
(fun p p' => p.1 = p'.1 /\ extends_eprogram_env p.2 p'.2) extends_eprogram.
1071+
Proof.
1072+
red. intros p p' pr pr' [eq [ext eq']]. cbn.
1073+
red. cbn. rewrite -eq -eq'. split => //. eapply EReorderCstrs.optimize_extends_env; eauto.
1074+
move: pr'; cbn. now intros []. apply pr. apply pr'.
1075+
Qed.
10591076

10601077
From MetaCoq.Erasure Require Import EUnboxing.
10611078

0 commit comments

Comments
 (0)