src/HOL/Tools/Sledgehammer/sledgehammer.ML
author blanchet
Fri Oct 22 11:58:33 2010 +0200 (2010-10-22 ago)
changeset 40063 d086e3699e78
parent 40062 cfaebaa8588f
child 40064 db8413d82c3b
permissions -rw-r--r--
bring ATPs and SMT solvers more in line with each other
blanchet@38021
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer.ML
wenzelm@28477
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@32996
     3
    Author:     Makarius
blanchet@35969
     4
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28477
     5
blanchet@38021
     6
Sledgehammer's heart.
wenzelm@28477
     7
*)
wenzelm@28477
     8
blanchet@38021
     9
signature SLEDGEHAMMER =
wenzelm@28477
    10
sig
blanchet@38023
    11
  type failure = ATP_Systems.failure
blanchet@38988
    12
  type locality = Sledgehammer_Filter.locality
blanchet@38988
    13
  type relevance_override = Sledgehammer_Filter.relevance_override
blanchet@39004
    14
  type fol_formula = Sledgehammer_Translate.fol_formula
blanchet@38988
    15
  type minimize_command = Sledgehammer_Reconstruct.minimize_command
blanchet@39493
    16
blanchet@35969
    17
  type params =
blanchet@38982
    18
    {blocking: bool,
blanchet@38982
    19
     debug: bool,
blanchet@35969
    20
     verbose: bool,
blanchet@36143
    21
     overlord: bool,
blanchet@40059
    22
     provers: string list,
blanchet@35969
    23
     full_types: bool,
blanchet@36235
    24
     explicit_apply: bool,
blanchet@38745
    25
     relevance_thresholds: real * real,
blanchet@38744
    26
     max_relevant: int option,
blanchet@35969
    27
     isar_proof: bool,
blanchet@36924
    28
     isar_shrink_factor: int,
blanchet@38985
    29
     timeout: Time.time,
blanchet@38985
    30
     expect: string}
blanchet@39493
    31
blanchet@40061
    32
  datatype axiom =
blanchet@40061
    33
    Unprepared of (string * locality) * thm |
blanchet@40061
    34
    Prepared of term * ((string * locality) * fol_formula) option
blanchet@40061
    35
blanchet@40061
    36
  type prover_problem =
blanchet@39318
    37
    {state: Proof.state,
blanchet@38998
    38
     goal: thm,
blanchet@38998
    39
     subgoal: int,
blanchet@40061
    40
     axioms: axiom list,
blanchet@39366
    41
     only: bool}
blanchet@39493
    42
blanchet@40061
    43
  type prover_result =
blanchet@36370
    44
    {outcome: failure option,
blanchet@40061
    45
     used_axioms: (string * locality) list,
blanchet@40062
    46
     run_time_in_msecs: int option,
blanchet@40062
    47
     message: string}
blanchet@39493
    48
blanchet@40061
    49
  type prover = params -> minimize_command -> prover_problem -> prover_result
blanchet@35867
    50
blanchet@40060
    51
  val smtN : string
blanchet@40063
    52
  val default_max_relevant_for_prover : theory -> string -> int
blanchet@38023
    53
  val dest_dir : string Config.T
blanchet@38023
    54
  val problem_prefix : string Config.T
blanchet@39003
    55
  val measure_run_time : bool Config.T
blanchet@40059
    56
  val available_provers : theory -> unit
blanchet@40059
    57
  val kill_provers : unit -> unit
blanchet@40059
    58
  val running_provers : unit -> unit
blanchet@40059
    59
  val messages : int option -> unit
blanchet@40063
    60
  val get_prover : theory -> bool -> string -> prover
blanchet@38044
    61
  val run_sledgehammer :
blanchet@39318
    62
    params -> bool -> int -> relevance_override -> (string -> minimize_command)
blanchet@39318
    63
    -> Proof.state -> bool * Proof.state
blanchet@38023
    64
  val setup : theory -> theory
wenzelm@28477
    65
end;
wenzelm@28477
    66
blanchet@38021
    67
structure Sledgehammer : SLEDGEHAMMER =
wenzelm@28477
    68
struct
wenzelm@28477
    69
blanchet@38028
    70
open ATP_Problem
blanchet@39491
    71
open ATP_Proof
blanchet@38028
    72
open ATP_Systems
blanchet@39494
    73
open Metis_Translate
blanchet@38023
    74
