src/HOL/Tools/Sledgehammer/sledgehammer.ML
author blanchet
Sat Sep 11 10:21:52 2010 +0200 (2010-09-11 ago)
changeset 39318 ad9a1f9b0558
parent 39263 e2a3c435334b
child 39327 61547eda78b4
permissions -rw-r--r--
implemented Auto Sledgehammer
blanchet@38021
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer.ML
wenzelm@28477
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@32996
     3
    Author:     Makarius
blanchet@35969
     4
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28477
     5
blanchet@38021
     6
Sledgehammer's heart.
wenzelm@28477
     7
*)
wenzelm@28477
     8
blanchet@38021
     9
signature SLEDGEHAMMER =
wenzelm@28477
    10
sig
blanchet@38023
    11
  type failure = ATP_Systems.failure
blanchet@38988
    12
  type locality = Sledgehammer_Filter.locality
blanchet@38988
    13
  type relevance_override = Sledgehammer_Filter.relevance_override
blanchet@39004
    14
  type fol_formula = Sledgehammer_Translate.fol_formula
blanchet@38988
    15
  type minimize_command = Sledgehammer_Reconstruct.minimize_command
blanchet@35969
    16
  type params =
blanchet@38982
    17
    {blocking: bool,
blanchet@38982
    18
     debug: bool,
blanchet@35969
    19
     verbose: bool,
blanchet@36143
    20
     overlord: bool,
blanchet@35969
    21
     atps: string list,
blanchet@35969
    22
     full_types: bool,
blanchet@36235
    23
     explicit_apply: bool,
blanchet@38745
    24
     relevance_thresholds: real * real,
blanchet@38744
    25
     max_relevant: int option,
blanchet@35969
    26
     isar_proof: bool,
blanchet@36924
    27
     isar_shrink_factor: int,
blanchet@38985
    28
     timeout: Time.time,
blanchet@38985
    29
     expect: string}
blanchet@35867
    30
  type problem =
