src/HOL/Tools/Sledgehammer/sledgehammer_commands.ML
author blanchet
Fri Mar 14 11:15:46 2014 +0100 (2014-03-14 ago)
changeset 56128 c106ac2ff76d
parent 56081 72fad75baf7e
child 56333 38f1422ef473
permissions -rw-r--r--
undo rewrite rules (e.g. for 'fun_app') in Isar
blanchet@55198
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_commands.ML
blanchet@35866
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@35866
     3
blanchet@35866
     4
Adds "sledgehammer" and related commands to Isabelle/Isar's outer syntax.
blanchet@35866
     5
*)
blanchet@35866
     6
blanchet@55198
     7
signature SLEDGEHAMMER_COMMANDS =
blanchet@35963
     8
sig
blanchet@55201
     9
  type params = Sledgehammer_Prover.params
blanchet@35963
    10
blanchet@40059
    11
  val provers : string Unsynchronized.ref
blanchet@55205
    12
  val default_params : theory -> (string * string) list -> params
blanchet@35963
    13
end;
blanchet@35963
    14
blanchet@55198
    15
structure Sledgehammer_Commands : SLEDGEHAMMER_COMMANDS =
blanchet@35866
    16
struct
blanchet@35866
    17
blanchet@44784
    18
open ATP_Util
blanchet@38028
    19
open ATP_Systems
blanchet@46320
    20
open ATP_Problem_Generate
blanchet@46320
    21
open ATP_Proof_Reconstruct
blanchet@35971
    22
open Sledgehammer_Util
blanchet@48250
    23
open Sledgehammer_Fact
blanchet@55201
    24
open Sledgehammer_Prover
blanchet@55475
    25
open Sledgehammer_Prover_SMT
blanchet@55202
    26
open Sledgehammer_Prover_Minimize
blanchet@48381
    27
open Sledgehammer_MaSh
blanchet@55202
    28
open Sledgehammer
blanchet@35866
    29
blanchet@48433
    30
(* val cvc3N = "cvc3" *)
blanchet@48405
    31
val yicesN = "yices"
blanchet@56081
    32
val z3_newN = "z3_new"
blanchet@48405
    33
blanchet@43020
    34
val runN = "run"
blanchet@43020
    35
val minN = "min"
blanchet@43020
    36
val messagesN = "messages"
blanchet@43020
    37
val supported_proversN = "supported_provers"
blanchet@50719
    38
val kill_allN = "kill_all"
blanchet@43020
    39
val running_proversN = "running_provers"
blanchet@48319
    40
val running_learnersN = "running_learners"
blanchet@43020
    41
val refresh_tptpN = "refresh_tptp"
blanchet@48301
    42
blanchet@39318
    43
val _ =
wenzelm@52639
    44
  ProofGeneral.preference_option ProofGeneral.category_tracing
wenzelm@52017
    45
    NONE
wenzelm@52639
    46
    @{option auto_sledgehammer}
wenzelm@52017
    47
    "auto-sledgehammer"
wenzelm@52017
    48
    "Run Sledgehammer automatically"
blanchet@39318
    49
blanchet@36394
    50
(** Sledgehammer commands **)
blanchet@36394
    51
blanchet@48292
    52
fun add_fact_override ns : fact_override = {add = ns, del = [], only = false}
blanchet@48292
    53
fun del_fact_override ns : fact_override = {add = [], del = ns, only = false}
blanchet@48292
    54
fun only_fact_override ns : fact_override = {add = ns, del = [], only = true}
blanchet@48292
    55
fun merge_fact_override_pairwise (r1 : fact_override) (r2 : fact_override) =
blanchet@35966
    56
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    57
   only = #only r1 andalso #only r2}
blanchet@48292
    58
fun merge_fact_overrides rs =
blanchet@48292
    59
  fold merge_fact_override_pairwise rs (only_fact_override [])
blanchet@35965
    60
blanchet@36371
    61
(*** parameters ***)
blanchet@36371
    62
blanchet@40059
    63
val provers = Unsynchronized.ref ""
blanchet@36371
    64
blanchet@36371
    65
val _ =
wenzelm@52007
    66
  ProofGeneral.preference_string ProofGeneral.category_proof
wenzelm@52017
    67
    NONE
