src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Thu Apr 22 16:30:54 2010 +0200 (2010-04-22 ago)
changeset 36290 c29283184c7a
parent 36281 dbbf4d5d584d
child 36371 8c83ea1a7740
permissions -rw-r--r--
remove hack that is no longer necessary now that "ATP_Wrapper" properly detects which ATPs are installed
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@36140
   105
val set_default_raw_param = Data.map o AList.update (op =) o unalias_raw_param
blanchet@35963
   106
fun default_raw_params thy =
blanchet@35963
   107
  Data.get thy
blanchet@36140
   108
  |> fold (AList.default (op =))
blanchet@36290
   109
          [("atps", [!atps]),
blanchet@36140
   110
           ("full_types", [if !full_types then "true" else "false"]),
blanchet@36142
   111
           ("timeout", let val timeout = !timeout in
blanchet@36142
   112
                         [if timeout <= 0 then "none"
blanchet@36142
   113
                          else string_of_int timeout ^ " s"]
blanchet@36142
   114
                       end)]
blanchet@35963
   115
blanchet@35963
   116
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
   117
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
   118
blanchet@35963
   119
fun extract_params thy default_params override_params =
blanchet@35963
   120
  let
blanchet@36140
   121
    val override_params = map unalias_raw_param override_params
blanchet@35963
   122
    val raw_params = rev override_params @ rev default_params
blanchet@35963
   123
    val lookup = Option.map (space_implode " ") o AList.lookup (op =) raw_params
blanchet@35963
   124
    val lookup_string = the_default "" o lookup
blanchet@35963
   125
    fun general_lookup_bool option default_value name =
blanchet@35963
   126
      case lookup name of
blanchet@35970
   127
        SOME s => parse_bool_option option name s
blanchet@35963
   128
      | NONE => default_value
blanchet@35963
   129
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   130
    val lookup_bool_option = general_lookup_bool true NONE
blanchet@35963
   131
    fun lookup_time name =
blanchet@35963
   132
      the_timeout (case lookup name of
blanchet@35963
   133
                     NONE => NONE
blanchet@35963
   134
                   | SOME s => parse_time_option name s)
blanchet@35966
   135
    fun lookup_int name =
blanchet@35963
   136
      case lookup name of
blanchet@35966
   137
        NONE => 0
blanchet@35966
   138
      | SOME s => case Int.fromString s of
blanchet@35966
   139
                    SOME n => n
blanchet@35966
   140
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   141
                                   " must be assigned an integer value.")
blanchet@35963
   142
    val debug = lookup_bool "debug"
blanchet@35963
   143
    val verbose = debug orelse lookup_bool "verbose"
blanchet@36143
   144
    val overlord = lookup_bool "overlord"
blanchet@35963
   145
    val atps = lookup_string "atps" |> space_explode " "
blanchet@35963
   146
    val full_types = lookup_bool "full_types"
blanchet@36235
   147
    val explicit_apply = lookup_bool "explicit_apply"
blanchet@36058
   148
    val respect_no_atp = lookup_bool "respect_no_atp"
blanchet@35966
   149
    val relevance_threshold =
blanchet@35966
   150
      0.01 * Real.fromInt (lookup_int "relevance_threshold")
blanchet@36058
   151
    val convergence = 0.01 * Real.fromInt (lookup_int "convergence")
blanchet@36220
   152
    val theory_relevant = lookup_bool_option "theory_relevant"
blanchet@35963
   153
    val higher_order = lookup_bool_option "higher_order"
blanchet@35963
   154
    val follow_defs = lookup_bool "follow_defs"
blanchet@35963
   155
    val isar_proof = lookup_bool "isar_proof"
blanchet@36064
   156
    val modulus = Int.max (1, lookup_int "modulus")
blanchet@36064
   157
    val sorts = lookup_bool "sorts"
blanchet@35963
   158
    val timeout = lookup_time "timeout"
