src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Sun Mar 28 18:39:27 2010 +0200 (2010-03-28 ago)
changeset 36003 eb44a5d40b9e
parent 35971 4f24a4e9af4a
child 36058 8256d5a185bd
permissions -rw-r--r--
make SML/NJ happy
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@35966
    30
val default_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@36003
    34
   only = #only r1 orelse #only r2}
blanchet@35966
    35
fun merge_relevance_overrides rs =
blanchet@35966
    36
  fold merge_relevance_override_pairwise rs default_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@35966
    43
   ("relevance_threshold", "50"),
blanchet@35963
    44
   ("higher_order", "smart"),
blanchet@35963
    45
   ("respect_no_atp", "true"),
blanchet@35963
    46
   ("follow_defs", "false"),
blanchet@35963
    47
   ("isar_proof", "false"),
blanchet@35963
    48
   ("minimize_timeout", "5 s")]
blanchet@35963
    49
blanchet@35963
    50
val negated_params =
blanchet@35963
    51
  [("no_debug", "debug"),
blanchet@35963
    52
   ("quiet", "verbose"),
blanchet@35963
    53
   ("partial_types", "full_types"),
blanchet@35963
    54
   ("first_order", "higher_order"),
blanchet@35963
    55
   ("ignore_no_atp", "respect_no_atp"),
blanchet@35963
    56
   ("dont_follow_defs", "follow_defs"),
blanchet@35963
    57
   ("metis_proof", "isar_proof")]
blanchet@35963
    58
blanchet@35971
    59
val property_dependent_params = ["atps", "full_types", "timeout"]
blanchet@35866
    60
blanchet@35963
    61
fun is_known_raw_param s =
blanchet@35963
    62
  AList.defined (op =) default_default_params s orelse
blanchet@35963
    63
  AList.defined (op =) negated_params s orelse
blanchet@35971
    64
  member (op =) property_dependent_params s
blanchet@35963
    65
blanchet@35963
    66
fun check_raw_param (s, _) =
blanchet@35963
    67
  if is_known_raw_param s then ()
blanchet@35963
    68
  else error ("Unknown parameter: " ^ quote s ^ ".")
blanchet@35866
    69
blanchet@35963
    70
fun unnegate_raw_param (name, value) =
blanchet@35963
    71
  case AList.lookup (op =) negated_params name of
blanchet@35963
    72
    SOME name' => (name', case value of
blanchet@35963
    73
                            ["false"] => ["true"]
blanchet@35963
    74
                          | ["true"] => ["false"]
blanchet@35963
    75
                          | [] => ["false"]
blanchet@35963
    76
                          | _ => value)
blanchet@35963
    77
  | NONE => (name, value)
blanchet@35963
    78
blanchet@35963
    79
structure Data = Theory_Data(
blanchet@35963
    80
  type T = raw_param list
blanchet@35963
    81
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
    82
  val extend = I
blanchet@35963
    83
  fun merge p : T = AList.merge (op =) (K true) p)
blanchet@35866
    84
blanchet@35963
    85
val set_default_raw_param = Data.map o AList.update (op =) o unnegate_raw_param
blanchet@35963
    86
fun default_raw_params thy =
blanchet@35963
    87
  [("atps", [!atps]),
blanchet@35963
    88
   ("full_types", [if !full_types then "true" else "false"]),
blanchet@35963
    89
   ("timeout", [string_of_int (!timeout) ^ " s"])] @
blanchet@35963
    90
  Data.get thy
blanchet@35963
    91
blanchet@35963
    92
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
    93
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
    94
blanchet@35963
    95
fun extract_params thy default_params override_params =
blanchet@35963
    96
  let
blanchet@35963
    97
    val override_params = map unnegate_raw_param override_params
blanchet@35963
    98
    val raw_params = rev override_params @ rev default_params
blanchet@35963
    99
    val lookup = Option.map (space_implode " ") o AList.lookup (op =) raw_params
blanchet@35963
   100
    val lookup_string = the_default "" o lookup
blanchet@35963
   101
    fun general_lookup_bool option default_value name =
blanchet@35963
   102
      case lookup name of
blanchet@35970
   103
        SOME s => parse_bool_option option name s
