]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/hbugs.ml
added HBugs interface module for gTopLevel
[helm.git] / helm / gTopLevel / hbugs.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Hbugs_types;;
30 open Printf;;
31
32 let debug_print = 
33   let debug = true in
34   fun s -> prerr_endline (sprintf "DEBUG: %s" s)
35 ;;
36
37 exception NoProofInProgress;;
38
39 module type HbugsActions =
40   sig
41     val enable: unit -> unit
42     val disable: unit -> unit
43     val toggle: bool -> unit
44
45     val quit: unit -> unit
46
47     val notify: unit -> unit
48   end
49
50 module Make (Tactics: InvokeTactics.Tactics) : HbugsActions =
51   struct
52     let hbugs_client = ref None
53
54     let quit () =
55       match !hbugs_client with
56       | Some c -> c#unregisterFromBroker ()
57       | None -> ()
58
59     let hbugs_enabled = ref false
60
61     let get_hbugs_client () =
62       match !hbugs_client with
63       | Some c -> c
64       | None -> assert false
65
66     let disable () =
67       match !hbugs_client with None -> () | Some c -> c#hide ()
68
69       (** send current proof assistant state to hbugs broker *)
70     let notify () =
71       try
72         if !hbugs_enabled then begin
73           let client = get_hbugs_client () in
74           let goal =
75             match !ProofEngine.goal with
76             | Some g -> g
77             | None -> raise NoProofInProgress
78           in
79           let (type_string, body_string) =
80             let (type_xml, body_xml) = ProofEngine.get_current_status_as_xml () in
81             (Xml.pp_to_string type_xml, Xml.pp_to_string body_xml)
82           in
83           let new_state =
84             (Misc.strip_xml_headings type_string,
85              Misc.strip_xml_headings body_string,
86              goal)
87           in
88           client#stateChange new_state
89         end
90       with NoProofInProgress -> ()
91
92     let use_hint = function
93       | Use_ring_Luke -> Tactics.ring ()
94       | Use_fourier_Luke -> Tactics.fourier ()
95       | Use_reflexivity_Luke -> Tactics.reflexivity ()
96       | Use_symmetry_Luke -> Tactics.symmetry ()
97       | Use_assumption_Luke -> Tactics.assumption ()
98       | Use_contradiction_Luke -> Tactics.contradiction ()
99       | Use_exists_Luke -> Tactics.exists ()
100       | Use_split_Luke -> Tactics.split ()
101       | Use_left_Luke -> Tactics.left ()
102       | Use_right_Luke -> Tactics.right ()
103       | Use_apply_Luke term -> Tactics.apply ~term ()
104       | Hints _ -> assert false
105
106     let rec enable () =
107       (match !hbugs_client with
108       | None -> (* create an hbugs client and show its window *)
109           hbugs_client :=
110             (try
111               Some
112                 (new Hbugs_client.hbugsClient
113                   ~on_use_hint: use_hint
114                   ~on_exit: ignore  (* TODO disable hbugs on 'on_exit' callback *)
115                   ())
116             with e ->
117               prerr_endline (sprintf "Can't start HBugs client: %s"
118                 (Printexc.to_string e));
119               None);
120           (match !hbugs_client with
121           |Some client ->
122               client#show ();
123               client#subscribeAll ()
124           | None -> ())
125       | Some c -> (* show hbugs client window *)
126           c#show ())
127
128     let toggle state =
129       if state <> !hbugs_enabled then (* status has been changed *)
130         (if state then enable () else disable ());
131       hbugs_enabled := state
132
133   end
134