Ejemplo n.º 1
0
    def test_is_methods(self):
        from fractions import Fraction as pyFraction

        self.assertFalse(is_pysmt_fraction(4))
        self.assertTrue(is_pysmt_fraction(Fraction(4)))

        self.assertFalse(is_pysmt_integer(4.0))
        self.assertTrue(is_pysmt_integer(Integer(4)))

        self.assertTrue(is_python_integer(int(2)))
        if PY2:
            self.assertTrue(is_python_integer(long(2)))
        if HAS_GMPY:
            from gmpy2 import mpz
            self.assertTrue(is_python_integer(mpz(1)))

        if HAS_GMPY:
            from gmpy2 import mpz, mpq
            self.assertTrue(is_python_rational(mpz(1)))
            self.assertTrue(is_python_rational(mpq(1)))
        if PY2:
            self.assertTrue(is_python_rational(long(1)))

        self.assertTrue(is_python_rational(pyFraction(5)))
        self.assertTrue(is_python_rational(3))

        self.assertTrue(is_python_boolean(True))
        self.assertTrue(is_python_boolean(False))
Ejemplo n.º 2
0
    def test_is_methods(self):
        self.assertFalse(is_pysmt_fraction(4))
        self.assertTrue(is_pysmt_fraction(Fraction(4)))

        self.assertFalse(is_pysmt_integer(4.0))
        self.assertTrue(is_pysmt_integer(Integer(4)))

        self.assertTrue(is_python_integer(int(2)))
        if PY2:
            self.assertTrue(is_python_integer(long(2)))
        if HAS_GMPY:
            from gmpy2 import mpz
            self.assertTrue(is_python_integer(mpz(1)))

        if HAS_GMPY:
            from gmpy2 import mpz, mpq
            self.assertTrue(is_python_rational(mpz(1)))
            self.assertTrue(is_python_rational(mpq(1)))
        if PY2:
            self.assertTrue(is_python_rational(long(1)))

        self.assertTrue(is_python_rational(pyFraction(5)))
        self.assertTrue(is_python_rational(3))

        self.assertTrue(is_python_boolean(True))
        self.assertTrue(is_python_boolean(False))
Ejemplo n.º 3
0
    def __init__(self, **base_options):
        SolverOptions.__init__(self, **base_options)
        if self.unsat_cores_mode is not None:
            raise PysmtValueError("'unsat_cores_mode' option not supported.")

        # Set Defaults
        self.preprocessing = True
        self.propagation_limit = None
        self.more_important_lit = None
        self.less_important_lit = None
        self.global_default_phase = None
        self.enable_trace_generation = False
        self.output = None
        self.verbosity = 0

        for k, v in self.solver_options.items():
            if k == "enable_trace_generation":
                if v not in (True, False):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "output":
                if v is not None and not hasattr(v, "fileno"):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))

            elif k == "global_default_phase":
                if v is not None and v not in PicosatOptions.ALL_GLOBAL_DEFAULT_PHASE:
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "preprocessing":
                if v not in (True, False):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "verbosity":
                if not is_python_integer(v):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "propagation_limit":
                if not is_python_integer(v):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k in ("more_important_lit", "less_important_lit"):
                if v is not None:
                    try:
                        valid = all(x.is_symbol(types.BOOL) for x in v)
                    except:
                        valid = False
                    if not valid:
                        raise PysmtValueError("'more_important_lit' and "
                                              "'less_important_lit' require a "
                                              "list of Boolean variables")
            else:
                raise PysmtValueError("Unrecognized option '%s'." % k)
            # Store option
            setattr(self, k, v)

        # Consistency
        if self.output is not None and self.verbosity == 0:
            self.verbosity = 1
Ejemplo n.º 4
0
    def __init__(self, **base_options):
        SolverOptions.__init__(self, **base_options)
        if self.unsat_cores_mode is not None:
            raise PysmtValueError("'unsat_cores_mode' option not supported.")

        # Set Defaults
        self.preprocessing = True
        self.propagation_limit = None
        self.more_important_lit = None
        self.less_important_lit = None
        self.global_default_phase = None
        self.enable_trace_generation = False
        self.output = None
        self.verbosity = 0

        for k,v in self.solver_options.items():
            if k == "enable_trace_generation":
                if v not in (True, False):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "output":
                if v is not None and not hasattr(v, "fileno"):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))

            elif k == "global_default_phase":
                if v is not None and v not in PicosatOptions.ALL_GLOBAL_DEFAULT_PHASE:
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "preprocessing":
                if v not in (True, False):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "verbosity":
                if not is_python_integer(v):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k == "propagation_limit":
                if not is_python_integer(v):
                    raise PysmtValueError("Invalid value for %s: %s" % \
                                     (str(k),str(v)))
            elif k in ("more_important_lit", "less_important_lit"):
                if v is not None:
                    try:
                        valid = all(x.is_symbol(types.BOOL) for x in v)
                    except:
                        valid = False
                    if not valid:
                        raise PysmtValueError("'more_important_lit' and "
                                              "'less_important_lit' require a "
                                              "list of Boolean variables")
            else:
                raise PysmtValueError("Unrecognized option '%s'." % k)
            # Store option
            setattr(self, k, v)

        # Consistency
        if self.output is not None and self.verbosity == 0:
            self.verbosity = 1
