src/HOL/Tools/SMT2/smt2_solver.ML
author blanchet
Thu Jun 12 17:02:03 2014 +0200 (2014-06-12)
changeset 57239 a40edeaa01b1
parent 57229 489083abce44
permissions -rw-r--r--
don't ask proof-disabled solvers to do proofs
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@57239
    23
     smt_options: (string * string) list,
blanchet@57163
    24
     default_max_relevant: int,
blanchet@57163
    25
     outcome: string -> string list -> outcome * string list,
blanchet@57164
    26
     parse_proof: (Proof.context -> SMT2_Translate.replay_data ->
blanchet@57164
    27
       ((string * ATP_Problem_Generate.stature) * thm) list -> term list -> term -> string list ->
blanchet@57164
    28
       parsed_proof) option,
blanchet@57163
    29
     replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> thm) option}
blanchet@56078
    30
blanchet@56078
    31
  (*registry*)
blanchet@56078
    32
  val add_solver: solver_config -> theory -> theory
blanchet@56078
    33
  val default_max_relevant: Proof.context -> string -> int
blanchet@56078
    34
blanchet@56078
    35
  (*filter*)
blanchet@57165
    36
  val smt2_filter: Proof.context -> thm -> ((string * ATP_Problem_Generate.stature) * thm) list ->
blanchet@57165
    37
    int -> Time.time -> parsed_proof
blanchet@56078
    38
blanchet@56078
    39
  (*tactic*)
blanchet@56078
    40
  val smt2_tac: Proof.context -> thm list -> int -> tactic
blanchet@56078
    41
  val smt2_tac': Proof.context -> thm list -> int -> tactic
blanchet@57229
    42
end;
blanchet@56078
    43
blanchet@56078
    44
structure SMT2_Solver: SMT2_SOLVER =
blanchet@56078
    45
struct
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@57239
   122
fun invoke name command smt_options 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@57239
   130
      |> SMT2_Translate.translate ctxt smt_options 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@57239
   152
   smt_options: (string * string) list,
blanchet@57163
   153
   default_max_relevant: int,
blanchet@57163
   154
   outcome: string -> string list -> outcome * string list,
blanchet@57164
   155
   parse_proof: (Proof.context -> SMT2_Translate.replay_data ->
blanchet@57164
   156
     ((string * ATP_Problem_Generate.stature) * thm) list -> term list -> term -> string list ->
blanchet@57164
   157
     parsed_proof) option,
blanchet@57163
   158
   replay: (Proof.context -> SMT2_Translate.replay_data -> string list -> thm) option}
blanchet@56078
   159
blanchet@56078
   160
blanchet@56106
   161
(* check well-sortedness *)
blanchet@56106
   162
blanchet@56106
   163
val has_topsort = Term.exists_type (Term.exists_subtype (fn
blanchet@56106
   164
    TFree (_, []) => true
blanchet@56106
   165
  | TVar (_, []) => true
blanchet@56106
   166
  | _ => false))
blanchet@56106
   167
blanchet@56106
   168
(* top sorts cause problems with atomization *)
blanchet@56106
   169
fun check_topsort ctxt thm =
blanchet@56106
   170
  if has_topsort (Thm.prop_of thm) then (SMT2_Normalize.drop_fact_warning ctxt thm; TrueI) else thm
blanchet@56106
   171
blanchet@56106
   172
blanchet@56078
   173
(* registry *)
blanchet@56078
   174
blanchet@56078
   175
type solver_info = {
blanchet@56078
   176
  command: unit -> string list,
blanchet@57239
   177
  smt_options: (string * string) list,
blanchet@56078
   178
  default_max_relevant: int,
blanchet@57164
   179
  parse_proof: Proof.context -> SMT2_Translate.replay_data ->
blanchet@57164
   180
    ((string * ATP_Problem_Generate.stature) * thm) list -> term list -> term -> string list ->
blanchet@57164
   181
    parsed_proof,
blanchet@57157
   182
  replay: Proof.context -> SMT2_Translate.replay_data -> string list -> thm}
blanchet@56078
   183
blanchet@56078
   184
structure Solvers = Generic_Data
blanchet@56078
   185
