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