author | blanchet |
Fri, 31 Jan 2014 16:10:39 +0100 | |
changeset 55212 | 5832470d956e |
parent 55202 | 824c48a539c9 |
child 55213 | dcb36a2540bc |
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 |
55212 | 12 |
type preplay_data = Sledgehammer_Isar_Preplay.preplay_data |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
13 |
|
55212 | 14 |
val compress_proof : real -> 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 |
|
54752 | 82 |
(* Tries merging the first step into the second step. Arbitrarily picks the second step's method. *) |
83 |
fun try_merge (Prove (_, [], lbl1, _, [], ((lfs1, gfs1), _))) |
|
54712 | 84 |
(Prove (qs2, fix, lbl2, t, subproofs, ((lfs2, gfs2), meth2))) = |
54752 | 85 |
let |
86 |
val lfs = remove (op =) lbl1 lfs2 |> union (op =) lfs1 |
|
87 |
val gfs = union (op =) gfs1 gfs2 |
|
88 |
in |
|
89 |
SOME (Prove (qs2, fix, lbl2, t, subproofs, ((lfs, gfs), meth2))) |
|
90 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
91 |
| try_merge _ _ = NONE |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
92 |
|
53763 | 93 |
val compress_degree = 2 |
53762 | 94 |
val merge_timeout_slack = 1.2 |
95 |
||
54712 | 96 |
(* Precondition: The proof must be labeled canonically |
97 |
(cf. "Slegehammer_Proof.relabel_proof_canonically"). *) |
|
55183
17ec4a29ef71
renamed Sledgehammer options for symmetry between positive and negative versions
blanchet
parents:
54828
diff
changeset
|
98 |
fun compress_proof compress_isar |
55212 | 99 |
({get_preplay_outcome, set_preplay_outcome, preplay_quietly, ...} : preplay_data) proof = |
55183
17ec4a29ef71
renamed Sledgehammer options for symmetry between positive and negative versions
blanchet
parents:
54828
diff
changeset
|
100 |
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
|
101 |
proof |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
102 |
else |
54712 | 103 |
let |
104 |
val (compress_further, decrement_step_count) = |
|
105 |
let |
|
55212 | 106 |
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
|
107 |
val target_number_of_steps = Real.round (Real.fromInt number_of_steps / compress_isar) |
54712 | 108 |
val delta = Unsynchronized.ref (number_of_steps - target_number_of_steps) |
109 |
in |
|
110 |
(fn () => !delta > 0, fn () => delta := !delta - 1) |
|
111 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
112 |
|
54712 | 113 |
val (get_successors, replace_successor) = |
114 |
let |
|
115 |
fun add_refs (Let _) = I |
|
116 |
| add_refs (Prove (_, _, v, _, _, ((lfs, _), _))) = |
|
55212 | 117 |
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
|
118 |
|
54712 | 119 |
val tab = |
55212 | 120 |
Canonical_Label_Tab.empty |
54712 | 121 |
|> fold_isar_steps add_refs (steps_of_proof proof) |
122 |
(* "rev" should have the same effect as "sort canonical_label_ord" *) |
|
55212 | 123 |
|> Canonical_Label_Tab.map (K rev) |
54712 | 124 |
|> Unsynchronized.ref |
51260 | 125 |
|
55212 | 126 |
fun get_successors l = Canonical_Label_Tab.lookup_list (!tab) l |
127 |
fun set_successors l refs = tab := Canonical_Label_Tab.update (l, refs) (!tab) |
|
54712 | 128 |
fun replace_successor old new dest = |
129 |
get_successors dest |
|
130 |
|> Ord_List.remove canonical_label_ord old |
|
131 |
|> Ord_List.union canonical_label_ord new |
|
132 |
|> set_successors dest |
|
133 |
in |
|
134 |
(get_successors, replace_successor) |
|
135 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
136 |
|
54712 | 137 |
(** elimination of trivial, one-step subproofs **) |
138 |
||
139 |
fun elim_subproofs' time qs fix l t lfs gfs meth subs nontriv_subs = |
|
140 |
if null subs orelse not (compress_further ()) then |
|
54828 | 141 |
(set_preplay_outcome l (Played time); |
54712 | 142 |
Prove (qs, fix, l, t, List.revAppend (nontriv_subs, subs), ((lfs, gfs), meth))) |
143 |
else |
|
144 |
(case subs of |
|
145 |
(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
|
146 |
(let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
147 |
(* trivial subproofs have exactly one Prove step *) |
54712 | 148 |
val SOME (Prove (_, [], l', _, [], ((lfs', gfs'), _))) = try the_single sub_steps |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
149 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
150 |
(* only touch proofs that can be preplayed sucessfully *) |
54828 | 151 |
val Played time' = get_preplay_outcome l' |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
152 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
153 |
(* merge steps *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
154 |
val subs'' = subs @ nontriv_subs |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
155 |
val lfs'' = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
156 |
subtract (op =) (map fst assms) lfs' |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
157 |
|> union (op =) lfs |
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 |
54712 | 159 |
val by = ((lfs'', gfs''), meth) |
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 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
166 |
in |
52626 | 167 |
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
|
168 |
map (replace_successor l' [l]) lfs'; |
54712 | 169 |
elim_subproofs' time'' qs fix l t lfs'' gfs'' meth subs nontriv_subs |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
170 |
end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
171 |
handle Bind => |
54712 | 172 |
elim_subproofs' time qs fix l t lfs gfs meth subs (sub :: nontriv_subs)) |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
173 |
| _ => 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
|
174 |
|
54712 | 175 |
fun elim_subproofs (step as Let _) = step |
54813 | 176 |
| elim_subproofs (step as Prove (qs, fix, l, t, subproofs, ((lfs, gfs), meth))) = |
177 |
if subproofs = [] then |
|
178 |
step |
|
179 |
else |
|
54828 | 180 |
(case get_preplay_outcome l of |
181 |
Played time => elim_subproofs' time qs fix l t lfs gfs meth 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 **) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
185 |
|
54712 | 186 |
fun compress_top_level steps = |
187 |
let |
|
188 |
(* (#successors, (size_of_term t, position)) *) |
|
189 |
fun cand_key (i, l, t_size) = (get_successors l |> length, (t_size, i)) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
190 |
|
54712 | 191 |
val compression_ord = |
192 |
prod_ord int_ord (prod_ord (int_ord #> rev_order) int_ord) |
|
193 |
#> rev_order |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
194 |
|
54712 | 195 |
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
|
196 |
|
55212 | 197 |
fun pop_next_cand [] = (NONE, []) |
198 |
| pop_next_cand (cands as (cand :: cands')) = |
|
199 |
let |
|
200 |
val best as (i, _, _) = |
|
201 |
fold (fn x => fn y => if cand_ord (x, y) = GREATER then x else y) cands' cand |
|
202 |
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
|
203 |
|
54712 | 204 |
val candidates = |
205 |
let |
|
206 |
fun add_cand (_, Let _) = I |
|
207 |
| add_cand (i, Prove (_, _, l, t, _, _)) = cons (i, l, size_of_term t) |
|
208 |
in |
|
209 |
(steps |
|
210 |
|> split_last |> fst (* keep last step *) |
|
211 |
|> fold_index add_cand) [] |
|
212 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
213 |
|
54712 | 214 |
fun try_eliminate (i, l, _) succ_lbls steps = |
215 |
let |
|
216 |
(* only touch steps that can be preplayed successfully *) |
|
54828 | 217 |
val Played time = get_preplay_outcome l |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
218 |
|
54826 | 219 |
val succ_times = |
54828 | 220 |
map (get_preplay_outcome #> (fn Played t => t)) succ_lbls |
54712 | 221 |
val timeslice = time_mult (1.0 / (Real.fromInt (length succ_lbls))) time |
222 |
val timeouts = |
|
223 |
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
|
224 |
|
54712 | 225 |
val ((cand as Prove (_, _, l, _, _, ((lfs, _), _))) :: steps') = drop i steps |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
226 |
|
54712 | 227 |
(* FIXME: debugging *) |
228 |
val _ = |
|
229 |
if the (label_of_step cand) <> l then |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
230 |
raise Fail "Sledgehammer_Isar_Compress: try_eliminate" |
54712 | 231 |
else |
232 |
() |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
233 |
|
54712 | 234 |
val succs = collect_successors steps' succ_lbls |
235 |
val succs' = map (try_merge cand #> the) succs |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
236 |
|
54813 | 237 |
(* TODO: should be lazy: stop preplaying as soon as one step fails/times out *) |
54828 | 238 |
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
|
239 |
|
54712 | 240 |
(* ensure none of the modified successors timed out *) |
54828 | 241 |
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
|
242 |
|
54712 | 243 |
val (steps1, _ :: steps2) = chop i steps |
244 |
(* replace successors with their modified versions *) |
|
245 |
val steps2 = update_steps steps2 succs' |
|
246 |
in |
|
247 |
decrement_step_count (); (* candidate successfully eliminated *) |
|
54828 | 248 |
map2 set_preplay_outcome succ_lbls play_outcomes; |
54712 | 249 |
map (replace_successor l succ_lbls) lfs; |
54813 | 250 |
(* removing the step would mess up the indices -> replace with dummy step instead *) |
54712 | 251 |
steps1 @ dummy_isar_step :: steps2 |
252 |
end |
|
253 |
handle Bind => steps |
|
254 |
| Match => steps |
|
255 |
| Option.Option => steps |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
256 |
|
54712 | 257 |
fun compression_loop candidates steps = |
258 |
if not (compress_further ()) then |
|
259 |
steps |
|
260 |
else |
|
261 |
(case pop_next_cand candidates of |
|
262 |
(NONE, _) => steps (* no more candidates for elimination *) |
|
263 |
| (SOME (cand as (_, l, _)), candidates) => |
|
264 |
let val successors = get_successors l in |
|
265 |
if length successors > compress_degree then steps |
|
266 |
else compression_loop candidates (try_eliminate cand successors steps) |
|
267 |
end) |
|
268 |
in |
|
269 |
compression_loop candidates steps |
|
270 |
|> remove (op =) dummy_isar_step |
|
271 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
272 |
|
54712 | 273 |
(** recusion over the proof tree **) |
274 |
(* |
|
275 |
Proofs are compressed bottom-up, beginning with the innermost |
|
276 |
subproofs. |
|
277 |
On the innermost proof level, the proof steps have no subproofs. |
|
278 |
In the best case, these steps can be merged into just one step, |
|
279 |
resulting in a trivial subproof. Going one level up, trivial subproofs |
|
280 |
can be eliminated. In the best case, this once again leads to a proof |
|
281 |
whose proof steps do not have subproofs. Applying this approach |
|
282 |
recursively will result in a flat proof in the best cast. |
|
283 |
*) |
|
284 |
fun do_proof (proof as (Proof (fix, assms, steps))) = |
|
285 |
if compress_further () then Proof (fix, assms, do_steps steps) else proof |
|
286 |
and do_steps steps = |
|
287 |
(* bottom-up: compress innermost proofs first *) |
|
288 |
steps |> map (fn step => step |> compress_further () ? do_sub_levels) |
|
289 |
|> compress_further () ? compress_top_level |
|
290 |
and do_sub_levels (Let x) = Let x |
|
291 |
| 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
|
292 |
(* compress subproofs *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
293 |
Prove (qs, xs, l, t, map do_proof subproofs, by) |
54712 | 294 |
(* eliminate trivial subproofs *) |
295 |
|> compress_further () ? elim_subproofs |
|
296 |
in |
|
297 |
do_proof proof |
|
298 |
end |
|
50259 | 299 |
|
54504 | 300 |
end; |