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