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