aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Emilio Jesus Gallego Arias <e+git@x80.org>2017-11-19 04:47:15 +0100
committerGravatar Emilio Jesus Gallego Arias <e+git@x80.org>2017-11-19 04:51:49 +0100
commita554519874c15d0a790082e5f15f3dc2419c6c38 (patch)
treefbca74c97943685e93e10b85de708cc7c54a7abe
parentedf1a8f36f75861b822081b3825357e122b6937d (diff)
[lib] Minor pending cleanup to consolidate helper function.
While we are at it we refactor a few special cases of the helper.
-rw-r--r--interp/constrextern.ml12
-rw-r--r--lib/flags.ml34
-rw-r--r--lib/flags.mli9
3 files changed, 21 insertions, 34 deletions
diff --git a/interp/constrextern.ml b/interp/constrextern.ml
index e1cf8f196..0ce672cc8 100644
--- a/interp/constrextern.ml
+++ b/interp/constrextern.ml
@@ -185,18 +185,8 @@ let with_universes f = Flags.with_option print_universes f
let with_meta_as_hole f = Flags.with_option print_meta_as_hole f
let without_symbols f = Flags.with_option print_no_symbol f
-(* XXX: Where to put this in the library? Util maybe? *)
-let protect_ref r nf f x =
- let old_ref = !r in
- r := nf !r;
- try let res = f x in r := old_ref; res
- with reraise ->
- let reraise = Backtrace.add_backtrace reraise in
- r := old_ref;
- Exninfo.iraise reraise
-
let without_specific_symbols l =
- protect_ref inactive_notations_table
+ Flags.with_modified_ref inactive_notations_table
(fun tbl -> IRuleSet.(union (of_list l) tbl))
(**********************************************************************)
diff --git a/lib/flags.ml b/lib/flags.ml
index 323b5492d..ddc8f8482 100644
--- a/lib/flags.ml
+++ b/lib/flags.ml
@@ -6,13 +6,17 @@
(* * GNU Lesser General Public License Version 2.1 *)
(************************************************************************)
-let with_option o f x =
- let old = !o in o:=true;
- try let r = f x in if !o = true then o := old; r
- with reraise ->
- let reraise = Backtrace.add_backtrace reraise in
- let () = o := old in
- Exninfo.iraise reraise
+let with_modified_ref r nf f x =
+ let old_ref = !r in r := nf !r;
+ try let res = f x in r := old_ref; res
+ with reraise ->
+ let reraise = Backtrace.add_backtrace reraise in
+ r := old_ref;
+ Exninfo.iraise reraise
+
+let with_option o f x = with_modified_ref o (fun _ -> true) f x
+let without_option o f x = with_modified_ref o (fun _ -> false) f x
+let with_extra_values o l f x = with_modified_ref o (fun ol -> ol@l) f x
let with_options ol f x =
let vl = List.map (!) ol in
@@ -25,22 +29,6 @@ let with_options ol f x =
let () = List.iter2 (:=) ol vl in
Exninfo.iraise reraise
-let without_option o f x =
- let old = !o in o:=false;
- try let r = f x in if !o = false then o := old; r
- with reraise ->
- let reraise = Backtrace.add_backtrace reraise in
- let () = o := old in
- Exninfo.iraise reraise
-
-let with_extra_values o l f x =
- let old = !o in o:=old@l;
- try let r = f x in o := old; r
- with reraise ->
- let reraise = Backtrace.add_backtrace reraise in
- let () = o := old in
- Exninfo.iraise reraise
-
let boot = ref false
let record_aux_file = ref false
diff --git a/lib/flags.mli b/lib/flags.mli
index 0ff3e0a81..c4afb8318 100644
--- a/lib/flags.mli
+++ b/lib/flags.mli
@@ -110,6 +110,15 @@ val warn : bool ref
val make_warn : bool -> unit
val if_warn : ('a -> unit) -> 'a -> unit
+(** [with_modified_ref r nf f x] Temporarily modify a reference in the
+ call to [f x] . Be very careful with these functions, it is very
+ easy to fall in the typical problem with effects:
+
+ with_modified_ref r nf f x y != with_modified_ref r nf (f x) y
+
+*)
+val with_modified_ref : 'c ref -> ('c -> 'c) -> ('a -> 'b) -> 'a -> 'b
+
(** Temporarily activate an option (to activate option [o] on [f x y z],
use [with_option o (f x y) z]) *)
val with_option : bool ref -> ('a -> 'b) -> 'a -> 'b