]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_kernel/nReference.ml
New licence used uniformly everywhere.
[helm.git] / helm / software / components / ng_kernel / nReference.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 (* $Id$ *)
13
14 exception IllFormedReference of string Lazy.t
15
16 type spec = 
17  | Decl 
18  | Def
19  | Fix of int * int (* fixno, recparamno *)
20  | CoFix of int
21  | Ind of bool * int (* inductive, indtyno *)
22  | Con of int * int (* indtyno, constrno *)
23
24 type reference = Ref of int * NUri.uri * spec
25
26 let eq = (==);;
27
28 module OrderedStrings =
29  struct
30   type t = string
31   let compare (s1 : t) (s2 : t) = compare s1 s2
32  end
33 ;;
34
35 module MapStringsToReference = Map.Make(OrderedStrings);;
36
37 let set_of_reference = ref MapStringsToReference.empty;;
38
39 (* '.' not allowed in path and foo
40  *
41  * Decl                   cic:/path/foo.dec
42  * Def                    cic:/path/foo.def
43  * Fix of int * int       cic:/path/foo.fix(i,j)
44  * CoFix of int           cic:/path/foo.cfx(i)
45  * Ind of int             cic:/path/foo.ind(i)
46  * Con of int * int       cic:/path/foo.con(i,j)
47  *)
48
49 let uri_suffix_of_ref_suffix = function
50     | "dec" | "fix" | "cfx" | "def" -> "con"
51     | "ind" | "con" -> "ind"
52     | x -> prerr_endline (x ^ " not a valid suffix"); assert false
53 ;;
54
55 let reference_of_string =
56   let counter = ref 0 in 
57   let c () = incr counter; !counter in 
58   let get2 s dot =
59     let comma = String.rindex s ',' in
60     let i = int_of_string (String.sub s (dot+5) (comma-dot-5)) in
61     let j = int_of_string (String.sub s (comma+1) (String.length s-comma-2)) in
62     i,j
63   in
64   let get1 s dot =
65     let i = int_of_string (String.sub s (dot+5) (String.length s-1-dot-5)) in
66     i
67   in
68 fun s ->
69   try MapStringsToReference.find s !set_of_reference
70   with Not_found ->
71     let new_reference =
72       try
73         let dot = String.rindex s '.' in
74         let prefix = String.sub s 0 (dot+1) in
75         let suffix = String.sub s (dot+1) 3 in
76         let u = NUri.uri_of_string (prefix ^ uri_suffix_of_ref_suffix suffix) in
77         match suffix with
78         | "dec" -> Ref (c (), u, Decl)
79         | "def" -> Ref (c (), u, Def)
80         | "fix" -> let i,j = get2 s dot in Ref (c (), u, Fix (i,j))
81         | "cfx" -> let i = get1 s dot in Ref (c (), u, CoFix (i))
82         | "ind" -> let b,i = get2 s dot in Ref (c (), u, Ind (b=1,i))
83         | "con" -> let i,j = get2 s dot in Ref (c (), u, Con (i,j))
84         | _ -> raise Not_found
85       with Not_found -> raise (IllFormedReference (lazy s))
86     in
87     set_of_reference := MapStringsToReference.add s new_reference !set_of_reference;
88     new_reference
89 ;;
90
91 let string_of_reference (Ref (_,u,indinfo)) =
92   let s = NUri.string_of_uri u in
93   let dot = String.rindex s '.' in
94   let s2 = String.sub s 0 dot in
95   match indinfo with
96   | Decl ->  s2 ^ ".dec"
97   | Def -> s2 ^ ".def"
98   | Fix (i,j)  -> s2 ^ ".fix(" ^ string_of_int i ^ "," ^ string_of_int j ^ ")"
99   | CoFix i -> s2 ^ ".cfx(" ^ string_of_int i ^ ")"
100   | Ind (b,i)->s2 ^".ind(" ^(if b then "1" else "0")^ "," ^ string_of_int i ^")"
101   | Con (i,j) -> s2 ^ ".con(" ^ string_of_int i ^ "," ^ string_of_int j ^ ")"
102 ;;
103
104 let mk_constructor j = function
105   | Ref (d, u, Ind (_,i)) -> 
106       reference_of_string (string_of_reference (Ref (d, u, Con (i,j))))
107   | _ -> assert false
108 ;;
109
110 let mk_fix i j = function
111   | Ref (d, u, Fix _) -> 
112       reference_of_string (string_of_reference (Ref (d, u, Fix (i,j))))
113   | _ -> assert false
114 ;;
115
116 let mk_cofix i = function
117   | Ref (d, u, CoFix _) -> 
118       reference_of_string (string_of_reference (Ref (d, u, CoFix i)))
119   | _ -> assert false
120 ;;
121
122 let reference_of_ouri u indinfo =
123   let u = NUri.nuri_of_ouri u in
124   reference_of_string (string_of_reference (Ref (max_int,u,indinfo)))
125 ;;
126
127 let ouri_of_reference (Ref (_,u,_)) = NUri.ouri_of_nuri u;;
128