src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Mon May 30 17:00:38 2011 +0200 (2011-05-30 ago)
changeset 43064 b6e61d22fa61
parent 43057 884b0bc19de4
child 43085 0a2f5b86bdd7
permissions -rw-r--r--
made "explicit_apply" smarter -- no need to force explicit applications in minimizer on all constants, better do it more fine granularly
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@43020
    11
  val sledgehammerN : string
blanchet@39318
    12
  val auto : bool Unsynchronized.ref
blanchet@40059
    13
  val provers : string Unsynchronized.ref
blanchet@39318
    14
  val timeout : int Unsynchronized.ref
blanchet@39318
    15
  val full_types : bool Unsynchronized.ref
blanchet@40069
    16
  val default_params : Proof.context -> (string * string) list -> params
blanchet@39318
    17
  val setup : theory -> theory
blanchet@35963
    18
end;
blanchet@35963
    19
blanchet@35971
    20
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    21
struct
blanchet@35866
    22
blanchet@38028
    23
open ATP_Systems
blanchet@35971
    24
open Sledgehammer_Util
blanchet@41138
    25
open Sledgehammer_ATP_Translate
blanchet@41087
    26
open Sledgehammer_Provers
blanchet@38988
    27
open Sledgehammer_Minimize
blanchet@41087
    28
open Sledgehammer_Run
blanchet@35866
    29
blanchet@43020
    30
val sledgehammerN = "sledgehammer"
blanchet@43020
    31
val sledgehammer_paramsN = "sledgehammer_params"
blanchet@43020
    32
blanchet@43020
    33
val runN = "run"
blanchet@43020
    34
val minN = "min"
blanchet@43020
    35
val messagesN = "messages"
blanchet@43020
    36
val supported_proversN = "supported_provers"
blanchet@43020
    37
val running_proversN = "running_provers"
blanchet@43020
    38
val kill_proversN = "kill_provers"
blanchet@43020
    39
val refresh_tptpN = "refresh_tptp"
blanchet@43020
    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@39318
    50
val no_relevance_override = {add = [], del = [], only = false}
blanchet@39318
    51
fun add_relevance_override ns : relevance_override =
blanchet@35966
    52
  {add = ns, del = [], only = false}
blanchet@39318
    53
fun del_relevance_override ns : relevance_override =
blanchet@35966
    54
  {add = [], del = ns, only = false}
blanchet@35966
    55
fun only_relevance_override ns : relevance_override =
blanchet@35966
    56
  {add = ns, del = [], only = true}
blanchet@36003
    57
fun merge_relevance_override_pairwise (r1 : relevance_override)
blanchet@36003
    58
                                      (r2 : relevance_override) =
blanchet@35966
    59
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    60
   only = #only r1 andalso #only r2}
blanchet@36188
    61
fun merge_relevance_overrides rs =
blanchet@36188
    62
  fold merge_relevance_override_pairwise rs (only_relevance_override [])
blanchet@35965
    63
blanchet@36371
    64
(*** parameters ***)
blanchet@36371
    65
blanchet@40059
    66
val provers = Unsynchronized.ref ""
blanchet@39335
    67
val timeout = Unsynchronized.ref 30
blanchet@36371
    68
val full_types = Unsynchronized.ref false
blanchet@36371
    69
blanchet@36371
    70
val _ =
blanchet@36371
    71
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@40059
    72
      (Preferences.string_pref provers
blanchet@40059
    73
          "Sledgehammer: Provers"
blanchet@36371
    74
          "Default automatic provers (separated by whitespace)")
blanchet@36371
    75
blanchet@36371
    76
val _ =
blanchet@36371
    77
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    78
      (Preferences.int_pref timeout
blanchet@36373
    79
          "Sledgehammer: Time Limit"
blanchet@36371
    80
          "ATPs will be interrupted after this time (in seconds)")
blanchet@36371
    81
blanchet@36371
    82
val _ =
blanchet@36371
    83
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    84
      (Preferences.bool_pref full_types
blanchet@36373
    85
          "Sledgehammer: Full Types" "ATPs will use full type information")
blanchet@36371
    86
blanchet@35963
    87
type raw_param = string * string list
blanchet@35963
    88
blanchet@35963
    89
