src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Wed Apr 21 16:21:19 2010 +0200 (2010-04-21 ago)
changeset 36281 dbbf4d5d584d
parent 36235 61159615a0c5
child 36290 c29283184c7a
permissions -rw-r--r--
pass relevant options from "sledgehammer" to "sledgehammer minimize";
one nice side effect of this change is that the "sledgehammer minimize" syntax now only occurs in "sledgehammer_isar.ML", instead of being spread across two files
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@35963
    11
  val default_params : theory -> (string * string) list -> params
blanchet@35963
    12
end;
blanchet@35963
    13
blanchet@35971
    14
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    15
struct
blanchet@35866
    16
blanchet@35971
    17
open Sledgehammer_Util
blanchet@35866
    18
open ATP_Manager
blanchet@35866
    19
open ATP_Minimal
blanchet@35965
    20
open ATP_Wrapper
blanchet@35866
    21
blanchet@35866
    22
structure K = OuterKeyword and P = OuterParse
blanchet@35866
    23
blanchet@35966
    24
fun add_to_relevance_override ns : relevance_override =
blanchet@35966
    25
  {add = ns, del = [], only = false}
blanchet@35966
    26
fun del_from_relevance_override ns : relevance_override =
blanchet@35966
    27
  {add = [], del = ns, only = false}
blanchet@35966
    28
fun only_relevance_override ns : relevance_override =
blanchet@35966
    29
  {add = ns, del = [], only = true}
blanchet@36188
    30
val no_relevance_override = add_to_relevance_override []
blanchet@36003
    31
fun merge_relevance_override_pairwise (r1 : relevance_override)
blanchet@36003
    32
                                      (r2 : relevance_override) =
blanchet@35966
    33
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    34
   only = #only r1 andalso #only r2}
blanchet@36188
    35
fun merge_relevance_overrides rs =
blanchet@36188
    36
  fold merge_relevance_override_pairwise rs (only_relevance_override [])
blanchet@35965
    37
blanchet@35963
    38
type raw_param = string * string list
blanchet@35963
    39
blanchet@35963
    40
val default_default_params =
blanchet@35963
    41
  [("debug", "false"),
blanchet@35963
    42
   ("verbose", "false"),
blanchet@36143
    43
   ("overlord", "false"),
blanchet@36235
    44
   ("explicit_apply", "false"),
blanchet@36058
    45
   ("respect_no_atp", "true"),
blanchet@35966
    46
   ("relevance_threshold", "50"),
blanchet@36058
    47
   ("convergence", "320"),
blanchet@36220
    48
   ("theory_relevant", "smart"),
blanchet@35963
    49
   ("higher_order", "smart"),
blanchet@35963
    50
   ("follow_defs", "false"),
blanchet@35963
    51
   ("isar_proof", "false"),
blanchet@36064
    52
   ("modulus", "1"),
blanchet@36064
    53
   ("sorts", "false"),
blanchet@35963
    54
   ("minimize_timeout", "5 s")]
blanchet@35963
    55
blanchet@36140
    56
val alias_params =
blanchet@36140
    57
  [("atp", "atps")]
blanchet@36140
    58
val negated_alias_params =
blanchet@35963
    59
  [("no_debug", "debug"),
blanchet@35963
    60
   ("quiet", "verbose"),
blanchet@36143
    61
   ("no_overlord", "overlord"),
blanchet@36235
    62
   ("implicit_apply", "explicit_apply"),
blanchet@36058
    63
   ("ignore_no_atp", "respect_no_atp"),
blanchet@35963
    64
   ("partial_types", "full_types"),
blanchet@36220
    65
   ("theory_irrelevant", "theory_relevant"),
blanchet@35963
    66
   ("first_order", "higher_order"),
blanchet@35963
    67
   ("dont_follow_defs", "follow_defs"),
blanchet@36064
    68
   ("metis_proof", "isar_proof"),
blanchet@36064
    69
   ("no_sorts", "sorts")]
blanchet@35963
    70
blanchet@36281
    71
val params_for_minimize =
blanchet@36281
    72
  ["full_types", "explicit_apply", "higher_order", "isar_proof", "modulus",
blanchet@36281
    73
   "sorts", "minimize_timeout"]
blanchet@36281
    74
blanchet@35971
    75
val property_dependent_params = ["atps", "full_types", "timeout"]
blanchet@35866
    76
