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