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
47 module PO = ProceduralOptimizer
49 let mpres_document pres_box =
50 Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
52 let mml_of_cic_sequent metasenv sequent =
53 let unsh_sequent,(asequent,ids_to_terms,
54 ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
56 Cic2acic.asequent_of_sequent metasenv sequent
58 let content_sequent = Acic2content.map_sequent asequent in
60 Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent in
61 let xmlpres = mpres_document pres_sequent in
62 (Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres,
65 (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
67 let mml_of_cic_object obj =
68 let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
69 ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses)
71 Cic2acic.acic_object_of_cic_object obj
74 Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
76 let pres = Content2pres.content2pres ~ids_to_inner_sorts content in
77 let xmlpres = mpres_document pres in
78 let mathml = Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres in
80 (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
81 ids_to_inner_sorts,ids_to_inner_types)))
83 let txt_of_cic_sequent ~map_unicode_to_tex size metasenv sequent =
84 let unsh_sequent,(asequent,ids_to_terms,
85 ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
87 Cic2acic.asequent_of_sequent metasenv sequent
89 let content_sequent = Acic2content.map_sequent asequent in
91 CicNotationPres.mpres_of_box
92 (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
94 BoxPp.render_to_string ~map_unicode_to_tex
95 (function x::_ -> x | _ -> assert false) size pres_sequent
97 let txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type size
99 let _,(asequent,_,_,ids_to_inner_sorts,_) =
100 Cic2acic.asequent_of_sequent metasenv sequent
102 let _,_,_,t = Acic2content.map_sequent asequent in
104 TermAcicContent.ast_of_acic ~output_type ids_to_inner_sorts t in
105 let t = TermContentPres.pp_ast t in
106 let t = CicNotationPres.render ids_to_uris t in
107 BoxPp.render_to_string ~map_unicode_to_tex
108 (function x::_ -> x | _ -> assert false) size t
110 let txt_of_cic_term ~map_unicode_to_tex size metasenv context t =
111 let fake_sequent = (-1,context,t) in
112 txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type:`Term size
113 metasenv fake_sequent
117 CicMetaSubst.set_ppterm_in_context
118 (fun ~metasenv subst term context ->
120 let context' = CicMetaSubst.apply_subst_context subst context in
121 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
122 let term' = CicMetaSubst.apply_subst subst term in
125 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
126 30 metasenv context' term' in
127 if String.contains res '\n' then
132 Sys.Break as exn -> raise exn
134 "[[ Exception raised during pretty-printing: " ^
136 Printexc.to_string exn
138 Sys.Break as exn -> raise exn
139 | _ -> "<<exception raised pretty-printing the exception>>"
141 (CicMetaSubst.use_low_level_ppterm_in_context := true;
144 CicMetaSubst.ppterm_in_context ~metasenv subst term context
146 CicMetaSubst.use_low_level_ppterm_in_context := false;
150 CicMetaSubst.use_low_level_ppterm_in_context := false;
154 (****************************************************************************)
155 (* txt_of_cic_object: IMPROVE ME *)
157 let remove_closed_substs s =
158 Pcre.replace ~pat:"{...}" ~templ:"" s
160 let term2pres ~map_unicode_to_tex n ids_to_inner_sorts annterm =
161 let ast, ids_to_uris =
162 TermAcicContent.ast_of_acic ~output_type:`Term ids_to_inner_sorts annterm in
164 CicNotationPres.box_of_mpres (
165 CicNotationPres.render ~prec:90 ids_to_uris
166 (TermContentPres.pp_ast ast)) in
167 let render = function _::x::_ -> x | _ -> assert false in
168 let mpres = CicNotationPres.mpres_of_box bobj in
169 let s = BoxPp.render_to_string ~map_unicode_to_tex render n mpres in
170 remove_closed_substs s
172 let txt_of_cic_object
173 ~map_unicode_to_tex ?skip_thm_and_qed ?skip_initial_lambdas
174 n style ?flavour prefix obj
178 let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
179 Cic2acic.acic_object_of_cic_object obj
181 aobj, ids_to_inner_sorts, ids_to_inner_types
183 | E.Object_not_found uri ->
184 let msg = "txt_of_cic_object: object not found: " ^ UM.string_of_uri uri in
187 let msg = "txt_of_cic_object: " ^ Printexc.to_string e in
192 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
194 Acic2content.annobj2content
195 ids_to_inner_sorts ids_to_inner_types aobj
198 Content2pres.content2pres
199 ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts cobj
201 remove_closed_substs ("\n\n" ^
202 BoxPp.render_to_string ~map_unicode_to_tex
203 (function _::x::_ -> x | _ -> assert false) n
204 (CicNotationPres.mpres_of_box bobj)
206 | G.Procedural depth ->
207 let obj, info = PO.optimize_obj obj in
208 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
209 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
210 let lazy_term_pp = term_pp in
211 let obj_pp = CicNotationPp.pp_obj term_pp in
212 let aux = GrafiteAstPp.pp_statement
213 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
215 Acic2Procedural.procedural_of_acic_object
216 ~ids_to_inner_sorts ~ids_to_inner_types ~info
217 ?depth ?flavour prefix aobj
219 "\n\n" ^ String.concat "" (List.map aux script)
221 let cic_prefix = Str.regexp_string "cic:/"
222 let matita_prefix = Str.regexp_string "cic:/matita/"
223 let suffixes = [".ind"; "_rec.con"; "_rect.con"; "_ind.con"; ".con"]
226 let map s = String.length s, s, Str.regexp_string s, "_discharged" ^ s in
227 List.map map suffixes
229 let replacement (ok, u) (l, s, x, t) =
230 if ok then ok, u else
231 if Str.last_chars u l = s then true, Str.replace_first x t u else ok, u
233 let discharge_uri style uri =
234 let template = match style with
235 | G.Declarative -> "cic:/matita/declarative/"
236 | G.Procedural _ -> "cic:/matita/procedural/"
238 let s = UM.string_of_uri uri in
239 if Str.string_match matita_prefix s 0 then uri else
240 let s = Str.replace_first cic_prefix template s in
241 let _, s = List.fold_left replacement (false, s) replacements in
244 let discharge_name s = s ^ "_discharged"
246 let txt_of_inline_uri ~map_unicode_to_tex style ?flavour prefix suri =
247 let print_exc = function
248 | ProofEngineHelpers.Bad_pattern s as e ->
249 Printexc.to_string e ^ " " ^ Lazy.force s
250 | e -> Printexc.to_string e
252 let dbd = LibraryDb.instance () in
253 let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
257 "ERROR IN THE GENERATION OF %s\nEXCEPTION: %s"
258 (UM.string_of_uri uri) e
260 Printf.eprintf "%s\n" msg;
261 GrafiteTypes.command_error msg
264 Librarian.time_stamp "AT: BEGIN MAP";
266 (* FG: for now the explicit variables must be discharged *)
268 let r = txt_of_cic_object ~map_unicode_to_tex 78 style ?flavour prefix obj in
269 Librarian.time_stamp "AT: END MAP "; r
272 let s = UM.string_of_uri uri in
273 if Str.string_match matita_prefix s 0 then begin
274 Librarian.time_stamp "AT: GETTING OBJECT";
275 let obj, _ = E.get_obj Un.default_ugraph uri in
276 Librarian.time_stamp "AT: DONE ";
279 Ds.discharge_uri discharge_name (discharge_uri style) uri
281 if real then do_it obj else
282 let newuri = discharge_uri style uri in
283 let _lemmas = LS.add_obj GE.refinement_toolkit newuri obj in
286 | TC.TypeCheckerFailure s ->
287 error uri ("failure : " ^ Lazy.force s)
288 | TC.AssertFailure s ->
289 error uri ("assert : " ^ Lazy.force s)
290 | E.Object_not_found u ->
291 error uri ("not found: " ^ UM.string_of_uri u)
292 | e -> error uri (print_exc e)
294 String.concat "" (List.map map sorted_uris)
296 let txt_of_inline_macro ~map_unicode_to_tex style ?flavour prefix name =
298 if Librarian.is_uri name then name else
300 Helm_registry.get_list Helm_registry.string "matita.includes"
302 let _, baseuri, _, _ =
303 Librarian.baseuri_of_script ~include_paths name
307 txt_of_inline_uri ~map_unicode_to_tex style ?flavour prefix suri
309 (****************************************************************************)
310 (* procedural_txt_of_cic_term *)
312 let procedural_txt_of_cic_term ~map_unicode_to_tex n ?depth context term =
313 let term, _info = PO.optimize_term context term in
314 let annterm, ids_to_inner_sorts, ids_to_inner_types =
315 try Cic2acic.acic_term_of_cic_term context term
317 let msg = "procedural_txt_of_cic_term: " ^ Printexc.to_string e in
320 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
321 let lazy_term_pp = term_pp in
322 let obj_pp = CicNotationPp.pp_obj term_pp in
323 let aux = GrafiteAstPp.pp_statement
324 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
326 Acic2Procedural.procedural_of_acic_term
327 ~ids_to_inner_sorts ~ids_to_inner_types ?depth "" context annterm
329 String.concat "" (List.map aux script)
332 (****************************************************************************)
334 let txt_of_macro ~map_unicode_to_tex metasenv context m =
335 GrafiteAstPp.pp_macro
336 ~term_pp:(txt_of_cic_term ~map_unicode_to_tex 80 metasenv context)
337 ~lazy_term_pp:(fun (f : Cic.lazy_term) ->
338 let t,metasenv,_ = f context metasenv CicUniv.empty_ugraph in
339 txt_of_cic_term ~map_unicode_to_tex 80 metasenv context t)