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