]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/common/hierarchy.ml
6e8c98fd66dc628ae4ab16cc13eb6008a98f7211
[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
15 module C = Cps
16
17 type graph = string * (int -> int)
18
19 let sorts = 3
20 let sort = K.create sorts
21
22 let default_graph = "Z1"
23
24 (* Internal functions *******************************************************)
25
26 let set_sort h s =
27    K.add sort h s; succ h
28
29 let graph_of_string err f s =
30    try 
31       let x = P.sscanf s "Z%u" C.start in 
32       if x > 0 then f (s, fun h -> x + h) else err ()
33    with
34       P.Scan_failure _ | Failure _ | End_of_file -> err ()
35
36 let graph = ref (graph_of_string C.err C.start default_graph)
37
38 (* Interface functions ******************************************************)
39
40 let set_sorts i ss =   
41    List.fold_left set_sort i ss
42
43 let string_of_sort err f h =
44    try f (K.find sort h) with Not_found -> err ()
45
46 let sort_of_string err f s =
47    let map h n = function
48       | None when n = s -> Some h
49       | xh              -> xh
50    in
51    match K.fold map sort None with
52       | None   -> err ()
53       | Some h -> f h
54
55 let string_of_graph () = fst !graph
56
57 let apply h = snd !graph h
58
59 let set_graph s =
60    let err () = false in
61    let f g = graph := g; true in
62    graph_of_string err f s
63
64 let clear () =
65    K.clear sort; graph := graph_of_string C.err C.start default_graph