src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Wed Jul 18 08:44:04 2012 +0200 (2012-07-18 ago)
changeset 48314 ee33ba3c0e05
parent 48308 89674e5a4d35
child 48319 340187063d84
permissions -rw-r--r--
added option to control which fact filter is used
blanchet@36375
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar.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@35963
     7
signature SLEDGEHAMMER_ISAR =
blanchet@35963
     8
sig
blanchet@41087
     9
  type params = Sledgehammer_Provers.params
blanchet@35963
    10
blanchet@39318
    11
  val auto : bool Unsynchronized.ref
blanchet@40059
    12
  val provers : string Unsynchronized.ref
blanchet@39318
    13
  val timeout : int Unsynchronized.ref
blanchet@40069
    14
  val default_params : Proof.context -> (string * string) list -> params
blanchet@39318
    15
  val setup : theory -> theory
blanchet@35963
    16
end;
blanchet@35963
    17
blanchet@35971
    18
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    19
struct
blanchet@35866
    20
blanchet@44784
    21
open ATP_Util
blanchet@38028
    22
open ATP_Systems
blanchet@46320
    23
open ATP_Problem_Generate
blanchet@46320
    24
open ATP_Proof_Reconstruct
blanchet@35971
    25
open Sledgehammer_Util
blanchet@48250
    26
open Sledgehammer_Fact
blanchet@41087
    27
open Sledgehammer_Provers
blanchet@38988
    28
open Sledgehammer_Minimize
blanchet@48301
    29
open Sledgehammer_Filter_MaSh
blanchet@41087
    30
open Sledgehammer_Run
blanchet@35866
    31
blanchet@43020
    32
val runN = "run"
blanchet@43020
    33
val minN = "min"
blanchet@43020
    34
val messagesN = "messages"
blanchet@43020
    35
val supported_proversN = "supported_provers"
blanchet@43020
    36
val running_proversN = "running_provers"
blanchet@43020
    37
val kill_proversN = "kill_provers"
blanchet@43020
    38
val refresh_tptpN = "refresh_tptp"
blanchet@48308
    39
val reset_mashN = "reset_mash"
blanchet@48301
    40
blanchet@39318
    41
val auto = Unsynchronized.ref false
blanchet@39318
    42
blanchet@39318
    43
val _ =
blanchet@39318
    44
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@39318
    45
      (Preferences.bool_pref auto "auto-sledgehammer"
blanchet@39327
    46
           "Run Sledgehammer automatically.")
blanchet@39318
    47
blanchet@36394
    48
(** Sledgehammer commands **)
blanchet@36394
    49
blanchet@48292
    50
fun add_fact_override ns : fact_override = {add = ns, del = [], only = false}
blanchet@48292
    51
fun del_fact_override ns : fact_override = {add = [], del = ns, only = false}
blanchet@48292
    52
fun only_fact_override ns : fact_override = {add = ns, del = [], only = true}
blanchet@48292
    53
fun merge_fact_override_pairwise (r1 : fact_override) (r2 : fact_override) =
blanchet@35966
    54
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    55
   only = #only r1 andalso #only r2}
blanchet@48292
    56
fun merge_fact_overrides rs =
blanchet@48292
    57
  fold merge_fact_override_pairwise rs (only_fact_override [])
blanchet@35965
    58
blanchet@36371
    59
(*** parameters ***)
blanchet@36371
    60
blanchet@40059
    61
val provers = Unsynchronized.ref ""
blanchet@39335
    62
val timeout = Unsynchronized.ref 30
blanchet@36371
    63
blanchet@36371
    64
val _ =
blanchet@36371
    65
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@40059
    66
      (Preferences.string_pref provers
blanchet@40059
    67
          "Sledgehammer: Provers"
blanchet@36371
    68
          "Default automatic provers (separated by whitespace)")
blanchet@36371
    69
blanchet@36371
    70
val _ =
blanchet@36371
    71
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    72
      (Preferences.int_pref timeout
blanchet@36373
    73
          "Sledgehammer: Time Limit"
blanchet@36371
    74
          "ATPs will be interrupted after this time (in seconds)")
