src/HOL/Tools/Sledgehammer/sledgehammer.ML
author blanchet
Thu Sep 16 15:16:08 2010 +0200 (2010-09-16 ago)
changeset 39491 2416666e6f94
parent 39453 1740a2d6bef9
child 39492 b1172d65dd28
permissions -rw-r--r--
refactoring: move ATP proof and error extraction code to "ATP_Proof" module
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@35969
    16
  type params =
blanchet@38982
    17
    {blocking: bool,
blanchet@38982
    18
     debug: bool,
blanchet@35969
    19
     verbose: bool,
blanchet@36143
    20
     overlord: bool,
blanchet@35969
    21
     atps: string list,
blanchet@35969
    22
     full_types: bool,
blanchet@36235
    23
     explicit_apply: bool,
blanchet@38745
    24
     relevance_thresholds: real * real,
blanchet@38744
    25
     max_relevant: int option,
blanchet@35969
    26
     isar_proof: bool,
blanchet@36924
    27
     isar_shrink_factor: int,
blanchet@38985
    28
     timeout: Time.time,
blanchet@38985
    29
     expect: string}
blanchet@35867
    30
  type problem =
blanchet@39318
    31
    {state: Proof.state,
blanchet@38998
    32
     goal: thm,
blanchet@38998
    33
     subgoal: int,
blanchet@39366
    34
     axioms: (term * ((string * locality) * fol_formula) option) list,
blanchet@39366
    35
     only: bool}
blanchet@35867
    36
  type prover_result =
blanchet@36370
    37
    {outcome: failure option,
blanchet@35969
    38
     message: string,
blanchet@37926
    39
     pool: string Symtab.table,
blanchet@38752
    40
     used_thm_names: (string * locality) list,
blanchet@35969
    41
     atp_run_time_in_msecs: int,
blanchet@36369
    42
     output: string,
blanchet@39452
    43
     tstplike_proof: string,
blanchet@38818
    44
     axiom_names: (string * locality) list vector,
blanchet@38083
    45
     conjecture_shape: int list list}
blanchet@38100
    46
  type prover = params -> minimize_command -> problem -> prover_result
blanchet@35867
    47
blanchet@38023
    48
  val dest_dir : string Config.T
blanchet@38023
    49
  val problem_prefix : string Config.T
blanchet@39003
    50
  val measure_run_time : bool Config.T
blanchet@35969
    51
  val kill_atps: unit -> unit
blanchet@35969
    52
  val running_atps: unit -> unit
wenzelm@29112
    53
  val messages: int option -> unit
blanchet@38023
    54
  val get_prover_fun : theory -> string -> prover
blanchet@38044
    55
  val run_sledgehammer :
blanchet@39318
    56
    params -> bool -> int -> relevance_override -> (string -> minimize_command)
blanchet@39318
    57
    -> Proof.state -> bool * Proof.state
blanchet@38023
    58
  val setup : theory -> theory
wenzelm@28477
    59
end;
wenzelm@28477
    60
blanchet@38021
    61
structure Sledgehammer : SLEDGEHAMMER =
wenzelm@28477
    62
struct
wenzelm@28477
    63
blanchet@38028
    64
open ATP_Problem
blanchet@39491
    65
open ATP_Proof
blanchet@38028
    66
open ATP_Systems
blanchet@37578
    67
open Metis_Clauses
blanchet@38023
    68
open Sledgehammer_Util
blanchet@38988
    69
open Sledgehammer_Filter
blanchet@38282
    70
open Sledgehammer_Translate
blanchet@38988
    71
open Sledgehammer_Reconstruct
blanchet@37583
    72
blanchet@38023
    73
blanchet@37583
    74
(** The Sledgehammer **)
blanchet@37583
    75
blanchet@38102
    76
(* Identifier to distinguish Sledgehammer from other tools using
blanchet@38102
    77
   "Async_Manager". *)
blanchet@37585
    78
val das_Tool = "Sledgehammer"
blanchet@37585
    79
blanchet@37585
    80
fun kill_atps () = Async_Manager.kill_threads das_Tool "ATPs"
blanchet@37585
    81
fun running_atps () = Async_Manager.running_threads das_Tool "ATPs"
blanchet@37585
    82
val messages = Async_Manager.thread_messages das_Tool "ATP"
blanchet@35969
    83
blanchet@36281
    84
(** problems, results, provers, etc. **)
blanchet@35969
    85
