src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Sat Sep 11 10:21:52 2010 +0200 (2010-09-11 ago)
changeset 39318 ad9a1f9b0558
parent 38997 78ac4468cf9d
child 39324 05452dd66b2b
permissions -rw-r--r--
implemented Auto Sledgehammer
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@38021
     9
  type params = Sledgehammer.params
blanchet@35963
    10
blanchet@39318
    11
  val auto : bool Unsynchronized.ref
blanchet@39318
    12
  val atps : string Unsynchronized.ref
blanchet@39318
    13
  val timeout : int Unsynchronized.ref
blanchet@39318
    14
  val full_types : bool Unsynchronized.ref
blanchet@35963
    15
  val default_params : theory -> (string * string) list -> params
blanchet@39318
    16
  val setup : theory -> theory
blanchet@35963
    17
end;
blanchet@35963
    18
blanchet@35971
    19
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    20
struct
blanchet@35866
    21
blanchet@38028
    22
open ATP_Systems
blanchet@35971
    23
open Sledgehammer_Util
blanchet@38021
    24
open Sledgehammer
blanchet@38988
    25
open Sledgehammer_Minimize
blanchet@35866
    26
blanchet@39318
    27
val auto = Unsynchronized.ref false
blanchet@39318
    28
blanchet@39318
    29
val _ =
blanchet@39318
    30
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@39318
    31
      (Preferences.bool_pref auto "auto-sledgehammer"
blanchet@39318
    32
           "Whether to run Sledgehammer automatically.")
blanchet@39318
    33
blanchet@36394
    34
(** Sledgehammer commands **)
blanchet@36394
    35
blanchet@39318
    36
val no_relevance_override = {add = [], del = [], only = false}
blanchet@39318
    37
fun add_relevance_override ns : relevance_override =
blanchet@35966
    38
  {add = ns, del = [], only = false}
blanchet@39318
    39
fun del_relevance_override ns : relevance_override =
blanchet@35966
    40
  {add = [], del = ns, only = false}
blanchet@35966
    41
fun only_relevance_override ns : relevance_override =
blanchet@35966
    42
  {add = ns, del = [], only = true}
blanchet@36003
    43
fun merge_relevance_override_pairwise (r1 : relevance_override)
blanchet@36003
    44
                                      (r2 : relevance_override) =
blanchet@35966
    45
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    46
   only = #only r1 andalso #only r2}
blanchet@36188
    47
fun merge_relevance_overrides rs =
blanchet@36188
    48
  fold merge_relevance_override_pairwise rs (only_relevance_override [])
blanchet@35965
    49
blanchet@36371
    50
(*** parameters ***)
blanchet@36371
    51
blanchet@36908
    52
val atps = Unsynchronized.ref ""
blanchet@36371
    53
val timeout = Unsynchronized.ref 60
blanchet@36371
    54
val full_types = Unsynchronized.ref false
blanchet@36371
    55
blanchet@36371
    56
val _ =
blanchet@36371
    57
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    58
      (Preferences.string_pref atps
blanchet@36371
    59
          "Sledgehammer: ATPs"
blanchet@36371
    60
          "Default automatic provers (separated by whitespace)")
blanchet@36371
    61
blanchet@36371
    62
val _ =
blanchet@36371
    63
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    64
      (Preferences.int_pref timeout
blanchet@36373
    65
          "Sledgehammer: Time Limit"
blanchet@36371
    66
          "ATPs will be interrupted after this time (in seconds)")
blanchet@36371
    67
blanchet@36371
    68
val _ =
blanchet@36371
    69
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    70
      (Preferences.bool_pref full_types
blanchet@36373
    71
          "Sledgehammer: Full Types" "ATPs will use full type information")
blanchet@36371
    72
blanchet@35963
    73
type raw_param = string * string list
blanchet@35963
    74
blanchet@35963
    75
val default_default_params =
blanchet@38982
    76
  [("blocking", "false"),
blanchet@38982
    77
   ("debug", "false"),
blanchet@35963
    78
   ("verbose", "false"),
blanchet@36143
    79
   ("overlord", "false"),
blanchet@36235
    80
   ("explicit_apply", "false"),
blanchet@38939
    81
   ("relevance_thresholds", "45 85"),
blanchet@38744
    82
   ("max_relevant", "smart"),
blanchet@35963
    83
   ("isar_proof", "false"),
blanchet@38590
    84
   ("isar_shrink_factor", "1")]
blanchet@35963
    85
blanchet@36140
    86
val alias_params =
blanchet@36140
    87
  [("atp", "atps")]
blanchet@36140
    88
