src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Fri Feb 15 09:17:26 2013 +0100 (2013-02-15 ago)
changeset 51138 583a37b9512d
parent 51130 76d68444cd59
child 51189 a55ef201b19d
permissions -rw-r--r--
killed legacy alias
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@48381
    29
open Sledgehammer_MaSh
blanchet@41087
    30
open Sledgehammer_Run
blanchet@35866
    31
blanchet@48433
    32
(* val cvc3N = "cvc3" *)
blanchet@48405
    33
val yicesN = "yices"
blanchet@48405
    34
val z3N = "z3"
blanchet@48405
    35
blanchet@43020
    36
val runN = "run"
blanchet@43020
    37
val minN = "min"
blanchet@43020
    38
val messagesN = "messages"
blanchet@43020
    39
val supported_proversN = "supported_provers"
blanchet@50719
    40
val kill_allN = "kill_all"
blanchet@43020
    41
val running_proversN = "running_provers"
blanchet@48319
    42
val running_learnersN = "running_learners"
blanchet@43020
    43
val refresh_tptpN = "refresh_tptp"
blanchet@48301
    44
blanchet@39318
    45
val auto = Unsynchronized.ref false
blanchet@39318
    46
blanchet@39318
    47
val _ =
blanchet@39318
    48
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@39318
    49
      (Preferences.bool_pref auto "auto-sledgehammer"
blanchet@39327
    50
           "Run Sledgehammer automatically.")
blanchet@39318
    51
blanchet@36394
    52
(** Sledgehammer commands **)
blanchet@36394
    53
blanchet@48292
    54
fun add_fact_override ns : fact_override = {add = ns, del = [], only = false}
blanchet@48292
    55
fun del_fact_override ns : fact_override = {add = [], del = ns, only = false}
blanchet@48292
    56
fun only_fact_override ns : fact_override = {add = ns, del = [], only = true}
blanchet@48292
    57
fun merge_fact_override_pairwise (r1 : fact_override) (r2 : fact_override) =
blanchet@35966
    58
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    59
   only = #only r1 andalso #only r2}
blanchet@48292
    60
fun merge_fact_overrides rs =
blanchet@48292
    61
  fold merge_fact_override_pairwise rs (only_fact_override [])
blanchet@35965
    62
blanchet@36371
    63
(*** parameters ***)
blanchet@36371
    64
blanchet@40059
    65
val provers = Unsynchronized.ref ""
blanchet@39335
    66
val timeout = Unsynchronized.ref 30
blanchet@36371
    67
blanchet@36371
    68
val _ =
blanchet@36371
    69
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@40059
    70
      (Preferences.string_pref provers
blanchet@40059
    71
          "Sledgehammer: Provers"
blanchet@36371
    72
          "Default automatic provers (separated by whitespace)")
blanchet@36371
    73
blanchet@36371
    74
val _ =
blanchet@36371
    75
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    76
      (Preferences.int_pref timeout
blanchet@36373
    77
          "Sledgehammer: Time Limit"
blanchet@36371
    78
          "ATPs will be interrupted after this time (in seconds)")
blanchet@36371
    79
blanchet@35963
    80
type raw_param = string * string list
blanchet@35963
    81
blanchet@35963
    82