blanchet@39318
    31
    {state: Proof.state,
blanchet@38998
    32
     goal: thm,
blanchet@38998
    33
     subgoal: int,
blanchet@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@39318
    55
    params -> bool -> int -> relevance_override -> (string -> minimize_command)
blanchet@39318
    56
    -> Proof.state -> bool * Proof.state
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@39318
   100
  {state: Proof.state,
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@39262
   165
             "" => ("", SOME (if res_code = 0 andalso output = "" then
blanchet@39262
   166
                                Interrupted
blanchet@39262
   167
                              else
blanchet@39263
   168
                                 UnknownError))
blanchet@38023
   169
           | proof => if res_code = 0 then (proof, NONE)
blanchet@38023
   170
                      else ("", SOME UnknownError))
blanchet@38061
   171
  | SOME failure =>
blanchet@38023
   172
    ("", SOME (if failure = IncompleteUnprovable andalso complete then
blanchet@38023
   173
                 Unprovable
blanchet@38023
   174
               else
blanchet@38023
   175
                 failure))
wenzelm@28582
   176
blanchet@38023
   177
fun extract_clause_sequence output =
blanchet@38023
   178
  let
blanchet@38023
   179
    val tokens_of = String.tokens (not o Char.isAlphaNum)
blanchet@38023
   180
    fun extract_num ("clause" :: (ss as _ :: _)) =
blanchet@38023
   181
    Int.fromString (List.last ss)
blanchet@38023
   182
      | extract_num _ = NONE
blanchet@38023
   183
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
blanchet@38023
   184
blanchet@38023
   185
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
blanchet@38023
   186
blanchet@38023
   187
val parse_clause_formula_pair =
blanchet@38515
   188
  $$ "(" |-- scan_integer --| $$ ","
blanchet@38515
   189
  -- (Symbol.scan_id ::: Scan.repeat ($$ "," |-- Symbol.scan_id)) --| $$ ")"
blanchet@38023
   190
  --| Scan.option ($$ ",")
blanchet@38023
   191
val parse_clause_formula_relation =
blanchet@38023
   192
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
blanchet@38023
   193
  |-- Scan.repeat parse_clause_formula_pair
blanchet@38023
   194
val extract_clause_formula_relation =
blanchet@38738
   195
  Substring.full #> Substring.position set_ClauseFormulaRelationN
blanchet@39007
   196
  #> snd #> Substring.position "." #> fst #> Substring.string
blanchet@39008
   197
  #> explode #> filter_out Symbol.is_blank #> parse_clause_formula_relation
blanchet@39007
   198
  #> fst
blanchet@38023
   199
blanchet@38988
   200
(* TODO: move to "Sledgehammer_Reconstruct" *)
blanchet@38023
   201
fun repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@38698
   202
                                              axiom_names =
blanchet@38023
   203
  if String.isSubstring set_ClauseFormulaRelationN output then
blanchet@38023
   204
    (* This is a hack required for keeping track of axioms after they have been
blanchet@38698
   205
       clausified by SPASS's Flotter tool. The "ATP/scripts/spass" script is
blanchet@38698
   206
       also part of this hack. *)
blanchet@38023
   207
    let
blanchet@38040
   208
      val j0 = hd (hd conjecture_shape)
blanchet@38023
   209
      val seq = extract_clause_sequence output
blanchet@38023
   210
      val name_map = extract_clause_formula_relation output
blanchet@38023
   211
      fun renumber_conjecture j =
blanchet@38744
   212
        conjecture_prefix ^ string_of_int (j - j0)
blanchet@38515
   213
        |> AList.find (fn (s, ss) => member (op =) ss s) name_map
blanchet@38040
   214
        |> map (fn s => find_index (curry (op =) s) seq + 1)
blanchet@38818
   215
      fun names_for_number j =
blanchet@38818
   216
        j |> AList.lookup (op =) name_map |> these
blanchet@38818
   217
          |> map_filter (try (unprefix axiom_prefix)) |> map unascii_of
blanchet@38818
   218
          |> map (fn name =>
blanchet@38818
   219
                     (name, name |> find_first_in_list_vector axiom_names
blanchet@38818
   220
                                 |> the)
blanchet@38818
   221
                     handle Option.Option =>
blanchet@38818
   222
                            error ("No such fact: " ^ quote name ^ "."))
blanchet@38023
   223
    in
blanchet@38040
   224
      (conjecture_shape |> map (maps renumber_conjecture),
blanchet@38818
   225
       seq |> map names_for_number |> Vector.fromList)
blanchet@38023
   226
    end
blanchet@38023
   227
  else
blanchet@38698
   228
    (conjecture_shape, axiom_names)
blanchet@38023
   229
blanchet@38023
   230
blanchet@38023
   231
(* generic TPTP-based provers *)
blanchet@38023
   232
blanchet@39318
   233
fun prover_fun auto atp_name
blanchet@38645
   234
        {exec, required_execs, arguments, has_incomplete_mode, proof_delims,
blanchet@38997
   235
         known_failures, default_max_relevant, explicit_forall,
blanchet@38997
   236
         use_conjecture_for_hypotheses}
blanchet@38455
   237
        ({debug, verbose, overlord, full_types, explicit_apply,
blanchet@38998
   238
          max_relevant, isar_proof, isar_shrink_factor, timeout, ...} : params)
blanchet@39318
   239
        minimize_command ({state, goal, subgoal, axioms} : problem) =
blanchet@38023
   240
  let
blanchet@39318
   241
    val ctxt = Proof.context_of state
blanchet@38998
   242
    val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal
blanchet@38998
   243
    val max_relevant = the_default default_max_relevant max_relevant
blanchet@39005
   244
    val axioms = take max_relevant axioms
blanchet@38023
   245
    val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@38998
   246
                       else Config.get ctxt dest_dir
blanchet@38998
   247
    val the_problem_prefix = Config.get ctxt problem_prefix
blanchet@39003
   248
    val problem_file_name =
blanchet@39003
   249
      Path.basic ((if overlord then "prob_" ^ atp_name
blanchet@39003
   250
                   else the_problem_prefix ^ serial_string ())
blanchet@39003
   251
                  ^ "_" ^ string_of_int subgoal)
blanchet@39003
   252
    val problem_path_name =
blanchet@39003
   253
      if the_dest_dir = "" then
blanchet@39003
   254
        File.tmp_path problem_file_name
blanchet@39003
   255
      else if File.exists (Path.explode the_dest_dir) then
blanchet@39003
   256
        Path.append (Path.explode the_dest_dir) problem_file_name
blanchet@39003
   257
      else
blanchet@39003
   258
        error ("No such directory: " ^ quote the_dest_dir ^ ".")
blanchet@39003
   259
    val measure_run_time = verbose orelse Config.get ctxt measure_run_time
blanchet@38092
   260
    val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
blanchet@38023
   261
    (* write out problem file and call prover *)
blanchet@38645
   262
    fun command_line complete timeout probfile =
blanchet@38023
   263
      let
blanchet@38023
   264
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@38023
   265
                   " " ^ File.shell_path probfile
blanchet@38023
   266
      in
blanchet@39010
   267
        (if measure_run_time then "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
blanchet@38744
   268
         else "exec " ^ core) ^ " 2>&1"
blanchet@38023
   269
      end
blanchet@38023
   270
    fun split_time s =
blanchet@38023
   271
      let
blanchet@38023
   272
        val split = String.tokens (fn c => str c = "\n");
blanchet@38023
   273
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
blanchet@38023
   274
        fun as_num f = f >> (fst o read_int);
blanchet@38023
   275
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
blanchet@38023
   276
        val digit = Scan.one Symbol.is_ascii_digit;
blanchet@38023
   277
        val num3 = as_num (digit ::: digit ::: (digit >> single));
blanchet@38023
   278
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
blanchet@38023
   279
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
blanchet@38023
   280
      in (output, as_time t) end;
blanchet@38023
   281
    fun run_on probfile =
blanchet@38092
   282
      case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
blanchet@38032
   283
        (home_var, _) :: _ =>
blanchet@38023
   284
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@38032
   285
      | [] =>
blanchet@38032
   286
        if File.exists command then
blanchet@38032
   287
          let
blanchet@39318
   288
            fun run complete timeout =
blanchet@38032
   289
              let
blanchet@38645
   290
                val command = command_line complete timeout probfile
blanchet@38032
   291
                val ((output, msecs), res_code) =
blanchet@38032
   292
                  bash_output command
blanchet@38032
   293
                  |>> (if overlord then
blanchet@38032
   294
                         prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@38032
   295
                       else
blanchet@38032
   296
                         I)
blanchet@38744
   297
                  |>> (if measure_run_time then split_time else rpair 0)
blanchet@38032
   298
                val (proof, outcome) =
blanchet@38032
   299
                  extract_proof_and_outcome complete res_code proof_delims
blanchet@38032
   300
                                            known_failures output
blanchet@38032
   301
              in (output, msecs, proof, outcome) end
blanchet@38032
   302
            val readable_names = debug andalso overlord
blanchet@38282
   303
            val (problem, pool, conjecture_offset, axiom_names) =
blanchet@38282
   304
              prepare_problem ctxt readable_names explicit_forall full_types
blanchet@39005
   305
                              explicit_apply hyp_ts concl_t axioms
blanchet@38631
   306
            val ss = strings_for_tptp_problem use_conjecture_for_hypotheses
blanchet@38631
   307
                                              problem
blanchet@38631
   308
            val _ = File.write_list probfile ss
blanchet@38032
   309
            val conjecture_shape =
blanchet@38032
   310
              conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@38040
   311
              |> map single
blanchet@39318
   312
            val run_twice = has_incomplete_mode andalso not auto
blanchet@38645
   313
            val timer = Timer.startRealTimer ()
blanchet@38032
   314
            val result =
blanchet@39318
   315
              run false (if run_twice then
blanchet@39318
   316
                           Time.fromMilliseconds
blanchet@38645
   317
                                         (2 * Time.toMilliseconds timeout div 3)
blanchet@39318
   318
                         else
blanchet@39318
   319
                           timeout)
blanchet@39318
   320
              |> run_twice
blanchet@38645
   321
                 ? (fn (_, msecs0, _, SOME _) =>
blanchet@39318
   322
                       run true (Time.- (timeout, Timer.checkRealTimer timer))
blanchet@38645
   323
                       |> (fn (output, msecs, proof, outcome) =>
blanchet@38645
   324
                              (output, msecs0 + msecs, proof, outcome))
blanchet@38645
   325
                     | result => result)
blanchet@38282
   326
          in ((pool, conjecture_shape, axiom_names), result) end
blanchet@38032
   327
        else
blanchet@38032
   328
          error ("Bad executable: " ^ Path.implode command ^ ".")
blanchet@38023
   329
blanchet@38023
   330
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   331
       the proof file too. *)
