]> matita.cs.unibo.it Git - helm.git/blob - matita/applyTransformation.ml
tagged 0.5.0-rc1
[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 module G = GrafiteAst
39
40 let mpres_document pres_box =
41   Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
42
43 let mml_of_cic_sequent metasenv sequent =
44   let unsh_sequent,(asequent,ids_to_terms,
45     ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
46   =
47     Cic2acic.asequent_of_sequent metasenv sequent
48   in
49   let content_sequent = Acic2content.map_sequent asequent in 
50   let pres_sequent = 
51    Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent in
52   let xmlpres = mpres_document pres_sequent in
53   (Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres,
54    unsh_sequent,
55    (asequent,
56     (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
57
58 let mml_of_cic_object obj =
59   let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
60     ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses)
61   =
62     Cic2acic.acic_object_of_cic_object obj
63   in
64   let content = 
65     Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
66   in
67   let pres = Content2pres.content2pres ~ids_to_inner_sorts content in
68   let xmlpres = mpres_document pres in
69   let mathml = Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres in
70   (mathml,(annobj,
71    (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
72   ids_to_inner_sorts,ids_to_inner_types)))
73
74 let txt_of_cic_sequent ~map_unicode_to_tex size metasenv sequent =
75   let unsh_sequent,(asequent,ids_to_terms,
76     ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
77   =
78     Cic2acic.asequent_of_sequent metasenv sequent
79   in
80   let content_sequent = Acic2content.map_sequent asequent in 
81   let pres_sequent = 
82    CicNotationPres.mpres_of_box
83     (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
84   in
85   BoxPp.render_to_string ~map_unicode_to_tex
86     (function x::_ -> x | _ -> assert false) size pres_sequent
87
88 let txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type size
89  metasenv sequent =
90   let _,(asequent,_,_,ids_to_inner_sorts,_) = 
91     Cic2acic.asequent_of_sequent metasenv sequent 
92   in
93   let _,_,_,t = Acic2content.map_sequent asequent in 
94   let t, ids_to_uris =
95    TermAcicContent.ast_of_acic ~output_type ids_to_inner_sorts t in
96   let t = TermContentPres.pp_ast t in
97   let t = CicNotationPres.render ids_to_uris t in
98   BoxPp.render_to_string ~map_unicode_to_tex
99     (function x::_ -> x | _ -> assert false) size t
100
101 let txt_of_cic_term ~map_unicode_to_tex size metasenv context t = 
102  let fake_sequent = (-1,context,t) in
103   txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type:`Term size
104    metasenv fake_sequent 
105 ;;
106
107 ignore (
108  CicMetaSubst.set_ppterm_in_context
109   (fun ~metasenv subst term context ->
110     try
111      let context' = CicMetaSubst.apply_subst_context subst context in
112      let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
113      let term' = CicMetaSubst.apply_subst subst term in
114      let res =
115       txt_of_cic_term
116        ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
117        30 metasenv context' term' in
118       if String.contains res '\n' then
119        "\n" ^ res ^ "\n"
120       else
121        res
122     with
123        Sys.Break as exn -> raise exn
124      | exn ->
125         "[[ Exception raised during pretty-printing: " ^
126          (try
127            Printexc.to_string exn
128           with
129              Sys.Break as exn -> raise exn
130            | _ -> "<<exception raised pretty-printing the exception>>"
131          ) ^ " ]] " ^
132         (CicMetaSubst.use_low_level_ppterm_in_context := true;
133          try
134           let res =
135            CicMetaSubst.ppterm_in_context ~metasenv subst term context
136           in
137            CicMetaSubst.use_low_level_ppterm_in_context := false;
138            res
139          with
140           exc -> 
141            CicMetaSubst.use_low_level_ppterm_in_context := false;
142            raise exc))
143 );;
144
145 (****************************************************************************)
146 (* txt_of_cic_object: IMPROVE ME *)
147
148 let remove_closed_substs s =
149     Pcre.replace ~pat:"{...}" ~templ:"" s
150
151 let term2pres ~map_unicode_to_tex n ids_to_inner_sorts annterm = 
152    let ast, ids_to_uris = 
153     TermAcicContent.ast_of_acic ~output_type:`Term ids_to_inner_sorts annterm in
154    let bobj =
155       CicNotationPres.box_of_mpres (
156          CicNotationPres.render ~prec:90 ids_to_uris 
157             (TermContentPres.pp_ast ast)) in
158    let render = function _::x::_ -> x | _ -> assert false in
159    let mpres = CicNotationPres.mpres_of_box bobj in
160    let s = BoxPp.render_to_string ~map_unicode_to_tex render n mpres in
161    remove_closed_substs s
162
163 let txt_of_cic_object 
164  ~map_unicode_to_tex ?skip_thm_and_qed ?skip_initial_lambdas n style prefix obj 
165 =
166   let get_aobj obj = 
167      try   
168         let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
169             Cic2acic.acic_object_of_cic_object obj
170         in
171         aobj, ids_to_inner_sorts, ids_to_inner_types
172      with e -> 
173         let msg = "txt_of_cic_object: " ^ Printexc.to_string e in
174         failwith msg
175   in
176   match style with
177      | G.Declarative      ->
178         let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
179         let cobj = 
180           Acic2content.annobj2content 
181             ids_to_inner_sorts ids_to_inner_types aobj 
182         in
183         let bobj = 
184           Content2pres.content2pres 
185             ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts cobj 
186         in
187         remove_closed_substs ("\n\n" ^
188            BoxPp.render_to_string ~map_unicode_to_tex
189             (function _::x::_ -> x | _ -> assert false) n
190             (CicNotationPres.mpres_of_box bobj)
191         )
192      | G.Procedural depth ->
193         let obj = ProceduralOptimizer.optimize_obj obj in
194         let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
195         let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
196         let lazy_term_pp = term_pp in
197         let obj_pp = CicNotationPp.pp_obj term_pp in
198         let aux = GrafiteAstPp.pp_statement
199          ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
200         let script = 
201            Acic2Procedural.acic2procedural 
202               ~ids_to_inner_sorts ~ids_to_inner_types 
203               ?depth ?skip_thm_and_qed prefix aobj 
204   in
205         "\n\n" ^ String.concat "" (List.map aux script)
206
207 let txt_of_inline_uri ~map_unicode_to_tex style suri prefix =
208    let print_exc = function
209       | ProofEngineHelpers.Bad_pattern s as e ->
210            Printexc.to_string e ^ " " ^ Lazy.force s
211       | e -> Printexc.to_string e
212    in
213    let dbd = LibraryDb.instance () in   
214    let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
215    let map uri =
216       try 
217         txt_of_cic_object 
218           ~map_unicode_to_tex 78 style prefix
219           (fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri))
220       with
221          | e -> 
222             Printf.sprintf "\n(* ERRORE IN STAMPA DI %s\nEXCEPTION: %s *)\n" 
223             (UriManager.string_of_uri uri) (print_exc e)
224    in
225    String.concat "" (List.map map sorted_uris)
226
227 let txt_of_inline_macro ~map_unicode_to_tex style name prefix =
228    let suri = 
229       if Librarian.is_uri name then name else
230       let include_paths = 
231          Helm_registry.get_list Helm_registry.string "matita.includes"
232       in
233       let _, baseuri, _, _ = 
234          Librarian.baseuri_of_script ~include_paths name
235       in
236       baseuri
237    in
238    txt_of_inline_uri ~map_unicode_to_tex style suri prefix
239    
240