blanchet@35963
    77
fun is_known_raw_param s =
blanchet@35963
    78
  AList.defined (op =) default_default_params s orelse
blanchet@36140
    79
  AList.defined (op =) alias_params s orelse
blanchet@36140
    80
  AList.defined (op =) negated_alias_params s orelse
blanchet@35971
    81
  member (op =) property_dependent_params s
blanchet@35963
    82
blanchet@35963
    83
fun check_raw_param (s, _) =
blanchet@35963
    84
  if is_known_raw_param s then ()
blanchet@35963
    85
  else error ("Unknown parameter: " ^ quote s ^ ".")
blanchet@35866
    86
blanchet@36140
    87
fun unalias_raw_param (name, value) =
blanchet@36140
    88
  case AList.lookup (op =) alias_params name of
blanchet@36140
    89
    SOME name' => (name', value)
blanchet@36140
    90
  | NONE =>
blanchet@36140
    91
    case AList.lookup (op =) negated_alias_params name of
blanchet@36140
    92
      SOME name' => (name', case value of
blanchet@36140
    93
                              ["false"] => ["true"]
blanchet@36140
    94
                            | ["true"] => ["false"]
blanchet@36140
    95
                            | [] => ["false"]
blanchet@36140
    96
                            | _ => value)
blanchet@36140
    97
    | NONE => (name, value)
blanchet@35963
    98
blanchet@35963
    99
structure Data = Theory_Data(
blanchet@35963
   100
  type T = raw_param list
blanchet@35963
   101
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   102
  val extend = I
blanchet@35963
   103
  fun merge p : T = AList.merge (op =) (K true) p)
blanchet@35866
   104
blanchet@36189
   105
(* Don't even try to start ATPs that are not installed.
blanchet@36189
   106
   Hack: Should not rely on naming convention. *)
blanchet@36189
   107
val filter_atps =
blanchet@36189
   108
  space_explode " "
blanchet@36189
   109
  #> filter (fn s => String.isPrefix "remote_" s orelse
blanchet@36189
   110
                     getenv (String.map Char.toUpper s ^ "_HOME") <> "")
blanchet@36189
   111
  #> space_implode " "
blanchet@36189
   112
blanchet@36140
   113
val set_default_raw_param = Data.map o AList.update (op =) o unalias_raw_param
blanchet@35963
   114
fun default_raw_params thy =
blanchet@35963
   115
  Data.get thy
blanchet@36140
   116
  |> fold (AList.default (op =))
blanchet@36189
   117
          [("atps", [filter_atps (!atps)]),
blanchet@36140
   118
           ("full_types", [if !full_types then "true" else "false"]),
blanchet@36142
   119
           ("timeout", let val timeout = !timeout in
blanchet@36142
   120
                         [if timeout <= 0 then "none"
blanchet@36142
   121
                          else string_of_int timeout ^ " s"]
blanchet@36142
   122
                       end)]
blanchet@35963
   123
blanchet@35963
   124
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
   125
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
   126
blanchet@35963
   127
fun extract_params thy default_params override_params =
blanchet@35963
   128
  let
blanchet@36140
   129
    val override_params = map unalias_raw_param override_params
blanchet@35963
   130
    val raw_params = rev override_params @ rev default_params
blanchet@35963
   131
    val lookup = Option.map (space_implode " ") o AList.lookup (op =) raw_params
blanchet@35963
   132
    val lookup_string = the_default "" o lookup
blanchet@35963
   133
    fun general_lookup_bool option default_value name =
blanchet@35963
   134
      case lookup name of
blanchet@35970
   135
        SOME s => parse_bool_option option name s
blanchet@35963
   136
      | NONE => default_value
blanchet@35963
   137
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   138
    val lookup_bool_option = general_lookup_bool true NONE
blanchet@35963
   139
    fun lookup_time name =
blanchet@35963
   140
      the_timeout (case lookup name of
blanchet@35963
   141
                     NONE => NONE
blanchet@35963
   142
                   | SOME s => parse_time_option name s)
blanchet@35966
   143
    fun lookup_int name =
blanchet@35963
   144
      case lookup name of
blanchet@35966
   145
        NONE => 0
blanchet@35966
   146
      | SOME s => case Int.fromString s of
blanchet@35966
   147
                    SOME n => n
blanchet@35966
   148
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   149
                                   " must be assigned an integer value.")