wenzelm@52007
    68
    provers
wenzelm@52007
    69
    "Sledgehammer: Provers"
wenzelm@52007
    70
    "Default automatic provers (separated by whitespace)"
blanchet@36371
    71
blanchet@36371
    72
val _ =
wenzelm@53057
    73
  ProofGeneral.preference_option ProofGeneral.category_proof
wenzelm@52017
    74
    NONE
wenzelm@53057
    75
    @{option sledgehammer_timeout}
wenzelm@52007
    76
    "Sledgehammer: Time Limit"
wenzelm@52007
    77
    "ATPs will be interrupted after this time (in seconds)"
blanchet@36371
    78
blanchet@35963
    79
type raw_param = string * string list
blanchet@35963
    80
blanchet@35963
    81
val default_default_params =
blanchet@41208
    82
  [("debug", "false"),
blanchet@35963
    83
   ("verbose", "false"),
blanchet@36143
    84
   ("overlord", "false"),
blanchet@53800
    85
   ("spy", "false"),
blanchet@41208
    86
   ("blocking", "false"),
blanchet@43626
    87
   ("type_enc", "smart"),
blanchet@46301
    88
   ("strict", "false"),
blanchet@45514
    89
   ("lam_trans", "smart"),
blanchet@46409
    90
   ("uncurried_aliases", "smart"),
blanchet@48321
    91
   ("learn", "true"),
blanchet@48314
    92
   ("fact_filter", "smart"),
blanchet@48314
    93
   ("max_facts", "smart"),
blanchet@48293
    94
   ("fact_thresholds", "0.45 0.85"),
blanchet@47962
    95
   ("max_mono_iters", "smart"),
blanchet@47962
    96
   ("max_new_mono_instances", "smart"),
blanchet@51190
    97
   ("isar_proofs", "smart"),
blanchet@55183
    98
   ("compress_isar", "10"),
blanchet@55183
    99
   ("try0_isar", "true"),
blanchet@55297
   100
   ("smt_proofs", "smart"),
blanchet@45706
   101
   ("slice", "true"),
blanchet@45707
   102
   ("minimize", "smart"),
blanchet@55277
   103
   ("preplay_timeout", "2")]
blanchet@35963
   104
blanchet@36140
   105
val alias_params =
blanchet@51138
   106
  [("prover", ("provers", [])), (* undocumented *)
blanchet@51189
   107
   ("dont_preplay", ("preplay_timeout", ["0"])),
blanchet@55183
   108
   ("dont_compress_isar", ("compress_isar", ["0"])),
blanchet@52093
   109
   ("isar_proof", ("isar_proofs", [])) (* legacy *)]
blanchet@36140
   110
val negated_alias_params =
blanchet@41208
   111
  [("no_debug", "debug"),
blanchet@35963
   112
   ("quiet", "verbose"),
blanchet@36143
   113
   ("no_overlord", "overlord"),
blanchet@53800
   114
   ("dont_spy", "spy"),
blanchet@41208
   115
   ("non_blocking", "blocking"),
blanchet@46301
   116
   ("non_strict", "strict"),
blanchet@46409
   117
   ("no_uncurried_aliases", "uncurried_aliases"),
blanchet@48321
   118
   ("dont_learn", "learn"),
blanchet@49918
   119
   ("no_isar_proofs", "isar_proofs"),
blanchet@45707
   120
   ("dont_slice", "slice"),
smolkas@51879
   121
   ("dont_minimize", "minimize"),
blanchet@55288
   122
   ("dont_try0_isar", "try0_isar"),
blanchet@55297
   123
   ("no_smt_proofs", "smt_proofs")]
blanchet@35963
   124
blanchet@53758
   125
val params_not_for_minimize =
blanchet@54816
   126
  ["blocking", "fact_filter", "max_facts", "fact_thresholds", "slice", "minimize"];
blanchet@36281
   127
blanchet@43569
   128
val property_dependent_params = ["provers", "timeout"]
blanchet@35866
   129
blanchet@35963
   130
fun is_known_raw_param s =
blanchet@35963
   131
  AList.defined (op =) default_default_params s orelse
blanchet@36140
   132
  AList.defined (op =) alias_params s orelse