blanchet@36371
    75
blanchet@35963
    76
type raw_param = string * string list
blanchet@35963
    77
blanchet@35963
    78
val default_default_params =
blanchet@41208
    79
  [("debug", "false"),
blanchet@35963
    80
   ("verbose", "false"),
blanchet@36143
    81
   ("overlord", "false"),
blanchet@41208
    82
   ("blocking", "false"),
blanchet@43626
    83
   ("type_enc", "smart"),
blanchet@46301
    84
   ("strict", "false"),
blanchet@45514
    85
   ("lam_trans", "smart"),
blanchet@46409
    86
   ("uncurried_aliases", "smart"),
blanchet@48314
    87
   ("fact_filter", "smart"),
blanchet@48314
    88
   ("max_facts", "smart"),
blanchet@48293
    89
   ("fact_thresholds", "0.45 0.85"),
blanchet@47962
    90
   ("max_mono_iters", "smart"),
blanchet@47962
    91
   ("max_new_mono_instances", "smart"),
blanchet@35963
    92
   ("isar_proof", "false"),
blanchet@42443
    93
   ("isar_shrink_factor", "1"),
blanchet@45706
    94
   ("slice", "true"),
blanchet@45707
    95
   ("minimize", "smart"),
blanchet@46297
    96
   ("preplay_timeout", "3")]
blanchet@35963
    97
blanchet@36140
    98
val alias_params =
blanchet@48293
    99
  [("prover", ("provers", [])), (* legacy *)
blanchet@48293
   100
   ("max_relevant", ("max_facts", [])), (* legacy *)
blanchet@47037
   101
   ("dont_preplay", ("preplay_timeout", ["0"]))]
blanchet@36140
   102
val negated_alias_params =
blanchet@41208
   103
  [("no_debug", "debug"),
blanchet@35963
   104
   ("quiet", "verbose"),
blanchet@36143
   105
   ("no_overlord", "overlord"),
blanchet@41208
   106
   ("non_blocking", "blocking"),
blanchet@46301
   107
   ("non_strict", "strict"),
blanchet@46409
   108
   ("no_uncurried_aliases", "uncurried_aliases"),
blanchet@42443
   109
   ("no_isar_proof", "isar_proof"),
blanchet@45707
   110
   ("dont_slice", "slice"),
blanchet@45707
   111
   ("dont_minimize", "minimize")]
blanchet@35963
   112
blanchet@36281
   113
val params_for_minimize =
blanchet@46301
   114
  ["debug", "verbose", "overlord", "type_enc", "strict", "lam_trans",
blanchet@46409
   115
   "uncurried_aliases", "max_mono_iters", "max_new_mono_instances",
blanchet@46409
   116
   "isar_proof", "isar_shrink_factor", "timeout", "preplay_timeout"]
blanchet@36281
   117
blanchet@43569
   118
val property_dependent_params = ["provers", "timeout"]
blanchet@35866
   119
blanchet@35963
   120
fun is_known_raw_param s =
blanchet@35963
   121
  AList.defined (op =) default_default_params s orelse
blanchet@36140
   122
  AList.defined (op =) alias_params s orelse
blanchet@36140
   123
  AList.defined (op =) negated_alias_params s orelse
blanchet@38985
   124
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35963
   125
blanchet@41258
   126
fun is_prover_list ctxt s =
blanchet@41258
   127
  case space_explode " " s of
blanchet@41727
   128
    ss as _ :: _ => forall (is_prover_supported ctxt) ss
blanchet@41258
   129
  | _ => false
blanchet@41258
   130
blanchet@36140
   131
fun unalias_raw_param (name, value) =
blanchet@36140
   132
  case AList.lookup (op =) alias_params name of
blanchet@47037
   133
    SOME (name', []) => (name', value)
blanchet@47037
   134
  | SOME (param' as (name', _)) =>
blanchet@47037
   135
    if value <> ["false"] then