(
blanchet@56078
   186
  type T = solver_info Symtab.table
blanchet@56078
   187
  val empty = Symtab.empty
blanchet@56078
   188
  val extend = I
blanchet@56078
   189
  fun merge data = Symtab.merge (K true) data
blanchet@56078
   190
)
blanchet@56078
   191
blanchet@56078
   192
local
blanchet@57164
   193
  fun parse_proof outcome parse_proof0 outer_ctxt replay_data xfacts prems concl output =
blanchet@57157
   194
    (case outcome output of
blanchet@57164
   195
      (Unsat, lines) =>
blanchet@57164
   196
        (case parse_proof0 of
blanchet@57164
   197
          SOME pp => pp outer_ctxt replay_data xfacts prems concl lines
blanchet@57164
   198
        | NONE => {outcome = NONE, fact_ids = [], atp_proof = K []})
blanchet@57158
   199
    | (result, _) => raise SMT2_Failure.SMT (SMT2_Failure.Counterexample (result = Sat)))
blanchet@57157
   200
blanchet@57157
   201
  fun replay outcome replay0 oracle outer_ctxt
blanchet@56132
   202
      (replay_data as {context = ctxt, ...} : SMT2_Translate.replay_data) output =
blanchet@56078
   203
    (case outcome output of
blanchet@57164
   204
      (Unsat, lines) =>
blanchet@57157
   205
        if not (Config.get ctxt SMT2_Config.oracle) andalso is_some replay0
blanchet@57164
   206
        then the replay0 outer_ctxt replay_data lines
blanchet@57157
   207
        else oracle ()
blanchet@57158
   208
    | (result, _) => raise SMT2_Failure.SMT (SMT2_Failure.Counterexample (result = Sat)))
blanchet@56078
   209
blanchet@57157
   210
  val cfalse = Thm.cterm_of @{theory} @{prop False}
blanchet@56078
   211
in
blanchet@56078
   212
blanchet@57239
   213
fun add_solver ({name, class, avail, command, options, smt_options, default_max_relevant, outcome,
blanchet@57164
   214
    parse_proof = parse_proof0, replay = replay0} : solver_config) =
blanchet@56078
   215
  let
blanchet@57157
   216
    fun solver oracle = {
blanchet@56078
   217
      command = command,
blanchet@57239
   218
      smt_options = smt_options,
blanchet@56078
   219
      default_max_relevant = default_max_relevant,
blanchet@57157
   220
      parse_proof = parse_proof (outcome name) parse_proof0,
blanchet@57157
   221
      replay = replay (outcome name) replay0 oracle}
blanchet@56078
   222
blanchet@56132
   223
    val info = {name = name, class = class, avail = avail, options = options}
blanchet@56078
   224
  in
blanchet@57157
   225
    Thm.add_oracle (Binding.name name, K cfalse) #-> (fn (_, oracle) =>
blanchet@57157
   226
    Context.theory_map (Solvers.map (Symtab.update_new (name, solver oracle)))) #>
blanchet@56078
   227
    Context.theory_map (SMT2_Config.add_solver info)
blanchet@56078
   228
  end
blanchet@56078
   229
blanchet@56078
   230
end
blanchet@56078
   231
blanchet@56125
   232
fun get_info ctxt name = the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)
blanchet@56078
   233
blanchet@56078
   234
fun name_and_info_of ctxt =
blanchet@56132
   235
  let val name = SMT2_Config.solver_of ctxt
blanchet@56078
   236
  in (name, get_info ctxt name) end
blanchet@56078
   237
blanchet@57163
   238
val default_max_relevant = #default_max_relevant oo get_info
blanchet@57157
   239
blanchet@57165
   240
fun apply_solver_and_replay ctxt thms0 =
blanchet@57157
   241
  let
blanchet@57165
   242
    val thms = map (check_topsort ctxt) thms0
blanchet@57239
   243
    val (name, {command, smt_options, replay, ...}) = name_and_info_of ctxt
blanchet@57239
   244
    val (output, replay_data) =
blanchet@57239
   245
      invoke name command smt_options (SMT2_Normalize.normalize ctxt thms) ctxt
blanchet@57157
   246
  in replay ctxt replay_data output end
blanchet@56078
   247
blanchet@56078
   248
blanchet@56078
   249
(* filter *)
blanchet@56078
   250
blanchet@57165
   251
