author | blanchet |
Wed, 05 Feb 2014 11:37:32 +0100 | |
changeset 55333 | fa079fd40db8 |
parent 55332 | 803a7400cc58 |
child 55452 | 29ec8680e61f |
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 |
|
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
14 |
val compress_isar_proof : Proof.context -> real -> Time.time -> |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
15 |
isar_preplay_data Unsynchronized.ref -> 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 |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
26 |
fun collect_successors steps lbls = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
27 |
let |
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
28 |
fun collect_steps _ (accum as ([], _)) = accum |
55263 | 29 |
| collect_steps [] accum = accum |
30 |
| collect_steps (step :: steps) accum = collect_steps steps (collect_step step accum) |
|
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
31 |
and collect_step (step as Prove (_, _, l, _, subproofs, _, _, _)) x = |
55263 | 32 |
(case collect_subproofs subproofs x of |
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
33 |
(accum as ([], _)) => accum |
54712 | 34 |
| accum as (l' :: lbls', accu) => if l = l' then (lbls', step :: accu) else accum) |
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
35 |
| collect_step _ accum = accum |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
36 |
and collect_subproofs [] accum = accum |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
37 |
| collect_subproofs (proof :: subproofs) accum = |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
38 |
(case collect_steps (steps_of_isar_proof proof) accum of |
54712 | 39 |
accum as ([], _) => accum |
55263 | 40 |
| accum => collect_subproofs subproofs accum) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
41 |
in |
55313 | 42 |
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
|
43 |
end |
50259 | 44 |
|
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
45 |
fun update_steps updates steps = |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
46 |
let |
55263 | 47 |
fun update_steps [] updates = ([], updates) |
48 |
| update_steps steps [] = (steps, []) |
|
49 |
| update_steps (step :: steps) updates = update_step step (update_steps steps updates) |
|
50 |
and update_step step (steps, []) = (step :: steps, []) |
|
55299 | 51 |
| update_step (Prove (qs, xs, l, t, subproofs, facts, meths, comment)) |
52 |
(steps, |
|
53 |
updates as Prove (qs', xs', l', t', subproofs', facts', meths', comment') :: updates') = |
|
54 |
(if l = l' then |
|
55 |
update_subproofs subproofs' updates' |
|
56 |
|>> (fn subproofs' => Prove (qs', xs', l', t', subproofs', facts', meths', comment')) |
|
57 |
else |
|
58 |
update_subproofs subproofs updates |
|
59 |
|>> (fn subproofs => Prove (qs, xs, l, t, subproofs, facts, meths, comment))) |
|
60 |
|>> (fn step => step :: steps) |
|
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
61 |
| update_step step (steps, updates) = (step :: steps, updates) |
55263 | 62 |
and update_subproofs [] updates = ([], updates) |
63 |
| update_subproofs steps [] = (steps, []) |
|
64 |
| update_subproofs (proof :: subproofs) updates = |
|
65 |
update_proof proof (update_subproofs subproofs updates) |
|
66 |
and update_proof proof (proofs, []) = (proof :: proofs, []) |
|
67 |
| update_proof (Proof (fix, assms, steps)) (proofs, updates) = |
|
68 |
let val (steps, updates) = update_steps steps updates in |
|
54712 | 69 |
(Proof (fix, assms, steps) :: proofs, updates) |
70 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
71 |
in |
55313 | 72 |
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
|
73 |
end |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
74 |
|
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
|
75 |
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
|
76 |
let |
55328 | 77 |
fun is_hopeful l meth = |
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
|
78 |
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
|
79 |
not (Lazy.is_finished outcome) orelse |
55328 | 80 |
(case Lazy.force outcome of Played _ => true | Play_Timed_Out _ => true | _ => false) |
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
|
81 |
end |
55328 | 82 |
|
83 |
val (hopeful, hopeless) = |
|
84 |
meths2 @ subtract (op =) meths2 meths1 |
|
85 |
|> List.partition (is_hopeful l1 andf is_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
|
86 |
in |
55328 | 87 |
(hopeful @ hopeless, hopeless) |
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
|
88 |
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
|
89 |
|
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
90 |
fun merge_steps preplay_data (Prove ([], fix1, l1, _, subproofs1, (lfs1, gfs1), meths1, comment1)) |
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
91 |
(Prove (qs2, fix2, l2, t, subproofs2, (lfs2, gfs2), meths2, comment2)) = |
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
92 |
let |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
93 |
val (meths, hopeless) = merge_methods preplay_data (l1, meths1) (l2, meths2) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
94 |
val lfs = union (op =) lfs1 (remove (op =) l1 lfs2) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
95 |
val gfs = union (op =) gfs1 gfs2 |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
96 |
in |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
97 |
(Prove (qs2, union (op =) fix1 fix2, l2, t, subproofs1 @ subproofs2, (lfs, gfs), meths, |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
98 |
comment1 ^ comment2), hopeless) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
99 |
end |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
100 |
|
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
101 |
val merge_slack_time = seconds 0.005 |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
102 |
val merge_slack_factor = 1.5 |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
103 |
|
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
104 |
fun adjust_merge_timeout max time = |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
105 |
let val timeout = time_mult merge_slack_factor (Time.+ (merge_slack_time, time)) in |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
106 |
if Time.< (max, timeout) then max else timeout |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
107 |
end |
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
108 |
|
53763 | 109 |
val compress_degree = 2 |
55271
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
110 |
|
e78476a99c70
better time slack, to account for ultra-quick proof methods
blanchet
parents:
55270
diff
changeset
|
111 |
(* Precondition: The proof must be labeled canonically. *) |
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
112 |
fun compress_isar_proof ctxt compress_isar preplay_timeout preplay_data proof = |
55183
17ec4a29ef71
renamed Sledgehammer options for symmetry between positive and negative versions
blanchet
parents:
54828
diff
changeset
|
113 |
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
|
114 |
proof |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
115 |
else |
54712 | 116 |
let |
117 |
val (compress_further, decrement_step_count) = |
|
118 |
let |
|
55260 | 119 |
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
|
120 |
val target_number_of_steps = Real.round (Real.fromInt number_of_steps / compress_isar) |
54712 | 121 |
val delta = Unsynchronized.ref (number_of_steps - target_number_of_steps) |
122 |
in |
|
123 |
(fn () => !delta > 0, fn () => delta := !delta - 1) |
|
124 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
125 |
|
54712 | 126 |
val (get_successors, replace_successor) = |
127 |
let |
|
55299 | 128 |
fun add_refs (Prove (_, _, l, _, _, (lfs, _), _, _)) = |
129 |
fold (fn key => Canonical_Label_Tab.cons_list (key, l)) lfs |
|
130 |
| add_refs _ = I |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
131 |
|
54712 | 132 |
val tab = |
55212 | 133 |
Canonical_Label_Tab.empty |
55260 | 134 |
|> fold_isar_steps add_refs (steps_of_isar_proof proof) |
54712 | 135 |
(* "rev" should have the same effect as "sort canonical_label_ord" *) |
55212 | 136 |
|> Canonical_Label_Tab.map (K rev) |
54712 | 137 |
|> Unsynchronized.ref |
51260 | 138 |
|
55212 | 139 |
fun get_successors l = Canonical_Label_Tab.lookup_list (!tab) l |
140 |
fun set_successors l refs = tab := Canonical_Label_Tab.update (l, refs) (!tab) |
|
54712 | 141 |
fun replace_successor old new dest = |
142 |
get_successors dest |
|
143 |
|> Ord_List.remove canonical_label_ord old |
|
144 |
|> Ord_List.union canonical_label_ord new |
|
145 |
|> set_successors dest |
|
146 |
in |
|
147 |
(get_successors, replace_successor) |
|
148 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
149 |
|
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
150 |
fun reference_time l = |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
151 |
(case forced_intermediate_preplay_outcome_of_isar_step (!preplay_data) l of |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
152 |
Played time => time |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
153 |
| _ => preplay_timeout) |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
154 |
|
55243 | 155 |
(* elimination of trivial, one-step subproofs *) |
55330
547d23e2abf7
corrected wrong 'meth :: _' pattern maching + tuned code
blanchet
parents:
55329
diff
changeset
|
156 |
fun elim_one_subproof time (step as Prove (qs, fix, l, t, _, (lfs, gfs), meths, comment)) subs |
55309 | 157 |
nontriv_subs = |
54712 | 158 |
if null subs orelse not (compress_further ()) then |
55330
547d23e2abf7
corrected wrong 'meth :: _' pattern maching + tuned code
blanchet
parents:
55329
diff
changeset
|
159 |
Prove (qs, fix, l, t, List.revAppend (nontriv_subs, subs), (lfs, gfs), meths, comment) |
54712 | 160 |
else |
161 |
(case subs of |
|
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
162 |
(sub as Proof (_, assms, [Prove (_, _, l', _, [], (lfs', gfs'), meths', _)])) :: subs => |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
163 |
let |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
164 |
(* merge steps *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
165 |
val subs'' = subs @ nontriv_subs |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
166 |
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
|
167 |
val gfs'' = union (op =) gfs' gfs |
55328 | 168 |
val (meths'' as _ :: _, hopeless) = |
169 |
merge_methods (!preplay_data) (l', meths') (l, meths) |
|
55299 | 170 |
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
|
171 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
172 |
(* check if the modified step can be preplayed fast enough *) |
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
173 |
val timeout = adjust_merge_timeout preplay_timeout (Time.+ (time, reference_time l')) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
174 |
in |
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
175 |
(case preplay_isar_step ctxt timeout hopeless step'' of |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
176 |
meths_outcomes as (_, Played time'') :: _ => |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
177 |
(* l' successfully eliminated *) |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
178 |
(decrement_step_count (); |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
179 |
set_preplay_outcomes_of_isar_step ctxt time'' preplay_data step'' meths_outcomes; |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
180 |
map (replace_successor l' [l]) lfs'; |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
181 |
elim_one_subproof time'' step'' subs nontriv_subs) |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
182 |
| _ => elim_one_subproof time step subs (sub :: nontriv_subs)) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
183 |
end |
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
184 |
| sub :: subs => elim_one_subproof time step subs (sub :: nontriv_subs)) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
185 |
|
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
186 |
fun elim_subproofs (step as Prove (_, _, l, _, subproofs, _, _, _)) = |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
187 |
if exists (null o tl o steps_of_isar_proof) subproofs then |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
188 |
elim_one_subproof (reference_time l) step subproofs [] |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
189 |
else |
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
190 |
step |
55299 | 191 |
| elim_subproofs step = step |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
192 |
|
54712 | 193 |
fun compress_top_level steps = |
194 |
let |
|
55331 | 195 |
fun cand_key (l, t_size) = (length (get_successors l), t_size) |
196 |
val cand_ord = prod_ord int_ord (int_ord o swap) o pairself cand_key |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
197 |
|
55298 | 198 |
fun pop_next_candidate [] = (NONE, []) |
199 |
| pop_next_candidate (cands as (cand :: cands')) = |
|
55331 | 200 |
fold (fn x => fn y => if cand_ord (x, y) = LESS then x else y) cands' cand |
55325 | 201 |
|> (fn best => (SOME best, remove (op =) best cands)) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
202 |
|
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
203 |
fun try_eliminate i l labels steps = |
54712 | 204 |
let |
55330
547d23e2abf7
corrected wrong 'meth :: _' pattern maching + tuned code
blanchet
parents:
55329
diff
changeset
|
205 |
val (steps_before, (cand as Prove (_, _, _, _, _, (lfs, _), _, _)) :: steps_after) = |
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
206 |
chop i steps |
55330
547d23e2abf7
corrected wrong 'meth :: _' pattern maching + tuned code
blanchet
parents:
55329
diff
changeset
|
207 |
val succs = collect_successors steps_after labels |
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
208 |
val (succs', hopelesses) = split_list (map (merge_steps (!preplay_data) cand) succs) |
54712 | 209 |
in |
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
210 |
(case try (map ((fn Played time => time) o |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
211 |
forced_intermediate_preplay_outcome_of_isar_step (!preplay_data))) labels of |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
212 |
NONE => steps |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
213 |
| SOME times0 => |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
214 |
let |
55333
fa079fd40db8
more exception cleanup + more liberal compressions of steps that timed out
blanchet
parents:
55332
diff
changeset
|
215 |
val time_slice = time_mult (1.0 / Real.fromInt (length labels)) (reference_time l) |
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
216 |
val timeouts = |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
217 |
map (adjust_merge_timeout preplay_timeout o curry Time.+ time_slice) times0 |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
218 |
val meths_outcomess = map3 (preplay_isar_step ctxt) timeouts hopelesses succs' |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
219 |
in |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
220 |
(case try (map (fn (_, Played time) :: _ => time)) meths_outcomess of |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
221 |
NONE => steps |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
222 |
| SOME times => |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
223 |
(* candidate successfully eliminated *) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
224 |
(decrement_step_count (); |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
225 |
map3 (fn time => set_preplay_outcomes_of_isar_step ctxt time preplay_data) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
226 |
times succs' meths_outcomess; |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
227 |
map (replace_successor l labels) lfs; |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
228 |
steps_before @ update_steps succs' steps_after)) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
229 |
end) |
54712 | 230 |
end |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
231 |
|
54712 | 232 |
fun compression_loop candidates steps = |
233 |
if not (compress_further ()) then |
|
234 |
steps |
|
235 |
else |
|
55298 | 236 |
(case pop_next_candidate candidates of |
54712 | 237 |
(NONE, _) => steps (* no more candidates for elimination *) |
55332
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
238 |
| (SOME (l, _), candidates') => |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
239 |
(case find_index (curry (op =) (SOME l) o label_of_isar_step) steps of |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
240 |
~1 => steps |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
241 |
| i => |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
242 |
let val successors = get_successors l in |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
243 |
if length successors > compress_degree then steps |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
244 |
else compression_loop candidates' (try_eliminate i l successors steps) |
803a7400cc58
tuned code to avoid noncanonical (and risky) exception handling
blanchet
parents:
55331
diff
changeset
|
245 |
end)) |
55331 | 246 |
|
247 |
fun add_cand (Prove (_, _, l, t, _, _, _, _)) = cons (l, size_of_term t) |
|
248 |
| add_cand _ = I |
|
249 |
||
250 |
(* the very last step is not a candidate *) |
|
251 |
val candidates = fold add_cand (fst (split_last steps)) [] |
|
54712 | 252 |
in |
253 |
compression_loop candidates steps |
|
254 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
255 |
|
55268 | 256 |
(* Proofs are compressed bottom-up, beginning with the innermost subproofs. On the innermost |
257 |
proof level, the proof steps have no subproofs. In the best case, these steps can be merged |
|
258 |
into just one step, resulting in a trivial subproof. Going one level up, trivial subproofs |
|
259 |
can be eliminated. In the best case, this once again leads to a proof whose proof steps do |
|
260 |
not have subproofs. Applying this approach recursively will result in a flat proof in the |
|
261 |
best cast. *) |
|
55263 | 262 |
fun compress_proof (proof as (Proof (fix, assms, steps))) = |
263 |
if compress_further () then Proof (fix, assms, compress_steps steps) else proof |
|
264 |
and compress_steps steps = |
|
54712 | 265 |
(* bottom-up: compress innermost proofs first *) |
55263 | 266 |
steps |> map (fn step => step |> compress_further () ? compress_sub_levels) |
54712 | 267 |
|> compress_further () ? compress_top_level |
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
268 |
and 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
|
269 |
(* compress subproofs *) |
55299 | 270 |
Prove (qs, xs, l, t, map compress_proof subproofs, facts, meths, comment) |
54712 | 271 |
(* eliminate trivial subproofs *) |
272 |
|> compress_further () ? elim_subproofs |
|
55329
3c2dbd2e221f
more generous Isar proof compression -- try to remove failing steps
blanchet
parents:
55328
diff
changeset
|
273 |
| compress_sub_levels step = step |
54712 | 274 |
in |
55263 | 275 |
compress_proof proof |
54712 | 276 |
end |
50259 | 277 |
|
54504 | 278 |
end; |