Skip to content

Commit b5f5c28

Browse files
committed
Add global options, fix object/summary declarations for plugins to be local.
1 parent 9db8088 commit b5f5c28

File tree

3 files changed

+73
-30
lines changed

3 files changed

+73
-30
lines changed

examples/erase-conv/fib.v

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
From VerifiedExtraction Require Import Loader.
1+
From VerifiedExtraction Require Import Extraction.
22

33
Set Verified Extraction Build Directory "_build".
44

55
(* Set Debug "verified-extraction". *)
66

7-
Definition foo := (@eq_refl bool false <<<: (false = negb true)).
7+
Set Verified Extraction Timing.
8+
Set Verified Extraction Format.
9+
Set Verified Extraction Optimize.
10+
11+
(* Definition foo := (@eq_refl bool false <<<: (false = negb true)). *)
812

913
From Stdlib Require Import Arith.
1014

@@ -16,9 +20,11 @@ Fixpoint fib (n : nat) :=
1620
end.
1721

1822
Time Definition fib2 := Eval compute in fib 2.
19-
Check (@eq_refl nat fib2 <<<: (fib2 = fib 2)).
23+
(* Check (ltac2:(erase_nocheck (fib 2) fib2)). *)
2024

21-
Time Definition fib23 := Eval compute in fib 23.
22-
(* > 1.5s *)
23-
Time Definition efib23 := (@eq_refl nat fib23 <<<: (fib23 = fib 23)).
24-
(* Runtime is lower (< 1s ) even with compilation + dynlink + reification *)
25+
Definition longtest (x: unit) :=
26+
let x := fib 30 in match x with 0 => tt | S _ => tt end.
27+
Time Definition fib23 := Eval vm_compute in longtest tt.
28+
(* 0.2s *)
29+
Time Definition efib23 := ltac2:(erase_forget (longtest tt)).
30+
(* 0.3s total, 0.09s runing time *)

lib/rocq_verified_extraction_plugin/lib/verified_extraction.ml

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ type malfunction_plugin_config =
6363
let debug_extract = CDebug.create ~name:"verified-extraction" ()
6464
let debug = debug_extract
6565

66+
let get_bool_option key =
67+
let open Goptions in
68+
match get_option_value key with
69+
| Some get -> fun () ->
70+
begin match get () with
71+
| BoolValue b -> b
72+
| _ -> assert false
73+
end
74+
| None ->
75+
(declare_bool_option_and_ref ~key ~value:false ()).get
76+
6677
let get_stringopt_option key =
6778
let open Goptions in
6879
match get_option_value key with
@@ -80,6 +91,21 @@ let get_build_dir_opt =
8091
let get_opam_path_opt =
8192
get_stringopt_option ["Verified"; "Extraction"; "Opam"; "Path"]
8293

94+
let get_timing_opt =
95+
get_bool_option ["Verified"; "Extraction"; "Timing"]
96+
97+
let get_typed_opt =
98+
get_bool_option ["Verified"; "Extraction"; "Typed"]
99+
100+
let get_verbose_opt =
101+
get_bool_option ["Verified"; "Extraction"; "Verbose"]
102+
103+
let get_optimize_opt =
104+
get_bool_option ["Verified"; "Extraction"; "Optimize"]
105+
106+
let get_format_opt =
107+
get_bool_option ["Verified"; "Extraction"; "Format"]
108+
83109
(* When building standalone programs still relying on Rocq's/MetaRocq's FFIs, use these packages for linking *)
84110
let statically_linked_pkgs =
85111
["rocq-runtime.boot";
@@ -280,8 +306,10 @@ let make_options loc l =
280306
let prims = get_global_prims () in
281307
let default = {
282308
malfunction_pipeline_config = default_malfunction_config inductives_mapping inlining prims;
283-
bypass_qeds = false; time = false; program_type = None; load = false; run = false;
284-
verbose = false; loc; format = false; optimize = false }
309+
bypass_qeds = false; time = get_timing_opt ();
310+
program_type = None; load = false; run = false;
311+
verbose = get_verbose_opt (); loc; format = get_format_opt ();
312+
optimize = get_optimize_opt () }
285313
in
286314
let parse_unsafe_flags unsafe l =
287315
match l with
@@ -421,17 +449,16 @@ type malfunction_program_type =
421449

422450
type plugin_function = Obj.t
423451

424-
let register_plugins = Summary.ref ~name:"verified-extraction-plugins" (CString.Map.empty : plugin_function CString.Map.t)
452+
let register_plugins = Summary.ref ~local:true ~name:"verified-extraction-plugins" (CString.Map.empty : plugin_function CString.Map.t)
425453

426454
let cache_plugin (name, fn) =
427455
register_plugins := CString.Map.add name fn !register_plugins
428456

429457
let plugin_input =
430458
let open Libobject in
431459
declare_object
432-
(global_object_nodischarge "verified-extraction-plugins"
433-
~cache:(fun r -> cache_plugin r)
434-
~subst:None)
460+
(local_object_nodischarge "verified-extraction-plugins"
461+
~cache:(fun r -> cache_plugin r))
435462

436463
let register_plugin name fn : unit =
437464
Lib.add_leaf (plugin_input (name, fn))
@@ -612,8 +639,7 @@ struct
612639

613640
let reify opts env sigma tyinfo result =
614641
let mapping = opts.malfunction_pipeline_config.reorder_constructors in
615-
if opts.time then time opts (Pp.str "Reification") (reify env sigma mapping tyinfo) result
616-
else reify env sigma mapping tyinfo result
642+
time opts (Pp.str "Reification") (reify env sigma mapping tyinfo) result
617643

618644
end
619645

