]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
- added support for -debug, which avoid catching top level exceptions (still useless...
[helm.git] / helm / 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 open Printf
27
28 open MatitaGtkMisc
29 open MatitaTypes
30 open MatitaMisc
31
32 (** {2 Initialization} *)
33
34 let _ =
35   Helm_registry.load_from BuildTimeConf.matita_conf;
36   CicNotation.load_notation BuildTimeConf.core_notation_script;
37   Http_getter.init ();
38   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
39   MatitaDb.create_owner_environment ();
40   MatitamakeLib.initialize ();
41   CicEnvironment.set_trust (* environment trust *)
42     (let trust = Helm_registry.get_bool "matita.environment_trust" in
43      fun _ -> trust);
44   Paramodulation.Saturation.init ()
45
46 (** {2 GUI callbacks} *)
47
48 let gui = MatitaGui.instance ()
49
50 let script =
51   let s = 
52     MatitaScript.script 
53       ~source_view:gui#sourceView
54       ~init:(Lazy.force MatitaEngine.initial_status) 
55       ~mathviewer:(MatitaMathView.mathViewer ())
56       ~urichooser:(fun uris ->
57         try
58           MatitaGui.interactive_uri_choice ~selection_mode:`SINGLE
59           ~title:"Matita: URI chooser" 
60           ~msg:"Select the URI" ~hide_uri_entry:true
61           ~hide_try:true ~ok_label:"_Apply" ~ok_action:`SELECT
62           ~copy_cb:(fun s -> gui#sourceView#buffer#insert ("\n"^s^"\n"))
63           () ~id:"boh?" uris
64         with MatitaTypes.Cancel -> [])
65       ~set_star:gui#setStar
66       ~ask_confirmation:
67         (fun ~title ~message -> 
68             MatitaGtkMisc.ask_confirmation ~title ~message 
69             ~parent:gui#main#toplevel ())
70       ~develcreator:gui#createDevelopment
71       ()
72   in
73   gui#sourceView#source_buffer#begin_not_undoable_action ();
74   s#reset (); 
75   s#template (); 
76   gui#sourceView#source_buffer#end_not_undoable_action ();
77   s
78   
79   (* math viewers *)
80 let _ =
81   let cic_math_view = MatitaMathView.cicMathView_instance () in
82   let sequents_viewer = MatitaMathView.sequentsViewer_instance () in
83   sequents_viewer#load_logo;
84   cic_math_view#set_href_callback
85     (Some (fun uri -> (MatitaMathView.cicBrowser ())#load
86       (`Uri (UriManager.uri_of_string uri))));
87   let browser_observer _ = MatitaMathView.refresh_all_browsers () in
88   let sequents_observer status =
89     sequents_viewer#reset;
90     match status.proof_status with
91     | Incomplete_proof ((proof, goal) as status) ->
92         sequents_viewer#load_sequents status;
93         sequents_viewer#goto_sequent goal
94     | Proof proof -> sequents_viewer#load_logo_with_qed
95     | No_proof -> sequents_viewer#load_logo
96     | Intermediate _ -> assert false (* only the engine may be in this state *)
97   in
98   script#addObserver sequents_observer;
99   script#addObserver browser_observer
100
101   (** {{{ Debugging *)
102 let _ =
103   if BuildTimeConf.debug then begin
104     gui#main#debugMenu#misc#show ();
105     let addDebugItem ~label callback =
106       let item =
107         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
108       in
109       ignore (item#connect#activate callback)
110     in
111     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
112       let oc = open_out "env.dump" in
113       CicEnvironment.dump_to_channel oc;
114       close_out oc);
115     addDebugItem "load environment from \"env.dump\"" (fun _ ->
116       let ic = open_in "env.dump" in
117       CicEnvironment.restore_from_channel ic;
118       close_in ic);
119     addDebugItem "dump universes" (fun _ ->
120       List.iter (fun (u,_,g) -> 
121         prerr_endline (UriManager.string_of_uri u); 
122         CicUniv.print_ugraph g) (CicEnvironment.list_obj ())
123       );
124     addDebugItem "dump environment content" (fun _ ->
125       List.iter (fun (u,_,_) -> 
126         prerr_endline (UriManager.string_of_uri u)) 
127         (CicEnvironment.list_obj ()));
128     addDebugItem "print selections" (fun () ->
129       let cicMathView = MatitaMathView.cicMathView_instance () in
130       List.iter MatitaLog.debug (cicMathView#string_of_selections));
131     addDebugItem "dump getter settings" (fun _ ->
132       prerr_endline (Http_getter_env.env_to_string ()));
133     addDebugItem "getter: getalluris" (fun _ ->
134       List.iter prerr_endline (Http_getter.getalluris ()));
135     addDebugItem "dump script status" script#dump;
136     addDebugItem "dump metasenv"
137       (fun _ ->
138          if script#onGoingProof () then
139            MatitaLog.debug (CicMetaSubst.ppmetasenv [] script#proofMetasenv));
140     addDebugItem "dump coercions Db" (fun _ ->
141       List.iter
142         (fun (s,t,u) -> 
143           MatitaLog.debug
144             (UriManager.name_of_uri u ^ ":"
145              ^ UriManager.name_of_uri s ^ " -> " ^ UriManager.name_of_uri t))
146         (CoercDb.to_list ()));
147     addDebugItem "print top-level grammar entries"
148       CicNotationParser.print_l2_pattern;
149     addDebugItem "dump moo to stderr" (fun _ ->
150       let status = (MatitaScript.instance ())#status in
151       List.iter (fun cmd -> prerr_endline
152         (GrafiteAstPp.pp_command cmd)) (List.rev status.moo_content_rev));
153     addDebugItem "rotate light bulbs"
154       (fun _ ->
155          let nb = gui#main#hintNotebook in
156          nb#goto_page ((nb#current_page + 1) mod 3));
157   end
158   (** Debugging }}} *)
159
160   (** {2 Command line parsing} *)
161
162 let debug = ref false
163 let args = ref []
164 let add_arg arg = args := arg :: !args
165
166 let arg_spec =
167   let std_arg_spec = [] in
168   let debug_arg_spec =
169     if BuildTimeConf.debug then
170       [ "-debug", Arg.Set debug,
171         "Do not catch top-level exception (useful for backtrace inspection)"; ]
172     else []
173   in
174   std_arg_spec @ debug_arg_spec
175
176 let usage () =
177   let heading = sprintf "Matita v%s\nUsage: " BuildTimeConf.version in
178   if Helm_registry.get "matita.mode" = "cicbrowser" then
179     heading ^ "cicbrowser [ URL | Whelp query ]\nOptions:"
180   else
181     heading ^  "matita [ FILE ]"
182
183 let set_matita_mode () =
184   let matita_mode =
185     if Filename.basename Sys.argv.(0) = "cicbrowser"
186     then "cicbrowser"
187     else "matita"
188   in
189   Helm_registry.set "matita.mode" matita_mode
190
191   (** {2 Main} *)
192
193 let _ =
194   set_matita_mode ();
195   Arg.parse arg_spec add_arg (usage ());
196   Helm_registry.set_bool "matita.catch_top_level_exn" (not !debug);
197   at_exit (fun () -> print_endline "\nThanks for using Matita!\n");
198   Sys.catch_break true;
199   if Helm_registry.get "matita.mode" = "cicbrowser" then  (* cicbrowser *)
200     let browser = MatitaMathView.cicBrowser () in
201     let uri = match !args with [] -> "cic:/" | _ -> String.concat " " !args in
202     browser#loadInput uri
203   else begin  (* matita *)
204     (try gui#loadScript (List.hd !args) with Failure _ -> ());
205     gui#main#mainWin#show ();
206   end;
207   try
208     GtkThread.main ()
209   with Sys.Break -> ()
210
211 (* vim:set foldmethod=marker: *)