]> 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 module DB = BuildTimeConf.DB
33
34 (** {2 Internal status} *)
35
36 let (get_proof, set_proof, has_proof) =
37   let (current_proof: MatitaTypes.proof option ref) = ref None in
38   ((fun () -> (* get_proof *)
39     match !current_proof with
40     | Some proof -> proof
41     | None -> failwith "No current proof"),
42    (fun proof ->  (* set_proof *)
43       current_proof := proof),
44    (fun () -> (* has_proof *)
45       !current_proof <> None))
46
47 (** {2 Initialization} *)
48
49 let _ =
50   Helm_registry.load_from "matita.conf.xml";
51   GtkMain.Rc.add_default_file BuildTimeConf.gtkrc;
52   GMain.Main.init ()
53 let parserr = new MatitaDisambiguator.parserr ()
54 let dbh =
55   new DB.connection ~host:(Helm_registry.get "db.host")
56     ~user:(Helm_registry.get "db.user") (Helm_registry.get "db.database")
57 let gui = MatitaGui.instance ()
58 let disambiguator =
59   new MatitaDisambiguator.disambiguator ~parserr ~dbh
60     ~chooseUris:(interactive_user_uri_choice ~gui)
61     ~chooseInterp:(interactive_interp_choice ~gui)
62     ()
63 let proof_viewer =
64   let frame = GBin.frame ~packing:(gui#proof#scrolledProof#add) ~show:true () in
65   MatitaMathView.proof_viewer ~show:true ~packing:(frame#add) ()
66 let sequent_viewer = MatitaMathView.sequent_viewer ~show:true ()
67 let sequents_viewer =
68   let set_goal goal =
69     debug_print (sprintf "Setting goal %d" goal);
70     if not (has_proof ()) then assert false;
71     (get_proof ())#set_goal goal
72   in
73   MatitaMathView.sequents_viewer ~notebook:gui#main#sequentsNotebook
74     ~sequent_viewer ~set_goal ()
75
76 let new_proof (proof: MatitaTypes.proof) =
77   let xmldump_observer _ _ =  print_endline proof#toString in
78   let proof_observer _ (status, ()) =
79     debug_print "proof_observer";
80     let ((uri_opt, _, _, _), _) = status in
81     let uri = MatitaTypes.unopt_uri uri_opt in
82     debug_print "apply transformation";
83     proof_viewer#load_proof status;
84     debug_print "/proof_observer"
85   in
86   let sequents_observer _ (((_, metasenv, _, _), goal_opt), ()) =
87     sequents_viewer#reset;
88     (match goal_opt with
89     | None -> ()
90     | Some goal ->
91         sequents_viewer#load_sequents metasenv;
92         sequents_viewer#goto_sequent goal)
93   in
94   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
95     sequents_observer);
96   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
97     proof_observer);
98   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
99     xmldump_observer);
100   proof#notify;
101   set_proof (Some proof)
102
103 let quit () = (* quit program, asking for confirmation if needed *)
104   if not (has_proof ()) ||
105     (ask_confirmation ~gui
106       ~msg:("Proof in progress, are you sure you want to quit?") ())
107   then
108     GMain.Main.quit ()
109
110 let abort_proof () =
111   if has_proof () then begin
112     set_proof None;
113     sequents_viewer#reset
114   end
115
116 let proof_handler =
117   { MatitaTypes.get_proof = get_proof;
118     MatitaTypes.abort_proof = abort_proof;
119     MatitaTypes.set_proof = set_proof;
120     MatitaTypes.has_proof = has_proof;
121     MatitaTypes.new_proof = new_proof;
122     MatitaTypes.quit = quit;
123   }
124
125 let interpreter =
126   let console = gui#console in
127   new MatitaInterpreter.interpreter ~disambiguator ~proof_handler ~console ()
128
129 let _ =
130   gui#setQuitCallback quit;
131   gui#setPhraseCallback interpreter#evalPhrase;
132   gui#main#debugMenu#misc#hide ();
133   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
134    if has_proof () &&
135       not (ask_confirmation ~gui
136         ~msg:("Proof in progress, are you sure you want to start a new one?")
137         ())
138     then
139       ()  (* abort new proof process *)
140     else
141       let input = ask_text ~gui ~msg:"Insert proof goal" ~multiline:true () in
142       let (env, metasenv, expr) =
143         disambiguator#disambiguateTerm (Stream.of_string input)
144       in
145       let proof = MatitaProof.proof ~typ:expr ~metasenv () in
146       new_proof proof));
147   ignore (gui#main#openMenuItem#connect#activate (fun _ ->
148     match gui#chooseFile () with
149     | None -> ()
150     | Some f when is_proof_script f ->
151         gui#script#scriptTextView#buffer#set_text (input_file f)
152     | Some f ->
153         gui#console#echo_error (sprintf
154           "Don't know what to do with file: %s\nUnrecognized file format." f)))
155
156   (** <DEBUGGING> *)
157
158 let save_dom =
159   let domImpl = lazy (Gdome.domImplementation ()) in
160   fun ~doc ~dest ->
161     ignore
162       ((Lazy.force domImpl)#saveDocumentToFile ~doc ~name:dest ~indent:true ())
163
164 let _ =
165   if BuildTimeConf.debug then begin
166     gui#main#debugMenu#misc#show ();
167     let addDebugItem ~label callback =
168       let item =
169         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
170       in
171       ignore (item#connect#activate callback)
172     in
173     addDebugItem "interactive user uri choice" (fun _ ->
174       try
175         let uris =
176           interactive_user_uri_choice ~gui ~selection_mode:`MULTIPLE
177             ~msg:"messaggio" ~nonvars_button:true
178             ["cic:/uno.con"; "cic:/due.var"; "cic:/tre.con"; "cic:/quattro.con";
179             "cic:/cinque.var"]
180         in
181         List.iter prerr_endline uris
182       with MatitaGtkMisc.Cancel -> MatitaTypes.error "no choice");
183     addDebugItem "toggle auto disambiguation" (fun _ ->
184       Helm_registry.set_bool "matita.auto_disambiguation"
185         (not (Helm_registry.get_bool "matita.auto_disambiguation")));
186     addDebugItem "mono line text input" (fun _ ->
187       prerr_endline (ask_text ~gui ~title:"title" ~msg:"message" ()));
188     addDebugItem "multi line text input" (fun _ ->
189       prerr_endline
190         (ask_text ~gui ~title:"title" ~multiline:true ~msg:"message" ()));
191     addDebugItem "dump proof status to stdout" (fun _ ->
192       print_endline ((get_proof ())#toString));
193   end
194
195   (** </DEBUGGING> *)
196
197 let _ = GtkThread.main ()
198