src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Thu Dec 16 15:12:17 2010 +0100 (2010-12-16 ago)
changeset 41208 1b28c43a7074
parent 41153 626a58993122
child 41258 73401632a80c
permissions -rw-r--r--
make "debug" imply "blocking", since in blocking mode the exceptions flow through and are more instructive
blanchet@36375
     1
(*  Title:      HOL/Tools/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@41087
     9
  type params = Sledgehammer_Provers.params
blanchet@35963
    10
blanchet@39318
    11
  val auto : bool Unsynchronized.ref
blanchet@40059
    12
  val provers : string Unsynchronized.ref
blanchet@39318
    13
  val timeout : int Unsynchronized.ref
blanchet@39318
    14
  val full_types : bool Unsynchronized.ref
blanchet@40069
    15
  val default_params : Proof.context -> (string * string) list -> params
blanchet@39318
    16
  val setup : theory -> theory
blanchet@35963
    17
end;
blanchet@35963
    18
blanchet@35971
    19
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    20
struct
blanchet@35866
    21
blanchet@38028
    22
open ATP_Systems
blanchet@35971
    23
open Sledgehammer_Util
blanchet@41138
    24
open Sledgehammer_ATP_Translate
blanchet@41087
    25
open Sledgehammer_Provers
blanchet@38988
    26
open Sledgehammer_Minimize
blanchet@41087
    27
open Sledgehammer_Run
blanchet@35866
    28
blanchet@39318
    29
val auto = Unsynchronized.ref false
blanchet@39318
    30
blanchet@39318
    31
val _ =
blanchet@39318
    32
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@39318
    33
      (Preferences.bool_pref auto "auto-sledgehammer"
blanchet@39327
    34
           "Run Sledgehammer automatically.")
blanchet@39318
    35
blanchet@36394
    36
(** Sledgehammer commands **)
blanchet@36394
    37
blanchet@39318
    38
val no_relevance_override = {add = [], del = [], only = false}
blanchet@39318
    39
fun add_relevance_override ns : relevance_override =
blanchet@35966
    40
  {add = ns, del = [], only = false}
blanchet@39318
    41
fun del_relevance_override ns : relevance_override =
blanchet@35966
    42
  {add = [], del = ns, only = false}
blanchet@35966
    43
fun only_relevance_override ns : relevance_override =
blanchet@35966
    44
  {add = ns, del = [], only = true}
blanchet@36003
    45
fun merge_relevance_override_pairwise (r1 : relevance_override)
blanchet@36003
    46
                                      (r2 : relevance_override) =
blanchet@35966
    47
  {add = #add r1 @ #add r2, del = #del r1 @ #del r2,
blanchet@36183
    48
   only = #only r1 andalso #only r2}
blanchet@36188
    49
fun merge_relevance_overrides rs =
blanchet@36188
    50
  fold merge_relevance_override_pairwise rs (only_relevance_override [])
blanchet@35965
    51
blanchet@36371
    52
(*** parameters ***)
blanchet@36371
    53
blanchet@40059
    54
val provers = Unsynchronized.ref ""
blanchet@39335
    55
val timeout = Unsynchronized.ref 30
blanchet@36371
    56
val full_types = Unsynchronized.ref false
blanchet@36371
    57
blanchet@36371
    58
val _ =
blanchet@36371
    59
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@40059
    60
      (Preferences.string_pref provers
blanchet@40059
    61
          "Sledgehammer: Provers"
blanchet@36371
    62
          "Default automatic provers (separated by whitespace)")
blanchet@36371
    63
blanchet@36371
    64
val _ =
blanchet@36371
    65
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    66
      (Preferences.int_pref timeout
blanchet@36373
    67
          "Sledgehammer: Time Limit"
blanchet@36371
    68
          "ATPs will be interrupted after this time (in seconds)")
blanchet@36371
    69
blanchet@36371
    70
val _ =
blanchet@36371
    71
  ProofGeneralPgip.add_preference Preferences.category_proof
blanchet@36371
    72
      (Preferences.bool_pref full_types
blanchet@36373
    73
          "Sledgehammer: Full Types" "ATPs will use full type information")
blanchet@36371
    74
blanchet@35963
    75
type raw_param = string * string list
blanchet@35963
    76
blanchet@35963
    77
val default_default_params =
blanchet@41208
    78
  [("debug", "false"),
blanchet@35963
    79
   ("verbose", "false"),
blanchet@36143
    80
   ("overlord", "false"),
blanchet@41208
    81
   ("blocking", "false"),
blanchet@41134
    82
   ("type_sys", "smart"),
blanchet@36235
    83
   ("explicit_apply", "false"),
blanchet@40343
    84
   ("relevance_thresholds", "0.45 0.85"),
blanchet@38744
    85
   ("max_relevant", "smart"),
blanchet@35963
    86
   ("isar_proof", "false"),
blanchet@38590
    87
   ("isar_shrink_factor", "1")]
blanchet@35963
    88
blanchet@36140
    89
val alias_params =
blanchet@40059
    90
  [("prover", "provers"),
blanchet@40059
    91
   ("atps", "provers"), (* FIXME: legacy *)
blanchet@40059
    92
   ("atp", "provers")]  (* FIXME: legacy *)
blanchet@36140
    93
val negated_alias_params =
blanchet@41208
    94
  [("no_debug", "debug"),
blanchet@35963
    95
   ("quiet", "verbose"),
blanchet@36143
    96
   ("no_overlord", "overlord"),
blanchet@41208
    97
   ("non_blocking", "blocking"),
blanchet@36399
    98
   ("partial_types", "full_types"),
blanchet@36235
    99
   ("implicit_apply", "explicit_apply"),
blanchet@36908
   100
   ("no_isar_proof", "isar_proof")]
blanchet@35963
   101
blanchet@36281
   102
val params_for_minimize =
blanchet@41136
   103
  ["debug", "verbose", "overlord", "type_sys", "full_types", "isar_proof",
blanchet@38590
   104
   "isar_shrink_factor", "timeout"]
blanchet@36281
   105
blanchet@40059
   106
val property_dependent_params = ["provers", "full_types", "timeout"]
blanchet@35866
   107
blanchet@35963
   108
fun is_known_raw_param s =
blanchet@35963
   109
  AList.defined (op =) default_default_params s orelse
blanchet@36140
   110
  AList.defined (op =) alias_params s orelse
blanchet@36140
   111
  AList.defined (op =) negated_alias_params s orelse
blanchet@38985
   112
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35963
   113
blanchet@35963
   114
fun check_raw_param (s, _) =
blanchet@35963
   115
  if is_known_raw_param s then ()
blanchet@35963
   116
  else error ("Unknown parameter: " ^ quote s ^ ".")
blanchet@35866
   117
blanchet@36140
   118
fun unalias_raw_param (name, value) =
blanchet@36140
   119
  case AList.lookup (op =) alias_params name of
blanchet@36140
   120
    SOME name' => (name', value)
blanchet@36140
   121
  | NONE =>
blanchet@36140
   122
    case AList.lookup (op =) negated_alias_params name of
blanchet@36140
   123
      SOME name' => (name', case value of
blanchet@36140
   124
                              ["false"] => ["true"]
blanchet@36140
   125
                            | ["true"] => ["false"]
blanchet@36140
   126
                            | [] => ["false"]
blanchet@36140
   127
                            | _ => value)
