1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@@cs.unibo.it> *)
33 (* This module is the objects level of a parser for cic objects from xml *)
34 (* files to the internal representation. It uses the module cicParser3 *)
35 (* cicParser3 (terms level) and it is used only through cicParser2 (top *)
38 (******************************************************************************)
40 exception IllFormedXml of int;;
41 exception NotImplemented;;
43 (* Utility functions that transform a Pxp attribute into something useful *)
45 let uri_list_of_attr a =
46 let module T = Pxp_types in
49 List.map UriManager.uri_of_string (Str.split (Str.regexp " ") s)
50 | _ -> raise (IllFormedXml 0)
53 let string_of_attr a =
54 let module T = Pxp_types in
57 | _ -> raise (IllFormedXml 0)
61 int_of_string (string_of_attr a)
65 bool_of_string (string_of_attr a)
69 let module T = Pxp_types in
73 | T.Implied_value -> C.Anonymous
74 | _ -> raise (IllFormedXml 0)
77 (* Other utility functions *)
80 match n#sub_nodes with
82 | _ -> raise (IllFormedXml 1)
85 (* Functions that, given the list of sons of a node of the cic dom (objects *)
86 (* level), retrieve the internal representation associated to the node. *)
87 (* Everytime a cic term subtree is found, it is translated to the internal *)
88 (* representation using the method to_cic_term defined in cicParser3. *)
89 (* Each function raise IllFormedXml if something goes wrong, but this should *)
90 (* be impossible due to the presence of the dtd *)
91 (* The functions should really be obvious looking at their name and the cic *)
94 (* called when a CurrentProof is found *)
95 let get_conjs_value l =
96 let rec rget (c, v) l =
97 let module D = Pxp_document in
100 | conj::tl when conj#node_type = D.T_element "Conjecture" ->
101 let no = int_of_attr (conj#attribute "no") in
102 let id = string_of_attr (conj#attribute "id") in
103 let typ,canonical_context =
104 match List.rev (conj#sub_nodes) with
105 [] -> raise (IllFormedXml 13)
106 | typ::canonical_context ->
107 (get_content typ)#extension#to_cic_term [],
110 let id = string_of_attr (n#attribute "id") in
111 match n#node_type with
112 D.T_element "Decl" ->
113 let name = name_of_attr (n#attribute "name") in
114 let term = (get_content n)#extension#to_cic_term [] in
115 id, Some (name,Cic.ADecl term)
116 | D.T_element "Def" ->
117 let name = name_of_attr (n#attribute "name") in
118 let term = (get_content n)#extension#to_cic_term [] in
119 id, Some (name,Cic.ADef term)
120 | D.T_element "Hidden" -> id, None
121 | _ -> raise (IllFormedXml 14)
124 rget ((id, no, canonical_context, typ)::c, v) tl
125 | value::tl when value#node_type = D.T_element "body" ->
126 let v' = (get_content value)#extension#to_cic_term [] in
128 None -> rget (c, Some v') tl
129 | _ -> raise (IllFormedXml 2)
131 | _ -> raise (IllFormedXml 4)
133 match rget ([], None) l with
134 (revc, Some v) -> (List.rev revc, v)
135 | _ -> raise (IllFormedXml 5)
138 (* used only by get_inductive_types; called one time for each inductive *)
139 (* definitions in a block of inductive definitions *)
140 let get_names_arity_constructors l =
141 let rec rget (a,c) l =
142 let module D = Pxp_document in
145 | arity::tl when arity#node_type = D.T_element "arity" ->
146 let a' = (get_content arity)#extension#to_cic_term [] in
148 | con::tl when con#node_type = D.T_element "Constructor" ->
149 let id = string_of_attr (con#attribute "name")
150 and ty = (get_content con)#extension#to_cic_term [] in
151 rget (a,(id,ty)::c) tl
152 | _ -> raise (IllFormedXml 9)
154 match rget (None,[]) l with
155 (Some a, c) -> (a, List.rev c)
156 | _ -> raise (IllFormedXml 8)
159 (* called when an InductiveDefinition is found *)
160 let rec get_inductive_types =
164 let tyname = string_of_attr (he#attribute "name")
165 and inductive = bool_of_attr (he#attribute "inductive")
166 and xid = string_of_attr (he#attribute "id")
168 get_names_arity_constructors (he#sub_nodes)
170 (xid,tyname,inductive,arity,cons)::(get_inductive_types tl)
173 (* This is the main function and also the only one used directly from *)
174 (* cicParser. Given the root of the dom tree, it returns the internal *)
175 (* representation of the cic object described in the tree *)
176 (* It uses the previous functions and the to_cic_term method defined *)
177 (* in cicParser3 (used for subtrees that encode cic terms) *)
178 let rec get_term (n : CicParser3.cic_term Pxp_document.node) nbody
180 let module U = UriManager in
181 let module D = Pxp_document in
182 let module C = Cic in
183 let ntype = n#node_type in
185 D.T_element "ConstantType" ->
186 let name = string_of_attr (n # attribute "name") in
187 let params = uri_list_of_attr (n#attribute "params") in
188 let xid = string_of_attr (n#attribute "id") in
189 let typ = (get_content n)#extension#to_cic_term [] in
193 C.AConstant (xid, None, name, None, typ, params)
195 let nbodytype = nbody'#node_type in
197 D.T_element "ConstantBody" ->
198 (*CSC: the attribute "for" is ignored and not checked
199 let for_ = string_of_attr (nbody'#attribute "for") in
201 let paramsbody = uri_list_of_attr (nbody'#attribute "params") in
202 let xidbody = string_of_attr (nbody'#attribute "id") in
203 let value = (get_content nbody')#extension#to_cic_term [] in
204 if paramsbody = params then
205 C.AConstant (xid, Some xidbody, name, Some value, typ, params)
207 raise (IllFormedXml 6)
208 | D.T_element "CurrentProof" ->
209 (*CSC: the attribute "of" is ignored and not checked
210 let for_ = string_of_attr (nbody'#attribute "of") in
212 let xidbody = string_of_attr (nbody'#attribute "id") in
213 let sons = nbody'#sub_nodes in
214 let (conjs, value) = get_conjs_value sons in
215 C.ACurrentProof (xid, xidbody, name, conjs, value, typ, params)
218 | _ -> raise (IllFormedXml 6)
220 | D.T_element "InductiveDefinition" ->
221 let sons = n#sub_nodes
222 and xid = string_of_attr (n#attribute "id") in
223 let inductiveTypes = get_inductive_types sons
224 and params = uri_list_of_attr (n#attribute "params")
225 and nparams = int_of_attr (n#attribute "noParams") in
226 C.AInductiveDefinition (xid, inductiveTypes, params, nparams)
227 | D.T_element "Variable" ->
228 let name = string_of_attr (n#attribute "name")
229 and params = uri_list_of_attr (n#attribute "params")
230 and xid = string_of_attr (n#attribute "id")
232 let sons = n#sub_nodes in
235 b#node_type = D.T_element "body" &&
236 t#node_type = D.T_element "type" ->
237 let b' = get_content b
238 and t' = get_content t in
239 (Some (b'#extension#to_cic_term []), t'#extension#to_cic_term [])
240 | [t] when t#node_type = D.T_element "type" ->
241 let t' = get_content t in
242 (None, t'#extension#to_cic_term [])
243 | _ -> raise (IllFormedXml 6)
245 C.AVariable (xid,name,body,typ,params)
248 | _ -> raise (IllFormedXml 7)