src/HOL/SMT.thy
author boehmes
Mon Nov 08 12:13:44 2010 +0100 (2010-11-08)
changeset 40424 7550b2cba1cb
parent 40277 4e3a3461c1a6
child 40662 798aad2229c0
permissions -rw-r--r--
better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
     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   "Tools/Datatype/datatype_selectors.ML"
    11   "Tools/SMT/smt_failure.ML"
    12   "Tools/SMT/smt_config.ML"
    13   "Tools/SMT/smt_monomorph.ML"
    14   ("Tools/SMT/smt_builtin.ML")
    15   ("Tools/SMT/smt_normalize.ML")
    16   ("Tools/SMT/smt_translate.ML")
    17   ("Tools/SMT/smt_solver.ML")
    18   ("Tools/SMT/smtlib_interface.ML")
    19   ("Tools/SMT/z3_proof_parser.ML")
    20   ("Tools/SMT/z3_proof_tools.ML")
    21   ("Tools/SMT/z3_proof_literals.ML")
    22   ("Tools/SMT/z3_proof_reconstruction.ML")
    23   ("Tools/SMT/z3_model.ML")
    24   ("Tools/SMT/z3_interface.ML")
    25   ("Tools/SMT/smt_setup_solvers.ML")
    26 begin
    27 
    28 
    29 
    30 subsection {* Triggers for quantifier instantiation *}
    31 
    32 text {*
    33 Some SMT solvers support triggers for quantifier instantiation.
    34 Each trigger consists of one ore more patterns.  A pattern may either
    35 be a list of positive subterms (each being tagged by "pat"), or a
    36 list of negative subterms (each being tagged by "nopat").
    37 
    38 When an SMT solver finds a term matching a positive pattern (a
    39 pattern with positive subterms only), it instantiates the
    40 corresponding quantifier accordingly.  Negative patterns inhibit
    41 quantifier instantiations.  Each pattern should mention all preceding
    42 bound variables.
    43 *}
    44 
    45 datatype pattern = Pattern
    46 
    47 definition pat :: "'a \<Rightarrow> pattern" where "pat _ = Pattern"
    48 definition nopat :: "'a \<Rightarrow> pattern" where "nopat _ = Pattern"
    49 
    50 definition trigger :: "pattern list list \<Rightarrow> bool \<Rightarrow> bool"
    51 where "trigger _ P = P"
    52 
    53 
    54 
    55 subsection {* Distinctness *}
    56 
    57 text {*
    58 As an abbreviation for a quadratic number of inequalities, SMT solvers
    59 provide a built-in @{text distinct}.  To avoid confusion with the
    60 already defined (and more general) @{term List.distinct}, a separate
    61 constant is defined.
    62 *}
    63 
    64 definition distinct :: "'a list \<Rightarrow> bool"
    65 where "distinct xs = List.distinct xs"
    66 
    67 
    68 
    69 subsection {* Higher-order encoding *}
    70 
    71 text {*
    72 Application is made explicit for constants occurring with varying
    73 numbers of arguments.  This is achieved by the introduction of the
    74 following constant.
    75 *}
    76 
    77 definition fun_app where "fun_app f x = f x"
    78 
    79 text {*
    80 Some solvers support a theory of arrays which can be used to encode
    81 higher-order functions.  The following set of lemmas specifies the
    82 properties of such (extensional) arrays.
    83 *}
    84 
    85 lemmas array_rules = ext fun_upd_apply fun_upd_same fun_upd_other
    86   fun_upd_upd fun_app_def
    87 
    88 
    89 
    90 subsection {* First-order logic *}
    91 
    92 text {*
    93 Some SMT solvers require a strict separation between formulas and
    94 terms.  When translating higher-order into first-order problems,
    95 all uninterpreted constants (those not builtin in the target solver)
    96 are treated as function symbols in the first-order sense.  Their
    97 occurrences as head symbols in atoms (i.e., as predicate symbols) is
    98 turned into terms by equating such atoms with @{term True} using the
    99 following term-level equation symbol.
   100 *}
   101 
   102 definition term_eq :: "bool \<Rightarrow> bool \<Rightarrow> bool" where "term_eq x y = (x = y)"
   103 
   104 
   105 
   106 subsection {* Integer division and modulo for Z3 *}
   107 
   108 definition z3div :: "int \<Rightarrow> int \<Rightarrow> int" where
   109   "z3div k l = (if 0 \<le> l then k div l else -(k div (-l)))"
   110 
   111 definition z3mod :: "int \<Rightarrow> int \<Rightarrow> int" where
   112   "z3mod k l = (if 0 \<le> l then k mod l else k mod (-l))"
   113 
   114 lemma div_by_z3div: "k div l = (
   115      if k = 0 \<or> l = 0 then 0
   116      else if (0 < k \<and> 0 < l) \<or> (k < 0 \<and> 0 < l) then z3div k l
   117      else z3div (-k) (-l))"
   118   by (auto simp add: z3div_def)
   119 
   120 lemma mod_by_z3mod: "k mod l = (
   121      if l = 0 then k
   122      else if k = 0 then 0
   123      else if (0 < k \<and> 0 < l) \<or> (k < 0 \<and> 0 < l) then z3mod k l
   124      else - z3mod (-k) (-l))"
   125   by (auto simp add: z3mod_def)
   126 
   127 
   128 
   129 subsection {* Setup *}
   130 
   131 use "Tools/SMT/smt_builtin.ML"
   132 use "Tools/SMT/smt_normalize.ML"
   133 use "Tools/SMT/smt_translate.ML"
   134 use "Tools/SMT/smt_solver.ML"
   135 use "Tools/SMT/smtlib_interface.ML"
   136 use "Tools/SMT/z3_interface.ML"
   137 use "Tools/SMT/z3_proof_parser.ML"
   138 use "Tools/SMT/z3_proof_tools.ML"
   139 use "Tools/SMT/z3_proof_literals.ML"
   140 use "Tools/SMT/z3_proof_reconstruction.ML"
   141 use "Tools/SMT/z3_model.ML"
   142 use "Tools/SMT/smt_setup_solvers.ML"
   143 
   144 setup {*
   145   SMT_Config.setup #>
   146   SMT_Solver.setup #>
   147   Z3_Proof_Reconstruction.setup #>
   148   SMT_Setup_Solvers.setup
   149 *}
   150 
   151 
   152 
   153 subsection {* Configuration *}
   154 
   155 text {*
   156 The current configuration can be printed by the command
   157 @{text smt_status}, which shows the values of most options.
   158 *}
   159 
   160 
   161 
   162 subsection {* General configuration options *}
   163 
   164 text {*
   165 The option @{text smt_solver} can be used to change the target SMT
   166 solver.  The possible values are @{text cvc3}, @{text yices}, and
   167 @{text z3}.  It is advisable to locally install the selected solver,
   168 although this is not necessary for @{text cvc3} and @{text z3}, which
   169 can also be used over an Internet-based service.
   170 
   171 When using local SMT solvers, the path to their binaries should be
   172 declared by setting the following environment variables:
   173 @{text CVC3_SOLVER}, @{text YICES_SOLVER}, and @{text Z3_SOLVER}.
   174 *}
   175 
   176 declare [[ smt_solver = z3 ]]
   177 
   178 text {*
   179 Since SMT solvers are potentially non-terminating, there is a timeout
   180 (given in seconds) to restrict their runtime.  A value greater than
   181 120 (seconds) is in most cases not advisable.
   182 *}
   183 
   184 declare [[ smt_timeout = 20 ]]
   185 
   186 text {*
   187 In general, the binding to SMT solvers runs as an oracle, i.e, the SMT
   188 solvers are fully trusted without additional checks.  The following
   189 option can cause the SMT solver to run in proof-producing mode, giving
   190 a checkable certificate.  This is currently only implemented for Z3.
   191 *}
   192 
   193 declare [[ smt_oracle = false ]]
   194 
   195 text {*
   196 Each SMT solver provides several commandline options to tweak its
   197 behaviour.  They can be passed to the solver by setting the following
   198 options.
   199 *}
   200 
   201 declare [[ cvc3_options = "", yices_options = "", z3_options = "" ]]
   202 
   203 text {*
   204 Enable the following option to use built-in support for datatypes and
   205 records.  Currently, this is only implemented for Z3 running in oracle
   206 mode.
   207 *}
   208 
   209 declare [[ smt_datatypes = false ]]
   210 
   211 
   212 
   213 subsection {* Certificates *}
   214 
   215 text {*
   216 By setting the option @{text smt_certificates} to the name of a file,
   217 all following applications of an SMT solver a cached in that file.
   218 Any further application of the same SMT solver (using the very same
   219 configuration) re-uses the cached certificate instead of invoking the
   220 solver.  An empty string disables caching certificates.
   221 
   222 The filename should be given as an explicit path.  It is good
   223 practice to use the name of the current theory (with ending
   224 @{text ".certs"} instead of @{text ".thy"}) as the certificates file.
   225 *}
   226 
   227 declare [[ smt_certificates = "" ]]
   228 
   229 text {*
   230 The option @{text smt_fixed} controls whether only stored
   231 certificates are should be used or invocation of an SMT solver is
   232 allowed.  When set to @{text true}, no SMT solver will ever be
   233 invoked and only the existing certificates found in the configured
   234 cache are used;  when set to @{text false} and there is no cached
   235 certificate for some proposition, then the configured SMT solver is
   236 invoked.
   237 *}
   238 
   239 declare [[ smt_fixed = false ]]
   240 
   241 
   242 
   243 subsection {* Tracing *}
   244 
   245 text {*
   246 The SMT method, when applied, traces important information.  To
   247 make it entirely silent, set the following option to @{text false}.
   248 *}
   249 
   250 declare [[ smt_verbose = true ]]
   251 
   252 text {*
   253 For tracing the generated problem file given to the SMT solver as
   254 well as the returned result of the solver, the option
   255 @{text smt_trace} should be set to @{text true}.
   256 *}
   257 
   258 declare [[ smt_trace = false ]]
   259 
   260 text {*
   261 From the set of assumptions given to the SMT solver, those assumptions
   262 used in the proof are traced when the following option is set to
   263 @{term true}.  This only works for Z3 when it runs in non-oracle mode
   264 (see options @{text smt_solver} and @{text smt_oracle} above).
   265 *}
   266 
   267 declare [[ smt_trace_used_facts = false ]]
   268 
   269 
   270 
   271 subsection {* Schematic rules for Z3 proof reconstruction *}
   272 
   273 text {*
   274 Several prof rules of Z3 are not very well documented.  There are two
   275 lemma groups which can turn failing Z3 proof reconstruction attempts
   276 into succeeding ones: the facts in @{text z3_rule} are tried prior to
   277 any implemented reconstruction procedure for all uncertain Z3 proof
   278 rules;  the facts in @{text z3_simp} are only fed to invocations of
   279 the simplifier when reconstructing theory-specific proof steps.
   280 *}
   281 
   282 lemmas [z3_rule] =
   283   refl eq_commute conj_commute disj_commute simp_thms nnf_simps
   284   ring_distribs field_simps times_divide_eq_right times_divide_eq_left
   285   if_True if_False not_not
   286 
   287 lemma [z3_rule]:
   288   "(P \<longrightarrow> Q) = (Q \<or> \<not>P)"
   289   "(\<not>P \<longrightarrow> Q) = (P \<or> Q)"
   290   "(\<not>P \<longrightarrow> Q) = (Q \<or> P)"
   291   by auto
   292 
   293 lemma [z3_rule]:
   294   "((P = Q) \<longrightarrow> R) = (R | (Q = (\<not>P)))"
   295   by auto
   296 
   297 lemma [z3_rule]:
   298   "((\<not>P) = P) = False"
   299   "(P = (\<not>P)) = False"
   300   "(P \<noteq> Q) = (Q = (\<not>P))"
   301   "(P = Q) = ((\<not>P \<or> Q) \<and> (P \<or> \<not>Q))"
   302   "(P \<noteq> Q) = ((\<not>P \<or> \<not>Q) \<and> (P \<or> Q))"
   303   by auto
   304 
   305 lemma [z3_rule]:
   306   "(if P then P else \<not>P) = True"
   307   "(if \<not>P then \<not>P else P) = True"
   308   "(if P then True else False) = P"
   309   "(if P then False else True) = (\<not>P)"
   310   "(if \<not>P then x else y) = (if P then y else x)"
   311   by auto
   312 
   313 lemma [z3_rule]:
   314   "P = Q \<or> P \<or> Q"
   315   "P = Q \<or> \<not>P \<or> \<not>Q"
   316   "(\<not>P) = Q \<or> \<not>P \<or> Q"
   317   "(\<not>P) = Q \<or> P \<or> \<not>Q"
   318   "P = (\<not>Q) \<or> \<not>P \<or> Q"
   319   "P = (\<not>Q) \<or> P \<or> \<not>Q"
   320   "P \<noteq> Q \<or> P \<or> \<not>Q"
   321   "P \<noteq> Q \<or> \<not>P \<or> Q"
   322   "P \<noteq> (\<not>Q) \<or> P \<or> Q"
   323   "(\<not>P) \<noteq> Q \<or> P \<or> Q"
   324   "P \<or> Q \<or> P \<noteq> (\<not>Q)"
   325   "P \<or> Q \<or> (\<not>P) \<noteq> Q"
   326   "P \<or> \<not>Q \<or> P \<noteq> Q"
   327   "\<not>P \<or> Q \<or> P \<noteq> Q"
   328   by auto
   329 
   330 lemma [z3_rule]:
   331   "0 + (x::int) = x"
   332   "x + 0 = x"
   333   "0 * x = 0"
   334   "1 * x = x"
   335   "x + y = y + x"
   336   by auto
   337 
   338 
   339 
   340 hide_type (open) pattern
   341 hide_const Pattern term_eq
   342 hide_const (open) trigger pat nopat distinct fun_app z3div z3mod
   343 
   344 
   345 
   346 subsection {* Selectors for datatypes *}
   347 
   348 setup {* Datatype_Selectors.setup *}
   349 
   350 declare [[ selector Pair 1 = fst, selector Pair 2 = snd ]]
   351 declare [[ selector Cons 1 = hd, selector Cons 2 = tl ]]
   352 
   353 end