blanchet@36140
   128
    | NONE => (name, value)
blanchet@35963
   129
blanchet@35963
   130
structure Data = Theory_Data(
blanchet@35963
   131
  type T = raw_param list
blanchet@35963
   132
  val empty = default_default_params |> map (apsnd single)
blanchet@35963
   133
  val extend = I
blanchet@35963
   134
  fun merge p : T = AList.merge (op =) (K true) p)
blanchet@35866
   135
blanchet@40941
   136
fun remotify_prover_if_available_and_not_already_remote ctxt name =
blanchet@40072
   137
  if String.isPrefix remote_prefix name then
blanchet@40072
   138
    SOME name
blanchet@40072
   139
  else
blanchet@40072
   140
    let val remote_name = remote_prefix ^ name in
blanchet@40941
   141
      if is_prover_available ctxt remote_name then SOME remote_name else NONE
blanchet@40072
   142
    end
blanchet@40072
   143
blanchet@40072
   144
fun remotify_prover_if_not_installed ctxt name =
blanchet@40941
   145
  if is_prover_available ctxt name andalso is_prover_installed ctxt name then
blanchet@40941
   146
    SOME name
blanchet@40941
   147
  else
blanchet@40941
   148
    remotify_prover_if_available_and_not_already_remote ctxt name
blanchet@40072
   149
blanchet@40072
   150
fun avoid_too_many_local_threads _ _ [] = []
blanchet@40941
   151
  | avoid_too_many_local_threads ctxt 0 provers =
blanchet@40941
   152
    map_filter (remotify_prover_if_available_and_not_already_remote ctxt)
