]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
snapshot (notably: ported to mysql instead of dbi)
[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 let _ =
131   gui#setQuitCallback quit;
132   gui#setPhraseCallback interpreter#evalPhrase;
133   gui#main#debugMenu#misc#hide ();
134   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
135    if has_proof () &&
136       not (ask_confirmation ~gui
137         ~msg:("Proof in progress, are you sure you want to start a new one?")
138         ())
139     then
140       ()  (* abort new proof process *)
141     else
142       let input = ask_text ~gui ~msg:"Insert proof goal" ~multiline:true () in
143       let (env, metasenv, expr) =
144         disambiguator#disambiguateTerm (Stream.of_string input)
145       in
146       let proof = MatitaProof.proof ~typ:expr ~metasenv () in
147       new_proof proof));
148   ignore (gui#main#openMenuItem#connect#activate (fun _ ->
149     match gui#chooseFile () with
150     | None -> ()
151     | Some f when is_proof_script f ->
152         gui#script#scriptTextView#buffer#set_text (input_file f);
153         gui#script#scriptWin#show ();
154         gui#main#showScriptMenuItem#set_active true
155     | Some f ->
156         gui#console#echo_error (sprintf
157           "Don't know what to do with file: %s\nUnrecognized file format."
158           f)))
159
160   (** <DEBUGGING> *)
161
162 let save_dom =
163   let domImpl = lazy (Gdome.domImplementation ()) in
164   fun ~doc ~dest ->
165     ignore
166       ((Lazy.force domImpl)#saveDocumentToFile ~doc ~name:dest ~indent:true ())
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 "interactive user uri choice" (fun _ ->
178       try
179         let uris =
180           interactive_user_uri_choice ~gui ~selection_mode:`MULTIPLE
181             ~msg:"messaggio" ~nonvars_button:true
182             ["cic:/uno.con"; "cic:/due.var"; "cic:/tre.con"; "cic:/quattro.con";
183             "cic:/cinque.var"]
184         in
185         List.iter prerr_endline uris
186       with MatitaGtkMisc.Cancel -> MatitaTypes.error "no choice");
187     addDebugItem "toggle auto disambiguation" (fun _ ->
188       Helm_registry.set_bool "matita.auto_disambiguation"
189         (not (Helm_registry.get_bool "matita.auto_disambiguation")));
190     addDebugItem "mono line text input" (fun _ ->
191       prerr_endline (ask_text ~gui ~title:"title" ~msg:"message" ()));
192     addDebugItem "multi line text input" (fun _ ->
193       prerr_endline
194         (ask_text ~gui ~title:"title" ~multiline:true ~msg:"message" ()));
195     addDebugItem "dump proof status to stdout" (fun _ ->
196       print_endline ((get_proof ())#toString));
197   end
198
199   (** </DEBUGGING> *)
200
201 let _ = GtkThread.main ()
202