blanchet@38023
   332
    fun cleanup probfile =
blanchet@38023
   333
      if the_dest_dir = "" then try File.rm probfile else NONE
blanchet@38023
   334
    fun export probfile (_, (output, _, _, _)) =
blanchet@38023
   335
      if the_dest_dir = "" then
blanchet@38023
   336
        ()
blanchet@38023
   337
      else
blanchet@38023
   338
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
blanchet@38282
   339
    val ((pool, conjecture_shape, axiom_names),
blanchet@38282
   340
         (output, msecs, proof, outcome)) =
blanchet@39003
   341
      with_path cleanup export run_on problem_path_name
blanchet@38282
   342
    val (conjecture_shape, axiom_names) =
blanchet@38023
   343
      repair_conjecture_shape_and_theorem_names output conjecture_shape
blanchet@38282
   344
                                                axiom_names
blanchet@39107
   345
    val important_message = extract_important_message output
blanchet@38023
   346
    val (message, used_thm_names) =
blanchet@38023
   347
      case outcome of
blanchet@38023
   348
        NONE =>
blanchet@38023
   349
        proof_text isar_proof
blanchet@38023
   350
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@38998
   351
            (full_types, minimize_command, proof, axiom_names, goal, subgoal)
blanchet@38744
   352
        |>> (fn message =>
blanchet@38744
   353
                message ^ (if verbose then
blanchet@39010
   354
                             "\nReal CPU time: " ^ string_of_int msecs ^ " ms."
blanchet@38744
   355
                           else
blanchet@39107
   356
                             "") ^
blanchet@39107
   357
                (if important_message <> "" then
blanchet@39107
   358
                   "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@39107
   359
                   important_message
blanchet@39107
   360
                 else
blanchet@39107
   361
                   ""))
