src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Fri, 20 Jul 2012 22:19:46 +0200
changeset 49407 ca998fa08cd9
parent 49403 fd7958ebee96
child 49410 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@35961
     7
signature SLEDGEHAMMER_ISAR =
blanchet@35961
     8
sig
blanchet@41335
     9
  type params = Sledgehammer_Provers.params
blanchet@35961
    10
blanchet@39564
    11
  val auto : bool Unsynchronized.ref
blanchet@40240
    12
  val provers : string Unsynchronized.ref
blanchet@39564
    13
  val timeout : int Unsynchronized.ref
blanchet@40250
    14
  val default_params : Proof.context -> (string * string) list -> params
blanchet@39564
    15
  val setup : theory -> theory
blanchet@35961
    16
end;
blanchet@35961
    17
blanchet@35969
    18
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    19
struct
blanchet@35866
    20
blanchet@45652
    21
open ATP_Util
blanchet@38262
    22
open ATP_Systems
blanchet@47148
    23
open ATP_Problem_Generate
blanchet@47148
    24
open ATP_Proof_Reconstruct
blanchet@35969
    25
open Sledgehammer_Util
blanchet@49265
    26
open Sledgehammer_Fact
blanchet@41335
    27
open Sledgehammer_Provers
blanchet@39232
    28
open Sledgehammer_Minimize
blanchet@49396
    29
open Sledgehammer_MaSh
blanchet@41335
    30
open Sledgehammer_Run
blanchet@35866
    31
blanchet@43861
    32
val runN = "run"
blanchet@43861
    33
val minN = "min"
blanchet@43861
    34
val messagesN = "messages"
blanchet@43861
    35
val supported_proversN = "supported_provers"
blanchet@49334
    36
val kill_proversN = "kill_provers"
blanchet@43861
    37
val running_proversN = "running_provers"
blanchet@49334
    38
val kill_learnersN = "kill_learners"
blanchet@49334
    39
val running_learnersN = "running_learners"
blanchet@43861
    40
val refresh_tptpN = "refresh_tptp"
blanchet@49316
    41
blanchet@39564
    42
val auto = Unsynchronized.ref false
blanchet@39564
    43
blanchet@39564
    44
val _ =
blanchet@39564
    45
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@39564
    46
      (Preferences.bool_pref auto "auto-sledgehammer"
blanchet@39573
    47
           "Run Sledgehammer automatically.")
blanchet@39564
    48
blanchet@36394
    49
(** Sledgehammer commands **)
blanchet@36394
    50
blanchet@49307
    51
fun add_fact_override ns : fact_override = {add = ns, del = [], only = false}
blanchet@49307
    52
fun del_fact_override ns : fact_override = {add = [], del = ns, only = false}
blanchet@49307
    53
fun only_fact_override ns : fact_override = {add = ns, del = [], only = true}
blanchet@49307
    54
fun merge_fact_override_pairwise (r1 : fact_override) (r2 : fact_override) =
blanchet@35964
    55
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    56
   only = #only r1 andalso #only r2}
blanchet@49307
    57
fun merge_fact_overrides rs =
blanchet@49307
    58
  fold merge_fact_override_pairwise rs (only_fact_override [])
blanchet@35963
    59
blanchet@36371
    60
(*** parameters ***)
blanchet@36371
    61
blanchet@40240
    62
val provers = Unsynchronized.ref ""
blanchet@39581
    63
val timeout = Unsynchronized.ref 30
blanchet@36371
    64
blanchet@36371
    65
val _ =
blanchet@36371
    66
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@40240
    67
      (Preferences.string_pref provers
blanchet@40240
    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@35961
    77
type raw_param = string * string list
blanchet@35866
    78
blanchet@35961
    79
val default_default_params =
blanchet@41456
    80
  [("debug", "false"),
blanchet@35961
    81
   ("verbose", "false"),
blanchet@36141
    82
   ("overlord", "false"),
blanchet@41456
    83
   ("blocking", "false"),
blanchet@44493
    84
   ("type_enc", "smart"),
blanchet@47129
    85
   ("strict", "false"),
blanchet@46385
    86
   ("lam_trans", "smart"),
blanchet@47237
    87
   ("uncurried_aliases", "smart"),
blanchet@49336
    88
   ("learn", "true"),
blanchet@49329
    89
   ("fact_filter", "smart"),
blanchet@49329
    90
   ("max_facts", "smart"),
blanchet@49308
    91
   ("fact_thresholds", "0.45 0.85"),
blanchet@48977
    92
   ("max_mono_iters", "smart"),
blanchet@48977
    93
   ("max_new_mono_instances", "smart"),
blanchet@35961
    94
   ("isar_proof", "false"),
blanchet@43314
    95
   ("isar_shrink_factor", "1"),
blanchet@46577
    96
   ("slice", "true"),
blanchet@46578
    97
   ("minimize", "smart"),
blanchet@47125
    98
   ("preplay_timeout", "3")]
blanchet@35866
    99
blanchet@36138
   100
val alias_params =
blanchet@49308
   101
  [("prover", ("provers", [])), (* legacy *)
blanchet@49308
   102
   ("max_relevant", ("max_facts", [])), (* legacy *)
blanchet@47908
   103
   ("dont_preplay", ("preplay_timeout", ["0"]))]
blanchet@36138
   104
val negated_alias_params =
blanchet@41456
   105
  [("no_debug", "debug"),
blanchet@35961
   106
   ("quiet", "verbose"),
blanchet@36141
   107
   ("no_overlord", "overlord"),
blanchet@41456
   108
   ("non_blocking", "blocking"),
blanchet@47129
   109
   ("non_strict", "strict"),
blanchet@47237
   110
   ("no_uncurried_aliases", "uncurried_aliases"),
blanchet@49336
   111
   ("dont_learn", "learn"),
blanchet@43314
   112
   ("no_isar_proof", "isar_proof"),
blanchet@46578
   113
   ("dont_slice", "slice"),
blanchet@46578
   114
   ("dont_minimize", "minimize")]
blanchet@35866
   115
blanchet@36281
   116
val params_for_minimize =
blanchet@47129
   117
  ["debug", "verbose", "overlord", "type_enc", "strict", "lam_trans",
blanchet@47237
   118
   "uncurried_aliases", "max_mono_iters", "max_new_mono_instances",
blanchet@47237
   119
   "isar_proof", "isar_shrink_factor", "timeout", "preplay_timeout"]
blanchet@36281
   120
blanchet@44431
   121
val property_dependent_params = ["provers", "timeout"]
blanchet@35866
   122
blanchet@35961
   123
fun is_known_raw_param s =
blanchet@35961
   124
  AList.defined (op =) default_default_params s orelse
blanchet@36138
   125
  AList.defined (op =) alias_params s orelse
blanchet@36138
   126
  AList.defined (op =) negated_alias_params s orelse
blanchet@39229
   127
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35866
   128
blanchet@41504
   129
fun is_prover_list ctxt s =
blanchet@41504
   130
  case space_explode " " s of
blanchet@42591
   131
    ss as _ :: _ => forall (is_prover_supported ctxt) ss
blanchet@41504
   132
  | _ => false
blanchet@41504
   133
blanchet@36138
   134
fun unalias_raw_param (name, value) =
blanchet@36138
   135
  case AList.lookup (op =) alias_params name of
blanchet@47908
   136
    SOME (name', []) => (name', value)
blanchet@47908
   137
  | SOME (param' as (name', _)) =>
blanchet@47908
   138
    if value <> ["false"] then
blanchet@47908
   139
      param'
blanchet@47908
   140
    else
blanchet@47908
   141
      error ("Parameter " ^ quote name ^ " cannot be set to \"false\" \
blanchet@47908
   142
             \(cf. " ^ quote name' ^ ").")
blanchet@36138
   143
  | NONE =>
blanchet@36138
   144
    case AList.lookup (op =) negated_alias_params name of
blanchet@36138
   145
      SOME name' => (name', case value of
blanchet@36138
   146
                              ["false"] => ["true"]
blanchet@36138
   147
                            | ["true"] => ["false"]
blanchet@36138
   148
                            | [] => ["false"]
blanchet@36138
   149
                            | _ => value)
blanchet@36138
   150
    | NONE => (name, value)
blanchet@35961
   151
blanchet@47129
   152
val any_type_enc = type_enc_from_string Strict "erased"
blanchet@46385
   153
blanchet@49329
   154
(* "provers =", "type_enc =", "lam_trans =", and "fact_filter =" can be omitted.
blanchet@49329
   155
   For the last three, this is a secret feature. *)
blanchet@45591
   156
fun normalize_raw_param ctxt =
blanchet@45591
   157
  unalias_raw_param
blanchet@45591
   158
  #> (fn (name, value) =>
blanchet@45591
   159
         if is_known_raw_param name then
blanchet@45591
   160
           (name, value)
blanchet@45591
   161
         else if is_prover_list ctxt name andalso null value then
blanchet@45591
   162
           ("provers", [name])
blanchet@47129
   163
         else if can (type_enc_from_string Strict) name andalso null value then
blanchet@45591
   164
           ("type_enc", [name])
blanchet@46385
   165
         else if can (trans_lams_from_string ctxt any_type_enc) name andalso
blanchet@46385
   166
                 null value then
blanchet@46385
   167
           ("lam_trans", [name])
blanchet@49329
   168
         else if member (op =) fact_filters name then
blanchet@49329
   169
           ("fact_filter", [name])
blanchet@45591
   170
         else
blanchet@45591
   171
           error ("Unknown parameter: " ^ quote name ^ "."))
blanchet@45591
   172
blanchet@45591
   173
blanchet@47263
   174
(* Ensures that type encodings such as "mono_native?" and "poly_guards!!" are
blanchet@45653
   175
   read correctly. *)
blanchet@45652
   176
val implode_param = strip_spaces_except_between_idents o space_implode " "
blanchet@43850
   177
wenzelm@41720
   178
structure Data = Theory_Data
wenzelm@41720
   179
(
blanchet@35961
   180
  type T = raw_param list
blanchet@35961
   181
  val empty = default_default_params |> map (apsnd single)
blanchet@35961
   182
  val extend = I
wenzelm@41720
   183
  fun merge data : T = AList.merge (op =) (K true) data
wenzelm@41720
   184
)
blanchet@35961
   185
blanchet@42591
   186
fun remotify_prover_if_supported_and_not_already_remote ctxt name =
blanchet@40253
   187
  if String.isPrefix remote_prefix name then
blanchet@40253
   188
    SOME name
blanchet@40253
   189
  else
blanchet@40253
   190
    let val remote_name = remote_prefix ^ name in
blanchet@42591
   191
      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
blanchet@40253
   192
    end
blanchet@40253
   193
blanchet@40253
   194
fun remotify_prover_if_not_installed ctxt name =
blanchet@42591
   195
  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
blanchet@41189
   196
    SOME name
blanchet@41189
   197
  else
blanchet@42591
   198
    remotify_prover_if_supported_and_not_already_remote ctxt name
blanchet@40253
   199
blanchet@43850
   200
fun avoid_too_many_threads _ _ [] = []
blanchet@43850
   201
  | avoid_too_many_threads _ (0, 0) _ = []
blanchet@43850
   202
  | avoid_too_many_threads ctxt (0, max_remote) provers =
blanchet@43850
   203
    provers
blanchet@43850
   204
    |> map_filter (remotify_prover_if_supported_and_not_already_remote ctxt)
blanchet@43850
   205
    |> take max_remote
blanchet@43850
   206
  | avoid_too_many_threads _ (max_local, 0) provers =
blanchet@43850
   207
    provers
blanchet@43850
   208
    |> filter_out (String.isPrefix remote_prefix)
blanchet@43850
   209
    |> take max_local
blanchet@43850
   210
  | avoid_too_many_threads ctxt max_local_and_remote (prover :: provers) =
blanchet@43850
   211
    let
blanchet@43850
   212
      val max_local_and_remote =
blanchet@43850
   213
        max_local_and_remote
blanchet@43850
   214
        |> (if String.isPrefix remote_prefix prover then apsnd else apfst)
blanchet@43850
   215
              (Integer.add ~1)
blanchet@43850
   216
    in prover :: avoid_too_many_threads ctxt max_local_and_remote provers end
blanchet@40240
   217
blanchet@43850
   218
val max_default_remote_threads = 4
blanchet@43558
   219