blanchet@35963
   150
    val debug = lookup_bool "debug"
blanchet@35963
   151
    val verbose = debug orelse lookup_bool "verbose"
blanchet@36143
   152
    val overlord = lookup_bool "overlord"
blanchet@35963
   153
    val atps = lookup_string "atps" |> space_explode " "
blanchet@35963
   154
    val full_types = lookup_bool "full_types"
blanchet@36235
   155
    val explicit_apply = lookup_bool "explicit_apply"
blanchet@36058
   156
    val respect_no_atp = lookup_bool "respect_no_atp"
blanchet@35966
   157
    val relevance_threshold =
blanchet@35966
   158
      0.01 * Real.fromInt (lookup_int "relevance_threshold")
blanchet@36058
   159
    val convergence = 0.01 * Real.fromInt (lookup_int "convergence")
blanchet@36220
   160
    val theory_relevant = lookup_bool_option "theory_relevant"
blanchet@35963
   161
    val higher_order = lookup_bool_option "higher_order"
blanchet@35963
   162
    val follow_defs = lookup_bool "follow_defs"
blanchet@35963
   163
    val isar_proof = lookup_bool "isar_proof"
blanchet@36064
   164
    val modulus = Int.max (1, lookup_int "modulus")
blanchet@36064
   165
    val sorts = lookup_bool "sorts"
blanchet@35963
   166
    val timeout = lookup_time "timeout"
blanchet@35963
   167
    val minimize_timeout = lookup_time "minimize_timeout"
blanchet@35963
   168
  in
blanchet@36143
   169
    {debug = debug, verbose = verbose, overlord = overlord, atps = atps,
blanchet@36235
   170
     full_types = full_types, explicit_apply = explicit_apply,
blanchet@36235
   171
     respect_no_atp = respect_no_atp, relevance_threshold = relevance_threshold,
blanchet@36235
   172
     convergence = convergence, theory_relevant = theory_relevant,
blanchet@36235
   173
     higher_order = higher_order, follow_defs = follow_defs,
blanchet@36235
   174
     isar_proof = isar_proof, modulus = modulus, sorts = sorts,
blanchet@36235
   175
     timeout = timeout, minimize_timeout = minimize_timeout}
blanchet@35963
   176
  end
blanchet@35866
   177
blanchet@35965
   178
fun get_params thy = extract_params thy (default_raw_params thy)
blanchet@35965
   179
fun default_params thy = get_params thy o map (apsnd single)
blanchet@35866
   180
blanchet@36141
   181
fun minimize override_params old_style_args i fact_refs state =
blanchet@35866
   182
  let
blanchet@35963
   183
    val thy = Proof.theory_of state
blanchet@35963
   184
    val ctxt = Proof.context_of state
blanchet@35965
   185
    fun theorems_from_refs ctxt =
