简体   繁体   中英

how to use mkForAll() in java z3

I am a new learner of z3 solver. I would like to implement universal quantifier and I found it in https://z3prover.github.io/api/html/namespacecom_1_1microsoft_1_1z3.html that context.mkForAll() may be helpful but the document is hard to follow since there are many arguments and no examples.

For example, how should I implement checking following statement: for any y in {2, 4, 6, 8}, there exists integer x where x > y?

This is my first time to ask a question here so please let me know how to improve my question.

There's a number of quantifier examples in the Java example that comes with Z3: https://github.com/Z3Prover/z3/blob/master/examples/java/JavaExample.java#L597-L767

Reading through them can give you an idea.

If you are new to z3 and programming, I'd strongly recommend against using the Java or C++ APIs. They are rather cumbersome, filled with lots of details that as a newcomer you neither need nor care about. Instead, see if you can play with its Python interface, which is much more lightweight and easy to play around with. Your example, for instance, can be coded rather succinctly like this:

from z3 import *

s = Solver()
x, y = Ints('x y')

s.add(Not(ForAll([y], Implies(Or([y == 2, y == 4, y == 6, y == 8]), Exists([x], x > y)))))
print(s.sexpr())
print(s.check())

(Note that in SMT, like in resolution theorem proving, you assert the negation of what you want to prove and check if the result is unsat . If so, you have proved your assertion. Hence the wrapping of Not in the above example.)

When I run this, I get:

(assert (let ((a!1 (forall ((y Int))
             (=> (or (= y 2) (= y 4) (= y 6) (= y 8))
                 (exists ((x Int)) (> x y))))))
  (not a!1)))

unsat

which also shows you how you'd code the same in SMTLib.

I should also point out that SMT solvers are usually not good for reasoning with quantifiers in general. The above example is simple enough that z3 can easily handle it, but where SMT solvers shine is usually quantifier-free combinations of arithmetic, bit-vectors, booleans, and uninterpreted values and functions. Here's a great tutorial to get you started on the basics: https://ericpony.github.io/z3py-tutorial/guide-examples.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM