src/HOL/Tools/SMT2/smt2_solver.ML
author blanchet
Thu Mar 13 13:18:13 2014 +0100 (2014-03-13)
changeset 56082 ffd99d397a9f
parent 56078 624faeda77b5
child 56083 b5d1d9c60341
permissions -rw-r--r--
tuned ML interface
blanchet@56078
     1
(*  Title:      HOL/Tools/SMT2/smt2_solver.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
SMT solvers registry and SMT tactic.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@56078
     7
signature SMT2_SOLVER =
blanchet@56078
     8
sig
blanchet@56078
     9
  (*configuration*)
blanchet@56078
    10
  datatype outcome = Unsat | Sat | Unknown
blanchet@56078
    11
  type solver_config = {
blanchet@56078
    12
    name: string,
blanchet@56078
    13
    class: Proof.context -> SMT2_Utils.class,
blanchet@56078
    14
    avail: unit -> bool,
blanchet@56078
    15
    command: unit -> string list,
blanchet@56078
    16
    options: Proof.context -> string list,
blanchet@56078
    17
    default_max_relevant: int,
blanchet@56078
    18
    supports_filter: bool,
blanchet@56078
    19
    outcome: string -> string list -> outcome * string list,
blanchet@56078
    20
    cex_parser: (Proof.context -> SMT2_Translate.replay_data -> string list ->
blanchet@56078
    21
      term list * term list) option,
blanchet@56078
    22
    replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> int list * thm) option }
blanchet@56078
    23
blanchet@56078
    24
  (*registry*)
blanchet@56078
    25
  val add_solver: solver_config -> theory -> theory
blanchet@56078
    26
  val solver_name_of: Proof.context -> string
blanchet@56078
    27
  val available_solvers_of: Proof.context -> string list
blanchet@56078
    28
  val apply_solver: Proof.context -> (int * (int option * thm)) list ->
blanchet@56078
    29
    int list * thm
blanchet@56078
    30
  val default_max_relevant: Proof.context -> string -> int
blanchet@56078
    31
blanchet@56078
    32
  (*filter*)
blanchet@56082
    33
  val smt2_filter: Proof.context -> thm list -> thm -> ('a * (int option * thm)) list -> int ->
blanchet@56082
    34
    Time.time -> {outcome: SMT2_Failure.failure option, used_facts: ('a * thm) list}
blanchet@56078
    35
blanchet@56078
    36
  (*tactic*)
blanchet@56078
    37
  val smt2_tac: Proof.context -> thm list -> int -> tactic
blanchet@56078
    38
  val smt2_tac': Proof.context -> thm list -> int -> tactic
blanchet@56078
    39
end
blanchet@56078
    40
blanchet@56078
    41
structure SMT2_Solver: SMT2_SOLVER =
blanchet@56078
    42
struct
blanchet@56078
    43
blanchet@56078
    44
blanchet@56078
    45
(* interface to external solvers *)
blanchet@56078
    46
blanchet@56078
    47
local
blanchet@56078
    48
blanchet@56078
    49
fun make_cmd command options problem_path proof_path = space_implode " " (
blanchet@56078
    50
  "(exec 2>&1;" :: map File.shell_quote (command @ options) @
blanchet@56078
    51
  [File.shell_path problem_path, ")", ">", File.shell_path proof_path])
blanchet@56078
    52
blanchet@56078
    53
fun trace_and ctxt msg f x =
blanchet@56078
    54
  let val _ = SMT2_Config.trace_msg ctxt (fn () => msg) ()
blanchet@56078
    55
  in f x end
blanchet@56078
    56
blanchet@56078
    57
fun run ctxt name mk_cmd input =
blanchet@56078
    58
  (case SMT2_Config.certificates_of ctxt of
blanchet@56078
    59
    NONE =>
blanchet@56078
    60
      if not (SMT2_Config.is_available ctxt name) then
blanchet@56078
    61
        error ("The SMT solver " ^ quote name ^ " is not installed.")
blanchet@56078
    62
      else if Config.get ctxt SMT2_Config.debug_files = "" then
blanchet@56078
    63
        trace_and ctxt ("Invoking SMT solver " ^ quote name ^ " ...")
blanchet@56078
    64
          (Cache_IO.run mk_cmd) input
blanchet@56078
    65
      else
blanchet@56078
    66
        let
blanchet@56078
    67
          val base_path = Path.explode (Config.get ctxt SMT2_Config.debug_files)
blanchet@56078
    68
          val in_path = Path.ext "smt2_in" base_path
blanchet@56078
    69
          val out_path = Path.ext "smt2_out" base_path
blanchet@56078
    70
        in Cache_IO.raw_run mk_cmd input in_path out_path end
blanchet@56078
    71
  | SOME certs =>
blanchet@56078
    72
      (case Cache_IO.lookup certs input of
blanchet@56078
    73
        (NONE, key) =>
blanchet@56078
    74
          if Config.get ctxt SMT2_Config.read_only_certificates then
blanchet@56078
    75
            error ("Bad certificate cache: missing certificate")
blanchet@56078
    76
          else
blanchet@56078
    77
            Cache_IO.run_and_cache certs key mk_cmd input
blanchet@56078
    78
      | (SOME output, _) =>
blanchet@56078
    79
          trace_and ctxt ("Using cached certificate from " ^
blanchet@56078
    80
            File.shell_path (Cache_IO.cache_path_of certs) ^ " ...")
blanchet@56078
    81
            I output))
blanchet@56078
    82
blanchet@56078
    83
fun run_solver ctxt name mk_cmd input =
blanchet@56078
    84
  let
blanchet@56078
    85
    fun pretty tag ls = Pretty.string_of (Pretty.big_list tag
blanchet@56078
    86
      (map Pretty.str ls))
blanchet@56078
    87
blanchet@56078
    88
    val _ = SMT2_Config.trace_msg ctxt (pretty "Problem:" o split_lines) input
blanchet@56078
    89
blanchet@56078
    90
    val {redirected_output=res, output=err, return_code} =
blanchet@56078
    91
      SMT2_Config.with_timeout ctxt (run ctxt name mk_cmd) input
blanchet@56078
    92
    val _ = SMT2_Config.trace_msg ctxt (pretty "Solver:") err
blanchet@56078
    93
blanchet@56078
    94
    val output = fst (take_suffix (equal "") res)
blanchet@56078
    95
    val _ = SMT2_Config.trace_msg ctxt (pretty "Result:") output
blanchet@56078
    96
blanchet@56078
    97
    val _ = return_code <> 0 andalso
blanchet@56078
    98
      raise SMT2_Failure.SMT (SMT2_Failure.Abnormal_Termination return_code)
blanchet@56078
    99
  in output end
blanchet@56078
   100
blanchet@56078
   101
fun trace_assms ctxt =
blanchet@56078
   102
  SMT2_Config.trace_msg ctxt (Pretty.string_of o
blanchet@56078
   103
    Pretty.big_list "Assertions:" o map (Display.pretty_thm ctxt o snd))
blanchet@56078
   104
blanchet@56078
   105
fun trace_replay_data ({context=ctxt, typs, terms, ...} : SMT2_Translate.replay_data) =
blanchet@56078
   106
  let
blanchet@56078
   107
    fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
blanchet@56078
   108
    fun p_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
blanchet@56078
   109
    fun p_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
blanchet@56078
   110
  in
blanchet@56078
   111
    SMT2_Config.trace_msg ctxt (fn () =>
blanchet@56078
   112
      Pretty.string_of (Pretty.big_list "Names:" [
blanchet@56078
   113
        Pretty.big_list "sorts:" (map p_typ (Symtab.dest typs)),
blanchet@56078
   114
        Pretty.big_list "functions:" (map p_term (Symtab.dest terms))])) ()
blanchet@56078
   115
  end
blanchet@56078
   116
blanchet@56078
   117
in
blanchet@56078
   118
blanchet@56078
   119
fun invoke name command ithms ctxt =
blanchet@56078
   120
  let
blanchet@56078
   121
    val options = SMT2_Config.solver_options_of ctxt
blanchet@56078
   122
    val cmd = command ()
blanchet@56078
   123
    val comments = [space_implode " " (cmd @ options)]
blanchet@56078
   124
blanchet@56078
   125
    val (str, replay_data as {context=ctxt', ...}) =
blanchet@56078
   126
      ithms
blanchet@56078
   127
      |> tap (trace_assms ctxt)
blanchet@56078
   128
      |> SMT2_Translate.translate ctxt comments
blanchet@56078
   129
      ||> tap trace_replay_data
blanchet@56078
   130
  in (run_solver ctxt' name (make_cmd cmd options) str, replay_data) end
blanchet@56078
   131
blanchet@56078
   132
end
blanchet@56078
   133
blanchet@56078
   134
blanchet@56078
   135
(* configuration *)
blanchet@56078
   136
blanchet@56078
   137
datatype outcome = Unsat | Sat | Unknown
blanchet@56078
   138
blanchet@56078
   139
type solver_config = {
blanchet@56078
   140
  name: string,
blanchet@56078
   141
  class: Proof.context -> SMT2_Utils.class,
blanchet@56078
   142
  avail: unit -> bool,
blanchet@56078
   143
  command: unit -> string list,
blanchet@56078
   144
  options: Proof.context -> string list,
blanchet@56078
   145
  default_max_relevant: int,
blanchet@56078
   146
  supports_filter: bool,
blanchet@56078
   147
  outcome: string -> string list -> outcome * string list,
blanchet@56078
   148
  cex_parser: (Proof.context -> SMT2_Translate.replay_data -> string list ->
blanchet@56078
   149
    term list * term list) option,
blanchet@56078
   150
  replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> int list * thm) option }
blanchet@56078
   151
blanchet@56078
   152
blanchet@56078
   153
(* registry *)
blanchet@56078
   154
blanchet@56078
   155
type solver_info = {
blanchet@56078
   156
  command: unit -> string list,
blanchet@56078
   157
  default_max_relevant: int,
blanchet@56078
   158
  supports_filter: bool,
blanchet@56078
   159
  replay: Proof.context -> string list * SMT2_Translate.replay_data -> int list * thm }
blanchet@56078
   160
blanchet@56078
   161
structure Solvers = Generic_Data
blanchet@56078
   162
(
blanchet@56078
   163
  type T = solver_info Symtab.table
blanchet@56078
   164
  val empty = Symtab.empty
blanchet@56078
   165
  val extend = I
blanchet@56078
   166
  fun merge data = Symtab.merge (K true) data
blanchet@56078
   167
)
blanchet@56078
   168
blanchet@56078
   169
local
blanchet@56078
   170
  fun finish outcome cex_parser replay ocl outer_ctxt
blanchet@56078
   171
      (output, (replay_data as {context=ctxt, ...} : SMT2_Translate.replay_data)) =
blanchet@56078
   172
    (case outcome output of
blanchet@56078
   173
      (Unsat, ls) =>
blanchet@56078
   174
        if not (Config.get ctxt SMT2_Config.oracle) andalso is_some replay
blanchet@56078
   175
        then the replay outer_ctxt replay_data ls
blanchet@56078
   176
        else ([], ocl ())
blanchet@56078
   177
    | (result, ls) =>
blanchet@56078
   178
        let
blanchet@56078
   179
          val (ts, us) =
blanchet@56078
   180
            (case cex_parser of SOME f => f ctxt replay_data ls | _ => ([], []))
blanchet@56078
   181
         in
blanchet@56078
   182
          raise SMT2_Failure.SMT (SMT2_Failure.Counterexample {
blanchet@56078
   183
            is_real_cex = (result = Sat),
blanchet@56078
   184
            free_constraints = ts,
blanchet@56078
   185
            const_defs = us})
blanchet@56078
   186
        end)
blanchet@56078
   187
blanchet@56078
   188
  val cfalse = Thm.cterm_of @{theory} (@{const Trueprop} $ @{const False})
blanchet@56078
   189
in
blanchet@56078
   190
blanchet@56078
   191
fun add_solver cfg =
blanchet@56078
   192
  let
blanchet@56078
   193
    val {name, class, avail, command, options, default_max_relevant,
blanchet@56078
   194
      supports_filter, outcome, cex_parser, replay} = cfg
blanchet@56078
   195
blanchet@56078
   196
    fun core_oracle () = cfalse
blanchet@56078
   197
blanchet@56078
   198
    fun solver ocl = {
blanchet@56078
   199
      command = command,
blanchet@56078
   200
      default_max_relevant = default_max_relevant,
blanchet@56078
   201
      supports_filter = supports_filter,
blanchet@56078
   202
      replay = finish (outcome name) cex_parser replay ocl }
blanchet@56078
   203
blanchet@56078
   204
    val info = {name=name, class=class, avail=avail, options=options}
blanchet@56078
   205
  in
blanchet@56078
   206
    Thm.add_oracle (Binding.name name, core_oracle) #-> (fn (_, ocl) =>
blanchet@56078
   207
    Context.theory_map (Solvers.map (Symtab.update_new (name, solver ocl)))) #>
blanchet@56078
   208
    Context.theory_map (SMT2_Config.add_solver info)
blanchet@56078
   209
  end
blanchet@56078
   210
blanchet@56078
   211
end
blanchet@56078
   212
blanchet@56078
   213
fun get_info ctxt name =
blanchet@56078
   214
  the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)
blanchet@56078
   215
blanchet@56078
   216
val solver_name_of = SMT2_Config.solver_of
blanchet@56078
   217
blanchet@56078
   218
val available_solvers_of = SMT2_Config.available_solvers_of
blanchet@56078
   219
blanchet@56078
   220
fun name_and_info_of ctxt =
blanchet@56078
   221
  let val name = solver_name_of ctxt
blanchet@56078
   222
  in (name, get_info ctxt name) end
blanchet@56078
   223
blanchet@56082
   224
fun apply_solver ctxt0 iwthms =
blanchet@56082
   225
  let
blanchet@56082
   226
    val (ithms, ctxt) = SMT2_Normalize.normalize iwthms ctxt0
blanchet@56082
   227
    val (name, {command, replay, ...}) = name_and_info_of ctxt
blanchet@56078
   228
  in
blanchet@56082
   229
    invoke name command ithms ctxt
blanchet@56078
   230
    |> replay ctxt
blanchet@56078
   231
  end
blanchet@56078
   232
blanchet@56078
   233
val default_max_relevant = #default_max_relevant oo get_info
blanchet@56078
   234
blanchet@56078
   235
val supports_filter = #supports_filter o snd o name_and_info_of 
blanchet@56078
   236
blanchet@56078
   237
blanchet@56078
   238
(* check well-sortedness *)
blanchet@56078
   239
blanchet@56078
   240
val has_topsort = Term.exists_type (Term.exists_subtype (fn
blanchet@56078
   241
    TFree (_, []) => true
blanchet@56078
   242
  | TVar (_, []) => true
blanchet@56078
   243
  | _ => false))
blanchet@56078
   244
blanchet@56078
   245
(* without this test, we would run into problems when atomizing the rules: *)
blanchet@56078
   246
fun check_topsort ctxt thm =
blanchet@56078
   247
  if has_topsort (Thm.prop_of thm) then
blanchet@56078
   248
    (SMT2_Normalize.drop_fact_warning ctxt thm; TrueI)
blanchet@56078
   249
  else
blanchet@56078
   250
    thm
blanchet@56078
   251
blanchet@56078
   252
fun check_topsorts ctxt iwthms = map (apsnd (apsnd (check_topsort ctxt))) iwthms
blanchet@56078
   253
blanchet@56078
   254
blanchet@56078
   255
(* filter *)
blanchet@56078
   256
blanchet@56078
   257
val cnot = Thm.cterm_of @{theory} @{const Not}
blanchet@56078
   258
blanchet@56078
   259
fun mk_result outcome xrules = { outcome = outcome, used_facts = xrules }
blanchet@56078
   260
blanchet@56082
   261
fun smt2_filter ctxt facts goal xwthms i time_limit =
blanchet@56078
   262
  let
blanchet@56078
   263
    val ctxt =
blanchet@56078
   264
      ctxt
blanchet@56078
   265
      |> Config.put SMT2_Config.oracle false
blanchet@56078
   266
      |> Config.put SMT2_Config.filter_only_facts true
blanchet@56082
   267
      |> Config.put SMT2_Config.timeout (Time.toReal time_limit)
blanchet@56078
   268
blanchet@56082
   269
    val ({context=ctxt, prems, concl, ...}, _) = Subgoal.focus ctxt i goal
blanchet@56078
   270
    fun negate ct = Thm.dest_comb ct ||> Thm.apply cnot |-> Thm.apply
blanchet@56078
   271
    val cprop =
blanchet@56082
   272
      (case try negate (Thm.rhs_of (SMT2_Normalize.atomize_conv ctxt concl)) of
blanchet@56078
   273
        SOME ct => ct
blanchet@56078
   274
      | NONE => raise SMT2_Failure.SMT (SMT2_Failure.Other_Failure (
blanchet@56078
   275
          "goal is not a HOL term")))
blanchet@56082
   276
blanchet@56082
   277
    val xthms = map (apsnd snd) xwthms
blanchet@56082
   278
blanchet@56082
   279
    fun filter_thms false = K xthms
blanchet@56082
   280
      | filter_thms true = map_filter (try (nth xthms)) o fst
blanchet@56078
   281
  in
blanchet@56078
   282
    map snd xwthms
blanchet@56078
   283
    |> map_index I
blanchet@56078
   284
    |> append (map (pair ~1 o pair NONE) (Thm.assume cprop :: prems @ facts))
blanchet@56082
   285
    |> check_topsorts ctxt
blanchet@56082
   286
    |> apply_solver ctxt
blanchet@56082
   287
    |> filter_thms (supports_filter ctxt)
blanchet@56078
   288
    |> mk_result NONE
blanchet@56078
   289
  end
blanchet@56078
   290
  handle SMT2_Failure.SMT fail => mk_result (SOME fail) []
blanchet@56078
   291
blanchet@56078
   292
blanchet@56078
   293
(* SMT tactic *)
blanchet@56078
   294
blanchet@56078
   295
local
blanchet@56078
   296
  fun trace_assumptions ctxt iwthms idxs =
blanchet@56078
   297
    let
blanchet@56078
   298
      val wthms =
blanchet@56078
   299
        idxs
blanchet@56078
   300
        |> filter (fn i => i >= 0)
blanchet@56078
   301
        |> map_filter (AList.lookup (op =) iwthms)
blanchet@56078
   302
    in
blanchet@56078
   303
      if Config.get ctxt SMT2_Config.trace_used_facts andalso length wthms > 0
blanchet@56078
   304
      then
blanchet@56078
   305
        tracing (Pretty.string_of (Pretty.big_list "SMT used facts:"
blanchet@56078
   306
          (map (Display.pretty_thm ctxt o snd) wthms)))
blanchet@56078
   307
      else ()
blanchet@56078
   308
    end
blanchet@56078
   309
blanchet@56078
   310
  fun solve ctxt iwthms =
blanchet@56078
   311
    iwthms
blanchet@56078
   312
    |> check_topsorts ctxt
blanchet@56078
   313
    |> apply_solver ctxt
blanchet@56078
   314
    |>> trace_assumptions ctxt iwthms
blanchet@56078
   315
    |> snd
blanchet@56078
   316
blanchet@56078
   317
  fun str_of ctxt fail =
blanchet@56078
   318
    SMT2_Failure.string_of_failure ctxt fail
blanchet@56078
   319
    |> prefix ("Solver " ^ SMT2_Config.solver_of ctxt ^ ": ")
blanchet@56078
   320
blanchet@56078
   321
  fun safe_solve ctxt iwthms = SOME (solve ctxt iwthms)
blanchet@56078
   322
    handle
blanchet@56078
   323
      SMT2_Failure.SMT (fail as SMT2_Failure.Counterexample _) =>
blanchet@56078
   324
        (SMT2_Config.verbose_msg ctxt (str_of ctxt) fail; NONE)
blanchet@56078
   325
    | SMT2_Failure.SMT (fail as SMT2_Failure.Time_Out) =>
blanchet@56078
   326
        error ("SMT: Solver " ^ quote (SMT2_Config.solver_of ctxt) ^ ": " ^
blanchet@56078
   327
          SMT2_Failure.string_of_failure ctxt fail ^ " (setting the " ^
blanchet@56078
   328
          "configuration option " ^ quote (Config.name_of SMT2_Config.timeout) ^ " might help)")
blanchet@56078
   329
    | SMT2_Failure.SMT fail => error (str_of ctxt fail)
blanchet@56078
   330
blanchet@56078
   331
  fun tag_rules thms = map_index (apsnd (pair NONE)) thms
blanchet@56078
   332
  fun tag_prems thms = map (pair ~1 o pair NONE) thms
blanchet@56078
   333
blanchet@56078
   334
  fun resolve (SOME thm) = rtac thm 1
blanchet@56078
   335
    | resolve NONE = no_tac
blanchet@56078
   336
blanchet@56078
   337
  fun tac prove ctxt rules =
blanchet@56078
   338
    CONVERSION (SMT2_Normalize.atomize_conv ctxt)
blanchet@56078
   339
    THEN' rtac @{thm ccontr}
blanchet@56078
   340
    THEN' SUBPROOF (fn {context, prems, ...} =>
blanchet@56078
   341
      resolve (prove context (tag_rules rules @ tag_prems prems))) ctxt
blanchet@56078
   342
in
blanchet@56078
   343
blanchet@56078
   344
val smt2_tac = tac safe_solve
blanchet@56078
   345
val smt2_tac' = tac (SOME oo solve)
blanchet@56078
   346
blanchet@56078
   347
end
blanchet@56078
   348
blanchet@56078
   349
end