blanchet@47037
   136
      param'
blanchet@47037
   137
    else
blanchet@47037
   138
      error ("Parameter " ^ quote name ^ " cannot be set to \"false\" \
blanchet@47037
   139
             \(cf. " ^ quote name' ^ ").")
blanchet@36140
   140
  | NONE =>
blanchet@36140
   141
    case AList.lookup (op =) negated_alias_params name of
blanchet@36140
   142
      SOME name' => (name', case value of
blanchet@36140
   143
                              ["false"] => ["true"]
blanchet@36140
   144
                            | ["true"] => ["false"]
blanchet@36140
   145
                            | [] => ["false"]
blanchet@36140
   146
                            | _ => value)
blanchet@36140
   147
    | NONE => (name, value)
blanchet@35963
   148
blanchet@46301
   149
val any_type_enc = type_enc_from_string Strict "erased"
blanchet@45514
   150
blanchet@48314
   151
(* "provers =", "type_enc =", "lam_trans =", and "fact_filter =" can be omitted.
blanchet@48314
   152
   For the last three, this is a secret feature. *)
blanchet@44720
   153
fun normalize_raw_param ctxt =
blanchet@44720
   154
  unalias_raw_param
blanchet@44720
   155
  #> (fn (name, value) =>
blanchet@44720
   156
         if is_known_raw_param name then
blanchet@44720
   157
           (name, value)
blanchet@44720
   158
         else if is_prover_list ctxt name andalso null value then
blanchet@44720
   159
           ("provers", [name])
blanchet@46301
   160
         else if can (type_enc_from_string Strict) name andalso null value then
blanchet@44720
   161
           ("type_enc", [name])
blanchet@45514
   162
         else if can (trans_lams_from_string ctxt any_type_enc) name andalso
blanchet@45514
   163
                 null value then
blanchet@45514
   164
           ("lam_trans", [name])
blanchet@48314
   165
         else if member (op =) fact_filters name then
blanchet@48314
   166
           ("fact_filter", [name])
blanchet@44720
   167
         else
blanchet@44720
   168
           error ("Unknown parameter: " ^ quote name ^ "."))
blanchet@44720
   169
blanchet@44720
   170
blanchet@46435
   171
(* Ensures that type encodings such as "mono_native?" and "poly_guards!!" are
blanchet@44785
   172
   read correctly. *)
blanchet@44784
   173
val implode_param = strip_spaces_except_between_idents o space_implode " "
blanchet@43009
   174
wenzelm@41472
   175
structure Data = Theory_Data
wenzelm@41472
   176
(
blanchet@35963
   177
  type T = raw_param list
blanchet@35963
   178
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   179
  val extend = I
wenzelm@41472
   180
  fun merge data : T = AList.merge (op =) (K true) data
wenzelm@41472
   181
)
blanchet@35866
   182
blanchet@41727
   183
fun remotify_prover_if_supported_and_not_already_remote ctxt name =
blanchet@40072
   184
  if String.isPrefix remote_prefix name then
blanchet@40072
   185
    SOME name
blanchet@40072
   186
  else
blanchet@40072
   187
    let val remote_name = remote_prefix ^ name in
blanchet@41727
   188
      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
blanchet@40072
   189
    end
blanchet@40072
   190
blanchet@40072
   191
fun remotify_prover_if_not_installed ctxt name =
blanchet@41727
   192
  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
blanchet@40941
   193
    SOME name
blanchet@40941
   194
  else
blanchet@41727
   195
    remotify_prover_if_supported_and_not_already_remote ctxt name
blanchet@40072
   196
blanchet@43009
   197
fun avoid_too_many_threads _ _ [] = []
blanchet@43009
   198
  | avoid_too_many_threads _ (0, 0) _ = []
blanchet@43009
   199
  | avoid_too_many_threads ctxt (0, max_remote) provers =
blanchet@43009
   200
    provers
blanchet@43009
   201
    |> map_filter (remotify_prover_if_supported_and_not_already_remote ctxt)
