]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/applyTransformation.ml
- MatitaMisc: we factorized here the function out_preamble used in matitadep
[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 UM = UriManager
39 module C  = Cic
40 module Un = CicUniv
41 module E  = CicEnvironment
42 module TC = CicTypeChecker
43 module G  = GrafiteAst
44 module GE = GrafiteEngine
45 module LS = LibrarySync
46 module Ds = CicDischarge
47 module PO = ProceduralOptimizer
48
49 let mpres_document pres_box =
50   Xml.add_xml_declaration (CicNotationPres.print_box pres_box)
51
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)
55   =
56     Cic2acic.asequent_of_sequent metasenv sequent
57   in
58   let content_sequent = Acic2content.map_sequent asequent in 
59   let pres_sequent = 
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,
63    unsh_sequent,
64    (asequent,
65     (ids_to_terms,ids_to_father_ids,ids_to_hypotheses,ids_to_inner_sorts)))
66
67 let nmml_of_cic_sequent metasenv subst sequent =
68   let content_sequent = NTermCicContent.nmap_sequent ~subst sequent in 
69   let pres_sequent = 
70    Sequent2pres.nsequent2pres subst content_sequent in
71   let xmlpres = mpres_document pres_sequent in
72    Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres
73
74 let mml_of_cic_object obj =
75   let (annobj, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
76     ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses)
77   =
78     Cic2acic.acic_object_of_cic_object obj
79   in
80   let content = 
81     Acic2content.annobj2content ~ids_to_inner_sorts ~ids_to_inner_types annobj
82   in
83   let pres = Content2pres.content2pres ~ids_to_inner_sorts content in
84   let xmlpres = mpres_document pres in
85   let mathml = Xml2Gdome.document_of_xml DomMisc.domImpl xmlpres in
86   (mathml,(annobj,
87    (ids_to_terms, ids_to_father_ids, ids_to_conjectures, ids_to_hypotheses,
88   ids_to_inner_sorts,ids_to_inner_types)))
89
90 let txt_of_cic_sequent ~map_unicode_to_tex size metasenv sequent =
91   let unsh_sequent,(asequent,ids_to_terms,
92     ids_to_father_ids,ids_to_inner_sorts,ids_to_hypotheses)
93   =
94     Cic2acic.asequent_of_sequent metasenv sequent
95   in
96   let content_sequent = Acic2content.map_sequent asequent in 
97   let pres_sequent = 
98    CicNotationPres.mpres_of_box
99     (Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent)
100   in
101   BoxPp.render_to_string ~map_unicode_to_tex
102     (function x::_ -> x | _ -> assert false) size pres_sequent
103
104 let txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type size
105  metasenv sequent =
106   let _,(asequent,_,_,ids_to_inner_sorts,_) = 
107     Cic2acic.asequent_of_sequent metasenv sequent 
108   in
109   let _,_,_,t = Acic2content.map_sequent asequent in 
110   let t, ids_to_uris =
111    TermAcicContent.ast_of_acic ~output_type ids_to_inner_sorts t in
112   let t = TermContentPres.pp_ast t in
113   let t = CicNotationPres.render ids_to_uris t in
114   BoxPp.render_to_string ~map_unicode_to_tex
115     (function x::_ -> x | _ -> assert false) size t
116
117 let txt_of_cic_term ~map_unicode_to_tex size metasenv context t = 
118  let fake_sequent = (-1,context,t) in
119   txt_of_cic_sequent_conclusion ~map_unicode_to_tex ~output_type:`Term size
120    metasenv fake_sequent 
121 ;;
122
123 ignore (
124  CicMetaSubst.set_ppterm_in_context
125   (fun ~metasenv subst term context ->
126     try
127      let context' = CicMetaSubst.apply_subst_context subst context in
128      let metasenv = CicMetaSubst.apply_subst_metasenv subst metasenv in
129      let term' = CicMetaSubst.apply_subst subst term in
130      let res =
131       txt_of_cic_term
132        ~map_unicode_to_tex:(Helm_registry.get_bool "matita.paste_unicode_as_tex")
133        30 metasenv context' term' in
134       if String.contains res '\n' then
135        "\n" ^ res ^ "\n"
136       else
137        res
138     with
139        Sys.Break as exn -> raise exn
140      | exn ->
141         "[[ Exception raised during pretty-printing: " ^
142          (try
143            Printexc.to_string exn
144           with
145              Sys.Break as exn -> raise exn
146            | _ -> "<<exception raised pretty-printing the exception>>"
147          ) ^ " ]] " ^
148         (CicMetaSubst.use_low_level_ppterm_in_context := true;
149          try
150           let res =
151            CicMetaSubst.ppterm_in_context ~metasenv subst term context
152           in
153            CicMetaSubst.use_low_level_ppterm_in_context := false;
154            res
155          with
156           exc -> 
157            CicMetaSubst.use_low_level_ppterm_in_context := false;
158            raise exc))
159 );;
160
161 (****************************************************************************)
162 (* txt_of_cic_object: IMPROVE ME *)
163
164 let remove_closed_substs s =
165     Pcre.replace ~pat:"{...}" ~templ:"" s
166
167 let term2pres ~map_unicode_to_tex n ids_to_inner_sorts annterm = 
168    let ast, ids_to_uris = 
169     TermAcicContent.ast_of_acic ~output_type:`Term ids_to_inner_sorts annterm in
170    let bobj =
171       CicNotationPres.box_of_mpres (
172          CicNotationPres.render ~prec:90 ids_to_uris 
173             (TermContentPres.pp_ast ast)) in
174    let render = function _::x::_ -> x | _ -> assert false in
175    let mpres = CicNotationPres.mpres_of_box bobj in
176    let s = BoxPp.render_to_string ~map_unicode_to_tex render n mpres in
177    remove_closed_substs s
178
179 let txt_of_cic_object 
180  ~map_unicode_to_tex ?skip_thm_and_qed ?skip_initial_lambdas
181  n style ?flavour prefix obj 
182 =
183   let get_aobj obj = 
184      try   
185         let aobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
186             Cic2acic.acic_object_of_cic_object obj
187         in
188         aobj, ids_to_inner_sorts, ids_to_inner_types
189      with 
190         | E.Object_not_found uri -> 
191              let msg = "txt_of_cic_object: object not found: " ^ UM.string_of_uri uri in
192              failwith msg
193         | e                     ->
194              let msg = "txt_of_cic_object: " ^ Printexc.to_string e in
195              failwith msg
196   in
197   match style with
198      | G.Declarative      ->
199         let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
200         let cobj = 
201           Acic2content.annobj2content 
202             ids_to_inner_sorts ids_to_inner_types aobj 
203         in
204         let bobj = 
205           Content2pres.content2pres 
206             ?skip_initial_lambdas ?skip_thm_and_qed ~ids_to_inner_sorts cobj 
207         in
208         remove_closed_substs (
209            BoxPp.render_to_string ~map_unicode_to_tex
210             (function _::x::_ -> x | _ -> assert false) n
211             (CicNotationPres.mpres_of_box bobj)
212         ^ "\n\n" )
213      | G.Procedural depth ->
214 (*
215         PO.critical := false;
216         Acic2Procedural.tex_formatter := Some Format.std_formatter;     
217         let _ = ProceduralTeX.tex_of_obj Format.std_formatter obj in
218 *)      
219         let obj, info = PO.optimize_obj obj in
220 (*      
221         let _ = ProceduralTeX.tex_of_obj Format.std_formatter obj in
222 *)      
223         let aobj, ids_to_inner_sorts, ids_to_inner_types = get_aobj obj in
224         let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
225         let lazy_term_pp = term_pp in
226         let obj_pp = CicNotationPp.pp_obj term_pp in
227         let aux = GrafiteAstPp.pp_statement
228          ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
229         let script = 
230            Acic2Procedural.procedural_of_acic_object 
231               ~ids_to_inner_sorts ~ids_to_inner_types ~info
232               ?depth ?flavour prefix aobj 
233   in
234         "\n\n" ^ String.concat "" (List.map aux script)
235
236 let cic_prefix = Str.regexp_string "cic:/"
237 let matita_prefix = Str.regexp_string "cic:/matita/"
238 let suffixes = [".ind"; "_rec.con"; "_rect.con"; "_ind.con"; ".con"]
239
240 let replacements = 
241    let map s = String.length s, s, Str.regexp_string s, "_discharged" ^ s in 
242    List.map map suffixes
243
244 let replacement (ok, u) (l, s, x, t) =
245    if ok then ok, u else
246    if Str.last_chars u l = s then true, Str.replace_first x t u else ok, u
247
248 let discharge_uri style uri =
249    let template = match style with
250       | G.Declarative  -> "cic:/matita/declarative/"   
251       | G.Procedural _ -> "cic:/matita/procedural/"
252    in
253    let s = UM.string_of_uri uri in
254    if Str.string_match matita_prefix s 0 then uri else
255    let s = Str.replace_first cic_prefix template s in
256    let _, s = List.fold_left replacement (false, s) replacements in 
257    UM.uri_of_string s
258
259 let discharge_name s = s ^ "_discharged"
260
261 let txt_of_inline_uri ~map_unicode_to_tex style ?flavour prefix suri =
262 (*   
263    Ds.debug := true;
264 *)
265    let print_exc = function
266       | ProofEngineHelpers.Bad_pattern s as e ->
267            Printexc.to_string e ^ " " ^ Lazy.force s
268       | e -> Printexc.to_string e
269    in
270    let dbd = LibraryDb.instance () in   
271    let sorted_uris = MetadataDeps.sorted_uris_of_baseuri ~dbd suri in
272    let error uri e =
273       let msg  = 
274          Printf.sprintf 
275             "ERROR IN THE GENERATION OF %s\nEXCEPTION: %s" 
276             (UM.string_of_uri uri) e
277       in
278       Printf.eprintf "%s\n" msg;
279       GrafiteTypes.command_error msg
280    in
281    let map uri =
282       Librarian.time_stamp "AT: BEGIN MAP";
283       try
284 (* FG: for now the explicit variables must be discharged *)
285         let do_it obj =
286            let r = txt_of_cic_object ~map_unicode_to_tex 78 style ?flavour prefix obj in
287            Librarian.time_stamp "AT: END MAP  "; r
288         in
289         let obj, real = 
290            let s = UM.string_of_uri uri in
291            if Str.string_match matita_prefix s 0 then begin
292               Librarian.time_stamp "AT: GETTING OBJECT";
293               let obj, _ = E.get_obj Un.default_ugraph uri in
294               Librarian.time_stamp "AT: DONE          ";
295               obj, true
296            end else
297               Ds.discharge_uri discharge_name (discharge_uri style) uri
298         in
299         if real then do_it obj else
300         let newuri = discharge_uri style uri in
301         let _lemmas = LS.add_obj ~pack_coercion_obj:CicRefine.pack_coercion_obj newuri obj in
302         do_it obj
303       with
304          | TC.TypeCheckerFailure s ->
305             error uri ("failure  : " ^ Lazy.force s)
306          | TC.AssertFailure s      ->
307             error uri ("assert   : " ^ Lazy.force s)
308          | E.Object_not_found u    ->
309             error uri ("not found: " ^ UM.string_of_uri u)
310          | e                       -> error uri (print_exc e)
311    in
312    String.concat "" (List.map map sorted_uris)
313
314 let txt_of_inline_macro ~map_unicode_to_tex style ?flavour prefix name =
315    let suri = 
316       if Librarian.is_uri name then name else
317       let include_paths = 
318          Helm_registry.get_list Helm_registry.string "matita.includes"
319       in
320       let _, baseuri, _, _ = 
321          Librarian.baseuri_of_script ~include_paths name
322       in
323       baseuri ^ "/"
324    in
325    txt_of_inline_uri ~map_unicode_to_tex style ?flavour prefix suri
326
327 (****************************************************************************)
328 (* procedural_txt_of_cic_term *)
329
330 let procedural_txt_of_cic_term ~map_unicode_to_tex n ?depth context term =
331   let term, _info = PO.optimize_term context term in
332   let annterm, ids_to_inner_sorts, ids_to_inner_types = 
333      try Cic2acic.acic_term_of_cic_term context term
334      with e -> 
335         let msg = "procedural_txt_of_cic_term: " ^ Printexc.to_string e in
336         failwith msg
337   in
338   let term_pp = term2pres ~map_unicode_to_tex (n - 8) ids_to_inner_sorts in
339   let lazy_term_pp = term_pp in
340   let obj_pp = CicNotationPp.pp_obj term_pp in
341   let aux = GrafiteAstPp.pp_statement
342      ~map_unicode_to_tex ~term_pp ~lazy_term_pp ~obj_pp in
343   let script = 
344      Acic2Procedural.procedural_of_acic_term 
345         ~ids_to_inner_sorts ~ids_to_inner_types ?depth "" context annterm 
346   in
347   String.concat "" (List.map aux script)
348 ;;
349
350 (****************************************************************************)
351
352 let txt_of_macro ~map_unicode_to_tex metasenv context m =
353    GrafiteAstPp.pp_macro
354      ~term_pp:(txt_of_cic_term ~map_unicode_to_tex 80 metasenv context) 
355      ~lazy_term_pp:(fun (f : Cic.lazy_term) ->
356         let t,metasenv,_ = f context metasenv CicUniv.empty_ugraph in
357         txt_of_cic_term ~map_unicode_to_tex 80 metasenv context t)
358      m
359 ;;
360
361