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
48 module N = CicNotationPt
50 let mpres_document pres_box =
51 Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
53 let mml_of_cic_sequent metasenv sequent =
54 let unsh_sequent,(asequent,ids_to_terms,
55 ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
57 Cic2acic.asequent_of_sequent metasenv sequent
59 let content_sequent = Acic2content.map_sequent asequent in
61 Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent in
62 let xmlpres = mpres_document pres_sequent in
63 (Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres,
66 (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
68 let nmml_of_cic_sequent metasenv subst sequent =
69 let content_sequent,ids_to_refs =
70 NTermCicContent.nmap_sequent ~subst sequent in
72 Sequent2pres.nsequent2pres ids_to_refs subst content_sequent in
73 let xmlpres = mpres_document pres_sequent in
74 Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres
76 let mml_of_cic_object obj =
77 let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
78 ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses)
80 Cic2acic.acic_object_of_cic_object obj
83 Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
85 let pres = Content2pres.content2pres ~ids_to_inner_sorts content in
86 let xmlpres = mpres_document pres in
87 let mathml = Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres in
89 (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
90 ids_to_inner_sorts,ids_to_inner_types)))
92 let nmml_of_cic_object obj =
93 prerr_endline (NCicPp.ppobj obj);
97 let txt_of_cic_sequent ~map_unicode_to_tex size metasenv sequent =
98 let unsh_sequent,(asequent,ids_to_terms,
99 ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
101 Cic2acic.asequent_of_sequent metasenv sequent
103 let content_sequent = Acic2content.map_sequent asequent in
105 CicNotationPres.mpres_of_box
106 (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
108 BoxPp.render_to_string ~map_unicode_to_tex
109 (function x::_ -> x | _ -> assert false) size pres_sequent
111 let txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type size
113 let _,(asequent,_,_,ids_to_inner_sorts,_) =
114 Cic2acic.asequent_of_sequent metasenv sequent
116 let _,_,_,t = Acic2content.map_sequent asequent in
118 TermAcicContent.ast_of_acic ~output_type ids_to_inner_sorts t in
119 let t = TermContentPres.pp_ast t in
121 CicNotationPres.render ~lookup_uri:(CicNotationPres.lookup_uri ids_to_uris) t
123 BoxPp.render_to_string ~map_unicode_to_tex
124 (function x::_ -> x | _ -> assert false) size t
126 let txt_of_cic_term ~map_unicode_to_tex size metasenv context t =
127 let fake_sequent = (-1,context,t) in
128 txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type:`Term size
129 metasenv fake_sequent
133 CicMetaSubst.set_ppterm_in_context
134 (fun ~metasenv subst term context ->
136 let context' = CicMetaSubst.apply_subst_context subst context in
137 let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
138 let term' = CicMetaSubst.apply_subst subst term in
141 ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
142 30 metasenv context' term' in
143 if String.contains res '\n' then
148 Sys.Break as exn -> raise exn
150 "[[ Exception raised during pretty-printing: " ^
152 Printexc.to_string exn
154 Sys.Break as exn -> raise exn
155 | _ -> "<<exception raised pretty-printing the exception>>"
157 (CicMetaSubst.use_low_level_ppterm_in_context := true;
160 CicMetaSubst.ppterm_in_context ~metasenv subst term context
162 CicMetaSubst.use_low_level_ppterm_in_context := false;
166 CicMetaSubst.use_low_level_ppterm_in_context := false;
170 (****************************************************************************)
171 (* txt_of_cic_object: IMPROVE ME *)
173 let remove_closed_substs s =
174 Pcre.replace ~pat:"{...}" ~templ:"" s
176 let term2pres ~map_unicode_to_tex n ids_to_inner_sorts annterm =
177 let ast, ids_to_uris =
178 TermAcicContent.ast_of_acic ~output_type:`Term ids_to_inner_sorts annterm in
180 CicNotationPres.box_of_mpres (
181 CicNotationPres.render ~prec:90
182 ~lookup_uri:(CicNotationPres.lookup_uri ids_to_uris)
183 (TermContentPres.pp_ast ast)) in
184 let render = function _::x::_ -> x | _ -> assert false in
185 let mpres = CicNotationPres.mpres_of_box bobj in
186 let s = BoxPp.render_to_string ~map_unicode_to_tex render n mpres in
187 remove_closed_substs s
189 let enable_notations = function
191 CicNotation.set_active_notations
192 (List.map fst (CicNotation.get_all_notations ()))
194 CicNotation.set_active_notations []
196 let txt_of_cic_object
197 ~map_unicode_to_tex ?skip_thm_and_qed ?skip_initial_lambdas n params obj
201 let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
202 Cic2acic.acic_object_of_cic_object obj
204 aobj, ids_to_inner_sorts, ids_to_inner_types
206 | E.Object_not_found uri ->
207 let msg = "txt_of_cic_object: object not found: " ^ UM.string_of_uri uri in
210 let msg = "txt_of_cic_object: " ^ Printexc.to_string e in
213 if List.mem G.IPProcedural params then begin
216 PO.critical := false;
217 Acic2Procedural.tex_formatter := Some Format.std_formatter;
218 let _ = ProceduralTeX.tex_of_obj Format.std_formatter obj in
220 let obj, info = PO.optimize_obj obj in
222 let _ = ProceduralTeX.tex_of_obj Format.std_formatter obj in
224 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
225 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
226 let lazy_term_pp = term_pp in
227 let obj_pp = CicNotationPp.pp_obj term_pp in
229 GrafiteAstPp.pp_statement
230 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp
233 | G.Executable (_, G.Command (_, G.Obj (_, N.Inductive _))) as stm
235 enable_notations false;
236 let str = stm_pp stm in
237 enable_notations true;
239 (* FG: we disable notation for Inductive to avoid recursive notation *)
240 | G.Executable (_, G.Tactic _) as stm ->
241 let hc = !Acic2content.hide_coercions in
242 Acic2content.hide_coercions := false;
243 let str = stm_pp stm in
244 Acic2content.hide_coercions := hc;
246 (* FG: we show coercion because the reconstruction is not aware of them *)
250 Acic2Procedural.procedural_of_acic_object
251 ~ids_to_inner_sorts ~ids_to_inner_types ~info params aobj
253 String.concat "" (List.map aux script) ^ "\n\n"
255 let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
257 Acic2content.annobj2content ids_to_inner_sorts ids_to_inner_types aobj
260 Content2pres.content2pres
261 ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts cobj
263 remove_closed_substs (
264 BoxPp.render_to_string ~map_unicode_to_tex
265 (function _::x::_ -> x | _ -> assert false) n
266 (CicNotationPres.mpres_of_box bobj)
270 let cic_prefix = Str.regexp_string "cic:/"
271 let matita_prefix = Str.regexp_string "cic:/matita/"
272 let suffixes = [".ind"; "_rec.con"; "_rect.con"; "_ind.con"; ".con"]
275 let map s = String.length s, s, Str.regexp_string s, "_discharged" ^ s in
276 List.map map suffixes
278 let replacement (ok, u) (l, s, x, t) =
279 if ok then ok, u else
280 if Str.last_chars u l = s then true, Str.replace_first x t u else ok, u
282 let discharge_uri params uri =
284 if List.mem G.IPProcedural params then "cic:/matita/procedural/"
285 else "cic:/matita/declarative/"
287 let s = UM.string_of_uri uri in
288 if Str.string_match matita_prefix s 0 then uri else
289 let s = Str.replace_first cic_prefix template s in
290 let _, s = List.fold_left replacement (false, s) replacements in
293 let discharge_name s = s ^ "_discharged"
295 let txt_of_inline_uri ~map_unicode_to_tex params suri =
299 let print_exc = function
300 | ProofEngineHelpers.Bad_pattern s as e ->
301 Printexc.to_string e ^ " " ^ Lazy.force s
302 | e -> Printexc.to_string e
304 let dbd = LibraryDb.instance () in
305 let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
309 "ERROR IN THE GENERATION OF %s\nEXCEPTION: %s"
310 (UM.string_of_uri uri) e
312 Printf.eprintf "%s\n" msg;
313 GrafiteTypes.command_error msg
316 Librarian.time_stamp "AT: BEGIN MAP";
318 (* FG: for now the explicit variables must be discharged *)
320 let r = txt_of_cic_object ~map_unicode_to_tex 78 params obj in
321 Librarian.time_stamp "AT: END MAP "; r
324 let s = UM.string_of_uri uri in
325 if Str.string_match matita_prefix s 0 then begin
326 Librarian.time_stamp "AT: GETTING OBJECT";
327 let obj, _ = E.get_obj Un.default_ugraph uri in
328 Librarian.time_stamp "AT: DONE ";
331 Ds.discharge_uri discharge_name (discharge_uri params) uri
333 if real then do_it obj else
334 let newuri = discharge_uri params uri in
335 let _lemmas = LS.add_obj ~pack_coercion_obj:CicRefine.pack_coercion_obj newuri obj in
338 | TC.TypeCheckerFailure s ->
339 error uri ("failure : " ^ Lazy.force s)
340 | TC.AssertFailure s ->
341 error uri ("assert : " ^ Lazy.force s)
342 | E.Object_not_found u ->
343 error uri ("not found: " ^ UM.string_of_uri u)
344 | e -> error uri (print_exc e)
346 String.concat "" (List.map map sorted_uris)
348 let txt_of_inline_macro ~map_unicode_to_tex params name =
350 if Librarian.is_uri name then name else
352 Helm_registry.get_list Helm_registry.string "matita.includes"
354 let _, baseuri, _, _ =
355 Librarian.baseuri_of_script ~include_paths name
359 txt_of_inline_uri ~map_unicode_to_tex params suri
361 (****************************************************************************)
362 (* procedural_txt_of_cic_term *)
364 let procedural_txt_of_cic_term ~map_unicode_to_tex n params context term =
365 let term, _info = PO.optimize_term context term in
366 let annterm, ids_to_inner_sorts, ids_to_inner_types =
367 try Cic2acic.acic_term_of_cic_term context term
369 let msg = "procedural_txt_of_cic_term: " ^ Printexc.to_string e in
372 let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
373 let lazy_term_pp = term_pp in
374 let obj_pp = CicNotationPp.pp_obj term_pp in
375 let aux = GrafiteAstPp.pp_statement
376 ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
378 Acic2Procedural.procedural_of_acic_term
379 ~ids_to_inner_sorts ~ids_to_inner_types params context annterm
381 String.concat "" (List.map aux script)
384 (****************************************************************************)
386 let txt_of_macro ~map_unicode_to_tex metasenv context m =
387 GrafiteAstPp.pp_macro
388 ~term_pp:(txt_of_cic_term ~map_unicode_to_tex 80 metasenv context)
389 ~lazy_term_pp:(fun (f : Cic.lazy_term) ->
390 let t,metasenv,_ = f context metasenv CicUniv.empty_ugraph in
391 txt_of_cic_term ~map_unicode_to_tex 80 metasenv context t)