blanchet@43009
   202
    |> take max_remote
blanchet@43009
   203
  | avoid_too_many_threads _ (max_local, 0) provers =
blanchet@43009
   204
    provers
blanchet@43009
   205
    |> filter_out (String.isPrefix remote_prefix)
blanchet@43009
   206
    |> take max_local
blanchet@43009
   207
  | avoid_too_many_threads ctxt max_local_and_remote (prover :: provers) =
blanchet@43009
   208
    let
blanchet@43009
   209
      val max_local_and_remote =
blanchet@43009
   210
        max_local_and_remote
blanchet@43009
   211
        |> (if String.isPrefix remote_prefix prover then apsnd else apfst)
blanchet@43009
   212
              (Integer.add ~1)
blanchet@43009
   213
    in prover :: avoid_too_many_threads ctxt max_local_and_remote provers end
blanchet@40059
   214
blanchet@43009
   215
val max_default_remote_threads = 4
blanchet@42688
   216
blanchet@40059
   217
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@40059
   218
   timeout, it makes sense to put SPASS first. *)
blanchet@40069
   219
fun default_provers_param_value ctxt =
blanchet@47642
   220
  [spassN, eN, vampireN, SMT_Solver.solver_name_of ctxt, e_sineN,
blanchet@47074
   221
   waldmeisterN]
blanchet@40941
   222
  |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@43009
   223
  |> avoid_too_many_threads ctxt (Multithreading.max_threads_value (),
blanchet@43009
   224
                                  max_default_remote_threads)
blanchet@42776
   225
  |> implode_param
blanchet@40059
   226
blanchet@44720
   227
fun set_default_raw_param param thy =
blanchet@44720
   228
  let val ctxt = Proof_Context.init_global thy in
blanchet@44720
   229
    thy |> Data.map (AList.update (op =) (normalize_raw_param ctxt param))
blanchet@44720
   230
  end
blanchet@40069
   231
fun default_raw_params ctxt =
wenzelm@42361
   232
  let val thy = Proof_Context.theory_of ctxt in
blanchet@40069
   233
    Data.get thy
blanchet@40069
   234
    |> fold (AList.default (op =))
blanchet@40069
   235
            [("provers", [case !provers of
blanchet@40069
   236
                            "" => default_provers_param_value ctxt
blanchet@40069
   237
                          | s => s]),
blanchet@40069
   238
             ("timeout", let val timeout = !timeout in
blanchet@40069
   239
                           [if timeout <= 0 then "none"
blanchet@40341
   240
                            else string_of_int timeout]
blanchet@40069
   241
                         end)]
blanchet@40069
   242
  end
blanchet@35963
   243
blanchet@35963
   244
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
   245
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
   246
blanchet@43021
   247
fun extract_params mode default_params override_params =
blanchet@35963
   248
  let
blanchet@35963
   249
    val raw_params = rev override_params @ rev default_params
blanchet@42776
   250
    val lookup = Option.map implode_param o AList.lookup (op =) raw_params
blanchet@35963
   251
    val lookup_string = the_default "" o lookup
blanchet@35963
   252
    fun general_lookup_bool option default_value name =
blanchet@35963
   253
      case lookup name of
blanchet@35970
   254
        SOME s => parse_bool_option option name s
blanchet@35963
   255
      | NONE => default_value
blanchet@35963
   256
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   257
    fun lookup_time name =
blanchet@39318
   258
      case lookup name of
blanchet@39318
   259
        SOME s => parse_time_option name s
blanchet@39318
   260
      | NONE => NONE
blanchet@35966
   261
    fun lookup_int name =
blanchet@35963
   262
      case lookup name of
blanchet@35966
   263
        NONE => 0
blanchet@35966
   264
      | SOME s => case Int.fromString s of
blanchet@35966
   265
                    SOME n => n
blanchet@35966
   266
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   267
                                   " must be assigned an integer value.")
blanchet@40343
   268
    fun lookup_real_pair name =
