]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
added script support a la coqide
[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";  (* read conf *)
36   Http_getter.init ();
37   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
38   MatitaDb.clean_owner_environment ();
39   MatitaDb.create_owner_environment ();
40   GtkMain.Rc.add_default_file BuildTimeConf.gtkrc;  (* loads gtk rc files *)
41   ignore (GMain.Main.init ())
42
43 let gui = MatitaGui.instance ()
44 let disambiguator = MatitaDisambiguator.instance ()
45 let _ = (* set disambiguator callbacks *)
46   disambiguator#setChooseUris (interactive_user_uri_choice ~gui);
47   disambiguator#setChooseInterp (interactive_interp_choice ~gui)
48
49 let _ = (* environment trust *)
50   CicEnvironment.set_trust
51     (let trust = Helm_registry.get_bool "matita.environment_trust" in
52      fun _ -> trust)
53
54 let currentProof = MatitaProof.instance ()
55
56 let sequent_viewer = MatitaMathView.sequent_viewer_instance ()
57 let sequents_viewer = MatitaMathView.sequents_viewer_instance ()
58 let _ = (* attach observers to proof status *)
59   let browser_observer _ _ = MatitaMathView.refresh_all_browsers () in
60   let sequents_observer _ (((_, metasenv, _, _), goal_opt), ()) =
61     sequents_viewer#reset;
62     (match goal_opt with
63     | None -> ()
64     | Some goal ->
65         sequents_viewer#load_sequents metasenv;
66         sequents_viewer#goto_sequent goal)
67   in
68   currentProof#addObserver sequents_observer;
69   currentProof#addObserver browser_observer;
70   currentProof#connect `Quit (fun () ->
71     (* quit program, asking for confirmation if needed *)
72     if not (currentProof#onGoing ()) ||
73       (ask_confirmation ~gui
74         ~msg:("Proof in progress, are you sure you want to quit?") ())
75     then
76       GMain.Main.quit ();
77     false);
78   currentProof#connect `Abort (fun () -> sequents_viewer#reset; false)
79
80 let interpreter =
81   let mathViewer = MatitaMathView.mathViewer () in
82   MatitaInterpreter.interpreter ~console:gui#console ~mathViewer ()
83 let script = MatitaScript.script ~interpreter
84 let _ =
85   let href_callback uri =
86     let term = CicAst.Uri (uri, None) in
87     ignore (interpreter#evalAst (TacticAst.Command (TacticAst.Check term)))
88   in
89   sequent_viewer#set_href_callback (Some href_callback)
90
91 let console_callback s =
92   let module A = TacticAst in
93   let rec strip_locations = function
94     | A.LocatedTactical (loc, tac) -> strip_locations tac
95     | tac -> tac
96   in
97   let needed_by_script ast =
98     prerr_endline (TacticAstPp.pp_tactical ast);
99     match strip_locations ast with
100     | A.Tactic _
101     | A.Command
102       (A.Inductive _ | A.Theorem _ | A.Coercion _ | A.Qed _ | A.Proof) ->
103         true
104     | _ -> false
105   in
106   let ast = disambiguator#parserr#parseTactical (Stream.of_string s) in
107   if needed_by_script ast then
108     script#advance ast
109   else
110     interpreter#evalAst ast
111
112 let console_callback s =
113   match gui#console#wrap_exn (fun () -> console_callback s) with
114   | None -> (false, false)
115   | Some outcome -> outcome
116
117 (** {2 GUI callbacks} *)
118
119 let _ =
120   gui#setQuitCallback currentProof#quit;
121   gui#setPhraseCallback console_callback;
122   gui#main#debugMenu#misc#hide ();
123   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
124     gui#console#clear ();
125     gui#console#show ~msg:"theorem " ()));
126   ignore (gui#main#openMenuItem#connect#activate (fun _ ->
127     match gui#chooseFile () with
128     | None -> ()
129     | Some f when is_proof_script f ->
130         ignore (gui#console#wrap_exn (fun () -> script#loadFrom f))
131     | Some f ->
132         gui#console#echo_error (sprintf
133           "Don't know what to do with file: %s\nUnrecognized file format."
134           f)));
135   ignore (gui#main#newCicBrowserMenuItem#connect#activate (fun _ ->
136     ignore (MatitaMathView.cicBrowser ())));
137   let module A = TacticAst in
138   let hole = CicAst.UserInput in
139   let tac ast _ =
140     let ast = A.Tactic ast in
141     ignore (interpreter#evalAst ast);
142     ignore (script#advance ast)
143   in
144   let tac_w_term ast _ =
145 (*     gui#console#clear (); *)
146     gui#console#show ~msg:(TacticAstPp.pp_tactic ast) ();
147     gui#main#mainWin#present ()
148   in
149   let tbar = gui#toolbar in
150   connect_button tbar#introsButton (tac (A.Intros (None, [])));
151   connect_button tbar#applyButton (tac_w_term (A.Apply hole));
152   connect_button tbar#exactButton (tac_w_term (A.Exact hole));
153   connect_button tbar#elimButton (tac_w_term (A.Elim (hole, None)));
154   connect_button tbar#elimTypeButton (tac_w_term (A.ElimType hole));
155   connect_button tbar#splitButton (tac A.Split);
156   connect_button tbar#leftButton (tac A.Left);
157   connect_button tbar#rightButton (tac A.Right);
158   connect_button tbar#existsButton (tac A.Exists);
159   connect_button tbar#reflexivityButton (tac A.Reflexivity);
160   connect_button tbar#symmetryButton (tac A.Symmetry);
161   connect_button tbar#transitivityButton (tac_w_term (A.Transitivity hole));
162   connect_button tbar#assumptionButton (tac A.Assumption);
163   connect_button tbar#cutButton (tac_w_term (A.Cut hole));
164   connect_button tbar#autoButton (tac A.Auto)
165
166   (** <DEBUGGING> *)
167
168 let _ =
169   if BuildTimeConf.debug then begin
170     gui#main#debugMenu#misc#show ();
171     let addDebugItem ~label callback =
172       let item =
173         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
174       in
175       ignore (item#connect#activate callback)
176     in
177     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
178       let oc = open_out "env.dump" in
179       CicEnvironment.dump_to_channel oc;
180       close_out oc);
181     addDebugItem "print selected terms" (fun () ->
182       let i = ref 0 in
183       List.iter
184         (fun t -> incr i; debug_print (sprintf "%d: %s" !i (CicPp.ppterm t)))
185         sequent_viewer#get_selected_terms);
186     addDebugItem "dump getter settings" (fun _ ->
187       prerr_endline (Http_getter_env.env_to_string ()));
188     addDebugItem "getter: update" Http_getter.update;
189     addDebugItem "getter: getalluris" (fun _ ->
190       List.iter prerr_endline (Http_getter.getalluris ()));
191   end
192
193   (** </DEBUGGING> *)
194
195 let _ =
196   (try script#loadFrom Sys.argv.(1) with Invalid_argument _ -> ());
197   if Filename.basename Sys.argv.(0) = "cicbrowser" then begin (* cicbrowser *)
198     Helm_registry.set "matita.mode" "cicbrowser";
199     let browser = MatitaMathView.cicBrowser () in
200     try
201       browser#loadUri Sys.argv.(1)
202     with Invalid_argument _ -> ()
203   end else begin  (* matita *)
204     Helm_registry.set "matita.mode" "matita";
205     gui#main#mainWin#show ();
206     gui#toolbar#toolBarWin#show ();
207     gui#console#show ()
208   end;
209   GtkThread.main ()
210