1 (******************************************************************************)
5 (* Claudio Sacerdoti Coen <sacerdot@@cs.unibo.it> *)
8 (* This module is the objects level of a parser for cic objects from xml *)
9 (* files to the internal representation. It uses the module cicParser3 *)
10 (* cicParser3 (terms level) and it is used only through cicParser2 (top *)
13 (******************************************************************************)
15 exception IllFormedXml of int;;
16 exception NotImplemented;;
18 (* Utility functions that transform a Pxp attribute into something useful *)
20 (* mk_absolute_uris "n1: v1 ... vn n2 : u1 ... un ...." *)
21 (* returns [(n1,[absolute_uri_for_v1 ; ... ; absolute_uri_for_vn]) ; (n2,...) *)
22 let mk_absolute_uris s =
23 let l = (Str.split (Str.regexp ":") s) in
24 let absolute_of_relative n v =
25 let module P3 = CicParser3 in
29 | (n,he::tl) when n > 0 ->
30 "/" ^ he ^ mkburi (n - 1, tl)
31 | _ -> raise (IllFormedXml 12)
33 let m = List.length !P3.current_sp - (int_of_string n) in
34 let buri = mkburi (m, !P3.current_sp) in
35 UriManager.uri_of_string ("cic:" ^ buri ^ v ^ ".var")
41 let vars = (Str.split (Str.regexp " ") vs) in
42 [(int_of_string no, List.map (absolute_of_relative no) vars)]
44 let vars = (Str.split (Str.regexp " ") vs) in
49 let (pvars, no2) = add_prefix tl in
50 ((absolute_of_relative no he)::pvars, no2)
51 | _ -> raise (IllFormedXml 11)
53 let (pvars, no2) = add_prefix vars in
54 (int_of_string no, pvars)::(absolutize (no2::tl))
55 | _ -> raise (IllFormedXml 10)
57 (* last parameter must be applied first *)
61 let option_uri_list_of_attr a1 a2 =
62 let module T = Pxp_types in
65 T.Value s -> mk_absolute_uris s
66 | _ -> raise (IllFormedXml 0)
69 T.Value "POSSIBLE" -> Cic.Possible parameters
70 | T.Implied_value -> Cic.Actual parameters
71 | _ -> raise (IllFormedXml 0)
74 let uri_list_of_attr a =
75 let module T = Pxp_types in
77 T.Value s -> mk_absolute_uris s
78 | _ -> raise (IllFormedXml 0)
81 let string_of_attr a =
82 let module T = Pxp_types in
85 | _ -> raise (IllFormedXml 0)
89 int_of_string (string_of_attr a)
93 bool_of_string (string_of_attr a)
96 (* Other utility functions *)
99 match n#sub_nodes with
101 | _ -> raise (IllFormedXml 1)
104 let register_id id node =
105 if !CicParser3.process_annotations then
106 match !CicParser3.ids_to_targets with
108 | Some ids_to_targets ->
109 Hashtbl.add ids_to_targets id (Cic.Object node)
112 (* Functions that, given the list of sons of a node of the cic dom (objects *)
113 (* level), retrieve the internal representation associated to the node. *)
114 (* Everytime a cic term subtree is found, it is translated to the internal *)
115 (* representation using the method to_cic_term defined in cicParser3. *)
116 (* Each function raise IllFormedXml if something goes wrong, but this should *)
117 (* be impossible due to the presence of the dtd *)
118 (* The functions should really be obvious looking at their name and the cic *)
121 (* called when a CurrentProof is found *)
122 let get_conjs_value_type l =
123 let rec rget (c, v, t) l =
124 let module D = Pxp_document in
127 | conj::tl when conj#node_type = D.T_element "Conjecture" ->
128 let no = int_of_attr (conj#attribute "no")
129 and typ = (get_content conj)#extension#to_cic_term in
130 rget ((no, typ)::c, v, t) tl
131 | value::tl when value#node_type = D.T_element "body" ->
132 let v' = (get_content value)#extension#to_cic_term in
134 None -> rget (c, Some v', t) tl
135 | _ -> raise (IllFormedXml 2)
137 | typ::tl when typ#node_type = D.T_element "type" ->
138 let t' = (get_content typ)#extension#to_cic_term in
140 None -> rget (c, v, Some t') tl
141 | _ -> raise (IllFormedXml 3)
143 | _ -> raise (IllFormedXml 4)
145 match rget ([], None, None) l with
146 (c, Some v, Some t) -> (c, v, t)
147 | _ -> raise (IllFormedXml 5)
150 (* used only by get_inductive_types; called one time for each inductive *)
151 (* definitions in a block of inductive definitions *)
152 let get_names_arity_constructors l =
153 let rec rget (a,c) l =
154 let module D = Pxp_document in
157 | arity::tl when arity#node_type = D.T_element "arity" ->
158 let a' = (get_content arity)#extension#to_cic_term in
160 | con::tl when con#node_type = D.T_element "Constructor" ->
161 let id = string_of_attr (con#attribute "name")
162 and ty = (get_content con)#extension#to_cic_term in
163 rget (a,(id,ty,ref None)::c) tl
164 | _ -> raise (IllFormedXml 9)
166 match rget (None,[]) l with
167 (Some a, c) -> (a, List.rev c)
168 | _ -> raise (IllFormedXml 8)
171 (* called when an InductiveDefinition is found *)
172 let rec get_inductive_types =
176 let tyname = string_of_attr (he#attribute "name")
177 and inductive = bool_of_attr (he#attribute "inductive")
179 get_names_arity_constructors (he#sub_nodes)
181 (tyname,inductive,arity,cons)::(get_inductive_types tl) (*CSC 0 a caso *)
184 (* This is the main function and also the only one used directly from *)
185 (* cicParser. Given the root of the dom tree, it returns the internal *)
186 (* representation of the cic object described in the tree *)
187 (* It uses the previous functions and the to_cic_term method defined *)
188 (* in cicParser3 (used for subtrees that encode cic terms) *)
190 let module D = Pxp_document in
191 let module C = Cic in
192 let ntype = n # node_type in
194 D.T_element "Definition" ->
195 let id = string_of_attr (n # attribute "name")
197 option_uri_list_of_attr (n#attribute "params") (n#attribute "paramMode")
199 let sons = n#sub_nodes in
202 v#node_type = D.T_element "body" &&
203 t#node_type = D.T_element "type" ->
204 let v' = get_content v
205 and t' = get_content t in
206 (v'#extension#to_cic_term, t'#extension#to_cic_term)
207 | _ -> raise (IllFormedXml 6)
208 and xid = string_of_attr (n#attribute "id") in
209 let res = C.ADefinition (xid, ref None, id, value, typ, params) in
210 register_id xid res ;
212 | D.T_element "Axiom" ->
213 let id = string_of_attr (n # attribute "name")
214 and params = uri_list_of_attr (n # attribute "params")
216 (get_content (get_content n))#extension#to_cic_term
217 and xid = string_of_attr (n#attribute "id") in
218 let res = C.AAxiom (xid, ref None, id, typ, params) in
219 register_id xid res ;
221 | D.T_element "CurrentProof" ->
222 let name = string_of_attr (n#attribute "name")
223 and xid = string_of_attr (n#attribute "id") in
224 let sons = n#sub_nodes in
225 let (conjs, value, typ) = get_conjs_value_type sons in
226 let res = C.ACurrentProof (xid, ref None, name, conjs, value, typ) in
227 register_id xid res ;
229 | D.T_element "InductiveDefinition" ->
230 let sons = n#sub_nodes
231 and xid = string_of_attr (n#attribute "id") in
232 let inductiveTypes = get_inductive_types sons
233 and params = uri_list_of_attr (n#attribute "params")
234 and nparams = int_of_attr (n#attribute "noParams") in
236 C.AInductiveDefinition (xid, ref None, inductiveTypes, params, nparams)
238 register_id xid res ;
240 | D.T_element "Variable" ->
241 let name = string_of_attr (n#attribute "name")
242 and xid = string_of_attr (n#attribute "id")
244 let sons = n#sub_nodes in
247 b#node_type = D.T_element "body" &&
248 t#node_type = D.T_element "type" ->
249 let b' = get_content b
250 and t' = get_content t in
251 (Some (b'#extension#to_cic_term), t'#extension#to_cic_term)
252 | [t] when t#node_type = D.T_element "type" ->
253 let t' = get_content t in
254 (None, t'#extension#to_cic_term)
255 | _ -> raise (IllFormedXml 6)
257 let res = C.AVariable (xid,ref None,name,body,typ) in
258 register_id xid res ;
263 raise (IllFormedXml 7)