src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Fri Apr 23 16:15:35 2010 +0200 (2010-04-23 ago)
changeset 36371 8c83ea1a7740
parent 36290 c29283184c7a
child 36373 66af0a49de39
permissions -rw-r--r--
move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet@35866
     1
(*  Title:      HOL/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@35971
    20
open Sledgehammer_Util
blanchet@35866
    21
open ATP_Manager
blanchet@35866
    22
open ATP_Minimal
blanchet@35965
    23
open ATP_Wrapper
blanchet@35866
    24
blanchet@35866
    25
structure K = OuterKeyword and P = OuterParse
blanchet@35866
    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@36188
    33
val no_relevance_override = add_to_relevance_override []
blanchet@36003
    34
fun merge_relevance_override_pairwise (r1 : relevance_override)
blanchet@36003
    35
                                      (r2 : relevance_override) =
blanchet@35966
    36
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    37
   only = #only r1 andalso #only r2}
blanchet@36188
    38
fun merge_relevance_overrides rs =
blanchet@36188
    39
  fold merge_relevance_override_pairwise rs (only_relevance_override [])
blanchet@35965
    40
blanchet@36371
    41
(*** parameters ***)
blanchet@36371
    42
blanchet@36371
    43
val atps = Unsynchronized.ref (default_atps_param_value ())
blanchet@36371
    44
val timeout = Unsynchronized.ref 60
blanchet@36371
    45
val full_types = Unsynchronized.ref false
blanchet@36371
    46
blanchet@36371
    47
val _ =
blanchet@36371
    48
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    49
      (Preferences.string_pref atps
blanchet@36371
    50
          "Sledgehammer: ATPs"
blanchet@36371
    51
          "Default automatic provers (separated by whitespace)")
blanchet@36371
    52
blanchet@36371
    53
val _ =
blanchet@36371
    54
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    55
      (Preferences.int_pref timeout
blanchet@36371
    56
          "Sledgehammer: Time limit"
blanchet@36371
    57
          "ATPs will be interrupted after this time (in seconds)")
blanchet@36371
    58
blanchet@36371
    59
val _ =
blanchet@36371
    60
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    61
      (Preferences.bool_pref full_types
blanchet@36371
    62
          "Sledgehammer: Full types" "ATPs will use full type information")
blanchet@36371
    63
blanchet@35963
    64
type raw_param = string * string list
blanchet@35963
    65
blanchet@35963
    66
val default_default_params =
blanchet@35963
    67
  [("debug", "false"),
blanchet@35963
    68
   ("verbose", "false"),
blanchet@36143
    69
   ("overlord", "false"),
blanchet@36235
    70
   ("explicit_apply", "false"),
blanchet@36058
    71
   ("respect_no_atp", "true"),
blanchet@35966
    72
   ("relevance_threshold", "50"),
blanchet@36058
    73
   ("convergence", "320"),
blanchet@36220
    74
   ("theory_relevant", "smart"),
blanchet@35963
    75
   ("higher_order", "smart"),
blanchet@35963
    76
   ("follow_defs", "false"),
blanchet@35963
    77
   ("isar_proof", "false"),
blanchet@36064
    78
   ("modulus", "1"),
blanchet@36064
    79
   ("sorts", "false"),
blanchet@35963
    80
   ("minimize_timeout", "5 s")]
blanchet@35963
    81
blanchet@36140
    82
val alias_params =
blanchet@36140
    83
  [("atp", "atps")]
blanchet@36140
    84
val negated_alias_params =
blanchet@35963
    85
  [("no_debug", "debug"),
blanchet@35963
    86
   ("quiet", "verbose"),
blanchet@36143
    87
   ("no_overlord", "overlord"),
blanchet@36235
    88
   ("implicit_apply", "explicit_apply"),
blanchet@36058
    89
   ("ignore_no_atp", "respect_no_atp"),
blanchet@35963
    90
   ("partial_types", "full_types"),
blanchet@36220
    91
   ("theory_irrelevant", "theory_relevant"),
blanchet@35963
    92
   ("first_order", "higher_order"),
blanchet@35963
    93
   ("dont_follow_defs", "follow_defs"),
blanchet@36064
    94
   ("metis_proof", "isar_proof"),
blanchet@36064
    95
   ("no_sorts", "sorts")]
blanchet@35963
    96
blanchet@36281
    97
val params_for_minimize =
blanchet@36281
    98
  ["full_types", "explicit_apply", "higher_order", "isar_proof", "modulus",
blanchet@36281
    99
   "sorts", "minimize_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@35971
   107
  member (op =) property_dependent_params s
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@36290
   135
          [("atps", [!atps]),
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@35963
   145
fun extract_params thy 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@35963
   158
      the_timeout (case lookup name of
blanchet@35963
   159
                     NONE => NONE
blanchet@35963
   160
                   | SOME s => parse_time_option name s)
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@35963
   168
    val debug = lookup_bool "debug"
blanchet@35963
   169
    val verbose = debug orelse lookup_bool "verbose"
blanchet@36143
   170
    val overlord = lookup_bool "overlord"
blanchet@35963
   171
    val atps = lookup_string "atps" |> space_explode " "
blanchet@35963
   172
    val full_types = lookup_bool "full_types"
blanchet@36235
   173
    val explicit_apply = lookup_bool "explicit_apply"
blanchet@36058
   174
    val respect_no_atp = lookup_bool "respect_no_atp"
blanchet@35966
   175
    val relevance_threshold =
blanchet@35966
   176
      0.01 * Real.fromInt (lookup_int "relevance_threshold")
blanchet@36058
   177
    val convergence = 0.01 * Real.fromInt (lookup_int "convergence")
blanchet@36220
   178
    val theory_relevant = lookup_bool_option "theory_relevant"
blanchet@35963
   179
    val higher_order = lookup_bool_option "higher_order"
blanchet@35963
   180
    val follow_defs = lookup_bool "follow_defs"
blanchet@35963
   181
    val isar_proof = lookup_bool "isar_proof"
blanchet@36064
   182
    val modulus = Int.max (1, lookup_int "modulus")
blanchet@36064
   183
    val sorts = lookup_bool "sorts"
blanchet@35963
   184
    val timeout = lookup_time "timeout"
blanchet@35963
   185
    val minimize_timeout = lookup_time "minimize_timeout"
blanchet@35963
   186
  in
blanchet@36143
   187
    {debug = debug, verbose = verbose, overlord = overlord, atps = atps,
blanchet@36235
   188
     full_types = full_types, explicit_apply = explicit_apply,
blanchet@36235
   189
     respect_no_atp = respect_no_atp, relevance_threshold = relevance_threshold,
blanchet@36235
   190
     convergence = convergence, theory_relevant = theory_relevant,
blanchet@36235
   191
     higher_order = higher_order, follow_defs = follow_defs,
blanchet@36235
   192
     isar_proof = isar_proof, modulus = modulus, sorts = sorts,
blanchet@36235
   193
     timeout = timeout, minimize_timeout = minimize_timeout}
blanchet@35963
   194
  end
blanchet@35866
   195
blanchet@35965
   196
fun get_params thy = extract_params thy (default_raw_params thy)
blanchet@35965
   197
fun default_params thy = get_params thy o map (apsnd single)
blanchet@35866
   198
blanchet@36141
   199
fun minimize override_params old_style_args i fact_refs state =
blanchet@35866
   200
  let
blanchet@35963
   201
    val thy = Proof.theory_of state
blanchet@35963
   202
    val ctxt = Proof.context_of state
blanchet@35965
   203
    fun theorems_from_refs ctxt =
blanchet@35965
   204
      map (fn fact_ref =>
blanchet@35866
   205
              let
blanchet@35965
   206
                val ths = ProofContext.get_fact ctxt fact_ref
blanchet@35965
   207
                val name' = Facts.string_of_ref fact_ref
blanchet@35866
   208
              in (name', ths) end)
blanchet@35963
   209
    fun get_time_limit_arg s =
blanchet@35963
   210
      (case Int.fromString s of
blanchet@35963
   211
        SOME t => Time.fromSeconds t
blanchet@36223
   212
      | NONE => error ("Invalid time limit: " ^ quote s ^ "."))
blanchet@35866
   213
    fun get_opt (name, a) (p, t) =
blanchet@35866
   214
      (case name of
blanchet@35866
   215
        "time" => (p, get_time_limit_arg a)
blanchet@35866
   216
      | "atp" => (a, t)
blanchet@36223
   217
      | n => error ("Invalid argument: " ^ n ^ "."))
blanchet@35965
   218
    val {atps, minimize_timeout, ...} = get_params thy override_params
blanchet@35965
   219
    val (atp, timeout) = fold get_opt old_style_args (hd atps, minimize_timeout)
blanchet@35963
   220
    val params =
blanchet@35965
   221
      get_params thy
blanchet@36223
   222
          (override_params @
blanchet@36223
   223
           [("atps", [atp]),
blanchet@36223
   224
            ("minimize_timeout",
blanchet@36223
   225
             [string_of_int (Time.toMilliseconds timeout) ^ " ms"])])
blanchet@35866
   226
    val prover =
blanchet@35963
   227
      (case get_prover thy atp of
blanchet@35866
   228
        SOME prover => prover
blanchet@36223
   229
      | NONE => error ("Unknown ATP: " ^ quote atp ^ "."))
blanchet@35965
   230
    val name_thms_pairs = theorems_from_refs ctxt fact_refs
blanchet@35866
   231
  in
blanchet@36223
   232
    priority (#2 (minimize_theorems params prover atp i state name_thms_pairs))
blanchet@35866
   233
  end
blanchet@35866
   234
blanchet@35965
   235
val runN = "run"
blanchet@35965
   236
val minimizeN = "minimize"
blanchet@35965
   237
val messagesN = "messages"
blanchet@35965
   238
val available_atpsN = "available_atps"
blanchet@35965
   239
val running_atpsN = "running_atps"
blanchet@35965
   240
val kill_atpsN = "kill_atps"
blanchet@35965
   241
val refresh_tptpN = "refresh_tptp"
blanchet@35965
   242
val helpN = "help"
blanchet@35866
   243
blanchet@36141
   244
fun minimizize_raw_param_name "timeout" = "minimize_timeout"
blanchet@36141
   245
  | minimizize_raw_param_name name = name
blanchet@36141
   246
blanchet@36281
   247
val is_raw_param_relevant_for_minimize =
blanchet@36281
   248
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   249
fun string_for_raw_param (key, values) =
blanchet@36281
   250
  key ^ (case space_implode " " values of
blanchet@36281
   251
           "" => ""
blanchet@36281
   252
         | value => " = " ^ value)
blanchet@36281
   253
blanchet@36281
   254
fun minimize_command override_params i atp_name facts =
blanchet@36281
   255
  "sledgehammer minimize [atp = " ^ atp_name ^
blanchet@36281
   256
  (override_params |> filter is_raw_param_relevant_for_minimize
blanchet@36281
   257
                   |> implode o map (prefix ", " o string_for_raw_param)) ^
blanchet@36281
   258
  "] (" ^ space_implode " " facts ^ ")" ^
blanchet@36281
   259
  (if i = 1 then "" else " " ^ string_of_int i)
blanchet@36281
   260
blanchet@35966
   261
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   262
  let
blanchet@35963
   263
    val thy = Proof.theory_of state
blanchet@35963
   264
    val _ = List.app check_raw_param override_params
blanchet@35965
   265
  in
blanchet@35965
   266
    if subcommand = runN then
blanchet@36281
   267
      let val i = the_default 1 opt_i in
blanchet@36281
   268
        sledgehammer (get_params thy override_params) i relevance_override
blanchet@36281
   269
                     (minimize_command override_params i) state
blanchet@36281
   270
      end
blanchet@35965
   271
    else if subcommand = minimizeN then
blanchet@36141
   272
      minimize (map (apfst minimizize_raw_param_name) override_params) []
blanchet@36141
   273
               (the_default 1 opt_i) (#add relevance_override) state
blanchet@35965
   274
    else if subcommand = messagesN then
blanchet@35965
   275
      messages opt_i
blanchet@35965
   276
    else if subcommand = available_atpsN then
blanchet@35965
   277
      available_atps thy
blanchet@35965
   278
    else if subcommand = running_atpsN then
blanchet@35965
   279
      running_atps ()
blanchet@35965
   280
    else if subcommand = kill_atpsN then
blanchet@35965
   281
      kill_atps ()
blanchet@35965
   282
    else if subcommand = refresh_tptpN then
blanchet@35965
   283
      refresh_systems_on_tptp ()
blanchet@35965
   284
    else
blanchet@35965
   285
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   286
  end
blanchet@35963
   287
blanchet@35966
   288
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   289
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   290
                 o Toplevel.proof_of)
blanchet@35963
   291
blanchet@35963
   292
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35963
   293
blanchet@35965
   294
fun sledgehammer_params_trans params =
blanchet@35963
   295
  Toplevel.theory
blanchet@35965
   296
      (fold set_default_raw_param params
blanchet@35963
   297
       #> tap (fn thy => 
blanchet@35963
   298
                  writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@35963
   299
                           (case rev (default_raw_params thy) of
blanchet@35963
   300
                              [] => "none"
blanchet@35963
   301
                            | params =>
blanchet@35963
   302
                              (map check_raw_param params;
blanchet@35963
   303
                               params |> map string_for_raw_param
blanchet@35963
   304
                                      |> sort_strings |> cat_lines)))))
blanchet@35963
   305
blanchet@35963
   306
val parse_key = Scan.repeat1 P.typ_group >> space_implode " "
blanchet@35966
   307
val parse_value = Scan.repeat1 P.xname
blanchet@35965
   308
val parse_param = parse_key -- Scan.optional (P.$$$ "=" |-- parse_value) []
blanchet@35965
   309
val parse_params = Scan.optional (Args.bracks (P.list parse_param)) []
blanchet@35965
   310
val parse_fact_refs =
blanchet@35966
   311
  Scan.repeat1 (Scan.unless (P.name -- Args.colon)
blanchet@35966
   312
                            (P.xname -- Scan.option Attrib.thm_sel)
blanchet@35966
   313
                >> (fn (name, interval) =>
blanchet@35966
   314
                       Facts.Named ((name, Position.none), interval)))
blanchet@35966
   315
val parse_relevance_chunk =
blanchet@35966
   316
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_to_relevance_override)
blanchet@35966
   317
  || (Args.del |-- Args.colon |-- parse_fact_refs
blanchet@35966
   318
      >> del_from_relevance_override)
blanchet@36188
   319
  || (parse_fact_refs >> only_relevance_override)
blanchet@35966
   320
val parse_relevance_override =
blanchet@36188
   321
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   322
                              >> merge_relevance_overrides))
blanchet@36188
   323
                (add_to_relevance_override [])
blanchet@35963
   324
val parse_sledgehammer_command =
blanchet@35966
   325
  (Scan.optional P.short_ident runN -- parse_params -- parse_relevance_override
blanchet@35966
   326
   -- Scan.option P.nat) #>> sledgehammer_trans
blanchet@35963
   327
val parse_sledgehammer_params_command =
blanchet@35963
   328
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   329
blanchet@35965
   330
val parse_minimize_args =
blanchet@35966
   331
  Scan.optional (Args.bracks (P.list (P.short_ident --| P.$$$ "=" -- P.xname)))
blanchet@35966
   332
                []
blanchet@35963
   333
val _ =
blanchet@35963
   334
  OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
blanchet@35965
   335
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill_atps))
blanchet@35963
   336
