]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/common/hierarchy.ml
28d95cc82cba6f14904971999362969a98df0aad
[helm.git] / helm / software / helena / src / common / hierarchy.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 module K = Hashtbl
13 module P = Scanf
14 module C = Cps
15
16 type graph = string * (int -> int)
17
18 let sorts = 3
19 let sort = K.create sorts
20
21 let default_graph = "Z1"
22
23 (* Internal functions *******************************************************)
24
25 let set_sort h s =
26    K.add sort h s; succ h
27
28 let graph_of_string err f s =
29    try 
30       let x = P.sscanf s "Z%u" C.start in 
31       if x > 0 then f (s, fun h -> x + h) else err ()
32    with
33       P.Scan_failure _ | Failure _ | End_of_file -> err ()
34
35 let graph = ref (graph_of_string C.err C.start default_graph)
36
37 (* Interface functions ******************************************************)
38
39 let set_sorts i ss =   
40    List.fold_left set_sort i ss
41
42 let string_of_sort err f h =
43    try f (K.find sort h) with Not_found -> err ()
44
45 let sort_of_string err f s =
46    let map h n = function
47       | None when n = s -> Some h
48       | xh              -> xh
49    in
50    match K.fold map sort None with
51       | None   -> err ()
52       | Some h -> f h
53
54 let string_of_graph () = fst !graph
55
56 let apply h = snd !graph h
57
58 let set_graph s =
59    let err () = false in
60    let f g = graph := g; true in
61    graph_of_string err f s
62
63 let clear () =
64    K.clear sort; graph := graph_of_string C.err C.start default_graph