]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matita.ml
lazy proof term to increase sharing and decrease memory consumption.
[helm.git] / helm / software / matita / matita.ml
1 (* Copyright (C) 2004-2005, 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://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 open MatitaGtkMisc
31 open GrafiteTypes
32
33 (** {2 Initialization} *)
34
35 let _ = 
36   MatitaInit.add_cmdline_spec
37     ["-tptppath",Arg.String 
38       (fun s -> Helm_registry.set_string "matita.tptppath" s),
39       "Where to find the Axioms/ and Problems/ directory"];
40   MatitaInit.initialize_all ()
41 ;;
42
43 (* let _ = Saturation.init () (* ALB to link paramodulation *) *)
44
45 (** {2 GUI callbacks} *)
46
47 let gui = MatitaGui.instance ()
48
49 let script =
50   let s = 
51     MatitaScript.script 
52       ~source_view:gui#sourceView
53       ~mathviewer:(MatitaMathView.mathViewer ())
54       ~urichooser:(fun uris ->
55         try
56           MatitaGui.interactive_uri_choice ~selection_mode:`SINGLE
57           ~title:"Matita: URI chooser" 
58           ~msg:"Select the URI" ~hide_uri_entry:true
59           ~hide_try:true ~ok_label:"_Apply" ~ok_action:`SELECT
60           ~copy_cb:(fun s -> gui#sourceView#buffer#insert ("\n"^s^"\n"))
61           () ~id:"boh?" uris
62         with MatitaTypes.Cancel -> [])
63       ~set_star:gui#setStar
64       ~ask_confirmation:
65         (fun ~title ~message -> 
66             MatitaGtkMisc.ask_confirmation ~title ~message 
67             ~parent:gui#main#toplevel ())
68       ()
69   in
70   gui#sourceView#source_buffer#begin_not_undoable_action ();
71   s#reset (); 
72   s#template (); 
73   gui#sourceView#source_buffer#end_not_undoable_action ();
74   s
75   
76   (* math viewers *)
77 let _ =
78   let cic_math_view = MatitaMathView.cicMathView_instance () in
79   let sequents_viewer = MatitaMathView.sequentsViewer_instance () in
80   sequents_viewer#load_logo;
81   cic_math_view#set_href_callback
82     (Some (fun uri -> (MatitaMathView.cicBrowser ())#load
83       (`Uri (UriManager.uri_of_string uri))));
84   let browser_observer _ _ = MatitaMathView.refresh_all_browsers () in
85   let sequents_observer _ grafite_status =
86     sequents_viewer#reset;
87     match grafite_status.proof_status with
88     | Incomplete_proof ({ stack = stack } as incomplete_proof) ->
89         sequents_viewer#load_sequents incomplete_proof;
90         (try
91           script#setGoal (Some (Continuationals.Stack.find_goal stack));
92           let goal =
93            match script#goal with
94               None -> assert false
95             | Some n -> n
96           in
97            sequents_viewer#goto_sequent goal
98         with Failure _ -> script#setGoal None);
99     | Proof proof -> sequents_viewer#load_logo_with_qed
100     | No_proof -> sequents_viewer#load_logo
101     | Intermediate _ -> assert false (* only the engine may be in this state *)
102   in
103   script#addObserver sequents_observer;
104   script#addObserver browser_observer
105
106   (** {{{ Debugging *)
107 let _ =
108   if BuildTimeConf.debug ||
109      Helm_registry.get_bool "matita.debug_menu" 
110   then begin
111     gui#main#debugMenu#misc#show ();
112     let addDebugItem ~label callback =
113       let item =
114         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label () in
115       ignore (item#connect#activate callback) in
116     let addDebugSeparator () =
117       ignore (GMenu.separator_item ~packing:gui#main#debugMenu_menu#append ())
118     in
119     addDebugItem "dump aliases" (fun _ ->
120       let status = script#lexicon_status in
121       HLog.debug (DisambiguatePp.pp_environment status.LexiconEngine.aliases));
122     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
123       let oc = open_out "env.dump" in
124       CicEnvironment.dump_to_channel oc;
125       close_out oc);
126     addDebugItem "load environment from \"env.dump\"" (fun _ ->
127       let ic = open_in "env.dump" in
128       CicEnvironment.restore_from_channel ic;
129       close_in ic);
130     addDebugItem "dump universes" (fun _ ->
131       List.iter (fun (u,_,g) -> 
132         prerr_endline (UriManager.string_of_uri u); 
133         CicUniv.print_ugraph g) (CicEnvironment.list_obj ())
134       );
135     addDebugItem "dump environment content" (fun _ ->
136       List.iter (fun (u,_,_) -> 
137         prerr_endline (UriManager.string_of_uri u)) 
138         (CicEnvironment.list_obj ()));
139     addDebugItem "dump script status" script#dump;
140     addDebugItem "dump configuration file to ./foo.conf.xml" (fun _ ->
141       Helm_registry.save_to "./foo.conf.xml");
142     addDebugItem "dump metasenv"
143       (fun _ ->
144          if script#onGoingProof () then
145            HLog.debug (CicMetaSubst.ppmetasenv [] script#proofMetasenv));
146     addDebugItem "print top-level grammar entries"
147       CicNotationParser.print_l2_pattern;
148     addDebugItem "dump moo to stderr" (fun _ ->
149       let grafite_status = (MatitaScript.current ())#grafite_status in
150       let moo = grafite_status.moo_content_rev in
151       List.iter
152         (fun cmd ->
153           prerr_endline
154            (GrafiteAstPp.pp_command
155              ~term_pp:(fun _ -> assert false)
156              ~obj_pp:(fun _ -> assert false)
157              cmd))
158         (List.rev moo));
159     addDebugItem "print metasenv goals and stack to stderr"
160       (fun _ ->
161         prerr_endline ("metasenv goals: " ^ String.concat " "
162           (List.map (fun (g, _, _) -> string_of_int g)
163             (MatitaScript.current ())#proofMetasenv));
164         prerr_endline ("stack: " ^ Continuationals.Stack.pp
165           (GrafiteTypes.get_stack (MatitaScript.current ())#grafite_status)));
166      addDebugItem "Print current proof term" 
167        (fun _ -> 
168         HLog.debug
169           (CicPp.ppterm 
170             (match 
171             (MatitaScript.current ())#grafite_status.GrafiteTypes.proof_status
172             with
173             | GrafiteTypes.No_proof -> (Cic.Implicit None)
174             | Incomplete_proof i -> 
175                  let _,_,_subst,p,_, _ = i.GrafiteTypes.proof in 
176                  Lazy.force p
177             | Proof p -> let _,_,_subst,p,_, _ = p in Lazy.force p
178             | Intermediate _ -> assert false)));
179      addDebugItem "Print current proof (natural language) to stderr" 
180        (fun _ -> 
181         prerr_endline 
182           (ApplyTransformation.txt_of_cic_object 120 GrafiteAst.Declarative "" 
183             ~map_unicode_to_tex:(Helm_registry.get_bool
184               "matita.paste_unicode_as_tex")
185             (match 
186             (MatitaScript.current ())#grafite_status.GrafiteTypes.proof_status
187             with
188             | GrafiteTypes.No_proof -> assert false
189             | Incomplete_proof i -> 
190                 let _,m,_subst,p,ty, attrs = i.GrafiteTypes.proof in 
191                 Cic.CurrentProof ("current (incomplete) proof",m,Lazy.force p,ty,[],attrs)
192             | Proof (_,m,_subst,p,ty, attrs) -> 
193                 Cic.CurrentProof ("current proof",m,Lazy.force p,ty,[],attrs)
194             | Intermediate _ -> assert false)));
195 (*     addDebugItem "ask record choice"
196       (fun _ ->
197         HLog.debug (string_of_int
198           (MatitaGtkMisc.ask_record_choice ~gui ~title:"title" ~message:"msg"
199           ~fields:["a"; "b"; "c"]
200           ~records:[
201             ["0"; "0"; "0"]; ["0"; "0"; "1"]; ["0"; "1"; "0"]; ["0"; "1"; "1"];
202             ["1"; "0"; "0"]; ["1"; "0"; "1"]; ["1"; "1"; "0"]; ["1"; "1"; "1"]]
203           ()))); *)
204 (*     addDebugItem "rotate light bulbs"
205       (fun _ ->
206          let nb = gui#main#hintNotebook in
207          nb#goto_page ((nb#current_page + 1) mod 3)); *)
208     addDebugSeparator ();
209 (*
210     addDebugItem "meets between L and R" 
211       (fun _ -> 
212         let l = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
213           (UriManager.uri_of_string "cic:/matita/test/L.ind#xpointer(1/1)" )) 
214         in
215         let r = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
216           (UriManager.uri_of_string "cic:/matita/test/R.ind#xpointer(1/1)" )) 
217         in
218         let meets = CoercGraph.meets l r in
219         prerr_endline "MEETS:";
220         List.iter (fun carr -> prerr_endline (CicPp.ppterm (CoercDb.term_of_carr
221         carr))) meets
222     );
223     addDebugSeparator ();
224 *)
225     addDebugItem "disable high level pretty printer"
226       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := true);
227     addDebugItem "enable high level pretty printer"
228       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := false);
229 (* ZACK moved to the View menu
230     addDebugItem "disable all (pretty printing) notations"
231       (fun _ -> CicNotation.set_active_notations []);
232     addDebugItem "enable all (pretty printing) notations"
233       (fun _ ->
234         CicNotation.set_active_notations
235           (List.map fst (CicNotation.get_all_notations ())));
236 *)
237     addDebugSeparator ();
238     addDebugItem "enable multiple disambiguation passes (default)"
239       (fun _ -> GrafiteDisambiguator.only_one_pass := false);
240     addDebugItem "enable only one disambiguation pass"
241       (fun _ -> GrafiteDisambiguator.only_one_pass := true);
242     addDebugItem "always show all disambiguation errors"
243       (fun _ -> MatitaGui.all_disambiguation_passes := true);
244     addDebugItem "prune disambiguation errors"
245       (fun _ -> MatitaGui.all_disambiguation_passes := false);
246     addDebugSeparator ();
247 (* ZACK moved to the View menu
248     addDebugItem "enable coercions hiding"
249       (fun _ -> Acic2content.hide_coercions := true);
250     addDebugItem "disable coercions hiding"
251       (fun _ -> Acic2content.hide_coercions := false);
252 *)
253     addDebugItem "show coercions graph" (fun _ ->
254       let c = MatitaMathView.cicBrowser () in
255       c#load (`About `Coercions));
256     addDebugItem "show coercions graph (full)" (fun _ ->
257       let c = MatitaMathView.cicBrowser () in
258       c#load (`About `CoercionsFull));
259     addDebugItem "dump coercions Db" (fun _ ->
260       List.iter
261       (fun (s,t,ul) -> 
262           HLog.debug
263            ((String.concat ","
264               (List.map
265                 (fun u,saturations,_ ->
266                   UriManager.name_of_uri u ^
267                    "(" ^ string_of_int saturations ^ ")")
268                 ul)) ^ ":"
269              ^ CoercDb.string_of_carr s ^ " -> " ^ CoercDb.string_of_carr t))
270         (CoercDb.to_list ()));
271     addDebugSeparator ();
272     let mview () = (MatitaMathView.sequentsViewer_instance ())#cicMathView in
273 (*     addDebugItem "save (sequent) MathML to matita.xml"
274       (fun _ -> ignore ((Gdome.domImplementation ())#saveDocumentToFile
275         ~doc:(HExtlib.unopt (mview ())#get_document) ~name:"matita.xml" ())); *)
276     addDebugItem "load (sequent) MathML from matita.xml"
277       (fun _ -> (mview ())#load_uri ~filename:"matita.xml");
278     addDebugItem "autoWin"
279     (fun _ -> MatitaAutoGui.auto_dialog Auto.get_auto_status);
280   end
281   (** Debugging }}} *)
282
283   (** {2 Main} *)
284
285 let _ =
286   at_exit (fun () -> print_endline "\nThanks for using Matita!\n");
287   Sys.catch_break true;
288   let args = Helm_registry.get_list Helm_registry.string "matita.args" in
289   (try gui#loadScript (List.hd args) with Failure _ -> ());
290   gui#main#mainWin#show ();
291   try
292    GtkThread.main ()
293   with Sys.Break ->
294    Sys.set_signal Sys.sigint
295     (Sys.Signal_handle
296       (fun _ ->
297         prerr_endline "Still cleaning the library: don't be impatient!"));
298    prerr_endline "Matita is cleaning up. Please wait.";
299    let baseuri = 
300      GrafiteTypes.get_baseuri (MatitaScript.current ())#grafite_status
301    in
302      LibraryClean.clean_baseuris [baseuri]
303
304 (* vim:set foldmethod=marker: *)