src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Fri Apr 22 00:00:05 2011 +0200 (2011-04-22)
changeset 42451 a75fcd103cbb
parent 42450 2765d4fb2b9c
child 42452 f7f796ce5d68
permissions -rw-r--r--
automatically remove offending facts when faced with an unsound proof -- instead of using the highly inefficient "full_types" option
     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 (* Important messages are important but not so important that users want to see
   333    them each time. *)
   334 val atp_important_message_keep_factor = 0.1
   335 
   336 fun run_atp auto name
   337         ({exec, required_execs, arguments, slices, proof_delims, known_failures,
   338           explicit_forall, use_conjecture_for_hypotheses, ...} : atp_config)
   339         ({debug, verbose, overlord, max_relevant, monomorphize,
   340           monomorphize_limit, type_sys, explicit_apply, isar_proof,
   341           isar_shrink_factor, slicing, timeout, ...} : params)
   342         minimize_command ({state, goal, subgoal, facts, ...} : prover_problem) =
   343   let
   344     val thy = Proof.theory_of state
   345     val ctxt = Proof.context_of state
   346     val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal
   347     val (dest_dir, problem_prefix) =
   348       if overlord then overlord_file_location_for_prover name
   349       else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
   350     val problem_file_name =
   351       Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
   352                   "_" ^ string_of_int subgoal)
   353     val problem_path_name =
   354       if dest_dir = "" then
   355         File.tmp_path problem_file_name
   356       else if File.exists (Path.explode dest_dir) then
   357         Path.append (Path.explode dest_dir) problem_file_name
   358       else
   359         error ("No such directory: " ^ quote dest_dir ^ ".")
   360     val measure_run_time = verbose orelse Config.get ctxt measure_run_time
   361     val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
   362     fun split_time s =
   363       let
   364         val split = String.tokens (fn c => str c = "\n")
   365         val (output, t) = s |> split |> split_last |> apfst cat_lines
   366         fun as_num f = f >> (fst o read_int)
   367         val num = as_num (Scan.many1 Symbol.is_ascii_digit)
   368         val digit = Scan.one Symbol.is_ascii_digit
   369         val num3 = as_num (digit ::: digit ::: (digit >> single))
   370         val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
   371         val as_time = Scan.read Symbol.stopper time o raw_explode
   372       in (output, as_time t) end
   373     fun run_on prob_file =
   374       case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
   375         (home_var, _) :: _ =>
   376         error ("The environment variable " ^ quote home_var ^ " is not set.")
   377       | [] =>
   378         if File.exists command then
   379           let
   380             val readable_names = debug andalso overlord
   381             (* If slicing is disabled, we expand the last slice to fill the
   382                entire time available. *)
   383             val actual_slices = get_slices slicing (slices ())
   384             val num_actual_slices = length actual_slices
   385             fun monomorphize_facts facts =
   386               let
   387                 val repair_context =
   388                   Config.put SMT_Config.verbose debug
   389                   #> Config.put SMT_Config.monomorph_full false
   390                   #> Config.put SMT_Config.monomorph_limit monomorphize_limit
   391                 val facts = facts |> map untranslated_fact
   392                 (* pseudo-theorem involving the same constants as the subgoal *)
   393                 val subgoal_th =
   394                   Logic.list_implies (hyp_ts, concl_t)
   395                   |> Skip_Proof.make_thm thy
   396                 val indexed_facts =
   397                   (~1, subgoal_th) :: (0 upto length facts - 1 ~~ map snd facts)
   398               in
   399                 SMT_Monomorph.monomorph indexed_facts (repair_context ctxt)
   400                 |> fst |> sort (int_ord o pairself fst)
   401                 |> filter_out (curry (op =) ~1 o fst)
   402                 |> map (Untranslated_Fact o apfst (fst o nth facts))
   403               end
   404             fun run_slice blacklisted_facts
   405                           (slice, (time_frac, (complete, default_max_relevant)))
   406                           time_left =
   407               let
   408                 val num_facts =
   409                   length facts |> is_none max_relevant
   410                                   ? Integer.min default_max_relevant
   411                 val facts =
   412                   facts |> take num_facts
   413                         |> not (null blacklisted_facts)
   414                            ? filter_out (member (op =) blacklisted_facts
   415                                          o fst o untranslated_fact)
   416                         |> monomorphize ? monomorphize_facts
   417                         |> map (atp_translated_fact ctxt)
   418                 val real_ms = Real.fromInt o Time.toMilliseconds
   419                 val slice_timeout =
   420                   ((real_ms time_left
   421                     |> (if slice < num_actual_slices - 1 then
   422                           curry Real.min (time_frac * real_ms timeout)
   423                         else
   424                           I))
   425                    * 0.001) |> seconds
   426                 val _ =
   427                   if verbose then
   428                     "ATP slice " ^ string_of_int (slice + 1) ^ " with " ^
   429                     string_of_int num_facts ^ " fact" ^ plural_s num_facts ^
   430                     " for " ^ string_from_time slice_timeout ^ "..."
   431                     |> Output.urgent_message
   432                   else
   433                     ()
   434                 val (atp_problem, pool, conjecture_offset, fact_names) =
   435                   prepare_atp_problem ctxt readable_names explicit_forall
   436                                       type_sys explicit_apply hyp_ts concl_t
   437                                       facts
   438                 fun weights () = atp_problem_weights atp_problem
   439                 val core =
   440                   File.shell_path command ^ " " ^
   441                   arguments slice slice_timeout weights ^ " " ^
   442                   File.shell_path prob_file
   443                 val command =
   444                   (if measure_run_time then
   445                      "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
   446                    else
   447                      "exec " ^ core) ^ " 2>&1"
   448                 val _ =
   449                   atp_problem
   450                   |> tptp_strings_for_atp_problem use_conjecture_for_hypotheses
   451                   |> cons ("% " ^ command ^ "\n")
   452                   |> File.write_list prob_file
   453                 val conjecture_shape =
   454                   conjecture_offset + 1
   455                     upto conjecture_offset + length hyp_ts + 1
   456                   |> map single
   457                 val ((output, msecs), res_code) =
   458                   bash_output command
   459                   |>> (if overlord then
   460                          prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
   461                        else
   462                          I)
   463                   |>> (if measure_run_time then split_time else rpair NONE)
   464                 val (atp_proof, outcome) =
   465                   extract_tstplike_proof_and_outcome debug verbose complete
   466                       res_code proof_delims known_failures output
   467                   |>> atp_proof_from_tstplike_proof
   468                 val (conjecture_shape, fact_names) =
   469                   if is_none outcome then
   470                     repair_conjecture_shape_and_fact_names output
   471                         conjecture_shape fact_names
   472                   else
   473                     (conjecture_shape, fact_names) (* don't bother repairing *)
   474                 val outcome =
   475                   case outcome of
   476                     NONE => if not (is_type_system_sound type_sys) andalso
   477                                is_unsound_proof conjecture_shape fact_names
   478                                                 atp_proof then
   479                               SOME UnsoundProof
   480                             else
   481                               NONE
   482                   | SOME Unprovable =>
   483                     if null blacklisted_facts then outcome
   484                     else SOME IncompleteUnprovable
   485                   | _ => outcome
   486               in
   487                 ((pool, conjecture_shape, fact_names),
   488                  (output, msecs, atp_proof, outcome))
   489               end
   490             val timer = Timer.startRealTimer ()
   491             fun maybe_run_slice blacklisted_facts slice
   492                                 (_, (_, msecs0, _, SOME _)) =
   493                 run_slice blacklisted_facts slice
   494                           (Time.- (timeout, Timer.checkRealTimer timer))
   495                 |> (fn (stuff, (output, msecs, atp_proof, outcome)) =>
   496                        (stuff, (output, int_opt_add msecs0 msecs, atp_proof,
   497                                 outcome)))
   498               | maybe_run_slice _ _ result = result
   499           in
   500             ((Symtab.empty, [], Vector.fromList []),
   501              ("", SOME 0, [], SOME InternalError))
   502             |> fold (maybe_run_slice []) actual_slices
   503                (* The ATP found an unsound proof? Automatically try again
   504                   without the offending facts! *)
   505             |> (fn result as ((_, _, fact_names),
   506                               (_, _, atp_proof, SOME UnsoundProof)) =>
   507                    let
   508                      val blacklisted_facts =
   509                        used_facts_in_atp_proof fact_names atp_proof
   510                    in
   511                      result |> fold (maybe_run_slice blacklisted_facts)
   512                                     actual_slices
   513                    end
   514                  | result => result)
   515           end
   516         else
   517           error ("Bad executable: " ^ Path.print command ^ ".")
   518 
   519     (* If the problem file has not been exported, remove it; otherwise, export
   520        the proof file too. *)
   521     fun cleanup prob_file =
   522       if dest_dir = "" then try File.rm prob_file else NONE
   523     fun export prob_file (_, (output, _, _, _)) =
   524       if dest_dir = "" then
   525         ()
   526       else
   527         File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
   528     val ((pool, conjecture_shape, fact_names),
   529          (output, msecs, atp_proof, outcome)) =
   530       with_path cleanup export run_on problem_path_name
   531     val important_message =
   532       if not auto andalso random () <= atp_important_message_keep_factor then
   533         extract_important_message output
   534       else
   535         ""
   536     fun append_to_message message =
   537       message ^
   538       (if verbose then
   539          "\nATP real CPU time: " ^
   540          string_from_time (Time.fromMilliseconds (the msecs)) ^ "."
   541        else
   542          "") ^
   543       (if important_message <> "" then
   544          "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^ important_message
   545        else
   546          "")
   547     val isar_params = (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
   548     val metis_params =
   549       (proof_banner auto, type_sys, minimize_command, atp_proof, fact_names,
   550        goal, subgoal)
   551     val (outcome, (message, used_facts)) =
   552       case outcome of
   553         NONE =>
   554         (NONE, proof_text isar_proof isar_params metis_params
   555                |>> append_to_message)
   556       | SOME ProofMissing =>
   557         (NONE, metis_proof_text metis_params |>> append_to_message)
   558       | SOME failure => (outcome, (string_for_failure failure, []))
   559   in
   560     {outcome = outcome, message = message, used_facts = used_facts,
   561      run_time_in_msecs = msecs}
   562   end
   563 
   564 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
   565    these are sorted out properly in the SMT module, we have to interpret these
   566    ourselves. *)
   567 val remote_smt_failures =
   568   [(22, CantConnect),
   569    (2, NoLibwwwPerl)]
   570 val z3_wrapper_failures =
   571   [(10, NoRealZ3),
   572    (11, InternalError),
   573    (12, InternalError),
   574    (13, InternalError)]
   575 val z3_failures =
   576   [(101, OutOfResources),
   577    (103, MalformedInput),
   578    (110, MalformedInput)]
   579 val unix_failures =
   580   [(139, Crashed)]
   581 val smt_failures =
   582   remote_smt_failures @ z3_wrapper_failures @ z3_failures @ unix_failures
   583 
   584 fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
   585     if is_real_cex then Unprovable else IncompleteUnprovable
   586   | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
   587   | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
   588     (case AList.lookup (op =) smt_failures code of
   589        SOME failure => failure
   590      | NONE => UnknownError ("Abnormal termination with exit code " ^
   591                              string_of_int code ^ "."))
   592   | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
   593   | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
   594     UnknownError msg
   595 
   596 (* FUDGE *)
   597 val smt_max_slices = Unsynchronized.ref 8
   598 val smt_slice_fact_frac = Unsynchronized.ref 0.5
   599 val smt_slice_time_frac = Unsynchronized.ref 0.5
   600 val smt_slice_min_secs = Unsynchronized.ref 5
   601 
   602 fun smt_filter_loop name ({debug, verbose, overlord, monomorphize_limit,
   603                            timeout, slicing, ...} : params)
   604                     state i smt_filter =
   605   let
   606     val ctxt = Proof.context_of state
   607     val max_slices = if slicing then !smt_max_slices else 1
   608     val repair_context =
   609       select_smt_solver name
   610       #> Config.put SMT_Config.verbose debug
   611       #> (if overlord then
   612             Config.put SMT_Config.debug_files
   613                        (overlord_file_location_for_prover name
   614                         |> (fn (path, name) => path ^ "/" ^ name))
   615           else
   616             I)
   617       #> Config.put SMT_Config.infer_triggers (!smt_triggers)
   618       #> Config.put SMT_Config.monomorph_full false
   619       #> Config.put SMT_Config.monomorph_limit monomorphize_limit
   620     val state = state |> Proof.map_context repair_context
   621 
   622     fun do_slice timeout slice outcome0 time_so_far facts =
   623       let
   624         val timer = Timer.startRealTimer ()
   625         val ms = timeout |> Time.toMilliseconds
   626         val slice_timeout =
   627           if slice < max_slices then
   628             Int.min (ms,
   629                 Int.max (1000 * !smt_slice_min_secs,
   630                     Real.ceil (!smt_slice_time_frac * Real.fromInt ms)))
   631             |> Time.fromMilliseconds
   632           else
   633             timeout
   634         val num_facts = length facts
   635         val _ =
   636           if verbose then
   637             "SMT slice with " ^ string_of_int num_facts ^ " fact" ^
   638             plural_s num_facts ^ " for " ^ string_from_time slice_timeout ^
   639             "..."
   640             |> Output.urgent_message
   641           else
   642             ()
   643         val birth = Timer.checkRealTimer timer
   644         val _ =
   645           if debug then Output.urgent_message "Invoking SMT solver..." else ()
   646         val (outcome, used_facts) =
   647           (case (slice, smt_filter) of
   648              (1, SOME head) => head |> apsnd (apsnd repair_context)
   649            | _ => SMT_Solver.smt_filter_preprocess state facts i)
   650           |> SMT_Solver.smt_filter_apply slice_timeout
   651           |> (fn {outcome, used_facts} => (outcome, used_facts))
   652           handle exn => if Exn.is_interrupt exn then
   653                           reraise exn
   654                         else
   655                           (ML_Compiler.exn_message exn
   656                            |> SMT_Failure.Other_Failure |> SOME, [])
   657         val death = Timer.checkRealTimer timer
   658         val _ =
   659           if verbose andalso is_some outcome then
   660             "SMT outcome: " ^ SMT_Failure.string_of_failure ctxt (the outcome)
   661             |> Output.urgent_message
   662           else if debug then
   663             Output.urgent_message "SMT solver returned."
   664           else
   665             ()
   666         val outcome0 = if is_none outcome0 then SOME outcome else outcome0
   667         val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
   668         val too_many_facts_perhaps =
   669           case outcome of
   670             NONE => false
   671           | SOME (SMT_Failure.Counterexample _) => false
   672           | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
   673           | SOME (SMT_Failure.Abnormal_Termination code) =>
   674             (if verbose then
   675                "The SMT solver invoked with " ^ string_of_int num_facts ^
   676                " fact" ^ plural_s num_facts ^ " terminated abnormally with \
   677                \exit code " ^ string_of_int code ^ "."
   678                |> warning
   679              else
   680                ();
   681              true (* kind of *))
   682           | SOME SMT_Failure.Out_Of_Memory => true
   683           | SOME (SMT_Failure.Other_Failure _) => true
   684         val timeout = Time.- (timeout, Timer.checkRealTimer timer)
   685       in
   686         if too_many_facts_perhaps andalso slice < max_slices andalso
   687            num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
   688           let
   689             val n = Real.ceil (!smt_slice_fact_frac * Real.fromInt num_facts)
   690           in
   691             do_slice timeout (slice + 1) outcome0 time_so_far (take n facts)
   692           end
   693         else
   694           {outcome = if is_none outcome then NONE else the outcome0,
   695            used_facts = used_facts,
   696            run_time_in_msecs = SOME (Time.toMilliseconds time_so_far)}
   697       end
   698   in do_slice timeout 1 NONE Time.zeroTime end
   699 
   700 (* taken from "Mirabelle" and generalized *)
   701 fun can_apply timeout tac state i =
   702   let
   703     val {context = ctxt, facts, goal} = Proof.goal state
   704     val full_tac = Method.insert_tac facts i THEN tac ctxt i
   705   in
   706     case try (TimeLimit.timeLimit timeout (Seq.pull o full_tac)) goal of
   707       SOME (SOME _) => true
   708     | _ => false
   709   end
   710 
   711 val smt_metis_timeout = seconds 1.0
   712 
   713 fun can_apply_metis debug state i ths =
   714   can_apply smt_metis_timeout
   715             (Config.put Metis_Tactics.verbose debug
   716              #> (fn ctxt => Metis_Tactics.metis_tac ctxt ths)) state i
   717 
   718 fun run_smt_solver auto name (params as {debug, verbose, ...}) minimize_command
   719                    ({state, subgoal, subgoal_count, facts, smt_filter, ...}
   720                     : prover_problem) =
   721   let
   722     val ctxt = Proof.context_of state
   723     val thy = Proof.theory_of state
   724     val num_facts = length facts
   725     val facts = facts ~~ (0 upto num_facts - 1)
   726                 |> map (smt_weighted_fact thy num_facts)
   727     val {outcome, used_facts, run_time_in_msecs} =
   728       smt_filter_loop name params state subgoal smt_filter facts
   729     val (chained_lemmas, other_lemmas) = split_used_facts (map fst used_facts)
   730     val outcome = outcome |> Option.map failure_from_smt_failure
   731     val message =
   732       case outcome of
   733         NONE =>
   734         let
   735           val (method, settings) =
   736             if can_apply_metis debug state subgoal (map snd used_facts) then
   737               ("metis", "")
   738             else
   739               ("smt", if name = SMT_Solver.solver_name_of ctxt then ""
   740                       else "smt_solver = " ^ maybe_quote name)
   741         in
   742           try_command_line (proof_banner auto)
   743               (apply_on_subgoal settings subgoal subgoal_count ^
   744                command_call method (map fst other_lemmas)) ^
   745           minimize_line minimize_command
   746                         (map fst (other_lemmas @ chained_lemmas)) ^
   747           (if verbose then
   748              "\nSMT solver real CPU time: " ^
   749              string_from_time (Time.fromMilliseconds (the run_time_in_msecs)) ^
   750              "."
   751            else
   752              "")
   753         end
   754       | SOME failure => string_for_failure failure
   755   in
   756     {outcome = outcome, used_facts = map fst used_facts,
   757      run_time_in_msecs = run_time_in_msecs, message = message}
   758   end
   759 
   760 fun get_prover ctxt auto name =
   761   let val thy = Proof_Context.theory_of ctxt in
   762     if is_smt_prover ctxt name then
   763       run_smt_solver auto name
   764     else if member (op =) (supported_atps thy) name then
   765       run_atp auto name (get_atp thy name)
   766     else
   767       error ("No such prover: " ^ name ^ ".")
   768   end
   769 
   770 val setup =
   771   dest_dir_setup
   772   #> problem_prefix_setup
   773   #> measure_run_time_setup
   774 
   775 end;