blanchet@35965
   186
      map (fn fact_ref =>
blanchet@35866
   187
              let
blanchet@35965
   188
                val ths = ProofContext.get_fact ctxt fact_ref
blanchet@35965
   189
                val name' = Facts.string_of_ref fact_ref
blanchet@35866
   190
              in (name', ths) end)
blanchet@35963
   191
    fun get_time_limit_arg s =
blanchet@35963
   192
      (case Int.fromString s of
blanchet@35963
   193
        SOME t => Time.fromSeconds t
blanchet@36223
   194
      | NONE => error ("Invalid time limit: " ^ quote s ^ "."))
blanchet@35866
   195
    fun get_opt (name, a) (p, t) =
blanchet@35866
   196
      (case name of
blanchet@35866
   197
        "time" => (p, get_time_limit_arg a)
blanchet@35866
   198
      | "atp" => (a, t)
blanchet@36223
   199
      | n => error ("Invalid argument: " ^ n ^ "."))
blanchet@35965
   200
    val {atps, minimize_timeout, ...} = get_params thy override_params
blanchet@35965
   201
    val (atp, timeout) = fold get_opt old_style_args (hd atps, minimize_timeout)
blanchet@35963
   202
    val params =
blanchet@35965
   203
      get_params thy
blanchet@36223
   204
          (override_params @
blanchet@36223
   205
           [("atps", [atp]),
blanchet@36223
   206
            ("minimize_timeout",
blanchet@36223
   207
             [string_of_int (Time.toMilliseconds timeout) ^ " ms"])])
blanchet@35866
   208
    val prover =
blanchet@35963
   209
      (case get_prover thy atp of
blanchet@35866
   210
        SOME prover => prover
blanchet@36223
   211
      | NONE => error ("Unknown ATP: " ^ quote atp ^ "."))
blanchet@35965
   212
    val name_thms_pairs = theorems_from_refs ctxt fact_refs
blanchet@35866
   213
  in
blanchet@36223
   214
    priority (#2 (minimize_theorems params prover atp i state name_thms_pairs))
blanchet@35866
   215
  end
blanchet@35866
   216
blanchet@35965
   217
val runN = "run"
blanchet@35965
   218
val minimizeN = "minimize"
blanchet@35965
   219
val messagesN = "messages"
blanchet@35965
   220
val available_atpsN = "available_atps"
blanchet@35965
   221
val running_atpsN = "running_atps"
blanchet@35965
   222
val kill_atpsN = "kill_atps"
blanchet@35965
   223
val refresh_tptpN = "refresh_tptp"
blanchet@35965
   224
val helpN = "help"
blanchet@35866
   225
blanchet@36141
   226
fun minimizize_raw_param_name "timeout" = "minimize_timeout"
blanchet@36141
   227
  | minimizize_raw_param_name name = name
blanchet@36141
   228
blanchet@36281
   229
val is_raw_param_relevant_for_minimize =
blanchet@36281
   230
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   231
fun string_for_raw_param (key, values) =
blanchet@36281
   232
  key ^ (case space_implode " " values of
blanchet@36281
   233
           "" => ""
blanchet@36281
   234
         | value => " = " ^ value)
blanchet@36281
   235
blanchet@36281
   236
fun minimize_command override_params i atp_name facts =
blanchet@36281
   237
  "sledgehammer minimize [atp = " ^ atp_name ^
blanchet@36281
   238
  (override_params |> filter is_raw_param_relevant_for_minimize
blanchet@36281
   239
                   |> implode o map (prefix ", " o string_for_raw_param)) ^
blanchet@36281
   240
  "] (" ^ space_implode " " facts ^ ")" ^
blanchet@36281
   241
  (if i = 1 then "" else " " ^ string_of_int i)
blanchet@36281
   242
blanchet@35966
   243
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   244
  let
blanchet@35963
   245
    val thy = Proof.theory_of state
blanchet@35963
   246
    val _ = List.app check_raw_param override_params
blanchet@35965
   247
  in
blanchet@35965
   248
    if subcommand = runN then
blanchet@36281
   249
      let val i = the_default 1 opt_i in
blanchet@36281
   250
        sledgehammer (get_params thy override_params) i relevance_override
blanchet@36281
   251
                     (minimize_command override_params i) state
blanchet@36281
   252
      end
blanchet@35965
   253
    else if subcommand = minimizeN then
blanchet@36141
   254
      minimize (map (apfst minimizize_raw_param_name) override_params) []
blanchet@36141
   255
               (the_default 1 opt_i) (#add relevance_override) state
blanchet@35965
   256
    else if subcommand = messagesN then
blanchet@35965
   257
      messages opt_i
blanchet@35965
   258
    else if subcommand = available_atpsN then
blanchet@35965
   259
      available_atps thy
blanchet@35965
   260
    else if subcommand = running_atpsN then
blanchet@35965
   261
      running_atps ()
blanchet@35965
   262
    else if subcommand = kill_atpsN then
blanchet@35965
   263
      kill_atps ()
blanchet@35965
   264
    else if subcommand = refresh_tptpN then
blanchet@35965
   265
      refresh_systems_on_tptp ()
blanchet@35965
   266
    else
blanchet@35965
   267
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   268
  end
blanchet@35963
   269
blanchet@35966
   270
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   271
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   272
                 o Toplevel.proof_of)
blanchet@35963
   273
blanchet@35963
   274
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35963
   275
blanchet@35965
   276
fun sledgehammer_params_trans params =
blanchet@35963
   277
  Toplevel.theory
blanchet@35965
   278
      (fold set_default_raw_param params
blanchet@35963
   279
       #> tap (fn thy => 
blanchet@35963
   280
                  writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@35963
   281
                           (case rev (default_raw_params thy) of
blanchet@35963
   282
                              [] => "none"
blanchet@35963
   283
                            | params =>
blanchet@35963
   284
                              (map check_raw_param params;
blanchet@35963
   285
                               params |> map string_for_raw_param
blanchet@35963
   286
                                      |> sort_strings |> cat_lines)))))
blanchet@35963
   287
blanchet@35963
   288
val parse_key = Scan.repeat1 P.typ_group >> space_implode " "
blanchet@35966
   289
val parse_value = Scan.repeat1 P.xname
blanchet@35965
   290
val parse_param = parse_key -- Scan.optional (P.$$$ "=" |-- parse_value) []
blanchet@35965
   291
val parse_params = Scan.optional (Args.bracks (P.list parse_param)) []
blanchet@35965
   292
val parse_fact_refs =
blanchet@35966
   293
  Scan.repeat1 (Scan.unless (P.name -- Args.colon)
blanchet@35966
   294
                            (P.xname -- Scan.option Attrib.thm_sel)
blanchet@35966
   295
                >> (fn (name, interval) =>
blanchet@35966
   296
                       Facts.Named ((name, Position.none), interval)))
blanchet@35966
   297
val parse_relevance_chunk =
blanchet@35966
   298
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_to_relevance_override)
blanchet@35966
   299
  || (Args.del |-- Args.colon |-- parse_fact_refs
blanchet@35966
   300
      >> del_from_relevance_override)