@@ -677,12 +703,15 @@ let compile opts names tyinfos fname =
677703
notice opts Pp.(fun () -> str "Compiled to " ++ str output);
678704
Some (StandaloneProgram output)
679705

680-
let run_code opts env sigma tyinfo code : Constr.t =
706+
let run_code opts env sigma fn tyinfo code : Constr.t =
681707
let open Reify in
682708
let value, tyinfo =
683-
match tyinfo with
684-
| IsThunk vty -> ((Obj.magic code : unit -> Obj.t) (), vty)
685-
| IsValue vty -> code, vty
709+
try
710+
match tyinfo with
711+
| IsThunk vty -> (time opts Pp.(str fn) (Obj.magic code : unit -> Obj.t) (), vty)
712+
| IsValue vty -> time opts Pp.(str fn) (fun _ -> code) (), vty
713+
with Stack_overflow ->
714+
CErrors.user_err Pp.(str"Compiled program has overflown the stack.")
686715
in
687716
Reify.reify opts env sigma tyinfo value
688717

@@ -696,7 +725,7 @@ let run opts env sigma result : Constr.t list option =
696725
let run fn tyinfo =
697726
match CString.Map.find_opt fn !register_plugins with
698727
| None -> CErrors.anomaly Pp.(str"Couldn't find funtion " ++ str fn ++ str" which should have been registered by " ++ str shared_lib)
699-
| Some code -> time opts Pp.(str fn) (run_code opts env sigma tyinfo) code
728+
| Some code -> run_code opts env sigma fn tyinfo code
700729
in
701730
Some (List.map2 run fns tyinfos)
702731
end else None
@@ -803,7 +832,7 @@ let eval_plugin_gen ?loc opts (gr : Libnames.qualid) =
803832
let sigma, grc = Evd.fresh_global env sigma gr in
804833
let tyinfo = Reify.check_reifyable_thunk_or_value env sigma grc in
805834
let code = eval_name fn in
806-
let c = run_code opts env sigma tyinfo code in
835+
let c = run_code opts env sigma fn tyinfo code in
807836
env, sigma, c
808837

809838
let eval_plugin ?loc opts (gr : Libnames.qualid) =
@@ -816,15 +845,23 @@ let eval ?loc opts gr =
816845

817846
let prc = Printer.pr_constr_env
818847

848+
let make_thunk term ty =
849+
let unit = EConstr.mkIndU (Globnames.destIndRef (Rocqlib.lib_ref "core.unit.type"), EConstr.EInstance.empty) in
850+
let binder = EConstr.anonR in
851+
let term = EConstr.mkLambda (binder, unit, term) in
852+
let ty = EConstr.mkProd (binder, unit, ty) in
853+
term, ty
854+
819855
let erase_eval_function compile_malfunction : Eraseconv.erase_evaluation_function =
856+
let opts = [ProgramType Plugin; Load; Run] in
820857
fun env term ty ->
858+
let opts = make_options None opts in
821859
let sigma = Evd.from_env env in
822860
debug Pp.(fun () -> str"Erasure evaluation called on term: " ++ prc env sigma term ++ str" of type " ++
823861
prc env sigma ty);
824-
let opts = [ProgramType Plugin; Time; Load; Run] in
825-
let opts = make_options None opts in
826-
let tyinfo = time opts Pp.(str"Checking reifyability") (Reify.check_reifyable_thunk_or_value_type env sigma) (EConstr.of_constr ty) in
827-
let res = time opts Pp.(str"Extracting, running and reifying") (extract_and_run compile_malfunction opts env sigma (EConstr.of_constr term) [tyinfo]) None in
862+
let term, ty = make_thunk (EConstr.of_constr term) (EConstr.of_constr ty) in
863+
let tyinfo = time opts Pp.(str"Checking reifyability") (Reify.check_reifyable_thunk_or_value_type env sigma) ty in
864+
let res = time opts Pp.(str"Extracting, running and reifying") (extract_and_run compile_malfunction opts env sigma term [tyinfo]) None in
828865
match res with
829866
| None ->
830867
debug Pp.(fun () -> str"Didn't reify to a value");

plugin/plugin-bootstrap/Tactics.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
From VerifiedExtraction Require Import Loader.
2+
From Ltac2 Require Import Ltac2.
23

34
Inductive value_of {A} : A -> Type :=
45
| value_is a : value_of a.
@@ -8,8 +9,6 @@ Definition get_value_of {A} {a : A} (c : value_of a) : A :=
89
Lemma value_of_eq {A} {a : A} (c : value_of a) : get_value_of c = a.
910
Proof. destruct c. reflexivity. Defined.
1011

11-
From Ltac2 Require Import Ltac2.
12-
1312
(** Runs erasure evaluation only once, in the kernel (if paired with Std.exact).
1413
Beware that no check is performed that the cast is correct when using this in a tactic.
1514
It will be checked at Qed-time only. *)
@@ -68,13 +67,14 @@ Ltac2 Notation "erase_forget" t(constr) := erase_forget0 t.
6867
Ltac2 Notation "value_of" c(constr) := show_value c.
6968

7069
(* Tests *)
70+
(*
7171
Definition foo (x : unit) := true.
7272
7373
Definition test := ltac2:(erase_nocheck (foo tt) true).
74-
Check test : foo tt = true.
74+
(* Check test : foo tt = true. *)
7575
7676
Definition testevar := ltac2:(erase_compute (foo tt)).
77-
Check testevar : foo tt = true.
77+
(* Check testevar : foo tt = true. *)
7878
7979
Definition foo_tt_value := ltac2:(erase_forget (foo tt)).
80-
Check eq_refl : foo_tt_value = true.
80+
Check eq_refl : foo_tt_value = true. *)

0 commit comments

Comments
 (0)