blanchet@35963
   104
      | NONE => default_value
blanchet@35963
   105
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   106
    val lookup_bool_option = general_lookup_bool true NONE
blanchet@35963
   107
    fun lookup_time name =
blanchet@35963
   108
      the_timeout (case lookup name of
blanchet@35963
   109
                     NONE => NONE
blanchet@35963
   110
                   | SOME s => parse_time_option name s)
blanchet@35966
   111
    fun lookup_int name =
blanchet@35963
   112
      case lookup name of
blanchet@35966
   113
        NONE => 0
blanchet@35966
   114
      | SOME s => case Int.fromString s of
blanchet@35966
   115
                    SOME n => n
blanchet@35966
   116
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   117
                                   " must be assigned an integer value.")
blanchet@35963
   118
    val debug = lookup_bool "debug"
blanchet@35963
   119
    val verbose = debug orelse lookup_bool "verbose"
blanchet@35963
   120
    val atps = lookup_string "atps" |> space_explode " "
blanchet@35963
   121
    val full_types = lookup_bool "full_types"
blanchet@35966
   122
    val relevance_threshold =
blanchet@35966
   123
      0.01 * Real.fromInt (lookup_int "relevance_threshold")
blanchet@35963
   124
    val higher_order = lookup_bool_option "higher_order"
blanchet@35963
   125
    val respect_no_atp = lookup_bool "respect_no_atp"
blanchet@35963
   126
    val follow_defs = lookup_bool "follow_defs"
blanchet@35963
   127
    val isar_proof = lookup_bool "isar_proof"
blanchet@35963
   128
    val timeout = lookup_time "timeout"
blanchet@35963
   129
    val minimize_timeout = lookup_time "minimize_timeout"
blanchet@35963
   130
  in
blanchet@35963
   131
    {debug = debug, verbose = verbose, atps = atps, full_types = full_types,
blanchet@35963
   132
     relevance_threshold = relevance_threshold, higher_order = higher_order,
blanchet@35963
   133
     respect_no_atp = respect_no_atp, follow_defs = follow_defs,
blanchet@35963
   134
     isar_proof = isar_proof, timeout = timeout,
blanchet@35963
   135
     minimize_timeout = minimize_timeout}
blanchet@35963
   136
  end
blanchet@35866
   137
blanchet@35965
   138
fun get_params thy = extract_params thy (default_raw_params thy)
blanchet@35965
   139
fun default_params thy = get_params thy o map (apsnd single)
blanchet@35866
   140
blanchet@35965
   141
fun atp_minimize_command override_params old_style_args fact_refs state =
blanchet@35866
   142
  let
blanchet@35963
   143
    val thy = Proof.theory_of state
blanchet@35963
   144
    val ctxt = Proof.context_of state
blanchet@35965
   145
    fun theorems_from_refs ctxt =