blanchet@36188
   301
  || (parse_fact_refs >> only_relevance_override)
blanchet@35966
   302
val parse_relevance_override =
blanchet@36188
   303
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   304
                              >> merge_relevance_overrides))
blanchet@36188
   305
                (add_to_relevance_override [])
blanchet@35963
   306
val parse_sledgehammer_command =
blanchet@35966
   307
  (Scan.optional P.short_ident runN -- parse_params -- parse_relevance_override
blanchet@35966
   308
   -- Scan.option P.nat) #>> sledgehammer_trans
blanchet@35963
   309
val parse_sledgehammer_params_command =
blanchet@35963
   310
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   311
blanchet@35965
   312
val parse_minimize_args =
blanchet@35966
   313
  Scan.optional (Args.bracks (P.list (P.short_ident --| P.$$$ "=" -- P.xname)))
blanchet@35966
   314
                []
blanchet@35963
   315
val _ =
blanchet@35963
   316
  OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
blanchet@35965
   317
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill_atps))
blanchet@35963
   318
val _ =
blanchet@35963
   319
  OuterSyntax.improper_command "atp_info"
blanchet@35963
   320
    "print information about managed provers" K.diag
blanchet@35965
   321
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative running_atps))
blanchet@35963
   322
val _ =
blanchet@35963
   323
  OuterSyntax.improper_command "atp_messages"
blanchet@35963
   324
    "print recent messages issued by managed provers" K.diag
blanchet@35963
   325
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
blanchet@35963
   326
      (fn limit => Toplevel.no_timing
blanchet@35963
   327
                   o Toplevel.imperative (fn () => messages limit)))
blanchet@35963
   328
val _ =
blanchet@35963
   329
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
blanchet@35963
   330
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
blanchet@35965
   331
      Toplevel.keep (available_atps o Toplevel.theory_of)))
blanchet@35965
   332
val _ =
blanchet@35965
   333
  OuterSyntax.improper_command "atp_minimize"
blanchet@35965
   334
    "minimize theorem list with external prover" K.diag
blanchet@35965
   335
    (parse_minimize_args -- parse_fact_refs >> (fn (args, fact_refs) =>
blanchet@35965
   336
      Toplevel.no_timing o Toplevel.unknown_proof o
blanchet@36141
   337
        Toplevel.keep (minimize [] args 1 fact_refs o Toplevel.proof_of)))
blanchet@35965
   338
blanchet@35963
   339
val _ =
blanchet@35963
   340
  OuterSyntax.improper_command "sledgehammer"
blanchet@35963
   341
    "search for first-order proof using automatic theorem provers" K.diag
blanchet@35963
   342
    parse_sledgehammer_command
blanchet@35963
   343
val _ =
blanchet@35963
   344
  OuterSyntax.command "sledgehammer_params"
blanchet@35963
   345
    "set and display the default parameters for Sledgehammer" K.thy_decl
blanchet@35963
   346
    parse_sledgehammer_params_command
blanchet@35866
   347
blanchet@35866
   348
end;