Example #1
0
        public static Z3BVExpr Int2BV(this Z3IntExpr expr1, Z3Context context, uint size)
        {
            Contract.Requires(expr1 != null && context != null && size > 0);

            Z3IntExpr xj, pow;
            var       xi     = new Z3IntExpr[size];
            var       yi     = new Z3BVExpr[size];
            var       zero   = context.MkInt(0);
            var       one    = context.MkInt(1);
            var       bvzero = context.MkBV(0, size);
            var       bvone  = context.MkBV(1, size);

            xi[size - 1] = expr1;
            for (int i = ((int)size) - 2; i >= 0; --i)
            {
                pow   = context.MkInt(BigInteger.Pow(2, i + 1).ToString());
                xj    = xi[i + 1];
                xi[i] = (Z3IntExpr)xj.Ge(context, pow).Ite(context, xj.Sub(context, pow), xj);
            }

            Z3BVExpr coercion = bvzero;

            for (int i = ((int)size) - 1; i >= 0; --i)
            {
                pow      = context.MkInt(BigInteger.Pow(2, i).ToString());
                coercion = (Z3BVExpr)xi[i].Ge(context, pow).Ite(
                    context,
                    context.MkBVAdd(context.MkBVSHL(coercion, bvone), bvone),
                    context.MkBVSHL(coercion, bvone));
            }

            Contract.Assert(coercion != null);
            return(coercion);
        }
Example #2
0
        /// <summary>
        /// Coerces an integer into a natural.
        /// </summary>
        public Z3Expr MkNegCoercion(Z3IntExpr i)
        {
            var zero = Context.MkInt(0);
            var one  = Context.MkInt(1);
            var two  = Context.MkInt(2);

            var oddCase = BoxingFun.Apply(i.Add(Context, one).Div(Context, two));

            var coercion = i.IsEven(Context).Ite(
                Context,
                BoxingFun.Apply(i.Neg(Context).Div(Context, two)),
                oddCase);

            return(coercion);
        }
Example #3
0
        public static Z3IntExpr BV2Int(this Z3BVExpr expr1, Z3Context context)
        {
            var size   = expr1.SortSize;
            var parts  = new Z3IntExpr[size];
            var zero   = context.MkInt(0);
            var bvzero = context.MkBV(0, 1);

            for (uint i = 0; i < size; ++i)
            {
                parts[i] = (Z3IntExpr)expr1.Index(context, i).Eq(context, bvzero).Ite(
                    context,
                    zero,
                    context.MkInt(BigInteger.Pow(2, (int)i).ToString()));
            }

            return((Z3IntExpr)context.MkAdd(parts));
        }
Example #4
0
        /// <summary>
        /// Coerces an integer into a pos integer.
        /// </summary>
        public Z3Expr MkPosCoercion(Z3IntExpr i)
        {
            var zero = Context.MkInt(0);
            var one  = Context.MkInt(1);
            var two  = Context.MkInt(2);

            var oddCase = BoxingFun.Apply(one.Sub(Context, i).Div(Context, two));

            var coercion = i.IsEven(Context).Ite(
                Context,
                BoxingFun.Apply(i.Div(Context, two)),
                oddCase);

            coercion = i.Eq(Context, one).Ite(
                Context,
                BoxingFun.Apply(zero),
                coercion);
            return(coercion);
        }
Example #5
0
        /// <summary>
        /// Coerces an integer into a natural.
        /// </summary>
        public Z3Expr MkNatCoercion(Z3IntExpr i)
        {
            var zero = Context.MkInt(0);
            var one  = Context.MkInt(1);
            var two  = Context.MkInt(2);

            var evenCase = BoxingFun.Apply(i.Neg(Context).Div(Context, two));

            var coercion = i.IsEven(Context).Not(Context).Ite(
                Context,
                BoxingFun.Apply(i.Add(Context, one).Div(Context, two)),
                evenCase);

            coercion = i.Eq(Context, zero).Ite(
                Context,
                BoxingFun.Apply(zero),
                coercion);
            return(coercion);
        }
Example #6
0
        public IntRangeEmbedding(TypeEmbedder owner, BigInteger lower, BigInteger upper)
        {
            Contract.Requires(owner != null);
            Contract.Requires(lower <= upper);
            Owner = owner;
            Lower = lower;
            Upper = upper;
            var width = upper - lower + 1;

            Contract.Assert(width > 1 && width.IsPowerOfTwo);
            bvSort = Context.MkBitVecSort(width.MostSignificantOne());

            bool wasAdded;

            Type = Index.MkApply(Index.RangeSymbol,
                                 new Term[]
            {
                Index.MkCnst(new Rational(lower, BigInteger.One), out wasAdded),
                Index.MkCnst(new Rational(upper, BigInteger.One), out wasAdded)
            },
                                 out wasAdded);

            boxingCon = Context.MkConstructor(
                string.Format(BoxingName, lower, upper),
                string.Format(TesterName, lower, upper),
                new string[] { string.Format(UnboxingName, lower, upper) },
                new Z3Sort[] { bvSort });

            Representation = Context.MkDatatypeSort(string.Format(SortName, lower, upper), new Z3Con[] { boxingCon });
            BoxingFun      = boxingCon.ConstructorDecl;
            UnboxingFun    = boxingCon.AccessorDecls[0];
            TesterFun      = boxingCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(
                Index.MkCnst(new Rational(lower, BigInteger.One), out wasAdded),
                BoxingFun.Apply(Context.MkBV(0, bvSort.Size)));

            z3Lower = Context.MkInt(Lower.ToString());
        }
Example #7
0
 public static Z3BoolExpr IsEven(this Z3IntExpr expr1, Z3Context context)
 {
     Contract.Assert(expr1 != null);
     return(context.MkEq(context.MkRem(expr1, context.MkInt(2)), context.MkInt(0)));
 }
Example #8
0
 public static Z3ArithExpr Mod(this Z3IntExpr expr1, Z3Context context, Z3IntExpr expr2)
 {
     Contract.Requires(expr1 != null && expr2 != null);
     return(context.MkMod(expr1, expr2));
 }