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_Filter_MaSh
34 val messagesN = "messages"
35 val supported_proversN = "supported_provers"
36 val running_proversN = "running_provers"
37 val kill_proversN = "kill_provers"
38 val refresh_tptpN = "refresh_tptp"
39 val mash_resetN = "mash_reset"
40 val mash_learnN = "mash_learn"
43 val mash_RESET_N = "mash_RESET"
44 val mash_ADD_N = "mash_ADD"
45 val mash_DEL_N = "mash_DEL"
46 val mash_SUGGEST_N = "mash_SUGGEST"
48 val auto = Unsynchronized.ref false
51 ProofGeneralPgip.add_preference Preferences.category_tracing
52 (Preferences.bool_pref auto "auto-sledgehammer"
53 "Run Sledgehammer automatically.")
55 (** Sledgehammer commands **)
57 fun add_fact_override ns : fact_override = {add = ns, del = [], only = false}
58 fun del_fact_override ns : fact_override = {add = [], del = ns, only = false}
59 fun only_fact_override ns : fact_override = {add = ns, del = [], only = true}
60 fun merge_fact_override_pairwise (r1 : fact_override) (r2 : fact_override) =
61 {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
62 only = #only r1 andalso #only r2}
63 fun merge_fact_overrides rs =
64 fold merge_fact_override_pairwise rs (only_fact_override [])
68 val provers = Unsynchronized.ref ""
69 val timeout = Unsynchronized.ref 30
72 ProofGeneralPgip.add_preference Preferences.category_proof
73 (Preferences.string_pref provers
74 "Sledgehammer: Provers"
75 "Default automatic provers (separated by whitespace)")
78 ProofGeneralPgip.add_preference Preferences.category_proof
79 (Preferences.int_pref timeout
80 "Sledgehammer: Time Limit"
81 "ATPs will be interrupted after this time (in seconds)")
83 type raw_param = string * string list
85 val default_default_params =
88 ("overlord", "false"),
89 ("blocking", "false"),
90 ("type_enc", "smart"),
92 ("lam_trans", "smart"),
93 ("uncurried_aliases", "smart"),
94 ("fact_thresholds", "0.45 0.85"),
95 ("max_facts", "smart"),
96 ("max_mono_iters", "smart"),
97 ("max_new_mono_instances", "smart"),
98 ("isar_proof", "false"),
99 ("isar_shrink_factor", "1"),
101 ("minimize", "smart"),
102 ("preplay_timeout", "3")]
105 [("prover", ("provers", [])), (* legacy *)
106 ("max_relevant", ("max_facts", [])), (* legacy *)
107 ("dont_preplay", ("preplay_timeout", ["0"]))]
108 val negated_alias_params =
109 [("no_debug", "debug"),
110 ("quiet", "verbose"),
111 ("no_overlord", "overlord"),
112 ("non_blocking", "blocking"),
113 ("non_strict", "strict"),
114 ("no_uncurried_aliases", "uncurried_aliases"),
115 ("no_isar_proof", "isar_proof"),
116 ("dont_slice", "slice"),
117 ("dont_minimize", "minimize")]
119 val params_for_minimize =
120 ["debug", "verbose", "overlord", "type_enc", "strict", "lam_trans",
121 "uncurried_aliases", "max_mono_iters", "max_new_mono_instances",
122 "isar_proof", "isar_shrink_factor", "timeout", "preplay_timeout"]
124 val property_dependent_params = ["provers", "timeout"]
126 fun is_known_raw_param s =
127 AList.defined (op =) default_default_params s orelse
128 AList.defined (op =) alias_params s orelse
129 AList.defined (op =) negated_alias_params s orelse
130 member (op =) property_dependent_params s orelse s = "expect"
132 fun is_prover_list ctxt s =
133 case space_explode " " s of
134 ss as _ :: _ => forall (is_prover_supported ctxt) ss
137 fun unalias_raw_param (name, value) =
138 case AList.lookup (op =) alias_params name of
139 SOME (name', []) => (name', value)
140 | SOME (param' as (name', _)) =>
141 if value <> ["false"] then
144 error ("Parameter " ^ quote name ^ " cannot be set to \"false\" \
145 \(cf. " ^ quote name' ^ ").")
147 case AList.lookup (op =) negated_alias_params name of
148 SOME name' => (name', case value of
149 ["false"] => ["true"]
150 | ["true"] => ["false"]
153 | NONE => (name, value)
155 val any_type_enc = type_enc_from_string Strict "erased"
157 (* "provers =", "type_enc =", and "lam_trans" can be omitted. For the last two,
158 this is a secret feature. *)
159 fun normalize_raw_param ctxt =
161 #> (fn (name, value) =>
162 if is_known_raw_param name then
164 else if is_prover_list ctxt name andalso null value then
166 else if can (type_enc_from_string Strict) name andalso null value then
168 else if can (trans_lams_from_string ctxt any_type_enc) name andalso
170 ("lam_trans", [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 fact_thresholds = lookup_real_pair "fact_thresholds"
302 val max_facts = lookup_option lookup_int "max_facts"
303 val max_mono_iters = lookup_option lookup_int "max_mono_iters"
304 val max_new_mono_instances =
305 lookup_option lookup_int "max_new_mono_instances"
306 val isar_proof = lookup_bool "isar_proof"
307 val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
308 val slice = mode <> Auto_Try andalso lookup_bool "slice"
310 if mode = Auto_Try then NONE else lookup_option lookup_bool "minimize"
312 (if mode = Auto_Try then NONE else lookup_time "timeout") |> the_timeout
313 val preplay_timeout =
314 if mode = Auto_Try then Time.zeroTime
315 else lookup_time "preplay_timeout" |> the_timeout
316 val expect = lookup_string "expect"
318 {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
319 provers = provers, type_enc = type_enc, strict = strict,
320 lam_trans = lam_trans, uncurried_aliases = uncurried_aliases,
321 fact_thresholds = fact_thresholds, max_facts = max_facts,
322 max_mono_iters = max_mono_iters,
323 max_new_mono_instances = max_new_mono_instances, isar_proof = isar_proof,
324 isar_shrink_factor = isar_shrink_factor, slice = slice,
325 minimize = minimize, timeout = timeout, preplay_timeout = preplay_timeout,
329 fun get_params mode = extract_params mode o default_raw_params
330 fun default_params thy = get_params Normal thy o map (apsnd single)
332 (* Sledgehammer the given subgoal *)
334 val default_minimize_prover = metisN
336 fun is_raw_param_relevant_for_minimize (name, _) =
337 member (op =) params_for_minimize name
338 fun string_for_raw_param (key, values) =
339 key ^ (case implode_param values of "" => "" | value => " = " ^ value)
341 fun minimize_command override_params i more_override_params prover_name
346 |> filter_out (AList.defined (op =) more_override_params o fst)) @
348 |> filter is_raw_param_relevant_for_minimize
349 |> map string_for_raw_param
350 |> (if prover_name = default_minimize_prover then I else cons prover_name)
351 |> space_implode ", "
353 "sledgehammer" ^ " " ^ minN ^
354 (if params = "" then "" else enclose " [" "]" params) ^
355 " (" ^ space_implode " " fact_names ^ ")" ^
356 (if i = 1 then "" else " " ^ string_of_int i)
359 fun hammer_away override_params subcommand opt_i fact_override state =
361 val ctxt = Proof.context_of state
362 val override_params = override_params |> map (normalize_raw_param ctxt)
364 if subcommand = runN then
365 let val i = the_default 1 opt_i in
366 run_sledgehammer (get_params Normal ctxt override_params) Normal i
367 fact_override (minimize_command override_params i)
371 else if subcommand = minN then
372 run_minimize (get_params Minimize ctxt
373 (("provers", [default_minimize_prover]) ::
375 (the_default 1 opt_i) (#add fact_override) state
376 else if subcommand = messagesN then
378 else if subcommand = supported_proversN then
379 supported_provers ctxt
380 else if subcommand = running_proversN then
382 else if subcommand = kill_proversN then
384 else if subcommand = refresh_tptpN then
385 refresh_systems_on_tptp ()
386 else if subcommand = mash_resetN then
388 else if subcommand = mash_learnN then
389 () (* TODO: implement *)
390 else if subcommand = mash_RESET_N then
391 () (* TODO: implement *)
392 else if subcommand = mash_ADD_N then
393 () (* TODO: implement *)
394 else if subcommand = mash_DEL_N then
395 () (* TODO: implement *)
396 else if subcommand = mash_SUGGEST_N then
397 () (* TODO: implement *)
399 error ("Unknown subcommand: " ^ quote subcommand ^ ".")
402 fun sledgehammer_trans (((subcommand, params), fact_override), opt_i) =
403 Toplevel.keep (hammer_away params subcommand opt_i fact_override
406 fun string_for_raw_param (name, value) = name ^ " = " ^ implode_param value
408 fun sledgehammer_params_trans params =
410 (fold set_default_raw_param params
412 let val ctxt = Proof_Context.init_global thy in
413 writeln ("Default parameters for Sledgehammer:\n" ^
414 (case default_raw_params ctxt |> rev of
417 params |> map string_for_raw_param
418 |> sort_strings |> cat_lines))
421 val parse_query_bang = @{keyword "?"} || @{keyword "!"} || @{keyword "!!"}
423 Scan.repeat1 (Parse.typ_group || parse_query_bang) >> implode_param
425 Scan.repeat1 (Parse.xname || Parse.float_number || parse_query_bang)
426 val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) []
427 val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
428 val parse_fact_refs =
429 Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
430 val parse_fact_override_chunk =
431 (Args.add |-- Args.colon |-- parse_fact_refs >> add_fact_override)
432 || (Args.del |-- Args.colon |-- parse_fact_refs >> del_fact_override)
433 || (parse_fact_refs >> only_fact_override)
434 val parse_fact_override =
435 Scan.optional (Args.parens (Scan.repeat parse_fact_override_chunk
436 >> merge_fact_overrides))
440 Outer_Syntax.improper_command @{command_spec "sledgehammer"}
441 "search for first-order proof using automatic theorem provers"
442 ((Scan.optional Parse.short_ident runN -- parse_params
443 -- parse_fact_override -- Scan.option Parse.nat) #>> sledgehammer_trans)
445 Outer_Syntax.command @{command_spec "sledgehammer_params"}
446 "set and display the default parameters for Sledgehammer"
447 (parse_params #>> sledgehammer_params_trans)
449 fun try_sledgehammer auto state =
451 val ctxt = Proof.context_of state
452 val mode = if auto then Auto_Try else Try
455 run_sledgehammer (get_params mode ctxt []) mode i no_fact_override
456 (minimize_command [] i) state
459 val setup = Try.register_tool ("sledgehammer", (40, auto, try_sledgehammer))