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