blanchet@36140
   133
  AList.defined (op =) negated_alias_params s orelse
blanchet@38985
   134
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35963
   135
blanchet@41258
   136
fun is_prover_list ctxt s =
blanchet@55286
   137
  (case space_explode " " s of
blanchet@41727
   138
    ss as _ :: _ => forall (is_prover_supported ctxt) ss
blanchet@55286
   139
  | _ => false)
blanchet@41258
   140
blanchet@36140
   141
fun unalias_raw_param (name, value) =
blanchet@55286
   142
  (case AList.lookup (op =) alias_params name of
blanchet@47037
   143
    SOME (name', []) => (name', value)
blanchet@47037
   144
  | SOME (param' as (name', _)) =>
blanchet@47037
   145
    if value <> ["false"] then
blanchet@47037
   146
      param'
blanchet@47037
   147
    else
blanchet@47037
   148
      error ("Parameter " ^ quote name ^ " cannot be set to \"false\" \
blanchet@47037
   149
             \(cf. " ^ quote name' ^ ").")
blanchet@36140
   150
  | NONE =>
blanchet@55286
   151
    (case AList.lookup (op =) negated_alias_params name of
blanchet@55286
   152
      SOME name' => (name',
blanchet@55286
   153
        (case value of
blanchet@55286
   154
          ["false"] => ["true"]
blanchet@55286
   155
        | ["true"] => ["false"]
blanchet@55286
   156
        | [] => ["false"]
blanchet@55286
   157
        | _ => value))
blanchet@55286
   158
    | NONE => (name, value)))
blanchet@35963
   159
blanchet@52031
   160
val any_type_enc = type_enc_of_string Strict "erased"
blanchet@45514
   161
blanchet@48397
   162
(* "provers =", "type_enc =", "lam_trans =", "fact_filter =", and "max_facts ="
blanchet@48397
   163
   can be omitted. For the last four, this is a secret feature. *)
blanchet@44720
   164
fun normalize_raw_param ctxt =
blanchet@44720
   165
  unalias_raw_param
blanchet@44720
   166
  #> (fn (name, value) =>
blanchet@44720
   167
         if is_known_raw_param name then
blanchet@44720
   168
           (name, value)
blanchet@48533
   169
         else if null value then
blanchet@48533
   170
           if is_prover_list ctxt name then
blanchet@48533
   171
             ("provers", [name])
blanchet@52031
   172
           else if can (type_enc_of_string Strict) name then
blanchet@48533
   173
             ("type_enc", [name])
blanchet@52031
   174
           else if can (trans_lams_of_string ctxt any_type_enc) name then
blanchet@48533
   175
             ("lam_trans", [name])
blanchet@48533
   176
           else if member (op =) fact_filters name then
blanchet@48533
   177
             ("fact_filter", [name])
blanchet@48533
   178
           else if is_some (Int.fromString name) then
blanchet@48533
   179
             ("max_facts", [name])
blanchet@48533
   180
           else
blanchet@48533
   181
             error ("Unknown parameter: " ^ quote name ^ ".")
blanchet@48400
   182
         else
blanchet@48400
   183
           error ("Unknown parameter: " ^ quote name ^ "."))
blanchet@44720
   184
blanchet@46435
   185
(* Ensures that type encodings such as "mono_native?" and "poly_guards!!" are
blanchet@44785
   186
   read correctly. *)
blanchet@44784
   187
val implode_param = strip_spaces_except_between_idents o space_implode " "
blanchet@43009
   188
blanchet@54059
   189
(* FIXME: use "Generic_Data" *)
wenzelm@41472
   190
structure Data = Theory_Data
wenzelm@41472
   191
(
blanchet@35963
   192
  type T = raw_param list
blanchet@54546
   193
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   194
  val extend = I
wenzelm@41472
   195
  fun merge data : T = AList.merge (op =) (K true) data
wenzelm@41472
   196
)
blanchet@35866
   197
blanchet@55475
   198
fun remotify_prover_if_supported_and_not_already_remote ctxt name =
blanchet@55475
   199
  if String.isPrefix remote_prefix name then
blanchet@55475
   200
    SOME name
blanchet@55475
   201
  else
blanchet@55475
   202
    let val remote_name = remote_prefix ^ name in
blanchet@55475
   203
      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