blanchet@35969
    86
type params =
blanchet@38982
    87
  {blocking: bool,
blanchet@38982
    88
   debug: bool,
blanchet@35969
    89
   verbose: bool,
blanchet@36143
    90
   overlord: bool,
blanchet@35969
    91
   atps: string list,
blanchet@35969
    92
   full_types: bool,
blanchet@36235
    93
   explicit_apply: bool,
blanchet@38745
    94
   relevance_thresholds: real * real,
blanchet@38744
    95
   max_relevant: int option,
blanchet@35969
    96
   isar_proof: bool,
blanchet@36924
    97
   isar_shrink_factor: int,
blanchet@38985
    98
   timeout: Time.time,
blanchet@38985
    99
   expect: string}
blanchet@35867
   100
blanchet@35867
   101
type problem =
blanchet@39318
   102
  {state: Proof.state,
blanchet@38998
   103
   goal: thm,
blanchet@38998
   104
   subgoal: int,
blanchet@39366
   105
   axioms: (term * ((string * locality) * fol_formula) option) list,
blanchet@39366
   106
   only: bool}
blanchet@35867
   107
blanchet@35867
   108
type prover_result =
blanchet@36370
   109
  {outcome: failure option,
blanchet@35969
   110
   message: string,
blanchet@37926
   111
   pool: string Symtab.table,
blanchet@38752
   112
   used_thm_names: (string * locality) list,
blanchet@35969
   113
   atp_run_time_in_msecs: int,
blanchet@36369
   114
   output: string,
blanchet@39452
   115
   tstplike_proof: string,
blanchet@38818
   116
   axiom_names: (string * locality) list vector,
blanchet@38083
   117
   conjecture_shape: int list list}
blanchet@35867
   118
blanchet@38100
   119
type prover = params -> minimize_command -> problem -> prover_result
blanchet@35867
   120
blanchet@38023
   121
(* configuration attributes *)
blanchet@38023
   122
blanchet@38991
   123
val (dest_dir, dest_dir_setup) =
blanchet@39003
   124
  Attrib.config_string "sledgehammer_dest_dir" (K "")
