src/HOL/Tools/Sledgehammer/sledgehammer.ML
author blanchet
Fri Oct 22 11:11:34 2010 +0200 (2010-10-22 ago)
changeset 40062 cfaebaa8588f
parent 40061 71cc5aac8b76
child 40063 d086e3699e78
permissions -rw-r--r--
make Sledgehammer minimizer fully work with SMT
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@39493
    16
blanchet@35969
    17
  type params =
blanchet@38982
    18
    {blocking: bool,
blanchet@38982
    19
     debug: bool,
blanchet@35969
    20
     verbose: bool,
blanchet@36143
    21
     overlord: bool,
blanchet@40059
    22
     provers: string list,
blanchet@35969
    23
     full_types: bool,
blanchet@36235
    24
     explicit_apply: bool,
blanchet@38745
    25
     relevance_thresholds: real * real,
blanchet@38744
    26
     max_relevant: int option,
blanchet@35969
    27
     isar_proof: bool,
blanchet@36924
    28
     isar_shrink_factor: int,
blanchet@38985
    29
     timeout: Time.time,
blanchet@38985
    30
     expect: string}
blanchet@39493
    31
blanchet@40061
    32
  datatype axiom =
blanchet@40061
    33
    Unprepared of (string * locality) * thm |
blanchet@40061
    34
    Prepared of term * ((string * locality) * fol_formula) option
blanchet@40061
    35
blanchet@40061
    36
  type prover_problem =
blanchet@39318
    37
    {state: Proof.state,
blanchet@38998
    38
     goal: thm,
blanchet@38998
    39
     subgoal: int,
blanchet@40061
    40
     axioms: axiom list,
blanchet@39366
    41
     only: bool}
blanchet@39493
    42
blanchet@40061
    43
  type prover_result =
blanchet@36370
    44
    {outcome: failure option,
blanchet@40061
    45
     used_axioms: (string * locality) list,
blanchet@40062
    46
     run_time_in_msecs: int option,
blanchet@40062
    47
     message: string}
blanchet@39493
    48
blanchet@40061
    49
  type prover = params -> minimize_command -> prover_problem -> prover_result
blanchet@35867
    50
blanchet@40060
    51
  val smtN : string
blanchet@40062
    52
  val smt_prover_names : string list
blanchet@40062
    53
  val smt_default_max_relevant : int
blanchet@38023
    54
  val dest_dir : string Config.T
blanchet@38023
    55
  val problem_prefix : string Config.T
blanchet@39003
    56
  val measure_run_time : bool Config.T
blanchet@40059
    57
  val available_provers : theory -> unit
blanchet@40059
    58
  val kill_provers : unit -> unit
blanchet@40059
    59
  val running_provers : unit -> unit
blanchet@40059
    60
  val messages : int option -> unit
blanchet@40062
    61
  val get_prover : theory -> string -> prover
blanchet@38044
    62
  val run_sledgehammer :
blanchet@39318
    63
    params -> bool -> int -> relevance_override -> (string -> minimize_command)
blanchet@39318
    64
    -> Proof.state -> bool * Proof.state
blanchet@38023
    65
  val setup : theory -> theory
wenzelm@28477
    66
end;
wenzelm@28477
    67
blanchet@38021
    68
structure Sledgehammer : SLEDGEHAMMER =
wenzelm@28477
    69
struct
wenzelm@28477
    70
blanchet@38028
    71
open ATP_Problem
blanchet@39491
    72
open ATP_Proof
blanchet@38028
    73
open ATP_Systems
blanchet@39494
    74
open Metis_Translate
blanchet@38023
    75
open Sledgehammer_Util
blanchet@38988
    76
open Sledgehammer_Filter
blanchet@38282
    77
open Sledgehammer_Translate
blanchet@38988
    78
open Sledgehammer_Reconstruct
blanchet@37583
    79
blanchet@38023
    80
blanchet@37583
    81
(** The Sledgehammer **)
blanchet@37583
    82
blanchet@38102
    83
(* Identifier to distinguish Sledgehammer from other tools using
blanchet@38102
    84
   "Async_Manager". *)
blanchet@37585
    85
val das_Tool = "Sledgehammer"
blanchet@37585
    86
blanchet@40060
    87
val smtN = "smt"
blanchet@40062
    88
