]> matita.cs.unibo.it Git - helm.git/blob - helm/gTopLevel/hbugs.ml
b0ad35a7402b7e97a5388066526bdf6013f3148c
[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 ->
104          (* CSC: patch momentanea: la stringa deve essere nel formato *)
105          (* CSC: corretto (ovvero quotata se e' TeX ;-(((             *)
106          let term =
107           let term' = String.sub term 4 (String.length term - 4) in
108            "$" ^ Str.global_replace (Str.regexp "_") "\\_" term' ^ "$"
109          in
110           Tactics.apply ~term ()
111       | Hints _ -> assert false
112
113     let rec enable () =
114       (match !hbugs_client with
115       | None -> (* create an hbugs client and show its window *)
116           hbugs_client :=
117             (try
118               Some
119                 (new Hbugs_client.hbugsClient
120                   ~on_use_hint: use_hint
121                   ~on_exit: ignore  (* TODO disable hbugs on 'on_exit' callback *)
122                   ())
123             with e ->
124               prerr_endline (sprintf "Can't start HBugs client: %s"
125                 (Printexc.to_string e));
126               None);
127           (match !hbugs_client with
128           |Some client ->
129               client#show ();
130               client#subscribeAll ()
131           | None -> ())
132       | Some c -> (* show hbugs client window *)
133           c#show ())
134
135     let toggle state =
136       if state <> !hbugs_enabled then (* status has been changed *)
137         (if state then enable () else disable ());
138       hbugs_enabled := state
139
140   end
141