]> matita.cs.unibo.it Git - helm.git/blobdiff - components/ng_kernel/nReference.ml
branch for universe
[helm.git] / components / ng_kernel / nReference.ml
diff --git a/components/ng_kernel/nReference.ml b/components/ng_kernel/nReference.ml
new file mode 100644 (file)
index 0000000..5da3305
--- /dev/null
@@ -0,0 +1,134 @@
+(* Copyright (C) 2000, HELM Team.
+ * 
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ * 
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA  02111-1307, USA.
+ * 
+ * For details, see the HELM World-Wide-Web page,
+ * http://cs.unibo.it/helm/.
+ *)
+
+exception IllFormedReference of string Lazy.t
+
+type spec = 
+ | Decl 
+ | Def
+ | Fix of int * int (* fixno, recparamno *)
+ | CoFix of int
+ | Ind of int
+ | Con of int * int (* indtyno, constrno *)
+
+type reference = Ref of int * NUri.uri * spec
+
+let eq = (==);;
+
+module OrderedStrings =
+ struct
+  type t = string
+  let compare (s1 : t) (s2 : t) = compare s1 s2
+ end
+;;
+
+module MapStringsToReference = Map.Make(OrderedStrings);;
+
+let set_of_reference = ref MapStringsToReference.empty;;
+
+(* '.' not allowed in path and foo
+ *
+ * Decl                   cic:/path/foo.dec
+ * Def                    cic:/path/foo.def
+ * Fix of int * int       cic:/path/foo.fix(i,j)
+ * CoFix of int           cic:/path/foo.cfx(i)
+ * Ind of int             cic:/path/foo.ind(i)
+ * Con of int * int       cic:/path/foo.con(i,j)
+ *)
+
+let uri_suffix_of_ref_suffix = function
+    | "dec" | "fix" | "cfx" | "def" -> "con"
+    | "ind" | "con" -> "ind"
+    | x -> prerr_endline (x ^ " not a valid suffix"); assert false
+;;
+
+let reference_of_string =
+  let counter = ref 0 in 
+  let c () = incr counter; !counter in 
+  let get2 s dot =
+    let comma = String.rindex s ',' in
+    let i = int_of_string (String.sub s (dot+5) (comma-dot-5)) in
+    let j = int_of_string (String.sub s (comma+1) (String.length s-comma-2)) in
+    i,j
+  in
+  let get1 s dot =
+    let i = int_of_string (String.sub s (dot+5) (String.length s-1-dot-5)) in
+    i
+  in
+fun s ->
+  try MapStringsToReference.find s !set_of_reference
+  with Not_found ->
+    let new_reference =
+      try
+        let dot = String.rindex s '.' in
+        let prefix = String.sub s 0 (dot+1) in
+        let suffix = String.sub s (dot+1) 3 in
+        let u = NUri.uri_of_string (prefix ^ uri_suffix_of_ref_suffix suffix) in
+        match suffix with
+        | "dec" -> Ref (c (), u, Decl)
+        | "def" -> Ref (c (), u, Def)
+        | "fix" -> let i,j = get2 s dot in Ref (c (), u, Fix (i,j))
+        | "cfx" -> let i = get1 s dot in Ref (c (), u, CoFix (i))
+        | "ind" -> let i = get1 s dot in Ref (c (), u, Ind (i))
+        | "con" -> let i,j = get2 s dot in Ref (c (), u, Con (i,j))
+        | _ -> raise Not_found
+      with Not_found -> raise (IllFormedReference (lazy s))
+    in
+    set_of_reference := MapStringsToReference.add s new_reference !set_of_reference;
+    new_reference
+;;
+
+let string_of_reference (Ref (_,u,indinfo)) =
+  let s = NUri.string_of_uri u in
+  let dot = String.rindex s '.' in
+  let s2 = String.sub s 0 dot in
+  match indinfo with
+  | Decl ->  s2 ^ ".dec"
+  | Def -> s2 ^ ".def"
+  | Fix (i,j)  -> s2 ^ ".fix(" ^ string_of_int i ^ "," ^ string_of_int j ^ ")"
+  | CoFix i -> s2 ^ ".cfx(" ^ string_of_int i ^ ")"
+  | Ind i -> s2 ^ ".ind(" ^ string_of_int i ^ ")"
+  | Con (i,j) -> s2 ^ ".con(" ^ string_of_int i ^ "," ^ string_of_int j ^ ")"
+;;
+
+let mk_constructor j = function
+  | Ref (d, u, Ind i) -> 
+      reference_of_string (string_of_reference (Ref (d, u, Con (i,j))))
+  | _ -> assert false
+;;
+
+let mk_fix i j = function
+  | Ref (d, u, Fix _) -> 
+      reference_of_string (string_of_reference (Ref (d, u, Fix (i,j))))
+  | _ -> assert false
+;;
+
+let reference_of_ouri u indinfo =
+  let u = NUri.nuri_of_ouri u in
+  reference_of_string (string_of_reference (Ref (max_int,u,indinfo)))
+;;
+
+let ouri_of_reference (Ref (_,u,_)) = NUri.ouri_of_nuri u;;
+