open Sledgehammer_Util
blanchet@38988
    75
open Sledgehammer_Filter
blanchet@38282
    76
open Sledgehammer_Translate
blanchet@38988
    77
open Sledgehammer_Reconstruct
blanchet@37583
    78
blanchet@38023
    79
blanchet@37583
    80
(** The Sledgehammer **)
blanchet@37583
    81
blanchet@38102
    82
(* Identifier to distinguish Sledgehammer from other tools using
blanchet@38102
    83
   "Async_Manager". *)
blanchet@37585
    84
val das_Tool = "Sledgehammer"
blanchet@37585
    85
blanchet@40060
    86
val smtN = "smt"
blanchet@40062
    87
val smt_prover_names = [smtN, remote_prefix ^ smtN]
blanchet@40062
    88
blanchet@40062
    89
val smt_default_max_relevant = 300 (* FUDGE *)
blanchet@40062
    90
val auto_max_relevant_divisor = 2 (* FUDGE *)
blanchet@40060
    91
blanchet@40063
    92
fun default_max_relevant_for_prover thy name =
blanchet@40063
    93
  if member (op =) smt_prover_names name then smt_default_max_relevant
blanchet@40063
    94
  else #default_max_relevant (get_atp thy name)
blanchet@40063
    95
blanchet@40059
    96
fun available_provers thy =
blanchet@40060
    97
  let
blanchet@40060
    98
    val (remote_provers, local_provers) =
blanchet@40062
    99
      sort_strings (available_atps thy) @ smt_prover_names
blanchet@40060
   100
      |> List.partition (String.isPrefix remote_prefix)
blanchet@40060
   101
  in
blanchet@40060
   102
    priority ("Available provers: " ^ commas (local_provers @ remote_provers) ^
blanchet@40060
   103
              ".")
blanchet@40060
   104
  end
blanchet@35969
   105
blanchet@40059
   106
fun kill_provers () = Async_Manager.kill_threads das_Tool "provers"
blanchet@40059
   107
fun running_provers () = Async_Manager.running_threads das_Tool "provers"
blanchet@40059
   108
val messages = Async_Manager.thread_messages das_Tool "prover"
blanchet@40059
   109
blanchet@40059
   110
(** problems, results, ATPs, etc. **)
blanchet@35969
   111
blanchet@35969
   112
type params =
blanchet@38982
   113
  {blocking: bool,
blanchet@38982
   114
   debug: bool,
blanchet@35969
   115
   verbose: bool,
blanchet@36143
   116
   overlord: bool,
blanchet@40059
   117
   provers: string list,
blanchet@35969
   118
   full_types: bool,
blanchet@36235
   119
   explicit_apply: bool,
blanchet@38745
   120
   relevance_thresholds: real * real,
blanchet@38744
   121
   max_relevant: int option,
blanchet@35969
   122
   isar_proof: bool,
blanchet@36924
   123
   isar_shrink_factor: int,
blanchet@38985
   124
   timeout: Time.time,
blanchet@38985
   125
   expect: string}
blanchet@35867
   126
blanchet@40061
   127
datatype axiom =
blanchet@40061
   128
  Unprepared of (string * locality) * thm |
blanchet@40061
   129
  Prepared of term * ((string * locality) * fol_formula) option
blanchet@40061
   130
blanchet@40061
   131
type prover_problem =
blanchet@39318
   132
  {state: Proof.state,
blanchet@38998
   133
   goal: thm,
blanchet@38998
   134
   subgoal: int,
blanchet@40061
   135
   axioms: axiom list,
blanchet@39366
   136
   only: bool}
blanchet@35867
   137
blanchet@40061
   138
type prover_result =
blanchet@36370
   139
  {outcome: failure option,
blanchet@35969
   140
   message: string,
blanchet@40061
   141
   used_axioms: (string * locality) list,
blanchet@40062
   142
   run_time_in_msecs: int option}
blanchet@35867
   143
blanchet@40061
   144
type prover = params -> minimize_command -> prover_problem -> prover_result
blanchet@35867
   145
blanchet@38023
   146
(* configuration attributes *)
blanchet@38023
   147
blanchet@38991
   148
val (dest_dir, dest_dir_setup) =
blanchet@39003
   149
  Attrib.config_string "sledgehammer_dest_dir" (K "")