val smt_prover_names = [smtN, remote_prefix ^ smtN]
blanchet@40062
    89
blanchet@40062
    90
val smt_default_max_relevant = 300 (* FUDGE *)
blanchet@40062
    91
val auto_max_relevant_divisor = 2 (* FUDGE *)
blanchet@40060
    92
blanchet@40059
    93
fun available_provers thy =
blanchet@40060
    94
  let
blanchet@40060
    95
    val (remote_provers, local_provers) =
blanchet@40062
    96
      sort_strings (available_atps thy) @ smt_prover_names
blanchet@40060
    97
      |> List.partition (String.isPrefix remote_prefix)
blanchet@40060
    98
  in
blanchet@40060
    99
    priority ("Available provers: " ^ commas (local_provers @ remote_provers) ^
blanchet@40060
   100
              ".")
blanchet@40060
   101
  end
blanchet@35969
   102
blanchet@40059
   103
fun kill_provers () = Async_Manager.kill_threads das_Tool "provers"
blanchet@40059
   104
fun running_provers () = Async_Manager.running_threads das_Tool "provers"
blanchet@40059
   105
val messages = Async_Manager.thread_messages das_Tool "prover"
blanchet@40059
   106
blanchet@40059
   107
(** problems, results, ATPs, etc. **)
blanchet@35969
   108
blanchet@35969
   109
type params =
blanchet@38982
   110
  {blocking: bool,
blanchet@38982
   111
   debug: bool,
blanchet@35969
   112
   verbose: bool,
blanchet@36143
   113
   overlord: bool,
blanchet@40059
   114
   provers: string list,
blanchet@35969
   115
   full_types: bool,
blanchet@36235
   116
   explicit_apply: bool,
blanchet@38745
   117
   relevance_thresholds: real * real,
blanchet@38744
   118
   max_relevant: int option,
blanchet@35969
   119
   isar_proof: bool,
blanchet@36924
   120
   isar_shrink_factor: int,
blanchet@38985
   121
   timeout: Time.time,
blanchet@38985
   122
   expect: string}
blanchet@35867
   123
blanchet@40061
   124
datatype axiom =
blanchet@40061
   125
  Unprepared of (string * locality) * thm |
blanchet@40061
   126
  Prepared of term * ((string * locality) * fol_formula) option
blanchet@40061
   127
blanchet@40061
   128
type prover_problem =
blanchet@39318
   129
  {state: Proof.state,
blanchet@38998
   130
   goal: thm,
blanchet@38998
   131
   subgoal: int,
blanchet@40061
   132
   axioms: axiom list,
blanchet@39366
   133
   only: bool}
blanchet@35867
   134
blanchet@40061
   135
type prover_result =
blanchet@36370
   136
  {outcome: failure option,
blanchet@35969
   137
   message: string,
blanchet@40061
   138
   used_axioms: (string * locality) list,
blanchet@40062
   139
   run_time_in_msecs: int option}
blanchet@35867
   140
blanchet@40061
   141
type prover = params -> minimize_command -> prover_problem -> prover_result
blanchet@35867
   142
blanchet@38023
   143
(* configuration attributes *)
blanchet@38023
   144
blanchet@38991
   145
val (dest_dir, dest_dir_setup) =
blanchet@39003
   146
  Attrib.config_string "sledgehammer_dest_dir" (K "")
