src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Sun May 01 18:37:24 2011 +0200 (2011-05-01)
changeset 42546 8591fcc56c34
parent 42544 75cb06eee990
child 42548 ea2a28b1938f
permissions -rw-r--r--
make sure typing fact names are unique (needed e.g. by SNARK)
     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      type_sys: type_system,
    25      relevance_thresholds: real * real,
    26      max_relevant: int option,
    27      monomorphize: bool,
    28      monomorphize_limit: int,
    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    type_sys: type_system,
   237    relevance_thresholds: real * real,
   238    max_relevant: int option,
   239    monomorphize: bool,
   240    monomorphize_limit: int,
   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           use_conjecture_for_hypotheses, ...} : atp_config)
   340         ({debug, verbose, overlord, type_sys, max_relevant, monomorphize,
   341           monomorphize_limit, explicit_apply, isar_proof, isar_shrink_factor,
   342           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, facts_offset,
   436                      fact_names) =
   437                   prepare_atp_problem ctxt readable_names type_sys
   438                                       explicit_apply hyp_ts concl_t 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 facts_offset
   479                                                 fact_names 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, facts_offset, 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 ((_, _, facts_offset, fact_names),
   508                                 (_, _, atp_proof, SOME UnsoundProof))) =
   509                 (case used_facts_in_atp_proof facts_offset fact_names
   510                                               atp_proof of
   511                    [] => result
   512                  | new_baddies =>
   513                    let val blacklist = new_baddies @ blacklist in
   514                      result
   515                      |> maybe_run_slice blacklist (List.last actual_slices)
   516                      |> iter < atp_blacklist_max_iters
   517                         ? maybe_blacklist_facts_and_retry (iter + 1) blacklist
   518                    end)
   519               | maybe_blacklist_facts_and_retry _ _ result = result
   520           in
   521             ((Symtab.empty, [], 0, Vector.fromList []),
   522              ("", SOME 0, [], SOME InternalError))
   523             |> fold (maybe_run_slice []) actual_slices
   524                (* The ATP found an unsound proof? Automatically try again
   525                   without the offending facts! *)
   526             |> maybe_blacklist_facts_and_retry 0 []
   527           end
   528         else
   529           error ("Bad executable: " ^ Path.print command ^ ".")
   530 
   531     (* If the problem file has not been exported, remove it; otherwise, export
   532        the proof file too. *)
   533     fun cleanup prob_file =
   534       if dest_dir = "" then try File.rm prob_file else NONE
   535     fun export prob_file (_, (output, _, _, _)) =
   536       if dest_dir = "" then
   537         ()
   538       else
   539         File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
   540     val ((pool, conjecture_shape, facts_offset, fact_names),
   541          (output, msecs, atp_proof, outcome)) =
   542       with_path cleanup export run_on problem_path_name
   543     val important_message =
   544       if not auto andalso random () <= atp_important_message_keep_factor then
   545         extract_important_message output
   546       else
   547         ""
   548     fun append_to_message message =
   549       message ^
   550       (if verbose then
   551          "\nATP real CPU time: " ^
   552          string_from_time (Time.fromMilliseconds (the msecs)) ^ "."
   553        else
   554          "") ^
   555       (if important_message <> "" then
   556          "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^ important_message
   557        else
   558          "")
   559     val isar_params = (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
   560     val metis_params =
   561       (proof_banner auto, type_sys, minimize_command, atp_proof, facts_offset,
   562        fact_names, goal, subgoal)
   563     val (outcome, (message, used_facts)) =
   564       case outcome of
   565         NONE =>
   566         (NONE, proof_text isar_proof isar_params metis_params
   567                |>> append_to_message)
   568       | SOME ProofMissing =>
   569         (NONE, metis_proof_text metis_params |>> append_to_message)
   570       | SOME failure => (outcome, (string_for_failure failure, []))
   571   in
   572     {outcome = outcome, message = message, used_facts = used_facts,
   573      run_time_in_msecs = msecs}
   574   end
   575 
   576 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
   577    these are sorted out properly in the SMT module, we have to interpret these
   578    ourselves. *)
   579 val remote_smt_failures =
   580   [(22, CantConnect),
   581    (2, NoLibwwwPerl)]
   582 val z3_wrapper_failures =
   583   [(10, NoRealZ3),
   584    (11, InternalError),
   585    (12, InternalError),
   586    (13, InternalError)]
   587 val z3_failures =
   588   [(101, OutOfResources),
   589    (103, MalformedInput),
   590    (110, MalformedInput)]
   591 val unix_failures =
   592   [(139, Crashed)]
   593 val smt_failures =
   594   remote_smt_failures @ z3_wrapper_failures @ z3_failures @ unix_failures
   595 
   596 fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
   597     if is_real_cex then Unprovable else IncompleteUnprovable
   598   | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
   599   | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
   600     (case AList.lookup (op =) smt_failures code of
   601        SOME failure => failure
   602      | NONE => UnknownError ("Abnormal termination with exit code " ^
   603                              string_of_int code ^ "."))
   604   | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
   605   | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
   606     UnknownError msg
   607 
   608 (* FUDGE *)
   609 val smt_max_slices = Unsynchronized.ref 8
   610 val smt_slice_fact_frac = Unsynchronized.ref 0.5
   611 val smt_slice_time_frac = Unsynchronized.ref 0.5
   612 val smt_slice_min_secs = Unsynchronized.ref 5
   613 
   614 fun smt_filter_loop name ({debug, verbose, overlord, monomorphize_limit,
   615                            timeout, slicing, ...} : params)
   616                     state i smt_filter =
   617   let
   618     val ctxt = Proof.context_of state
   619     val max_slices = if slicing then !smt_max_slices else 1
   620     val repair_context =
   621       select_smt_solver name
   622       #> Config.put SMT_Config.verbose debug
   623       #> (if overlord then
   624             Config.put SMT_Config.debug_files
   625                        (overlord_file_location_for_prover name
   626                         |> (fn (path, name) => path ^ "/" ^ name))
   627           else
   628             I)
   629       #> Config.put SMT_Config.infer_triggers (!smt_triggers)
   630       #> Config.put SMT_Config.monomorph_full false
   631       #> Config.put SMT_Config.monomorph_limit monomorphize_limit
   632     val state = state |> Proof.map_context repair_context
   633 
   634     fun do_slice timeout slice outcome0 time_so_far facts =
   635       let
   636         val timer = Timer.startRealTimer ()
   637         val ms = timeout |> Time.toMilliseconds
   638         val slice_timeout =
   639           if slice < max_slices then
   640             Int.min (ms,
   641                 Int.max (1000 * !smt_slice_min_secs,
   642                     Real.ceil (!smt_slice_time_frac * Real.fromInt ms)))
   643             |> Time.fromMilliseconds
   644           else
   645             timeout
   646         val num_facts = length facts
   647         val _ =
   648           if verbose then
   649             "SMT slice with " ^ string_of_int num_facts ^ " fact" ^
   650             plural_s num_facts ^ " for " ^ string_from_time slice_timeout ^
   651             "..."
   652             |> Output.urgent_message
   653           else
   654             ()
   655         val birth = Timer.checkRealTimer timer
   656         val _ =
   657           if debug then Output.urgent_message "Invoking SMT solver..." else ()
   658         val (outcome, used_facts) =
   659           (case (slice, smt_filter) of
   660              (1, SOME head) => head |> apsnd (apsnd repair_context)
   661            | _ => SMT_Solver.smt_filter_preprocess state facts i)
   662           |> SMT_Solver.smt_filter_apply slice_timeout
   663           |> (fn {outcome, used_facts} => (outcome, used_facts))
   664           handle exn => if Exn.is_interrupt exn then
   665                           reraise exn
   666                         else
   667                           (ML_Compiler.exn_message exn
   668                            |> SMT_Failure.Other_Failure |> SOME, [])
   669         val death = Timer.checkRealTimer timer
   670         val _ =
   671           if verbose andalso is_some outcome then
   672             "SMT outcome: " ^ SMT_Failure.string_of_failure ctxt (the outcome)
   673             |> Output.urgent_message
   674           else if debug then
   675             Output.urgent_message "SMT solver returned."
   676           else
   677             ()
   678         val outcome0 = if is_none outcome0 then SOME outcome else outcome0
   679         val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
   680         val too_many_facts_perhaps =
   681           case outcome of
   682             NONE => false
   683           | SOME (SMT_Failure.Counterexample _) => false
   684           | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
   685           | SOME (SMT_Failure.Abnormal_Termination code) =>
   686             (if verbose then
   687                "The SMT solver invoked with " ^ string_of_int num_facts ^
   688                " fact" ^ plural_s num_facts ^ " terminated abnormally with \
   689                \exit code " ^ string_of_int code ^ "."
   690                |> warning
   691              else
   692                ();
   693              true (* kind of *))
   694           | SOME SMT_Failure.Out_Of_Memory => true
   695           | SOME (SMT_Failure.Other_Failure _) => true
   696         val timeout = Time.- (timeout, Timer.checkRealTimer timer)
   697       in
   698         if too_many_facts_perhaps andalso slice < max_slices andalso
   699            num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
   700           let
   701             val n = Real.ceil (!smt_slice_fact_frac * Real.fromInt num_facts)
   702           in
   703             do_slice timeout (slice + 1) outcome0 time_so_far (take n facts)
   704           end
   705         else
   706           {outcome = if is_none outcome then NONE else the outcome0,
   707            used_facts = used_facts,
   708            run_time_in_msecs = SOME (Time.toMilliseconds time_so_far)}
   709       end
   710   in do_slice timeout 1 NONE Time.zeroTime end
   711 
   712 (* taken from "Mirabelle" and generalized *)
   713 fun can_apply timeout tac state i =
   714   let
   715     val {context = ctxt, facts, goal} = Proof.goal state
   716     val full_tac = Method.insert_tac facts i THEN tac ctxt i
   717   in
   718     case try (TimeLimit.timeLimit timeout (Seq.pull o full_tac)) goal of
   719       SOME (SOME _) => true
   720     | _ => false
   721   end
   722 
   723 val smt_metis_timeout = seconds 1.0
   724 
   725 fun can_apply_metis debug state i ths =
   726   can_apply smt_metis_timeout
   727             (Config.put Metis_Tactics.verbose debug
   728              #> (fn ctxt => Metis_Tactics.metis_tac ctxt ths)) state i
   729 
   730 fun run_smt_solver auto name (params as {debug, verbose, ...}) minimize_command
   731                    ({state, subgoal, subgoal_count, facts, smt_filter, ...}
   732                     : prover_problem) =
   733   let
   734     val ctxt = Proof.context_of state
   735     val thy = Proof.theory_of state
   736     val num_facts = length facts
   737     val facts = facts ~~ (0 upto num_facts - 1)
   738                 |> map (smt_weighted_fact thy num_facts)
   739     val {outcome, used_facts, run_time_in_msecs} =
   740       smt_filter_loop name params state subgoal smt_filter facts
   741     val (chained_lemmas, other_lemmas) = split_used_facts (map fst used_facts)
   742     val outcome = outcome |> Option.map failure_from_smt_failure
   743     val message =
   744       case outcome of
   745         NONE =>
   746         let
   747           val (method, settings) =
   748             if can_apply_metis debug state subgoal (map snd used_facts) then
   749               ("metis", "")
   750             else
   751               ("smt", if name = SMT_Solver.solver_name_of ctxt then ""
   752                       else "smt_solver = " ^ maybe_quote name)
   753         in
   754           try_command_line (proof_banner auto)
   755               (apply_on_subgoal settings subgoal subgoal_count ^
   756                command_call method (map fst other_lemmas)) ^
   757           minimize_line minimize_command
   758                         (map fst (other_lemmas @ chained_lemmas)) ^
   759           (if verbose then
   760              "\nSMT solver real CPU time: " ^
   761              string_from_time (Time.fromMilliseconds (the run_time_in_msecs)) ^
   762              "."
   763            else
   764              "")
   765         end
   766       | SOME failure => string_for_failure failure
   767   in
   768     {outcome = outcome, used_facts = map fst used_facts,
   769      run_time_in_msecs = run_time_in_msecs, message = message}
   770   end
   771 
   772 fun get_prover ctxt auto name =
   773   let val thy = Proof_Context.theory_of ctxt in
   774     if is_smt_prover ctxt name then
   775       run_smt_solver auto name
   776     else if member (op =) (supported_atps thy) name then
   777       run_atp auto name (get_atp thy name)
   778     else
   779       error ("No such prover: " ^ name ^ ".")
   780   end
   781 
   782 val setup =
   783   dest_dir_setup
   784   #> problem_prefix_setup
   785   #> measure_run_time_setup
   786 
   787 end;