blanchet@38991
   150
  (* Empty string means create files in Isabelle's temporary files directory. *)
blanchet@38023
   151
blanchet@38023
   152
val (problem_prefix, problem_prefix_setup) =
blanchet@39003
   153
  Attrib.config_string "sledgehammer_problem_prefix" (K "prob")
blanchet@38023
   154
blanchet@39003
   155
val (measure_run_time, measure_run_time_setup) =
blanchet@39003
   156
  Attrib.config_bool "sledgehammer_measure_run_time" (K false)
wenzelm@28484
   157
blanchet@38023
   158
fun with_path cleanup after f path =
blanchet@38023
   159
  Exn.capture f path
blanchet@38023
   160
  |> tap (fn _ => cleanup path)
blanchet@38023
   161
  |> Exn.release
blanchet@38023
   162
  |> tap (after path)
blanchet@38023
   163
blanchet@40060
   164
fun prover_description ctxt ({blocking, verbose, ...} : params) name num_axioms
blanchet@40060
   165
                       i n goal =
blanchet@40060
   166
  quote name ^
blanchet@40060
   167
  (if verbose then
blanchet@40060
   168
     " with " ^ string_of_int num_axioms ^ " fact" ^ plural_s num_axioms
blanchet@40060
   169
   else
blanchet@40060
   170
     "") ^
blanchet@40060
   171
  " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
blanchet@40060
   172
  (if blocking then
blanchet@40060
   173
     ""
blanchet@40060
   174
   else
blanchet@40060
   175
     "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@40060
   176
blanchet@40060
   177
fun proof_banner auto =
blanchet@40060
   178
  if auto then "Sledgehammer found a proof" else "Try this command"
blanchet@40060
   179
blanchet@40059
   180
(* generic TPTP-based ATPs *)
blanchet@38023
   181
blanchet@40061
   182
fun dest_Unprepared (Unprepared p) = p
blanchet@40061
   183
  | dest_Unprepared (Prepared _) = raise Fail "dest_Unprepared"
blanchet@40061
   184
fun prepared_axiom ctxt (Unprepared p) = prepare_axiom ctxt p
blanchet@40061
   185
  | prepared_axiom _ (Prepared p) = p
blanchet@40061
   186
blanchet@40062
   187
fun int_option_add (SOME m) (SOME n) = SOME (m + n)
blanchet@40062
   188
  | int_option_add _ _ = NONE
blanchet@40062
   189
blanchet@39492
   190
(* Important messages are important but not so important that users want to see
blanchet@39492
   191
   them each time. *)
blanchet@39493
   192
val important_message_keep_factor = 0.1
blanchet@39492
   193
blanchet@40063
   194
fun run_atp auto atp_name
blanchet@38645
   195
        {exec, required_execs, arguments, has_incomplete_mode, proof_delims,
blanchet@38997
   196
         known_failures, default_max_relevant, explicit_forall,
blanchet@38997
   197
         use_conjecture_for_hypotheses}
blanchet@38455
   198
        ({debug, verbose, overlord, full_types, explicit_apply,
blanchet@38998
   199
          max_relevant, isar_proof, isar_shrink_factor, timeout, ...} : params)
blanchet@40061
   200
        minimize_command
blanchet@40061
   201
        ({state, goal, subgoal, axioms, only} : prover_problem) =
blanchet@38023
   202
  let
blanchet@39318
   203
    val ctxt = Proof.context_of state
blanchet@38998
   204
    val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal
blanchet@40061
   205
    val axioms =
blanchet@40061
   206
      axioms |> not only ? take (the_default default_max_relevant max_relevant)
blanchet@40061
   207
             |> map (prepared_axiom ctxt)
blanchet@40059
   208
    val dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@40059
   209
                   else Config.get ctxt dest_dir
blanchet@40059
   210
    val problem_prefix = Config.get ctxt problem_prefix
blanchet@40061
   211
    val problem_file_name =
blanchet@39003
   212
      Path.basic ((if overlord then "prob_" ^ atp_name
blanchet@40059
   213
                   else problem_prefix ^ serial_string ())
blanchet@39003
   214
                  ^ "_" ^ string_of_int subgoal)
blanchet@40061
   215
    val problem_path_name =
blanchet@40059
   216
      if dest_dir = "" then
blanchet@40061
   217
        File.tmp_path problem_file_name
blanchet@40059
   218
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   219
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   220
      else
blanchet@40059
   221
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@39003
   222
    val measure_run_time = verbose orelse Config.get ctxt measure_run_time
blanchet@38092
   223
    val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
blanchet@40059
   224
    (* write out problem file and call ATP *)
blanchet@38645
   225
    fun command_line complete timeout probfile =
blanchet@38023
   226
      let
blanchet@38023
   227
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@38023
   228
                   " " ^ File.shell_path probfile
blanchet@38023
   229
      in
blanchet@39010
   230
        (if measure_run_time then "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
blanchet@38744
   231
         else "exec " ^ core) ^ " 2>&1"
blanchet@38023
   232
      end
blanchet@38023
   233
    fun split_time s =
blanchet@38023
   234
      let
blanchet@38023
   235
        val split = String.tokens (fn c => str c = "\n");
blanchet@38023
   236
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
blanchet@38023
   237
        fun as_num f = f >> (fst o read_int);
blanchet@38023
   238
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
blanchet@38023
   239
        val digit = Scan.one Symbol.is_ascii_digit;
blanchet@38023
   240
        val num3 = as_num (digit ::: digit ::: (digit >> single));
blanchet@38023
   241
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
blanchet@40062
   242
        val as_time = Scan.read Symbol.stopper time o explode
blanchet@38023
   243
      in (output, as_time t) end;
blanchet@38023
   244
    fun run_on probfile =
blanchet@38092
   245
      case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
blanchet@38032
   246
        (home_var, _) :: _ =>
blanchet@38023
   247
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@38032
   248
      | [] =>
blanchet@38032
   249
        if File.exists command then
blanchet@38032
   250
          let
blanchet@39318
   251
            fun run complete timeout =
blanchet@38032
   252
              let
blanchet@38645
   253
                val command = command_line complete timeout probfile
blanchet@38032
   254
                val ((output, msecs), res_code) =
blanchet@38032
   255
                  bash_output command
blanchet@38032
   256
                  |>> (if overlord then
blanchet@38032
   257
                         prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@38032
   258
                       else
blanchet@38032
   259
                         I)
blanchet@40062
   260
                  |>> (if measure_run_time then split_time else rpair NONE)
blanchet@39452
   261
                val (tstplike_proof, outcome) =
blanchet@39452
   262
                  extract_tstplike_proof_and_outcome complete res_code
blanchet@39452
   263
                      proof_delims known_failures output
blanchet@39452
   264
              in (output, msecs, tstplike_proof, outcome) end
blanchet@38032
   265
            val readable_names = debug andalso overlord
blanchet@40059
   266
            val (atp_problem, pool, conjecture_offset, axiom_names) =
blanchet@40059
   267
              prepare_atp_problem ctxt readable_names explicit_forall full_types
blanchet@40059
   268
                                  explicit_apply hyp_ts concl_t axioms
blanchet@39452
   269
            val ss = tptp_strings_for_atp_problem use_conjecture_for_hypotheses
blanchet@40059
   270
                                                  atp_problem
blanchet@38631
   271
            val _ = File.write_list probfile ss
blanchet@38032
   272
            val conjecture_shape =
blanchet@38032
   273
              conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@38040
   274
              |> map single
blanchet@39318
   275
            val run_twice = has_incomplete_mode andalso not auto
blanchet@38645
   276
            val timer = Timer.startRealTimer ()
blanchet@38032
   277
            val result =
blanchet@39318
   278
              run false (if run_twice then
blanchet@39318
   279
                           Time.fromMilliseconds
blanchet@38645
   280
                                         (2 * Time.toMilliseconds timeout div 3)
blanchet@39318
   281
                         else
blanchet@39318
   282
                           timeout)
blanchet@39318
   283
              |> run_twice
blanchet@38645
   284
                 ? (fn (_, msecs0, _, SOME _) =>
blanchet@39318
   285
                       run true (Time.- (timeout, Timer.checkRealTimer timer))
blanchet@39452
   286
                       |> (fn (output, msecs, tstplike_proof, outcome) =>
blanchet@40062
   287
                              (output, int_option_add msecs0 msecs,
blanchet@40062
   288
                               tstplike_proof, outcome))
blanchet@38645
   289
                     | result => result)
blanchet@38282
   290
          in ((pool, conjecture_shape, axiom_names), result) end
blanchet@38032
   291
        else
blanchet@38032
   292
          error ("Bad executable: " ^ Path.implode command ^ ".")
blanchet@38023
   293
blanchet@38023
   294
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   295
       the proof file too. *)
blanchet@38023
   296
    fun cleanup probfile =
blanchet@40059
   297
      if dest_dir = "" then try File.rm probfile else NONE
blanchet@38023
   298
    fun export probfile (_, (output, _, _, _)) =
blanchet@40059
   299
      if dest_dir = "" then
blanchet@38023
   300
        ()
blanchet@38023
   301
      else
blanchet@38023
   302
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
blanchet@38282
   303
    val ((pool, conjecture_shape, axiom_names),
blanchet@39452
   304
         (output, msecs, tstplike_proof, outcome)) =
blanchet@40061
   305
      with_path cleanup export run_on problem_path_name
blanchet@38282
   306
    val (conjecture_shape, axiom_names) =
blanchet@39493
   307
      repair_conjecture_shape_and_axiom_names output conjecture_shape
blanchet@39493
   308
                                              axiom_names
blanchet@39492
   309
    val important_message =
blanchet@39493
   310
      if random () <= important_message_keep_factor then
blanchet@39492
   311
        extract_important_message output
blanchet@39492
   312
      else
blanchet@39492
   313
        ""
blanchet@40061
   314
    val (message, used_axioms) =
blanchet@38023
   315
      case outcome of
blanchet@38023
   316
        NONE =>
blanchet@38023
   317
        proof_text isar_proof
blanchet@38023
   318
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@40060
   319
            (proof_banner auto, full_types, minimize_command, tstplike_proof,
blanchet@40060
   320
             axiom_names, goal, subgoal)
blanchet@38744
   321
        |>> (fn message =>
blanchet@38744
   322
                message ^ (if verbose then
blanchet@40062
   323
                             "\nATP real CPU time: " ^
blanchet@40062
   324
                             string_of_int (the msecs) ^ " ms."
blanchet@38744
   325
                           else
blanchet@39107
   326
                             "") ^
blanchet@39107
   327
                (if important_message <> "" then
blanchet@39107
   328
                   "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@39107
   329
                   important_message
blanchet@39107
   330
                 else
blanchet@39107
   331
                   ""))
blanchet@38597
   332
      | SOME failure => (string_for_failure failure, [])
blanchet@38023
   333
  in
blanchet@40061
   334
    {outcome = outcome, message = message, used_axioms = used_axioms,
blanchet@40061
   335
     run_time_in_msecs = msecs}
blanchet@38023
   336
  end
blanchet@38023
   337
blanchet@40063
   338
fun get_atp_prover thy auto name = run_atp auto name (get_atp thy name)
blanchet@40063
   339
blanchet@40063
   340
(* FIXME: dummy *)
blanchet@40063
   341
fun run_smt_solver remote timeout state axioms i =
blanchet@40063
   342
  {outcome = NONE, used_axioms = axioms |> take 5 |> map fst,
blanchet@40063
   343
   run_time_in_msecs = NONE}
blanchet@38023
   344
blanchet@40063
   345
fun get_smt_prover remote ({timeout, ...} : params) minimize_command
blanchet@40063
   346
                   ({state, subgoal, axioms, ...} : prover_problem) =
blanchet@36379
   347
  let
blanchet@40063
   348
    val {outcome, used_axioms, run_time_in_msecs} =
blanchet@40063
   349
      run_smt_solver remote timeout state
blanchet@40063
   350
                     (map_filter (try dest_Unprepared) axioms) subgoal
blanchet@40063
   351
    val message =
blanchet@40063
   352
      if outcome = NONE then
blanchet@40063
   353
        sendback_line (proof_banner false)
blanchet@40063
   354
                      (apply_on_subgoal subgoal (subgoal_count state) ^
blanchet@40063
   355
                       command_call smtN (map fst used_axioms))
blanchet@40063
   356
      else
blanchet@40063
   357
        ""
blanchet@40063
   358
  in
blanchet@40063
   359
    {outcome = outcome, used_axioms = used_axioms,
blanchet@40063
   360
     run_time_in_msecs = run_time_in_msecs, message = message}
blanchet@40063
   361
  end
blanchet@40063
   362
blanchet@40063
   363
fun get_prover thy auto name =
blanchet@40063
   364
  if member (op =) smt_prover_names name then
blanchet@40063
   365
    get_smt_prover (String.isPrefix remote_prefix)
blanchet@40063
   366
  else
blanchet@40063
   367
    get_atp_prover thy auto name
blanchet@40063
   368
blanchet@40063
   369
fun run_prover (params as {blocking, debug, max_relevant, timeout, expect, ...})
blanchet@40063
   370
               auto i n minimize_command (problem as {state, goal, axioms, ...})
blanchet@40063
   371
               name =
blanchet@40063
   372
  let
blanchet@40063
   373
    val thy = Proof.theory_of state
blanchet@39318
   374
    val ctxt = Proof.context_of state
blanchet@37584
   375
    val birth_time = Time.now ()
blanchet@37584
   376
    val death_time = Time.+ (birth_time, timeout)
blanchet@40063
   377
    val max_relevant =
blanchet@40063
   378
      the_default (default_max_relevant_for_prover thy name) max_relevant
blanchet@39110
   379
    val num_axioms = Int.min (length axioms, max_relevant)
blanchet@40063
   380
    val desc = prover_description ctxt params name num_axioms i n goal
blanchet@39318
   381
    fun go () =
blanchet@38982
   382
      let
blanchet@39453
   383
        fun really_go () =
blanchet@40063
   384
          get_prover thy auto name params (minimize_command name) problem
blanchet@38985
   385
          |> (fn {outcome, message, ...} =>
blanchet@38985
   386
                 (if is_some outcome then "none" else "some", message))
blanchet@39453
   387
        val (outcome_code, message) =
blanchet@39453
   388
          if debug then
blanchet@39453
   389
            really_go ()
blanchet@39453
   390
          else
blanchet@39453
   391
            (really_go ()
blanchet@39453
   392
             handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@39453
   393
                  | exn => ("unknown", "Internal error:\n" ^
blanchet@39453
   394
                                       ML_Compiler.exn_message exn ^ "\n"))
blanchet@39318
   395
        val _ =
blanchet@39318
   396
          if expect = "" orelse outcome_code = expect then
blanchet@39318
   397
            ()
blanchet@39318
   398
          else if blocking then
blanchet@39318
   399
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@39318
   400
          else
blanchet@39318
   401
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@39318
   402
      in (outcome_code = "some", message) end
blanchet@39318
   403
  in
blanchet@39318
   404
    if auto then
blanchet@39318
   405
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39318
   406
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@39318
   407
             Pretty.chunks [Pretty.str "", Pretty.mark Markup.hilite
blanchet@39327
   408
                 (Pretty.str message)]))