blanchet@55475
   204
    end
blanchet@55475
   205
blanchet@55475
   206
fun remotify_prover_if_not_installed ctxt name =
blanchet@55475
   207
  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
blanchet@55475
   208
    SOME name
blanchet@55475
   209
  else
blanchet@55475
   210
    remotify_prover_if_supported_and_not_already_remote ctxt name
blanchet@55475
   211
blanchet@40059
   212
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@48405
   213
   timeout, it makes sense to put E first. *)
blanchet@55475
   214
fun default_provers_param_value mode ctxt =
blanchet@56081
   215
  [eN, spassN, z3_newN, e_sineN, vampireN, yicesN]
blanchet@55475
   216
  |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@54059
   217
  (* In "try" mode, leave at least one thread to another slow tool (e.g. Nitpick) *)
blanchet@54059
   218
  |> take (Multithreading.max_threads_value () - (if mode = Try then 1 else 0))
blanchet@42776
   219
  |> implode_param
blanchet@40059
   220
blanchet@44720
   221
fun set_default_raw_param param thy =
blanchet@44720
   222
  let val ctxt = Proof_Context.init_global thy in
blanchet@44720
   223
    thy |> Data.map (AList.update (op =) (normalize_raw_param ctxt param))
blanchet@44720
   224
  end
blanchet@54059
   225
blanchet@55205
   226
fun default_raw_params mode thy =
blanchet@55475
   227
  let val ctxt = Proof_Context.init_global thy in
blanchet@55475
   228
    Data.get thy
blanchet@55475
   229
    |> fold (AList.default (op =))
blanchet@55475
   230
         [("provers", [(case !provers of "" => default_provers_param_value mode ctxt | s => s)]),
blanchet@55475
   231
          ("timeout",
blanchet@55475
   232
           let val timeout = Options.default_int @{option sledgehammer_timeout} in
blanchet@55475
   233
             [if timeout <= 0 then "none" else string_of_int timeout]
blanchet@55475
   234
           end)]
blanchet@55475
   235
  end
blanchet@35963
   236
blanchet@43021
   237
fun extract_params mode default_params override_params =
blanchet@35963
   238
  let
blanchet@35963
   239
    val raw_params = rev override_params @ rev default_params
blanchet@42776
   240
    val lookup = Option.map implode_param o AList.lookup (op =) raw_params
blanchet@35963
   241
    val lookup_string = the_default "" o lookup
blanchet@35963
   242
    fun general_lookup_bool option default_value name =
blanchet@54816
   243
      (case lookup name of
blanchet@35970
   244
        SOME s => parse_bool_option option name s
blanchet@54816
   245
      | NONE => default_value)
blanchet@35963
   246
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   247
    fun lookup_time name =
blanchet@54816
   248
      (case lookup name of
blanchet@54816
   249
        SOME s => parse_time name s
blanchet@54816
   250
      | NONE => Time.zeroTime)
blanchet@35966
   251
    fun lookup_int name =
blanchet@54816
   252
      (case lookup name of
blanchet@35966
   253
        NONE => 0
blanchet@54816
   254
      | SOME s =>
blanchet@54816
   255
        (case Int.fromString s of
blanchet@54816
   256
          SOME n => n
blanchet@54816
   257
        | NONE => error ("Parameter " ^ quote name ^ " must be assigned an integer value.")))
blanchet@49918
   258
    fun lookup_real name =
blanchet@54816
   259
      (case lookup name of
blanchet@49918
   260
        NONE => 0.0
blanchet@54816
   261
      | SOME s =>
blanchet@54816
   262
        (case Real.fromString s of
blanchet@54816
   263
          SOME n => n
blanchet@54816
   264
        | NONE => error ("Parameter " ^ quote name ^ " must be assigned a real value.")))
blanchet@40343
   265
    fun lookup_real_pair name =
