src/HOL/SMT.thy
author blanchet
Thu Sep 25 13:30:57 2014 +0200 (2014-09-25)
changeset 58441 c1b489999de9
parent 58360 dee1fd1cc631
child 58481 62bc7c79212b
permissions -rw-r--r--
added useful options to CVC4
     1 (*  Title:      HOL/SMT.thy
     2     Author:     Sascha Boehme, TU Muenchen
     3 *)
     4 
     5 header {* Bindings to Satisfiability Modulo Theories (SMT) solvers based on SMT-LIB 2 *}
     6 
     7 theory SMT
     8 imports Divides
     9 keywords "smt_status" :: diag
    10 begin
    11 
    12 subsection {* Triggers for quantifier instantiation *}
    13 
    14 text {*
    15 Some SMT solvers support patterns as a quantifier instantiation
    16 heuristics. Patterns may either be positive terms (tagged by "pat")
    17 triggering quantifier instantiations -- when the solver finds a
    18 term matching a positive pattern, it instantiates the corresponding
    19 quantifier accordingly -- or negative terms (tagged by "nopat")
    20 inhibiting quantifier instantiations. A list of patterns
    21 of the same kind is called a multipattern, and all patterns in a
    22 multipattern are considered conjunctively for quantifier instantiation.
    23 A list of multipatterns is called a trigger, and their multipatterns
    24 act disjunctively during quantifier instantiation. Each multipattern
    25 should mention at least all quantified variables of the preceding
    26 quantifier block.
    27 *}
    28 
    29 typedecl 'a symb_list
    30 
    31 consts
    32   Symb_Nil :: "'a symb_list"
    33   Symb_Cons :: "'a \<Rightarrow> 'a symb_list \<Rightarrow> 'a symb_list"
    34 
    35 typedecl pattern
    36 
    37 consts
    38   pat :: "'a \<Rightarrow> pattern"
    39   nopat :: "'a \<Rightarrow> pattern"
    40 
    41 definition trigger :: "pattern symb_list symb_list \<Rightarrow> bool \<Rightarrow> bool" where
    42   "trigger _ P = P"
    43 
    44 
    45 subsection {* Higher-order encoding *}
    46 
    47 text {*
    48 Application is made explicit for constants occurring with varying
    49 numbers of arguments. This is achieved by the introduction of the
    50 following constant.
    51 *}
    52 
    53 definition fun_app :: "'a \<Rightarrow> 'a" where "fun_app f = f"
    54 
    55 text {*
    56 Some solvers support a theory of arrays which can be used to encode
    57 higher-order functions. The following set of lemmas specifies the
    58 properties of such (extensional) arrays.
    59 *}
    60 
    61 lemmas array_rules = ext fun_upd_apply fun_upd_same fun_upd_other  fun_upd_upd fun_app_def
    62 
    63 
    64 subsection {* Normalization *}
    65 
    66 lemma case_bool_if[abs_def]: "case_bool x y P = (if P then x else y)"
    67   by simp
    68 
    69 lemmas Ex1_def_raw = Ex1_def[abs_def]
    70 lemmas Ball_def_raw = Ball_def[abs_def]
    71 lemmas Bex_def_raw = Bex_def[abs_def]
    72 lemmas abs_if_raw = abs_if[abs_def]
    73 lemmas min_def_raw = min_def[abs_def]
    74 lemmas max_def_raw = max_def[abs_def]
    75 
    76 
    77 subsection {* Integer division and modulo for Z3 *}
    78 
    79 text {*
    80 The following Z3-inspired definitions are overspecified for the case where @{text "l = 0"}. This
    81 Schönheitsfehler is corrected in the @{text div_as_z3div} and @{text mod_as_z3mod} theorems.
    82 *}
    83 
    84 definition z3div :: "int \<Rightarrow> int \<Rightarrow> int" where
    85   "z3div k l = (if l \<ge> 0 then k div l else - (k div - l))"
    86 
    87 definition z3mod :: "int \<Rightarrow> int \<Rightarrow> int" where
    88   "z3mod k l = k mod (if l \<ge> 0 then l else - l)"
    89 
    90 lemma div_as_z3div:
    91   "\<forall>k l. k div l = (if l = 0 then 0 else if l > 0 then z3div k l else z3div (- k) (- l))"
    92   by (simp add: z3div_def)
    93 
    94 lemma mod_as_z3mod:
    95   "\<forall>k l. k mod l = (if l = 0 then k else if l > 0 then z3mod k l else - z3mod (- k) (- l))"
    96   by (simp add: z3mod_def)
    97 
    98 
    99 subsection {* Setup *}
   100 
   101 ML_file "Tools/SMT/smt_util.ML"
   102 ML_file "Tools/SMT/smt_failure.ML"
   103 ML_file "Tools/SMT/smt_config.ML"
   104 ML_file "Tools/SMT/smt_builtin.ML"
   105 ML_file "Tools/SMT/smt_datatypes.ML"
   106 ML_file "Tools/SMT/smt_normalize.ML"
   107 ML_file "Tools/SMT/smt_translate.ML"
   108 ML_file "Tools/SMT/smtlib.ML"
   109 ML_file "Tools/SMT/smtlib_interface.ML"
   110 ML_file "Tools/SMT/smtlib_proof.ML"
   111 ML_file "Tools/SMT/smtlib_isar.ML"
   112 ML_file "Tools/SMT/z3_proof.ML"
   113 ML_file "Tools/SMT/z3_isar.ML"
   114 ML_file "Tools/SMT/smt_solver.ML"
   115 ML_file "Tools/SMT/cvc4_interface.ML"
   116 ML_file "Tools/SMT/verit_proof.ML"
   117 ML_file "Tools/SMT/verit_isar.ML"
   118 ML_file "Tools/SMT/verit_proof_parse.ML"
   119 ML_file "Tools/SMT/z3_interface.ML"
   120 ML_file "Tools/SMT/z3_replay_util.ML"
   121 ML_file "Tools/SMT/z3_replay_literals.ML"
   122 ML_file "Tools/SMT/z3_replay_rules.ML"
   123 ML_file "Tools/SMT/z3_replay_methods.ML"
   124 ML_file "Tools/SMT/z3_replay.ML"
   125 ML_file "Tools/SMT/smt_systems.ML"
   126 
   127 method_setup smt = {*
   128   Scan.optional Attrib.thms [] >>
   129     (fn thms => fn ctxt =>
   130       METHOD (fn facts => HEADGOAL (SMT_Solver.smt_tac ctxt (thms @ facts))))
   131 *} "apply an SMT solver to the current goal"
   132 
   133 
   134 subsection {* Configuration *}
   135 
   136 text {*
   137 The current configuration can be printed by the command
   138 @{text smt_status}, which shows the values of most options.
   139 *}
   140 
   141 
   142 subsection {* General configuration options *}
   143 
   144 text {*
   145 The option @{text smt_solver} can be used to change the target SMT
   146 solver. The possible values can be obtained from the @{text smt_status}
   147 command.
   148 
   149 Due to licensing restrictions, Z3 is not enabled by default. Z3 is free
   150 for non-commercial applications and can be enabled by setting Isabelle
   151 system option @{text z3_non_commercial} to @{text yes}.
   152 *}
   153 
   154 declare [[smt_solver = z3]]
   155 
   156 text {*
   157 Since SMT solvers are potentially nonterminating, there is a timeout
   158 (given in seconds) to restrict their runtime.
   159 *}
   160 
   161 declare [[smt_timeout = 20]]
   162 
   163 text {*
   164 SMT solvers apply randomized heuristics. In case a problem is not
   165 solvable by an SMT solver, changing the following option might help.
   166 *}
   167 
   168 declare [[smt_random_seed = 1]]
   169 
   170 text {*
   171 In general, the binding to SMT solvers runs as an oracle, i.e, the SMT
   172 solvers are fully trusted without additional checks. The following
   173 option can cause the SMT solver to run in proof-producing mode, giving
   174 a checkable certificate. This is currently only implemented for Z3.
   175 *}
   176 
   177 declare [[smt_oracle = false]]
   178 
   179 text {*
   180 Each SMT solver provides several commandline options to tweak its
   181 behaviour. They can be passed to the solver by setting the following
   182 options.
   183 *}
   184 
   185 declare [[cvc3_options = ""]]
   186 declare [[cvc4_options = "--full-saturate-quant --quant-cf"]]
   187 declare [[veriT_options = ""]]
   188 declare [[z3_options = ""]]
   189 
   190 text {*
   191 The SMT method provides an inference mechanism to detect simple triggers
   192 in quantified formulas, which might increase the number of problems
   193 solvable by SMT solvers (note: triggers guide quantifier instantiations
   194 in the SMT solver). To turn it on, set the following option.
   195 *}
   196 
   197 declare [[smt_infer_triggers = false]]
   198 
   199 text {*
   200 Enable the following option to use built-in support for datatypes,
   201 codatatypes, and records in CVC4. Currently, this is implemented only
   202 in oracle mode.
   203 *}
   204 
   205 declare [[cvc4_extensions = false]]
   206 
   207 text {*
   208 Enable the following option to use built-in support for div/mod, datatypes,
   209 and records in Z3. Currently, this is implemented only in oracle mode.
   210 *}
   211 
   212 declare [[z3_extensions = false]]
   213 
   214 
   215 subsection {* Certificates *}
   216 
   217 text {*
   218 By setting the option @{text smt_certificates} to the name of a file,
   219 all following applications of an SMT solver a cached in that file.
   220 Any further application of the same SMT solver (using the very same
   221 configuration) re-uses the cached certificate instead of invoking the
   222 solver. An empty string disables caching certificates.
   223 
   224 The filename should be given as an explicit path. It is good
   225 practice to use the name of the current theory (with ending
   226 @{text ".certs"} instead of @{text ".thy"}) as the certificates file.
   227 Certificate files should be used at most once in a certain theory context,
   228 to avoid race conditions with other concurrent accesses.
   229 *}
   230 
   231 declare [[smt_certificates = ""]]
   232 
   233 text {*
   234 The option @{text smt_read_only_certificates} controls whether only
   235 stored certificates are should be used or invocation of an SMT solver
   236 is allowed. When set to @{text true}, no SMT solver will ever be
   237 invoked and only the existing certificates found in the configured
   238 cache are used;  when set to @{text false} and there is no cached
   239 certificate for some proposition, then the configured SMT solver is
   240 invoked.
   241 *}
   242 
   243 declare [[smt_read_only_certificates = false]]
   244 
   245 
   246 subsection {* Tracing *}
   247 
   248 text {*
   249 The SMT method, when applied, traces important information. To
   250 make it entirely silent, set the following option to @{text false}.
   251 *}
   252 
   253 declare [[smt_verbose = true]]
   254 
   255 text {*
   256 For tracing the generated problem file given to the SMT solver as
   257 well as the returned result of the solver, the option
   258 @{text smt_trace} should be set to @{text true}.
   259 *}
   260 
   261 declare [[smt_trace = false]]
   262 
   263 
   264 subsection {* Schematic rules for Z3 proof reconstruction *}
   265 
   266 text {*
   267 Several prof rules of Z3 are not very well documented. There are two
   268 lemma groups which can turn failing Z3 proof reconstruction attempts
   269 into succeeding ones: the facts in @{text z3_rule} are tried prior to
   270 any implemented reconstruction procedure for all uncertain Z3 proof
   271 rules;  the facts in @{text z3_simp} are only fed to invocations of
   272 the simplifier when reconstructing theory-specific proof steps.
   273 *}
   274 
   275 lemmas [z3_rule] =
   276   refl eq_commute conj_commute disj_commute simp_thms nnf_simps
   277   ring_distribs field_simps times_divide_eq_right times_divide_eq_left
   278   if_True if_False not_not
   279 
   280 lemma [z3_rule]:
   281   "(P \<and> Q) = (\<not> (\<not> P \<or> \<not> Q))"
   282   "(P \<and> Q) = (\<not> (\<not> Q \<or> \<not> P))"
   283   "(\<not> P \<and> Q) = (\<not> (P \<or> \<not> Q))"
   284   "(\<not> P \<and> Q) = (\<not> (\<not> Q \<or> P))"
   285   "(P \<and> \<not> Q) = (\<not> (\<not> P \<or> Q))"
   286   "(P \<and> \<not> Q) = (\<not> (Q \<or> \<not> P))"
   287   "(\<not> P \<and> \<not> Q) = (\<not> (P \<or> Q))"
   288   "(\<not> P \<and> \<not> Q) = (\<not> (Q \<or> P))"
   289   by auto
   290 
   291 lemma [z3_rule]:
   292   "(P \<longrightarrow> Q) = (Q \<or> \<not> P)"
   293   "(\<not> P \<longrightarrow> Q) = (P \<or> Q)"
   294   "(\<not> P \<longrightarrow> Q) = (Q \<or> P)"
   295   "(True \<longrightarrow> P) = P"
   296   "(P \<longrightarrow> True) = True"
   297   "(False \<longrightarrow> P) = True"
   298   "(P \<longrightarrow> P) = True"
   299   by auto
   300 
   301 lemma [z3_rule]:
   302   "((P = Q) \<longrightarrow> R) = (R | (Q = (\<not> P)))"
   303   by auto
   304 
   305 lemma [z3_rule]:
   306   "(\<not> True) = False"
   307   "(\<not> False) = True"
   308   "(x = x) = True"
   309   "(P = True) = P"
   310   "(True = P) = P"
   311   "(P = False) = (\<not> P)"
   312   "(False = P) = (\<not> P)"
   313   "((\<not> P) = P) = False"
   314   "(P = (\<not> P)) = False"
   315   "((\<not> P) = (\<not> Q)) = (P = Q)"
   316   "\<not> (P = (\<not> Q)) = (P = Q)"
   317   "\<not> ((\<not> P) = Q) = (P = Q)"
   318   "(P \<noteq> Q) = (Q = (\<not> P))"
   319   "(P = Q) = ((\<not> P \<or> Q) \<and> (P \<or> \<not> Q))"
   320   "(P \<noteq> Q) = ((\<not> P \<or> \<not> Q) \<and> (P \<or> Q))"
   321   by auto
   322 
   323 lemma [z3_rule]:
   324   "(if P then P else \<not> P) = True"
   325   "(if \<not> P then \<not> P else P) = True"
   326   "(if P then True else False) = P"
   327   "(if P then False else True) = (\<not> P)"
   328   "(if P then Q else True) = ((\<not> P) \<or> Q)"
   329   "(if P then Q else True) = (Q \<or> (\<not> P))"
   330   "(if P then Q else \<not> Q) = (P = Q)"
   331   "(if P then Q else \<not> Q) = (Q = P)"
   332   "(if P then \<not> Q else Q) = (P = (\<not> Q))"
   333   "(if P then \<not> Q else Q) = ((\<not> Q) = P)"
   334   "(if \<not> P then x else y) = (if P then y else x)"
   335   "(if P then (if Q then x else y) else x) = (if P \<and> (\<not> Q) then y else x)"
   336   "(if P then (if Q then x else y) else x) = (if (\<not> Q) \<and> P then y else x)"
   337   "(if P then (if Q then x else y) else y) = (if P \<and> Q then x else y)"
   338   "(if P then (if Q then x else y) else y) = (if Q \<and> P then x else y)"
   339   "(if P then x else if P then y else z) = (if P then x else z)"
   340   "(if P then x else if Q then x else y) = (if P \<or> Q then x else y)"
   341   "(if P then x else if Q then x else y) = (if Q \<or> P then x else y)"
   342   "(if P then x = y else x = z) = (x = (if P then y else z))"
   343   "(if P then x = y else y = z) = (y = (if P then x else z))"
   344   "(if P then x = y else z = y) = (y = (if P then x else z))"
   345   by auto
   346 
   347 lemma [z3_rule]:
   348   "0 + (x::int) = x"
   349   "x + 0 = x"
   350   "x + x = 2 * x"
   351   "0 * x = 0"
   352   "1 * x = x"
   353   "x + y = y + x"
   354   by (auto simp add: mult_2)
   355 
   356 lemma [z3_rule]:  (* for def-axiom *)
   357   "P = Q \<or> P \<or> Q"
   358   "P = Q \<or> \<not> P \<or> \<not> Q"
   359   "(\<not> P) = Q \<or> \<not> P \<or> Q"
   360   "(\<not> P) = Q \<or> P \<or> \<not> Q"
   361   "P = (\<not> Q) \<or> \<not> P \<or> Q"
   362   "P = (\<not> Q) \<or> P \<or> \<not> Q"
   363   "P \<noteq> Q \<or> P \<or> \<not> Q"
   364   "P \<noteq> Q \<or> \<not> P \<or> Q"
   365   "P \<noteq> (\<not> Q) \<or> P \<or> Q"
   366   "(\<not> P) \<noteq> Q \<or> P \<or> Q"
   367   "P \<or> Q \<or> P \<noteq> (\<not> Q)"
   368   "P \<or> Q \<or> (\<not> P) \<noteq> Q"
   369   "P \<or> \<not> Q \<or> P \<noteq> Q"
   370   "\<not> P \<or> Q \<or> P \<noteq> Q"
   371   "P \<or> y = (if P then x else y)"
   372   "P \<or> (if P then x else y) = y"
   373   "\<not> P \<or> x = (if P then x else y)"
   374   "\<not> P \<or> (if P then x else y) = x"
   375   "P \<or> R \<or> \<not> (if P then Q else R)"
   376   "\<not> P \<or> Q \<or> \<not> (if P then Q else R)"
   377   "\<not> (if P then Q else R) \<or> \<not> P \<or> Q"
   378   "\<not> (if P then Q else R) \<or> P \<or> R"
   379   "(if P then Q else R) \<or> \<not> P \<or> \<not> Q"
   380   "(if P then Q else R) \<or> P \<or> \<not> R"
   381   "(if P then \<not> Q else R) \<or> \<not> P \<or> Q"
   382   "(if P then Q else \<not> R) \<or> P \<or> R"
   383   by auto
   384 
   385 hide_type (open) symb_list pattern
   386 hide_const (open) Symb_Nil Symb_Cons trigger pat nopat fun_app z3div z3mod
   387 
   388 end