blanchet@38982
   409
      end
blanchet@39318
   410
    else if blocking then
blanchet@39318
   411
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39370
   412
        List.app priority
blanchet@39370
   413
                 (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
blanchet@39370
   414
        (success, state)
blanchet@39318
   415
      end
blanchet@39318
   416
    else
blanchet@39318
   417
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@39318
   418
       (false, state))
blanchet@37584
   419
  end
wenzelm@28582
   420
blanchet@40063
   421
fun run_smt_solver_somehow state params minimize_command i n goal axioms name =
blanchet@40060
   422
  let
blanchet@40061
   423
    val ctxt = Proof.context_of state
blanchet@40063
   424
    val remote = String.isPrefix remote_prefix name
blanchet@40061
   425
    val desc = prover_description ctxt params name (length axioms) i n goal
blanchet@40062
   426
    val problem =
blanchet@40062
   427
      {state = state, goal = goal, subgoal = i,
blanchet@40062
   428
       axioms = axioms |> map Unprepared, only = true}
blanchet@40062
   429
    val {outcome, used_axioms, message, ...} =
blanchet@40063
   430
      get_smt_prover remote params minimize_command problem
blanchet@40062
   431
    val _ =
blanchet@40062
   432
      priority (das_Tool ^ ": " ^ desc ^ "\n" ^
blanchet@40062
   433
                (if outcome = NONE then message
blanchet@40062
   434
                 else "An unknown error occurred."))
