]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/gTopLevel.ml
ProofEngine.goal := ==> set_proof_engine_goal
[helm.git] / helm / gTopLevel / gTopLevel.ml
1 (* Copyright (C) 2000-2003, 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://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
31 (*                                 06/01/2002                                 *)
32 (*                                                                            *)
33 (*                                                                            *)
34 (******************************************************************************)
35
36 open Printf;;
37
38 (* DEBUGGING *)
39
40 module MQI  = MQueryInterpreter
41 module MQIC = MQIConn
42 module MQGT = MQGTypes
43 module MQGU = MQGUtil
44 module MQG  = MQueryGenerator
45
46 (* GLOBAL CONSTANTS *)
47
48 let mqi_flags = [MQIC.Postgres ; MQIC.Stat ; MQIC.Warn ; MQIC.Log] (* default MathQL interpreter options *)
49 (*
50 let mqi_flags = [] (* default MathQL interpreter options *)
51 *)
52 let mqi_handle = MQIC.init mqi_flags prerr_string
53
54 let xlinkns = Gdome.domString "http://www.w3.org/1999/xlink";;
55
56 let htmlheader =
57  "<html>" ^
58  " <body bgColor=\"white\">"
59 ;;
60
61 let htmlfooter =
62  " </body>" ^
63  "</html>"
64 ;;
65
66 let prooffile =
67  try
68   Sys.getenv "GTOPLEVEL_PROOFFILE"
69  with
70   Not_found -> "/public/currentproof"
71 ;;
72
73 let prooffiletype =
74  try
75   Sys.getenv "GTOPLEVEL_PROOFFILETYPE"
76  with
77   Not_found -> "/public/currentprooftype"
78 ;;
79
80 let environmentfile =
81  try
82   Sys.getenv "GTOPLEVEL_ENVIRONMENTFILE"
83  with
84   Not_found -> "/public/environment"
85 ;;
86
87 let restore_environment_on_boot = true ;;
88 let notify_hbugs_on_goal_change = false ;;
89
90 (* GLOBAL REFERENCES (USED BY CALLBACKS) *)
91
92 let htmlheader_and_content = ref htmlheader;;
93
94 let check_term = ref (fun _ _ _ -> assert false);;
95
96 exception RenderingWindowsNotInitialized;;
97
98 let set_rendering_window,rendering_window =
99  let rendering_window_ref = ref None in
100   (function rw -> rendering_window_ref := Some rw),
101   (function () ->
102     match !rendering_window_ref with
103        None -> raise RenderingWindowsNotInitialized
104      | Some rw -> rw
105   )
106 ;;
107
108 exception SettingsWindowsNotInitialized;;
109
110 let set_settings_window,settings_window =
111  let settings_window_ref = ref None in
112   (function rw -> settings_window_ref := Some rw),
113   (function () ->
114     match !settings_window_ref with
115        None -> raise SettingsWindowsNotInitialized
116      | Some rw -> rw
117   )
118 ;;
119
120 exception OutputHtmlNotInitialized;;
121
122 let set_outputhtml,outputhtml =
123  let outputhtml_ref = ref None in
124   (function rw -> outputhtml_ref := Some rw),
125   (function () ->
126     match !outputhtml_ref with
127        None -> raise OutputHtmlNotInitialized
128      | Some outputhtml -> outputhtml
129   )
130 ;;
131
132 exception QedSetSensitiveNotInitialized;;
133 let qed_set_sensitive =
134  ref (function _ -> raise QedSetSensitiveNotInitialized)
135 ;;
136
137 exception SaveSetSensitiveNotInitialized;;
138 let save_set_sensitive =
139  ref (function _ -> raise SaveSetSensitiveNotInitialized)
140 ;;
141
142 (* COMMAND LINE OPTIONS *)
143
144 let usedb = ref true
145
146 let argspec =
147   [
148     "-nodb", Arg.Clear usedb, "disable use of MathQL DB"
149   ]
150 in
151 Arg.parse argspec ignore ""
152
153 (* MISC FUNCTIONS *)
154
155 let term_of_cic_textual_parser_uri uri =
156  let module C = Cic in
157  let module CTP = CicTextualParser0 in
158   match uri with
159      CTP.ConUri uri -> C.Const (uri,[])
160    | CTP.VarUri uri -> C.Var (uri,[])
161    | CTP.IndTyUri (uri,tyno) -> C.MutInd (uri,tyno,[])
162    | CTP.IndConUri (uri,tyno,consno) -> C.MutConstruct (uri,tyno,consno,[])
163 ;;
164
165 let string_of_cic_textual_parser_uri uri =
166  let module C = Cic in
167  let module CTP = CicTextualParser0 in
168   let uri' =
169    match uri with
170       CTP.ConUri uri -> UriManager.string_of_uri uri
171     | CTP.VarUri uri -> UriManager.string_of_uri uri
172     | CTP.IndTyUri (uri,tyno) ->
173        UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1)
174     | CTP.IndConUri (uri,tyno,consno) ->
175        UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (tyno + 1) ^ "/" ^
176         string_of_int consno
177   in
178    (* 4 = String.length "cic:" *)
179    String.sub uri' 4 (String.length uri' - 4)
180 ;;
181
182 let output_html outputhtml msg =
183  htmlheader_and_content := !htmlheader_and_content ^ msg ;
184  outputhtml#source (!htmlheader_and_content ^ htmlfooter) ;
185  outputhtml#set_topline (-1)
186 ;;
187
188 (* UTILITY FUNCTIONS TO DISAMBIGUATE AN URI *)
189
190 (* Check window *)
191
192 let check_window outputhtml uris =
193  let window =
194   GWindow.window
195    ~width:800 ~modal:true ~title:"Check" ~border_width:2 () in
196  let notebook =
197   GPack.notebook ~scrollable:true ~packing:window#add () in
198  window#show () ;
199  let render_terms =
200   List.map
201    (function uri ->
202      let scrolled_window =
203       GBin.scrolled_window ~border_width:10
204        ~packing:
205          (notebook#append_page ~tab_label:((GMisc.label ~text:uri ())#coerce))
206        ()
207      in
208       lazy 
209        (let mmlwidget =
210          TermViewer.sequent_viewer
211           ~packing:scrolled_window#add ~width:400 ~height:280 () in
212         let expr =
213          let term =
214           term_of_cic_textual_parser_uri
215            (MQueryMisc.cic_textual_parser_uri_of_string uri)
216          in
217           (Cic.Cast (term, CicTypeChecker.type_of_aux' [] [] term))
218         in
219          try
220           mmlwidget#load_sequent [] (111,[],expr)
221          with
222           e ->
223            output_html outputhtml
224             ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
225        )
226    ) uris
227  in
228   ignore
229    (notebook#connect#switch_page
230      (function i ->
231        Lazy.force (List.nth render_terms i)))
232 ;;
233
234 exception NoChoice;;
235
236 let
237  interactive_user_uri_choice ~(selection_mode:[`SINGLE|`EXTENDED]) ?(ok="Ok")
238   ?(enable_button_for_non_vars=false) ~title ~msg uris
239 =
240  let choices = ref [] in
241  let chosen = ref false in
242  let use_only_constants = ref false in
243  let window =
244   GWindow.dialog ~modal:true ~title ~width:600 () in
245  let lMessage =
246   GMisc.label ~text:msg
247    ~packing:(window#vbox#pack ~expand:false ~fill:false ~padding:5) () in
248  let scrolled_window =
249   GBin.scrolled_window ~border_width:10
250    ~packing:(window#vbox#pack ~expand:true ~fill:true ~padding:5) () in
251  let clist =
252   let expected_height = 18 * List.length uris in
253    let height = if expected_height > 400 then 400 else expected_height in
254     GList.clist ~columns:1 ~packing:scrolled_window#add
255      ~height ~selection_mode:(selection_mode :> Gtk.Tags.selection_mode) () in
256  let _ = List.map (function x -> clist#append [x]) uris in
257  let hbox2 =
258   GPack.hbox ~border_width:0
259    ~packing:(window#vbox#pack ~expand:false ~fill:false ~padding:5) () in
260  let explain_label =
261   GMisc.label ~text:"None of the above. Try this one:"
262    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
263  let manual_input =
264   GEdit.entry ~editable:true
265    ~packing:(hbox2#pack ~expand:true ~fill:true ~padding:5) () in
266  let hbox =
267   GPack.hbox ~border_width:0 ~packing:window#action_area#add () in
268  let okb =
269   GButton.button ~label:ok
270    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
271  let _ = okb#misc#set_sensitive false in
272  let nonvarsb =
273   GButton.button
274    ~packing:
275     (function w ->
276       if enable_button_for_non_vars then
277        hbox#pack ~expand:false ~fill:false ~padding:5 w)
278    ~label:"Try constants only" () in
279  let checkb =
280   GButton.button ~label:"Check"
281    ~packing:(hbox#pack ~padding:5) () in
282  let _ = checkb#misc#set_sensitive false in
283  let cancelb =
284   GButton.button ~label:"Abort"
285    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
286  (* actions *)
287  let check_callback () =
288   assert (List.length !choices > 0) ;
289   check_window (outputhtml ()) !choices
290  in
291   ignore (window#connect#destroy GMain.Main.quit) ;
292   ignore (cancelb#connect#clicked window#destroy) ;
293   ignore
294    (okb#connect#clicked (function () -> chosen := true ; window#destroy ())) ;
295   ignore
296    (nonvarsb#connect#clicked
297      (function () ->
298        use_only_constants := true ;
299        chosen := true ;
300        window#destroy ()
301    )) ;
302   ignore (checkb#connect#clicked check_callback) ;
303   ignore
304    (clist#connect#select_row
305      (fun ~row ~column ~event ->
306        checkb#misc#set_sensitive true ;
307        okb#misc#set_sensitive true ;
308        choices := (List.nth uris row)::!choices)) ;
309   ignore
310    (clist#connect#unselect_row
311      (fun ~row ~column ~event ->
312        choices :=
313         List.filter (function uri -> uri != (List.nth uris row)) !choices)) ;
314   ignore
315    (manual_input#connect#changed
316      (fun _ ->
317        if manual_input#text = "" then
318         begin
319          choices := [] ;
320          checkb#misc#set_sensitive false ;
321          okb#misc#set_sensitive false ;
322          clist#misc#set_sensitive true
323         end
324        else
325         begin
326          choices := [manual_input#text] ;
327          clist#unselect_all () ;
328          checkb#misc#set_sensitive true ;
329          okb#misc#set_sensitive true ;
330          clist#misc#set_sensitive false
331         end));
332   window#set_position `CENTER ;
333   window#show () ;
334   GtkThread.main ();
335   if !chosen then
336    if !use_only_constants then
337     List.filter
338      (function uri -> not (String.sub uri (String.length uri - 4) 4 = ".var"))
339      uris
340    else
341     if List.length !choices > 0 then !choices else raise NoChoice
342   else
343    raise NoChoice
344 ;;
345
346 let interactive_interpretation_choice interpretations =
347  let chosen = ref None in
348  let window =
349   GWindow.window
350    ~modal:true ~title:"Ambiguous well-typed input." ~border_width:2 () in
351  let vbox = GPack.vbox ~packing:window#add () in
352  let lMessage =
353   GMisc.label
354    ~text:
355     ("Ambiguous input since there are many well-typed interpretations." ^
356      " Please, choose one of them.")
357    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
358  let notebook =
359   GPack.notebook ~scrollable:true
360    ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
361  let _ =
362   List.map
363    (function interpretation ->
364      let clist =
365       let expected_height = 18 * List.length interpretation in
366        let height = if expected_height > 400 then 400 else expected_height in
367         GList.clist ~columns:2 ~packing:notebook#append_page ~height
368          ~titles:["id" ; "URI"] ()
369      in
370       ignore
371        (List.map
372          (function (id,uri) ->
373            let n = clist#append [id;uri] in
374             clist#set_row ~selectable:false n
375          ) interpretation
376        ) ;
377       clist#columns_autosize ()
378    ) interpretations in
379  let hbox =
380   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
381  let okb =
382   GButton.button ~label:"Ok"
383    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
384  let cancelb =
385   GButton.button ~label:"Abort"
386    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
387  (* actions *)
388  ignore (window#connect#destroy GMain.Main.quit) ;
389  ignore (cancelb#connect#clicked window#destroy) ;
390  ignore
391   (okb#connect#clicked
392     (function () -> chosen := Some notebook#current_page ; window#destroy ())) ;
393  window#set_position `CENTER ;
394  window#show () ;
395  GtkThread.main ();
396  match !chosen with
397     None -> raise NoChoice
398   | Some n -> n
399 ;;
400
401
402 (* MISC FUNCTIONS *)
403
404 let
405  save_object_to_disk uri annobj ids_to_inner_sorts ids_to_inner_types pathname
406 =
407  let name =
408   let struri = UriManager.string_of_uri uri in
409   let idx = (String.rindex struri '/') + 1 in
410    String.sub struri idx (String.length struri - idx)
411  in
412   let path = pathname ^ "/" ^ name in
413   let xml, bodyxml =
414    Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
415     annobj 
416   in
417   let xmlinnertypes =
418    Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
419     ~ask_dtd_to_the_getter:false
420   in
421    (* innertypes *)
422    let innertypesuri = UriManager.innertypesuri_of_uri uri in
423     Xml.pp ~quiet:true xmlinnertypes (Some (path ^ ".types.xml")) ;
424     Getter.register innertypesuri
425      (Configuration.annotations_url ^
426        Str.replace_first (Str.regexp "^cic:") ""
427         (UriManager.string_of_uri innertypesuri) ^ ".xml"
428      ) ;
429     (* constant type / variable / mutual inductive types definition *)
430     Xml.pp ~quiet:true xml (Some (path ^ ".xml")) ;
431     Getter.register uri
432      (Configuration.annotations_url ^
433        Str.replace_first (Str.regexp "^cic:") ""
434         (UriManager.string_of_uri uri) ^ ".xml"
435      ) ;
436     match bodyxml with
437        None -> ()
438      | Some bodyxml' ->
439         (* constant body *)
440         let bodyuri =
441          match UriManager.bodyuri_of_uri uri with
442             None -> assert false
443           | Some bodyuri -> bodyuri
444         in
445          Xml.pp ~quiet:true bodyxml' (Some (path ^ ".body.xml")) ;
446          Getter.register bodyuri
447           (Configuration.annotations_url ^
448             Str.replace_first (Str.regexp "^cic:") ""
449              (UriManager.string_of_uri bodyuri) ^ ".xml"
450           )
451 ;;
452
453
454 (* CALLBACKS *)
455
456 exception OpenConjecturesStillThere;;
457 exception WrongProof;;
458
459 let pathname_of_annuri uristring =
460  Configuration.annotations_dir ^    
461   Str.replace_first (Str.regexp "^cic:") "" uristring
462 ;;
463
464 let make_dirs dirpath =
465  ignore (Unix.system ("mkdir -p " ^ dirpath))
466 ;;
467
468 let save_obj uri obj =
469  let
470   (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
471    ids_to_inner_types,ids_to_conjectures,ids_to_hypotheses)
472  =
473   Cic2acic.acic_object_of_cic_object obj
474  in
475   (* let's save the theorem and register it to the getter *) 
476   let pathname = pathname_of_annuri (UriManager.buri_of_uri uri) in
477    make_dirs pathname ;
478    save_object_to_disk uri acic ids_to_inner_sorts ids_to_inner_types
479     pathname
480 ;;
481
482 let qed () =
483  match ProofEngine.get_proof () with
484     None -> assert false
485   | Some (uri,[],bo,ty) ->
486      if
487       CicReduction.are_convertible []
488        (CicTypeChecker.type_of_aux' [] [] bo) ty
489      then
490       begin
491        (*CSC: Wrong: [] is just plainly wrong *)
492        let proof = Cic.Constant (UriManager.name_of_uri uri,Some bo,ty,[]) in
493        let (acic,ids_to_inner_types,ids_to_inner_sorts) =
494         (rendering_window ())#output#load_proof uri proof
495        in
496         !qed_set_sensitive false ;
497         (* let's save the theorem and register it to the getter *) 
498         let pathname = pathname_of_annuri (UriManager.buri_of_uri uri) in
499          make_dirs pathname ;
500          save_object_to_disk uri acic ids_to_inner_sorts ids_to_inner_types
501           pathname
502       end
503      else
504       raise WrongProof
505   | _ -> raise OpenConjecturesStillThere
506 ;;
507
508   (** save an unfinished proof on the filesystem *)
509 let save_unfinished_proof () =
510  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
511  let (xml, bodyxml) = ProofEngine.get_current_status_as_xml () in
512  Xml.pp ~quiet:true xml (Some prooffiletype) ;
513  output_html outputhtml
514   ("<h1 color=\"Green\">Current proof type saved to " ^
515    prooffiletype ^ "</h1>") ;
516  Xml.pp ~quiet:true bodyxml (Some prooffile) ;
517  output_html outputhtml
518   ("<h1 color=\"Green\">Current proof body saved to " ^
519    prooffile ^ "</h1>")
520 ;;
521
522 (* Used to typecheck the loaded proofs *)
523 let typecheck_loaded_proof metasenv bo ty =
524  let module T = CicTypeChecker in
525   ignore (
526    List.fold_left
527     (fun metasenv ((_,context,ty) as conj) ->
528       ignore (T.type_of_aux' metasenv context ty) ;
529       metasenv @ [conj]
530     ) [] metasenv) ;
531   ignore (T.type_of_aux' metasenv [] ty) ;
532   ignore (T.type_of_aux' metasenv [] bo)
533 ;;
534
535 let decompose_uris_choice_callback uris = 
536 (* N.B.: in questo passaggio perdo l'informazione su exp_named_subst !!!! *)
537   let module U = UriManager in 
538    List.map 
539     (function uri ->
540       match MQueryMisc.cic_textual_parser_uri_of_string uri with
541          CicTextualParser0.IndTyUri (uri,typeno) -> (uri,typeno,[])
542        | _ -> assert false)
543     (interactive_user_uri_choice 
544       ~selection_mode:`EXTENDED ~ok:"Ok" ~enable_button_for_non_vars:false 
545       ~title:"Decompose" ~msg:"Please, select the Inductive Types to decompose" 
546       (List.map 
547         (function (uri,typeno,_) ->
548           U.string_of_uri uri ^ "#1/" ^ string_of_int (typeno+1)
549         ) uris)
550     ) 
551 ;;
552
553 let mk_fresh_name_callback context name ~typ =
554  let fresh_name =
555   match ProofEngineHelpers.mk_fresh_name context name ~typ with
556      Cic.Name fresh_name -> fresh_name
557    | Cic.Anonymous -> assert false
558  in
559   match
560    GToolbox.input_string ~title:"Enter a fresh hypothesis name" ~text:fresh_name
561     ("Enter a fresh name for the hypothesis " ^
562       CicPp.pp typ
563        (List.map (function None -> None | Some (n,_) -> Some n) context))
564   with
565      Some fresh_name' -> Cic.Name fresh_name'
566    | None -> raise NoChoice
567 ;;
568
569 let refresh_proof (output : TermViewer.proof_viewer) =
570  try
571   let uri,currentproof =
572    match ProofEngine.get_proof () with
573       None -> assert false
574     | Some (uri,metasenv,bo,ty) ->
575        ProofEngine.set_proof (Some (uri,metasenv,bo,ty)) ;
576        if List.length metasenv = 0 then
577         begin
578          !qed_set_sensitive true ;
579          Hbugs.clear ()
580         end
581        else
582         Hbugs.notify () ;
583        (*CSC: Wrong: [] is just plainly wrong *)
584        uri,
585         (Cic.CurrentProof (UriManager.name_of_uri uri, metasenv, bo, ty, []))
586   in
587    ignore (output#load_proof uri currentproof)
588  with
589   e ->
590  match ProofEngine.get_proof () with
591     None -> assert false
592   | Some (uri,metasenv,bo,ty) ->
593 prerr_endline ("Offending proof: " ^ CicPp.ppobj (Cic.CurrentProof ("questa",metasenv,bo,ty,[]))) ; flush stderr ;
594    raise (InvokeTactics.RefreshProofException e)
595
596 let set_proof_engine_goal g =
597  ProofEngine.goal := g
598 ;;
599
600 let refresh_goals ?(empty_notebook=true) notebook =
601  try
602   match !ProofEngine.goal with
603      None ->
604       if empty_notebook then
605        begin 
606         notebook#remove_all_pages ~skip_switch_page_event:false ;
607         notebook#set_empty_page
608        end
609       else
610        notebook#proofw#unload
611    | Some metano ->
612       let metasenv =
613        match ProofEngine.get_proof () with
614           None -> assert false
615         | Some (_,metasenv,_,_) -> metasenv
616       in
617       let currentsequent =
618        List.find (function (m,_,_) -> m=metano) metasenv
619       in
620         let regenerate_notebook () = 
621          let skip_switch_page_event =
622           match metasenv with
623              (m,_,_)::_ when m = metano -> false
624            | _ -> true
625          in
626           notebook#remove_all_pages ~skip_switch_page_event ;
627           List.iter (function (m,_,_) -> notebook#add_page m) metasenv ;
628         in
629          if empty_notebook then
630           begin
631            regenerate_notebook () ;
632            notebook#set_current_page
633             ~may_skip_switch_page_event:false metano
634           end
635          else
636           begin
637            notebook#set_current_page
638             ~may_skip_switch_page_event:true metano ;
639            notebook#proofw#load_sequent metasenv currentsequent
640           end
641  with
642   e ->
643 let metano =
644   match !ProofEngine.goal with
645      None -> assert false
646    | Some m -> m
647 in
648 let metasenv =
649  match ProofEngine.get_proof () with
650     None -> assert false
651   | Some (_,metasenv,_,_) -> metasenv
652 in
653 try
654 let currentsequent = List.find (function (m,_,_) -> m=metano) metasenv in
655    prerr_endline ("Offending sequent: " ^ SequentPp.TextualPp.print_sequent currentsequent) ; flush stderr ;
656       raise (InvokeTactics.RefreshSequentException e)
657 with Not_found -> prerr_endline ("Offending sequent " ^ string_of_int metano ^ " unknown."); raise (InvokeTactics.RefreshSequentException e)
658
659 module InvokeTacticsCallbacks =
660  struct
661   let sequent_viewer () = (rendering_window ())#notebook#proofw
662   let term_editor () = (rendering_window ())#inputt
663   let scratch_window () = (rendering_window ())#scratch_window
664
665   let refresh_proof () =
666    let output = ((rendering_window ())#output : TermViewer.proof_viewer) in
667     refresh_proof output
668
669   let refresh_goals () =
670    let notebook = (rendering_window ())#notebook in
671     refresh_goals notebook
672
673   let decompose_uris_choice_callback = decompose_uris_choice_callback
674   let mk_fresh_name_callback = mk_fresh_name_callback
675   let output_html msg = output_html (outputhtml ()) msg
676  end
677 ;;
678 module InvokeTactics' = InvokeTactics.Make (InvokeTacticsCallbacks);;
679
680 (* Just to initialize the Hbugs module *)
681 module Ignore = Hbugs.Initialize (InvokeTactics');;
682 Hbugs.set_describe_hint_callback (fun hint ->
683   match hint with
684   | Hbugs_types.Use_apply_Luke term ->
685       let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
686       check_window outputhtml [term]
687   | _ -> ())
688 ;;
689
690 let dummy_uri = "/dummy.con"
691
692   (** load an unfinished proof from filesystem *)
693 let load_unfinished_proof () =
694  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
695  let output = ((rendering_window ())#output : TermViewer.proof_viewer) in
696  let notebook = (rendering_window ())#notebook in
697   try
698    match 
699     GToolbox.input_string ~title:"Load Unfinished Proof" ~text:dummy_uri
700      "Choose an URI:"
701    with
702       None -> raise NoChoice
703     | Some uri0 ->
704        let uri = UriManager.uri_of_string ("cic:" ^ uri0) in
705         match CicParser.obj_of_xml prooffiletype (Some prooffile) with
706            Cic.CurrentProof (_,metasenv,bo,ty,_) ->
707             typecheck_loaded_proof metasenv bo ty ;
708             ProofEngine.set_proof (Some (uri, metasenv, bo, ty)) ;
709             refresh_proof output ;
710             set_proof_engine_goal
711              (match metasenv with
712                  [] -> None
713                | (metano,_,_)::_ -> Some metano
714              ) ;
715             refresh_goals notebook ;
716              output_html outputhtml
717               ("<h1 color=\"Green\">Current proof type loaded from " ^
718                 prooffiletype ^ "</h1>") ;
719              output_html outputhtml
720               ("<h1 color=\"Green\">Current proof body loaded from " ^
721                 prooffile ^ "</h1>") ;
722             !save_set_sensitive true;
723          | _ -> assert false
724   with
725      InvokeTactics.RefreshSequentException e ->
726       output_html outputhtml
727        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
728         "sequent: " ^ Printexc.to_string e ^ "</h1>")
729    | InvokeTactics.RefreshProofException e ->
730       output_html outputhtml
731        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
732         "proof: " ^ Printexc.to_string e ^ "</h1>")
733    | e ->
734       output_html outputhtml
735        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
736 ;;
737
738 let edit_aliases () =
739  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
740  let id_to_uris = inputt#id_to_uris in
741  let chosen = ref false in
742  let window =
743   GWindow.window
744    ~width:400 ~modal:true ~title:"Edit Aliases..." ~border_width:2 () in
745  let vbox =
746   GPack.vbox ~border_width:0 ~packing:window#add () in
747  let scrolled_window =
748   GBin.scrolled_window ~border_width:10
749    ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
750  let input = GEdit.text ~editable:true ~width:400 ~height:100
751    ~packing:scrolled_window#add () in
752  let hbox =
753   GPack.hbox ~border_width:0
754    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
755  let okb =
756   GButton.button ~label:"Ok"
757    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
758  let cancelb =
759   GButton.button ~label:"Cancel"
760    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
761  ignore (window#connect#destroy GMain.Main.quit) ;
762  ignore (cancelb#connect#clicked window#destroy) ;
763  ignore
764   (okb#connect#clicked (function () -> chosen := true ; window#destroy ())) ;
765  let dom,resolve_id = !id_to_uris in
766   ignore
767    (input#insert_text ~pos:0
768     (String.concat "\n"
769       (List.map
770         (function v ->
771           let uri =
772            match resolve_id v with
773               None -> assert false
774             | Some (CicTextualParser0.Uri uri) -> uri
775             | Some (CicTextualParser0.Term _)
776             | Some CicTextualParser0.Implicit -> assert false
777           in
778            "alias " ^
779             (match v with
780                 CicTextualParser0.Id id -> id
781               | CicTextualParser0.Symbol (descr,_) ->
782                  (* CSC: To be implemented *)
783                  assert false
784             )^ " " ^ (string_of_cic_textual_parser_uri uri)
785         ) dom))) ;
786   window#show () ;
787   GtkThread.main ();
788   if !chosen then
789    let dom,resolve_id =
790     let inputtext = input#get_chars 0 input#length in
791     let regexpr =
792      let alfa = "[a-zA-Z_-]" in
793      let digit = "[0-9]" in
794      let ident = alfa ^ "\(" ^ alfa ^ "\|" ^ digit ^ "\)*" in
795      let blanks = "\( \|\t\|\n\)+" in
796      let nonblanks = "[^ \t\n]+" in
797      let uri = "/\(" ^ ident ^ "/\)*" ^ nonblanks in (* not very strict check *)
798       Str.regexp
799        ("alias" ^ blanks ^ "\(" ^ ident ^ "\)" ^ blanks ^ "\(" ^ uri ^ "\)")
800     in
801      let rec aux n =
802       try
803        let n' = Str.search_forward regexpr inputtext n in
804         let id = CicTextualParser0.Id (Str.matched_group 2 inputtext) in
805         let uri =
806          MQueryMisc.cic_textual_parser_uri_of_string
807           ("cic:" ^ (Str.matched_group 5 inputtext))
808         in
809          let dom,resolve_id = aux (n' + 1) in
810           if List.mem id dom then
811            dom,resolve_id
812           else
813            id::dom,
814             (function id' ->
815               if id = id' then
816                Some (CicTextualParser0.Uri uri)
817               else resolve_id id')
818       with
819        Not_found -> TermEditor.empty_id_to_uris
820      in
821       aux 0
822    in
823     id_to_uris := (dom,resolve_id)
824 ;;
825
826 let proveit () =
827  let module L = LogicalOperations in
828  let module G = Gdome in
829  let notebook = (rendering_window ())#notebook in
830  let output = (rendering_window ())#output in
831  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
832   try
833    output#make_sequent_of_selected_term ;
834    refresh_proof output ;
835    refresh_goals notebook
836   with
837      InvokeTactics.RefreshSequentException e ->
838       output_html outputhtml
839        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
840         "sequent: " ^ Printexc.to_string e ^ "</h1>")
841    | InvokeTactics.RefreshProofException e ->
842       output_html outputhtml
843        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
844         "proof: " ^ Printexc.to_string e ^ "</h1>")
845    | e ->
846       output_html outputhtml
847        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
848 ;;
849
850 let focus () =
851  let module L = LogicalOperations in
852  let module G = Gdome in
853  let notebook = (rendering_window ())#notebook in
854  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
855  let output = (rendering_window ())#output in
856   try
857    output#focus_sequent_of_selected_term ;
858    refresh_goals notebook
859   with
860      InvokeTactics.RefreshSequentException e ->
861       output_html outputhtml
862        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
863         "sequent: " ^ Printexc.to_string e ^ "</h1>")
864    | InvokeTactics.RefreshProofException e ->
865       output_html outputhtml
866        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
867         "proof: " ^ Printexc.to_string e ^ "</h1>")
868    | e ->
869       output_html outputhtml
870        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
871 ;;
872
873 exception NoPrevGoal;;
874 exception NoNextGoal;;
875
876 let setgoal metano =
877  let module L = LogicalOperations in
878  let module G = Gdome in
879  let notebook = (rendering_window ())#notebook in
880  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
881   let metasenv =
882    match ProofEngine.get_proof () with
883       None -> assert false
884     | Some (_,metasenv,_,_) -> metasenv
885   in
886    try
887     refresh_goals ~empty_notebook:false notebook
888    with
889       InvokeTactics.RefreshSequentException e ->
890        output_html outputhtml
891         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
892          "sequent: " ^ Printexc.to_string e ^ "</h1>")
893     | e ->
894        output_html outputhtml
895         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
896 ;;
897
898 let
899  show_in_show_window_obj, show_in_show_window_uri, show_in_show_window_callback
900 =
901  let window =
902   GWindow.window ~width:800 ~border_width:2 () in
903  let scrolled_window =
904   GBin.scrolled_window ~border_width:10 ~packing:window#add () in
905  let mmlwidget =
906   GMathViewAux.single_selection_math_view
907     ~packing:scrolled_window#add ~width:600 ~height:400 ()
908  in
909  let _ = window#event#connect#delete (fun _ -> window#misc#hide () ; true ) in
910  let href = Gdome.domString "href" in
911   let show_in_show_window_obj uri obj =
912    let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
913     try
914      let
915       (acic,ids_to_terms,ids_to_father_ids,ids_to_inner_sorts,
916        ids_to_inner_types,ids_to_conjectures,ids_to_hypotheses)
917      =
918       Cic2acic.acic_object_of_cic_object obj
919      in
920       let mml =
921        ApplyStylesheets.mml_of_cic_object
922         ~explode_all:false uri acic ids_to_inner_sorts ids_to_inner_types
923       in
924        window#set_title (UriManager.string_of_uri uri) ;
925        window#misc#hide () ; window#show () ;
926        mmlwidget#load_doc mml ;
927     with
928      e ->
929       output_html outputhtml
930        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
931   in
932   let show_in_show_window_uri uri =
933    let obj = CicEnvironment.get_obj uri in
934     show_in_show_window_obj uri obj
935   in
936    let show_in_show_window_callback mmlwidget (n : Gdome.element option) _ =
937     match n with
938        None -> ()
939      | Some n' ->
940         if n'#hasAttributeNS ~namespaceURI:xlinkns ~localName:href then
941          let uri =
942           (n'#getAttributeNS ~namespaceURI:xlinkns ~localName:href)#to_string
943          in 
944           show_in_show_window_uri (UriManager.uri_of_string uri)
945         else
946          ignore (mmlwidget#action_toggle n')
947    in
948     let _ =
949      mmlwidget#connect#click (show_in_show_window_callback mmlwidget)
950     in
951      show_in_show_window_obj, show_in_show_window_uri,
952       show_in_show_window_callback
953 ;;
954
955 exception NoObjectsLocated;;
956
957 let user_uri_choice ~title ~msg uris =
958  let uri =
959   match uris with
960      [] -> raise NoObjectsLocated
961    | [uri] -> uri
962    | uris ->
963       match
964        interactive_user_uri_choice ~selection_mode:`SINGLE ~title ~msg uris
965       with
966          [uri] -> uri
967        | _ -> assert false
968  in
969   String.sub uri 4 (String.length uri - 4)
970 ;;
971
972 let locate_callback id =
973  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
974  let out = output_html outputhtml in
975  let query = MQG.locate id in
976  let result = MQI.execute mqi_handle query in
977  let uris =
978   List.map
979    (function uri,_ ->
980      MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format' uri)
981    result in
982   out "<h1>Locate Query: </h1><pre>";
983   MQueryUtil.text_of_query out query ""; 
984   out "<h1>Result:</h1>";
985   MQueryUtil.text_of_result out result "<br>";
986   user_uri_choice ~title:"Ambiguous input."
987    ~msg:
988      ("Ambiguous input \"" ^ id ^
989       "\". Please, choose one interpetation:")
990    uris
991 ;;
992
993
994 let input_or_locate_uri ~title =
995  let uri = ref None in
996  let window =
997   GWindow.window
998    ~width:400 ~modal:true ~title ~border_width:2 () in
999  let vbox = GPack.vbox ~packing:window#add () in
1000  let hbox1 =
1001   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1002  let _ =
1003   GMisc.label ~text:"Enter a valid URI:" ~packing:(hbox1#pack ~padding:5) () in
1004  let manual_input =
1005   GEdit.entry ~editable:true
1006    ~packing:(hbox1#pack ~expand:true ~fill:true ~padding:5) () in
1007  let checkb =
1008   GButton.button ~label:"Check"
1009    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1010  let _ = checkb#misc#set_sensitive false in
1011  let hbox2 =
1012   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1013  let _ =
1014   GMisc.label ~text:"You can also enter an indentifier to locate:"
1015    ~packing:(hbox2#pack ~padding:5) () in
1016  let locate_input =
1017   GEdit.entry ~editable:true
1018    ~packing:(hbox2#pack ~expand:true ~fill:true ~padding:5) () in
1019  let locateb =
1020   GButton.button ~label:"Locate"
1021    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
1022  let _ = locateb#misc#set_sensitive false in
1023  let hbox3 =
1024   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1025  let okb =
1026   GButton.button ~label:"Ok"
1027    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
1028  let _ = okb#misc#set_sensitive false in
1029  let cancelb =
1030   GButton.button ~label:"Cancel"
1031    ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) ()
1032  in
1033   ignore (window#connect#destroy GMain.Main.quit) ;
1034   ignore
1035    (cancelb#connect#clicked (function () -> uri := None ; window#destroy ())) ;
1036   let check_callback () =
1037    let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1038    let uri = "cic:" ^ manual_input#text in
1039     try
1040       ignore (Getter.resolve (UriManager.uri_of_string uri)) ;
1041       output_html outputhtml "<h1 color=\"Green\">OK</h1>" ;
1042       true
1043     with
1044        Getter.Unresolved ->
1045         output_html outputhtml
1046          ("<h1 color=\"Red\">URI " ^ uri ^
1047           " does not correspond to any object.</h1>") ;
1048         false
1049      | UriManager.IllFormedUri _ ->
1050         output_html outputhtml
1051          ("<h1 color=\"Red\">URI " ^ uri ^ " is not well-formed.</h1>") ;
1052         false
1053      | e ->
1054         output_html outputhtml
1055          ("<h1 color=\"Red\">" ^ Printexc.to_string e ^ "</h1>") ;
1056         false
1057   in
1058   ignore
1059    (okb#connect#clicked
1060      (function () ->
1061        if check_callback () then
1062         begin
1063          uri := Some manual_input#text ;
1064          window#destroy ()
1065         end
1066    )) ;
1067   ignore (checkb#connect#clicked (function () -> ignore (check_callback ()))) ;
1068   ignore
1069    (manual_input#connect#changed
1070      (fun _ ->
1071        if manual_input#text = "" then
1072         begin
1073          checkb#misc#set_sensitive false ;
1074          okb#misc#set_sensitive false
1075         end
1076        else
1077         begin
1078          checkb#misc#set_sensitive true ;
1079          okb#misc#set_sensitive true
1080         end));
1081   ignore
1082    (locate_input#connect#changed
1083      (fun _ -> locateb#misc#set_sensitive (locate_input#text <> ""))) ;
1084   ignore
1085    (locateb#connect#clicked
1086      (function () ->
1087        let id = locate_input#text in
1088         manual_input#set_text (locate_callback id) ;
1089         locate_input#delete_text 0 (String.length id)
1090    )) ;
1091   window#show () ;
1092   GtkThread.main ();
1093   match !uri with
1094      None -> raise NoChoice
1095    | Some uri -> UriManager.uri_of_string ("cic:" ^ uri)
1096 ;;
1097
1098 exception AmbiguousInput;;
1099
1100 (* A WIDGET TO ENTER CIC TERMS *)
1101
1102 module ChosenTermEditor  = TexTermEditor;;
1103 module ChosenTextualParser0 = TexCicTextualParser0;;
1104 (*
1105 module ChosenTermEditor = TermEditor;;
1106 module ChosenTextualParser0 = CicTextualParser0;;
1107 *)
1108
1109 module Callbacks =
1110  struct
1111   let get_metasenv () = !ChosenTextualParser0.metasenv
1112   let set_metasenv metasenv = ChosenTextualParser0.metasenv := metasenv
1113
1114   let output_html msg = output_html (outputhtml ()) msg;;
1115   let interactive_user_uri_choice =
1116    fun ~selection_mode ?ok ?enable_button_for_non_vars ~title ~msg ~id ->
1117     interactive_user_uri_choice ~selection_mode ?ok
1118      ?enable_button_for_non_vars ~title ~msg;;
1119   let interactive_interpretation_choice = interactive_interpretation_choice;;
1120   let input_or_locate_uri = input_or_locate_uri;;
1121  end
1122 ;;
1123
1124 module TexTermEditor' = ChosenTermEditor.Make(Callbacks);;
1125
1126 (* OTHER FUNCTIONS *)
1127
1128 let locate () =
1129  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
1130  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1131    try
1132     match
1133      GToolbox.input_string ~title:"Locate" "Enter an identifier to locate:"
1134     with
1135        None -> raise NoChoice
1136      | Some input ->
1137         let uri = locate_callback input in
1138          inputt#set_term uri
1139    with
1140     e ->
1141      output_html outputhtml
1142       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
1143 ;;
1144
1145
1146 exception UriAlreadyInUse;;
1147 exception NotAUriToAConstant;;
1148
1149 let new_inductive () =
1150  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
1151  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1152  let output = ((rendering_window ())#output : TermViewer.proof_viewer) in
1153  let notebook = (rendering_window ())#notebook in
1154
1155  let chosen = ref false in
1156  let inductive = ref true in
1157  let paramsno = ref 0 in
1158  let get_uri = ref (function _ -> assert false) in
1159  let get_base_uri = ref (function _ -> assert false) in
1160  let get_names = ref (function _ -> assert false) in
1161  let get_types_and_cons = ref (function _ -> assert false) in
1162  let get_context_and_subst = ref (function _ -> assert false) in 
1163  let window =
1164   GWindow.window
1165    ~width:600 ~modal:true ~position:`CENTER
1166    ~title:"New Block of Mutual (Co)Inductive Definitions"
1167    ~border_width:2 () in
1168  let vbox = GPack.vbox ~packing:window#add () in
1169  let hbox =
1170   GPack.hbox ~border_width:0
1171    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1172  let _ =
1173   GMisc.label ~text:"Enter the URI for the new block:"
1174    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1175  let uri_entry =
1176   GEdit.entry ~editable:true
1177    ~packing:(hbox#pack ~expand:true ~fill:true ~padding:5) () in
1178  let hbox0 =
1179   GPack.hbox ~border_width:0
1180    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1181  let _ =
1182   GMisc.label
1183    ~text:
1184      "Enter the number of left parameters in every arity and constructor type:"
1185    ~packing:(hbox0#pack ~expand:false ~fill:false ~padding:5) () in
1186  let paramsno_entry =
1187   GEdit.entry ~editable:true ~text:"0"
1188    ~packing:(hbox0#pack ~expand:true ~fill:true ~padding:5) () in
1189  let hbox1 =
1190   GPack.hbox ~border_width:0
1191    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1192  let _ =
1193   GMisc.label ~text:"Are the definitions inductive or coinductive?"
1194    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1195  let inductiveb =
1196   GButton.radio_button ~label:"Inductive"
1197    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1198  let coinductiveb =
1199   GButton.radio_button ~label:"Coinductive"
1200    ~group:inductiveb#group
1201    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1202  let hbox2 =
1203   GPack.hbox ~border_width:0
1204    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1205  let _ =
1206   GMisc.label ~text:"Enter the list of the names of the types:"
1207    ~packing:(hbox2#pack ~expand:false ~fill:false ~padding:5) () in
1208  let names_entry =
1209   GEdit.entry ~editable:true
1210    ~packing:(hbox2#pack ~expand:true ~fill:true ~padding:5) () in
1211  let hboxn =
1212   GPack.hbox ~border_width:0
1213    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1214  let okb =
1215   GButton.button ~label:"> Next"
1216    ~packing:(hboxn#pack ~expand:false ~fill:false ~padding:5) () in
1217  let _ = okb#misc#set_sensitive true in
1218  let cancelb =
1219   GButton.button ~label:"Abort"
1220    ~packing:(hboxn#pack ~expand:false ~fill:false ~padding:5) () in
1221  ignore (window#connect#destroy GMain.Main.quit) ;
1222  ignore (cancelb#connect#clicked window#destroy) ;
1223  (* First phase *)
1224  let rec phase1 () =
1225   ignore
1226    (okb#connect#clicked
1227      (function () ->
1228        try
1229         let uristr = "cic:" ^ uri_entry#text in
1230         let namesstr = names_entry#text in
1231         let paramsno' = int_of_string (paramsno_entry#text) in
1232          match Str.split (Str.regexp " +") namesstr with
1233             [] -> assert false
1234           | (he::tl) as names ->
1235              let uri = UriManager.uri_of_string (uristr ^ "/" ^ he ^ ".ind") in
1236               begin
1237                try
1238                 ignore (Getter.resolve uri) ;
1239                 raise UriAlreadyInUse
1240                with
1241                 Getter.Unresolved ->
1242                  get_uri := (function () -> uri) ; 
1243                  get_names := (function () -> names) ;
1244                  inductive := inductiveb#active ;
1245                  paramsno := paramsno' ;
1246                  phase2 ()
1247               end
1248        with
1249         e ->
1250          output_html outputhtml
1251           ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1252      ))
1253  (* Second phase *)
1254  and phase2 () =
1255   let type_widgets =
1256    List.map
1257     (function name ->
1258       let frame =
1259        GBin.frame ~label:name
1260         ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
1261       let vbox = GPack.vbox ~packing:frame#add () in
1262       let hbox = GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false) () in
1263       let _ =
1264        GMisc.label ~text:("Enter its type:")
1265         ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1266       let scrolled_window =
1267        GBin.scrolled_window ~border_width:5
1268         ~packing:(vbox#pack ~expand:true ~padding:0) () in
1269       let newinputt =
1270        TexTermEditor'.term_editor
1271         mqi_handle
1272         ~width:400 ~height:20 ~packing:scrolled_window#add 
1273         ~share_id_to_uris_with:inputt ()
1274         ~isnotempty_callback:
1275          (function b ->
1276            (*non_empty_type := b ;*)
1277            okb#misc#set_sensitive true) (*(b && uri_entry#text <> ""))*)
1278       in
1279       let hbox =
1280        GPack.hbox ~border_width:0
1281         ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1282       let _ =
1283        GMisc.label ~text:("Enter the list of its constructors:")
1284         ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1285       let cons_names_entry =
1286        GEdit.entry ~editable:true
1287         ~packing:(hbox#pack ~expand:true ~fill:true ~padding:5) () in
1288       (newinputt,cons_names_entry)
1289     ) (!get_names ())
1290   in
1291    vbox#remove hboxn#coerce ;
1292    let hboxn =
1293     GPack.hbox ~border_width:0
1294      ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1295    let okb =
1296     GButton.button ~label:"> Next"
1297      ~packing:(hboxn#pack ~expand:false ~fill:false ~padding:5) () in
1298    let cancelb =
1299     GButton.button ~label:"Abort"
1300      ~packing:(hboxn#pack ~expand:false ~fill:false ~padding:5) () in
1301    ignore (cancelb#connect#clicked window#destroy) ;
1302    ignore
1303     (okb#connect#clicked
1304       (function () ->
1305         try
1306          let names = !get_names () in
1307          let types_and_cons =
1308           List.map2
1309            (fun name (newinputt,cons_names_entry) ->
1310              let consnamesstr = cons_names_entry#text in
1311              let cons_names = Str.split (Str.regexp " +") consnamesstr in
1312              let metasenv,expr =
1313               newinputt#get_metasenv_and_term ~context:[] ~metasenv:[]
1314              in
1315               match metasenv with
1316                  [] -> expr,cons_names
1317                | _ -> raise AmbiguousInput
1318            ) names type_widgets
1319          in
1320           let uri = !get_uri () in
1321           let _ =
1322            (* Let's see if so far the definition is well-typed *)
1323            let params = [] in
1324            let paramsno = 0 in
1325            (* To test if the arities of the inductive types are well *)
1326            (* typed, we check the inductive block definition where   *)
1327            (* no constructor is given to each type.                  *)
1328            let tys =
1329             List.map2
1330              (fun name (ty,cons) -> (name, !inductive, ty, []))
1331              names types_and_cons
1332            in
1333             CicTypeChecker.typecheck_mutual_inductive_defs uri
1334              (tys,params,paramsno)
1335           in
1336            get_context_and_subst :=
1337             (function () ->
1338               let i = ref 0 in
1339                List.fold_left2
1340                 (fun (context,subst) name (ty,_) ->
1341                   let res =
1342                    (Some (Cic.Name name, Cic.Decl ty))::context,
1343                     (Cic.MutInd (uri,!i,[]))::subst
1344                   in
1345                    incr i ; res
1346                 ) ([],[]) names types_and_cons) ;
1347            let types_and_cons' =
1348             List.map2
1349              (fun name (ty,cons) -> (name, !inductive, ty, phase3 name cons))
1350              names types_and_cons
1351            in
1352             get_types_and_cons := (function () -> types_and_cons') ;
1353             chosen := true ;
1354             window#destroy ()
1355         with
1356          e ->
1357           output_html outputhtml
1358            ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1359       ))
1360  (* Third phase *)
1361  and phase3 name cons =
1362   let get_cons_types = ref (function () -> assert false) in
1363   let window2 =
1364    GWindow.window
1365     ~width:600 ~modal:true ~position:`CENTER
1366     ~title:(name ^ " Constructors")
1367     ~border_width:2 () in
1368   let vbox = GPack.vbox ~packing:window2#add () in
1369   let cons_type_widgets =
1370    List.map
1371     (function consname ->
1372       let hbox =
1373        GPack.hbox ~border_width:0
1374         ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1375       let _ =
1376        GMisc.label ~text:("Enter the type of " ^ consname ^ ":")
1377         ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1378       let scrolled_window =
1379        GBin.scrolled_window ~border_width:5
1380         ~packing:(vbox#pack ~expand:true ~padding:0) () in
1381       let newinputt =
1382        TexTermEditor'.term_editor
1383         mqi_handle
1384         ~width:400 ~height:20 ~packing:scrolled_window#add
1385         ~share_id_to_uris_with:inputt ()
1386         ~isnotempty_callback:
1387          (function b ->
1388            (* (*non_empty_type := b ;*)
1389            okb#misc#set_sensitive true) (*(b && uri_entry#text <> ""))*) *)())
1390       in
1391        newinputt
1392     ) cons in
1393   let hboxn =
1394    GPack.hbox ~border_width:0
1395     ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1396   let okb =
1397    GButton.button ~label:"> Next"
1398     ~packing:(hboxn#pack ~expand:false ~fill:false ~padding:5) () in
1399   let _ = okb#misc#set_sensitive true in
1400   let cancelb =
1401    GButton.button ~label:"Abort"
1402     ~packing:(hboxn#pack ~expand:false ~fill:false ~padding:5) () in
1403   ignore (window2#connect#destroy GMain.Main.quit) ;
1404   ignore (cancelb#connect#clicked window2#destroy) ;
1405   ignore
1406    (okb#connect#clicked
1407      (function () ->
1408        try
1409         chosen := true ;
1410         let context,subst= !get_context_and_subst () in
1411         let cons_types =
1412          List.map2
1413           (fun name inputt ->
1414             let metasenv,expr =
1415              inputt#get_metasenv_and_term ~context ~metasenv:[]
1416             in
1417              match metasenv with
1418                 [] ->
1419                  let undebrujined_expr =
1420                   List.fold_left
1421                    (fun expr t -> CicSubstitution.subst t expr) expr subst
1422                  in
1423                   name, undebrujined_expr
1424               | _ -> raise AmbiguousInput
1425           ) cons cons_type_widgets
1426         in
1427          get_cons_types := (function () -> cons_types) ;
1428          window2#destroy ()
1429        with
1430         e ->
1431          output_html outputhtml
1432           ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1433      )) ;
1434   window2#show () ;
1435   GtkThread.main ();
1436   let okb_pressed = !chosen in
1437    chosen := false ;
1438    if (not okb_pressed) then
1439     begin
1440      window#destroy () ;
1441      assert false (* The control never reaches this point *)
1442     end
1443    else
1444     (!get_cons_types ())
1445  in
1446   phase1 () ;
1447   (* No more phases left or Abort pressed *) 
1448   window#show () ;
1449   GtkThread.main ();
1450   window#destroy () ;
1451   if !chosen then
1452    try
1453     let uri = !get_uri () in
1454 (*CSC: Da finire *)
1455     let params = [] in
1456     let tys = !get_types_and_cons () in
1457      let obj = Cic.InductiveDefinition tys params !paramsno in
1458       begin
1459        try
1460         prerr_endline (CicPp.ppobj obj) ;
1461         CicTypeChecker.typecheck_mutual_inductive_defs uri
1462          (tys,params,!paramsno) ;
1463         with
1464          e ->
1465           prerr_endline "Offending mutual (co)inductive type declaration:" ;
1466           prerr_endline (CicPp.ppobj obj) ;
1467       end ;
1468       (* We already know that obj is well-typed. We need to add it to the  *)
1469       (* environment in order to compute the inner-types without having to *)
1470       (* debrujin it or having to modify lots of other functions to avoid  *)
1471       (* asking the environment for the MUTINDs we are defining now.       *)
1472       CicEnvironment.put_inductive_definition uri obj ;
1473       save_obj uri obj ;
1474       show_in_show_window_obj uri obj
1475    with
1476     e ->
1477      output_html outputhtml
1478       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1479 ;;
1480
1481 let new_proof () =
1482  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
1483  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1484  let output = ((rendering_window ())#output : TermViewer.proof_viewer) in
1485  let notebook = (rendering_window ())#notebook in
1486
1487  let chosen = ref false in
1488  let get_metasenv_and_term = ref (function _ -> assert false) in
1489  let get_uri = ref (function _ -> assert false) in
1490  let non_empty_type = ref false in
1491  let window =
1492   GWindow.window
1493    ~width:600 ~modal:true ~title:"New Proof or Definition"
1494    ~border_width:2 () in
1495  let vbox = GPack.vbox ~packing:window#add () in
1496  let hbox =
1497   GPack.hbox ~border_width:0
1498    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1499  let _ =
1500   GMisc.label ~text:"Enter the URI for the new theorem or definition:"
1501    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1502  let uri_entry =
1503   GEdit.entry ~editable:true
1504    ~packing:(hbox#pack ~expand:true ~fill:true ~padding:5) () in
1505  uri_entry#set_text dummy_uri;
1506  uri_entry#select_region ~start:1 ~stop:(String.length dummy_uri);
1507  let hbox1 =
1508   GPack.hbox ~border_width:0
1509    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1510  let _ =
1511   GMisc.label ~text:"Enter the theorem or definition type:"
1512    ~packing:(hbox1#pack ~expand:false ~fill:false ~padding:5) () in
1513  let scrolled_window =
1514   GBin.scrolled_window ~border_width:5
1515    ~packing:(vbox#pack ~expand:true ~padding:0) () in
1516  (* the content of the scrolled_window is moved below (see comment) *)
1517  let hbox =
1518   GPack.hbox ~border_width:0
1519    ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1520  let okb =
1521   GButton.button ~label:"Ok"
1522    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1523  let _ = okb#misc#set_sensitive false in
1524  let cancelb =
1525   GButton.button ~label:"Cancel"
1526    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1527  (* moved here to have visibility of the ok button *)
1528  let newinputt =
1529   TexTermEditor'.term_editor
1530    mqi_handle
1531    ~width:400 ~height:100 ~packing:scrolled_window#add
1532    ~share_id_to_uris_with:inputt ()
1533    ~isnotempty_callback:
1534     (function b ->
1535       non_empty_type := b ;
1536       okb#misc#set_sensitive (b && uri_entry#text <> ""))
1537  in
1538  let _ =
1539 let xxx = inputt#get_as_string in
1540 prerr_endline ("######################## " ^ xxx) ;
1541   newinputt#set_term xxx ;
1542 (*
1543   newinputt#set_term inputt#get_as_string ;
1544 *)
1545   inputt#reset in
1546  let _ =
1547   uri_entry#connect#changed
1548    (function () ->
1549      okb#misc#set_sensitive (!non_empty_type && uri_entry#text <> ""))
1550  in
1551  ignore (window#connect#destroy GMain.Main.quit) ;
1552  ignore (cancelb#connect#clicked window#destroy) ;
1553  ignore
1554   (okb#connect#clicked
1555     (function () ->
1556       chosen := true ;
1557       try
1558        let metasenv,parsed = newinputt#get_metasenv_and_term [] [] in
1559        let uristr = "cic:" ^ uri_entry#text in
1560        let uri = UriManager.uri_of_string uristr in
1561         if String.sub uristr (String.length uristr - 4) 4 <> ".con" then
1562          raise NotAUriToAConstant
1563         else
1564          begin
1565           try
1566            ignore (Getter.resolve uri) ;
1567            raise UriAlreadyInUse
1568           with
1569            Getter.Unresolved ->
1570             get_metasenv_and_term := (function () -> metasenv,parsed) ;
1571             get_uri := (function () -> uri) ; 
1572             window#destroy ()
1573          end
1574       with
1575        e ->
1576         output_html outputhtml
1577          ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1578   )) ;
1579  window#show () ;
1580  GtkThread.main ();
1581  if !chosen then
1582   try
1583    let metasenv,expr = !get_metasenv_and_term () in
1584     let _  = CicTypeChecker.type_of_aux' metasenv [] expr in
1585      ProofEngine.set_proof
1586       (Some (!get_uri (), (1,[],expr)::metasenv, Cic.Meta (1,[]), expr)) ;
1587      set_proof_engine_goal (Some 1) ;
1588      refresh_goals notebook ;
1589      refresh_proof output ;
1590      !save_set_sensitive true ;
1591      inputt#reset ;
1592      ProofEngine.intros ~mk_fresh_name_callback () ;
1593      refresh_goals notebook ;
1594      refresh_proof output
1595   with
1596      InvokeTactics.RefreshSequentException e ->
1597       output_html outputhtml
1598        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
1599         "sequent: " ^ Printexc.to_string e ^ "</h1>")
1600    | InvokeTactics.RefreshProofException e ->
1601       output_html outputhtml
1602        ("<h1 color=\"red\">Exception raised during the refresh of the " ^
1603         "proof: " ^ Printexc.to_string e ^ "</h1>")
1604    | e ->
1605       output_html outputhtml
1606        ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1607 ;;
1608
1609 let check_term_in_scratch scratch_window metasenv context expr = 
1610  try
1611   let ty = CicTypeChecker.type_of_aux' metasenv context expr in
1612   let expr = Cic.Cast (expr,ty) in
1613    scratch_window#show () ;
1614    scratch_window#set_term expr ;
1615    scratch_window#set_context context ;
1616    scratch_window#set_metasenv metasenv ;
1617    scratch_window#sequent_viewer#load_sequent metasenv (111,context,expr)
1618  with
1619   e ->
1620    print_endline ("? " ^ CicPp.ppterm expr) ;
1621    raise e
1622 ;;
1623
1624 let check scratch_window () =
1625  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
1626  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1627   let metasenv =
1628    match ProofEngine.get_proof () with
1629       None -> []
1630     | Some (_,metasenv,_,_) -> metasenv
1631   in
1632   let context =
1633    match !ProofEngine.goal with
1634       None -> []
1635     | Some metano ->
1636        let (_,canonical_context,_) =
1637         List.find (function (m,_,_) -> m=metano) metasenv
1638        in
1639         canonical_context
1640   in
1641    try
1642     let metasenv',expr = inputt#get_metasenv_and_term context metasenv in
1643      check_term_in_scratch scratch_window metasenv' context expr
1644    with
1645     e ->
1646      output_html outputhtml
1647       ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1648 ;;
1649
1650 let show () =
1651  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1652   try
1653    show_in_show_window_uri (input_or_locate_uri ~title:"Show")
1654   with
1655    e ->
1656     output_html outputhtml
1657      ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1658 ;;
1659
1660 exception NotADefinition;;
1661
1662 let open_ () =
1663  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1664  let output = ((rendering_window ())#output : TermViewer.proof_viewer) in
1665  let notebook = (rendering_window ())#notebook in
1666    try
1667     let uri = input_or_locate_uri ~title:"Open" in
1668      CicTypeChecker.typecheck uri ;
1669      let metasenv,bo,ty =
1670       match CicEnvironment.get_cooked_obj uri with
1671          Cic.Constant (_,Some bo,ty,_) -> [],bo,ty
1672        | Cic.CurrentProof (_,metasenv,bo,ty,_) -> metasenv,bo,ty
1673        | Cic.Constant _
1674        | Cic.Variable _
1675        | Cic.InductiveDefinition _ -> raise NotADefinition
1676      in
1677       ProofEngine.set_proof
1678        (Some (uri, metasenv, bo, ty)) ;
1679       set_proof_engine_goal None ;
1680       refresh_goals notebook ;
1681       refresh_proof output ;
1682       !save_set_sensitive true
1683    with
1684       InvokeTactics.RefreshSequentException e ->
1685        output_html outputhtml
1686         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
1687          "sequent: " ^ Printexc.to_string e ^ "</h1>")
1688     | InvokeTactics.RefreshProofException e ->
1689        output_html outputhtml
1690         ("<h1 color=\"red\">Exception raised during the refresh of the " ^
1691          "proof: " ^ Printexc.to_string e ^ "</h1>")
1692     | e ->
1693        output_html outputhtml
1694         ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1695 ;;
1696
1697 let show_query_results results =
1698  let window =
1699   GWindow.window
1700    ~modal:false ~title:"Query results." ~border_width:2 () in
1701  let vbox = GPack.vbox ~packing:window#add () in
1702  let hbox =
1703   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1704  let lMessage =
1705   GMisc.label
1706    ~text:"Click on a URI to show that object"
1707    ~packing:hbox#add () in
1708  let scrolled_window =
1709   GBin.scrolled_window ~border_width:10 ~height:400 ~width:600
1710    ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
1711  let clist = GList.clist ~columns:1 ~packing:scrolled_window#add () in
1712   ignore
1713    (List.map
1714      (function (uri,_) ->
1715        let n =
1716         clist#append [uri]
1717        in
1718         clist#set_row ~selectable:false n
1719      ) results
1720    ) ;
1721   clist#columns_autosize () ;
1722   ignore
1723    (clist#connect#select_row
1724      (fun ~row ~column ~event ->
1725        let (uristr,_) = List.nth results row in
1726         match
1727          MQueryMisc.cic_textual_parser_uri_of_string
1728           (MQueryMisc.wrong_xpointer_format_from_wrong_xpointer_format'
1729             uristr)
1730         with
1731            CicTextualParser0.ConUri uri
1732          | CicTextualParser0.VarUri uri
1733          | CicTextualParser0.IndTyUri (uri,_)
1734          | CicTextualParser0.IndConUri (uri,_,_) ->
1735             show_in_show_window_uri uri
1736      )
1737    ) ;
1738   window#show ()
1739 ;;
1740
1741 let refine_constraints (must_obj,must_rel,must_sort) =
1742  let chosen = ref false in
1743  let use_only = ref false in
1744  let window =
1745   GWindow.window
1746    ~modal:true ~title:"Constraints refinement."
1747    ~width:800 ~border_width:2 () in
1748  let vbox = GPack.vbox ~packing:window#add () in
1749  let hbox =
1750   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1751  let lMessage =
1752   GMisc.label
1753    ~text: "\"Only\" constraints can be enforced or not."
1754    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1755  let onlyb =
1756   GButton.toggle_button ~label:"Enforce \"only\" constraints"
1757    ~active:false ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) ()
1758  in
1759   ignore
1760    (onlyb#connect#toggled (function () -> use_only := onlyb#active)) ;
1761  (* Notebook for the constraints choice *)
1762  let notebook =
1763   GPack.notebook ~scrollable:true
1764    ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
1765  (* Rel constraints *)
1766  let label =
1767   GMisc.label
1768    ~text: "Constraints on Rels" () in
1769  let vbox' =
1770   GPack.vbox ~packing:(notebook#append_page ~tab_label:label#coerce)
1771    () in
1772  let hbox =
1773   GPack.hbox ~packing:(vbox'#pack ~expand:false ~fill:false ~padding:5) () in
1774  let lMessage =
1775   GMisc.label
1776    ~text: "You can now specify the constraints on Rels."
1777    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1778  let expected_height = 25 * (List.length must_rel + 2) in
1779  let height = if expected_height > 400 then 400 else expected_height in
1780  let scrolled_window =
1781   GBin.scrolled_window ~border_width:10 ~height ~width:600
1782    ~packing:(vbox'#pack ~expand:true ~fill:true ~padding:5) () in
1783  let scrolled_vbox = GPack.vbox ~packing:scrolled_window#add_with_viewport () in
1784  let mk_depth_button (hbox:GPack.box) d =
1785     let mutable_ref = ref (Some d) in
1786     let depthb =
1787      GButton.toggle_button
1788       ~label:("depth = " ^ string_of_int d) 
1789       ~active:true
1790       ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) ()
1791     in
1792      ignore
1793       (depthb#connect#toggled
1794        (function () ->
1795         let sel_depth = if depthb#active then Some d else None in
1796          mutable_ref := sel_depth
1797        )) ; mutable_ref
1798  in
1799  let rel_constraints =
1800   List.map
1801    (function p ->
1802      let hbox =
1803       GPack.hbox
1804        ~packing:(scrolled_vbox#pack ~expand:false ~fill:false ~padding:5) () in
1805      let lMessage =
1806       GMisc.label
1807        ~text:(MQGU.text_of_position (p:>MQGT.full_position))
1808        ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1809      match p with
1810       | `MainHypothesis None 
1811       | `MainConclusion None -> p, ref None
1812       | `MainHypothesis (Some depth') 
1813       | `MainConclusion (Some depth') -> p, mk_depth_button hbox depth'
1814    ) must_rel in
1815  (* Sort constraints *)
1816  let label =
1817   GMisc.label
1818    ~text: "Constraints on Sorts" () in
1819  let vbox' =
1820   GPack.vbox ~packing:(notebook#append_page ~tab_label:label#coerce)
1821    () in
1822  let hbox =
1823   GPack.hbox ~packing:(vbox'#pack ~expand:false ~fill:false ~padding:5) () in
1824  let lMessage =
1825   GMisc.label
1826    ~text: "You can now specify the constraints on Sorts."
1827    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1828  let expected_height = 25 * (List.length must_sort + 2) in
1829  let height = if expected_height > 400 then 400 else expected_height in
1830  let scrolled_window =
1831   GBin.scrolled_window ~border_width:10 ~height ~width:600
1832    ~packing:(vbox'#pack ~expand:true ~fill:true ~padding:5) () in
1833  let scrolled_vbox = GPack.vbox ~packing:scrolled_window#add_with_viewport () in
1834  let sort_constraints =
1835   List.map
1836    (function (p, sort) ->
1837      let hbox =
1838       GPack.hbox
1839        ~packing:(scrolled_vbox#pack ~expand:false ~fill:false ~padding:5) () in
1840      let lMessage =
1841       GMisc.label
1842        ~text:(MQGU.text_of_sort sort ^ " " ^ MQGU.text_of_position (p:>MQGT.full_position))
1843        ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1844      match p with
1845       | `MainHypothesis None 
1846       | `MainConclusion None -> p, ref None, sort
1847       | `MainHypothesis (Some depth') 
1848       | `MainConclusion (Some depth') -> p, mk_depth_button hbox depth', sort
1849    ) must_sort in
1850  (* Obj constraints *)
1851  let label =
1852   GMisc.label
1853    ~text: "Constraints on constants" () in
1854  let vbox' =
1855   GPack.vbox ~packing:(notebook#append_page ~tab_label:label#coerce)
1856    () in
1857  let hbox =
1858   GPack.hbox ~packing:(vbox'#pack ~expand:false ~fill:false ~padding:5) () in
1859  let lMessage =
1860   GMisc.label
1861    ~text: "You can now specify the constraints on constants."
1862    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1863  let expected_height = 25 * (List.length must_obj + 2) in
1864  let height = if expected_height > 400 then 400 else expected_height in
1865  let scrolled_window =
1866   GBin.scrolled_window ~border_width:10 ~height ~width:600
1867    ~packing:(vbox'#pack ~expand:true ~fill:true ~padding:5) () in
1868  let scrolled_vbox = GPack.vbox ~packing:scrolled_window#add_with_viewport () in
1869  let obj_constraints =
1870   List.map
1871    (function (p, uri) ->
1872      let hbox =
1873       GPack.hbox
1874        ~packing:(scrolled_vbox#pack ~expand:false ~fill:false ~padding:5) () in
1875      let lMessage =
1876       GMisc.label
1877        ~text:(uri ^ " " ^ (MQGU.text_of_position p))
1878        ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1879      match p with
1880       | `InBody
1881       | `InHypothesis 
1882       | `InConclusion 
1883       | `MainHypothesis None 
1884       | `MainConclusion None -> p, ref None, uri
1885       | `MainHypothesis (Some depth') 
1886       | `MainConclusion (Some depth') -> p, mk_depth_button hbox depth', uri
1887    ) must_obj in
1888  (* Confirm/abort buttons *)
1889  let hbox =
1890   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1891  let okb =
1892   GButton.button ~label:"Ok"
1893    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1894  let cancelb =
1895   GButton.button ~label:"Abort"
1896    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) ()
1897  in
1898   ignore (window#connect#destroy GMain.Main.quit) ;
1899   ignore (cancelb#connect#clicked window#destroy) ;
1900   ignore
1901    (okb#connect#clicked (function () -> chosen := true ; window#destroy ()));
1902   window#set_position `CENTER ;
1903   window#show () ;
1904   GtkThread.main ();
1905   if !chosen then
1906    let chosen_must_rel =
1907     List.map
1908      (function (position, ref_depth) -> MQGU.set_main_position position !ref_depth)
1909      rel_constraints
1910    in
1911    let chosen_must_sort =
1912     List.map
1913      (function (position, ref_depth, sort) -> 
1914       MQGU.set_main_position position !ref_depth,sort)
1915      sort_constraints
1916    in
1917    let chosen_must_obj =
1918     List.map
1919      (function (position, ref_depth, uri) -> MQGU.set_full_position position !ref_depth, uri)
1920      obj_constraints
1921    in
1922     (chosen_must_obj,chosen_must_rel,chosen_must_sort),
1923      (if !use_only then
1924 (*CSC: ???????????????????????? I assume that must and only are the same... *)
1925        Some chosen_must_obj,Some chosen_must_rel,Some chosen_must_sort
1926       else
1927        None,None,None
1928      )
1929   else
1930    raise NoChoice
1931 ;;
1932
1933 let completeSearchPattern () =
1934  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
1935  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1936   try
1937    let metasenv,expr = inputt#get_metasenv_and_term ~context:[] ~metasenv:[] in
1938    let must = CGSearchPattern.get_constraints expr in
1939    let must',only = refine_constraints must in
1940    let query =
1941     MQG.query_of_constraints (Some CGSearchPattern.universe) must' only
1942    in
1943    let results = MQI.execute mqi_handle query in 
1944     show_query_results results
1945   with
1946    e ->
1947     output_html outputhtml
1948      ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
1949 ;;
1950
1951 let insertQuery () =
1952  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
1953   try
1954    let chosen = ref None in
1955    let window =
1956     GWindow.window
1957      ~modal:true ~title:"Insert Query (Experts Only)" ~border_width:2 () in
1958    let vbox = GPack.vbox ~packing:window#add () in
1959    let label =
1960     GMisc.label ~text:"Insert Query. For Experts Only."
1961      ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1962    let scrolled_window =
1963     GBin.scrolled_window ~border_width:10 ~height:400 ~width:600
1964      ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
1965    let input = GEdit.text ~editable:true
1966     ~packing:scrolled_window#add () in
1967    let hbox =
1968     GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
1969    let okb =
1970     GButton.button ~label:"Ok"
1971      ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1972    let loadb =
1973     GButton.button ~label:"Load from file..."
1974      ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1975    let cancelb =
1976     GButton.button ~label:"Abort"
1977      ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
1978    ignore (window#connect#destroy GMain.Main.quit) ;
1979    ignore (cancelb#connect#clicked window#destroy) ;
1980    ignore
1981     (okb#connect#clicked
1982       (function () ->
1983         chosen := Some (input#get_chars 0 input#length) ; window#destroy ())) ;
1984    ignore
1985     (loadb#connect#clicked
1986       (function () ->
1987         match
1988          GToolbox.select_file ~title:"Select Query File" ()
1989         with
1990            None -> ()
1991          | Some filename ->
1992             let inch = open_in filename in
1993              let rec read_file () =
1994               try
1995                let line = input_line inch in
1996                 line ^ "\n" ^ read_file ()
1997               with
1998                End_of_file -> ""
1999              in
2000               let text = read_file () in
2001                input#delete_text 0 input#length ;
2002                ignore (input#insert_text text ~pos:0))) ;
2003    window#set_position `CENTER ;
2004    window#show () ;
2005    GtkThread.main ();
2006    match !chosen with
2007       None -> ()
2008     | Some q ->
2009        let results =
2010         MQI.execute mqi_handle (MQueryUtil.query_of_text (Lexing.from_string q))
2011        in
2012         show_query_results results
2013   with
2014    e ->
2015     output_html outputhtml
2016      ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>") ;
2017 ;;
2018
2019 let choose_must list_of_must only =
2020  let chosen = ref None in
2021  let user_constraints = ref [] in
2022  let window =
2023   GWindow.window
2024    ~modal:true ~title:"Query refinement." ~border_width:2 () in
2025  let vbox = GPack.vbox ~packing:window#add () in
2026  let hbox =
2027   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
2028  let lMessage =
2029   GMisc.label
2030    ~text:
2031     ("You can now specify the genericity of the query. " ^
2032      "The more generic the slower.")
2033    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2034  let hbox =
2035   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
2036  let lMessage =
2037   GMisc.label
2038    ~text:
2039     "Suggestion: start with faster queries before moving to more generic ones."
2040    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2041  let notebook =
2042   GPack.notebook ~scrollable:true
2043    ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
2044  let _ =
2045   let page = ref 0 in
2046   let last = List.length list_of_must in
2047   List.map
2048    (function must ->
2049      incr page ;
2050      let label =
2051       GMisc.label ~text:
2052        (if !page = 1 then "More generic" else
2053          if !page = last then "More precise" else "          ") () in
2054      let expected_height = 25 * (List.length must + 2) in
2055      let height = if expected_height > 400 then 400 else expected_height in
2056      let scrolled_window =
2057       GBin.scrolled_window ~border_width:10 ~height ~width:600
2058        ~packing:(notebook#append_page ~tab_label:label#coerce) () in
2059      let clist =
2060         GList.clist ~columns:2 ~packing:scrolled_window#add
2061          ~titles:["URI" ; "Position"] ()
2062      in
2063       ignore
2064        (List.map
2065          (function (position, uri) ->
2066            let n =
2067             clist#append 
2068              [uri; MQGUtil.text_of_position position]
2069            in
2070             clist#set_row ~selectable:false n
2071          ) must
2072        ) ;
2073       clist#columns_autosize ()
2074    ) list_of_must in
2075  let _ =
2076   let label = GMisc.label ~text:"User provided" () in
2077   let vbox =
2078    GPack.vbox ~packing:(notebook#append_page ~tab_label:label#coerce) () in
2079   let hbox =
2080    GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
2081   let lMessage =
2082    GMisc.label
2083    ~text:"Select the constraints that must be satisfied and press OK."
2084    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2085   let expected_height = 25 * (List.length only + 2) in
2086   let height = if expected_height > 400 then 400 else expected_height in
2087   let scrolled_window =
2088    GBin.scrolled_window ~border_width:10 ~height ~width:600
2089     ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
2090   let clist =
2091    GList.clist ~columns:2 ~packing:scrolled_window#add
2092     ~selection_mode:`EXTENDED
2093     ~titles:["URI" ; "Position"] ()
2094   in
2095    ignore
2096     (List.map
2097       (function (position, uri) ->
2098         let n =
2099          clist#append 
2100           [uri; MQGUtil.text_of_position position]
2101         in
2102          clist#set_row ~selectable:true n
2103       ) only
2104     ) ;
2105    clist#columns_autosize () ;
2106    ignore
2107     (clist#connect#select_row
2108       (fun ~row ~column ~event ->
2109         user_constraints := (List.nth only row)::!user_constraints)) ;
2110    ignore
2111     (clist#connect#unselect_row
2112       (fun ~row ~column ~event ->
2113         user_constraints :=
2114          List.filter
2115           (function uri -> uri != (List.nth only row)) !user_constraints)) ;
2116  in
2117  let hbox =
2118   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
2119  let okb =
2120   GButton.button ~label:"Ok"
2121    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2122  let cancelb =
2123   GButton.button ~label:"Abort"
2124    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2125  (* actions *)
2126  ignore (window#connect#destroy GMain.Main.quit) ;
2127  ignore (cancelb#connect#clicked window#destroy) ;
2128  ignore
2129   (okb#connect#clicked
2130     (function () -> chosen := Some notebook#current_page ; window#destroy ())) ;
2131  window#set_position `CENTER ;
2132  window#show () ;
2133  GtkThread.main ();
2134  match !chosen with
2135     None -> raise NoChoice
2136   | Some n ->
2137      if n = List.length list_of_must then
2138       (* user provided constraints *)
2139       !user_constraints
2140      else
2141       List.nth list_of_must n
2142 ;;
2143
2144 let searchPattern () =
2145  let inputt = ((rendering_window ())#inputt : TermEditor.term_editor) in
2146  let outputhtml = ((rendering_window ())#outputhtml : GHtml.xmhtml) in
2147   try
2148     let proof =
2149      match ProofEngine.get_proof () with
2150         None -> assert false
2151       | Some proof -> proof
2152     in
2153      match !ProofEngine.goal with
2154       | None -> ()
2155       | Some metano ->
2156          let uris' =
2157            TacticChaser.matchConclusion
2158             mqi_handle
2159             ~output_html:(output_html outputhtml) ~choose_must ()
2160             ~status:(proof, metano)
2161          in
2162          let uri' =
2163           user_uri_choice ~title:"Ambiguous input."
2164           ~msg: "Many lemmas can be successfully applied. Please, choose one:"
2165            uris'
2166          in
2167           inputt#set_term uri' ;
2168           InvokeTactics'.apply ()
2169   with
2170    e -> 
2171     output_html outputhtml 
2172      ("<h1 color=\"red\">" ^ Printexc.to_string e ^ "</h1>")
2173 ;;
2174       
2175 let choose_selection mmlwidget (element : Gdome.element option) =
2176  let module G = Gdome in
2177   let rec aux element =
2178    if element#hasAttributeNS
2179        ~namespaceURI:Misc.helmns
2180        ~localName:(G.domString "xref")
2181    then
2182      mmlwidget#set_selection (Some element)
2183    else
2184     try
2185       match element#get_parentNode with
2186          None -> assert false
2187        (*CSC: OCAML DIVERGES!
2188        | Some p -> aux (new G.element_of_node p)
2189        *)
2190        | Some p -> aux (new Gdome.element_of_node p)
2191     with
2192        GdomeInit.DOMCastException _ ->
2193         prerr_endline
2194          "******* trying to select above the document root ********"
2195   in
2196    match element with
2197      Some x -> aux x
2198    | None   -> mmlwidget#set_selection None
2199 ;;
2200
2201 (* STUFF TO BUILD THE GTK INTERFACE *)
2202
2203 (* Stuff for the widget settings *)
2204
2205 let export_to_postscript output =
2206  let lastdir = ref (Unix.getcwd ()) in
2207   function () ->
2208    match
2209     GToolbox.select_file ~title:"Export to PostScript"
2210      ~dir:lastdir ~filename:"screenshot.ps" ()
2211    with
2212       None -> ()
2213     | Some filename ->
2214        (output :> GMathView.math_view)#export_to_postscript
2215          ~filename:filename ();
2216 ;;
2217
2218 let activate_t1 output button_set_anti_aliasing
2219  button_set_transparency export_to_postscript_menu_item
2220  button_t1 ()
2221 =
2222  let is_set = button_t1#active in
2223   output#set_font_manager_type
2224    ~fm_type:(if is_set then `font_manager_t1 else `font_manager_gtk) ;
2225   if is_set then
2226    begin
2227     button_set_anti_aliasing#misc#set_sensitive true ;
2228     button_set_transparency#misc#set_sensitive true ;
2229     export_to_postscript_menu_item#misc#set_sensitive true ;
2230    end
2231   else
2232    begin
2233     button_set_anti_aliasing#misc#set_sensitive false ;
2234     button_set_transparency#misc#set_sensitive false ;
2235     export_to_postscript_menu_item#misc#set_sensitive false ;
2236    end
2237 ;;
2238
2239 let set_anti_aliasing output button_set_anti_aliasing () =
2240  output#set_anti_aliasing button_set_anti_aliasing#active
2241 ;;
2242
2243 let set_transparency output button_set_transparency () =
2244  output#set_transparency button_set_transparency#active
2245 ;;
2246
2247 let changefont output font_size_spinb () =
2248  output#set_font_size font_size_spinb#value_as_int
2249 ;;
2250
2251 let set_log_verbosity output log_verbosity_spinb () =
2252  output#set_log_verbosity log_verbosity_spinb#value_as_int
2253 ;;
2254
2255 class settings_window output sw
2256  export_to_postscript_menu_item selection_changed_callback
2257 =
2258  let settings_window = GWindow.window ~title:"GtkMathView settings" () in
2259  let vbox =
2260   GPack.vbox ~packing:settings_window#add () in
2261  let table =
2262   GPack.table
2263    ~rows:1 ~columns:3 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
2264    ~border_width:5 ~packing:vbox#add () in
2265  let button_t1 =
2266   GButton.toggle_button ~label:"activate t1 fonts"
2267    ~packing:(table#attach ~left:0 ~top:0) () in
2268  let button_set_anti_aliasing =
2269   GButton.toggle_button ~label:"set_anti_aliasing"
2270    ~packing:(table#attach ~left:0 ~top:1) () in
2271  let button_set_transparency =
2272   GButton.toggle_button ~label:"set_transparency"
2273    ~packing:(table#attach ~left:2 ~top:1) () in
2274  let table =
2275   GPack.table
2276    ~rows:2 ~columns:2 ~homogeneous:false ~row_spacings:5 ~col_spacings:5
2277    ~border_width:5 ~packing:vbox#add () in
2278  let font_size_label =
2279   GMisc.label ~text:"font size:"
2280    ~packing:(table#attach ~left:0 ~top:0 ~expand:`NONE) () in
2281  let font_size_spinb =
2282   let sadj =
2283    GData.adjustment ~value:(float_of_int output#get_font_size)
2284     ~lower:5.0 ~upper:50.0 ~step_incr:1.0 ()
2285   in
2286    GEdit.spin_button 
2287     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:0 ~fill:`NONE) () in
2288  let log_verbosity_label =
2289   GMisc.label ~text:"log verbosity:"
2290    ~packing:(table#attach ~left:0 ~top:1) () in
2291  let log_verbosity_spinb =
2292   let sadj =
2293    GData.adjustment ~value:0.0 ~lower:0.0 ~upper:3.0 ~step_incr:1.0 ()
2294   in
2295    GEdit.spin_button 
2296     ~adjustment:sadj ~packing:(table#attach ~left:1 ~top:1) () in
2297  let hbox =
2298   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
2299  let closeb =
2300   GButton.button ~label:"Close"
2301    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2302 object(self)
2303  method show = settings_window#show
2304  initializer
2305   button_set_anti_aliasing#misc#set_sensitive false ;
2306   button_set_transparency#misc#set_sensitive false ;
2307   (* Signals connection *)
2308   ignore(button_t1#connect#clicked
2309    (activate_t1 output button_set_anti_aliasing
2310     button_set_transparency export_to_postscript_menu_item button_t1)) ;
2311   ignore(font_size_spinb#connect#changed (changefont output font_size_spinb)) ;
2312   ignore(button_set_anti_aliasing#connect#toggled
2313    (set_anti_aliasing output button_set_anti_aliasing));
2314   ignore(button_set_transparency#connect#toggled
2315    (set_transparency output button_set_transparency)) ;
2316   ignore(log_verbosity_spinb#connect#changed
2317    (set_log_verbosity output log_verbosity_spinb)) ;
2318   ignore(closeb#connect#clicked settings_window#misc#hide)
2319 end;;
2320
2321 (* Scratch window *)
2322
2323 class scratch_window =
2324  let window =
2325   GWindow.window
2326     ~title:"MathML viewer"
2327     ~border_width:2 () in
2328  let vbox =
2329   GPack.vbox ~packing:window#add () in
2330  let hbox =
2331   GPack.hbox ~packing:(vbox#pack ~expand:false ~fill:false ~padding:5) () in
2332  let whdb =
2333   GButton.button ~label:"Whd"
2334    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2335  let reduceb =
2336   GButton.button ~label:"Reduce"
2337    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2338  let simplb =
2339   GButton.button ~label:"Simpl"
2340    ~packing:(hbox#pack ~expand:false ~fill:false ~padding:5) () in
2341  let scrolled_window =
2342   GBin.scrolled_window ~border_width:10
2343    ~packing:(vbox#pack ~expand:true ~padding:5) () in
2344  let sequent_viewer =
2345   TermViewer.sequent_viewer
2346    ~packing:(scrolled_window#add) ~width:400 ~height:280 () in
2347 object(self)
2348  val mutable term = Cic.Rel 1                 (* dummy value *)
2349  val mutable context = ([] : Cic.context)     (* dummy value *)
2350  val mutable metasenv = ([] : Cic.metasenv)   (* dummy value *)
2351  method sequent_viewer = sequent_viewer
2352  method show () = window#misc#hide () ; window#show ()
2353  method term = term
2354  method set_term t = term <- t
2355  method context = context
2356  method set_context t = context <- t
2357  method metasenv = metasenv
2358  method set_metasenv t = metasenv <- t
2359  initializer
2360   ignore
2361    (sequent_viewer#connect#selection_changed (choose_selection sequent_viewer));
2362   ignore(window#event#connect#delete (fun _ -> window#misc#hide () ; true )) ;
2363   ignore(whdb#connect#clicked InvokeTactics'.whd_in_scratch) ;
2364   ignore(reduceb#connect#clicked InvokeTactics'.reduce_in_scratch) ;
2365   ignore(simplb#connect#clicked InvokeTactics'.simpl_in_scratch)
2366 end;;
2367
2368 let open_contextual_menu_for_selected_terms mmlwidget infos =
2369  let button = GdkEvent.Button.button infos in 
2370  let terms_selected = List.length mmlwidget#get_selections > 0 in
2371   if button = 3 then
2372    begin
2373     let time = GdkEvent.Button.time infos in
2374     let menu = GMenu.menu () in
2375     let f = new GMenu.factory menu in
2376     let whd_menu_item =
2377      f#add_item "Whd" ~key:GdkKeysyms._W ~callback:InvokeTactics'.whd in
2378     let reduce_menu_item =
2379      f#add_item "Reduce" ~key:GdkKeysyms._R ~callback:InvokeTactics'.reduce in
2380     let simpl_menu_item =
2381      f#add_item "Simpl" ~key:GdkKeysyms._S ~callback:InvokeTactics'.simpl in
2382     let _ = f#add_separator () in
2383     let generalize_menu_item =
2384      f#add_item "Generalize"
2385       ~key:GdkKeysyms._G ~callback:InvokeTactics'.generalize in
2386     let _ = f#add_separator () in
2387     let clear_menu_item =
2388      f#add_item "Clear" ~key:GdkKeysyms._C ~callback:InvokeTactics'.clear in
2389     let clearbody_menu_item =
2390      f#add_item "ClearBody"
2391       ~key:GdkKeysyms._B ~callback:InvokeTactics'.clearbody
2392     in
2393      whd_menu_item#misc#set_sensitive terms_selected ; 
2394      reduce_menu_item#misc#set_sensitive terms_selected ; 
2395      simpl_menu_item#misc#set_sensitive terms_selected ;
2396      generalize_menu_item#misc#set_sensitive terms_selected ;
2397      clear_menu_item#misc#set_sensitive terms_selected ;
2398      clearbody_menu_item#misc#set_sensitive terms_selected ;
2399      menu#popup ~button ~time
2400    end ;
2401   true
2402 ;;
2403
2404 class page () =
2405  let vbox1 = GPack.vbox () in
2406 object(self)
2407  val mutable proofw_ref = None
2408  val mutable compute_ref = None
2409  method proofw =
2410   Lazy.force self#compute ;
2411   match proofw_ref with
2412      None -> assert false
2413    | Some proofw -> proofw
2414  method content = vbox1
2415  method compute =
2416   match compute_ref with
2417      None -> assert false
2418    | Some compute -> compute
2419  initializer
2420   compute_ref <-
2421    Some (lazy (
2422    let scrolled_window1 =
2423     GBin.scrolled_window ~border_width:10
2424      ~packing:(vbox1#pack ~expand:true ~padding:5) () in
2425    let proofw =
2426     TermViewer.sequent_viewer ~width:400 ~height:275
2427      ~packing:(scrolled_window1#add) () in
2428    let _ = proofw_ref <- Some proofw in
2429    let hbox3 =
2430     GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
2431    let ringb =
2432     GButton.button ~label:"Ring"
2433      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2434    let fourierb =
2435     GButton.button ~label:"Fourier"
2436      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2437    let reflexivityb =
2438     GButton.button ~label:"Reflexivity"
2439      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2440    let symmetryb =
2441     GButton.button ~label:"Symmetry"
2442      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2443    let assumptionb =
2444     GButton.button ~label:"Assumption"
2445      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2446    let contradictionb =
2447     GButton.button ~label:"Contradiction"
2448      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2449    let hbox4 =
2450     GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
2451    let existsb =
2452     GButton.button ~label:"Exists"
2453      ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
2454    let splitb =
2455     GButton.button ~label:"Split"
2456      ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
2457    let leftb =
2458     GButton.button ~label:"Left"
2459      ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
2460    let rightb =
2461     GButton.button ~label:"Right"
2462      ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
2463    let searchpatternb =
2464     GButton.button ~label:"SearchPattern_Apply"
2465      ~packing:(hbox4#pack ~expand:false ~fill:false ~padding:5) () in
2466    let hbox5 =
2467     GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
2468    let exactb =
2469     GButton.button ~label:"Exact"
2470      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2471    let introsb =
2472     GButton.button ~label:"Intros"
2473      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2474    let applyb =
2475     GButton.button ~label:"Apply"
2476      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2477    let elimintrossimplb =
2478     GButton.button ~label:"ElimIntrosSimpl"
2479      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2480    let elimtypeb =
2481     GButton.button ~label:"ElimType"
2482      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2483    let foldwhdb =
2484     GButton.button ~label:"Fold_whd"
2485      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2486    let foldreduceb =
2487     GButton.button ~label:"Fold_reduce"
2488      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2489    let hbox6 =
2490     GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
2491    let foldsimplb =
2492     GButton.button ~label:"Fold_simpl"
2493      ~packing:(hbox6#pack ~expand:false ~fill:false ~padding:5) () in
2494    let cutb =
2495     GButton.button ~label:"Cut"
2496      ~packing:(hbox6#pack ~expand:false ~fill:false ~padding:5) () in
2497    let changeb =
2498     GButton.button ~label:"Change"
2499      ~packing:(hbox6#pack ~expand:false ~fill:false ~padding:5) () in
2500    let letinb =
2501     GButton.button ~label:"Let ... In"
2502      ~packing:(hbox6#pack ~expand:false ~fill:false ~padding:5) () in
2503    let rewritesimplb =
2504     GButton.button ~label:"RewriteSimpl ->"
2505      ~packing:(hbox6#pack ~expand:false ~fill:false ~padding:5) () in
2506    let rewritebacksimplb =
2507     GButton.button ~label:"RewriteSimpl <-"
2508      ~packing:(hbox6#pack ~expand:false ~fill:false ~padding:5) () in
2509    let hbox7 =
2510     GPack.hbox ~packing:(vbox1#pack ~expand:false ~fill:false ~padding:5) () in
2511    let absurdb =
2512     GButton.button ~label:"Absurd"
2513      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2514    let decomposeb =
2515     GButton.button ~label:"Decompose"
2516      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2517    let transitivityb =
2518     GButton.button ~label:"Transitivity"
2519      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2520    let replaceb =
2521     GButton.button ~label:"Replace"
2522      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2523    let injectionb =
2524     GButton.button ~label:"Injection"
2525      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2526    let discriminateb =
2527     GButton.button ~label:"Discriminate"
2528      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2529 (* Zack: spostare in una toolbar
2530    let generalizeb =
2531     GButton.button ~label:"Generalize"
2532      ~packing:(hbox7#pack ~expand:false ~fill:false ~padding:5) () in
2533    let clearbodyb =
2534     GButton.button ~label:"ClearBody"
2535      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2536    let clearb =
2537     GButton.button ~label:"Clear"
2538      ~packing:(hbox5#pack ~expand:false ~fill:false ~padding:5) () in
2539    let whdb =
2540     GButton.button ~label:"Whd"
2541      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2542    let reduceb =
2543     GButton.button ~label:"Reduce"
2544      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2545    let simplb =
2546     GButton.button ~label:"Simpl"
2547      ~packing:(hbox3#pack ~expand:false ~fill:false ~padding:5) () in
2548 *)
2549
2550    ignore(exactb#connect#clicked InvokeTactics'.exact) ;
2551    ignore(applyb#connect#clicked InvokeTactics'.apply) ;
2552    ignore(elimintrossimplb#connect#clicked InvokeTactics'.elimintrossimpl) ;
2553    ignore(elimtypeb#connect#clicked InvokeTactics'.elimtype) ;
2554    ignore(foldwhdb#connect#clicked InvokeTactics'.fold_whd) ;
2555    ignore(foldreduceb#connect#clicked InvokeTactics'.fold_reduce) ;
2556    ignore(foldsimplb#connect#clicked InvokeTactics'.fold_simpl) ;
2557    ignore(cutb#connect#clicked InvokeTactics'.cut) ;
2558    ignore(changeb#connect#clicked InvokeTactics'.change) ;
2559    ignore(letinb#connect#clicked InvokeTactics'.letin) ;
2560    ignore(ringb#connect#clicked InvokeTactics'.ring) ;
2561    ignore(fourierb#connect#clicked InvokeTactics'.fourier) ;
2562    ignore(rewritesimplb#connect#clicked InvokeTactics'.rewritesimpl) ;
2563    ignore(rewritebacksimplb#connect#clicked InvokeTactics'.rewritebacksimpl) ;
2564    ignore(replaceb#connect#clicked InvokeTactics'.replace) ;
2565    ignore(reflexivityb#connect#clicked InvokeTactics'.reflexivity) ;
2566    ignore(symmetryb#connect#clicked InvokeTactics'.symmetry) ;
2567    ignore(transitivityb#connect#clicked InvokeTactics'.transitivity) ;
2568    ignore(existsb#connect#clicked InvokeTactics'.exists) ;
2569    ignore(splitb#connect#clicked InvokeTactics'.split) ;
2570    ignore(leftb#connect#clicked InvokeTactics'.left) ;
2571    ignore(rightb#connect#clicked InvokeTactics'.right) ;
2572    ignore(assumptionb#connect#clicked InvokeTactics'.assumption) ;
2573    ignore(absurdb#connect#clicked InvokeTactics'.absurd) ;
2574    ignore(contradictionb#connect#clicked InvokeTactics'.contradiction) ;
2575    ignore(introsb#connect#clicked InvokeTactics'.intros) ;
2576    ignore(decomposeb#connect#clicked InvokeTactics'.decompose) ;
2577    ignore(searchpatternb#connect#clicked searchPattern) ;
2578    ignore(injectionb#connect#clicked InvokeTactics'.injection) ;
2579    ignore(discriminateb#connect#clicked InvokeTactics'.discriminate) ;
2580 (* Zack: spostare in una toolbar
2581    ignore(whdb#connect#clicked whd) ;
2582    ignore(reduceb#connect#clicked reduce) ;
2583    ignore(simplb#connect#clicked simpl) ;
2584    ignore(clearbodyb#connect#clicked clearbody) ;
2585    ignore(clearb#connect#clicked clear) ;
2586    ignore(generalizeb#connect#clicked generalize) ;
2587 *)
2588    ignore(proofw#connect#selection_changed (choose_selection proofw)) ;
2589    ignore
2590      ((new GObj.event_ops proofw#as_widget)#connect#button_press
2591         (open_contextual_menu_for_selected_terms proofw)) ;
2592   ))
2593 end
2594 ;;
2595
2596 class empty_page =
2597  let vbox1 = GPack.vbox () in
2598  let scrolled_window1 =
2599   GBin.scrolled_window ~border_width:10
2600    ~packing:(vbox1#pack ~expand:true ~padding:5) () in
2601  let proofw =
2602   TermViewer.sequent_viewer ~width:400 ~height:275
2603    ~packing:(scrolled_window1#add) () in
2604 object(self)
2605  method proofw = (assert false : TermViewer.sequent_viewer)
2606  method content = vbox1
2607  method compute = (assert false : unit)
2608 end
2609 ;;
2610
2611 let empty_page = new empty_page;;
2612
2613 class notebook =
2614 object(self)
2615  val notebook = GPack.notebook ()
2616  val pages = ref []
2617  val mutable skip_switch_page_event = false 
2618  val mutable empty = true
2619  method notebook = notebook
2620  method add_page n =
2621   let new_page = new page () in
2622    empty <- false ;
2623    pages := !pages @ [n,lazy (setgoal n),new_page] ;
2624    notebook#append_page
2625     ~tab_label:((GMisc.label ~text:("?" ^ string_of_int n) ())#coerce)
2626     new_page#content#coerce
2627  method remove_all_pages ~skip_switch_page_event:skip =
2628   if empty then
2629    notebook#remove_page 0 (* let's remove the empty page *)
2630   else
2631    List.iter (function _ -> notebook#remove_page 0) !pages ;
2632   pages := [] ;
2633   skip_switch_page_event <- skip
2634  method set_current_page ~may_skip_switch_page_event n =
2635   let (_,_,page) = List.find (function (m,_,_) -> m=n) !pages in
2636    let new_page = notebook#page_num page#content#coerce in
2637     if may_skip_switch_page_event && new_page <> notebook#current_page then
2638      skip_switch_page_event <- true ;
2639     notebook#goto_page new_page
2640  method set_empty_page =
2641   empty <- true ;
2642   pages := [] ;
2643   notebook#append_page
2644    ~tab_label:((GMisc.label ~text:"No proof in progress" ())#coerce)
2645    empty_page#content#coerce
2646  method proofw =
2647   let (_,_,page) = List.nth !pages notebook#current_page in
2648    page#proofw
2649  initializer
2650   ignore
2651    (notebook#connect#switch_page
2652     (function i ->
2653       let skip = skip_switch_page_event in
2654        skip_switch_page_event <- false ;
2655        if not skip then
2656         try
2657          let (metano,setgoal,page) = List.nth !pages i in
2658           set_proof_engine_goal (Some metano) ;
2659           Lazy.force (page#compute) ;
2660           Lazy.force setgoal;
2661           if notify_hbugs_on_goal_change then
2662             Hbugs.notify ()
2663         with _ -> ()
2664     ))
2665 end
2666 ;;
2667
2668 let dump_environment () =
2669   try
2670     let oc = open_out environmentfile in
2671     output_html (outputhtml ()) "<b>Dumping environment ... </b><br />";
2672     CicEnvironment.dump_to_channel
2673       ~callback:(fun uri -> output_html (outputhtml ()) (uri ^ "<br />"))
2674       oc;
2675     output_html (outputhtml ()) "<b>... done!</b><br />";
2676     close_out oc
2677   with exc ->
2678     output_html (outputhtml ())
2679       (Printf.sprintf
2680         "<h1 color=\"red\">Dump failure, uncaught exception:%s</h1>"
2681         (Printexc.to_string exc))
2682 ;;
2683 let restore_environment () =
2684   try
2685     let ic = open_in environmentfile in
2686     output_html (outputhtml ()) "<b>Restoring environment ... </b><br />";
2687     CicEnvironment.restore_from_channel
2688       ~callback:(fun uri -> output_html (outputhtml ()) (uri ^ "<br />"))
2689       ic;
2690     output_html (outputhtml ()) "<b>... done!</b><br />";
2691     close_in ic
2692   with exc ->
2693     output_html (outputhtml ())
2694       (Printf.sprintf
2695         "<h1 color=\"red\">Restore failure, uncaught exception:%s</h1>"
2696         (Printexc.to_string exc))
2697 ;;
2698
2699 (* Main window *)
2700
2701 class rendering_window output (notebook : notebook) =
2702  let scratch_window = new scratch_window in
2703  let window =
2704   GWindow.window
2705    ~title:"gTopLevel - Helm's Proof Assistant"
2706    ~border_width:0 ~allow_shrink:false () in
2707  let vbox_for_menu = GPack.vbox ~packing:window#add () in
2708  (* menus *)
2709  let handle_box = GBin.handle_box ~border_width:2
2710   ~packing:(vbox_for_menu#pack ~padding:0) () in
2711  let menubar = GMenu.menu_bar ~packing:handle_box#add () in
2712  let factory0 = new GMenu.factory menubar in
2713  let accel_group = factory0#accel_group in
2714  (* file menu *)
2715  let file_menu = factory0#add_submenu "File" in
2716  let factory1 = new GMenu.factory file_menu ~accel_group in
2717  let export_to_postscript_menu_item =
2718   begin
2719    let _ =
2720     factory1#add_item "New Block of (Co)Inductive Definitions..."
2721      ~key:GdkKeysyms._B ~callback:new_inductive
2722    in
2723    let _ =
2724     factory1#add_item "New Proof or Definition..." ~key:GdkKeysyms._N
2725      ~callback:new_proof
2726    in
2727    let reopen_menu_item =
2728     factory1#add_item "Reopen a Finished Proof..." ~key:GdkKeysyms._R
2729      ~callback:open_
2730    in
2731    let qed_menu_item =
2732     factory1#add_item "Qed" ~key:GdkKeysyms._E ~callback:qed in
2733    ignore (factory1#add_separator ()) ;
2734    ignore
2735     (factory1#add_item "Load Unfinished Proof..." ~key:GdkKeysyms._L
2736       ~callback:load_unfinished_proof) ;
2737    let save_menu_item =
2738     factory1#add_item "Save Unfinished Proof" ~key:GdkKeysyms._S
2739       ~callback:save_unfinished_proof
2740    in
2741    ignore (factory1#add_separator ()) ;
2742    ignore (factory1#add_item "Clear Environment" ~callback:CicEnvironment.empty);
2743    ignore (factory1#add_item "Dump Environment" ~callback:dump_environment);
2744    ignore
2745     (factory1#add_item "Restore Environment" ~callback:restore_environment);
2746    ignore
2747     (save_set_sensitive := function b -> save_menu_item#misc#set_sensitive b);
2748    ignore (!save_set_sensitive false);
2749    ignore (qed_set_sensitive:=function b -> qed_menu_item#misc#set_sensitive b);
2750    ignore (!qed_set_sensitive false);
2751    ignore (factory1#add_separator ()) ;
2752    let export_to_postscript_menu_item =
2753     factory1#add_item "Export to PostScript..."
2754      ~callback:(export_to_postscript output) in
2755    ignore (factory1#add_separator ()) ;
2756    ignore
2757     (factory1#add_item "Exit" ~key:GdkKeysyms._Q ~callback:GMain.Main.quit) ;
2758    export_to_postscript_menu_item
2759   end in
2760  (* edit menu *)
2761  let edit_menu = factory0#add_submenu "Edit Current Proof" in
2762  let factory2 = new GMenu.factory edit_menu ~accel_group in
2763  let focus_and_proveit_set_sensitive = ref (function _ -> assert false) in
2764  let proveit_menu_item =
2765   factory2#add_item "Prove It" ~key:GdkKeysyms._I
2766    ~callback:(function () -> proveit ();!focus_and_proveit_set_sensitive false)
2767  in
2768  let focus_menu_item =
2769   factory2#add_item "Focus" ~key:GdkKeysyms._F
2770    ~callback:(function () -> focus () ; !focus_and_proveit_set_sensitive false)
2771  in
2772  let _ =
2773   focus_and_proveit_set_sensitive :=
2774    function b ->
2775     proveit_menu_item#misc#set_sensitive b ;
2776     focus_menu_item#misc#set_sensitive b
2777  in
2778  let _ = !focus_and_proveit_set_sensitive false in
2779  (* edit term menu *)
2780  let edit_term_menu = factory0#add_submenu "Edit Term" in
2781  let factory5 = new GMenu.factory edit_term_menu ~accel_group in
2782  let check_menu_item =
2783   factory5#add_item "Check Term" ~key:GdkKeysyms._C
2784    ~callback:(check scratch_window) in
2785  let _ = check_menu_item#misc#set_sensitive false in
2786  (* search menu *)
2787  let search_menu = factory0#add_submenu "Search" in
2788  let factory4 = new GMenu.factory search_menu ~accel_group in
2789  let _ =
2790   factory4#add_item "Locate..." ~key:GdkKeysyms._T
2791    ~callback:locate in
2792  let searchPattern_menu_item =
2793   factory4#add_item "SearchPattern..." ~key:GdkKeysyms._D
2794    ~callback:completeSearchPattern in
2795  let _ = searchPattern_menu_item#misc#set_sensitive false in
2796  let show_menu_item =
2797   factory4#add_item "Show..." ~key:GdkKeysyms._H ~callback:show
2798  in
2799  let insert_query_item =
2800   factory4#add_item "Insert Query (Experts Only)..." ~key:GdkKeysyms._Y
2801    ~callback:insertQuery in
2802  (* hbugs menu *)
2803  let hbugs_menu = factory0#add_submenu "HBugs" in
2804  let factory6 = new GMenu.factory hbugs_menu ~accel_group in
2805  let _ =
2806   factory6#add_check_item
2807     ~active:false ~key:GdkKeysyms._F5 ~callback:Hbugs.toggle "HBugs enabled"
2808  in
2809  let _ =
2810   factory6#add_item ~key:GdkKeysyms._Return ~callback:Hbugs.notify
2811    "(Re)Submit status!"
2812  in
2813  let _ = factory6#add_separator () in
2814  let _ =
2815   factory6#add_item ~callback:Hbugs.start_web_services "Start Web Services"
2816  in
2817  let _ =
2818   factory6#add_item ~callback:Hbugs.stop_web_services "Stop Web Services"
2819  in
2820  (* settings menu *)
2821  let settings_menu = factory0#add_submenu "Settings" in
2822  let factory3 = new GMenu.factory settings_menu ~accel_group in
2823  let _ =
2824   factory3#add_item "Edit Aliases..." ~key:GdkKeysyms._A
2825    ~callback:edit_aliases in
2826  let _ = factory3#add_separator () in
2827  let _ =
2828   factory3#add_item "MathML Widget Preferences..." ~key:GdkKeysyms._P
2829    ~callback:(function _ -> (settings_window ())#show ()) in
2830  let _ = factory3#add_separator () in
2831  let _ =
2832   factory3#add_item "Reload Stylesheets"
2833    ~callback:
2834      (function _ ->
2835        ApplyStylesheets.reload_stylesheets () ;
2836        if ProofEngine.get_proof () <> None then
2837         try
2838          refresh_goals notebook ;
2839          refresh_proof output
2840         with
2841            InvokeTactics.RefreshSequentException e ->
2842             output_html (outputhtml ())
2843              ("<h1 color=\"red\">An error occurred while refreshing the " ^
2844                "sequent: " ^ Printexc.to_string e ^ "</h1>") ;
2845            (*notebook#remove_all_pages ~skip_switch_page_event:false ;*)
2846            notebook#set_empty_page
2847          | InvokeTactics.RefreshProofException e ->
2848             output_html (outputhtml ())
2849              ("<h1 color=\"red\">An error occurred while refreshing the proof: "               ^ Printexc.to_string e ^ "</h1>") ;
2850             output#unload
2851      ) in
2852  (* accel group *)
2853  let _ = window#add_accel_group accel_group in
2854  (* end of menus *)
2855  let hbox0 =
2856   GPack.hbox
2857    ~packing:(vbox_for_menu#pack ~expand:true ~fill:true ~padding:5) () in
2858  let vbox =
2859   GPack.vbox ~packing:(hbox0#pack ~expand:true ~fill:true ~padding:5) () in
2860  let scrolled_window0 =
2861   GBin.scrolled_window ~border_width:10
2862    ~packing:(vbox#pack ~expand:true ~padding:5) () in
2863  let _ = scrolled_window0#add output#coerce in
2864  let frame =
2865   GBin.frame ~label:"Insert Term"
2866    ~packing:(vbox#pack ~expand:true ~fill:true ~padding:5) () in
2867  let scrolled_window1 =
2868   GBin.scrolled_window ~border_width:5
2869    ~packing:frame#add () in
2870  let inputt =
2871   TexTermEditor'.term_editor
2872    mqi_handle
2873    ~width:400 ~height:100 ~packing:scrolled_window1#add ()
2874    ~isnotempty_callback:
2875     (function b ->
2876       check_menu_item#misc#set_sensitive b ;
2877       searchPattern_menu_item#misc#set_sensitive b) in
2878  let vboxl =
2879   GPack.vbox ~packing:(hbox0#pack ~expand:true ~fill:true ~padding:5) () in
2880  let _ =
2881   vboxl#pack ~expand:true ~fill:true ~padding:5 notebook#notebook#coerce in
2882  let frame =
2883   GBin.frame ~shadow_type:`IN ~packing:(vboxl#pack ~expand:true ~padding:5) ()
2884  in
2885  let outputhtml =
2886   GHtml.xmhtml
2887    ~source:"<html><body bgColor=\"white\"></body></html>"
2888    ~width:400 ~height: 100
2889    ~border_width:20
2890    ~packing:frame#add
2891    ~show:true () in
2892 object
2893  method outputhtml = outputhtml
2894  method inputt = inputt
2895  method output = (output : TermViewer.proof_viewer)
2896  method scratch_window = scratch_window
2897  method notebook = notebook
2898  method show = window#show
2899  initializer
2900   notebook#set_empty_page ;
2901   export_to_postscript_menu_item#misc#set_sensitive false ;
2902   check_term := (check_term_in_scratch scratch_window) ;
2903
2904   (* signal handlers here *)
2905   ignore(output#connect#selection_changed
2906    (function elem ->
2907      choose_selection output elem ;
2908      !focus_and_proveit_set_sensitive true
2909    )) ;
2910   ignore (output#connect#click (show_in_show_window_callback output)) ;
2911   let settings_window = new settings_window output scrolled_window0
2912    export_to_postscript_menu_item (choose_selection output) in
2913   set_settings_window settings_window ;
2914   set_outputhtml outputhtml ;
2915   ignore(window#event#connect#delete (fun _ -> GMain.Main.quit () ; true )) ;
2916   Logger.log_callback :=
2917    (Logger.log_to_html ~print_and_flush:(output_html outputhtml))
2918 end;;
2919
2920 (* MAIN *)
2921
2922 let initialize_everything () =
2923  let module U = Unix in
2924   let output = TermViewer.proof_viewer ~width:350 ~height:280 () in
2925   let notebook = new notebook in
2926    let rendering_window' = new rendering_window output notebook in
2927     set_rendering_window rendering_window' ;
2928     let print_error_as_html prefix msg =
2929      output_html (outputhtml ())
2930       ("<h1 color=\"red\">" ^ prefix ^ msg ^ "</h1>")
2931     in
2932      Gdome_xslt.setErrorCallback (Some (print_error_as_html "XSLT Error: "));
2933      Gdome_xslt.setDebugCallback
2934       (Some (print_error_as_html "XSLT Debug Message: "));
2935      rendering_window'#show () ;
2936      if restore_environment_on_boot && Sys.file_exists environmentfile then
2937        restore_environment ();
2938      GtkThread.main ()
2939 ;;
2940
2941 let main () =
2942  ignore (GtkMain.Main.init ()) ;
2943  initialize_everything () ;
2944  MQIC.close mqi_handle;
2945  Hbugs.quit ()
2946 ;;
2947
2948 try
2949   Sys.catch_break true;
2950   main ();
2951 with Sys.Break -> ()  (* exit nicely, invoking at_exit functions *)
2952