val default_default_params =
blanchet@41208
    90
  [("debug", "false"),
blanchet@35963
    91
   ("verbose", "false"),
blanchet@36143
    92
   ("overlord", "false"),
blanchet@41208
    93
   ("blocking", "false"),
blanchet@42523
    94
   ("type_sys", "smart"),
blanchet@42180
    95
   ("relevance_thresholds", "0.45 0.85"),
blanchet@42180
    96
   ("max_relevant", "smart"),
blanchet@42783
    97
   ("max_mono_iters", "3"),
blanchet@42783
    98
   ("max_new_mono_instances", "400"),
blanchet@43064
    99
   ("explicit_apply", "smart"),
blanchet@35963
   100
   ("isar_proof", "false"),
blanchet@42443
   101
   ("isar_shrink_factor", "1"),
blanchet@43011
   102
   ("slicing", "true"),
blanchet@43034
   103
   ("preplay_timeout", "4")]
blanchet@35963
   104
blanchet@36140
   105
val alias_params =
blanchet@40059
   106
  [("prover", "provers"),
blanchet@40059
   107
   ("atps", "provers"), (* FIXME: legacy *)
blanchet@40059
   108
   ("atp", "provers")]  (* FIXME: legacy *)
blanchet@36140
   109
val negated_alias_params =
blanchet@41208
   110
  [("no_debug", "debug"),
blanchet@35963
   111
   ("quiet", "verbose"),
blanchet@36143
   112
   ("no_overlord", "overlord"),
blanchet@41208
   113
   ("non_blocking", "blocking"),
blanchet@36399
   114
   ("partial_types", "full_types"),
blanchet@36235
   115
   ("implicit_apply", "explicit_apply"),
blanchet@42443
   116
   ("no_isar_proof", "isar_proof"),
blanchet@42443
   117
   ("no_slicing", "slicing")]
blanchet@35963
   118
blanchet@36281
   119
val params_for_minimize =
blanchet@42724
   120
  ["debug", "verbose", "overlord", "type_sys", "full_types", "max_mono_iters",
blanchet@43064
   121
   "max_new_mono_instances", "explicit_apply", "isar_proof",
blanchet@43064
   122
   "isar_shrink_factor", "timeout", "preplay_timeout"]
blanchet@36281
   123
blanchet@40059
   124
val property_dependent_params = ["provers", "full_types", "timeout"]
blanchet@35866
   125
blanchet@35963
   126
fun is_known_raw_param s =
blanchet@35963
   127
  AList.defined (op =) default_default_params s orelse
blanchet@36140
   128
  AList.defined (op =) alias_params s orelse
blanchet@36140
   129
  AList.defined (op =) negated_alias_params s orelse
blanchet@38985
   130
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35963
   131
blanchet@41258
   132
fun is_prover_list ctxt s =
blanchet@41258
   133
  case space_explode " " s of
blanchet@41727
   134
    ss as _ :: _ => forall (is_prover_supported ctxt) ss
blanchet@41258
   135
  | _ => false
blanchet@41258
   136
blanchet@42776
   137
(* Secret feature: "provers =" and "type_sys =" can be left out. *)
blanchet@41258
   138
fun check_and_repair_raw_param ctxt (name, value) =
blanchet@42776
   139
  if is_known_raw_param name then
blanchet@42776
   140
    (name, value)
blanchet@42776
   141
  else if is_prover_list ctxt name andalso null value then
blanchet@42776
   142
    ("provers", [name])
blanchet@42776
   143
  else if can type_sys_from_string name andalso null value then
blanchet@42776
   144
    ("type_sys", [name])
blanchet@42776
   145
  else
blanchet@42776
   146
    error ("Unknown parameter: " ^ quote name ^ ".")
blanchet@35866
   147
blanchet@36140
   148
fun unalias_raw_param (name, value) =
blanchet@36140
   149
  case AList.lookup (op =) alias_params name of
blanchet@36140
   150
    SOME name' => (name', value)
blanchet@36140
   151
  | NONE =>
blanchet@36140
   152
    case AList.lookup (op =) negated_alias_params name of
blanchet@36140
   153
      SOME name' => (name', case value of
blanchet@36140
   154
                              ["false"] => ["true"]
blanchet@36140
   155
                            | ["true"] => ["false"]
blanchet@36140
   156
                            | [] => ["false"]
