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