src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Thu, 26 Jul 2012 10:48:03 +0200
changeset 49547 c0f44941e674
parent 49407 ca998fa08cd9
child 49671 5caa414ce9a2
permissions -rw-r--r--
Sledgehammer already has its own ways of reporting and recovering from crashes in external provers -- no need to additionally print scores of warnings (cf. 4b0daca2bf88)
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_provers.ML
     2     Author:     Fabian Immler, TU Muenchen
     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 stature = ATP_Problem_Generate.stature
    13   type type_enc = ATP_Problem_Generate.type_enc
    14   type reconstructor = ATP_Proof_Reconstruct.reconstructor
    15   type play = ATP_Proof_Reconstruct.play
    16   type minimize_command = ATP_Proof_Reconstruct.minimize_command
    17 
    18   datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
    19 
    20   type params =
    21     {debug : bool,
    22      verbose : bool,
    23      overlord : bool,
    24      blocking : bool,
    25      provers : string list,
    26      type_enc : string option,
    27      strict : bool,
    28      lam_trans : string option,
    29      uncurried_aliases : bool option,
    30      learn : bool,
    31      fact_filter : string option,
    32      max_facts : int option,
    33      fact_thresholds : real * real,
    34      max_mono_iters : int option,
    35      max_new_mono_instances : int option,
    36      isar_proof : bool,
    37      isar_shrink_factor : int,
    38      slice : bool,
    39      minimize : bool option,
    40      timeout : Time.time,
    41      preplay_timeout : Time.time,
    42      expect : string}
    43 
    44   type relevance_fudge =
    45     {local_const_multiplier : real,
    46      worse_irrel_freq : real,
    47      higher_order_irrel_weight : real,
    48      abs_rel_weight : real,
    49      abs_irrel_weight : real,
    50      skolem_irrel_weight : real,
    51      theory_const_rel_weight : real,
    52      theory_const_irrel_weight : real,
    53      chained_const_irrel_weight : real,
    54      intro_bonus : real,
    55      elim_bonus : real,
    56      simp_bonus : real,
    57      local_bonus : real,
    58      assum_bonus : real,
    59      chained_bonus : real,
    60      max_imperfect : real,
    61      max_imperfect_exp : real,
    62      threshold_divisor : real,
    63      ridiculous_threshold : real}
    64 
    65   datatype prover_fact =
    66     Untranslated_Fact of (string * stature) * thm |
    67     SMT_Weighted_Fact of (string * stature) * (int option * thm)
    68 
    69   type prover_problem =
    70     {state : Proof.state,
    71      goal : thm,
    72      subgoal : int,
    73      subgoal_count : int,
    74      facts : prover_fact list}
    75 
    76   type prover_result =
    77     {outcome : failure option,
    78      used_facts : (string * stature) list,
    79      run_time : Time.time,
    80      preplay : unit -> play,
    81      message : play -> string,
    82      message_tail : string}
    83 
    84   type prover =
    85     params -> ((string * string list) list -> string -> minimize_command)
    86     -> prover_problem -> prover_result
    87 
    88   val dest_dir : string Config.T
    89   val problem_prefix : string Config.T
    90   val completish : bool Config.T
    91   val atp_full_names : bool Config.T
    92   val smt_triggers : bool Config.T
    93   val smt_weights : bool Config.T
    94   val smt_weight_min_facts : int Config.T
    95   val smt_min_weight : int Config.T
    96   val smt_max_weight : int Config.T
    97   val smt_max_weight_index : int Config.T
    98   val smt_weight_curve : (int -> int) Unsynchronized.ref
    99   val smt_max_slices : int Config.T
   100   val smt_slice_fact_frac : real Config.T
   101   val smt_slice_time_frac : real Config.T
   102   val smt_slice_min_secs : int Config.T
   103   val SledgehammerN : string
   104   val plain_metis : reconstructor
   105   val select_smt_solver : string -> Proof.context -> Proof.context
   106   val extract_reconstructor :
   107     params -> reconstructor -> string * (string * string list) list
   108   val is_reconstructor : string -> bool
   109   val is_atp : theory -> string -> bool
   110   val is_smt_prover : Proof.context -> string -> bool
   111   val is_ho_atp: Proof.context -> string -> bool
   112   val is_unit_equational_atp : Proof.context -> string -> bool
   113   val is_prover_supported : Proof.context -> string -> bool
   114   val is_prover_installed : Proof.context -> string -> bool
   115   val default_max_facts_for_prover : Proof.context -> bool -> string -> int
   116   val is_unit_equality : term -> bool
   117   val is_appropriate_prop_for_prover : Proof.context -> string -> term -> bool
   118   val is_built_in_const_for_prover :
   119     Proof.context -> string -> string * typ -> term list -> bool * term list
   120   val atp_relevance_fudge : relevance_fudge
   121   val smt_relevance_fudge : relevance_fudge
   122   val relevance_fudge_for_prover : Proof.context -> string -> relevance_fudge
   123   val weight_smt_fact :
   124     Proof.context -> int -> ((string * stature) * thm) * int
   125     -> (string * stature) * (int option * thm)
   126   val untranslated_fact : prover_fact -> (string * stature) * thm
   127   val smt_weighted_fact :
   128     Proof.context -> int -> prover_fact * int
   129     -> (string * stature) * (int option * thm)
   130   val supported_provers : Proof.context -> unit
   131   val kill_provers : unit -> unit
   132   val running_provers : unit -> unit
   133   val messages : int option -> unit
   134   val filter_used_facts : ''a list -> (''a * 'b) list -> (''a * 'b) list
   135   val get_prover : Proof.context -> mode -> string -> prover
   136 end;
   137 
   138 structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
   139 struct
   140 
   141 open ATP_Util
   142 open ATP_Problem
   143 open ATP_Proof
   144 open ATP_Systems
   145 open ATP_Problem_Generate
   146 open ATP_Proof_Reconstruct
   147 open Metis_Tactic
   148 open Sledgehammer_Util
   149 
   150 
   151 (** The Sledgehammer **)
   152 
   153 datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
   154 
   155 (* Identifier that distinguishes Sledgehammer from other tools that could use
   156    "Async_Manager". *)
   157 val SledgehammerN = "Sledgehammer"
   158 
   159 val reconstructor_names = [metisN, smtN]
   160 val plain_metis = Metis (hd partial_type_encs, combsN)
   161 val is_reconstructor = member (op =) reconstructor_names
   162 
   163 val is_atp = member (op =) o supported_atps
   164 
   165 val select_smt_solver = Context.proof_map o SMT_Config.select_solver
   166 
   167 fun is_smt_prover ctxt = member (op =) (SMT_Solver.available_solvers_of ctxt)
   168 
   169 fun is_atp_for_format is_format ctxt name =
   170   let val thy = Proof_Context.theory_of ctxt in
   171     case try (get_atp thy) name of
   172       SOME config =>
   173       exists (fn (_, (_, ((_, format, _, _, _), _))) => is_format format)
   174              (#best_slices (config ()) ctxt)
   175     | NONE => false
   176   end
   177 
   178 val is_unit_equational_atp = is_atp_for_format (curry (op =) CNF_UEQ)
   179 val is_ho_atp = is_atp_for_format is_format_higher_order
   180 
   181 fun is_prover_supported ctxt =
   182   let val thy = Proof_Context.theory_of ctxt in
   183     is_reconstructor orf is_atp thy orf is_smt_prover ctxt
   184   end
   185 
   186 fun is_prover_installed ctxt =
   187   is_reconstructor orf is_smt_prover ctxt orf
   188   is_atp_installed (Proof_Context.theory_of ctxt)
   189 
   190 fun get_slices slice slices =
   191   (0 upto length slices - 1) ~~ slices |> not slice ? (List.last #> single)
   192 
   193 val reconstructor_default_max_facts = 20
   194 
   195 fun default_max_facts_for_prover ctxt slice name =
   196   let val thy = Proof_Context.theory_of ctxt in
   197     if is_reconstructor name then
   198       reconstructor_default_max_facts
   199     else if is_atp thy name then
   200       fold (Integer.max o #1 o fst o snd o snd o snd)
   201            (get_slices slice (#best_slices (get_atp thy name ()) ctxt)) 0
   202     else (* is_smt_prover ctxt name *)
   203       SMT_Solver.default_max_relevant ctxt name
   204   end
   205 
   206 fun is_if (@{const_name If}, _) = true
   207   | is_if _ = false
   208 
   209 (* Beware of "if and only if" (which is translated as such) and "If" (which is
   210    translated to conditional equations). *)
   211 fun is_good_unit_equality T t u =
   212   T <> @{typ bool} andalso not (exists (exists_Const is_if) [t, u])
   213 
   214 fun is_unit_equality (@{const Trueprop} $ t) = is_unit_equality t
   215   | is_unit_equality (Const (@{const_name all}, _) $ Abs (_, _, t)) =
   216     is_unit_equality t
   217   | is_unit_equality (Const (@{const_name All}, _) $ Abs (_, _, t)) =
   218     is_unit_equality t
   219   | is_unit_equality (Const (@{const_name "=="}, Type (_, [T, _])) $ t $ u) =
   220     is_good_unit_equality T t u
   221   | is_unit_equality (Const (@{const_name HOL.eq}, Type (_ , [T, _])) $ t $ u) =
   222     is_good_unit_equality T t u
   223   | is_unit_equality _ = false
   224 
   225 fun is_appropriate_prop_for_prover ctxt name =
   226   if is_unit_equational_atp ctxt name then is_unit_equality else K true
   227 
   228 fun is_built_in_const_for_prover ctxt name =
   229   if is_smt_prover ctxt name then
   230     let val ctxt = ctxt |> select_smt_solver name in
   231       fn x => fn ts =>
   232          if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
   233            (true, [])
   234          else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
   235            (true, ts)
   236          else
   237            (false, ts)
   238     end
   239   else
   240     fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
   241 
   242 (* FUDGE *)
   243 val atp_relevance_fudge =
   244   {local_const_multiplier = 1.5,
   245    worse_irrel_freq = 100.0,
   246    higher_order_irrel_weight = 1.05,
   247    abs_rel_weight = 0.5,
   248    abs_irrel_weight = 2.0,
   249    skolem_irrel_weight = 0.05,
   250    theory_const_rel_weight = 0.5,
   251    theory_const_irrel_weight = 0.25,
   252    chained_const_irrel_weight = 0.25,
   253    intro_bonus = 0.15,
   254    elim_bonus = 0.15,
   255    simp_bonus = 0.15,
   256    local_bonus = 0.55,
   257    assum_bonus = 1.05,
   258    chained_bonus = 1.5,
   259    max_imperfect = 11.5,
   260    max_imperfect_exp = 1.0,
   261    threshold_divisor = 2.0,
   262    ridiculous_threshold = 0.01}
   263 
   264 (* FUDGE (FIXME) *)
   265 val smt_relevance_fudge =
   266   {local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
   267    worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
   268    higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
   269    abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
   270    abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
   271    skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
   272    theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
   273    theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
   274    chained_const_irrel_weight = #chained_const_irrel_weight atp_relevance_fudge,
   275    intro_bonus = #intro_bonus atp_relevance_fudge,
   276    elim_bonus = #elim_bonus atp_relevance_fudge,
   277    simp_bonus = #simp_bonus atp_relevance_fudge,
   278    local_bonus = #local_bonus atp_relevance_fudge,
   279    assum_bonus = #assum_bonus atp_relevance_fudge,
   280    chained_bonus = #chained_bonus atp_relevance_fudge,
   281    max_imperfect = #max_imperfect atp_relevance_fudge,
   282    max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
   283    threshold_divisor = #threshold_divisor atp_relevance_fudge,
   284    ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
   285 
   286 fun relevance_fudge_for_prover ctxt name =
   287   if is_smt_prover ctxt name then smt_relevance_fudge else atp_relevance_fudge
   288 
   289 fun supported_provers ctxt =
   290   let
   291     val thy = Proof_Context.theory_of ctxt
   292     val (remote_provers, local_provers) =
   293       reconstructor_names @
   294       sort_strings (supported_atps thy) @
   295       sort_strings (SMT_Solver.available_solvers_of ctxt)
   296       |> List.partition (String.isPrefix remote_prefix)
   297   in
   298     Output.urgent_message ("Supported provers: " ^
   299                            commas (local_provers @ remote_provers) ^ ".")
   300   end
   301 
   302 fun kill_provers () = Async_Manager.kill_threads SledgehammerN "prover"
   303 fun running_provers () = Async_Manager.running_threads SledgehammerN "prover"
   304 val messages = Async_Manager.thread_messages SledgehammerN "prover"
   305 
   306 
   307 (** problems, results, ATPs, etc. **)
   308 
   309 type params =
   310   {debug : bool,
   311    verbose : bool,
   312    overlord : bool,
   313    blocking : bool,
   314    provers : string list,
   315    type_enc : string option,
   316    strict : bool,
   317    lam_trans : string option,
   318    uncurried_aliases : bool option,
   319    learn : bool,
   320    fact_filter : string option,
   321    max_facts : int option,
   322    fact_thresholds : real * real,
   323    max_mono_iters : int option,
   324    max_new_mono_instances : int option,
   325    isar_proof : bool,
   326    isar_shrink_factor : int,
   327    slice : bool,
   328    minimize : bool option,
   329    timeout : Time.time,
   330    preplay_timeout : Time.time,
   331    expect : string}
   332 
   333 type relevance_fudge =
   334   {local_const_multiplier : real,
   335    worse_irrel_freq : real,
   336    higher_order_irrel_weight : real,
   337    abs_rel_weight : real,
   338    abs_irrel_weight : real,
   339    skolem_irrel_weight : real,
   340    theory_const_rel_weight : real,
   341    theory_const_irrel_weight : real,
   342    chained_const_irrel_weight : real,
   343    intro_bonus : real,
   344    elim_bonus : real,
   345    simp_bonus : real,
   346    local_bonus : real,
   347    assum_bonus : real,
   348    chained_bonus : real,
   349    max_imperfect : real,
   350    max_imperfect_exp : real,
   351    threshold_divisor : real,
   352    ridiculous_threshold : real}
   353 
   354 datatype prover_fact =
   355   Untranslated_Fact of (string * stature) * thm |
   356   SMT_Weighted_Fact of (string * stature) * (int option * thm)
   357 
   358 type prover_problem =
   359   {state : Proof.state,
   360    goal : thm,
   361    subgoal : int,
   362    subgoal_count : int,
   363    facts : prover_fact list}
   364 
   365 type prover_result =
   366   {outcome : failure option,
   367    used_facts : (string * stature) list,
   368    run_time : Time.time,
   369    preplay : unit -> play,
   370    message : play -> string,
   371    message_tail : string}
   372 
   373 type prover =
   374   params -> ((string * string list) list -> string -> minimize_command)
   375   -> prover_problem -> prover_result
   376 
   377 (* configuration attributes *)
   378 
   379 (* Empty string means create files in Isabelle's temporary files directory. *)
   380 val dest_dir =
   381   Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
   382 val problem_prefix =
   383   Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
   384 val completish =
   385   Attrib.setup_config_bool @{binding sledgehammer_completish} (K false)
   386 
   387 (* In addition to being easier to read, readable names are often much shorter,
   388    especially if types are mangled in names. This makes a difference for some
   389    provers (e.g., E). For these reason, short names are enabled by default. *)
   390 val atp_full_names =
   391   Attrib.setup_config_bool @{binding sledgehammer_atp_full_names} (K false)
   392 
   393 val smt_triggers =
   394   Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
   395 val smt_weights =
   396   Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
   397 val smt_weight_min_facts =
   398   Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
   399 
   400 (* FUDGE *)
   401 val smt_min_weight =
   402   Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
   403 val smt_max_weight =
   404   Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
   405 val smt_max_weight_index =
   406   Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
   407 val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
   408 
   409 fun smt_fact_weight ctxt j num_facts =
   410   if Config.get ctxt smt_weights andalso
   411      num_facts >= Config.get ctxt smt_weight_min_facts then
   412     let
   413       val min = Config.get ctxt smt_min_weight
   414       val max = Config.get ctxt smt_max_weight
   415       val max_index = Config.get ctxt smt_max_weight_index
   416       val curve = !smt_weight_curve
   417     in
   418       SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
   419             div curve max_index)
   420     end
   421   else
   422     NONE
   423 
   424 fun weight_smt_fact ctxt num_facts ((info, th), j) =
   425   let val thy = Proof_Context.theory_of ctxt in
   426     (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
   427   end
   428 
   429 fun untranslated_fact (Untranslated_Fact p) = p
   430   | untranslated_fact (SMT_Weighted_Fact (info, (_, th))) = (info, th)
   431 fun smt_weighted_fact _ _ (SMT_Weighted_Fact p, _) = p
   432   | smt_weighted_fact ctxt num_facts (fact, j) =
   433     (untranslated_fact fact, j) |> weight_smt_fact ctxt num_facts
   434 
   435 fun overlord_file_location_for_prover prover =
   436   (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
   437 
   438 fun with_path cleanup after f path =
   439   Exn.capture f path
   440   |> tap (fn _ => cleanup path)
   441   |> Exn.release
   442   |> tap (after path)
   443 
   444 fun proof_banner mode name =
   445   case mode of
   446     Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
   447   | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
   448   | _ => "Try this"
   449 
   450 fun bunch_of_reconstructors needs_full_types lam_trans =
   451   [(false, Metis (partial_type_enc, lam_trans false)),
   452    (true, Metis (full_type_enc, lam_trans false)),
   453    (false, Metis (no_typesN, lam_trans true)),
   454    (true, Metis (really_full_type_enc, lam_trans true)),
   455    (true, SMT)]
   456   |> map_filter (fn (full_types, reconstr) =>
   457                     if needs_full_types andalso not full_types then NONE
   458                     else SOME reconstr)
   459 
   460 fun extract_reconstructor ({type_enc, lam_trans, ...} : params)
   461                           (Metis (type_enc', lam_trans')) =
   462     let
   463       val override_params =
   464         (if is_none type_enc andalso type_enc' = hd partial_type_encs then
   465            []
   466          else
   467            [("type_enc", [hd (unalias_type_enc type_enc')])]) @
   468         (if is_none lam_trans andalso lam_trans' = metis_default_lam_trans then
   469            []
   470          else
   471            [("lam_trans", [lam_trans'])])
   472     in (metisN, override_params) end
   473   | extract_reconstructor _ SMT = (smtN, [])
   474 
   475 (* based on "Mirabelle.can_apply" and generalized *)
   476 fun timed_apply timeout tac state i =
   477   let
   478     val {context = ctxt, facts, goal} = Proof.goal state
   479     val full_tac = Method.insert_tac facts i THEN tac ctxt i
   480   in TimeLimit.timeLimit timeout (try (Seq.pull o full_tac)) goal end
   481 
   482 fun tac_for_reconstructor (Metis (type_enc, lam_trans)) =
   483     metis_tac [type_enc] lam_trans
   484   | tac_for_reconstructor SMT = SMT_Solver.smt_tac
   485 
   486 fun timed_reconstructor reconstr debug timeout ths =
   487   (Config.put Metis_Tactic.verbose debug
   488    #> Config.put SMT_Config.verbose debug
   489    #> (fn ctxt => tac_for_reconstructor reconstr ctxt ths))
   490   |> timed_apply timeout
   491 
   492 fun filter_used_facts used = filter (member (op =) used o fst)
   493 
   494 fun play_one_line_proof mode debug verbose timeout pairs state i preferred
   495                         reconstrs =
   496   let
   497     val _ =
   498       if mode = Minimize andalso Time.> (timeout, Time.zeroTime) then
   499         Output.urgent_message "Preplaying proof..."
   500       else
   501         ()
   502     val ths = pairs |> sort_wrt (fst o fst) |> map snd
   503     fun get_preferred reconstrs =
   504       if member (op =) reconstrs preferred then preferred
   505       else List.last reconstrs
   506     fun play [] [] = Failed_to_Play (get_preferred reconstrs)
   507       | play timed_outs [] =
   508         Trust_Playable (get_preferred timed_outs, SOME timeout)
   509       | play timed_out (reconstr :: reconstrs) =
   510         let
   511           val _ =
   512             if verbose then
   513               "Trying \"" ^ string_for_reconstructor reconstr ^ "\" for " ^
   514               string_from_time timeout ^ "..."
   515               |> Output.urgent_message
   516             else
   517               ()
   518           val timer = Timer.startRealTimer ()
   519         in
   520           case timed_reconstructor reconstr debug timeout ths state i of
   521             SOME (SOME _) => Played (reconstr, Timer.checkRealTimer timer)
   522           | _ => play timed_out reconstrs
   523         end
   524         handle TimeLimit.TimeOut => play (reconstr :: timed_out) reconstrs
   525   in
   526     if timeout = Time.zeroTime then
   527       Trust_Playable (get_preferred reconstrs, NONE)
   528     else
   529       play [] reconstrs
   530   end
   531 
   532 
   533 (* generic TPTP-based ATPs *)
   534 
   535 (* Too general means, positive equality literal with a variable X as one
   536    operand, when X does not occur properly in the other operand. This rules out
   537    clearly inconsistent facts such as X = a | X = b, though it by no means
   538    guarantees soundness. *)
   539 
   540 (* Unwanted equalities are those between a (bound or schematic) variable that
   541    does not properly occur in the second operand. *)
   542 val is_exhaustive_finite =
   543   let
   544     fun is_bad_equal (Var z) t =
   545         not (exists_subterm (fn Var z' => z = z' | _ => false) t)
   546       | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
   547       | is_bad_equal _ _ = false
   548     fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
   549     fun do_formula pos t =
   550       case (pos, t) of
   551         (_, @{const Trueprop} $ t1) => do_formula pos t1
   552       | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
   553         do_formula pos t'
   554       | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
   555         do_formula pos t'
   556       | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
   557         do_formula pos t'
   558       | (_, @{const "==>"} $ t1 $ t2) =>
   559         do_formula (not pos) t1 andalso
   560         (t2 = @{prop False} orelse do_formula pos t2)
   561       | (_, @{const HOL.implies} $ t1 $ t2) =>
   562         do_formula (not pos) t1 andalso
   563         (t2 = @{const False} orelse do_formula pos t2)
   564       | (_, @{const Not} $ t1) => do_formula (not pos) t1
   565       | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
   566       | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
   567       | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
   568       | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
   569       | _ => false
   570   in do_formula true end
   571 
   572 fun has_bound_or_var_of_type pred =
   573   exists_subterm (fn Var (_, T as Type _) => pred T
   574                    | Abs (_, T as Type _, _) => pred T
   575                    | _ => false)
   576 
   577 (* Facts are forbidden to contain variables of these types. The typical reason
   578    is that they lead to unsoundness. Note that "unit" satisfies numerous
   579    equations like "?x = ()". The resulting clauses will have no type constraint,
   580    yielding false proofs. Even "bool" leads to many unsound proofs, though only
   581    for higher-order problems. *)
   582 
   583 (* Facts containing variables of type "unit" or "bool" or of the form
   584    "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
   585    are omitted. *)
   586 fun is_dangerous_prop ctxt =
   587   transform_elim_prop
   588   #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
   589       is_exhaustive_finite)
   590 
   591 (* Important messages are important but not so important that users want to see
   592    them each time. *)
   593 val atp_important_message_keep_quotient = 25
   594 
   595 fun choose_type_enc soundness best_type_enc format =
   596   the_default best_type_enc
   597   #> type_enc_from_string soundness
   598   #> adjust_type_enc format
   599 
   600 val metis_minimize_max_time = seconds 2.0
   601 
   602 fun choose_minimize_command params minimize_command name preplay =
   603   let
   604     val (name, override_params) =
   605       case preplay of
   606         Played (reconstr, time) =>
   607         if Time.<= (time, metis_minimize_max_time) then
   608           extract_reconstructor params reconstr
   609         else
   610           (name, [])
   611       | _ => (name, [])
   612   in minimize_command override_params name end
   613 
   614 fun repair_monomorph_context max_iters best_max_iters max_new_instances
   615                              best_max_new_instances =
   616   Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
   617   #> Config.put Monomorph.max_new_instances
   618          (max_new_instances |> the_default best_max_new_instances)
   619   #> Config.put Monomorph.keep_partial_instances false
   620 
   621 fun suffix_for_mode Auto_Try = "_auto_try"
   622   | suffix_for_mode Try = "_try"
   623   | suffix_for_mode Normal = ""
   624   | suffix_for_mode MaSh = "_mash"
   625   | suffix_for_mode Auto_Minimize = "_auto_min"
   626   | suffix_for_mode Minimize = "_min"
   627 
   628 (* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on
   629    Linux appears to be the only ATP that does not honor its time limit. *)
   630 val atp_timeout_slack = seconds 1.0
   631 
   632 val mono_max_privileged_facts = 10
   633 
   634 fun run_atp mode name
   635         ({exec, arguments, proof_delims, known_failures, prem_role, best_slices,
   636           best_max_mono_iters, best_max_new_mono_instances, ...} : atp_config)
   637         (params as {debug, verbose, overlord, type_enc, strict, lam_trans,
   638                     uncurried_aliases, max_facts, max_mono_iters,
   639                     max_new_mono_instances, isar_proof, isar_shrink_factor,
   640                     slice, timeout, preplay_timeout, ...})
   641         minimize_command
   642         ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
   643   let
   644     val thy = Proof.theory_of state
   645     val ctxt = Proof.context_of state
   646     val atp_mode =
   647       if Config.get ctxt completish then Sledgehammer_Completish
   648       else Sledgehammer
   649     val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
   650     val (dest_dir, problem_prefix) =
   651       if overlord then overlord_file_location_for_prover name
   652       else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
   653     val problem_file_name =
   654       Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
   655                   suffix_for_mode mode ^ "_" ^ string_of_int subgoal)
   656     val problem_path_name =
   657       if dest_dir = "" then
   658         File.tmp_path problem_file_name
   659       else if File.exists (Path.explode dest_dir) then
   660         Path.append (Path.explode dest_dir) problem_file_name
   661       else
   662         error ("No such directory: " ^ quote dest_dir ^ ".")
   663     val command0 =
   664       case find_first (fn var => getenv var <> "") (fst exec) of
   665         SOME var =>
   666         let
   667           val pref = getenv var ^ "/"
   668           val paths = map (Path.explode o prefix pref) (snd exec)
   669         in
   670           case find_first File.exists paths of
   671             SOME path => path
   672           | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".")
   673         end
   674       | NONE => error ("The environment variable " ^ quote (hd (fst exec)) ^
   675                        " is not set.")
   676     fun split_time s =
   677       let
   678         val split = String.tokens (fn c => str c = "\n")
   679         val (output, t) =
   680           s |> split |> (try split_last #> the_default ([], "0"))
   681             |>> cat_lines
   682         fun as_num f = f >> (fst o read_int)
   683         val num = as_num (Scan.many1 Symbol.is_ascii_digit)
   684         val digit = Scan.one Symbol.is_ascii_digit
   685         val num3 = as_num (digit ::: digit ::: (digit >> single))
   686         val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
   687         val as_time =
   688           raw_explode #> Scan.read Symbol.stopper time #> the_default 0
   689       in (output, as_time t |> Time.fromMilliseconds) end
   690     fun run_on prob_file =
   691       let
   692         (* If slicing is disabled, we expand the last slice to fill the entire
   693            time available. *)
   694         val actual_slices = get_slices slice (best_slices ctxt)
   695         val num_actual_slices = length actual_slices
   696         fun monomorphize_facts facts =
   697           let
   698             val ctxt =
   699               ctxt
   700               |> repair_monomorph_context max_mono_iters best_max_mono_iters
   701                       max_new_mono_instances best_max_new_mono_instances
   702             (* pseudo-theorem involving the same constants as the subgoal *)
   703             val subgoal_th =
   704               Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy
   705             val rths =
   706               facts |> chop mono_max_privileged_facts
   707                     |>> map (pair 1 o snd)
   708                     ||> map (pair 2 o snd)
   709                     |> op @
   710                     |> cons (0, subgoal_th)
   711           in
   712             Monomorph.monomorph atp_schematic_consts_of rths ctxt |> fst |> tl
   713             |> curry ListPair.zip (map fst facts)
   714             |> maps (fn (name, rths) =>
   715                         map (pair name o zero_var_indexes o snd) rths)
   716           end
   717         fun run_slice time_left (cache_key, cache_value)
   718                 (slice, (time_frac, (complete,
   719                      (key as (best_max_facts, format, best_type_enc,
   720                               best_lam_trans, best_uncurried_aliases),
   721                       extra)))) =
   722           let
   723             val num_facts =
   724               length facts |> is_none max_facts ? Integer.min best_max_facts
   725             val soundness = if strict then Strict else Non_Strict
   726             val type_enc =
   727               type_enc |> choose_type_enc soundness best_type_enc format
   728             val sound = is_type_enc_sound type_enc
   729             val real_ms = Real.fromInt o Time.toMilliseconds
   730             val slice_timeout =
   731               ((real_ms time_left
   732                 |> (if slice < num_actual_slices - 1 then
   733                       curry Real.min (time_frac * real_ms timeout)
   734                     else
   735                       I))
   736                * 0.001) |> seconds
   737             val generous_slice_timeout =
   738               Time.+ (slice_timeout, atp_timeout_slack)
   739             val _ =
   740               if debug then
   741                 quote name ^ " slice #" ^ string_of_int (slice + 1) ^
   742                 " with " ^ string_of_int num_facts ^ " fact" ^
   743                 plural_s num_facts ^ " for " ^
   744                 string_from_time slice_timeout ^ "..."
   745                 |> Output.urgent_message
   746               else
   747                 ()
   748             val readable_names = not (Config.get ctxt atp_full_names)
   749             val lam_trans =
   750               case lam_trans of
   751                 SOME s => s
   752               | NONE => best_lam_trans
   753             val uncurried_aliases =
   754               case uncurried_aliases of
   755                 SOME b => b
   756               | NONE => best_uncurried_aliases
   757             val value as (atp_problem, _, fact_names, _, _) =
   758               if cache_key = SOME key then
   759                 cache_value
   760               else
   761                 facts
   762                 |> map untranslated_fact
   763                 |> not sound
   764                    ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
   765                 |> take num_facts
   766                 |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts
   767                 |> map (apsnd prop_of)
   768                 |> prepare_atp_problem ctxt format prem_role type_enc atp_mode
   769                                        lam_trans uncurried_aliases
   770                                        readable_names true hyp_ts concl_t
   771             fun sel_weights () = atp_problem_selection_weights atp_problem
   772             fun ord_info () = atp_problem_term_order_info atp_problem
   773             val ord = effective_term_order ctxt name
   774             val full_proof = debug orelse isar_proof
   775             val args = arguments ctxt full_proof extra slice_timeout
   776                                  (ord, ord_info, sel_weights)
   777             val command =
   778               "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^
   779               File.shell_path prob_file ^ ")"
   780               |> enclose "TIMEFORMAT='%3R'; { time " " ; }"
   781             val _ =
   782               atp_problem
   783               |> lines_for_atp_problem format ord ord_info
   784               |> cons ("% " ^ command ^ "\n")
   785               |> File.write_list prob_file
   786             val ((output, run_time), (atp_proof, outcome)) =
   787               TimeLimit.timeLimit generous_slice_timeout
   788                                   Isabelle_System.bash_output command
   789               |>> (if overlord then
   790                      prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
   791                    else
   792                      I)
   793               |> fst |> split_time
   794               |> (fn accum as (output, _) =>
   795                      (accum,
   796                       extract_tstplike_proof_and_outcome verbose complete
   797                           proof_delims known_failures output
   798                       |>> atp_proof_from_tstplike_proof atp_problem
   799                       handle UNRECOGNIZED_ATP_PROOF () =>
   800                              ([], SOME ProofIncomplete)))
   801               handle TimeLimit.TimeOut =>
   802                      (("", slice_timeout), ([], SOME TimedOut))
   803             val outcome =
   804               case outcome of
   805                 NONE =>
   806                 (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof
   807                       |> Option.map (sort string_ord) of
   808                    SOME facts =>
   809                    let
   810                      val failure =
   811                        UnsoundProof (is_type_enc_sound type_enc, facts)
   812                    in
   813                      if debug then (warning (string_for_failure failure); NONE)
   814                      else SOME failure
   815                    end
   816                  | NONE => NONE)
   817               | _ => outcome
   818           in ((SOME key, value), (output, run_time, atp_proof, outcome)) end
   819         val timer = Timer.startRealTimer ()
   820         fun maybe_run_slice slice
   821                 (result as (cache, (_, run_time0, _, SOME _))) =
   822             let
   823               val time_left = Time.- (timeout, Timer.checkRealTimer timer)
   824             in
   825               if Time.<= (time_left, Time.zeroTime) then
   826                 result
   827               else
   828                 run_slice time_left cache slice
   829                 |> (fn (cache, (output, run_time, atp_proof, outcome)) =>
   830                        (cache, (output, Time.+ (run_time0, run_time),
   831                                 atp_proof, outcome)))
   832             end
   833           | maybe_run_slice _ result = result
   834       in
   835         ((NONE, ([], Symtab.empty, Vector.fromList [], [], Symtab.empty)),
   836          ("", Time.zeroTime, [], SOME InternalError))
   837         |> fold maybe_run_slice actual_slices
   838       end
   839 
   840     (* If the problem file has not been exported, remove it; otherwise, export
   841        the proof file too. *)
   842     fun cleanup prob_file =
   843       if dest_dir = "" then try File.rm prob_file else NONE
   844     fun export prob_file (_, (output, _, _, _)) =
   845       if dest_dir = "" then ()
   846       else File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
   847     val ((_, (_, pool, fact_names, _, sym_tab)),
   848          (output, run_time, atp_proof, outcome)) =
   849       with_path cleanup export run_on problem_path_name
   850     val important_message =
   851       if mode = Normal andalso
   852          random_range 0 (atp_important_message_keep_quotient - 1) = 0 then
   853         extract_important_message output
   854       else
   855         ""
   856     val (used_facts, preplay, message, message_tail) =
   857       case outcome of
   858         NONE =>
   859         let
   860           val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof
   861           val needs_full_types = is_typed_helper_used_in_atp_proof atp_proof
   862           val reconstrs =
   863             bunch_of_reconstructors needs_full_types
   864                 (lam_trans_from_atp_proof atp_proof
   865                  o (fn desperate => if desperate then hide_lamsN
   866                                     else metis_default_lam_trans))
   867         in
   868           (used_facts,
   869            fn () =>
   870               let
   871                 val used_pairs =
   872                   facts |> map untranslated_fact |> filter_used_facts used_facts
   873               in
   874                 play_one_line_proof mode debug verbose preplay_timeout
   875                     used_pairs state subgoal (hd reconstrs) reconstrs
   876               end,
   877            fn preplay =>
   878               let
   879                 val isar_params =
   880                   (debug, isar_shrink_factor, pool, fact_names, sym_tab,
   881                    atp_proof, goal)
   882                 val one_line_params =
   883                   (preplay, proof_banner mode name, used_facts,
   884                    choose_minimize_command params minimize_command name preplay,
   885                    subgoal, subgoal_count)
   886               in proof_text ctxt isar_proof isar_params one_line_params end,
   887            (if verbose then
   888               "\nATP real CPU time: " ^ string_from_time run_time ^ "."
   889             else
   890               "") ^
   891            (if important_message <> "" then
   892               "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
   893               important_message
   894             else
   895               ""))
   896         end
   897       | SOME failure =>
   898         ([], K (Failed_to_Play plain_metis),
   899          fn _ => string_for_failure failure, "")
   900   in
   901     {outcome = outcome, used_facts = used_facts, run_time = run_time,
   902      preplay = preplay, message = message, message_tail = message_tail}
   903   end
   904 
   905 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
   906    these are sorted out properly in the SMT module, we have to interpret these
   907    ourselves. *)
   908 val remote_smt_failures =
   909   [(2, NoLibwwwPerl),
   910    (22, CantConnect)]
   911 val z3_failures =
   912   [(101, OutOfResources),
   913    (103, MalformedInput),
   914    (110, MalformedInput)]
   915 val unix_failures =
   916   [(139, Crashed)]
   917 val smt_failures = remote_smt_failures @ z3_failures @ unix_failures
   918 
   919 fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
   920     if is_real_cex then Unprovable else GaveUp
   921   | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
   922   | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
   923     (case AList.lookup (op =) smt_failures code of
   924        SOME failure => failure
   925      | NONE => UnknownError ("Abnormal termination with exit code " ^
   926                              string_of_int code ^ "."))
   927   | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
   928   | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
   929     UnknownError msg
   930 
   931 (* FUDGE *)
   932 val smt_max_slices =
   933   Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
   934 val smt_slice_fact_frac =
   935   Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.5)
   936 val smt_slice_time_frac =
   937   Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.5)
   938 val smt_slice_min_secs =
   939   Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 5)
   940 
   941 fun smt_filter_loop ctxt name
   942                     ({debug, verbose, overlord, max_mono_iters,
   943                       max_new_mono_instances, timeout, slice, ...} : params)
   944                     state i =
   945   let
   946     val max_slices = if slice then Config.get ctxt smt_max_slices else 1
   947     val repair_context =
   948       select_smt_solver name
   949       #> Config.put SMT_Config.verbose debug
   950       #> (if overlord then
   951             Config.put SMT_Config.debug_files
   952                        (overlord_file_location_for_prover name
   953                         |> (fn (path, name) => path ^ "/" ^ name))
   954           else
   955             I)
   956       #> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
   957     val state = state |> Proof.map_context repair_context
   958     fun do_slice timeout slice outcome0 time_so_far facts =
   959       let
   960         val timer = Timer.startRealTimer ()
   961         val state =
   962           state |> Proof.map_context
   963                        (repair_monomorph_context max_mono_iters
   964                             default_max_mono_iters max_new_mono_instances
   965                             default_max_new_mono_instances)
   966         val ms = timeout |> Time.toMilliseconds
   967         val slice_timeout =
   968           if slice < max_slices then
   969             Int.min (ms,
   970                 Int.max (1000 * Config.get ctxt smt_slice_min_secs,
   971                     Real.ceil (Config.get ctxt smt_slice_time_frac
   972                                * Real.fromInt ms)))
   973             |> Time.fromMilliseconds
   974           else
   975             timeout
   976         val num_facts = length facts
   977         val _ =
   978           if debug then
   979             quote name ^ " slice " ^ string_of_int slice ^ " with " ^
   980             string_of_int num_facts ^ " fact" ^ plural_s num_facts ^ " for " ^
   981             string_from_time slice_timeout ^ "..."
   982             |> Output.urgent_message
   983           else
   984             ()
   985         val birth = Timer.checkRealTimer timer
   986         val _ =
   987           if debug then Output.urgent_message "Invoking SMT solver..." else ()
   988         val (outcome, used_facts) =
   989           SMT_Solver.smt_filter_preprocess state facts i
   990           |> SMT_Solver.smt_filter_apply slice_timeout
   991           |> (fn {outcome, used_facts} => (outcome, used_facts))
   992           handle exn => if Exn.is_interrupt exn then
   993                           reraise exn
   994                         else
   995                           (ML_Compiler.exn_message exn
   996                            |> SMT_Failure.Other_Failure |> SOME, [])
   997         val death = Timer.checkRealTimer timer
   998         val outcome0 = if is_none outcome0 then SOME outcome else outcome0
   999         val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
  1000         val too_many_facts_perhaps =
  1001           case outcome of
  1002             NONE => false
  1003           | SOME (SMT_Failure.Counterexample _) => false
  1004           | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
  1005           | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
  1006           | SOME SMT_Failure.Out_Of_Memory => true
  1007           | SOME (SMT_Failure.Other_Failure _) => true
  1008         val timeout = Time.- (timeout, Timer.checkRealTimer timer)
  1009       in
  1010         if too_many_facts_perhaps andalso slice < max_slices andalso
  1011            num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
  1012           let
  1013             val new_num_facts =
  1014               Real.ceil (Config.get ctxt smt_slice_fact_frac
  1015                          * Real.fromInt num_facts)
  1016             val _ =
  1017               if verbose andalso is_some outcome then
  1018                 quote name ^ " invoked with " ^ string_of_int num_facts ^
  1019                 " fact" ^ plural_s num_facts ^ ": " ^
  1020                 string_for_failure (failure_from_smt_failure (the outcome)) ^
  1021                 " Retrying with " ^ string_of_int new_num_facts ^ " fact" ^
  1022                 plural_s new_num_facts ^ "..."
  1023                 |> Output.urgent_message
  1024               else
  1025                 ()
  1026           in
  1027             facts |> take new_num_facts
  1028                   |> do_slice timeout (slice + 1) outcome0 time_so_far
  1029           end
  1030         else
  1031           {outcome = if is_none outcome then NONE else the outcome0,
  1032            used_facts = used_facts, run_time = time_so_far}
  1033       end
  1034   in do_slice timeout 1 NONE Time.zeroTime end
  1035 
  1036 fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...})
  1037         minimize_command
  1038         ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
  1039   let
  1040     val ctxt = Proof.context_of state
  1041     val num_facts = length facts
  1042     val facts = facts ~~ (0 upto num_facts - 1)
  1043                 |> map (smt_weighted_fact ctxt num_facts)
  1044     val {outcome, used_facts = used_pairs, run_time} =
  1045       smt_filter_loop ctxt name params state subgoal facts
  1046     val used_facts = used_pairs |> map fst
  1047     val outcome = outcome |> Option.map failure_from_smt_failure
  1048     val (preplay, message, message_tail) =
  1049       case outcome of
  1050         NONE =>
  1051         (fn () =>
  1052             play_one_line_proof mode debug verbose preplay_timeout used_pairs
  1053                 state subgoal SMT
  1054                 (bunch_of_reconstructors false
  1055                      (fn plain =>
  1056                          if plain then metis_default_lam_trans else liftingN)),
  1057          fn preplay =>
  1058             let
  1059               val one_line_params =
  1060                 (preplay, proof_banner mode name, used_facts,
  1061                  choose_minimize_command params minimize_command name preplay,
  1062                  subgoal, subgoal_count)
  1063             in one_line_proof_text one_line_params end,
  1064          if verbose then
  1065            "\nSMT solver real CPU time: " ^ string_from_time run_time ^ "."
  1066          else
  1067            "")
  1068       | SOME failure =>
  1069         (K (Failed_to_Play plain_metis), fn _ => string_for_failure failure, "")
  1070   in
  1071     {outcome = outcome, used_facts = used_facts, run_time = run_time,
  1072      preplay = preplay, message = message, message_tail = message_tail}
  1073   end
  1074 
  1075 fun run_reconstructor mode name
  1076         (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
  1077         minimize_command
  1078         ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
  1079   let
  1080     val reconstr =
  1081       if name = metisN then
  1082         Metis (type_enc |> the_default (hd partial_type_encs),
  1083                lam_trans |> the_default metis_default_lam_trans)
  1084       else if name = smtN then
  1085         SMT
  1086       else
  1087         raise Fail ("unknown reconstructor: " ^ quote name)
  1088     val used_pairs = facts |> map untranslated_fact
  1089     val used_facts = used_pairs |> map fst
  1090   in
  1091     case play_one_line_proof (if mode = Minimize then Normal else mode) debug
  1092                              verbose timeout used_pairs state subgoal reconstr
  1093                              [reconstr] of
  1094       play as Played (_, time) =>
  1095       {outcome = NONE, used_facts = used_facts, run_time = time,
  1096        preplay = K play,
  1097        message =
  1098          fn play =>
  1099             let
  1100               val (_, override_params) = extract_reconstructor params reconstr
  1101               val one_line_params =
  1102                 (play, proof_banner mode name, used_facts,
  1103                  minimize_command override_params name, subgoal,
  1104                  subgoal_count)
  1105             in one_line_proof_text one_line_params end,
  1106        message_tail = ""}
  1107     | play =>
  1108       let
  1109         val failure = case play of Failed_to_Play _ => GaveUp | _ => TimedOut
  1110       in
  1111         {outcome = SOME failure, used_facts = [], run_time = Time.zeroTime,
  1112          preplay = K play, message = fn _ => string_for_failure failure,
  1113          message_tail = ""}
  1114       end
  1115   end
  1116 
  1117 fun get_prover ctxt mode name =
  1118   let val thy = Proof_Context.theory_of ctxt in
  1119     if is_reconstructor name then run_reconstructor mode name
  1120     else if is_atp thy name then run_atp mode name (get_atp thy name ())
  1121     else if is_smt_prover ctxt name then run_smt_solver mode name
  1122     else error ("No such prover: " ^ name ^ ".")
  1123   end
  1124 
  1125 end;