blanchet@35963
   159
    val minimize_timeout = lookup_time "minimize_timeout"
blanchet@35963
   160
  in
blanchet@36143
   161
    {debug = debug, verbose = verbose, overlord = overlord, atps = atps,
blanchet@36235
   162
     full_types = full_types, explicit_apply = explicit_apply,
blanchet@36235
   163
     respect_no_atp = respect_no_atp, relevance_threshold = relevance_threshold,
blanchet@36235
   164
     convergence = convergence, theory_relevant = theory_relevant,
blanchet@36235
   165
     higher_order = higher_order, follow_defs = follow_defs,
blanchet@36235
   166
     isar_proof = isar_proof, modulus = modulus, sorts = sorts,
blanchet@36235
   167
     timeout = timeout, minimize_timeout = minimize_timeout}
blanchet@35963
   168
  end
blanchet@35866
   169
blanchet@35965
   170
fun get_params thy = extract_params thy (default_raw_params thy)
blanchet@35965
   171
fun default_params thy = get_params thy o map (apsnd single)
blanchet@35866
   172
blanchet@36141
   173
fun minimize override_params old_style_args i fact_refs state =
blanchet@35866
   174
  let
blanchet@35963
   175
    val thy = Proof.theory_of state
blanchet@35963
   176
    val ctxt = Proof.context_of state
blanchet@35965
   177
    fun theorems_from_refs ctxt =