blanchet@35965
   146
      map (fn fact_ref =>
blanchet@35866
   147
              let
blanchet@35965
   148
                val ths = ProofContext.get_fact ctxt fact_ref
blanchet@35965
   149
                val name' = Facts.string_of_ref fact_ref
blanchet@35866
   150
              in (name', ths) end)
blanchet@35963
   151
    fun get_time_limit_arg s =
blanchet@35963
   152
      (case Int.fromString s of
blanchet@35963
   153
        SOME t => Time.fromSeconds t
blanchet@35963
   154
      | NONE => error ("Invalid time limit: " ^ quote s))
blanchet@35866
   155
    fun get_opt (name, a) (p, t) =
blanchet@35866
   156
      (case name of
blanchet@35866
   157
        "time" => (p, get_time_limit_arg a)
blanchet@35866
   158
      | "atp" => (a, t)
blanchet@35866
   159
      | n => error ("Invalid argument: " ^ n))
blanchet@35965
   160
    val {atps, minimize_timeout, ...} = get_params thy override_params
blanchet@35965
   161
    val (atp, timeout) = fold get_opt old_style_args (hd atps, minimize_timeout)
blanchet@35963
   162
    val params =
blanchet@35965
   163
      get_params thy
blanchet@35965
   164
          [("atps", [atp]),
blanchet@35965
   165
           ("minimize_timeout",
blanchet@35965
   166
            [string_of_int (Time.toSeconds timeout) ^ " s"])]
blanchet@35866
   167
    val prover =
blanchet@35963
   168
      (case get_prover thy atp of
blanchet@35866
   169
        SOME prover => prover
blanchet@35963
   170
      | NONE => error ("Unknown ATP: " ^ quote atp))
blanchet@35965
   171
    val name_thms_pairs = theorems_from_refs ctxt fact_refs
blanchet@35866
   172
  in
blanchet@35963
   173
    writeln (#2 (minimize_theorems params linear_minimize prover atp state
blanchet@35963
   174
                                   name_thms_pairs))
blanchet@35866
   175
  end
blanchet@35866
   176
blanchet@35965
   177
val runN = "run"
blanchet@35965
   178
val minimizeN = "minimize"
blanchet@35965
   179
val messagesN = "messages"
blanchet@35965
   180
val available_atpsN = "available_atps"
blanchet@35965
   181
val running_atpsN = "running_atps"
blanchet@35965
   182
val kill_atpsN = "kill_atps"
blanchet@35965
   183
val refresh_tptpN = "refresh_tptp"
blanchet@35965
   184
val helpN = "help"
blanchet@35866
   185
blanchet@35965
   186
val addN = "add"
blanchet@35965
   187
val delN = "del"
blanchet@35965
   188
val onlyN = "only"
blanchet@35965
   189
blanchet@35966
   190
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   191
  let
blanchet@35963
   192
    val thy = Proof.theory_of state
blanchet@35963
   193
    val _ = List.app check_raw_param override_params
blanchet@35965
   194
  in
blanchet@35965
   195
    if subcommand = runN then
blanchet@35966
   196
      sledgehammer (get_params thy override_params) (the_default 1 opt_i)
blanchet@35966
   197
                   relevance_override state
blanchet@35965
   198
    else if subcommand = minimizeN then
blanchet@35966
   199
      atp_minimize_command override_params [] (#add relevance_override) state
blanchet@35965
   200
    else if subcommand = messagesN then
blanchet@35965
   201
      messages opt_i
blanchet@35965
   202
    else if subcommand = available_atpsN then
blanchet@35965
   203
      available_atps thy
blanchet@35965
   204
    else if subcommand = running_atpsN then
blanchet@35965
   205
      running_atps ()
blanchet@35965
   206
    else if subcommand = kill_atpsN then
blanchet@35965
   207
      kill_atps ()
blanchet@35965
   208
    else if subcommand = refresh_tptpN then
blanchet@35965
   209
      refresh_systems_on_tptp ()
blanchet@35965
   210
    else
blanchet@35965
   211
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   212
  end
blanchet@35963
   213
blanchet@35966
   214
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   215
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   216
                 o Toplevel.proof_of)
blanchet@35963
   217
blanchet@35963
   218
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35963
   219
blanchet@35965
   220
fun sledgehammer_params_trans params =
blanchet@35963
   221
  Toplevel.theory
blanchet@35965
   222
      (fold set_default_raw_param params
blanchet@35963
   223
       #> tap (fn thy => 
blanchet@35963
   224
                  writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@35963
   225
                           (case rev (default_raw_params thy) of
blanchet@35963
   226
                              [] => "none"
blanchet@35963
   227
                            | params =>
blanchet@35963
   228
                              (map check_raw_param params;
blanchet@35963
   229
                               params |> map string_for_raw_param
blanchet@35963
   230
                                      |> sort_strings |> cat_lines)))))
blanchet@35963
   231
blanchet@35963
   232
val parse_key = Scan.repeat1 P.typ_group >> space_implode " "
blanchet@35966
   233
val parse_value = Scan.repeat1 P.xname
blanchet@35965
   234
val parse_param = parse_key -- Scan.optional (P.$$$ "=" |-- parse_value) []
blanchet@35965
   235
val parse_params = Scan.optional (Args.bracks (P.list parse_param)) []
blanchet@35965
   236
