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