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