示例#1
0
    def back(self, expr):
        res = None
        if expr.isConst():
            if expr.getType().isBoolean():
                v = expr.getConstBoolean()
                res = self.mgr.Bool(v)
            elif expr.getType().isInteger():
                v = expr.getConstRational().toString()
                res = self.mgr.Int(int(v))
            elif expr.getType().isReal():
                v = expr.getConstRational().toString()
                res = self.mgr.Real(Fraction(v))
            elif expr.getType().isBitVector():
                bv = expr.getConstBitVector()
                v = bv.getValue().toString()
                width = bv.getSize()
                res = self.mgr.BV(int(v), width)
            elif expr.getType().isString():
                v = expr.getConstString()
                res = self.mgr.String(v.toString())
            elif expr.getType().isArray():
                const_ = expr.getConstArrayStoreAll()
                array_type = self._cvc4_type_to_type(const_.getType())
                base_value = self.back(const_.getExpr())
                res = self.mgr.Array(array_type.index_type, base_value)
            else:
                raise PysmtTypeError("Unsupported constant type:",
                                     expr.getType().toString())
        else:
            raise PysmtTypeError("Unsupported expression:", expr.toString())

        return res
示例#2
0
    def substitute_helper(self, formula, subs, ssubs, simple, bmode):
        """Replaces any subformula in formula with the definition in subs."""

        # Check that formula is a term
        if not formula.is_term():
            raise PysmtTypeError("substitute() can only be used on terms.")

        for (i, k) in enumerate(subs):
            v = subs[k]
            #             # Check that substitutions are terms
            #             if not k.is_term():
            #                 raise PysmtTypeError(
            #                     "Only terms should be provided as substitutions." +
            #                     " Non-term '%s' found." % k)
            #             if not v.is_term():
            #                 raise PysmtTypeError(
            #                     "Only terms should be provided as substitutions." +
            #                     " Non-term '%s' found." % v)
            # Check that substitutions belong to the current formula manager
            if k not in self.manager:
                raise PysmtTypeError(
                    "Key %d does not belong to the Formula Manager." % i)
            if v not in self.manager:
                raise PysmtTypeError(
                    "Value %d does not belong to the Formula Manager." % i)
        res = self.walk(formula,
                        substitutions=subs,
                        ssubstitutions=ssubs,
                        simplemode=simple,
                        boolmode=bmode)
        return res
示例#3
0
    def substitute(self, formula, subs):
        """Replaces any subformula in formula with the definition in subs."""

        # Check that formula is a term
        if not formula.is_term():
            raise PysmtTypeError("substitute() can only be used on terms.")

        for (i, k) in enumerate(subs):
            v = subs[k]
            # Check that substitutions are terms
            if not k.is_term() and k.node_type() != op.SYMBOL:
                raise PysmtTypeError(
                    "Only terms or EUFS should be provided as substitutions." +
                    " Non-term '%s' found." % k)
            if not v.is_term() and v.node_type() != op.SYMBOL:
                raise PysmtTypeError(
                    "Only terms or EUFS should be provided as substitutions." +
                    " Non-term '%s' found." % v)
            # Check that substitutions belong to the current formula manager
            if k not in self.manager and v.node_type() != op.SYMBOL:
                raise PysmtTypeError(
                    "Key %d does not belong to the Formula Manager." % i)
            if v not in self.manager and v.node_type() != op.SYMBOL:
                raise PysmtTypeError(
                    "Value %d does not belong to the Formula Manager." % i)

        res = self.walk(formula, substitutions=subs)
        return res
示例#4
0
    def Real(self, value):
        """ Returns a Real-type constant of the given value.

        value can be:
          - A Fraction(n,d)
          - A tuple (n,d)
          - A long or int n
          - A float
          - (Optionally) a mpq or mpz object
        """
        if value in self.real_constants:
            return self.real_constants[value]

        if is_pysmt_fraction(value):
            val = value
        elif type(value) == tuple:
            val = Fraction(value[0], value[1])
        elif is_python_rational(value):
            val = pysmt_fraction_from_rational(value)
        else:
            raise PysmtTypeError("Invalid type in constant. The type was:" + \
                                 str(type(value)))

        n = self.create_node(node_type=op.REAL_CONSTANT,
                             args=tuple(),
                             payload=val)
        self.real_constants[value] = n
        return n
