1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_isar.ML
2 Author: Jasmin Blanchette, TU Muenchen
4 Adds "sledgehammer" and related commands to Isabelle/Isar's outer syntax.
7 signature SLEDGEHAMMER_ISAR =
9 type params = Sledgehammer_Provers.params
11 val auto : bool Unsynchronized.ref
12 val provers : string Unsynchronized.ref
13 val timeout : int Unsynchronized.ref
14 val default_params : Proof.context -> (string * string) list -> params
15 val setup : theory -> theory
18 structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
23 open ATP_Problem_Generate
24 open ATP_Proof_Reconstruct
25 open Sledgehammer_Util
26 open Sledgehammer_Fact
27 open Sledgehammer_Provers
28 open Sledgehammer_Minimize
29 open Sledgehammer_MaSh
34 val messagesN = "messages"
35 val supported_proversN = "supported_provers"
36 val kill_proversN = "kill_provers"
37 val running_proversN = "running_provers"
38 val kill_learnersN = "kill_learners"
39 val running_learnersN = "running_learners"
40 val unlearnN = "unlearn"
41 val refresh_tptpN = "refresh_tptp"
43 val auto = Unsynchronized.ref false
46 ProofGeneralPgip.add_preference Preferences.category_tracing
47 (Preferences.bool_pref auto "auto-sledgehammer"
48 "Run Sledgehammer automatically.")
50 (** Sledgehammer commands **)
52 fun add_fact_override ns : fact_override = {add = ns, del = [], only = false}
53 fun del_fact_override ns : fact_override = {add = [], del = ns, only = false}
54 fun only_fact_override ns : fact_override = {add = ns, del = [], only = true}
55 fun merge_fact_override_pairwise (r1 : fact_override) (r2 : fact_override) =
56 {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
57 only = #only r1 andalso #only r2}
58 fun merge_fact_overrides rs =
59 fold merge_fact_override_pairwise rs (only_fact_override [])
63 val provers = Unsynchronized.ref ""
64 val timeout = Unsynchronized.ref 30
67 ProofGeneralPgip.add_preference Preferences.category_proof
68 (Preferences.string_pref provers
69 "Sledgehammer: Provers"
70 "Default automatic provers (separated by whitespace)")
73 ProofGeneralPgip.add_preference Preferences.category_proof
74 (Preferences.int_pref timeout
75 "Sledgehammer: Time Limit"
76 "ATPs will be interrupted after this time (in seconds)")
78 type raw_param = string * string list
80 val default_default_params =
83 ("overlord", "false"),
84 ("blocking", "false"),
85 ("type_enc", "smart"),
87 ("lam_trans", "smart"),
88 ("uncurried_aliases", "smart"),
90 ("fact_filter", "smart"),
91 ("max_facts", "smart"),
92 ("fact_thresholds", "0.45 0.85"),
93 ("max_mono_iters", "smart"),
94 ("max_new_mono_instances", "smart"),
95 ("isar_proof", "false"),
96 ("isar_shrink_factor", "1"),
98 ("minimize", "smart"),
99 ("preplay_timeout", "3")]
102 [("prover", ("provers", [])), (* legacy *)
103 ("max_relevant", ("max_facts", [])), (* legacy *)
104 ("dont_preplay", ("preplay_timeout", ["0"]))]
105 val negated_alias_params =
106 [("no_debug", "debug"),
107 ("quiet", "verbose"),
108 ("no_overlord", "overlord"),
109 ("non_blocking", "blocking"),
110 ("non_strict", "strict"),
111 ("no_uncurried_aliases", "uncurried_aliases"),
112 ("dont_learn", "learn"),
113 ("no_isar_proof", "isar_proof"),
114 ("dont_slice", "slice"),
115 ("dont_minimize", "minimize")]
117 val params_for_minimize =
118 ["debug", "verbose", "overlord", "type_enc", "strict", "lam_trans",
119 "uncurried_aliases", "max_mono_iters", "max_new_mono_instances",
120 "isar_proof", "isar_shrink_factor", "timeout", "preplay_timeout"]
122 val property_dependent_params = ["provers", "timeout"]
124 fun is_known_raw_param s =
125 AList.defined (op =) default_default_params s orelse
126 AList.defined (op =) alias_params s orelse
127 AList.defined (op =) negated_alias_params s orelse
128 member (op =) property_dependent_params s orelse s = "expect"
130 fun is_prover_list ctxt s =
131 case space_explode " " s of
132 ss as _ :: _ => forall (is_prover_supported ctxt) ss
135 fun unalias_raw_param (name, value) =
136 case AList.lookup (op =) alias_params name of
137 SOME (name', []) => (name', value)
138 | SOME (param' as (name', _)) =>
139 if value <> ["false"] then
142 error ("Parameter " ^ quote name ^ " cannot be set to \"false\" \
143 \(cf. " ^ quote name' ^ ").")
145 case AList.lookup (op =) negated_alias_params name of
146 SOME name' => (name', case value of
147 ["false"] => ["true"]
148 | ["true"] => ["false"]
151 | NONE => (name, value)
153 val any_type_enc = type_enc_from_string Strict "erased"
155 (* "provers =", "type_enc =", "lam_trans =", and "fact_filter =" can be omitted.
156 For the last three, this is a secret feature. *)
157 fun normalize_raw_param ctxt =
159 #> (fn (name, value) =>
160 if is_known_raw_param name then
162 else if is_prover_list ctxt name andalso null value then
164 else if can (type_enc_from_string Strict) name andalso null value then
166 else if can (trans_lams_from_string ctxt any_type_enc) name andalso
168 ("lam_trans", [name])
169 else if member (op =) fact_filters name then
170 ("fact_filter", [name])
172 error ("Unknown parameter: " ^ quote name ^ "."))
175 (* Ensures that type encodings such as "mono_native?" and "poly_guards!!" are
177 val implode_param = strip_spaces_except_between_idents o space_implode " "
179 structure Data = Theory_Data
181 type T = raw_param list
182 val empty = default_default_params |> map (apsnd single)
184 fun merge data : T = AList.merge (op =) (K true) data
187 fun remotify_prover_if_supported_and_not_already_remote ctxt name =
188 if String.isPrefix remote_prefix name then
191 let val remote_name = remote_prefix ^ name in
192 if is_prover_supported ctxt remote_name then SOME remote_name else NONE
195 fun remotify_prover_if_not_installed ctxt name =
196 if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
199 remotify_prover_if_supported_and_not_already_remote ctxt name
201 fun avoid_too_many_threads _ _ [] = []
202 | avoid_too_many_threads _ (0, 0) _ = []
203 | avoid_too_many_threads ctxt (0, max_remote) provers =
205 |> map_filter (remotify_prover_if_supported_and_not_already_remote ctxt)
207 | avoid_too_many_threads _ (max_local, 0) provers =
209 |> filter_out (String.isPrefix remote_prefix)
211 | avoid_too_many_threads ctxt max_local_and_remote (prover :: provers) =
213 val max_local_and_remote =
215 |> (if String.isPrefix remote_prefix prover then apsnd else apfst)
217 in prover :: avoid_too_many_threads ctxt max_local_and_remote provers end
219 val max_default_remote_threads = 4
221 (* The first ATP of the list is used by Auto Sledgehammer. Because of the low
222 timeout, it makes sense to put SPASS first. *)
223 fun default_provers_param_value ctxt =
224 [spassN, eN, vampireN, SMT_Solver.solver_name_of ctxt, e_sineN,
226 |> map_filter (remotify_prover_if_not_installed ctxt)
227 |> avoid_too_many_threads ctxt (Multithreading.max_threads_value (),
228 max_default_remote_threads)
231 fun set_default_raw_param param thy =
232 let val ctxt = Proof_Context.init_global thy in
233 thy |> Data.map (AList.update (op =) (normalize_raw_param ctxt param))
235 fun default_raw_params ctxt =
236 let val thy = Proof_Context.theory_of ctxt in
238 |> fold (AList.default (op =))
239 [("provers", [case !provers of
240 "" => default_provers_param_value ctxt
242 ("timeout", let val timeout = !timeout in
243 [if timeout <= 0 then "none"
244 else string_of_int timeout]
248 val infinity_time_in_secs = 60 * 60 * 24 * 365
249 val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
251 fun extract_params mode default_params override_params =
253 val raw_params = rev override_params @ rev default_params
254 val lookup = Option.map implode_param o AList.lookup (op =) raw_params
255 val lookup_string = the_default "" o lookup
256 fun general_lookup_bool option default_value name =
258 SOME s => parse_bool_option option name s
259 | NONE => default_value
260 val lookup_bool = the o general_lookup_bool false (SOME false)
261 fun lookup_time name =
263 SOME s => parse_time_option name s
265 fun lookup_int name =
268 | SOME s => case Int.fromString s of
270 | NONE => error ("Parameter " ^ quote name ^
271 " must be assigned an integer value.")
272 fun lookup_real_pair name =
275 | SOME s => case s |> space_explode " " |> map Real.fromString of
276 [SOME r1, SOME r2] => (r1, r2)
277 | _ => error ("Parameter " ^ quote name ^
278 "must be assigned a pair of floating-point \
279 \values (e.g., \"0.6 0.95\")")
280 fun lookup_option lookup' name =
283 | _ => SOME (lookup' name)
284 val debug = mode <> Auto_Try andalso lookup_bool "debug"
285 val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose")
286 val overlord = lookup_bool "overlord"
288 Isabelle_Process.is_active () orelse mode <> Normal orelse debug orelse
289 lookup_bool "blocking"
290 val provers = lookup_string "provers" |> space_explode " "
291 |> mode = Auto_Try ? single o hd
293 if mode = Auto_Try then
295 else case lookup_string "type_enc" of
297 | s => (type_enc_from_string Strict s; SOME s)
298 val strict = mode = Auto_Try orelse lookup_bool "strict"
299 val lam_trans = lookup_option lookup_string "lam_trans"
300 val uncurried_aliases = lookup_option lookup_bool "uncurried_aliases"
301 val learn = lookup_bool "learn"
302 val fact_filter = lookup_option lookup_string "fact_filter"
303 val max_facts = lookup_option lookup_int "max_facts"
304 val fact_thresholds = lookup_real_pair "fact_thresholds"
305 val max_mono_iters = lookup_option lookup_int "max_mono_iters"
306 val max_new_mono_instances =
307 lookup_option lookup_int "max_new_mono_instances"
308 val isar_proof = lookup_bool "isar_proof"
309 val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
310 val slice = mode <> Auto_Try andalso lookup_bool "slice"
312 if mode = Auto_Try then NONE else lookup_option lookup_bool "minimize"
314 (if mode = Auto_Try then NONE else lookup_time "timeout") |> the_timeout
315 val preplay_timeout =
316 if mode = Auto_Try then Time.zeroTime
317 else lookup_time "preplay_timeout" |> the_timeout
318 val expect = lookup_string "expect"
320 {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
321 provers = provers, type_enc = type_enc, strict = strict,
322 lam_trans = lam_trans, uncurried_aliases = uncurried_aliases,
323 learn = learn, fact_filter = fact_filter, max_facts = max_facts,
324 fact_thresholds = fact_thresholds, max_mono_iters = max_mono_iters,
325 max_new_mono_instances = max_new_mono_instances, isar_proof = isar_proof,
326 isar_shrink_factor = isar_shrink_factor, slice = slice,
327 minimize = minimize, timeout = timeout, preplay_timeout = preplay_timeout,
331 fun get_params mode = extract_params mode o default_raw_params
332 fun default_params thy = get_params Normal thy o map (apsnd single)
334 (* Sledgehammer the given subgoal *)
336 val default_minimize_prover = metisN
338 fun is_raw_param_relevant_for_minimize (name, _) =
339 member (op =) params_for_minimize name
340 fun string_for_raw_param (key, values) =
341 key ^ (case implode_param values of "" => "" | value => " = " ^ value)
343 fun minimize_command override_params i more_override_params prover_name
348 |> filter_out (AList.defined (op =) more_override_params o fst)) @
350 |> filter is_raw_param_relevant_for_minimize
351 |> map string_for_raw_param
352 |> (if prover_name = default_minimize_prover then I else cons prover_name)
353 |> space_implode ", "
355 "sledgehammer" ^ " " ^ minN ^
356 (if params = "" then "" else enclose " [" "]" params) ^
357 " (" ^ space_implode " " fact_names ^ ")" ^
358 (if i = 1 then "" else " " ^ string_of_int i)
361 fun hammer_away override_params subcommand opt_i fact_override state =
363 val ctxt = Proof.context_of state
364 val override_params = override_params |> map (normalize_raw_param ctxt)
366 if subcommand = runN then
367 let val i = the_default 1 opt_i in
368 run_sledgehammer (get_params Normal ctxt override_params) Normal i
369 fact_override (minimize_command override_params i)
373 else if subcommand = minN then
374 run_minimize (get_params Minimize ctxt
375 (("provers", [default_minimize_prover]) ::
377 (K ()) (the_default 1 opt_i) (#add fact_override) state
378 else if subcommand = messagesN then
380 else if subcommand = supported_proversN then
381 supported_provers ctxt
382 else if subcommand = kill_proversN then
384 else if subcommand = running_proversN then
386 else if subcommand = kill_learnersN then
388 else if subcommand = running_learnersN then
390 else if subcommand = unlearnN then
392 else if subcommand = refresh_tptpN then
393 refresh_systems_on_tptp ()
395 error ("Unknown subcommand: " ^ quote subcommand ^ ".")
398 fun sledgehammer_trans (((subcommand, params), fact_override), opt_i) =
399 Toplevel.keep (hammer_away params subcommand opt_i fact_override
402 fun string_for_raw_param (name, value) = name ^ " = " ^ implode_param value
404 fun sledgehammer_params_trans params =
406 (fold set_default_raw_param params
408 let val ctxt = Proof_Context.init_global thy in
409 writeln ("Default parameters for Sledgehammer:\n" ^
410 (case default_raw_params ctxt |> rev of
413 params |> map string_for_raw_param
414 |> sort_strings |> cat_lines))
417 val parse_query_bang = @{keyword "?"} || @{keyword "!"} || @{keyword "!!"}
419 Scan.repeat1 (Parse.typ_group || parse_query_bang) >> implode_param
421 Scan.repeat1 (Parse.xname || Parse.float_number || parse_query_bang)
422 val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) []
423 val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
424 val parse_fact_refs =
425 Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
426 val parse_fact_override_chunk =
427 (Args.add |-- Args.colon |-- parse_fact_refs >> add_fact_override)
428 || (Args.del |-- Args.colon |-- parse_fact_refs >> del_fact_override)
429 || (parse_fact_refs >> only_fact_override)
430 val parse_fact_override =
431 Scan.optional (Args.parens (Scan.repeat parse_fact_override_chunk
432 >> merge_fact_overrides))
436 Outer_Syntax.improper_command @{command_spec "sledgehammer"}
437 "search for first-order proof using automatic theorem provers"
438 ((Scan.optional Parse.short_ident runN -- parse_params
439 -- parse_fact_override -- Scan.option Parse.nat) #>> sledgehammer_trans)
441 Outer_Syntax.command @{command_spec "sledgehammer_params"}
442 "set and display the default parameters for Sledgehammer"
443 (parse_params #>> sledgehammer_params_trans)
445 fun try_sledgehammer auto state =
447 val ctxt = Proof.context_of state
448 val mode = if auto then Auto_Try else Try
451 run_sledgehammer (get_params mode ctxt []) mode i no_fact_override
452 (minimize_command [] i) state
455 val setup = Try.register_tool ("sledgehammer", (40, auto, try_sledgehammer))