]> matita.cs.unibo.it Git - helm.git/blob - matita/matita.ml
tagged 0.5.0-rc1
[helm.git] / 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 then begin
109     gui#main#debugMenu#misc#show ();
110     let addDebugItem ~label callback =
111       let item =
112         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label () in
113       ignore (item#connect#activate callback) in
114     let addDebugSeparator () =
115       ignore (GMenu.separator_item ~packing:gui#main#debugMenu_menu#append ())
116     in
117     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
118       let oc = open_out "env.dump" in
119       CicEnvironment.dump_to_channel oc;
120       close_out oc);
121     addDebugItem "load environment from \"env.dump\"" (fun _ ->
122       let ic = open_in "env.dump" in
123       CicEnvironment.restore_from_channel ic;
124       close_in ic);
125     addDebugItem "dump universes" (fun _ ->
126       List.iter (fun (u,_,g) -> 
127         prerr_endline (UriManager.string_of_uri u); 
128         CicUniv.print_ugraph g) (CicEnvironment.list_obj ())
129       );
130     addDebugItem "dump environment content" (fun _ ->
131       List.iter (fun (u,_,_) -> 
132         prerr_endline (UriManager.string_of_uri u)) 
133         (CicEnvironment.list_obj ()));
134     addDebugItem "dump script status" script#dump;
135     addDebugItem "dump configuration file to ./foo.conf.xml" (fun _ ->
136       Helm_registry.save_to "./foo.conf.xml");
137     addDebugItem "dump metasenv"
138       (fun _ ->
139          if script#onGoingProof () then
140            HLog.debug (CicMetaSubst.ppmetasenv [] script#proofMetasenv));
141     addDebugItem "print top-level grammar entries"
142       CicNotationParser.print_l2_pattern;
143     addDebugItem "dump moo to stderr" (fun _ ->
144       let grafite_status = (MatitaScript.current ())#grafite_status in
145       let moo = grafite_status.moo_content_rev in
146       List.iter
147         (fun cmd ->
148           prerr_endline
149            (GrafiteAstPp.pp_command
150              ~term_pp:(fun _ -> assert false)
151              ~obj_pp:(fun _ -> assert false)
152              cmd))
153         (List.rev moo));
154     addDebugItem "print metasenv goals and stack to stderr"
155       (fun _ ->
156         prerr_endline ("metasenv goals: " ^ String.concat " "
157           (List.map (fun (g, _, _) -> string_of_int g)
158             (MatitaScript.current ())#proofMetasenv));
159         prerr_endline ("stack: " ^ Continuationals.Stack.pp
160           (GrafiteTypes.get_stack (MatitaScript.current ())#grafite_status)));
161      addDebugItem "Print current proof term" 
162        (fun _ -> 
163         HLog.debug
164           (CicPp.ppterm 
165             (match 
166             (MatitaScript.current ())#grafite_status.GrafiteTypes.proof_status
167             with
168             | GrafiteTypes.No_proof -> (Cic.Implicit None)
169             | Incomplete_proof i -> let _,_,_subst,p,_, _ = i.GrafiteTypes.proof in p
170             | Proof p -> let _,_,_subst,p,_, _ = p in p
171             | Intermediate _ -> assert false)));
172      addDebugItem "Print current proof (natural language) to stderr" 
173        (fun _ -> 
174         prerr_endline 
175           (ApplyTransformation.txt_of_cic_object 120 GrafiteAst.Declarative "" 
176             ~map_unicode_to_tex:(Helm_registry.get_bool
177               "matita.paste_unicode_as_tex")
178             (match 
179             (MatitaScript.current ())#grafite_status.GrafiteTypes.proof_status
180             with
181             | GrafiteTypes.No_proof -> assert false
182             | Incomplete_proof i -> 
183                 let _,m,_subst,p,ty, attrs = i.GrafiteTypes.proof in 
184                 Cic.CurrentProof ("current (incomplete) proof",m,p,ty,[],attrs)
185             | Proof (_,m,_subst,p,ty, attrs) -> 
186                 Cic.CurrentProof ("current proof",m,p,ty,[],attrs)
187             | Intermediate _ -> assert false)));
188 (*     addDebugItem "ask record choice"
189       (fun _ ->
190         HLog.debug (string_of_int
191           (MatitaGtkMisc.ask_record_choice ~gui ~title:"title" ~message:"msg"
192           ~fields:["a"; "b"; "c"]
193           ~records:[
194             ["0"; "0"; "0"]; ["0"; "0"; "1"]; ["0"; "1"; "0"]; ["0"; "1"; "1"];
195             ["1"; "0"; "0"]; ["1"; "0"; "1"]; ["1"; "1"; "0"]; ["1"; "1"; "1"]]
196           ()))); *)
197 (*     addDebugItem "rotate light bulbs"
198       (fun _ ->
199          let nb = gui#main#hintNotebook in
200          nb#goto_page ((nb#current_page + 1) mod 3)); *)
201     addDebugSeparator ();
202 (*
203     addDebugItem "meets between L and R" 
204       (fun _ -> 
205         let l = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
206           (UriManager.uri_of_string "cic:/matita/test/L.ind#xpointer(1/1)" )) 
207         in
208         let r = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
209           (UriManager.uri_of_string "cic:/matita/test/R.ind#xpointer(1/1)" )) 
210         in
211         let meets = CoercGraph.meets l r in
212         prerr_endline "MEETS:";
213         List.iter (fun carr -> prerr_endline (CicPp.ppterm (CoercDb.term_of_carr
214         carr))) meets
215     );
216     addDebugSeparator ();
217 *)
218     addDebugItem "disable high level pretty printer"
219       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := true);
220     addDebugItem "enable high level pretty printer"
221       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := false);
222 (* ZACK moved to the View menu
223     addDebugItem "disable all (pretty printing) notations"
224       (fun _ -> CicNotation.set_active_notations []);
225     addDebugItem "enable all (pretty printing) notations"
226       (fun _ ->
227         CicNotation.set_active_notations
228           (List.map fst (CicNotation.get_all_notations ())));
229 *)
230     addDebugSeparator ();
231     addDebugItem "enable multiple disambiguation passes (default)"
232       (fun _ -> GrafiteDisambiguator.only_one_pass := false);
233     addDebugItem "enable only one disambiguation pass"
234       (fun _ -> GrafiteDisambiguator.only_one_pass := true);
235     addDebugItem "always show all disambiguation errors"
236       (fun _ -> MatitaGui.all_disambiguation_passes := true);
237     addDebugItem "prune disambiguation errors"
238       (fun _ -> MatitaGui.all_disambiguation_passes := false);
239     addDebugSeparator ();
240 (* ZACK moved to the View menu
241     addDebugItem "enable coercions hiding"
242       (fun _ -> Acic2content.hide_coercions := true);
243     addDebugItem "disable coercions hiding"
244       (fun _ -> Acic2content.hide_coercions := false);
245 *)
246     addDebugItem "show coercions graph" (fun _ ->
247       let c = MatitaMathView.cicBrowser () in
248       c#load (`About `Coercions));
249     addDebugItem "show coercions graph (full)" (fun _ ->
250       let c = MatitaMathView.cicBrowser () in
251       c#load (`About `CoercionsFull));
252     addDebugItem "dump coercions Db" (fun _ ->
253       List.iter
254       (fun (s,t,ul) -> 
255           HLog.debug
256            ((String.concat ","
257               (List.map
258                 (fun u,saturations ->
259                   UriManager.name_of_uri u ^
260                    "(" ^ string_of_int saturations ^ ")")
261                 ul)) ^ ":"
262              ^ CoercDb.name_of_carr s ^ " -> " ^ CoercDb.name_of_carr t))
263         (CoercDb.to_list ()));
264     addDebugSeparator ();
265     let mview () = (MatitaMathView.sequentsViewer_instance ())#cicMathView in
266 (*     addDebugItem "save (sequent) MathML to matita.xml"
267       (fun _ -> ignore ((Gdome.domImplementation ())#saveDocumentToFile
268         ~doc:(HExtlib.unopt (mview ())#get_document) ~name:"matita.xml" ())); *)
269     addDebugItem "load (sequent) MathML from matita.xml"
270       (fun _ -> (mview ())#load_uri ~filename:"matita.xml");
271     addDebugItem "autoWin"
272     (fun _ -> MatitaAutoGui.auto_dialog Auto.get_auto_status);
273   end
274   (** Debugging }}} *)
275
276   (** {2 Main} *)
277
278 let _ =
279   at_exit (fun () -> print_endline "\nThanks for using Matita!\n");
280   Sys.catch_break true;
281   let args = Helm_registry.get_list Helm_registry.string "matita.args" in
282   (try gui#loadScript (List.hd args) with Failure _ -> ());
283   gui#main#mainWin#show ();
284   try
285    GtkThread.main ()
286   with Sys.Break ->
287    Sys.set_signal Sys.sigint
288     (Sys.Signal_handle
289       (fun _ ->
290         prerr_endline "Still cleaning the library: don't be impatient!"));
291    prerr_endline "Matita is cleaning up. Please wait.";
292    let baseuri = 
293      GrafiteTypes.get_baseuri (MatitaScript.current ())#grafite_status
294    in
295      LibraryClean.clean_baseuris [baseuri]
296
297 (* vim:set foldmethod=marker: *)