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