Ejemplo n.º 5
0
    def BVExtract(self, formula, start=0, end=None):
        """Returns the slice of formula from start to end (inclusive)."""
        if end is None: end = formula.bv_width()-1
        assert is_python_integer(start) and is_python_integer(end)
        assert end >= start and start >= 0, "Start: %d ; End: %d" % (start,end)
        size = end-start+1

        assert size <= formula.bv_width(), \
            "Invalid size: start=%d, end=%d, width=%d" % \
            (start, end, formula.bv_width())
        return self.create_node(node_type=op.BV_EXTRACT,
                                args=(formula,),
                                payload=(size, start, end))
Ejemplo n.º 6
0
    def BVExtract(self, formula, start=0, end=None):
        """Returns the slice of formula from start to end (inclusive)."""
        if end is None: end = formula.bv_width()-1
        assert is_python_integer(start) and is_python_integer(end)
        assert end >= start and start >= 0, "Start: %d ; End: %d" % (start,end)
        size = end-start+1

        assert size <= formula.bv_width(), \
            "Invalid size: start=%d, end=%d, width=%d" % \
            (start, end, formula.bv_width())
        return self.create_node(node_type=op.BV_EXTRACT,
                                args=(formula,),
                                payload=(size, start, end))
Ejemplo n.º 7
0
 def BVLShr(self, left, right):
     """Returns the logical right shift the BV."""
     if is_python_integer(right):
         right = self.BV(right, left.bv_width())
     return self.create_node(node_type=op.BV_LSHR,
                             args=(left, right),
                             payload=(left.bv_width(),))
Ejemplo n.º 8
0
    def SBV(self, value, width=None):
        """Returns a constant of type BitVector interpreting the sign.

        If the specified value is an integer, it is converted in the
        2-complement representation of the given number, otherwise the
        behavior is the same as BV().
        """
        if is_python_integer(value):
            if width is None:
                raise PysmtValueError("Need to specify a width for the constant")

            min_val = -(2**(width-1))
            max_val = (2**(width-1)) - 1
            if value < min_val:
                raise PysmtValueError("Cannot represent a value (%d) lower " \
                                      "than %d in %d bits" % (value, min_val,
                                                              width))
            if value > max_val:
                raise PysmtValueError("Cannot represent a value (%d) greater " \
                                      "than %d in %d bits" % (value, max_val,
                                                              width))

            if value >= 0:
                return self.BV(value, width)
            else:
                comp_value = (2**width) + value # value is negative!
                return self.BV(comp_value, width)
        else:
            return self.BV(value, width=width)
Ejemplo n.º 9
0
 def BVLShr(self, left, right):
     """Returns the logical right shift the BV."""
     if is_python_integer(right):
         right = self.BV(right, left.bv_width())
     return self.create_node(node_type=op.BV_LSHR,
                             args=(left, right),
                             payload=(left.bv_width(),))
Ejemplo n.º 10
0
    def SBV(self, value, width=None):
        """Returns a constant of type BitVector interpreting the sign.

        If the specified value is an integer, it is converted in the
        2-complement representation of the given number, otherwise the
        behavior is the same as BV().
        """
        if is_python_integer(value):
            if width is None:
                raise PysmtValueError("Need to specify a width for the constant")

            min_val = -(2**(width-1))
            max_val = (2**(width-1)) - 1
            if value < min_val:
                raise PysmtValueError("Cannot represent a value (%d) lower " \
                                      "than %d in %d bits" % (value, min_val,
                                                              width))
            if value > max_val:
                raise PysmtValueError("Cannot represent a value (%d) greater " \
                                      "than %d in %d bits" % (value, max_val,
                                                              width))

            if value >= 0:
                return self.BV(value, width)
            else:
                comp_value = (2**width) + value # value is negative!
                return self.BV(comp_value, width)
        else:
            return self.BV(value, width=width)
