Commit 9398f32e authored by POTTIER Francois's avatar POTTIER Francois

VisitorsString: new functions [print_longident] and [protect].

parent e2418caf
......@@ -25,3 +25,39 @@ let unquote alpha =
String.sub alpha 1 (n-1)
else
alpha
(* [print_longident] converts an OCaml long identifier to a string. *)
let print_longident (x : Longident.t) : string =
String.concat "." (Longident.flatten x)
(* Suppose the function [f] is a lossy (non-injective) mapping of ['a] to
[string]. Then, the function [protect equal f warn] is also a function of
['a] to [string], which behaves like [f], except it warns when [f] is
applied to two values of type ['a] that have the same image of type
[string]. *)
(* [equal] should implement equality at type ['a]. *)
(* [warn x1 x2 y] is invoked when [f] is applied at two values [x1] and [x2]
that have the same image [y] through [f]. *)
module H = Hashtbl
let protect
(equal : 'a -> 'a -> bool)
(f : 'a -> string)
(warn : 'a -> 'a -> string -> unit)
: 'a -> string =
(* A hash table memoizes the inverse of [f]. *)
let table : (string, 'a) H.t = H.create 127 in
fun (x : 'a) ->
let y = f x in
begin try
let x' = H.find table y in
if not (equal x x') then
warn x' x y
with Not_found ->
H.add table y x
end;
y
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment