src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author blanchet
Mon, 25 Oct 2010 09:29:43 +0200
changeset 40359 e5ed638e49b0
parent 40253 27f2a45b0aab
child 40385 c0da8252b2fa
permissions -rw-r--r--
make "sledgehammer_params" work on single-threaded platforms
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@35961
     7
signature SLEDGEHAMMER_ISAR =
blanchet@35961
     8
sig
blanchet@38255
     9
  type params = Sledgehammer.params
blanchet@35961
    10
blanchet@39564
    11
  val auto : bool Unsynchronized.ref
blanchet@40240
    12
  val provers : string Unsynchronized.ref
blanchet@39564
    13
  val timeout : int Unsynchronized.ref
blanchet@39564
    14
  val full_types : bool Unsynchronized.ref
blanchet@40250
    15
  val default_params : Proof.context -> (string * string) list -> params
blanchet@39564
    16
  val setup : theory -> theory
blanchet@35961
    17
end;
blanchet@35961
    18
blanchet@35969
    19
structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
blanchet@35866
    20
struct
blanchet@35866
    21
blanchet@38262
    22
open ATP_Systems
blanchet@35969
    23
open Sledgehammer_Util
blanchet@38255
    24
open Sledgehammer
blanchet@39232
    25
open Sledgehammer_Minimize
blanchet@35866
    26
blanchet@39564
    27
val auto = Unsynchronized.ref false
blanchet@39564
    28
blanchet@39564
    29
val _ =
blanchet@39564
    30
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@39564
    31
      (Preferences.bool_pref auto "auto-sledgehammer"
blanchet@39573
    32
           "Run Sledgehammer automatically.")
blanchet@39564
    33
blanchet@36394
    34
(** Sledgehammer commands **)
blanchet@36394
    35
blanchet@39564
    36
val no_relevance_override = {add = [], del = [], only = false}
blanchet@39564
    37
fun add_relevance_override ns : relevance_override =
blanchet@35964
    38
  {add = ns, del = [], only = false}
blanchet@39564
    39
fun del_relevance_override ns : relevance_override =
blanchet@35964
    40
  {add = [], del = ns, only = false}
blanchet@35964
    41
fun only_relevance_override ns : relevance_override =
blanchet@35964
    42
  {add = ns, del = [], only = true}
blanchet@36003
    43
fun merge_relevance_override_pairwise (r1 : relevance_override)
blanchet@36003
    44
                                      (r2 : relevance_override) =
blanchet@35964
    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@35963
    49
blanchet@36371
    50
(*** parameters ***)
blanchet@36371
    51
blanchet@40240
    52
val provers = Unsynchronized.ref ""
blanchet@39581
    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@40240
    58
      (Preferences.string_pref provers
blanchet@40240
    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@35961
    73
type raw_param = string * string list
blanchet@35866
    74
blanchet@35961
    75
val default_default_params =
blanchet@39226
    76
  [("blocking", "false"),
blanchet@39226
    77
   ("debug", "false"),
blanchet@35961
    78
   ("verbose", "false"),
blanchet@36141
    79
   ("overlord", "false"),
blanchet@36235
    80
   ("explicit_apply", "false"),
blanchet@39165
    81
   ("relevance_thresholds", "45 85"),
blanchet@38983
    82
   ("max_relevant", "smart"),
blanchet@35961
    83
   ("isar_proof", "false"),
blanchet@38813
    84
   ("isar_shrink_factor", "1")]
blanchet@35866
    85
blanchet@36138
    86
val alias_params =
blanchet@40240
    87
  [("prover", "provers"),
blanchet@40240
    88
   ("atps", "provers"), (* FIXME: legacy *)
blanchet@40240
    89
   ("atp", "provers")]  (* FIXME: legacy *)
blanchet@36138
    90
val negated_alias_params =
blanchet@39226
    91
  [("non_blocking", "blocking"),
blanchet@39226
    92
   ("no_debug", "debug"),
blanchet@35961
    93
   ("quiet", "verbose"),
blanchet@36141
    94
   ("no_overlord", "overlord"),
blanchet@36399
    95
   ("partial_types", "full_types"),
blanchet@36235
    96
   ("implicit_apply", "explicit_apply"),
blanchet@36900
    97
   ("no_isar_proof", "isar_proof")]
blanchet@35866
    98
blanchet@36281
    99
val params_for_minimize =
blanchet@38346
   100
  ["debug", "verbose", "overlord", "full_types", "isar_proof",
blanchet@38813
   101
   "isar_shrink_factor", "timeout"]
blanchet@36281
   102
blanchet@40240
   103
val property_dependent_params = ["provers", "full_types", "timeout"]
blanchet@35866
   104
blanchet@35961
   105
fun is_known_raw_param s =
blanchet@35961
   106
  AList.defined (op =) default_default_params s orelse
blanchet@36138
   107
  AList.defined (op =) alias_params s orelse
blanchet@36138
   108
  AList.defined (op =) negated_alias_params s orelse
blanchet@39229
   109
  member (op =) property_dependent_params s orelse s = "expect"
blanchet@35866
   110
blanchet@35961
   111
fun check_raw_param (s, _) =
blanchet@35961
   112
  if is_known_raw_param s then ()
blanchet@35961
   113
  else error ("Unknown parameter: " ^ quote s ^ ".")
blanchet@35961
   114
blanchet@36138
   115
fun unalias_raw_param (name, value) =
blanchet@36138
   116
  case AList.lookup (op =) alias_params name of
blanchet@36138
   117
    SOME name' => (name', value)
blanchet@36138
   118
  | NONE =>
blanchet@36138
   119
    case AList.lookup (op =) negated_alias_params name of
blanchet@36138
   120
      SOME name' => (name', case value of
blanchet@36138
   121
                              ["false"] => ["true"]
blanchet@36138
   122
                            | ["true"] => ["false"]
blanchet@36138
   123
                            | [] => ["false"]
blanchet@36138
   124
                            | _ => value)
blanchet@36138
   125
    | NONE => (name, value)
blanchet@35961
   126
blanchet@35961
   127
structure Data = Theory_Data(
blanchet@35961
   128
  type T = raw_param list
blanchet@35961
   129
  val empty = default_default_params |> map (apsnd single)
blanchet@35961
   130
  val extend = I
blanchet@35961
   131
  fun merge p : T = AList.merge (op =) (K true) p)
blanchet@35961
   132
blanchet@40243
   133
(* FIXME: dummy *)
blanchet@40250
   134
fun is_smt_solver_installed ctxt = true
blanchet@40243
   135
blanchet@40253
   136
fun remotify_prover_if_available_and_not_already_remote thy name =
blanchet@40253
   137
  if String.isPrefix remote_prefix name then
blanchet@40253
   138
    SOME name
blanchet@40253
   139
  else
blanchet@40253
   140
    let val remote_name = remote_prefix ^ name in
blanchet@40253
   141
      if is_prover_available thy remote_name then SOME remote_name else NONE
blanchet@40253
   142
    end
blanchet@40253
   143
blanchet@40253
   144
fun remotify_prover_if_not_installed ctxt name =
blanchet@40253
   145
  let val thy = ProofContext.theory_of ctxt in
blanchet@40253
   146
    if is_prover_available thy name andalso is_prover_installed ctxt name then
blanchet@40253
   147
      SOME name
blanchet@40253
   148
    else
blanchet@40253
   149
      remotify_prover_if_available_and_not_already_remote thy name
blanchet@40253
   150
  end
blanchet@40253
   151
blanchet@40253
   152
fun avoid_too_many_local_threads _ _ [] = []
blanchet@40253
   153
  | avoid_too_many_local_threads thy 0 provers =
blanchet@40253
   154
    map_filter (remotify_prover_if_available_and_not_already_remote thy) provers
blanchet@40253
   155
  | avoid_too_many_local_threads thy n (prover :: provers) =
blanchet@40253
   156
    let val n = if String.isPrefix remote_prefix prover then n else n - 1 in
blanchet@40253
   157
      prover :: avoid_too_many_local_threads thy n provers
blanchet@40253
   158
    end
blanchet@40240
   159
blanchet@40359
   160
val num_processors = try Thread.numProcessors #> the_default 1
blanchet@40359
   161
blanchet@40240
   162
(* The first ATP of the list is used by Auto Sledgehammer. Because of the low
blanchet@40240
   163
   timeout, it makes sense to put SPASS first. *)
blanchet@40250
   164
fun default_provers_param_value ctxt =
blanchet@40250
   165
  let val thy = ProofContext.theory_of ctxt in
blanchet@40253
   166
    [spassN, eN, vampireN, sine_eN (* FIXME: , smtN *)]
blanchet@40253
   167
    |> map_filter (remotify_prover_if_not_installed ctxt)
blanchet@40359
   168
    |> avoid_too_many_local_threads thy (num_processors ())
blanchet@40250
   169
    |> space_implode " "
blanchet@40250
   170
  end
blanchet@40240
   171
blanchet@36138
   172
val set_default_raw_param = Data.map o AList.update (op =) o unalias_raw_param
blanchet@40250
   173
fun default_raw_params ctxt =
blanchet@40250
   174
  let val thy = ProofContext.theory_of ctxt in
blanchet@40250
   175
    Data.get thy
blanchet@40250
   176
    |> fold (AList.default (op =))
blanchet@40250
   177
            [("provers", [case !provers of
blanchet@40250
   178
                            "" => default_provers_param_value ctxt
blanchet@40250
   179
                          | s => s]),
blanchet@40250
   180
             ("full_types", [if !full_types then "true" else "false"]),
blanchet@40250
   181
             ("timeout", let val timeout = !timeout in
blanchet@40250
   182
                           [if timeout <= 0 then "none"
blanchet@40250
   183
                            else string_of_int timeout ^ " s"]
blanchet@40250
   184
                         end)]
blanchet@40250
   185
  end
blanchet@35961
   186
blanchet@35961
   187
val infinity_time_in_secs = 60 * 60 * 24 * 365
blanchet@35961
   188
val the_timeout = the_default (Time.fromSeconds infinity_time_in_secs)
blanchet@35961
   189
blanchet@39564
   190
fun extract_params auto default_params override_params =
blanchet@35961
   191
  let
blanchet@36138
   192
    val override_params = map unalias_raw_param override_params
blanchet@35961
   193
    val raw_params = rev override_params @ rev default_params
blanchet@35961
   194
    val lookup = Option.map (space_implode " ") o AList.lookup (op =) raw_params
blanchet@35961
   195
    val lookup_string = the_default "" o lookup
blanchet@35961
   196
    fun general_lookup_bool option default_value name =
blanchet@35961
   197
      case lookup name of
blanchet@35968
   198
        SOME s => parse_bool_option option name s
blanchet@35961
   199
      | NONE => default_value
blanchet@35961
   200
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35961
   201
    fun lookup_time name =
blanchet@39564
   202
      case lookup name of
blanchet@39564
   203
        SOME s => parse_time_option name s
blanchet@39564
   204
      | NONE => NONE
blanchet@35964
   205
    fun lookup_int name =
blanchet@35961
   206
      case lookup name of
blanchet@35964
   207
        NONE => 0
blanchet@35964
   208
      | SOME s => case Int.fromString s of
blanchet@35964
   209
                    SOME n => n
blanchet@35964
   210
                  | NONE => error ("Parameter " ^ quote name ^
blanchet@35964
   211
                                   " must be assigned an integer value.")
blanchet@38984
   212
    fun lookup_int_pair name =
blanchet@38984
   213
      case lookup name of
blanchet@38984
   214
        NONE => (0, 0)
blanchet@38984
   215
      | SOME s => case s |> space_explode " " |> map Int.fromString of
blanchet@38984
   216
                    [SOME n1, SOME n2] => (n1, n2)
