Sledgehammer already has its own ways of reporting and recovering from crashes in external provers -- no need to additionally print scores of warnings (cf. 4b0daca2bf88)
1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_provers.ML
2 Author: Fabian Immler, TU Muenchen
4 Author: Jasmin Blanchette, TU Muenchen
6 Generic prover abstraction for Sledgehammer.
9 signature SLEDGEHAMMER_PROVERS =
11 type failure = ATP_Proof.failure
12 type stature = ATP_Problem_Generate.stature
13 type type_enc = ATP_Problem_Generate.type_enc
14 type reconstructor = ATP_Proof_Reconstruct.reconstructor
15 type play = ATP_Proof_Reconstruct.play
16 type minimize_command = ATP_Proof_Reconstruct.minimize_command
18 datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
25 provers : string list,
26 type_enc : string option,
28 lam_trans : string option,
29 uncurried_aliases : bool option,
31 fact_filter : string option,
32 max_facts : int option,
33 fact_thresholds : real * real,
34 max_mono_iters : int option,
35 max_new_mono_instances : int option,
37 isar_shrink_factor : int,
39 minimize : bool option,
41 preplay_timeout : Time.time,
44 type relevance_fudge =
45 {local_const_multiplier : real,
46 worse_irrel_freq : real,
47 higher_order_irrel_weight : real,
48 abs_rel_weight : real,
49 abs_irrel_weight : real,
50 skolem_irrel_weight : real,
51 theory_const_rel_weight : real,
52 theory_const_irrel_weight : real,
53 chained_const_irrel_weight : real,
61 max_imperfect_exp : real,
62 threshold_divisor : real,
63 ridiculous_threshold : real}
65 datatype prover_fact =
66 Untranslated_Fact of (string * stature) * thm |
67 SMT_Weighted_Fact of (string * stature) * (int option * thm)
74 facts : prover_fact list}
77 {outcome : failure option,
78 used_facts : (string * stature) list,
80 preplay : unit -> play,
81 message : play -> string,
82 message_tail : string}
85 params -> ((string * string list) list -> string -> minimize_command)
86 -> prover_problem -> prover_result
88 val dest_dir : string Config.T
89 val problem_prefix : string Config.T
90 val completish : bool Config.T
91 val atp_full_names : bool Config.T
92 val smt_triggers : bool Config.T
93 val smt_weights : bool Config.T
94 val smt_weight_min_facts : int Config.T
95 val smt_min_weight : int Config.T
96 val smt_max_weight : int Config.T
97 val smt_max_weight_index : int Config.T
98 val smt_weight_curve : (int -> int) Unsynchronized.ref
99 val smt_max_slices : int Config.T
100 val smt_slice_fact_frac : real Config.T
101 val smt_slice_time_frac : real Config.T
102 val smt_slice_min_secs : int Config.T
103 val SledgehammerN : string
104 val plain_metis : reconstructor
105 val select_smt_solver : string -> Proof.context -> Proof.context
106 val extract_reconstructor :
107 params -> reconstructor -> string * (string * string list) list
108 val is_reconstructor : string -> bool
109 val is_atp : theory -> string -> bool
110 val is_smt_prover : Proof.context -> string -> bool
111 val is_ho_atp: Proof.context -> string -> bool
112 val is_unit_equational_atp : Proof.context -> string -> bool
113 val is_prover_supported : Proof.context -> string -> bool
114 val is_prover_installed : Proof.context -> string -> bool
115 val default_max_facts_for_prover : Proof.context -> bool -> string -> int
116 val is_unit_equality : term -> bool
117 val is_appropriate_prop_for_prover : Proof.context -> string -> term -> bool
118 val is_built_in_const_for_prover :
119 Proof.context -> string -> string * typ -> term list -> bool * term list
120 val atp_relevance_fudge : relevance_fudge
121 val smt_relevance_fudge : relevance_fudge
122 val relevance_fudge_for_prover : Proof.context -> string -> relevance_fudge
123 val weight_smt_fact :
124 Proof.context -> int -> ((string * stature) * thm) * int
125 -> (string * stature) * (int option * thm)
126 val untranslated_fact : prover_fact -> (string * stature) * thm
127 val smt_weighted_fact :
128 Proof.context -> int -> prover_fact * int
129 -> (string * stature) * (int option * thm)
130 val supported_provers : Proof.context -> unit
131 val kill_provers : unit -> unit
132 val running_provers : unit -> unit
133 val messages : int option -> unit
134 val filter_used_facts : ''a list -> (''a * 'b) list -> (''a * 'b) list
135 val get_prover : Proof.context -> mode -> string -> prover
138 structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
145 open ATP_Problem_Generate
146 open ATP_Proof_Reconstruct
148 open Sledgehammer_Util
151 (** The Sledgehammer **)
153 datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
155 (* Identifier that distinguishes Sledgehammer from other tools that could use
157 val SledgehammerN = "Sledgehammer"
159 val reconstructor_names = [metisN, smtN]
160 val plain_metis = Metis (hd partial_type_encs, combsN)
161 val is_reconstructor = member (op =) reconstructor_names
163 val is_atp = member (op =) o supported_atps
165 val select_smt_solver = Context.proof_map o SMT_Config.select_solver
167 fun is_smt_prover ctxt = member (op =) (SMT_Solver.available_solvers_of ctxt)
169 fun is_atp_for_format is_format ctxt name =
170 let val thy = Proof_Context.theory_of ctxt in
171 case try (get_atp thy) name of
173 exists (fn (_, (_, ((_, format, _, _, _), _))) => is_format format)
174 (#best_slices (config ()) ctxt)
178 val is_unit_equational_atp = is_atp_for_format (curry (op =) CNF_UEQ)
179 val is_ho_atp = is_atp_for_format is_format_higher_order
181 fun is_prover_supported ctxt =
182 let val thy = Proof_Context.theory_of ctxt in
183 is_reconstructor orf is_atp thy orf is_smt_prover ctxt
186 fun is_prover_installed ctxt =
187 is_reconstructor orf is_smt_prover ctxt orf
188 is_atp_installed (Proof_Context.theory_of ctxt)
190 fun get_slices slice slices =
191 (0 upto length slices - 1) ~~ slices |> not slice ? (List.last #> single)
193 val reconstructor_default_max_facts = 20
195 fun default_max_facts_for_prover ctxt slice name =
196 let val thy = Proof_Context.theory_of ctxt in
197 if is_reconstructor name then
198 reconstructor_default_max_facts
199 else if is_atp thy name then
200 fold (Integer.max o #1 o fst o snd o snd o snd)
201 (get_slices slice (#best_slices (get_atp thy name ()) ctxt)) 0
202 else (* is_smt_prover ctxt name *)
203 SMT_Solver.default_max_relevant ctxt name
206 fun is_if (@{const_name If}, _) = true
209 (* Beware of "if and only if" (which is translated as such) and "If" (which is
210 translated to conditional equations). *)
211 fun is_good_unit_equality T t u =
212 T <> @{typ bool} andalso not (exists (exists_Const is_if) [t, u])
214 fun is_unit_equality (@{const Trueprop} $ t) = is_unit_equality t
215 | is_unit_equality (Const (@{const_name all}, _) $ Abs (_, _, t)) =
217 | is_unit_equality (Const (@{const_name All}, _) $ Abs (_, _, t)) =
219 | is_unit_equality (Const (@{const_name "=="}, Type (_, [T, _])) $ t $ u) =
220 is_good_unit_equality T t u
221 | is_unit_equality (Const (@{const_name HOL.eq}, Type (_ , [T, _])) $ t $ u) =
222 is_good_unit_equality T t u
223 | is_unit_equality _ = false
225 fun is_appropriate_prop_for_prover ctxt name =
226 if is_unit_equational_atp ctxt name then is_unit_equality else K true
228 fun is_built_in_const_for_prover ctxt name =
229 if is_smt_prover ctxt name then
230 let val ctxt = ctxt |> select_smt_solver name in
232 if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
234 else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
240 fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
243 val atp_relevance_fudge =
244 {local_const_multiplier = 1.5,
245 worse_irrel_freq = 100.0,
246 higher_order_irrel_weight = 1.05,
247 abs_rel_weight = 0.5,
248 abs_irrel_weight = 2.0,
249 skolem_irrel_weight = 0.05,
250 theory_const_rel_weight = 0.5,
251 theory_const_irrel_weight = 0.25,
252 chained_const_irrel_weight = 0.25,
259 max_imperfect = 11.5,
260 max_imperfect_exp = 1.0,
261 threshold_divisor = 2.0,
262 ridiculous_threshold = 0.01}
265 val smt_relevance_fudge =
266 {local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
267 worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
268 higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
269 abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
270 abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
271 skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
272 theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
273 theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
274 chained_const_irrel_weight = #chained_const_irrel_weight atp_relevance_fudge,
275 intro_bonus = #intro_bonus atp_relevance_fudge,
276 elim_bonus = #elim_bonus atp_relevance_fudge,
277 simp_bonus = #simp_bonus atp_relevance_fudge,
278 local_bonus = #local_bonus atp_relevance_fudge,
279 assum_bonus = #assum_bonus atp_relevance_fudge,
280 chained_bonus = #chained_bonus atp_relevance_fudge,
281 max_imperfect = #max_imperfect atp_relevance_fudge,
282 max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
283 threshold_divisor = #threshold_divisor atp_relevance_fudge,
284 ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
286 fun relevance_fudge_for_prover ctxt name =
287 if is_smt_prover ctxt name then smt_relevance_fudge else atp_relevance_fudge
289 fun supported_provers ctxt =
291 val thy = Proof_Context.theory_of ctxt
292 val (remote_provers, local_provers) =
293 reconstructor_names @
294 sort_strings (supported_atps thy) @
295 sort_strings (SMT_Solver.available_solvers_of ctxt)
296 |> List.partition (String.isPrefix remote_prefix)
298 Output.urgent_message ("Supported provers: " ^
299 commas (local_provers @ remote_provers) ^ ".")
302 fun kill_provers () = Async_Manager.kill_threads SledgehammerN "prover"
303 fun running_provers () = Async_Manager.running_threads SledgehammerN "prover"
304 val messages = Async_Manager.thread_messages SledgehammerN "prover"
307 (** problems, results, ATPs, etc. **)
314 provers : string list,
315 type_enc : string option,
317 lam_trans : string option,
318 uncurried_aliases : bool option,
320 fact_filter : string option,
321 max_facts : int option,
322 fact_thresholds : real * real,
323 max_mono_iters : int option,
324 max_new_mono_instances : int option,
326 isar_shrink_factor : int,
328 minimize : bool option,
330 preplay_timeout : Time.time,
333 type relevance_fudge =
334 {local_const_multiplier : real,
335 worse_irrel_freq : real,
336 higher_order_irrel_weight : real,
337 abs_rel_weight : real,
338 abs_irrel_weight : real,
339 skolem_irrel_weight : real,
340 theory_const_rel_weight : real,
341 theory_const_irrel_weight : real,
342 chained_const_irrel_weight : real,
348 chained_bonus : real,
349 max_imperfect : real,
350 max_imperfect_exp : real,
351 threshold_divisor : real,
352 ridiculous_threshold : real}
354 datatype prover_fact =
355 Untranslated_Fact of (string * stature) * thm |
356 SMT_Weighted_Fact of (string * stature) * (int option * thm)
358 type prover_problem =
359 {state : Proof.state,
363 facts : prover_fact list}
366 {outcome : failure option,
367 used_facts : (string * stature) list,
368 run_time : Time.time,
369 preplay : unit -> play,
370 message : play -> string,
371 message_tail : string}
374 params -> ((string * string list) list -> string -> minimize_command)
375 -> prover_problem -> prover_result
377 (* configuration attributes *)
379 (* Empty string means create files in Isabelle's temporary files directory. *)
381 Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
383 Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
385 Attrib.setup_config_bool @{binding sledgehammer_completish} (K false)
387 (* In addition to being easier to read, readable names are often much shorter,
388 especially if types are mangled in names. This makes a difference for some
389 provers (e.g., E). For these reason, short names are enabled by default. *)
391 Attrib.setup_config_bool @{binding sledgehammer_atp_full_names} (K false)
394 Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
396 Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
397 val smt_weight_min_facts =
398 Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
402 Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
404 Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
405 val smt_max_weight_index =
406 Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
407 val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
409 fun smt_fact_weight ctxt j num_facts =
410 if Config.get ctxt smt_weights andalso
411 num_facts >= Config.get ctxt smt_weight_min_facts then
413 val min = Config.get ctxt smt_min_weight
414 val max = Config.get ctxt smt_max_weight
415 val max_index = Config.get ctxt smt_max_weight_index
416 val curve = !smt_weight_curve
418 SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
424 fun weight_smt_fact ctxt num_facts ((info, th), j) =
425 let val thy = Proof_Context.theory_of ctxt in
426 (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
429 fun untranslated_fact (Untranslated_Fact p) = p
430 | untranslated_fact (SMT_Weighted_Fact (info, (_, th))) = (info, th)
431 fun smt_weighted_fact _ _ (SMT_Weighted_Fact p, _) = p
432 | smt_weighted_fact ctxt num_facts (fact, j) =
433 (untranslated_fact fact, j) |> weight_smt_fact ctxt num_facts
435 fun overlord_file_location_for_prover prover =
436 (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
438 fun with_path cleanup after f path =
440 |> tap (fn _ => cleanup path)
444 fun proof_banner mode name =
446 Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
447 | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
450 fun bunch_of_reconstructors needs_full_types lam_trans =
451 [(false, Metis (partial_type_enc, lam_trans false)),
452 (true, Metis (full_type_enc, lam_trans false)),
453 (false, Metis (no_typesN, lam_trans true)),
454 (true, Metis (really_full_type_enc, lam_trans true)),
456 |> map_filter (fn (full_types, reconstr) =>
457 if needs_full_types andalso not full_types then NONE
460 fun extract_reconstructor ({type_enc, lam_trans, ...} : params)
461 (Metis (type_enc', lam_trans')) =
463 val override_params =
464 (if is_none type_enc andalso type_enc' = hd partial_type_encs then
467 [("type_enc", [hd (unalias_type_enc type_enc')])]) @
468 (if is_none lam_trans andalso lam_trans' = metis_default_lam_trans then
471 [("lam_trans", [lam_trans'])])
472 in (metisN, override_params) end
473 | extract_reconstructor _ SMT = (smtN, [])
475 (* based on "Mirabelle.can_apply" and generalized *)
476 fun timed_apply timeout tac state i =
478 val {context = ctxt, facts, goal} = Proof.goal state
479 val full_tac = Method.insert_tac facts i THEN tac ctxt i
480 in TimeLimit.timeLimit timeout (try (Seq.pull o full_tac)) goal end
482 fun tac_for_reconstructor (Metis (type_enc, lam_trans)) =
483 metis_tac [type_enc] lam_trans
484 | tac_for_reconstructor SMT = SMT_Solver.smt_tac
486 fun timed_reconstructor reconstr debug timeout ths =
487 (Config.put Metis_Tactic.verbose debug
488 #> Config.put SMT_Config.verbose debug
489 #> (fn ctxt => tac_for_reconstructor reconstr ctxt ths))
490 |> timed_apply timeout
492 fun filter_used_facts used = filter (member (op =) used o fst)
494 fun play_one_line_proof mode debug verbose timeout pairs state i preferred
498 if mode = Minimize andalso Time.> (timeout, Time.zeroTime) then
499 Output.urgent_message "Preplaying proof..."
502 val ths = pairs |> sort_wrt (fst o fst) |> map snd
503 fun get_preferred reconstrs =
504 if member (op =) reconstrs preferred then preferred
505 else List.last reconstrs
506 fun play [] [] = Failed_to_Play (get_preferred reconstrs)
507 | play timed_outs [] =
508 Trust_Playable (get_preferred timed_outs, SOME timeout)
509 | play timed_out (reconstr :: reconstrs) =
513 "Trying \"" ^ string_for_reconstructor reconstr ^ "\" for " ^
514 string_from_time timeout ^ "..."
515 |> Output.urgent_message
518 val timer = Timer.startRealTimer ()
520 case timed_reconstructor reconstr debug timeout ths state i of
521 SOME (SOME _) => Played (reconstr, Timer.checkRealTimer timer)
522 | _ => play timed_out reconstrs
524 handle TimeLimit.TimeOut => play (reconstr :: timed_out) reconstrs
526 if timeout = Time.zeroTime then
527 Trust_Playable (get_preferred reconstrs, NONE)
533 (* generic TPTP-based ATPs *)
535 (* Too general means, positive equality literal with a variable X as one
536 operand, when X does not occur properly in the other operand. This rules out
537 clearly inconsistent facts such as X = a | X = b, though it by no means
538 guarantees soundness. *)
540 (* Unwanted equalities are those between a (bound or schematic) variable that
541 does not properly occur in the second operand. *)
542 val is_exhaustive_finite =
544 fun is_bad_equal (Var z) t =
545 not (exists_subterm (fn Var z' => z = z' | _ => false) t)
546 | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
547 | is_bad_equal _ _ = false
548 fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
549 fun do_formula pos t =
551 (_, @{const Trueprop} $ t1) => do_formula pos t1
552 | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
554 | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
556 | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
558 | (_, @{const "==>"} $ t1 $ t2) =>
559 do_formula (not pos) t1 andalso
560 (t2 = @{prop False} orelse do_formula pos t2)
561 | (_, @{const HOL.implies} $ t1 $ t2) =>
562 do_formula (not pos) t1 andalso
563 (t2 = @{const False} orelse do_formula pos t2)
564 | (_, @{const Not} $ t1) => do_formula (not pos) t1
565 | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
566 | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
567 | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
568 | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
570 in do_formula true end
572 fun has_bound_or_var_of_type pred =
573 exists_subterm (fn Var (_, T as Type _) => pred T
574 | Abs (_, T as Type _, _) => pred T
577 (* Facts are forbidden to contain variables of these types. The typical reason
578 is that they lead to unsoundness. Note that "unit" satisfies numerous
579 equations like "?x = ()". The resulting clauses will have no type constraint,
580 yielding false proofs. Even "bool" leads to many unsound proofs, though only
581 for higher-order problems. *)
583 (* Facts containing variables of type "unit" or "bool" or of the form
584 "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
586 fun is_dangerous_prop ctxt =
588 #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
589 is_exhaustive_finite)
591 (* Important messages are important but not so important that users want to see
593 val atp_important_message_keep_quotient = 25
595 fun choose_type_enc soundness best_type_enc format =
596 the_default best_type_enc
597 #> type_enc_from_string soundness
598 #> adjust_type_enc format
600 val metis_minimize_max_time = seconds 2.0
602 fun choose_minimize_command params minimize_command name preplay =
604 val (name, override_params) =
606 Played (reconstr, time) =>
607 if Time.<= (time, metis_minimize_max_time) then
608 extract_reconstructor params reconstr
612 in minimize_command override_params name end
614 fun repair_monomorph_context max_iters best_max_iters max_new_instances
615 best_max_new_instances =
616 Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
617 #> Config.put Monomorph.max_new_instances
618 (max_new_instances |> the_default best_max_new_instances)
619 #> Config.put Monomorph.keep_partial_instances false
621 fun suffix_for_mode Auto_Try = "_auto_try"
622 | suffix_for_mode Try = "_try"
623 | suffix_for_mode Normal = ""
624 | suffix_for_mode MaSh = "_mash"
625 | suffix_for_mode Auto_Minimize = "_auto_min"
626 | suffix_for_mode Minimize = "_min"
628 (* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on
629 Linux appears to be the only ATP that does not honor its time limit. *)
630 val atp_timeout_slack = seconds 1.0
632 val mono_max_privileged_facts = 10
634 fun run_atp mode name
635 ({exec, arguments, proof_delims, known_failures, prem_role, best_slices,
636 best_max_mono_iters, best_max_new_mono_instances, ...} : atp_config)
637 (params as {debug, verbose, overlord, type_enc, strict, lam_trans,
638 uncurried_aliases, max_facts, max_mono_iters,
639 max_new_mono_instances, isar_proof, isar_shrink_factor,
640 slice, timeout, preplay_timeout, ...})
642 ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
644 val thy = Proof.theory_of state
645 val ctxt = Proof.context_of state
647 if Config.get ctxt completish then Sledgehammer_Completish
649 val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
650 val (dest_dir, problem_prefix) =
651 if overlord then overlord_file_location_for_prover name
652 else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
653 val problem_file_name =
654 Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
655 suffix_for_mode mode ^ "_" ^ string_of_int subgoal)
656 val problem_path_name =
657 if dest_dir = "" then
658 File.tmp_path problem_file_name
659 else if File.exists (Path.explode dest_dir) then
660 Path.append (Path.explode dest_dir) problem_file_name
662 error ("No such directory: " ^ quote dest_dir ^ ".")
664 case find_first (fn var => getenv var <> "") (fst exec) of
667 val pref = getenv var ^ "/"
668 val paths = map (Path.explode o prefix pref) (snd exec)
670 case find_first File.exists paths of
672 | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".")
674 | NONE => error ("The environment variable " ^ quote (hd (fst exec)) ^
678 val split = String.tokens (fn c => str c = "\n")
680 s |> split |> (try split_last #> the_default ([], "0"))
682 fun as_num f = f >> (fst o read_int)
683 val num = as_num (Scan.many1 Symbol.is_ascii_digit)
684 val digit = Scan.one Symbol.is_ascii_digit
685 val num3 = as_num (digit ::: digit ::: (digit >> single))
686 val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
688 raw_explode #> Scan.read Symbol.stopper time #> the_default 0
689 in (output, as_time t |> Time.fromMilliseconds) end
690 fun run_on prob_file =
692 (* If slicing is disabled, we expand the last slice to fill the entire
694 val actual_slices = get_slices slice (best_slices ctxt)
695 val num_actual_slices = length actual_slices
696 fun monomorphize_facts facts =
700 |> repair_monomorph_context max_mono_iters best_max_mono_iters
701 max_new_mono_instances best_max_new_mono_instances
702 (* pseudo-theorem involving the same constants as the subgoal *)
704 Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy
706 facts |> chop mono_max_privileged_facts
707 |>> map (pair 1 o snd)
708 ||> map (pair 2 o snd)
710 |> cons (0, subgoal_th)
712 Monomorph.monomorph atp_schematic_consts_of rths ctxt |> fst |> tl
713 |> curry ListPair.zip (map fst facts)
714 |> maps (fn (name, rths) =>
715 map (pair name o zero_var_indexes o snd) rths)
717 fun run_slice time_left (cache_key, cache_value)
718 (slice, (time_frac, (complete,
719 (key as (best_max_facts, format, best_type_enc,
720 best_lam_trans, best_uncurried_aliases),
724 length facts |> is_none max_facts ? Integer.min best_max_facts
725 val soundness = if strict then Strict else Non_Strict
727 type_enc |> choose_type_enc soundness best_type_enc format
728 val sound = is_type_enc_sound type_enc
729 val real_ms = Real.fromInt o Time.toMilliseconds
732 |> (if slice < num_actual_slices - 1 then
733 curry Real.min (time_frac * real_ms timeout)
737 val generous_slice_timeout =
738 Time.+ (slice_timeout, atp_timeout_slack)
741 quote name ^ " slice #" ^ string_of_int (slice + 1) ^
742 " with " ^ string_of_int num_facts ^ " fact" ^
743 plural_s num_facts ^ " for " ^
744 string_from_time slice_timeout ^ "..."
745 |> Output.urgent_message
748 val readable_names = not (Config.get ctxt atp_full_names)
752 | NONE => best_lam_trans
753 val uncurried_aliases =
754 case uncurried_aliases of
756 | NONE => best_uncurried_aliases
757 val value as (atp_problem, _, fact_names, _, _) =
758 if cache_key = SOME key then
762 |> map untranslated_fact
764 ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
766 |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts
767 |> map (apsnd prop_of)
768 |> prepare_atp_problem ctxt format prem_role type_enc atp_mode
769 lam_trans uncurried_aliases
770 readable_names true hyp_ts concl_t
771 fun sel_weights () = atp_problem_selection_weights atp_problem
772 fun ord_info () = atp_problem_term_order_info atp_problem
773 val ord = effective_term_order ctxt name
774 val full_proof = debug orelse isar_proof
775 val args = arguments ctxt full_proof extra slice_timeout
776 (ord, ord_info, sel_weights)
778 "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^
779 File.shell_path prob_file ^ ")"
780 |> enclose "TIMEFORMAT='%3R'; { time " " ; }"
783 |> lines_for_atp_problem format ord ord_info
784 |> cons ("% " ^ command ^ "\n")
785 |> File.write_list prob_file
786 val ((output, run_time), (atp_proof, outcome)) =
787 TimeLimit.timeLimit generous_slice_timeout
788 Isabelle_System.bash_output command
789 |>> (if overlord then
790 prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
794 |> (fn accum as (output, _) =>
796 extract_tstplike_proof_and_outcome verbose complete
797 proof_delims known_failures output
798 |>> atp_proof_from_tstplike_proof atp_problem
799 handle UNRECOGNIZED_ATP_PROOF () =>
800 ([], SOME ProofIncomplete)))
801 handle TimeLimit.TimeOut =>
802 (("", slice_timeout), ([], SOME TimedOut))
806 (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof
807 |> Option.map (sort string_ord) of
811 UnsoundProof (is_type_enc_sound type_enc, facts)
813 if debug then (warning (string_for_failure failure); NONE)
818 in ((SOME key, value), (output, run_time, atp_proof, outcome)) end
819 val timer = Timer.startRealTimer ()
820 fun maybe_run_slice slice
821 (result as (cache, (_, run_time0, _, SOME _))) =
823 val time_left = Time.- (timeout, Timer.checkRealTimer timer)
825 if Time.<= (time_left, Time.zeroTime) then
828 run_slice time_left cache slice
829 |> (fn (cache, (output, run_time, atp_proof, outcome)) =>
830 (cache, (output, Time.+ (run_time0, run_time),
831 atp_proof, outcome)))
833 | maybe_run_slice _ result = result
835 ((NONE, ([], Symtab.empty, Vector.fromList [], [], Symtab.empty)),
836 ("", Time.zeroTime, [], SOME InternalError))
837 |> fold maybe_run_slice actual_slices
840 (* If the problem file has not been exported, remove it; otherwise, export
841 the proof file too. *)
842 fun cleanup prob_file =
843 if dest_dir = "" then try File.rm prob_file else NONE
844 fun export prob_file (_, (output, _, _, _)) =
845 if dest_dir = "" then ()
846 else File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
847 val ((_, (_, pool, fact_names, _, sym_tab)),
848 (output, run_time, atp_proof, outcome)) =
849 with_path cleanup export run_on problem_path_name
850 val important_message =
851 if mode = Normal andalso
852 random_range 0 (atp_important_message_keep_quotient - 1) = 0 then
853 extract_important_message output
856 val (used_facts, preplay, message, message_tail) =
860 val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof
861 val needs_full_types = is_typed_helper_used_in_atp_proof atp_proof
863 bunch_of_reconstructors needs_full_types
864 (lam_trans_from_atp_proof atp_proof
865 o (fn desperate => if desperate then hide_lamsN
866 else metis_default_lam_trans))
872 facts |> map untranslated_fact |> filter_used_facts used_facts
874 play_one_line_proof mode debug verbose preplay_timeout
875 used_pairs state subgoal (hd reconstrs) reconstrs
880 (debug, isar_shrink_factor, pool, fact_names, sym_tab,
882 val one_line_params =
883 (preplay, proof_banner mode name, used_facts,
884 choose_minimize_command params minimize_command name preplay,
885 subgoal, subgoal_count)
886 in proof_text ctxt isar_proof isar_params one_line_params end,
888 "\nATP real CPU time: " ^ string_from_time run_time ^ "."
891 (if important_message <> "" then
892 "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
898 ([], K (Failed_to_Play plain_metis),
899 fn _ => string_for_failure failure, "")
901 {outcome = outcome, used_facts = used_facts, run_time = run_time,
902 preplay = preplay, message = message, message_tail = message_tail}
905 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
906 these are sorted out properly in the SMT module, we have to interpret these
908 val remote_smt_failures =
912 [(101, OutOfResources),
913 (103, MalformedInput),
914 (110, MalformedInput)]
917 val smt_failures = remote_smt_failures @ z3_failures @ unix_failures
919 fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
920 if is_real_cex then Unprovable else GaveUp
921 | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
922 | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
923 (case AList.lookup (op =) smt_failures code of
924 SOME failure => failure
925 | NONE => UnknownError ("Abnormal termination with exit code " ^
926 string_of_int code ^ "."))
927 | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
928 | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
933 Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
934 val smt_slice_fact_frac =
935 Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.5)
936 val smt_slice_time_frac =
937 Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.5)
938 val smt_slice_min_secs =
939 Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 5)
941 fun smt_filter_loop ctxt name
942 ({debug, verbose, overlord, max_mono_iters,
943 max_new_mono_instances, timeout, slice, ...} : params)
946 val max_slices = if slice then Config.get ctxt smt_max_slices else 1
948 select_smt_solver name
949 #> Config.put SMT_Config.verbose debug
951 Config.put SMT_Config.debug_files
952 (overlord_file_location_for_prover name
953 |> (fn (path, name) => path ^ "/" ^ name))
956 #> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
957 val state = state |> Proof.map_context repair_context
958 fun do_slice timeout slice outcome0 time_so_far facts =
960 val timer = Timer.startRealTimer ()
962 state |> Proof.map_context
963 (repair_monomorph_context max_mono_iters
964 default_max_mono_iters max_new_mono_instances
965 default_max_new_mono_instances)
966 val ms = timeout |> Time.toMilliseconds
968 if slice < max_slices then
970 Int.max (1000 * Config.get ctxt smt_slice_min_secs,
971 Real.ceil (Config.get ctxt smt_slice_time_frac
973 |> Time.fromMilliseconds
976 val num_facts = length facts
979 quote name ^ " slice " ^ string_of_int slice ^ " with " ^
980 string_of_int num_facts ^ " fact" ^ plural_s num_facts ^ " for " ^
981 string_from_time slice_timeout ^ "..."
982 |> Output.urgent_message
985 val birth = Timer.checkRealTimer timer
987 if debug then Output.urgent_message "Invoking SMT solver..." else ()
988 val (outcome, used_facts) =
989 SMT_Solver.smt_filter_preprocess state facts i
990 |> SMT_Solver.smt_filter_apply slice_timeout
991 |> (fn {outcome, used_facts} => (outcome, used_facts))
992 handle exn => if Exn.is_interrupt exn then
995 (ML_Compiler.exn_message exn
996 |> SMT_Failure.Other_Failure |> SOME, [])
997 val death = Timer.checkRealTimer timer
998 val outcome0 = if is_none outcome0 then SOME outcome else outcome0
999 val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
1000 val too_many_facts_perhaps =
1003 | SOME (SMT_Failure.Counterexample _) => false
1004 | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
1005 | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
1006 | SOME SMT_Failure.Out_Of_Memory => true
1007 | SOME (SMT_Failure.Other_Failure _) => true
1008 val timeout = Time.- (timeout, Timer.checkRealTimer timer)
1010 if too_many_facts_perhaps andalso slice < max_slices andalso
1011 num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
1014 Real.ceil (Config.get ctxt smt_slice_fact_frac
1015 * Real.fromInt num_facts)
1017 if verbose andalso is_some outcome then
1018 quote name ^ " invoked with " ^ string_of_int num_facts ^
1019 " fact" ^ plural_s num_facts ^ ": " ^
1020 string_for_failure (failure_from_smt_failure (the outcome)) ^
1021 " Retrying with " ^ string_of_int new_num_facts ^ " fact" ^
1022 plural_s new_num_facts ^ "..."
1023 |> Output.urgent_message
1027 facts |> take new_num_facts
1028 |> do_slice timeout (slice + 1) outcome0 time_so_far
1031 {outcome = if is_none outcome then NONE else the outcome0,
1032 used_facts = used_facts, run_time = time_so_far}
1034 in do_slice timeout 1 NONE Time.zeroTime end
1036 fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...})
1038 ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
1040 val ctxt = Proof.context_of state
1041 val num_facts = length facts
1042 val facts = facts ~~ (0 upto num_facts - 1)
1043 |> map (smt_weighted_fact ctxt num_facts)
1044 val {outcome, used_facts = used_pairs, run_time} =
1045 smt_filter_loop ctxt name params state subgoal facts
1046 val used_facts = used_pairs |> map fst
1047 val outcome = outcome |> Option.map failure_from_smt_failure
1048 val (preplay, message, message_tail) =
1052 play_one_line_proof mode debug verbose preplay_timeout used_pairs
1054 (bunch_of_reconstructors false
1056 if plain then metis_default_lam_trans else liftingN)),
1059 val one_line_params =
1060 (preplay, proof_banner mode name, used_facts,
1061 choose_minimize_command params minimize_command name preplay,
1062 subgoal, subgoal_count)
1063 in one_line_proof_text one_line_params end,
1065 "\nSMT solver real CPU time: " ^ string_from_time run_time ^ "."
1069 (K (Failed_to_Play plain_metis), fn _ => string_for_failure failure, "")
1071 {outcome = outcome, used_facts = used_facts, run_time = run_time,
1072 preplay = preplay, message = message, message_tail = message_tail}
1075 fun run_reconstructor mode name
1076 (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
1078 ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
1081 if name = metisN then
1082 Metis (type_enc |> the_default (hd partial_type_encs),
1083 lam_trans |> the_default metis_default_lam_trans)
1084 else if name = smtN then
1087 raise Fail ("unknown reconstructor: " ^ quote name)
1088 val used_pairs = facts |> map untranslated_fact
1089 val used_facts = used_pairs |> map fst
1091 case play_one_line_proof (if mode = Minimize then Normal else mode) debug
1092 verbose timeout used_pairs state subgoal reconstr
1094 play as Played (_, time) =>
1095 {outcome = NONE, used_facts = used_facts, run_time = time,
1100 val (_, override_params) = extract_reconstructor params reconstr
1101 val one_line_params =
1102 (play, proof_banner mode name, used_facts,
1103 minimize_command override_params name, subgoal,
1105 in one_line_proof_text one_line_params end,
1109 val failure = case play of Failed_to_Play _ => GaveUp | _ => TimedOut
1111 {outcome = SOME failure, used_facts = [], run_time = Time.zeroTime,
1112 preplay = K play, message = fn _ => string_for_failure failure,
1117 fun get_prover ctxt mode name =
1118 let val thy = Proof_Context.theory_of ctxt in
1119 if is_reconstructor name then run_reconstructor mode name
1120 else if is_atp thy name then run_atp mode name (get_atp thy name ())
1121 else if is_smt_prover ctxt name then run_smt_solver mode name
1122 else error ("No such prover: " ^ name ^ ".")