val default_default_params =
blanchet@41208
    83
  [("debug", "false"),
blanchet@35963
    84
   ("verbose", "false"),
blanchet@36143
    85
   ("overlord", "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@49918
    97
   ("isar_proofs", "false"),
smolkas@51130
    98
   ("isar_compress", "10"),
blanchet@45706
    99
   ("slice", "true"),
blanchet@45707
   100
   ("minimize", "smart"),
blanchet@46297
   101
   ("preplay_timeout", "3")]
blanchet@35963
   102
blanchet@36140
   103
val alias_params =
blanchet@51138
   104
  [("prover", ("provers", [])), (* undocumented *)
blanchet@47037
   105
   ("dont_preplay", ("preplay_timeout", ["0"]))]
blanchet@36140
   106
val negated_alias_params =
blanchet@41208
   107
  [("no_debug", "debug"),
blanchet@35963
   108
   ("quiet", "verbose"),
blanchet@36143
   109
   ("no_overlord", "overlord"),
blanchet@41208
   110
   ("non_blocking", "blocking"),
blanchet@46301
   111
   ("non_strict", "strict"),
blanchet@46409
   112
   ("no_uncurried_aliases", "uncurried_aliases"),
blanchet@48321
   113
   ("dont_learn", "learn"),
blanchet@49918
   114
   ("no_isar_proofs", "isar_proofs"),
blanchet@45707
   115
   ("dont_slice", "slice"),
blanchet@45707
   116
   ("dont_minimize", "minimize")]
blanchet@35963
   117
blanchet@36281
   118
val params_for_minimize =
blanchet@46301
   119
  ["debug", "verbose", "overlord", "type_enc", "strict", "lam_trans",
blanchet@46409
   120
   "uncurried_aliases", "max_mono_iters", "max_new_mono_instances",
smolkas@51130
   121
   "learn", "isar_proofs", "isar_compress", "timeout", "preplay_timeout"]
blanchet@36281
   122
blanchet@43569
   123
val property_dependent_params = ["provers", "timeout"]
blanchet@35866
   124
blanchet@35963
   125
fun is_known_raw_param s =
blanchet@35963
   126
  AList.defined (op =) default_default_params s orelse
blanchet@36140
   127
  AList.defined (op =) alias_params s orelse
blanchet@36140
   128
  AList.defined (op =) negated_alias_params s orelse
blanchet@38985
   129
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35963
   130
blanchet@41258
   131
fun is_prover_list ctxt s =
blanchet@41258
   132
  case space_explode " " s of
blanchet@41727
   133
    ss as _ :: _ => forall (is_prover_supported ctxt) ss
blanchet@41258
   134
  | _ => false
blanchet@41258
   135
blanchet@36140
   136
fun unalias_raw_param (name, value) =
blanchet@36140
   137
  case AList.lookup (op =) alias_params name of
blanchet@47037
   138
    SOME (name', []) => (name', value)
blanchet@47037
   139
  | SOME (param' as (name', _)) =>
blanchet@47037
   140
    if value <> ["false"] then
blanchet@47037
   141
      param'
blanchet@47037
   142
    else
blanchet@47037
   143
      error ("Parameter " ^ quote name ^ " cannot be set to \"false\" \
blanchet@47037
   144
             \(cf. " ^ quote name' ^ ").")
blanchet@36140
   145
  | NONE =>
blanchet@36140
   146
    case AList.lookup (op =) negated_alias_params name of
blanchet@36140
   147
      SOME name' => (name', case value of
blanchet@36140
   148
                              ["false"] => ["true"]
blanchet@36140
   149
                            | ["true"] => ["false"]
blanchet@36140
   150
                            | [] => ["false"]
blanchet@36140
   151
                            | _ => value)
blanchet@36140
   152
    | NONE => (name, value)
blanchet@35963
   153
blanchet@46301
   154
val any_type_enc = type_enc_from_string Strict "erased"
blanchet@45514
   155
blanchet@48397
   156
(* "provers =", "type_enc =", "lam_trans =", "fact_filter =", and "max_facts ="
blanchet@48397
   157
   can be omitted. For the last four, this is a secret feature. *)
blanchet@44720
   158
fun normalize_raw_param ctxt =
blanchet@44720
   159
  unalias_raw_param
blanchet@44720
   160
  #> (fn (name, value) =>
blanchet@44720
   161
         if is_known_raw_param name then
blanchet@44720
   162
           (name, value)
blanchet@48533
   163
         else if null value then
blanchet@48533
   164
           if is_prover_list ctxt name then
blanchet@48533
   165
             ("provers", [name])
blanchet@48533
   166
           else if can (type_enc_from_string Strict) name then
blanchet@48533
   167
             ("type_enc", [name])
blanchet@48533
   168
           else if can (trans_lams_from_string ctxt any_type_enc) name then
blanchet@48533
   169
             ("lam_trans", [name])
blanchet@48533
   170
           else if member (op =) fact_filters name then
blanchet@48533
   171
             ("fact_filter", [name])
blanchet@48533
   172
           else if is_some (Int.fromString name) then
blanchet@48533
   173
             ("max_facts", [name])