blanchet@38984
   217
                  | _ => error ("Parameter " ^ quote name ^
blanchet@38984
   218
                                "must be assigned a pair of integer values \
blanchet@38984
   219
                                \(e.g., \"60 95\")")
blanchet@38812
   220
    fun lookup_int_option name =
blanchet@38812
   221
      case lookup name of
blanchet@38812
   222
        SOME "smart" => NONE
blanchet@38812
   223
      | _ => SOME (lookup_int name)
blanchet@39564
   224
    val blocking = auto orelse lookup_bool "blocking"
blanchet@39564
   225
    val debug = not auto andalso lookup_bool "debug"
blanchet@39564
   226
    val verbose = debug orelse (not auto andalso lookup_bool "verbose")
blanchet@36141
   227
    val overlord = lookup_bool "overlord"
blanchet@40240
   228
    val provers = lookup_string "provers" |> space_explode " "
blanchet@40240
   229
                  |> auto ? single o hd
blanchet@35961
   230
    val full_types = lookup_bool "full_types"
blanchet@36235
   231
    val explicit_apply = lookup_bool "explicit_apply"
blanchet@38984
   232
    val relevance_thresholds =
blanchet@38984
   233
      lookup_int_pair "relevance_thresholds"
blanchet@38984
   234
      |> pairself (fn n => 0.01 * Real.fromInt n)
blanchet@38983
   235
    val max_relevant = lookup_int_option "max_relevant"
blanchet@35961
   236
    val isar_proof = lookup_bool "isar_proof"
blanchet@36916
   237
    val isar_shrink_factor = Int.max (1, lookup_int "isar_shrink_factor")
blanchet@39564
   238
    val timeout = (if auto then NONE else lookup_time "timeout") |> the_timeout
blanchet@39229
   239
    val expect = lookup_string "expect"
blanchet@35961
   240
  in
blanchet@39226
   241
    {blocking = blocking, debug = debug, verbose = verbose, overlord = overlord,
blanchet@40240
   242
     provers = provers, full_types = full_types,
blanchet@40240
   243
     explicit_apply = explicit_apply,
blanchet@38984
   244
     relevance_thresholds = relevance_thresholds, max_relevant = max_relevant,
blanchet@39241
   245
     isar_proof = isar_proof, isar_shrink_factor = isar_shrink_factor,
blanchet@39241
   246
     timeout = timeout, expect = expect}
blanchet@35961
   247
  end
blanchet@35961
   248
blanchet@40250
   249
fun get_params auto ctxt = extract_params auto (default_raw_params ctxt)
blanchet@39564
   250
fun default_params thy = get_params false thy o map (apsnd single)
blanchet@35866
   251
blanchet@36373
   252
(* Sledgehammer the given subgoal *)
blanchet@36373
   253
blanchet@36373
   254
val sledgehammerN = "sledgehammer"
blanchet@36373
   255
val sledgehammer_paramsN = "sledgehammer_params"
blanchet@36373
   256
blanchet@35963
   257
val runN = "run"
blanchet@35963
   258
val minimizeN = "minimize"
blanchet@35963
   259
val messagesN = "messages"
blanchet@40240
   260
val available_proversN = "available_provers"
blanchet@40240
   261
val running_proversN = "running_provers"
blanchet@40240
   262
val kill_proversN = "kill_provers"
blanchet@35963
   263
val refresh_tptpN = "refresh_tptp"
blanchet@35866
   264
blanchet@36281
   265
val is_raw_param_relevant_for_minimize =
blanchet@36281
   266
  member (op =) params_for_minimize o fst o unalias_raw_param
blanchet@36281
   267
fun string_for_raw_param (key, values) =
blanchet@36477
   268
  key ^ (case space_implode " " values of "" => "" | value => " = " ^ value)
blanchet@36281
   269
blanchet@40240
   270
fun minimize_command override_params i prover_name fact_names =
blanchet@40240
   271
  sledgehammerN ^ " " ^ minimizeN ^ " [prover = " ^ prover_name ^
blanchet@36281
   272
  (override_params |> filter is_raw_param_relevant_for_minimize
blanchet@36281
   273
                   |> implode o map (prefix ", " o string_for_raw_param)) ^
blanchet@37165
   274
  "] (" ^ space_implode " " fact_names ^ ")" ^
blanchet@36281
   275
  (if i = 1 then "" else " " ^ string_of_int i)
blanchet@36281
   276
blanchet@35964
   277
fun hammer_away override_params subcommand opt_i relevance_override state =
blanchet@35961
   278
  let
blanchet@40250
   279
    val ctxt = Proof.context_of state
blanchet@35961
   280
    val thy = Proof.theory_of state
blanchet@36477
   281
    val _ = app check_raw_param override_params
blanchet@35963
   282
  in
blanchet@35963
   283
    if subcommand = runN then
blanchet@36281
   284
      let val i = the_default 1 opt_i in
blanchet@40250
   285
        run_sledgehammer (get_params false ctxt override_params) false i
blanchet@39564
   286
                         relevance_override (minimize_command override_params i)
blanchet@39564
   287
                         state
blanchet@39564
   288
        |> K ()
blanchet@36281
   289
      end
blanchet@35963
   290
    else if subcommand = minimizeN then
blanchet@40250
   291
      run_minimize (get_params false ctxt override_params) (the_default 1 opt_i)
blanchet@38813
   292
                   (#add relevance_override) state
blanchet@35963
   293
    else if subcommand = messagesN then
blanchet@35963
   294
      messages opt_i
blanchet@40240
   295
    else if subcommand = available_proversN then
blanchet@40240
   296
      available_provers thy
blanchet@40240
   297
    else if subcommand = running_proversN then
blanchet@40240
   298
      running_provers ()
blanchet@40240
   299
    else if subcommand = kill_proversN then
blanchet@40240
   300
      kill_provers ()
blanchet@35963
   301
    else if subcommand = refresh_tptpN then
blanchet@35963
   302
      refresh_systems_on_tptp ()
blanchet@35963
   303
    else
blanchet@35963
   304
      error ("Unknown subcommand: " ^ quote subcommand ^ ".")
blanchet@35963
   305
  end
blanchet@35961
   306
blanchet@35964
   307
fun sledgehammer_trans (((subcommand, params), relevance_override), opt_i) =
blanchet@35964
   308
  Toplevel.keep (hammer_away params subcommand opt_i relevance_override
blanchet@35964
   309
                 o Toplevel.proof_of)
blanchet@35961
   310
blanchet@35961
   311
fun string_for_raw_param (name, value) = name ^ " = " ^ space_implode " " value
blanchet@35961
   312
blanchet@35963
   313
fun sledgehammer_params_trans params =
blanchet@35961
   314
  Toplevel.theory
blanchet@35963
   315
      (fold set_default_raw_param params
blanchet@39564
   316
       #> tap (fn thy =>
blanchet@40250
   317
                  let val ctxt = ProofContext.init_global thy in
blanchet@40250
   318
                    writeln ("Default parameters for Sledgehammer:\n" ^
blanchet@40250
   319
                             (case default_raw_params ctxt |> rev of
blanchet@40250
   320
                                [] => "none"
blanchet@40250
   321
                              | params =>
blanchet@40250
   322
                                (map check_raw_param params;
blanchet@40250
   323
                                 params |> map string_for_raw_param
blanchet@40250
   324
                                        |> sort_strings |> cat_lines)))
blanchet@40250
   325
                  end))
blanchet@35961
   326
wenzelm@36970
   327
val parse_key = Scan.repeat1 Parse.typ_group >> space_implode " "
wenzelm@36970
   328
val parse_value = Scan.repeat1 Parse.xname
wenzelm@36970
   329
val parse_param = parse_key -- Scan.optional (Parse.$$$ "=" |-- parse_value) []
wenzelm@36970
   330
val parse_params = Scan.optional (Args.bracks (Parse.list parse_param)) []
blanchet@35963
   331
val parse_fact_refs =
blanchet@39240
   332
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse_Spec.xthm)
blanchet@35964
   333
