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