2

I want to implement a constraint which requires AND logic in Z3Py. Suppose we have two variables a and b, I just want to add one constraint which requires a == 0 and b == 1. There should be several ways which can do this in Z3, like s.add(a == 0, b == 0) or s.add(And(a==0, b==0). However, I tried one method which is s.add(a == 0 and b == 0). This method doesn't work, the code is:

from z3 import *

a = Int('a')
b = Int('b')

s = Solver()

#s.add(a == 0, b ==1)
#s.add(And(a == 0, b == 1))
s.add(a == 0 and b == 1)

print(s.check())
print(s.model())

The output of this file is a = 0, which means b is ignored... Can someone explain why does this happen? It seems And(a == 0, b == 1) and a == 0 and b == 1 are different in Z3Py.

Francis
  • 189
  • 1
  • 11
  • On my system `a == 0` is ignored and `b == 1` is retained. I tested this by conjoining an unsatisfiable constraint. `And()` is clearly a `z3` -defined function which joins together terms. It doesn't look like `and` is overloaded within `z3py` library for the same purpose, though I could have missed it in the docs. If this is the case, then `and` is the regular logical operator of `python`.. why it behaves this way I could not say, though. – Patrick Trentin Jul 31 '18 at 21:15
  • 1
    Yes, Python does not allow overloading for some operators. Also the "and" operator doesn't return a Boolean value, but the last value that was evaluated; see https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not – Christoph Wintersteiger Aug 01 '18 at 10:37
  • If you want an AND logic, so you're doing Boolean algebra. Why then creating integer variables... from a string? – Aster Apr 21 '23 at 12:10

0 Answers0