src/HOL/Tools/Sledgehammer/sledgehammer.ML
author blanchet
Tue Oct 26 21:43:50 2010 +0200 (2010-10-26 ago)
changeset 40206 8819ffd60357
parent 40205 277508b07418
child 40207 5da3e8ede850
permissions -rw-r--r--
better list of irrelevant SMT constants
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@40204
    33
  datatype fact =
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@40204
    42
     facts: fact list,
blanchet@39366
    43
     only: bool}
blanchet@39493
    44
blanchet@40061
    45
  type prover_result =
blanchet@36370
    46
    {outcome: failure option,
blanchet@40204
    47
     used_facts: (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@40200
    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@40206
   118
blanchet@40206
   119
(* FIXME: check types *)
blanchet@40206
   120
val smt_irrelevant_consts =
blanchet@40206
   121
  atp_irrelevant_consts @
blanchet@40206
   122
  [@{const_name distinct},
blanchet@40206
   123
   (* numeral-related: *)
blanchet@40206
   124
   @{const_name number_of}, @{const_name Int.Pls}, @{const_name Int.Min},
blanchet@40206
   125
   @{const_name Int.Bit0}, @{const_name Int.Bit1}, @{const_name Suc},
blanchet@40206
   126
   (* FIXME: @{const_name Numeral0}, @{const_name Numeral1}, *)
blanchet@40206
   127
   (* int => nat *)
blanchet@40206
   128
   @{const_name nat},
blanchet@40206
   129
   (* nat => int *)
blanchet@40206
   130
   (* FIXME: @{const_name int}, *)
blanchet@40206
   131
   (* for "nat", "int", and "real": *)
blanchet@40206
   132
   @{const_name plus}, @{const_name uminus}, @{const_name minus},
blanchet@40206
   133
   @{const_name times}, @{const_name less}, @{const_name less_eq},
blanchet@40206
   134
   @{const_name abs}, @{const_name min}, @{const_name max},
blanchet@40206
   135
   (* for "nat" and "div": *)
blanchet@40206
   136
   @{const_name div}, @{const_name mod} (* , *)
blanchet@40206
   137
   (* for real: *)
blanchet@40206
   138
   (* FIXME: @{const_name "op /"} *)]
blanchet@40071
   139
blanchet@40071
   140
fun irrelevant_consts_for_prover name =
blanchet@40071
   141
  if is_smt_prover name then smt_irrelevant_consts else atp_irrelevant_consts
blanchet@40071
   142
blanchet@40070
   143
(* FUDGE *)
blanchet@40070
   144
val atp_relevance_fudge =
blanchet@40070
   145
  {worse_irrel_freq = 100.0,
blanchet@40070
   146
   higher_order_irrel_weight = 1.05,
blanchet@40070
   147
   abs_rel_weight = 0.5,
blanchet@40070
   148
   abs_irrel_weight = 2.0,
blanchet@40070
   149
   skolem_irrel_weight = 0.75,
blanchet@40070
   150
   theory_const_rel_weight = 0.5,
blanchet@40070
   151
   theory_const_irrel_weight = 0.25,
blanchet@40070
   152
   intro_bonus = 0.15,
blanchet@40070
   153
   elim_bonus = 0.15,
blanchet@40070
   154
   simp_bonus = 0.15,
blanchet@40070
   155
   local_bonus = 0.55,
blanchet@40070
   156
   assum_bonus = 1.05,
blanchet@40070
   157
   chained_bonus = 1.5,
blanchet@40070
   158
   max_imperfect = 11.5,
blanchet@40070
   159
   max_imperfect_exp = 1.0,
blanchet@40070
   160
   threshold_divisor = 2.0,
blanchet@40070
   161
   ridiculous_threshold = 0.1}
blanchet@40070
   162
blanchet@40071
   163
(* FUDGE (FIXME) *)
blanchet@40070
   164
val smt_relevance_fudge =
blanchet@40071
   165
  {worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
blanchet@40071
   166
   higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
blanchet@40071
   167
   abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
blanchet@40071
   168
   abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
blanchet@40071
   169
   skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
blanchet@40071
   170
   theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
blanchet@40071
   171
   theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
blanchet@40071
   172
   intro_bonus = #intro_bonus atp_relevance_fudge,
blanchet@40071
   173
   elim_bonus = #elim_bonus atp_relevance_fudge,
blanchet@40071
   174
   simp_bonus = #simp_bonus atp_relevance_fudge,
blanchet@40071
   175
   local_bonus = #local_bonus atp_relevance_fudge,
blanchet@40071
   176
   assum_bonus = #assum_bonus atp_relevance_fudge,
blanchet@40071
   177
   chained_bonus = #chained_bonus atp_relevance_fudge,
blanchet@40071
   178
   max_imperfect = #max_imperfect atp_relevance_fudge,
blanchet@40071
   179
   max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
blanchet@40071
   180
   threshold_divisor = #threshold_divisor atp_relevance_fudge,
blanchet@40071
   181
   ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
blanchet@40070
   182
blanchet@40070
   183
fun relevance_fudge_for_prover name =
blanchet@40070
   184
  if is_smt_prover name then smt_relevance_fudge else atp_relevance_fudge
blanchet@40070
   185
blanchet@40059
   186
fun available_provers thy =
blanchet@40060
   187
  let
blanchet@40060
   188
    val (remote_provers, local_provers) =
blanchet@40062
   189
      sort_strings (available_atps thy) @ smt_prover_names
blanchet@40060
   190
      |> List.partition (String.isPrefix remote_prefix)
blanchet@40060
   191
  in
blanchet@40205
   192
    Output.urgent_message ("Available provers: " ^
blanchet@40205
   193
                           commas (local_provers @ remote_provers) ^ ".")
blanchet@40060
   194
  end
blanchet@35969
   195
blanchet@40059
   196
fun kill_provers () = Async_Manager.kill_threads das_Tool "provers"
blanchet@40059
   197
fun running_provers () = Async_Manager.running_threads das_Tool "provers"
blanchet@40059
   198
val messages = Async_Manager.thread_messages das_Tool "prover"
blanchet@40059
   199
blanchet@40059
   200
(** problems, results, ATPs, etc. **)
blanchet@35969
   201
blanchet@35969
   202
type params =
blanchet@38982
   203
  {blocking: bool,
blanchet@38982
   204
   debug: bool,
blanchet@35969
   205
   verbose: bool,
blanchet@36143
   206
   overlord: bool,
blanchet@40059
   207
   provers: string list,
blanchet@35969
   208
   full_types: bool,
blanchet@36235
   209
   explicit_apply: bool,
blanchet@38745
   210
   relevance_thresholds: real * real,
blanchet@38744
   211
   max_relevant: int option,
blanchet@35969
   212
   isar_proof: bool,
blanchet@36924
   213
   isar_shrink_factor: int,
blanchet@38985
   214
   timeout: Time.time,
blanchet@38985
   215
   expect: string}
blanchet@35867
   216
blanchet@40204
   217
datatype fact =
blanchet@40114
   218
  Untranslated of (string * locality) * thm |
blanchet@40114
   219
  Translated of term * ((string * locality) * translated_formula) option
blanchet@40061
   220
blanchet@40061
   221
type prover_problem =
blanchet@39318
   222
  {state: Proof.state,
blanchet@38998
   223
   goal: thm,
blanchet@38998
   224
   subgoal: int,
blanchet@40065
   225
   subgoal_count: int,
blanchet@40204
   226
   facts: fact list,
blanchet@39366
   227
   only: bool}
blanchet@35867
   228
blanchet@40061
   229
type prover_result =
blanchet@36370
   230
  {outcome: failure option,
blanchet@35969
   231
   message: string,
blanchet@40204
   232
   used_facts: (string * locality) list,
blanchet@40062
   233
   run_time_in_msecs: int option}
blanchet@35867
   234
blanchet@40061
   235
type prover = params -> minimize_command -> prover_problem -> prover_result
blanchet@35867
   236
blanchet@38023
   237
(* configuration attributes *)
blanchet@38023
   238
blanchet@38991
   239
val (dest_dir, dest_dir_setup) =
blanchet@39003
   240
  Attrib.config_string "sledgehammer_dest_dir" (K "")
blanchet@38991
   241
  (* Empty string means create files in Isabelle's temporary files directory. *)
blanchet@38023
   242
blanchet@38023
   243
val (problem_prefix, problem_prefix_setup) =
blanchet@39003
   244
  Attrib.config_string "sledgehammer_problem_prefix" (K "prob")
blanchet@38023
   245
blanchet@39003
   246
val (measure_run_time, measure_run_time_setup) =
blanchet@39003
   247
  Attrib.config_bool "sledgehammer_measure_run_time" (K false)
wenzelm@28484
   248
blanchet@38023
   249
fun with_path cleanup after f path =
blanchet@38023
   250
  Exn.capture f path
blanchet@38023
   251
  |> tap (fn _ => cleanup path)
blanchet@38023
   252
  |> Exn.release
blanchet@38023
   253
  |> tap (after path)
blanchet@38023
   254
blanchet@40204
   255
fun prover_description ctxt ({blocking, verbose, ...} : params) name num_facts i
blanchet@40204
   256
                       n goal =
blanchet@40060
   257
  quote name ^
blanchet@40060
   258
  (if verbose then
blanchet@40204
   259
     " with " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts
blanchet@40060
   260
   else
blanchet@40060
   261
     "") ^
blanchet@40060
   262
  " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
blanchet@40060
   263
  (if blocking then
blanchet@40060
   264
     ""
blanchet@40060
   265
   else
blanchet@40060
   266
     "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@40060
   267
blanchet@40060
   268
fun proof_banner auto =
blanchet@40060
   269
  if auto then "Sledgehammer found a proof" else "Try this command"
blanchet@40060
   270
blanchet@40059
   271
(* generic TPTP-based ATPs *)
blanchet@38023
   272
blanchet@40114
   273
fun dest_Untranslated (Untranslated p) = p
blanchet@40114
   274
  | dest_Untranslated (Translated _) = raise Fail "dest_Untranslated"
blanchet@40204
   275
fun translated_fact ctxt (Untranslated p) = translate_fact ctxt p
blanchet@40204
   276
  | translated_fact _ (Translated p) = p
blanchet@40205
   277
fun fact_name (Untranslated ((name, _), _)) = SOME name
blanchet@40205
   278
  | fact_name (Translated (_, p)) = Option.map (fst o fst) p
blanchet@40061
   279
blanchet@40062
   280
fun int_option_add (SOME m) (SOME n) = SOME (m + n)
blanchet@40062
   281
  | int_option_add _ _ = NONE
blanchet@40062
   282
blanchet@39492
   283
(* Important messages are important but not so important that users want to see
blanchet@39492
   284
   them each time. *)
blanchet@39493
   285
val important_message_keep_factor = 0.1
blanchet@39492
   286
blanchet@40063
   287
fun run_atp auto atp_name
blanchet@38645
   288
        {exec, required_execs, arguments, has_incomplete_mode, proof_delims,
blanchet@38997
   289
         known_failures, default_max_relevant, explicit_forall,
blanchet@38997
   290
         use_conjecture_for_hypotheses}
blanchet@38455
   291
        ({debug, verbose, overlord, full_types, explicit_apply,
blanchet@38998
   292
          max_relevant, isar_proof, isar_shrink_factor, timeout, ...} : params)
blanchet@40061
   293
        minimize_command
blanchet@40204
   294
        ({state, goal, subgoal, facts, only, ...} : prover_problem) =
blanchet@38023
   295
  let
blanchet@39318
   296
    val ctxt = Proof.context_of state
blanchet@38998
   297
    val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal
blanchet@40204
   298
    val facts =
blanchet@40204
   299
      facts |> not only ? take (the_default default_max_relevant max_relevant)
blanchet@40204
   300
            |> map (translated_fact ctxt)
blanchet@40059
   301
    val dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
blanchet@40059
   302
                   else Config.get ctxt dest_dir
blanchet@40059
   303
    val problem_prefix = Config.get ctxt problem_prefix
blanchet@40061
   304
    val problem_file_name =
blanchet@39003
   305
      Path.basic ((if overlord then "prob_" ^ atp_name
blanchet@40059
   306
                   else problem_prefix ^ serial_string ())
blanchet@39003
   307
                  ^ "_" ^ string_of_int subgoal)
blanchet@40061
   308
    val problem_path_name =
blanchet@40059
   309
      if dest_dir = "" then
blanchet@40061
   310
        File.tmp_path problem_file_name
blanchet@40059
   311
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   312
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   313
      else
blanchet@40059
   314
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@39003
   315
    val measure_run_time = verbose orelse Config.get ctxt measure_run_time
blanchet@38092
   316
    val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
blanchet@40059
   317
    (* write out problem file and call ATP *)
blanchet@38645
   318
    fun command_line complete timeout probfile =
blanchet@38023
   319
      let
blanchet@38023
   320
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
blanchet@38023
   321
                   " " ^ File.shell_path probfile
blanchet@38023
   322
      in
blanchet@39010
   323
        (if measure_run_time then "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
blanchet@38744
   324
         else "exec " ^ core) ^ " 2>&1"
blanchet@38023
   325
      end
blanchet@38023
   326
    fun split_time s =
blanchet@38023
   327
      let
blanchet@38023
   328
        val split = String.tokens (fn c => str c = "\n");
blanchet@38023
   329
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
blanchet@38023
   330
        fun as_num f = f >> (fst o read_int);
blanchet@38023
   331
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
blanchet@38023
   332
        val digit = Scan.one Symbol.is_ascii_digit;
blanchet@38023
   333
        val num3 = as_num (digit ::: digit ::: (digit >> single));
blanchet@38023
   334
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
blanchet@40062
   335
        val as_time = Scan.read Symbol.stopper time o explode
blanchet@38023
   336
      in (output, as_time t) end;
blanchet@38023
   337
    fun run_on probfile =
blanchet@38092
   338
      case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
blanchet@38032
   339
        (home_var, _) :: _ =>
blanchet@38023
   340
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@38032
   341
      | [] =>
blanchet@38032
   342
        if File.exists command then
blanchet@38032
   343
          let
blanchet@39318
   344
            fun run complete timeout =
blanchet@38032
   345
              let
blanchet@38645
   346
                val command = command_line complete timeout probfile
blanchet@38032
   347
                val ((output, msecs), res_code) =
blanchet@38032
   348
                  bash_output command
blanchet@38032
   349
                  |>> (if overlord then
blanchet@38032
   350
                         prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@38032
   351
                       else
blanchet@38032
   352
                         I)
blanchet@40062
   353
                  |>> (if measure_run_time then split_time else rpair NONE)
blanchet@39452
   354
                val (tstplike_proof, outcome) =
blanchet@39452
   355
                  extract_tstplike_proof_and_outcome complete res_code
blanchet@39452
   356
                      proof_delims known_failures output
blanchet@39452
   357
              in (output, msecs, tstplike_proof, outcome) end
blanchet@38032
   358
            val readable_names = debug andalso overlord
blanchet@40204
   359
            val (atp_problem, pool, conjecture_offset, fact_names) =
blanchet@40059
   360
              prepare_atp_problem ctxt readable_names explicit_forall full_types
blanchet@40204
   361
                                  explicit_apply hyp_ts concl_t facts
blanchet@39452
   362
            val ss = tptp_strings_for_atp_problem use_conjecture_for_hypotheses
blanchet@40059
   363
                                                  atp_problem
blanchet@38631
   364
            val _ = File.write_list probfile ss
blanchet@38032
   365
            val conjecture_shape =
blanchet@38032
   366
              conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
blanchet@38040
   367
              |> map single
blanchet@39318
   368
            val run_twice = has_incomplete_mode andalso not auto
blanchet@38645
   369
            val timer = Timer.startRealTimer ()
blanchet@38032
   370
            val result =
blanchet@39318
   371
              run false (if run_twice then
blanchet@39318
   372
                           Time.fromMilliseconds
blanchet@38645
   373
                                         (2 * Time.toMilliseconds timeout div 3)
blanchet@39318
   374
                         else
blanchet@39318
   375
                           timeout)
blanchet@39318
   376
              |> run_twice
blanchet@38645
   377
                 ? (fn (_, msecs0, _, SOME _) =>
blanchet@39318
   378
                       run true (Time.- (timeout, Timer.checkRealTimer timer))
blanchet@39452
   379
                       |> (fn (output, msecs, tstplike_proof, outcome) =>
blanchet@40062
   380
                              (output, int_option_add msecs0 msecs,
blanchet@40062
   381
                               tstplike_proof, outcome))
blanchet@38645
   382
                     | result => result)
blanchet@40204
   383
          in ((pool, conjecture_shape, fact_names), result) end
blanchet@38032
   384
        else
blanchet@38032
   385
          error ("Bad executable: " ^ Path.implode command ^ ".")
blanchet@38023
   386
blanchet@38023
   387
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   388
       the proof file too. *)
blanchet@38023
   389
    fun cleanup probfile =
blanchet@40059
   390
      if dest_dir = "" then try File.rm probfile else NONE
blanchet@38023
   391
    fun export probfile (_, (output, _, _, _)) =
blanchet@40059
   392
      if dest_dir = "" then
blanchet@38023
   393
        ()
blanchet@38023
   394
      else
blanchet@38023
   395
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
blanchet@40204
   396
    val ((pool, conjecture_shape, fact_names),
blanchet@39452
   397
         (output, msecs, tstplike_proof, outcome)) =
blanchet@40061
   398
      with_path cleanup export run_on problem_path_name
blanchet@40204
   399
    val (conjecture_shape, fact_names) =
blanchet@40204
   400
      repair_conjecture_shape_and_fact_names output conjecture_shape fact_names
blanchet@39492
   401
    val important_message =
blanchet@39493
   402
      if random () <= important_message_keep_factor then
blanchet@39492
   403
        extract_important_message output
blanchet@39492
   404
      else
blanchet@39492
   405
        ""
blanchet@40204
   406
    val (message, used_facts) =
blanchet@38023
   407
      case outcome of
blanchet@38023
   408
        NONE =>
blanchet@38023
   409
        proof_text isar_proof
blanchet@38023
   410
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@40060
   411
            (proof_banner auto, full_types, minimize_command, tstplike_proof,
blanchet@40204
   412
             fact_names, goal, subgoal)
blanchet@38744
   413
        |>> (fn message =>
blanchet@38744
   414
                message ^ (if verbose then
blanchet@40062
   415
                             "\nATP real CPU time: " ^
blanchet@40062
   416
                             string_of_int (the msecs) ^ " ms."
blanchet@38744
   417
                           else
blanchet@39107
   418
                             "") ^
blanchet@39107
   419
                (if important_message <> "" then
blanchet@39107
   420
                   "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@39107
   421
                   important_message
blanchet@39107
   422
                 else
blanchet@39107
   423
                   ""))
blanchet@38597
   424
      | SOME failure => (string_for_failure failure, [])
blanchet@38023
   425
  in
blanchet@40204
   426
    {outcome = outcome, message = message, used_facts = used_facts,
blanchet@40061
   427
     run_time_in_msecs = msecs}
blanchet@38023
   428
  end
blanchet@38023
   429
blanchet@40181
   430
fun failure_from_smt_failure SMT_Solver.Time_Out = TimedOut
blanchet@40181
   431
  | failure_from_smt_failure (SMT_Solver.Counterexample _) = Unprovable
blanchet@40181
   432
  | failure_from_smt_failure (SMT_Solver.Other_Failure _) = UnknownError
blanchet@40063
   433
blanchet@40200
   434
fun run_smt_solver remote ({timeout, ...} : params) minimize_command
blanchet@40204
   435
                   ({state, subgoal, subgoal_count, facts, ...}
blanchet@40065
   436
                    : prover_problem) =
blanchet@36379
   437
  let
blanchet@40181
   438
    val {outcome, used_facts, run_time_in_msecs} =
blanchet@40181
   439
      SMT_Solver.smt_filter remote timeout state
blanchet@40204
   440
                            (map_filter (try dest_Untranslated) facts) subgoal
blanchet@40063
   441
    val message =
blanchet@40184
   442
      case outcome of
blanchet@40184
   443
        NONE =>
blanchet@40064
   444
        try_command_line (proof_banner false)
blanchet@40065
   445
                         (apply_on_subgoal subgoal subgoal_count ^
blanchet@40181
   446
                          command_call smtN (map fst used_facts)) ^
blanchet@40181
   447
        minimize_line minimize_command (map fst used_facts)
blanchet@40200
   448
      | SOME SMT_Solver.Time_Out => "Timed out."
blanchet@40200
   449
      | SOME (SMT_Solver.Counterexample _) => "The SMT problem is unprovable."
blanchet@40204
   450
      | SOME (SMT_Solver.Other_Failure message) => message
blanchet@40200
   451
    val outcome = outcome |> Option.map failure_from_smt_failure (* FIXME *)
blanchet@40063
   452
  in
blanchet@40204
   453
    {outcome = outcome, used_facts = used_facts,
blanchet@40204
   454
     run_time_in_msecs = run_time_in_msecs, message = message}
blanchet@40063
   455
  end
blanchet@40063
   456
blanchet@40200
   457
fun get_prover thy auto name =
blanchet@40200
   458
  if is_smt_prover name then run_smt_solver (String.isPrefix remote_prefix name)
blanchet@40200
   459
  else run_atp auto name (get_atp thy name)
blanchet@40063
   460
blanchet@40063
   461
fun run_prover (params as {blocking, debug, max_relevant, timeout, expect, ...})
blanchet@40065
   462
               auto minimize_command
blanchet@40204
   463
               (problem as {state, goal, subgoal, subgoal_count, facts, ...})
blanchet@40063
   464
               name =
blanchet@40063
   465
  let
blanchet@40063
   466
    val thy = Proof.theory_of state
blanchet@39318
   467
    val ctxt = Proof.context_of state
blanchet@37584
   468
    val birth_time = Time.now ()
blanchet@37584
   469
    val death_time = Time.+ (birth_time, timeout)
blanchet@40063
   470
    val max_relevant =
blanchet@40063
   471
      the_default (default_max_relevant_for_prover thy name) max_relevant
blanchet@40204
   472
    val num_facts = Int.min (length facts, max_relevant)
blanchet@40065
   473
    val desc =
blanchet@40204
   474
      prover_description ctxt params name num_facts subgoal subgoal_count goal
blanchet@39318
   475
    fun go () =
blanchet@38982
   476
      let
blanchet@39453
   477
        fun really_go () =
blanchet@40200
   478
          get_prover thy auto name params (minimize_command name) problem
blanchet@38985
   479
          |> (fn {outcome, message, ...} =>
blanchet@38985
   480
                 (if is_some outcome then "none" else "some", message))
blanchet@39453
   481
        val (outcome_code, message) =
blanchet@39453
   482
          if debug then
blanchet@39453
   483
            really_go ()
blanchet@39453
   484
          else
blanchet@39453
   485
            (really_go ()
blanchet@39453
   486
             handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@39453
   487
                  | exn => ("unknown", "Internal error:\n" ^
blanchet@39453
   488
                                       ML_Compiler.exn_message exn ^ "\n"))
blanchet@39318
   489
        val _ =
blanchet@39318
   490
          if expect = "" orelse outcome_code = expect then
blanchet@39318
   491
            ()
blanchet@39318
   492
          else if blocking then
blanchet@39318
   493
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@39318
   494
          else
blanchet@39318
   495
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@39318
   496
      in (outcome_code = "some", message) end
blanchet@39318
   497
  in
blanchet@39318
   498
    if auto then
blanchet@39318
   499
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@39318
   500
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@39318
   501
             Pretty.chunks [Pretty.str "", Pretty.mark Markup.hilite
blanchet@39327
   502
                 (Pretty.str message)]))
blanchet@38982
   503
      end
blanchet@39318
   504
    else if blocking then
blanchet@39318
   505
      let val (success, message) = TimeLimit.timeLimit timeout go () in
wenzelm@40132
   506
        List.app Output.urgent_message
blanchet@39370
   507
                 (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
blanchet@39370
   508
        (success, state)
blanchet@39318
   509
      end
blanchet@39318
   510
    else
blanchet@39318
   511
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@39318
   512
       (false, state))
blanchet@37584
   513
  end
wenzelm@28582
   514
blanchet@40205
   515
fun run_sledgehammer (params as {blocking, debug, provers, full_types,
blanchet@40069
   516
                                 relevance_thresholds, max_relevant, ...})
blanchet@39366
   517
                     auto i (relevance_override as {only, ...}) minimize_command
blanchet@39366
   518
                     state =
blanchet@40059
   519
  if null provers then
blanchet@40059
   520
    error "No prover is set."
blanchet@39318
   521
  else case subgoal_count state of
wenzelm@40132
   522
    0 => (Output.urgent_message "No subgoal!"; (false, state))
blanchet@39318
   523
  | n =>
blanchet@39318
   524
    let
blanchet@39364
   525
      val _ = Proof.assert_backward state
blanchet@39318
   526
      val thy = Proof.theory_of state
blanchet@40200
   527
      val ctxt = Proof.context_of state
blanchet@40200
   528
      val {facts = chained_ths, goal, ...} = Proof.goal state
blanchet@40060
   529
      val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@40059
   530
      val _ = () |> not blocking ? kill_provers
wenzelm@40132
   531
      val _ = if auto then () else Output.urgent_message "Sledgehammering..."
blanchet@40070
   532
      val (smts, atps) = provers |> List.partition is_smt_prover
blanchet@40205
   533
      fun run_provers label full_types irrelevant_consts relevance_fudge
blanchet@40114
   534
                      maybe_translate provers (res as (success, state)) =
blanchet@40065
   535
        if success orelse null provers then
blanchet@40060
   536
          res
blanchet@40060
   537
        else
blanchet@40060
   538
          let
blanchet@40060
   539
            val max_max_relevant =
blanchet@40060
   540
              case max_relevant of
blanchet@40060
   541
                SOME n => n
blanchet@40060
   542
              | NONE =>
blanchet@40063
   543
                0 |> fold (Integer.max o default_max_relevant_for_prover thy)
blanchet@40065
   544
                          provers
blanchet@40060
   545
                  |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@40204
   546
            val facts =
blanchet@40065
   547
              relevant_facts ctxt full_types relevance_thresholds
blanchet@40071
   548
                             max_max_relevant irrelevant_consts relevance_fudge
blanchet@40071
   549
                             relevance_override chained_ths hyp_ts concl_t
blanchet@40114
   550
              |> map maybe_translate
blanchet@40062
   551
            val problem =
blanchet@40065
   552
              {state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet@40204
   553
               facts = facts, only = only}
blanchet@40065
   554
            val run_prover = run_prover params auto minimize_command
blanchet@40060
   555
          in
blanchet@40205
   556
            if debug then
blanchet@40205
   557
              Output.urgent_message (label ^ ": " ^
blanchet@40205
   558
                  (if null facts then
blanchet@40205
   559
                     "Found no relevant facts."
blanchet@40205
   560
                   else
blanchet@40205
   561
                     "Including (up to) " ^ string_of_int (length facts) ^
blanchet@40205
   562
                     " relevant fact" ^ plural_s (length facts) ^ ":\n" ^
blanchet@40205
   563
                     (facts |> map_filter fact_name
blanchet@40205
   564
                            |> space_implode " ") ^ "."))
blanchet@40205
   565
            else
blanchet@40205
   566
              ();
blanchet@40060
   567
            if auto then
blanchet@40061
   568
              fold (fn prover => fn (true, state) => (true, state)
blanchet@40064
   569
                                  | (false, _) => run_prover problem prover)
blanchet@40065
   570
                   provers (false, state)
blanchet@40060
   571
            else
blanchet@40065
   572
              provers |> (if blocking then Par_List.map else map)
blanchet@40065
   573
                             (run_prover problem)
blanchet@40065
   574
                      |> exists fst |> rpair state
blanchet@40060
   575
          end
blanchet@40071
   576
      val run_atps =
blanchet@40205
   577
        run_provers "ATPs" full_types atp_irrelevant_consts atp_relevance_fudge
blanchet@40204
   578
                    (Translated o translate_fact ctxt) atps
blanchet@40071
   579
      val run_smts =
blanchet@40205
   580
        run_provers "SMT" true smt_irrelevant_consts smt_relevance_fudge
blanchet@40205
   581
                    Untranslated smts
blanchet@40060
   582
      fun run_atps_and_smt_solvers () =
blanchet@40065
   583
        [run_atps, run_smts] |> Par_List.map (fn f => f (false, state) |> K ())
blanchet@40060
   584
    in
blanchet@40065
   585
      (false, state)
blanchet@40065
   586
      |> (if blocking then run_atps #> not auto ? run_smts
blanchet@40065
   587
          else (fn p => Future.fork (tap run_atps_and_smt_solvers) |> K p))
blanchet@40060
   588
    end
blanchet@38044
   589
blanchet@38023
   590
val setup =
blanchet@38023
   591
  dest_dir_setup
blanchet@38023
   592
  #> problem_prefix_setup
blanchet@39003
   593
  #> measure_run_time_setup
blanchet@38023
   594
wenzelm@28582
   595
end;