blanchet@38991
   147
  (* Empty string means create files in Isabelle's temporary files directory. *)
blanchet@38023
   148
blanchet@38023
   149
val (problem_prefix, problem_prefix_setup) =
blanchet@39003
   150
  Attrib.config_string "sledgehammer_problem_prefix" (K "prob")
blanchet@38023
   151
blanchet@39003
   152
val (measure_run_time, measure_run_time_setup) =
blanchet@39003
   153
  Attrib.config_bool "sledgehammer_measure_run_time" (K false)
wenzelm@28484
   154
blanchet@38023
   155
fun with_path cleanup after f path =
blanchet@38023
   156
  Exn.capture f path
blanchet@38023
   157
  |> tap (fn _ => cleanup path)
blanchet@38023
   158
  |> Exn.release
blanchet@38023
   159
  |> tap (after path)
blanchet@38023
   160
blanchet@40060
   161
fun prover_description ctxt ({blocking, verbose, ...} : params) name num_axioms
blanchet@40060
   162
                       i n goal =
blanchet@40060
   163
  quote name ^
blanchet@40060
   164
  (if verbose then
blanchet@40060
   165
     " with " ^ string_of_int num_axioms ^ " fact" ^ plural_s num_axioms
blanchet@40060
   166
   else
blanchet@40060
   167
     "") ^
blanchet@40060
   168
  " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
blanchet@40060
   169
  (if blocking then
blanchet@40060
   170
     ""
blanchet@40060
   171
   else
blanchet@40060
   172
     "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@40060
   173
blanchet@40060
   174
fun proof_banner auto =
blanchet@40060
   175
  if auto then "Sledgehammer found a proof" else "Try this command"
blanchet@40060
   176
blanchet@40059
   177
(* generic TPTP-based ATPs *)
blanchet@38023
   178
blanchet@40061
   179
fun dest_Unprepared (Unprepared p) = p
blanchet@40061
   180
  | dest_Unprepared (Prepared _) = raise Fail "dest_Unprepared"
blanchet@40061
   181
fun prepared_axiom ctxt (Unprepared p) = prepare_axiom ctxt p
blanchet@40061
   182
  | prepared_axiom _ (Prepared p) = p
blanchet@40061
   183
blanchet@40062
   184
fun int_option_add (SOME m) (SOME n) = SOME (m + n)
blanchet@40062
   185
  | int_option_add _ _ = NONE
blanchet@40062
   186
blanchet@39492
   187
(* Important messages are important but not so important that users want to see
blanchet@39492
   188
   them each time. *)
blanchet@39493
   189
val important_message_keep_factor = 0.1
blanchet@39492
   190
blanchet@40059
   191
fun atp_fun auto atp_name
blanchet@38645
   192
        {exec, required_execs, arguments, has_incomplete_mode, proof_delims,
blanchet@38997
   193
         known_failures, default_max_relevant, explicit_forall,
blanchet@38997
   194
         use_conjecture_for_hypotheses}
blanchet@38455
   195
        ({debug, verbose, overlord, full_types, explicit_apply,
blanchet@38998
   196
          max_relevant, isar_proof, isar_shrink_factor, timeout, ...} : params)
blanchet@40061
   197
        minimize_command
blanchet@40061
   198
        ({state, goal, subgoal, axioms, only} : prover_problem) =
blanchet@38023
   199
  let
blanchet@39318
   200
    val ctxt = Proof.context_of state
blanchet@38998
   201
    val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal
blanchet@40061
   202
    val axioms =
blanchet@40061
   203
      axioms |> not only ? take (the_default default_max_relevant max_relevant)
blanchet@40061
   204
             |> map (prepared_axiom ctxt)
blanchet@40059
   205
    val dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@40059
   206
                   else Config.get ctxt dest_dir
blanchet@40059
   207
    val problem_prefix = Config.get ctxt problem_prefix
blanchet@40061
   208
    val problem_file_name =
blanchet@39003
   209
      Path.basic ((if overlord then "prob_" ^ atp_name
blanchet@40059
   210
                   else problem_prefix ^ serial_string ())
blanchet@39003
   211
                  ^ "_" ^ string_of_int subgoal)
blanchet@40061
   212
    val problem_path_name =
blanchet@40059
   213
      if dest_dir = "" then
blanchet@40061
   214
        File.tmp_path problem_file_name
blanchet@40059
   215
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   216
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   217
      else
blanchet@40059
   218
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@39003
   219
    val measure_run_time = verbose orelse Config.get ctxt measure_run_time
blanchet@38092
   220
    val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
blanchet@40059
   221
    (* write out problem file and call ATP *)
blanchet@38645
   222
    fun command_line complete timeout probfile =
blanchet@38023
   223
      let
blanchet@38023
   224
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@38023
   225
                   " " ^ File.shell_path probfile
blanchet@38023
   226
      in
blanchet@39010
   227
        (if measure_run_time then "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
blanchet@38744
   228
         else "exec " ^ core) ^ " 2>&1"
blanchet@38023
   229
      end
blanchet@38023
   230
    fun split_time s =
blanchet@38023
   231
      let
blanchet@38023
   232
        val split = String.tokens (fn c => str c = "\n");
blanchet@38023
   233
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
blanchet@38023
   234
        fun as_num f = f >> (fst o read_int);
blanchet@38023
   235
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
blanchet@38023
   236
        val digit = Scan.one Symbol.is_ascii_digit;
blanchet@38023
   237
        val num3 = as_num (digit ::: digit ::: (digit >> single));
blanchet@38023
   238
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
blanchet@40062
   239
        val as_time = Scan.read Symbol.stopper time o explode
blanchet@38023
   240
      in (output, as_time t) end;
blanchet@38023
   241
    fun run_on probfile =
blanchet@38092
   242
      case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
blanchet@38032
   243
        (home_var, _) :: _ =>
blanchet@38023
   244
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@38032
   245
      | [] =>
blanchet@38032
   246
        if File.exists command then
blanchet@38032
   247
          let
blanchet@39318
   248
            fun run complete timeout =
blanchet@38032
   249
              let
blanchet@38645
   250
                val command = command_line complete timeout probfile
blanchet@38032
   251
                val ((output, msecs), res_code) =
blanchet@38032
   252
                  bash_output command
blanchet@38032
   253
                  |>> (if overlord then
blanchet@38032
   254
                         prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@38032
   255
                       else
blanchet@38032
   256
                         I)
blanchet@40062
   257
                  |>> (if measure_run_time then split_time else rpair NONE)
blanchet@39452
   258
                val (tstplike_proof, outcome) =
blanchet@39452
   259
                  extract_tstplike_proof_and_outcome complete res_code
blanchet@39452
   260
                      proof_delims known_failures output
blanchet@39452
   261
              in (output, msecs, tstplike_proof, outcome) end
blanchet@38032
   262
            val readable_names = debug andalso overlord
blanchet@40059
   263
            val (atp_problem, pool, conjecture_offset, axiom_names) =
blanchet@40059
   264
              prepare_atp_problem ctxt readable_names explicit_forall full_types
blanchet@40059
   265
                                  explicit_apply hyp_ts concl_t axioms
blanchet@39452
   266
            val ss = tptp_strings_for_atp_problem use_conjecture_for_hypotheses
blanchet@40059
   267
                                                  atp_problem
blanchet@38631
   268
            val _ = File.write_list probfile ss
blanchet@38032
   269
            val conjecture_shape =
blanchet@38032
   270
              conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@38040
   271
              |> map single
blanchet@39318
   272
            val run_twice = has_incomplete_mode andalso not auto
blanchet@38645
   273
            val timer = Timer.startRealTimer ()
blanchet@38032
   274
            val result =
blanchet@39318
   275
              run false (if run_twice then
blanchet@39318
   276
                           Time.fromMilliseconds
blanchet@38645
   277
                                         (2 * Time.toMilliseconds timeout div 3)
blanchet@39318
   278
                         else
blanchet@39318
   279
                           timeout)
blanchet@39318
   280
              |> run_twice
blanchet@38645
   281
                 ? (fn (_, msecs0, _, SOME _) =>
blanchet@39318
   282
                       run true (Time.- (timeout, Timer.checkRealTimer timer))
blanchet@39452
   283
                       |> (fn (output, msecs, tstplike_proof, outcome) =>
blanchet@40062
   284
                              (output, int_option_add msecs0 msecs,
blanchet@40062
   285
                               tstplike_proof, outcome))
blanchet@38645
   286
                     | result => result)
blanchet@38282
   287
          in ((pool, conjecture_shape, axiom_names), result) end
blanchet@38032
   288
        else
blanchet@38032
   289
          error ("Bad executable: " ^ Path.implode command ^ ".")
blanchet@38023
   290
blanchet@38023
   291
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   292
       the proof file too. *)
blanchet@38023
   293
    fun cleanup probfile =
blanchet@40059
   294
      if dest_dir = "" then try File.rm probfile else NONE
blanchet@38023
   295
    fun export probfile (_, (output, _, _, _)) =
blanchet@40059
   296
      if dest_dir = "" then
blanchet@38023
   297
        ()
blanchet@38023
   298
      else
blanchet@38023
   299
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
blanchet@38282
   300
    val ((pool, conjecture_shape, axiom_names),
blanchet@39452
   301
         (output, msecs, tstplike_proof, outcome)) =
blanchet@40061
   302
      with_path cleanup export run_on problem_path_name
blanchet@38282
   303
    val (conjecture_shape, axiom_names) =
blanchet@39493
   304
      repair_conjecture_shape_and_axiom_names output conjecture_shape
blanchet@39493
   305
                                              axiom_names
blanchet@39492
   306
    val important_message =
blanchet@39493
   307
      if random () <= important_message_keep_factor then
blanchet@39492
   308
        extract_important_message output
blanchet@39492
   309
      else
blanchet@39492
   310
        ""
blanchet@40061
   311
    val (message, used_axioms) =
blanchet@38023
   312
      case outcome of
blanchet@38023
   313
        NONE =>
blanchet@38023
   314
        proof_text isar_proof
blanchet@38023
   315
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@40060
   316
            (proof_banner auto, full_types, minimize_command, tstplike_proof,
blanchet@40060
   317
             axiom_names, goal, subgoal)
blanchet@38744
   318
        |>> (fn message =>
blanchet@38744
   319
                message ^ (if verbose then
blanchet@40062
   320
                             "\nATP real CPU time: " ^
blanchet@40062
   321
                             string_of_int (the msecs) ^ " ms."
blanchet@38744
   322
                           else
blanchet@39107
   323
                             "") ^
blanchet@39107
   324
                (if important_message <> "" then
blanchet@39107
   325
                   "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@39107
   326
                   important_message
blanchet@39107
   327
                 else
blanchet@39107
   328
                   ""))
blanchet@38597
   329
      | SOME failure => (string_for_failure failure, [])
blanchet@38023
   330
  in
blanchet@40061
   331
    {outcome = outcome, message = message, used_axioms = used_axioms,
blanchet@40061
   332
     run_time_in_msecs = msecs}
blanchet@38023
   333
  end
blanchet@38023
   334
blanchet@40062
   335
fun get_atp_as_prover thy name = atp_fun false name (get_atp thy name)
blanchet@38023
   336
blanchet@40060
   337
fun run_atp_somehow (params as {blocking, debug, max_relevant, timeout,
blanchet@40060
   338
                                expect, ...})
blanchet@40060
   339
                    auto i n minimize_command
blanchet@40062
   340
                    (problem as {state, goal, axioms, ...})
blanchet@40061
   341
                    (prover as {default_max_relevant, ...}, atp_name) =
blanchet@36379
   342
  let
blanchet@39318
   343
    val ctxt = Proof.context_of state
blanchet@37584
   344
    val birth_time = Time.now ()
blanchet@37584
   345
    val death_time = Time.+ (birth_time, timeout)
blanchet@39110
   346
    val max_relevant = the_default default_max_relevant max_relevant
blanchet@39110
   347
    val num_axioms = Int.min (length axioms, max_relevant)
blanchet@40060
   348
    val desc = prover_description ctxt params atp_name num_axioms i n goal
blanchet@39318
   349
    fun go () =
blanchet@38982
   350
      let
blanchet@39453
   351
        fun really_go () =
blanchet@40061
   352
          atp_fun auto atp_name prover params (minimize_command atp_name)
blanchet@40062
   353
                  problem
blanchet@38985
   354
          |> (fn {outcome, message, ...} =>
blanchet@38985
   355
                 (if is_some outcome then "none" else "some", message))
blanchet@39453
   356
        val (outcome_code, message) =
blanchet@39453
   357
          if debug then
blanchet@39453
   358
            really_go ()
blanchet@39453
   359
          else
blanchet@39453
   360
            (really_go ()
blanchet@39453
   361
             handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@39453
   362
                  | exn => ("unknown", "Internal error:\n" ^
blanchet@39453
   363
                                       ML_Compiler.exn_message exn ^ "\n"))
blanchet@39318
   364
        val _ =
blanchet@39318
   365
          if expect = "" orelse outcome_code = expect then
blanchet@39318
   366
            ()
blanchet@39318
   367
          else if blocking then
blanchet@39318
   368
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@39318
   369
          else
blanchet@39318
   370
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@39318
   371
      in (outcome_code = "some", message) end
blanchet@39318
   372
  in
blanchet@39318
   373
    if auto then
blanchet@39318
   374
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39318
   375
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@39318
   376
             Pretty.chunks [Pretty.str "", Pretty.mark Markup.hilite
blanchet@39327
   377
                 (Pretty.str message)]))
