src/HOL/Tools/SMT/smt_solver.ML
author boehmes
Tue, 26 Oct 2010 11:39:26 +0200
changeset 40161 539d07b00e5f
parent 39811 0659e84bdc5f
child 40162 7f58a9a843c2
permissions -rw-r--r--
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
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 ->
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    20
      (int list * thm) * Proof.context }
36898
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*)
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    33
  type solver = Proof.context -> (int * thm) list -> int list * thm
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    34
  type solver_info = Context.generic -> Pretty.T list
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    35
  val add_solver: string * (Proof.context -> solver_config) ->
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    36
    Context.generic -> Context.generic
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    37
  val all_solver_names_of: Context.generic -> string list
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    38
  val add_solver_info: string * solver_info -> Context.generic ->
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    39
    Context.generic
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    40
  val solver_name_of: Context.generic -> string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    41
  val select_solver: string -> Context.generic -> Context.generic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    42
  val solver_of: Context.generic -> solver
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    43
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    44
  (*solver*)
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    45
  val smt_solver: Proof.context -> ('a * thm) list -> 'a list * thm
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    46
  val smt_filter: Proof.context -> ('a * thm) list -> 'a list * string
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    47
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    48
  (*tactic*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    49
  val smt_tac': bool -> Proof.context -> thm list -> int -> Tactical.tactic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    50
  val smt_tac: Proof.context -> thm list -> int -> Tactical.tactic
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    51
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    52
  (*setup*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    53
  val setup: theory -> theory
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    54
  val print_setup: Context.generic -> unit
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    55
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    56
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    57
structure SMT_Solver: SMT_SOLVER =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    58
struct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    59
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    60
exception SMT of string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    61
exception SMT_COUNTEREXAMPLE of bool * term list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    62
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    63
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    64
type interface = {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    65
  extra_norm: SMT_Normalize.extra_norm,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    66
  translate: SMT_Translate.config }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    67
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    68
type solver_config = {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    69
  command: {env_var: string, remote_name: string option},
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    70
  arguments: string list,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    71
  interface: interface,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    72
  reconstruct: (string list * SMT_Translate.recon) -> Proof.context ->
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
    73
    (int list * thm) * Proof.context }
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    74
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    75
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    76
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    77
(* SMT options *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    78
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    79
val (timeout, setup_timeout) = Attrib.config_int "smt_timeout" (K 30)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    80
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    81
fun with_timeout ctxt f x =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    82
  TimeLimit.timeLimit (Time.fromSeconds (Config.get ctxt timeout)) f x
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    83
  handle TimeLimit.TimeOut => raise SMT "timeout"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    84
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    85
val (trace, setup_trace) = Attrib.config_bool "smt_trace" (K false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    86
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    87
fun trace_msg ctxt f x =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    88
  if Config.get ctxt trace then tracing (f x) else ()
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    89
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    90
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    91
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    92
(* SMT certificates *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    93
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    94
val (fixed_certificates, setup_fixed_certificates) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    95
  Attrib.config_bool "smt_fixed" (K false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    96
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    97
structure Certificates = Generic_Data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    98
(
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    99
  type T = Cache_IO.cache option
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   100
  val empty = NONE
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   101
  val extend = I
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   102
  fun merge (s, _) = s
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   103
)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   104
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   105
val get_certificates_path =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   106
  Option.map (Cache_IO.cache_path_of) o Certificates.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   107
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   108
fun select_certificates name = Certificates.put (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   109
  if name = "" then NONE
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   110
  else SOME (Cache_IO.make (Path.explode name)))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   111
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   112
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   113
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   114
(* interface to external solvers *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   115
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   116
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   117
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   118
fun choose {env_var, remote_name} =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   119
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   120
    val local_solver = getenv env_var
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   121
    val remote_solver = the_default "" remote_name
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   122
    val remote_url = getenv "REMOTE_SMT_URL"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   123
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   124
    if local_solver <> ""
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   125
    then 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   126
     (tracing ("Invoking local SMT solver " ^ quote local_solver ^ " ...");
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   127
      [local_solver])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   128
    else if remote_solver <> ""
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   129
    then
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   130
     (tracing ("Invoking remote SMT solver " ^ quote remote_solver ^ " at " ^
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   131
        quote remote_url ^ " ...");
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   132
      [getenv "REMOTE_SMT", remote_solver])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   133
    else error ("Undefined Isabelle environment variable: " ^ quote env_var)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   134
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   135
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   136
fun make_cmd solver args problem_path proof_path = space_implode " " (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   137
  map File.shell_quote (solver @ args) @
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   138
  [File.shell_path problem_path, "2>&1", ">", File.shell_path proof_path])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   139
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   140
fun run ctxt cmd args input =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   141
  (case Certificates.get (Context.Proof ctxt) of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   142
    NONE => Cache_IO.run (make_cmd (choose cmd) args) input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   143
  | SOME certs =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   144
      (case Cache_IO.lookup certs input of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   145
        (NONE, key) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   146
          if Config.get ctxt fixed_certificates
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   147
          then error ("Bad certificates cache: missing certificate")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   148
          else Cache_IO.run_and_cache certs key (make_cmd (choose cmd) args)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   149
            input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   150
      | (SOME output, _) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   151
         (tracing ("Using cached certificate from " ^
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   152
            File.shell_path (Cache_IO.cache_path_of certs) ^ " ...");
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   153
          output)))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   154
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   155
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   156
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   157
fun run_solver ctxt cmd args input =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   158
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   159
    fun pretty tag ls = Pretty.string_of (Pretty.big_list tag
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   160
      (map Pretty.str ls))
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 _ = trace_msg ctxt (pretty "SMT problem:" o split_lines) input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   163
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   164
    val (res, err) = with_timeout ctxt (run ctxt cmd args) input
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   165
    val _ = trace_msg ctxt (pretty "SMT solver:") err
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   166
39811
0659e84bdc5f chop_while replace drop_while and take_while
haftmann
parents: 39809
diff changeset
   167
    val ls = rev (snd (chop_while (equal "") (rev res)))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   168
    val _ = trace_msg ctxt (pretty "SMT result:") ls
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   169
  in ls end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   170
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   171
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   172
36940
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   173
fun trace_recon_data ctxt ({typs, terms, ...} : SMT_Translate.recon) =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   174
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   175
    fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   176
    fun pretty_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   177
    fun pretty_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   178
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   179
    trace_msg ctxt (fn () => Pretty.string_of (Pretty.big_list "SMT names:" [
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   180
      Pretty.big_list "sorts:" (map pretty_typ (Symtab.dest typs)),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   181
      Pretty.big_list "functions:" (map pretty_term (Symtab.dest terms))])) ()
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   182
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   183
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   184
fun invoke translate_config comments command arguments irules ctxt =
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   185
  irules
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   186
  |> SMT_Translate.translate translate_config ctxt comments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   187
  ||> tap (trace_recon_data ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   188
  |>> run_solver ctxt command arguments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   189
  |> rpair ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   190
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   191
fun discharge_definitions thm =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   192
  if Thm.nprems_of thm = 0 then thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   193
  else discharge_definitions (@{thm reflexive} RS thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   194
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   195
fun gen_solver name solver ctxt prems =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   196
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   197
    val {command, arguments, interface, reconstruct} = solver ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   198
    val comments = ("solver: " ^ name) ::
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   199
      ("timeout: " ^ string_of_int (Config.get ctxt timeout)) ::
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   200
      "arguments:" :: arguments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   201
    val {extra_norm, translate} = interface
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38808
diff changeset
   202
    val {builtins, ...} = translate
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38808
diff changeset
   203
    val {has_datatypes, ...} = builtins
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   204
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   205
    (prems, ctxt)
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38808
diff changeset
   206
    |-> SMT_Normalize.normalize extra_norm has_datatypes
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   207
    |-> invoke translate comments command arguments
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   208
    |-> reconstruct
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   209
    |-> (fn (idxs, thm) => fn ctxt' => thm
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   210
    |> singleton (ProofContext.export ctxt' ctxt)
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   211
    |> discharge_definitions
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   212
    |> pair idxs)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   213
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   214
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   215
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   216
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   217
(* solver store *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   218
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   219
type solver = Proof.context -> (int * thm) list -> int list * thm
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   220
type solver_info = Context.generic -> Pretty.T list
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   221
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   222
structure Solvers = Generic_Data
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   223
(
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   224
  type T = ((Proof.context -> solver_config) * solver_info) Symtab.table
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   225
  val empty = Symtab.empty
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   226
  val extend = I
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   227
  fun merge data = Symtab.merge (K true) data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   228
    handle Symtab.DUP name => error ("Duplicate SMT solver: " ^ quote name)
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
val no_solver = "(none)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   232
val add_solver = Solvers.map o Symtab.update_new o apsnd (rpair (K []))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   233
val all_solver_names_of = Symtab.keys o Solvers.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   234
val lookup_solver = Symtab.lookup o Solvers.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   235
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
   236
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   237
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   238
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   239
(* selected solver *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   240
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   241
structure Selected_Solver = Generic_Data
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   242
(
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   243
  type T = string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   244
  val empty = no_solver
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   245
  val extend = I
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   246
  fun merge (s, _) = s
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   247
)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   248
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   249
val solver_name_of = Selected_Solver.get
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   250
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   251
fun select_solver name context =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   252
  if is_none (lookup_solver context name)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   253
  then error ("SMT solver not registered: " ^ quote name)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   254
  else Selected_Solver.map (K name) context
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   255
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   256
fun raw_solver_of context name =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   257
  (case lookup_solver context name of
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   258
    NONE => error "No SMT solver selected"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   259
  | SOME (s, _) => s)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   260
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   261
fun solver_of context =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   262
  let val name = solver_name_of context
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   263
  in gen_solver name (raw_solver_of context name) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   264
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   265
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   266
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   267
(* SMT solver *)
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   268
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   269
val has_topsort = Term.exists_type (Term.exists_subtype (fn
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   270
    TFree (_, []) => true
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   271
  | TVar (_, []) => true
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   272
  | _ => false))
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   273
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   274
fun smt_solver ctxt xrules =
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   275
  (* without this test, we would run into problems when atomizing the rules: *)
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   276
  if exists (has_topsort o Thm.prop_of o snd) xrules
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   277
  then raise SMT "proof state contains the universal sort {}"
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   278
  else
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   279
    split_list xrules
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   280
    ||>> solver_of (Context.Proof ctxt) ctxt o map_index I
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   281
    |>> uncurry (map_filter o try o nth) o apsnd (distinct (op =))
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   282
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   283
fun smt_filter ctxt xrules =
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   284
  (fst (smt_solver ctxt xrules), "")
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   285
  handle SMT msg => ([], "SMT: " ^ msg)
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   286
       | SMT_COUNTEREXAMPLE _ => ([], "SMT: potential counterexample")
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   287
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   288
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   289
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   290
(* SMT tactic *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   291
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   292
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   293
  fun pretty_cex ctxt (real, ex) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   294
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   295
      val msg = if real then "SMT: counterexample found"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   296
        else "SMT: potential counterexample found"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   297
    in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   298
      if null ex then msg ^ "."
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   299
      else Pretty.string_of (Pretty.big_list (msg ^ ":")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   300
        (map (Syntax.pretty_term ctxt) ex))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   301
    end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   302
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   303
  fun fail_tac f msg st = (f msg; Tactical.no_tac st)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   304
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   305
  fun SAFE pass_exns tac ctxt i st =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   306
    if pass_exns then tac ctxt i st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   307
    else (tac ctxt i st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   308
      handle SMT msg => fail_tac (trace_msg ctxt (prefix "SMT: ")) msg st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   309
           | SMT_COUNTEREXAMPLE ce => fail_tac tracing (pretty_cex ctxt ce) st)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   310
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   311
fun smt_tac' pass_exns ctxt rules =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   312
  CONVERSION (SMT_Normalize.atomize_conv ctxt)
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   313
  THEN' Tactic.rtac @{thm ccontr}
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   314
  THEN' SUBPROOF (fn {context, prems, ...} =>
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   315
    let fun solve cx = snd (smt_solver cx (map (pair ()) (rules @ prems)))
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   316
    in SAFE pass_exns (Tactic.rtac o solve) context 1 end) ctxt
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   317
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   318
val smt_tac = smt_tac' false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   319
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   320
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   321
val smt_method =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   322
  Scan.optional Attrib.thms [] >>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   323
  (fn thms => fn ctxt => METHOD (fn facts =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   324
    HEADGOAL (smt_tac ctxt (thms @ facts))))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   325
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   326
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   327
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   328
(* setup *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   329
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   330
val setup =
38808
89ae86205739 more antiquotations;
wenzelm
parents: 36960
diff changeset
   331
  Attrib.setup @{binding smt_solver}
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36940
diff changeset
   332
    (Scan.lift (Parse.$$$ "=" |-- Args.name) >>
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   333
      (Thm.declaration_attribute o K o select_solver))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   334
    "SMT solver configuration" #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   335
  setup_timeout #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   336
  setup_trace #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   337
  setup_fixed_certificates #>
38808
89ae86205739 more antiquotations;
wenzelm
parents: 36960
diff changeset
   338
  Attrib.setup @{binding smt_certificates}
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36940
diff changeset
   339
    (Scan.lift (Parse.$$$ "=" |-- Args.name) >>
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   340
      (Thm.declaration_attribute o K o select_certificates))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   341
    "SMT certificates" #>
38808
89ae86205739 more antiquotations;
wenzelm
parents: 36960
diff changeset
   342
  Method.setup @{binding smt} smt_method
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   343
    "Applies an SMT solver to the current goal."
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   344
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   345
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   346
fun print_setup context =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   347
  let
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   348
    val t = string_of_int (Config.get_generic context timeout)
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   349
    val names = sort_strings (all_solver_names_of context)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   350
    val ns = if null names then [no_solver] else names
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   351
    val take_info = (fn (_, []) => NONE | info => SOME info)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   352
    val infos =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   353
      Solvers.get context
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   354
      |> Symtab.dest
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   355
      |> map_filter (fn (n, (_, info)) => take_info (n, info context))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   356
      |> sort (prod_ord string_ord (K EQUAL))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   357
      |> map (fn (n, ps) => Pretty.big_list (n ^ ":") ps)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   358
    val certs_filename =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   359
      (case get_certificates_path context of
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   360
        SOME path => Path.implode path
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   361
      | NONE => "(disabled)")
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   362
    val fixed = if Config.get_generic context fixed_certificates then "true"
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   363
      else "false"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   364
  in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   365
    Pretty.writeln (Pretty.big_list "SMT setup:" [
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   366
      Pretty.str ("Current SMT solver: " ^ solver_name_of context),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   367
      Pretty.str_list "Available SMT solvers: "  "" ns,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   368
      Pretty.str ("Current timeout: " ^ t ^ " seconds"),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   369
      Pretty.str ("Certificates cache: " ^ certs_filename),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   370
      Pretty.str ("Fixed certificates: " ^ fixed),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   371
      Pretty.big_list "Solver-specific settings:" infos])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   372
  end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   373
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36940
diff changeset
   374
val _ =
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36940
diff changeset
   375
  Outer_Syntax.improper_command "smt_status"
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   376
    "show the available SMT solvers and the currently selected solver"
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39811
diff changeset
   377
    Keyword.diag
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   378
    (Scan.succeed (Toplevel.no_timing o Toplevel.keep (fn state =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   379
      print_setup (Context.Proof (Toplevel.context_of state)))))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   380
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   381
end