示例#5
0
    def Array(self, idx_type, default, assigned_values=None):
        """Creates a node representing an array having index type equal to
           idx_type, initialized with default values.

           If assigned_values is specified, then it must be a map from
           constants of type idx_type to values of the same type as
           default and the array is initialized correspondingly.
        """
        if not isinstance(idx_type, types.PySMTType):
            raise PysmtTypeError("idx_type is not a valid type: '%s'" %
                                 idx_type)

        args = [default]
        if assigned_values:
            for k in sorted(assigned_values, key=id):
                if not k.is_constant():
                    raise PysmtValueError("Array initialization indexes must "
                                          "be constants")
                # It is useless to represent assignments equal to the default
                if assigned_values[k] != default:
                    args.append(k)
                    args.append(assigned_values[k])
        return self.create_node(node_type=op.ARRAY_VALUE,
                                args=tuple(args),
                                payload=idx_type)
示例#6
0
    def _assert_is_boolean(self, formula):
        """Enforces that argument 'formula' is of type Boolean.

        Raises TypeError.
        """
        if formula.get_type() != BOOL:
            raise PysmtTypeError("Argument must be boolean.")
示例#7
0
    def _assert_no_function_type(self, item):
        """Enforces that argument 'item' cannot be a FunctionType.

        Raises TypeError.
        """
        if item.is_symbol() and item.symbol_type().is_function_type():
            raise PysmtTypeError("Cannot call get_value() on a FunctionType")
示例#8
0
 def get_type(self, formula):
     """ Returns the pysmt.types type of the formula """
     res = self.walk(formula)
     if not self.be_nice and res is None:
         raise PysmtTypeError("The formula '%s' is not well-formed" \
                              % str(formula))
     return res
示例#9
0
 def declare_variable(self, var):
     if not var.is_symbol(type_=types.BOOL):
         raise PysmtTypeError("Trying to declare as a variable something "
                              "that is not a symbol: %s" % var)
     if var not in self.var2node:
         node = self.ddmanager.NewVar()
         self.idx2var[node.NodeReadIndex()] = var
         self.var2node[var] = node
示例#10
0
文件: cvc4.py 项目: bdrodes/pysmt
    def back(self, expr):
        res = None
        if expr.isConst():
            if expr.getType().isBoolean():
                v = expr.getConstBoolean()
                res = self.mgr.Bool(v)
            elif expr.getType().isInteger():
                v = expr.getConstRational().toString()
                res = self.mgr.Int(int(v))
            elif expr.getType().isReal():
                v = expr.getConstRational().toString()
                res = self.mgr.Real(Fraction(v))
            elif expr.getType().isBitVector():
                bv = expr.getConstBitVector()
                v = bv.getValue().toString()
                width = bv.getSize()
                res = self.mgr.BV(int(v), width)
            elif expr.getType().isString():
                v = expr.getConstString()
                res = self.mgr.String(v.toString())
            elif expr.getType().isArray():
                # if no children, typical array declaration
                if not expr.getChildren():
                    const_ = expr.getConstArrayStoreAll()
                    array_type = self._cvc4_type_to_type(const_.getType())
                    base_value = self.back(const_.getExpr())
                    res = self.mgr.Array(array_type.index_type, base_value)
                # if children, expecting index assignments of this form:
                #     ARRAY(STRING OF STRING) : "" WITH ["a"] := "1", ["b"] := "2"
                # The children have been observed to be as follows for the above example:
                #   child 0: ARRAY(STRING OF STRING) : "" WITH ["a"] := "1"
                #   child 1: "b"
                #   child 2: "2"
                else:
                    assert len(expr.getChildren()) == 3
                    arr = self.back(expr.getChild(0))
                    ind = self.back(expr.getChild(1))
                    val = self.back(expr.getChild(2))
                    res = self.mgr.Store(arr, ind, val)
            else:
                raise PysmtTypeError("Unsupported constant type:",
                                     expr.getType().toString())
        else:
            raise PysmtTypeError("Unsupported expression:", expr.toString())

        return res
示例#11
0
def assert_args_type_in(args, allowed_types):
    """ Enforces that the type of the arguments is an allowed type """
    for arg in args:
        t = arg.get_type()
        if (t not in allowed_types):
            raise PysmtTypeError(
                "Argument is of type %s, but one of %s was expected!\n" %
                (t, str(allowed_types)))
示例#12
0
def assert_same_type_args(args):
    """ Enforces that all elements in args have the same type. """
    ref_t = args[0].get_type()
    for arg in args[1:]:
        t = arg.get_type()
        if (t != ref_t):
            raise PysmtTypeError("Arguments should be of the same type!\n" +
                                 str([str((a, a.get_type())) for a in args]))
示例#13
0
    def Bool(self, value):
        if type(value) != bool:
            raise PysmtTypeError("Expecting bool, got %s" % type(value))

        if value:
            return self.true_formula
        else:
            return self.false_formula
