1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_minimize_isar.ML |
1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_minimize_isar.ML |
|
2 Author: Steffen Juilf Smolka, TU Muenchen |
2 Author: Jasmin Blanchette, TU Muenchen |
3 Author: Jasmin Blanchette, TU Muenchen |
3 Author: Steffen Juilf Smolka, TU Muenchen |
|
4 |
4 |
5 Minimize dependencies (used facts) of Isar proof steps. |
5 Minimize dependencies (used facts) of Isar proof steps. |
6 *) |
6 *) |
7 |
7 |
8 signature SLEDGEHAMMER_MINIMIZE_ISAR = |
8 signature SLEDGEHAMMER_MINIMIZE_ISAR = |
9 sig |
9 sig |
10 type preplay_interface = Sledgehammer_Preplay.preplay_interface |
10 type preplay_interface = Sledgehammer_Preplay.preplay_interface |
|
11 type isar_step = Sledgehammer_Proof.isar_step |
11 type isar_proof = Sledgehammer_Proof.isar_proof |
12 type isar_proof = Sledgehammer_Proof.isar_proof |
12 val minimize_dependencies_and_remove_unrefed_steps : |
13 |
13 bool -> preplay_interface -> isar_proof -> isar_proof |
14 val min_deps_of_step : preplay_interface -> isar_step -> isar_step |
|
15 val postprocess_remove_unreferenced_steps : (isar_step -> isar_step) -> isar_proof -> isar_proof |
14 end; |
16 end; |
15 |
17 |
16 structure Sledgehammer_Minimize_Isar : SLEDGEHAMMER_MINIMIZE_ISAR = |
18 structure Sledgehammer_Minimize_Isar : SLEDGEHAMMER_MINIMIZE_ISAR = |
17 struct |
19 struct |
18 |
20 |
19 open Sledgehammer_Util |
21 open Sledgehammer_Util |
20 open Sledgehammer_Proof |
22 open Sledgehammer_Proof |
21 open Sledgehammer_Preplay |
23 open Sledgehammer_Preplay |
22 |
24 |
23 val slack = 1.3 |
25 val slack = seconds 0.1 |
24 |
26 |
25 fun min_deps_of_step (_ : preplay_interface) (step as Let _) = step |
27 fun min_deps_of_step (_ : preplay_interface) (step as Let _) = step |
26 | min_deps_of_step {get_preplay_time, set_preplay_time, preplay_quietly, ...} |
28 | min_deps_of_step {get_preplay_time, set_preplay_time, preplay_quietly, ...} |
27 (step as Prove (qs, xs, l, t, subproofs, ((lfs, gfs), method))) = |
29 (step as Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meth))) = |
28 (case get_preplay_time l of |
30 (case get_preplay_time l of |
29 (* don't touch steps that time out or fail; minimizing won't help *) |
31 (* don't touch steps that time out or fail; minimizing won't help (not true -- FIXME) *) |
30 (true, _) => step |
32 (true, _) => step |
31 | (false, time) => |
33 | (false, time) => |
32 let |
34 let |
33 fun mk_step_lfs_gfs lfs gfs = |
35 fun mk_step_lfs_gfs lfs gfs = Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meth)) |
34 Prove (qs, xs, l, t, subproofs, ((lfs, gfs), method)) |
36 val mk_step_gfs_lfs = curry (swap #> uncurry mk_step_lfs_gfs) |
35 val mk_step_gfs_lfs = curry (swap #> uncurry mk_step_lfs_gfs) |
|
36 |
37 |
37 fun minimize_facts _ time min_facts [] = (time, min_facts) |
38 fun minimize_facts _ time min_facts [] = (time, min_facts) |
38 | minimize_facts mk_step time min_facts (f :: facts) = |
39 | minimize_facts mk_step time min_facts (f :: facts) = |
39 (case preplay_quietly (time_mult slack time) |
40 (case preplay_quietly (Time.+ (time, slack)) (mk_step (min_facts @ facts)) of |
40 (mk_step (min_facts @ facts)) of |
41 (true, _) => minimize_facts mk_step time (f :: min_facts) facts |
41 (true, _) => minimize_facts mk_step time (f :: min_facts) facts |
42 | (false, time) => minimize_facts mk_step time min_facts facts) |
42 | (false, time) => minimize_facts mk_step time min_facts facts) |
|
43 |
43 |
44 val (time, min_lfs) = minimize_facts (mk_step_gfs_lfs gfs) time [] lfs |
44 val (time, min_lfs) = minimize_facts (mk_step_gfs_lfs gfs) time [] lfs |
45 val (time, min_gfs) = minimize_facts (mk_step_lfs_gfs min_lfs) time [] gfs |
45 val (time, min_gfs) = minimize_facts (mk_step_lfs_gfs min_lfs) time [] gfs |
|
46 in |
|
47 set_preplay_time l (false, time); mk_step_lfs_gfs min_lfs min_gfs |
|
48 end) |
46 |
49 |
47 in |
50 fun postprocess_remove_unreferenced_steps postproc_step = |
48 set_preplay_time l (false, time); |
|
49 mk_step_lfs_gfs min_lfs min_gfs |
|
50 end) |
|
51 |
|
52 fun minimize_dependencies_and_remove_unrefed_steps isar_try0 preplay_interface |
|
53 proof = |
|
54 let |
51 let |
55 fun cons_to xs x = x :: xs |
|
56 |
|
57 val add_lfs = fold (Ord_List.insert label_ord) |
52 val add_lfs = fold (Ord_List.insert label_ord) |
58 |
53 |
59 fun do_proof (Proof (fix, assms, steps)) = |
54 fun do_proof (Proof (fix, assms, steps)) = |
60 let |
55 let val (refed, steps) = do_steps steps in |
61 val (refed, steps) = do_steps steps |
|
62 in |
|
63 (refed, Proof (fix, assms, steps)) |
56 (refed, Proof (fix, assms, steps)) |
64 end |
57 end |
65 |
|
66 and do_steps steps = |
58 and do_steps steps = |
67 let |
59 let |
68 (* the last step is always implicitly referenced *) |
60 (* the last step is always implicitly referenced *) |
69 val (steps, (refed, concl)) = |
61 val (steps, (refed, concl)) = |
70 split_last steps |
62 split_last steps |
71 ||> do_refed_step |
63 ||> do_refed_step |
72 in |
64 in |
73 fold_rev do_step steps (refed, [concl]) |
65 fold_rev do_step steps (refed, [concl]) |
74 end |
66 end |
75 |
|
76 and do_step step (refed, accu) = |
67 and do_step step (refed, accu) = |
77 case label_of_step step of |
68 case label_of_step step of |
78 NONE => (refed, step :: accu) |
69 NONE => (refed, step :: accu) |
79 | SOME l => |
70 | SOME l => |
80 if Ord_List.member label_ord refed l then |
71 if Ord_List.member label_ord refed l then |
81 do_refed_step step |
72 do_refed_step step |
82 |>> Ord_List.union label_ord refed |
73 |>> Ord_List.union label_ord refed |
83 ||> cons_to accu |
74 ||> (fn x => x :: accu) |
84 else |
75 else |
85 (refed, accu) |
76 (refed, accu) |
86 |
77 and do_refed_step step = step |> postproc_step |> do_refed_step' |
87 and do_refed_step step = |
|
88 step |
|
89 |> isar_try0 ? min_deps_of_step preplay_interface |
|
90 |> do_refed_step' |
|
91 |
|
92 and do_refed_step' (Let _) = raise Fail "Sledgehammer_Minimize_Isar" |
78 and do_refed_step' (Let _) = raise Fail "Sledgehammer_Minimize_Isar" |
93 | do_refed_step' (Prove (qs, xs, l, t, subproofs, ((lfs, gfs), m))) = |
79 | do_refed_step' (Prove (qs, xs, l, t, subproofs, ((lfs, gfs), m))) = |
94 let |
80 let |
95 val (refed, subproofs) = |
81 val (refed, subproofs) = |
96 map do_proof subproofs |
82 map do_proof subproofs |