blanchet@36140
   157
                            | _ => value)
blanchet@36140
   158
    | NONE => (name, value)
blanchet@35963
   159
blanchet@43009
   160
(* Ensure that type systems such as "simple!" and "mangled_preds?" are handled
blanchet@43009
   161
   correctly. *)
blanchet@43009
   162
fun implode_param [s, "?"] = s ^ "?"
blanchet@43009
   163
  | implode_param [s, "!"] = s ^ "!"
blanchet@43009
   164
  | implode_param ss = space_implode " " ss
blanchet@43009
   165
wenzelm@41472
   166
structure Data = Theory_Data
wenzelm@41472
   167
(
blanchet@35963
   168
  type T = raw_param list
blanchet@35963
   169
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   170
  val extend = I
wenzelm@41472
   171
  fun merge data : T = AList.merge (op =) (K true) data
wenzelm@41472
   172
)
blanchet@35866
   173
blanchet@41727
   174
fun remotify_prover_if_supported_and_not_already_remote ctxt name =
blanchet@40072
   175
  if String.isPrefix remote_prefix name then
blanchet@40072
   176
    SOME name
blanchet@40072
   177
  else
blanchet@40072
   178
    let val remote_name = remote_prefix ^ name in
blanchet@41727
   179
      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
blanchet@40072
   180
    end
blanchet@40072
   181
blanchet@40072
   182
fun remotify_prover_if_not_installed ctxt name =
blanchet@41727
   183
  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
blanchet@40941
   184
    SOME name
blanchet@40941
   185
  else
blanchet@41727
   186
    remotify_prover_if_supported_and_not_already_remote ctxt name
blanchet@40072
   187
blanchet@43009
   188
fun avoid_too_many_threads _ _ [] = []
blanchet@43009
   189
  | avoid_too_many_threads _ (0, 0) _ = []
blanchet@43009
   190
  | avoid_too_many_threads ctxt (0, max_remote) provers =
blanchet@43009
   191
    provers
blanchet@43009
   192
    |> map_filter (remotify_prover_if_supported_and_not_already_remote ctxt)
blanchet@43009
   193
    |> take max_remote
blanchet@43009
   194
  | avoid_too_many_threads _ (max_local, 0) provers =
blanchet@43009
   195
    provers
blanchet@43009
   196
    |> filter_out (String.isPrefix remote_prefix)
blanchet@43009
   197
    |> take max_local
blanchet@43009
   198
  | avoid_too_many_threads ctxt max_local_and_remote (prover :: provers) =
blanchet@43009
   199
    let
blanchet@43009
   200
      val max_local_and_remote =
blanchet@43009
   201
        max_local_and_remote
blanchet@43009
   202
        |> (if String.isPrefix remote_prefix prover then apsnd else apfst)
blanchet@43009
   203
              (Integer.add ~1)
blanchet@43009
   204
    in prover :: avoid_too_many_threads ctxt max_local_and_remote provers end
blanchet@40059
   205
blanchet@43009
   206
val max_default_remote_threads = 4
blanchet@42688
   207
blanchet@40059
   208
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@40059
   209
   timeout, it makes sense to put SPASS first. *)
blanchet@40069
   210
fun default_provers_param_value ctxt =
blanchet@43009
   211
  [spassN, eN, vampireN, SMT_Solver.solver_name_of ctxt, sine_eN, waldmeisterN]
blanchet@40941
   212
  |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@43009
   213
  |> avoid_too_many_threads ctxt (Multithreading.max_threads_value (),
blanchet@43009
   214
                                  max_default_remote_threads)
blanchet@42776
   215
  |> implode_param
blanchet@40059
   216
blanchet@36140
   217
val set_default_raw_param = Data.map o AList.update (op =) o unalias_raw_param
blanchet@40069
   218
fun default_raw_params ctxt =
wenzelm@42361
   219
  let val thy = Proof_Context.theory_of ctxt in
blanchet@40069
   220
    Data.get thy
blanchet@40069
   221
    |> fold (AList.default (op =))
blanchet@40069
   222
            [("provers", [case !provers of
blanchet@40069
   223
                            "" => default_provers_param_value ctxt
blanchet@40069
   224
                          | s => s]),
blanchet@40069
   225
             ("full_types", [if !full_types then "true" else "false"]),
blanchet@40069
   226
             ("timeout", let val timeout = !timeout in
blanchet@40069
   227
                           [if timeout <= 0 then "none"
blanchet@40341
   228
                            else string_of_int timeout]
blanchet@40069
   229
                         end)]
