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