Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z3: convert Z3py expression to SMT-LIB2?

Tags:

z3

Given an expression in Z3py, can I convert that to SMT-LIB2 language? (So I can feed this SMT-LIB2 expression to other SMT solvers that support SMT-LIB2)

If this is possible, please give one example.

Thanks a lot.

like image 227
user311703 Avatar asked Jan 31 '13 14:01

user311703


1 Answers

We can use the C API Z3_benchmark_to_smtlib_string. Every function in the C API is available in Z3Py. This function was initially used to dump benchmarks in SMT 1.0 format, and it predates SMT 2.0. That is why it has some parameters that may seem unnecessary. Now, by default, it will display benchmarks in SMT 2.0 format. The output is not meant to be human readable. We can write the following Python function to make it more convenient to use:

def toSMT2Benchmark(f, status="unknown", name="benchmark", logic=""):
  v = (Ast * 0)()
  return Z3_benchmark_to_smtlib_string(f.ctx_ref(), name, logic, status, "", 0, v, f.as_ast())

Here is a small example using it (also available online here)

a = Int('a')
b = Int('b')
f = And(Or(a > If(b > 0, -b, b) + 1, a <= 0), b > 0)
print toSMT2Benchmark(f, logic="QF_LIA")
like image 166
Leonardo de Moura Avatar answered Oct 25 '22 19:10

Leonardo de Moura