src/HOL/Tools/SMT2/smt2_solver.ML
author blanchet
Mon Jun 02 17:34:26 2014 +0200 (2014-06-02)
changeset 57157 87b4d54b1fbe
parent 57156 3546a67226ea
child 57158 f028d93798e6
permissions -rw-r--r--
split replay and proof parsing for Z3
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@56090
    13
    class: Proof.context -> SMT2_Util.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@56125
    18
    can_filter: bool,
blanchet@56078
    19
    outcome: string -> string list -> outcome * string list,
blanchet@57157
    20
    parse_proof: (Proof.context -> SMT2_Translate.replay_data -> string list ->
blanchet@57157
    21
      (int * (int * thm)) list * Z3_New_Proof.z3_step list) option,
blanchet@57157
    22
    replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> thm) option}
blanchet@56078
    23
blanchet@56078
    24
  (*registry*)
blanchet@56078
    25
  val add_solver: solver_config -> theory -> theory
blanchet@56078
    26
  val default_max_relevant: Proof.context -> string -> int
blanchet@56078
    27
blanchet@56078
    28
  (*filter*)
blanchet@56104
    29
  val smt2_filter: Proof.context -> thm -> ('a * (int option * thm)) list -> int -> Time.time ->
blanchet@56128
    30
    {outcome: SMT2_Failure.failure option, rewrite_rules: thm list, conjecture_id: int,
blanchet@56981
    31
     prem_ids: int list, helper_ids: (int * thm) list, fact_ids: (int * ('a * thm)) list,
blanchet@56128
    32
     z3_proof: Z3_New_Proof.z3_step list}
blanchet@56078
    33
blanchet@56078
    34
  (*tactic*)
blanchet@56078
    35
  val smt2_tac: Proof.context -> thm list -> int -> tactic
blanchet@56078
    36
  val smt2_tac': Proof.context -> thm list -> int -> tactic
blanchet@56078
    37
end
blanchet@56078
    38
blanchet@56078
    39
structure SMT2_Solver: SMT2_SOLVER =
blanchet@56078
    40
struct
blanchet@56078
    41
blanchet@56078
    42
blanchet@56078
    43
(* interface to external solvers *)
blanchet@56078
    44
blanchet@56078
    45
local
blanchet@56078
    46
blanchet@56132
    47
fun make_command command options problem_path proof_path =
blanchet@56132
    48
  "(exec 2>&1;" :: map File.shell_quote (command () @ options) @
blanchet@56132
    49
  [File.shell_path problem_path, ")", ">", File.shell_path proof_path]
blanchet@56132
    50
  |> space_implode " "
blanchet@56078
    51
blanchet@56132
    52
fun with_trace ctxt msg f x =
blanchet@56078
    53
  let val _ = SMT2_Config.trace_msg ctxt (fn () => msg) ()
blanchet@56078
    54
  in f x end
blanchet@56078
    55
blanchet@56078
    56
fun run ctxt name mk_cmd input =
blanchet@56078
    57
  (case SMT2_Config.certificates_of ctxt of
blanchet@56078
    58
    NONE =>
blanchet@56078
    59
      if not (SMT2_Config.is_available ctxt name) then
blanchet@56131
    60
        error ("The SMT solver " ^ quote name ^ " is not installed")
blanchet@56078
    61
      else if Config.get ctxt SMT2_Config.debug_files = "" then
blanchet@56132
    62
        with_trace ctxt ("Invoking SMT solver " ^ quote name ^ " ...") (Cache_IO.run mk_cmd) input
blanchet@56078
    63
      else
blanchet@56078
    64
        let
blanchet@56078
    65
          val base_path = Path.explode (Config.get ctxt SMT2_Config.debug_files)
blanchet@56078
    66
          val in_path = Path.ext "smt2_in" base_path
blanchet@56078
    67
          val out_path = Path.ext "smt2_out" base_path
blanchet@56078
    68
        in Cache_IO.raw_run mk_cmd input in_path out_path end
blanchet@56078
    69
  | SOME certs =>
blanchet@56078
    70
      (case Cache_IO.lookup certs input of
blanchet@56078
    71
        (NONE, key) =>
blanchet@56078
    72
          if Config.get ctxt SMT2_Config.read_only_certificates then
blanchet@56078
    73
            error ("Bad certificate cache: missing certificate")
blanchet@56078
    74
          else
blanchet@56078
    75
            Cache_IO.run_and_cache certs key mk_cmd input
blanchet@56078
    76
      | (SOME output, _) =>
blanchet@56132
    77
          with_trace ctxt ("Using cached certificate from " ^
blanchet@56132
    78
            File.shell_path (Cache_IO.cache_path_of certs) ^ " ...") I output))
blanchet@56078
    79
blanchet@56087
    80
(* Z3 returns 1 if "get-model" or "get-model" fails *)
blanchet@56087
    81
val normal_return_codes = [0, 1]
blanchet@56087
    82
blanchet@56078
    83
fun run_solver ctxt name mk_cmd input =
blanchet@56078
    84
  let
blanchet@56087
    85
    fun pretty tag ls = Pretty.string_of (Pretty.big_list tag (map Pretty.str ls))
blanchet@56078
    86
blanchet@56078
    87
    val _ = SMT2_Config.trace_msg ctxt (pretty "Problem:" o split_lines) input
blanchet@56078
    88
blanchet@56132
    89
    val {redirected_output = res, output = err, return_code} =
blanchet@56078
    90
      SMT2_Config.with_timeout ctxt (run ctxt name mk_cmd) input
blanchet@56078
    91
    val _ = SMT2_Config.trace_msg ctxt (pretty "Solver:") err
blanchet@56078
    92
blanchet@56078
    93
    val output = fst (take_suffix (equal "") res)
blanchet@56078
    94
    val _ = SMT2_Config.trace_msg ctxt (pretty "Result:") output
blanchet@56078
    95
blanchet@56087
    96
    val _ = member (op =) normal_return_codes return_code orelse
blanchet@56078
    97
      raise SMT2_Failure.SMT (SMT2_Failure.Abnormal_Termination return_code)
blanchet@56078
    98
  in output end
blanchet@56078
    99
blanchet@56078
   100
fun trace_assms ctxt =
blanchet@56078
   101
  SMT2_Config.trace_msg ctxt (Pretty.string_of o
blanchet@56078
   102
    Pretty.big_list "Assertions:" o map (Display.pretty_thm ctxt o snd))
blanchet@56078
   103
blanchet@56132
   104
fun trace_replay_data ({context = ctxt, typs, terms, ...} : SMT2_Translate.replay_data) =
blanchet@56078
   105
  let
blanchet@56078
   106
    fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
blanchet@56078
   107
    fun p_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
blanchet@56078
   108
    fun p_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
blanchet@56078
   109
  in
blanchet@56078
   110
    SMT2_Config.trace_msg ctxt (fn () =>
blanchet@56078
   111
      Pretty.string_of (Pretty.big_list "Names:" [
blanchet@56078
   112
        Pretty.big_list "sorts:" (map p_typ (Symtab.dest typs)),
blanchet@56078
   113
        Pretty.big_list "functions:" (map p_term (Symtab.dest terms))])) ()
blanchet@56078
   114
  end
blanchet@56078
   115
blanchet@56078
   116
in
blanchet@56078
   117
blanchet@56078
   118
fun invoke name command ithms ctxt =
blanchet@56078
   119
  let
blanchet@56078
   120
    val options = SMT2_Config.solver_options_of ctxt
blanchet@56112
   121
    val comments = [space_implode " " options]
blanchet@56078
   122
blanchet@56132
   123
    val (str, replay_data as {context = ctxt', ...}) =
blanchet@56078
   124
      ithms
blanchet@56078
   125
      |> tap (trace_assms ctxt)
blanchet@56078
   126
      |> SMT2_Translate.translate ctxt comments
blanchet@56078
   127
      ||> tap trace_replay_data
blanchet@56132
   128
  in (run_solver ctxt' name (make_command command options) str, replay_data) end
blanchet@56078
   129
blanchet@56078
   130
end
blanchet@56078
   131
blanchet@56078
   132
blanchet@56078
   133
(* configuration *)
blanchet@56078
   134
blanchet@56078
   135
datatype outcome = Unsat | Sat | Unknown
blanchet@56078
   136
blanchet@56078
   137
type solver_config = {
blanchet@56078
   138
  name: string,
blanchet@56090
   139
  class: Proof.context -> SMT2_Util.class,
blanchet@56078
   140
  avail: unit -> bool,
blanchet@56078
   141
  command: unit -> string list,
blanchet@56078
   142
  options: Proof.context -> string list,
blanchet@56078
   143
  default_max_relevant: int,
blanchet@56125
   144
  can_filter: bool,
blanchet@56078
   145
  outcome: string -> string list -> outcome * string list,
blanchet@57157
   146
  parse_proof: (Proof.context -> SMT2_Translate.replay_data -> string list ->
blanchet@57157
   147
    (int * (int * thm)) list * Z3_New_Proof.z3_step list) option,
blanchet@57157
   148
  replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> thm) option}
blanchet@56078
   149
blanchet@56078
   150
blanchet@56106
   151
(* check well-sortedness *)
blanchet@56106
   152
blanchet@56106
   153
val has_topsort = Term.exists_type (Term.exists_subtype (fn
blanchet@56106
   154
    TFree (_, []) => true
blanchet@56106
   155
  | TVar (_, []) => true
blanchet@56106
   156
  | _ => false))
blanchet@56106
   157
blanchet@56106
   158
(* top sorts cause problems with atomization *)
blanchet@56106
   159
fun check_topsort ctxt thm =
blanchet@56106
   160
  if has_topsort (Thm.prop_of thm) then (SMT2_Normalize.drop_fact_warning ctxt thm; TrueI) else thm
blanchet@56106
   161
blanchet@56106
   162
blanchet@56078
   163
(* registry *)
blanchet@56078
   164
blanchet@56078
   165
type solver_info = {
blanchet@56078
   166
  command: unit -> string list,
blanchet@56078
   167
  default_max_relevant: int,
blanchet@56125
   168
  can_filter: bool,
blanchet@57157
   169
  parse_proof: Proof.context -> SMT2_Translate.replay_data -> string list ->
blanchet@57157
   170
    (int * (int * thm)) list * Z3_New_Proof.z3_step list,
blanchet@57157
   171
  replay: Proof.context -> SMT2_Translate.replay_data -> string list -> thm}
blanchet@56078
   172
blanchet@56078
   173
structure Solvers = Generic_Data
blanchet@56078
   174
(
blanchet@56078
   175
  type T = solver_info Symtab.table
blanchet@56078
   176
  val empty = Symtab.empty
blanchet@56078
   177
  val extend = I
blanchet@56078
   178
  fun merge data = Symtab.merge (K true) data
blanchet@56078
   179
)
blanchet@56078
   180
blanchet@56078
   181
local
blanchet@57157
   182
  fun parse_proof outcome parse_proof0 outer_ctxt replay_data output =
blanchet@57157
   183
    (case outcome output of
blanchet@57157
   184
      (Unsat, ls) =>
blanchet@57157
   185
        (case parse_proof0 of SOME pp => pp outer_ctxt replay_data ls | NONE => ([], []))
blanchet@57157
   186
    | (result, _) =>
blanchet@57157
   187
        raise SMT2_Failure.SMT (SMT2_Failure.Counterexample {
blanchet@57157
   188
          is_real_cex = (result = Sat), free_constraints = [], const_defs = []}))
blanchet@57157
   189
blanchet@57157
   190
  fun replay outcome replay0 oracle outer_ctxt
blanchet@56132
   191
      (replay_data as {context = ctxt, ...} : SMT2_Translate.replay_data) output =
blanchet@56078
   192
    (case outcome output of
blanchet@56078
   193
      (Unsat, ls) =>
blanchet@57157
   194
        if not (Config.get ctxt SMT2_Config.oracle) andalso is_some replay0
blanchet@57157
   195
        then the replay0 outer_ctxt replay_data ls
blanchet@57157
   196
        else oracle ()
blanchet@57157
   197
    | (result, _) =>
blanchet@57156
   198
        raise SMT2_Failure.SMT (SMT2_Failure.Counterexample {
blanchet@57157
   199
          is_real_cex = (result = Sat), free_constraints = [], const_defs = []}))
blanchet@56078
   200
blanchet@57157
   201
  val cfalse = Thm.cterm_of @{theory} @{prop False}
blanchet@56078
   202
in
blanchet@56078
   203
blanchet@56125
   204
fun add_solver ({name, class, avail, command, options, default_max_relevant, can_filter,
blanchet@57157
   205
    outcome, parse_proof = parse_proof0, replay = replay0} : solver_config) =
blanchet@56078
   206
  let
blanchet@57157
   207
    fun solver oracle = {
blanchet@56078
   208
      command = command,
blanchet@56078
   209
      default_max_relevant = default_max_relevant,
blanchet@56125
   210
      can_filter = can_filter,
blanchet@57157
   211
      parse_proof = parse_proof (outcome name) parse_proof0,
blanchet@57157
   212
      replay = replay (outcome name) replay0 oracle}
blanchet@56078
   213
blanchet@56132
   214
    val info = {name = name, class = class, avail = avail, options = options}
blanchet@56078
   215
  in
blanchet@57157
   216
    Thm.add_oracle (Binding.name name, K cfalse) #-> (fn (_, oracle) =>
blanchet@57157
   217
    Context.theory_map (Solvers.map (Symtab.update_new (name, solver oracle)))) #>
blanchet@56078
   218
    Context.theory_map (SMT2_Config.add_solver info)
blanchet@56078
   219
  end
blanchet@56078
   220
blanchet@56078
   221
end
blanchet@56078
   222
blanchet@56125
   223
fun get_info ctxt name = the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)
blanchet@56078
   224
blanchet@56078
   225
fun name_and_info_of ctxt =
blanchet@56132
   226
  let val name = SMT2_Config.solver_of ctxt
blanchet@56078
   227
  in (name, get_info ctxt name) end
blanchet@56078
   228
blanchet@57157
   229
fun apply_solver_and_parse_proof ctxt wthms0 =
blanchet@56082
   230
  let
blanchet@56106
   231
    val wthms = map (apsnd (check_topsort ctxt)) wthms0
blanchet@57157
   232
    val (name, {command, parse_proof, ...}) = name_and_info_of ctxt
blanchet@56125
   233
    val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt wthms) ctxt
blanchet@57157
   234
  in (parse_proof ctxt replay_data output, replay_data) end
blanchet@57157
   235
blanchet@57157
   236
fun apply_solver_and_replay ctxt wthms0 =
blanchet@57157
   237
  let
blanchet@57157
   238
    val wthms = map (apsnd (check_topsort ctxt)) wthms0
blanchet@57157
   239
    val (name, {command, replay, ...}) = name_and_info_of ctxt
blanchet@57157
   240
    val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt wthms) ctxt
blanchet@57157
   241
  in replay ctxt replay_data output end
blanchet@56078
   242
blanchet@56078
   243
val default_max_relevant = #default_max_relevant oo get_info
blanchet@56125
   244
val can_filter = #can_filter o snd o name_and_info_of 
blanchet@56078
   245
blanchet@56078
   246
blanchet@56078
   247
(* filter *)
blanchet@56078
   248
blanchet@56106
   249
val no_id = ~1
blanchet@56078
   250
blanchet@56104
   251
fun smt2_filter ctxt goal xwfacts i time_limit =
blanchet@56078
   252
  let
blanchet@56078
   253
    val ctxt =
blanchet@56078
   254
      ctxt
blanchet@56078
   255
      |> Config.put SMT2_Config.oracle false
blanchet@56082
   256
      |> Config.put SMT2_Config.timeout (Time.toReal time_limit)
blanchet@56078
   257
blanchet@56132
   258
    val ({context = ctxt, prems, concl, ...}, _) = Subgoal.focus ctxt i goal
blanchet@56106
   259
    fun negate ct = Thm.dest_comb ct ||> Thm.apply @{cterm Not} |-> Thm.apply
blanchet@56078
   260
    val cprop =
blanchet@56082
   261
      (case try negate (Thm.rhs_of (SMT2_Normalize.atomize_conv ctxt concl)) of
blanchet@56078
   262
        SOME ct => ct
blanchet@56094
   263
      | NONE => raise SMT2_Failure.SMT (SMT2_Failure.Other_Failure "goal is not a HOL term"))
blanchet@56082
   264
blanchet@56106
   265
    val wconjecture = (NONE, Thm.assume cprop)
blanchet@56106
   266
    val wprems = map (pair NONE) prems
blanchet@56106
   267
    val wfacts = map snd xwfacts
blanchet@56106
   268
    val wthms = wconjecture :: wprems @ wfacts
blanchet@56106
   269
    val iwthms = map_index I wthms
blanchet@56082
   270
blanchet@56106
   271
    val conjecture_i = 0
blanchet@56981
   272
    val prems_i = 1
blanchet@56981
   273
    val facts_i = prems_i + length wprems
blanchet@56078
   274
  in
blanchet@56106
   275
    wthms
blanchet@57157
   276
    |> apply_solver_and_parse_proof ctxt
blanchet@57157
   277
    |> (fn ((iidths0, z3_proof), {rewrite_rules, ...}) =>
blanchet@56981
   278
      let
blanchet@56981
   279
        val iidths = if can_filter ctxt then iidths0 else map (apsnd (apfst (K no_id))) iwthms
blanchet@56981
   280
        fun id_of_index i = the_default no_id (Option.map fst (AList.lookup (op =) iidths i))
blanchet@56104
   281
      in
blanchet@56128
   282
        {outcome = NONE,
blanchet@56128
   283
         rewrite_rules = rewrite_rules,
blanchet@56981
   284
         conjecture_id = id_of_index conjecture_i,
blanchet@56981
   285
         prem_ids = map id_of_index (prems_i upto facts_i - 1),
blanchet@56106
   286
         helper_ids = map_filter (try (fn (~1, idth) => idth)) iidths,
blanchet@56106
   287
         fact_ids = map_filter (fn (i, (id, _)) =>
blanchet@56106
   288
           try (apsnd (apsnd snd o nth xwfacts)) (id, i - facts_i)) iidths,
blanchet@56104
   289
         z3_proof = z3_proof}
blanchet@56104
   290
      end)
blanchet@56078
   291
  end
blanchet@56128
   292
  handle SMT2_Failure.SMT fail => {outcome = SOME fail, rewrite_rules = [], conjecture_id = no_id,
blanchet@56981
   293
    prem_ids = [], helper_ids = [], fact_ids = [], z3_proof = []}
blanchet@56078
   294
blanchet@56078
   295
blanchet@56078
   296
(* SMT tactic *)
blanchet@56078
   297
blanchet@56078
   298
local
blanchet@56078
   299
  fun str_of ctxt fail =
blanchet@56132
   300
    "Solver " ^ SMT2_Config.solver_of ctxt ^ ": " ^ SMT2_Failure.string_of_failure ctxt fail
blanchet@56078
   301
blanchet@57157
   302
  fun safe_solve ctxt wfacts = SOME (apply_solver_and_replay ctxt wfacts)
blanchet@56078
   303
    handle
blanchet@56078
   304
      SMT2_Failure.SMT (fail as SMT2_Failure.Counterexample _) =>
blanchet@56078
   305
        (SMT2_Config.verbose_msg ctxt (str_of ctxt) fail; NONE)
blanchet@56078
   306
    | SMT2_Failure.SMT (fail as SMT2_Failure.Time_Out) =>
blanchet@56078
   307
        error ("SMT: Solver " ^ quote (SMT2_Config.solver_of ctxt) ^ ": " ^
blanchet@56078
   308
          SMT2_Failure.string_of_failure ctxt fail ^ " (setting the " ^
blanchet@56078
   309
          "configuration option " ^ quote (Config.name_of SMT2_Config.timeout) ^ " might help)")
blanchet@56078
   310
    | SMT2_Failure.SMT fail => error (str_of ctxt fail)
blanchet@56078
   311
blanchet@56078
   312
  fun resolve (SOME thm) = rtac thm 1
blanchet@56078
   313
    | resolve NONE = no_tac
blanchet@56078
   314
blanchet@56078
   315
  fun tac prove ctxt rules =
blanchet@56078
   316
    CONVERSION (SMT2_Normalize.atomize_conv ctxt)
blanchet@56078
   317
    THEN' rtac @{thm ccontr}
blanchet@56106
   318
    THEN' SUBPROOF (fn {context = ctxt, prems, ...} =>
blanchet@56106
   319
      resolve (prove ctxt (map (pair NONE) (rules @ prems)))) ctxt
blanchet@56078
   320
in
blanchet@56078
   321
blanchet@56078
   322
val smt2_tac = tac safe_solve
blanchet@57157
   323
val smt2_tac' = tac (SOME oo apply_solver_and_replay)
blanchet@56078
   324
blanchet@56078
   325
end
blanchet@56078
   326
blanchet@56078
   327
end