Z3 with string expressions

前端 未结 1 1648
醉酒成梦
醉酒成梦 2020-12-21 17:00

I\'m trying to use Z3 to determine if an expression is satisfiable. I could easily do this by defining the context then the int_const variables and the formula. To evaluate

相关标签:
1条回答
  • 2020-12-21 17:50

    I see the following options:

    1) you can implement your own parser, and invoke the Z3 API functions. Pro: you can use your "favorite" language for writing formulas. Con: it is "busy" work.

    2) you can use the API Z3_parse_smtlib2_string. Con: your formulas must be in SMT 2.0 format. For example, you would have to write (and (= x y) (not (= x y))) instead of (x == y) && !(x == z).

    3) You can use the Z3 Python API, and parse strings using the eval function in Python. Here is an example:

    from z3 import *
    # Creating x, y 
    x = Int('x')
    y = Int('y')
    
    # Creating the formula using Python
    f = And(x == y, Not(x == y))
    print f
    
    # Using eval to parse the string.
    s = "And(x == y, Not(x == y))"
    f2 = eval(s)
    print f2
    

    BTW, this script does not work at rise4fun http://rise4fun.com/z3py because the function eval is not allowed there, but you can use the script above in your local Z3 installation.

    0 讨论(0)
提交回复
热议问题