blanchet@40941
   153
               provers
blanchet@40941
   154
  | avoid_too_many_local_threads ctxt n (prover :: provers) =
blanchet@40072
   155
    let val n = if String.isPrefix remote_prefix prover then n else n - 1 in
blanchet@40941
   156
      prover :: avoid_too_many_local_threads ctxt n provers
blanchet@40072
   157
    end
blanchet@40059
   158
blanchet@40059
   159
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@40059
   160
   timeout, it makes sense to put SPASS first. *)
blanchet@40069
   161
fun default_provers_param_value ctxt =
blanchet@40941
   162
  [spassN, eN, vampireN, sine_eN, SMT_Solver.solver_name_of ctxt]
blanchet@40941
   163
  |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@40941
   164
  |> avoid_too_many_local_threads ctxt (Multithreading.max_threads_value ())
blanchet@40941
   165
  |> space_implode " "
blanchet@40059
   166
blanchet@36140
   167
val set_default_raw_param = Data.map o AList.update (op =) o unalias_raw_param
blanchet@40069
   168
fun default_raw_params ctxt =
blanchet@40069
   169
  let val thy = ProofContext.theory_of ctxt in
blanchet@40069
   170
    Data.get thy
blanchet@40069
   171
    |> fold (AList.default (op =))
blanchet@40069
   172
            [("provers", [case !provers of
blanchet@40069
   173
                            "" => default_provers_param_value ctxt
blanchet@40069
   174
                          | s => s]),
blanchet@40069
   175
             ("full_types", [if !full_types then "true" else "false"]),
blanchet@40069
   176
             ("timeout", let val timeout = !timeout in
blanchet@40069
   177
                           [if timeout <= 0 then "none"
blanchet@40341
   178
                            else string_of_int timeout]
blanchet@40069
   179
                         end)]
blanchet@40069
   180
  end
blanchet@35963
   181
blanchet@41153
   182
val smart_full_type_sys = Tags true
blanchet@41153
   183
val smart_partial_type_sys = Const_Args
blanchet@41153
   184
blanchet@35963
   185
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35963
   186
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35866
   187
blanchet@39318
   188
fun extract_params auto default_params override_params =
blanchet@35963
   189
  let
blanchet@36140
   190
    val override_params = map unalias_raw_param override_params
blanchet@35963
   191
    val raw_params = rev override_params @ rev default_params
blanchet@35963
   192
    val lookup = Option.map (space_implode " ") o AList.lookup (op =) raw_params
blanchet@35963
   193
    val lookup_string = the_default "" o lookup
blanchet@35963
   194
    fun general_lookup_bool option default_value name =
blanchet@35963
   195
      case lookup name of
blanchet@35970
   196
        SOME s => parse_bool_option option name s
blanchet@35963
   197
      | NONE => default_value
blanchet@35963
   198
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35963
   199
    fun lookup_time name =
blanchet@39318
   200
      case lookup name of
blanchet@39318
   201
        SOME s => parse_time_option name s
blanchet@39318
   202
      | NONE => NONE
blanchet@35966
   203
    fun lookup_int name =
blanchet@35963
   204
      case lookup name of
blanchet@35966
   205
        NONE => 0
blanchet@35966
   206
      | SOME s => case Int.fromString s of
blanchet@35966
   207
                    SOME n => n
blanchet@35966
   208
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35966
   209
                                   " must be assigned an integer value.")
blanchet@40343
   210
    fun lookup_real_pair name =
blanchet@38745
   211
      case lookup name of
blanchet@40343
   212
        NONE => (0.0, 0.0)
blanchet@40343
   213
      | SOME s => case s |> space_explode " " |> map Real.fromString of
blanchet@40343
   214
                    [SOME r1, SOME r2] => (r1, r2)
