]> matita.cs.unibo.it Git - helm.git/blob - matita/applyTransformation.ml
Added toggle for enabling/disabling the conversion from multibyte unicode
[helm.git] / matita / applyTransformation.ml
1 (* Copyright (C) 2000-2002, 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 (***************************************************************************)
27 (*                                                                         *)
28 (*                               PROJECT HELM                              *)
29 (*                                                                         *)
30 (*                   Andrea Asperti <asperti@cs.unibo.it>                  *)
31 (*                                21/11/2003                               *)
32 (*                                                                         *)
33 (*                                                                         *)
34 (***************************************************************************)
35
36 (* $Id$ *)
37
38 let mpres_document pres_box =
39   Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
40
41 let mml_of_cic_sequent metasenv sequent =
42   let unsh_sequent,(asequent,ids_to_terms,
43     ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
44   =
45     Cic2acic.asequent_of_sequent metasenv sequent
46   in
47   let content_sequent = Acic2content.map_sequent asequent in 
48   let pres_sequent = 
49     (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
50   in
51   let xmlpres = mpres_document pres_sequent in
52   (Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres,
53    unsh_sequent,
54    (asequent,
55     (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
56
57 let mml_of_cic_object obj =
58   let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
59     ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses)
60   =
61     Cic2acic.acic_object_of_cic_object obj
62   in
63   let content = 
64     Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
65   in
66   let pres = Content2pres.content2pres ~ids_to_inner_sorts content in
67   let xmlpres = mpres_document pres in
68   let mathml = Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres in
69   (mathml,(annobj,
70    (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
71   ids_to_inner_sorts,ids_to_inner_types)))
72
73 let txt_of_cic_sequent ?map_unicode_to_tex size metasenv sequent =
74   let unsh_sequent,(asequent,ids_to_terms,
75     ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
76   =
77     Cic2acic.asequent_of_sequent metasenv sequent
78   in
79   let content_sequent = Acic2content.map_sequent asequent in 
80   let pres_sequent = 
81    CicNotationPres.mpres_of_box
82     (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
83   in
84   BoxPp.render_to_string ?map_unicode_to_tex
85     (function x::_ -> x | _ -> assert false) size pres_sequent
86
87 let txt_of_cic_sequent_conclusion ?map_unicode_to_tex size metasenv sequent =
88   let _,(asequent,_,_,ids_to_inner_sorts,_) = 
89     Cic2acic.asequent_of_sequent metasenv sequent 
90   in
91   let _,_,_,t = Acic2content.map_sequent asequent in 
92   let t, ids_to_uris = TermAcicContent.ast_of_acic ids_to_inner_sorts t in
93   let t = TermContentPres.pp_ast t in
94   let t = CicNotationPres.render ids_to_uris t in
95   BoxPp.render_to_string ?map_unicode_to_tex
96     (function x::_ -> x | _ -> assert false) size t
97
98 let txt_of_cic_term ?map_unicode_to_tex size metasenv context t = 
99   let fake_sequent = (-1,context,t) in
100   txt_of_cic_sequent_conclusion ?map_unicode_to_tex size metasenv fake_sequent 
101 ;;
102
103 ignore (
104  CicMetaSubst.set_ppterm_in_context
105   (fun ~metasenv subst term context ->
106     try
107      let context' = CicMetaSubst.apply_subst_context subst context in
108      let term' = CicMetaSubst.apply_subst subst term in
109      let res = txt_of_cic_term 30 metasenv context' term' in
110       if String.contains res '\n' then
111        "\n" ^ res ^ "\n"
112       else
113        res
114     with
115        Sys.Break as exn -> raise exn
116      | exn ->
117         "[[ Exception raised during pretty-printing: " ^
118          (try
119            Printexc.to_string exn
120           with
121              Sys.Break as exn -> raise exn
122            | _ -> "<<exception raised pretty-printing the exception>>"
123          ) ^ " ]] " ^
124         (CicMetaSubst.use_low_level_ppterm_in_context := true;
125          try
126           let res =
127            CicMetaSubst.ppterm_in_context ~metasenv subst term context
128           in
129            CicMetaSubst.use_low_level_ppterm_in_context := false;
130            res
131          with
132           exc -> 
133            CicMetaSubst.use_low_level_ppterm_in_context := false;
134            raise exc))
135 );;
136
137 (****************************************************************************)
138 (* txt_of_cic_object: IMPROVE ME *)
139
140 let remove_closed_substs s =
141     Pcre.replace ~pat:"{...}" ~templ:"" s
142
143 let term2pres ?map_unicode_to_tex n ids_to_inner_sorts annterm = 
144    let ast, ids_to_uris = 
145       TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
146    in
147    let bobj =
148       CicNotationPres.box_of_mpres (
149          CicNotationPres.render ~prec:90 ids_to_uris 
150             (TermContentPres.pp_ast ast)
151       )
152    in
153    let render = function _::x::_ -> x | _ -> assert false in
154    let mpres = CicNotationPres.mpres_of_box bobj in
155    let s = BoxPp.render_to_string ?map_unicode_to_tex render n mpres in
156    remove_closed_substs s
157
158 let txt_of_cic_object ?map_unicode_to_tex n style prefix obj =
159   let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ = 
160      try Cic2acic.acic_object_of_cic_object obj
161      with e -> 
162         let msg = "Cic2ACic: " ^ Printexc.to_string e in
163         failwith msg
164   in
165   match style with
166      | GrafiteAst.Declarative      ->
167         let cobj = Acic2content.annobj2content ids_to_inner_sorts ids_to_inner_types aobj in
168         let bobj = Content2pres.content2pres ids_to_inner_sorts cobj in
169         remove_closed_substs ("\n\n" ^
170            BoxPp.render_to_string ?map_unicode_to_tex
171             (function _::x::_ -> x | _ -> assert false) n
172             (CicNotationPres.mpres_of_box bobj)
173         )
174      | GrafiteAst.Procedural depth ->
175         let term_pp = term2pres (n - 8) ids_to_inner_sorts in
176         let lazy_term_pp = term_pp in
177         let obj_pp = CicNotationPp.pp_obj term_pp in
178         let aux = GrafiteAstPp.pp_statement ~term_pp ~lazy_term_pp ~obj_pp in
179         let script = Acic2Procedural.acic2procedural 
180            ~ids_to_inner_sorts ~ids_to_inner_types ?depth prefix aobj in
181         "\n\n" ^ String.concat "" (List.map aux script)
182
183 let txt_of_inline_macro style suri prefix =
184    let dbd = LibraryDb.instance () in   
185    let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
186    let map uri =
187       try txt_of_cic_object 78 style prefix (* FG: mi pare meglio 78 *)
188                             (fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri))
189       with
190          | e -> 
191             Printf.sprintf "\n(* ERRORE IN STAMPA DI %s\nEXCEPTION: %s *)\n" 
192             (UriManager.string_of_uri uri) (Printexc.to_string e)
193    in
194    String.concat "\n\n" (List.map map sorted_uris)