val negated_alias_params =
blanchet@38982
    89
  [("non_blocking", "blocking"),
blanchet@38982
    90
   ("no_debug", "debug"),
blanchet@35963
    91
   ("quiet", "verbose"),
blanchet@36143
    92
   ("no_overlord", "overlord"),
blanchet@36399
    93
   ("partial_types", "full_types"),
blanchet@36235
    94
   ("implicit_apply", "explicit_apply"),
blanchet@36908
    95
   ("no_isar_proof", "isar_proof")]
blanchet@35963
    96
blanchet@36281
    97
val params_for_minimize =
blanchet@38100
    98
  ["debug", "verbose", "overlord", "full_types", "isar_proof",
blanchet@38590
    99
   "isar_shrink_factor", "timeout"]
blanchet@36281
   100
blanchet@35971
   101
val property_dependent_params = ["atps", "full_types", "timeout"]
blanchet@35866
   102
blanchet@35963
   103
fun is_known_raw_param s =
blanchet@35963
   104
  AList.defined (op =) default_default_params s orelse
blanchet@36140
   105
  AList.defined (op =) alias_params s orelse
blanchet@36140
   106
  AList.defined (op =) negated_alias_params s orelse
blanchet@38985
   107
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35963
   108
blanchet@35963
   109
fun check_raw_param (s, _) =
blanchet@35963
   110
  if is_known_raw_param s then ()
blanchet@35963
   111
  else error ("Unknown parameter: " ^ quote s ^ ".")
blanchet@35866
   112
blanchet@36140
   113
fun unalias_raw_param (name, value) =
blanchet@36140
   114
  case AList.lookup (op =) alias_params name of
blanchet@36140
   115
    SOME name' => (name', value)
blanchet@36140
   116
  | NONE =>
blanchet@36140
   117
    case AList.lookup (op =) negated_alias_params name of
blanchet@36140
   118
      SOME name' => (name', case value of
blanchet@36140
   119
                              ["false"] => ["true"]
blanchet@36140
   120
                            | ["true"] => ["false"]
blanchet@36140
   121
                            | [] => ["false"]
blanchet@36140
   122
                            | _ => value)
blanchet@36140
   123
    | NONE => (name, value)
blanchet@35963
   124
blanchet@35963
   125
structure Data = Theory_Data(
blanchet@35963
   126
  type T = raw_param list
blanchet@35963
   127
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   128
  val extend = I
blanchet@35963
   129
  fun merge p : T = AList.merge (op =) (K true) p)
blanchet@35866
   130
blanchet@36140
   131
val set_default_raw_param = Data.map o AList.update (op =) o unalias_raw_param
blanchet@35963
   132
fun default_raw_params thy =
blanchet@35963
   133
  Data.get thy
blanchet@36140
   134
  |> fold (AList.default (op =))
blanchet@36908
   135
          [("atps", [case !atps of "" => default_atps_param_value () | s => s]),
blanchet@36140
   136
           ("full_types", [if !full_types then "true" else "false"]),
blanchet@36142
   137
           ("timeout", let val timeout = !timeout in
blanchet@36142
   138
                         [if timeout <= 0 then "none"
blanchet@36142
   139
                          else string_of_int timeout ^ " s"]
blanchet@36142
   140
                       end)]
blanchet@35963
   141
blanchet@35963
   142
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
   143
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
   144
blanchet@39318
   145
fun extract_params auto default_params override_params =
blanchet@35963
   146
  let
blanchet@36140
   147
    val override_params = map unalias_raw_param override_params
blanchet@35963
   148
    val raw_params = rev override_params @ rev default_params
blanchet@35963
   149
    val lookup = Option.map (space_implode " ") o AList.lookup (op =) raw_params
blanchet@35963
   150
    val lookup_string = the_default "" o lookup
blanchet@35963
   151
    fun general_lookup_bool option default_value name =
blanchet@35963
   152
      case lookup name of
blanchet@35970
   153
        SOME s => parse_bool_option option name s
blanchet@35963
   154
      | NONE => default_value
blanchet@35963
   155
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   156
    val lookup_bool_option = general_lookup_bool true NONE
blanchet@35963
   157
    fun lookup_time name =
blanchet@39318
   158
      case lookup name of
blanchet@39318
   159
        SOME s => parse_time_option name s
blanchet@39318
   160
      | NONE => NONE
blanchet@35966
   161
    fun lookup_int name =
blanchet@35963
   162
      case lookup name of
blanchet@35966
   163
        NONE => 0
blanchet@35966
   164
      | SOME s => case Int.fromString s of
blanchet@35966
   165
                    SOME n => n
blanchet@35966
   166
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   167
                                   " must be assigned an integer value.")
blanchet@38745
   168
    fun lookup_int_pair name =
blanchet@38745
   169
      case lookup name of
