]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/cic2Xml.ml
Added Coer/Coercions print_kind
[helm.git] / helm / ocaml / cic_transformations / cic2Xml.ml
1 (* Copyright (C) 2000-2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (*CSC codice cut & paste da cicPp e xmlcommand *)
27
28 exception ImpossiblePossible;;
29 exception NotImplemented;;
30
31 let dtdname ~ask_dtd_to_the_getter dtd =
32  if ask_dtd_to_the_getter then
33   Helm_registry.get "getter.url" ^ "getdtd?uri=" ^ dtd
34  else
35   "http://mowgli.cs.unibo.it/dtd/" ^ dtd
36 ;;
37
38 let param_attribute_of_params params =
39  String.concat " " (List.map UriManager.string_of_uri params)
40 ;;
41
42 (*CSC ottimizzazione: al posto di curi cdepth (vedi codice) *)
43 let print_term ~ids_to_inner_sorts =
44  let rec aux =
45   let module C = Cic in
46   let module X = Xml in
47   let module U = UriManager in
48     function
49        C.ARel (id,idref,n,b) ->
50         let sort = Hashtbl.find ids_to_inner_sorts id in
51          X.xml_empty "REL"
52           [None,"value",(string_of_int n) ; None,"binder",b ; None,"id",id ;
53            None,"idref",idref ; None,"sort",sort]
54      | C.AVar (id,uri,exp_named_subst) ->
55         let sort = Hashtbl.find ids_to_inner_sorts id in
56          aux_subst uri
57           (X.xml_empty "VAR"
58             [None,"uri",U.string_of_uri uri;None,"id",id;None,"sort",sort])
59           exp_named_subst
60      | C.AMeta (id,n,l) ->
61         let sort = Hashtbl.find ids_to_inner_sorts id in
62          X.xml_nempty "META"
63           [None,"no",(string_of_int n) ; None,"id",id ; None,"sort",sort]
64           (List.fold_left
65             (fun i t ->
66               match t with
67                  Some t' ->
68                   [< i ; X.xml_nempty "substitution" [] (aux t') >]
69                | None ->
70                   [< i ; X.xml_empty "substitution" [] >]
71             ) [< >] l)
72      | C.ASort (id,s) ->
73         let string_of_sort =
74          function
75             C.Prop  -> "Prop"
76           | C.Set   -> "Set"
77           | C.Type _ -> "Type" (* TASSI *)
78           | C.CProp -> "CProp"
79         in
80          X.xml_empty "SORT" [None,"value",(string_of_sort s) ; None,"id",id]
81      | C.AImplicit _ -> raise NotImplemented
82      | C.AProd (last_id,_,_,_) as prods ->
83         let rec eat_prods =
84          function
85             C.AProd (id,n,s,t) ->
86              let prods,t' = eat_prods t in
87               (id,n,s)::prods,t'
88           | t -> [],t
89         in
90          let prods,t = eat_prods prods in
91           let sort = Hashtbl.find ids_to_inner_sorts last_id in
92            X.xml_nempty "PROD" [None,"type",sort]
93             [< List.fold_left
94                 (fun i (id,binder,s) ->
95                   let sort =
96                    Hashtbl.find ids_to_inner_sorts (Cic2acic.source_id_of_id id)
97                   in
98                    let attrs =
99                     (None,"id",id)::(None,"type",sort)::
100                     match binder with
101                        C.Anonymous -> []
102                      | C.Name b -> [None,"binder",b]
103                    in
104                     [< i ; X.xml_nempty "decl" attrs (aux s) >]
105                 ) [< >] prods ;
106                X.xml_nempty "target" [] (aux t)
107             >]
108      | C.ACast (id,v,t) ->
109         let sort = Hashtbl.find ids_to_inner_sorts id in
110          X.xml_nempty "CAST" [None,"id",id ; None,"sort",sort]
111           [< X.xml_nempty "term" [] (aux v) ;
112              X.xml_nempty "type" [] (aux t)
113           >]
114      | C.ALambda (last_id,_,_,_) as lambdas ->
115         let rec eat_lambdas =
116          function
117             C.ALambda (id,n,s,t) ->
118              let lambdas,t' = eat_lambdas t in
119               (id,n,s)::lambdas,t'
120           | t -> [],t
121         in
122          let lambdas,t = eat_lambdas lambdas in
123           let sort = Hashtbl.find ids_to_inner_sorts last_id in
124            X.xml_nempty "LAMBDA" [None,"sort",sort]
125             [< List.fold_left
126                 (fun i (id,binder,s) ->
127                   let sort =
128                    Hashtbl.find ids_to_inner_sorts (Cic2acic.source_id_of_id id)
129                   in
130                    let attrs =
131                     (None,"id",id)::(None,"type",sort)::
132                     match binder with
133                        C.Anonymous -> []
134                      | C.Name b -> [None,"binder",b]
135                    in
136                     [< i ; X.xml_nempty "decl" attrs (aux s) >]
137                 ) [< >] lambdas ;
138                X.xml_nempty "target" [] (aux t)
139             >]
140      | C.ALetIn (xid,C.Anonymous,s,t) ->
141        assert false
142      | C.ALetIn (last_id,C.Name _,_,_) as letins ->
143         let rec eat_letins =
144          function
145             C.ALetIn (id,n,s,t) ->
146              let letins,t' = eat_letins t in
147               (id,n,s)::letins,t'
148           | t -> [],t
149         in
150          let letins,t = eat_letins letins in
151           let sort = Hashtbl.find ids_to_inner_sorts last_id in
152            X.xml_nempty "LETIN" [None,"sort",sort]
153             [< List.fold_left
154                 (fun i (id,binder,s) ->
155                   let sort = Hashtbl.find ids_to_inner_sorts id in
156                    let attrs =
157                     (None,"id",id)::(None,"sort",sort)::
158                     match binder with
159                        C.Anonymous -> []
160                      | C.Name b -> [None,"binder",b]
161                    in
162                     [< i ; X.xml_nempty "def" attrs (aux s) >]
163                 ) [< >] letins ;
164                X.xml_nempty "target" [] (aux t)
165             >]
166      | C.AAppl (id,li) ->
167         let sort = Hashtbl.find ids_to_inner_sorts id in
168          X.xml_nempty "APPLY" [None,"id",id ; None,"sort",sort]
169           [< (List.fold_right (fun x i -> [< (aux x) ; i >]) li [<>])
170           >]
171      | C.AConst (id,uri,exp_named_subst) ->
172         let sort = Hashtbl.find ids_to_inner_sorts id in
173          aux_subst uri
174           (X.xml_empty "CONST"
175             [None,"uri",(U.string_of_uri uri) ; None,"id",id ; None,"sort",sort]
176           ) exp_named_subst
177      | C.AMutInd (id,uri,i,exp_named_subst) ->
178         aux_subst uri
179          (X.xml_empty "MUTIND"
180            [None, "uri", (U.string_of_uri uri) ;
181             None, "noType", (string_of_int i) ;
182             None, "id", id]
183          ) exp_named_subst
184      | C.AMutConstruct (id,uri,i,j,exp_named_subst) ->
185         let sort = Hashtbl.find ids_to_inner_sorts id in
186          aux_subst uri
187           (X.xml_empty "MUTCONSTRUCT"
188             [None,"uri", (U.string_of_uri uri) ;
189              None,"noType",(string_of_int i) ;
190              None,"noConstr",(string_of_int j) ;
191              None,"id",id ; None,"sort",sort]
192           ) exp_named_subst
193      | C.AMutCase (id,uri,typeno,ty,te,patterns) ->
194         let sort = Hashtbl.find ids_to_inner_sorts id in
195          X.xml_nempty "MUTCASE"
196           [None,"uriType",(U.string_of_uri uri) ;
197            None,"noType", (string_of_int typeno) ;
198            None,"id", id ; None,"sort",sort]
199           [< X.xml_nempty "patternsType" [] [< (aux ty) >] ;
200              X.xml_nempty "inductiveTerm" [] [< (aux te) >] ;
201              List.fold_right
202               (fun x i -> [< X.xml_nempty "pattern" [] [< aux x >] ; i>])
203               patterns [<>]
204           >]
205      | C.AFix (id, no, funs) ->
206         let sort = Hashtbl.find ids_to_inner_sorts id in
207          X.xml_nempty "FIX"
208           [None,"noFun", (string_of_int no) ; None,"id",id ; None,"sort",sort]
209           [< List.fold_right
210               (fun (id,fi,ai,ti,bi) i ->
211                 [< X.xml_nempty "FixFunction"
212                     [None,"id",id ; None,"name", fi ;
213                      None,"recIndex", (string_of_int ai)]
214                     [< X.xml_nempty "type" [] [< aux ti >] ;
215                        X.xml_nempty "body" [] [< aux bi >]
216                     >] ;
217                    i
218                 >]
219               ) funs [<>]
220           >]
221      | C.ACoFix (id,no,funs) ->
222         let sort = Hashtbl.find ids_to_inner_sorts id in
223          X.xml_nempty "COFIX"
224           [None,"noFun", (string_of_int no) ; None,"id",id ; None,"sort",sort]
225           [< List.fold_right
226               (fun (id,fi,ti,bi) i ->
227                 [< X.xml_nempty "CofixFunction" [None,"id",id ; None,"name", fi]
228                     [< X.xml_nempty "type" [] [< aux ti >] ;
229                        X.xml_nempty "body" [] [< aux bi >]
230                     >] ;
231                    i
232                 >]
233               ) funs [<>]
234           >]
235  and aux_subst buri target subst =
236 (*CSC: I have now no way to assign an ID to the explicit named substitution *)
237   let id = None in
238    if subst = [] then
239     target
240    else
241     Xml.xml_nempty "instantiate"
242      (match id with None -> [] | Some id -> [None,"id",id])
243      [< target ;
244         List.fold_left
245          (fun i (uri,arg) ->
246            let relUri =
247             let buri_frags =
248              Str.split (Str.regexp "/") (UriManager.string_of_uri buri) in
249             let uri_frags = 
250              Str.split (Str.regexp "/") (UriManager.string_of_uri uri)  in
251              let rec find_relUri buri_frags uri_frags =
252               match buri_frags,uri_frags with
253                  [_], _ -> String.concat "/" uri_frags
254                | he1::tl1, he2::tl2 ->
255                   assert (he1 = he2) ;
256                   find_relUri tl1 tl2
257                | _,_ -> assert false (* uri is not relative to buri *)
258              in
259               find_relUri buri_frags uri_frags
260            in
261             [< i ; Xml.xml_nempty "arg" [None,"relUri", relUri] (aux arg) >]
262          ) [<>] subst
263      >]
264   in
265    aux
266 ;;
267
268   (* TODO ZACK implement attributes pretty printing *)
269 let xml_of_attrs attributes = [< >]
270
271 let print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter obj =
272  let module C = Cic in
273  let module X = Xml in
274  let module U = UriManager in
275   let dtdname = dtdname ~ask_dtd_to_the_getter "cic.dtd" in
276    match obj with
277        C.ACurrentProof (id,idbody,n,conjectures,bo,ty,params,obj_attrs) ->
278         let params' = param_attribute_of_params params in
279         let xml_attrs = xml_of_attrs obj_attrs in
280         let xml_for_current_proof_body =
281 (*CSC: Should the CurrentProof also have the list of variables it depends on? *)
282 (*CSC: I think so. Not implemented yet.                                       *)
283          X.xml_nempty "CurrentProof"
284           [None,"of",UriManager.string_of_uri uri ; None,"id", id]
285           [< xml_attrs;
286             List.fold_left
287               (fun i (cid,n,canonical_context,t) ->
288                 [< i ;
289                    X.xml_nempty "Conjecture"
290                     [None,"id",cid ; None,"no",(string_of_int n)]
291                     [< List.fold_left
292                         (fun i (hid,t) ->
293                           [< (match t with
294                                  Some (n,C.ADecl t) ->
295                                   X.xml_nempty "Decl"
296                                    (match n with
297                                        C.Name n' ->
298                                         [None,"id",hid;None,"name",n']
299                                      | C.Anonymous -> [None,"id",hid])
300                                    (print_term ids_to_inner_sorts t)
301                                | Some (n,C.ADef t) ->
302                                   X.xml_nempty "Def"
303                                    (match n with
304                                        C.Name n' ->
305                                         [None,"id",hid;None,"name",n']
306                                      | C.Anonymous -> [None,"id",hid])
307                                    (print_term ids_to_inner_sorts t)
308                               | None -> X.xml_empty "Hidden" [None,"id",hid]
309                              ) ;
310                              i
311                           >]
312                         ) [< >] canonical_context ;
313                        X.xml_nempty "Goal" []
314                         (print_term ids_to_inner_sorts t)
315                     >]
316                 >])
317               [< >] conjectures ;
318              X.xml_nempty "body" [] (print_term ids_to_inner_sorts bo) >]
319         in
320         let xml_for_current_proof_type =
321          X.xml_nempty "ConstantType"
322           [None,"name",n ; None,"params",params' ; None,"id", id]
323           (print_term ids_to_inner_sorts ty)
324         in
325         let xmlbo =
326          [< X.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
327             X.xml_cdata ("<!DOCTYPE CurrentProof SYSTEM \""^ dtdname ^ "\">\n");
328             xml_for_current_proof_body
329          >] in
330         let xmlty =
331          [< X.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
332             X.xml_cdata ("<!DOCTYPE ConstantType SYSTEM \""^ dtdname ^ "\">\n");
333             xml_for_current_proof_type
334          >]
335         in
336          xmlty, Some xmlbo
337      | C.AConstant (id,idbody,n,bo,ty,params,obj_attrs) ->
338         let params' = param_attribute_of_params params in
339         let xml_attrs = xml_of_attrs obj_attrs in
340         let xmlbo =
341          match bo with
342             None -> None
343           | Some bo ->
344              Some
345               [< X.xml_cdata
346                   "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
347                  X.xml_cdata
348                   ("<!DOCTYPE ConstantBody SYSTEM \"" ^ dtdname ^ "\">\n") ;
349                  X.xml_nempty "ConstantBody"
350                   [None,"for",UriManager.string_of_uri uri ;
351                    None,"params",params' ; None,"id", id]
352                   [< print_term ids_to_inner_sorts bo >]
353               >]
354         in
355         let xmlty =
356          [< X.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
357             X.xml_cdata ("<!DOCTYPE ConstantType SYSTEM \""^ dtdname ^ "\">\n");
358              X.xml_nempty "ConstantType"
359               [None,"name",n ; None,"params",params' ; None,"id", id]
360               [< xml_attrs; print_term ids_to_inner_sorts ty >]
361          >]
362         in
363          xmlty, xmlbo
364      | C.AVariable (id,n,bo,ty,params,obj_attrs) ->
365         let params' = param_attribute_of_params params in
366         let xml_attrs = xml_of_attrs obj_attrs in
367         let xmlbo =
368          match bo with
369             None -> [< >]
370           | Some bo ->
371              X.xml_nempty "body" [] [< print_term ids_to_inner_sorts bo >]
372         in
373         let aobj =
374          [< X.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
375             X.xml_cdata ("<!DOCTYPE Variable SYSTEM \"" ^ dtdname ^ "\">\n");
376              X.xml_nempty "Variable"
377               [None,"name",n ; None,"params",params' ; None,"id", id]
378               [< xml_attrs; xmlbo;
379                  X.xml_nempty "type" [] (print_term ids_to_inner_sorts ty)
380               >]
381          >]
382         in
383          aobj, None
384      | C.AInductiveDefinition (id,tys,params,nparams,obj_attrs) ->
385         let params' = param_attribute_of_params params in
386         let xml_attrs = xml_of_attrs obj_attrs in
387          [< X.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
388             X.xml_cdata
389              ("<!DOCTYPE InductiveDefinition SYSTEM \"" ^ dtdname ^ "\">\n") ;
390             X.xml_nempty "InductiveDefinition"
391              [None,"noParams",string_of_int nparams ;
392               None,"id",id ;
393               None,"params",params']
394              [< xml_attrs;
395                 (List.fold_left
396                   (fun i (id,typename,finite,arity,cons) ->
397                     [< i ;
398                        X.xml_nempty "InductiveType"
399                         [None,"id",id ; None,"name",typename ;
400                          None,"inductive",(string_of_bool finite)
401                         ]
402                         [< X.xml_nempty "arity" []
403                             (print_term ids_to_inner_sorts arity) ;
404                            (List.fold_left
405                             (fun i (name,lc) ->
406                               [< i ;
407                                  X.xml_nempty "Constructor"
408                                   [None,"name",name]
409                                   (print_term ids_to_inner_sorts lc)
410                               >]) [<>] cons
411                            )
412                         >]
413                     >]
414                   ) [< >] tys
415                 )
416              >]
417          >], None
418 ;;
419
420 let
421  print_inner_types curi ~ids_to_inner_sorts ~ids_to_inner_types
422   ~ask_dtd_to_the_getter
423 =
424  let module C2A = Cic2acic in
425  let module X = Xml in
426   let dtdname = dtdname ~ask_dtd_to_the_getter "cictypes.dtd" in
427    [< X.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
428       X.xml_cdata
429        ("<!DOCTYPE InnerTypes SYSTEM \"" ^ dtdname ^ "\">\n") ;
430       X.xml_nempty "InnerTypes" [None,"of",UriManager.string_of_uri curi]
431        (Hashtbl.fold
432          (fun id {C2A.annsynthesized = synty ; C2A.annexpected = expty} x ->
433            [< x ;
434               X.xml_nempty "TYPE" [None,"of",id]
435                [< X.xml_nempty "synthesized" []
436                  [< print_term ids_to_inner_sorts synty >] ;
437                  match expty with
438                    None -> [<>]
439                  | Some expty' -> X.xml_nempty "expected" [] [< print_term ids_to_inner_sorts expty' >]
440                >]
441            >]
442          ) ids_to_inner_types [<>]
443        )
444    >]
445 ;;