blanchet@40062
   435
  in outcome = NONE end
blanchet@39318
   436
blanchet@40059
   437
fun run_sledgehammer (params as {blocking, provers, full_types,
blanchet@40060
   438
                                 relevance_thresholds, max_relevant, timeout,
blanchet@40060
   439
                                 ...})
blanchet@39366
   440
                     auto i (relevance_override as {only, ...}) minimize_command
blanchet@39366
   441
                     state =
blanchet@40059
   442
  if null provers then
blanchet@40059
   443
    error "No prover is set."
blanchet@39318
   444
  else case subgoal_count state of
blanchet@39318
   445
    0 => (priority "No subgoal!"; (false, state))
blanchet@39318
   446
  | n =>
blanchet@39318
   447
    let
blanchet@39364
   448
      val _ = Proof.assert_backward state
blanchet@39318
   449
      val thy = Proof.theory_of state
blanchet@40060
   450
      val {context = ctxt, facts = chained_ths, goal} = Proof.goal state
blanchet@40060
   451
      val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@40059
   452
      val _ = () |> not blocking ? kill_provers
blanchet@39318
   453
      val _ = if auto then () else priority "Sledgehammering..."
blanchet@40063
   454
      fun relevant full_types max_relevant =
blanchet@40063
   455
        relevant_facts ctxt full_types relevance_thresholds max_relevant