blanchet@38991
   125
  (* Empty string means create files in Isabelle's temporary files directory. *)
blanchet@38023
   126
blanchet@38023
   127
val (problem_prefix, problem_prefix_setup) =
blanchet@39003
   128
  Attrib.config_string "sledgehammer_problem_prefix" (K "prob")
blanchet@38023
   129
blanchet@39003
   130
val (measure_run_time, measure_run_time_setup) =
blanchet@39003
   131
  Attrib.config_bool "sledgehammer_measure_run_time" (K false)
wenzelm@28484
   132
blanchet@38023
   133
fun with_path cleanup after f path =
blanchet@38023
   134
  Exn.capture f path
blanchet@38023
   135
  |> tap (fn _ => cleanup path)
blanchet@38023
   136
  |> Exn.release
blanchet@38023
   137
  |> tap (after path)
blanchet@38023
   138
blanchet@38023
   139
fun extract_clause_sequence output =
blanchet@38023
   140
  let
blanchet@38023
   141
    val tokens_of = String.tokens (not o Char.isAlphaNum)
blanchet@38023
   142
    fun extract_num ("clause" :: (ss as _ :: _)) =
blanchet@38023
   143
    Int.fromString (List.last ss)
blanchet@38023
   144
      | extract_num _ = NONE
blanchet@38023
   145
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
blanchet@38023
   146
blanchet@38023
   147
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
blanchet@38023
   148
blanchet@38023
   149
val parse_clause_formula_pair =
blanchet@38515
   150
  $$ "(" |-- scan_integer --| $$ ","
blanchet@38515
   151
  -- (Symbol.scan_id ::: Scan.repeat ($$ "," |-- Symbol.scan_id)) --| $$ ")"
blanchet@38023
   152
  --| Scan.option ($$ ",")
blanchet@38023
   153
val parse_clause_formula_relation =
blanchet@38023
   154
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
blanchet@38023
   155
  |-- Scan.repeat parse_clause_formula_pair
blanchet@38023
   156
val extract_clause_formula_relation =
blanchet@38738
   157
  Substring.full #> Substring.position set_ClauseFormulaRelationN
blanchet@39007
   158
  #> snd #> Substring.position "." #> fst #> Substring.string
blanchet@39008
   159
  #> explode #> filter_out Symbol.is_blank #> parse_clause_formula_relation
blanchet@39007
   160
  #> fst
blanchet@38023
   161
blanchet@38988
   162
(* TODO: move to "Sledgehammer_Reconstruct" *)
blanchet@38023
   163
fun repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@38698
   164
                                              axiom_names =
blanchet@38023
   165
  if String.isSubstring set_ClauseFormulaRelationN output then
blanchet@38023
   166
    (* This is a hack required for keeping track of axioms after they have been
blanchet@38698
   167
       clausified by SPASS's Flotter tool. The "ATP/scripts/spass" script is
blanchet@38698
   168
       also part of this hack. *)
blanchet@38023
   169
    let
blanchet@38040
   170
      val j0 = hd (hd conjecture_shape)
blanchet@38023
   171
      val seq = extract_clause_sequence output
blanchet@38023
   172
      val name_map = extract_clause_formula_relation output
blanchet@38023
   173
      fun renumber_conjecture j =
blanchet@38744
   174
        conjecture_prefix ^ string_of_int (j - j0)
blanchet@38515
   175
        |> AList.find (fn (s, ss) => member (op =) ss s) name_map
blanchet@38040
   176
        |> map (fn s => find_index (curry (op =) s) seq + 1)
blanchet@38818
   177
      fun names_for_number j =
blanchet@38818
   178
        j |> AList.lookup (op =) name_map |> these
blanchet@38818
   179
          |> map_filter (try (unprefix axiom_prefix)) |> map unascii_of
blanchet@38818
   180
          |> map (fn name =>
blanchet@38818
   181
                     (name, name |> find_first_in_list_vector axiom_names
blanchet@38818
   182
                                 |> the)
blanchet@38818
   183
                     handle Option.Option =>
blanchet@38818
   184
                            error ("No such fact: " ^ quote name ^ "."))
blanchet@38023
   185
    in
blanchet@38040
   186
      (conjecture_shape |> map (maps renumber_conjecture),
blanchet@38818
   187
       seq |> map names_for_number |> Vector.fromList)
blanchet@38023
   188
    end
blanchet@38023
   189
  else
blanchet@38698
   190
    (conjecture_shape, axiom_names)
blanchet@38023
   191
blanchet@38023
   192
blanchet@38023
   193
(* generic TPTP-based provers *)
blanchet@38023
   194
blanchet@39318
   195
fun prover_fun auto atp_name
blanchet@38645
   196
        {exec, required_execs, arguments, has_incomplete_mode, proof_delims,
blanchet@38997
   197
         known_failures, default_max_relevant, explicit_forall,
blanchet@38997
   198
         use_conjecture_for_hypotheses}
blanchet@38455
   199
        ({debug, verbose, overlord, full_types, explicit_apply,
blanchet@38998
   200
          max_relevant, isar_proof, isar_shrink_factor, timeout, ...} : params)
blanchet@39366
   201
        minimize_command ({state, goal, subgoal, axioms, only} : 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@39366
   205
    val axioms = axioms |> not only
blanchet@39366
   206
                          ? take (the_default default_max_relevant max_relevant)
blanchet@38023
   207
    val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@38998
   208
                       else Config.get ctxt dest_dir
blanchet@38998
   209
    val the_problem_prefix = Config.get ctxt problem_prefix
blanchet@39003
   210
    val problem_file_name =
blanchet@39003
   211
      Path.basic ((if overlord then "prob_" ^ atp_name
blanchet@39003
   212
                   else the_problem_prefix ^ serial_string ())
blanchet@39003
   213
                  ^ "_" ^ string_of_int subgoal)
blanchet@39003
   214
    val problem_path_name =
blanchet@39003
   215
      if the_dest_dir = "" then
blanchet@39003
   216
        File.tmp_path problem_file_name
blanchet@39003
   217
      else if File.exists (Path.explode the_dest_dir) then
blanchet@39003
   218
        Path.append (Path.explode the_dest_dir) problem_file_name
blanchet@39003
   219
      else
blanchet@39003
   220
        error ("No such directory: " ^ quote the_dest_dir ^ ".")
blanchet@39003
   221
    val measure_run_time = verbose orelse Config.get ctxt measure_run_time
blanchet@38092
   222
    val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
blanchet@38023
   223
    (* write out problem file and call prover *)
blanchet@38645
   224
    fun command_line complete timeout probfile =
blanchet@38023
   225
      let
blanchet@38023
   226
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@38023
   227
                   " " ^ File.shell_path probfile
blanchet@38023
   228
      in
blanchet@39010
   229
        (if measure_run_time then "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
blanchet@38744
   230
         else "exec " ^ core) ^ " 2>&1"
blanchet@38023
   231
      end
blanchet@38023
   232
    fun split_time s =
blanchet@38023
   233
      let
blanchet@38023
   234
        val split = String.tokens (fn c => str c = "\n");
blanchet@38023
   235
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
blanchet@38023
   236
        fun as_num f = f >> (fst o read_int);
blanchet@38023
   237
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
blanchet@38023
   238
        val digit = Scan.one Symbol.is_ascii_digit;
blanchet@38023
   239
        val num3 = as_num (digit ::: digit ::: (digit >> single));
blanchet@38023
   240
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
blanchet@38023
   241
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
blanchet@38023
   242
      in (output, as_time t) end;
blanchet@38023
   243
    fun run_on probfile =
blanchet@38092
   244
      case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
blanchet@38032
   245
        (home_var, _) :: _ =>
blanchet@38023
   246
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@38032
   247
      | [] =>
blanchet@38032
   248
        if File.exists command then
blanchet@38032
   249
          let
blanchet@39318
   250
            fun run complete timeout =
blanchet@38032
   251
              let
blanchet@38645
   252
                val command = command_line complete timeout probfile
blanchet@38032
   253
                val ((output, msecs), res_code) =
blanchet@38032
   254
                  bash_output command
blanchet@38032
   255
                  |>> (if overlord then
blanchet@38032
   256
                         prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@38032
   257
                       else
blanchet@38032
   258
                         I)
blanchet@38744
   259
                  |>> (if measure_run_time then split_time else rpair 0)
blanchet@39452
   260
                val (tstplike_proof, outcome) =
blanchet@39452
   261
                  extract_tstplike_proof_and_outcome complete res_code
blanchet@39452
   262
                      proof_delims known_failures output
blanchet@39452
   263
              in (output, msecs, tstplike_proof, outcome) end
blanchet@38032
   264
            val readable_names = debug andalso overlord
blanchet@38282
   265
            val (problem, pool, conjecture_offset, axiom_names) =
blanchet@38282
   266
              prepare_problem ctxt readable_names explicit_forall full_types
blanchet@39005
   267
                              explicit_apply hyp_ts concl_t axioms
blanchet@39452
   268
            val ss = tptp_strings_for_atp_problem use_conjecture_for_hypotheses
blanchet@39452
   269
                                                  problem
blanchet@38631
   270
            val _ = File.write_list probfile ss
blanchet@38032
   271
            val conjecture_shape =
blanchet@38032
   272
              conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@38040
   273
              |> map single
blanchet@39318
   274
            val run_twice = has_incomplete_mode andalso not auto
blanchet@38645
   275
            val timer = Timer.startRealTimer ()
blanchet@38032
   276
            val result =
blanchet@39318
   277
              run false (if run_twice then
blanchet@39318
   278
                           Time.fromMilliseconds
blanchet@38645
   279
                                         (2 * Time.toMilliseconds timeout div 3)
blanchet@39318
   280
                         else
blanchet@39318
   281
                           timeout)
blanchet@39318
   282
              |> run_twice
blanchet@38645
   283
                 ? (fn (_, msecs0, _, SOME _) =>
blanchet@39318
   284
                       run true (Time.- (timeout, Timer.checkRealTimer timer))
blanchet@39452
   285
                       |> (fn (output, msecs, tstplike_proof, outcome) =>
blanchet@39452
   286
                              (output, msecs0 + msecs, tstplike_proof, outcome))
blanchet@38645
   287
                     | result => result)
blanchet@38282
   288
          in ((pool, conjecture_shape, axiom_names), result) end
blanchet@38032
   289
        else
blanchet@38032
   290
          error ("Bad executable: " ^ Path.implode command ^ ".")
blanchet@38023
   291
blanchet@38023
   292
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   293
       the proof file too. *)
blanchet@38023
   294
    fun cleanup probfile =
blanchet@38023
   295
      if the_dest_dir = "" then try File.rm probfile else NONE
blanchet@38023
   296
    fun export probfile (_, (output, _, _, _)) =
blanchet@38023
   297
      if the_dest_dir = "" then
blanchet@38023
   298
        ()
blanchet@38023
   299
      else
blanchet@38023
   300
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
blanchet@38282
   301
    val ((pool, conjecture_shape, axiom_names),
blanchet@39452
   302
         (output, msecs, tstplike_proof, outcome)) =
blanchet@39003
   303
      with_path cleanup export run_on problem_path_name
blanchet@38282
   304
    val (conjecture_shape, axiom_names) =
blanchet@38023
   305
      repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@38282
   306
                                                axiom_names
blanchet@39107
   307
    val important_message = extract_important_message output
blanchet@39327
   308
    val banner = if auto then "Sledgehammer found a proof"
blanchet@39327
   309
                 else "Try this command"
blanchet@38023
   310
    val (message, used_thm_names) =
blanchet@38023
   311
      case outcome of
blanchet@38023
   312
        NONE =>
blanchet@38023
   313
        proof_text isar_proof
blanchet@38023
   314
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@39452
   315
            (banner, full_types, minimize_command, tstplike_proof, axiom_names,
blanchet@39452
   316
             goal, subgoal)
blanchet@38744
   317
        |>> (fn message =>
blanchet@38744
   318
                message ^ (if verbose then
blanchet@39371
   319
                             "\nATP real CPU time: " ^ string_of_int msecs ^
blanchet@39371
   320
                             " ms."
blanchet@38744
   321
                           else
blanchet@39107
   322
                             "") ^
blanchet@39107
   323
                (if important_message <> "" then
blanchet@39107
   324
                   "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@39107
   325
                   important_message
blanchet@39107
   326
                 else
blanchet@39107
   327
                   ""))
blanchet@38597
   328
      | SOME failure => (string_for_failure failure, [])
blanchet@38023
   329
  in
blanchet@38023
   330
    {outcome = outcome, message = message, pool = pool,
blanchet@38023
   331
     used_thm_names = used_thm_names, atp_run_time_in_msecs = msecs,
blanchet@39452
   332
     output = output, tstplike_proof = tstplike_proof,
blanchet@39452
   333
     axiom_names = axiom_names, conjecture_shape = conjecture_shape}
blanchet@38023
   334
  end
blanchet@38023
   335
blanchet@39318
   336
fun get_prover_fun thy name = prover_fun false name (get_prover thy name)
blanchet@38023
   337
blanchet@39453
   338
fun run_prover (params as {blocking, debug, verbose, max_relevant, timeout,
blanchet@39453
   339
                           expect, ...})
blanchet@39338
   340
               auto i n minimize_command (problem as {state, goal, axioms, ...})
blanchet@39110
   341
               (prover as {default_max_relevant, ...}, atp_name) =
blanchet@36379
   342
  let
blanchet@39318
   343
    val ctxt = Proof.context_of state
blanchet@37584
   344
    val birth_time = Time.now ()
blanchet@37584
   345
    val death_time = Time.+ (birth_time, timeout)
blanchet@39110
   346
    val max_relevant = the_default default_max_relevant max_relevant
blanchet@39110
   347
    val num_axioms = Int.min (length axioms, max_relevant)
blanchet@36379
   348
    val desc =
blanchet@39110
   349
      "ATP " ^ quote atp_name ^
blanchet@39110
   350
      (if verbose then
blanchet@39110
   351
         " with " ^ string_of_int num_axioms ^ " fact" ^ plural_s num_axioms
blanchet@39110
   352
       else
blanchet@39110
   353
         "") ^
blanchet@39110
   354
      " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
blanchet@38985
   355
      (if blocking then
blanchet@38985
   356
         ""
blanchet@38985
   357
       else
blanchet@38985
   358
         "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@39318
   359
    fun go () =
blanchet@38982
   360
      let
blanchet@39453
   361
        fun really_go () =
blanchet@39318
   362
          prover_fun auto atp_name prover params (minimize_command atp_name)
blanchet@39318
   363
                     problem
blanchet@38985
   364
          |> (fn {outcome, message, ...} =>
blanchet@38985
   365
                 (if is_some outcome then "none" else "some", message))
blanchet@39453
   366
        val (outcome_code, message) =
blanchet@39453
   367
          if debug then
blanchet@39453
   368
            really_go ()
blanchet@39453
   369
          else
blanchet@39453
   370
            (really_go ()
blanchet@39453
   371
             handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@39453
   372
                  | exn => ("unknown", "Internal error:\n" ^
blanchet@39453
   373
                                       ML_Compiler.exn_message exn ^ "\n"))
blanchet@39318
   374
        val _ =
blanchet@39318
   375
          if expect = "" orelse outcome_code = expect then
blanchet@39318
   376
            ()
blanchet@39318
   377
          else if blocking then
blanchet@39318
   378
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@39318
   379
          else
blanchet@39318
   380
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@39318
   381
      in (outcome_code = "some", message) end
blanchet@39318
   382
  in
blanchet@39318
   383
    if auto then
blanchet@39318
   384
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39318
   385
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@39318
   386
             Pretty.chunks [Pretty.str "", Pretty.mark Markup.hilite
blanchet@39327
   387
                 (Pretty.str message)]))
