src/HOL/Tools/Sledgehammer/sledgehammer_prover.ML
changeset 55205 8450622db0c5
parent 55202 824c48a539c9
child 55207 42ad887a1c7c
     1.1 --- a/src/HOL/Tools/Sledgehammer/sledgehammer_prover.ML	Fri Jan 31 10:34:20 2014 +0100
     1.2 +++ b/src/HOL/Tools/Sledgehammer/sledgehammer_prover.ML	Fri Jan 31 12:30:54 2014 +0100
     1.3 @@ -69,49 +69,35 @@
     1.4    val problem_prefix : string Config.T
     1.5    val completish : bool Config.T
     1.6    val atp_full_names : bool Config.T
     1.7 -  val smt_builtins : bool Config.T
     1.8 -  val smt_triggers : bool Config.T
     1.9 -  val smt_weights : bool Config.T
    1.10 -  val smt_weight_min_facts : int Config.T
    1.11 -  val smt_min_weight : int Config.T
    1.12 -  val smt_max_weight : int Config.T
    1.13 -  val smt_max_weight_index : int Config.T
    1.14 -  val smt_weight_curve : (int -> int) Unsynchronized.ref
    1.15 -  val smt_max_slices : int Config.T
    1.16 -  val smt_slice_fact_frac : real Config.T
    1.17 -  val smt_slice_time_frac : real Config.T
    1.18 -  val smt_slice_min_secs : int Config.T
    1.19    val SledgehammerN : string
    1.20    val plain_metis : reconstructor
    1.21 -  val select_smt_solver : string -> Proof.context -> Proof.context
    1.22 +  val overlord_file_location_of_prover : string -> string * string
    1.23 +  val proof_banner : mode -> string -> string
    1.24    val extract_reconstructor : params -> reconstructor -> string * (string * string list) list
    1.25    val is_reconstructor : string -> bool
    1.26    val is_atp : theory -> string -> bool
    1.27 -  val is_smt_prover : Proof.context -> string -> bool
    1.28    val is_ho_atp: Proof.context -> string -> bool
    1.29    val is_unit_equational_atp : Proof.context -> string -> bool
    1.30 -  val is_prover_supported : Proof.context -> string -> bool
    1.31 -  val is_prover_installed : Proof.context -> string -> bool
    1.32 -  val remotify_prover_if_supported_and_not_already_remote :
    1.33 -    Proof.context -> string -> string option
    1.34 -  val remotify_prover_if_not_installed :
    1.35 -    Proof.context -> string -> string option
    1.36 -  val default_max_facts_of_prover : Proof.context -> string -> int
    1.37    val is_unit_equality : term -> bool
    1.38    val is_appropriate_prop_of_prover : Proof.context -> string -> term -> bool
    1.39 -  val weight_smt_fact :
    1.40 -    Proof.context -> int -> ((string * stature) * thm) * int
    1.41 -    -> (string * stature) * (int option * thm)
    1.42    val supported_provers : Proof.context -> unit
    1.43    val kill_provers : unit -> unit
    1.44    val running_provers : unit -> unit
    1.45    val messages : int option -> unit
    1.46    val is_fact_chained : (('a * stature) * 'b) -> bool
    1.47 +  val bunch_of_reconstructors : bool -> (bool -> string) -> reconstructor list
    1.48    val filter_used_facts :
    1.49      bool -> (''a * stature) list -> ((''a * stature) * 'b) list ->
    1.50      ((''a * stature) * 'b) list
    1.51 -  val isar_proof_reconstructor : Proof.context -> string -> string
    1.52 -  val get_prover : Proof.context -> mode -> string -> prover
    1.53 +  val play_one_line_proof : mode -> bool -> bool -> Time.time -> ((string * 'a) * thm) list ->
    1.54 +    Proof.state -> int -> reconstructor -> reconstructor list -> reconstructor * play_outcome
    1.55 +  val remotify_atp_if_not_installed : theory -> string -> string option
    1.56 +  val isar_supported_prover_of : theory -> string -> string
    1.57 +  val choose_minimize_command : theory -> params -> ((string * string list) list -> string -> 'a) ->
    1.58 +    string -> reconstructor * play_outcome -> 'a
    1.59 +  val repair_monomorph_context : int option -> int -> int option -> int -> Proof.context ->
    1.60 +    Proof.context
    1.61 +  val run_reconstructor : mode -> string -> prover
    1.62  end;
    1.63  
    1.64  structure Sledgehammer_Prover : SLEDGEHAMMER_PROVER =
    1.65 @@ -130,9 +116,6 @@
    1.66  open Sledgehammer_Isar_Print
    1.67  open Sledgehammer_Isar
    1.68  
    1.69 -
    1.70 -(** The Sledgehammer **)
    1.71 -
    1.72  (* Empty string means create files in Isabelle's temporary files directory. *)
    1.73  val dest_dir = Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
    1.74  val problem_prefix = Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
    1.75 @@ -143,12 +126,6 @@
    1.76     provers (e.g., E). For these reason, short names are enabled by default. *)
    1.77  val atp_full_names = Attrib.setup_config_bool @{binding sledgehammer_atp_full_names} (K false)
    1.78  
    1.79 -val smt_builtins = Attrib.setup_config_bool @{binding sledgehammer_smt_builtins} (K true)
    1.80 -val smt_triggers = Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
    1.81 -val smt_weights = Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
    1.82 -val smt_weight_min_facts =
    1.83 -  Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
    1.84 -
    1.85  datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
    1.86  
    1.87  (* Identifier that distinguishes Sledgehammer from other tools that could use
    1.88 @@ -161,61 +138,28 @@
    1.89  
    1.90  val is_atp = member (op =) o supported_atps
    1.91  
    1.92 -val select_smt_solver = Context.proof_map o SMT_Config.select_solver
    1.93 -
    1.94 -fun is_smt_prover ctxt = member (op =) (SMT_Solver.available_solvers_of ctxt)
    1.95 -
    1.96  fun is_atp_of_format is_format ctxt name =
    1.97    let val thy = Proof_Context.theory_of ctxt in
    1.98 -    case try (get_atp thy) name of
    1.99 +    (case try (get_atp thy) name of
   1.100        SOME config =>
   1.101 -      exists (fn (_, ((_, format, _, _, _), _)) => is_format format)
   1.102 -             (#best_slices (config ()) ctxt)
   1.103 -    | NONE => false
   1.104 +      exists (fn (_, ((_, format, _, _, _), _)) => is_format format) (#best_slices (config ()) ctxt)
   1.105 +    | NONE => false)
   1.106    end
   1.107  
   1.108  val is_unit_equational_atp = is_atp_of_format (curry (op =) CNF_UEQ)
   1.109  val is_ho_atp = is_atp_of_format is_format_higher_order
   1.110  
   1.111 -fun is_prover_supported ctxt =
   1.112 -  let val thy = Proof_Context.theory_of ctxt in
   1.113 -    is_reconstructor orf is_atp thy orf is_smt_prover ctxt
   1.114 -  end
   1.115 -
   1.116 -fun is_prover_installed ctxt =
   1.117 -  is_reconstructor orf is_smt_prover ctxt orf
   1.118 -  is_atp_installed (Proof_Context.theory_of ctxt)
   1.119 -
   1.120 -fun remotify_prover_if_supported_and_not_already_remote ctxt name =
   1.121 +fun remotify_atp_if_supported_and_not_already_remote thy name =
   1.122    if String.isPrefix remote_prefix name then
   1.123      SOME name
   1.124    else
   1.125      let val remote_name = remote_prefix ^ name in
   1.126 -      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
   1.127 +      if is_atp thy remote_name then SOME remote_name else NONE
   1.128      end
   1.129  
   1.130 -fun remotify_prover_if_not_installed ctxt name =
   1.131 -  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
   1.132 -    SOME name
   1.133 -  else
   1.134 -    remotify_prover_if_supported_and_not_already_remote ctxt name
   1.135 -
   1.136 -fun get_slices slice slices =
   1.137 -  (0 upto length slices - 1) ~~ slices |> not slice ? (List.last #> single)
   1.138 -
   1.139 -val reconstructor_default_max_facts = 20
   1.140 -
   1.141 -fun slice_max_facts (_, ( ((max_facts, _), _, _, _, _), _)) = max_facts
   1.142 -
   1.143 -fun default_max_facts_of_prover ctxt name =
   1.144 -  let val thy = Proof_Context.theory_of ctxt in
   1.145 -    if is_reconstructor name then
   1.146 -      reconstructor_default_max_facts
   1.147 -    else if is_atp thy name then
   1.148 -      fold (Integer.max o slice_max_facts) (#best_slices (get_atp thy name ()) ctxt) 0
   1.149 -    else (* is_smt_prover ctxt name *)
   1.150 -      SMT_Solver.default_max_relevant ctxt name
   1.151 -  end
   1.152 +fun remotify_atp_if_not_installed thy name =
   1.153 +  if is_atp_installed thy name then SOME name
   1.154 +  else remotify_atp_if_supported_and_not_already_remote thy name
   1.155  
   1.156  fun is_if (@{const_name If}, _) = true
   1.157    | is_if _ = false
   1.158 @@ -256,9 +200,6 @@
   1.159  fun running_provers () = Async_Manager.running_threads SledgehammerN "prover"
   1.160  val messages = Async_Manager.thread_messages SledgehammerN "prover"
   1.161  
   1.162 -
   1.163 -(** problems, results, ATPs, etc. **)
   1.164 -
   1.165  type params =
   1.166    {debug : bool,
   1.167     verbose : bool,
   1.168 @@ -306,43 +247,13 @@
   1.169    params -> ((string * string list) list -> string -> minimize_command)
   1.170    -> prover_problem -> prover_result
   1.171  
   1.172 -(* FUDGE *)
   1.173 -val smt_min_weight =
   1.174 -  Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
   1.175 -val smt_max_weight =
   1.176 -  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
   1.177 -val smt_max_weight_index =
   1.178 -  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
   1.179 -val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
   1.180 -
   1.181 -fun smt_fact_weight ctxt j num_facts =
   1.182 -  if Config.get ctxt smt_weights andalso
   1.183 -     num_facts >= Config.get ctxt smt_weight_min_facts then
   1.184 -    let
   1.185 -      val min = Config.get ctxt smt_min_weight
   1.186 -      val max = Config.get ctxt smt_max_weight
   1.187 -      val max_index = Config.get ctxt smt_max_weight_index
   1.188 -      val curve = !smt_weight_curve
   1.189 -    in
   1.190 -      SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
   1.191 -            div curve max_index)
   1.192 -    end
   1.193 -  else
   1.194 -    NONE
   1.195 -
   1.196 -fun weight_smt_fact ctxt num_facts ((info, th), j) =
   1.197 -  let val thy = Proof_Context.theory_of ctxt in
   1.198 -    (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
   1.199 -  end
   1.200 -
   1.201 -fun overlord_file_location_of_prover prover =
   1.202 -  (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
   1.203 +fun overlord_file_location_of_prover prover = (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
   1.204  
   1.205  fun proof_banner mode name =
   1.206 -  case mode of
   1.207 +  (case mode of
   1.208      Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
   1.209    | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
   1.210 -  | _ => "Try this"
   1.211 +  | _ => "Try this")
   1.212  
   1.213  fun bunch_of_reconstructors needs_full_types lam_trans =
   1.214    if needs_full_types then
   1.215 @@ -423,91 +334,17 @@
   1.216        end
   1.217    end
   1.218  
   1.219 -
   1.220 -(* generic TPTP-based ATPs *)
   1.221 -
   1.222 -(* Too general means, positive equality literal with a variable X as one
   1.223 -   operand, when X does not occur properly in the other operand. This rules out
   1.224 -   clearly inconsistent facts such as X = a | X = b, though it by no means
   1.225 -   guarantees soundness. *)
   1.226 -
   1.227 -fun get_facts_of_filter _ [(_, facts)] = facts
   1.228 -  | get_facts_of_filter fact_filter factss =
   1.229 -    case AList.lookup (op =) factss fact_filter of
   1.230 -      SOME facts => facts
   1.231 -    | NONE => snd (hd factss)
   1.232 +val canonical_isar_supported_prover = eN
   1.233  
   1.234 -(* Unwanted equalities are those between a (bound or schematic) variable that
   1.235 -   does not properly occur in the second operand. *)
   1.236 -val is_exhaustive_finite =
   1.237 -  let
   1.238 -    fun is_bad_equal (Var z) t =
   1.239 -        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
   1.240 -      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
   1.241 -      | is_bad_equal _ _ = false
   1.242 -    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
   1.243 -    fun do_formula pos t =
   1.244 -      case (pos, t) of
   1.245 -        (_, @{const Trueprop} $ t1) => do_formula pos t1
   1.246 -      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
   1.247 -        do_formula pos t'
   1.248 -      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
   1.249 -        do_formula pos t'
   1.250 -      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
   1.251 -        do_formula pos t'
   1.252 -      | (_, @{const "==>"} $ t1 $ t2) =>
   1.253 -        do_formula (not pos) t1 andalso
   1.254 -        (t2 = @{prop False} orelse do_formula pos t2)
   1.255 -      | (_, @{const HOL.implies} $ t1 $ t2) =>
   1.256 -        do_formula (not pos) t1 andalso
   1.257 -        (t2 = @{const False} orelse do_formula pos t2)
   1.258 -      | (_, @{const Not} $ t1) => do_formula (not pos) t1
   1.259 -      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
   1.260 -      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
   1.261 -      | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
   1.262 -      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
   1.263 -      | _ => false
   1.264 -  in do_formula true end
   1.265 -
   1.266 -fun has_bound_or_var_of_type pred =
   1.267 -  exists_subterm (fn Var (_, T as Type _) => pred T
   1.268 -                   | Abs (_, T as Type _, _) => pred T
   1.269 -                   | _ => false)
   1.270 -
   1.271 -(* Facts are forbidden to contain variables of these types. The typical reason
   1.272 -   is that they lead to unsoundness. Note that "unit" satisfies numerous
   1.273 -   equations like "?x = ()". The resulting clauses will have no type constraint,
   1.274 -   yielding false proofs. Even "bool" leads to many unsound proofs, though only
   1.275 -   for higher-order problems. *)
   1.276 -
   1.277 -(* Facts containing variables of type "unit" or "bool" or of the form
   1.278 -   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
   1.279 -   are omitted. *)
   1.280 -fun is_dangerous_prop ctxt =
   1.281 -  transform_elim_prop
   1.282 -  #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
   1.283 -      is_exhaustive_finite)
   1.284 -
   1.285 -(* Important messages are important but not so important that users want to see
   1.286 -   them each time. *)
   1.287 -val atp_important_message_keep_quotient = 25
   1.288 -
   1.289 -fun choose_type_enc strictness best_type_enc format =
   1.290 -  the_default best_type_enc
   1.291 -  #> type_enc_of_string strictness
   1.292 -  #> adjust_type_enc format
   1.293 -
   1.294 -fun isar_proof_reconstructor ctxt name =
   1.295 -  let val thy = Proof_Context.theory_of ctxt in
   1.296 -    if is_atp thy name then name
   1.297 -    else remotify_prover_if_not_installed ctxt eN |> the_default name
   1.298 -  end
   1.299 +fun isar_supported_prover_of thy name =
   1.300 +  if is_atp thy name then name
   1.301 +  else the_default name (remotify_atp_if_not_installed thy canonical_isar_supported_prover)
   1.302  
   1.303  (* FIXME: See the analogous logic in the function "maybe_minimize" in
   1.304     "sledgehammer_prover_minimize.ML". *)
   1.305 -fun choose_minimize_command ctxt (params as {isar_proofs, ...}) minimize_command name preplay =
   1.306 +fun choose_minimize_command thy (params as {isar_proofs, ...}) minimize_command name preplay =
   1.307    let
   1.308 -    val maybe_isar_name = name |> isar_proofs = SOME true ? isar_proof_reconstructor ctxt
   1.309 +    val maybe_isar_name = name |> isar_proofs = SOME true ? isar_supported_prover_of thy
   1.310      val (min_name, override_params) =
   1.311        (case preplay of
   1.312          (reconstr, Played _) =>
   1.313 @@ -518,502 +355,15 @@
   1.314  
   1.315  val max_fact_instances = 10 (* FUDGE *)
   1.316  
   1.317 -fun repair_monomorph_context max_iters best_max_iters max_new_instances
   1.318 -                             best_max_new_instances =
   1.319 +fun repair_monomorph_context max_iters best_max_iters max_new_instances best_max_new_instances =
   1.320    Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
   1.321    #> Config.put Monomorph.max_new_instances
   1.322 -         (max_new_instances |> the_default best_max_new_instances)
   1.323 +       (max_new_instances |> the_default best_max_new_instances)
   1.324    #> Config.put Monomorph.max_thm_instances max_fact_instances
   1.325  
   1.326 -fun suffix_of_mode Auto_Try = "_try"
   1.327 -  | suffix_of_mode Try = "_try"
   1.328 -  | suffix_of_mode Normal = ""
   1.329 -  | suffix_of_mode MaSh = ""
   1.330 -  | suffix_of_mode Auto_Minimize = "_min"
   1.331 -  | suffix_of_mode Minimize = "_min"
   1.332 -
   1.333 -(* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on
   1.334 -   Linux appears to be the only ATP that does not honor its time limit. *)
   1.335 -val atp_timeout_slack = seconds 1.0
   1.336 -
   1.337 -val mono_max_privileged_facts = 10
   1.338 -
   1.339 -(* For low values of "max_facts", this fudge value ensures that most slices are
   1.340 -   invoked with a nontrivial amount of facts. *)
   1.341 -val max_fact_factor_fudge = 5
   1.342 -
   1.343 -fun run_atp mode name
   1.344 -    ({exec, arguments, proof_delims, known_failures, prem_role, best_slices, best_max_mono_iters,
   1.345 -      best_max_new_mono_instances, ...} : atp_config)
   1.346 -    (params as {debug, verbose, overlord, type_enc, strict, lam_trans, uncurried_aliases,
   1.347 -       fact_filter, max_facts, max_mono_iters, max_new_mono_instances, isar_proofs, compress_isar,
   1.348 -       try0_isar, slice, timeout, preplay_timeout, ...})
   1.349 +fun run_reconstructor mode name (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
   1.350      minimize_command
   1.351 -    ({comment, state, goal, subgoal, subgoal_count, factss, ...} : prover_problem) =
   1.352 -  let
   1.353 -    val thy = Proof.theory_of state
   1.354 -    val ctxt = Proof.context_of state
   1.355 -    val atp_mode =
   1.356 -      if Config.get ctxt completish then Sledgehammer_Completish
   1.357 -      else Sledgehammer
   1.358 -    val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal ctxt
   1.359 -    val (dest_dir, problem_prefix) =
   1.360 -      if overlord then overlord_file_location_of_prover name
   1.361 -      else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
   1.362 -    val problem_file_name =
   1.363 -      Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
   1.364 -                  suffix_of_mode mode ^ "_" ^ string_of_int subgoal)
   1.365 -    val prob_path =
   1.366 -      if dest_dir = "" then
   1.367 -        File.tmp_path problem_file_name
   1.368 -      else if File.exists (Path.explode dest_dir) then
   1.369 -        Path.append (Path.explode dest_dir) problem_file_name
   1.370 -      else
   1.371 -        error ("No such directory: " ^ quote dest_dir ^ ".")
   1.372 -    val exec = exec ()
   1.373 -    val command0 =
   1.374 -      case find_first (fn var => getenv var <> "") (fst exec) of
   1.375 -        SOME var =>
   1.376 -        let
   1.377 -          val pref = getenv var ^ "/"
   1.378 -          val paths = map (Path.explode o prefix pref) (snd exec)
   1.379 -        in
   1.380 -          case find_first File.exists paths of
   1.381 -            SOME path => path
   1.382 -          | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".")
   1.383 -        end
   1.384 -      | NONE => error ("The environment variable " ^ quote (List.last (fst exec)) ^
   1.385 -                       " is not set.")
   1.386 -    fun split_time s =
   1.387 -      let
   1.388 -        val split = String.tokens (fn c => str c = "\n")
   1.389 -        val (output, t) =
   1.390 -          s |> split |> (try split_last #> the_default ([], "0"))
   1.391 -            |>> cat_lines
   1.392 -        fun as_num f = f >> (fst o read_int)
   1.393 -        val num = as_num (Scan.many1 Symbol.is_ascii_digit)
   1.394 -        val digit = Scan.one Symbol.is_ascii_digit
   1.395 -        val num3 = as_num (digit ::: digit ::: (digit >> single))
   1.396 -        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
   1.397 -        val as_time =
   1.398 -          raw_explode #> Scan.read Symbol.stopper time #> the_default 0
   1.399 -      in (output, as_time t |> Time.fromMilliseconds) end
   1.400 -    fun run () =
   1.401 -      let
   1.402 -        (* If slicing is disabled, we expand the last slice to fill the entire
   1.403 -           time available. *)
   1.404 -        val all_slices = best_slices ctxt
   1.405 -        val actual_slices = get_slices slice all_slices
   1.406 -        fun max_facts_of_slices f slices = fold (Integer.max o slice_max_facts o f) slices 0
   1.407 -        val num_actual_slices = length actual_slices
   1.408 -        val max_fact_factor =
   1.409 -          Real.fromInt (case max_facts of
   1.410 -              NONE => max_facts_of_slices I all_slices
   1.411 -            | SOME max => max)
   1.412 -          / Real.fromInt (max_facts_of_slices snd actual_slices)
   1.413 -        fun monomorphize_facts facts =
   1.414 -          let
   1.415 -            val ctxt =
   1.416 -              ctxt
   1.417 -              |> repair_monomorph_context max_mono_iters
   1.418 -                     best_max_mono_iters max_new_mono_instances
   1.419 -                     best_max_new_mono_instances
   1.420 -            (* pseudo-theorem involving the same constants as the subgoal *)
   1.421 -            val subgoal_th =
   1.422 -              Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy
   1.423 -            val rths =
   1.424 -              facts |> chop mono_max_privileged_facts
   1.425 -                    |>> map (pair 1 o snd)
   1.426 -                    ||> map (pair 2 o snd)
   1.427 -                    |> op @
   1.428 -                    |> cons (0, subgoal_th)
   1.429 -          in
   1.430 -            Monomorph.monomorph atp_schematic_consts_of ctxt rths
   1.431 -            |> tl |> curry ListPair.zip (map fst facts)
   1.432 -            |> maps (fn (name, rths) =>
   1.433 -                        map (pair name o zero_var_indexes o snd) rths)
   1.434 -          end
   1.435 -        fun run_slice time_left (cache_key, cache_value)
   1.436 -                (slice, (time_frac,
   1.437 -                     (key as ((best_max_facts, best_fact_filter), format,
   1.438 -                              best_type_enc, best_lam_trans,
   1.439 -                              best_uncurried_aliases),
   1.440 -                      extra))) =
   1.441 -          let
   1.442 -            val effective_fact_filter =
   1.443 -              fact_filter |> the_default best_fact_filter
   1.444 -            val facts = get_facts_of_filter effective_fact_filter factss
   1.445 -            val num_facts =
   1.446 -              Real.ceil (max_fact_factor * Real.fromInt best_max_facts) +
   1.447 -              max_fact_factor_fudge
   1.448 -              |> Integer.min (length facts)
   1.449 -            val strictness = if strict then Strict else Non_Strict
   1.450 -            val type_enc =
   1.451 -              type_enc |> choose_type_enc strictness best_type_enc format
   1.452 -            val sound = is_type_enc_sound type_enc
   1.453 -            val real_ms = Real.fromInt o Time.toMilliseconds
   1.454 -            val slice_timeout =
   1.455 -              (real_ms time_left
   1.456 -               |> (if slice < num_actual_slices - 1 then
   1.457 -                     curry Real.min (time_frac * real_ms timeout)
   1.458 -                   else
   1.459 -                     I))
   1.460 -              * 0.001
   1.461 -              |> seconds
   1.462 -            val generous_slice_timeout =
   1.463 -              if mode = MaSh then one_day else Time.+ (slice_timeout, atp_timeout_slack)
   1.464 -            val _ =
   1.465 -              if debug then
   1.466 -                quote name ^ " slice #" ^ string_of_int (slice + 1) ^
   1.467 -                " with " ^ string_of_int num_facts ^ " fact" ^
   1.468 -                plural_s num_facts ^ " for " ^ string_of_time slice_timeout ^ "..."
   1.469 -                |> Output.urgent_message
   1.470 -              else
   1.471 -                ()
   1.472 -            val readable_names = not (Config.get ctxt atp_full_names)
   1.473 -            val lam_trans =
   1.474 -              case lam_trans of
   1.475 -                SOME s => s
   1.476 -              | NONE => best_lam_trans
   1.477 -            val uncurried_aliases =
   1.478 -              case uncurried_aliases of
   1.479 -                SOME b => b
   1.480 -              | NONE => best_uncurried_aliases
   1.481 -            val value as (atp_problem, _, fact_names, _, _) =
   1.482 -              if cache_key = SOME key then
   1.483 -                cache_value
   1.484 -              else
   1.485 -                facts
   1.486 -                |> not sound
   1.487 -                   ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
   1.488 -                |> take num_facts
   1.489 -                |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts
   1.490 -                |> map (apsnd prop_of)
   1.491 -                |> prepare_atp_problem ctxt format prem_role type_enc atp_mode
   1.492 -                                       lam_trans uncurried_aliases
   1.493 -                                       readable_names true hyp_ts concl_t
   1.494 -            fun sel_weights () = atp_problem_selection_weights atp_problem
   1.495 -            fun ord_info () = atp_problem_term_order_info atp_problem
   1.496 -            val ord = effective_term_order ctxt name
   1.497 -            val full_proof = isar_proofs |> the_default (mode = Minimize)
   1.498 -            val args =
   1.499 -              arguments ctxt full_proof extra slice_timeout (File.shell_path prob_path)
   1.500 -                (ord, ord_info, sel_weights)
   1.501 -            val command =
   1.502 -              "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^ ")"
   1.503 -              |> enclose "TIMEFORMAT='%3R'; { time " " ; }"
   1.504 -            val _ =
   1.505 -              atp_problem
   1.506 -              |> lines_of_atp_problem format ord ord_info
   1.507 -              |> cons ("% " ^ command ^ "\n" ^ (if comment = "" then "" else "% " ^ comment ^ "\n"))
   1.508 -              |> File.write_list prob_path
   1.509 -            val ((output, run_time), (atp_proof, outcome)) =
   1.510 -              TimeLimit.timeLimit generous_slice_timeout Isabelle_System.bash_output command
   1.511 -              |>> (if overlord then prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n") else I)
   1.512 -              |> fst |> split_time
   1.513 -              |> (fn accum as (output, _) =>
   1.514 -                     (accum,
   1.515 -                      extract_tstplike_proof_and_outcome verbose proof_delims known_failures output
   1.516 -                      |>> atp_proof_of_tstplike_proof atp_problem
   1.517 -                      handle UNRECOGNIZED_ATP_PROOF () => ([], SOME ProofIncomplete)))
   1.518 -              handle TimeLimit.TimeOut => (("", slice_timeout), ([], SOME TimedOut))
   1.519 -            val outcome =
   1.520 -              (case outcome of
   1.521 -                NONE =>
   1.522 -                (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof
   1.523 -                      |> Option.map (sort string_ord) of
   1.524 -                   SOME facts =>
   1.525 -                   let val failure = UnsoundProof (is_type_enc_sound type_enc, facts) in
   1.526 -                     if debug then (warning (string_of_atp_failure failure); NONE) else SOME failure
   1.527 -                   end
   1.528 -                 | NONE => NONE)
   1.529 -              | _ => outcome)
   1.530 -          in
   1.531 -            ((SOME key, value), (output, run_time, facts, atp_proof, outcome))
   1.532 -          end
   1.533 -        val timer = Timer.startRealTimer ()
   1.534 -        fun maybe_run_slice slice
   1.535 -                (result as (cache, (_, run_time0, _, _, SOME _))) =
   1.536 -            let
   1.537 -              val time_left = Time.- (timeout, Timer.checkRealTimer timer)
   1.538 -            in
   1.539 -              if Time.<= (time_left, Time.zeroTime) then
   1.540 -                result
   1.541 -              else
   1.542 -                run_slice time_left cache slice
   1.543 -                |> (fn (cache, (output, run_time, used_from, atp_proof, outcome)) =>
   1.544 -                  (cache, (output, Time.+ (run_time0, run_time), used_from, atp_proof, outcome)))
   1.545 -            end
   1.546 -          | maybe_run_slice _ result = result
   1.547 -      in
   1.548 -        ((NONE, ([], Symtab.empty, Vector.fromList [], [], Symtab.empty)),
   1.549 -         ("", Time.zeroTime, [], [], SOME InternalError))
   1.550 -        |> fold maybe_run_slice actual_slices
   1.551 -      end
   1.552 -    (* If the problem file has not been exported, remove it; otherwise, export
   1.553 -       the proof file too. *)
   1.554 -    fun clean_up () =
   1.555 -      if dest_dir = "" then (try File.rm prob_path; ()) else ()
   1.556 -    fun export (_, (output, _, _, _, _)) =
   1.557 -      if dest_dir = "" then ()
   1.558 -      else File.write (Path.explode (Path.implode prob_path ^ "_proof")) output
   1.559 -    val ((_, (_, pool, fact_names, lifted, sym_tab)),
   1.560 -         (output, run_time, used_from, atp_proof, outcome)) =
   1.561 -      with_cleanup clean_up run () |> tap export
   1.562 -    val important_message =
   1.563 -      if mode = Normal andalso random_range 0 (atp_important_message_keep_quotient - 1) = 0
   1.564 -      then
   1.565 -        extract_important_message output
   1.566 -      else
   1.567 -        ""
   1.568 -    val (used_facts, preplay, message, message_tail) =
   1.569 -      (case outcome of
   1.570 -        NONE =>
   1.571 -        let
   1.572 -          val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof
   1.573 -          val needs_full_types = is_typed_helper_used_in_atp_proof atp_proof
   1.574 -          val reconstrs =
   1.575 -            bunch_of_reconstructors needs_full_types (lam_trans_of_atp_proof atp_proof
   1.576 -              o (fn desperate => if desperate then hide_lamsN else default_metis_lam_trans))
   1.577 -        in
   1.578 -          (used_facts,
   1.579 -           Lazy.lazy (fn () =>
   1.580 -             let val used_pairs = used_from |> filter_used_facts false used_facts in
   1.581 -               play_one_line_proof mode debug verbose preplay_timeout used_pairs state subgoal
   1.582 -                 (hd reconstrs) reconstrs
   1.583 -             end),
   1.584 -           fn preplay =>
   1.585 -              let
   1.586 -                val _ = if verbose then Output.urgent_message "Generating proof text..." else ()
   1.587 -                fun isar_params () =
   1.588 -                  let
   1.589 -                    val metis_type_enc =
   1.590 -                      if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
   1.591 -                      else partial_typesN
   1.592 -                    val metis_lam_trans = lam_trans_of_atp_proof atp_proof default_metis_lam_trans
   1.593 -                    val atp_proof =
   1.594 -                      atp_proof
   1.595 -                      |> termify_atp_proof ctxt pool lifted sym_tab
   1.596 -                      |> introduce_spass_skolem
   1.597 -                      |> factify_atp_proof fact_names hyp_ts concl_t
   1.598 -                  in
   1.599 -                    (verbose, metis_type_enc, metis_lam_trans, preplay_timeout, compress_isar,
   1.600 -                     try0_isar, atp_proof, goal)
   1.601 -                  end
   1.602 -                val one_line_params =
   1.603 -                  (preplay, proof_banner mode name, used_facts,
   1.604 -                   choose_minimize_command ctxt params minimize_command name preplay,
   1.605 -                   subgoal, subgoal_count)
   1.606 -                val num_chained = length (#facts (Proof.goal state))
   1.607 -              in
   1.608 -                proof_text ctxt debug isar_proofs isar_params num_chained one_line_params
   1.609 -              end,
   1.610 -           (if verbose then "\nATP real CPU time: " ^ string_of_time run_time ^ "." else "") ^
   1.611 -           (if important_message <> "" then
   1.612 -              "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^ important_message
   1.613 -            else
   1.614 -              ""))
   1.615 -        end
   1.616 -      | SOME failure =>
   1.617 -        ([], Lazy.value (plain_metis, Play_Failed), fn _ => string_of_atp_failure failure, ""))
   1.618 -  in
   1.619 -    {outcome = outcome, used_facts = used_facts, used_from = used_from, run_time = run_time,
   1.620 -     preplay = preplay, message = message, message_tail = message_tail}
   1.621 -  end
   1.622 -
   1.623 -(* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
   1.624 -   these are sorted out properly in the SMT module, we have to interpret these
   1.625 -   ourselves. *)
   1.626 -val remote_smt_failures =
   1.627 -  [(2, NoLibwwwPerl),
   1.628 -   (22, CantConnect)]
   1.629 -val z3_failures =
   1.630 -  [(101, OutOfResources),
   1.631 -   (103, MalformedInput),
   1.632 -   (110, MalformedInput),
   1.633 -   (112, TimedOut)]
   1.634 -val unix_failures =
   1.635 -  [(138, Crashed),
   1.636 -   (139, Crashed)]
   1.637 -val smt_failures = remote_smt_failures @ z3_failures @ unix_failures
   1.638 -
   1.639 -fun failure_of_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
   1.640 -    if is_real_cex then Unprovable else GaveUp
   1.641 -  | failure_of_smt_failure SMT_Failure.Time_Out = TimedOut
   1.642 -  | failure_of_smt_failure (SMT_Failure.Abnormal_Termination code) =
   1.643 -    (case AList.lookup (op =) smt_failures code of
   1.644 -       SOME failure => failure
   1.645 -     | NONE => UnknownError ("Abnormal termination with exit code " ^
   1.646 -                             string_of_int code ^ "."))
   1.647 -  | failure_of_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
   1.648 -  | failure_of_smt_failure (SMT_Failure.Other_Failure s) = UnknownError s
   1.649 -
   1.650 -(* FUDGE *)
   1.651 -val smt_max_slices =
   1.652 -  Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
   1.653 -val smt_slice_fact_frac =
   1.654 -  Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac}
   1.655 -                           (K 0.667)
   1.656 -val smt_slice_time_frac =
   1.657 -  Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.333)
   1.658 -val smt_slice_min_secs =
   1.659 -  Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 3)
   1.660 -
   1.661 -val is_boring_builtin_typ =
   1.662 -  not o exists_subtype (member (op =) [@{typ nat}, @{typ int}, HOLogic.realT])
   1.663 -
   1.664 -fun smt_filter_loop name ({debug, overlord, max_mono_iters, max_new_mono_instances, timeout, slice,
   1.665 -      ...} : params) state goal i =
   1.666 -  let
   1.667 -    fun repair_context ctxt =
   1.668 -      ctxt |> select_smt_solver name
   1.669 -           |> Config.put SMT_Config.verbose debug
   1.670 -           |> (if overlord then
   1.671 -                 Config.put SMT_Config.debug_files
   1.672 -                            (overlord_file_location_of_prover name
   1.673 -                             |> (fn (path, name) => path ^ "/" ^ name))
   1.674 -               else
   1.675 -                 I)
   1.676 -           |> Config.put SMT_Config.infer_triggers
   1.677 -                         (Config.get ctxt smt_triggers)
   1.678 -           |> not (Config.get ctxt smt_builtins)
   1.679 -              ? (SMT_Builtin.filter_builtins is_boring_builtin_typ
   1.680 -                 #> Config.put SMT_Config.datatypes false)
   1.681 -           |> repair_monomorph_context max_mono_iters default_max_mono_iters
   1.682 -                  max_new_mono_instances default_max_new_mono_instances
   1.683 -    val state = Proof.map_context (repair_context) state
   1.684 -    val ctxt = Proof.context_of state
   1.685 -    val max_slices = if slice then Config.get ctxt smt_max_slices else 1
   1.686 -    fun do_slice timeout slice outcome0 time_so_far
   1.687 -                 (weighted_factss as (fact_filter, weighted_facts) :: _) =
   1.688 -      let
   1.689 -        val timer = Timer.startRealTimer ()
   1.690 -        val slice_timeout =
   1.691 -          if slice < max_slices then
   1.692 -            let val ms = Time.toMilliseconds timeout in
   1.693 -              Int.min (ms,
   1.694 -                  Int.max (1000 * Config.get ctxt smt_slice_min_secs,
   1.695 -                      Real.ceil (Config.get ctxt smt_slice_time_frac
   1.696 -                                 * Real.fromInt ms)))
   1.697 -              |> Time.fromMilliseconds
   1.698 -            end
   1.699 -          else
   1.700 -            timeout
   1.701 -        val num_facts = length weighted_facts
   1.702 -        val _ =
   1.703 -          if debug then
   1.704 -            quote name ^ " slice " ^ string_of_int slice ^ " with " ^ string_of_int num_facts ^
   1.705 -            " fact" ^ plural_s num_facts ^ " for " ^ string_of_time slice_timeout
   1.706 -            |> Output.urgent_message
   1.707 -          else
   1.708 -            ()
   1.709 -        val birth = Timer.checkRealTimer timer
   1.710 -        val _ =
   1.711 -          if debug then Output.urgent_message "Invoking SMT solver..." else ()
   1.712 -        val (outcome, used_facts) =
   1.713 -          SMT_Solver.smt_filter_preprocess ctxt [] goal weighted_facts i
   1.714 -          |> SMT_Solver.smt_filter_apply slice_timeout
   1.715 -          |> (fn {outcome, used_facts} => (outcome, used_facts))
   1.716 -          handle exn => if Exn.is_interrupt exn then
   1.717 -                          reraise exn
   1.718 -                        else
   1.719 -                          (ML_Compiler.exn_message exn
   1.720 -                           |> SMT_Failure.Other_Failure |> SOME, [])
   1.721 -        val death = Timer.checkRealTimer timer
   1.722 -        val outcome0 = if is_none outcome0 then SOME outcome else outcome0
   1.723 -        val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
   1.724 -        val too_many_facts_perhaps =
   1.725 -          case outcome of
   1.726 -            NONE => false
   1.727 -          | SOME (SMT_Failure.Counterexample _) => false
   1.728 -          | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
   1.729 -          | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
   1.730 -          | SOME SMT_Failure.Out_Of_Memory => true
   1.731 -          | SOME (SMT_Failure.Other_Failure _) => true
   1.732 -        val timeout = Time.- (timeout, Timer.checkRealTimer timer)
   1.733 -      in
   1.734 -        if too_many_facts_perhaps andalso slice < max_slices andalso num_facts > 0 andalso
   1.735 -           Time.> (timeout, Time.zeroTime) then
   1.736 -          let
   1.737 -            val new_num_facts =
   1.738 -              Real.ceil (Config.get ctxt smt_slice_fact_frac * Real.fromInt num_facts)
   1.739 -            val weighted_factss as (new_fact_filter, _) :: _ =
   1.740 -              weighted_factss
   1.741 -              |> (fn (x :: xs) => xs @ [x])
   1.742 -              |> app_hd (apsnd (take new_num_facts))
   1.743 -            val show_filter = fact_filter <> new_fact_filter
   1.744 -            fun num_of_facts fact_filter num_facts =
   1.745 -              string_of_int num_facts ^
   1.746 -              (if show_filter then " " ^ quote fact_filter else "") ^
   1.747 -              " fact" ^ plural_s num_facts
   1.748 -            val _ =
   1.749 -              if debug then
   1.750 -                quote name ^ " invoked with " ^
   1.751 -                num_of_facts fact_filter num_facts ^ ": " ^
   1.752 -                string_of_atp_failure (failure_of_smt_failure (the outcome)) ^
   1.753 -                " Retrying with " ^ num_of_facts new_fact_filter new_num_facts ^
   1.754 -                "..."
   1.755 -                |> Output.urgent_message
   1.756 -              else
   1.757 -                ()
   1.758 -          in
   1.759 -            do_slice timeout (slice + 1) outcome0 time_so_far weighted_factss
   1.760 -          end
   1.761 -        else
   1.762 -          {outcome = if is_none outcome then NONE else the outcome0, used_facts = used_facts,
   1.763 -           used_from = map (apsnd snd) weighted_facts, run_time = time_so_far}
   1.764 -      end
   1.765 -  in
   1.766 -    do_slice timeout 1 NONE Time.zeroTime
   1.767 -  end
   1.768 -
   1.769 -fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...}) minimize_command
   1.770 -    ({state, goal, subgoal, subgoal_count, factss, ...} : prover_problem) =
   1.771 -  let
   1.772 -    val ctxt = Proof.context_of state
   1.773 -    fun weight_facts facts =
   1.774 -      let val num_facts = length facts in
   1.775 -        facts ~~ (0 upto num_facts - 1)
   1.776 -        |> map (weight_smt_fact ctxt num_facts)
   1.777 -      end
   1.778 -    val weighted_factss = factss |> map (apsnd weight_facts)
   1.779 -    val {outcome, used_facts = used_pairs, used_from, run_time} =
   1.780 -      smt_filter_loop name params state goal subgoal weighted_factss
   1.781 -    val used_facts = used_pairs |> map fst
   1.782 -    val outcome = outcome |> Option.map failure_of_smt_failure
   1.783 -    val (preplay, message, message_tail) =
   1.784 -      case outcome of
   1.785 -        NONE =>
   1.786 -        (Lazy.lazy (fn () =>
   1.787 -           play_one_line_proof mode debug verbose preplay_timeout used_pairs
   1.788 -               state subgoal SMT
   1.789 -               (bunch_of_reconstructors false (fn desperate =>
   1.790 -                  if desperate then liftingN else default_metis_lam_trans))),
   1.791 -         fn preplay =>
   1.792 -            let
   1.793 -              val one_line_params =
   1.794 -                (preplay, proof_banner mode name, used_facts,
   1.795 -                 choose_minimize_command ctxt params minimize_command name preplay,
   1.796 -                 subgoal, subgoal_count)
   1.797 -              val num_chained = length (#facts (Proof.goal state))
   1.798 -            in
   1.799 -              one_line_proof_text num_chained one_line_params
   1.800 -            end,
   1.801 -         if verbose then "\nSMT solver real CPU time: " ^ string_of_time run_time ^ "." else "")
   1.802 -      | SOME failure =>
   1.803 -        (Lazy.value (plain_metis, Play_Failed),
   1.804 -         fn _ => string_of_atp_failure failure, "")
   1.805 -  in
   1.806 -    {outcome = outcome, used_facts = used_facts, used_from = used_from,
   1.807 -     run_time = run_time, preplay = preplay, message = message,
   1.808 -     message_tail = message_tail}
   1.809 -  end
   1.810 -
   1.811 -fun run_reconstructor mode name
   1.812 -        (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
   1.813 -        minimize_command
   1.814 -        ({state, subgoal, subgoal_count, factss = (_, facts) :: _, ...}
   1.815 -         : prover_problem) =
   1.816 +    ({state, subgoal, subgoal_count, factss = (_, facts) :: _, ...} : prover_problem) =
   1.817    let
   1.818      val reconstr =
   1.819        if name = metisN then
   1.820 @@ -1052,12 +402,4 @@
   1.821        end)
   1.822    end
   1.823  
   1.824 -fun get_prover ctxt mode name =
   1.825 -  let val thy = Proof_Context.theory_of ctxt in
   1.826 -    if is_reconstructor name then run_reconstructor mode name
   1.827 -    else if is_atp thy name then run_atp mode name (get_atp thy name ())
   1.828 -    else if is_smt_prover ctxt name then run_smt_solver mode name
   1.829 -    else error ("No such prover: " ^ name ^ ".")
   1.830 -  end
   1.831 -
   1.832  end;