blanchet@40063
   456
                       relevance_override chained_ths hyp_ts concl_t
blanchet@40060
   457
      val (smts, atps) =
blanchet@40062
   458
        provers |> List.partition (member (op =) smt_prover_names)
blanchet@40063
   459
                |>> take 1 (* no point in running both "smt" and "remote_smt" *)
blanchet@40060
   460
      fun run_atps (res as (success, state)) =
blanchet@40060
   461
        if success orelse null atps then
blanchet@40060
   462
          res
blanchet@40060
   463
        else
blanchet@40060
   464
          let
blanchet@40060
   465
            val max_max_relevant =
blanchet@40060
   466
              case max_relevant of
blanchet@40060
   467
                SOME n => n
blanchet@40060
   468
              | NONE =>
blanchet@40063
   469
                0 |> fold (Integer.max o default_max_relevant_for_prover thy)
blanchet@40063
   470
                          atps
blanchet@40060
   471
                  |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@40063
   472
            val axioms = relevant full_types max_max_relevant
blanchet@40063
   473
                         |> map (Prepared o prepare_axiom ctxt)
blanchet@40062
   474
            val problem =
blanchet@40063
   475
              {state = state, goal = goal, subgoal = i, axioms = axioms,
blanchet@40061
   476
               only = only}
