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