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