Evaluate a Z3 expression - z3

I have a constraint in Z3py, say,
z3.Real('x')<=3
Is there a simple, built-in way to evaluate the constraint, to 'true' or 'false' for a potential model? For example, evaluating the constraint with z3.Real('x')->2 should give 'true'.

You could either evaluate expressions under a model that can be extracted when Solver.check returns sat (retrieve the model using Solver.model()), or you can represent your own model as a substitution (when the model only mentions constants), and then substitute the values from the model into the expression using the 'substitute' method, then use 'simplify' to evaluate the resulting expression.

Related

Is it possible to express rising/falling edge operators as SAT/SMT formula?

I am working on a satisfiability check for transition conditions of GRAFCET Diagrams (which is used to model the behaviour of a programmable logic controller). For this purpose I am using the Z3 SMT Solver.
In addition to normal operators (AND, OR, NOT and EQUALITY) the GRAFCET specification allows RISING and FALLING EDGE operators in its conditions.
Exemple: ↑a (RISING EDGE)
Explanation: The conditions is statisfied if the variable a changes its value from FALSE to TRUE.
My first thought would be to check, if there is a variable combination that statisfies a and also a variable combination that statisfies NOT(a). This way I could proof that the RISING EDGE could possibly occure.
[Q]: Is it possible to translate these operators directly in propositional logic or somthing similar to check satisfiablity in one forumula.
Raising/falling edges suggests change over time. In a SAT/SMT context, variables do not change. To model what you want, you’ll have to capture the value in successive points in different variables and check that the first is False and second is True for raising, etc.
You can also use an array indexed by an integer to represent the value. It all depends on how you translate these diagrams to SAT. In any case, the value of each variable will be constant in the model. (That is, checking a and Not(a) at the same time will always be unsatisfiable.)

Display all values from Z3 model (Python)

When I want to get the values of all variables in an SMT2 instance, I use the command (set-option :auto-config false). In Z3py, setting this option doesn't work - the model doesn't display variables that I define but do not use in any constraints. If I ask for the values of these variables, I get None. I have tried these options but none of them produce the result that I want:
set_option('model_evaluator.completion', True)
set_option('smt.auto-config', False)
set_option('auto-config', False)
What should I do to get concrete values for these variables?
The auto-config option does not tell Z3 to print or omit parts of models; it just enables or disables automatic configuration of the solver (based on static formula features).
When a variable is not assigned a value in a model, it is simply irrelevant, i.e., you can make up any value for it, and it will still be a correct model. Depending on which solvers/tactics you use, the option model_evaluator.completion may solve that problem, but the safest way is to enable model completion at the time of model evaluation, i.e., use the eval(..) function with model_completion=True

How can I represent integer infinity in Z3?

I need to be able to compare two integer expressions, which may include literal integers, addition, unary negation, integer constants, and infinity, and decide if an inequality between them is satisfiable. This is part of a larger program, so there is no way for me to know ahead of time what those expressions will look like.
I have considered defining an integer constant and just letting it take any value, but then I realized that Infinity < 5 would be satisfiable.
I have considered defining a constant and making a universally quantified assertion that it is greater than all integers, but I don't know what Sort I should say it is. If I tell Z3 that the Sort of my Infinity constant is integer, I think it will probably happily go off and try to find me THE LARGEST INTEGER! I'm pretty sure that won't end the way I want.
I would create a composite type consisting of an integer and a boolean that says whether this particular value is infinity or not. Then, you need to define arithmetic on this. For example, if one of the operands is infinity then the result of an addition is infinity as well. Otherwise, it's the actual sum of the integers. Comparisons would be defined in a similar way (case based).
There is no need to actually create a type in the Z3 system. Just always create values in int/bool pairs in your code. A few helper functions can do that.
Doing this you will probably create a harder problem for Z3 to solve and maybe even escalate the logic you are using.

Fastest way to check if a Z3 model fulfills constraints? BitVectors, Z3 3.2, C# API, x64, multi-threaded

Given a set of constraints (assertions) in Z3 I am wondering what is the most efficient way of checking if a model that I already have fulfills these assertions. The model was obtained from similar set of constraints. I require a yes/no answer, not a soft constraint as in Specifying initial model values for Z3.
I am operating on bit vectors using x64 version of Z3 3.2, using C# API on Windows 7 x64. I am multi-threading by instantiating multiple Z3 Context objects, one per thread. I am not using Z3 4.0 due to lack of support for multi-threading.
My current approach is just to assert the model as an additional set of constraints using Context.AssertCnstr(Term) and then simply calling Context.Check().
Z3 exposes a method called "Z3_model_eval" or "Model.Eval" (from C#)
that takes a model and an expression. Evaluation may fail if the
expression contains quantifiers and the evaluator cannot determine
the truth value of the quantified formula modulo the model.
If model evaluation succeeds, you can inspect the returned value to determine
whether the model forces the assertions to be true.
The documentation for Z3_model_eval details the contract in more detail:
http://research.microsoft.com/en-us/um/redmond/projects/z3/group__capi.html#ga86670c291a16640b932e7892176a9d1b
Model evaluation will not detect tautologies, so serializing a model as a
formula and having Z3 check the implication between the model and assertions
may be more appropriate for some uses.

models with uninterpreted sorts

When Z3 returns a model with an uninterpreted sort, say Q, it uses constants of the form Q!val!0.
However, if I create a query from scratch and refer to this symbol as an inhabitant of Q, then Z3 rightfully complains that Q!val!0 is an unknown constant.
Essentially, I'm trying to get Z3 to enumerate all the inhabitants of Q, by asking it to give me a model that's not the same as the previously returned one. Hence, in subsequent calls to Z3, I need to refer to these constants.
Is there a way to do this using the SMT-Lib2 interface?

Resources