Exemple #1
0
def evaluate_command(cmd, solver):
    if cmd.name == smtcmd.SET_INFO:
        return solver.set_info(cmd.args[0], cmd.args[1])

    if cmd.name == smtcmd.SET_OPTION:
        return solver.set_option(cmd.args[0], cmd.args[1])

    elif cmd.name == smtcmd.ASSERT:
        return solver.assert_(cmd.args[0])

    elif cmd.name == smtcmd.CHECK_SAT:
        return solver.check_sat()

    elif cmd.name == smtcmd.RESET_ASSERTIONS:
        return solver.reset_assertions()

    elif cmd.name == smtcmd.GET_VALUE:
        return solver.get_values(cmd.args)

    elif cmd.name == smtcmd.PUSH:
        return solver.push(cmd.args[0])

    elif cmd.name == smtcmd.POP:
        return solver.pop(cmd.args[0])

    elif cmd.name == smtcmd.EXIT:
        return solver.exit()

    elif cmd.name == smtcmd.SET_LOGIC:
        name = cmd.args[0]
        return solver.set_logic(name)

    elif cmd.name == smtcmd.DECLARE_FUN:
        return solver.declare_fun(cmd.args[0])

    elif cmd.name == smtcmd.DECLARE_CONST:
        return solver.declare_const(cmd.args[0])

    elif cmd.name == smtcmd.DEFINE_FUN:
        (var, formals, typename, body) = cmd.args
        return solver.define_fun(var, formals, typename, body)

    elif cmd.name == smtcmd.ECHO:
        print(cmd.args[0])
        return None

    elif cmd.name == smtcmd.CHECK_SAT_ASSUMING:
        return solver.check_sat(cmd.args)

    elif cmd.name == smtcmd.GET_UNSAT_CORE:
        return solver.get_unsat_core()

    elif cmd.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." % cmd.name)
    else:
        raise UnknownSmtLibCommandError(cmd.name)
Exemple #2
0
 def get_command(self, tokens):
     """Builds an SmtLibCommand instance out of a parsed term."""
     while True:
         self.consume_opening(tokens, "<main>")
         current = tokens.consume()
         if current in self.commands:
             fun = self.commands[current]
             yield fun(current, tokens)
         else:
             raise UnknownSmtLibCommandError(current)
Exemple #3
0
def evaluate_command(cmd, solver):
    if cmd.name == smtcmd.SET_INFO:
        return solver.set_info(cmd.args[0], cmd.args[1])

    if cmd.name == smtcmd.SET_OPTION:
        return solver.set_option(cmd.args[0], cmd.args[1])

    elif cmd.name == smtcmd.ASSERT:
        return solver.assert_(cmd.args[0])

    elif cmd.name == smtcmd.CHECK_SAT:
        return solver.check_sat()

    elif cmd.name == smtcmd.RESET_ASSERTIONS:
        return solver.reset_assertions()

    elif cmd.name == smtcmd.GET_VALUE:
        return solver.get_values(cmd.args)

    elif cmd.name == smtcmd.PUSH:
        return solver.push(cmd.args[0])

    elif cmd.name == smtcmd.POP:
        return solver.pop(cmd.args[0])

    elif cmd.name == smtcmd.EXIT:
        return solver.exit()

    elif cmd.name == smtcmd.SET_LOGIC:
        name = cmd.args[0]
        return solver.set_logic(name)

    elif cmd.name == smtcmd.DECLARE_FUN:
        return solver.declare_fun(cmd.args[0])

    elif cmd.name == smtcmd.DECLARE_CONST:
        return solver.declare_const(cmd.args[0])

    elif cmd.name == smtcmd.DEFINE_FUN:
        (var, formals, typename, body) = cmd.args
        return solver.define_fun(var, formals, typename, body)

    else:
        raise UnknownSmtLibCommandError(cmd.name)
Exemple #4
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 #5
0
    def get_command(self, tokens):
        """Builds an SmtLibCommand instance out of a parsed term."""

        while True:
            self.consume_opening(tokens, "<main>")
            current = next(tokens)
            if current in [smtcmd.SET_INFO, smtcmd.SET_OPTION]:
                elements = self.parse_atoms(tokens, current, 2)
                yield SmtLibCommand(current, elements)

            elif current == smtcmd.ASSERT:
                expr = self.get_expression(tokens)
                self.consume_closing(tokens, current)
                yield SmtLibCommand(current, [expr])

            elif current == smtcmd.CHECK_SAT:
                self.parse_atoms(tokens, current, 0)
                yield SmtLibCommand(current, [])

            elif current == smtcmd.PUSH:
                elements = self.parse_atoms(tokens, current, 0, 1)

                levels = 1
                if len(elements) > 0:
                    levels = int(elements[0])
                yield SmtLibCommand(current, [levels])

            elif current == smtcmd.POP:
                elements = self.parse_atoms(tokens, current, 0, 1)

                levels = 1
                if len(elements) > 0:
                    levels = int(elements[0])
                yield SmtLibCommand(current, [levels])

            elif current == smtcmd.EXIT:
                self.parse_atoms(tokens, current, 0)
                yield SmtLibCommand(current, [])

            elif current == smtcmd.SET_LOGIC:
                elements = self.parse_atoms(tokens, current, 1)

                name = elements[0]
                try:
                    self.logic = get_logic_by_name(name)
                    yield SmtLibCommand(current, [self.logic])
                except UndefinedLogicError:
                    warn("Unknown logic '" + name + \
                         "'. Ignoring set-logic command.")
                    yield SmtLibCommand(current, [None])

            elif current == smtcmd.DECLARE_CONST:
                elements = self.parse_atoms(tokens, current, 2)

                (var, typename) = elements
                v = self._get_var(var, typename)
                self.cache.bind(var, v)
                yield SmtLibCommand(current, [v])

            elif current == smtcmd.GET_VALUE:
                params = self.parse_expr_list(tokens, current)
                self.consume_closing(tokens, current)
                yield SmtLibCommand(current, params)

            elif current == smtcmd.DECLARE_FUN:
                var = self.parse_atom(tokens, current)
                params = self.parse_params(tokens, current)
                typename = self.parse_atom(tokens, current)
                self.consume_closing(tokens, current)

                v = self._get_var(var, typename, params)
                if v.symbol_type().is_function_type():
                    self.cache.bind(var, \
                            functools.partial(self._function_call_helper, v))
                else:
                    self.cache.bind(var, v)
                yield SmtLibCommand(current, [v])

            elif current == smtcmd.DEFINE_FUN:
                var = self.parse_atom(tokens, current)
                params = self.parse_params(tokens, current)
                typename = self.parse_atom(tokens, current)
                ebody = self.get_expression(tokens)
                self.consume_closing(tokens, current)

                formal = []
                for k in params:
                    (x, t) = k[0], k[1]
                    v = self._get_var(x, t)
                    self.cache.bind(x, v)
                    formal.append(v)

                for x in formal:
                    self.cache.unbind(x.symbol_name())

                self.cache.define(var, formal, ebody)
                yield SmtLibCommand(current, [var, formal, ebody])

            else:
                raise UnknownSmtLibCommandError(current)