Example #1
0
 public Z3Expr MkGround(Symbol symb, Z3Expr[] args)
 {
     Contract.Assert(symb != null && symb.IsNonVarConstant);
     Contract.Assert(args == null || args.Length == 0);
     Contract.Assert(symbToVal.ContainsKey(symb));
     return(BoxingFun.Apply(Context.MkBV(symbToVal[symb], bvSort.Size)));
 }
Example #2
0
        public Z3Expr MkGround(Symbol symb, Z3Expr[] args)
        {
            Contract.Assert(symb != null && symb.Kind == SymbolKind.BaseCnstSymb);
            Contract.Assert(args == null || args.Length == 0);
            var bc = (BaseCnstSymb)symb;

            Contract.Assert(bc.CnstKind == CnstKind.Numeric);
            var r = (Rational)bc.Raw;

            Contract.Assert(r.IsInteger);
            Contract.Assert(r.Sign > 0);
            var n = r.Numerator;

            if (n.IsOne)
            {
                return(BoxingFun.Apply(Context.MkInt(0)));
            }
            else if (n.IsEven)
            {
                n = n / 2;
                return(BoxingFun.Apply(Context.MkInt(n.ToString())));
            }
            else
            {
                n = (1 - n) / 2;
                return(BoxingFun.Apply(Context.MkInt(n.ToString())));
            }
        }
Example #3
0
        public Z3Expr MkGround(Symbol symb, Z3Expr[] args)
        {
            Contract.Assert(symb != null && symb.Kind == SymbolKind.BaseCnstSymb);
            Contract.Assert(args == null || args.Length == 0);
            var bc = (BaseCnstSymb)symb;

            Contract.Assert(bc.CnstKind == CnstKind.Numeric);
            var r = (Rational)bc.Raw;

            Contract.Assert(r.IsInteger);
            Contract.Assert(Lower <= r.Numerator && r.Numerator <= Upper);
            return(BoxingFun.Apply(Context.MkBV((r.Numerator - Lower).ToString(), bvSort.Size)));
        }
Example #4
0
        private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, NaturalEmbedding te)
        {
            var zero = Context.MkInt(0);
            var one  = Context.MkInt(1);
            var i    = (Z3IntExpr)te.UnboxingFun.Apply(t);

            var negCase = BoxingFun.Apply(i.Neg(Context));

            var coercion = i.Ge(Context, zero).Ite(
                Context,
                BoxingFun.Apply(one.Sub(Context, i)),
                negCase);

            return(coercion);
        }
Example #5
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 #6
0
        private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntRangeEmbedding te)
        {
            Z3BVExpr translate;
            var      unbox = (Z3BVExpr)te.UnboxingFun.Apply(t);

            if (te.Lower <= Lower)
            {
                translate = unbox.BVSub(Context, Context.MkBV((Lower - te.Lower).ToString(), unbox.SortSize)).FitBV(Context, bvSort.Size);
            }
            else
            {
                translate = unbox.FitBV(Context, bvSort.Size).BVAdd(Context, Context.MkBV((te.Lower - Lower).ToString(), bvSort.Size));
            }

            return(BoxingFun.Apply(translate));
        }
Example #7
0
        public NegIntegerEmbedding(TypeEmbedder owner, uint cost)
        {
            Contract.Requires(owner != null);
            Owner        = owner;
            EncodingCost = cost;
            bool wasAdded;

            Type           = Index.MkApply(Index.SymbolTable.GetSortSymbol(BaseSortKind.NegInteger), TermIndex.EmptyArgs, out wasAdded);
            boxingCon      = Context.MkConstructor(BoxingName, TesterName, new string[] { UnboxingName }, new Z3Sort[] { Context.MkIntSort() });
            Representation = Context.MkDatatypeSort(SortName, new Z3Con[] { boxingCon });
            BoxingFun      = boxingCon.ConstructorDecl;
            UnboxingFun    = boxingCon.AccessorDecls[0];
            TesterFun      = boxingCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(
                Index.MkCnst(new Rational(BigInteger.MinusOne, BigInteger.One), out wasAdded),
                BoxingFun.Apply(Context.MkInt(-1)));
        }
Example #8
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 #9
0
        private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, PosIntegerEmbedding te)
        {
            var zero = Context.MkInt(0);
            var one  = Context.MkInt(1);
            var i    = (Z3IntExpr)te.UnboxingFun.Apply(t);

            var negCase = BoxingFun.Apply(i.Neg(Context).Add(Context, one));

            var coercion = i.Gt(Context, zero).Ite(
                Context,
                BoxingFun.Apply(i.Neg(Context)),
                negCase);

            coercion = i.Eq(Context, zero).Ite(
                Context,
                BoxingFun.Apply(one),
                coercion);
            return(coercion);
        }
Example #10
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 #11
0
        public EnumEmbedding(TypeEmbedder owner, Term type, string name)
        {
            Contract.Requires(owner != null);
            Contract.Requires(type != null);
            Owner = owner;

            uint id;

            Type = type;
            foreach (var t in type.Enumerate(x => x.Args))
            {
                if (t.Symbol.Arity == 0)
                {
                    Contract.Assert(t.Symbol.Kind == SymbolKind.BaseCnstSymb || t.Symbol.Kind == SymbolKind.UserCnstSymb);
                    id = (uint)valToSymb.Count;
                    valToSymb.Add(id, t.Symbol);
                    symbToVal.Add(t.Symbol, id);
                }
            }

            var size = ((uint)valToSymb.Count).MostSignificantOne();

            Contract.Assert(((uint)valToSymb.Count).PopulationCount() == 1);
            bvSort = Context.MkBitVecSort(size);

            bool wasAdded;

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

            Representation = Context.MkDatatypeSort(string.Format(SortName, name), new Z3Con[] { boxingCon });
            BoxingFun      = boxingCon.ConstructorDecl;
            UnboxingFun    = boxingCon.AccessorDecls[0];
            TesterFun      = boxingCon.TesterDecl;
            DefaultMember  = new Tuple <Term, Z3Expr>(
                Index.MkApply(valToSymb[0], TermIndex.EmptyArgs, out wasAdded),
                BoxingFun.Apply(Context.MkBV(0, size)));
        }
Example #12
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 #13
0
        private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te)
        {
            var val = ((Rational)((BaseCnstSymb)te.Value.Symbol).Raw).Numerator - Lower;

            return(BoxingFun.Apply(Context.MkBV(val.ToString(), bvSort.Size)));
        }
Example #14
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, RealEmbedding te)
 {
     return(BoxingFun.Apply(
                ((Z3IntExpr)Context.MkSub(Context.MkReal2Int((Z3RealExpr)t), Context.MkInt(Lower.ToString()))).Int2BV(Context, bvSort.Size)));
 }