author | blanchet |
Sat, 15 Dec 2012 19:57:12 +0100 | |
changeset 50557 | 31313171deb5 |
parent 50278 | 05f8ec128e83 |
child 50672 | ab5b8b5c9cbe |
permissions | -rw-r--r-- |
50263 | 1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_shrink.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Author: Steffen Juilf Smolka, TU Muenchen |
|
4 |
||
50265 | 5 |
Shrinking and preplaying of reconstructed isar proofs. |
50263 | 6 |
*) |
7 |
||
50259 | 8 |
signature SLEDGEHAMMER_SHRINK = |
9 |
sig |
|
50264
a9ec48b98734
renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents:
50263
diff
changeset
|
10 |
type isar_step = Sledgehammer_Proof.isar_step |
50259 | 11 |
val shrink_proof : |
50557 | 12 |
bool -> Proof.context -> string -> string -> bool -> Time.time option |
13 |
-> real -> isar_step list -> isar_step list * (bool * (bool * Time.time)) |
|
50259 | 14 |
end |
15 |
||
50269 | 16 |
structure Sledgehammer_Shrink : SLEDGEHAMMER_SHRINK = |
50259 | 17 |
struct |
18 |
||
50265 | 19 |
open Sledgehammer_Util |
50264
a9ec48b98734
renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents:
50263
diff
changeset
|
20 |
open Sledgehammer_Proof |
50259 | 21 |
|
22 |
(* Parameters *) |
|
23 |
val merge_timeout_slack = 1.2 |
|
24 |
||
25 |
(* Data structures, orders *) |
|
26 |
val label_ord = prod_ord int_ord fast_string_ord o pairself swap |
|
27 |
structure Label_Table = Table( |
|
28 |
type key = label |
|
29 |
val ord = label_ord) |
|
30 |
||
31 |
(* clean vector interface *) |
|
32 |
fun get i v = Vector.sub (v, i) |
|
33 |
fun replace x i v = Vector.update (v, i, x) |
|
34 |
fun update f i v = replace (get i v |> f) i v |
|
50273 | 35 |
fun v_map_index f v = Vector.foldr (op::) nil v |> map_index f |> Vector.fromList |
50259 | 36 |
fun v_fold_index f v s = |
37 |
Vector.foldl (fn (x, (i, s)) => (i+1, f (i, x) s)) (0, s) v |> snd |
|
38 |
||
39 |
(* Queue interface to table *) |
|
40 |
fun pop tab key = |
|
41 |
let val v = hd (Inttab.lookup_list tab key) in |
|
42 |
(v, Inttab.remove_list (op =) (key, v) tab) |
|
43 |
end |
|
44 |
fun pop_max tab = pop tab (the (Inttab.max_key tab)) |
|
45 |
fun add_list tab xs = fold (Inttab.insert_list (op =)) xs tab |
|
46 |
||
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
47 |
(* Timing *) |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
48 |
fun ext_time_add (b1, t1) (b2, t2) = (b1 orelse b2, Time.+(t1,t2)) |
50557 | 49 |
val no_time = (false, Time.zeroTime) |
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
50 |
fun take_time timeout tac arg = |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
51 |
let val timing = Timing.start () in |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
52 |
(TimeLimit.timeLimit timeout tac arg; |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
53 |
Timing.result timing |> #cpu |> SOME) |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
54 |
handle TimeLimit.TimeOut => NONE |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
55 |
end |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
56 |
|
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
57 |
|
50259 | 58 |
(* Main function for shrinking proofs *) |
59 |
fun shrink_proof debug ctxt type_enc lam_trans preplay preplay_timeout |
|
60 |
isar_shrink proof = |
|
61 |
let |
|
50557 | 62 |
(* 60 seconds seems like a good interpreation of "no timeout" *) |
63 |
val preplay_timeout = preplay_timeout |> the_default (seconds 60.0) |
|
64 |
||
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
65 |
(* handle metis preplay fail *) |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
66 |
local |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
67 |
open Unsynchronized |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
68 |
val metis_fail = ref false |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
69 |
in |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
70 |
fun handle_metis_fail try_metis () = |
50557 | 71 |
try_metis () handle _ => (metis_fail := true; SOME Time.zeroTime) |
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
72 |
fun get_time lazy_time = |
50557 | 73 |
if !metis_fail then SOME Time.zeroTime else Lazy.force lazy_time |
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
74 |
val metis_fail = fn () => !metis_fail |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
75 |
end |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
76 |
|
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
77 |
(* Shrink top level proof - do not shrink case splits *) |
50269 | 78 |
fun shrink_top_level on_top_level ctxt proof = |
50259 | 79 |
let |
80 |
(* proof vector *) |
|
81 |
val proof_vect = proof |> map SOME |> Vector.fromList |
|
50260 | 82 |
val n = Vector.length proof_vect |
83 |
val n_metis = metis_steps_top_level proof |
|
84 |
val target_n_metis = Real.fromInt n_metis / isar_shrink |> Real.round |
|
50259 | 85 |
|
86 |
(* table for mapping from (top-level-)label to proof position *) |
|
87 |
fun update_table (i, Assume (label, _)) = |
|
88 |
Label_Table.update_new (label, i) |
|
89 |
| update_table (i, Prove (_, label, _, _)) = |
|
90 |
Label_Table.update_new (label, i) |
|
91 |
| update_table _ = I |
|
92 |
val label_index_table = fold_index update_table proof Label_Table.empty |
|
93 |
||
94 |
(* proof references *) |
|
95 |
fun refs (Prove (_, _, _, By_Metis (lfs, _))) = |
|
50269 | 96 |
map_filter (Label_Table.lookup label_index_table) lfs |
50259 | 97 |
| refs (Prove (_, _, _, Case_Split (cases, (lfs, _)))) = |
50269 | 98 |
map_filter (Label_Table.lookup label_index_table) lfs |
50259 | 99 |
@ maps (maps refs) cases |
100 |
| refs _ = [] |
|
101 |
val refed_by_vect = |
|
50260 | 102 |
Vector.tabulate (n, (fn _ => [])) |
50259 | 103 |
|> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) proof |
104 |
|> Vector.map rev (* after rev, indices are sorted in ascending order *) |
|
105 |
||
106 |
(* candidates for elimination, use table as priority queue (greedy |
|
107 |
algorithm) *) |
|
108 |
fun add_if_cand proof_vect (i, [j]) = |
|
109 |
(case (the (get i proof_vect), the (get j proof_vect)) of |
|
110 |
(Prove (_, _, t, By_Metis _), Prove (_, _, _, By_Metis _)) => |
|
111 |
cons (Term.size_of_term t, i) |
|
112 |
| _ => I) |
|
113 |
| add_if_cand _ _ = I |
|
114 |
val cand_tab = |
|
115 |
v_fold_index (add_if_cand proof_vect) refed_by_vect [] |
|
116 |
|> Inttab.make_list |
|
117 |
||
118 |
(* Metis Preplaying *) |
|
50273 | 119 |
fun resolve_fact_names names = |
120 |
names |
|
121 |
|>> map string_for_label |
|
122 |
|> op @ |
|
123 |
|> maps (thms_of_name ctxt) |
|
50275 | 124 |
|
50273 | 125 |
fun try_metis timeout (succedent, Prove (_, _, t, byline)) = |
50557 | 126 |
if not preplay then (fn () => SOME Time.zeroTime) else |
50273 | 127 |
(case byline of |
128 |
By_Metis fact_names => |
|
129 |
let |
|
130 |
val facts = resolve_fact_names fact_names |
|
131 |
val goal = |
|
132 |
Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t |
|
133 |
fun tac {context = ctxt, prems = _} = |
|
134 |
Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1 |
|
135 |
in |
|
136 |
take_time timeout (fn () => goal tac) |
|
137 |
end |
|
138 |
| Case_Split (cases, fact_names) => |
|
139 |
let |
|
50275 | 140 |
val facts = |
141 |
resolve_fact_names fact_names |
|
142 |
@ (case the succedent of |
|
143 |
Prove (_, _, t, _) => |
|
144 |
Skip_Proof.make_thm (Proof_Context.theory_of ctxt) t |
|
145 |
| Assume (_, t) => |
|
146 |
Skip_Proof.make_thm (Proof_Context.theory_of ctxt) t |
|
147 |
| _ => error "Internal error: unexpected succedent of case split") |
|
50278 | 148 |
:: map |
149 |
(hd #> (fn Assume (_, a) => Logic.mk_implies(a, t) |
|
150 |
| _ => error "Internal error: malformed case split") |
|
151 |
#> Skip_Proof.make_thm (Proof_Context.theory_of ctxt)) |
|
50273 | 152 |
cases |
153 |
val goal = |
|
50275 | 154 |
Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t |
50273 | 155 |
fun tac {context = ctxt, prems = _} = |
156 |
Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1 |
|
157 |
in |
|
158 |
take_time timeout (fn () => goal tac) |
|
159 |
end) |
|
50557 | 160 |
| try_metis _ _ = (fn () => SOME Time.zeroTime) |
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
161 |
|
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
162 |
val try_metis_quietly = the_default NONE oo try oo try_metis |
50259 | 163 |
|
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
164 |
(* cache metis preplay times in lazy time vector *) |
50259 | 165 |
val metis_time = |
50274
2f6035e858b6
fixed preplaying of case splits; incorperated new name of structure: Isabelle_Markup -> Markup
smolkas
parents:
50273
diff
changeset
|
166 |
v_map_index |
50273 | 167 |
(Lazy.lazy o handle_metis_fail o try_metis preplay_timeout |
50275 | 168 |
o apfst (fn i => try (the o get (i-1)) proof_vect) o apsnd the) |
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
169 |
proof_vect |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
170 |
fun sum_up_time lazy_time_vector = |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
171 |
Vector.foldl |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
172 |
((fn (SOME t, (b, ts)) => (b, Time.+(t, ts)) |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
173 |
| (NONE, (_, ts)) => (true, Time.+(ts, preplay_timeout))) |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
174 |
o apfst get_time) |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
175 |
no_time lazy_time_vector |
50259 | 176 |
|
177 |
(* Merging *) |
|
50260 | 178 |
fun merge (Prove (_, label1, _, By_Metis (lfs1, gfs1))) |
50259 | 179 |
(Prove (qs2, label2 , t, By_Metis (lfs2, gfs2))) = |
180 |
let |
|
50269 | 181 |
val lfs = remove (op =) label1 lfs2 |> union (op =) lfs1 |
182 |
val gfs = union (op =) gfs1 gfs2 |
|
183 |
in Prove (qs2, label2, t, By_Metis (lfs, gfs)) end |
|
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
184 |
| merge _ _ = error "Internal error: Tring to merge unmergable isar steps" |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
185 |
|
50259 | 186 |
fun try_merge metis_time (s1, i) (s2, j) = |
187 |
(case get i metis_time |> Lazy.force of |
|
188 |
NONE => (NONE, metis_time) |
|
189 |
| SOME t1 => |
|
190 |
(case get j metis_time |> Lazy.force of |
|
191 |
NONE => (NONE, metis_time) |
|
192 |
| SOME t2 => |
|
193 |
let |
|
194 |
val s12 = merge s1 s2 |
|
50270 | 195 |
val timeout = time_mult merge_timeout_slack (Time.+(t1, t2)) |
50259 | 196 |
in |
50274
2f6035e858b6
fixed preplaying of case splits; incorperated new name of structure: Isabelle_Markup -> Markup
smolkas
parents:
50273
diff
changeset
|
197 |
case try_metis_quietly timeout (NONE, s12) () of |
50259 | 198 |
NONE => (NONE, metis_time) |
199 |
| some_t12 => |
|
200 |
(SOME s12, metis_time |
|
50557 | 201 |
|> replace (Time.zeroTime |> SOME |> Lazy.value) i |
50259 | 202 |
|> replace (Lazy.value some_t12) j) |
203 |
||
204 |
end)) |
|
205 |
||
50260 | 206 |
fun merge_steps metis_time proof_vect refed_by cand_tab n' n_metis' = |
50259 | 207 |
if Inttab.is_empty cand_tab |
50260 | 208 |
orelse n_metis' <= target_n_metis |
50269 | 209 |
orelse (on_top_level andalso n'<3) |
50259 | 210 |
then |
211 |
(Vector.foldr |
|
212 |
(fn (NONE, proof) => proof | (SOME s, proof) => s :: proof) |
|
213 |
[] proof_vect, |
|
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
214 |
sum_up_time metis_time) |
50259 | 215 |
else |
216 |
let |
|
217 |
val (i, cand_tab) = pop_max cand_tab |
|
218 |
val j = get i refed_by |> the_single |
|
219 |
val s1 = get i proof_vect |> the |
|
220 |
val s2 = get j proof_vect |> the |
|
221 |
in |
|
222 |
case try_merge metis_time (s1, i) (s2, j) of |
|
223 |
(NONE, metis_time) => |
|
50260 | 224 |
merge_steps metis_time proof_vect refed_by cand_tab n' n_metis' |
50259 | 225 |
| (s, metis_time) => |
226 |
let |
|
227 |
val refs = refs s1 |
|
228 |
val refed_by = refed_by |> fold |
|
229 |
(update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j)) refs |
|
230 |
val new_candidates = |
|
231 |
fold (add_if_cand proof_vect) |
|
232 |
(map (fn i => (i, get i refed_by)) refs) [] |
|
233 |
val cand_tab = add_list cand_tab new_candidates |
|
234 |
val proof_vect = proof_vect |> replace NONE i |> replace s j |
|
235 |
in |
|
50260 | 236 |
merge_steps metis_time proof_vect refed_by cand_tab (n' - 1) (n_metis' - 1) |
50259 | 237 |
end |
238 |
end |
|
239 |
in |
|
50260 | 240 |
merge_steps metis_time proof_vect refed_by_vect cand_tab n n_metis |
50259 | 241 |
end |
242 |
||
50269 | 243 |
fun shrink_proof' on_top_level ctxt proof = |
50259 | 244 |
let |
245 |
(* Enrich context with top-level facts *) |
|
246 |
val thy = Proof_Context.theory_of ctxt |
|
247 |
fun enrich_ctxt (Assume (label, t)) ctxt = |
|
248 |
Proof_Context.put_thms false |
|
249 |
(string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt |
|
250 |
| enrich_ctxt (Prove (_, label, t, _)) ctxt = |
|
251 |
Proof_Context.put_thms false |
|
252 |
(string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt |
|
253 |
| enrich_ctxt _ ctxt = ctxt |
|
254 |
val rich_ctxt = fold enrich_ctxt proof ctxt |
|
255 |
||
256 |
(* Shrink case_splits and top-levl *) |
|
257 |
val ((proof, top_level_time), lower_level_time) = |
|
258 |
proof |> shrink_case_splits rich_ctxt |
|
50269 | 259 |
|>> shrink_top_level on_top_level rich_ctxt |
50259 | 260 |
in |
261 |
(proof, ext_time_add lower_level_time top_level_time) |
|
262 |
end |
|
263 |
||
264 |
and shrink_case_splits ctxt proof = |
|
265 |
let |
|
50269 | 266 |
fun shrink_each_and_collect_time shrink candidates = |
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
267 |
let fun f_m cand time = shrink cand ||> ext_time_add time |
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
268 |
in fold_map f_m candidates no_time end |
50269 | 269 |
val shrink_case_split = shrink_each_and_collect_time (shrink_proof' false ctxt) |
50259 | 270 |
fun shrink (Prove (qs, lbl, t, Case_Split (cases, facts))) = |
271 |
let val (cases, time) = shrink_case_split cases |
|
272 |
in (Prove (qs, lbl, t, Case_Split (cases, facts)), time) end |
|
273 |
| shrink step = (step, no_time) |
|
274 |
in |
|
50269 | 275 |
shrink_each_and_collect_time shrink proof |
50259 | 276 |
end |
277 |
in |
|
278 |
shrink_proof' true ctxt proof |
|
50271
2be84eaf7ebb
deal with the case that metis does not time out, but fails instead
smolkas
parents:
50270
diff
changeset
|
279 |
|> apsnd (pair (metis_fail () ) ) |
50259 | 280 |
end |
281 |
||
282 |
end |