]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
snapshot
[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 _ = Helm_registry.load_from "matita.conf.xml"
56 let _ = GMain.Main.init ()
57 let gui = new MatitaGui.gui (Helm_registry.get "matita.glade_file")
58 let parserr = new MatitaDisambiguator.parserr ()
59 let mqiconn = MQIConn.init ()
60 let disambiguator =
61   new MatitaDisambiguator.disambiguator ~parserr ~mqiconn
62     ~chooseUris:(interactive_user_uri_choice ~gui)
63     ~chooseInterp:(interactive_interp_choice ~gui)
64     ()
65 let proof_viewer =
66   let frame = GBin.frame ~packing:(gui#proof#scrolledProof#add) ~show:true () in
67   MatitaMathView.proof_viewer ~show:true ~packing:(frame#add) ()
68 let sequent_viewer = MatitaMathView.sequent_viewer ~show:true ()
69
70 let new_proof (proof: MatitaTypes.proof) =
71   let xmldump_observer _ _ =  print_endline proof#toString in
72   let proof_observer _ (status, metadata) =
73     debug_print "proof_observer";
74     let (acic, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
75         ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses) =
76       (fst metadata)
77     in
78     let ((uri_opt, _, _, _), _) = status in
79     let uri = MatitaTypes.unopt_uri uri_opt in
80     debug_print "apply transformation";
81     let mathml =
82       ApplyTransformation.mml_of_cic_object
83         ~explode_all:true uri acic ids_to_inner_sorts ids_to_inner_types
84     in
85     if BuildTimeConf.debug then save_dom ~doc:mathml ~dest:"/tmp/matita.xml";
86     proof_viewer#load_proof mathml metadata;
87     debug_print "/proof_observer"
88   in
89   let sequents_observer =
90     let pages = ref 0 in
91     let callback_id = ref None in
92     let mathmls = ref [] in
93     fun _ ((proof, goal_opt) as status, metadata) ->
94       debug_print "sequents_observer";
95       let notebook = gui#main#sequentsNotebook in
96       for i = 1 to !pages do
97         notebook#remove_page 0
98       done;
99       mathmls := [];
100       (match !callback_id with
101       | Some id -> GtkSignal.disconnect notebook#as_widget id
102       | None -> ());
103       if goal_opt <> None then begin
104         let sequents = snd metadata in
105         let sequents_no = List.length sequents in
106         debug_print (sprintf "sequents no: %d" sequents_no);
107         pages := sequents_no;
108         let widget = sequent_viewer#coerce in
109         List.iter
110           (fun (metano, (asequent, _, _, ids_to_inner_sorts, _)) ->
111             let tab_label =
112               (GMisc.label ~text:(sprintf "?%d" metano) ~show:true ())#coerce
113             in
114             notebook#append_page ~tab_label widget;
115             let mathml = lazy
116               (let content_sequent = Cic2content.map_sequent asequent in 
117               let pres_sequent = 
118                 Sequent2pres.sequent2pres ~ids_to_inner_sorts content_sequent
119               in
120               let xmlpres = Box.document_of_box pres_sequent in
121               Xml2Gdome.document_of_xml Misc.domImpl xmlpres)
122             in
123             mathmls := (metano, mathml) :: !mathmls)
124           sequents;
125         mathmls := List.rev !mathmls;
126         let render_page page =
127           let (metano, mathml) = List.nth !mathmls page in
128           sequent_viewer#load_sequent (Lazy.force mathml) metadata metano
129         in
130         callback_id :=
131           (* TODO Zack the "#after" may probably be removed after Luca's fix for
132           * widget not loading documents before being realized *)
133           Some (notebook#connect#after#switch_page ~callback:(fun page ->
134             debug_print "switch_page callback";
135             render_page page));
136         (match goal_opt with
137         | Some goal ->  (* current goal available, go to corresponding page *)
138             let page = ref 0 in
139             (try
140               List.iter
141                 (fun (metano, _) ->
142                   if (metano = goal) then raise Exit;
143                   incr page)
144                 sequents;
145             with Exit ->
146               debug_print (sprintf "going to page %d" !page);
147               notebook#goto_page !page;
148               render_page !page)
149         | None -> ());
150       end;
151       debug_print "/sequents_observer"
152   in
153   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
154       sequents_observer);
155   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
156       proof_observer);
157   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
158     xmldump_observer);
159   proof#notify;
160   set_proof (Some proof)
161
162 let quit () = (* quit program, asking for confirmation if needed *)
163   if not (has_proof ()) ||
164     (ask_confirmation ~gui
165       ~msg:("Proof in progress, are you sure you want to quit?") ())
166   then
167     GMain.Main.quit ()
168
169 let abort_proof () =
170   MatitaTypes.not_implemented "Matita.abort_proof"
171
172 let proof_handler =
173   { MatitaTypes.get_proof = get_proof;
174     MatitaTypes.abort_proof = abort_proof;
175     MatitaTypes.has_proof = has_proof;
176     MatitaTypes.new_proof = new_proof;
177     MatitaTypes.quit = quit;
178   }
179
180 let interpreter =
181   let console = gui#console in
182   new MatitaInterpreter.interpreter ~disambiguator ~proof_handler ~console ()
183
184 let _ =
185   gui#setQuitCallback quit;
186   gui#setPhraseCallback interpreter#evalPhrase;
187   gui#main#debugMenu#misc#hide ();
188   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
189    if has_proof () &&
190       not (ask_confirmation ~gui
191         ~msg:("Proof in progress, are you sure you want to start a new one?")
192         ())
193     then
194       ()  (* abort new proof process *)
195     else
196       let input = ask_text ~gui ~msg:"Insert proof goal" ~multiline:true () in
197       let (env, metasenv, expr) =
198         disambiguator#disambiguateTerm (Stream.of_string input)
199       in
200       let proof = MatitaProof.proof ~typ:expr ~metasenv () in
201       new_proof proof))
202
203   (** <DEBUGGING> *)
204 let _ =
205   if BuildTimeConf.debug then begin
206     gui#main#debugMenu#misc#show ();
207     let addDebugItem ~label callback =
208       let item =
209         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
210       in
211       ignore (item#connect#activate callback)
212     in
213     addDebugItem "interactive user uri choice" (fun _ ->
214       try
215         let uris =
216           interactive_user_uri_choice ~gui ~selection_mode:`MULTIPLE
217             ~msg:"messaggio" ~nonvars_button:true
218             ["cic:/uno.con"; "cic:/due.var"; "cic:/tre.con"; "cic:/quattro.con";
219             "cic:/cinque.var"]
220         in
221         List.iter prerr_endline uris
222       with MatitaGtkMisc.Cancel -> MatitaTypes.error "no choice");
223     addDebugItem "toggle auto disambiguation" (fun _ ->
224       Helm_registry.set_bool "matita.auto_disambiguation"
225         (not (Helm_registry.get_bool "matita.auto_disambiguation")));
226     addDebugItem "mono line text input" (fun _ ->
227       prerr_endline (ask_text ~gui ~title:"title" ~msg:"message" ()));
228     addDebugItem "multi line text input" (fun _ ->
229       prerr_endline
230         (ask_text ~gui ~title:"title" ~multiline:true ~msg:"message" ()));
231     addDebugItem "dump proof status to stdout" (fun _ ->
232       print_endline ((get_proof ())#toString));
233   end
234   (** </DEBUGGING> *)
235
236 let _ = GtkThread.main ()
237