author | smolkas |
Wed, 28 Nov 2012 12:25:43 +0100 | |
changeset 50270 | 64d5767ea9b3 |
parent 50269 | 20a01c3e8072 |
child 50271 | 2be84eaf7ebb |
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 : |
12 |
bool -> Proof.context -> string -> string -> bool -> Time.time -> real |
|
13 |
-> isar_step list -> isar_step list * (bool * Time.time) |
|
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 |
(* Timing *) |
|
50270 | 32 |
fun ext_time_add (b1, t1) (b2, t2) = (b1 orelse b2, Time.+(t1,t2)) |
50259 | 33 |
val no_time = (false, seconds 0.0) |
34 |
fun take_time timeout tac arg = |
|
35 |
let val timing = Timing.start () in |
|
36 |
(TimeLimit.timeLimit timeout tac arg; |
|
37 |
Timing.result timing |> #cpu |> SOME) |
|
38 |
handle _ => NONE |
|
39 |
end |
|
50269 | 40 |
fun sum_up_time timeout lazy_time_vector = |
50259 | 41 |
Vector.foldl |
50270 | 42 |
((fn (SOME t, (b, ts)) => (b, Time.+(t, ts)) |
43 |
| (NONE, (_, ts)) => (true, Time.+(ts, timeout))) o apfst Lazy.force) |
|
50269 | 44 |
no_time lazy_time_vector |
50259 | 45 |
|
46 |
(* clean vector interface *) |
|
47 |
fun get i v = Vector.sub (v, i) |
|
48 |
fun replace x i v = Vector.update (v, i, x) |
|
49 |
fun update f i v = replace (get i v |> f) i v |
|
50 |
fun v_fold_index f v s = |
|
51 |
Vector.foldl (fn (x, (i, s)) => (i+1, f (i, x) s)) (0, s) v |> snd |
|
52 |
||
53 |
(* Queue interface to table *) |
|
54 |
fun pop tab key = |
|
55 |
let val v = hd (Inttab.lookup_list tab key) in |
|
56 |
(v, Inttab.remove_list (op =) (key, v) tab) |
|
57 |
end |
|
58 |
fun pop_max tab = pop tab (the (Inttab.max_key tab)) |
|
59 |
fun add_list tab xs = fold (Inttab.insert_list (op =)) xs tab |
|
60 |
||
61 |
(* Main function for shrinking proofs *) |
|
62 |
fun shrink_proof debug ctxt type_enc lam_trans preplay preplay_timeout |
|
63 |
isar_shrink proof = |
|
64 |
let |
|
50269 | 65 |
fun shrink_top_level on_top_level ctxt proof = |
50259 | 66 |
let |
67 |
(* proof vector *) |
|
68 |
val proof_vect = proof |> map SOME |> Vector.fromList |
|
50260 | 69 |
val n = Vector.length proof_vect |
70 |
val n_metis = metis_steps_top_level proof |
|
71 |
val target_n_metis = Real.fromInt n_metis / isar_shrink |> Real.round |
|
50259 | 72 |
|
73 |
(* table for mapping from (top-level-)label to proof position *) |
|
74 |
fun update_table (i, Assume (label, _)) = |
|
75 |
Label_Table.update_new (label, i) |
|
76 |
| update_table (i, Prove (_, label, _, _)) = |
|
77 |
Label_Table.update_new (label, i) |
|
78 |
| update_table _ = I |
|
79 |
val label_index_table = fold_index update_table proof Label_Table.empty |
|
80 |
||
81 |
(* proof references *) |
|
82 |
fun refs (Prove (_, _, _, By_Metis (lfs, _))) = |
|
50269 | 83 |
map_filter (Label_Table.lookup label_index_table) lfs |
50259 | 84 |
| refs (Prove (_, _, _, Case_Split (cases, (lfs, _)))) = |
50269 | 85 |
map_filter (Label_Table.lookup label_index_table) lfs |
50259 | 86 |
@ maps (maps refs) cases |
87 |
| refs _ = [] |
|
88 |
val refed_by_vect = |
|
50260 | 89 |
Vector.tabulate (n, (fn _ => [])) |
50259 | 90 |
|> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) proof |
91 |
|> Vector.map rev (* after rev, indices are sorted in ascending order *) |
|
92 |
||
93 |
(* candidates for elimination, use table as priority queue (greedy |
|
94 |
algorithm) *) |
|
95 |
fun add_if_cand proof_vect (i, [j]) = |
|
96 |
(case (the (get i proof_vect), the (get j proof_vect)) of |
|
97 |
(Prove (_, _, t, By_Metis _), Prove (_, _, _, By_Metis _)) => |
|
98 |
cons (Term.size_of_term t, i) |
|
99 |
| _ => I) |
|
100 |
| add_if_cand _ _ = I |
|
101 |
val cand_tab = |
|
102 |
v_fold_index (add_if_cand proof_vect) refed_by_vect [] |
|
103 |
|> Inttab.make_list |
|
104 |
||
105 |
(* Metis Preplaying *) |
|
106 |
fun try_metis timeout (Prove (_, _, t, By_Metis fact_names)) = |
|
107 |
if not preplay then (fn () => SOME (seconds 0.0)) else |
|
108 |
let |
|
109 |
val facts = |
|
110 |
fact_names |
|
111 |
|>> map string_for_label |
|
112 |
|> op @ |
|
50267
1da2e67242d6
moved thms_of_name to Sledgehammer_Util and removed copies, updated references
smolkas
parents:
50265
diff
changeset
|
113 |
|> maps (thms_of_name ctxt) |
50259 | 114 |
val goal = |
115 |
Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t |
|
116 |
fun tac {context = ctxt, prems = _} = |
|
117 |
Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1 |
|
118 |
in |
|
119 |
take_time timeout (fn () => goal tac) |
|
120 |
end |
|
50269 | 121 |
(* FIXME: Add case_split preplaying *) |
50259 | 122 |
| try_metis _ _ = (fn () => SOME (seconds 0.0) ) |
123 |
||
50269 | 124 |
(* Lazy metis time vector = cache *) |
50259 | 125 |
val metis_time = |
126 |
Vector.map (Lazy.lazy o try_metis preplay_timeout o the) proof_vect |
|
127 |
||
128 |
(* Merging *) |
|
50260 | 129 |
fun merge (Prove (_, label1, _, By_Metis (lfs1, gfs1))) |
50259 | 130 |
(Prove (qs2, label2 , t, By_Metis (lfs2, gfs2))) = |
131 |
let |
|
50269 | 132 |
val lfs = remove (op =) label1 lfs2 |> union (op =) lfs1 |
133 |
val gfs = union (op =) gfs1 gfs2 |
|
134 |
in Prove (qs2, label2, t, By_Metis (lfs, gfs)) end |
|
50259 | 135 |
fun try_merge metis_time (s1, i) (s2, j) = |
136 |
(case get i metis_time |> Lazy.force of |
|
137 |
NONE => (NONE, metis_time) |
|
138 |
| SOME t1 => |
|
139 |
(case get j metis_time |> Lazy.force of |
|
140 |
NONE => (NONE, metis_time) |
|
141 |
| SOME t2 => |
|
142 |
let |
|
143 |
val s12 = merge s1 s2 |
|
50270 | 144 |
val timeout = time_mult merge_timeout_slack (Time.+(t1, t2)) |
50259 | 145 |
in |
146 |
case try_metis timeout s12 () of |
|
147 |
NONE => (NONE, metis_time) |
|
148 |
| some_t12 => |
|
149 |
(SOME s12, metis_time |
|
150 |
|> replace (seconds 0.0 |> SOME |> Lazy.value) i |
|
151 |
|> replace (Lazy.value some_t12) j) |
|
152 |
||
153 |
end)) |
|
154 |
||
50260 | 155 |
fun merge_steps metis_time proof_vect refed_by cand_tab n' n_metis' = |
50259 | 156 |
if Inttab.is_empty cand_tab |
50260 | 157 |
orelse n_metis' <= target_n_metis |
50269 | 158 |
orelse (on_top_level andalso n'<3) |
50259 | 159 |
then |
160 |
(Vector.foldr |
|
161 |
(fn (NONE, proof) => proof | (SOME s, proof) => s :: proof) |
|
162 |
[] proof_vect, |
|
163 |
sum_up_time preplay_timeout metis_time) |
|
164 |
else |
|
165 |
let |
|
166 |
val (i, cand_tab) = pop_max cand_tab |
|
167 |
val j = get i refed_by |> the_single |
|
168 |
val s1 = get i proof_vect |> the |
|
169 |
val s2 = get j proof_vect |> the |
|
170 |
in |
|
171 |
case try_merge metis_time (s1, i) (s2, j) of |
|
172 |
(NONE, metis_time) => |
|
50260 | 173 |
merge_steps metis_time proof_vect refed_by cand_tab n' n_metis' |
50259 | 174 |
| (s, metis_time) => |
175 |
let |
|
176 |
val refs = refs s1 |
|
177 |
val refed_by = refed_by |> fold |
|
178 |
(update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j)) refs |
|
179 |
val new_candidates = |
|
180 |
fold (add_if_cand proof_vect) |
|
181 |
(map (fn i => (i, get i refed_by)) refs) [] |
|
182 |
val cand_tab = add_list cand_tab new_candidates |
|
183 |
val proof_vect = proof_vect |> replace NONE i |> replace s j |
|
184 |
in |
|
50260 | 185 |
merge_steps metis_time proof_vect refed_by cand_tab (n' - 1) (n_metis' - 1) |
50259 | 186 |
end |
187 |
end |
|
188 |
in |
|
50260 | 189 |
merge_steps metis_time proof_vect refed_by_vect cand_tab n n_metis |
50259 | 190 |
end |
191 |
||
50269 | 192 |
fun shrink_proof' on_top_level ctxt proof = |
50259 | 193 |
let |
194 |
(* Enrich context with top-level facts *) |
|
195 |
val thy = Proof_Context.theory_of ctxt |
|
196 |
fun enrich_ctxt (Assume (label, t)) ctxt = |
|
197 |
Proof_Context.put_thms false |
|
198 |
(string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt |
|
199 |
| enrich_ctxt (Prove (_, label, t, _)) ctxt = |
|
200 |
Proof_Context.put_thms false |
|
201 |
(string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt |
|
202 |
| enrich_ctxt _ ctxt = ctxt |
|
203 |
val rich_ctxt = fold enrich_ctxt proof ctxt |
|
204 |
||
205 |
(* Shrink case_splits and top-levl *) |
|
206 |
val ((proof, top_level_time), lower_level_time) = |
|
207 |
proof |> shrink_case_splits rich_ctxt |
|
50269 | 208 |
|>> shrink_top_level on_top_level rich_ctxt |
50259 | 209 |
in |
210 |
(proof, ext_time_add lower_level_time top_level_time) |
|
211 |
end |
|
212 |
||
213 |
and shrink_case_splits ctxt proof = |
|
214 |
let |
|
50269 | 215 |
fun shrink_each_and_collect_time shrink candidates = |
50259 | 216 |
let fun f_m cand time = shrink cand ||> ext_time_add time |
217 |
in fold_map f_m candidates no_time end |
|
50269 | 218 |
val shrink_case_split = shrink_each_and_collect_time (shrink_proof' false ctxt) |
50259 | 219 |
fun shrink (Prove (qs, lbl, t, Case_Split (cases, facts))) = |
220 |
let val (cases, time) = shrink_case_split cases |
|
221 |
in (Prove (qs, lbl, t, Case_Split (cases, facts)), time) end |
|
222 |
| shrink step = (step, no_time) |
|
223 |
in |
|
50269 | 224 |
shrink_each_and_collect_time shrink proof |
50259 | 225 |
end |
226 |
in |
|
227 |
shrink_proof' true ctxt proof |
|
228 |
end |
|
229 |
||
230 |
end |