blanchet@48533
   174
           else
blanchet@48533
   175
             error ("Unknown parameter: " ^ quote name ^ ".")
blanchet@48400
   176
         else
blanchet@48400
   177
           error ("Unknown parameter: " ^ quote name ^ "."))
blanchet@44720
   178
blanchet@46435
   179
(* Ensures that type encodings such as "mono_native?" and "poly_guards!!" are
blanchet@44785
   180
   read correctly. *)
blanchet@44784
   181
val implode_param = strip_spaces_except_between_idents o space_implode " "
blanchet@43009
   182
wenzelm@41472
   183
structure Data = Theory_Data
wenzelm@41472
   184
(
blanchet@35963
   185
  type T = raw_param list
blanchet@35963
   186
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   187
  val extend = I
wenzelm@41472
   188
  fun merge data : T = AList.merge (op =) (K true) data
wenzelm@41472
   189
)
blanchet@35866
   190
blanchet@41727
   191
fun remotify_prover_if_supported_and_not_already_remote ctxt name =
blanchet@40072
   192
  if String.isPrefix remote_prefix name then
blanchet@40072
   193
    SOME name
blanchet@40072
   194
  else
blanchet@40072
   195
    let val remote_name = remote_prefix ^ name in
blanchet@41727
   196
      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
blanchet@40072
   197
    end
blanchet@40072
   198
blanchet@40072
   199
fun remotify_prover_if_not_installed ctxt name =
blanchet@41727
   200
  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
blanchet@40941
   201
    SOME name
blanchet@40941
   202
  else
blanchet@41727
   203
    remotify_prover_if_supported_and_not_already_remote ctxt name
blanchet@40072
   204
blanchet@43009
   205
fun avoid_too_many_threads _ _ [] = []
blanchet@43009
   206
  | avoid_too_many_threads _ (0, 0) _ = []
blanchet@43009
   207
  | avoid_too_many_threads ctxt (0, max_remote) provers =
blanchet@43009
   208
    provers
blanchet@43009
   209
    |> map_filter (remotify_prover_if_supported_and_not_already_remote ctxt)
blanchet@43009
   210
    |> take max_remote
blanchet@43009
   211
  | avoid_too_many_threads _ (max_local, 0) provers =
blanchet@43009
   212
    provers
blanchet@43009
   213
    |> filter_out (String.isPrefix remote_prefix)
blanchet@43009
   214
    |> take max_local
blanchet@43009
   215
  | avoid_too_many_threads ctxt max_local_and_remote (prover :: provers) =
blanchet@43009
   216
    let
blanchet@43009
   217
      val max_local_and_remote =
blanchet@43009
   218
        max_local_and_remote
blanchet@43009
   219
        |> (if String.isPrefix remote_prefix prover then apsnd else apfst)
blanchet@48406
   220
               (Integer.add ~1)
blanchet@43009
   221
    in prover :: avoid_too_many_threads ctxt max_local_and_remote provers end
blanchet@40059
   222
blanchet@43009
   223
val max_default_remote_threads = 4
blanchet@42688
   224
blanchet@40059
   225
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@48405
   226
   timeout, it makes sense to put E first. *)
blanchet@40069
   227
fun default_provers_param_value ctxt =
blanchet@48432
   228
  [eN, spassN, vampireN, z3N, e_sineN, waldmeisterN, yicesN]
blanchet@40941
   229
  |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@43009
   230
  |> avoid_too_many_threads ctxt (Multithreading.max_threads_value (),
blanchet@43009
   231
                                  max_default_remote_threads)
blanchet@42776
   232
  |> implode_param
blanchet@40059
   233
blanchet@44720
   234
fun set_default_raw_param param thy =
blanchet@44720
   235
  let val ctxt = Proof_Context.init_global thy in
blanchet@44720
   236
    thy |> Data.map (AList.update (op =) (normalize_raw_param ctxt param))
blanchet@44720
   237
  end
blanchet@40069
   238
fun default_raw_params ctxt =
wenzelm@42361
   239
  let val thy = Proof_Context.theory_of ctxt in
blanchet@40069
   240
    Data.get thy
blanchet@40069
   241
    |> fold (AList.default (op =))