Ejemplo n.º 11
0
 def BVAShr(self, left, right):
     """Returns the RIGHT arithmetic rotation of the left BV by the number
     of steps specified by the right BV."""
     if is_python_integer(right):
         right = self.BV(right, left.bv_width())
     return self.create_node(node_type=op.BV_ASHR,
                             args=(left, right),
                             payload=(left.bv_width(),))
Ejemplo n.º 12
0
 def BVAShr(self, left, right):
     """Returns the RIGHT arithmetic rotation of the left BV by the number
     of steps specified by the right BV."""
     if is_python_integer(right):
         right = self.BV(right, left.bv_width())
     return self.create_node(node_type=op.BV_ASHR,
                             args=(left, right),
                             payload=(left.bv_width(),))
Ejemplo n.º 13
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))
Ejemplo n.º 14
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))
Ejemplo n.º 15
0
 def __rsub__(self, left):
     # Swap operators to perform right-subtract
     # For BVs we might need to build the BV constant
     if self.get_type().is_bv_type():
         if is_python_integer(left):
             left = _mgr().BV(left, width=self.bv_width())
         return left._apply_infix(self, _mgr().BVSub)
     # (x - y) = (-y + x)
     minus_self = -self
     return minus_self._apply_infix(left, _mgr().Plus)
Ejemplo n.º 16
0
 def __rsub__(self, left):
     # Swap operators to perform right-subtract
     # For BVs we might need to build the BV constant
     if self.get_type().is_bv_type():
         if is_python_integer(left):
             left = _mgr().BV(left, width=self.bv_width())
         return left._apply_infix(self, _mgr().BVSub)
     # (x - y) = (-y + x)
     minus_self = -self
     return minus_self._apply_infix(left, _mgr().Plus)
Ejemplo n.º 17
0
 def walk_bv_rotate(self, formula, args, **kwargs):
     #pylint: disable=unused-argument
     target_width = formula.bv_width()
     rot_step = formula.bv_rotation_step()
     if is_python_integer(
             rot_step
     ) and target_width < formula.bv_rotation_step() or target_width < 0:
         return None
     if target_width != args[0].width:
         return None
     return BVType(target_width)
Ejemplo n.º 18
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))
Ejemplo n.º 19
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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 22
0
 def _apply_infix(self, right, function, bv_function=None):
     if _env().enable_infix_notation:
         mgr = _mgr()
         # BVs
         # Default bv_function to function
         if bv_function is None: bv_function = function
         if _is_bv(self):
             if is_python_integer(right):
                 right = mgr.BV(right, width=self.bv_width())
             return bv_function(self, right)
         # Boolean, Integer and Arithmetic
         if is_python_boolean(right):
             right = mgr.Bool(right)
         elif is_python_integer(right):
             ty = self.get_type()
             if ty.is_real_type():
                 right = mgr.Real(right)
             else:
                 right = mgr.Int(right)
         elif is_python_rational(right):
             right = mgr.Real(right)
         return function(self, right)
     else:
         raise PysmtModeError("Cannot use infix notation")
Ejemplo n.º 23
0
 def _apply_infix(self, right, function, bv_function=None):
     if _env().enable_infix_notation:
         mgr = _mgr()
         # BVs
         # Default bv_function to function
         if bv_function is None: bv_function = function
         if _is_bv(self):
             if is_python_integer(right):
                 right = mgr.BV(right, width=self.bv_width())
             return bv_function(self, right)
         # Boolean, Integer and Arithmetic
         if is_python_boolean(right):
             right = mgr.Bool(right)
         elif is_python_integer(right):
             ty = self.get_type()
             if ty.is_real_type():
                 right = mgr.Real(right)
             else:
                 right = mgr.Int(right)
         elif is_python_rational(right):
             right = mgr.Real(right)
         return function(self, right)
     else:
         raise Exception("Cannot use infix notation")
Ejemplo n.º 24
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))
Ejemplo n.º 25
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))
Ejemplo n.º 26
0
 def walk_bv_rotate(self, formula):
     if formula.is_bv_ror():
         rotate_type = "rotate_right"
     else:
         assert formula.is_bv_rol()
         rotate_type = "rotate_left"
     rot_step = formula.bv_rotation_step()
     if is_python_integer(rot_step):
         self.write("((_ %s %d) " % (rotate_type, rot_step))
         yield formula.arg(0)
     else:
         self.write("(ext_%s " % rotate_type)
         yield formula.arg(0)
         self.write(" ")
         yield rot_step
     self.write(")")
Ejemplo n.º 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
Ejemplo n.º 28
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