blanchet@38597
   362
      | SOME failure => (string_for_failure failure, [])
blanchet@38023
   363
  in
blanchet@38023
   364
    {outcome = outcome, message = message, pool = pool,
blanchet@38023
   365
     used_thm_names = used_thm_names, atp_run_time_in_msecs = msecs,
blanchet@38282
   366
     output = output, proof = proof, axiom_names = axiom_names,
blanchet@38083
   367
     conjecture_shape = conjecture_shape}
blanchet@38023
   368
  end
blanchet@38023
   369
blanchet@39318
   370
fun get_prover_fun thy name = prover_fun false name (get_prover thy name)
blanchet@38023
   371
blanchet@39318
   372
fun run_prover (params as {blocking, verbose, max_relevant, timeout, expect,
blanchet@39318
   373
                           ...})
blanchet@39318
   374
               auto i n relevance_override minimize_command
blanchet@39318
   375
               (problem as {state, goal, axioms, ...})
blanchet@39110
   376
               (prover as {default_max_relevant, ...}, atp_name) =
blanchet@36379
   377
  let
blanchet@39318
   378
    val ctxt = Proof.context_of state
blanchet@37584
   379
    val birth_time = Time.now ()
blanchet@37584
   380
    val death_time = Time.+ (birth_time, timeout)
blanchet@39110
   381
    val max_relevant = the_default default_max_relevant max_relevant
blanchet@39110
   382
    val num_axioms = Int.min (length axioms, max_relevant)
blanchet@36379
   383
    val desc =
blanchet@39110
   384
      "ATP " ^ quote atp_name ^
blanchet@39110
   385
      (if verbose then
blanchet@39110
   386
         " with " ^ string_of_int num_axioms ^ " fact" ^ plural_s num_axioms
blanchet@39110
   387
       else
blanchet@39110
   388
         "") ^
blanchet@39110
   389
      " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
blanchet@38985
   390
      (if blocking then
blanchet@38985
   391
         ""
blanchet@38985
   392
       else
blanchet@38985
   393
         "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@39318
   394
    fun go () =
blanchet@38982
   395
      let
blanchet@38985
   396
        val (outcome_code, message) =
blanchet@39318
   397
          prover_fun auto atp_name prover params (minimize_command atp_name)
blanchet@39318
   398
                     problem
blanchet@38985
   399
          |> (fn {outcome, message, ...} =>
blanchet@38985
   400
                 (if is_some outcome then "none" else "some", message))
blanchet@38985
   401
          handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@38985
   402
               | exn => ("unknown", "Internal error:\n" ^
blanchet@38985
   403
                                    ML_Compiler.exn_message exn ^ "\n")
blanchet@39318
   404
        val _ =
blanchet@39318
   405
          if expect = "" orelse outcome_code = expect then
blanchet@39318
   406
            ()
blanchet@39318
   407
          else if blocking then
blanchet@39318
   408
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@39318
   409
          else
blanchet@39318
   410
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@39318
   411
      in (outcome_code = "some", message) end
blanchet@39318
   412
  in
blanchet@39318
   413
    if auto then
blanchet@39318
   414
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39318
   415
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@39318
   416
             Pretty.chunks [Pretty.str "", Pretty.mark Markup.hilite
blanchet@39318
   417
                 (Pretty.str ("Sledgehammer found a proof! " ^ message))]))