blanchet@54816
   266
      (case lookup name of
blanchet@40343
   267
        NONE => (0.0, 0.0)
blanchet@54816
   268
      | SOME s =>
blanchet@54816
   269
        (case s |> space_explode " " |> map Real.fromString of
blanchet@54816
   270
          [SOME r1, SOME r2] => (r1, r2)
blanchet@54816
   271
        | _ => error ("Parameter " ^ quote name ^ " must be assigned a pair of floating-point \
blanchet@54816
   272
                 \values (e.g., \"0.6 0.95\")")))
blanchet@43064
   273
    fun lookup_option lookup' name =
blanchet@54816
   274
      (case lookup name of
blanchet@38589
   275
        SOME "smart" => NONE
blanchet@54816
   276
      | _ => SOME (lookup' name))
blanchet@43021
   277
    val debug = mode <> Auto_Try andalso lookup_bool "debug"
blanchet@43021
   278
    val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose")
blanchet@36143
   279
    val overlord = lookup_bool "overlord"
blanchet@54546
   280
    val spy = getenv "SLEDGEHAMMER_SPY" = "yes" orelse lookup_bool "spy"
blanchet@42995
   281
    val blocking =
blanchet@43021
   282
      Isabelle_Process.is_active () orelse mode <> Normal orelse debug orelse
blanchet@42995
   283
      lookup_bool "blocking"
blanchet@40059
   284
    val provers = lookup_string "provers" |> space_explode " "
blanchet@43021
   285
                  |> mode = Auto_Try ? single o hd
blanchet@43626
   286
    val type_enc =
blanchet@43021
   287
      if mode = Auto_Try then
blanchet@43569
   288
        NONE
blanchet@55286
   289
      else
blanchet@55286
   290
        (case lookup_string "type_enc" of
blanchet@55286
   291
          "smart" => NONE
blanchet@55286
   292
        | s => (type_enc_of_string Strict s; SOME s))
blanchet@46301
   293
    val strict = mode = Auto_Try orelse lookup_bool "strict"
blanchet@45520
   294
    val lam_trans = lookup_option lookup_string "lam_trans"
blanchet@46409
   295
    val uncurried_aliases = lookup_option lookup_bool "uncurried_aliases"
blanchet@48321
   296
    val learn = lookup_bool "learn"
blanchet@54113
   297
    val fact_filter =
blanchet@54113
   298
      lookup_option lookup_string "fact_filter"
blanchet@54113
   299
      |> mode = Auto_Try ? (fn NONE => SOME mepoN | sf => sf)
blanchet@48314
   300
    val max_facts = lookup_option lookup_int "max_facts"
blanchet@48293
   301
    val fact_thresholds = lookup_real_pair "fact_thresholds"
blanchet@47962
   302
    val max_mono_iters = lookup_option lookup_int "max_mono_iters"
blanchet@47962
   303
    val max_new_mono_instances =
blanchet@47962
   304
      lookup_option lookup_int "max_new_mono_instances"
blanchet@51190
   305
    val isar_proofs = lookup_option lookup_bool "isar_proofs"
blanchet@55183
   306
    val compress_isar = Real.max (1.0, lookup_real "compress_isar")
blanchet@55183
   307
    val try0_isar = lookup_bool "try0_isar"
blanchet@55297
   308
    val smt_proofs = lookup_option lookup_bool "smt_proofs"
blanchet@45706
   309
    val slice = mode <> Auto_Try andalso lookup_bool "slice"
blanchet@54113
   310
    val minimize = if mode = Auto_Try then NONE else lookup_option lookup_bool "minimize"
blanchet@54816
   311
    val timeout = lookup_time "timeout"
blanchet@54816
   312
    val preplay_timeout = if mode = Auto_Try then Time.zeroTime else lookup_time "preplay_timeout"
blanchet@38985
   313
    val expect = lookup_string "expect"
blanchet@35963
   314
  in
blanchet@53800
   315
    {debug = debug, verbose = verbose, overlord = overlord, spy = spy, blocking = blocking,
blanchet@53800
   316
     provers = provers, type_enc = type_enc, strict = strict, lam_trans = lam_trans,
blanchet@53800
   317
     uncurried_aliases = uncurried_aliases, learn = learn, fact_filter = fact_filter,
blanchet@53800
   318
     max_facts = max_facts, fact_thresholds = fact_thresholds, max_mono_iters = max_mono_iters,
blanchet@49918
   319
     max_new_mono_instances = max_new_mono_instances, isar_proofs = isar_proofs,
blanchet@55297
   320
     compress_isar = compress_isar, try0_isar = try0_isar, smt_proofs = smt_proofs, slice = slice,
blanchet@55288
   321
     minimize = minimize, timeout = timeout, preplay_timeout = preplay_timeout, expect = expect}
blanchet@35963
   322
  end
blanchet@35866
   323
blanchet@54059
   324
fun get_params mode = extract_params mode o default_raw_params mode
blanchet@43021
   325
fun default_params thy = get_params Normal thy o map (apsnd single)
blanchet@35866
   326
blanchet@36373
   327
(* Sledgehammer the given subgoal *)
blanchet@36373
   328
blanchet@45519
   329
val default_minimize_prover = metisN
blanchet@43051
   330
blanchet@44720
   331
fun is_raw_param_relevant_for_minimize (name, _) =
blanchet@53758
   332
  not (member (op =) params_not_for_minimize name)
blanchet@51998
   333
fun string_of_raw_param (key, values) =
blanchet@42776
   334
  key ^ (case implode_param values of "" => "" | value => " = " ^ value)
blanchet@51998
   335
fun nice_string_of_raw_param (p as (key, ["false"])) =
blanchet@50748
   336
    (case AList.find (op =) negated_alias_params key of
blanchet@50748
   337
       [neg] => neg
blanchet@51998
   338
     | _ => string_of_raw_param p)
blanchet@51998
   339
  | nice_string_of_raw_param p = string_of_raw_param p
blanchet@36281
   340
blanchet@45520
   341
fun minimize_command override_params i more_override_params prover_name
blanchet@45520
   342
                     fact_names =
blanchet@43057
   343
  let
blanchet@43057
   344
    val params =
blanchet@45520
   345
      (override_params
blanchet@45520
   346
       |> filter_out (AList.defined (op =) more_override_params o fst)) @
blanchet@45520
   347
      more_override_params
blanchet@43057
   348
      |> filter is_raw_param_relevant_for_minimize
blanchet@51998
   349
      |> map nice_string_of_raw_param
blanchet@43057
   350
      |> (if prover_name = default_minimize_prover then I else cons prover_name)
blanchet@43057
   351
      |> space_implode ", "
blanchet@43057
   352
  in
blanchet@48392
   353
    sledgehammerN ^ " " ^ minN ^
blanchet@43057
   354
    (if params = "" then "" else enclose " [" "]" params) ^
blanchet@43057
   355
    " (" ^ space_implode " " fact_names ^ ")" ^
blanchet@43057
   356
    (if i = 1 then "" else " " ^ string_of_int i)
blanchet@43057
   357
  end
blanchet@36281
   358
blanchet@50604
   359
val default_learn_prover_timeout = 2.0
blanchet@48392
   360
blanchet@48292
   361
fun hammer_away override_params subcommand opt_i fact_override state =
blanchet@35963
   362
  let
blanchet@55205
   363
    val thy = Proof.theory_of state
blanchet@40069
   364
    val ctxt = Proof.context_of state
blanchet@55205
   365
blanchet@44720
   366
    val override_params = override_params |> map (normalize_raw_param ctxt)
blanchet@50052
   367
    val _ = Isabelle_System.mkdir (Path.explode (getenv "ISABELLE_TMP"))
blanchet@35965
   368
  in
blanchet@35965
   369
    if subcommand = runN then
blanchet@36281
   370
      let val i = the_default 1 opt_i in
blanchet@55205
   371
        run_sledgehammer (get_params Normal thy override_params) Normal NONE i fact_override
blanchet@55205
   372
          (minimize_command override_params i) state
blanchet@39318
   373
        |> K ()
blanchet@36281
   374
      end
blanchet@43008
   375
    else if subcommand = minN then
blanchet@48384
   376
      let
blanchet@48395
   377
        val ctxt = ctxt |> Config.put instantiate_inducts false
blanchet@48384
   378
        val i = the_default 1 opt_i
blanchet@48400
   379
        val params =
blanchet@55205
   380
          get_params Minimize thy (("provers", [default_minimize_prover]) :: override_params)
blanchet@48384
   381
        val goal = prop_of (#goal (Proof.goal state))
blanchet@48384
   382
        val facts = nearly_all_facts ctxt false fact_override Symtab.empty
blanchet@48384
   383
                                     Termtab.empty [] [] goal
blanchet@54503
   384
        val learn = mash_learn_proof ctxt params goal facts
blanchet@48399
   385
      in run_minimize params learn i (#add fact_override) state end
blanchet@35965
   386
    else if subcommand = messagesN then
blanchet@35965
   387
      messages opt_i
blanchet@41727
   388
    else if subcommand = supported_proversN then
blanchet@41727
   389
      supported_provers ctxt
blanchet@50719
   390
    else if subcommand = kill_allN then
blanchet@53148
   391
      (kill_provers ();
blanchet@55205
   392
       kill_learners ctxt (get_params Normal thy override_params))
blanchet@48319
   393
    else if subcommand = running_proversN then
blanchet@48319
   394
      running_provers ()
blanchet@48383
   395
    else if subcommand = unlearnN then
blanchet@55205
   396
      mash_unlearn ctxt (get_params Normal thy override_params)
blanchet@50484
   397
    else if subcommand = learn_isarN orelse subcommand = learn_proverN orelse
blanchet@50484
   398
            subcommand = relearn_isarN orelse subcommand = relearn_proverN then
blanchet@50484
   399
      (if subcommand = relearn_isarN orelse subcommand = relearn_proverN then
blanchet@55205
   400
         mash_unlearn ctxt (get_params Normal thy override_params)
blanchet@48392
   401
       else
blanchet@48392
   402
         ();
blanchet@50484
   403
       mash_learn ctxt
blanchet@54129
   404
           (* TODO: Use MaSh mode instead and have the special defaults hardcoded in "get_params" *)
blanchet@55205
   405
           (get_params Normal thy
blanchet@55205
   406
                ([("timeout", [string_of_real default_learn_prover_timeout]),
blanchet@50484
   407
                  ("slice", ["false"])] @
blanchet@50484
   408
                 override_params @
blanchet@50484
   409
                 [("minimize", ["true"]),
blanchet@50484
   410
                  ("preplay_timeout", ["0"])]))
blanchet@50484
   411
           fact_override (#facts (Proof.goal state))
blanchet@50484
   412
           (subcommand = learn_proverN orelse subcommand = relearn_proverN))
blanchet@48319
   413
    else if subcommand = running_learnersN then
blanchet@48319
   414
      running_learners ()
blanchet@35965
   415
    else if subcommand = refresh_tptpN then
blanchet@35965
   416
      refresh_systems_on_tptp ()
blanchet@35965
   417
    else
blanchet@35965
   418
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   419
  end
blanchet@35963
   420
blanchet@48292
   421
fun sledgehammer_trans (((subcommand, params), fact_override), opt_i) =
wenzelm@51557
   422
  Toplevel.unknown_proof o
blanchet@48292
   423
  Toplevel.keep (hammer_away params subcommand opt_i fact_override
blanchet@35966
   424
                 o Toplevel.proof_of)
blanchet@35963
   425
blanchet@51998
   426
fun string_of_raw_param (name, value) = name ^ " = " ^ implode_param value
blanchet@35963
   427
blanchet@35965
   428
fun sledgehammer_params_trans params =
blanchet@35963
   429
  Toplevel.theory
blanchet@55205
   430
    (fold set_default_raw_param params
blanchet@55205
   431
     #> tap (fn thy =>
blanchet@55205
   432
          writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@55205
   433
            (case rev (default_raw_params Normal thy) of
blanchet@55205
   434
              [] => "none"
blanchet@55205
   435
            | params => params |> map string_of_raw_param |> sort_strings |> cat_lines))))
blanchet@35963
   436
wenzelm@46949
   437
val parse_query_bang = @{keyword "?"} || @{keyword "!"} || @{keyword "!!"}
blanchet@42776
   438
val parse_key =
blanchet@44768
   439
  Scan.repeat1 (Parse.typ_group || parse_query_bang) >> implode_param
blanchet@42581
   440
val parse_value =
blanchet@44768
   441
  Scan.repeat1 (Parse.xname || Parse.float_number || parse_query_bang)
wenzelm@46949
   442
val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) []
wenzelm@36960
   443
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35965
   444
val parse_fact_refs =
blanchet@38996
   445
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@48293
   446
val parse_fact_override_chunk =
blanchet@48292
   447
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_fact_override)
blanchet@48292
   448
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_fact_override)
blanchet@48292
   449
  || (parse_fact_refs >> only_fact_override)
blanchet@48292
   450
val parse_fact_override =
blanchet@48293
   451
  Scan.optional (Args.parens (Scan.repeat parse_fact_override_chunk
blanchet@48292
   452
                              >> merge_fact_overrides))
blanchet@48292
   453
                no_fact_override
blanchet@35963
   454
blanchet@35963
   455
val _ =
wenzelm@46961
   456
  Outer_Syntax.improper_command @{command_spec "sledgehammer"}
wenzelm@46961
   457
    "search for first-order proof using automatic theorem provers"
wenzelm@46961
   458
    ((Scan.optional Parse.short_ident runN -- parse_params
blanchet@48292
   459
      -- parse_fact_override -- Scan.option Parse.nat) #>> sledgehammer_trans)
blanchet@35963
   460
val _ =
wenzelm@46961
   461
  Outer_Syntax.command @{command_spec "sledgehammer_params"}
wenzelm@46961
   462
    "set and display the default parameters for Sledgehammer"
wenzelm@46961
   463
    (parse_params #>> sledgehammer_params_trans)
blanchet@36394
   464
blanchet@43020
   465
fun try_sledgehammer auto state =
blanchet@43020
   466
  let
blanchet@55205
   467
    val thy = Proof.theory_of state
blanchet@43021
   468
    val mode = if auto then Auto_Try else Try
blanchet@43020
   469
    val i = 1
blanchet@43020
   470
  in
blanchet@55205
   471
    run_sledgehammer (get_params mode thy []) mode NONE i no_fact_override (minimize_command [] i)
blanchet@55205
   472
      state
blanchet@40931
   473
  end
blanchet@39318
   474
wenzelm@52641
   475
val _ = Try.tool_setup (sledgehammerN, (40, @{option auto_sledgehammer}, try_sledgehammer))
blanchet@39318
   476
wenzelm@52908
   477
val _ =
wenzelm@52982
   478
  Query_Operation.register sledgehammerN (fn {state = st, args, output_result} =>
wenzelm@52982
   479
    (case try Toplevel.proof_of st of
wenzelm@52982
   480
      SOME state =>
wenzelm@52982
   481
        let
blanchet@55205
   482
          val thy = Proof.theory_of state
wenzelm@52982
   483
          val ctxt = Proof.context_of state
blanchet@55205
   484
          val [provers_arg, isar_proofs_arg] = args
wenzelm@52908
   485
wenzelm@52982
   486
          val override_params =
wenzelm@53053
   487
            ((if provers_arg = "" then [] else [("provers", space_explode " " provers_arg)]) @
wenzelm@53057
   488
              [("isar_proofs", [isar_proofs_arg]),
wenzelm@53053
   489
               ("blocking", ["true"]),
wenzelm@53053
   490
               ("minimize", ["true"]),
wenzelm@53053
   491
               ("debug", ["false"]),
wenzelm@53053
   492
               ("verbose", ["false"]),
wenzelm@53053
   493
               ("overlord", ["false"])])
wenzelm@52982
   494
            |> map (normalize_raw_param ctxt)
blanchet@55205
   495
blanchet@55205
   496
          val _ = run_sledgehammer (get_params Normal thy override_params) Normal
blanchet@55205
   497
            (SOME output_result) 1 no_fact_override (minimize_command override_params 1) state
wenzelm@52982
   498
        in () end
wenzelm@53055
   499
    | NONE => error "Unknown proof context"))
wenzelm@53055
   500
wenzelm@53055
   501
val _ = Session.protocol_handler "isabelle.Sledgehammer_Params$Handler"
wenzelm@53055
   502
wenzelm@53055
   503
val _ = Isabelle_Process.protocol_command "Sledgehammer.provers"
wenzelm@53055
   504
  (fn [] =>
wenzelm@53055
   505
    let
blanchet@55205
   506
      val this_thy = @{theory}
blanchet@55205
   507
      val provers = space_implode " " (#provers (default_params this_thy []))
wenzelm@53055
   508
    in Output.protocol_message Markup.sledgehammer_provers provers end)
wenzelm@52908
   509
blanchet@35866
   510
end;