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