type graph = string * (int -> int)
-let sorts = 2
+let sorts = 3
let sort = H.create sorts
+let default_graph = "Z1"
+
(* Internal functions *******************************************************)
let set_sort h s =
H.add sort h s; succ h
+let graph_of_string err f s =
+ try
+ let x = S.sscanf s "Z%u" C.start in
+ if x > 0 then f (s, fun h -> x + h) else err ()
+ with
+ S.Scan_failure _ | Failure _ | End_of_file -> err ()
+
+let graph = ref (graph_of_string C.err C.start default_graph)
+
(* Interface functions ******************************************************)
-let set_sorts ss i =
+let set_sorts i ss =
List.fold_left set_sort i ss
-let get_sort err f h =
+let string_of_sort err f h =
try f (H.find sort h) with Not_found -> err ()
-let string_of_graph (s, _) = s
+let sort_of_string err f s =
+ let map h n = function
+ | None when n = s -> Some h
+ | xh -> xh
+ in
+ match H.fold map sort None with
+ | None -> err ()
+ | Some h -> f h
-let apply (_, g) h = (g h)
+let string_of_graph () = fst !graph
-let graph_of_string err f s =
- try
- let x = S.sscanf s "Z%u" C.start in
- if x > 0 then f (s, fun h -> x + h) else err ()
- with
- S.Scan_failure _ | Failure _ | End_of_file -> err ()
+let apply h = snd !graph h
+
+let set_graph s =
+ let err () = false in
+ let f g = graph := g; true in
+ graph_of_string err f s
+
+let clear () =
+ H.clear sort; graph := graph_of_string C.err C.start default_graph