blanchet@38745
   269
      case lookup name of
blanchet@40343
   270
        NONE => (0.0, 0.0)
blanchet@40343
   271
      | SOME s => case s |> space_explode " " |> map Real.fromString of
blanchet@40343
   272
                    [SOME r1, SOME r2] => (r1, r2)
blanchet@38745
   273
                  | _ => error ("Parameter " ^ quote name ^
blanchet@40343
   274
                                "must be assigned a pair of floating-point \
blanchet@40343
   275
                                \values (e.g., \"0.6 0.95\")")
blanchet@43064
   276
    fun lookup_option lookup' name =
blanchet@38589
   277
      case lookup name of
blanchet@38589
   278
        SOME "smart" => NONE
blanchet@43064
   279
      | _ => SOME (lookup' name)
blanchet@43021
   280
    val debug = mode <> Auto_Try andalso lookup_bool "debug"
blanchet@43021
   281
    val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose")
blanchet@36143
   282
    val overlord = lookup_bool "overlord"
blanchet@42995
   283
    val blocking =
blanchet@43021
   284
      Isabelle_Process.is_active () orelse mode <> Normal orelse debug orelse
blanchet@42995
   285
      lookup_bool "blocking"
blanchet@40059
   286
    val provers = lookup_string "provers" |> space_explode " "
blanchet@43021
   287
                  |> mode = Auto_Try ? single o hd
blanchet@43626
   288
    val type_enc =
blanchet@43021
   289
      if mode = Auto_Try then
blanchet@43569
   290
        NONE
blanchet@43626
   291
      else case lookup_string "type_enc" of
blanchet@43569
   292
        "smart" => NONE
blanchet@46301
   293
      | s => (type_enc_from_string Strict s; SOME s)
blanchet@46301
   294
    val strict = mode = Auto_Try orelse lookup_bool "strict"
blanchet@45520
   295
    val lam_trans = lookup_option lookup_string "lam_trans"
blanchet@46409
   296
    val uncurried_aliases = lookup_option lookup_bool "uncurried_aliases"
blanchet@48314
   297
    val fact_filter = lookup_option lookup_string "fact_filter"
blanchet@48314
   298
    val max_facts = lookup_option lookup_int "max_facts"
blanchet@48293
   299
    val fact_thresholds = lookup_real_pair "fact_thresholds"
blanchet@47962
   300
    val max_mono_iters = lookup_option lookup_int "max_mono_iters"
blanchet@47962
   301
    val max_new_mono_instances =
blanchet@47962
   302
      lookup_option lookup_int "max_new_mono_instances"
blanchet@35963
   303
    val isar_proof = lookup_bool "isar_proof"
blanchet@36924
   304
    val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
blanchet@45706
   305
    val slice = mode <> Auto_Try andalso lookup_bool "slice"
blanchet@45707
   306
    val minimize =
blanchet@45707
   307
      if mode = Auto_Try then NONE else lookup_option lookup_bool "minimize"
blanchet@43021
   308
    val timeout =
blanchet@43021
   309
      (if mode = Auto_Try then NONE else lookup_time "timeout") |> the_timeout
blanchet@43015
   310
    val preplay_timeout =
blanchet@43021
   311
      if mode = Auto_Try then Time.zeroTime
blanchet@43015
   312
      else lookup_time "preplay_timeout" |> the_timeout
blanchet@38985
   313
    val expect = lookup_string "expect"
blanchet@35963
   314
  in
blanchet@41208
   315
    {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
blanchet@46301
   316
     provers = provers, type_enc = type_enc, strict = strict,
blanchet@46409
   317
     lam_trans = lam_trans, uncurried_aliases = uncurried_aliases,
blanchet@48314
   318
     fact_filter = fact_filter, max_facts = max_facts,
blanchet@48314
   319
     fact_thresholds = fact_thresholds, max_mono_iters = max_mono_iters,
blanchet@45514
   320
     max_new_mono_instances = max_new_mono_instances,  isar_proof = isar_proof,
blanchet@45707
   321
     isar_shrink_factor = isar_shrink_factor, slice = slice,
blanchet@45707
   322
     minimize = minimize, timeout = timeout, preplay_timeout = preplay_timeout,
blanchet@45707
   323
     expect = expect}
blanchet@35963
   324
  end
blanchet@35866
   325
blanchet@44720
   326
fun get_params mode = extract_params mode o default_raw_params
blanchet@43021
   327
fun default_params thy = get_params Normal thy o map (apsnd single)
blanchet@35866
   328
blanchet@36373
   329
(* Sledgehammer the given subgoal *)
blanchet@36373
   330
blanchet@45519
   331
val default_minimize_prover = metisN
blanchet@43051
   332
blanchet@44720
   333
fun is_raw_param_relevant_for_minimize (name, _) =
blanchet@44720
   334
  member (op =) params_for_minimize name
blanchet@36281
   335
fun string_for_raw_param (key, values) =
blanchet@42776
   336
  key ^ (case implode_param values of "" => "" | value => " = " ^ value)
blanchet@36281
   337
blanchet@45520
   338
fun minimize_command override_params i more_override_params prover_name
blanchet@45520
   339
                     fact_names =
blanchet@43057
   340
  let
blanchet@43057
   341
    val params =
blanchet@45520
   342
      (override_params
blanchet@45520
   343
       |> filter_out (AList.defined (op =) more_override_params o fst)) @
blanchet@45520
   344
      more_override_params
blanchet@43057
   345
      |> filter is_raw_param_relevant_for_minimize
blanchet@43057
   346
      |> map string_for_raw_param
blanchet@43057
   347
      |> (if prover_name = default_minimize_prover then I else cons prover_name)
blanchet@43057
   348
      |> space_implode ", "
blanchet@43057
   349
  in
blanchet@47037
   350
    "sledgehammer" ^ " " ^ minN ^
blanchet@43057
   351
    (if params = "" then "" else enclose " [" "]" params) ^
blanchet@43057
   352
    " (" ^ space_implode " " fact_names ^ ")" ^
blanchet@43057
   353
    (if i = 1 then "" else " " ^ string_of_int i)
blanchet@43057
   354
  end
blanchet@36281
   355
blanchet@48292
   356
fun hammer_away override_params subcommand opt_i fact_override state =
blanchet@35963
   357
  let
blanchet@40069
   358
    val ctxt = Proof.context_of state
blanchet@44720
   359
    val override_params = override_params |> map (normalize_raw_param ctxt)
blanchet@35965
   360
  in
blanchet@35965
   361
    if subcommand = runN then
blanchet@36281
   362
      let val i = the_default 1 opt_i in
blanchet@43021
   363
        run_sledgehammer (get_params Normal ctxt override_params) Normal i
blanchet@48292
   364
                         fact_override (minimize_command override_params i)
blanchet@39318
   365
                         state
blanchet@39318
   366
        |> K ()
blanchet@36281
   367
      end
blanchet@43008
   368
    else if subcommand = minN then
blanchet@43051
   369
      run_minimize (get_params Minimize ctxt
blanchet@43051
   370
                               (("provers", [default_minimize_prover]) ::
blanchet@43051
   371
                                override_params))
blanchet@48292
   372
                   (the_default 1 opt_i) (#add fact_override) state
blanchet@35965
   373
    else if subcommand = messagesN then
blanchet@35965
   374
      messages opt_i
blanchet@41727
   375
    else if subcommand = supported_proversN then
blanchet@41727
   376
      supported_provers ctxt
blanchet@40059
   377
    else if subcommand = running_proversN then
blanchet@40059
   378
      running_provers ()
blanchet@40059
   379
    else if subcommand = kill_proversN then
blanchet@40059
   380
      kill_provers ()
blanchet@35965
   381
    else if subcommand = refresh_tptpN then
blanchet@35965
   382
      refresh_systems_on_tptp ()
blanchet@48308
   383
    else if subcommand = reset_mashN then
blanchet@48308
   384
      mash_reset ctxt
blanchet@35965
   385
    else
blanchet@35965
   386
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   387
  end
blanchet@35963
   388
blanchet@48292
   389
fun sledgehammer_trans (((subcommand, params), fact_override), opt_i) =
blanchet@48292
   390
  Toplevel.keep (hammer_away params subcommand opt_i fact_override
blanchet@35966
   391
                 o Toplevel.proof_of)
blanchet@35963
   392
blanchet@42776
   393
fun string_for_raw_param (name, value) = name ^ " = " ^ implode_param value
blanchet@35963
   394
blanchet@35965
   395
fun sledgehammer_params_trans params =
blanchet@35963
   396
  Toplevel.theory
blanchet@35965
   397
      (fold set_default_raw_param params
blanchet@39318
   398
       #> tap (fn thy =>
wenzelm@42361
   399
                  let val ctxt = Proof_Context.init_global thy in
blanchet@40069
   400
                    writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@40069
   401
                             (case default_raw_params ctxt |> rev of
blanchet@40069
   402
                                [] => "none"
blanchet@40069
   403
                              | params =>
blanchet@44720
   404
                                params |> map string_for_raw_param
blanchet@41258
   405
                                       |> sort_strings |> cat_lines))
blanchet@40069
   406
                  end))
blanchet@35963
   407
wenzelm@46949
   408
val parse_query_bang = @{keyword "?"} || @{keyword "!"} || @{keyword "!!"}
blanchet@42776
   409
val parse_key =
blanchet@44768
   410
  Scan.repeat1 (Parse.typ_group || parse_query_bang) >> implode_param
blanchet@42581
   411
val parse_value =
blanchet@44768
   412
  Scan.repeat1 (Parse.xname || Parse.float_number || parse_query_bang)
wenzelm@46949
   413
val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) []
wenzelm@36960
   414
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35965
   415
