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