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 (***************************************************************************)
38 module UM = UriManager
41 module E = CicEnvironment
42 module TC = CicTypeChecker
44 module GE = GrafiteEngine
45 module LS = LibrarySync
46 module Ds = CicDischarge
48 let mpres_document pres_box =
49 Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
51 let mml_of_cic_sequent metasenv sequent =
52 let unsh_sequent,(asequent,ids_to_terms,
53 ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
55 Cic2acic.asequent_of_sequent metasenv sequent
57 let content_sequent = Acic2content.map_sequent asequent in
59 Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent in
60 let xmlpres = mpres_document pres_sequent in
61 (Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres,
64 (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
66 let mml_of_cic_object obj =
67 let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
68 ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses)
70 Cic2acic.acic_object_of_cic_object obj
73 Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
75 let pres = Content2pres.content2pres ~ids_to_inner_sorts content in
76 let xmlpres = mpres_document pres in
77 let mathml = Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres in
79 (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
80 ids_to_inner_sorts,ids_to_inner_types)))
82 let txt_of_cic_sequent ~map_unicode_to_tex size metasenv sequent =
83 let unsh_sequent,(asequent,ids_to_terms,
84 ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
86 Cic2acic.asequent_of_sequent metasenv sequent
88 let content_sequent = Acic2content.map_sequent asequent in
90 CicNotationPres.mpres_of_box
91 (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
93 BoxPp.render_to_string ~map_unicode_to_tex
94 (function x::_ -> x | _ -> assert false) size pres_sequent
96 let txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type size
98 let _,(asequent,_,_,ids_to_inner_sorts,_) =
99 Cic2acic.asequent_of_sequent metasenv sequent
101 let _,_,_,t = Acic2content.map_sequent asequent in
103 TermAcicContent.ast_of_acic ~output_type ids_to_inner_sorts t in
104 let t = TermContentPres.pp_ast t in
105 let t = CicNotationPres.render ids_to_uris t in
106 BoxPp.render_to_string ~map_unicode_to_tex
107 (function x::_ -> x | _ -> assert false) size t
109 let txt_of_cic_term ~map_unicode_to_tex size metasenv context t =
110 let fake_sequent = (-1,context,t) in
111 txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type:`Term size
112 metasenv fake_sequent
116 CicMetaSubst.set_ppterm_in_context
117 (fun ~metasenv subst term context ->
119 let context' = CicMetaSubst.apply_subst_context subst context in
120 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
121 let term' = CicMetaSubst.apply_subst subst term in
124 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
125 30 metasenv context' term' in
126 if String.contains res '\n' then
131 Sys.Break as exn -> raise exn
133 "[[ Exception raised during pretty-printing: " ^
135 Printexc.to_string exn
137 Sys.Break as exn -> raise exn
138 | _ -> "<<exception raised pretty-printing the exception>>"
140 (CicMetaSubst.use_low_level_ppterm_in_context := true;
143 CicMetaSubst.ppterm_in_context ~metasenv subst term context
145 CicMetaSubst.use_low_level_ppterm_in_context := false;
149 CicMetaSubst.use_low_level_ppterm_in_context := false;
153 (****************************************************************************)
154 (* txt_of_cic_object: IMPROVE ME *)
156 let remove_closed_substs s =
157 Pcre.replace ~pat:"{...}" ~templ:"" s
159 let term2pres ~map_unicode_to_tex n ids_to_inner_sorts annterm =
160 let ast, ids_to_uris =
161 TermAcicContent.ast_of_acic ~output_type:`Term ids_to_inner_sorts annterm in
163 CicNotationPres.box_of_mpres (
164 CicNotationPres.render ~prec:90 ids_to_uris
165 (TermContentPres.pp_ast ast)) in
166 let render = function _::x::_ -> x | _ -> assert false in
167 let mpres = CicNotationPres.mpres_of_box bobj in
168 let s = BoxPp.render_to_string ~map_unicode_to_tex render n mpres in
169 remove_closed_substs s
171 let txt_of_cic_object
172 ~map_unicode_to_tex ?skip_thm_and_qed ?skip_initial_lambdas
173 n style ?flavour prefix obj
177 let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
178 Cic2acic.acic_object_of_cic_object obj
180 aobj, ids_to_inner_sorts, ids_to_inner_types
182 | E.Object_not_found uri ->
183 let msg = "txt_of_cic_object: object not found: " ^ UM.string_of_uri uri in
186 let msg = "txt_of_cic_object: " ^ Printexc.to_string e in
191 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
193 Acic2content.annobj2content
194 ids_to_inner_sorts ids_to_inner_types aobj
197 Content2pres.content2pres
198 ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts cobj
200 remove_closed_substs ("\n\n" ^
201 BoxPp.render_to_string ~map_unicode_to_tex
202 (function _::x::_ -> x | _ -> assert false) n
203 (CicNotationPres.mpres_of_box bobj)
205 | G.Procedural depth ->
206 let obj = ProceduralOptimizer.optimize_obj obj in
207 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
208 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
209 let lazy_term_pp = term_pp in
210 let obj_pp = CicNotationPp.pp_obj term_pp in
211 let aux = GrafiteAstPp.pp_statement
212 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
214 Acic2Procedural.procedural_of_acic_object
215 ~ids_to_inner_sorts ~ids_to_inner_types
216 ?depth ?flavour prefix aobj
218 "\n\n" ^ String.concat "" (List.map aux script)
220 let cic_prefix = Str.regexp_string "cic:/"
221 let matita_prefix = Str.regexp_string "cic:/matita/"
222 let suffixes = [".ind"; "_rec.con"; "_rect.con"; "_ind.con"; ".con"]
225 let map s = String.length s, s, Str.regexp_string s, "_discharged" ^ s in
226 List.map map suffixes
228 let replacement (ok, u) (l, s, x, t) =
229 if ok then ok, u else
230 if Str.last_chars u l = s then true, Str.replace_first x t u else ok, u
232 let discharge_uri style uri =
233 let template = match style with
234 | G.Declarative -> "cic:/matita/declarative/"
235 | G.Procedural _ -> "cic:/matita/procedural/"
237 let s = UM.string_of_uri uri in
238 if Str.string_match matita_prefix s 0 then uri else
239 let s = Str.replace_first cic_prefix template s in
240 let _, s = List.fold_left replacement (false, s) replacements in
243 let discharge_name s = s ^ "_discharged"
245 let txt_of_inline_uri ~map_unicode_to_tex style ?flavour prefix suri =
246 let print_exc = function
247 | ProofEngineHelpers.Bad_pattern s as e ->
248 Printexc.to_string e ^ " " ^ Lazy.force s
249 | e -> Printexc.to_string e
251 let dbd = LibraryDb.instance () in
252 let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
256 "ERROR IN THE GENERATION OF %s\nEXCEPTION: %s"
257 (UM.string_of_uri uri) e
259 Printf.eprintf "%s\n" msg;
260 GrafiteTypes.command_error msg
263 Librarian.time_stamp "AT: BEGIN MAP";
265 (* FG: for now the explicit variables must be discharged *)
267 let r = txt_of_cic_object ~map_unicode_to_tex 78 style ?flavour prefix obj in
268 Librarian.time_stamp "AT: END MAP "; r
271 let s = UM.string_of_uri uri in
272 if Str.string_match matita_prefix s 0 then begin
273 Librarian.time_stamp "AT: GETTING OBJECT";
274 let obj, _ = E.get_obj Un.default_ugraph uri in
275 Librarian.time_stamp "AT: DONE ";
278 Ds.discharge_uri discharge_name (discharge_uri style) uri
280 if real then do_it obj else
281 let newuri = discharge_uri style uri in
282 let _lemmas = LS.add_obj GE.refinement_toolkit newuri obj in
285 | TC.TypeCheckerFailure s ->
286 error uri ("failure : " ^ Lazy.force s)
287 | TC.AssertFailure s ->
288 error uri ("assert : " ^ Lazy.force s)
289 | E.Object_not_found u ->
290 error uri ("not found: " ^ UM.string_of_uri u)
291 | e -> error uri (print_exc e)
293 String.concat "" (List.map map sorted_uris)
295 let txt_of_inline_macro ~map_unicode_to_tex style ?flavour prefix name =
297 if Librarian.is_uri name then name else
299 Helm_registry.get_list Helm_registry.string "matita.includes"
301 let _, baseuri, _, _ =
302 Librarian.baseuri_of_script ~include_paths name
306 txt_of_inline_uri ~map_unicode_to_tex style ?flavour prefix suri
308 (****************************************************************************)
309 (* procedural_txt_of_cic_term *)
311 let procedural_txt_of_cic_term ~map_unicode_to_tex n ?depth context term =
312 let annterm, ids_to_inner_sorts, ids_to_inner_types =
313 try Cic2acic.acic_term_of_cic_term context term
315 let msg = "procedural_txt_of_cic_term: " ^ Printexc.to_string e in
318 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
319 let lazy_term_pp = term_pp in
320 let obj_pp = CicNotationPp.pp_obj term_pp in
321 let aux = GrafiteAstPp.pp_statement
322 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
324 Acic2Procedural.procedural_of_acic_term
325 ~ids_to_inner_sorts ~ids_to_inner_types ?depth "" context annterm
327 String.concat "" (List.map aux script)