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