blanchet@40069
   242
            [("provers", [case !provers of
blanchet@40069
   243
                            "" => default_provers_param_value ctxt
blanchet@40069
   244
                          | s => s]),
blanchet@40069
   245
             ("timeout", let val timeout = !timeout in
blanchet@40069
   246
                           [if timeout <= 0 then "none"
blanchet@40341
   247
                            else string_of_int timeout]
blanchet@40069
   248
                         end)]
blanchet@40069
   249
  end
blanchet@35963
   250
blanchet@43021
   251
fun extract_params mode default_params override_params =
blanchet@35963
   252
  let
blanchet@35963
   253
    val raw_params = rev override_params @ rev default_params
blanchet@42776
   254
    val lookup = Option.map implode_param o AList.lookup (op =) raw_params
blanchet@35963
   255
    val lookup_string = the_default "" o lookup
blanchet@35963
   256
    fun general_lookup_bool option default_value name =
blanchet@35963
   257
      case lookup name of
blanchet@35970
   258
        SOME s => parse_bool_option option name s
blanchet@35963
   259
      | NONE => default_value
blanchet@35963
   260
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   261
    fun lookup_time name =
blanchet@39318
   262
      case lookup name of
blanchet@39318
   263
        SOME s => parse_time_option name s
blanchet@39318
   264
      | NONE => NONE
blanchet@35966
   265
    fun lookup_int name =
blanchet@35963
   266
      case lookup name of
blanchet@35966
   267
        NONE => 0
blanchet@35966
   268
      | SOME s => case Int.fromString s of
blanchet@35966
   269
                    SOME n => n
blanchet@35966
   270
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   271
                                   " must be assigned an integer value.")
blanchet@49918
   272
    fun lookup_real name =
blanchet@49918
   273
      case lookup name of
blanchet@49918
   274
        NONE => 0.0
blanchet@49918
   275
      | SOME s => case Real.fromString s of
blanchet@49918
   276
                    SOME n => n
blanchet@49918
   277
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@49918
   278
                                   " must be assigned a real value.")
blanchet@40343
   279
    fun lookup_real_pair name =
blanchet@38745
   280
      case lookup name of
blanchet@40343
   281
        NONE => (0.0, 0.0)
blanchet@40343
   282
      | SOME s => case s |> space_explode " " |> map Real.fromString of
blanchet@40343
   283
                    [SOME r1, SOME r2] => (r1, r2)