blanchet@38982
   388
      end
blanchet@39318
   389
    else if blocking then
blanchet@39318
   390
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39370
   391
        List.app priority
blanchet@39370
   392
                 (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
blanchet@39370
   393
        (success, state)
blanchet@39318
   394
      end
blanchet@39318
   395
    else
blanchet@39318
   396
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@39318
   397
       (false, state))
blanchet@37584
   398
  end
wenzelm@28582
   399
blanchet@39318
   400
val auto_max_relevant_divisor = 2
blanchet@39318
   401
blanchet@39373
   402
fun run_sledgehammer (params as {blocking, atps, full_types,
blanchet@38998
   403
                                 relevance_thresholds, max_relevant, ...})
blanchet@39366
   404
                     auto i (relevance_override as {only, ...}) minimize_command
blanchet@39366
   405
                     state =
blanchet@39318
   406
  if null atps then
blanchet@39318
   407
    error "No ATP is set."
blanchet@39318
   408
  else case subgoal_count state of
blanchet@39318
   409
    0 => (priority "No subgoal!"; (false, state))
blanchet@39318
   410
  | n =>
blanchet@39318
   411
    let
blanchet@39364
   412
      val _ = Proof.assert_backward state
blanchet@39318
   413
      val thy = Proof.theory_of state