blanchet@40240
   220
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@40240
   221
   timeout, it makes sense to put SPASS first. *)
blanchet@40250
   222
fun default_provers_param_value ctxt =
blanchet@48513
   223
  [spassN, eN, vampireN, SMT_Solver.solver_name_of ctxt, e_sineN,
blanchet@47934
   224
   waldmeisterN]
blanchet@41189
   225
  |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@43850
   226
  |> avoid_too_many_threads ctxt (Multithreading.max_threads_value (),
blanchet@43850
   227
                                  max_default_remote_threads)
blanchet@43647
   228
  |> implode_param
blanchet@40240
   229
blanchet@45591
   230
fun set_default_raw_param param thy =
blanchet@45591
   231
  let val ctxt = Proof_Context.init_global thy in
blanchet@45591
   232
    thy |> Data.map (AList.update (op =) (normalize_raw_param ctxt param))
blanchet@45591
   233
  end
blanchet@40250
   234
fun default_raw_params ctxt =
wenzelm@43232
   235
  let val thy = Proof_Context.theory_of ctxt in
blanchet@40250
   236
    Data.get thy
blanchet@40250
   237
    |> fold (AList.default (op =))
blanchet@40250
   238
            [("provers", [case !provers of
blanchet@40250
   239
                            "" => default_provers_param_value ctxt
blanchet@40250
   240
                          | s => s]),
blanchet@40250
   241
             ("timeout", let val timeout = !timeout in
blanchet@40250
   242
                           [if timeout <= 0 then "none"
blanchet@40582
   243
                            else string_of_int timeout]
blanchet@40250
   244
                         end)]
blanchet@40250
   245
  end
blanchet@35961
   246
blanchet@49398
   247
val the_timeout = the_default infinite_timeout
blanchet@35961
   248
blanchet@43862
   249
fun extract_params mode default_params override_params =
blanchet@35961
   250
  let
blanchet@35961
   251
    val raw_params = rev override_params @ rev default_params
blanchet@43647
   252
    val lookup = Option.map implode_param o AList.lookup (op =) raw_params
blanchet@35961
   253
    val lookup_string = the_default "" o lookup
blanchet@35961
   254
    fun general_lookup_bool option default_value name =
blanchet@35961
   255
      case lookup name of
blanchet@35968
   256
        SOME s => parse_bool_option option name s
blanchet@35961
   257
      | NONE => default_value
blanchet@35961
   258
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35961
   259
    fun lookup_time name =
blanchet@39564
   260
      case lookup name of
blanchet@39564
   261
        SOME s => parse_time_option name s
blanchet@39564
   262
      | NONE => NONE
blanchet@35964
   263
    fun lookup_int name =
blanchet@35961
   264
      case lookup name of
blanchet@35964
   265
        NONE => 0
blanchet@35964
   266
      | SOME s => case Int.fromString s of
blanchet@35964
   267
                    SOME n => n
blanchet@35964
   268
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35964
   269
                                   " must be assigned an integer value.")
blanchet@40584
   270
    fun lookup_real_pair name =
blanchet@38984
   271
      case lookup name of
blanchet@40584
   272
        NONE => (0.0, 0.0)
blanchet@40584
   273
      | SOME s => case s |> space_explode " " |> map Real.fromString of
blanchet@40584
   274
                    [SOME r1, SOME r2] => (r1, r2)