blanchet@38982
   378
      end
blanchet@39318
   379
    else if blocking then
blanchet@39318
   380
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39370
   381
        List.app priority
blanchet@39370
   382
                 (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
blanchet@39370
   383
        (success, state)
blanchet@39318
   384
      end
blanchet@39318
   385
    else
blanchet@39318
   386
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@39318
   387
       (false, state))
blanchet@37584
   388
  end
wenzelm@28582
   389
blanchet@40060
   390
(* FIXME: dummy *)
blanchet@40062
   391
fun run_smt_solver remote timeout state axioms i =
blanchet@40062
   392
  {outcome = NONE, used_axioms = axioms |> take 5 |> map fst,
blanchet@40062
   393
   run_time_in_msecs = NONE}
blanchet@40061
   394
blanchet@40062
   395
fun get_smt_solver_as_prover remote ({timeout, ...} : params) minimize_command
blanchet@40062
   396
                             ({state, subgoal, axioms, ...} : prover_problem) =
blanchet@40062
   397
  let
blanchet@40062
   398
    val {outcome, used_axioms, run_time_in_msecs} =
blanchet@40062
   399
      run_smt_solver remote timeout state
blanchet@40061
   400
                     (map_filter (try dest_Unprepared) axioms) subgoal
blanchet@40062
   401
    val message =
