Exemplo n.º 1
0
 def _cvc4_type_to_type(self, type_):
     if type_.isBoolean():
         return types.BOOL
     elif type_.isInteger():
         return types.INT
     elif type_.isReal():
         return types.REAL
     elif type_.isArray():
         # Casting Type into ArrayType
         type_ = CVC4.ArrayType(type_)
         # Recursively convert the types of index and elem
         idx_type = self._cvc4_type_to_type(type_.getIndexType())
         elem_type = self._cvc4_type_to_type(type_.getConstituentType())
         return types.ArrayType(idx_type, elem_type)
     elif type_.isBitVector():
         # Casting Type into BitVectorType
         type_ = CVC4.BitVectorType(type_)
         return types.BVType(type_.getSize())
     elif type_.isFunction():
         # Casting Type into FunctionType
         type_ = CVC4.FunctionType(type_)
         return_type = type_.getRangeType()
         param_types = tuple(
             self._cvc4_type_to_type(ty) for ty in type_.getArgTypes())
         return types.FunctionType(return_type, param_types)
     else:
         raise NotImplementedError("Unsupported type: %s" % type_)
Exemplo n.º 2
0
 def _convert_sort(self, sort):
     kind = sort.get_sort_kind()
     if kind is ss.sortkinds.ARRAY:
         Kt = self._convert_sort(sort.get_indexsort())
         Vt = self._convert_sort(sort.get_elemsort())
         return pysmt_types.ArrayType(Kt, Vt)
     elif kind is ss.sortkinds.BOOL:
         return pysmt_types.BOOL
     elif kind is ss.sortkinds.BV:
         return pysmt_types.BVType(sort.get_width())
     elif kind is ss.sortkinds.FUNCTION:
         domain = [self._convert_sort(s) for s in sort.get_domain_sorts()]
         codomain = self._convert_sort(sort.get_codomain())
         return pysmt_types.FunctionType(codomain, domain)
     elif kind is ss.sortkinds.INT:
         return pysmt_types.INT
     else:
         assert kind is ss.sortkinds.REAL
         return pysmt_types.REAL
Exemplo n.º 3
0
Arquivo: msat.py Projeto: idkwim/pysmt
    def _get_signature(self, term, args):
        """Returns the signature of the given term.
        For example:
        - a term x & y returns a function type Bool -> Bool -> Bool,
        - a term 14 returns Int
        - a term x ? 13 : 15.0 returns Bool -> Real -> Real -> Real
        """
        res = None

        if mathsat.msat_term_is_true(self.msat_env, term) or \
            mathsat.msat_term_is_false(self.msat_env, term) or \
            mathsat.msat_term_is_boolean_constant(self.msat_env, term):
            res = types.BOOL

        elif mathsat.msat_term_is_number(self.msat_env, term):
            ty = mathsat.msat_term_get_type(term)
            if mathsat.msat_is_integer_type(self.msat_env, ty):
                res = types.INT
            elif mathsat.msat_is_rational_type(self.msat_env, ty):
                res = types.REAL
            else:
                assert "_" in str(
                    term), "Unrecognized type for '%s'" % str(term)
                width = int(str(term).split("_")[1])
                res = types.BVType(width)


        elif mathsat.msat_term_is_and(self.msat_env, term) or \
             mathsat.msat_term_is_or(self.msat_env, term) or \
             mathsat.msat_term_is_iff(self.msat_env, term):
            res = types.FunctionType(types.BOOL, [types.BOOL, types.BOOL])

        elif mathsat.msat_term_is_not(self.msat_env, term):
            res = types.FunctionType(types.BOOL, [types.BOOL])

        elif mathsat.msat_term_is_term_ite(self.msat_env, term):
            t1 = self.env.stc.get_type(args[1])
            t2 = self.env.stc.get_type(args[2])
            t = self._most_generic(t1, t2)
            res = types.FunctionType(t, [types.BOOL, t, t])

        elif mathsat.msat_term_is_equal(self.msat_env, term) or \
             mathsat.msat_term_is_leq(self.msat_env, term):
            t1 = self.env.stc.get_type(args[0])
            t2 = self.env.stc.get_type(args[1])
            t = self._most_generic(t1, t2)
            res = types.FunctionType(types.BOOL, [t, t])

        elif mathsat.msat_term_is_plus(self.msat_env, term) or \
             mathsat.msat_term_is_times(self.msat_env, term):
            t1 = self.env.stc.get_type(args[0])
            t2 = self.env.stc.get_type(args[1])
            t = self._most_generic(t1, t2)
            res = types.FunctionType(t, [t, t])

        elif mathsat.msat_term_is_constant(self.msat_env, term):
            ty = mathsat.msat_term_get_type(term)
            if mathsat.msat_is_rational_type(self.msat_env, ty):
                res = types.REAL
            elif mathsat.msat_is_integer_type(self.msat_env, ty):
                res = types.INT
            else:
                _, width = mathsat.msat_is_bv_type(self.msat_env, ty)
                assert width is not None, "Unsupported type for '%s'" % str(
                    term)
                res = types.BVType(width)

        elif mathsat.msat_term_is_uf(self.msat_env, term):
            d = mathsat.msat_term_get_decl(term)
            fun = self.get_symbol_from_declaration(d)
            res = fun.symbol_type()

        elif mathsat.msat_term_is_bv_times(self.msat_env, term) or \
             mathsat.msat_term_is_bv_plus(self.msat_env, term) or \
             mathsat.msat_term_is_bv_minus(self.msat_env, term) or \
             mathsat.msat_term_is_bv_or(self.msat_env, term) or \
             mathsat.msat_term_is_bv_and(self.msat_env, term) or \
             mathsat.msat_term_is_bv_lshl(self.msat_env, term) or \
             mathsat.msat_term_is_bv_lshr(self.msat_env, term) or \
             mathsat.msat_term_is_bv_ashr(self.msat_env, term) or \
             mathsat.msat_term_is_bv_xor(self.msat_env, term) or \
             mathsat.msat_term_is_bv_urem(self.msat_env, term) or \
             mathsat.msat_term_is_bv_udiv(self.msat_env, term) or \
             mathsat.msat_term_is_bv_sdiv(self.msat_env, term) or \
             mathsat.msat_term_is_bv_srem(self.msat_env, term) or \
             mathsat.msat_term_is_bv_concat(self.msat_env, term):
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(t, [t, t])

        elif mathsat.msat_term_is_bv_not(self.msat_env, term) or \
             mathsat.msat_term_is_bv_neg(self.msat_env, term):
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(t, [t])

        elif mathsat.msat_term_is_bv_ult(self.msat_env, term) or \
             mathsat.msat_term_is_bv_slt(self.msat_env, term) or \
             mathsat.msat_term_is_bv_uleq(self.msat_env, term) or \
             mathsat.msat_term_is_bv_sleq(self.msat_env, term):
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(types.BOOL, [t, t])

        elif mathsat.msat_term_is_bv_comp(self.msat_env, term):
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(types.BVType(1), [t, t])

        elif mathsat.msat_term_is_bv_rol(self.msat_env, term)[0] or \
             mathsat.msat_term_is_bv_ror(self.msat_env, term)[0]:
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(t, [t])

        elif mathsat.msat_term_is_bv_sext(self.msat_env, term)[0]:
            _, amount = mathsat.msat_term_is_bv_sext(self.msat_env, term)
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(types.BVType(amount + t.width), [t])

        elif mathsat.msat_term_is_bv_zext(self.msat_env, term)[0]:
            _, amount = mathsat.msat_term_is_bv_zext(self.msat_env, term)
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(types.BVType(amount + t.width), [t])

        elif mathsat.msat_term_is_bv_extract(self.msat_env, term)[0]:
            _, msb, lsb = mathsat.msat_term_is_bv_extract(self.msat_env, term)
            t = self.env.stc.get_type(args[0])
            res = types.FunctionType(types.BVType(msb - lsb + 1), [t])

        else:
            raise TypeError("Unsupported expression:",
                            mathsat.msat_term_repr(term))
        return res
