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