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