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