Ejemplo n.º 1
0
    def test_annotations(self):
        x = Symbol('x')
        x_next = Symbol('x.next')
        f = Iff(x, Not(x_next))

        ann = Annotations()
        ann.add(x, 'next', x_next.symbol_name())
        ann.add(f, 'trans', 'true')
        ann.add(x, 'init', 'true')

        tree_buf = StringIO()
        dag_buf = StringIO()
        tree_printer = SmtPrinter(tree_buf, annotations=ann)
        dag_printer = SmtDagPrinter(dag_buf, annotations=ann)

        dag_printer.printer(f)
        tree_printer.printer(f)

        self.assertEqual(
            tree_buf.getvalue(),
            "(! (= (! x :next x.next :init true) (not x.next)) :trans true)")
        self.assertEqual(
            dag_buf.getvalue(),
            "(let ((.def_0 (not x.next))) (let ((.def_1 (= (! x :next x.next :init true) .def_0))) (! .def_1 :trans true)))"
        )
Ejemplo n.º 2
0
 def mkInit(self, inv, inv_args, init_def):
     init_args = init_def[0]
     init_name = init_def[3]
     # check that the number of inv_args and init_args are the same
     assert inv_args == len(init_args), 'args(%s) != args(%s)' % (inv,
                                                                  init_name)
     init_body = init_def[2]
     args_str = ' '.join(str(x) for x in init_args)
     buf_out = cStringIO.StringIO()
     p = SmtPrinter(buf_out)
     p.printer(init_body)
     body_str = buf_out.getvalue()
     rule = "(rule (=> %s  (%s %s)))" % (body_str, inv, args_str)
     return rule
Ejemplo n.º 3
0
 def mkProp(self, inv, inv_args, prop_def):
     prop_name = prop_def[3]
     prop_args = prop_def[0]
     # check that the number of inv_args*2 and init_args are the same
     assert (inv_args
             ) == len(prop_args), 'args(%s) != args(%s)' % (inv, prop_name)
     # (rule (=> (and (str_invariant prop_args) (not prop_body)) ERROR))
     prop_body = prop_def[2]
     arg_str = ' '.join(str(x) for x in prop_args)
     inv_str = "(%s %s)" % (inv, arg_str)
     buf_out = cStringIO.StringIO()
     p = SmtPrinter(buf_out)
     p.printer(prop_body)
     body_str = buf_out.getvalue()
     rule = "(rule (=> (and %s (not %s)) ERROR))" % (inv_str, body_str)
     return rule
Ejemplo n.º 4
0
    def test_daggify(self):
        x = Symbol("x")
        f = And(x, x)
        for _ in range(10):
            f = And(f, f)

        tree_buf = StringIO()
        dag_buf = StringIO()
        tree_printer = SmtPrinter(tree_buf)
        dag_printer = SmtDagPrinter(dag_buf)

        dag_printer.printer(f)
        tree_printer.printer(f)

        short_f_str = dag_buf.getvalue()
        long_f_str = tree_buf.getvalue()
        self.assertTrue(len(short_f_str) < len(long_f_str))
Ejemplo n.º 5
0
    def test_daggify(self):
        x = Symbol("x")
        f = And(x,x)
        for _ in xrange(10):
            f = And(f,f)

        tree_buf = cStringIO()
        dag_buf = cStringIO()
        tree_printer = SmtPrinter(tree_buf)
        dag_printer = SmtDagPrinter(dag_buf)

        dag_printer.printer(f)
        tree_printer.printer(f)

        short_f_str = dag_buf.getvalue()
        long_f_str = tree_buf.getvalue()
        self.assertTrue(len(short_f_str) < len(long_f_str))
Ejemplo n.º 6
0
    def _add_assertion(self, solver, formula, comment=None):
        if not self.config.skip_solving:
            solver.solver.add_assertion(formula)

        if Logger.level(3):
            buf = cStringIO()
            printer = SmtPrinter(buf)
            printer.printer(formula)
            print(buf.getvalue() + "\n")

        if solver.trace_file is not None:
            if comment:
                self._write_smt2_comment(solver, "%s: START" % comment)

            formula_fv = get_free_variables(formula)

            for v in formula_fv:
                if v in solver.smt2vars:
                    continue

                if v.symbol_type() == BOOL:
                    self._write_smt2_log(
                        solver, "(declare-fun %s () Bool)" % (v.symbol_name()))
                elif v.symbol_type().is_array_type():
                    st = v.symbol_type()
                    assert st.index_type.is_bv_type(), "Expecting BV indices"
                    assert st.elem_type.is_bv_type(), "Expecting BV elements"
                    self._write_smt2_log(
                        solver,
                        "(declare-fun %s () (Array (_ BitVec %s) (_ BitVec %s)))"
                        % (v.symbol_name(), st.index_type.width,
                           st.elem_type.width))
                elif v.symbol_type().is_bv_type():
                    self._write_smt2_log(
                        solver, "(declare-fun %s () (_ BitVec %s))" %
                        (v.symbol_name(), v.symbol_type().width))
                else:
                    Logger.error("Unhandled type in smt2 translation")

            self._write_smt2_log(solver, "")

            for v in formula_fv:
                solver.smt2vars.add(v)

            if formula.is_and():
                for f in conjunctive_partition(formula):
                    buf = cStringIO()
                    printer = SmtPrinter(buf)
                    printer.printer(f)
                    self._write_smt2_log(solver,
                                         "(assert %s)" % buf.getvalue())
            else:
                buf = cStringIO()
                printer = SmtPrinter(buf)
                printer.printer(formula)
                self._write_smt2_log(solver, "(assert %s)" % buf.getvalue())

            if comment:
                self._write_smt2_comment(solver, "%s: END" % comment)
Ejemplo n.º 7
0
 def mkTrans(self, inv, inv_args, trans_def):
     trans_name = trans_def[3]
     trans_args = trans_def[0]
     # check that the number of inv_args*2 and init_args are the same
     assert (
         inv_args *
         2) == len(trans_args), 'args(%s) != args(%s)' % (inv, trans_name)
     unprimed_args = trans_args[0:inv_args]
     primed_args = trans_args[inv_args:]
     decl_vars = self.declareVars(primed_args)
     self.allVars.update({'primed_var': decl_vars})
     trans_body = trans_def[2]
     primed_str = ' '.join(str(x) for x in primed_args)
     unprimed_str = ' '.join(str(x) for x in unprimed_args)
     inv_primed = "(%s %s)" % (inv, primed_str)
     inv_unprimed = "(%s %s)" % (inv, unprimed_str)
     buf_out = cStringIO.StringIO()
     p = SmtPrinter(buf_out)
     p.printer(trans_body)
     body_str = buf_out.getvalue()
     rule = "(rule (=> (and %s %s) %s))" % (inv_unprimed, body_str,
                                            inv_primed)
     return rule
Ejemplo n.º 8
0
    def print_to_string(self, formula):
        buf = cStringIO()
        printer = SmtPrinter(buf)
        printer.printer(formula)

        return buf.getvalue()
Ejemplo n.º 9
0
 def __str__(self):
     buf = cStringIO()
     p = SmtPrinter(buf)
     p.printer(self.f)
     return buf.getvalue()
Ejemplo n.º 10
0
    def print_to_string(self, formula):
        buf = cStringIO()
        printer = SmtPrinter(buf)
        printer.printer(formula)

        return buf.getvalue()