blanchet@38745
   284
                  | _ => error ("Parameter " ^ quote name ^
blanchet@49632
   285
                                " must be assigned a pair of floating-point \
blanchet@40343
   286
                                \values (e.g., \"0.6 0.95\")")
blanchet@43064
   287
    fun lookup_option lookup' name =
blanchet@38589
   288
      case lookup name of
blanchet@38589
   289
        SOME "smart" => NONE
blanchet@43064
   290
      | _ => SOME (lookup' name)
blanchet@43021
   291
    val debug = mode <> Auto_Try andalso lookup_bool "debug"
blanchet@43021
   292
    val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose")
blanchet@36143
   293
    val overlord = lookup_bool "overlord"
blanchet@42995
   294
    val blocking =
blanchet@43021
   295
      Isabelle_Process.is_active () orelse mode <> Normal orelse debug orelse
blanchet@42995
   296
      lookup_bool "blocking"
blanchet@40059
   297
    val provers = lookup_string "provers" |> space_explode " "
blanchet@43021
   298
                  |> mode = Auto_Try ? single o hd
blanchet@43626
   299
    val type_enc =
blanchet@43021
   300
      if mode = Auto_Try then
blanchet@43569
   301
        NONE
blanchet@43626
   302
      else case lookup_string "type_enc" of
blanchet@43569
   303
        "smart" => NONE
blanchet@46301
   304
      | s => (type_enc_from_string Strict s; SOME s)
blanchet@46301
   305
    val strict = mode = Auto_Try orelse lookup_bool "strict"
blanchet@45520
   306
    val lam_trans = lookup_option lookup_string "lam_trans"
blanchet@46409
   307
    val uncurried_aliases = lookup_option lookup_bool "uncurried_aliases"
blanchet@48321
   308
    val learn = lookup_bool "learn"
blanchet@48314
   309
    val fact_filter = lookup_option lookup_string "fact_filter"
blanchet@48314
   310
    val max_facts = lookup_option lookup_int "max_facts"
blanchet@48293
   311
    val fact_thresholds = lookup_real_pair "fact_thresholds"
blanchet@47962
   312
    val max_mono_iters = lookup_option lookup_int "max_mono_iters"
blanchet@47962
   313
    val max_new_mono_instances =
blanchet@47962
   314
      lookup_option lookup_int "max_new_mono_instances"
blanchet@49918
   315
    val isar_proofs = lookup_bool "isar_proofs"
smolkas@51130
   316
    val isar_compress = Real.max (1.0, lookup_real "isar_compress")
blanchet@45706
   317
    val slice = mode <> Auto_Try andalso lookup_bool "slice"
blanchet@45707
   318
    val minimize =
blanchet@45707
   319
      if mode = Auto_Try then NONE else lookup_option lookup_bool "minimize"
blanchet@50557
   320
    val timeout = if mode = Auto_Try then NONE else lookup_time "timeout"
blanchet@43015
   321
    val preplay_timeout =
blanchet@50557
   322
      if mode = Auto_Try then SOME Time.zeroTime
blanchet@50557
   323
      else lookup_time "preplay_timeout"
blanchet@38985
   324
    val expect = lookup_string "expect"
blanchet@35963
   325
  in
blanchet@41208
   326
    {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
blanchet@46301
   327
     provers = provers, type_enc = type_enc, strict = strict,
blanchet@46409
   328
     lam_trans = lam_trans, uncurried_aliases = uncurried_aliases,
blanchet@48321
   329
     learn = learn, fact_filter = fact_filter, max_facts = max_facts,
blanchet@48314
   330
     fact_thresholds = fact_thresholds, max_mono_iters = max_mono_iters,
blanchet@49918
   331
     max_new_mono_instances = max_new_mono_instances, isar_proofs = isar_proofs,
smolkas@51130
   332
     isar_compress = isar_compress, slice = slice, minimize = minimize,
blanchet@49918
   333
     timeout = timeout, preplay_timeout = preplay_timeout, expect = expect}
blanchet@35963
   334
  end
blanchet@35866
   335
blanchet@44720
   336
fun get_params mode = extract_params mode o default_raw_params
blanchet@43021
   337
fun default_params thy = get_params Normal thy o map (apsnd single)
blanchet@35866
   338
blanchet@36373
   339
(* Sledgehammer the given subgoal *)
blanchet@36373
   340
blanchet@45519
   341
val default_minimize_prover = metisN
blanchet@43051
   342
blanchet@44720
   343
fun is_raw_param_relevant_for_minimize (name, _) =
blanchet@44720
   344
  member (op =) params_for_minimize name
blanchet@36281
   345
fun string_for_raw_param (key, values) =
blanchet@42776
   346
  key ^ (case implode_param values of "" => "" | value => " = " ^ value)
blanchet@50748
   347
fun nice_string_for_raw_param (p as (key, ["false"])) =
blanchet@50748
   348
    (case AList.find (op =) negated_alias_params key of
blanchet@50748
   349
       [neg] => neg
blanchet@50748
   350
     | _ => string_for_raw_param p)
blanchet@50748
   351
  | nice_string_for_raw_param p = string_for_raw_param p
blanchet@36281
   352
blanchet@45520
   353
fun minimize_command override_params i more_override_params prover_name
blanchet@45520
   354
                     fact_names =
blanchet@43057
   355
  let
blanchet@43057
   356
    val params =
blanchet@45520
   357
      (override_params
blanchet@45520
   358
       |> filter_out (AList.defined (op =) more_override_params o fst)) @
blanchet@45520
   359
      more_override_params
blanchet@43057
   360
      |> filter is_raw_param_relevant_for_minimize
blanchet@50748
   361
      |> map nice_string_for_raw_param
blanchet@43057
   362
      |> (if prover_name = default_minimize_prover then I else cons prover_name)
blanchet@43057
   363
      |> space_implode ", "
blanchet@43057
   364
  in
blanchet@48392
   365
    sledgehammerN ^ " " ^ minN ^
blanchet@43057
   366
    (if params = "" then "" else enclose " [" "]" params) ^
blanchet@43057
   367
    " (" ^ space_implode " " fact_names ^ ")" ^
blanchet@43057
   368
    (if i = 1 then "" else " " ^ string_of_int i)
blanchet@43057
   369
  end
blanchet@36281
   370
blanchet@50604
   371
val default_learn_prover_timeout = 2.0
blanchet@48392
   372
blanchet@48292
   373
fun hammer_away override_params subcommand opt_i fact_override state =
blanchet@35963
   374
  let
smolkas@50824
   375
    (* necessary to avoid problems in jedit *)
smolkas@50823
   376
    val state = state |> Proof.map_context (Config.put show_markup false)
blanchet@40069
   377
    val ctxt = Proof.context_of state
blanchet@44720
   378
    val override_params = override_params |> map (normalize_raw_param ctxt)
blanchet@50052
   379
    val _ = Isabelle_System.mkdir (Path.explode (getenv "ISABELLE_TMP"))
blanchet@35965
   380
  in
blanchet@35965
   381
    if subcommand = runN then
blanchet@36281
   382
      let val i = the_default 1 opt_i in
blanchet@43021
   383
        run_sledgehammer (get_params Normal ctxt override_params) Normal i
blanchet@48292
   384
                         fact_override (minimize_command override_params i)
blanchet@39318
   385
                         state
blanchet@39318
   386
        |> K ()
blanchet@36281
   387
      end
blanchet@43008
   388
    else if subcommand = minN then
blanchet@48384
   389
      let
blanchet@48395
   390
        val ctxt = ctxt |> Config.put instantiate_inducts false
blanchet@48384
   391
        val i = the_default 1 opt_i
blanchet@48400
   392
        val params =
blanchet@48384
   393
          get_params Minimize ctxt (("provers", [default_minimize_prover]) ::
blanchet@48384
   394
                                    override_params)
blanchet@48384
   395
        val goal = prop_of (#goal (Proof.goal state))
blanchet@48384
   396
        val facts = nearly_all_facts ctxt false fact_override Symtab.empty
blanchet@48384
   397
                                     Termtab.empty [] [] goal
blanchet@48399
   398
        fun learn prover = mash_learn_proof ctxt params prover goal facts
blanchet@48399
   399
      in run_minimize params learn i (#add fact_override) state end
blanchet@35965
   400
    else if subcommand = messagesN then
blanchet@35965
   401
      messages opt_i
blanchet@41727
   402
    else if subcommand = supported_proversN then
blanchet@41727
   403
      supported_provers ctxt
blanchet@50719
   404
    else if subcommand = kill_allN then
blanchet@49365
   405
      (kill_provers (); kill_learners ())
blanchet@48319
   406
    else if subcommand = running_proversN then
blanchet@48319
   407
      running_provers ()
blanchet@48383
   408
    else if subcommand = unlearnN then
blanchet@48383
   409
      mash_unlearn ctxt
blanchet@50484
   410
    else if subcommand = learn_isarN orelse subcommand = learn_proverN orelse
blanchet@50484
   411
            subcommand = relearn_isarN orelse subcommand = relearn_proverN then
blanchet@50484
   412
      (if subcommand = relearn_isarN orelse subcommand = relearn_proverN then
blanchet@48392
   413
         mash_unlearn ctxt
blanchet@48392
   414
       else
blanchet@48392
   415
         ();
blanchet@50484
   416
       mash_learn ctxt
blanchet@50484
   417
           (get_params Normal ctxt
blanchet@50484
   418
                ([("timeout",
blanchet@50484
   419
                   [string_of_real default_learn_prover_timeout]),
blanchet@50484
   420
                  ("slice", ["false"])] @
blanchet@50484
   421
                 override_params @
blanchet@50484
   422
                 [("minimize", ["true"]),
blanchet@50484
   423
                  ("preplay_timeout", ["0"])]))
blanchet@50484
   424
           fact_override (#facts (Proof.goal state))
blanchet@50484
   425
           (subcommand = learn_proverN orelse subcommand = relearn_proverN))
blanchet@48319
   426
    else if subcommand = running_learnersN then
blanchet@48319
   427
      running_learners ()
blanchet@35965
   428
    else if subcommand = refresh_tptpN then
blanchet@35965
   429
      refresh_systems_on_tptp ()
blanchet@35965
   430
    else
blanchet@35965
   431
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   432
  end
blanchet@35963
   433
blanchet@48292
   434
fun sledgehammer_trans (((subcommand, params), fact_override), opt_i) =
blanchet@48292
   435
  Toplevel.keep (hammer_away params subcommand opt_i fact_override
blanchet@35966
   436
                 o Toplevel.proof_of)
blanchet@35963
   437
blanchet@42776
   438
fun string_for_raw_param (name, value) = name ^ " = " ^ implode_param value
blanchet@35963
   439
blanchet@35965
   440
fun sledgehammer_params_trans params =
blanchet@35963
   441
  Toplevel.theory
blanchet@35965
   442
      (fold set_default_raw_param params
blanchet@39318
   443
       #> tap (fn thy =>
wenzelm@42361
   444
                  let val ctxt = Proof_Context.init_global thy in
blanchet@40069
   445
                    writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@40069
   446
                             (case default_raw_params ctxt |> rev of
blanchet@40069
   447
                                [] => "none"
blanchet@40069
   448
                              | params =>
blanchet@44720
   449
                                params |> map string_for_raw_param
blanchet@41258
   450
                                       |> sort_strings |> cat_lines))
blanchet@40069
   451
                  end))
blanchet@35963
   452
wenzelm@46949
   453
val parse_query_bang = @{keyword "?"} || @{keyword "!"} || @{keyword "!!"}
blanchet@42776
   454
val parse_key =
blanchet@44768
   455
  Scan.repeat1 (Parse.typ_group || parse_query_bang) >> implode_param
blanchet@42581
   456
val parse_value =
blanchet@44768
   457
  Scan.repeat1 (Parse.xname || Parse.float_number || parse_query_bang)
wenzelm@46949
   458
val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) []
wenzelm@36960
   459
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35965
   460
val parse_fact_refs =
blanchet@38996
   461
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@48293
   462
val parse_fact_override_chunk =
blanchet@48292
   463
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_fact_override)
blanchet@48292
   464
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_fact_override)
blanchet@48292
   465
  || (parse_fact_refs >> only_fact_override)
