Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z3: convert Z3py expression to SMT-LIB2 from Solver object

Tags:

z3

This question is very similar to: Z3: convert Z3py expression to SMT-LIB2?

Is it possible to generate SMT-LIB2 output from Solver object ?

like image 383
user1217406 Avatar asked Sep 15 '25 00:09

user1217406


1 Answers

The Solver class has method called assertions(). It returns all formulas asserted into the given solver. After we extract the assertions, we can use the same approach used in the previous question. Here is a quick and dirty modification to

def toSMT2Benchmark(f, status="unknown", name="benchmark", logic=""):
  v = (Ast * 0)()
  if isinstance(f, Solver):
    a = f.assertions()
    if len(a) == 0:
      f = BoolVal(True)
    else:
      f = And(*a)
  return Z3_benchmark_to_smtlib_string(f.ctx_ref(), name, logic, status, "", 0, v, f.as_ast())

Here is an example (also available online at here)

s = Solver()
print toSMT2Benchmark(s, logic="QF_LIA")
a, b = Ints('a b')
s.add(a > 1)
s.add(Or(a < 1, b < 2))
print toSMT2Benchmark(s, logic="QF_LIA")

EDIT We can use the following script to display the output in SMTLIB 1.x format (also available online here). Note that SMTLIB 1.x is very limited, and several features are not supported. We also strongly encourage all users to move to SMTLIB 2.x.

def toSMTBenchmark(f, status="unknown", name="benchmark", logic=""):
  v = (Ast * 0)()
  if isinstance(f, Solver):
    a = f.assertions()
    if len(a) == 0:
      f = BoolVal(True)
    else:
      f = And(*a)
  Z3_set_ast_print_mode(f.ctx_ref(), Z3_PRINT_SMTLIB_COMPLIANT)  # Set SMTLIB 1.x pretty print mode  
  r = Z3_benchmark_to_smtlib_string(f.ctx_ref(), name, logic, status, "", 0, v, f.as_ast())
  Z3_set_ast_print_mode(f.ctx_ref(), Z3_PRINT_SMTLIB2_COMPLIANT) # Restore SMTLIB 2.x pretty print mode
  return r

s = Solver()
print toSMTBenchmark(s, logic="QF_LIA")
a, b = Ints('a b')
s.add(a > 1)
s.add(Or(a < 1, b < 2))
print toSMTBenchmark(s, logic="QF_LIA")

END EDIT

like image 139
Leonardo de Moura Avatar answered Sep 17 '25 18:09

Leonardo de Moura