Beispiel #1
0
    def add_assertion(self, formula, named=None):
        self._assert_is_boolean(formula)
        term = self.converter.convert(formula)

        res = mathsat.msat_assert_formula(self.msat_env, term)

        if res != 0:
            msat_msg = mathsat.msat_last_error_message(self.msat_env)
            raise InternalSolverError(msat_msg)
        return
Beispiel #2
0
    def __init__(self, environment, msat_env):
        Model.__init__(self, environment)
        self.msat_env = msat_env
        self.converter = MSatConverter(environment, self.msat_env)
        self.msat_model = None

        msat_model = mathsat.msat_get_model(self.msat_env())
        if mathsat.MSAT_ERROR_MODEL(msat_model):
            msat_msg = mathsat.msat_last_error_message(self.msat_env())
            raise InternalSolverError(msat_msg)
        self.msat_model = msat_model
Beispiel #3
0
    def convert(self, formula):
        """Convert a PySMT formula into a MathSat Term.

        This function might throw a InternalSolverError exception if
        an error during conversion occurs.
        """
        res = self.walk(formula)
        if mathsat.MSAT_ERROR_TERM(res):
            msat_msg = mathsat.msat_last_error_message(self.msat_env)
            raise InternalSolverError(msat_msg)
        return res
Beispiel #4
0
    def convert(self, formula):
        """Convert a PySMT formula into a MathSat Term.

        This function might throw a InternalSolverError exception if
        an error during conversion occurs.
        """
        res = self.walk(formula)
        if mathsat.MSAT_ERROR_TERM(res):
            msat_msg = mathsat.msat_last_error_message(self.msat_env)
            raise InternalSolverError(msat_msg)
        return res
Beispiel #5
0
 def declare_variable(self, var):
     if not var.is_symbol(): raise TypeError(var)
     if var.symbol_name() not in self.symbol_to_decl:
         tp = self._type_to_msat(var.symbol_type())
         decl = mathsat.msat_declare_function(self.msat_env(),
                                              var.symbol_name(),
                                              tp)
         if mathsat.MSAT_ERROR_DECL(decl):
             msat_msg = mathsat.msat_last_error_message(self.msat_env())
             raise InternalSolverError(msat_msg)
         self.symbol_to_decl[var] = decl
         self.decl_to_symbol[mathsat.msat_decl_id(decl)] = var
Beispiel #6
0
    def get_unsat_core(self):
        """After a call to solve() yielding UNSAT, returns the unsat core as a
        set of formulae"""
        if self.options.unsat_cores_mode == "all":
            self._check_unsat_core_config()

            terms = mathsat.msat_get_unsat_core(self.msat_env())
            if terms is None:
                raise InternalSolverError(
                    mathsat.msat_last_error_message(self.msat_env()))
            return set(self.converter.back(t) for t in terms)
        else:
            return self.get_named_unsat_core().values()
Beispiel #7
0
    def get_unsat_core(self):
        """After a call to solve() yielding UNSAT, returns the unsat core as a
        set of formulae"""
        if self.options.unsat_cores_mode == "all":
            self._check_unsat_core_config()

            terms = mathsat.msat_get_unsat_core(self.msat_env)
            if terms is None:
                raise InternalSolverError(
                    mathsat.msat_last_error_message(self.msat_env))
            return set(self.converter.back(t) for t in terms)
        else:
            return self.get_named_unsat_core().values()
Beispiel #8
0
    def convert(self, formula):
        """Convert a PySMT formula into a MathSat Term.

        This function might throw a InternalSolverError exception if
        an error during conversion occurs.
        """
        # Rewrite to avoid UF with bool args
        rformula = self._ufrewriter.walk(formula)
        res = self.walk(rformula)
        if mathsat.MSAT_ERROR_TERM(res):
            msat_msg = mathsat.msat_last_error_message(self.msat_env())
            raise InternalSolverError(msat_msg)
        if rformula != formula:
            warn("MathSAT convert(): UF with bool arguments have been translated")
        return res
Beispiel #9
0
    def _add_assertion(self, formula, named=None):
        self._assert_is_boolean(formula)

        result = formula
        if self.options.unsat_cores_mode == "named":
            # If we want named unsat cores, we need to rewrite the
            # formulae as implications
            key = self.mgr.FreshSymbol(template="_assertion_%d")
            result = (key, named, formula)
            formula = self.mgr.Implies(key, formula)

        term = self.converter.convert(formula)
        res = mathsat.msat_assert_formula(self.msat_env(), term)

        if res != 0:
            msat_msg = mathsat.msat_last_error_message(self.msat_env())
            raise InternalSolverError(msat_msg)

        return result
