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