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

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

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

+3
source share
1 answer

The class Solverhas a method called assertions(). It returns all formulas approved in the given solver. After extracting the statements, we can use the same approach as in the previous question. Here is a quick and dirty modification

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 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. script SMTLIB 1.x( ). , SMTLIB 1.x , . 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

+5

Source: https://habr.com/ru/post/1533230/


All Articles