示例#14
0
 def declare_variable(self, var):
     if not var.is_symbol():
         raise PysmtTypeError("Trying to declare as a variable something "
                              "that is not a symbol: %s" % var)
     if var.symbol_name() not in self.declared_vars:
         cvc4_type = self._type_to_cvc4(var.symbol_type())
         decl = self.mkVar(var.symbol_name(), cvc4_type)
         self.declared_vars[var] = decl
示例#15
0
 def BVRor(self, formula, steps):
     """Returns the RIGHT rotation of the BV by the number of steps."""
     if not is_python_integer(steps):
         raise PysmtTypeError("BVRor: 'steps' should be an integer. Got %s" \
                              % steps)
     return self.create_node(node_type=op.BV_ROR,
                             args=(formula,),
                             payload=(formula.bv_width(), steps))
示例#16
0
 def get_or_create_symbol(self, name, typename):
     s = self.symbols.get(name, None)
     if s is None:
         return self._create_symbol(name, typename)
     if not s.symbol_type() == typename:
         raise PysmtTypeError("Trying to redefine symbol '%s' with a new type"
                              ". Previous type was '%s' new type is '%s'" %
                              (name, s.symbol_type(), typename))
     return s
示例#17
0
 def walk_equals(self, formula, args, **kwargs):
     #pylint: disable=unused-argument
     if args[0].is_bool_type():
         raise PysmtTypeError("The formula '%s' is not well-formed."
                              "Equality operator is not supported for Boolean"
                              " terms. Use Iff instead." \
                              % str(formula))
     elif args[0].is_bv_type():
         return self.walk_bv_to_bool(formula, args)
     return self.walk_type_to_type(formula, args, args[0], BOOL)
示例#18
0
 def declare_variable(self, var):
     if not var.is_symbol():
         raise PysmtTypeError("Trying to declare as a variable something "
                              "that is not a symbol: %s" % var)
     if var.symbol_name() not in self.symbol_to_decl:
         tp = self._type_to_yices(var.symbol_type())
         decl = yicespy.yices_new_uninterpreted_term(tp)
         yicespy.yices_set_term_name(decl, var.symbol_name())
         self.symbol_to_decl[var] = decl
         self.decl_to_symbol[decl] = var
示例#19
0
    def fsubstitute(self, formula):
        """Replaces any subformula in formula with the definition in subs."""
        # Check that formula is a term
        if not formula.is_term():
            raise PysmtTypeError("substitute() can only be used on terms.")

        res = self.walk(formula, substitutions=dict())

        key = self.get_fkey(formula)
        self.memoization[key] = res
        return res
示例#20
0
    def BVZExt(self, formula, increase):
        """Returns the extension of the BV with 'increase' additional bits

        New bits are set to zero.
        """
        if not is_python_integer(increase):
            raise PysmtTypeError("BVZext: 'increase' should be an integer. "
                                 "Got %s" % increase)
        return self.create_node(node_type=op.BV_ZEXT,
                                args=(formula,),
                                payload=(formula.bv_width()+increase,
                                         increase))
示例#21
0
    def BVSExt(self, formula, increase):
        """Returns the signed extension of the BV with 'increase' additional bits

        New bits are set according to the most-significant-bit.
        """
        if not is_python_integer(increase):
            raise PysmtTypeError("BVSext: 'increase' should be an integer. "
                                 "Got %s" % increase)
        return self.create_node(node_type=op.BV_SEXT,
                                args=(formula,),
                                payload=(formula.bv_width()+increase,
                                         increase))
示例#22
0
    def get_value(self, formula, model_completion=True):
        if model_completion:
            syms = formula.get_free_variables()
            self._complete_model(syms)
            r = formula.substitute(self.completed_assignment)
        else:
            r = formula.substitute(self.assignment)

        res = r.simplify()
        if not res.is_constant():
            raise PysmtTypeError("Was expecting a constant but got %s" % res)
        return res
示例#23
0
 def ToReal(self, formula):
     """ Cast a formula to real type. """
     t = self.env.stc.get_type(formula)
     if t == types.REAL:
         # Ignore casting of a Real
         return formula
     elif t == types.INT:
         if formula.is_int_constant():
             return self.Real(formula.constant_value())
         return self.create_node(node_type=op.TOREAL, args=(formula, ))
     else:
         raise PysmtTypeError("Argument is of type %s, but INT was "
                              "expected!\n" % t)
