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