]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
filled toolbar and implemented buttons behaviours
[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 Internal status} *)
33
34 let (get_proof, set_proof, has_proof) =
35   let (current_proof: MatitaTypes.proof option ref) = ref None in
36   ((fun () -> (* get_proof *)
37     match !current_proof with
38     | Some proof -> proof
39     | None -> failwith "No current proof"),
40    (fun proof ->  (* set_proof *)
41       current_proof := proof),
42    (fun () -> (* has_proof *)
43       !current_proof <> None))
44
45 (** {2 Initialization} *)
46
47 let _ =
48   Helm_registry.load_from "matita.conf.xml";
49   GtkMain.Rc.add_default_file BuildTimeConf.gtkrc;
50   GMain.Main.init ()
51 let parserr = new MatitaDisambiguator.parserr ()
52 let dbd =
53   Mysql.quick_connect
54     ~host:(Helm_registry.get "db.host")
55     ~user:(Helm_registry.get "db.user")
56     ~database:(Helm_registry.get "db.database")
57     ()
58 let gui = MatitaGui.instance ()
59 let disambiguator =
60   new MatitaDisambiguator.disambiguator ~parserr ~dbd
61     ~chooseUris:(interactive_user_uri_choice ~gui)
62     ~chooseInterp:(interactive_interp_choice ~gui)
63     ()
64 let proof_viewer =
65   let frame = GBin.frame ~packing:(gui#proof#scrolledProof#add) ~show:true () in
66   MatitaMathView.proof_viewer ~show:true ~packing:(frame#add) ()
67 let sequent_viewer = MatitaMathView.sequent_viewer ~show:true ()
68 let sequents_viewer =
69   let set_goal goal =
70     debug_print (sprintf "Setting goal %d" goal);
71     if not (has_proof ()) then assert false;
72     (get_proof ())#set_goal goal
73   in
74   MatitaMathView.sequents_viewer ~notebook:gui#main#sequentsNotebook
75     ~sequent_viewer ~set_goal ()
76
77 let new_proof (proof: MatitaTypes.proof) =
78   let xmldump_observer _ _ =  print_endline proof#toString in
79   let proof_observer _ (status, ()) =
80     debug_print "proof_observer";
81     let ((uri_opt, _, _, _), _) = status in
82     let uri = MatitaTypes.unopt_uri uri_opt in
83     debug_print "apply transformation";
84     proof_viewer#load_proof status;
85     debug_print "/proof_observer"
86   in
87   let sequents_observer _ (((_, metasenv, _, _), goal_opt), ()) =
88     sequents_viewer#reset;
89     (match goal_opt with
90     | None -> ()
91     | Some goal ->
92         sequents_viewer#load_sequents metasenv;
93         sequents_viewer#goto_sequent goal)
94   in
95   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
96     sequents_observer);
97   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
98     proof_observer);
99   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
100     xmldump_observer);
101   proof#notify;
102   set_proof (Some proof)
103
104 let quit () = (* quit program, asking for confirmation if needed *)
105   if not (has_proof ()) ||
106     (ask_confirmation ~gui
107       ~msg:("Proof in progress, are you sure you want to quit?") ())
108   then
109     GMain.Main.quit ()
110
111 let abort_proof () =
112   if has_proof () then begin
113     set_proof None;
114     sequents_viewer#reset
115   end
116
117 let proof_handler =
118   { MatitaTypes.get_proof = get_proof;
119     MatitaTypes.abort_proof = abort_proof;
120     MatitaTypes.set_proof = set_proof;
121     MatitaTypes.has_proof = has_proof;
122     MatitaTypes.new_proof = new_proof;
123     MatitaTypes.quit = quit;
124   }
125
126 let interpreter =
127   let console = gui#console in
128   new MatitaInterpreter.interpreter ~disambiguator ~proof_handler ~console ()
129
130   (** prompt the user for the textual input of a term and disambiguate it *)
131 let ask_term ?(title = "term input") ?(msg = "insert term") () =
132   match gui#askText ~title ~msg () with
133   | Some t ->
134       let (_, _, term) = disambiguator#disambiguateTerm (Stream.of_string t) in
135       Some term
136   | None -> None
137
138 let _ =
139   gui#setQuitCallback quit;
140   gui#setPhraseCallback interpreter#evalPhrase;
141   gui#main#debugMenu#misc#hide ();
142   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
143    if has_proof () &&
144       not (ask_confirmation ~gui
145         ~msg:("Proof in progress, are you sure you want to start a new one?")
146         ())
147     then
148       ()  (* abort new proof process *)
149     else
150       let input = ask_text ~gui ~msg:"Insert proof goal" ~multiline:true () in
151       let (env, metasenv, expr) =
152         disambiguator#disambiguateTerm (Stream.of_string input)
153       in
154       let proof = MatitaProof.proof ~typ:expr ~metasenv () in
155       new_proof proof));
156   ignore (gui#main#openMenuItem#connect#activate (fun _ ->
157     match gui#chooseFile () with
158     | None -> ()
159     | Some f when is_proof_script f ->
160         gui#script#scriptTextView#buffer#set_text (input_file f);
161         gui#script#scriptWin#show ();
162         gui#main#showScriptMenuItem#set_active true
163     | Some f ->
164         gui#console#echo_error (sprintf
165           "Don't know what to do with file: %s\nUnrecognized file format."
166           f)));
167   let tac_w_term name tac _ =
168     match ask_term ~title:name ~msg:("term for " ^ name) () with
169     | Some term -> (get_proof ())#apply_tactic (tac ~term)
170     | None -> ()
171   in
172   let tac _ tac _ = (get_proof ())#apply_tactic tac in
173   let tbar = gui#toolbar in
174   ignore (tbar#introsButton#connect#clicked (tac "intros" (Tactics.intros ())));
175   ignore (tbar#applyButton#connect#clicked (tac_w_term "apply" Tactics.apply));
176   ignore (tbar#exactButton#connect#clicked (tac_w_term "exact" Tactics.exact));
177   ignore (tbar#elimButton#connect#clicked (tac_w_term "elim"
178     Tactics.elim_intros_simpl));
179   ignore (tbar#elimTypeButton#connect#clicked (tac_w_term "elim type"
180     Tactics.elim_type));
181   ignore (tbar#splitButton#connect#clicked (tac "split" Tactics.split));
182   ignore (tbar#leftButton#connect#clicked (tac "left" Tactics.left));
183   ignore (tbar#rightButton#connect#clicked (tac "right" Tactics.right));
184   ignore (tbar#existsButton#connect#clicked (tac "exists" Tactics.exists));
185   ignore (tbar#reflexivityButton#connect#clicked (tac "reflexivity"
186     Tactics.reflexivity));
187   ignore (tbar#symmetryButton#connect#clicked (tac "symmetry"
188     Tactics.symmetry));
189   ignore (tbar#transitivityButton#connect#clicked (tac_w_term "transitivity"
190     Tactics.transitivity));
191   ignore (tbar#assumptionButton#connect#clicked (tac "assumption"
192     Tactics.assumption));
193   ignore (tbar#cutButton#connect#clicked (tac_w_term "cut"
194     (Tactics.cut ?mk_fresh_name_callback:None)));
195   ignore (tbar#autoButton#connect#clicked (tac "auto" (Tactics.auto ~dbd)))
196
197   (** <DEBUGGING> *)
198
199 let save_dom =
200   let domImpl = lazy (Gdome.domImplementation ()) in
201   fun ~doc ~dest ->
202     ignore
203       ((Lazy.force domImpl)#saveDocumentToFile ~doc ~name:dest ~indent:true ())
204
205 let _ =
206   if BuildTimeConf.debug then begin
207     gui#main#debugMenu#misc#show ();
208     let addDebugItem ~label callback =
209       let item =
210         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
211       in
212       ignore (item#connect#activate callback)
213     in
214     addDebugItem "interactive user uri choice" (fun _ ->
215       try
216         let uris =
217           interactive_user_uri_choice ~gui ~selection_mode:`MULTIPLE
218             ~msg:"messaggio" ~nonvars_button:true
219             ["cic:/uno.con"; "cic:/due.var"; "cic:/tre.con"; "cic:/quattro.con";
220             "cic:/cinque.var"]
221         in
222         List.iter prerr_endline uris
223       with MatitaGtkMisc.Cancel -> MatitaTypes.error "no choice");
224     addDebugItem "toggle auto disambiguation" (fun _ ->
225       Helm_registry.set_bool "matita.auto_disambiguation"
226         (not (Helm_registry.get_bool "matita.auto_disambiguation")));
227     addDebugItem "mono line text input" (fun _ ->
228       prerr_endline (ask_text ~gui ~title:"title" ~msg:"message" ()));
229     addDebugItem "multi line text input" (fun _ ->
230       prerr_endline
231         (ask_text ~gui ~title:"title" ~multiline:true ~msg:"message" ()));
232     addDebugItem "dump proof status to stdout" (fun _ ->
233       print_endline ((get_proof ())#toString));
234   end
235
236   (** </DEBUGGING> *)
237
238 let _ = GtkThread.main ()
239