src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Sun May 01 18:37:23 2011 +0200 (2011-05-01)
changeset 42520 d1f7c4a01dbe
parent 42452 f7f796ce5d68
child 42521 02df3b78a438
permissions -rw-r--r--
renamings
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_provers.ML
     2     Author:     Fabian Immler, TU Muenchen
     3     Author:     Makarius
     4     Author:     Jasmin Blanchette, TU Muenchen
     5 
     6 Generic prover abstraction for Sledgehammer.
     7 *)
     8 
     9 signature SLEDGEHAMMER_PROVERS =
    10 sig
    11   type failure = ATP_Proof.failure
    12   type locality = Sledgehammer_Filter.locality
    13   type relevance_fudge = Sledgehammer_Filter.relevance_fudge
    14   type translated_formula = Sledgehammer_ATP_Translate.translated_formula
    15   type type_system = Sledgehammer_ATP_Translate.type_system
    16   type minimize_command = Sledgehammer_ATP_Reconstruct.minimize_command
    17 
    18   type params =
    19     {debug: bool,
    20      verbose: bool,
    21      overlord: bool,
    22      blocking: bool,
    23      provers: string list,
    24      relevance_thresholds: real * real,
    25      max_relevant: int option,
    26      monomorphize: bool,
    27      monomorphize_limit: int,
    28      type_sys: type_system,
    29      explicit_apply: bool,
    30      isar_proof: bool,
    31      isar_shrink_factor: int,
    32      slicing: bool,
    33      timeout: Time.time,
    34      expect: string}
    35 
    36   datatype prover_fact =
    37     Untranslated_Fact of (string * locality) * thm |
    38     ATP_Translated_Fact of
    39       translated_formula option * ((string * locality) * thm) |
    40     SMT_Weighted_Fact of (string * locality) * (int option * thm)
    41 
    42   type prover_problem =
    43     {state: Proof.state,
    44      goal: thm,
    45      subgoal: int,
    46      subgoal_count: int,
    47      facts: prover_fact list,
    48      smt_filter: (string * locality) SMT_Solver.smt_filter_data option}
    49 
    50   type prover_result =
    51     {outcome: failure option,
    52      used_facts: (string * locality) list,
    53      run_time_in_msecs: int option,
    54      message: string}
    55 
    56   type prover = params -> minimize_command -> prover_problem -> prover_result
    57 
    58   (* for experimentation purposes -- do not use in production code *)
    59   val smt_triggers : bool Unsynchronized.ref
    60   val smt_weights : bool Unsynchronized.ref
    61   val smt_weight_min_facts : int Unsynchronized.ref
    62   val smt_min_weight : int Unsynchronized.ref
    63   val smt_max_weight : int Unsynchronized.ref
    64   val smt_max_weight_index : int Unsynchronized.ref
    65   val smt_weight_curve : (int -> int) Unsynchronized.ref
    66   val smt_max_slices : int Unsynchronized.ref
    67   val smt_slice_fact_frac : real Unsynchronized.ref
    68   val smt_slice_time_frac : real Unsynchronized.ref
    69   val smt_slice_min_secs : int Unsynchronized.ref
    70 
    71   val das_Tool : string
    72   val select_smt_solver : string -> Proof.context -> Proof.context
    73   val is_smt_prover : Proof.context -> string -> bool
    74   val is_prover_supported : Proof.context -> string -> bool
    75   val is_prover_installed : Proof.context -> string -> bool
    76   val default_max_relevant_for_prover : Proof.context -> bool -> string -> int
    77   val is_built_in_const_for_prover :
    78     Proof.context -> string -> string * typ -> term list -> bool * term list
    79   val atp_relevance_fudge : relevance_fudge
    80   val smt_relevance_fudge : relevance_fudge
    81   val relevance_fudge_for_prover : Proof.context -> string -> relevance_fudge
    82   val dest_dir : string Config.T
    83   val problem_prefix : string Config.T
    84   val measure_run_time : bool Config.T
    85   val weight_smt_fact :
    86     theory -> int -> ((string * locality) * thm) * int
    87     -> (string * locality) * (int option * thm)
    88   val untranslated_fact : prover_fact -> (string * locality) * thm
    89   val smt_weighted_fact :
    90     theory -> int -> prover_fact * int
    91     -> (string * locality) * (int option * thm)
    92   val supported_provers : Proof.context -> unit
    93   val kill_provers : unit -> unit
    94   val running_provers : unit -> unit
    95   val messages : int option -> unit
    96   val get_prover : Proof.context -> bool -> string -> prover
    97   val setup : theory -> theory
    98 end;
    99 
   100 structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
   101 struct
   102 
   103 open ATP_Problem
   104 open ATP_Proof
   105 open ATP_Systems
   106 open Metis_Translate
   107 open Sledgehammer_Util
   108 open Sledgehammer_Filter
   109 open Sledgehammer_ATP_Translate
   110 open Sledgehammer_ATP_Reconstruct
   111 
   112 (** The Sledgehammer **)
   113 
   114 (* Identifier to distinguish Sledgehammer from other tools using
   115    "Async_Manager". *)
   116 val das_Tool = "Sledgehammer"
   117 
   118 val select_smt_solver =
   119   Context.proof_map o SMT_Config.select_solver
   120 
   121 fun is_smt_prover ctxt name =
   122   member (op =) (SMT_Solver.available_solvers_of ctxt) name
   123 
   124 fun is_prover_supported ctxt name =
   125   let val thy = Proof_Context.theory_of ctxt in
   126     is_smt_prover ctxt name orelse member (op =) (supported_atps thy) name
   127   end
   128 
   129 fun is_prover_installed ctxt =
   130   is_smt_prover ctxt orf is_atp_installed (Proof_Context.theory_of ctxt)
   131 
   132 fun get_slices slicing slices =
   133   (0 upto length slices - 1) ~~ slices |> not slicing ? (List.last #> single)
   134 
   135 fun default_max_relevant_for_prover ctxt slicing name =
   136   let val thy = Proof_Context.theory_of ctxt in
   137     if is_smt_prover ctxt name then
   138       SMT_Solver.default_max_relevant ctxt name
   139     else
   140       fold (Integer.max o snd o snd o snd)
   141            (get_slices slicing (#slices (get_atp thy name) ())) 0
   142   end
   143 
   144 (* These are either simplified away by "Meson.presimplify" (most of the time) or
   145    handled specially via "fFalse", "fTrue", ..., "fequal". *)
   146 val atp_irrelevant_consts =
   147   [@{const_name False}, @{const_name True}, @{const_name Not},
   148    @{const_name conj}, @{const_name disj}, @{const_name implies},
   149    @{const_name HOL.eq}, @{const_name If}, @{const_name Let}]
   150 
   151 fun is_built_in_const_for_prover ctxt name =
   152   if is_smt_prover ctxt name then
   153     let val ctxt = ctxt |> select_smt_solver name in
   154       fn x => fn ts =>
   155          if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
   156            (true, [])
   157          else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
   158            (true, ts)
   159          else
   160            (false, ts)
   161     end
   162   else
   163     fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
   164 
   165 (* FUDGE *)
   166 val atp_relevance_fudge =
   167   {allow_ext = true,
   168    local_const_multiplier = 1.5,
   169    worse_irrel_freq = 100.0,
   170    higher_order_irrel_weight = 1.05,
   171    abs_rel_weight = 0.5,
   172    abs_irrel_weight = 2.0,
   173    skolem_irrel_weight = 0.75,
   174    theory_const_rel_weight = 0.5,
   175    theory_const_irrel_weight = 0.25,
   176    intro_bonus = 0.15,
   177    elim_bonus = 0.15,
   178    simp_bonus = 0.15,
   179    local_bonus = 0.55,
   180    assum_bonus = 1.05,
   181    chained_bonus = 1.5,
   182    max_imperfect = 11.5,
   183    max_imperfect_exp = 1.0,
   184    threshold_divisor = 2.0,
   185    ridiculous_threshold = 0.01}
   186 
   187 (* FUDGE (FIXME) *)
   188 val smt_relevance_fudge =
   189   {allow_ext = false,
   190    local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
   191    worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
   192    higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
   193    abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
   194    abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
   195    skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
   196    theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
   197    theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
   198    intro_bonus = #intro_bonus atp_relevance_fudge,
   199    elim_bonus = #elim_bonus atp_relevance_fudge,
   200    simp_bonus = #simp_bonus atp_relevance_fudge,
   201    local_bonus = #local_bonus atp_relevance_fudge,
   202    assum_bonus = #assum_bonus atp_relevance_fudge,
   203    chained_bonus = #chained_bonus atp_relevance_fudge,
   204    max_imperfect = #max_imperfect atp_relevance_fudge,
   205    max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
   206    threshold_divisor = #threshold_divisor atp_relevance_fudge,
   207    ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
   208 
   209 fun relevance_fudge_for_prover ctxt name =
   210   if is_smt_prover ctxt name then smt_relevance_fudge else atp_relevance_fudge
   211 
   212 fun supported_provers ctxt =
   213   let
   214     val thy = Proof_Context.theory_of ctxt
   215     val (remote_provers, local_provers) =
   216       sort_strings (supported_atps thy) @
   217       sort_strings (SMT_Solver.available_solvers_of ctxt)
   218       |> List.partition (String.isPrefix remote_prefix)
   219   in
   220     Output.urgent_message ("Supported provers: " ^
   221                            commas (local_provers @ remote_provers) ^ ".")
   222   end
   223 
   224 fun kill_provers () = Async_Manager.kill_threads das_Tool "provers"
   225 fun running_provers () = Async_Manager.running_threads das_Tool "provers"
   226 val messages = Async_Manager.thread_messages das_Tool "prover"
   227 
   228 (** problems, results, ATPs, etc. **)
   229 
   230 type params =
   231   {debug: bool,
   232    verbose: bool,
   233    overlord: bool,
   234    blocking: bool,
   235    provers: string list,
   236    relevance_thresholds: real * real,
   237    max_relevant: int option,
   238    monomorphize: bool,
   239    monomorphize_limit: int,
   240    type_sys: type_system,
   241    explicit_apply: bool,
   242    isar_proof: bool,
   243    isar_shrink_factor: int,
   244    slicing: bool,
   245    timeout: Time.time,
   246    expect: string}
   247 
   248 datatype prover_fact =
   249   Untranslated_Fact of (string * locality) * thm |
   250   ATP_Translated_Fact of
   251     translated_formula option * ((string * locality) * thm) |
   252   SMT_Weighted_Fact of (string * locality) * (int option * thm)
   253 
   254 type prover_problem =
   255   {state: Proof.state,
   256    goal: thm,
   257    subgoal: int,
   258    subgoal_count: int,
   259    facts: prover_fact list,
   260    smt_filter: (string * locality) SMT_Solver.smt_filter_data option}
   261 
   262 type prover_result =
   263   {outcome: failure option,
   264    message: string,
   265    used_facts: (string * locality) list,
   266    run_time_in_msecs: int option}
   267 
   268 type prover = params -> minimize_command -> prover_problem -> prover_result
   269 
   270 (* configuration attributes *)
   271 
   272 val (dest_dir, dest_dir_setup) =
   273   Attrib.config_string "sledgehammer_dest_dir" (K "")
   274   (* Empty string means create files in Isabelle's temporary files directory. *)
   275 
   276 val (problem_prefix, problem_prefix_setup) =
   277   Attrib.config_string "sledgehammer_problem_prefix" (K "prob")
   278 
   279 val (measure_run_time, measure_run_time_setup) =
   280   Attrib.config_bool "sledgehammer_measure_run_time" (K false)
   281 
   282 fun with_path cleanup after f path =
   283   Exn.capture f path
   284   |> tap (fn _ => cleanup path)
   285   |> Exn.release
   286   |> tap (after path)
   287 
   288 fun proof_banner auto =
   289   if auto then "Auto Sledgehammer found a proof" else "Try this command"
   290 
   291 val smt_triggers = Unsynchronized.ref true
   292 val smt_weights = Unsynchronized.ref true
   293 val smt_weight_min_facts = Unsynchronized.ref 20
   294 
   295 (* FUDGE *)
   296 val smt_min_weight = Unsynchronized.ref 0
   297 val smt_max_weight = Unsynchronized.ref 10
   298 val smt_max_weight_index = Unsynchronized.ref 200
   299 val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
   300 
   301 fun smt_fact_weight j num_facts =
   302   if !smt_weights andalso num_facts >= !smt_weight_min_facts then
   303     SOME (!smt_max_weight
   304           - (!smt_max_weight - !smt_min_weight + 1)
   305             * !smt_weight_curve (Int.max (0, !smt_max_weight_index - j - 1))
   306             div !smt_weight_curve (!smt_max_weight_index))
   307   else
   308     NONE
   309 
   310 fun weight_smt_fact thy num_facts ((info, th), j) =
   311   (info, (smt_fact_weight j num_facts, th |> Thm.transfer thy))
   312 
   313 fun untranslated_fact (Untranslated_Fact p) = p
   314   | untranslated_fact (ATP_Translated_Fact (_, p)) = p
   315   | untranslated_fact (SMT_Weighted_Fact (info, (_, th))) = (info, th)
   316 fun atp_translated_fact _ (ATP_Translated_Fact p) = p
   317   | atp_translated_fact ctxt fact =
   318     translate_atp_fact ctxt false (untranslated_fact fact)
   319 fun smt_weighted_fact _ _ (SMT_Weighted_Fact p, _) = p
   320   | smt_weighted_fact thy num_facts (fact, j) =
   321     (untranslated_fact fact, j) |> weight_smt_fact thy num_facts
   322 
   323 fun overlord_file_location_for_prover prover =
   324   (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
   325 
   326 
   327 (* generic TPTP-based ATPs *)
   328 
   329 fun int_opt_add (SOME m) (SOME n) = SOME (m + n)
   330   | int_opt_add _ _ = NONE
   331 
   332 val atp_blacklist_max_iters = 10
   333 (* Important messages are important but not so important that users want to see
   334    them each time. *)
   335 val atp_important_message_keep_factor = 0.1
   336 
   337 fun run_atp auto name
   338         ({exec, required_execs, arguments, slices, proof_delims, known_failures,
   339           explicit_forall, use_conjecture_for_hypotheses, ...} : atp_config)
   340         ({debug, verbose, overlord, max_relevant, monomorphize,
   341           monomorphize_limit, type_sys, explicit_apply, isar_proof,
   342           isar_shrink_factor, slicing, timeout, ...} : params)
   343         minimize_command ({state, goal, subgoal, facts, ...} : prover_problem) =
   344   let
   345     val thy = Proof.theory_of state
   346     val ctxt = Proof.context_of state
   347     val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal
   348     val (dest_dir, problem_prefix) =
   349       if overlord then overlord_file_location_for_prover name
   350       else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
   351     val problem_file_name =
   352       Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
   353                   "_" ^ string_of_int subgoal)
   354     val problem_path_name =
   355       if dest_dir = "" then
   356         File.tmp_path problem_file_name
   357       else if File.exists (Path.explode dest_dir) then
   358         Path.append (Path.explode dest_dir) problem_file_name
   359       else
   360         error ("No such directory: " ^ quote dest_dir ^ ".")
   361     val measure_run_time = verbose orelse Config.get ctxt measure_run_time
   362     val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
   363     fun split_time s =
   364       let
   365         val split = String.tokens (fn c => str c = "\n")
   366         val (output, t) = s |> split |> split_last |> apfst cat_lines
   367         fun as_num f = f >> (fst o read_int)
   368         val num = as_num (Scan.many1 Symbol.is_ascii_digit)
   369         val digit = Scan.one Symbol.is_ascii_digit
   370         val num3 = as_num (digit ::: digit ::: (digit >> single))
   371         val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
   372         val as_time = Scan.read Symbol.stopper time o raw_explode
   373       in (output, as_time t) end
   374     fun run_on prob_file =
   375       case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
   376         (home_var, _) :: _ =>
   377         error ("The environment variable " ^ quote home_var ^ " is not set.")
   378       | [] =>
   379         if File.exists command then
   380           let
   381             val readable_names = debug andalso overlord
   382             (* If slicing is disabled, we expand the last slice to fill the
   383                entire time available. *)
   384             val actual_slices = get_slices slicing (slices ())
   385             val num_actual_slices = length actual_slices
   386             fun monomorphize_facts facts =
   387               let
   388                 val repair_context =
   389                   Config.put SMT_Config.verbose debug
   390                   #> Config.put SMT_Config.monomorph_full false
   391                   #> Config.put SMT_Config.monomorph_limit monomorphize_limit
   392                 val facts = facts |> map untranslated_fact
   393                 (* pseudo-theorem involving the same constants as the subgoal *)
   394                 val subgoal_th =
   395                   Logic.list_implies (hyp_ts, concl_t)
   396                   |> Skip_Proof.make_thm thy
   397                 val indexed_facts =
   398                   (~1, subgoal_th) :: (0 upto length facts - 1 ~~ map snd facts)
   399               in
   400                 SMT_Monomorph.monomorph indexed_facts (repair_context ctxt)
   401                 |> fst |> sort (int_ord o pairself fst)
   402                 |> filter_out (curry (op =) ~1 o fst)
   403                 |> map (Untranslated_Fact o apfst (fst o nth facts))
   404               end
   405             fun run_slice blacklist
   406                           (slice, (time_frac, (complete, default_max_relevant)))
   407                           time_left =
   408               let
   409                 val num_facts =
   410                   length facts |> is_none max_relevant
   411                                   ? Integer.min default_max_relevant
   412                 val facts =
   413                   facts |> take num_facts
   414                         |> not (null blacklist)
   415                            ? filter_out (member (op =) blacklist o fst
   416                                          o untranslated_fact)
   417                         |> monomorphize ? monomorphize_facts
   418                         |> map (atp_translated_fact ctxt)
   419                 val real_ms = Real.fromInt o Time.toMilliseconds
   420                 val slice_timeout =
   421                   ((real_ms time_left
   422                     |> (if slice < num_actual_slices - 1 then
   423                           curry Real.min (time_frac * real_ms timeout)
   424                         else
   425                           I))
   426                    * 0.001) |> seconds
   427                 val _ =
   428                   if verbose then
   429                     "ATP slice " ^ string_of_int (slice + 1) ^ " with " ^
   430                     string_of_int num_facts ^ " fact" ^ plural_s num_facts ^
   431                     " for " ^ string_from_time slice_timeout ^ "..."
   432                     |> Output.urgent_message
   433                   else
   434                     ()
   435                 val (atp_problem, pool, conjecture_offset, fact_names) =
   436                   prepare_atp_problem ctxt readable_names explicit_forall
   437                                       type_sys explicit_apply hyp_ts concl_t
   438                                       facts
   439                 fun weights () = atp_problem_weights atp_problem
   440                 val core =
   441                   File.shell_path command ^ " " ^
   442                   arguments slice slice_timeout weights ^ " " ^
   443                   File.shell_path prob_file
   444                 val command =
   445                   (if measure_run_time then
   446                      "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
   447                    else
   448                      "exec " ^ core) ^ " 2>&1"
   449                 val _ =
   450                   atp_problem
   451                   |> tptp_strings_for_atp_problem use_conjecture_for_hypotheses
   452                   |> cons ("% " ^ command ^ "\n")
   453                   |> File.write_list prob_file
   454                 val conjecture_shape =
   455                   conjecture_offset + 1
   456                     upto conjecture_offset + length hyp_ts + 1
   457                   |> map single
   458                 val ((output, msecs), res_code) =
   459                   bash_output command
   460                   |>> (if overlord then
   461                          prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
   462                        else
   463                          I)
   464                   |>> (if measure_run_time then split_time else rpair NONE)
   465                 val (atp_proof, outcome) =
   466                   extract_tstplike_proof_and_outcome debug verbose complete
   467                       res_code proof_delims known_failures output
   468                   |>> atp_proof_from_tstplike_proof
   469                 val (conjecture_shape, fact_names) =
   470                   if is_none outcome then
   471                     repair_conjecture_shape_and_fact_names output
   472                         conjecture_shape fact_names
   473                   else
   474                     (conjecture_shape, fact_names) (* don't bother repairing *)
   475                 val outcome =
   476                   case outcome of
   477                     NONE => if not (is_type_system_sound type_sys) andalso
   478                                is_unsound_proof conjecture_shape fact_names
   479                                                 atp_proof then
   480                               SOME UnsoundProof
   481                             else
   482                               NONE
   483                   | SOME Unprovable =>
   484                     if null blacklist then outcome
   485                     else SOME IncompleteUnprovable
   486                   | _ => outcome
   487               in
   488                 ((pool, conjecture_shape, fact_names),
   489                  (output, msecs, atp_proof, outcome))
   490               end
   491             val timer = Timer.startRealTimer ()
   492             fun maybe_run_slice blacklist slice
   493                                 (result as (_, (_, msecs0, _, SOME _))) =
   494                 let
   495                   val time_left = Time.- (timeout, Timer.checkRealTimer timer)
   496                 in
   497                   if Time.<= (time_left, Time.zeroTime) then
   498                     result
   499                   else
   500                     (run_slice blacklist slice time_left
   501                      |> (fn (stuff, (output, msecs, atp_proof, outcome)) =>
   502                             (stuff, (output, int_opt_add msecs0 msecs, atp_proof,
   503                                      outcome))))
   504                 end
   505               | maybe_run_slice _ _ result = result
   506             fun maybe_blacklist_facts_and_retry iter blacklist
   507                     (result as ((_, _, fact_names),
   508                                 (_, _, atp_proof, SOME UnsoundProof))) =
   509                 (case used_facts_in_atp_proof fact_names atp_proof of
   510                    [] => result
   511                  | new_baddies =>
   512                    let val blacklist = new_baddies @ blacklist in
   513                      result
   514                      |> maybe_run_slice blacklist (List.last actual_slices)
   515                      |> iter < atp_blacklist_max_iters
   516                         ? maybe_blacklist_facts_and_retry (iter + 1) blacklist
   517                    end)
   518               | maybe_blacklist_facts_and_retry _ _ result = result
   519           in
   520             ((Symtab.empty, [], Vector.fromList []),
   521              ("", SOME 0, [], SOME InternalError))
   522             |> fold (maybe_run_slice []) actual_slices
   523                (* The ATP found an unsound proof? Automatically try again
   524                   without the offending facts! *)
   525             |> maybe_blacklist_facts_and_retry 0 []
   526           end
   527         else
   528           error ("Bad executable: " ^ Path.print command ^ ".")
   529 
   530     (* If the problem file has not been exported, remove it; otherwise, export
   531        the proof file too. *)
   532     fun cleanup prob_file =
   533       if dest_dir = "" then try File.rm prob_file else NONE
   534     fun export prob_file (_, (output, _, _, _)) =
   535       if dest_dir = "" then
   536         ()
   537       else
   538         File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
   539     val ((pool, conjecture_shape, fact_names),
   540          (output, msecs, atp_proof, outcome)) =
   541       with_path cleanup export run_on problem_path_name
   542     val important_message =
   543       if not auto andalso random () <= atp_important_message_keep_factor then
   544         extract_important_message output
   545       else
   546         ""
   547     fun append_to_message message =
   548       message ^
   549       (if verbose then
   550          "\nATP real CPU time: " ^
   551          string_from_time (Time.fromMilliseconds (the msecs)) ^ "."
   552        else
   553          "") ^
   554       (if important_message <> "" then
   555          "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^ important_message
   556        else
   557          "")
   558     val isar_params = (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
   559     val metis_params =
   560       (proof_banner auto, type_sys, minimize_command, atp_proof, fact_names,
   561        goal, subgoal)
   562     val (outcome, (message, used_facts)) =
   563       case outcome of
   564         NONE =>
   565         (NONE, proof_text isar_proof isar_params metis_params
   566                |>> append_to_message)
   567       | SOME ProofMissing =>
   568         (NONE, metis_proof_text metis_params |>> append_to_message)
   569       | SOME failure => (outcome, (string_for_failure failure, []))
   570   in
   571     {outcome = outcome, message = message, used_facts = used_facts,
   572      run_time_in_msecs = msecs}
   573   end
   574 
   575 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
   576    these are sorted out properly in the SMT module, we have to interpret these
   577    ourselves. *)
   578 val remote_smt_failures =
   579   [(22, CantConnect),
   580    (2, NoLibwwwPerl)]
   581 val z3_wrapper_failures =
   582   [(10, NoRealZ3),
   583    (11, InternalError),
   584    (12, InternalError),
   585    (13, InternalError)]
   586 val z3_failures =
   587   [(101, OutOfResources),
   588    (103, MalformedInput),
   589    (110, MalformedInput)]
   590 val unix_failures =
   591   [(139, Crashed)]
   592 val smt_failures =
   593   remote_smt_failures @ z3_wrapper_failures @ z3_failures @ unix_failures
   594 
   595 fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
   596     if is_real_cex then Unprovable else IncompleteUnprovable
   597   | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
   598   | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
   599     (case AList.lookup (op =) smt_failures code of
   600        SOME failure => failure
   601      | NONE => UnknownError ("Abnormal termination with exit code " ^
   602                              string_of_int code ^ "."))
   603   | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
   604   | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
   605     UnknownError msg
   606 
   607 (* FUDGE *)
   608 val smt_max_slices = Unsynchronized.ref 8
   609 val smt_slice_fact_frac = Unsynchronized.ref 0.5
   610 val smt_slice_time_frac = Unsynchronized.ref 0.5
   611 val smt_slice_min_secs = Unsynchronized.ref 5
   612 
   613 fun smt_filter_loop name ({debug, verbose, overlord, monomorphize_limit,
   614                            timeout, slicing, ...} : params)
   615                     state i smt_filter =
   616   let
   617     val ctxt = Proof.context_of state
   618     val max_slices = if slicing then !smt_max_slices else 1
   619     val repair_context =
   620       select_smt_solver name
   621       #> Config.put SMT_Config.verbose debug
   622       #> (if overlord then
   623             Config.put SMT_Config.debug_files
   624                        (overlord_file_location_for_prover name
   625                         |> (fn (path, name) => path ^ "/" ^ name))
   626           else
   627             I)
   628       #> Config.put SMT_Config.infer_triggers (!smt_triggers)
   629       #> Config.put SMT_Config.monomorph_full false
   630       #> Config.put SMT_Config.monomorph_limit monomorphize_limit
   631     val state = state |> Proof.map_context repair_context
   632 
   633     fun do_slice timeout slice outcome0 time_so_far facts =
   634       let
   635         val timer = Timer.startRealTimer ()
   636         val ms = timeout |> Time.toMilliseconds
   637         val slice_timeout =
   638           if slice < max_slices then
   639             Int.min (ms,
   640                 Int.max (1000 * !smt_slice_min_secs,
   641                     Real.ceil (!smt_slice_time_frac * Real.fromInt ms)))
   642             |> Time.fromMilliseconds
   643           else
   644             timeout
   645         val num_facts = length facts
   646         val _ =
   647           if verbose then
   648             "SMT slice with " ^ string_of_int num_facts ^ " fact" ^
   649             plural_s num_facts ^ " for " ^ string_from_time slice_timeout ^
   650             "..."
   651             |> Output.urgent_message
   652           else
   653             ()
   654         val birth = Timer.checkRealTimer timer
   655         val _ =
   656           if debug then Output.urgent_message "Invoking SMT solver..." else ()
   657         val (outcome, used_facts) =
   658           (case (slice, smt_filter) of
   659              (1, SOME head) => head |> apsnd (apsnd repair_context)
   660            | _ => SMT_Solver.smt_filter_preprocess state facts i)
   661           |> SMT_Solver.smt_filter_apply slice_timeout
   662           |> (fn {outcome, used_facts} => (outcome, used_facts))
   663           handle exn => if Exn.is_interrupt exn then
   664                           reraise exn
   665                         else
   666                           (ML_Compiler.exn_message exn
   667                            |> SMT_Failure.Other_Failure |> SOME, [])
   668         val death = Timer.checkRealTimer timer
   669         val _ =
   670           if verbose andalso is_some outcome then
   671             "SMT outcome: " ^ SMT_Failure.string_of_failure ctxt (the outcome)
   672             |> Output.urgent_message
   673           else if debug then
   674             Output.urgent_message "SMT solver returned."
   675           else
   676             ()
   677         val outcome0 = if is_none outcome0 then SOME outcome else outcome0
   678         val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
   679         val too_many_facts_perhaps =
   680           case outcome of
   681             NONE => false
   682           | SOME (SMT_Failure.Counterexample _) => false
   683           | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
   684           | SOME (SMT_Failure.Abnormal_Termination code) =>
   685             (if verbose then
   686                "The SMT solver invoked with " ^ string_of_int num_facts ^
   687                " fact" ^ plural_s num_facts ^ " terminated abnormally with \
   688                \exit code " ^ string_of_int code ^ "."
   689                |> warning
   690              else
   691                ();
   692              true (* kind of *))
   693           | SOME SMT_Failure.Out_Of_Memory => true
   694           | SOME (SMT_Failure.Other_Failure _) => true
   695         val timeout = Time.- (timeout, Timer.checkRealTimer timer)
   696       in
   697         if too_many_facts_perhaps andalso slice < max_slices andalso
   698            num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
   699           let
   700             val n = Real.ceil (!smt_slice_fact_frac * Real.fromInt num_facts)
   701           in
   702             do_slice timeout (slice + 1) outcome0 time_so_far (take n facts)
   703           end
   704         else
   705           {outcome = if is_none outcome then NONE else the outcome0,
   706            used_facts = used_facts,
   707            run_time_in_msecs = SOME (Time.toMilliseconds time_so_far)}
   708       end
   709   in do_slice timeout 1 NONE Time.zeroTime end
   710 
   711 (* taken from "Mirabelle" and generalized *)
   712 fun can_apply timeout tac state i =
   713   let
   714     val {context = ctxt, facts, goal} = Proof.goal state
   715     val full_tac = Method.insert_tac facts i THEN tac ctxt i
   716   in
   717     case try (TimeLimit.timeLimit timeout (Seq.pull o full_tac)) goal of
   718       SOME (SOME _) => true
   719     | _ => false
   720   end
   721 
   722 val smt_metis_timeout = seconds 1.0
   723 
   724 fun can_apply_metis debug state i ths =
   725   can_apply smt_metis_timeout
   726             (Config.put Metis_Tactics.verbose debug
   727              #> (fn ctxt => Metis_Tactics.metis_tac ctxt ths)) state i
   728 
   729 fun run_smt_solver auto name (params as {debug, verbose, ...}) minimize_command
   730                    ({state, subgoal, subgoal_count, facts, smt_filter, ...}
   731                     : prover_problem) =
   732   let
   733     val ctxt = Proof.context_of state
   734     val thy = Proof.theory_of state
   735     val num_facts = length facts
   736     val facts = facts ~~ (0 upto num_facts - 1)
   737                 |> map (smt_weighted_fact thy num_facts)
   738     val {outcome, used_facts, run_time_in_msecs} =
   739       smt_filter_loop name params state subgoal smt_filter facts
   740     val (chained_lemmas, other_lemmas) = split_used_facts (map fst used_facts)
   741     val outcome = outcome |> Option.map failure_from_smt_failure
   742     val message =
   743       case outcome of
   744         NONE =>
   745         let
   746           val (method, settings) =
   747             if can_apply_metis debug state subgoal (map snd used_facts) then
   748               ("metis", "")
   749             else
   750               ("smt", if name = SMT_Solver.solver_name_of ctxt then ""
   751                       else "smt_solver = " ^ maybe_quote name)
   752         in
   753           try_command_line (proof_banner auto)
   754               (apply_on_subgoal settings subgoal subgoal_count ^
   755                command_call method (map fst other_lemmas)) ^
   756           minimize_line minimize_command
   757                         (map fst (other_lemmas @ chained_lemmas)) ^
   758           (if verbose then
   759              "\nSMT solver real CPU time: " ^
   760              string_from_time (Time.fromMilliseconds (the run_time_in_msecs)) ^
   761              "."
   762            else
   763              "")
   764         end
   765       | SOME failure => string_for_failure failure
   766   in
   767     {outcome = outcome, used_facts = map fst used_facts,
   768      run_time_in_msecs = run_time_in_msecs, message = message}
   769   end
   770 
   771 fun get_prover ctxt auto name =
   772   let val thy = Proof_Context.theory_of ctxt in
   773     if is_smt_prover ctxt name then
   774       run_smt_solver auto name
   775     else if member (op =) (supported_atps thy) name then
   776       run_atp auto name (get_atp thy name)
   777     else
   778       error ("No such prover: " ^ name ^ ".")
   779   end
   780 
   781 val setup =
   782   dest_dir_setup
   783   #> problem_prefix_setup
   784   #> measure_run_time_setup
   785 
   786 end;