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