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