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