1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_fact_minimize.ML
2 Author: Philipp Meyer, TU Muenchen
3 Author: Jasmin Blanchette, TU Muenchen
5 Minimization of theorem list for Metis using automatic theorem provers.
8 signature SLEDGEHAMMER_FACT_MINIMIZE =
10 type locality = Sledgehammer_Fact_Filter.locality
11 type params = Sledgehammer.params
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
19 structure Sledgehammer_Fact_Minimize : SLEDGEHAMMER_FACT_MINIMIZE =
23 open Sledgehammer_Util
24 open Sledgehammer_Fact_Filter
25 open Sledgehammer_Proof_Reconstruct
28 (* wrapper for calling external prover *)
30 fun string_for_failure Unprovable = "Unprovable."
31 | string_for_failure TimedOut = "Timed out."
32 | string_for_failure _ = "Unknown error."
34 fun n_theorems names =
35 let val n = length names in
36 string_of_int n ^ " theorem" ^ plural_s n ^
38 ": " ^ (names |> map fst
39 |> sort_distinct string_ord |> space_implode " ")
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
50 priority ("Testing " ^ n_theorems (map fst axioms) ^ "...")
52 {debug = debug, verbose = verbose, overlord = overlord, atps = atps,
53 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}
57 val axioms = maps (fn (n, ths) => map (pair n) ths) axioms
58 val {context = ctxt, facts, goal} = Proof.goal state
60 {subgoal = subgoal, goal = (ctxt, (facts, goal)),
61 relevance_override = {add = [], del = [], only = false},
63 val result as {outcome, used_thm_names, ...} = prover params (K "") problem
65 priority (case outcome of
67 if length used_thm_names = length axioms then
70 "Found proof with " ^ n_theorems used_thm_names ^ "."
71 | SOME failure => string_for_failure failure);
75 (* minimalization of thms *)
77 fun filter_used_facts used = filter (member (op =) used o fst)
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)
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
90 val fudge_msecs = 1000
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, ...})
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
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 ()
110 (case do_test timeout axioms of
111 result as {outcome = NONE, pool, used_thm_names,
112 conjecture_shape, ...} =>
114 val time = Timer.checkRealTimer timer
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
126 | n => " (including " ^ Int.toString n ^ " chained)") ^ ".")
129 proof_text isar_proof
130 (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
131 (full_types, K "", proof, axiom_names, goal, i) |> fst)
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)
147 fun run_minimize params i refs state =
149 val ctxt = Proof.context_of state
150 val reserved = reserved_isar_keyword_table ()
151 val chained_ths = #facts (Proof.goal state)
153 maps (map (apsnd single)
154 o name_thm_pairs_from_ref ctxt reserved chained_ths) refs
156 case subgoal_count state of
157 0 => priority "No subgoal!"
159 (kill_atps (); priority (#2 (minimize_theorems params i n state axioms)))