src/HOL/Tools/SMT/smt_solver.ML
author boehmes
Wed Nov 24 15:33:35 2010 +0100 (2010-11-24)
changeset 40686 4725ed462387
parent 40666 8db6c2b1591d
child 40828 47ff261431c4
permissions -rw-r--r--
swap names for built-in tester functions (to better reflect the intuition of what they do);
eta-expand all built-in functions (even those which are only partially supported)
boehmes@36898
     1
(*  Title:      HOL/Tools/SMT/smt_solver.ML
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
boehmes@36898
     4
SMT solvers registry and SMT tactic.
boehmes@36898
     5
*)
boehmes@36898
     6
boehmes@36898
     7
signature SMT_SOLVER =
boehmes@36898
     8
sig
boehmes@40424
     9
  (*configuration*)
boehmes@36898
    10
  type interface = {
boehmes@36898
    11
    extra_norm: SMT_Normalize.extra_norm,
boehmes@36898
    12
    translate: SMT_Translate.config }
boehmes@40162
    13
  datatype outcome = Unsat | Sat | Unknown
boehmes@36898
    14
  type solver_config = {
boehmes@40162
    15
    name: string,
boehmes@40162
    16
    env_var: string,
boehmes@40162
    17
    is_remote: bool,
boehmes@40162
    18
    options: Proof.context -> string list,
boehmes@36898
    19
    interface: interface,
boehmes@40162
    20
    outcome: string -> string list -> outcome * string list,
boehmes@40162
    21
    cex_parser: (Proof.context -> SMT_Translate.recon -> string list ->
boehmes@40162
    22
      term list) option,
boehmes@40162
    23
    reconstruct: (Proof.context -> SMT_Translate.recon -> string list ->
boehmes@40162
    24
      (int list * thm) * Proof.context) option }
boehmes@36898
    25
boehmes@40424
    26
  (*registry*)
boehmes@40196
    27
  type solver = bool option -> Proof.context -> (int * thm) list ->
boehmes@40196
    28
    int list * thm
boehmes@40162
    29
  val add_solver: solver_config -> theory -> theory
boehmes@40424
    30
  val solver_of: Proof.context -> solver
boehmes@40166
    31
  val is_locally_installed: Proof.context -> bool
boehmes@36898
    32
boehmes@40162
    33
  (*filter*)
