fewer monomorphic instances are necessary, thanks to Sascha's new monomorphizer -- backed up by Judgment Day
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 locality = ATP_Translate.locality
13 type type_sys = ATP_Translate.type_sys
14 type reconstructor = ATP_Reconstruct.reconstructor
15 type play = ATP_Reconstruct.play
16 type minimize_command = ATP_Reconstruct.minimize_command
17 type relevance_fudge = Sledgehammer_Filter.relevance_fudge
19 datatype mode = Auto_Try | Try | Normal | Minimize
21 datatype rich_type_sys =
22 Dumb_Type_Sys of type_sys |
23 Smart_Type_Sys of bool
31 type_sys: rich_type_sys,
32 relevance_thresholds: real * real,
33 max_relevant: int option,
35 max_new_mono_instances: int,
37 isar_shrink_factor: int,
40 preplay_timeout: Time.time,
43 datatype prover_fact =
44 Untranslated_Fact of (string * locality) * thm |
45 SMT_Weighted_Fact of (string * locality) * (int option * thm)
52 facts: prover_fact list,
53 smt_filter: (string * locality) SMT_Solver.smt_filter_data option}
56 {outcome: failure option,
57 used_facts: (string * locality) list,
58 run_time_in_msecs: int option,
59 preplay: unit -> play,
60 message: play -> string,
64 params -> (string -> minimize_command) -> prover_problem -> prover_result
66 val dest_dir : string Config.T
67 val problem_prefix : string Config.T
68 val measure_run_time : bool Config.T
69 val atp_readable_names : bool Config.T
70 val smt_triggers : bool Config.T
71 val smt_weights : bool Config.T
72 val smt_weight_min_facts : int Config.T
73 val smt_min_weight : int Config.T
74 val smt_max_weight : int Config.T
75 val smt_max_weight_index : int Config.T
76 val smt_weight_curve : (int -> int) Unsynchronized.ref
77 val smt_max_slices : int Config.T
78 val smt_slice_fact_frac : real Config.T
79 val smt_slice_time_frac : real Config.T
80 val smt_slice_min_secs : int Config.T
82 val select_smt_solver : string -> Proof.context -> Proof.context
83 val prover_name_for_reconstructor : reconstructor -> string option
84 val is_metis_prover : string -> bool
85 val is_atp : theory -> string -> bool
86 val is_smt_prover : Proof.context -> string -> bool
87 val is_unit_equational_atp : Proof.context -> string -> bool
88 val is_prover_supported : Proof.context -> string -> bool
89 val is_prover_installed : Proof.context -> string -> bool
90 val default_max_relevant_for_prover : Proof.context -> bool -> string -> int
91 val is_unit_equality : term -> bool
92 val is_appropriate_prop_for_prover : Proof.context -> string -> term -> bool
93 val is_built_in_const_for_prover :
94 Proof.context -> string -> string * typ -> term list -> bool * term list
95 val atp_relevance_fudge : relevance_fudge
96 val smt_relevance_fudge : relevance_fudge
97 val relevance_fudge_for_prover : Proof.context -> string -> relevance_fudge
99 Proof.context -> int -> ((string * locality) * thm) * int
100 -> (string * locality) * (int option * thm)
101 val untranslated_fact : prover_fact -> (string * locality) * thm
102 val smt_weighted_fact :
103 Proof.context -> int -> prover_fact * int
104 -> (string * locality) * (int option * thm)
105 val supported_provers : Proof.context -> unit
106 val kill_provers : unit -> unit
107 val running_provers : unit -> unit
108 val messages : int option -> unit
109 val filter_used_facts : ''a list -> (''a * 'b) list -> (''a * 'b) list
110 val get_prover : Proof.context -> mode -> string -> prover
113 structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
122 open Sledgehammer_Util
123 open Sledgehammer_Filter
125 (** The Sledgehammer **)
127 datatype mode = Auto_Try | Try | Normal | Minimize
129 (* Identifier to distinguish Sledgehammer from other tools using
131 val das_tool = "Sledgehammer"
133 val metisN = Metis_Tactics.metisN
134 val metis_full_typesN = metisN ^ "_" ^ Metis_Tactics.full_typesN
135 val metis_no_typesN = metisN ^ "_" ^ Metis_Tactics.no_typesN
137 val metis_prover_infos =
139 (metis_full_typesN, Metis_Full_Types),
140 (metis_no_typesN, Metis_No_Types)]
142 val prover_name_for_reconstructor =
143 AList.find (op =) metis_prover_infos #> try hd
145 val metis_reconstructors = map snd metis_prover_infos
147 val is_metis_prover = AList.defined (op =) metis_prover_infos
148 val is_atp = member (op =) o supported_atps
150 val select_smt_solver = Context.proof_map o SMT_Config.select_solver
152 fun is_smt_prover ctxt name =
153 member (op =) (SMT_Solver.available_solvers_of ctxt) name
155 fun is_unit_equational_atp ctxt name =
156 let val thy = Proof_Context.theory_of ctxt in
157 case try (get_atp thy) name of
158 SOME {formats, ...} => member (op =) formats CNF_UEQ
162 fun is_prover_supported ctxt name =
163 let val thy = Proof_Context.theory_of ctxt in
164 is_metis_prover name orelse is_atp thy name orelse is_smt_prover ctxt name
167 fun is_prover_installed ctxt =
168 is_metis_prover orf is_smt_prover ctxt orf
169 is_atp_installed (Proof_Context.theory_of ctxt)
171 fun get_slices slicing slices =
172 (0 upto length slices - 1) ~~ slices |> not slicing ? (List.last #> single)
174 val metis_default_max_relevant = 20
176 fun default_max_relevant_for_prover ctxt slicing name =
177 let val thy = Proof_Context.theory_of ctxt in
178 if is_metis_prover name then
179 metis_default_max_relevant
180 else if is_atp thy name then
181 fold (Integer.max o fst o snd o snd o snd)
182 (get_slices slicing (#best_slices (get_atp thy name) ctxt)) 0
183 else (* is_smt_prover ctxt name *)
184 SMT_Solver.default_max_relevant ctxt name
187 fun is_if (@{const_name If}, _) = true
190 (* Beware of "if and only if" (which is translated as such) and "If" (which is
191 translated to conditional equations). *)
192 fun is_good_unit_equality T t u =
193 T <> @{typ bool} andalso not (exists (exists_Const is_if) [t, u])
195 fun is_unit_equality (@{const Trueprop} $ t) = is_unit_equality t
196 | is_unit_equality (Const (@{const_name all}, _) $ Abs (_, _, t)) =
198 | is_unit_equality (Const (@{const_name All}, _) $ Abs (_, _, t)) =
200 | is_unit_equality (Const (@{const_name "=="}, Type (_, [T, _])) $ t $ u) =
201 is_good_unit_equality T t u
202 | is_unit_equality (Const (@{const_name HOL.eq}, Type (_ , [T, _])) $ t $ u) =
203 is_good_unit_equality T t u
204 | is_unit_equality _ = false
206 fun is_appropriate_prop_for_prover ctxt name =
207 if is_unit_equational_atp ctxt name then is_unit_equality else K true
209 fun is_built_in_const_for_prover ctxt name =
210 if is_smt_prover ctxt name then
211 let val ctxt = ctxt |> select_smt_solver name in
213 if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
215 else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
221 fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
224 val atp_relevance_fudge =
225 {local_const_multiplier = 1.5,
226 worse_irrel_freq = 100.0,
227 higher_order_irrel_weight = 1.05,
228 abs_rel_weight = 0.5,
229 abs_irrel_weight = 2.0,
230 skolem_irrel_weight = 0.25,
231 theory_const_rel_weight = 0.5,
232 theory_const_irrel_weight = 0.25,
233 chained_const_irrel_weight = 0.25,
240 max_imperfect = 11.5,
241 max_imperfect_exp = 1.0,
242 threshold_divisor = 2.0,
243 ridiculous_threshold = 0.01}
246 val smt_relevance_fudge =
247 {local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
248 worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
249 higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
250 abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
251 abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
252 skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
253 theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
254 theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
255 chained_const_irrel_weight = #chained_const_irrel_weight atp_relevance_fudge,
256 intro_bonus = #intro_bonus atp_relevance_fudge,
257 elim_bonus = #elim_bonus atp_relevance_fudge,
258 simp_bonus = #simp_bonus atp_relevance_fudge,
259 local_bonus = #local_bonus atp_relevance_fudge,
260 assum_bonus = #assum_bonus atp_relevance_fudge,
261 chained_bonus = #chained_bonus atp_relevance_fudge,
262 max_imperfect = #max_imperfect atp_relevance_fudge,
263 max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
264 threshold_divisor = #threshold_divisor atp_relevance_fudge,
265 ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
267 fun relevance_fudge_for_prover ctxt name =
268 if is_smt_prover ctxt name then smt_relevance_fudge else atp_relevance_fudge
270 fun supported_provers ctxt =
272 val thy = Proof_Context.theory_of ctxt
273 val (remote_provers, local_provers) =
274 map fst metis_prover_infos @
275 sort_strings (supported_atps thy) @
276 sort_strings (SMT_Solver.available_solvers_of ctxt)
277 |> List.partition (String.isPrefix remote_prefix)
279 Output.urgent_message ("Supported provers: " ^
280 commas (local_provers @ remote_provers) ^ ".")
283 fun kill_provers () = Async_Manager.kill_threads das_tool "prover"
284 fun running_provers () = Async_Manager.running_threads das_tool "prover"
285 val messages = Async_Manager.thread_messages das_tool "prover"
287 (** problems, results, ATPs, etc. **)
289 datatype rich_type_sys =
290 Dumb_Type_Sys of type_sys |
291 Smart_Type_Sys of bool
298 provers: string list,
299 type_sys: rich_type_sys,
300 relevance_thresholds: real * real,
301 max_relevant: int option,
303 max_new_mono_instances: int,
305 isar_shrink_factor: int,
308 preplay_timeout: Time.time,
311 datatype prover_fact =
312 Untranslated_Fact of (string * locality) * thm |
313 SMT_Weighted_Fact of (string * locality) * (int option * thm)
315 type prover_problem =
320 facts: prover_fact list,
321 smt_filter: (string * locality) SMT_Solver.smt_filter_data option}
324 {outcome: failure option,
325 used_facts: (string * locality) list,
326 run_time_in_msecs: int option,
327 preplay: unit -> play,
328 message: play -> string,
329 message_tail: string}
332 params -> (string -> minimize_command) -> prover_problem -> prover_result
335 (* configuration attributes *)
337 (* Empty string means create files in Isabelle's temporary files directory. *)
339 Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
341 Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
342 val measure_run_time =
343 Attrib.setup_config_bool @{binding sledgehammer_measure_run_time} (K false)
345 (* In addition to being easier to read, readable names are often much shorter,
346 especially if types are mangled in names. For these reason, they are enabled
348 val atp_readable_names =
349 Attrib.setup_config_bool @{binding sledgehammer_atp_readable_names} (K true)
352 Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
354 Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
355 val smt_weight_min_facts =
356 Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
360 Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
362 Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
363 val smt_max_weight_index =
364 Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
365 val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
367 fun smt_fact_weight ctxt j num_facts =
368 if Config.get ctxt smt_weights andalso
369 num_facts >= Config.get ctxt smt_weight_min_facts then
371 val min = Config.get ctxt smt_min_weight
372 val max = Config.get ctxt smt_max_weight
373 val max_index = Config.get ctxt smt_max_weight_index
374 val curve = !smt_weight_curve
376 SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
382 fun weight_smt_fact ctxt num_facts ((info, th), j) =
383 let val thy = Proof_Context.theory_of ctxt in
384 (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
387 fun untranslated_fact (Untranslated_Fact p) = p
388 | untranslated_fact (SMT_Weighted_Fact (info, (_, th))) = (info, th)
389 fun smt_weighted_fact _ _ (SMT_Weighted_Fact p, _) = p
390 | smt_weighted_fact ctxt num_facts (fact, j) =
391 (untranslated_fact fact, j) |> weight_smt_fact ctxt num_facts
393 fun overlord_file_location_for_prover prover =
394 (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
396 fun with_path cleanup after f path =
398 |> tap (fn _ => cleanup path)
402 fun proof_banner mode name =
404 Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
405 | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
408 (* based on "Mirabelle.can_apply" and generalized *)
409 fun timed_apply timeout tac state i =
411 val {context = ctxt, facts, goal} = Proof.goal state
412 val full_tac = Method.insert_tac facts i THEN tac ctxt i
413 in TimeLimit.timeLimit timeout (try (Seq.pull o full_tac)) goal end
415 fun tac_for_reconstructor Metis =
416 Metis_Tactics.metis_tac [Metis_Tactics.partial_type_sys]
417 | tac_for_reconstructor Metis_Full_Types =
418 Metis_Tactics.metis_tac Metis_Tactics.full_type_syss
419 | tac_for_reconstructor Metis_No_Types =
420 Metis_Tactics.metis_tac [Metis_Tactics.no_type_sys]
421 | tac_for_reconstructor _ = raise Fail "unexpected reconstructor"
423 fun timed_reconstructor reconstructor debug timeout ths =
424 (Config.put Metis_Tactics.verbose debug
425 #> (fn ctxt => tac_for_reconstructor reconstructor ctxt ths))
426 |> timed_apply timeout
428 fun filter_used_facts used = filter (member (op =) used o fst)
430 fun play_one_line_proof debug timeout ths state i reconstructors =
432 fun play [] [] = Failed_to_Play (hd reconstructors)
433 | play timed_outs [] = Trust_Playable (List.last timed_outs, SOME timeout)
434 | play timed_out (reconstructor :: reconstructors) =
435 let val timer = Timer.startRealTimer () in
436 case timed_reconstructor reconstructor debug timeout ths state i of
437 SOME (SOME _) => Played (reconstructor, Timer.checkRealTimer timer)
438 | _ => play timed_out reconstructors
440 handle TimeLimit.TimeOut =>
441 play (reconstructor :: timed_out) reconstructors
443 if timeout = Time.zeroTime then Trust_Playable (hd reconstructors, NONE)
444 else play [] reconstructors
448 (* generic TPTP-based ATPs *)
450 (* Too general means, positive equality literal with a variable X as one
451 operand, when X does not occur properly in the other operand. This rules out
452 clearly inconsistent facts such as X = a | X = b, though it by no means
453 guarantees soundness. *)
455 (* Unwanted equalities are those between a (bound or schematic) variable that
456 does not properly occur in the second operand. *)
457 val is_exhaustive_finite =
459 fun is_bad_equal (Var z) t =
460 not (exists_subterm (fn Var z' => z = z' | _ => false) t)
461 | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
462 | is_bad_equal _ _ = false
463 fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
464 fun do_formula pos t =
466 (_, @{const Trueprop} $ t1) => do_formula pos t1
467 | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
469 | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
471 | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
473 | (_, @{const "==>"} $ t1 $ t2) =>
474 do_formula (not pos) t1 andalso
475 (t2 = @{prop False} orelse do_formula pos t2)
476 | (_, @{const HOL.implies} $ t1 $ t2) =>
477 do_formula (not pos) t1 andalso
478 (t2 = @{const False} orelse do_formula pos t2)
479 | (_, @{const Not} $ t1) => do_formula (not pos) t1
480 | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
481 | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
482 | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
483 | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
485 in do_formula true end
487 fun has_bound_or_var_of_type pred =
488 exists_subterm (fn Var (_, T as Type _) => pred T
489 | Abs (_, T as Type _, _) => pred T
492 (* Facts are forbidden to contain variables of these types. The typical reason
493 is that they lead to unsoundness. Note that "unit" satisfies numerous
494 equations like "?x = ()". The resulting clauses will have no type constraint,
495 yielding false proofs. Even "bool" leads to many unsound proofs, though only
496 for higher-order problems. *)
498 (* Facts containing variables of type "unit" or "bool" or of the form
499 "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
501 fun is_dangerous_prop ctxt =
503 #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
504 is_exhaustive_finite)
506 fun int_opt_add (SOME m) (SOME n) = SOME (m + n)
507 | int_opt_add _ _ = NONE
509 val atp_blacklist_max_iters = 10
510 (* Important messages are important but not so important that users want to see
512 val atp_important_message_keep_quotient = 10
514 val fallback_best_type_systems =
515 [Preds (Mangled_Monomorphic, Nonmonotonic_Types, Lightweight)]
517 fun choose_format_and_type_sys _ formats (Dumb_Type_Sys type_sys) =
518 choose_format formats type_sys
519 | choose_format_and_type_sys best_type_systems formats
520 (Smart_Type_Sys full_types) =
521 map type_sys_from_string best_type_systems @ fallback_best_type_systems
522 |> find_first (if full_types then is_type_sys_virtually_sound else K true)
523 |> the |> choose_format formats
525 val metis_minimize_max_time = seconds 2.0
527 fun choose_minimize_command minimize_command name preplay =
529 Played (reconstructor, time) =>
530 if Time.<= (time, metis_minimize_max_time) then
531 prover_name_for_reconstructor reconstructor |> the_default name
537 fun repair_monomorph_context max_iters max_new_instances =
538 Config.put Monomorph.max_rounds max_iters
539 #> Config.put Monomorph.max_new_instances max_new_instances
540 #> Config.put Monomorph.keep_partial_instances false
542 fun run_atp mode name
543 ({exec, required_execs, arguments, proof_delims, known_failures,
544 conj_sym_kind, prem_kind, formats, best_slices, ...} : atp_config)
545 ({debug, verbose, overlord, type_sys, max_relevant, max_mono_iters,
546 max_new_mono_instances, isar_proof, isar_shrink_factor, slicing,
547 timeout, preplay_timeout, ...} : params)
549 ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
551 val thy = Proof.theory_of state
552 val ctxt = Proof.context_of state
553 val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
554 val (dest_dir, problem_prefix) =
555 if overlord then overlord_file_location_for_prover name
556 else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
557 val problem_file_name =
558 Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
559 "_" ^ string_of_int subgoal)
560 val problem_path_name =
561 if dest_dir = "" then
562 File.tmp_path problem_file_name
563 else if File.exists (Path.explode dest_dir) then
564 Path.append (Path.explode dest_dir) problem_file_name
566 error ("No such directory: " ^ quote dest_dir ^ ".")
567 val measure_run_time = verbose orelse Config.get ctxt measure_run_time
568 val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
571 val split = String.tokens (fn c => str c = "\n")
572 val (output, t) = s |> split |> split_last |> apfst cat_lines
573 fun as_num f = f >> (fst o read_int)
574 val num = as_num (Scan.many1 Symbol.is_ascii_digit)
575 val digit = Scan.one Symbol.is_ascii_digit
576 val num3 = as_num (digit ::: digit ::: (digit >> single))
577 val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
578 val as_time = Scan.read Symbol.stopper time o raw_explode
579 in (output, as_time t) end
580 fun run_on prob_file =
581 case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
582 (home_var, _) :: _ =>
583 error ("The environment variable " ^ quote home_var ^ " is not set.")
585 if File.exists command then
587 (* If slicing is disabled, we expand the last slice to fill the
588 entire time available. *)
589 val actual_slices = get_slices slicing (best_slices ctxt)
590 val num_actual_slices = length actual_slices
591 fun monomorphize_facts facts =
594 ctxt |> repair_monomorph_context max_mono_iters
595 max_new_mono_instances
596 (* pseudo-theorem involving the same constants as the subgoal *)
598 Logic.list_implies (hyp_ts, concl_t)
599 |> Skip_Proof.make_thm thy
601 facts |> chop (length facts div 4)
602 |>> map (pair 1 o snd)
603 ||> map (pair 2 o snd)
605 |> cons (0, subgoal_th)
607 Monomorph.monomorph atp_schematic_consts_of rths ctxt
609 |> curry ListPair.zip (map fst facts)
610 |> maps (fn (name, rths) => map (pair name o snd) rths)
612 fun run_slice blacklist (slice, (time_frac, (complete,
613 (best_max_relevant, best_type_systems))))
617 length facts |> is_none max_relevant
618 ? Integer.min best_max_relevant
619 val (format, type_sys) =
620 choose_format_and_type_sys best_type_systems formats type_sys
621 val fairly_sound = is_type_sys_fairly_sound type_sys
623 facts |> map untranslated_fact
625 ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
627 |> not (null blacklist)
628 ? filter_out (member (op =) blacklist o fst)
629 |> polymorphism_of_type_sys type_sys <> Polymorphic
631 |> map (apsnd prop_of)
632 val real_ms = Real.fromInt o Time.toMilliseconds
635 |> (if slice < num_actual_slices - 1 then
636 curry Real.min (time_frac * real_ms timeout)
642 quote name ^ " slice #" ^ string_of_int (slice + 1) ^
643 " with " ^ string_of_int num_facts ^ " fact" ^
644 plural_s num_facts ^ " for " ^
645 string_from_time slice_timeout ^ "..."
646 |> Output.urgent_message
649 val (atp_problem, pool, conjecture_offset, facts_offset,
650 fact_names, typed_helpers, sym_tab) =
651 prepare_atp_problem ctxt format conj_sym_kind prem_kind
652 type_sys true (Config.get ctxt atp_readable_names) true
654 fun weights () = atp_problem_weights atp_problem
656 File.shell_path command ^ " " ^
657 arguments ctxt slice slice_timeout weights ^ " " ^
658 File.shell_path prob_file
660 (if measure_run_time then
661 "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
663 "exec " ^ core) ^ " 2>&1"
666 |> tptp_lines_for_atp_problem format
667 |> cons ("% " ^ command ^ "\n")
668 |> File.write_list prob_file
669 val conjecture_shape =
670 conjecture_offset + 1
671 upto conjecture_offset + length hyp_ts + 1
673 val ((output, msecs), res_code) =
675 |>> (if overlord then
676 prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
679 |>> (if measure_run_time then split_time else rpair NONE)
680 val (atp_proof, outcome) =
681 extract_tstplike_proof_and_outcome verbose complete res_code
682 proof_delims known_failures output
683 |>> atp_proof_from_tstplike_proof atp_problem
684 handle UNRECOGNIZED_ATP_PROOF () => ([], SOME ProofIncomplete)
685 val (conjecture_shape, facts_offset, fact_names,
687 if is_none outcome then
688 repair_conjecture_shape_and_fact_names output
689 conjecture_shape facts_offset fact_names typed_helpers
691 (* don't bother repairing *)
692 (conjecture_shape, facts_offset, fact_names, typed_helpers)
696 used_facts_in_unsound_atp_proof ctxt
697 conjecture_shape facts_offset fact_names atp_proof
698 |> Option.map (fn facts =>
699 UnsoundProof (is_type_sys_virtually_sound type_sys,
700 facts |> sort string_ord))
702 if null blacklist then outcome else SOME GaveUp
705 ((pool, conjecture_shape, facts_offset, fact_names,
706 typed_helpers, sym_tab),
707 (output, msecs, atp_proof, outcome))
709 val timer = Timer.startRealTimer ()
710 fun maybe_run_slice blacklist slice
711 (result as (_, (_, msecs0, _, SOME _))) =
713 val time_left = Time.- (timeout, Timer.checkRealTimer timer)
715 if Time.<= (time_left, Time.zeroTime) then
718 (run_slice blacklist slice time_left
719 |> (fn (stuff, (output, msecs, atp_proof, outcome)) =>
720 (stuff, (output, int_opt_add msecs0 msecs,
721 atp_proof, outcome))))
723 | maybe_run_slice _ _ result = result
724 fun maybe_blacklist_facts_and_retry iter blacklist
725 (result as ((_, _, facts_offset, fact_names, _, _),
727 SOME (UnsoundProof (false, _))))) =
728 (case used_facts_in_atp_proof ctxt facts_offset fact_names
732 if slicing andalso iter < atp_blacklist_max_iters - 1 then
733 let val blacklist = new_baddies @ blacklist in
735 |> maybe_run_slice blacklist (List.last actual_slices)
736 |> maybe_blacklist_facts_and_retry (iter + 1) blacklist
740 | maybe_blacklist_facts_and_retry _ _ result = result
742 ((Symtab.empty, [], 0, Vector.fromList [], [], Symtab.empty),
743 ("", SOME 0, [], SOME InternalError))
744 |> fold (maybe_run_slice []) actual_slices
745 (* The ATP found an unsound proof? Automatically try again
746 without the offending facts! *)
747 |> maybe_blacklist_facts_and_retry 0 []
750 error ("Bad executable: " ^ Path.print command ^ ".")
752 (* If the problem file has not been exported, remove it; otherwise, export
753 the proof file too. *)
754 fun cleanup prob_file =
755 if dest_dir = "" then try File.rm prob_file else NONE
756 fun export prob_file (_, (output, _, _, _)) =
757 if dest_dir = "" then
760 File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
761 val ((pool, conjecture_shape, facts_offset, fact_names, typed_helpers,
763 (output, msecs, atp_proof, outcome)) =
764 with_path cleanup export run_on problem_path_name
765 val important_message =
766 if mode = Normal andalso
767 random_range 0 (atp_important_message_keep_quotient - 1) = 0 then
768 extract_important_message output
771 val (used_facts, preplay, message, message_tail) =
776 used_facts_in_atp_proof ctxt facts_offset fact_names atp_proof
782 facts |> map untranslated_fact |> filter_used_facts used_facts
785 play_one_line_proof debug preplay_timeout used_ths state subgoal
790 val full_types = uses_typed_helpers typed_helpers atp_proof
792 (debug, full_types, isar_shrink_factor, pool,
793 conjecture_shape, facts_offset, fact_names, sym_tab,
795 val one_line_params =
796 (preplay, proof_banner mode name, used_facts,
797 choose_minimize_command minimize_command name preplay,
798 subgoal, subgoal_count)
799 in proof_text ctxt isar_proof isar_params one_line_params end,
801 "\nATP real CPU time: " ^
802 string_from_time (Time.fromMilliseconds (the msecs)) ^ "."
805 (if important_message <> "" then
806 "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
812 ([], K (Failed_to_Play Metis), fn _ => string_for_failure failure, "")
814 {outcome = outcome, used_facts = used_facts, run_time_in_msecs = msecs,
815 preplay = preplay, message = message, message_tail = message_tail}
818 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
819 these are sorted out properly in the SMT module, we have to interpret these
821 val remote_smt_failures =
824 val z3_wrapper_failures =
825 [(11, InternalError),
829 [(101, OutOfResources),
830 (103, MalformedInput),
831 (110, MalformedInput)]
835 remote_smt_failures @ z3_wrapper_failures @ z3_failures @ unix_failures
837 fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
838 if is_real_cex then Unprovable else GaveUp
839 | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
840 | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
841 (case AList.lookup (op =) smt_failures code of
842 SOME failure => failure
843 | NONE => UnknownError ("Abnormal termination with exit code " ^
844 string_of_int code ^ "."))
845 | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
846 | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
851 Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
852 val smt_slice_fact_frac =
853 Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.5)
854 val smt_slice_time_frac =
855 Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.5)
856 val smt_slice_min_secs =
857 Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 5)
859 fun smt_filter_loop ctxt name
860 ({debug, verbose, overlord, max_mono_iters,
861 max_new_mono_instances, timeout, slicing, ...} : params)
864 val max_slices = if slicing then Config.get ctxt smt_max_slices else 1
866 select_smt_solver name
868 Config.put SMT_Config.debug_files
869 (overlord_file_location_for_prover name
870 |> (fn (path, name) => path ^ "/" ^ name))
873 #> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
874 val state = state |> Proof.map_context repair_context
875 fun do_slice timeout slice outcome0 time_so_far facts =
877 val timer = Timer.startRealTimer ()
879 state |> Proof.map_context
880 (repair_monomorph_context max_mono_iters
881 max_new_mono_instances)
882 val ms = timeout |> Time.toMilliseconds
884 if slice < max_slices then
886 Int.max (1000 * Config.get ctxt smt_slice_min_secs,
887 Real.ceil (Config.get ctxt smt_slice_time_frac
889 |> Time.fromMilliseconds
892 val num_facts = length facts
895 quote name ^ " slice " ^ string_of_int slice ^ " with " ^
896 string_of_int num_facts ^ " fact" ^ plural_s num_facts ^ " for " ^
897 string_from_time slice_timeout ^ "..."
898 |> Output.urgent_message
901 val birth = Timer.checkRealTimer timer
903 if debug then Output.urgent_message "Invoking SMT solver..." else ()
904 val (outcome, used_facts) =
905 (case (slice, smt_filter) of
906 (1, SOME head) => head |> apsnd (apsnd repair_context)
907 | _ => SMT_Solver.smt_filter_preprocess state facts i)
908 |> SMT_Solver.smt_filter_apply slice_timeout
909 |> (fn {outcome, used_facts} => (outcome, used_facts))
910 handle exn => if Exn.is_interrupt exn then
913 (ML_Compiler.exn_message exn
914 |> SMT_Failure.Other_Failure |> SOME, [])
915 val death = Timer.checkRealTimer timer
916 val outcome0 = if is_none outcome0 then SOME outcome else outcome0
917 val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
918 val too_many_facts_perhaps =
921 | SOME (SMT_Failure.Counterexample _) => false
922 | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
923 | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
924 | SOME SMT_Failure.Out_Of_Memory => true
925 | SOME (SMT_Failure.Other_Failure _) => true
926 val timeout = Time.- (timeout, Timer.checkRealTimer timer)
928 if too_many_facts_perhaps andalso slice < max_slices andalso
929 num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
932 Real.ceil (Config.get ctxt smt_slice_fact_frac
933 * Real.fromInt num_facts)
935 if verbose andalso is_some outcome then
936 quote name ^ " invoked with " ^ string_of_int num_facts ^
937 " fact" ^ plural_s num_facts ^ ": " ^
938 string_for_failure (failure_from_smt_failure (the outcome)) ^
939 " Retrying with " ^ string_of_int new_num_facts ^ " fact" ^
940 plural_s new_num_facts ^ "..."
941 |> Output.urgent_message
945 facts |> take new_num_facts
946 |> do_slice timeout (slice + 1) outcome0 time_so_far
949 {outcome = if is_none outcome then NONE else the outcome0,
950 used_facts = used_facts,
951 run_time_in_msecs = SOME (Time.toMilliseconds time_so_far)}
953 in do_slice timeout 1 NONE Time.zeroTime end
955 fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...})
957 ({state, subgoal, subgoal_count, facts, smt_filter, ...}
960 val ctxt = Proof.context_of state
961 val num_facts = length facts
962 val facts = facts ~~ (0 upto num_facts - 1)
963 |> map (smt_weighted_fact ctxt num_facts)
964 val {outcome, used_facts, run_time_in_msecs} =
965 smt_filter_loop ctxt name params state subgoal smt_filter facts
966 val (used_facts, used_ths) = used_facts |> ListPair.unzip
967 val outcome = outcome |> Option.map failure_from_smt_failure
968 val (preplay, message, message_tail) =
973 fun smt_settings () =
974 if name = SMT_Solver.solver_name_of ctxt then ""
975 else "smt_solver = " ^ maybe_quote name
977 case play_one_line_proof debug preplay_timeout used_ths state
978 subgoal metis_reconstructors of
980 | _ => Trust_Playable (SMT (smt_settings ()), NONE)
984 val one_line_params =
985 (preplay, proof_banner mode name, used_facts,
986 choose_minimize_command minimize_command name preplay,
987 subgoal, subgoal_count)
988 in one_line_proof_text one_line_params end,
990 "\nSMT solver real CPU time: " ^
991 string_from_time (Time.fromMilliseconds
992 (the run_time_in_msecs)) ^ "."
996 (K (Failed_to_Play Metis), fn _ => string_for_failure failure, "")
998 {outcome = outcome, used_facts = used_facts,
999 run_time_in_msecs = run_time_in_msecs, preplay = preplay,
1000 message = message, message_tail = message_tail}
1003 fun run_metis mode name ({debug, timeout, ...} : params) minimize_command
1004 ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
1007 case AList.lookup (op =) metis_prover_infos name of
1009 | NONE => raise Fail ("unknown Metis version: " ^ quote name)
1010 val (used_facts, used_ths) =
1011 facts |> map untranslated_fact |> ListPair.unzip
1013 case play_one_line_proof debug timeout used_ths state subgoal
1015 play as Played (_, time) =>
1016 {outcome = NONE, used_facts = used_facts,
1017 run_time_in_msecs = SOME (Time.toMilliseconds time),
1019 message = fn play =>
1021 val one_line_params =
1022 (play, proof_banner mode name, used_facts,
1023 minimize_command name, subgoal, subgoal_count)
1024 in one_line_proof_text one_line_params end,
1028 val failure = case play of Failed_to_Play _ => GaveUp | _ => TimedOut
1030 {outcome = SOME failure, used_facts = [], run_time_in_msecs = NONE,
1031 preplay = K play, message = fn _ => string_for_failure failure,
1036 fun get_prover ctxt mode name =
1037 let val thy = Proof_Context.theory_of ctxt in
1038 if is_metis_prover name then run_metis mode name
1039 else if is_atp thy name then run_atp mode name (get_atp thy name)
1040 else if is_smt_prover ctxt name then run_smt_solver mode name
1041 else error ("No such prover: " ^ name ^ ".")