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