]> matita.cs.unibo.it Git - helm.git/blob - components/ng_kernel/nReference.ml
matita 0.5.1 tagged
[helm.git] / 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 of int (* height *)
19  | Fix of int * int * int (* fixno, recparamno, height *)
20  | CoFix of int
21  | Ind of bool * int * int (* inductive, indtyno, leftno *)
22  | Con of int * int * int  (* indtyno, constrno, leftno  *)
23
24 type reference = Ref of 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 get3 s dot =
57     let comma2 = String.rindex s ',' in
58     let comma = String.rindex_from s (comma2-1) ',' in
59     let s_i = String.sub s (dot+5) (comma-dot-5) in
60     let s_j = String.sub s (comma+1) (comma2-comma-1) in
61     let s_h = String.sub s (comma2+1) (String.length s-comma2-2) in
62     let i = int_of_string s_i in
63     let j = int_of_string s_j in
64     let h = int_of_string s_h in
65     i,j,h
66   in
67 (*
68   let get2 s dot =
69     let comma = String.rindex s ',' in
70     let i = int_of_string (String.sub s (dot+5) (comma-dot-5)) in
71     let j = int_of_string (String.sub s (comma+1) (String.length s-comma-2)) in
72     i,j
73   in
74 *)
75   let get1 s dot =
76     let i = int_of_string (String.sub s (dot+5) (String.length s-1-dot-5)) in
77     i
78   in
79 fun s ->
80   try MapStringsToReference.find s !set_of_reference
81   with Not_found ->
82     let new_reference =
83       try
84         let dot = String.rindex s '.' in
85         let prefix = String.sub s 0 (dot+1) in
86         let suffix = String.sub s (dot+1) 3 in
87         let u = NUri.uri_of_string (prefix ^ uri_suffix_of_ref_suffix suffix) in
88         match suffix with
89         | "dec" -> Ref (u, Decl)
90         | "def" -> let i = get1 s dot in Ref (u, Def i)
91         | "fix" -> let i,j,h = get3 s dot in Ref (u, Fix (i,j,h))
92         | "cfx" -> let i = get1 s dot in Ref (u, CoFix (i))
93         | "ind" -> let b,i,l = get3 s dot in Ref (u, Ind (b=1,i,l))
94         | "con" -> let i,j,l = get3 s dot in Ref (u, Con (i,j,l))
95         | _ -> raise Not_found
96       with Not_found -> raise (IllFormedReference (lazy s))
97     in
98     set_of_reference := MapStringsToReference.add s new_reference !set_of_reference;
99     new_reference
100 ;;
101
102 let string_of_reference (Ref (u,indinfo)) =
103   let s = NUri.string_of_uri u in
104   let dot = String.rindex s '.' in
105   let s2 = String.sub s 0 dot in
106   match indinfo with
107   | Decl ->  s2 ^ ".dec"
108   | Def h -> s2 ^ ".def(" ^ string_of_int h ^ ")"
109   | Fix (i,j,h)  -> 
110       s2 ^ ".fix(" ^ string_of_int i ^ "," ^ 
111       string_of_int j ^ "," ^ string_of_int h ^ ")"
112   | CoFix i -> s2 ^ ".cfx(" ^ string_of_int i ^ ")"
113   | Ind (b,i,l)->s2 ^".ind(" ^(if b then "1" else "0")^ "," ^ string_of_int i ^
114       "," ^ string_of_int l ^ ")"
115   | Con (i,j,l) -> s2 ^ ".con(" ^ string_of_int i ^ "," ^ string_of_int j ^ 
116       "," ^ string_of_int l ^ ")"
117 ;;
118
119 let mk_constructor j = function
120   | Ref (u, Ind (_,i,l)) -> 
121       reference_of_string (string_of_reference (Ref (u, Con (i,j,l))))
122   | _ -> assert false
123 ;;
124
125 let mk_fix i j = function
126   | Ref (u, Fix (_,_,h)) -> 
127       reference_of_string (string_of_reference (Ref (u, Fix (i,j,h))))
128   | _ -> assert false
129 ;;
130
131 let mk_cofix i = function
132   | Ref (u, CoFix _) -> 
133       reference_of_string (string_of_reference (Ref (u, CoFix i)))
134   | _ -> assert false
135 ;;