blanchet@38745
   170
        NONE => (0, 0)
blanchet@38745
   171
      | SOME s => case s |> space_explode " " |> map Int.fromString of
blanchet@38745
   172
                    [SOME n1, SOME n2] => (n1, n2)
blanchet@38745
   173
                  | _ => error ("Parameter " ^ quote name ^
blanchet@38745
   174
                                "must be assigned a pair of integer values \
blanchet@38745
   175
                                \(e.g., \"60 95\")")
blanchet@38589
   176
    fun lookup_int_option name =
blanchet@38589
   177
      case lookup name of
blanchet@38589
   178
        SOME "smart" => NONE
blanchet@38589
   179
      | _ => SOME (lookup_int name)
blanchet@39318
   180
    val blocking = auto orelse lookup_bool "blocking"
blanchet@39318
   181
    val debug = not auto andalso lookup_bool "debug"
blanchet@39318
   182
    val verbose = debug orelse (not auto andalso lookup_bool "verbose")
blanchet@36143
   183
    val overlord = lookup_bool "overlord"
blanchet@39318
   184
    val atps = lookup_string "atps" |> space_explode " " |> auto ? single o hd
blanchet@35963
   185
    val full_types = lookup_bool "full_types"
blanchet@36235
   186
    val explicit_apply = lookup_bool "explicit_apply"
blanchet@38745
   187
    val relevance_thresholds =
blanchet@38745
   188
      lookup_int_pair "relevance_thresholds"
blanchet@38745
   189
      |> pairself (fn n => 0.01 * Real.fromInt n)
blanchet@38744
   190
    val max_relevant = lookup_int_option "max_relevant"
blanchet@35963
   191
    val isar_proof = lookup_bool "isar_proof"
blanchet@36924
   192
    val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
blanchet@39318
   193
    val timeout = (if auto then NONE else lookup_time "timeout") |> the_timeout
blanchet@38985
   194
    val expect = lookup_string "expect"
blanchet@35963
   195
  in
blanchet@38982
   196
    {blocking = blocking, debug = debug, verbose = verbose, overlord = overlord,
blanchet@38982
   197
     atps = atps, full_types = full_types, explicit_apply = explicit_apply,
blanchet@38745
   198
     relevance_thresholds = relevance_thresholds, max_relevant = max_relevant,
blanchet@38997
   199
     isar_proof = isar_proof, isar_shrink_factor = isar_shrink_factor,
blanchet@38997
   200
     timeout = timeout, expect = expect}
blanchet@35963
   201
  end
blanchet@35866
   202
blanchet@39318
   203
fun get_params auto thy = extract_params auto (default_raw_params thy)
blanchet@39318
   204
fun default_params thy = get_params false thy o map (apsnd single)
blanchet@35866
   205
blanchet@36373
   206
(* Sledgehammer the given subgoal *)
blanchet@36373
   207
blanchet@36373
   208
val sledgehammerN = "sledgehammer"
blanchet@36373
   209
val sledgehammer_paramsN = "sledgehammer_params"
blanchet@36373
   210
blanchet@35965
   211
val runN = "run"
blanchet@35965
   212
val minimizeN = "minimize"
blanchet@35965
   213
val messagesN = "messages"
blanchet@35965
   214
val available_atpsN = "available_atps"
blanchet@35965
   215
val running_atpsN = "running_atps"
blanchet@35965
   216
val kill_atpsN = "kill_atps"
blanchet@35965
   217
val refresh_tptpN = "refresh_tptp"
blanchet@35866
   218
blanchet@36281
   219
val is_raw_param_relevant_for_minimize =
blanchet@36281
   220
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   221
fun string_for_raw_param (key, values) =
blanchet@36481
   222
  key ^ (case space_implode " " values of "" => "" | value => " = " ^ value)
blanchet@36281
   223
blanchet@37171
   224
fun minimize_command override_params i atp_name fact_names =
blanchet@36373
   225
  sledgehammerN ^ " " ^ minimizeN ^ " [atp = " ^ atp_name ^
blanchet@36281
   226
  (override_params |> filter is_raw_param_relevant_for_minimize
blanchet@36281
   227
                   |> implode o map (prefix ", " o string_for_raw_param)) ^
blanchet@37171
   228
  "] (" ^ space_implode " " fact_names ^ ")" ^
blanchet@36281
   229
  (if i = 1 then "" else " " ^ string_of_int i)
blanchet@36281
   230
blanchet@35966
   231
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   232
  let
blanchet@35963
   233
    val thy = Proof.theory_of state
blanchet@36481
   234
    val _ = app check_raw_param override_params
blanchet@35965
   235
  in
blanchet@35965
   236
    if subcommand = runN then
blanchet@36281
   237
      let val i = the_default 1 opt_i in
blanchet@39318
   238
        run_sledgehammer (get_params false thy override_params) false i
blanchet@39318
   239
                         relevance_override (minimize_command override_params i)
blanchet@39318
   240
                         state
blanchet@39318
   241
        |> K ()
blanchet@36281
   242
      end
blanchet@35965
   243
    else if subcommand = minimizeN then
blanchet@39318
   244
      run_minimize (get_params false thy override_params) (the_default 1 opt_i)
blanchet@38590
   245
                   (#add relevance_override) state
blanchet@35965
   246
    else if subcommand = messagesN then
blanchet@35965
   247
      messages opt_i
blanchet@35965
   248
    else if subcommand = available_atpsN then
blanchet@35965
   249
      available_atps thy
blanchet@35965
   250
    else if subcommand = running_atpsN then
blanchet@35965
   251
      running_atps ()
blanchet@35965
   252
    else if subcommand = kill_atpsN then
blanchet@35965
   253
      kill_atps ()
blanchet@35965
   254
    else if subcommand = refresh_tptpN then
blanchet@35965
   255
      refresh_systems_on_tptp ()
blanchet@35965
   256
    else
blanchet@35965
   257
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   258
  end
blanchet@35963
   259
blanchet@35966
   260
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   261
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   262
                 o Toplevel.proof_of)
blanchet@35963
   263
blanchet@35963
   264
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35963
   265
blanchet@35965
   266
fun sledgehammer_params_trans params =
blanchet@35963
   267
  Toplevel.theory
blanchet@35965
   268
      (fold set_default_raw_param params
blanchet@39318
   269
       #> tap (fn thy =>
blanchet@35963
   270
                  writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@35963
   271
                           (case rev (default_raw_params thy) of
blanchet@35963
   272
                              [] => "none"
blanchet@35963
   273
                            | params =>
blanchet@35963
   274
                              (map check_raw_param params;
blanchet@35963
   275
                               params |> map string_for_raw_param
blanchet@35963
   276
                                      |> sort_strings |> cat_lines)))))