blanchet@40062
   402
      if outcome = NONE then
blanchet@40062
   403
        sendback_line (proof_banner false)
blanchet@40062
   404
                      (apply_on_subgoal subgoal (subgoal_count state) ^
blanchet@40062
   405
                       command_call smtN (map fst used_axioms))
blanchet@40062
   406
      else
blanchet@40062
   407
        ""
blanchet@40062
   408
  in
blanchet@40062
   409
    {outcome = outcome, used_axioms = used_axioms,
blanchet@40062
   410
     run_time_in_msecs = run_time_in_msecs, message = message}
blanchet@40062
   411
  end
blanchet@40061
   412
blanchet@40062
   413
fun get_prover thy name =
blanchet@40062
   414
  if member (op =) smt_prover_names name then
blanchet@40062
   415
    get_smt_solver_as_prover (String.isPrefix remote_prefix)
blanchet@40062
   416
  else
blanchet@40062
   417
    get_atp_as_prover thy name
blanchet@40062
   418
blanchet@40062
   419
fun run_smt_solver_somehow state params minimize_command i n goal axioms
blanchet@40060
   420
                           (remote, name) =
blanchet@40060
   421
  let
blanchet@40061
   422
    val ctxt = Proof.context_of state
blanchet@40061
   423
    val desc = prover_description ctxt params name (length axioms) i n goal