blanchet@40069
   230
  end
blanchet@35963
   231
blanchet@35963
   232
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
   233
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
   234
blanchet@43021
   235
fun extract_params mode default_params override_params =
blanchet@35963
   236
  let
blanchet@36140
   237
    val override_params = map unalias_raw_param override_params
blanchet@35963
   238
    val raw_params = rev override_params @ rev default_params
blanchet@42776
   239
    val lookup = Option.map implode_param o AList.lookup (op =) raw_params
blanchet@35963
   240
    val lookup_string = the_default "" o lookup
blanchet@35963
   241
    fun general_lookup_bool option default_value name =
blanchet@35963
   242
      case lookup name of
blanchet@35970
   243
        SOME s => parse_bool_option option name s
blanchet@35963
   244
      | NONE => default_value
blanchet@35963
   245
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   246
    fun lookup_time name =
blanchet@39318
   247
      case lookup name of
blanchet@39318
   248
        SOME s => parse_time_option name s
blanchet@39318
   249
      | NONE => NONE
blanchet@35966
   250
    fun lookup_int name =
blanchet@35963
   251
      case lookup name of
blanchet@35966
   252
        NONE => 0
blanchet@35966
   253
      | SOME s => case Int.fromString s of
blanchet@35966
   254
                    SOME n => n
blanchet@35966
   255
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   256
                                   " must be assigned an integer value.")
blanchet@40343
   257
    fun lookup_real_pair name =
blanchet@38745
   258
      case lookup name of
blanchet@40343
   259
        NONE => (0.0, 0.0)
blanchet@40343
   260
      | SOME s => case s |> space_explode " " |> map Real.fromString of
blanchet@40343
   261
                    [SOME r1, SOME r2] => (r1, r2)