boehmes@40164
    34
  val smt_filter: bool -> Time.time -> Proof.state -> ('a * thm) list -> int ->
blanchet@40666
    35
    {outcome: SMT_Failure.failure option, used_facts: ('a * thm) list,
boehmes@40197
    36
    run_time_in_msecs: int option}
boehmes@40161
    37
boehmes@36898
    38
  (*tactic*)
boehmes@36898
    39
  val smt_tac': bool -> Proof.context -> thm list -> int -> Tactical.tactic
boehmes@36898
    40
  val smt_tac: Proof.context -> thm list -> int -> Tactical.tactic
boehmes@36898
    41
boehmes@36898
    42
  (*setup*)
boehmes@36898
    43
  val setup: theory -> theory
boehmes@36898
    44
end
boehmes@36898
    45
boehmes@36898
    46
structure SMT_Solver: SMT_SOLVER =
boehmes@36898
    47
struct
boehmes@36898
    48
boehmes@40424
    49
structure C = SMT_Config
boehmes@36898
    50
boehmes@40162
    51
boehmes@40424
    52
(* configuration *)
boehmes@36898
    53
boehmes@36898
    54
type interface = {
boehmes@36898
    55
  extra_norm: SMT_Normalize.extra_norm,
boehmes@36898
    56
  translate: SMT_Translate.config }
boehmes@36898
    57
boehmes@40162
    58
datatype outcome = Unsat | Sat | Unknown
boehmes@40162
    59
boehmes@36898
    60
type solver_config = {
boehmes@40162
    61
  name: string,
boehmes@40162
    62
  env_var: string,
boehmes@40162
    63
  is_remote: bool,
boehmes@40162
    64
  options: Proof.context -> string list,
boehmes@36898
    65
  interface: interface,
boehmes@40162
    66
  outcome: string -> string list -> outcome * string list,
boehmes@40162
    67
  cex_parser: (Proof.context -> SMT_Translate.recon -> string list ->
boehmes@40162
    68
    term list) option,
boehmes@40162
    69
  reconstruct: (Proof.context -> SMT_Translate.recon -> string list ->
boehmes@40162
    70
    (int list * thm) * Proof.context) option }
boehmes@36898
    71
boehmes@36898
    72
boehmes@36898
    73
(* interface to external solvers *)
boehmes@36898
    74
boehmes@40166
    75
fun get_local_solver env_var =
boehmes@40166
    76
  let val local_solver = getenv env_var
boehmes@40166
    77
  in if local_solver <> "" then SOME local_solver else NONE end
boehmes@40166
    78
boehmes@36898
    79
local
boehmes@36898
    80
boehmes@40196
    81
fun choose (rm, env_var, is_remote, name) =
boehmes@36898
    82
  let
boehmes@40196
    83
    val force_local = (case rm of SOME false => true | _ => false)
boehmes@40196
    84
    val force_remote = (case rm of SOME true => true | _ => false)
boehmes@40166
    85
    val lsolver = get_local_solver env_var
boehmes@36898
    86
    val remote_url = getenv "REMOTE_SMT_URL"
boehmes@40196
    87
    val trace = if is_some rm then K () else tracing
boehmes@36898
    88
  in
boehmes@40166
    89
    if not force_remote andalso is_some lsolver
boehmes@36898
    90
    then 
boehmes@40196
    91
     (trace ("Invoking local SMT solver " ^ quote (the lsolver) ^ " ...");
boehmes@40166
    92
      [the lsolver])
boehmes@40196
    93
    else if not force_local andalso is_remote
boehmes@36898
    94
    then
boehmes@40196
    95
     (trace ("Invoking remote SMT solver " ^ quote name ^ " at " ^
boehmes@36898
    96
        quote remote_url ^ " ...");
boehmes@40166
    97
      [getenv "REMOTE_SMT", name])
boehmes@40166
    98
    else if force_remote
boehmes@40196
    99
    then error ("The SMT solver " ^ quote name ^ " is not remotely available.")
boehmes@40196
   100
    else error ("The SMT solver " ^ quote name ^ " has not been found " ^
boehmes@40196
   101
      "on this computer. Please set the Isabelle environment variable " ^
boehmes@40196
   102
      quote env_var ^ ".")
boehmes@36898
   103
  end
boehmes@36898
   104
boehmes@36898
   105
fun make_cmd solver args problem_path proof_path = space_implode " " (
boehmes@36898
   106
  map File.shell_quote (solver @ args) @
boehmes@36898
   107
  [File.shell_path problem_path, "2>&1", ">", File.shell_path proof_path])
boehmes@36898
   108
boehmes@36898
   109
fun run ctxt cmd args input =
boehmes@40424
   110
  (case C.certificates_of ctxt of
boehmes@40578
   111
    NONE =>
boehmes@40578
   112
      if Config.get ctxt C.debug_files = "" then
boehmes@40578
   113
        Cache_IO.run (make_cmd (choose cmd) args) input
boehmes@40578
   114
      else
boehmes@40578
   115
        let
boehmes@40578
   116
          val base_path = Path.explode (Config.get ctxt C.debug_files)
boehmes@40578
   117
          val in_path = Path.ext "smt_in" base_path
boehmes@40578
   118
          val out_path = Path.ext "smt_out" base_path
boehmes@40578
   119
        in
boehmes@40578
   120
          Cache_IO.raw_run (make_cmd (choose cmd) args) input in_path out_path 
boehmes@40578
   121
        end
boehmes@36898
   122
  | SOME certs =>
boehmes@36898
   123
      (case Cache_IO.lookup certs input of
boehmes@36898
   124
        (NONE, key) =>
boehmes@40538
   125
          if Config.get ctxt C.fixed then
boehmes@40538
   126
            error ("Bad certificates cache: missing certificate")
boehmes@40538
   127
          else
boehmes@40538
   128
            Cache_IO.run_and_cache certs key (make_cmd (choose cmd) args) input
boehmes@36898
   129
      | (SOME output, _) =>
boehmes@36898
   130
         (tracing ("Using cached certificate from " ^
boehmes@36898
   131
            File.shell_path (Cache_IO.cache_path_of certs) ^ " ...");
boehmes@40550
   132
          output)))
boehmes@36898
   133
boehmes@36898
   134
in
boehmes@36898
   135
boehmes@36898
   136
fun run_solver ctxt cmd args input =
boehmes@36898
   137
  let
boehmes@36898
   138
    fun pretty tag ls = Pretty.string_of (Pretty.big_list tag
boehmes@36898
   139
      (map Pretty.str ls))
boehmes@36898
   140
boehmes@40424
   141
    val _ = C.trace_msg ctxt (pretty "Problem:" o split_lines) input
boehmes@36898
   142
boehmes@40550
   143
    val {redirected_output=res, output=err, return_code} =
boehmes@40550
   144
      C.with_timeout ctxt (run ctxt cmd args) input
boehmes@40424
   145
    val _ = C.trace_msg ctxt (pretty "Solver:") err
boehmes@36898
   146
haftmann@39811
   147
    val ls = rev (snd (chop_while (equal "") (rev res)))
boehmes@40424
   148
    val _ = C.trace_msg ctxt (pretty "Result:") ls
boehmes@40550
   149
boehmes@40550
   150
    val _ = null ls andalso return_code <> 0 andalso
boehmes@40561
   151
      raise SMT_Failure.SMT (SMT_Failure.Abnormal_Termination return_code)
boehmes@36898
   152
  in ls end
boehmes@36898
   153
boehmes@36898
   154
end
boehmes@36898
   155
boehmes@40424
   156
fun trace_assms ctxt = C.trace_msg ctxt (Pretty.string_of o
boehmes@40424
   157
  Pretty.big_list "Assertions:" o map (Display.pretty_thm ctxt o snd))
boehmes@40198
   158
blanchet@36940
   159
fun trace_recon_data ctxt ({typs, terms, ...} : SMT_Translate.recon) =
boehmes@36898
   160
  let
boehmes@36898
   161
    fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
boehmes@40424
   162
    fun p_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
boehmes@40424
   163
    fun p_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
boehmes@36898
   164
  in
boehmes@40424
   165
    C.trace_msg ctxt (fn () =>
boehmes@40424
   166
      Pretty.string_of (Pretty.big_list "Names:" [
boehmes@40424
   167
        Pretty.big_list "sorts:" (map p_typ (Symtab.dest typs)),
boehmes@40424
   168
        Pretty.big_list "functions:" (map p_term (Symtab.dest terms))])) ()
boehmes@36898
   169
  end
boehmes@36898
   170
boehmes@40424
   171
fun invoke translate_config name cmd options irules ctxt =
boehmes@40162
   172
  let
boehmes@40424
   173
    val args = C.solver_options_of ctxt @ options ctxt
boehmes@40162
   174
    val comments = ("solver: " ^ name) ::
boehmes@40424
   175
      ("timeout: " ^ Time.toString (seconds (Config.get ctxt C.timeout))) ::
boehmes@40162
   176
      "arguments:" :: args
boehmes@40162
   177
  in
boehmes@40162
   178
    irules
boehmes@40198
   179
    |> tap (trace_assms ctxt)
boehmes@40162
   180
    |> SMT_Translate.translate translate_config ctxt comments
boehmes@40162
   181
    ||> tap (trace_recon_data ctxt)
boehmes@40162
   182
    |>> run_solver ctxt cmd args
boehmes@40162
   183
    |> rpair ctxt
boehmes@40162
   184
  end
boehmes@36898
   185
boehmes@36898
   186
fun discharge_definitions thm =
boehmes@36898
   187
  if Thm.nprems_of thm = 0 then thm
boehmes@36898
   188
  else discharge_definitions (@{thm reflexive} RS thm)
boehmes@36898
   189
boehmes@40162
   190
fun set_has_datatypes with_datatypes translate =
boehmes@36898
   191
  let
boehmes@40162
   192
    val {prefixes, header, strict, builtins, serialize} = translate
boehmes@40162
   193
    val {builtin_typ, builtin_num, builtin_fun, has_datatypes} = builtins
boehmes@40162
   194
    val with_datatypes' = has_datatypes andalso with_datatypes
boehmes@40162
   195
    val builtins' = {builtin_typ=builtin_typ, builtin_num=builtin_num,
boehmes@40162
   196
      builtin_fun=builtin_fun, has_datatypes=with_datatypes}
boehmes@40162
   197
    val translate' = {prefixes=prefixes, header=header, strict=strict,
boehmes@40162
   198
      builtins=builtins', serialize=serialize}
boehmes@40162
   199
  in (with_datatypes', translate') end
boehmes@40162
   200
boehmes@40164
   201
fun trace_assumptions ctxt irules idxs =
boehmes@40164
   202
  let
boehmes@40164
   203
    val thms = filter (fn i => i >= 0) idxs
boehmes@40164
   204
      |> map_filter (AList.lookup (op =) irules)
boehmes@40164
   205
  in
boehmes@40424
   206
    if Config.get ctxt C.trace_used_facts andalso length thms > 0
boehmes@40164
   207
    then
boehmes@40164
   208
      tracing (Pretty.string_of (Pretty.big_list "SMT used facts:"
boehmes@40164
   209
        (map (Display.pretty_thm ctxt) thms)))
boehmes@40164
   210
    else ()
boehmes@40164
   211
  end
boehmes@40164
   212
boehmes@40196
   213
fun gen_solver name info rm ctxt irules =
boehmes@40162
   214
  let
boehmes@40424
   215
    val {env_var, is_remote, options, interface, reconstruct} = info
boehmes@36898
   216
    val {extra_norm, translate} = interface
boehmes@40162
   217
    val (with_datatypes, translate') =
boehmes@40424
   218
      set_has_datatypes (Config.get ctxt C.datatypes) translate
boehmes@40196
   219
    val cmd = (rm, env_var, is_remote, name)
boehmes@36898
   220
  in
boehmes@40162
   221
    (irules, ctxt)
boehmes@40424
   222
    |-> SMT_Normalize.normalize extra_norm with_datatypes
boehmes@40424
   223
    |-> invoke translate' name cmd options
boehmes@36898
   224
    |-> reconstruct
boehmes@40161
   225
    |-> (fn (idxs, thm) => fn ctxt' => thm
boehmes@36898
   226
    |> singleton (ProofContext.export ctxt' ctxt)
boehmes@40161
   227
    |> discharge_definitions
boehmes@40164
   228
    |> tap (fn _ => trace_assumptions ctxt irules idxs)
boehmes@40161
   229
    |> pair idxs)
boehmes@36898
   230
  end
boehmes@36898
   231
boehmes@36898
   232
boehmes@36898
   233
boehmes@40424
   234
(* registry *)
boehmes@36898
   235
boehmes@40196
   236
type solver = bool option -> Proof.context -> (int * thm) list -> int list * thm
boehmes@40162
   237
boehmes@40162
   238
type solver_info = {
boehmes@40162
   239
  env_var: string,
boehmes@40162
   240
  is_remote: bool,
boehmes@40162
   241
  options: Proof.context -> string list,
boehmes@40162
   242
  interface: interface,
boehmes@40162
   243
  reconstruct: string list * SMT_Translate.recon -> Proof.context ->
boehmes@40162
   244
    (int list * thm) * Proof.context }
boehmes@36898
   245
boehmes@36899
   246
structure Solvers = Generic_Data
boehmes@36898
   247
(
boehmes@40162
   248
  type T = solver_info Symtab.table
boehmes@36898
   249
  val empty = Symtab.empty
boehmes@36898
   250
  val extend = I
boehmes@36898
   251
  fun merge data = Symtab.merge (K true) data
boehmes@36898
   252
    handle Symtab.DUP name => error ("Duplicate SMT solver: " ^ quote name)
boehmes@36898
   253
)
boehmes@36898
   254
boehmes@40162
   255
local
boehmes@40162
   256
  fun finish outcome cex_parser reconstruct ocl (output, recon) ctxt =
boehmes@40162
   257
    (case outcome output of
boehmes@40162
   258
      (Unsat, ls) =>
boehmes@40424
   259
        if not (Config.get ctxt C.oracle) andalso is_some reconstruct
boehmes@40162
   260
        then the reconstruct ctxt recon ls
boehmes@40162
   261
        else (([], ocl ()), ctxt)
boehmes@40162
   262
    | (result, ls) =>
boehmes@40162
   263
        let val ts = (case cex_parser of SOME f => f ctxt recon ls | _ => [])
boehmes@40424
   264
        in
boehmes@40424
   265
          raise SMT_Failure.SMT (SMT_Failure.Counterexample (result = Sat, ts))
boehmes@40424
   266
        end)
boehmes@40579
   267
boehmes@40579
   268
  val cfalse = Thm.cterm_of @{theory} (@{const Trueprop} $ @{const False})
boehmes@40162
   269
in
boehmes@40162
   270
boehmes@40162
   271
fun add_solver cfg =
boehmes@40162
   272
  let
boehmes@40162
   273
    val {name, env_var, is_remote, options, interface, outcome, cex_parser,
boehmes@40162
   274
      reconstruct} = cfg
boehmes@40162
   275
boehmes@40579
   276
    fun core_oracle () = cfalse
boehmes@40162
   277
boehmes@40162
   278
    fun solver ocl = { env_var=env_var, is_remote=is_remote, options=options,
boehmes@40424
   279
      interface=interface,
boehmes@40162
   280
      reconstruct=finish (outcome name) cex_parser reconstruct ocl }
boehmes@40162
   281
  in
boehmes@40162
   282
    Thm.add_oracle (Binding.name name, core_oracle) #-> (fn (_, ocl) =>
boehmes@40162
   283
    Context.theory_map (Solvers.map (Symtab.update_new (name, solver ocl)))) #>
boehmes@40424
   284
    Context.theory_map (C.add_solver name)
boehmes@40162
   285
  end
boehmes@40162
   286
boehmes@40162
   287
end
boehmes@40162
   288
boehmes@40424
   289
fun name_and_solver_of ctxt =
boehmes@40424
   290
  let val name = C.solver_of ctxt
boehmes@40424
   291
  in (name, the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)) end
boehmes@36898
   292
boehmes@40424
   293
fun solver_of ctxt =
boehmes@40424
   294
  let val (name, raw_solver) = name_and_solver_of ctxt
boehmes@40424
   295
  in gen_solver name raw_solver end
boehmes@36898
   296
boehmes@40166
   297
fun is_locally_installed ctxt =
boehmes@40424
   298
  let val (_, {env_var, ...}) = name_and_solver_of ctxt
boehmes@40166
   299
  in is_some (get_local_solver env_var) end
boehmes@40166
   300
boehmes@36898
   301
boehmes@36898
   302
boehmes@40424
   303
(* filter *)
boehmes@40161
   304
boehmes@40161
   305
val has_topsort = Term.exists_type (Term.exists_subtype (fn
boehmes@40161
   306
    TFree (_, []) => true
boehmes@40161
   307
  | TVar (_, []) => true
boehmes@40161
   308
  | _ => false))
boehmes@40161
   309
boehmes@40196
   310
fun smt_solver rm ctxt irules =
boehmes@40161
   311
  (* without this test, we would run into problems when atomizing the rules: *)
boehmes@40424
   312
  if exists (has_topsort o Thm.prop_of o snd) irules then
boehmes@40424
   313
    raise SMT_Failure.SMT (SMT_Failure.Other_Failure ("proof state " ^
boehmes@40424
   314
      "contains the universal sort {}"))
boehmes@40424
   315
  else solver_of ctxt rm ctxt irules
boehmes@40162
   316
boehmes@40579
   317
val cnot = Thm.cterm_of @{theory} @{const Not}
boehmes@40579
   318
boehmes@40196
   319
fun smt_filter run_remote time_limit st xrules i =
boehmes@40162
   320
  let
boehmes@40199
   321
    val {facts, goal, ...} = Proof.goal st
boehmes@40164
   322
    val ctxt =
boehmes@40199
   323
      Proof.context_of st
boehmes@40424
   324
      |> Config.put C.oracle false
boehmes@40560
   325
      |> Config.put C.timeout (Time.toReal time_limit)
boehmes@40424
   326
      |> Config.put C.drop_bad_facts true
boehmes@40424
   327
      |> Config.put C.filter_only_facts true
boehmes@40357
   328
    val ({context=ctxt', prems, concl, ...}, _) = Subgoal.focus ctxt i goal
boehmes@40579
   329
    fun negate ct = Thm.dest_comb ct ||> Thm.capply cnot |-> Thm.capply
boehmes@40579
   330
    val cprop = negate (Thm.rhs_of (SMT_Normalize.atomize_conv ctxt' concl))
boehmes@40357
   331
    val irs = map (pair ~1) (Thm.assume cprop :: prems @ facts)
boehmes@40196
   332
    val rm = SOME run_remote
boehmes@40162
   333
  in
blanchet@40666
   334
    (xrules, map snd xrules)
boehmes@40357
   335
    ||> distinct (op =) o fst o smt_solver rm ctxt' o append irs o map_index I
boehmes@40164
   336
    |-> map_filter o try o nth
boehmes@40197
   337
    |> (fn xs => {outcome=NONE, used_facts=xs, run_time_in_msecs=NONE})
boehmes@40162
   338
  end
boehmes@40424
   339
  handle SMT_Failure.SMT fail => {outcome=SOME fail, used_facts=[],
boehmes@40424
   340
    run_time_in_msecs=NONE}
boehmes@40166
   341
  (* FIXME: measure runtime *)
boehmes@40161
   342
boehmes@40161
   343
boehmes@40161
   344
boehmes@36898
   345
(* SMT tactic *)
boehmes@36898
   346
boehmes@36898
   347
fun smt_tac' pass_exns ctxt rules =
boehmes@36899
   348
  CONVERSION (SMT_Normalize.atomize_conv ctxt)
boehmes@36899
   349
  THEN' Tactic.rtac @{thm ccontr}
boehmes@40424
   350
  THEN' SUBPROOF (fn {context=ctxt', prems, ...} =>
boehmes@40165
   351
    let
boehmes@40424
   352
      fun solve irules = snd (smt_solver NONE ctxt' irules)
boehmes@40424
   353
      val tag = "Solver " ^ C.solver_of ctxt' ^ ": "
boehmes@40424
   354
      val str_of = SMT_Failure.string_of_failure ctxt'
boehmes@40424
   355
      fun safe_solve irules =
boehmes@40424
   356
        if pass_exns then SOME (solve irules)
boehmes@40515
   357
        else (SOME (solve irules)
boehmes@40515
   358
          handle
boehmes@40515
   359
            SMT_Failure.SMT (fail as SMT_Failure.Counterexample _) =>
boehmes@40515
   360
              (C.verbose_msg ctxt' (prefix tag o str_of) fail; NONE)
boehmes@40515
   361
          | SMT_Failure.SMT fail =>
boehmes@40515
   362
              (C.trace_msg ctxt' (prefix tag o str_of) fail; NONE))
boehmes@40165
   363
    in
boehmes@40424
   364
      safe_solve (map (pair ~1) (rules @ prems))
boehmes@40165
   365
      |> (fn SOME thm => Tactic.rtac thm 1 | _ => Tactical.no_tac)
boehmes@40166
   366
    end) ctxt
boehmes@36898
   367
boehmes@36898
   368
val smt_tac = smt_tac' false
boehmes@40162
   369
boehmes@36898
   370
val smt_method =
boehmes@36898
   371
  Scan.optional Attrib.thms [] >>
boehmes@36898
   372
  (fn thms => fn ctxt => METHOD (fn facts =>
boehmes@36898
   373
    HEADGOAL (smt_tac ctxt (thms @ facts))))
boehmes@36898
   374
boehmes@36898
   375
boehmes@36898
   376
boehmes@36898
   377
(* setup *)
boehmes@36898
   378
boehmes@36898
   379
val setup =
wenzelm@38808
   380
  Method.setup @{binding smt} smt_method
boehmes@36898
   381
    "Applies an SMT solver to the current goal."
boehmes@36898
   382
boehmes@36898
   383
end