blanchet@40063
   477
            val run_prover = run_prover params auto i n minimize_command problem
blanchet@40060
   478
          in
blanchet@40060
   479
            if auto then
blanchet@40061
   480
              fold (fn prover => fn (true, state) => (true, state)
blanchet@40063
   481
                                  | (false, _) => run_prover prover)
blanchet@40060
   482
                   atps (false, state)
blanchet@40060
   483
            else
blanchet@40063
   484
              atps |> (if blocking then Par_List.map else map) run_prover
blanchet@40060
   485
                   |> exists fst |> rpair state
blanchet@40060
   486
          end
blanchet@40060
   487
      fun run_smt_solvers (res as (success, state)) =
blanchet@40060
   488
        if success orelse null smts then
blanchet@40060
   489
          res
blanchet@40060
   490
        else
blanchet@40060
   491
          let
blanchet@40060
   492
            val max_relevant =
blanchet@40060
   493
              max_relevant |> the_default smt_default_max_relevant
blanchet@40063
   494
            val axioms = relevant true max_relevant
blanchet@40060
   495
          in
blanchet@40062
   496
            smts |> map (run_smt_solver_somehow state params minimize_command i
blanchet@40062
   497
                                                n goal axioms)
blanchet@40060
   498
                 |> exists I |> rpair state
blanchet@40060
   499
          end
blanchet@40060
   500
      fun run_atps_and_smt_solvers () =
blanchet@40060
   501
        [run_atps, run_smt_solvers]
blanchet@40060
   502
        |> Par_List.map (fn f => f (false, state) |> K ())
blanchet@40060
   503
    in
blanchet@40060
   504
      if blocking then
blanchet@40060
   505
        (false, state) |> run_atps |> not auto ? run_smt_solvers
blanchet@40060
   506
      else
blanchet@40060
   507
        Future.fork (tap run_atps_and_smt_solvers) |> K (false, state)
blanchet@40060
   508
    end
blanchet@38044
   509
blanchet@38023
   510
val setup =
blanchet@38023
   511
  dest_dir_setup
blanchet@38023
   512
  #> problem_prefix_setup
blanchet@39003
   513
  #> measure_run_time_setup
blanchet@38023
   514
wenzelm@28582
   515
end;