blanchet@38745
   262
                  | _ => error ("Parameter " ^ quote name ^
blanchet@40343
   263
                                "must be assigned a pair of floating-point \
blanchet@40343
   264
                                \values (e.g., \"0.6 0.95\")")
blanchet@43064
   265
    fun lookup_option lookup' name =
blanchet@38589
   266
      case lookup name of
blanchet@38589
   267
        SOME "smart" => NONE
blanchet@43064
   268
      | _ => SOME (lookup' name)
blanchet@43021
   269
    val debug = mode <> Auto_Try andalso lookup_bool "debug"
blanchet@43021
   270
    val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose")
blanchet@36143
   271
    val overlord = lookup_bool "overlord"
blanchet@42995
   272
    val blocking =
blanchet@43021
   273
      Isabelle_Process.is_active () orelse mode <> Normal orelse debug orelse
blanchet@42995
   274
      lookup_bool "blocking"
blanchet@40059
   275
    val provers = lookup_string "provers" |> space_explode " "
blanchet@43021
   276
                  |> mode = Auto_Try ? single o hd
blanchet@42548
   277
    val type_sys =
blanchet@43021
   278
      if mode = Auto_Try then
blanchet@42736
   279
        Smart_Type_Sys true
blanchet@42736
   280
      else case lookup_string "type_sys" of
blanchet@43021
   281
        "smart" => Smart_Type_Sys (mode = Try orelse lookup_bool "full_types")
blanchet@42613
   282
      | s => Dumb_Type_Sys (type_sys_from_string s)
blanchet@42180
   283
    val relevance_thresholds = lookup_real_pair "relevance_thresholds"
blanchet@43064
   284
    val max_relevant = lookup_option lookup_int "max_relevant"
blanchet@42724
   285
    val max_mono_iters = lookup_int "max_mono_iters"
blanchet@42740
   286
    val max_new_mono_instances = lookup_int "max_new_mono_instances"
blanchet@43064
   287
    val explicit_apply = lookup_option lookup_bool "explicit_apply"
blanchet@35963
   288
    val isar_proof = lookup_bool "isar_proof"
blanchet@36924
   289
    val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
blanchet@43021
   290
    val slicing = mode <> Auto_Try andalso lookup_bool "slicing"
blanchet@43021
   291
    val timeout =
blanchet@43021
   292
      (if mode = Auto_Try then NONE else lookup_time "timeout") |> the_timeout
blanchet@43015
   293
    val preplay_timeout =
blanchet@43021
   294
      if mode = Auto_Try then Time.zeroTime
blanchet@43015
   295
      else lookup_time "preplay_timeout" |> the_timeout
blanchet@38985
   296
    val expect = lookup_string "expect"
blanchet@35963
   297
  in
blanchet@41208
   298
    {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
blanchet@42180
   299
     provers = provers, relevance_thresholds = relevance_thresholds,
blanchet@42724
   300
     max_relevant = max_relevant, max_mono_iters = max_mono_iters,
blanchet@42740
   301
     max_new_mono_instances = max_new_mono_instances, type_sys = type_sys,
blanchet@42724
   302
     explicit_apply = explicit_apply, isar_proof = isar_proof,
blanchet@42724
   303
     isar_shrink_factor = isar_shrink_factor, slicing = slicing,
blanchet@43015
   304
     timeout = timeout, preplay_timeout = preplay_timeout, expect = expect}
blanchet@35963
   305
  end
blanchet@35866
   306
blanchet@43021
   307
fun get_params mode ctxt = extract_params mode (default_raw_params ctxt)
blanchet@43021
   308
fun default_params thy = get_params Normal thy o map (apsnd single)
blanchet@35866
   309
blanchet@36373
   310
(* Sledgehammer the given subgoal *)
blanchet@36373
   311
blanchet@43051
   312
val default_minimize_prover = Metis_Tactics.metisN
blanchet@43051
   313
blanchet@36281
   314
val is_raw_param_relevant_for_minimize =
blanchet@36281
   315
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   316
fun string_for_raw_param (key, values) =
blanchet@42776
   317
  key ^ (case implode_param values of "" => "" | value => " = " ^ value)
blanchet@36281
   318
blanchet@40059
   319
fun minimize_command override_params i prover_name fact_names =
blanchet@43057
   320
  let
blanchet@43057
   321
    val params =
blanchet@43057
   322
      override_params
blanchet@43057
   323
      |> filter is_raw_param_relevant_for_minimize
blanchet@43057
   324
      |> map string_for_raw_param
blanchet@43057
   325
      |> (if prover_name = default_minimize_prover then I else cons prover_name)
blanchet@43057
   326
      |> space_implode ", "
blanchet@43057
   327
  in
blanchet@43057
   328
    sledgehammerN ^ " " ^ minN ^
blanchet@43057
   329
    (if params = "" then "" else enclose " [" "]" params) ^
blanchet@43057
   330
    " (" ^ space_implode " " fact_names ^ ")" ^
blanchet@43057
   331
    (if i = 1 then "" else " " ^ string_of_int i)
blanchet@43057
   332
  end
blanchet@36281
   333
blanchet@35966
   334
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   335
  let
blanchet@40069
   336
    val ctxt = Proof.context_of state
blanchet@41258
   337
    val override_params =
blanchet@41258
   338
      override_params |> map (check_and_repair_raw_param ctxt)
blanchet@35965
   339
  in
blanchet@35965
   340
    if subcommand = runN then
blanchet@36281
   341
      let val i = the_default 1 opt_i in
blanchet@43021
   342
        run_sledgehammer (get_params Normal ctxt override_params) Normal i
blanchet@39318
   343
                         relevance_override (minimize_command override_params i)
blanchet@39318
   344
                         state
blanchet@39318
   345
        |> K ()
blanchet@36281
   346
      end
blanchet@43008
   347
    else if subcommand = minN then
blanchet@43051
   348
      run_minimize (get_params Minimize ctxt
blanchet@43051
   349
                               (("provers", [default_minimize_prover]) ::
blanchet@43051
   350
                                override_params))
blanchet@43021
   351
                   (the_default 1 opt_i) (#add relevance_override) state
blanchet@35965
   352
    else if subcommand = messagesN then
blanchet@35965
   353
      messages opt_i
blanchet@41727
   354
    else if subcommand = supported_proversN then
blanchet@41727
   355
      supported_provers ctxt
blanchet@40059
   356
    else if subcommand = running_proversN then
blanchet@40059
   357
      running_provers ()
blanchet@40059
   358
    else if subcommand = kill_proversN then
blanchet@40059
   359
      kill_provers ()
blanchet@35965
   360
    else if subcommand = refresh_tptpN then
blanchet@35965
   361
      refresh_systems_on_tptp ()
blanchet@35965
   362
    else
blanchet@35965
   363
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   364
  end
blanchet@35963
   365
blanchet@35966
   366
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   367
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   368
                 o Toplevel.proof_of)
blanchet@35963
   369
blanchet@42776
   370
fun string_for_raw_param (name, value) = name ^ " = " ^ implode_param value
blanchet@35963
   371
blanchet@35965
   372
fun sledgehammer_params_trans params =
blanchet@35963
   373
  Toplevel.theory
blanchet@35965
   374
      (fold set_default_raw_param params
blanchet@39318
   375
       #> tap (fn thy =>
wenzelm@42361
   376
                  let val ctxt = Proof_Context.init_global thy in
blanchet@40069
   377
                    writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@40069
   378
                             (case default_raw_params ctxt |> rev of
blanchet@40069
   379
                                [] => "none"
blanchet@40069
   380
                              | params =>
blanchet@41258
   381
                                params |> map (check_and_repair_raw_param ctxt)
blanchet@41258
   382
                                       |> map string_for_raw_param
blanchet@41258
   383
                                       |> sort_strings |> cat_lines))
blanchet@40069
   384
                  end))