blanchet@40062
   424
    val problem =
blanchet@40062
   425
      {state = state, goal = goal, subgoal = i,
blanchet@40062
   426
       axioms = axioms |> map Unprepared, only = true}
blanchet@40062
   427
    val {outcome, used_axioms, message, ...} =
blanchet@40062
   428
      get_smt_solver_as_prover remote params minimize_command problem
blanchet@40062
   429
    val _ =
blanchet@40062
   430
      priority (das_Tool ^ ": " ^ desc ^ "\n" ^
blanchet@40062
   431
                (if outcome = NONE then message
blanchet@40062
   432
                 else "An unknown error occurred."))
blanchet@40062
   433
  in outcome = NONE end
blanchet@39318
   434
blanchet@40059
   435
fun run_sledgehammer (params as {blocking, provers, full_types,
blanchet@40060
   436
                                 relevance_thresholds, max_relevant, timeout,
blanchet@40060
   437
                                 ...})
blanchet@39366
   438
                     auto i (relevance_override as {only, ...}) minimize_command
blanchet@39366
   439
                     state =
blanchet@40059
   440
  if null provers then
blanchet@40059
   441
    error "No prover is set."
blanchet@39318
   442
  else case subgoal_count state of
blanchet@39318
   443
    0 => (priority "No subgoal!"; (false, state))
blanchet@39318
   444
  | n =>
blanchet@39318
   445
    let
blanchet@39364
   446
      val _ = Proof.assert_backward state
