1 (* Copyright (C) 2000-2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (***************************************************************************)
30 (* Andrea Asperti <asperti@cs.unibo.it> *)
34 (***************************************************************************)
40 let mpres_document pres_box =
41 Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
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)
47 Cic2acic.asequent_of_sequent metasenv sequent
49 let content_sequent = Acic2content.map_sequent asequent in
51 (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
53 let xmlpres = mpres_document pres_sequent in
54 (Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres,
57 (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
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)
63 Cic2acic.acic_object_of_cic_object obj
66 Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
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
72 (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
73 ids_to_inner_sorts,ids_to_inner_types)))
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)
79 Cic2acic.asequent_of_sequent metasenv sequent
81 let content_sequent = Acic2content.map_sequent asequent in
83 CicNotationPres.mpres_of_box
84 (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
86 BoxPp.render_to_string ~map_unicode_to_tex
87 (function x::_ -> x | _ -> assert false) size pres_sequent
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
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
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
106 CicMetaSubst.set_ppterm_in_context
107 (fun ~metasenv subst term context ->
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
114 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
115 30 metasenv context' term' in
116 if String.contains res '\n' then
121 Sys.Break as exn -> raise exn
123 "[[ Exception raised during pretty-printing: " ^
125 Printexc.to_string exn
127 Sys.Break as exn -> raise exn
128 | _ -> "<<exception raised pretty-printing the exception>>"
130 (CicMetaSubst.use_low_level_ppterm_in_context := true;
133 CicMetaSubst.ppterm_in_context ~metasenv subst term context
135 CicMetaSubst.use_low_level_ppterm_in_context := false;
139 CicMetaSubst.use_low_level_ppterm_in_context := false;
143 (****************************************************************************)
144 (* txt_of_cic_object: IMPROVE ME *)
146 let remove_closed_substs s =
147 Pcre.replace ~pat:"{...}" ~templ:"" s
149 let term2pres ~map_unicode_to_tex n ids_to_inner_sorts annterm =
150 let ast, ids_to_uris =
151 TermAcicContent.ast_of_acic ids_to_inner_sorts annterm
154 CicNotationPres.box_of_mpres (
155 CicNotationPres.render ~prec:90 ids_to_uris
156 (TermContentPres.pp_ast ast)
159 let render = function _::x::_ -> x | _ -> assert false in
160 let mpres = CicNotationPres.mpres_of_box bobj in
161 let s = BoxPp.render_to_string ~map_unicode_to_tex render n mpres in
162 remove_closed_substs s
164 let txt_of_cic_object
165 ~map_unicode_to_tex ?skip_thm_and_qed ?skip_initial_lambdas n style prefix obj
169 let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
170 Cic2acic.acic_object_of_cic_object obj
172 aobj, ids_to_inner_sorts, ids_to_inner_types
174 let msg = "txt_of_cic_object: " ^ Printexc.to_string e in
179 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
181 Acic2content.annobj2content
182 ids_to_inner_sorts ids_to_inner_types aobj
185 Content2pres.content2pres
186 ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts cobj
188 remove_closed_substs ("\n\n" ^
189 BoxPp.render_to_string ~map_unicode_to_tex
190 (function _::x::_ -> x | _ -> assert false) n
191 (CicNotationPres.mpres_of_box bobj)
193 | G.Procedural depth ->
194 let obj = ProceduralOptimizer.optimize_obj obj in
195 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
196 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
197 let lazy_term_pp = term_pp in
198 let obj_pp = CicNotationPp.pp_obj term_pp in
199 let aux = GrafiteAstPp.pp_statement
200 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
202 Acic2Procedural.acic2procedural
203 ~ids_to_inner_sorts ~ids_to_inner_types ?depth ?skip_thm_and_qed prefix aobj
205 String.concat "" (List.map aux script) ^ "\n\n"
207 let txt_of_inline_macro ~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
213 let dbd = LibraryDb.instance () in
214 let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
218 ~map_unicode_to_tex 78 style prefix
219 (fst (CicEnvironment.get_obj CicUniv.empty_ugraph uri))
222 Printf.sprintf "\n(* ERRORE IN STAMPA DI %s\nEXCEPTION: %s *)\n"
223 (UriManager.string_of_uri uri) (print_exc e)
225 String.concat "" (List.map map sorted_uris)