blanchet@35965
   178
      map (fn fact_ref =>
blanchet@35866
   179
              let
blanchet@35965
   180
                val ths = ProofContext.get_fact ctxt fact_ref
blanchet@35965
   181
                val name' = Facts.string_of_ref fact_ref
blanchet@35866
   182
              in (name', ths) end)
blanchet@35963
   183
    fun get_time_limit_arg s =
blanchet@35963
   184
      (case Int.fromString s of
blanchet@35963
   185
        SOME t => Time.fromSeconds t
blanchet@36223
   186
      | NONE => error ("Invalid time limit: " ^ quote s ^ "."))
blanchet@35866
   187
    fun get_opt (name, a) (p, t) =
blanchet@35866
   188
      (case name of
blanchet@35866
   189
        "time" => (p, get_time_limit_arg a)
blanchet@35866
   190
      | "atp" => (a, t)
blanchet@36223
   191
      | n => error ("Invalid argument: " ^ n ^ "."))
blanchet@35965
   192
    val {atps, minimize_timeout, ...} = get_params thy override_params
blanchet@35965
   193
    val (atp, timeout) = fold get_opt old_style_args (hd atps, minimize_timeout)
blanchet@35963
   194
    val params =
blanchet@35965
   195
      get_params thy
blanchet@36223
   196
          (override_params @
blanchet@36223
   197
           [("atps", [atp]),
blanchet@36223
   198
            ("minimize_timeout",
blanchet@36223
   199
             [string_of_int (Time.toMilliseconds timeout) ^ " ms"])])
blanchet@35866
   200
    val prover =
blanchet@35963
   201
      (case get_prover thy atp of
blanchet@35866
   202
        SOME prover => prover
blanchet@36223
   203
      | NONE => error ("Unknown ATP: " ^ quote atp ^ "."))
blanchet@35965
   204
    val name_thms_pairs = theorems_from_refs ctxt fact_refs
blanchet@35866
   205
  in
blanchet@36223
   206
    priority (#2 (minimize_theorems params prover atp i state name_thms_pairs))
blanchet@35866
   207
  end
blanchet@35866
   208
blanchet@35965
   209
val runN = "run"
blanchet@35965
   210
val minimizeN = "minimize"
blanchet@35965
   211
val messagesN = "messages"
blanchet@35965
   212
val available_atpsN = "available_atps"
blanchet@35965
   213
val running_atpsN = "running_atps"
blanchet@35965
   214
val kill_atpsN = "kill_atps"
blanchet@35965
   215
val refresh_tptpN = "refresh_tptp"
blanchet@35965
   216
val helpN = "help"
blanchet@35866
   217
blanchet@36141
   218
fun minimizize_raw_param_name "timeout" = "minimize_timeout"
blanchet@36141
   219
  | minimizize_raw_param_name name = name
blanchet@36141
   220
blanchet@36281
   221
val is_raw_param_relevant_for_minimize =
blanchet@36281
   222
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   223
fun string_for_raw_param (key, values) =
blanchet@36281
   224
  key ^ (case space_implode " " values of
blanchet@36281
   225
           "" => ""
blanchet@36281
   226
         | value => " = " ^ value)
blanchet@36281
   227
blanchet@36281
   228
fun minimize_command override_params i atp_name facts =
blanchet@36281
   229
  "sledgehammer minimize [atp = " ^ atp_name ^
blanchet@36281
   230
  (override_params |> filter is_raw_param_relevant_for_minimize
blanchet@36281
   231
                   |> implode o map (prefix ", " o string_for_raw_param)) ^
blanchet@36281
   232
  "] (" ^ space_implode " " facts ^ ")" ^
blanchet@36281
   233
  (if i = 1 then "" else " " ^ string_of_int i)
blanchet@36281
   234
blanchet@35966
   235
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   236
  let
blanchet@35963
   237
    val thy = Proof.theory_of state
blanchet@35963
   238
    val _ = List.app check_raw_param override_params
blanchet@35965
   239
  in
blanchet@35965
   240
    if subcommand = runN then
blanchet@36281
   241
      let val i = the_default 1 opt_i in
blanchet@36281
   242
        sledgehammer (get_params thy override_params) i relevance_override
blanchet@36281
   243
                     (minimize_command override_params i) state
blanchet@36281
   244
      end
blanchet@35965
   245
    else if subcommand = minimizeN then
blanchet@36141
   246
      minimize (map (apfst minimizize_raw_param_name) override_params) []
blanchet@36141
   247
               (the_default 1 opt_i) (#add relevance_override) state
blanchet@35965
   248
    else if subcommand = messagesN then
blanchet@35965
   249
      messages opt_i
blanchet@35965
   250
    else if subcommand = available_atpsN then
blanchet@35965
   251
      available_atps thy
blanchet@35965
   252
    else if subcommand = running_atpsN then
blanchet@35965
   253
      running_atps ()
blanchet@35965
   254
    else if subcommand = kill_atpsN then
blanchet@35965
   255
      kill_atps ()
blanchet@35965
   256
    else if subcommand = refresh_tptpN then
blanchet@35965
   257
      refresh_systems_on_tptp ()
blanchet@35965
   258
    else
blanchet@35965
   259
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   260
  end
blanchet@35963
   261
blanchet@35966
   262
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   263
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   264
                 o Toplevel.proof_of)
blanchet@35963
   265
blanchet@35963
   266
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35963
   267
blanchet@35965
   268
fun sledgehammer_params_trans params =
blanchet@35963
   269
  Toplevel.theory
blanchet@35965
   270
      (fold set_default_raw_param params
blanchet@35963
   271
       #> tap (fn thy => 
blanchet@35963
   272
                  writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@35963
   273
                           (case rev (default_raw_params thy) of
blanchet@35963
   274
                              [] => "none"
blanchet@35963
   275
                            | params =>
blanchet@35963
   276
                              (map check_raw_param params;
blanchet@35963
   277
                               params |> map string_for_raw_param
blanchet@35963
   278
                                      |> sort_strings |> cat_lines)))))
blanchet@35963
   279
blanchet@35963
   280
val parse_key = Scan.repeat1 P.typ_group >> space_implode " "
blanchet@35966
   281
val parse_value = Scan.repeat1 P.xname
blanchet@35965
   282
val parse_param = parse_key -- Scan.optional (P.$$$ "=" |-- parse_value) []
blanchet@35965
   283
val parse_params = Scan.optional (Args.bracks (P.list parse_param)) []
blanchet@35965
   284
val parse_fact_refs =
blanchet@35966
   285
  Scan.repeat1 (Scan.unless (P.name -- Args.colon)
blanchet@35966
   286
                            (P.xname -- Scan.option Attrib.thm_sel)
blanchet@35966
   287
                >> (fn (name, interval) =>
blanchet@35966
   288
                       Facts.Named ((name, Position.none), interval)))
blanchet@35966
   289
val parse_relevance_chunk =
blanchet@35966
   290
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_to_relevance_override)
blanchet@35966
   291
  || (Args.del |-- Args.colon |-- parse_fact_refs
blanchet@35966
   292
      >> del_from_relevance_override)