fun smt2_filter ctxt0 goal xfacts i time_limit =
blanchet@56078
   252
  let
blanchet@57163
   253
    val ctxt = ctxt0 |> Config.put SMT2_Config.timeout (Time.toReal time_limit)
blanchet@56078
   254
blanchet@56132
   255
    val ({context = ctxt, prems, concl, ...}, _) = Subgoal.focus ctxt i goal
blanchet@56106
   256
    fun negate ct = Thm.dest_comb ct ||> Thm.apply @{cterm Not} |-> Thm.apply
blanchet@56078
   257
    val cprop =
blanchet@56082
   258
      (case try negate (Thm.rhs_of (SMT2_Normalize.atomize_conv ctxt concl)) of
blanchet@56078
   259
        SOME ct => ct
blanchet@56094
   260
      | NONE => raise SMT2_Failure.SMT (SMT2_Failure.Other_Failure "goal is not a HOL term"))
blanchet@56082
   261
blanchet@57165
   262
    val conjecture = Thm.assume cprop
blanchet@57165
   263
    val facts = map snd xfacts
blanchet@57165
   264
    val thms = conjecture :: prems @ facts
blanchet@57165
   265
    val thms' = map (check_topsort ctxt) thms
blanchet@57159
   266
blanchet@57239
   267
    val (name, {command, smt_options, parse_proof, ...}) = name_and_info_of ctxt
blanchet@57239
   268
    val (output, replay_data) =
blanchet@57239
   269
      invoke name command smt_options (SMT2_Normalize.normalize ctxt thms') ctxt
blanchet@57164
   270
  in
blanchet@57164
   271
    parse_proof ctxt replay_data xfacts (map Thm.prop_of prems) (Thm.term_of concl) output
blanchet@56078
   272
  end
blanchet@57159
   273
  handle SMT2_Failure.SMT fail => {outcome = SOME fail, fact_ids = [], atp_proof = K []}
blanchet@56078
   274
blanchet@56078
   275
blanchet@56078
   276
(* SMT tactic *)
blanchet@56078
   277
blanchet@56078
   278
local
blanchet@56078
   279
  fun str_of ctxt fail =
blanchet@57158
   280
    "Solver " ^ SMT2_Config.solver_of ctxt ^ ": " ^ SMT2_Failure.string_of_failure fail
blanchet@56078
   281
blanchet@57165
   282
  fun safe_solve ctxt facts = SOME (apply_solver_and_replay ctxt facts)
blanchet@56078
   283
    handle
blanchet@56078
   284
      SMT2_Failure.SMT (fail as SMT2_Failure.Counterexample _) =>
blanchet@56078
   285
        (SMT2_Config.verbose_msg ctxt (str_of ctxt) fail; NONE)
blanchet@56078
   286
    | SMT2_Failure.SMT (fail as SMT2_Failure.Time_Out) =>
blanchet@56078
   287
        error ("SMT: Solver " ^ quote (SMT2_Config.solver_of ctxt) ^ ": " ^
blanchet@57158
   288
          SMT2_Failure.string_of_failure fail ^ " (setting the " ^
blanchet@56078
   289
          "configuration option " ^ quote (Config.name_of SMT2_Config.timeout) ^ " might help)")
blanchet@56078
   290
    | SMT2_Failure.SMT fail => error (str_of ctxt fail)
blanchet@56078
   291
blanchet@56078
   292
  fun resolve (SOME thm) = rtac thm 1
blanchet@56078
   293
    | resolve NONE = no_tac
blanchet@56078
   294
blanchet@56078
   295
  fun tac prove ctxt rules =
blanchet@56078
   296
    CONVERSION (SMT2_Normalize.atomize_conv ctxt)
blanchet@56078
   297
    THEN' rtac @{thm ccontr}
blanchet@57165
   298
    THEN' SUBPROOF (fn {context = ctxt, prems, ...} => resolve (prove ctxt (rules @ prems))) ctxt
blanchet@56078
   299
in
blanchet@56078
   300
blanchet@56078
   301
val smt2_tac = tac safe_solve
blanchet@57157
   302
val smt2_tac' = tac (SOME oo apply_solver_and_replay)
blanchet@56078
   303
blanchet@56078
   304
end
blanchet@56078
   305
blanchet@57229
   306
end;