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 terms level of a parser for cic objects from xml *)
34 (* files to the internal representation. It is used by the module cicParser2 *)
35 (* (objects level). It defines an extension of the standard dom using the *)
36 (* object-oriented extension machinery of markup: an object with a method *)
37 (* to_cic_term that returns the internal representation of the subtree is *)
38 (* added to each node of the dom tree *)
40 (******************************************************************************)
42 exception IllFormedXml of int;;
44 (* Utility functions to map a markup attribute to something useful *)
46 let cic_attr_of_xml_attr =
48 Pxp_types.Value s -> Cic.Name s
49 | Pxp_types.Implied_value -> Cic.Anonymous
50 | _ -> raise (IllFormedXml 1)
52 let cic_sort_of_xml_attr =
54 Pxp_types.Value "Prop" -> Cic.Prop
55 | Pxp_types.Value "Set" -> Cic.Set
56 | Pxp_types.Value "Type" -> Cic.Type
57 | _ -> raise (IllFormedXml 2)
61 Pxp_types.Value n -> int_of_string n
62 | _ -> raise (IllFormedXml 3)
66 Pxp_types.Value s -> UriManager.uri_of_string s
67 | _ -> raise (IllFormedXml 4)
69 let string_of_xml_attr =
71 Pxp_types.Value s -> s
72 | _ -> raise (IllFormedXml 5)
74 let binder_of_xml_attr =
76 Pxp_types.Value s -> s
77 | _ -> raise (IllFormedXml 17)
80 (* the "interface" of the class linked to each node of the dom tree *)
82 class virtual cic_term =
85 (* fields and methods always required by markup *)
86 val mutable node = (None : cic_term Pxp_document.node option)
97 (* a method that returns the internal representation of the tree (term) *)
98 (* rooted in this node *)
99 method virtual to_cic_term :
100 (UriManager.uri * Cic.annterm) list -> Cic.annterm
104 (* the class of the objects linked to nodes that are not roots of cic terms *)
105 class eltype_not_of_cic =
110 method to_cic_term _ = raise (IllFormedXml 6)
114 (* the class of the objects linked to nodes whose content is a cic term *)
115 (* (syntactic sugar xml entities) e.g. <type> ... </type> *)
116 class eltype_transparent =
121 method to_cic_term exp_named_subst =
122 assert (exp_named_subst = []) ;
124 match n#sub_nodes with
125 [ t ] -> t#extension#to_cic_term []
126 | _ -> raise (IllFormedXml 7)
130 (* A class for each cic node type *)
137 method to_cic_term exp_named_subst =
138 assert (exp_named_subst = []) ;
140 let nofun = int_of_xml_attr (n#attribute "noFun")
141 and id = string_of_xml_attr (n#attribute "id")
143 let sons = n#sub_nodes in
146 f when f#node_type = Pxp_document.T_element "FixFunction" ->
147 let name = string_of_xml_attr (f#attribute "name")
148 and id = string_of_xml_attr (f#attribute "id")
149 and recindex = int_of_xml_attr (f#attribute "recIndex")
151 match f#sub_nodes with
153 t#node_type = Pxp_document.T_element "type" &&
154 b#node_type = Pxp_document.T_element "body" ->
155 (t#extension#to_cic_term [], b#extension#to_cic_term [])
156 | _ -> raise (IllFormedXml 14)
158 (id, name, recindex, ty, body)
159 | _ -> raise (IllFormedXml 13)
162 Cic.AFix (id, nofun, functions)
171 method to_cic_term exp_named_subst =
172 assert (exp_named_subst = []) ;
174 let nofun = int_of_xml_attr (n#attribute "noFun")
175 and id = string_of_xml_attr (n#attribute "id")
177 let sons = n#sub_nodes in
180 f when f#node_type = Pxp_document.T_element "CofixFunction" ->
181 let name = string_of_xml_attr (f#attribute "name")
182 and id = string_of_xml_attr (f#attribute "id")
184 match f#sub_nodes with
186 t#node_type = Pxp_document.T_element "type" &&
187 b#node_type = Pxp_document.T_element "body" ->
188 (t#extension#to_cic_term [], b#extension#to_cic_term [])
189 | _ -> raise (IllFormedXml 16)
192 | _ -> raise (IllFormedXml 15)
195 Cic.ACoFix (id, nofun, functions)
199 class eltype_implicit =
204 method to_cic_term exp_named_subst =
205 assert (exp_named_subst = []) ;
207 let id = string_of_xml_attr (n#attribute "id") in
217 method to_cic_term exp_named_subst =
218 assert (exp_named_subst = []) ;
220 let value = int_of_xml_attr (n#attribute "value")
221 and binder = binder_of_xml_attr (n#attribute "binder")
222 and id = string_of_xml_attr (n#attribute "id")
223 and idref = string_of_xml_attr (n#attribute "idref") in
224 Cic.ARel (id,idref,value,binder)
233 method to_cic_term exp_named_subst =
234 assert (exp_named_subst = []) ;
236 let value = int_of_xml_attr (n#attribute "no")
237 and id = string_of_xml_attr (n#attribute "id")
240 let sons = n#sub_nodes in
242 (function substitution ->
243 match substitution#sub_nodes with
245 | [he] -> Some (he#extension#to_cic_term [])
246 | _ -> raise (IllFormedXml 20)
249 Cic.AMeta (id,value,local_context)
258 method to_cic_term exp_named_subst =
259 assert (exp_named_subst = []) ;
261 let uri = uri_of_xml_attr (n#attribute "uri")
262 and xid = string_of_xml_attr (n#attribute "id") in
263 (*CSC: BIG BUG: [] MUST BE REPLACED WITH THE PARSED EXPLICIT NAMED SUBSTITUTION *)
264 Cic.AVar (xid,uri,[])
273 method to_cic_term exp_named_subst =
274 assert (exp_named_subst = []) ;
276 let children = n#sub_nodes
277 and id = string_of_xml_attr (n#attribute "id") in
278 if List.length children < 2 then raise (IllFormedXml 8)
281 (id,List.map (fun x -> x#extension#to_cic_term []) children)
290 method to_cic_term exp_named_subst =
291 assert (exp_named_subst = []) ;
293 let sons = n#sub_nodes
294 and id = string_of_xml_attr (n#attribute "id") in
297 te#node_type = Pxp_document.T_element "term" &&
298 ty#node_type = Pxp_document.T_element "type" ->
299 let term = te#extension#to_cic_term []
300 and typ = ty#extension#to_cic_term [] in
301 Cic.ACast (id,term,typ)
302 | _ -> raise (IllFormedXml 9)
311 method to_cic_term exp_named_subst =
312 assert (exp_named_subst = []) ;
314 let sort = cic_sort_of_xml_attr (n#attribute "value")
315 and id = string_of_xml_attr (n#attribute "id") in
325 method to_cic_term exp_named_subst =
326 let module U = UriManager in
328 let value = uri_of_xml_attr (n#attribute "uri")
329 and id = string_of_xml_attr (n#attribute "id") in
330 Cic.AConst (id,value, exp_named_subst)
334 class eltype_mutind =
339 method to_cic_term exp_named_subst =
340 let module U = UriManager in
342 let name = uri_of_xml_attr (n#attribute "uri")
343 and noType = int_of_xml_attr (n#attribute "noType")
344 and id = string_of_xml_attr (n#attribute "id") in
346 (id,name, noType, exp_named_subst)
350 class eltype_mutconstruct =
355 method to_cic_term exp_named_subst =
356 let module U = UriManager in
358 let name = uri_of_xml_attr (n#attribute "uri")
359 and noType = int_of_xml_attr (n#attribute "noType")
360 and noConstr = int_of_xml_attr (n#attribute "noConstr")
361 and id = string_of_xml_attr (n#attribute "id") in
363 (id, name, noType, noConstr, exp_named_subst)
372 method to_cic_term exp_named_subst =
373 assert (exp_named_subst = []) ;
375 let sons = n#sub_nodes in
376 let rec get_decls_and_target =
378 [t] when t#node_type = Pxp_document.T_element "target" ->
379 [],t#extension#to_cic_term []
380 | s::tl when s#node_type = Pxp_document.T_element "decl" ->
381 let decls,target = get_decls_and_target tl in
382 let id = string_of_xml_attr (s#attribute "id") in
383 let binder = cic_attr_of_xml_attr (s#attribute "binder") in
384 (id,binder,s#extension#to_cic_term [])::decls, target
385 | _ -> raise (IllFormedXml 10)
387 let decls,target = get_decls_and_target sons in
389 (fun (id,b,s) t -> Cic.AProd (id,b,s,t))
394 class eltype_mutcase =
399 method to_cic_term exp_named_subst =
400 assert (exp_named_subst = []) ;
401 let module U = UriManager in
403 let sons = n#sub_nodes
404 and id = string_of_xml_attr (n#attribute "id") in
406 ty::te::patterns when
407 ty#node_type = Pxp_document.T_element "patternsType" &&
408 te#node_type = Pxp_document.T_element "inductiveTerm" ->
409 let ci = uri_of_xml_attr (n#attribute "uriType")
410 and typeno = int_of_xml_attr (n#attribute "noType")
411 and inductiveType = ty#extension#to_cic_term []
412 and inductiveTerm = te#extension#to_cic_term []
414 List.map (fun x -> x#extension#to_cic_term []) patterns
416 Cic.AMutCase (id,ci, typeno,inductiveType,inductiveTerm,lpattern)
417 | _ -> raise (IllFormedXml 11)
421 class eltype_lambda =
426 method to_cic_term exp_named_subst =
427 assert (exp_named_subst = []) ;
429 let sons = n#sub_nodes in
430 let rec get_decls_and_target =
432 [t] when t#node_type = Pxp_document.T_element "target" ->
433 [],t#extension#to_cic_term []
434 | s::tl when s#node_type = Pxp_document.T_element "decl" ->
435 let decls,target = get_decls_and_target tl in
436 let id = string_of_xml_attr (s#attribute "id") in
437 let binder = cic_attr_of_xml_attr (s#attribute "binder") in
438 (id,binder,s#extension#to_cic_term [])::decls, target
439 | _ -> raise (IllFormedXml 12)
441 let decls,target = get_decls_and_target sons in
443 (fun (id,b,s) t -> Cic.ALambda (id,b,s,t))
453 method to_cic_term exp_named_subst =
454 assert (exp_named_subst = []) ;
456 let sons = n#sub_nodes in
457 let rec get_defs_and_target =
459 [t] when t#node_type = Pxp_document.T_element "target" ->
460 [],t#extension#to_cic_term []
461 | s::tl when s#node_type = Pxp_document.T_element "def" ->
462 let defs,target = get_defs_and_target tl in
463 let id = string_of_xml_attr (s#attribute "id") in
464 let binder = cic_attr_of_xml_attr (s#attribute "binder") in
465 (id,binder,s#extension#to_cic_term [])::defs, target
466 | _ -> raise (IllFormedXml 12)
468 let defs,target = get_defs_and_target sons in
470 (fun (id,b,s) t -> Cic.ALetIn (id,b,s,t))
475 class eltype_instantiate =
480 method to_cic_term exp_named_subst =
481 assert (exp_named_subst = []) ;
483 (* CSC: this optional attribute should be parsed and reflected in Cic.annterm
484 and id = string_of_xml_attr (n#attribute "id")
486 match n#sub_nodes with
489 UriManager.buri_of_uri (uri_of_xml_attr (t#attribute "uri")) in
490 let exp_named_subst =
493 n when n#node_type = Pxp_document.T_element "arg" ->
494 let relUri = string_of_xml_attr (n#attribute "relUri") in
495 let uri = UriManager.uri_of_string (baseUri ^ "/" ^ relUri) in
497 match n#sub_nodes with
498 [ t ] -> t#extension#to_cic_term []
499 | _ -> raise (IllFormedXml 7)
502 | _ -> raise (IllFormedXml 7)
505 t#extension#to_cic_term exp_named_subst
506 | _ -> raise (IllFormedXml 7)
511 (* The definition of domspec, an hashtable that maps each node type to the *)
512 (* object that must be linked to it. Used by markup. *)
515 let module D = Pxp_document in
516 D.make_spec_from_alist
517 ~data_exemplar: (new D.data_impl (new eltype_not_of_cic))
518 ~default_element_exemplar: (new D.element_impl (new eltype_not_of_cic))
520 [ "REL", (new D.element_impl (new eltype_rel)) ;
521 "VAR", (new D.element_impl (new eltype_var)) ;
522 "META", (new D.element_impl (new eltype_meta)) ;
523 "SORT", (new D.element_impl (new eltype_sort)) ;
524 "IMPLICIT", (new D.element_impl (new eltype_implicit)) ;
525 "CAST", (new D.element_impl (new eltype_cast)) ;
526 "PROD", (new D.element_impl (new eltype_prod)) ;
527 "LAMBDA", (new D.element_impl (new eltype_lambda)) ;
528 "LETIN", (new D.element_impl (new eltype_letin)) ;
529 "APPLY", (new D.element_impl (new eltype_apply)) ;
530 "CONST", (new D.element_impl (new eltype_const)) ;
531 "MUTIND", (new D.element_impl (new eltype_mutind)) ;
532 "MUTCONSTRUCT", (new D.element_impl (new eltype_mutconstruct)) ;
533 "MUTCASE", (new D.element_impl (new eltype_mutcase)) ;
534 "FIX", (new D.element_impl (new eltype_fix)) ;
535 "COFIX", (new D.element_impl (new eltype_cofix)) ;
536 "instantiate", (new D.element_impl (new eltype_instantiate)) ;
537 "arity", (new D.element_impl (new eltype_transparent)) ;
538 "term", (new D.element_impl (new eltype_transparent)) ;
539 "type", (new D.element_impl (new eltype_transparent)) ;
540 "body", (new D.element_impl (new eltype_transparent)) ;
541 "decl", (new D.element_impl (new eltype_transparent)) ;
542 "def", (new D.element_impl (new eltype_transparent)) ;
543 "target", (new D.element_impl (new eltype_transparent)) ;
544 "letintarget", (new D.element_impl (new eltype_transparent)) ;
545 "patternsType", (new D.element_impl (new eltype_transparent)) ;
546 "inductiveTerm", (new D.element_impl (new eltype_transparent)) ;
547 "pattern", (new D.element_impl (new eltype_transparent))