author | blanchet |
Sun, 02 Feb 2014 20:53:51 +0100 | |
changeset 55252 | 0dc4993b4f56 |
parent 55250 | 982e082cd2ba |
child 55255 | eceebcea3e00 |
permissions | -rw-r--r-- |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_isar_compress.ML |
54813 | 2 |
Author: Steffen Juilf Smolka, TU Muenchen |
50263 | 3 |
Author: Jasmin Blanchette, TU Muenchen |
4 |
||
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
5 |
Compression of Isar proofs by merging steps. |
54712 | 6 |
Only proof steps using the same proof method are merged. |
50263 | 7 |
*) |
8 |
||
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
9 |
signature SLEDGEHAMMER_ISAR_COMPRESS = |
50259 | 10 |
sig |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
11 |
type isar_proof = Sledgehammer_Isar_Proof.isar_proof |
55213 | 12 |
type isar_preplay_data = Sledgehammer_Isar_Preplay.isar_preplay_data |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
13 |
|
55213 | 14 |
val compress_isar_proof : real -> isar_preplay_data -> isar_proof -> isar_proof |
54504 | 15 |
end; |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
16 |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
17 |
structure Sledgehammer_Isar_Compress : SLEDGEHAMMER_ISAR_COMPRESS = |
50259 | 18 |
struct |
19 |
||
50265 | 20 |
open Sledgehammer_Util |
54828 | 21 |
open Sledgehammer_Reconstructor |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
22 |
open Sledgehammer_Isar_Proof |
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
23 |
open Sledgehammer_Isar_Preplay |
50259 | 24 |
|
55212 | 25 |
val dummy_isar_step = Let (Term.dummy, Term.dummy) |
26 |
||
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
27 |
(* traverses steps in post-order and collects the steps with the given labels *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
28 |
fun collect_successors steps lbls = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
29 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
30 |
fun do_steps _ ([], accu) = ([], accu) |
54712 | 31 |
| do_steps [] accum = accum |
32 |
| do_steps (step :: steps) accum = do_steps steps (do_step step accum) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
33 |
and do_step (Let _) x = x |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
34 |
| do_step (step as Prove (_, _, l, _, subproofs, _)) x = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
35 |
(case do_subproofs subproofs x of |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
36 |
([], accu) => ([], accu) |
54712 | 37 |
| accum as (l' :: lbls', accu) => if l = l' then (lbls', step :: accu) else accum) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
38 |
and do_subproofs [] x = x |
54712 | 39 |
| do_subproofs (proof :: subproofs) x = |
40 |
(case do_steps (steps_of_proof proof) x of |
|
41 |
accum as ([], _) => accum |
|
42 |
| accum => do_subproofs subproofs accum) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
43 |
in |
54712 | 44 |
(case do_steps steps (lbls, []) of |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
45 |
([], succs) => rev succs |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
46 |
| _ => raise Fail "Sledgehammer_Isar_Compress: collect_successors") |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
47 |
end |
50259 | 48 |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
49 |
(* traverses steps in reverse post-order and inserts the given updates *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
50 |
fun update_steps steps updates = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
51 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
52 |
fun do_steps [] updates = ([], updates) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
53 |
| do_steps steps [] = (steps, []) |
54712 | 54 |
| do_steps (step :: steps) updates = do_step step (do_steps steps updates) |
55 |
and do_step step (steps, []) = (step :: steps, []) |
|
56 |
| do_step (step as Let _) (steps, updates) = (step :: steps, updates) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
57 |
| do_step (Prove (qs, xs, l, t, subproofs, by)) |
54712 | 58 |
(steps, updates as Prove (qs', xs', l', t', subproofs', by') :: updates') = |
59 |
let |
|
60 |
val (subproofs, updates) = |
|
61 |
if l = l' then do_subproofs subproofs' updates' else do_subproofs subproofs updates |
|
62 |
in |
|
63 |
if l = l' then (Prove (qs', xs', l', t', subproofs, by') :: steps, updates) |
|
64 |
else (Prove (qs, xs, l, t, subproofs, by) :: steps, updates) |
|
65 |
end |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
66 |
| do_step _ _ = raise Fail "Sledgehammer_Isar_Compress: update_steps (invalid update)" |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
67 |
and do_subproofs [] updates = ([], updates) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
68 |
| do_subproofs steps [] = (steps, []) |
54712 | 69 |
| do_subproofs (proof :: subproofs) updates = |
70 |
do_proof proof (do_subproofs subproofs updates) |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
71 |
and do_proof proof (proofs, []) = (proof :: proofs, []) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
72 |
| do_proof (Proof (fix, assms, steps)) (proofs, updates) = |
54712 | 73 |
let val (steps, updates) = do_steps steps updates in |
74 |
(Proof (fix, assms, steps) :: proofs, updates) |
|
75 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
76 |
in |
54712 | 77 |
(case do_steps steps (rev updates) of |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
78 |
(steps, []) => steps |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
79 |
| _ => raise Fail "Sledgehammer_Isar_Compress: update_steps") |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
80 |
end |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
81 |
|
55246 | 82 |
fun try_merge (Prove (_, [], l1, _, [], ((lfs1, gfs1), meths1))) |
83 |
(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
|
84 |
(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
|
85 |
[] => NONE |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
86 |
| meths => |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
87 |
let |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
in |
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
91 |
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
|
92 |
end) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
93 |
| try_merge _ _ = NONE |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50557
diff
changeset
|
94 |
|
53763 | 95 |
val compress_degree = 2 |
55250 | 96 |
val merge_timeout_slack = 1.25 |
53762 | 97 |
|
54712 | 98 |
(* Precondition: The proof must be labeled canonically |
55250 | 99 |
(cf. "Slegehammer_Isar_Proof.relabel_isar_proof_canonically"). *) |
55213 | 100 |
fun compress_isar_proof compress_isar |
55252 | 101 |
({preplay_outcome, set_preplay_outcomes, preplay_quietly, ...} : 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 |
|
55212 | 108 |
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
|
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 |
54712 | 123 |
|> fold_isar_steps add_refs (steps_of_proof proof) |
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 *) |
55252 | 140 |
fun elim_subproofs' time qs fix l t lfs gfs (meths as meth :: meths') subs nontriv_subs = |
54712 | 141 |
if null subs orelse not (compress_further ()) then |
55252 | 142 |
(set_preplay_outcomes l ((meth, Lazy.value (Played time)) :: |
143 |
map (rpair (Lazy.value Not_Played)(*FIXME*)) meths'); |
|
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55243
diff
changeset
|
144 |
Prove (qs, fix, l, t, List.revAppend (nontriv_subs, subs), ((lfs, gfs), meths))) |
54712 | 145 |
else |
146 |
(case subs of |
|
147 |
(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
|
148 |
(let |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
149 |
(* 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
|
150 |
val SOME (Prove (_, [], l', _, [], ((lfs', gfs'), meth' :: _))) = |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
151 |
try the_single sub_steps |
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 |
(* only touch proofs that can be preplayed sucessfully *) |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
154 |
val Played time' = Lazy.force (preplay_outcome l' meth') |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
155 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
156 |
(* merge steps *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
157 |
val subs'' = subs @ nontriv_subs |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
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
|
162 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
163 |
(* 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
|
164 |
val timeout = time_mult merge_timeout_slack (Time.+(time, time')) |
54828 | 165 |
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
|
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'; |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55243
diff
changeset
|
169 |
elim_subproofs' 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
|
170 |
end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
171 |
handle Bind => |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55243
diff
changeset
|
172 |
elim_subproofs' time qs fix l t lfs gfs meths 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 |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
176 |
| 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
|
177 |
((lfs, gfs), meths as meth :: _))) = |
54813 | 178 |
if subproofs = [] then |
179 |
step |
|
180 |
else |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
181 |
(case Lazy.force (preplay_outcome l meth) of |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55243
diff
changeset
|
182 |
Played time => elim_subproofs' time qs fix l t lfs gfs meths subproofs [] |
54826 | 183 |
| _ => step) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
184 |
|
55212 | 185 |
(** top_level compression: eliminate steps by merging them into their successors **) |
54712 | 186 |
fun compress_top_level steps = |
187 |
let |
|
188 |
(* (#successors, (size_of_term t, position)) *) |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
189 |
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
|
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 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
210 |
|> split_last |> fst (* keep last step *) |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
211 |
|> fold_index add_cand) [] |
54712 | 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 |
|
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55243
diff
changeset
|
216 |
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
|
217 |
|
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
218 |
val succs = collect_successors steps' succ_lbls |
55252 | 219 |
val succ_methss = map (snd o the o byline_of_isar_step) succs |
220 |
val succ_meths = map hd succ_methss (* FIXME *) |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
221 |
|
54712 | 222 |
(* only touch steps that can be preplayed successfully *) |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
223 |
val Played time = Lazy.force (preplay_outcome l meth) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
224 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
225 |
val succs' = map (try_merge cand #> the) succs |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
226 |
|
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
227 |
val succ_times = |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
228 |
map2 ((fn Played t => t) o Lazy.force oo preplay_outcome) succ_lbls succ_meths |
54712 | 229 |
val timeslice = time_mult (1.0 / (Real.fromInt (length succ_lbls))) time |
230 |
val timeouts = |
|
231 |
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
|
232 |
|
54712 | 233 |
(* FIXME: debugging *) |
234 |
val _ = |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
235 |
if the (label_of_isar_step cand) <> l then |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55183
diff
changeset
|
236 |
raise Fail "Sledgehammer_Isar_Compress: try_eliminate" |
54712 | 237 |
else |
238 |
() |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
239 |
|
54813 | 240 |
(* TODO: should be lazy: stop preplaying as soon as one step fails/times out *) |
54828 | 241 |
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
|
242 |
|
54712 | 243 |
(* ensure none of the modified successors timed out *) |
55247
4aa3e1c6222c
take intersection rather than union of methods when merging steps -- more efficient and natural
blanchet
parents:
55246
diff
changeset
|
244 |
val true = forall (fn Played _ => true) play_outcomes |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
245 |
|
54712 | 246 |
val (steps1, _ :: steps2) = chop i steps |
247 |
(* replace successors with their modified versions *) |
|
248 |
val steps2 = update_steps steps2 succs' |
|
55252 | 249 |
|
250 |
val succ_meths_outcomess = |
|
251 |
map2 (fn meth :: meths => fn outcome => (meth, Lazy.value outcome) :: |
|
252 |
map (rpair (Lazy.value Not_Played)(*FIXME*)) meths) succ_methss play_outcomes |
|
54712 | 253 |
in |
254 |
decrement_step_count (); (* candidate successfully eliminated *) |
|
55252 | 255 |
map2 set_preplay_outcomes succ_lbls succ_meths_outcomess; |
54712 | 256 |
map (replace_successor l succ_lbls) lfs; |
55243 | 257 |
(* removing the step would mess up the indices; replace with dummy step instead *) |
54712 | 258 |
steps1 @ dummy_isar_step :: steps2 |
259 |
end |
|
260 |
handle Bind => steps |
|
261 |
| Match => steps |
|
262 |
| Option.Option => steps |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
263 |
|
54712 | 264 |
fun compression_loop candidates steps = |
265 |
if not (compress_further ()) then |
|
266 |
steps |
|
267 |
else |
|
268 |
(case pop_next_cand candidates of |
|
269 |
(NONE, _) => steps (* no more candidates for elimination *) |
|
270 |
| (SOME (cand as (_, l, _)), candidates) => |
|
271 |
let val successors = get_successors l in |
|
272 |
if length successors > compress_degree then steps |
|
273 |
else compression_loop candidates (try_eliminate cand successors steps) |
|
274 |
end) |
|
275 |
in |
|
276 |
compression_loop candidates steps |
|
277 |
|> remove (op =) dummy_isar_step |
|
278 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
279 |
|
54712 | 280 |
(** recusion over the proof tree **) |
281 |
(* |
|
282 |
Proofs are compressed bottom-up, beginning with the innermost |
|
283 |
subproofs. |
|
284 |
On the innermost proof level, the proof steps have no subproofs. |
|
285 |
In the best case, these steps can be merged into just one step, |
|
286 |
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 |
|
288 |
whose proof steps do not have subproofs. Applying this approach |
|
289 |
recursively will result in a flat proof in the best cast. |
|
290 |
*) |
|
291 |
fun do_proof (proof as (Proof (fix, assms, steps))) = |
|
292 |
if compress_further () then Proof (fix, assms, do_steps steps) else proof |
|
293 |
and do_steps steps = |
|
294 |
(* bottom-up: compress innermost proofs first *) |
|
295 |
steps |> map (fn step => step |> compress_further () ? do_sub_levels) |
|
296 |
|> compress_further () ? compress_top_level |
|
297 |
and do_sub_levels (Let x) = Let x |
|
298 |
| 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
|
299 |
(* compress subproofs *) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
300 |
Prove (qs, xs, l, t, map do_proof subproofs, by) |
54712 | 301 |
(* eliminate trivial subproofs *) |
302 |
|> compress_further () ? elim_subproofs |
|
303 |
in |
|
304 |
do_proof proof |
|
305 |
end |
|
50259 | 306 |
|
54504 | 307 |
end; |