Exemple #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)))"
        )
Exemple #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
Exemple #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
Exemple #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))
Exemple #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))
Exemple #6
0
    def serialize(self, outstream=None, printer=None, daggify=False):
        """Serializes the SmtLibCommand into outstream using the given printer.

        Exactly one of outstream or printer must be specified. When
        specifying the printer, the associated outstream will be used.
        If printer is not specified, daggify controls the printer to
        be created. If true a daggified formula is produced, otherwise
        a tree printing is done.

        """

        if (outstream is None) and (printer is not None):
            outstream = printer.stream
        elif (outstream is not None) and (printer is None):
            if daggify:
                printer = SmtDagPrinter(outstream)
            else:
                printer = SmtPrinter(outstream)
        else:
            assert (outstream is not None and printer is not None) or \
                   (outstream is None and printer is None), \
                   "Exactly one of outstream and printer must be set."

        if self.name in [smtcmd.SET_OPTION, smtcmd.SET_INFO]:
            outstream.write("(%s %s %s)" %
                            (self.name, self.args[0], self.args[1]))

        elif self.name == smtcmd.ASSERT:
            outstream.write("(%s " % self.name)
            printer.printer(self.args[0])
            outstream.write(")")

        elif self.name == smtcmd.GET_VALUE:
            outstream.write("(%s (" % self.name)
            for a in self.args:
                printer.printer(a)
                outstream.write(" ")
            outstream.write("))")

        elif self.name in [
                smtcmd.CHECK_SAT, smtcmd.EXIT, smtcmd.RESET_ASSERTIONS
        ]:
            outstream.write("(%s)" % self.name)

        elif self.name == smtcmd.SET_LOGIC:
            outstream.write("(%s %s)" % (self.name, self.args[0]))

        elif self.name in [smtcmd.DECLARE_FUN, smtcmd.DECLARE_CONST]:
            symbol = self.args[0]
            type_str = symbol.symbol_type().as_smtlib()
            outstream.write("(%s %s %s)" %
                            (self.name, symbol.symbol_name(), type_str))

        elif self.name == smtcmd.DEFINE_FUN:
            raise NotImplementedError

        elif self.name in [smtcmd.PUSH, smtcmd.POP]:
            outstream.write("(%s %d)" % (self.name, self.args[0]))
Exemple #7
0
    def serialize(self, outstream, daggify=False):
        """Serializes the SmtLibScript expanding commands"""
        if daggify:
            printer = SmtDagPrinter(outstream)
        else:
            printer = SmtPrinter(outstream)

        for cmd in self.commands:
            cmd.serialize(printer=printer)
            outstream.write("\n")
Exemple #8
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
Exemple #9
0
    def serialize(self, outstream=None, printer=None, daggify=True):
        """Serializes the SmtLibCommand into outstream using the given printer.

        Exactly one of outstream or printer must be specified. When
        specifying the printer, the associated outstream will be used.
        If printer is not specified, daggify controls the printer to
        be created. If true a daggified formula is produced, otherwise
        a tree printing is done.

        """

        if (outstream is None) and (printer is not None):
            outstream = printer.stream
        elif (outstream is not None) and (printer is None):
            if daggify:
                printer = SmtDagPrinter(outstream)
            else:
                printer = SmtPrinter(outstream)
        else:
            assert (outstream is not None and printer is not None) or \
                   (outstream is None and printer is None), \
                   "Exactly one of outstream and printer must be set."

        if self.name == smtcmd.SET_OPTION:
            outstream.write("(%s %s %s)" %
                            (self.name, self.args[0], self.args[1]))

        elif self.name == smtcmd.SET_INFO:
            outstream.write("(%s %s %s)" %
                            (self.name, self.args[0], quote(self.args[1])))

        elif self.name == smtcmd.ASSERT:
            outstream.write("(%s " % self.name)
            printer.printer(self.args[0])
            outstream.write(")")

        elif self.name == smtcmd.GET_VALUE:
            outstream.write("(%s (" % self.name)
            for a in self.args:
                printer.printer(a)
                outstream.write(" ")
            outstream.write("))")

        elif self.name in [
                smtcmd.CHECK_SAT, smtcmd.EXIT, smtcmd.RESET_ASSERTIONS
        ]:
            outstream.write("(%s)" % self.name)

        elif self.name == smtcmd.SET_LOGIC:
            outstream.write("(%s %s)" % (self.name, self.args[0]))

        elif self.name in [smtcmd.DECLARE_FUN, smtcmd.DECLARE_CONST]:
            symbol = self.args[0]
            type_str = symbol.symbol_type().as_smtlib()
            outstream.write("(%s %s %s)" %
                            (self.name, quote(symbol.symbol_name()), type_str))

        elif self.name == smtcmd.DEFINE_FUN:
            name = self.args[0]
            params_list = self.args[1]
            params = " ".join(
                ["(%s %s)" % (v, v.symbol_type()) for v in params_list])
            rtype = self.args[2]
            expr = self.args[3]
            outstream.write("(%s %s (%s) %s " %
                            (self.name, name, params, rtype))
            printer.printer(expr)
            outstream.write(")")

        elif self.name in [smtcmd.PUSH, smtcmd.POP]:
            outstream.write("(%s %d)" % (self.name, self.args[0]))

        elif self.name == smtcmd.DEFINE_SORT:
            name = self.args[0]
            params_list = self.args[1]
            params = " ".join(params_list)
            rtype = self.args[2]
            outstream.write("(%s %s (%s) %s)" %
                            (self.name, name, params, rtype))

        elif self.name in smtcmd.ALL_COMMANDS:
            raise NotImplementedError("'%s' is a valid SMT-LIB command "\
                                      "but it is currently not supported. "\
                                      "Please open a bug-report." % self.name)
        else:
            raise UnknownSmtLibCommandError(self.name)
Exemple #10
0
IdentityDagWalker.set_handler(walk_sin, SIN)
IdentityDagWalker.set_handler(walk_cos, COS)

from pysmt.smtlib.printers import SmtPrinter


def walk_sin(self, formula):
    return self.walk_nary(formula, "sin")


def walk_cos(self, formula):
    return self.walk_nary(formula, "cos")


SmtPrinter.set_handler(walk_sin, SIN)
SmtPrinter.set_handler(walk_cos, COS)

#smtlib parser
from pysmt.smtlib.parser import SmtLibParser


class ExtendedSmtLibParser(SmtLibParser):
    def __init__(self, environment=None, interactive=False):
        super().__init__(environment, interactive)
        mgr = self.env.formula_manager
        self.interpreted[TRANS_TYPE_TO_STR[SIN]] = self._operator_adapter(
            mgr.Sin)
        self.interpreted[TRANS_TYPE_TO_STR[COS]] = self._operator_adapter(
            mgr.Cos)
Exemple #11
0
    def print_to_string(self, formula):
        buf = cStringIO()
        printer = SmtPrinter(buf)
        printer.printer(formula)

        return buf.getvalue()
Exemple #12
0
 def __str__(self):
     buf = cStringIO()
     p = SmtPrinter(buf)
     p.printer(self.f)
     return buf.getvalue()
Exemple #13
0
    def print_to_string(self, formula):
        buf = cStringIO()
        printer = SmtPrinter(buf)
        printer.printer(formula)

        return buf.getvalue()
Exemple #14
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)