src/HOL/Tools/SMT/smt_solver.ML
author boehmes
Wed, 12 May 2010 23:54:02 +0200
changeset 36898 8e55aa1306c5
child 36899 bcd6fce5bf06
permissions -rw-r--r--
integrated SMT into the HOL image
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/Tools/SMT/smt_solver.ML
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     3
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     4
SMT solvers registry and SMT tactic.
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     5
*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     6
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     7
signature SMT_SOLVER =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     8
sig
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     9
  exception SMT of string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    10
  exception SMT_COUNTEREXAMPLE of bool * term list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    11
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    12
  type interface = {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    13
    extra_norm: SMT_Normalize.extra_norm,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    14
    translate: SMT_Translate.config }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    15
  type solver_config = {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    16
    command: {env_var: string, remote_name: string option},
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    17
    arguments: string list,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    18
    interface: interface,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    19
    reconstruct: (string list * SMT_Translate.recon) -> Proof.context ->
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    20
      thm * Proof.context }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    21
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    22
  (*options*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    23
  val timeout: int Config.T
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    24
  val with_timeout: Proof.context -> ('a -> 'b) -> 'a -> 'b
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    25
  val trace: bool Config.T
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    26
  val trace_msg: Proof.context -> ('a -> string) -> 'a -> unit
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    27
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    28
  (*certificates*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    29
  val fixed_certificates: bool Config.T
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    30
  val select_certificates: string -> Context.generic -> Context.generic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    31
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    32
  (*solvers*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    33
  type solver = Proof.context -> thm list -> thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    34
  type solver_info = Context.generic -> Pretty.T list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    35
  val add_solver: string * (Proof.context -> solver_config) -> theory ->
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    36
    theory
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    37
  val all_solver_names_of: theory -> string list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    38
  val add_solver_info: string * solver_info -> theory -> theory
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    39
  val solver_name_of: Context.generic -> string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    40
  val select_solver: string -> Context.generic -> Context.generic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    41
  val solver_of: Context.generic -> solver
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    42
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    43
  (*tactic*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    44
  val smt_tac': bool -> Proof.context -> thm list -> int -> Tactical.tactic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    45
  val smt_tac: Proof.context -> thm list -> int -> Tactical.tactic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    46
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    47
  (*setup*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    48
  val setup: theory -> theory
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    49
  val print_setup: Context.generic -> unit
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    50
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    51
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    52
structure SMT_Solver: SMT_SOLVER =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    53
struct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    54
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    55
exception SMT of string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    56
exception SMT_COUNTEREXAMPLE of bool * term list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    57
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    58
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    59
type interface = {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    60
  extra_norm: SMT_Normalize.extra_norm,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    61
  translate: SMT_Translate.config }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    62
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    63
type solver_config = {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    64
  command: {env_var: string, remote_name: string option},
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    65
  arguments: string list,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    66
  interface: interface,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    67
  reconstruct: (string list * SMT_Translate.recon) -> Proof.context ->
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    68
    thm * Proof.context }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    69
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    70
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    71
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    72
(* SMT options *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    73
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    74
val (timeout, setup_timeout) = Attrib.config_int "smt_timeout" (K 30)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    75
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    76
fun with_timeout ctxt f x =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    77
  TimeLimit.timeLimit (Time.fromSeconds (Config.get ctxt timeout)) f x
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    78
  handle TimeLimit.TimeOut => raise SMT "timeout"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    79
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    80
val (trace, setup_trace) = Attrib.config_bool "smt_trace" (K false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    81
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    82
fun trace_msg ctxt f x =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    83
  if Config.get ctxt trace then tracing (f x) else ()
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    84
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    85
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    86
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    87
(* SMT certificates *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    88
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    89
val (fixed_certificates, setup_fixed_certificates) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    90
  Attrib.config_bool "smt_fixed" (K false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    91
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    92
structure Certificates = Generic_Data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    93
(
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    94
  type T = Cache_IO.cache option
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    95
  val empty = NONE
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    96
  val extend = I
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    97
  fun merge (s, _) = s
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    98
)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    99
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   100
val get_certificates_path =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   101
  Option.map (Cache_IO.cache_path_of) o Certificates.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   102
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   103
fun select_certificates name = Certificates.put (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   104
  if name = "" then NONE
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   105
  else SOME (Cache_IO.make (Path.explode name)))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   106
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   107
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   108
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   109
(* interface to external solvers *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   110
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   111
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   112
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   113
fun choose {env_var, remote_name} =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   114
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   115
    val local_solver = getenv env_var
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   116
    val remote_solver = the_default "" remote_name
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   117
    val remote_url = getenv "REMOTE_SMT_URL"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   118
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   119
    if local_solver <> ""
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   120
    then 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   121
     (tracing ("Invoking local SMT solver " ^ quote local_solver ^ " ...");
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   122
      [local_solver])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   123
    else if remote_solver <> ""
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   124
    then
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   125
     (tracing ("Invoking remote SMT solver " ^ quote remote_solver ^ " at " ^
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   126
        quote remote_url ^ " ...");
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   127
      [getenv "REMOTE_SMT", remote_solver])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   128
    else error ("Undefined Isabelle environment variable: " ^ quote env_var)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   129
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   130
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   131
fun make_cmd solver args problem_path proof_path = space_implode " " (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   132
  map File.shell_quote (solver @ args) @
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   133
  [File.shell_path problem_path, "2>&1", ">", File.shell_path proof_path])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   134
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   135
fun run ctxt cmd args input =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   136
  (case Certificates.get (Context.Proof ctxt) of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   137
    NONE => Cache_IO.run (make_cmd (choose cmd) args) input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   138
  | SOME certs =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   139
      (case Cache_IO.lookup certs input of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   140
        (NONE, key) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   141
          if Config.get ctxt fixed_certificates
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   142
          then error ("Bad certificates cache: missing certificate")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   143
          else Cache_IO.run_and_cache certs key (make_cmd (choose cmd) args)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   144
            input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   145
      | (SOME output, _) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   146
         (tracing ("Using cached certificate from " ^
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   147
            File.shell_path (Cache_IO.cache_path_of certs) ^ " ...");
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   148
          output)))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   149
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   150
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   151
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   152
fun run_solver ctxt cmd args input =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   153
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   154
    fun pretty tag ls = Pretty.string_of (Pretty.big_list tag
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   155
      (map Pretty.str ls))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   156
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   157
    val _ = trace_msg ctxt (pretty "SMT problem:" o split_lines) input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   158
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   159
    val (res, err) = with_timeout ctxt (run ctxt cmd args) input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   160
    val _ = trace_msg ctxt (pretty "SMT solver:") err
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   161
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   162
    val ls = rev (dropwhile (equal "") (rev res))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   163
    val _ = trace_msg ctxt (pretty "SMT result:") ls
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   164
  in ls end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   165
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   166
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   167
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   168
fun trace_recon_data ctxt {typs, terms, ...} =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   169
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   170
    fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   171
    fun pretty_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   172
    fun pretty_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   173
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   174
    trace_msg ctxt (fn () => Pretty.string_of (Pretty.big_list "SMT names:" [
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   175
      Pretty.big_list "sorts:" (map pretty_typ (Symtab.dest typs)),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   176
      Pretty.big_list "functions:" (map pretty_term (Symtab.dest terms))])) ()
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   177
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   178
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   179
fun invoke translate_config comments command arguments thms ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   180
  thms
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   181
  |> SMT_Translate.translate translate_config ctxt comments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   182
  ||> tap (trace_recon_data ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   183
  |>> run_solver ctxt command arguments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   184
  |> rpair ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   185
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   186
fun discharge_definitions thm =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   187
  if Thm.nprems_of thm = 0 then thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   188
  else discharge_definitions (@{thm reflexive} RS thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   189
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   190
fun gen_solver name solver ctxt prems =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   191
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   192
    val {command, arguments, interface, reconstruct} = solver ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   193
    val comments = ("solver: " ^ name) ::
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   194
      ("timeout: " ^ string_of_int (Config.get ctxt timeout)) ::
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   195
      "arguments:" :: arguments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   196
    val {extra_norm, translate} = interface
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   197
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   198
    (prems, ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   199
    |-> SMT_Normalize.normalize extra_norm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   200
    |-> invoke translate comments command arguments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   201
    |-> reconstruct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   202
    |-> (fn thm => fn ctxt' => thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   203
    |> singleton (ProofContext.export ctxt' ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   204
    |> discharge_definitions)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   205
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   206
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   207
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   208
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   209
(* solver store *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   210
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   211
type solver = Proof.context -> thm list -> thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   212
type solver_info = Context.generic -> Pretty.T list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   213
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   214
structure Solvers = Theory_Data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   215
(
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   216
  type T = ((Proof.context -> solver_config) * solver_info) Symtab.table
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   217
  val empty = Symtab.empty
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   218
  val extend = I
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   219
  fun merge data = Symtab.merge (K true) data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   220
    handle Symtab.DUP name => error ("Duplicate SMT solver: " ^ quote name)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   221
)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   222
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   223
val no_solver = "(none)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   224
val add_solver = Solvers.map o Symtab.update_new o apsnd (rpair (K []))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   225
val all_solver_names_of = Symtab.keys o Solvers.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   226
val lookup_solver = Symtab.lookup o Solvers.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   227
fun add_solver_info (n, i) = Solvers.map (Symtab.map_entry n (apsnd (K i)))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   228
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   229
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   230
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   231
(* selected solver *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   232
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   233
structure Selected_Solver = Generic_Data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   234
(
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   235
  type T = string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   236
  val empty = no_solver
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   237
  val extend = I
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   238
  fun merge (s, _) = s
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   239
)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   240
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   241
val solver_name_of = Selected_Solver.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   242
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   243
fun select_solver name context =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   244
  if is_none (lookup_solver (Context.theory_of context) name)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   245
  then error ("SMT solver not registered: " ^ quote name)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   246
  else Selected_Solver.map (K name) context
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   247
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   248
fun raw_solver_of context name =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   249
  (case lookup_solver (Context.theory_of context) name of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   250
    NONE => error "No SMT solver selected"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   251
  | SOME (s, _) => s)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   252
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   253
fun solver_of context =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   254
  let val name = solver_name_of context
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   255
  in gen_solver name (raw_solver_of context name) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   256
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   257
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   258
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   259
(* SMT tactic *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   260
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   261
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   262
  fun pretty_cex ctxt (real, ex) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   263
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   264
      val msg = if real then "SMT: counterexample found"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   265
        else "SMT: potential counterexample found"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   266
    in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   267
      if null ex then msg ^ "."
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   268
      else Pretty.string_of (Pretty.big_list (msg ^ ":")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   269
        (map (Syntax.pretty_term ctxt) ex))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   270
    end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   271
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   272
  fun fail_tac f msg st = (f msg; Tactical.no_tac st)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   273
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   274
  fun SAFE pass_exns tac ctxt i st =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   275
    if pass_exns then tac ctxt i st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   276
    else (tac ctxt i st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   277
      handle SMT msg => fail_tac (trace_msg ctxt (prefix "SMT: ")) msg st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   278
           | SMT_COUNTEREXAMPLE ce => fail_tac tracing (pretty_cex ctxt ce) st)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   279
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   280
  fun smt_solver rules ctxt = solver_of (Context.Proof ctxt) ctxt rules
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   281
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   282
  val has_topsort = Term.exists_type (Term.exists_subtype (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   283
      TFree (_, []) => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   284
    | TVar (_, []) => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   285
    | _ => false))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   286
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   287
fun smt_tac' pass_exns ctxt rules =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   288
  Tactic.rtac @{thm ccontr} THEN'
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   289
  SUBPROOF (fn {context, prems, ...} =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   290
    let val thms = rules @ prems
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   291
    in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   292
      if exists (has_topsort o Thm.prop_of) thms
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   293
      then fail_tac (trace_msg context I)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   294
        "SMT: proof state contains the universal sort {}"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   295
      else SAFE pass_exns (Tactic.rtac o smt_solver thms) context 1
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   296
    end) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   297
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   298
val smt_tac = smt_tac' false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   299
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   300
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   301
val smt_method =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   302
  Scan.optional Attrib.thms [] >>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   303
  (fn thms => fn ctxt => METHOD (fn facts =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   304
    HEADGOAL (smt_tac ctxt (thms @ facts))))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   305
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   306
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   307
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   308
(* setup *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   309
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   310
val setup =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   311
  Attrib.setup (Binding.name "smt_solver")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   312
    (Scan.lift (OuterParse.$$$ "=" |-- Args.name) >>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   313
      (Thm.declaration_attribute o K o select_solver))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   314
    "SMT solver configuration" #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   315
  setup_timeout #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   316
  setup_trace #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   317
  setup_fixed_certificates #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   318
  Attrib.setup (Binding.name "smt_certificates")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   319
    (Scan.lift (OuterParse.$$$ "=" |-- Args.name) >>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   320
      (Thm.declaration_attribute o K o select_certificates))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   321
    "SMT certificates" #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   322
  Method.setup (Binding.name "smt") smt_method
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   323
    "Applies an SMT solver to the current goal."
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   324
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   325
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   326
fun print_setup gen =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   327
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   328
    val t = string_of_int (Config.get_generic gen timeout)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   329
    val names = sort_strings (all_solver_names_of (Context.theory_of gen))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   330
    val ns = if null names then [no_solver] else names
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   331
    val take_info = (fn (_, []) => NONE | info => SOME info)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   332
    val infos =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   333
      Context.theory_of gen
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   334
      |> Symtab.dest o Solvers.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   335
      |> map_filter (fn (n, (_, info)) => take_info (n, info gen))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   336
      |> sort (prod_ord string_ord (K EQUAL))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   337
      |> map (fn (n, ps) => Pretty.big_list (n ^ ":") ps)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   338
    val certs_filename =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   339
      (case get_certificates_path gen of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   340
        SOME path => Path.implode path
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   341
      | NONE => "(disabled)")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   342
    val fixed = if Config.get_generic gen fixed_certificates then "true"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   343
      else "false"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   344
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   345
    Pretty.writeln (Pretty.big_list "SMT setup:" [
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   346
      Pretty.str ("Current SMT solver: " ^ solver_name_of gen),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   347
      Pretty.str_list "Available SMT solvers: "  "" ns,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   348
      Pretty.str ("Current timeout: " ^ t ^ " seconds"),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   349
      Pretty.str ("Certificates cache: " ^ certs_filename),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   350
      Pretty.str ("Fixed certificates: " ^ fixed),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   351
      Pretty.big_list "Solver-specific settings:" infos])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   352
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   353
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   354
val _ = OuterSyntax.improper_command "smt_status"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   355
  "Show the available SMT solvers and the currently selected solver."
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   356
  OuterKeyword.diag
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   357
    (Scan.succeed (Toplevel.no_timing o Toplevel.keep (fn state =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   358
      print_setup (Context.Proof (Toplevel.context_of state)))))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   359
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   360
end