Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z3: how to encode If-the-else in Z3 python?

Tags:

python

z3

I want to encode If-the-else in Z3 python, but cannot find any docs or sample on how to do that.

I have a sample code like below.

F = True
tmp = BitVec('tmp', 1)
tmp1 = BitVec('tmp1', 8)

Now how can I encode this condition into F:

if tmp == 1, then tmp1 == 100. otherwise, tmp1 == 0 

Thanks so much.

like image 698
user311703 Avatar asked Dec 07 '22 08:12

user311703


2 Answers

You'll need Z3's If function:

def z3py.If   (       a,
          b,
          c,
          ctx = None 
  )   

Create a Z3 if-then-else expression.

>>> x = Int('x')
>>> y = Int('y')
>>> max = If(x > y, x, y)
>>> max
If(x > y, x, y)
>>> simplify(max)
If(x <= y, y, x)

(from here)

like image 65
Ilmo Euro Avatar answered Dec 23 '22 08:12

Ilmo Euro


You can use If for this. If takes three arguments: the condition, an expression that should be true if the condition is true and an expression that should be true if the condition is false. So to express your logic, you'd write:

If(tmp==1, tmp1==100, tmp1==0)
like image 29
sepp2k Avatar answered Dec 23 '22 08:12

sepp2k