]> matita.cs.unibo.it Git - helm.git/blob - matita/matitaAutoGui.ml
tagged 0.5.0-rc1
[helm.git] / matita / matitaAutoGui.ml
1 (* Copyright (C) 2003, HELM Team.
2  * 
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 type status = 
28   Cic.context * (int * Cic.term * bool * int * (int * Cic.term) list) list * 
29   (int * Cic.term * int) list *
30   Cic.term list
31 let fake_goal = Cic.Implicit None;;
32 let fake_candidates = [];;
33
34 let start = ref 0;;
35 let len = ref 10;;
36
37 let pp c t =
38   let t = 
39     ProofEngineReduction.replace 
40       ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false) 
41       ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:t
42   in
43     ApplyTransformation.txt_of_cic_term ~map_unicode_to_tex:false
44       max_int [] c t
45 ;;
46 let pp_goal context x = 
47   if x == fake_goal then "" else pp context x
48 ;;
49 let pp_candidate context x = pp context x
50
51 let sublist start len l = 
52   let rec aux pos len = function
53     | [] when pos < start -> aux (pos+1) len []
54     | [] when len > 0 -> 
55           (0,fake_goal, false, 0, fake_candidates) :: aux (pos+1) (len-1) [] 
56     | [] (* when len <= 0 *) -> []
57     | he::tl when pos < start -> aux (pos+1) len tl
58     | he::tl when pos >= start && len > 0 -> he::aux (pos+1) (len-1) tl
59     | he::tl (* when pos >= start && len <= 0 *) -> []
60   in
61   aux 0 len l
62 ;;
63
64 let cell_of_candidate height context ?(active=false) g id c = 
65   let tooltip = GData.tooltips () in (* should be only one isn't it? *)
66   let button = 
67     GButton.button 
68       ~label:(pp_candidate context c(* ^ " - " ^ string_of_int id*))
69       () 
70   in
71   button#misc#set_size_request ~height ();
72   if active then
73     button#misc#set_sensitive false;
74   let text = 
75     "Follow computation of "^pp_candidate context c^" on "^pp_goal context g
76   in
77   tooltip#set_tip ~text (button :> GObj.widget);
78   ignore(button#connect#clicked 
79     (fun _ -> 
80       let menu = GMenu.menu () in
81       let follow = GMenu.menu_item ~label:"Follow" () in
82       let prune = GMenu.menu_item ~label:"Prune" () in
83       ignore (follow#connect#activate 
84         (fun _ -> HLog.warn (string_of_int id); Auto.give_hint id));
85       ignore (prune#connect#activate 
86         (fun _ -> HLog.warn (string_of_int id); Auto.give_prune_hint id));
87       menu#append follow;
88       menu#append prune;
89       menu#popup 0 (GtkMain.Main.get_current_event_time ())));
90   button
91 ;;
92 let cell_of_goal height win_width context goal = 
93   GMisc.label 
94     ~markup:(pp_goal context goal) ~xalign:0.0 
95     ~width:(min (win_width * 60 / 100) 500) 
96     ~line_wrap:false
97     ~ellipsize:`END
98     ~height
99     ()
100 ;;
101 let cell_of_row_header height n k id = 
102   GMisc.label
103     ~markup:("<span stretch=\"ultracondensed\">" ^ string_of_int n ^ "<sub>(" ^
104              string_of_int id ^ "|" ^ string_of_int k ^ ")</sub></span>") 
105     ~line_wrap:true ~justify:`LEFT ~height ~width:80 ()
106 ;;
107 let cell_of_candidates height grey context goal cads = 
108   let hbox = GPack.hbox () in
109   match cads with
110   | [] -> hbox
111   | (id,c)::tl ->
112       hbox#pack 
113         (cell_of_candidate height ~active:grey 
114           context goal id c :> GObj.widget);
115       List.iter
116         (fun (id,c) -> 
117         hbox#pack (cell_of_candidate height context goal id c :> GObj.widget)) tl;
118         hbox
119 ;;
120
121 let elems_to_rows height context win_width (table : GPack.table) (or_list) =
122   let height = height / ((List.length or_list) + 1) in
123   let _ = 
124    List.fold_left 
125     (fun position (goal_id, goal, grey, depth, candidates) ->
126       table#attach ~left:0 ~top:position 
127         (cell_of_row_header height (position + !start) 
128           depth goal_id :> GObj.widget);
129       table#attach ~left:1 ~top:position ~fill:`BOTH
130         (cell_of_goal height win_width context goal :> GObj.widget);
131       table#attach ~left:2 ~top:position 
132         (cell_of_candidates height grey context goal candidates :> GObj.widget);
133       position+1)
134     0 or_list
135   in 
136   ()
137 ;;
138
139 let old_displayed_status = ref [];;
140 let old_size = ref 0;;
141
142 let fill_win (win : MatitaGeneratedGui.autoWin) cb =
143   let init = Unix.gettimeofday () in
144     try 
145       let (status : status) = cb () in
146       let win_size = win#toplevel#misc#allocation.Gtk.width in
147       let ctx, or_list, and_list, last_moves = status in
148       let displayed_status = 
149         sublist !start !len 
150           (or_list @ (List.map (fun (id,x,d) -> id,x,false,d,[]) and_list)) 
151       in
152       if displayed_status <> !old_displayed_status || !old_size <> win_size then
153         begin
154           old_displayed_status := displayed_status;
155           old_size := win_size;
156           win#labelLAST#set_text 
157             (String.concat ";" (List.map (pp_candidate ctx) last_moves));
158           List.iter win#table#remove win#table#children;
159           let height = win#viewportAREA#misc#allocation.Gtk.height in
160           elems_to_rows height ctx win_size win#table displayed_status;
161           Printf.eprintf 
162             "REDRAW COST: %2.1f\n%!" (Unix.gettimeofday () -.  init);
163         end
164     with exn -> prerr_endline (Printexc.to_string exn); ()
165 ;;
166
167 let auto_dialog elems =
168   let win = new MatitaGeneratedGui.autoWin () in
169   let width = Gdk.Screen.width () in
170   let height = Gdk.Screen.height () in
171   let main_w = width * 70 / 100 in 
172   let main_h = height * 60 / 100 in
173   win#toplevel#resize ~width:main_w ~height:main_h;
174   win#check_widgets ();
175   win#table#set_columns 3;
176   win#table#set_col_spacings 6;
177   win#table#set_row_spacings 4;
178   ignore(win#buttonUP#connect#clicked 
179     (fun _ -> start := max 0 (!start -1); fill_win win elems));
180   ignore(win#buttonDOWN#connect#clicked 
181     (fun _ -> incr start; fill_win win elems));
182   ignore(win#buttonCLOSE#connect#clicked 
183     (fun _ -> 
184       Auto.pause false;
185       Auto.step ();
186       win#toplevel#destroy ();
187       GMain.Main.quit ()));
188   ignore(win#toplevel#event#connect#delete
189     (fun _ -> 
190       Auto.pause false;
191       Auto.step ();
192       win#toplevel#destroy ();
193       GMain.Main.quit ();true));
194   let redraw_callback _ = fill_win win elems;true in
195   let redraw = GMain.Timeout.add ~ms:400 ~callback:redraw_callback in
196   ignore(win#buttonPAUSE#connect#clicked 
197     (fun _ -> 
198       Auto.pause true;
199       win#buttonPLAY#misc#set_sensitive true;
200       win#buttonPAUSE#misc#set_sensitive false;));
201   ignore(win#buttonPLAY#connect#clicked 
202     (fun _ -> 
203       Auto.pause false;
204       Auto.step ();
205       win#buttonPLAY#misc#set_sensitive false;
206       win#buttonPAUSE#misc#set_sensitive true;));
207   ignore(win#buttonNEXT#connect#clicked 
208     (fun _ -> Auto.step ()));
209   Auto.pause true;
210   win#buttonPLAY#misc#set_sensitive true;
211   win#buttonPAUSE#misc#set_sensitive false;  
212   fill_win win elems;
213   win#toplevel#show ();
214   GtkThread.main ();
215   GMain.Timeout.remove redraw;
216 ;;
217