blanchet@39318
   414
      val _ = () |> not blocking ? kill_atps
blanchet@39318
   415
      val _ = if auto then () else priority "Sledgehammering..."
blanchet@39318
   416
      val provers = map (`(get_prover thy)) atps
blanchet@39318
   417
      fun go () =
blanchet@39318
   418
        let
blanchet@39318
   419
          val {context = ctxt, facts = chained_ths, goal} = Proof.goal state
blanchet@39318
   420
          val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@39318
   421
          val max_max_relevant =
blanchet@39318
   422
            case max_relevant of
blanchet@39318
   423
              SOME n => n
blanchet@39318
   424
            | NONE =>
blanchet@39318
   425
              0 |> fold (Integer.max o #default_max_relevant o fst) provers
blanchet@39318
   426
                |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@39318
   427
          val axioms =
blanchet@39318
   428
            relevant_facts ctxt full_types relevance_thresholds
blanchet@39318
   429
                           max_max_relevant relevance_override chained_ths
blanchet@39318
   430
                           hyp_ts concl_t
blanchet@39318
   431
          val problem =
blanchet@39318
   432
            {state = state, goal = goal, subgoal = i,
blanchet@39366
   433
             axioms = map (prepare_axiom ctxt) axioms, only = only}
blanchet@39338
   434
          val run_prover = run_prover params auto i n minimize_command problem
blanchet@39318
   435
        in
blanchet@39318
   436
          if auto then
blanchet@39318
   437
            fold (fn prover => fn (true, state) => (true, state)
blanchet@39318
   438
                                | (false, _) => run_prover prover)
blanchet@39318
   439
                 provers (false, state)
blanchet@39318
   440
          else
blanchet@39318
   441
            (if blocking then Par_List.map else map) run_prover provers
blanchet@39318
   442
            |> exists fst |> rpair state
blanchet@39318
   443
        end
blanchet@39318
   444
    in if blocking then go () else Future.fork (tap go) |> K (false, state) end
blanchet@38044
   445
blanchet@38023
   446
val setup =
blanchet@38023
   447
  dest_dir_setup
blanchet@38023
   448
  #> problem_prefix_setup
blanchet@39003
   449
  #> measure_run_time_setup
blanchet@38023
   450
wenzelm@28582
   451
end;