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