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