Read func interp of a z3 array from the z3 model

The representation for array models is indeed a bit confusing. The meaning of (define-fun some_array_1 () (Array Int Int) (_ as-array k!0)) is that the model for array some_array_1 is the function k!0 which is to be interpreted as an array (signified by the call to as-array. The latter is a parametric function, which has … Read more

Equivalent of define-fun in Z3 API

The define-fun command is just creating a macro. Note that the SMT 2.0 standard doesn’t allow recursive definitions. Z3 will expand every occurrence of my-div during parsing time. The command define-fun may be used to make the input file simpler and easier to read, but internally it does not really help Z3. In the current … Read more

Z3 support for nonlinear arithmetic

Z3 supports nonlinear polynomial Real arithmetic. So, there is no support for transcendental functions (e.g., sine and cosine), and exponential (e.g., 2^x). Actually, for the exponential, Z3 can handle exponents that can be simplified to numerals. Here is an example, x = Real(‘x’) y = Real(‘y’) solve(y == 3, x**y == 2) In this example, … Read more

K-out-of-N constraint in Z3Py

Yes, Z3Py has built-in support for this. There is an undocumented API for this, that isn’t mentioned in the Z3Py docs: use PbEq. In particular, the expression PbEq(((x1,1),(x2,1),..,(xN,1)),K) will be true if exactly K out of the N boolean variables are set to true. There are some reports that this encoding will be faster than … Read more

Soft/Hard constraints in Z3

Assumptions are available in the SMT 2.0 frontend. They are used to extract unsatisfiable cores. They may be also used to “retract” assumptions. Note that, assumptions are not really “soft constraints”, but they can be used to implement them. See the maxsat example (subdir maxsat) in the Z3 distribution. That being said, here is an … Read more

Z3: finding all satisfying models

One way to accomplish this is using one of the APIs, along with the model generation capability. You can then use the generated model from one satisfiability check to add constraints to prevent previous model values from being used in subsequent satisfiability checks, until there are no more satisfying assignments. Of course, you have to … Read more

How does Z3 handle non-linear integer arithmetic?

Yes, the decision problem for nonlinear integer arithmetic is undecidable. We can encode the Halting problem for Turing machines in nonlinear integer arithmetic. I strongly recommend the beautiful book Hilbert’s tenth problem for anybody interested in this problem. Note that, if a formula has a solution, we can always find it by brute force. That … Read more