Beispiel #10
0
    def sequence_interpolant(self, formulas):
        cfg, env = None, None
        try:
            self._check_logic(formulas)

            if len(formulas) < 2:
                raise Exception("interpolation needs at least 2 formulae")

            cfg = mathsat.msat_create_config()
            mathsat.msat_set_option(cfg, "interpolation", "true")
            if self.logic == QF_BV:
                mathsat.msat_set_option(cfg, "theory.bv.eager", "false")
                mathsat.msat_set_option(cfg, "theory.eq_propagaion", "false")
            env = mathsat.msat_create_env(cfg, self.msat_env())

            groups = []
            for f in formulas:
                f = self.converter.convert(f)
                g = mathsat.msat_create_itp_group(env)
                mathsat.msat_set_itp_group(env, g)
                groups.append(g)
                mathsat.msat_assert_formula(env, f)

            res = mathsat.msat_solve(env)
            if res == mathsat.MSAT_UNKNOWN:
                raise Exception("error in mathsat interpolation: %s" %
                                mathsat.msat_last_error_message(env))

            if res == mathsat.MSAT_SAT:
                return None

            pysmt_ret = []
            for i in xrange(1, len(groups)):
                itp = mathsat.msat_get_interpolant(env, groups[:i])
                f = self.converter.back(itp)
                pysmt_ret.append(f)

            return pysmt_ret
        finally:
            if cfg:
                mathsat.msat_destroy_config(cfg)
            if env:
                mathsat.msat_destroy_env(env)
Beispiel #11
0
    def _add_assertion(self, formula, named=None):
        self._assert_is_boolean(formula)

        result = formula
        if self.options.unsat_cores_mode == "named":
            # If we want named unsat cores, we need to rewrite the
            # formulae as implications
            key = self.mgr.FreshSymbol(template="_assertion_%d")
            result = (key, named, formula)
            formula = self.mgr.Implies(key, formula)

        term = self.converter.convert(formula)
        res = mathsat.msat_assert_formula(self.msat_env, term)

        if res != 0:
            msat_msg = mathsat.msat_last_error_message(self.msat_env)
            raise InternalSolverError(msat_msg)

        return result
Beispiel #12
0
    def sequence_interpolant(self, formulas):
        cfg, env = None, None
        try:
            self._check_logic(formulas)

            if len(formulas) < 2:
                raise Exception("interpolation needs at least 2 formulae")

            cfg = mathsat.msat_create_config()
            mathsat.msat_set_option(cfg, "interpolation", "true")
            if self.logic == QF_BV:
                mathsat.msat_set_option(cfg, "theory.bv.eager", "false")
                mathsat.msat_set_option(cfg, "theory.eq_propagaion", "false")
            env = mathsat.msat_create_env(cfg, self.msat_env)

            groups = []
            for f in formulas:
                f = self.converter.convert(f)
                g = mathsat.msat_create_itp_group(env)
                mathsat.msat_set_itp_group(env, g)
                groups.append(g)
                mathsat.msat_assert_formula(env, f)

            res = mathsat.msat_solve(env)
            if res == mathsat.MSAT_UNKNOWN:
                raise Exception("error in mathsat interpolation: %s" %
                                mathsat.msat_last_error_message(env))

            if res == mathsat.MSAT_SAT:
                return None

            pysmt_ret = []
            for i in xrange(1, len(groups)):
                itp = mathsat.msat_get_interpolant(env, groups[:i])
                f = self.converter.back(itp)
                pysmt_ret.append(f)

            return pysmt_ret
        finally:
            if cfg:
                mathsat.msat_destroy_config(cfg)
            if env:
                mathsat.msat_destroy_env(env)
Beispiel #13
0
 def _type_to_msat(self, tp):
     """Convert a pySMT type into a MathSAT type."""
     if tp.is_bool_type():
         return self.boolType
     elif tp.is_real_type():
         return self.realType
     elif tp.is_int_type():
         return self.intType
     elif tp.is_function_type():
         stps = [self._type_to_msat(x) for x in tp.param_types]
         rtp = self._type_to_msat(tp.return_type)
         msat_type = mathsat.msat_get_function_type(self.msat_env(),
                                                    stps,
                                                    rtp)
         if mathsat.MSAT_ERROR_TYPE(msat_type):
             msat_msg = mathsat.msat_last_error_message(self.msat_env())
             raise InternalSolverError(msat_msg)
         return msat_type
     else:
         assert tp.is_bv_type(), "Usupported type for '%s'" % tp
         return mathsat.msat_get_bv_type(self.msat_env(), tp.width)