blanchet@35963
   277
wenzelm@36960
   278
val parse_key = Scan.repeat1 Parse.typ_group >> space_implode " "
wenzelm@36960
   279
val parse_value = Scan.repeat1 Parse.xname
wenzelm@36960
   280
val parse_param = parse_key -- Scan.optional (Parse.$$$ "=" |-- parse_value) []
wenzelm@36960
   281
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35965
   282
val parse_fact_refs =
blanchet@38996
   283
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@35966
   284
val parse_relevance_chunk =
blanchet@39318
   285
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_relevance_override)
blanchet@39318
   286
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_relevance_override)
blanchet@36188
   287
  || (parse_fact_refs >> only_relevance_override)
blanchet@35966
   288
val parse_relevance_override =
blanchet@36188
   289
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   290
                              >> merge_relevance_overrides))
blanchet@39318
   291
                no_relevance_override
blanchet@35963
   292
val parse_sledgehammer_command =
blanchet@37399
   293
  (Scan.optional Parse.short_ident runN -- parse_params
blanchet@37399
   294
   -- parse_relevance_override -- Scan.option Parse.nat) #>> sledgehammer_trans
blanchet@35963
   295
val parse_sledgehammer_params_command =
blanchet@35963
   296
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   297
blanchet@35963
   298
val _ =
wenzelm@36960
   299
  Outer_Syntax.improper_command sledgehammerN
wenzelm@36960
   300
      "search for first-order proof using automatic theorem provers" Keyword.diag
blanchet@36394
   301
      parse_sledgehammer_command
blanchet@35963
   302
val _ =
wenzelm@36960
   303
  Outer_Syntax.command sledgehammer_paramsN
wenzelm@36960
   304
      "set and display the default parameters for Sledgehammer" Keyword.thy_decl
blanchet@36394
   305
      parse_sledgehammer_params_command
blanchet@36394
   306
blanchet@39318
   307
fun auto_sledgehammer state =
blanchet@39318
   308
  if not (!auto) then
blanchet@39318
   309
    (false, state)
blanchet@39318
   310
  else
blanchet@39318
   311
    let val thy = Proof.theory_of state in
blanchet@39318
   312
      run_sledgehammer (get_params true thy []) true 1 no_relevance_override
blanchet@39318
   313
                       (minimize_command [] 1) state
blanchet@39318
   314
    end
blanchet@39318
   315
blanchet@39318
   316
val setup =
blanchet@39318
   317
  Auto_Counterexample.register_tool ("sledgehammer", auto_sledgehammer)
blanchet@39318
   318
blanchet@35866
   319
end;