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