author | blanchet |
Fri, 31 Jan 2014 19:16:41 +0100 | |
changeset 55223 | 3c593bad6b31 |
parent 55221 | ee90eebb8b73 |
child 55243 | 66709d41601e |
permissions | -rw-r--r-- |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_isar_compress.ML |
54813 | 2 |
Author: Steffen Juilf Smolka, TU Muenchen |
50263 | 3 |
Author: Jasmin Blanchette, TU Muenchen |
4 |
||
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
5 |
Compression of Isar proofs by merging steps. |
54712 | 6 |
Only proof steps using the same proof method are merged. |
50263 | 7 |
*) |
8 |
||
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
9 |
signature SLEDGEHAMMER_ISAR_COMPRESS = |
50259 | 10 |
sig |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
11 |
type isar_proof = Sledgehammer_Isar_Proof.isar_proof |
55213 | 12 |
type isar_preplay_data = Sledgehammer_Isar_Preplay.isar_preplay_data |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
13 |
|
55213 | 14 |
val compress_isar_proof : real -> isar_preplay_data -> isar_proof -> isar_proof |
54504 | 15 |
end; |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
16 |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
17 |
structure Sledgehammer_Isar_Compress : SLEDGEHAMMER_ISAR_COMPRESS = |
50259 | 18 |
struct |
19 |
||
50265 | 20 |
open Sledgehammer_Util |
54828 | 21 |
open Sledgehammer_Reconstructor |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
22 |
open Sledgehammer_Isar_Proof |
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
23 |
open Sledgehammer_Isar_Preplay |
50259 | 24 |
|
55212 | 25 |
val dummy_isar_step = Let (Term.dummy, Term.dummy) |
26 |
||
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
27 |
(* traverses steps in post-order and collects the steps with the given labels *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
28 |
fun collect_successors steps lbls = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
29 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
30 |
fun do_steps _ ([], accu) = ([], accu) |
54712 | 31 |
| do_steps [] accum = accum |
32 |
| do_steps (step :: steps) accum = do_steps steps (do_step step accum) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
33 |
and do_step (Let _) x = x |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
34 |
| do_step (step as Prove (_, _, l, _, subproofs, _)) x = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
35 |
(case do_subproofs subproofs x of |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
36 |
([], accu) => ([], accu) |
54712 | 37 |
| accum as (l' :: lbls', accu) => if l = l' then (lbls', step :: accu) else accum) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
38 |
and do_subproofs [] x = x |
54712 | 39 |
| do_subproofs (proof :: subproofs) x = |
40 |
(case do_steps (steps_of_proof proof) x of |
|
41 |
accum as ([], _) => accum |
|
42 |
| accum => do_subproofs subproofs accum) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
43 |
in |
54712 | 44 |
(case do_steps steps (lbls, []) of |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
45 |
([], succs) => rev succs |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
46 |
| _ => raise Fail "Sledgehammer_Isar_Compress: collect_successors") |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
47 |
end |
50259 | 48 |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
49 |
(* traverses steps in reverse post-order and inserts the given updates *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
50 |
fun update_steps steps updates = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
51 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
52 |
fun do_steps [] updates = ([], updates) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
53 |
| do_steps steps [] = (steps, []) |
54712 | 54 |
| do_steps (step :: steps) updates = do_step step (do_steps steps updates) |
55 |
and do_step step (steps, []) = (step :: steps, []) |
|
56 |
| do_step (step as Let _) (steps, updates) = (step :: steps, updates) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
57 |
| do_step (Prove (qs, xs, l, t, subproofs, by)) |
54712 | 58 |
(steps, updates as Prove (qs', xs', l', t', subproofs', by') :: updates') = |
59 |
let |
|
60 |
val (subproofs, updates) = |
|
61 |
if l = l' then do_subproofs subproofs' updates' else do_subproofs subproofs updates |
|
62 |
in |
|
63 |
if l = l' then (Prove (qs', xs', l', t', subproofs, by') :: steps, updates) |
|
64 |
else (Prove (qs, xs, l, t, subproofs, by) :: steps, updates) |
|
65 |
end |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
66 |
| do_step _ _ = raise Fail "Sledgehammer_Isar_Compress: update_steps (invalid update)" |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
67 |
and do_subproofs [] updates = ([], updates) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
68 |
| do_subproofs steps [] = (steps, []) |
54712 | 69 |
| do_subproofs (proof :: subproofs) updates = |
70 |
do_proof proof (do_subproofs subproofs updates) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
71 |
and do_proof proof (proofs, []) = (proof :: proofs, []) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
72 |
| do_proof (Proof (fix, assms, steps)) (proofs, updates) = |
54712 | 73 |
let val (steps, updates) = do_steps steps updates in |
74 |
(Proof (fix, assms, steps) :: proofs, updates) |
|
75 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
76 |
in |
54712 | 77 |
(case do_steps steps (rev updates) of |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
78 |
(steps, []) => steps |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
79 |
| _ => raise Fail "Sledgehammer_Isar_Compress: update_steps") |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
80 |
end |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
81 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
82 |
(* Tries merging the first step into the second step. |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
83 |
FIXME: Arbitrarily picks the second step's method. *) |
54752 | 84 |
fun try_merge (Prove (_, [], lbl1, _, [], ((lfs1, gfs1), _))) |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
85 |
(Prove (qs2, fix, lbl2, t, subproofs, ((lfs2, gfs2), methss2))) = |
54752 | 86 |
let |
87 |
val lfs = remove (op =) lbl1 lfs2 |> union (op =) lfs1 |
|
88 |
val gfs = union (op =) gfs1 gfs2 |
|
89 |
in |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
90 |
SOME (Prove (qs2, fix, lbl2, t, subproofs, ((lfs, gfs), methss2))) |
54752 | 91 |
end |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
92 |
| try_merge _ _ = NONE |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
93 |
|
53763 | 94 |
val compress_degree = 2 |
53762 | 95 |
val merge_timeout_slack = 1.2 |
96 |
||
54712 | 97 |
(* Precondition: The proof must be labeled canonically |
98 |
(cf. "Slegehammer_Proof.relabel_proof_canonically"). *) |
|
55213 | 99 |
fun compress_isar_proof compress_isar |
55221 | 100 |
({preplay_outcome, set_preplay_outcome, preplay_quietly, ...} : isar_preplay_data) proof = |
55183
17ec4a29ef71
renamed Sledgehammer options for symmetry between positive and negative versions
blanchet
parents:
54828
diff
changeset
|
101 |
if compress_isar <= 1.0 then |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
102 |
proof |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
103 |
else |
54712 | 104 |
let |
105 |
val (compress_further, decrement_step_count) = |
|
106 |
let |
|
55212 | 107 |
val number_of_steps = add_isar_steps (steps_of_proof proof) 0 |
55183
17ec4a29ef71
renamed Sledgehammer options for symmetry between positive and negative versions
blanchet
parents:
54828
diff
changeset
|
108 |
val target_number_of_steps = Real.round (Real.fromInt number_of_steps / compress_isar) |
54712 | 109 |
val delta = Unsynchronized.ref (number_of_steps - target_number_of_steps) |
110 |
in |
|
111 |
(fn () => !delta > 0, fn () => delta := !delta - 1) |
|
112 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
113 |
|
54712 | 114 |
val (get_successors, replace_successor) = |
115 |
let |
|
116 |
fun add_refs (Let _) = I |
|
117 |
| add_refs (Prove (_, _, v, _, _, ((lfs, _), _))) = |
|
55212 | 118 |
fold (fn key => Canonical_Label_Tab.cons_list (key, v)) lfs |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
119 |
|
54712 | 120 |
val tab = |
55212 | 121 |
Canonical_Label_Tab.empty |
54712 | 122 |
|> fold_isar_steps add_refs (steps_of_proof proof) |
123 |
(* "rev" should have the same effect as "sort canonical_label_ord" *) |
|
55212 | 124 |
|> Canonical_Label_Tab.map (K rev) |
54712 | 125 |
|> Unsynchronized.ref |
51260 | 126 |
|
55212 | 127 |
fun get_successors l = Canonical_Label_Tab.lookup_list (!tab) l |
128 |
fun set_successors l refs = tab := Canonical_Label_Tab.update (l, refs) (!tab) |
|
54712 | 129 |
fun replace_successor old new dest = |
130 |
get_successors dest |
|
131 |
|> Ord_List.remove canonical_label_ord old |
|
132 |
|> Ord_List.union canonical_label_ord new |
|
133 |
|> set_successors dest |
|
134 |
in |
|
135 |
(get_successors, replace_successor) |
|
136 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
137 |
|
54712 | 138 |
(** elimination of trivial, one-step subproofs **) |
139 |
||
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
140 |
fun elim_subproofs' time qs fix l t lfs gfs (methss as (meth :: _) :: _) subs nontriv_subs = |
54712 | 141 |
if null subs orelse not (compress_further ()) then |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
142 |
(set_preplay_outcome l meth (Played time); |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
143 |
Prove (qs, fix, l, t, List.revAppend (nontriv_subs, subs), ((lfs, gfs), methss))) |
54712 | 144 |
else |
145 |
(case subs of |
|
146 |
(sub as Proof (_, assms, sub_steps)) :: subs => |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
147 |
(let |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
148 |
(* trivial subproofs have exactly one "Prove" step *) |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
149 |
val SOME (Prove (_, [], l', _, [], ((lfs', gfs'), (meth' :: _) :: _))) = |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
150 |
try the_single sub_steps |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
151 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
152 |
(* only touch proofs that can be preplayed sucessfully *) |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
153 |
val Played time' = Lazy.force (preplay_outcome l' meth') |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
154 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
155 |
(* merge steps *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
156 |
val subs'' = subs @ nontriv_subs |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
157 |
val lfs'' = union (op =) lfs (subtract (op =) (map fst assms) lfs') |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
158 |
val gfs'' = union (op =) gfs' gfs |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
159 |
val by = ((lfs'', gfs''), methss(*FIXME*)) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
160 |
val step'' = Prove (qs, fix, l, t, subs'', by) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
161 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
162 |
(* check if the modified step can be preplayed fast enough *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
163 |
val timeout = time_mult merge_timeout_slack (Time.+(time, time')) |
54828 | 164 |
val Played time'' = preplay_quietly timeout step'' |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
165 |
in |
52626 | 166 |
decrement_step_count (); (* l' successfully eliminated! *) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
167 |
map (replace_successor l' [l]) lfs'; |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
168 |
elim_subproofs' time'' qs fix l t lfs'' gfs'' methss subs nontriv_subs |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
169 |
end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
170 |
handle Bind => |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
171 |
elim_subproofs' time qs fix l t lfs gfs methss subs (sub :: nontriv_subs)) |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
172 |
| _ => raise Fail "Sledgehammer_Isar_Compress: elim_subproofs'") |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
173 |
|
54712 | 174 |
fun elim_subproofs (step as Let _) = step |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
175 |
| elim_subproofs (step as Prove (qs, fix, l, t, subproofs, |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
176 |
((lfs, gfs), methss as (meth :: _) :: _))) = |
54813 | 177 |
if subproofs = [] then |
178 |
step |
|
179 |
else |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
180 |
(case Lazy.force (preplay_outcome l meth) of |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
181 |
Played time => elim_subproofs' time qs fix l t lfs gfs methss subproofs [] |
54826 | 182 |
| _ => step) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
183 |
|
55212 | 184 |
(** top_level compression: eliminate steps by merging them into their successors **) |
54712 | 185 |
fun compress_top_level steps = |
186 |
let |
|
187 |
(* (#successors, (size_of_term t, position)) *) |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
188 |
fun cand_key (i, l, t_size) = (length (get_successors l), (t_size, i)) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
189 |
|
54712 | 190 |
val compression_ord = |
191 |
prod_ord int_ord (prod_ord (int_ord #> rev_order) int_ord) |
|
192 |
#> rev_order |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
193 |
|
54712 | 194 |
val cand_ord = pairself cand_key #> compression_ord |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
195 |
|
55212 | 196 |
fun pop_next_cand [] = (NONE, []) |
197 |
| pop_next_cand (cands as (cand :: cands')) = |
|
198 |
let |
|
199 |
val best as (i, _, _) = |
|
200 |
fold (fn x => fn y => if cand_ord (x, y) = GREATER then x else y) cands' cand |
|
201 |
in (SOME best, filter_out (fn (j, _, _) => j = i) cands) end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
202 |
|
54712 | 203 |
val candidates = |
204 |
let |
|
205 |
fun add_cand (_, Let _) = I |
|
206 |
| add_cand (i, Prove (_, _, l, t, _, _)) = cons (i, l, size_of_term t) |
|
207 |
in |
|
208 |
(steps |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
209 |
|> split_last |> fst (* keep last step *) |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
210 |
|> fold_index add_cand) [] |
54712 | 211 |
end |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
212 |
|
54712 | 213 |
fun try_eliminate (i, l, _) succ_lbls steps = |
214 |
let |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
215 |
val ((cand as Prove (_, _, l, _, _, ((lfs, _), (meth :: _) :: _))) :: steps') = |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
216 |
drop i steps |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
217 |
|
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
218 |
val succs = collect_successors steps' succ_lbls |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
219 |
val succ_meths = map (hd o hd o snd o the o byline_of_isar_step) succs |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
220 |
|
54712 | 221 |
(* only touch steps that can be preplayed successfully *) |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
222 |
val Played time = Lazy.force (preplay_outcome l meth) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
223 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
224 |
val succs' = map (try_merge cand #> the) succs |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
225 |
|
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
226 |
val succ_times = |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
227 |
map2 ((fn Played t => t) o Lazy.force oo preplay_outcome) succ_lbls succ_meths |
54712 | 228 |
val timeslice = time_mult (1.0 / (Real.fromInt (length succ_lbls))) time |
229 |
val timeouts = |
|
230 |
map (curry Time.+ timeslice #> time_mult merge_timeout_slack) succ_times |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
231 |
|
54712 | 232 |
(* FIXME: debugging *) |
233 |
val _ = |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
234 |
if the (label_of_isar_step cand) <> l then |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
235 |
raise Fail "Sledgehammer_Isar_Compress: try_eliminate" |
54712 | 236 |
else |
237 |
() |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
238 |
|
54813 | 239 |
(* TODO: should be lazy: stop preplaying as soon as one step fails/times out *) |
54828 | 240 |
val play_outcomes = map2 preplay_quietly timeouts succs' |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
241 |
|
54712 | 242 |
(* ensure none of the modified successors timed out *) |
54828 | 243 |
val true = List.all (fn Played _ => true) play_outcomes |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
244 |
|
54712 | 245 |
val (steps1, _ :: steps2) = chop i steps |
246 |
(* replace successors with their modified versions *) |
|
247 |
val steps2 = update_steps steps2 succs' |
|
248 |
in |
|
249 |
decrement_step_count (); (* candidate successfully eliminated *) |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
250 |
map3 set_preplay_outcome succ_lbls succ_meths play_outcomes; |
54712 | 251 |
map (replace_successor l succ_lbls) lfs; |
54813 | 252 |
(* removing the step would mess up the indices -> replace with dummy step instead *) |
54712 | 253 |
steps1 @ dummy_isar_step :: steps2 |
254 |
end |
|
255 |
handle Bind => steps |
|
256 |
| Match => steps |
|
257 |
| Option.Option => steps |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
258 |
|
54712 | 259 |
fun compression_loop candidates steps = |
260 |
if not (compress_further ()) then |
|
261 |
steps |
|
262 |
else |
|
263 |
(case pop_next_cand candidates of |
|
264 |
(NONE, _) => steps (* no more candidates for elimination *) |
|
265 |
| (SOME (cand as (_, l, _)), candidates) => |
|
266 |
let val successors = get_successors l in |
|
267 |
if length successors > compress_degree then steps |
|
268 |
else compression_loop candidates (try_eliminate cand successors steps) |
|
269 |
end) |
|
270 |
in |
|
271 |
compression_loop candidates steps |
|
272 |
|> remove (op =) dummy_isar_step |
|
273 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
274 |
|
54712 | 275 |
(** recusion over the proof tree **) |
276 |
(* |
|
277 |
Proofs are compressed bottom-up, beginning with the innermost |
|
278 |
subproofs. |
|
279 |
On the innermost proof level, the proof steps have no subproofs. |
|
280 |
In the best case, these steps can be merged into just one step, |
|
281 |
resulting in a trivial subproof. Going one level up, trivial subproofs |
|
282 |
can be eliminated. In the best case, this once again leads to a proof |
|
283 |
whose proof steps do not have subproofs. Applying this approach |
|
284 |
recursively will result in a flat proof in the best cast. |
|
285 |
*) |
|
286 |
fun do_proof (proof as (Proof (fix, assms, steps))) = |
|
287 |
if compress_further () then Proof (fix, assms, do_steps steps) else proof |
|
288 |
and do_steps steps = |
|
289 |
(* bottom-up: compress innermost proofs first *) |
|
290 |
steps |> map (fn step => step |> compress_further () ? do_sub_levels) |
|
291 |
|> compress_further () ? compress_top_level |
|
292 |
and do_sub_levels (Let x) = Let x |
|
293 |
| do_sub_levels (Prove (qs, xs, l, t, subproofs, by)) = |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
294 |
(* compress subproofs *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
295 |
Prove (qs, xs, l, t, map do_proof subproofs, by) |
54712 | 296 |
(* eliminate trivial subproofs *) |
297 |
|> compress_further () ? elim_subproofs |
|
298 |
in |
|
299 |
do_proof proof |
|
300 |
end |
|
50259 | 301 |
|
54504 | 302 |
end; |