blanchet@38745
   215
                  | _ => error ("Parameter " ^ quote name ^
blanchet@40343
   216
                                "must be assigned a pair of floating-point \
blanchet@40343
   217
                                \values (e.g., \"0.6 0.95\")")
blanchet@38589
   218
    fun lookup_int_option name =
blanchet@38589
   219
      case lookup name of
blanchet@38589
   220
        SOME "smart" => NONE
blanchet@38589
   221
      | _ => SOME (lookup_int name)
blanchet@39318
   222
    val debug = not auto andalso lookup_bool "debug"
blanchet@39318
   223
    val verbose = debug orelse (not auto andalso lookup_bool "verbose")
blanchet@36143
   224
    val overlord = lookup_bool "overlord"
blanchet@41208
   225
    val blocking = auto orelse debug orelse lookup_bool "blocking"
blanchet@40059
   226
    val provers = lookup_string "provers" |> space_explode " "
blanchet@40059
   227
                  |> auto ? single o hd
blanchet@41153
   228
    val type_sys =
blanchet@41138
   229
      case (lookup_string "type_sys", lookup_bool "full_types") of
blanchet@41138
   230
        ("tags", full_types) => Tags full_types
blanchet@41138
   231
      | ("preds", full_types) => Preds full_types
blanchet@41153
   232
      | ("const_args", false) => Const_Args
blanchet@41153
   233
      | ("none", false) => No_Types
blanchet@41153
   234
      | (type_sys, full_types) =>
blanchet@41153
   235
        if member (op =) ["const_args", "none", "smart"] type_sys then
blanchet@41153
   236
          if full_types then smart_full_type_sys else smart_partial_type_sys
blanchet@41153
   237
        else
blanchet@41153
   238
          error ("Unknown type system: " ^ quote type_sys ^ ".")
blanchet@36235
   239
    val explicit_apply = lookup_bool "explicit_apply"
blanchet@40343
   240
    val relevance_thresholds = lookup_real_pair "relevance_thresholds"
blanchet@38744
   241
    val max_relevant = lookup_int_option "max_relevant"
blanchet@35963
   242
    val isar_proof = lookup_bool "isar_proof"
blanchet@36924
   243
    val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
blanchet@39318
   244
    val timeout = (if auto then NONE else lookup_time "timeout") |> the_timeout
blanchet@38985
   245
    val expect = lookup_string "expect"
blanchet@35963
   246
  in
blanchet@41208
   247
    {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
blanchet@41138
   248
     provers = provers, type_sys = type_sys, explicit_apply = explicit_apply,
blanchet@38745
   249
     relevance_thresholds = relevance_thresholds, max_relevant = max_relevant,
blanchet@38997
   250
     isar_proof = isar_proof, isar_shrink_factor = isar_shrink_factor,
blanchet@38997
   251
     timeout = timeout, expect = expect}
blanchet@35963
   252
  end
blanchet@35866
   253
blanchet@40069
   254
fun get_params auto ctxt = extract_params auto (default_raw_params ctxt)
blanchet@39318
   255
fun default_params thy = get_params false thy o map (apsnd single)
blanchet@35866
   256
blanchet@36373
   257
(* Sledgehammer the given subgoal *)
blanchet@36373
   258
blanchet@36373
   259
val sledgehammerN = "sledgehammer"
blanchet@36373
   260
val sledgehammer_paramsN = "sledgehammer_params"
blanchet@36373
   261
blanchet@35965
   262
val runN = "run"
blanchet@35965
   263
val minimizeN = "minimize"
blanchet@35965
   264
val messagesN = "messages"
blanchet@40059
   265
val available_proversN = "available_provers"
blanchet@40059
   266
val running_proversN = "running_provers"
blanchet@40059
   267
val kill_proversN = "kill_provers"
blanchet@35965
   268
val refresh_tptpN = "refresh_tptp"
blanchet@35866
   269
blanchet@36281
   270
val is_raw_param_relevant_for_minimize =
blanchet@36281
   271
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   272
fun string_for_raw_param (key, values) =
blanchet@36481
   273
  key ^ (case space_implode " " values of "" => "" | value => " = " ^ value)
blanchet@36281
   274
blanchet@40059
   275
fun minimize_command override_params i prover_name fact_names =
blanchet@40059
   276
  sledgehammerN ^ " " ^ minimizeN ^ " [prover = " ^ prover_name ^
blanchet@36281
   277
  (override_params |> filter is_raw_param_relevant_for_minimize
blanchet@36281
   278
                   |> implode o map (prefix ", " o string_for_raw_param)) ^
blanchet@37171
   279
  "] (" ^ space_implode " " fact_names ^ ")" ^
blanchet@36281
   280
  (if i = 1 then "" else " " ^ string_of_int i)
blanchet@36281
   281
blanchet@35966
   282
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35963
   283
  let
blanchet@40069
   284
    val ctxt = Proof.context_of state
blanchet@36481
   285
    val _ = app check_raw_param override_params
blanchet@35965
   286
  in
blanchet@35965
   287
    if subcommand = runN then
blanchet@36281
   288
      let val i = the_default 1 opt_i in
blanchet@40069
   289
        run_sledgehammer (get_params false ctxt override_params) false i
blanchet@39318
   290
                         relevance_override (minimize_command override_params i)
blanchet@39318
   291
                         state
blanchet@39318
   292
        |> K ()
blanchet@36281
   293
      end
blanchet@35965
   294
    else if subcommand = minimizeN then
blanchet@40069
   295
      run_minimize (get_params false ctxt override_params) (the_default 1 opt_i)
blanchet@38590
   296
                   (#add relevance_override) state
blanchet@35965
   297
    else if subcommand = messagesN then
blanchet@35965
   298
      messages opt_i
blanchet@40059
   299
    else if subcommand = available_proversN then
blanchet@40941
   300
      available_provers ctxt
blanchet@40059
   301
    else if subcommand = running_proversN then
blanchet@40059
   302
      running_provers ()
blanchet@40059
   303
    else if subcommand = kill_proversN then
blanchet@40059
   304
      kill_provers ()
blanchet@35965
   305
    else if subcommand = refresh_tptpN then
blanchet@35965
   306
      refresh_systems_on_tptp ()
blanchet@35965
   307
    else
blanchet@35965
   308
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35965
   309
  end
blanchet@35963
   310
blanchet@35966
   311
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35966
   312
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35966
   313
                 o Toplevel.proof_of)
blanchet@35963
   314
blanchet@35963
   315
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35963
   316
blanchet@35965
   317
fun sledgehammer_params_trans params =
blanchet@35963
   318
  Toplevel.theory
blanchet@35965
   319
      (fold set_default_raw_param params
blanchet@39318
   320
       #> tap (fn thy =>
blanchet@40069
   321
                  let val ctxt = ProofContext.init_global thy in
blanchet@40069
   322
                    writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@40069
   323
                             (case default_raw_params ctxt |> rev of
blanchet@40069
   324
                                [] => "none"
blanchet@40069
   325
                              | params =>
blanchet@40069
   326
                                (map check_raw_param params;
blanchet@40069
   327
                                 params |> map string_for_raw_param
blanchet@40069
   328
                                        |> sort_strings |> cat_lines)))
blanchet@40069
   329
                  end))
