|
1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_fact_minimize.ML |
|
2 Author: Philipp Meyer, TU Muenchen |
|
3 Author: Jasmin Blanchette, TU Muenchen |
|
4 |
|
5 Minimization of theorem list for Metis using automatic theorem provers. |
|
6 *) |
|
7 |
|
8 signature SLEDGEHAMMER_FACT_MINIMIZE = |
|
9 sig |
|
10 type locality = Sledgehammer_Fact_Filter.locality |
|
11 type params = Sledgehammer.params |
|
12 |
|
13 val minimize_theorems : |
|
14 params -> int -> int -> Proof.state -> ((string * locality) * thm list) list |
|
15 -> ((string * locality) * thm list) list option * string |
|
16 val run_minimize : params -> int -> Facts.ref list -> Proof.state -> unit |
|
17 end; |
|
18 |
|
19 structure Sledgehammer_Fact_Minimize : SLEDGEHAMMER_FACT_MINIMIZE = |
|
20 struct |
|
21 |
|
22 open ATP_Systems |
|
23 open Sledgehammer_Util |
|
24 open Sledgehammer_Fact_Filter |
|
25 open Sledgehammer_Proof_Reconstruct |
|
26 open Sledgehammer |
|
27 |
|
28 (* wrapper for calling external prover *) |
|
29 |
|
30 fun string_for_failure Unprovable = "Unprovable." |
|
31 | string_for_failure TimedOut = "Timed out." |
|
32 | string_for_failure _ = "Unknown error." |
|
33 |
|
34 fun n_theorems names = |
|
35 let val n = length names in |
|
36 string_of_int n ^ " theorem" ^ plural_s n ^ |
|
37 (if n > 0 then |
|
38 ": " ^ (names |> map fst |
|
39 |> sort_distinct string_ord |> space_implode " ") |
|
40 else |
|
41 "") |
|
42 end |
|
43 |
|
44 fun test_theorems ({debug, verbose, overlord, atps, full_types, isar_proof, |
|
45 isar_shrink_factor, ...} : params) |
|
46 (prover : prover) explicit_apply timeout subgoal state |
|
47 axioms = |
|
48 let |
|
49 val _ = |
|
50 priority ("Testing " ^ n_theorems (map fst axioms) ^ "...") |
|
51 val params = |
|
52 {blocking = true, debug = debug, verbose = verbose, overlord = overlord, |
|
53 atps = atps, full_types = full_types, explicit_apply = explicit_apply, |
|
54 relevance_thresholds = (1.01, 1.01), max_relevant = NONE, |
|
55 theory_relevant = NONE, isar_proof = isar_proof, |
|
56 isar_shrink_factor = isar_shrink_factor, timeout = timeout, expect = ""} |
|
57 val axioms = maps (fn (n, ths) => map (pair n) ths) axioms |
|
58 val {context = ctxt, facts, goal} = Proof.goal state |
|
59 val problem = |
|
60 {subgoal = subgoal, goal = (ctxt, (facts, goal)), |
|
61 relevance_override = {add = [], del = [], only = false}, |
|
62 axioms = SOME axioms} |
|
63 val result as {outcome, used_thm_names, ...} = prover params (K "") problem |
|
64 in |
|
65 priority (case outcome of |
|
66 NONE => |
|
67 if length used_thm_names = length axioms then |
|
68 "Found proof." |
|
69 else |
|
70 "Found proof with " ^ n_theorems used_thm_names ^ "." |
|
71 | SOME failure => string_for_failure failure); |
|
72 result |
|
73 end |
|
74 |
|
75 (* minimalization of thms *) |
|
76 |
|
77 fun filter_used_facts used = filter (member (op =) used o fst) |
|
78 |
|
79 fun sublinear_minimize _ [] p = p |
|
80 | sublinear_minimize test (x :: xs) (seen, result) = |
|
81 case test (xs @ seen) of |
|
82 result as {outcome = NONE, proof, used_thm_names, ...} : prover_result => |
|
83 sublinear_minimize test (filter_used_facts used_thm_names xs) |
|
84 (filter_used_facts used_thm_names seen, result) |
|
85 | _ => sublinear_minimize test xs (x :: seen, result) |
|
86 |
|
87 (* Give the ATP some slack. The ATP gets further slack because the Sledgehammer |
|
88 preprocessing time is included in the estimate below but isn't part of the |
|
89 timeout. *) |
|
90 val fudge_msecs = 1000 |
|
91 |
|
92 fun minimize_theorems {atps = [], ...} _ _ _ _ = error "No ATP is set." |
|
93 | minimize_theorems (params as {debug, atps = atp :: _, full_types, |
|
94 isar_proof, isar_shrink_factor, timeout, ...}) |
|
95 i n state axioms = |
|
96 let |
|
97 val thy = Proof.theory_of state |
|
98 val prover = get_prover_fun thy atp |
|
99 val msecs = Time.toMilliseconds timeout |
|
100 val _ = priority ("Sledgehammer minimize: ATP " ^ quote atp ^ ".") |
|
101 val {context = ctxt, goal, ...} = Proof.goal state |
|
102 val (_, hyp_ts, concl_t) = strip_subgoal goal i |
|
103 val explicit_apply = |
|
104 not (forall (Meson.is_fol_term thy) |
|
105 (concl_t :: hyp_ts @ maps (map prop_of o snd) axioms)) |
|
106 fun do_test timeout = |
|
107 test_theorems params prover explicit_apply timeout i state |
|
108 val timer = Timer.startRealTimer () |
|
109 in |
|
110 (case do_test timeout axioms of |
|
111 result as {outcome = NONE, pool, used_thm_names, |
|
112 conjecture_shape, ...} => |
|
113 let |
|
114 val time = Timer.checkRealTimer timer |
|
115 val new_timeout = |
|
116 Int.min (msecs, Time.toMilliseconds time + fudge_msecs) |
|
117 |> Time.fromMilliseconds |
|
118 val (min_thms, {proof, axiom_names, ...}) = |
|
119 sublinear_minimize (do_test new_timeout) |
|
120 (filter_used_facts used_thm_names axioms) ([], result) |
|
121 val n = length min_thms |
|
122 val _ = priority (cat_lines |
|
123 ["Minimized: " ^ string_of_int n ^ " theorem" ^ plural_s n] ^ |
|
124 (case length (filter (curry (op =) Chained o snd o fst) min_thms) of |
|
125 0 => "" |
|
126 | n => " (including " ^ Int.toString n ^ " chained)") ^ ".") |
|
127 in |
|
128 (SOME min_thms, |
|
129 proof_text isar_proof |
|
130 (pool, debug, isar_shrink_factor, ctxt, conjecture_shape) |
|
131 (full_types, K "", proof, axiom_names, goal, i) |> fst) |
|
132 end |
|
133 | {outcome = SOME TimedOut, ...} => |
|
134 (NONE, "Timeout: You can increase the time limit using the \"timeout\" \ |
|
135 \option (e.g., \"timeout = " ^ |
|
136 string_of_int (10 + msecs div 1000) ^ " s\").") |
|
137 | {outcome = SOME UnknownError, ...} => |
|
138 (* Failure sometimes mean timeout, unfortunately. *) |
|
139 (NONE, "Failure: No proof was found with the current time limit. You \ |
|
140 \can increase the time limit using the \"timeout\" \ |
|
141 \option (e.g., \"timeout = " ^ |
|
142 string_of_int (10 + msecs div 1000) ^ " s\").") |
|
143 | {message, ...} => (NONE, "ATP error: " ^ message)) |
|
144 handle ERROR msg => (NONE, "Error: " ^ msg) |
|
145 end |
|
146 |
|
147 fun run_minimize params i refs state = |
|
148 let |
|
149 val ctxt = Proof.context_of state |
|
150 val reserved = reserved_isar_keyword_table () |
|
151 val chained_ths = #facts (Proof.goal state) |
|
152 val axioms = |
|
153 maps (map (apsnd single) |
|
154 o name_thm_pairs_from_ref ctxt reserved chained_ths) refs |
|
155 in |
|
156 case subgoal_count state of |
|
157 0 => priority "No subgoal!" |
|
158 | n => |
|
159 (kill_atps (); priority (#2 (minimize_theorems params i n state axioms))) |
|
160 end |
|
161 |
|
162 end; |