src/HOL/SMT.thy
author huffman
Wed May 12 22:33:10 2010 -0700 (2010-05-12)
changeset 36902 c6bae4456741
parent 36899 bcd6fce5bf06
child 37124 fe22fc54b876
permissions -rw-r--r--
use 'subsection' instead of 'section', to maintain 1 chapter per file in generated document
     1 (*  Title:      HOL/SMT.thy
     2     Author:     Sascha Boehme, TU Muenchen
     3 *)
     4 
     5 header {* Bindings to Satisfiability Modulo Theories (SMT) solvers *}
     6 
     7 theory SMT
     8 imports List
     9 uses
    10   "~~/src/Tools/cache_io.ML"
    11   ("Tools/SMT/smt_monomorph.ML")
    12   ("Tools/SMT/smt_normalize.ML")
    13   ("Tools/SMT/smt_translate.ML")
    14   ("Tools/SMT/smt_solver.ML")
    15   ("Tools/SMT/smtlib_interface.ML")
    16   ("Tools/SMT/z3_proof_parser.ML")
    17   ("Tools/SMT/z3_proof_tools.ML")
    18   ("Tools/SMT/z3_proof_literals.ML")
    19   ("Tools/SMT/z3_proof_reconstruction.ML")
    20   ("Tools/SMT/z3_model.ML")
    21   ("Tools/SMT/z3_interface.ML")
    22   ("Tools/SMT/z3_solver.ML")
    23   ("Tools/SMT/cvc3_solver.ML")
    24   ("Tools/SMT/yices_solver.ML")
    25 begin
    26 
    27 
    28 
    29 subsection {* Triggers for quantifier instantiation *}
    30 
    31 text {*
    32 Some SMT solvers support triggers for quantifier instantiation.
    33 Each trigger consists of one ore more patterns.  A pattern may either
    34 be a list of positive subterms (the first being tagged by "pat" and
    35 the consecutive subterms tagged by "andpat"), or a list of negative
    36 subterms (the first being tagged by "nopat" and the consecutive
    37 subterms tagged by "andpat").
    38 *}
    39 
    40 datatype pattern = Pattern
    41 
    42 definition pat :: "'a \<Rightarrow> pattern"
    43 where "pat _ = Pattern"
    44 
    45 definition nopat :: "'a \<Rightarrow> pattern"
    46 where "nopat _ = Pattern"
    47 
    48 definition andpat :: "pattern \<Rightarrow> 'a \<Rightarrow> pattern" (infixl "andpat" 60)
    49 where "_ andpat _ = Pattern"
    50 
    51 definition trigger :: "pattern list \<Rightarrow> bool \<Rightarrow> bool"
    52 where "trigger _ P = P"
    53 
    54 
    55 
    56 subsection {* Higher-order encoding *}
    57 
    58 text {*
    59 Application is made explicit for constants occurring with varying
    60 numbers of arguments.  This is achieved by the introduction of the
    61 following constant.
    62 *}
    63 
    64 definition "apply" where "apply f x = f x"
    65 
    66 text {*
    67 Some solvers support a theory of arrays which can be used to encode
    68 higher-order functions.  The following set of lemmas specifies the
    69 properties of such (extensional) arrays.
    70 *}
    71 
    72 lemmas array_rules = ext fun_upd_apply fun_upd_same fun_upd_other
    73   fun_upd_upd
    74 
    75 
    76 
    77 subsection {* First-order logic *}
    78 
    79 text {*
    80 Some SMT solvers require a strict separation between formulas and
    81 terms.  When translating higher-order into first-order problems,
    82 all uninterpreted constants (those not builtin in the target solver)
    83 are treated as function symbols in the first-order sense.  Their
    84 occurrences as head symbols in atoms (i.e., as predicate symbols) is
    85 turned into terms by equating such atoms with @{term True} using the
    86 following term-level equation symbol.
    87 *}
    88 
    89 definition term_eq :: "bool \<Rightarrow> bool \<Rightarrow> bool" (infix "term'_eq" 50)
    90   where "(x term_eq y) = (x = y)"
    91 
    92 
    93 
    94 subsection {* Setup *}
    95 
    96 use "Tools/SMT/smt_monomorph.ML"
    97 use "Tools/SMT/smt_normalize.ML"
    98 use "Tools/SMT/smt_translate.ML"
    99 use "Tools/SMT/smt_solver.ML"
   100 use "Tools/SMT/smtlib_interface.ML"
   101 use "Tools/SMT/z3_interface.ML"
   102 use "Tools/SMT/z3_proof_parser.ML"
   103 use "Tools/SMT/z3_proof_tools.ML"
   104 use "Tools/SMT/z3_proof_literals.ML"
   105 use "Tools/SMT/z3_proof_reconstruction.ML"
   106 use "Tools/SMT/z3_model.ML"
   107 use "Tools/SMT/z3_solver.ML"
   108 use "Tools/SMT/cvc3_solver.ML"
   109 use "Tools/SMT/yices_solver.ML"
   110 
   111 setup {*
   112   SMT_Solver.setup #>
   113   Z3_Proof_Reconstruction.setup #>
   114   Z3_Solver.setup #>
   115   CVC3_Solver.setup #>
   116   Yices_Solver.setup
   117 *}
   118 
   119 
   120 
   121 subsection {* Configuration *}
   122 
   123 text {*
   124 The current configuration can be printed by the command
   125 @{text smt_status}, which shows the values of most options.
   126 *}
   127 
   128 
   129 
   130 subsection {* General configuration options *}
   131 
   132 text {*
   133 The option @{text smt_solver} can be used to change the target SMT
   134 solver.  The possible values are @{text cvc3}, @{text yices}, and
   135 @{text z3}.  It is advisable to locally install the selected solver,
   136 although this is not necessary for @{text cvc3} and @{text z3}, which
   137 can also be used over an Internet-based service.
   138 
   139 When using local SMT solvers, the path to their binaries should be
   140 declared by setting the following environment variables:
   141 @{text CVC3_SOLVER}, @{text YICES_SOLVER}, and @{text Z3_SOLVER}.
   142 *}
   143 
   144 declare [[ smt_solver = z3 ]]
   145 
   146 text {*
   147 Since SMT solvers are potentially non-terminating, there is a timeout
   148 (given in seconds) to restrict their runtime.  A value greater than
   149 120 (seconds) is in most cases not advisable.
   150 *}
   151 
   152 declare [[ smt_timeout = 20 ]]
   153 
   154 
   155 
   156 subsection {* Certificates *}
   157 
   158 text {*
   159 By setting the option @{text smt_certificates} to the name of a file,
   160 all following applications of an SMT solver a cached in that file.
   161 Any further application of the same SMT solver (using the very same
   162 configuration) re-uses the cached certificate instead of invoking the
   163 solver.  An empty string disables caching certificates.
   164 
   165 The filename should be given as an explicit path.  It is good
   166 practice to use the name of the current theory (with ending
   167 @{text ".certs"} instead of @{text ".thy"}) as the certificates file.
   168 *}
   169 
   170 declare [[ smt_certificates = "" ]]
   171 
   172 text {*
   173 The option @{text smt_fixed} controls whether only stored
   174 certificates are should be used or invocation of an SMT solver is
   175 allowed.  When set to @{text true}, no SMT solver will ever be
   176 invoked and only the existing certificates found in the configured
   177 cache are used;  when set to @{text false} and there is no cached
   178 certificate for some proposition, then the configured SMT solver is
   179 invoked.
   180 *}
   181 
   182 declare [[ smt_fixed = false ]]
   183 
   184 
   185 
   186 subsection {* Tracing *}
   187 
   188 text {*
   189 For tracing the generated problem file given to the SMT solver as
   190 well as the returned result of the solver, the option
   191 @{text smt_trace} should be set to @{text true}.
   192 *}
   193 
   194 declare [[ smt_trace = false ]]
   195 
   196 
   197 
   198 subsection {* Z3-specific options *}
   199 
   200 text {*
   201 Z3 is the only SMT solver whose proofs are checked (or reconstructed)
   202 in Isabelle (all other solvers are implemented as oracles).  Enabling
   203 or disabling proof reconstruction for Z3 is controlled by the option
   204 @{text z3_proofs}. 
   205 *}
   206 
   207 declare [[ z3_proofs = true ]]
   208 
   209 text {*
   210 From the set of assumptions given to Z3, those assumptions used in
   211 the proof are traced when the option @{text z3_trace_assms} is set to
   212 @{term true}.
   213 *}
   214 
   215 declare [[ z3_trace_assms = false ]]
   216 
   217 text {*
   218 Z3 provides several commandline options to tweak its behaviour.  They
   219 can be configured by writing them literally as value for the option
   220 @{text z3_options}.
   221 *}
   222 
   223 declare [[ z3_options = "" ]]
   224 
   225 
   226 
   227 subsection {* Schematic rules for Z3 proof reconstruction *}
   228 
   229 text {*
   230 Several prof rules of Z3 are not very well documented.  There are two
   231 lemma groups which can turn failing Z3 proof reconstruction attempts
   232 into succeeding ones: the facts in @{text z3_rule} are tried prior to
   233 any implemented reconstruction procedure for all uncertain Z3 proof
   234 rules;  the facts in @{text z3_simp} are only fed to invocations of
   235 the simplifier when reconstructing theory-specific proof steps.
   236 *}
   237 
   238 lemmas [z3_rule] =
   239   refl eq_commute conj_commute disj_commute simp_thms nnf_simps
   240   ring_distribs field_simps times_divide_eq_right times_divide_eq_left
   241   if_True if_False not_not
   242 
   243 lemma [z3_rule]:
   244   "(P \<longrightarrow> Q) = (Q \<or> \<not>P)"
   245   "(\<not>P \<longrightarrow> Q) = (P \<or> Q)"
   246   "(\<not>P \<longrightarrow> Q) = (Q \<or> P)"
   247   by auto
   248 
   249 lemma [z3_rule]:
   250   "((P = Q) \<longrightarrow> R) = (R | (Q = (\<not>P)))"
   251   by auto
   252 
   253 lemma [z3_rule]:
   254   "((\<not>P) = P) = False"
   255   "(P = (\<not>P)) = False"
   256   "(P \<noteq> Q) = (Q = (\<not>P))"
   257   "(P = Q) = ((\<not>P \<or> Q) \<and> (P \<or> \<not>Q))"
   258   "(P \<noteq> Q) = ((\<not>P \<or> \<not>Q) \<and> (P \<or> Q))"
   259   by auto
   260 
   261 lemma [z3_rule]:
   262   "(if P then P else \<not>P) = True"
   263   "(if \<not>P then \<not>P else P) = True"
   264   "(if P then True else False) = P"
   265   "(if P then False else True) = (\<not>P)"
   266   "(if \<not>P then x else y) = (if P then y else x)"
   267   by auto
   268 
   269 lemma [z3_rule]:
   270   "P = Q \<or> P \<or> Q"
   271   "P = Q \<or> \<not>P \<or> \<not>Q"
   272   "(\<not>P) = Q \<or> \<not>P \<or> Q"
   273   "(\<not>P) = Q \<or> P \<or> \<not>Q"
   274   "P = (\<not>Q) \<or> \<not>P \<or> Q"
   275   "P = (\<not>Q) \<or> P \<or> \<not>Q"
   276   "P \<noteq> Q \<or> P \<or> \<not>Q"
   277   "P \<noteq> Q \<or> \<not>P \<or> Q"
   278   "P \<noteq> (\<not>Q) \<or> P \<or> Q"
   279   "(\<not>P) \<noteq> Q \<or> P \<or> Q"
   280   "P \<or> Q \<or> P \<noteq> (\<not>Q)"
   281   "P \<or> Q \<or> (\<not>P) \<noteq> Q"
   282   "P \<or> \<not>Q \<or> P \<noteq> Q"
   283   "\<not>P \<or> Q \<or> P \<noteq> Q"
   284   by auto
   285 
   286 lemma [z3_rule]:
   287   "0 + (x::int) = x"
   288   "x + 0 = x"
   289   "0 * x = 0"
   290   "1 * x = x"
   291   "x + y = y + x"
   292   by auto
   293 
   294 end