val _ =
blanchet@35963
   337
  OuterSyntax.improper_command "atp_info"
blanchet@35963
   338
    "print information about managed provers" K.diag
blanchet@35965
   339
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative running_atps))
blanchet@35963
   340
val _ =
blanchet@35963
   341
  OuterSyntax.improper_command "atp_messages"
blanchet@35963
   342
    "print recent messages issued by managed provers" K.diag
blanchet@35963
   343
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
blanchet@35963
   344
      (fn limit => Toplevel.no_timing
blanchet@35963
   345
                   o Toplevel.imperative (fn () => messages limit)))
blanchet@35963
   346
val _ =
blanchet@35963
   347
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
blanchet@35963
   348
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
blanchet@35965
   349
      Toplevel.keep (available_atps o Toplevel.theory_of)))
blanchet@35965
   350
val _ =
blanchet@35965
   351
  OuterSyntax.improper_command "atp_minimize"
blanchet@35965
   352
    "minimize theorem list with external prover" K.diag
blanchet@35965
   353
    (parse_minimize_args -- parse_fact_refs >> (fn (args, fact_refs) =>
blanchet@35965
   354
      Toplevel.no_timing o Toplevel.unknown_proof o
blanchet@36141
   355
        Toplevel.keep (minimize [] args 1 fact_refs o Toplevel.proof_of)))
blanchet@35965
   356
blanchet@35963
   357
val _ =
blanchet@35963
   358
  OuterSyntax.improper_command "sledgehammer"
blanchet@35963
   359
    "search for first-order proof using automatic theorem provers" K.diag
blanchet@35963
   360
    parse_sledgehammer_command
blanchet@35963
   361
val _ =
blanchet@35963
   362
  OuterSyntax.command "sledgehammer_params"
blanchet@35963
   363
    "set and display the default parameters for Sledgehammer" K.thy_decl
blanchet@35963
   364
    parse_sledgehammer_params_command
blanchet@35866
   365
blanchet@35866
   366
end;