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

后端 未结 1 2016
耶瑟儿~
耶瑟儿~ 2020-12-11 10:00

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

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

相关标签:
1条回答
  • 2020-12-11 10:29

    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

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