示例#24
0
    def _complete_model(self, symbols):
        undefined_symbols = (s for s in symbols
                             if s not in self.completed_assignment)
        mgr = self.environment.formula_manager

        for s in undefined_symbols:
            if not s.is_symbol():
                raise PysmtTypeError("Was expecting a symbol but got %s" %s)

            if s.symbol_type().is_bool_type():
                value = mgr.Bool(False)
            elif s.symbol_type().is_real_type():
                value = mgr.Real(0)
            elif s.symbol_type().is_int_type():
                value = mgr.Int(0)
            elif s.symbol_type().is_bv_type():
                value = mgr.BVZero(s.bv_width())
            else:
                raise PysmtTypeError("Unhandled type for %s: %s" %
                                     (s, s.symbol_type()))

            self.completed_assignment[s] = value
示例#25
0
    def Implies(self, *args):
        """ Creates an expression of the form:
            arg0 ^ ... ^ arg{N-1} -> argN

        Restriction: arg{i} must be of boolean type
        """
        tuple_args = self._polymorph_args_to_tuple(args)
        if len(tuple_args) == 0:
            raise PysmtTypeError("Cannot create a Implies without arguments.")
        elif len(tuple_args) == 1:
            # Empty LHS
            return tuple_args[0]
        else:
            return self.create_node(node_type=op.IMPLIES, args=tuple_args)
示例#26
0
    def BV(self, value, width=None):
        """Return a constant of type BitVector.

        value can be either:
        - a string of 0s and 1s
        - a string starting with "#b" followed by a sequence of 0s and 1s
        - an integer number s.t. 0 <= value < 2**width

        In order to create the BV representation of a signed integer,
        the SBV() method shall be used.
        """

        if type(value) is str:
            if value.startswith("#b"):
                str_width = len(value)-2
                value = int(value[2:],2)
            elif all(v in ["0", "1"] for v in value):
                str_width = len(value)
                value = int(value, 2)
            else:
                raise PysmtValueError("Expecting binary value as string, got " \
                                      "%s instead." % value)

            if width is not None and width != str_width:
                raise PysmtValueError("Specified width does not match string " \
                                      "width (%d != %d)" % (width, str_width))
            width = str_width

        if width is None:
            raise PysmtValueError("Need to specify a width for the constant")

        if is_pysmt_integer(value):
            _value = value
        elif is_python_integer(value):
            _value = pysmt_integer_from_integer(value)
        else:
            raise PysmtTypeError("Invalid type in constant. The type was: %s" \
                                 % str(type(value)))
        if _value < 0:
            raise PysmtValueError("Cannot specify a negative value: %d" \
                                  % _value)
        if _value >= 2**width:
            raise PysmtValueError("Cannot express %d in %d bits" \
                                  % (_value, width))

        return self.create_node(node_type=op.BV_CONSTANT,
                                args=tuple(),
                                payload=(_value, width))
示例#27
0
    def Int(self, value):
        """Return a constant of type INT."""
        if value in self.int_constants:
            return self.int_constants[value]

        if is_pysmt_integer(value):
            val = value
        elif is_python_integer(value):
            val = pysmt_integer_from_integer(value)
        else:
            raise PysmtTypeError("Invalid type in constant. The type was:" + \
                                 str(type(value)))
        n = self.create_node(node_type=op.INT_CONSTANT,
                             args=tuple(),
                             payload=val)
        self.int_constants[value] = n
        return n
示例#28
0
    def Plus(self, *args):
        """ Returns an sum of terms.

        This function has polimorphic n-arguments:
          - Plus(a,b,c)
          - Plus([a,b,c])

        Restriction:
         - Arguments must be all of the same type
         - Arguments must be INT or REAL
        """
        tuple_args = self._polymorph_args_to_tuple(args)
        if len(tuple_args) == 0:
            raise PysmtTypeError("Cannot create a Plus without arguments.")

        if len(tuple_args) == 1:
            return tuple_args[0]
        else:
            return self.create_node(node_type=op.PLUS, args=tuple_args)
示例#29
0
    def Times(self, *args):
        """ Creates a multiplication of terms

        This function has polimorphic n-arguments:
          - Times(a,b,c)
          - Times([a,b,c])

        Restriction:
         - Arguments must be all of the same type
         - Arguments must be INT or REAL
        """
        tuple_args = self._polymorph_args_to_tuple(args)
        if len(tuple_args) == 0:
            raise PysmtTypeError("Cannot create a Times without arguments.")

        if len(tuple_args) == 1:
            return tuple_args[0]
        else:
            return self.create_node(node_type=op.TIMES, args=tuple_args)
示例#30
0
def assert_boolean_args(args):
    """ Enforces that the elements in args are of BOOL type. """
    for arg in args:
        t = arg.get_type()
        if (t != BOOL):
            raise PysmtTypeError("%s is not allowed in arguments" % t)