blanchet@38984
   275
                  | _ => error ("Parameter " ^ quote name ^
blanchet@40584
   276
                                "must be assigned a pair of floating-point \
blanchet@40584
   277
                                \values (e.g., \"0.6 0.95\")")
blanchet@43905
   278
    fun lookup_option lookup' name =
blanchet@38812
   279
      case lookup name of
blanchet@38812
   280
        SOME "smart" => NONE
blanchet@43905
   281
      | _ => SOME (lookup' name)
blanchet@43862
   282
    val debug = mode <> Auto_Try andalso lookup_bool "debug"
blanchet@43862
   283
    val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose")
blanchet@36141
   284
    val overlord = lookup_bool "overlord"
blanchet@43836
   285
    val blocking =
blanchet@43862
   286
      Isabelle_Process.is_active () orelse mode <> Normal orelse debug orelse
blanchet@43836
   287
      lookup_bool "blocking"
blanchet@40240
   288
    val provers = lookup_string "provers" |> space_explode " "
blanchet@43862
   289
                  |> mode = Auto_Try ? single o hd
blanchet@44493
   290
    val type_enc =
blanchet@43862
   291
      if mode = Auto_Try then
blanchet@44431
   292
        NONE
blanchet@44493
   293
      else case lookup_string "type_enc" of
blanchet@44431
   294
        "smart" => NONE
blanchet@47129
   295
      | s => (type_enc_from_string Strict s; SOME s)
blanchet@47129
   296
    val strict = mode = Auto_Try orelse lookup_bool "strict"
blanchet@46391
   297
    val lam_trans = lookup_option lookup_string "lam_trans"
blanchet@47237
   298
    val uncurried_aliases = lookup_option lookup_bool "uncurried_aliases"
blanchet@49336
   299
    val learn = lookup_bool "learn"
blanchet@49329
   300
    val fact_filter = lookup_option lookup_string "fact_filter"
blanchet@49329
   301
    val max_facts = lookup_option lookup_int "max_facts"
blanchet@49308
   302
    val fact_thresholds = lookup_real_pair "fact_thresholds"
blanchet@48977
   303
    val max_mono_iters = lookup_option lookup_int "max_mono_iters"
blanchet@48977
   304
    val max_new_mono_instances =
blanchet@48977
   305
      lookup_option lookup_int "max_new_mono_instances"
blanchet@35961
   306
    val isar_proof = lookup_bool "isar_proof"
blanchet@36916
   307
    val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
blanchet@46577
   308
    val slice = mode <> Auto_Try andalso lookup_bool "slice"
blanchet@46578
   309
    val minimize =
blanchet@46578
   310
      if mode = Auto_Try then NONE else lookup_option lookup_bool "minimize"
blanchet@43862
   311
    val timeout =
blanchet@43862
   312
      (if mode = Auto_Try then NONE else lookup_time "timeout") |> the_timeout
blanchet@43856
   313
    val preplay_timeout =
blanchet@43862
   314
      if mode = Auto_Try then Time.zeroTime
blanchet@43856
   315
      else lookup_time "preplay_timeout" |> the_timeout
blanchet@39229
   316
    val expect = lookup_string "expect"
blanchet@35961
   317
  in
blanchet@41456
   318
    {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
blanchet@47129
   319
     provers = provers, type_enc = type_enc, strict = strict,
blanchet@47237
   320
     lam_trans = lam_trans, uncurried_aliases = uncurried_aliases,
blanchet@49336
   321
     learn = learn, fact_filter = fact_filter, max_facts = max_facts,
blanchet@49329
   322
     fact_thresholds = fact_thresholds, max_mono_iters = max_mono_iters,
blanchet@49403
   323
     max_new_mono_instances = max_new_mono_instances, isar_proof = isar_proof,
blanchet@46578
   324
     isar_shrink_factor = isar_shrink_factor, slice = slice,
blanchet@46578
   325
     minimize = minimize, timeout = timeout, preplay_timeout = preplay_timeout,
blanchet@46578
   326
     expect = expect}
blanchet@35961
   327
  end
blanchet@35961
   328
blanchet@45591
   329
fun get_params mode = extract_params mode o default_raw_params
blanchet@43862
   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@46390
   334
val default_minimize_prover = metisN
blanchet@43892
   335
blanchet@45591
   336
fun is_raw_param_relevant_for_minimize (name, _) =
blanchet@45591
   337
  member (op =) params_for_minimize name
blanchet@36281
   338
fun string_for_raw_param (key, values) =
blanchet@43647
   339
  key ^ (case implode_param values of "" => "" | value => " = " ^ value)
blanchet@36281
   340
blanchet@46391
   341
fun minimize_command override_params i more_override_params prover_name
blanchet@46391
   342
                     fact_names =
blanchet@43898
   343
  let
blanchet@43898
   344
    val params =
blanchet@46391
   345
      (override_params
blanchet@46391
   346
       |> filter_out (AList.defined (op =) more_override_params o fst)) @
blanchet@46391
   347
      more_override_params
blanchet@43898
   348
      |> filter is_raw_param_relevant_for_minimize
blanchet@43898
   349
      |> map string_for_raw_param
blanchet@43898
   350
      |> (if prover_name = default_minimize_prover then I else cons prover_name)
blanchet@43898
   351
      |> space_implode ", "
blanchet@43898
   352
  in
blanchet@49407
   353
    sledgehammerN ^ " " ^ minN ^
blanchet@43898
   354
    (if params = "" then "" else enclose " [" "]" params) ^
blanchet@43898
   355
    " (" ^ space_implode " " fact_names ^ ")" ^
blanchet@43898
   356
    (if i = 1 then "" else " " ^ string_of_int i)
blanchet@43898
   357
  end
blanchet@36281
   358
blanchet@49407
   359
val default_learn_atp_timeout = 0.5
blanchet@49407
   360
blanchet@49307
   361
fun hammer_away override_params subcommand opt_i fact_override state =
blanchet@35961
   362
  let
blanchet@40250
   363
    val ctxt = Proof.context_of state
blanchet@45591
   364
    val override_params = override_params |> map (normalize_raw_param ctxt)
blanchet@35963
   365
  in
blanchet@35963
   366
    if subcommand = runN then
blanchet@36281
   367
      let val i = the_default 1 opt_i in
blanchet@43862
   368
        run_sledgehammer (get_params Normal ctxt override_params) Normal i
blanchet@49307
   369
                         fact_override (minimize_command override_params i)
blanchet@39564
   370
                         state
blanchet@39564
   371
        |> K ()
blanchet@36281
   372
      end
blanchet@43849
   373
    else if subcommand = minN then
blanchet@49399
   374
      let
blanchet@49399
   375
        val i = the_default 1 opt_i
blanchet@49399
   376
        val params as {provers, ...} =
blanchet@49399
   377
          get_params Minimize ctxt (("provers", [default_minimize_prover]) ::
blanchet@49399
   378
                                    override_params)
blanchet@49399
   379
        val goal = prop_of (#goal (Proof.goal state))
blanchet@49399
   380
        val facts = nearly_all_facts ctxt false fact_override Symtab.empty
blanchet@49399
   381
                                     Termtab.empty [] [] goal
blanchet@49399
   382
        val do_learn = mash_learn_proof ctxt params (hd provers) goal facts
blanchet@49399
   383
      in run_minimize params do_learn i (#add fact_override) state end
blanchet@35963
   384
    else if subcommand = messagesN then
blanchet@35963
   385
      messages opt_i
blanchet@42591
   386
    else if subcommand = supported_proversN then
blanchet@42591
   387
      supported_provers ctxt
blanchet@49334
   388
    else if subcommand = kill_proversN then
blanchet@49334
   389
      kill_provers ()
blanchet@40240
   390
    else if subcommand = running_proversN then
blanchet@40240
   391
      running_provers ()
blanchet@49398
   392
    else if subcommand = unlearnN then
blanchet@49398
   393
      mash_unlearn ctxt
blanchet@49407
   394
    else if subcommand = learn_isarN orelse subcommand = learn_atpN orelse
blanchet@49407
   395
            subcommand = relearn_isarN orelse subcommand = relearn_atpN then
blanchet@49407
   396
      (if subcommand = relearn_isarN orelse subcommand = relearn_atpN then
blanchet@49407
   397
         mash_unlearn ctxt
blanchet@49407
   398
       else
blanchet@49407
   399
         ();
blanchet@49398
   400
       mash_learn ctxt (get_params Normal ctxt
blanchet@49407
   401
                           (("timeout",
blanchet@49407
   402
                             [string_of_real default_learn_atp_timeout]) ::
blanchet@49407
   403
                            override_params @
blanchet@49407
   404
                            [("slice", ["false"]),
blanchet@49407
   405
                             ("minimize", ["true"]),
blanchet@49407
   406
                             ("preplay_timeout", ["0"])])))
blanchet@49407
   407
           (subcommand = learn_atpN orelse subcommand = relearn_atpN)
blanchet@49334
   408
    else if subcommand = kill_learnersN then
blanchet@49334
   409
      kill_learners ()
blanchet@49334
   410
    else if subcommand = running_learnersN then
blanchet@49334
   411
      running_learners ()
blanchet@35963
   412
    else if subcommand = refresh_tptpN then
blanchet@35963
   413
      refresh_systems_on_tptp ()
blanchet@35963
   414
    else
blanchet@35963
   415
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35963
   416
  end
blanchet@35961
   417
blanchet@49307
   418
fun sledgehammer_trans (((subcommand, params), fact_override), opt_i) =
blanchet@49307
   419
  Toplevel.keep (hammer_away params subcommand opt_i fact_override
blanchet@35964
   420
                 o Toplevel.proof_of)
blanchet@35961
   421
blanchet@43647
   422
fun string_for_raw_param (name, value) = name ^ " = " ^ implode_param value
blanchet@35961
   423
blanchet@35963
   424
fun sledgehammer_params_trans params =
blanchet@35961
   425
  Toplevel.theory
blanchet@35963
   426
      (fold set_default_raw_param params
blanchet@39564
   427
       #> tap (fn thy =>
wenzelm@43232
   428
                  let val ctxt = Proof_Context.init_global thy in
blanchet@40250
   429
                    writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@40250
   430
                             (case default_raw_params ctxt |> rev of
blanchet@40250
   431
                                [] => "none"
blanchet@40250
   432
                              | params =>
blanchet@45591
   433
                                params |> map string_for_raw_param
blanchet@41504
   434
                                       |> sort_strings |> cat_lines))
blanchet@40250
   435
                  end))
blanchet@35961
   436
wenzelm@47823
   437
val parse_query_bang = @{keyword "?"} || @{keyword "!"} || @{keyword "!!"}
blanchet@43647
   438
val parse_key =
blanchet@45639
   439
  Scan.repeat1 (Parse.typ_group || parse_query_bang) >> implode_param
blanchet@43452
   440
val parse_value =
blanchet@45639
   441
  Scan.repeat1 (Parse.xname || Parse.float_number || parse_query_bang)
wenzelm@47823
   442
val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) []
wenzelm@36970
   443
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35963
   444
val parse_fact_refs =
blanchet@39240
   445
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@49308
   446
val parse_fact_override_chunk =
blanchet@49307
   447
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_fact_override)
blanchet@49307
   448
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_fact_override)
blanchet@49307
   449
  || (parse_fact_refs >> only_fact_override)
