]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matita.ml
Huge commit with several changes:
[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_status with
111            ProofMode nstatus ->
112             sequents_viewer#nload_sequents nstatus;
113             (try
114               script#setGoal (Some (Continuationals.Stack.find_goal nstatus.NTacStatus.gstatus));
115               let goal =
116                match script#goal with
117                   None -> assert false
118                 | Some n -> n
119               in
120                sequents_viewer#goto_sequent goal
121             with Failure _ -> script#setGoal None);
122          | CommandMode _ -> sequents_viewer#load_logo
123        )
124     | Intermediate _ -> assert false (* only the engine may be in this state *)
125   in
126   script#addObserver sequents_observer;
127   script#addObserver browser_observer
128
129   (** {{{ Debugging *)
130 let _ =
131   if BuildTimeConf.debug ||
132      Helm_registry.get_bool "matita.debug_menu" 
133   then begin
134     gui#main#debugMenu#misc#show ();
135     let addDebugItem ~label callback =
136       let item =
137         GMenu.menu_item ~packing:gui#main#debugMenu_menu#append ~label () in
138       ignore (item#connect#activate callback) in
139     let addDebugSeparator () =
140       ignore (GMenu.separator_item ~packing:gui#main#debugMenu_menu#append ())
141     in
142     addDebugItem "dump aliases" (fun _ ->
143       let status = script#lexicon_status in
144       LexiconEngine.dump_aliases HLog.debug "" status);
145 (* FG: DEBUGGING   
146     addDebugItem "dump interpretations" (fun _ ->
147       let status = script#lexicon_status in
148       let filter = 
149        List.filter (function LexiconAst.Interpretation _ -> true | _ -> false)
150       in
151       HLog.debug (String.concat "\n"
152        (List.fold_right
153          (fun x l -> (LexiconAstPp.pp_command x)::l)
154          (filter status.LexiconEngine.lexicon_content_rev) [])));
155 *)
156     addDebugItem "dump environment to \"env.dump\"" (fun _ ->
157       let oc = open_out "env.dump" in
158       CicEnvironment.dump_to_channel oc;
159       close_out oc);
160     addDebugItem "load environment from \"env.dump\"" (fun _ ->
161       let ic = open_in "env.dump" in
162       CicEnvironment.restore_from_channel ic;
163       close_in ic);
164     addDebugItem "dump universes" (fun _ ->
165       List.iter (fun (u,_,g) -> 
166         prerr_endline (UriManager.string_of_uri u); 
167         CicUniv.print_ugraph g) (CicEnvironment.list_obj ())
168       );
169     addDebugItem "dump environment content" (fun _ ->
170       List.iter (fun (u,_,_) -> 
171         prerr_endline (UriManager.string_of_uri u)) 
172         (CicEnvironment.list_obj ()));
173     addDebugItem "dump script status" script#dump;
174     addDebugItem "dump configuration file to ./foo.conf.xml" (fun _ ->
175       Helm_registry.save_to "./foo.conf.xml");
176     addDebugItem "dump metasenv"
177       (fun _ ->
178          if script#onGoingProof () then
179            HLog.debug (CicMetaSubst.ppmetasenv [] script#proofMetasenv));
180     addDebugItem "print top-level grammar entries"
181       CicNotationParser.print_l2_pattern;
182     addDebugItem "dump moo to stderr" (fun _ ->
183       let grafite_status = (MatitaScript.current ())#grafite_status in
184       let moo = grafite_status.moo_content_rev in
185       List.iter
186         (fun cmd ->
187           prerr_endline
188            (GrafiteAstPp.pp_command
189              ~term_pp:(fun t -> CicPp.ppterm t)
190              ~obj_pp:(fun _ -> assert false)
191              cmd))
192         (List.rev moo));
193     addDebugItem "print metasenv goals and stack to stderr"
194       (fun _ ->
195         prerr_endline ("metasenv goals: " ^ String.concat " "
196           (List.map (fun (g, _, _) -> string_of_int g)
197             (MatitaScript.current ())#proofMetasenv));
198         prerr_endline ("stack: " ^ Continuationals.Stack.pp
199           (GrafiteTypes.get_stack (MatitaScript.current ())#grafite_status)));
200      addDebugItem "Print current proof term" 
201        (fun _ -> 
202         HLog.debug
203           (CicPp.ppterm 
204             (match 
205             (MatitaScript.current ())#grafite_status.GrafiteTypes.proof_status
206             with
207             | GrafiteTypes.No_proof -> (Cic.Implicit None)
208             | Incomplete_proof i -> 
209                  let _,_,_subst,p,_, _ = i.GrafiteTypes.proof in 
210                  Lazy.force p
211             | Proof p -> let _,_,_subst,p,_, _ = p in Lazy.force p
212             | Intermediate _ -> assert false)));
213      addDebugItem "Print current proof (natural language) to stderr" 
214        (fun _ -> 
215         prerr_endline 
216           (ApplyTransformation.txt_of_cic_object 120 [] 
217             ~map_unicode_to_tex:(Helm_registry.get_bool
218               "matita.paste_unicode_as_tex")
219             (match 
220             (MatitaScript.current ())#grafite_status.GrafiteTypes.proof_status
221             with
222             | GrafiteTypes.No_proof -> assert false
223             | Incomplete_proof i -> 
224                 let _,m,_subst,p,ty, attrs = i.GrafiteTypes.proof in 
225                 Cic.CurrentProof ("current (incomplete) proof",m,Lazy.force p,ty,[],attrs)
226             | Proof (_,m,_subst,p,ty, attrs) -> 
227                 Cic.CurrentProof ("current proof",m,Lazy.force p,ty,[],attrs)
228             | Intermediate _ -> assert false)));
229 (*     addDebugItem "ask record choice"
230       (fun _ ->
231         HLog.debug (string_of_int
232           (MatitaGtkMisc.ask_record_choice ~gui ~title:"title" ~message:"msg"
233           ~fields:["a"; "b"; "c"]
234           ~records:[
235             ["0"; "0"; "0"]; ["0"; "0"; "1"]; ["0"; "1"; "0"]; ["0"; "1"; "1"];
236             ["1"; "0"; "0"]; ["1"; "0"; "1"]; ["1"; "1"; "0"]; ["1"; "1"; "1"]]
237           ()))); *)
238 (*     addDebugItem "rotate light bulbs"
239       (fun _ ->
240          let nb = gui#main#hintNotebook in
241          nb#goto_page ((nb#current_page + 1) mod 3)); *)
242     addDebugSeparator ();
243 (*
244     addDebugItem "meets between L and R" 
245       (fun _ -> 
246         let l = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
247           (UriManager.uri_of_string "cic:/matita/test/L.ind#xpointer(1/1)" )) 
248         in
249         let r = CoercDb.coerc_carr_of_term (CicUtil.term_of_uri
250           (UriManager.uri_of_string "cic:/matita/test/R.ind#xpointer(1/1)" )) 
251         in
252         let meets = CoercGraph.meets l r in
253         prerr_endline "MEETS:";
254         List.iter (fun carr -> prerr_endline (CicPp.ppterm (CoercDb.term_of_carr
255         carr))) meets
256     );
257     addDebugSeparator ();
258 *)
259     addDebugItem "disable high level pretty printer"
260       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := true);
261     addDebugItem "enable high level pretty printer"
262       (fun _ -> CicMetaSubst.use_low_level_ppterm_in_context := false);
263 (* ZACK moved to the View menu
264     addDebugItem "disable all (pretty printing) notations"
265       (fun _ -> CicNotation.set_active_notations []);
266     addDebugItem "enable all (pretty printing) notations"
267       (fun _ ->
268         CicNotation.set_active_notations
269           (List.map fst (CicNotation.get_all_notations ())));
270 *)
271     addDebugSeparator ();
272     addDebugItem "enable multiple disambiguation passes (default)"
273       (fun _ -> MultiPassDisambiguator.only_one_pass := false);
274     addDebugItem "enable only one disambiguation pass"
275       (fun _ -> MultiPassDisambiguator.only_one_pass := true);
276     addDebugItem "always show all disambiguation errors"
277       (fun _ -> MatitaGui.all_disambiguation_passes := true);
278     addDebugItem "prune disambiguation errors"
279       (fun _ -> MatitaGui.all_disambiguation_passes := false);
280     addDebugSeparator ();
281 (* ZACK moved to the View menu
282     addDebugItem "enable coercions hiding"
283       (fun _ -> Acic2content.hide_coercions := true);
284     addDebugItem "disable coercions hiding"
285       (fun _ -> Acic2content.hide_coercions := false);
286 *)
287     addDebugItem "show coercions graph" (fun _ ->
288       let c = MatitaMathView.cicBrowser () in
289       c#load (`About `Coercions));
290     addDebugItem "show coercions graph (full)" (fun _ ->
291       let c = MatitaMathView.cicBrowser () in
292       c#load (`About `CoercionsFull));
293     addDebugItem "dump coercions Db" (fun _ ->
294       List.iter
295       (fun (s,t,ul) -> 
296           HLog.debug
297            ((String.concat ","
298               (List.map
299                 (fun u,saturations,_ ->
300                   UriManager.name_of_uri u ^
301                    "(" ^ string_of_int saturations ^ ")")
302                 ul)) ^ ":"
303              ^ CoercDb.string_of_carr s ^ " -> " ^ CoercDb.string_of_carr t))
304         (CoercDb.to_list ()));
305     addDebugSeparator ();
306     let mview () = (MatitaMathView.sequentsViewer_instance ())#cicMathView in
307 (*     addDebugItem "save (sequent) MathML to matita.xml"
308       (fun _ -> ignore ((Gdome.domImplementation ())#saveDocumentToFile
309         ~doc:(HExtlib.unopt (mview ())#get_document) ~name:"matita.xml" ())); *)
310     addDebugItem "load (sequent) MathML from matita.xml"
311       (fun _ -> (mview ())#load_uri ~filename:"matita.xml");
312     addDebugSeparator ();
313     addDebugItem "Expand virtuals"
314     (fun _ -> (MatitaScript.current ())#expandAllVirtuals);
315   end
316   (** Debugging }}} *)
317
318   (** {2 Main} *)
319
320 let _ =
321   at_exit (fun () -> print_endline "\nThanks for using Matita!\n");
322   Sys.catch_break true;
323   let args = Helm_registry.get_list Helm_registry.string "matita.args" in
324   (try gui#loadScript (List.hd args) with Failure _ -> ());
325   gui#main#mainWin#show ();
326   try
327    GtkThread.main ()
328   with Sys.Break ->
329    Sys.set_signal Sys.sigint
330     (Sys.Signal_handle
331       (fun _ ->
332         prerr_endline "Still cleaning the library: don't be impatient!"));
333    prerr_endline "Matita is cleaning up. Please wait.";
334    let baseuri = 
335      GrafiteTypes.get_baseuri (MatitaScript.current ())#grafite_status
336    in
337      LibraryClean.clean_baseuris [baseuri]
338
339 (* vim:set foldmethod=marker: *)