src/HOL/SMT.thy
author wenzelm
Mon Apr 23 21:44:36 2012 +0200 (2012-04-23)
changeset 47701 157e6108a342
parent 47152 446cfc760ccf
child 48892 0b2407f406e8
permissions -rw-r--r--
more standard method setup;
     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 Record
     9 keywords "smt_status" :: diag
    10 uses
    11   "Tools/SMT/smt_utils.ML"
    12   "Tools/SMT/smt_failure.ML"
    13   "Tools/SMT/smt_config.ML"
    14   ("Tools/SMT/smt_builtin.ML")
    15   ("Tools/SMT/smt_datatypes.ML")
    16   ("Tools/SMT/smt_normalize.ML")
    17   ("Tools/SMT/smt_translate.ML")
    18   ("Tools/SMT/smt_solver.ML")
    19   ("Tools/SMT/smtlib_interface.ML")
    20   ("Tools/SMT/z3_interface.ML")
    21   ("Tools/SMT/z3_proof_parser.ML")
    22   ("Tools/SMT/z3_proof_tools.ML")
    23   ("Tools/SMT/z3_proof_literals.ML")
    24   ("Tools/SMT/z3_proof_methods.ML")
    25   ("Tools/SMT/z3_proof_reconstruction.ML")
    26   ("Tools/SMT/z3_model.ML")
    27   ("Tools/SMT/smt_setup_solvers.ML")
    28 begin
    29 
    30 
    31 
    32 subsection {* Triggers for quantifier instantiation *}
    33 
    34 text {*
    35 Some SMT solvers support patterns as a quantifier instantiation
    36 heuristics.  Patterns may either be positive terms (tagged by "pat")
    37 triggering quantifier instantiations -- when the solver finds a
    38 term matching a positive pattern, it instantiates the corresponding
    39 quantifier accordingly -- or negative terms (tagged by "nopat")
    40 inhibiting quantifier instantiations.  A list of patterns
    41 of the same kind is called a multipattern, and all patterns in a
    42 multipattern are considered conjunctively for quantifier instantiation.
    43 A list of multipatterns is called a trigger, and their multipatterns
    44 act disjunctively during quantifier instantiation.  Each multipattern
    45 should mention at least all quantified variables of the preceding
    46 quantifier block.
    47 *}
    48 
    49 datatype pattern = Pattern
    50 
    51 definition pat :: "'a \<Rightarrow> pattern" where "pat _ = Pattern"
    52 definition nopat :: "'a \<Rightarrow> pattern" where "nopat _ = Pattern"
    53 
    54 definition trigger :: "pattern list list \<Rightarrow> bool \<Rightarrow> bool"
    55 where "trigger _ P = P"
    56 
    57 
    58 
    59 subsection {* Quantifier weights *}
    60 
    61 text {*
    62 Weight annotations to quantifiers influence the priority of quantifier
    63 instantiations.  They should be handled with care for solvers, which support
    64 them, because incorrect choices of weights might render a problem unsolvable.
    65 *}
    66 
    67 definition weight :: "int \<Rightarrow> bool \<Rightarrow> bool" where "weight _ P = P"
    68 
    69 text {*
    70 Weights must be non-negative.  The value @{text 0} is equivalent to providing
    71 no weight at all.
    72 
    73 Weights should only be used at quantifiers and only inside triggers (if the
    74 quantifier has triggers).  Valid usages of weights are as follows:
    75 
    76 \begin{itemize}
    77 \item
    78 @{term "\<forall>x. trigger [[pat (P x)]] (weight 2 (P x))"}
    79 \item
    80 @{term "\<forall>x. weight 3 (P x)"}
    81 \end{itemize}
    82 *}
    83 
    84 
    85 
    86 subsection {* Higher-order encoding *}
    87 
    88 text {*
    89 Application is made explicit for constants occurring with varying
    90 numbers of arguments.  This is achieved by the introduction of the
    91 following constant.
    92 *}
    93 
    94 definition fun_app where "fun_app f = f"
    95 
    96 text {*
    97 Some solvers support a theory of arrays which can be used to encode
    98 higher-order functions.  The following set of lemmas specifies the
    99 properties of such (extensional) arrays.
   100 *}
   101 
   102 lemmas array_rules = ext fun_upd_apply fun_upd_same fun_upd_other
   103   fun_upd_upd fun_app_def
   104 
   105 
   106 
   107 subsection {* First-order logic *}
   108 
   109 text {*
   110 Some SMT solvers only accept problems in first-order logic, i.e.,
   111 where formulas and terms are syntactically separated. When
   112 translating higher-order into first-order problems, all
   113 uninterpreted constants (those not built-in in the target solver)
   114 are treated as function symbols in the first-order sense.  Their
   115 occurrences as head symbols in atoms (i.e., as predicate symbols) are
   116 turned into terms by logically equating such atoms with @{term True}.
   117 For technical reasons, @{term True} and @{term False} occurring inside
   118 terms are replaced by the following constants.
   119 *}
   120 
   121 definition term_true where "term_true = True"
   122 definition term_false where "term_false = False"
   123 
   124 
   125 
   126 subsection {* Integer division and modulo for Z3 *}
   127 
   128 definition z3div :: "int \<Rightarrow> int \<Rightarrow> int" where
   129   "z3div k l = (if 0 \<le> l then k div l else -(k div (-l)))"
   130 
   131 definition z3mod :: "int \<Rightarrow> int \<Rightarrow> int" where
   132   "z3mod k l = (if 0 \<le> l then k mod l else k mod (-l))"
   133 
   134 
   135 
   136 subsection {* Setup *}
   137 
   138 use "Tools/SMT/smt_builtin.ML"
   139 use "Tools/SMT/smt_datatypes.ML"
   140 use "Tools/SMT/smt_normalize.ML"
   141 use "Tools/SMT/smt_translate.ML"
   142 use "Tools/SMT/smt_solver.ML"
   143 use "Tools/SMT/smtlib_interface.ML"
   144 use "Tools/SMT/z3_interface.ML"
   145 use "Tools/SMT/z3_proof_parser.ML"
   146 use "Tools/SMT/z3_proof_tools.ML"
   147 use "Tools/SMT/z3_proof_literals.ML"
   148 use "Tools/SMT/z3_proof_methods.ML"
   149 use "Tools/SMT/z3_proof_reconstruction.ML"
   150 use "Tools/SMT/z3_model.ML"
   151 use "Tools/SMT/smt_setup_solvers.ML"
   152 
   153 setup {*
   154   SMT_Config.setup #>
   155   SMT_Normalize.setup #>
   156   SMTLIB_Interface.setup #>
   157   Z3_Interface.setup #>
   158   Z3_Proof_Reconstruction.setup #>
   159   SMT_Setup_Solvers.setup
   160 *}
   161 
   162 method_setup smt = {*
   163   Scan.optional Attrib.thms [] >>
   164     (fn thms => fn ctxt =>
   165       METHOD (fn facts => HEADGOAL (SMT_Solver.smt_tac ctxt (thms @ facts))))
   166 *} "apply an SMT solver to the current goal"
   167 
   168 
   169 subsection {* Configuration *}
   170 
   171 text {*
   172 The current configuration can be printed by the command
   173 @{text smt_status}, which shows the values of most options.
   174 *}
   175 
   176 
   177 
   178 subsection {* General configuration options *}
   179 
   180 text {*
   181 The option @{text smt_solver} can be used to change the target SMT
   182 solver.  The possible values can be obtained from the @{text smt_status}
   183 command.
   184 
   185 Due to licensing restrictions, Yices and Z3 are not installed/enabled
   186 by default.  Z3 is free for non-commercial applications and can be enabled
   187 by simply setting the environment variable @{text Z3_NON_COMMERCIAL} to
   188 @{text yes}.
   189 *}
   190 
   191 declare [[ smt_solver = z3 ]]
   192 
   193 text {*
   194 Since SMT solvers are potentially non-terminating, there is a timeout
   195 (given in seconds) to restrict their runtime.  A value greater than
   196 120 (seconds) is in most cases not advisable.
   197 *}
   198 
   199 declare [[ smt_timeout = 20 ]]
   200 
   201 text {*
   202 SMT solvers apply randomized heuristics.  In case a problem is not
   203 solvable by an SMT solver, changing the following option might help.
   204 *}
   205 
   206 declare [[ smt_random_seed = 1 ]]
   207 
   208 text {*
   209 In general, the binding to SMT solvers runs as an oracle, i.e, the SMT
   210 solvers are fully trusted without additional checks.  The following
   211 option can cause the SMT solver to run in proof-producing mode, giving
   212 a checkable certificate.  This is currently only implemented for Z3.
   213 *}
   214 
   215 declare [[ smt_oracle = false ]]
   216 
   217 text {*
   218 Each SMT solver provides several commandline options to tweak its
   219 behaviour.  They can be passed to the solver by setting the following
   220 options.
   221 *}
   222 
   223 declare [[ cvc3_options = "", remote_cvc3_options = "" ]]
   224 declare [[ yices_options = "" ]]
   225 declare [[ z3_options = "", remote_z3_options = "" ]]
   226 
   227 text {*
   228 Enable the following option to use built-in support for datatypes and
   229 records.  Currently, this is only implemented for Z3 running in oracle
   230 mode.
   231 *}
   232 
   233 declare [[ smt_datatypes = false ]]
   234 
   235 text {*
   236 The SMT method provides an inference mechanism to detect simple triggers
   237 in quantified formulas, which might increase the number of problems
   238 solvable by SMT solvers (note: triggers guide quantifier instantiations
   239 in the SMT solver).  To turn it on, set the following option.
   240 *}
   241 
   242 declare [[ smt_infer_triggers = false ]]
   243 
   244 text {*
   245 The SMT method monomorphizes the given facts, that is, it tries to
   246 instantiate all schematic type variables with fixed types occurring
   247 in the problem.  This is a (possibly nonterminating) fixed-point
   248 construction whose cycles are limited by the following option.
   249 *}
   250 
   251 declare [[ monomorph_max_rounds = 5 ]]
   252 
   253 text {*
   254 In addition, the number of generated monomorphic instances is limited
   255 by the following option.
   256 *}
   257 
   258 declare [[ monomorph_max_new_instances = 500 ]]
   259 
   260 
   261 
   262 subsection {* Certificates *}
   263 
   264 text {*
   265 By setting the option @{text smt_certificates} to the name of a file,
   266 all following applications of an SMT solver a cached in that file.
   267 Any further application of the same SMT solver (using the very same
   268 configuration) re-uses the cached certificate instead of invoking the
   269 solver.  An empty string disables caching certificates.
   270 
   271 The filename should be given as an explicit path.  It is good
   272 practice to use the name of the current theory (with ending
   273 @{text ".certs"} instead of @{text ".thy"}) as the certificates file.
   274 *}
   275 
   276 declare [[ smt_certificates = "" ]]
   277 
   278 text {*
   279 The option @{text smt_read_only_certificates} controls whether only
   280 stored certificates are should be used or invocation of an SMT solver
   281 is allowed.  When set to @{text true}, no SMT solver will ever be
   282 invoked and only the existing certificates found in the configured
   283 cache are used;  when set to @{text false} and there is no cached
   284 certificate for some proposition, then the configured SMT solver is
   285 invoked.
   286 *}
   287 
   288 declare [[ smt_read_only_certificates = false ]]
   289 
   290 
   291 
   292 subsection {* Tracing *}
   293 
   294 text {*
   295 The SMT method, when applied, traces important information.  To
   296 make it entirely silent, set the following option to @{text false}.
   297 *}
   298 
   299 declare [[ smt_verbose = true ]]
   300 
   301 text {*
   302 For tracing the generated problem file given to the SMT solver as
   303 well as the returned result of the solver, the option
   304 @{text smt_trace} should be set to @{text true}.
   305 *}
   306 
   307 declare [[ smt_trace = false ]]
   308 
   309 text {*
   310 From the set of assumptions given to the SMT solver, those assumptions
   311 used in the proof are traced when the following option is set to
   312 @{term true}.  This only works for Z3 when it runs in non-oracle mode
   313 (see options @{text smt_solver} and @{text smt_oracle} above).
   314 *}
   315 
   316 declare [[ smt_trace_used_facts = false ]]
   317 
   318 
   319 
   320 subsection {* Schematic rules for Z3 proof reconstruction *}
   321 
   322 text {*
   323 Several prof rules of Z3 are not very well documented.  There are two
   324 lemma groups which can turn failing Z3 proof reconstruction attempts
   325 into succeeding ones: the facts in @{text z3_rule} are tried prior to
   326 any implemented reconstruction procedure for all uncertain Z3 proof
   327 rules;  the facts in @{text z3_simp} are only fed to invocations of
   328 the simplifier when reconstructing theory-specific proof steps.
   329 *}
   330 
   331 lemmas [z3_rule] =
   332   refl eq_commute conj_commute disj_commute simp_thms nnf_simps
   333   ring_distribs field_simps times_divide_eq_right times_divide_eq_left
   334   if_True if_False not_not
   335 
   336 lemma [z3_rule]:
   337   "(P \<and> Q) = (\<not>(\<not>P \<or> \<not>Q))"
   338   "(P \<and> Q) = (\<not>(\<not>Q \<or> \<not>P))"
   339   "(\<not>P \<and> Q) = (\<not>(P \<or> \<not>Q))"
   340   "(\<not>P \<and> Q) = (\<not>(\<not>Q \<or> P))"
   341   "(P \<and> \<not>Q) = (\<not>(\<not>P \<or> Q))"
   342   "(P \<and> \<not>Q) = (\<not>(Q \<or> \<not>P))"
   343   "(\<not>P \<and> \<not>Q) = (\<not>(P \<or> Q))"
   344   "(\<not>P \<and> \<not>Q) = (\<not>(Q \<or> P))"
   345   by auto
   346 
   347 lemma [z3_rule]:
   348   "(P \<longrightarrow> Q) = (Q \<or> \<not>P)"
   349   "(\<not>P \<longrightarrow> Q) = (P \<or> Q)"
   350   "(\<not>P \<longrightarrow> Q) = (Q \<or> P)"
   351   "(True \<longrightarrow> P) = P"
   352   "(P \<longrightarrow> True) = True"
   353   "(False \<longrightarrow> P) = True"
   354   "(P \<longrightarrow> P) = True"
   355   by auto
   356 
   357 lemma [z3_rule]:
   358   "((P = Q) \<longrightarrow> R) = (R | (Q = (\<not>P)))"
   359   by auto
   360 
   361 lemma [z3_rule]:
   362   "(\<not>True) = False"
   363   "(\<not>False) = True"
   364   "(x = x) = True"
   365   "(P = True) = P"
   366   "(True = P) = P"
   367   "(P = False) = (\<not>P)"
   368   "(False = P) = (\<not>P)"
   369   "((\<not>P) = P) = False"
   370   "(P = (\<not>P)) = False"
   371   "((\<not>P) = (\<not>Q)) = (P = Q)"
   372   "\<not>(P = (\<not>Q)) = (P = Q)"
   373   "\<not>((\<not>P) = Q) = (P = Q)"
   374   "(P \<noteq> Q) = (Q = (\<not>P))"
   375   "(P = Q) = ((\<not>P \<or> Q) \<and> (P \<or> \<not>Q))"
   376   "(P \<noteq> Q) = ((\<not>P \<or> \<not>Q) \<and> (P \<or> Q))"
   377   by auto
   378 
   379 lemma [z3_rule]:
   380   "(if P then P else \<not>P) = True"
   381   "(if \<not>P then \<not>P else P) = True"
   382   "(if P then True else False) = P"
   383   "(if P then False else True) = (\<not>P)"
   384   "(if P then Q else True) = ((\<not>P) \<or> Q)"
   385   "(if P then Q else True) = (Q \<or> (\<not>P))"
   386   "(if P then Q else \<not>Q) = (P = Q)"
   387   "(if P then Q else \<not>Q) = (Q = P)"
   388   "(if P then \<not>Q else Q) = (P = (\<not>Q))"
   389   "(if P then \<not>Q else Q) = ((\<not>Q) = P)"
   390   "(if \<not>P then x else y) = (if P then y else x)"
   391   "(if P then (if Q then x else y) else x) = (if P \<and> (\<not>Q) then y else x)"
   392   "(if P then (if Q then x else y) else x) = (if (\<not>Q) \<and> P then y else x)"
   393   "(if P then (if Q then x else y) else y) = (if P \<and> Q then x else y)"
   394   "(if P then (if Q then x else y) else y) = (if Q \<and> P then x else y)"
   395   "(if P then x else if P then y else z) = (if P then x else z)"
   396   "(if P then x else if Q then x else y) = (if P \<or> Q then x else y)"
   397   "(if P then x else if Q then x else y) = (if Q \<or> P then x else y)"
   398   "(if P then x = y else x = z) = (x = (if P then y else z))"
   399   "(if P then x = y else y = z) = (y = (if P then x else z))"
   400   "(if P then x = y else z = y) = (y = (if P then x else z))"
   401   by auto
   402 
   403 lemma [z3_rule]:
   404   "0 + (x::int) = x"
   405   "x + 0 = x"
   406   "x + x = 2 * x"
   407   "0 * x = 0"
   408   "1 * x = x"
   409   "x + y = y + x"
   410   by auto
   411 
   412 lemma [z3_rule]:  (* for def-axiom *)
   413   "P = Q \<or> P \<or> Q"
   414   "P = Q \<or> \<not>P \<or> \<not>Q"
   415   "(\<not>P) = Q \<or> \<not>P \<or> Q"
   416   "(\<not>P) = Q \<or> P \<or> \<not>Q"
   417   "P = (\<not>Q) \<or> \<not>P \<or> Q"
   418   "P = (\<not>Q) \<or> P \<or> \<not>Q"
   419   "P \<noteq> Q \<or> P \<or> \<not>Q"
   420   "P \<noteq> Q \<or> \<not>P \<or> Q"
   421   "P \<noteq> (\<not>Q) \<or> P \<or> Q"
   422   "(\<not>P) \<noteq> Q \<or> P \<or> Q"
   423   "P \<or> Q \<or> P \<noteq> (\<not>Q)"
   424   "P \<or> Q \<or> (\<not>P) \<noteq> Q"
   425   "P \<or> \<not>Q \<or> P \<noteq> Q"
   426   "\<not>P \<or> Q \<or> P \<noteq> Q"
   427   "P \<or> y = (if P then x else y)"
   428   "P \<or> (if P then x else y) = y"
   429   "\<not>P \<or> x = (if P then x else y)"
   430   "\<not>P \<or>  (if P then x else y) = x"
   431   "P \<or> R \<or> \<not>(if P then Q else R)"
   432   "\<not>P \<or> Q \<or> \<not>(if P then Q else R)"
   433   "\<not>(if P then Q else R) \<or> \<not>P \<or> Q"
   434   "\<not>(if P then Q else R) \<or> P \<or> R"
   435   "(if P then Q else R) \<or> \<not>P \<or> \<not>Q"
   436   "(if P then Q else R) \<or> P \<or> \<not>R"
   437   "(if P then \<not>Q else R) \<or> \<not>P \<or> Q"
   438   "(if P then Q else \<not>R) \<or> P \<or> R"
   439   by auto
   440 
   441 
   442 
   443 hide_type (open) pattern
   444 hide_const Pattern fun_app term_true term_false z3div z3mod
   445 hide_const (open) trigger pat nopat weight
   446 
   447 end