blanchet@48292
   466
val parse_fact_override =
blanchet@48293
   467
  Scan.optional (Args.parens (Scan.repeat parse_fact_override_chunk
blanchet@48292
   468
                              >> merge_fact_overrides))
blanchet@48292
   469
                no_fact_override
blanchet@35963
   470
blanchet@35963
   471
val _ =
wenzelm@46961
   472
  Outer_Syntax.improper_command @{command_spec "sledgehammer"}
wenzelm@46961
   473
    "search for first-order proof using automatic theorem provers"
wenzelm@46961
   474
    ((Scan.optional Parse.short_ident runN -- parse_params
blanchet@48292
   475
      -- parse_fact_override -- Scan.option Parse.nat) #>> sledgehammer_trans)
blanchet@35963
   476
val _ =
wenzelm@46961
   477
  Outer_Syntax.command @{command_spec "sledgehammer_params"}
wenzelm@46961
   478
    "set and display the default parameters for Sledgehammer"
wenzelm@46961
   479
    (parse_params #>> sledgehammer_params_trans)
blanchet@36394
   480
blanchet@43020
   481
fun try_sledgehammer auto state =
blanchet@43020
   482
  let
blanchet@43020
   483
    val ctxt = Proof.context_of state
blanchet@43021
   484
    val mode = if auto then Auto_Try else Try
blanchet@43020
   485
    val i = 1
blanchet@43020
   486
  in
blanchet@48292
   487
    run_sledgehammer (get_params mode ctxt []) mode i no_fact_override
blanchet@43020
   488
                     (minimize_command [] i) state
blanchet@40931
   489
  end
blanchet@39318
   490
blanchet@48392
   491
val setup = Try.register_tool (sledgehammerN, (40, auto, try_sledgehammer))
blanchet@39318
   492
blanchet@35866
   493
end;