blanchet@35963
   385
blanchet@42776
   386
val parse_key =
blanchet@42776
   387
  Scan.repeat1 (Parse.typ_group || Parse.$$$ "?" || Parse.$$$ "!")
blanchet@42776
   388
               >> implode_param
blanchet@42581
   389
val parse_value =
blanchet@42589
   390
  Scan.repeat1 (Parse.xname || Parse.float_number || Parse.$$$ "?"
blanchet@42581
   391
                || Parse.$$$ "!")
wenzelm@36960
   392
val parse_param = parse_key -- Scan.optional (Parse.$$$ "=" |-- parse_value) []
wenzelm@36960
   393
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35965
   394
val parse_fact_refs =
blanchet@38996
   395
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@35966
   396
val parse_relevance_chunk =
blanchet@39318
   397
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_relevance_override)
blanchet@39318
   398
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_relevance_override)
blanchet@36188
   399
  || (parse_fact_refs >> only_relevance_override)
blanchet@35966
   400
val parse_relevance_override =
blanchet@36188
   401
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   402
                              >> merge_relevance_overrides))
blanchet@39318
   403
                no_relevance_override
blanchet@35963
   404
val parse_sledgehammer_command =
blanchet@37399
   405
  (Scan.optional Parse.short_ident runN -- parse_params
blanchet@37399
   406
   -- parse_relevance_override -- Scan.option Parse.nat) #>> sledgehammer_trans
blanchet@35963
   407
val parse_sledgehammer_params_command =
blanchet@35963
   408
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   409
blanchet@35963
   410
val _ =
wenzelm@36960
   411
  Outer_Syntax.improper_command sledgehammerN
wenzelm@36960
   412
      "search for first-order proof using automatic theorem provers" Keyword.diag
blanchet@36394
   413
      parse_sledgehammer_command
blanchet@35963
   414
val _ =
wenzelm@36960
   415
  Outer_Syntax.command sledgehammer_paramsN
wenzelm@36960
   416
      "set and display the default parameters for Sledgehammer" Keyword.thy_decl
blanchet@36394
   417
      parse_sledgehammer_params_command
blanchet@36394
   418
blanchet@43020
   419
fun try_sledgehammer auto state =
blanchet@43020
   420
  let
blanchet@43020
   421
    val ctxt = Proof.context_of state
blanchet@43021
   422
    val mode = if auto then Auto_Try else Try
blanchet@43020
   423
    val i = 1
blanchet@43020
   424
  in
blanchet@43021
   425
    run_sledgehammer (get_params mode ctxt []) mode i no_relevance_override
blanchet@43020
   426
                     (minimize_command [] i) state
blanchet@40931
   427
  end
blanchet@39318
   428
blanchet@43024
   429
val setup = Try.register_tool (sledgehammerN, (40, auto, try_sledgehammer))
blanchet@39318
   430
blanchet@35866
   431
end;