blanchet@38982
   418
      end
blanchet@39318
   419
    else if blocking then
blanchet@39318
   420
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39318
   421
        priority (desc ^ "\n" ^ message); (success, state)
blanchet@39318
   422
      end
blanchet@39318
   423
    else
blanchet@39318
   424
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@39318
   425
       (false, state))
blanchet@37584
   426
  end
wenzelm@28582
   427
blanchet@39318
   428
val auto_max_relevant_divisor = 2
blanchet@39318
   429
blanchet@39003
   430
fun run_sledgehammer (params as {blocking, verbose, atps, full_types,
blanchet@38998
   431
                                 relevance_thresholds, max_relevant, ...})
blanchet@39318
   432
                     auto i relevance_override minimize_command state =
blanchet@39318
   433
  if null atps then
blanchet@39318
   434
    error "No ATP is set."
blanchet@39318
   435
  else case subgoal_count state of
blanchet@39318
   436
    0 => (priority "No subgoal!"; (false, state))
blanchet@39318
   437
  | n =>
blanchet@39318
   438
    let
blanchet@39318
   439
      val thy = Proof.theory_of state
blanchet@39318
   440
      val timer = Timer.startRealTimer ()
blanchet@39318
   441
      val _ = () |> not blocking ? kill_atps
blanchet@39318
   442
      val _ = if auto then () else priority "Sledgehammering..."
blanchet@39318
   443
      val provers = map (`(get_prover thy)) atps
blanchet@39318
   444
      fun go () =
blanchet@39318
   445
        let
blanchet@39318
   446
          val {context = ctxt, facts = chained_ths, goal} = Proof.goal state
blanchet@39318
   447
          val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@39318
   448
          val max_max_relevant =
blanchet@39318
   449
            case max_relevant of
blanchet@39318
   450
              SOME n => n
blanchet@39318
   451
            | NONE =>
blanchet@39318
   452
              0 |> fold (Integer.max o #default_max_relevant o fst) provers
blanchet@39318
   453
                |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@39318
   454
          val axioms =
blanchet@39318
   455
            relevant_facts ctxt full_types relevance_thresholds
blanchet@39318
   456
                           max_max_relevant relevance_override chained_ths
blanchet@39318
   457
                           hyp_ts concl_t
blanchet@39318
   458
          val problem =
blanchet@39318
   459
            {state = state, goal = goal, subgoal = i,
blanchet@39318
   460
             axioms = map (prepare_axiom ctxt) axioms}
blanchet@39318
   461
          val run_prover =
blanchet@39318
   462
            run_prover params auto i n relevance_override minimize_command
blanchet@39318
   463
                       problem
blanchet@39318
   464
          val num_axioms = length axioms
blanchet@39318
   465
        in
blanchet@39318
   466
          if auto then
blanchet@39318
   467
            fold (fn prover => fn (true, state) => (true, state)
blanchet@39318
   468
                                | (false, _) => run_prover prover)
blanchet@39318
   469
                 provers (false, state)
blanchet@39318
   470
          else
blanchet@39318
   471
            (if blocking then Par_List.map else map) run_prover provers
blanchet@39318
   472
            |> tap (fn _ =>
blanchet@39318
   473
                       if verbose then
blanchet@39318
   474
                         priority ("Total time: " ^
blanchet@39318
   475
                                   signed_string_of_int (Time.toMilliseconds
blanchet@39318
   476
                                       (Timer.checkRealTimer timer)) ^ " ms.")
blanchet@39318
   477
                       else
blanchet@39318
   478
                         ())
blanchet@39318
   479
            |> exists fst |> rpair state
blanchet@39318
   480
        end
blanchet@39318
   481
    in if blocking then go () else Future.fork (tap go) |> K (false, state) end
blanchet@38044
   482
blanchet@38023
   483
val setup =
blanchet@38023
   484
  dest_dir_setup
blanchet@38023
   485
  #> problem_prefix_setup
blanchet@39003
   486
  #> measure_run_time_setup
blanchet@38023
   487
wenzelm@28582
   488
end;