val parse_relevance_chunk =
blanchet@39564
   334
  (Args.add |-- Args.colon |-- parse_fact_refs >> add_relevance_override)
blanchet@39564
   335
  || (Args.del |-- Args.colon |-- parse_fact_refs >> del_relevance_override)
blanchet@36188
   336
  || (parse_fact_refs >> only_relevance_override)
blanchet@35964
   337
val parse_relevance_override =
blanchet@36188
   338
  Scan.optional (Args.parens (Scan.repeat parse_relevance_chunk
blanchet@36188
   339
                              >> merge_relevance_overrides))
blanchet@39564
   340
                no_relevance_override
blanchet@35961
   341
val parse_sledgehammer_command =
blanchet@37374
   342
  (Scan.optional Parse.short_ident runN -- parse_params
blanchet@37374
   343
   -- parse_relevance_override -- Scan.option Parse.nat) #>> sledgehammer_trans
blanchet@35961
   344
val parse_sledgehammer_params_command =
blanchet@35961
   345
  parse_params #>> sledgehammer_params_trans
blanchet@35961
   346
blanchet@35961
   347
val _ =
wenzelm@36970
   348
  Outer_Syntax.improper_command sledgehammerN
wenzelm@36970
   349
      "search for first-order proof using automatic theorem provers" Keyword.diag
blanchet@36394
   350
      parse_sledgehammer_command
blanchet@35961
   351
val _ =
wenzelm@36970
   352
  Outer_Syntax.command sledgehammer_paramsN
wenzelm@36970
   353
      "set and display the default parameters for Sledgehammer" Keyword.thy_decl
blanchet@36394
   354
      parse_sledgehammer_params_command
blanchet@36394
   355
blanchet@39564
   356
fun auto_sledgehammer state =
blanchet@39564
   357
  if not (!auto) then
blanchet@39564
   358
    (false, state)
blanchet@39564
   359
  else
blanchet@40250
   360
    let val ctxt = Proof.context_of state in
blanchet@40250
   361
      run_sledgehammer (get_params true ctxt []) true 1 no_relevance_override
blanchet@39564
   362
                       (minimize_command [] 1) state
blanchet@39564
   363
    end
blanchet@39564
   364
blanchet@39570
   365
val setup = Auto_Tools.register_tool ("sledgehammer", auto_sledgehammer)
blanchet@39564
   366
blanchet@35866
   367
end;