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