val parse_fact_refs =
blanchet@35966
   237
  Scan.repeat1 (Scan.unless (P.name -- Args.colon)
blanchet@35966
   238
                            (P.xname -- Scan.option Attrib.thm_sel)
blanchet@35966
   239
                >> (fn (name, interval) =>
blanchet@35966
   240
                       Facts.Named ((name, Position.none), interval)))
blanchet@35966
   241
val parse_relevance_chunk =
blanchet@35966
   242
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_to_relevance_override)
blanchet@35966
   243
  || (Args.del |-- Args.colon |-- parse_fact_refs
blanchet@35966
   244
      >> del_from_relevance_override)
blanchet@35966
   245
  || (P.$$$ onlyN |-- Args.colon |-- parse_fact_refs >> only_relevance_override)
blanchet@35966
   246
val parse_relevance_override =
blanchet@35966
   247
  Scan.optional (Args.parens
blanchet@35966
   248
      (Scan.optional (parse_fact_refs >> only_relevance_override)
blanchet@35966
   249
                     default_relevance_override
blanchet@35966
   250
       -- Scan.repeat parse_relevance_chunk)
blanchet@35966
   251
       >> op :: >> merge_relevance_overrides)
blanchet@35966
   252
      default_relevance_override
blanchet@35963
   253
val parse_sledgehammer_command =
blanchet@35966
   254
  (Scan.optional P.short_ident runN -- parse_params -- parse_relevance_override
blanchet@35966
   255
   -- Scan.option P.nat) #>> sledgehammer_trans
blanchet@35963
   256
val parse_sledgehammer_params_command =
blanchet@35963
   257
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   258
blanchet@35965
   259
val parse_minimize_args =
blanchet@35966
   260
  Scan.optional (Args.bracks (P.list (P.short_ident --| P.$$$ "=" -- P.xname)))
blanchet@35966
   261
                []
blanchet@35963
   262
val _ =
blanchet@35963
   263
  OuterSyntax.improper_command "atp_kill" "kill all managed provers" K.diag
blanchet@35965
   264
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative kill_atps))
blanchet@35963
   265
val _ =
blanchet@35963
   266
  OuterSyntax.improper_command "atp_info"
blanchet@35963
   267
    "print information about managed provers" K.diag
blanchet@35965
   268
    (Scan.succeed (Toplevel.no_timing o Toplevel.imperative running_atps))
blanchet@35963
   269
val _ =
blanchet@35963
   270
  OuterSyntax.improper_command "atp_messages"
blanchet@35963
   271
    "print recent messages issued by managed provers" K.diag
blanchet@35963
   272
    (Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")") >>
blanchet@35963
   273
      (fn limit => Toplevel.no_timing
blanchet@35963
   274
                   o Toplevel.imperative (fn () => messages limit)))
blanchet@35963
   275
val _ =
blanchet@35963
   276
  OuterSyntax.improper_command "print_atps" "print external provers" K.diag
blanchet@35963
   277
    (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_theory o
blanchet@35965
   278
      Toplevel.keep (available_atps o Toplevel.theory_of)))
blanchet@35965
   279
val _ =
blanchet@35965
   280
  OuterSyntax.improper_command "atp_minimize"
blanchet@35965
   281
    "minimize theorem list with external prover" K.diag
blanchet@35965
   282
    (parse_minimize_args -- parse_fact_refs >> (fn (args, fact_refs) =>
blanchet@35965
   283
      Toplevel.no_timing o Toplevel.unknown_proof o
blanchet@35965
   284
        Toplevel.keep (atp_minimize_command [] args fact_refs
blanchet@35965
   285
                       o Toplevel.proof_of)))
blanchet@35965
   286
blanchet@35963
   287
val _ =
blanchet@35963
   288
  OuterSyntax.improper_command "sledgehammer"
blanchet@35963
   289
    "search for first-order proof using automatic theorem provers" K.diag
blanchet@35963
   290
    parse_sledgehammer_command
blanchet@35963
   291
val _ =
blanchet@35963
   292
  OuterSyntax.command "sledgehammer_params"
blanchet@35963
   293
    "set and display the default parameters for Sledgehammer" K.thy_decl
blanchet@35963
   294
    parse_sledgehammer_params_command
blanchet@35866
   295
blanchet@35866
   296
end;