blanchet@35963
   330
wenzelm@36960
   331
val parse_key = Scan.repeat1 Parse.typ_group >> space_implode " "
blanchet@40341
   332
val parse_value = Scan.repeat1 (Parse.xname || Parse.float_number)
wenzelm@36960
   333
val parse_param = parse_key -- Scan.optional (Parse.$$$ "=" |-- parse_value) []
wenzelm@36960
   334
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35965
   335
val parse_fact_refs =
blanchet@38996
   336
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@35966
   337
val parse_relevance_chunk =
blanchet@39318
   338
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_relevance_override)
blanchet@39318
   339
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_relevance_override)
blanchet@36188
   340
  || (parse_fact_refs >> only_relevance_override)
blanchet@35966
   341
val parse_relevance_override =
blanchet@36188
   342
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   343
                              >> merge_relevance_overrides))
blanchet@39318
   344
                no_relevance_override
blanchet@35963
   345
val parse_sledgehammer_command =
blanchet@37399
   346
  (Scan.optional Parse.short_ident runN -- parse_params
blanchet@37399
   347
   -- parse_relevance_override -- Scan.option Parse.nat) #>> sledgehammer_trans
blanchet@35963
   348
val parse_sledgehammer_params_command =
blanchet@35963
   349
  parse_params #>> sledgehammer_params_trans
blanchet@35963
   350
blanchet@35963
   351
val _ =
wenzelm@36960
   352
  Outer_Syntax.improper_command sledgehammerN
wenzelm@36960
   353
      "search for first-order proof using automatic theorem provers" Keyword.diag
blanchet@36394
   354
      parse_sledgehammer_command
blanchet@35963
   355
val _ =
wenzelm@36960
   356
  Outer_Syntax.command sledgehammer_paramsN
wenzelm@36960
   357
      "set and display the default parameters for Sledgehammer" Keyword.thy_decl
blanchet@36394
   358
      parse_sledgehammer_params_command
blanchet@36394
   359
blanchet@39318
   360
fun auto_sledgehammer state =
blanchet@40931
   361
  let val ctxt = Proof.context_of state in
blanchet@40931
   362
    run_sledgehammer (get_params true ctxt []) true 1 no_relevance_override
blanchet@40931
   363
                     (minimize_command [] 1) state
blanchet@40931
   364
  end
blanchet@39318
   365
blanchet@40931
   366
val setup = Auto_Tools.register_tool (auto, auto_sledgehammer)
blanchet@39318
   367
blanchet@35866
   368
end;