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