]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matita.ml
cd3fe798253830c9df78ed1d63523fed8096a58c
[helm.git] / helm / software / matita / matita.ml
1 (* Copyright (C) 2004-2005, 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 (* $Id$ *)
27
28 open Printf
29
30 open MatitaGtkMisc
31 open GrafiteTypes
32
33 (** {2 Initialization} *)
34
35 let _ = 
36   MatitaInit.add_cmdline_spec
37     ["-tptppath",Arg.String 
38       (fun s -> Helm_registry.set_string "matita.tptppath" s),
39       "Where to find the Axioms/ and Problems/ directory"];
40   MatitaInit.initialize_all ()
41 ;;
42
43 (* let _ = Saturation.init () (* ALB to link paramodulation *) *)
44
45 (** {2 GUI callbacks} *)
46
47 let gui = MatitaGui.instance ()
48
49 let script =
50   let s = 
51     MatitaScript.script 
52       ~source_view:gui#sourceView
53       ~mathviewer:(MatitaMathView.mathViewer ())
54       ~urichooser:(fun uris ->
55         try
56           MatitaGui.interactive_uri_choice ~selection_mode:`SINGLE
57           ~title:"Matita: URI chooser" 
58           ~msg:"Select the URI" ~hide_uri_entry:true
59           ~hide_try:true ~ok_label:"_Apply" ~ok_action:`SELECT
60           ~copy_cb:(fun s -> gui#sourceView#buffer#insert ("\n"^s^"\n"))
61           () ~id:"boh?" uris
62         with MatitaTypes.Cancel -> [])
63       ~set_star:gui#setStar
64       ~ask_confirmation:
65         (fun ~title ~message -> 
66             MatitaGtkMisc.ask_confirmation ~title ~message 
67             ~parent:gui#main#toplevel ())
68       ()
69   in
70   Predefined_virtuals.load_predefined_virtuals ();
71   Predefined_virtuals.load_predefined_classes ();
72   gui#sourceView#source_buffer#begin_not_undoable_action ();
73   s#reset (); 
74   s#template (); 
75   gui#sourceView#source_buffer#end_not_undoable_action ();
76   s
77   
78   (* math viewers *)
79 let _ =
80   let cic_math_view = MatitaMathView.cicMathView_instance () in
81   let sequents_viewer = MatitaMathView.sequentsViewer_instance () in
82   sequents_viewer#load_logo;
83   cic_math_view#set_href_callback
84     (Some (fun uri ->
85       let uri =
86        try
87         `Uri (UriManager.uri_of_string uri)
88        with
89         UriManager.IllFormedUri _ ->
90          `NRef (NReference.reference_of_string uri)
91       in
92       (MatitaMathView.cicBrowser ())#load uri));
93   let browser_observer _ = MatitaMathView.refresh_all_browsers () in
94   let sequents_observer grafite_status =
95     sequents_viewer#reset;
96     match grafite_status#proof_status with
97     | Incomplete_proof ({ stack = stack } as incomplete_proof) ->
98         sequents_viewer#load_sequents incomplete_proof;
99         (try
100           script#setGoal (Some (Continuationals.Stack.find_goal stack));
101           let goal =
102            match script#goal with
103               None -> assert false
104             | Some n -> n
105           in
106            sequents_viewer#goto_sequent goal
107         with Failure _ -> script#setGoal None);
108     | Proof proof -> sequents_viewer#load_logo_with_qed
109     | No_proof ->
110        (match grafite_status#ng_mode with
111            `ProofMode ->
112             sequents_viewer#nload_sequents grafite_status;
113             (try
114               script#setGoal
115                (Some (Continuationals.Stack.find_goal grafite_status#stack));
116               let goal =
117                match script#goal with
118                   None -> assert false
119                 | Some n -> n
120               in
121                sequents_viewer#goto_sequent goal
122             with Failure _ -> script#setGoal None);
123          | `CommandMode -> sequents_viewer#load_logo
124        )
125     | Intermediate _ -> assert false (* only the engine may be in this state *)
126   in
127   script#addObserver sequents_observer;
128   script#addObserver browser_observer
129
130   (** {{{ Debugging *)
131 let _ =
132   if BuildTimeConf.debug ||
133      Helm_registry.get_bool "matita.debug_menu" 
134   then begin
135     gui#main#debugMenu#misc#show ();
136     let addDebugItem ~label callback =
137       let item =
138         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label () in
139       ignore (item#connect#activate callback) in
140     let addDebugSeparator () =
141       ignore (GMenu.separator_item ~packing:gui#main#debugMenu_menu#append ())
142     in
143     addDebugItem "dump aliases" (fun _ ->
144       let status = script#grafite_status in
145       LexiconEngine.dump_aliases HLog.debug "" status);
146 (* FG: DEBUGGING   
147     addDebugItem "dump interpretations" (fun _ ->
148       let status = script#lexicon_status in
149       let filter = 
150        List.filter (function LexiconAst.Interpretation _ -> true | _ -> false)
151       in
152       HLog.debug (String.concat "\n"
153        (List.fold_right
154          (fun x l -> (LexiconAstPp.pp_command x)::l)
155          (filter status.LexiconEngine.lexicon_content_rev) [])));
156 *)
157     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
158       let oc = open_out "env.dump" in
159       CicEnvironment.dump_to_channel oc;
160       close_out oc);
161     addDebugItem "load environment from \"env.dump\"" (fun _ ->
162       let ic = open_in "env.dump" in
163       CicEnvironment.restore_from_channel ic;
164       close_in ic);
165     addDebugItem "dump universes" (fun _ ->
166       List.iter (fun (u,_,g) -> 
167         prerr_endline (UriManager.string_of_uri u); 
168         CicUniv.print_ugraph g) (CicEnvironment.list_obj ())
169       );
170     addDebugItem "dump environment content" (fun _ ->
171       List.iter (fun (u,_,_) -> 
172         prerr_endline (UriManager.string_of_uri u)) 
173         (CicEnvironment.list_obj ()));
174     addDebugItem "dump script status" script#dump;
175     addDebugItem "dump configuration file to ./foo.conf.xml" (fun _ ->
176       Helm_registry.save_to "./foo.conf.xml");
177     addDebugItem "dump metasenv"
178       (fun _ ->
179          if script#onGoingProof () then
180            HLog.debug (CicMetaSubst.ppmetasenv [] script#proofMetasenv));
181     addDebugItem "print top-level grammar entries"
182       CicNotationParser.print_l2_pattern;
183     addDebugItem "dump moo to stderr" (fun _ ->
184       let grafite_status = (MatitaScript.current ())#grafite_status in
185       let moo = grafite_status#moo_content_rev in
186       List.iter
187         (fun cmd ->
188           prerr_endline
189            (GrafiteAstPp.pp_command
190              ~term_pp:(fun t -> CicPp.ppterm t)
191              ~obj_pp:(fun _ -> assert false)
192              cmd))
193         (List.rev moo));
194     addDebugItem "print metasenv goals and stack to stderr"
195       (fun _ ->
196         prerr_endline ("metasenv goals: " ^ String.concat " "
197           (List.map (fun (g, _, _) -> string_of_int g)
198             (MatitaScript.current ())#proofMetasenv));
199         prerr_endline ("stack: " ^ Continuationals.Stack.pp
200           (GrafiteTypes.get_stack (MatitaScript.current ())#grafite_status)));
201      addDebugItem "Print current proof term" 
202        (fun _ -> 
203         HLog.debug
204           (CicPp.ppterm 
205             (match 
206             (MatitaScript.current ())#grafite_status#proof_status
207             with
208             | GrafiteTypes.No_proof -> (Cic.Implicit None)
209             | Incomplete_proof i -> 
210                  let _,_,_subst,p,_, _ = i.GrafiteTypes.proof in 
211                  Lazy.force p
212             | Proof p -> let _,_,_subst,p,_, _ = p in Lazy.force p
213             | Intermediate _ -> assert false)));
214      addDebugItem "Print current proof (natural language) to stderr" 
215        (fun _ -> 
216         prerr_endline 
217           (ApplyTransformation.txt_of_cic_object 120 [] 
218             ~map_unicode_to_tex:(Helm_registry.get_bool
219               "matita.paste_unicode_as_tex")
220             (match 
221             (MatitaScript.current ())#grafite_status#proof_status
222             with
223             | GrafiteTypes.No_proof -> assert false
224             | Incomplete_proof i -> 
225                 let _,m,_subst,p,ty, attrs = i.GrafiteTypes.proof in 
226                 Cic.CurrentProof ("current (incomplete) proof",m,Lazy.force p,ty,[],attrs)
227             | Proof (_,m,_subst,p,ty, attrs) -> 
228                 Cic.CurrentProof ("current proof",m,Lazy.force p,ty,[],attrs)
229             | Intermediate _ -> assert false)));
230 (*     addDebugItem "ask record choice"
231       (fun _ ->
232         HLog.debug (string_of_int
233           (MatitaGtkMisc.ask_record_choice ~gui ~title:"title" ~message:"msg"
234           ~fields:["a"; "b"; "c"]
235           ~records:[
236             ["0"; "0"; "0"]; ["0"; "0"; "1"]; ["0"; "1"; "0"]; ["0"; "1"; "1"];
237             ["1"; "0"; "0"]; ["1"; "0"; "1"]; ["1"; "1"; "0"]; ["1"; "1"; "1"]]
238           ()))); *)
239 (*     addDebugItem "rotate light bulbs"
240       (fun _ ->
241          let nb = gui#main#hintNotebook in
242          nb#goto_page ((nb#current_page + 1) mod 3)); *)
243     addDebugSeparator ();
244 (*
245     addDebugItem "meets between L and R" 
246       (fun _ -> 
247         let l = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
248           (UriManager.uri_of_string "cic:/matita/test/L.ind#xpointer(1/1)" )) 
249         in
250         let r = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
251           (UriManager.uri_of_string "cic:/matita/test/R.ind#xpointer(1/1)" )) 
252         in
253         let meets = CoercGraph.meets l r in
254         prerr_endline "MEETS:";
255         List.iter (fun carr -> prerr_endline (CicPp.ppterm (CoercDb.term_of_carr
256         carr))) meets
257     );
258     addDebugSeparator ();
259 *)
260     addDebugItem "disable high level pretty printer"
261       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := true);
262     addDebugItem "enable high level pretty printer"
263       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := false);
264 (* ZACK moved to the View menu
265     addDebugItem "disable all (pretty printing) notations"
266       (fun _ -> CicNotation.set_active_notations []);
267     addDebugItem "enable all (pretty printing) notations"
268       (fun _ ->
269         CicNotation.set_active_notations
270           (List.map fst (CicNotation.get_all_notations ())));
271 *)
272     addDebugSeparator ();
273     addDebugItem "enable multiple disambiguation passes (default)"
274       (fun _ -> MultiPassDisambiguator.only_one_pass := false);
275     addDebugItem "enable only one disambiguation pass"
276       (fun _ -> MultiPassDisambiguator.only_one_pass := true);
277     addDebugItem "always show all disambiguation errors"
278       (fun _ -> MatitaGui.all_disambiguation_passes := true);
279     addDebugItem "prune disambiguation errors"
280       (fun _ -> MatitaGui.all_disambiguation_passes := false);
281     addDebugSeparator ();
282 (* ZACK moved to the View menu
283     addDebugItem "enable coercions hiding"
284       (fun _ -> Acic2content.hide_coercions := true);
285     addDebugItem "disable coercions hiding"
286       (fun _ -> Acic2content.hide_coercions := false);
287 *)
288     addDebugItem "show coercions graph" (fun _ ->
289       let c = MatitaMathView.cicBrowser () in
290       c#load (`About `Coercions));
291     addDebugItem "show coercions graph (full)" (fun _ ->
292       let c = MatitaMathView.cicBrowser () in
293       c#load (`About `CoercionsFull));
294     addDebugItem "dump coercions Db" (fun _ ->
295       List.iter
296       (fun (s,t,ul) -> 
297           HLog.debug
298            ((String.concat ","
299               (List.map
300                 (fun u,saturations,_ ->
301                   UriManager.name_of_uri u ^
302                    "(" ^ string_of_int saturations ^ ")")
303                 ul)) ^ ":"
304              ^ CoercDb.string_of_carr s ^ " -> " ^ CoercDb.string_of_carr t))
305         (CoercDb.to_list (CoercDb.dump ())));
306     addDebugSeparator ();
307     let mview () = (MatitaMathView.sequentsViewer_instance ())#cicMathView in
308 (*     addDebugItem "save (sequent) MathML to matita.xml"
309       (fun _ -> ignore ((Gdome.domImplementation ())#saveDocumentToFile
310         ~doc:(HExtlib.unopt (mview ())#get_document) ~name:"matita.xml" ())); *)
311     addDebugItem "load (sequent) MathML from matita.xml"
312       (fun _ -> (mview ())#load_uri ~filename:"matita.xml");
313     addDebugSeparator ();
314     addDebugItem "Expand virtuals"
315     (fun _ -> (MatitaScript.current ())#expandAllVirtuals);
316   end
317   (** Debugging }}} *)
318
319   (** {2 Main} *)
320
321 let _ =
322   at_exit (fun () -> print_endline "\nThanks for using Matita!\n");
323   Sys.catch_break true;
324   let args = Helm_registry.get_list Helm_registry.string "matita.args" in
325   (try gui#loadScript (List.hd args) with Failure _ -> ());
326   gui#main#mainWin#show ();
327   try
328    GtkThread.main ()
329   with Sys.Break ->
330    Sys.set_signal Sys.sigint
331     (Sys.Signal_handle
332       (fun _ ->
333         prerr_endline "Still cleaning the library: don't be impatient!"));
334    prerr_endline "Matita is cleaning up. Please wait.";
335    let baseuri = 
336     (MatitaScript.current ())#grafite_status#baseuri
337    in
338      LibraryClean.clean_baseuris [baseuri]
339
340 (* vim:set foldmethod=marker: *)