author | blanchet |
Tue, 04 Feb 2014 23:11:18 +0100 | |
changeset 55323 | 253a029335a2 |
parent 55313 | cddd94fb0e8d |
child 55325 | 462ffd3b7065 |
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 |
55287 | 22 |
open Sledgehammer_Proof_Methods |
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 |
|
55299 | 35 |
| collect_step (step as Prove (_, _, l, _, subproofs, _, _, _)) x = |
55263 | 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 |
55313 | 45 |
rev (snd (collect_steps steps (lbls, []))) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
46 |
end |
50259 | 47 |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
48 |
(* 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
|
49 |
fun update_steps steps updates = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
50 |
let |
55263 | 51 |
fun update_steps [] updates = ([], updates) |
52 |
| update_steps steps [] = (steps, []) |
|
53 |
| update_steps (step :: steps) updates = update_step step (update_steps steps updates) |
|
54 |
and update_step step (steps, []) = (step :: steps, []) |
|
55 |
| update_step (step as Let _) (steps, updates) = (step :: steps, updates) |
|
55299 | 56 |
| update_step (Prove (qs, xs, l, t, subproofs, facts, meths, comment)) |
57 |
(steps, |
|
58 |
updates as Prove (qs', xs', l', t', subproofs', facts', meths', comment') :: updates') = |
|
59 |
(if l = l' then |
|
60 |
update_subproofs subproofs' updates' |
|
61 |
|>> (fn subproofs' => Prove (qs', xs', l', t', subproofs', facts', meths', comment')) |
|
62 |
else |
|
63 |
update_subproofs subproofs updates |
|
64 |
|>> (fn subproofs => Prove (qs, xs, l, t, subproofs, facts, meths, comment))) |
|
65 |
|>> (fn step => step :: steps) |
|
55263 | 66 |
and update_subproofs [] updates = ([], updates) |
67 |
| update_subproofs steps [] = (steps, []) |
|
68 |
| update_subproofs (proof :: subproofs) updates = |
|
69 |
update_proof proof (update_subproofs subproofs updates) |
|
70 |
and update_proof proof (proofs, []) = (proof :: proofs, []) |
|
71 |
| update_proof (Proof (fix, assms, steps)) (proofs, updates) = |
|
72 |
let val (steps, updates) = update_steps steps updates in |
|
54712 | 73 |
(Proof (fix, assms, steps) :: proofs, updates) |
74 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
75 |
in |
55313 | 76 |
fst (update_steps steps (rev updates)) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
77 |
end |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
78 |
|
55283
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
79 |
fun merge_methods preplay_data (l1, meths1) (l2, meths2) = |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
80 |
let |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
81 |
fun is_method_hopeful l meth = |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
82 |
let val outcome = preplay_outcome_of_isar_step_for_method preplay_data l meth in |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
83 |
not (Lazy.is_finished outcome) orelse |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
84 |
(case Lazy.force outcome of Played _ => true | _ => false) |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
85 |
end |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
86 |
in |
55323
253a029335a2
split 'linarith' and 'presburger' (to avoid annoying warnings + to speed up reconstruction when 'presburger' is needed)
blanchet
parents:
55313
diff
changeset
|
87 |
meths2 @ subtract (op =) meths2 meths1 |
55312 | 88 |
|> filter (is_method_hopeful l1 andf is_method_hopeful l2) |
55283
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
89 |
end |
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
90 |
|
55299 | 91 |
fun try_merge preplay_data (Prove (_, [], l1, _, subproofs1, (lfs1, gfs1), meths1, comment1)) |
92 |
(Prove (qs2, fix, l2, t, subproofs2, (lfs2, gfs2), meths2, comment2)) = |
|
55283
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
93 |
(case merge_methods preplay_data (l1, meths1) (l2, meths2) of |
55247
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
94 |
[] => NONE |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
95 |
| meths => |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
96 |
let |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
in |
55299 | 100 |
SOME (Prove (qs2, fix, l2, t, subproofs1 @ subproofs2, (lfs, gfs), meths, |
101 |
comment1 ^ comment2)) |
|
55247
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
102 |
end) |
55283
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
103 |
| try_merge _ _ _ = NONE |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
104 |
|
53763 | 105 |
val compress_degree = 2 |
55271
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
106 |
val merge_timeout_slack_time = seconds 0.005 |
55278
73372494fd80
more flexible compression, choosing whichever proof method works
blanchet
parents:
55272
diff
changeset
|
107 |
val merge_timeout_slack_factor = 1.25 |
53762 | 108 |
|
55271
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
109 |
fun slackify_merge_timeout time = |
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
110 |
time_mult merge_timeout_slack_factor (Time.+ (merge_timeout_slack_time, time)) |
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
111 |
|
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
112 |
(* Precondition: The proof must be labeled canonically. *) |
55260 | 113 |
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
|
114 |
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
|
115 |
proof |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
116 |
else |
54712 | 117 |
let |
118 |
val (compress_further, decrement_step_count) = |
|
119 |
let |
|
55260 | 120 |
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
|
121 |
val target_number_of_steps = Real.round (Real.fromInt number_of_steps / compress_isar) |
54712 | 122 |
val delta = Unsynchronized.ref (number_of_steps - target_number_of_steps) |
123 |
in |
|
124 |
(fn () => !delta > 0, fn () => delta := !delta - 1) |
|
125 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
126 |
|
54712 | 127 |
val (get_successors, replace_successor) = |
128 |
let |
|
55299 | 129 |
fun add_refs (Prove (_, _, l, _, _, (lfs, _), _, _)) = |
130 |
fold (fn key => Canonical_Label_Tab.cons_list (key, l)) lfs |
|
131 |
| add_refs _ = I |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
132 |
|
54712 | 133 |
val tab = |
55212 | 134 |
Canonical_Label_Tab.empty |
55260 | 135 |
|> fold_isar_steps add_refs (steps_of_isar_proof proof) |
54712 | 136 |
(* "rev" should have the same effect as "sort canonical_label_ord" *) |
55212 | 137 |
|> Canonical_Label_Tab.map (K rev) |
54712 | 138 |
|> Unsynchronized.ref |
51260 | 139 |
|
55212 | 140 |
fun get_successors l = Canonical_Label_Tab.lookup_list (!tab) l |
141 |
fun set_successors l refs = tab := Canonical_Label_Tab.update (l, refs) (!tab) |
|
54712 | 142 |
fun replace_successor old new dest = |
143 |
get_successors dest |
|
144 |
|> Ord_List.remove canonical_label_ord old |
|
145 |
|> Ord_List.union canonical_label_ord new |
|
146 |
|> set_successors dest |
|
147 |
in |
|
148 |
(get_successors, replace_successor) |
|
149 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
150 |
|
55243 | 151 |
(* elimination of trivial, one-step subproofs *) |
55309 | 152 |
fun elim_one_subproof time meths_outcomes qs fix l t lfs gfs (meths as meth :: _) comment subs |
153 |
nontriv_subs = |
|
54712 | 154 |
if null subs orelse not (compress_further ()) then |
55264 | 155 |
let |
156 |
val subproofs = List.revAppend (nontriv_subs, subs) |
|
55299 | 157 |
val step = Prove (qs, fix, l, t, subproofs, (lfs, gfs), meths, comment) |
55264 | 158 |
in |
55309 | 159 |
set_preplay_outcomes_of_isar_step ctxt time preplay_data step |
160 |
((meth, Played time) :: meths_outcomes); |
|
55264 | 161 |
step |
162 |
end |
|
54712 | 163 |
else |
164 |
(case subs of |
|
165 |
(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
|
166 |
(let |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
167 |
(* trivial subproofs have exactly one "Prove" step *) |
55299 | 168 |
val [Prove (_, [], l', _, [], (lfs', gfs'), meths', _)] = sub_steps |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
169 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
170 |
(* only touch proofs that can be preplayed sucessfully *) |
55272 | 171 |
val Played time' = forced_intermediate_preplay_outcome_of_isar_step (!preplay_data) l' |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
172 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
173 |
(* merge steps *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
174 |
val subs'' = subs @ nontriv_subs |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
175 |
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
|
176 |
val gfs'' = union (op =) gfs' gfs |
55283
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
177 |
val meths'' as _ :: _ = merge_methods (!preplay_data) (l', meths') (l, meths) |
55299 | 178 |
val step'' = Prove (qs, fix, l, t, subs'', (lfs'', gfs''), meths'', comment) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
179 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
180 |
(* check if the modified step can be preplayed fast enough *) |
55271
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
181 |
val timeout = slackify_merge_timeout (Time.+ (time, time')) |
55309 | 182 |
val (_, Played time'') :: meths_outcomes = preplay_isar_step ctxt timeout step'' |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
183 |
in |
52626 | 184 |
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
|
185 |
map (replace_successor l' [l]) lfs'; |
55309 | 186 |
elim_one_subproof time'' meths_outcomes qs fix l t lfs'' gfs'' meths comment subs |
187 |
nontriv_subs |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
188 |
end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
189 |
handle Bind => |
55309 | 190 |
elim_one_subproof time [] qs fix l t lfs gfs meths comment subs (sub :: nontriv_subs)) |
55264 | 191 |
| _ => 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
|
192 |
|
55299 | 193 |
fun elim_subproofs (step as Prove (qs, fix, l, t, subproofs, (lfs, gfs), meths, comment)) = |
54813 | 194 |
if subproofs = [] then |
195 |
step |
|
196 |
else |
|
55272 | 197 |
(case forced_intermediate_preplay_outcome_of_isar_step (!preplay_data) l of |
55309 | 198 |
Played time => elim_one_subproof time [] qs fix l t lfs gfs meths comment subproofs [] |
54826 | 199 |
| _ => step) |
55299 | 200 |
| elim_subproofs step = step |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
201 |
|
54712 | 202 |
fun compress_top_level steps = |
203 |
let |
|
204 |
(* (#successors, (size_of_term t, position)) *) |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
205 |
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
|
206 |
|
54712 | 207 |
val compression_ord = |
208 |
prod_ord int_ord (prod_ord (int_ord #> rev_order) int_ord) |
|
209 |
#> rev_order |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
210 |
|
54712 | 211 |
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
|
212 |
|
55298 | 213 |
fun pop_next_candidate [] = (NONE, []) |
214 |
| pop_next_candidate (cands as (cand :: cands')) = |
|
55212 | 215 |
let |
216 |
val best as (i, _, _) = |
|
217 |
fold (fn x => fn y => if cand_ord (x, y) = GREATER then x else y) cands' cand |
|
218 |
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
|
219 |
|
54712 | 220 |
val candidates = |
221 |
let |
|
55299 | 222 |
fun add_cand (i, Prove (_, _, l, t, _, _, _, _)) = cons (i, l, size_of_term t) |
223 |
| add_cand _ = I |
|
54712 | 224 |
in |
225 |
(steps |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
226 |
|> split_last |> fst (* keep last step *) |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
227 |
|> fold_index add_cand) [] |
54712 | 228 |
end |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
229 |
|
55264 | 230 |
fun try_eliminate (i, l, _) labels steps = |
54712 | 231 |
let |
55299 | 232 |
val ((cand as Prove (_, _, _, _, _, (lfs, _), _, _)) :: steps') = drop i steps |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
233 |
|
55264 | 234 |
val succs = collect_successors steps' labels |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
235 |
|
55307 | 236 |
(* only touch steps that can be preplayed successfully; FIXME: more generous *) |
55272 | 237 |
val Played time = forced_intermediate_preplay_outcome_of_isar_step (!preplay_data) l |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
238 |
|
55283
b90c06d54d38
when merging, don't try methods that failed or timed out for either of the steps for the combined step
blanchet
parents:
55280
diff
changeset
|
239 |
val succs' = map (try_merge (!preplay_data) cand #> the) succs |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
240 |
|
55307 | 241 |
(* FIXME: more generous *) |
55269 | 242 |
val times0 = map ((fn Played time => time) o |
55272 | 243 |
forced_intermediate_preplay_outcome_of_isar_step (!preplay_data)) labels |
55264 | 244 |
val time_slice = time_mult (1.0 / (Real.fromInt (length labels))) time |
55271
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
245 |
val timeouts = map (curry Time.+ time_slice #> slackify_merge_timeout) times0 |
55307 | 246 |
(* FIXME: "preplay_timeout" should be an ultimate maximum *) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
247 |
|
55278
73372494fd80
more flexible compression, choosing whichever proof method works
blanchet
parents:
55272
diff
changeset
|
248 |
val meths_outcomess = map2 (preplay_isar_step ctxt) timeouts succs' |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
249 |
|
54712 | 250 |
(* ensure none of the modified successors timed out *) |
55278
73372494fd80
more flexible compression, choosing whichever proof method works
blanchet
parents:
55272
diff
changeset
|
251 |
val times = map (fn (_, Played time) :: _ => time) meths_outcomess |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
252 |
|
55298 | 253 |
val (steps_before, _ :: steps_after) = chop i steps |
54712 | 254 |
(* replace successors with their modified versions *) |
55298 | 255 |
val steps_after = update_steps steps_after succs' |
54712 | 256 |
in |
257 |
decrement_step_count (); (* candidate successfully eliminated *) |
|
55264 | 258 |
map3 (fn time => set_preplay_outcomes_of_isar_step ctxt time preplay_data) times |
55278
73372494fd80
more flexible compression, choosing whichever proof method works
blanchet
parents:
55272
diff
changeset
|
259 |
succs' meths_outcomess; |
55264 | 260 |
map (replace_successor l labels) lfs; |
55243 | 261 |
(* removing the step would mess up the indices; replace with dummy step instead *) |
55298 | 262 |
steps_before @ dummy_isar_step :: steps_after |
54712 | 263 |
end |
264 |
handle Bind => steps |
|
265 |
| Match => steps |
|
266 |
| Option.Option => steps |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
267 |
|
54712 | 268 |
fun compression_loop candidates steps = |
269 |
if not (compress_further ()) then |
|
270 |
steps |
|
271 |
else |
|
55298 | 272 |
(case pop_next_candidate candidates of |
54712 | 273 |
(NONE, _) => steps (* no more candidates for elimination *) |
274 |
| (SOME (cand as (_, l, _)), candidates) => |
|
275 |
let val successors = get_successors l in |
|
276 |
if length successors > compress_degree then steps |
|
277 |
else compression_loop candidates (try_eliminate cand successors steps) |
|
278 |
end) |
|
279 |
in |
|
280 |
compression_loop candidates steps |
|
281 |
|> remove (op =) dummy_isar_step |
|
282 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
283 |
|
55268 | 284 |
(* Proofs are compressed bottom-up, beginning with the innermost subproofs. On the innermost |
285 |
proof level, the proof steps have no subproofs. In the best case, these steps can be merged |
|
286 |
into just one step, resulting in a trivial subproof. Going one level up, trivial subproofs |
|
287 |
can be eliminated. In the best case, this once again leads to a proof whose proof steps do |
|
288 |
not have subproofs. Applying this approach recursively will result in a flat proof in the |
|
289 |
best cast. *) |
|
55263 | 290 |
fun compress_proof (proof as (Proof (fix, assms, steps))) = |
291 |
if compress_further () then Proof (fix, assms, compress_steps steps) else proof |
|
292 |
and compress_steps steps = |
|
54712 | 293 |
(* bottom-up: compress innermost proofs first *) |
55263 | 294 |
steps |> map (fn step => step |> compress_further () ? compress_sub_levels) |
54712 | 295 |
|> compress_further () ? compress_top_level |
55263 | 296 |
and compress_sub_levels (step as Let _) = step |
55299 | 297 |
| compress_sub_levels (Prove (qs, xs, l, t, subproofs, facts, meths, comment)) = |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
298 |
(* compress subproofs *) |
55299 | 299 |
Prove (qs, xs, l, t, map compress_proof subproofs, facts, meths, comment) |
54712 | 300 |
(* eliminate trivial subproofs *) |
301 |
|> compress_further () ? elim_subproofs |
|
302 |
in |
|
55263 | 303 |
compress_proof proof |
54712 | 304 |
end |
50259 | 305 |
|
54504 | 306 |
end; |