blanchet@36188
   293
  || (parse_fact_refs >> only_relevance_override)
blanchet@35966
   294
val parse_relevance_override =
blanchet@36188
   295
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   296
                              >> merge_relevance_overrides))
blanchet@36188
   297
                (add_to_relevance_override [])
blanchet@35963
   298
val parse_sledgehammer_command =
blanchet@35966
   299
  (Scan.optional P.short_ident runN -- parse_params -- parse_relevance_override
blanchet@35966
   300
   -- Scan.option P.nat) #>> sledgehammer_trans
blanchet@35963
   301
val parse_sledgehammer_params_command =
blanchet@35963
   302
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   303
blanchet@35965
   304
val parse_minimize_args =
blanchet@35966
   305
  Scan.optional (Args.bracks (P.list (P.short_ident --| P.$$$ "=" -- P.xname)))
blanchet@35966
   306
                []
blanchet@35963
   307
val _ =
blanchet@35963
   308
  OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
blanchet@35965
   309
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill_atps))
blanchet@35963
   310
val _ =
blanchet@35963
   311
  OuterSyntax.improper_command "atp_info"
blanchet@35963
   312
    "print information about managed provers" K.diag
blanchet@35965
   313
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative running_atps))
blanchet@35963
   314
val _ =
blanchet@35963
   315
  OuterSyntax.improper_command "atp_messages"
blanchet@35963
   316
    "print recent messages issued by managed provers" K.diag
blanchet@35963
   317
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
blanchet@35963
   318
      (fn limit => Toplevel.no_timing
blanchet@35963
   319
                   o Toplevel.imperative (fn () => messages limit)))
blanchet@35963
   320
val _ =
blanchet@35963
   321
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
blanchet@35963
   322
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
blanchet@35965
   323
      Toplevel.keep (available_atps o Toplevel.theory_of)))
blanchet@35965
   324
val _ =
blanchet@35965
   325
  OuterSyntax.improper_command "atp_minimize"
blanchet@35965
   326
    "minimize theorem list with external prover" K.diag
blanchet@35965
   327
    (parse_minimize_args -- parse_fact_refs >> (fn (args, fact_refs) =>
blanchet@35965
   328
      Toplevel.no_timing o Toplevel.unknown_proof o
blanchet@36141
   329
        Toplevel.keep (minimize [] args 1 fact_refs o Toplevel.proof_of)))
blanchet@35965
   330
blanchet@35963
   331
val _ =
blanchet@35963
   332
  OuterSyntax.improper_command "sledgehammer"
blanchet@35963
   333
    "search for first-order proof using automatic theorem provers" K.diag
blanchet@35963
   334
    parse_sledgehammer_command
blanchet@35963
   335
val _ =
blanchet@35963
   336
  OuterSyntax.command "sledgehammer_params"
blanchet@35963
   337
    "set and display the default parameters for Sledgehammer" K.thy_decl
blanchet@35963
   338
    parse_sledgehammer_params_command
blanchet@35866
   339
blanchet@35866
   340
end;