|
1 (* Title: HOL/SMT2.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 SMT2 |
|
8 imports Record |
|
9 keywords "smt2_status" :: diag |
|
10 begin |
|
11 |
|
12 ML_file "Tools/SMT2/smt2_utils.ML" |
|
13 ML_file "Tools/SMT2/smt2_failure.ML" |
|
14 ML_file "Tools/SMT2/smt2_config.ML" |
|
15 |
|
16 |
|
17 subsection {* Triggers for quantifier instantiation *} |
|
18 |
|
19 text {* |
|
20 Some SMT solvers support patterns as a quantifier instantiation |
|
21 heuristics. Patterns may either be positive terms (tagged by "pat") |
|
22 triggering quantifier instantiations -- when the solver finds a |
|
23 term matching a positive pattern, it instantiates the corresponding |
|
24 quantifier accordingly -- or negative terms (tagged by "nopat") |
|
25 inhibiting quantifier instantiations. A list of patterns |
|
26 of the same kind is called a multipattern, and all patterns in a |
|
27 multipattern are considered conjunctively for quantifier instantiation. |
|
28 A list of multipatterns is called a trigger, and their multipatterns |
|
29 act disjunctively during quantifier instantiation. Each multipattern |
|
30 should mention at least all quantified variables of the preceding |
|
31 quantifier block. |
|
32 *} |
|
33 |
|
34 typedecl pattern |
|
35 |
|
36 consts |
|
37 pat :: "'a \<Rightarrow> pattern" |
|
38 nopat :: "'a \<Rightarrow> pattern" |
|
39 |
|
40 definition trigger :: "pattern list list \<Rightarrow> bool \<Rightarrow> bool" where "trigger _ P = P" |
|
41 |
|
42 |
|
43 subsection {* Quantifier weights *} |
|
44 |
|
45 text {* |
|
46 Weight annotations to quantifiers influence the priority of quantifier |
|
47 instantiations. They should be handled with care for solvers, which support |
|
48 them, because incorrect choices of weights might render a problem unsolvable. |
|
49 *} |
|
50 |
|
51 definition weight :: "int \<Rightarrow> bool \<Rightarrow> bool" where "weight _ P = P" |
|
52 |
|
53 text {* |
|
54 Weights must be non-negative. The value @{text 0} is equivalent to providing |
|
55 no weight at all. |
|
56 |
|
57 Weights should only be used at quantifiers and only inside triggers (if the |
|
58 quantifier has triggers). Valid usages of weights are as follows: |
|
59 |
|
60 \begin{itemize} |
|
61 \item |
|
62 @{term "\<forall>x. trigger [[pat (P x)]] (weight 2 (P x))"} |
|
63 \item |
|
64 @{term "\<forall>x. weight 3 (P x)"} |
|
65 \end{itemize} |
|
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 :: "'a \<Rightarrow> 'a" where "fun_app f = f" |
|
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 fun_upd_upd fun_app_def |
|
86 |
|
87 |
|
88 subsection {* Integer division and modulo for Z3 *} |
|
89 |
|
90 definition z3div :: "int \<Rightarrow> int \<Rightarrow> int" where |
|
91 "z3div k l = (if 0 \<le> l then k div l else -(k div (-l)))" |
|
92 |
|
93 definition z3mod :: "int \<Rightarrow> int \<Rightarrow> int" where |
|
94 "z3mod k l = (if 0 \<le> l then k mod l else k mod (-l))" |
|
95 |
|
96 |
|
97 subsection {* Setup *} |
|
98 |
|
99 ML_file "Tools/SMT2/smt2_builtin.ML" |
|
100 ML_file "Tools/SMT2/smt2_datatypes.ML" |
|
101 ML_file "Tools/SMT2/smt2_normalize.ML" |
|
102 ML_file "Tools/SMT2/smt2_translate.ML" |
|
103 ML_file "Tools/SMT2/smt2_solver.ML" |
|
104 ML_file "Tools/SMT2/smtlib2.ML" |
|
105 ML_file "Tools/SMT2/smtlib2_interface.ML" |
|
106 ML_file "Tools/SMT2/z3_new_interface.ML" |
|
107 ML_file "Tools/SMT2/z3_new_proof.ML" |
|
108 ML_file "Tools/SMT2/z3_new_proof_tools.ML" |
|
109 ML_file "Tools/SMT2/z3_new_proof_literals.ML" |
|
110 ML_file "Tools/SMT2/z3_new_proof_rules.ML" |
|
111 ML_file "Tools/SMT2/z3_new_proof_methods.ML" |
|
112 ML_file "Tools/SMT2/z3_new_proof_replay.ML" |
|
113 ML_file "Tools/SMT2/z3_new_isar.ML" |
|
114 ML_file "Tools/SMT2/smt2_setup_solvers.ML" |
|
115 |
|
116 method_setup smt2 = {* |
|
117 Scan.optional Attrib.thms [] >> |
|
118 (fn thms => fn ctxt => |
|
119 METHOD (fn facts => HEADGOAL (SMT2_Solver.smt2_tac ctxt (thms @ facts)))) |
|
120 *} "apply an SMT solver to the current goal (based on SMT-LIB 2)" |
|
121 |
|
122 |
|
123 subsection {* Configuration *} |
|
124 |
|
125 text {* |
|
126 The current configuration can be printed by the command |
|
127 @{text smt2_status}, which shows the values of most options. |
|
128 *} |
|
129 |
|
130 |
|
131 |
|
132 subsection {* General configuration options *} |
|
133 |
|
134 text {* |
|
135 The option @{text smt2_solver} can be used to change the target SMT |
|
136 solver. The possible values can be obtained from the @{text smt2_status} |
|
137 command. |
|
138 |
|
139 Due to licensing restrictions, Yices and Z3 are not installed/enabled |
|
140 by default. Z3 is free for non-commercial applications and can be enabled |
|
141 by setting Isabelle system option @{text z3_non_commercial} to @{text yes}. |
|
142 *} |
|
143 |
|
144 declare [[ smt2_solver = z3_new ]] |
|
145 |
|
146 text {* |
|
147 Since SMT solvers are potentially non-terminating, there is a timeout |
|
148 (given in seconds) to restrict their runtime. A value greater than |
|
149 120 (seconds) is in most cases not advisable. |
|
150 *} |
|
151 |
|
152 declare [[ smt2_timeout = 20 ]] |
|
153 |
|
154 text {* |
|
155 SMT solvers apply randomized heuristics. In case a problem is not |
|
156 solvable by an SMT solver, changing the following option might help. |
|
157 *} |
|
158 |
|
159 declare [[ smt2_random_seed = 1 ]] |
|
160 |
|
161 text {* |
|
162 In general, the binding to SMT solvers runs as an oracle, i.e, the SMT |
|
163 solvers are fully trusted without additional checks. The following |
|
164 option can cause the SMT solver to run in proof-producing mode, giving |
|
165 a checkable certificate. This is currently only implemented for Z3. |
|
166 *} |
|
167 |
|
168 declare [[ smt2_oracle = false ]] |
|
169 |
|
170 text {* |
|
171 Each SMT solver provides several commandline options to tweak its |
|
172 behaviour. They can be passed to the solver by setting the following |
|
173 options. |
|
174 *} |
|
175 |
|
176 (* declare [[ cvc3_options = "" ]] TODO *) |
|
177 (* declare [[ yices_options = "" ]] TODO *) |
|
178 (* declare [[ z3_options = "" ]] TODO *) |
|
179 |
|
180 text {* |
|
181 The SMT method provides an inference mechanism to detect simple triggers |
|
182 in quantified formulas, which might increase the number of problems |
|
183 solvable by SMT solvers (note: triggers guide quantifier instantiations |
|
184 in the SMT solver). To turn it on, set the following option. |
|
185 *} |
|
186 |
|
187 declare [[ smt2_infer_triggers = false ]] |
|
188 |
|
189 text {* |
|
190 Enable the following option to use built-in support for div/mod, datatypes, |
|
191 and records in Z3. Currently, this is implemented only in oracle mode. |
|
192 *} |
|
193 |
|
194 declare [[ z3_new_extensions = false ]] |
|
195 |
|
196 text {* |
|
197 The SMT method monomorphizes the given facts, that is, it tries to |
|
198 instantiate all schematic type variables with fixed types occurring |
|
199 in the problem. This is a (possibly nonterminating) fixed-point |
|
200 construction whose cycles are limited by the following option. |
|
201 *} |
|
202 |
|
203 declare [[ monomorph_max_rounds = 5 ]] |
|
204 |
|
205 text {* |
|
206 In addition, the number of generated monomorphic instances is limited |
|
207 by the following option. |
|
208 *} |
|
209 |
|
210 declare [[ monomorph_max_new_instances = 500 ]] |
|
211 |
|
212 |
|
213 |
|
214 subsection {* Certificates *} |
|
215 |
|
216 text {* |
|
217 By setting the option @{text smt2_certificates} to the name of a file, |
|
218 all following applications of an SMT solver a cached in that file. |
|
219 Any further application of the same SMT solver (using the very same |
|
220 configuration) re-uses the cached certificate instead of invoking the |
|
221 solver. An empty string disables caching certificates. |
|
222 |
|
223 The filename should be given as an explicit path. It is good |
|
224 practice to use the name of the current theory (with ending |
|
225 @{text ".certs"} instead of @{text ".thy"}) as the certificates file. |
|
226 Certificate files should be used at most once in a certain theory context, |
|
227 to avoid race conditions with other concurrent accesses. |
|
228 *} |
|
229 |
|
230 declare [[ smt2_certificates = "" ]] |
|
231 |
|
232 text {* |
|
233 The option @{text smt2_read_only_certificates} controls whether only |
|
234 stored certificates are should be used or invocation of an SMT solver |
|
235 is allowed. When set to @{text true}, no SMT solver will ever be |
|
236 invoked and only the existing certificates found in the configured |
|
237 cache are used; when set to @{text false} and there is no cached |
|
238 certificate for some proposition, then the configured SMT solver is |
|
239 invoked. |
|
240 *} |
|
241 |
|
242 declare [[ smt2_read_only_certificates = false ]] |
|
243 |
|
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 [[ smt2_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 smt2_trace} should be set to @{text true}. |
|
259 *} |
|
260 |
|
261 declare [[ smt2_trace = false ]] |
|
262 |
|
263 text {* |
|
264 From the set of assumptions given to the SMT solver, those assumptions |
|
265 used in the proof are traced when the following option is set to |
|
266 @{term true}. This only works for Z3 when it runs in non-oracle mode |
|
267 (see options @{text smt2_solver} and @{text smt2_oracle} above). |
|
268 *} |
|
269 |
|
270 declare [[ smt2_trace_used_facts = false ]] |
|
271 |
|
272 |
|
273 subsection {* Schematic rules for Z3 proof reconstruction *} |
|
274 |
|
275 text {* |
|
276 Several prof rules of Z3 are not very well documented. There are two |
|
277 lemma groups which can turn failing Z3 proof reconstruction attempts |
|
278 into succeeding ones: the facts in @{text z3_rule} are tried prior to |
|
279 any implemented reconstruction procedure for all uncertain Z3 proof |
|
280 rules; the facts in @{text z3_simp} are only fed to invocations of |
|
281 the simplifier when reconstructing theory-specific proof steps. |
|
282 *} |
|
283 |
|
284 lemmas [z3_new_rule] = |
|
285 refl eq_commute conj_commute disj_commute simp_thms nnf_simps |
|
286 ring_distribs field_simps times_divide_eq_right times_divide_eq_left |
|
287 if_True if_False not_not |
|
288 |
|
289 lemma [z3_new_rule]: |
|
290 "(P \<and> Q) = (\<not>(\<not>P \<or> \<not>Q))" |
|
291 "(P \<and> Q) = (\<not>(\<not>Q \<or> \<not>P))" |
|
292 "(\<not>P \<and> Q) = (\<not>(P \<or> \<not>Q))" |
|
293 "(\<not>P \<and> Q) = (\<not>(\<not>Q \<or> P))" |
|
294 "(P \<and> \<not>Q) = (\<not>(\<not>P \<or> Q))" |
|
295 "(P \<and> \<not>Q) = (\<not>(Q \<or> \<not>P))" |
|
296 "(\<not>P \<and> \<not>Q) = (\<not>(P \<or> Q))" |
|
297 "(\<not>P \<and> \<not>Q) = (\<not>(Q \<or> P))" |
|
298 by auto |
|
299 |
|
300 lemma [z3_new_rule]: |
|
301 "(P \<longrightarrow> Q) = (Q \<or> \<not>P)" |
|
302 "(\<not>P \<longrightarrow> Q) = (P \<or> Q)" |
|
303 "(\<not>P \<longrightarrow> Q) = (Q \<or> P)" |
|
304 "(True \<longrightarrow> P) = P" |
|
305 "(P \<longrightarrow> True) = True" |
|
306 "(False \<longrightarrow> P) = True" |
|
307 "(P \<longrightarrow> P) = True" |
|
308 by auto |
|
309 |
|
310 lemma [z3_new_rule]: |
|
311 "((P = Q) \<longrightarrow> R) = (R | (Q = (\<not>P)))" |
|
312 by auto |
|
313 |
|
314 lemma [z3_new_rule]: |
|
315 "(\<not>True) = False" |
|
316 "(\<not>False) = True" |
|
317 "(x = x) = True" |
|
318 "(P = True) = P" |
|
319 "(True = P) = P" |
|
320 "(P = False) = (\<not>P)" |
|
321 "(False = P) = (\<not>P)" |
|
322 "((\<not>P) = P) = False" |
|
323 "(P = (\<not>P)) = False" |
|
324 "((\<not>P) = (\<not>Q)) = (P = Q)" |
|
325 "\<not>(P = (\<not>Q)) = (P = Q)" |
|
326 "\<not>((\<not>P) = Q) = (P = Q)" |
|
327 "(P \<noteq> Q) = (Q = (\<not>P))" |
|
328 "(P = Q) = ((\<not>P \<or> Q) \<and> (P \<or> \<not>Q))" |
|
329 "(P \<noteq> Q) = ((\<not>P \<or> \<not>Q) \<and> (P \<or> Q))" |
|
330 by auto |
|
331 |
|
332 lemma [z3_new_rule]: |
|
333 "(if P then P else \<not>P) = True" |
|
334 "(if \<not>P then \<not>P else P) = True" |
|
335 "(if P then True else False) = P" |
|
336 "(if P then False else True) = (\<not>P)" |
|
337 "(if P then Q else True) = ((\<not>P) \<or> Q)" |
|
338 "(if P then Q else True) = (Q \<or> (\<not>P))" |
|
339 "(if P then Q else \<not>Q) = (P = Q)" |
|
340 "(if P then Q else \<not>Q) = (Q = P)" |
|
341 "(if P then \<not>Q else Q) = (P = (\<not>Q))" |
|
342 "(if P then \<not>Q else Q) = ((\<not>Q) = P)" |
|
343 "(if \<not>P then x else y) = (if P then y else x)" |
|
344 "(if P then (if Q then x else y) else x) = (if P \<and> (\<not>Q) then y else x)" |
|
345 "(if P then (if Q then x else y) else x) = (if (\<not>Q) \<and> P then y else x)" |
|
346 "(if P then (if Q then x else y) else y) = (if P \<and> Q then x else y)" |
|
347 "(if P then (if Q then x else y) else y) = (if Q \<and> P then x else y)" |
|
348 "(if P then x else if P then y else z) = (if P then x else z)" |
|
349 "(if P then x else if Q then x else y) = (if P \<or> Q then x else y)" |
|
350 "(if P then x else if Q then x else y) = (if Q \<or> P then x else y)" |
|
351 "(if P then x = y else x = z) = (x = (if P then y else z))" |
|
352 "(if P then x = y else y = z) = (y = (if P then x else z))" |
|
353 "(if P then x = y else z = y) = (y = (if P then x else z))" |
|
354 by auto |
|
355 |
|
356 lemma [z3_new_rule]: |
|
357 "0 + (x::int) = x" |
|
358 "x + 0 = x" |
|
359 "x + x = 2 * x" |
|
360 "0 * x = 0" |
|
361 "1 * x = x" |
|
362 "x + y = y + x" |
|
363 by auto |
|
364 |
|
365 lemma [z3_new_rule]: (* for def-axiom *) |
|
366 "P = Q \<or> P \<or> Q" |
|
367 "P = Q \<or> \<not>P \<or> \<not>Q" |
|
368 "(\<not>P) = Q \<or> \<not>P \<or> Q" |
|
369 "(\<not>P) = Q \<or> P \<or> \<not>Q" |
|
370 "P = (\<not>Q) \<or> \<not>P \<or> Q" |
|
371 "P = (\<not>Q) \<or> P \<or> \<not>Q" |
|
372 "P \<noteq> Q \<or> P \<or> \<not>Q" |
|
373 "P \<noteq> Q \<or> \<not>P \<or> Q" |
|
374 "P \<noteq> (\<not>Q) \<or> P \<or> Q" |
|
375 "(\<not>P) \<noteq> Q \<or> P \<or> Q" |
|
376 "P \<or> Q \<or> P \<noteq> (\<not>Q)" |
|
377 "P \<or> Q \<or> (\<not>P) \<noteq> Q" |
|
378 "P \<or> \<not>Q \<or> P \<noteq> Q" |
|
379 "\<not>P \<or> Q \<or> P \<noteq> Q" |
|
380 "P \<or> y = (if P then x else y)" |
|
381 "P \<or> (if P then x else y) = y" |
|
382 "\<not>P \<or> x = (if P then x else y)" |
|
383 "\<not>P \<or> (if P then x else y) = x" |
|
384 "P \<or> R \<or> \<not>(if P then Q else R)" |
|
385 "\<not>P \<or> Q \<or> \<not>(if P then Q else R)" |
|
386 "\<not>(if P then Q else R) \<or> \<not>P \<or> Q" |
|
387 "\<not>(if P then Q else R) \<or> P \<or> R" |
|
388 "(if P then Q else R) \<or> \<not>P \<or> \<not>Q" |
|
389 "(if P then Q else R) \<or> P \<or> \<not>R" |
|
390 "(if P then \<not>Q else R) \<or> \<not>P \<or> Q" |
|
391 "(if P then Q else \<not>R) \<or> P \<or> R" |
|
392 by auto |
|
393 |
|
394 hide_type (open) pattern |
|
395 hide_const fun_app z3div z3mod |
|
396 hide_const (open) trigger pat nopat weight |
|
397 |
|
398 end |