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