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