]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matita.ml
snapshot (notably: implemented "check")
[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   MatitaMathView.sequents_viewer ~notebook:gui#main#sequentsNotebook
74     ~sequent_viewer ()
75
76 let new_proof (proof: MatitaTypes.proof) =
77   let xmldump_observer _ _ =  print_endline proof#toString in
78   let proof_observer _ (status, (proof_metadata, _)) =
79     debug_print "proof_observer";
80     let (acic, ids_to_terms, ids_to_father_ids, ids_to_inner_sorts,
81         ids_to_inner_types, ids_to_conjectures, ids_to_hypotheses) =
82       proof_metadata
83     in
84     let ((uri_opt, _, _, _), _) = status in
85     let uri = MatitaTypes.unopt_uri uri_opt in
86     debug_print "apply transformation";
87     let mathml =
88       ApplyTransformation.mml_of_cic_object
89         ~explode_all:true uri acic ids_to_inner_sorts ids_to_inner_types
90     in
91     if BuildTimeConf.debug then save_dom ~doc:mathml ~dest:"/tmp/matita.xml";
92     proof_viewer#load_proof mathml proof_metadata;
93     debug_print "/proof_observer"
94   in
95   let sequents_observer _ ((proof, goal_opt), (_, sequents_metadata)) =
96     sequents_viewer#reset;
97     (match goal_opt with
98     | None -> ()
99     | Some goal ->
100         sequents_viewer#load_sequents sequents_metadata;
101         sequents_viewer#goto_sequent goal)
102   in
103   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
104       sequents_observer);
105   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
106       proof_observer);
107   ignore (proof#attach_observer ~interested_in:StatefulProofEngine.all_events
108     xmldump_observer);
109   proof#notify;
110   set_proof (Some proof)
111
112 let quit () = (* quit program, asking for confirmation if needed *)
113   if not (has_proof ()) ||
114     (ask_confirmation ~gui
115       ~msg:("Proof in progress, are you sure you want to quit?") ())
116   then
117     GMain.Main.quit ()
118
119 let abort_proof () =
120   if has_proof () then begin
121     set_proof None;
122     sequents_viewer#reset
123   end
124
125 let proof_handler =
126   { MatitaTypes.get_proof = get_proof;
127     MatitaTypes.abort_proof = abort_proof;
128     MatitaTypes.has_proof = has_proof;
129     MatitaTypes.new_proof = new_proof;
130     MatitaTypes.quit = quit;
131   }
132
133 let interpreter =
134   let console = gui#console in
135   new MatitaInterpreter.interpreter ~disambiguator ~proof_handler ~console ()
136
137 let _ =
138   gui#setQuitCallback quit;
139   gui#setPhraseCallback interpreter#evalPhrase;
140   gui#main#debugMenu#misc#hide ();
141   ignore (gui#main#newProofMenuItem#connect#activate (fun _ ->
142    if has_proof () &&
143       not (ask_confirmation ~gui
144         ~msg:("Proof in progress, are you sure you want to start a new one?")
145         ())
146     then
147       ()  (* abort new proof process *)
148     else
149       let input = ask_text ~gui ~msg:"Insert proof goal" ~multiline:true () in
150       let (env, metasenv, expr) =
151         disambiguator#disambiguateTerm (Stream.of_string input)
152       in
153       let proof = MatitaProof.proof ~typ:expr ~metasenv () in
154       new_proof proof))
155
156   (** <DEBUGGING> *)
157 let _ =
158   if BuildTimeConf.debug then begin
159     gui#main#debugMenu#misc#show ();
160     let addDebugItem ~label callback =
161       let item =
162         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label ()
163       in
164       ignore (item#connect#activate callback)
165     in
166     addDebugItem "interactive user uri choice" (fun _ ->
167       try
168         let uris =
169           interactive_user_uri_choice ~gui ~selection_mode:`MULTIPLE
170             ~msg:"messaggio" ~nonvars_button:true
171             ["cic:/uno.con"; "cic:/due.var"; "cic:/tre.con"; "cic:/quattro.con";
172             "cic:/cinque.var"]
173         in
174         List.iter prerr_endline uris
175       with MatitaGtkMisc.Cancel -> MatitaTypes.error "no choice");
176     addDebugItem "toggle auto disambiguation" (fun _ ->
177       Helm_registry.set_bool "matita.auto_disambiguation"
178         (not (Helm_registry.get_bool "matita.auto_disambiguation")));
179     addDebugItem "mono line text input" (fun _ ->
180       prerr_endline (ask_text ~gui ~title:"title" ~msg:"message" ()));
181     addDebugItem "multi line text input" (fun _ ->
182       prerr_endline
183         (ask_text ~gui ~title:"title" ~multiline:true ~msg:"message" ()));
184     addDebugItem "dump proof status to stdout" (fun _ ->
185       print_endline ((get_proof ())#toString));
186   end
187   (** </DEBUGGING> *)
188
189 let _ = GtkThread.main ()
190