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