blanchet@39318
   447
      val thy = Proof.theory_of state
blanchet@40060
   448
      val {context = ctxt, facts = chained_ths, goal} = Proof.goal state
blanchet@40060
   449
      val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@40059
   450
      val _ = () |> not blocking ? kill_provers
blanchet@39318
   451
      val _ = if auto then () else priority "Sledgehammering..."
blanchet@40060
   452
      val (smts, atps) =
blanchet@40062
   453
        provers |> List.partition (member (op =) smt_prover_names)
blanchet@40060
   454
                |>> (take 1 #> map (`(String.isPrefix remote_prefix)))
blanchet@40060
   455
                ||> map (`(get_atp thy))
blanchet@40060
   456
      fun run_atps (res as (success, state)) =
blanchet@40060
   457
        if success orelse null atps then
blanchet@40060
   458
          res
blanchet@40060
   459
        else
blanchet@40060
   460
          let
blanchet@40060
   461
            val max_max_relevant =
blanchet@40060
   462
              case max_relevant of
blanchet@40060
   463
                SOME n => n
blanchet@40060
   464
              | NONE =>
blanchet@40060
   465
                0 |> fold (Integer.max o #default_max_relevant o fst) atps
blanchet@40060
   466
                  |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@40060
   467
            val axioms =
blanchet@40060
   468
              relevant_facts ctxt full_types relevance_thresholds
blanchet@40060
   469
                             max_max_relevant relevance_override chained_ths
blanchet@40060
   470
                             hyp_ts concl_t
blanchet@40062
   471
            val problem =
blanchet@40060
   472
              {state = state, goal = goal, subgoal = i,
blanchet@40061
   473
               axioms = axioms |> map (Prepared o prepare_axiom ctxt),
blanchet@40061
   474
               only = only}
blanchet@40060
   475
            val run_atp_somehow =
blanchet@40062
   476
              run_atp_somehow params auto i n minimize_command problem
blanchet@40060
   477
          in
blanchet@40060
   478
            if auto then
blanchet@40061
   479
              fold (fn prover => fn (true, state) => (true, state)
blanchet@40061
   480
                                  | (false, _) => run_atp_somehow prover)
blanchet@40060
   481
                   atps (false, state)
blanchet@40060
   482
            else
blanchet@40060
   483
              atps |> (if blocking then Par_List.map else map) run_atp_somehow
blanchet@40060
   484
                   |> exists fst |> rpair state
blanchet@40060
   485
          end
blanchet@40060
   486
      fun run_smt_solvers (res as (success, state)) =
blanchet@40060
   487
        if success orelse null smts then
blanchet@40060
   488
          res
blanchet@40060
   489
        else
blanchet@40060
   490
          let
blanchet@40060
   491
            val max_relevant =
blanchet@40060
   492
              max_relevant |> the_default smt_default_max_relevant
blanchet@40060
   493
            val axioms =
blanchet@40060
   494
              relevant_facts ctxt full_types relevance_thresholds
blanchet@40060
   495
                             max_relevant relevance_override chained_ths
blanchet@40060
   496
                             hyp_ts concl_t
blanchet@40060
   497
          in
blanchet@40062
   498
            smts |> map (run_smt_solver_somehow state params minimize_command i
blanchet@40062
   499
                                                n goal axioms)
blanchet@40060
   500
                 |> exists I |> rpair state
blanchet@40060
   501
          end
blanchet@40060
   502
      fun run_atps_and_smt_solvers () =
blanchet@40060
   503
        [run_atps, run_smt_solvers]
blanchet@40060
   504
        |> Par_List.map (fn f => f (false, state) |> K ())
blanchet@40060
   505
    in
blanchet@40060
   506
      if blocking then
blanchet@40060
   507
        (false, state) |> run_atps |> not auto ? run_smt_solvers
blanchet@40060
   508
      else
blanchet@40060
   509
        Future.fork (tap run_atps_and_smt_solvers) |> K (false, state)
blanchet@40060
   510
    end
blanchet@38044
   511
blanchet@38023
   512
val setup =
blanchet@38023
   513
  dest_dir_setup
blanchet@38023
   514
  #> problem_prefix_setup
blanchet@39003
   515
  #> measure_run_time_setup
blanchet@38023
   516
wenzelm@28582
   517
end;