blanchet@49307
   450
val parse_fact_override =
blanchet@49308
   451
  Scan.optional (Args.parens (Scan.repeat parse_fact_override_chunk
blanchet@49307
   452
                              >> merge_fact_overrides))
blanchet@49307
   453
                no_fact_override
blanchet@35961
   454
blanchet@35961
   455
val _ =
wenzelm@47836
   456
  Outer_Syntax.improper_command @{command_spec "sledgehammer"}
wenzelm@47836
   457
    "search for first-order proof using automatic theorem provers"
wenzelm@47836
   458
    ((Scan.optional Parse.short_ident runN -- parse_params
blanchet@49307
   459
      -- parse_fact_override -- Scan.option Parse.nat) #>> sledgehammer_trans)
blanchet@35961
   460
val _ =
wenzelm@47836
   461
  Outer_Syntax.command @{command_spec "sledgehammer_params"}
wenzelm@47836
   462
    "set and display the default parameters for Sledgehammer"
wenzelm@47836
   463
    (parse_params #>> sledgehammer_params_trans)
blanchet@36394
   464
blanchet@43861
   465
fun try_sledgehammer auto state =
blanchet@43861
   466
  let
blanchet@43861
   467
    val ctxt = Proof.context_of state
blanchet@43862
   468
    val mode = if auto then Auto_Try else Try
blanchet@43861
   469
    val i = 1
blanchet@43861
   470
  in
blanchet@49307
   471
    run_sledgehammer (get_params mode ctxt []) mode i no_fact_override
blanchet@43861
   472
                     (minimize_command [] i) state
blanchet@41175
   473
  end
blanchet@39564
   474
blanchet@49407
   475
val setup = Try.register_tool (sledgehammerN, (40, auto, try_sledgehammer))
blanchet@39564
   476
blanchet@35866
   477
end;