Exemplo n.º 4
0
    def _get_signature(self, term, args):
        """Returns the signature of the given term.
        For example:
        - a term x & y returns a function type Bool -> Bool -> Bool,
        - a term 14 returns Int
        - a term x ? 13 : 15.0 returns Bool -> Real -> Real -> Real
        """
        res = None

        if mathsat.msat_term_is_true(self.msat_env, term) or \
            mathsat.msat_term_is_false(self.msat_env, term) or \
            mathsat.msat_term_is_boolean_constant(self.msat_env, term):
            res = types.BOOL

        elif mathsat.msat_term_is_number(self.msat_env, term):
            ty = mathsat.msat_term_get_type(term)
            if mathsat.msat_is_integer_type(self.msat_env, ty):
                res = types.INT
            elif mathsat.msat_is_rational_type(self.msat_env, ty):
                res = types.REAL
            else:
                raise NotImplementedError

        elif mathsat.msat_term_is_and(self.msat_env, term) or \
             mathsat.msat_term_is_or(self.msat_env, term) or \
             mathsat.msat_term_is_iff(self.msat_env, term):
            res = types.FunctionType(types.BOOL, [types.BOOL, types.BOOL])

        elif mathsat.msat_term_is_not(self.msat_env, term):
            res = types.FunctionType(types.BOOL, [types.BOOL])

        elif mathsat.msat_term_is_term_ite(self.msat_env, term):
            t1 = self.env.stc.get_type(args[1])
            t2 = self.env.stc.get_type(args[2])
            t = self._most_generic(t1, t2)
            res = types.FunctionType(t, [types.BOOL, t, t])

        elif mathsat.msat_term_is_equal(self.msat_env, term) or \
             mathsat.msat_term_is_leq(self.msat_env, term):
            t1 = self.env.stc.get_type(args[0])
            t2 = self.env.stc.get_type(args[1])
            t = self._most_generic(t1, t2)
            res = types.FunctionType(types.BOOL, [t, t])

        elif mathsat.msat_term_is_plus(self.msat_env, term) or \
             mathsat.msat_term_is_times(self.msat_env, term):
            t1 = self.env.stc.get_type(args[0])
            t2 = self.env.stc.get_type(args[1])
            t = self._most_generic(t1, t2)
            res = types.FunctionType(t, [t, t])

        elif mathsat.msat_term_is_constant(self.msat_env, term):
            ty = mathsat.msat_term_get_type(term)
            if mathsat.msat_is_rational_type(self.msat_env, ty):
                res = types.REAL
            elif mathsat.msat_is_integer_type(self.msat_env, ty):
                res = types.INT
            else:
                raise NotImplementedError("Unsupported variable type found")

        elif mathsat.msat_term_is_uf(self.msat_env, term):
            d = mathsat.msat_term_get_decl(term)
            fun = self.get_symbol_from_declaration(d)
            res = fun.symbol_type()

        else:
            raise TypeError("Unsupported expression:",
                            mathsat.msat_term_repr(term))
        return res