# Theory SMT

Up to index of Isabelle/HOL-Proofs

theory SMT
imports Record
(*  Title:      HOL/SMT.thy    Author:     Sascha Boehme, TU Muenchen*)header {* Bindings to Satisfiability Modulo Theories (SMT) solvers *}theory SMTimports Recordkeywords "smt_status" :: diagbeginML_file "Tools/SMT/smt_utils.ML"ML_file "Tools/SMT/smt_failure.ML"ML_file "Tools/SMT/smt_config.ML"subsection {* Triggers for quantifier instantiation *}text {*Some SMT solvers support patterns as a quantifier instantiationheuristics.  Patterns may either be positive terms (tagged by "pat")triggering quantifier instantiations -- when the solver finds aterm matching a positive pattern, it instantiates the correspondingquantifier accordingly -- or negative terms (tagged by "nopat")inhibiting quantifier instantiations.  A list of patternsof the same kind is called a multipattern, and all patterns in amultipattern are considered conjunctively for quantifier instantiation.A list of multipatterns is called a trigger, and their multipatternsact disjunctively during quantifier instantiation.  Each multipatternshould mention at least all quantified variables of the precedingquantifier block.*}datatype pattern = Patterndefinition pat :: "'a => pattern" where "pat _ = Pattern"definition nopat :: "'a => pattern" where "nopat _ = Pattern"definition trigger :: "pattern list list => bool => bool"where "trigger _ P = P"subsection {* Quantifier weights *}text {*Weight annotations to quantifiers influence the priority of quantifierinstantiations.  They should be handled with care for solvers, which supportthem, because incorrect choices of weights might render a problem unsolvable.*}definition weight :: "int => bool => bool" where "weight _ P = P"text {*Weights must be non-negative.  The value @{text 0} is equivalent to providingno weight at all.Weights should only be used at quantifiers and only inside triggers (if thequantifier has triggers).  Valid usages of weights are as follows:\begin{itemize}\item@{term "∀x. trigger [[pat (P x)]] (weight 2 (P x))"}\item@{term "∀x. weight 3 (P x)"}\end{itemize}*}subsection {* Higher-order encoding *}text {*Application is made explicit for constants occurring with varyingnumbers of arguments.  This is achieved by the introduction of thefollowing constant.*}definition fun_app where "fun_app f = f"text {*Some solvers support a theory of arrays which can be used to encodehigher-order functions.  The following set of lemmas specifies theproperties of such (extensional) arrays.*}lemmas array_rules = ext fun_upd_apply fun_upd_same fun_upd_other  fun_upd_upd fun_app_defsubsection {* First-order logic *}text {*Some SMT solvers only accept problems in first-order logic, i.e.,where formulas and terms are syntactically separated. Whentranslating higher-order into first-order problems, alluninterpreted constants (those not built-in in the target solver)are treated as function symbols in the first-order sense.  Theiroccurrences as head symbols in atoms (i.e., as predicate symbols) areturned into terms by logically equating such atoms with @{term True}.For technical reasons, @{term True} and @{term False} occurring insideterms are replaced by the following constants.*}definition term_true where "term_true = True"definition term_false where "term_false = False"subsection {* Integer division and modulo for Z3 *}definition z3div :: "int => int => int" where  "z3div k l = (if 0 ≤ l then k div l else -(k div (-l)))"definition z3mod :: "int => int => int" where  "z3mod k l = (if 0 ≤ l then k mod l else k mod (-l))"subsection {* Setup *}ML_file "Tools/SMT/smt_builtin.ML"ML_file "Tools/SMT/smt_datatypes.ML"ML_file "Tools/SMT/smt_normalize.ML"ML_file "Tools/SMT/smt_translate.ML"ML_file "Tools/SMT/smt_solver.ML"ML_file "Tools/SMT/smtlib_interface.ML"ML_file "Tools/SMT/z3_interface.ML"ML_file "Tools/SMT/z3_proof_parser.ML"ML_file "Tools/SMT/z3_proof_tools.ML"ML_file "Tools/SMT/z3_proof_literals.ML"ML_file "Tools/SMT/z3_proof_methods.ML"ML_file "Tools/SMT/z3_proof_reconstruction.ML"ML_file "Tools/SMT/z3_model.ML"ML_file "Tools/SMT/smt_setup_solvers.ML"setup {*  SMT_Config.setup #>  SMT_Normalize.setup #>  SMTLIB_Interface.setup #>  Z3_Interface.setup #>  Z3_Proof_Reconstruction.setup #>  SMT_Setup_Solvers.setup*}method_setup smt = {*  Scan.optional Attrib.thms [] >>    (fn thms => fn ctxt =>      METHOD (fn facts => HEADGOAL (SMT_Solver.smt_tac ctxt (thms @ facts))))*} "apply an SMT solver to the current goal"subsection {* Configuration *}text {*The current configuration can be printed by the command@{text smt_status}, which shows the values of most options.*}subsection {* General configuration options *}text {*The option @{text smt_solver} can be used to change the target SMTsolver.  The possible values can be obtained from the @{text smt_status}command.Due to licensing restrictions, Yices and Z3 are not installed/enabledby default.  Z3 is free for non-commercial applications and can be enabledby simply setting the environment variable @{text Z3_NON_COMMERCIAL} to@{text yes}.*}declare [[ smt_solver = z3 ]]text {*Since SMT solvers are potentially non-terminating, there is a timeout(given in seconds) to restrict their runtime.  A value greater than120 (seconds) is in most cases not advisable.*}declare [[ smt_timeout = 20 ]]text {*SMT solvers apply randomized heuristics.  In case a problem is notsolvable by an SMT solver, changing the following option might help.*}declare [[ smt_random_seed = 1 ]]text {*In general, the binding to SMT solvers runs as an oracle, i.e, the SMTsolvers are fully trusted without additional checks.  The followingoption can cause the SMT solver to run in proof-producing mode, givinga checkable certificate.  This is currently only implemented for Z3.*}declare [[ smt_oracle = false ]]text {*Each SMT solver provides several commandline options to tweak itsbehaviour.  They can be passed to the solver by setting the followingoptions.*}declare [[ cvc3_options = "", remote_cvc3_options = "" ]]declare [[ yices_options = "" ]]declare [[ z3_options = "", remote_z3_options = "" ]]text {*Enable the following option to use built-in support for datatypes andrecords.  Currently, this is only implemented for Z3 running in oraclemode.*}declare [[ smt_datatypes = false ]]text {*The SMT method provides an inference mechanism to detect simple triggersin quantified formulas, which might increase the number of problemssolvable by SMT solvers (note: triggers guide quantifier instantiationsin the SMT solver).  To turn it on, set the following option.*}declare [[ smt_infer_triggers = false ]]text {*The SMT method monomorphizes the given facts, that is, it tries toinstantiate all schematic type variables with fixed types occurringin the problem.  This is a (possibly nonterminating) fixed-pointconstruction whose cycles are limited by the following option.*}declare [[ monomorph_max_rounds = 5 ]]text {*In addition, the number of generated monomorphic instances is limitedby the following option.*}declare [[ monomorph_max_new_instances = 500 ]]subsection {* Certificates *}text {*By setting the option @{text smt_certificates} to the name of a file,all following applications of an SMT solver a cached in that file.Any further application of the same SMT solver (using the very sameconfiguration) re-uses the cached certificate instead of invoking thesolver.  An empty string disables caching certificates.The filename should be given as an explicit path.  It is goodpractice to use the name of the current theory (with ending@{text ".certs"} instead of @{text ".thy"}) as the certificates file.Certificate files should be used at most once in a certain theory context,to avoid race conditions with other concurrent accesses.*}declare [[ smt_certificates = "" ]]text {*The option @{text smt_read_only_certificates} controls whether onlystored certificates are should be used or invocation of an SMT solveris allowed.  When set to @{text true}, no SMT solver will ever beinvoked and only the existing certificates found in the configuredcache are used;  when set to @{text false} and there is no cachedcertificate for some proposition, then the configured SMT solver isinvoked.*}declare [[ smt_read_only_certificates = false ]]subsection {* Tracing *}text {*The SMT method, when applied, traces important information.  Tomake it entirely silent, set the following option to @{text false}.*}declare [[ smt_verbose = true ]]text {*For tracing the generated problem file given to the SMT solver aswell as the returned result of the solver, the option@{text smt_trace} should be set to @{text true}.*}declare [[ smt_trace = false ]]text {*From the set of assumptions given to the SMT solver, those assumptionsused in the proof are traced when the following option is set to@{term true}.  This only works for Z3 when it runs in non-oracle mode(see options @{text smt_solver} and @{text smt_oracle} above).*}declare [[ smt_trace_used_facts = false ]]subsection {* Schematic rules for Z3 proof reconstruction *}text {*Several prof rules of Z3 are not very well documented.  There are twolemma groups which can turn failing Z3 proof reconstruction attemptsinto succeeding ones: the facts in @{text z3_rule} are tried prior toany implemented reconstruction procedure for all uncertain Z3 proofrules;  the facts in @{text z3_simp} are only fed to invocations ofthe simplifier when reconstructing theory-specific proof steps.*}lemmas [z3_rule] =  refl eq_commute conj_commute disj_commute simp_thms nnf_simps  ring_distribs field_simps times_divide_eq_right times_divide_eq_left  if_True if_False not_notlemma [z3_rule]:  "(P ∧ Q) = (¬(¬P ∨ ¬Q))"  "(P ∧ Q) = (¬(¬Q ∨ ¬P))"  "(¬P ∧ Q) = (¬(P ∨ ¬Q))"  "(¬P ∧ Q) = (¬(¬Q ∨ P))"  "(P ∧ ¬Q) = (¬(¬P ∨ Q))"  "(P ∧ ¬Q) = (¬(Q ∨ ¬P))"  "(¬P ∧ ¬Q) = (¬(P ∨ Q))"  "(¬P ∧ ¬Q) = (¬(Q ∨ P))"  by autolemma [z3_rule]:  "(P --> Q) = (Q ∨ ¬P)"  "(¬P --> Q) = (P ∨ Q)"  "(¬P --> Q) = (Q ∨ P)"  "(True --> P) = P"  "(P --> True) = True"  "(False --> P) = True"  "(P --> P) = True"  by autolemma [z3_rule]:  "((P = Q) --> R) = (R | (Q = (¬P)))"  by autolemma [z3_rule]:  "(¬True) = False"  "(¬False) = True"  "(x = x) = True"  "(P = True) = P"  "(True = P) = P"  "(P = False) = (¬P)"  "(False = P) = (¬P)"  "((¬P) = P) = False"  "(P = (¬P)) = False"  "((¬P) = (¬Q)) = (P = Q)"  "¬(P = (¬Q)) = (P = Q)"  "¬((¬P) = Q) = (P = Q)"  "(P ≠ Q) = (Q = (¬P))"  "(P = Q) = ((¬P ∨ Q) ∧ (P ∨ ¬Q))"  "(P ≠ Q) = ((¬P ∨ ¬Q) ∧ (P ∨ Q))"  by autolemma [z3_rule]:  "(if P then P else ¬P) = True"  "(if ¬P then ¬P else P) = True"  "(if P then True else False) = P"  "(if P then False else True) = (¬P)"  "(if P then Q else True) = ((¬P) ∨ Q)"  "(if P then Q else True) = (Q ∨ (¬P))"  "(if P then Q else ¬Q) = (P = Q)"  "(if P then Q else ¬Q) = (Q = P)"  "(if P then ¬Q else Q) = (P = (¬Q))"  "(if P then ¬Q else Q) = ((¬Q) = P)"  "(if ¬P then x else y) = (if P then y else x)"  "(if P then (if Q then x else y) else x) = (if P ∧ (¬Q) then y else x)"  "(if P then (if Q then x else y) else x) = (if (¬Q) ∧ P then y else x)"  "(if P then (if Q then x else y) else y) = (if P ∧ Q then x else y)"  "(if P then (if Q then x else y) else y) = (if Q ∧ P then x else y)"  "(if P then x else if P then y else z) = (if P then x else z)"  "(if P then x else if Q then x else y) = (if P ∨ Q then x else y)"  "(if P then x else if Q then x else y) = (if Q ∨ P then x else y)"  "(if P then x = y else x = z) = (x = (if P then y else z))"  "(if P then x = y else y = z) = (y = (if P then x else z))"  "(if P then x = y else z = y) = (y = (if P then x else z))"  by autolemma [z3_rule]:  "0 + (x::int) = x"  "x + 0 = x"  "x + x = 2 * x"  "0 * x = 0"  "1 * x = x"  "x + y = y + x"  by autolemma [z3_rule]:  (* for def-axiom *)  "P = Q ∨ P ∨ Q"  "P = Q ∨ ¬P ∨ ¬Q"  "(¬P) = Q ∨ ¬P ∨ Q"  "(¬P) = Q ∨ P ∨ ¬Q"  "P = (¬Q) ∨ ¬P ∨ Q"  "P = (¬Q) ∨ P ∨ ¬Q"  "P ≠ Q ∨ P ∨ ¬Q"  "P ≠ Q ∨ ¬P ∨ Q"  "P ≠ (¬Q) ∨ P ∨ Q"  "(¬P) ≠ Q ∨ P ∨ Q"  "P ∨ Q ∨ P ≠ (¬Q)"  "P ∨ Q ∨ (¬P) ≠ Q"  "P ∨ ¬Q ∨ P ≠ Q"  "¬P ∨ Q ∨ P ≠ Q"  "P ∨ y = (if P then x else y)"  "P ∨ (if P then x else y) = y"  "¬P ∨ x = (if P then x else y)"  "¬P ∨  (if P then x else y) = x"  "P ∨ R ∨ ¬(if P then Q else R)"  "¬P ∨ Q ∨ ¬(if P then Q else R)"  "¬(if P then Q else R) ∨ ¬P ∨ Q"  "¬(if P then Q else R) ∨ P ∨ R"  "(if P then Q else R) ∨ ¬P ∨ ¬Q"  "(if P then Q else R) ∨ P ∨ ¬R"  "(if P then ¬Q else R) ∨ ¬P ∨ Q"  "(if P then Q else ¬R) ∨ P ∨ R"  by autohide_type (open) patternhide_const Pattern fun_app term_true term_false z3div z3modhide_const (open) trigger pat nopat weightend