]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
snapshot, notably:
[helm.git] / helm / matita / matita.ml
1 (* Copyright (C) 2004, 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 "matita.conf.xml";
36   GtkMain.Rc.add_default_file BuildTimeConf.gtkrc;
37   GMain.Main.init ()
38
39 let parserr = new MatitaDisambiguator.parserr ()
40 let dbd =
41   Mysql.quick_connect
42     ~host:(Helm_registry.get "db.host")
43     ~user:(Helm_registry.get "db.user")
44     ~database:(Helm_registry.get "db.database")
45     ()
46 let _ = MetadataDb.clean ~dbd ~owner:(Helm_registry.get "matita.owner")
47 let gui = MatitaGui.instance ()
48 let disambiguator =
49   new MatitaDisambiguator.disambiguator ~parserr ~dbd
50     ~chooseUris:(interactive_user_uri_choice ~gui)
51     ~chooseInterp:(interactive_interp_choice ~gui)
52     ()
53
54 let currentProof = new MatitaProof.currentProof
55
56 let sequent_viewer = MatitaMathView.sequent_viewer ~show:true ()
57 let sequents_viewer =
58   let set_goal goal =
59     if not (currentProof#onGoing ()) then assert false;
60     currentProof#proof#set_goal goal
61   in
62   MatitaMathView.sequents_viewer ~notebook:gui#main#sequentsNotebook
63     ~sequent_viewer ~set_goal ()
64 let _ = (* attach observers to proof status *)
65   let browser_observer _ _ = MatitaMathView.refresh_all_browsers () in
66   let sequents_observer _ (((_, metasenv, _, _), goal_opt), ()) =
67     sequents_viewer#reset;
68     (match goal_opt with
69     | None -> ()
70     | Some goal ->
71         sequents_viewer#load_sequents metasenv;
72         sequents_viewer#goto_sequent goal)
73   in
74   currentProof#addObserver sequents_observer;
75   currentProof#addObserver browser_observer;
76   currentProof#connect `Quit (fun () ->
77     (* quit program, asking for confirmation if needed *)
78     if not (currentProof#onGoing ()) ||
79       (ask_confirmation ~gui
80         ~msg:("Proof in progress, are you sure you want to quit?") ())
81     then
82       GMain.Main.quit ();
83     false);
84   currentProof#connect `Abort (fun () -> sequents_viewer#reset; false)
85
86 let currentProof = (currentProof :> MatitaTypes.currentProof)
87 let mathViewer =
88   MatitaMathView.mathViewer ~disambiguator
89     ~currentProof:(currentProof :> MatitaTypes.currentProof) ()
90 let interpreter =
91   let console = (gui#console :> MatitaTypes.console) in
92   new MatitaInterpreter.interpreter
93     ~disambiguator ~currentProof:(currentProof :> MatitaTypes.currentProof)
94     ~console ~mathViewer ~dbd ()
95 let _ =
96   let href_callback uri =
97     let term = CicAst.Uri (UriManager.string_of_uri uri, None) in
98     ignore (interpreter#evalAst (TacticAst.Command (TacticAst.Check term)))
99   in
100   sequent_viewer#set_href_callback (Some href_callback)
101
102 (** {2 Script window handling} *)
103
104 let script_forward _ =
105   let buf = gui#script#scriptTextView#buffer in
106   let locked_iter = buf#get_iter_at_mark (`NAME "locked") in
107   let (success, hide) =
108     interpreter#evalPhrase
109       (buf#get_text ~start:locked_iter ~stop:buf#end_iter ())
110   in
111   if success then
112     gui#lockScript (locked_iter#offset + interpreter#endOffset)
113
114 let script_jump _ =
115   let buf = gui#script#scriptTextView#buffer in
116   let locked_iter = buf#get_iter_at_mark (`NAME "locked") in
117   let cursor_iter = buf#get_iter_at_mark (`NAME "insert") in
118   let raw_text = buf#get_text ~start:locked_iter ~stop:cursor_iter () in
119   let len = String.length raw_text in
120   let rec parse offset =
121     if offset < len then begin
122       let (success, hide) =
123         interpreter#evalPhrase (String.sub raw_text offset (len - offset))
124       in
125       if success then begin
126         let new_offset = interpreter#endOffset + offset in
127         gui#lockScript (new_offset + locked_iter#offset);
128         parse new_offset
129       end else
130         raise Exit
131     end
132   in
133   try
134     parse 0
135   with Exit -> ()
136
137 let script_back _ = not_implemented "script_back"
138
139 let load_script fname =
140   gui#script#scriptTextView#buffer#set_text (input_file fname);
141   gui#script#scriptWin#show ();
142   gui#main#showScriptMenuItem#set_active true
143
144 (** {2 GUI callbacks} *)
145
146 let _ =
147   gui#setQuitCallback currentProof#quit;
148   gui#setPhraseCallback interpreter#evalPhrase;
149   gui#main#debugMenu#misc#hide ();
150   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
151     gui#console#clear ();
152     gui#console#show ~msg:"theorem " ()));
153   ignore (gui#main#openMenuItem#connect#activate (fun _ ->
154     match gui#chooseFile () with
155     | None -> ()
156     | Some f when is_proof_script f -> load_script f
157     | Some f ->
158         gui#console#echo_error (sprintf
159           "Don't know what to do with file: %s\nUnrecognized file format."
160           f)));
161   ignore (gui#main#newCicBrowserMenuItem#connect#activate (fun _ ->
162     let currentProof = (currentProof :> MatitaTypes.currentProof) in
163     ignore (MatitaMathView.cicBrowser ~disambiguator ~currentProof ())));
164   connect_button gui#script#scriptWinForwardButton script_forward;
165   connect_button gui#script#scriptWinBackButton script_back;
166   connect_button gui#script#scriptWinJumpButton script_jump;
167   let module A = TacticAst in
168   let hole = CicAst.UserInput in
169   let tac ast _ = ignore (interpreter#evalAst (A.Tactic ast)) in
170   let tac_w_term ast _ =
171 (*     gui#console#clear (); *)
172     gui#console#show ~msg:(TacticAstPp.pp_tactic ast) ();
173     gui#main#mainWin#present ()
174   in
175   let tbar = gui#toolbar in
176   connect_button tbar#introsButton (tac (A.Intros (None, [])));
177   connect_button tbar#applyButton (tac_w_term (A.Apply hole));
178   connect_button tbar#exactButton (tac_w_term (A.Exact hole));
179   connect_button tbar#elimButton (tac_w_term (A.Elim (hole, None)));
180   connect_button tbar#elimTypeButton (tac_w_term (A.ElimType hole));
181   connect_button tbar#splitButton (tac A.Split);
182   connect_button tbar#leftButton (tac A.Left);
183   connect_button tbar#rightButton (tac A.Right);
184   connect_button tbar#existsButton (tac A.Exists);
185   connect_button tbar#reflexivityButton (tac A.Reflexivity);
186   connect_button tbar#symmetryButton (tac A.Symmetry);
187   connect_button tbar#transitivityButton (tac_w_term (A.Transitivity hole));
188   connect_button tbar#assumptionButton (tac A.Assumption);
189   connect_button tbar#cutButton (tac_w_term (A.Cut hole));
190   connect_button tbar#autoButton (tac A.Auto)
191
192   (** <DEBUGGING> *)
193
194 let _ =
195   if BuildTimeConf.debug then begin
196     gui#main#debugMenu#misc#show ();
197     let addDebugItem ~label callback =
198       let item =
199         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
200       in
201       ignore (item#connect#activate callback)
202     in
203     addDebugItem "toggle auto disambiguation" (fun _ ->
204       Helm_registry.set_bool "matita.auto_disambiguation"
205         (not (Helm_registry.get_bool "matita.auto_disambiguation")));
206     addDebugItem "dump proof status to stdout" (fun _ ->
207       print_endline (currentProof#proof#toString));
208     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
209       let oc = open_out "env.dump" in
210       CicEnvironment.dump_to_channel oc;
211       close_out oc);
212     addDebugItem "print selected terms" (fun () ->
213       let i = ref 0 in
214       List.iter
215         (fun t -> incr i; debug_print (sprintf "%d: %s" !i (CicPp.ppterm t)))
216         sequent_viewer#get_selected_terms);
217     addDebugItem "refresh all browsers" MatitaMathView.refresh_all_browsers;
218   end
219
220   (** </DEBUGGING> *)
221
222 let _ =
223 (*   CicEnvironment.set_trust (fun _ -> false); *)
224   (try
225     load_script Sys.argv.(1)
226   with Invalid_argument _ -> ());
227   gui#console#show ();
228   GtkThread.main ()
229