src/HOL/Tools/SMT2/smt2_solver.ML
author blanchet
Mon Jun 02 17:34:27 2014 +0200 (2014-06-02)
changeset 57159 24cbdebba35a
parent 57158 f028d93798e6
child 57163 7fc7de3b387e
permissions -rw-r--r--
refactored Z3 to Isar proof construction code
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@57159
    29
  val smt2_filter: Proof.context -> thm -> ((string * 'a) * (int option * thm)) list -> int ->
blanchet@57159
    30
    Time.time ->
blanchet@57159
    31
    {outcome: SMT2_Failure.failure option, fact_ids: (int * ((string * 'a) * thm)) list,
blanchet@57159
    32
     atp_proof: unit -> (term, string) ATP_Proof.atp_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@57158
   186
    | (result, _) => raise SMT2_Failure.SMT (SMT2_Failure.Counterexample (result = Sat)))
blanchet@57157
   187
blanchet@57157
   188
  fun replay outcome replay0 oracle outer_ctxt
blanchet@56132
   189
      (replay_data as {context = ctxt, ...} : SMT2_Translate.replay_data) output =
blanchet@56078
   190
    (case outcome output of
blanchet@56078
   191
      (Unsat, ls) =>
blanchet@57157
   192
        if not (Config.get ctxt SMT2_Config.oracle) andalso is_some replay0
blanchet@57157
   193
        then the replay0 outer_ctxt replay_data ls
blanchet@57157
   194
        else oracle ()
blanchet@57158
   195
    | (result, _) => raise SMT2_Failure.SMT (SMT2_Failure.Counterexample (result = Sat)))
blanchet@56078
   196
blanchet@57157
   197
  val cfalse = Thm.cterm_of @{theory} @{prop False}
blanchet@56078
   198
in
blanchet@56078
   199
blanchet@56125
   200
fun add_solver ({name, class, avail, command, options, default_max_relevant, can_filter,
blanchet@57157
   201
    outcome, parse_proof = parse_proof0, replay = replay0} : solver_config) =
blanchet@56078
   202
  let
blanchet@57157
   203
    fun solver oracle = {
blanchet@56078
   204
      command = command,
blanchet@56078
   205
      default_max_relevant = default_max_relevant,
blanchet@56125
   206
      can_filter = can_filter,
blanchet@57157
   207
      parse_proof = parse_proof (outcome name) parse_proof0,
blanchet@57157
   208
      replay = replay (outcome name) replay0 oracle}
blanchet@56078
   209
blanchet@56132
   210
    val info = {name = name, class = class, avail = avail, options = options}
blanchet@56078
   211
  in
blanchet@57157
   212
    Thm.add_oracle (Binding.name name, K cfalse) #-> (fn (_, oracle) =>
blanchet@57157
   213
    Context.theory_map (Solvers.map (Symtab.update_new (name, solver oracle)))) #>
blanchet@56078
   214
    Context.theory_map (SMT2_Config.add_solver info)
blanchet@56078
   215
  end
blanchet@56078
   216
blanchet@56078
   217
end
blanchet@56078
   218
blanchet@56125
   219
fun get_info ctxt name = the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)
blanchet@56078
   220
blanchet@56078
   221
fun name_and_info_of ctxt =
blanchet@56132
   222
  let val name = SMT2_Config.solver_of ctxt
blanchet@56078
   223
  in (name, get_info ctxt name) end
blanchet@56078
   224
blanchet@57157
   225
fun apply_solver_and_parse_proof ctxt wthms0 =
blanchet@56082
   226
  let
blanchet@56106
   227
    val wthms = map (apsnd (check_topsort ctxt)) wthms0
blanchet@57157
   228
    val (name, {command, parse_proof, ...}) = name_and_info_of ctxt
blanchet@56125
   229
    val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt wthms) ctxt
blanchet@57157
   230
  in (parse_proof ctxt replay_data output, replay_data) end
blanchet@57157
   231
blanchet@57157
   232
fun apply_solver_and_replay ctxt wthms0 =
blanchet@57157
   233
  let
blanchet@57157
   234
    val wthms = map (apsnd (check_topsort ctxt)) wthms0
blanchet@57157
   235
    val (name, {command, replay, ...}) = name_and_info_of ctxt
blanchet@57157
   236
    val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt wthms) ctxt
blanchet@57157
   237
  in replay ctxt replay_data output end
blanchet@56078
   238
blanchet@56078
   239
val default_max_relevant = #default_max_relevant oo get_info
blanchet@56125
   240
val can_filter = #can_filter o snd o name_and_info_of 
blanchet@56078
   241
blanchet@56078
   242
blanchet@56078
   243
(* filter *)
blanchet@56078
   244
blanchet@56106
   245
val no_id = ~1
blanchet@56078
   246
blanchet@56104
   247
fun smt2_filter ctxt goal xwfacts i time_limit =
blanchet@56078
   248
  let
blanchet@56078
   249
    val ctxt =
blanchet@56078
   250
      ctxt
blanchet@56078
   251
      |> Config.put SMT2_Config.oracle false
blanchet@56082
   252
      |> Config.put SMT2_Config.timeout (Time.toReal time_limit)
blanchet@56078
   253
blanchet@56132
   254
    val ({context = ctxt, prems, concl, ...}, _) = Subgoal.focus ctxt i goal
blanchet@56106
   255
    fun negate ct = Thm.dest_comb ct ||> Thm.apply @{cterm Not} |-> Thm.apply
blanchet@56078
   256
    val cprop =
blanchet@56082
   257
      (case try negate (Thm.rhs_of (SMT2_Normalize.atomize_conv ctxt concl)) of
blanchet@56078
   258
        SOME ct => ct
blanchet@56094
   259
      | NONE => raise SMT2_Failure.SMT (SMT2_Failure.Other_Failure "goal is not a HOL term"))
blanchet@56082
   260
blanchet@56106
   261
    val wconjecture = (NONE, Thm.assume cprop)
blanchet@56106
   262
    val wprems = map (pair NONE) prems
blanchet@56106
   263
    val wfacts = map snd xwfacts
blanchet@56106
   264
    val wthms = wconjecture :: wprems @ wfacts
blanchet@56106
   265
    val iwthms = map_index I wthms
blanchet@56082
   266
blanchet@56106
   267
    val conjecture_i = 0
blanchet@56981
   268
    val prems_i = 1
blanchet@56981
   269
    val facts_i = prems_i + length wprems
blanchet@56078
   270
  in
blanchet@56106
   271
    wthms
blanchet@57157
   272
    |> apply_solver_and_parse_proof ctxt
blanchet@57157
   273
    |> (fn ((iidths0, z3_proof), {rewrite_rules, ...}) =>
blanchet@56981
   274
      let
blanchet@56981
   275
        val iidths = if can_filter ctxt then iidths0 else map (apsnd (apfst (K no_id))) iwthms
blanchet@57159
   276
blanchet@56981
   277
        fun id_of_index i = the_default no_id (Option.map fst (AList.lookup (op =) iidths i))
blanchet@57159
   278
blanchet@57159
   279
        val conjecture_id = id_of_index conjecture_i
blanchet@57159
   280
        val prem_ids = map id_of_index (prems_i upto facts_i - 1)
blanchet@57159
   281
        val helper_ids = map_filter (try (fn (~1, idth) => idth)) iidths
blanchet@57159
   282
        val fact_ids = map_filter (fn (i, (id, _)) =>
blanchet@57159
   283
          try (apsnd (apsnd snd o nth xwfacts)) (id, i - facts_i)) iidths
blanchet@57159
   284
        val fact_helper_ts =
blanchet@57159
   285
          map (fn (_, th) => (ATP_Util.short_thm_name ctxt th, prop_of th)) helper_ids @
blanchet@57159
   286
          map (fn (_, ((s, _), th)) => (s, prop_of th)) fact_ids
blanchet@57159
   287
        val fact_helper_ids =
blanchet@57159
   288
          map (fn (id, th) => (id, ATP_Util.short_thm_name ctxt th)) helper_ids @
blanchet@57159
   289
          map (fn (id, ((name, _), _)) => (id, name)) fact_ids
blanchet@56104
   290
      in
blanchet@57159
   291
        {outcome = NONE, fact_ids = fact_ids,
blanchet@57159
   292
         atp_proof = fn () => Z3_New_Isar.atp_proof_of_z3_proof ctxt rewrite_rules
blanchet@57159
   293
           (map Thm.prop_of prems) (Thm.term_of concl) fact_helper_ts prem_ids conjecture_id
blanchet@57159
   294
           fact_helper_ids z3_proof}
blanchet@56104
   295
      end)
blanchet@56078
   296
  end
blanchet@57159
   297
  handle SMT2_Failure.SMT fail => {outcome = SOME fail, fact_ids = [], atp_proof = K []}
blanchet@56078
   298
blanchet@56078
   299
blanchet@56078
   300
(* SMT tactic *)
blanchet@56078
   301
blanchet@56078
   302
local
blanchet@56078
   303
  fun str_of ctxt fail =
blanchet@57158
   304
    "Solver " ^ SMT2_Config.solver_of ctxt ^ ": " ^ SMT2_Failure.string_of_failure fail
blanchet@56078
   305
blanchet@57157
   306
  fun safe_solve ctxt wfacts = SOME (apply_solver_and_replay ctxt wfacts)
blanchet@56078
   307
    handle
blanchet@56078
   308
      SMT2_Failure.SMT (fail as SMT2_Failure.Counterexample _) =>
blanchet@56078
   309
        (SMT2_Config.verbose_msg ctxt (str_of ctxt) fail; NONE)
blanchet@56078
   310
    | SMT2_Failure.SMT (fail as SMT2_Failure.Time_Out) =>
blanchet@56078
   311
        error ("SMT: Solver " ^ quote (SMT2_Config.solver_of ctxt) ^ ": " ^
blanchet@57158
   312
          SMT2_Failure.string_of_failure fail ^ " (setting the " ^
blanchet@56078
   313
          "configuration option " ^ quote (Config.name_of SMT2_Config.timeout) ^ " might help)")
blanchet@56078
   314
    | SMT2_Failure.SMT fail => error (str_of ctxt fail)
blanchet@56078
   315
blanchet@56078
   316
  fun resolve (SOME thm) = rtac thm 1
blanchet@56078
   317
    | resolve NONE = no_tac
blanchet@56078
   318
blanchet@56078
   319
  fun tac prove ctxt rules =
blanchet@56078
   320
    CONVERSION (SMT2_Normalize.atomize_conv ctxt)
blanchet@56078
   321
    THEN' rtac @{thm ccontr}
blanchet@56106
   322
    THEN' SUBPROOF (fn {context = ctxt, prems, ...} =>
blanchet@56106
   323
      resolve (prove ctxt (map (pair NONE) (rules @ prems)))) ctxt
blanchet@56078
   324
in
blanchet@56078
   325
blanchet@56078
   326
val smt2_tac = tac safe_solve
blanchet@57157
   327
val smt2_tac' = tac (SOME oo apply_solver_and_replay)
blanchet@56078
   328
blanchet@56078
   329
end
blanchet@56078
   330
blanchet@56078
   331
end