val parse_fact_refs =
blanchet@38996
   416
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@48293
   417
val parse_fact_override_chunk =
blanchet@48292
   418
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_fact_override)
blanchet@48292
   419
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_fact_override)
blanchet@48292
   420
  || (parse_fact_refs >> only_fact_override)
blanchet@48292
   421
val parse_fact_override =
blanchet@48293
   422
  Scan.optional (Args.parens (Scan.repeat parse_fact_override_chunk
blanchet@48292
   423
                              >> merge_fact_overrides))
blanchet@48292
   424
                no_fact_override
blanchet@35963
   425
blanchet@35963
   426
val _ =
wenzelm@46961
   427
  Outer_Syntax.improper_command @{command_spec "sledgehammer"}
wenzelm@46961
   428
    "search for first-order proof using automatic theorem provers"
wenzelm@46961
   429
    ((Scan.optional Parse.short_ident runN -- parse_params
blanchet@48292
   430
      -- parse_fact_override -- Scan.option Parse.nat) #>> sledgehammer_trans)
blanchet@35963
   431
val _ =
wenzelm@46961
   432
  Outer_Syntax.command @{command_spec "sledgehammer_params"}
wenzelm@46961
   433
    "set and display the default parameters for Sledgehammer"
wenzelm@46961
   434
    (parse_params #>> sledgehammer_params_trans)
blanchet@36394
   435
blanchet@43020
   436
fun try_sledgehammer auto state =
blanchet@43020
   437
  let
blanchet@43020
   438
    val ctxt = Proof.context_of state
blanchet@43021
   439
    val mode = if auto then Auto_Try else Try
blanchet@43020
   440
    val i = 1
blanchet@43020
   441
  in
blanchet@48292
   442
    run_sledgehammer (get_params mode ctxt []) mode i no_fact_override
blanchet@43020
   443
                     (minimize_command [] i) state
blanchet@40931
   444
  end
blanchet@39318
   445
blanchet@47037
   446
val setup = Try.register_tool ("sledgehammer", (40, auto, try_sledgehammer))
blanchet@39318
   447
blanchet@35866
   448
end;