MkNumeral() public method

Create a Term of a given sort. This function can be use to create numerals that fit in a machine integer. It is slightly faster than MakeNumeral since it is not necessary to parse a string.
public MkNumeral ( int v, Microsoft.Z3.Sort ty ) : Expr
v int Value of the numeral
ty Microsoft.Z3.Sort Sort of the numeral
return Expr
Esempio n. 1
0
        public static Expr EeAndGt(String left1, int left2, String right1, int right2)
        {
            using (Context ctx = new Context())
            {
                Expr a = ctx.MkConst(left1, ctx.MkIntSort());
                Expr b = ctx.MkNumeral(left2, ctx.MkIntSort());
                Expr c = ctx.MkConst(right1, ctx.MkIntSort());
                Expr d = ctx.MkNumeral(right2, ctx.MkIntSort());

                Solver s = ctx.MkSolver();
                s.Assert(ctx.MkAnd(ctx.MkEq((ArithExpr)a, (ArithExpr)b), ctx.MkGt((ArithExpr)c, (ArithExpr)d)));
                s.Check();

                BoolExpr testing = ctx.MkAnd(ctx.MkEq((ArithExpr)a, (ArithExpr)b), ctx.MkGt((ArithExpr)c, (ArithExpr)d));
                Model model = Check(ctx, testing, Status.SATISFIABLE);

                Expr result2;
                Model m2 = s.Model;
                foreach (FuncDecl d2 in m2.Decls)
                {
                    result2 = m2.ConstInterp(d2);
                    return result2;
                }
            }
            return null;
        }
Esempio n. 2
0
 /// <summary>
 /// Returns the Z3 term corresponding to the MSF rational number.
 /// </summary>
 /// <param name="rational">The MSF rational</param>
 /// <returns>The Z3 term</returns>
 public static ArithExpr GetNumeral(Context context, Rational rational, Sort sort = null)
 {
     try
     {
         sort = rational.IsInteger() ? ((Sort)context.IntSort) : (sort == null ? (Sort)context.RealSort : sort);
         return (ArithExpr)context.MkNumeral(rational.ToString(), sort);                
     }
     catch (Z3Exception e)
     {
         Console.Error.WriteLine("Conversion of {0} failed:\n {1}", rational, e);
         throw new NotSupportedException();
     }
 }
Esempio n. 3
0
        public static void CheckLessThan(int a, String b)
        {
            // Console.WriteLine("This test worked" + a + " " + b);

            using (Context ctx = new Context())
            {
                // 3 < x
                Expr x = ctx.MkConst(b, ctx.MkIntSort());
                Expr value = ctx.MkNumeral(a, ctx.MkIntSort());
                BoolExpr test = ctx.MkLt((ArithExpr)value,(ArithExpr) x);
                Model model = Check(ctx, test, Status.SATISFIABLE);
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            using (Context ctx = new Context())
            {
                Expr x = ctx.MkConst("x", ctx.MkIntSort());
                Expr zero = ctx.MkNumeral(0, ctx.MkIntSort());

                Solver s = ctx.MkSolver();
                s.Assert(ctx.MkLt((ArithExpr)zero, (ArithExpr)x)); // 0<x

                Status result = s.Check();
                Console.WriteLine(result);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Simple bit-vector example.
        /// </summary>
        /// <remarks>
        /// This example disproves that x - 10 &lt;= 0 IFF x &lt;= 10 for (32-bit) machine integers
        /// </remarks>
        public static void BitvectorExample1()
        {
            var ctx = new Z3.Context();

            using var svc = Z3Api.Own(ctx);


            var        bv_type     = ctx.MkBitVecSort(32);
            var        x           = (BitVecExpr)ctx.MkConst("x", bv_type);
            var        zero        = (BitVecNum)ctx.MkNumeral("0", bv_type);
            BitVecNum  ten         = ctx.MkBV(10, 32);
            BitVecExpr x_minus_ten = ctx.MkBVSub(x, ten);
            /* bvsle is signed less than or equal to */
            BoolExpr c1  = ctx.MkBVSLE(x, ten);
            BoolExpr c2  = ctx.MkBVSLE(x_minus_ten, zero);
            BoolExpr thm = ctx.MkIff(c1, c2);

            print("disprove: x - 10 <= 0 IFF x <= 10 for (32-bit) machine integers");
            Disprove(ctx, thm);
        }
Esempio n. 6
0
        /// <summary>
        /// Some basic tests.
        /// </summary>
        static void BasicTests(Context ctx)
        {
            Console.WriteLine("BasicTests");

            Symbol qi = ctx.MkSymbol(1);
            Symbol fname = ctx.MkSymbol("f");
            Symbol x = ctx.MkSymbol("x");
            Symbol y = ctx.MkSymbol("y");

            Sort bs = ctx.MkBoolSort();

            Sort[] domain = { bs, bs };
            FuncDecl f = ctx.MkFuncDecl(fname, domain, bs);
            Expr fapp = ctx.MkApp(f, ctx.MkConst(x, bs), ctx.MkConst(y, bs));

            Expr[] fargs2 = { ctx.MkFreshConst("cp", bs) };
            Sort[] domain2 = { bs };
            Expr fapp2 = ctx.MkApp(ctx.MkFreshFuncDecl("fp", domain2, bs), fargs2);

            BoolExpr trivial_eq = ctx.MkEq(fapp, fapp);
            BoolExpr nontrivial_eq = ctx.MkEq(fapp, fapp2);

            Goal g = ctx.MkGoal(true);
            g.Assert(trivial_eq);
            g.Assert(nontrivial_eq);
            Console.WriteLine("Goal: " + g);

            Solver solver = ctx.MkSolver();

            foreach (BoolExpr a in g.Formulas)
                solver.Assert(a);

            if (solver.Check() != Status.SATISFIABLE)
                throw new TestFailedException();

            ApplyResult ar = ApplyTactic(ctx, ctx.MkTactic("simplify"), g);
            if (ar.NumSubgoals == 1 && (ar.Subgoals[0].IsDecidedSat || ar.Subgoals[0].IsDecidedUnsat))
                throw new TestFailedException();

            ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g);
            if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedSat)
                throw new TestFailedException();

            g.Assert(ctx.MkEq(ctx.MkNumeral(1, ctx.MkBitVecSort(32)),
                                      ctx.MkNumeral(2, ctx.MkBitVecSort(32))));
            ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g);
            if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedUnsat)
                throw new TestFailedException();


            Goal g2 = ctx.MkGoal(true, true);
            ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g2);
            if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedSat)
                throw new TestFailedException();

            g2 = ctx.MkGoal(true, true);
            g2.Assert(ctx.MkFalse());
            ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g2);
            if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedUnsat)
                throw new TestFailedException();

            Goal g3 = ctx.MkGoal(true, true);
            Expr xc = ctx.MkConst(ctx.MkSymbol("x"), ctx.IntSort);
            Expr yc = ctx.MkConst(ctx.MkSymbol("y"), ctx.IntSort);
            g3.Assert(ctx.MkEq(xc, ctx.MkNumeral(1, ctx.IntSort)));
            g3.Assert(ctx.MkEq(yc, ctx.MkNumeral(2, ctx.IntSort)));
            BoolExpr constr = ctx.MkEq(xc, yc);
            g3.Assert(constr);
            ar = ApplyTactic(ctx, ctx.MkTactic("smt"), g3);
            if (ar.NumSubgoals != 1 || !ar.Subgoals[0].IsDecidedUnsat)
                throw new TestFailedException();

            ModelConverterTest(ctx);

            // Real num/den test.
            RatNum rn = ctx.MkReal(42, 43);
            Expr inum = rn.Numerator;
            Expr iden = rn.Denominator;
            Console.WriteLine("Numerator: " + inum + " Denominator: " + iden);
            if (inum.ToString() != "42" || iden.ToString() != "43")
                throw new TestFailedException();

            if (rn.ToDecimalString(3) != "0.976?")
                throw new TestFailedException();

            BigIntCheck(ctx, ctx.MkReal("-1231231232/234234333"));
            BigIntCheck(ctx, ctx.MkReal("-123123234234234234231232/234234333"));
            BigIntCheck(ctx, ctx.MkReal("-234234333"));
            BigIntCheck(ctx, ctx.MkReal("234234333/2"));


            string bn = "1234567890987654321";

            if (ctx.MkInt(bn).BigInteger.ToString() != bn)
                throw new TestFailedException();

            if (ctx.MkBV(bn, 128).BigInteger.ToString() != bn)
                throw new TestFailedException();

            if (ctx.MkBV(bn, 32).BigInteger.ToString() == bn)
                throw new TestFailedException();

            // Error handling test.
            try
            {
                IntExpr i = ctx.MkInt("1/2");
                throw new TestFailedException(); // unreachable
            }
            catch (Z3Exception)
            {
            }
        }
Esempio n. 7
0
        public static void FloatingPointExample2(Context ctx)
        {
            Console.WriteLine("FloatingPointExample2");
            FPSort double_sort = ctx.MkFPSort(11, 53);
            FPRMSort rm_sort = ctx.MkFPRoundingModeSort();

            FPRMExpr rm = (FPRMExpr)ctx.MkConst(ctx.MkSymbol("rm"), rm_sort);
            BitVecExpr x = (BitVecExpr)ctx.MkConst(ctx.MkSymbol("x"), ctx.MkBitVecSort(64));
            FPExpr y = (FPExpr)ctx.MkConst(ctx.MkSymbol("y"), double_sort);
            FPExpr fp_val = ctx.MkFP(42, double_sort);

            BoolExpr c1 = ctx.MkEq(y, fp_val);
            BoolExpr c2 = ctx.MkEq(x, ctx.MkFPToBV(rm, y, 64, false));
            BoolExpr c3 = ctx.MkEq(x, ctx.MkBV(42, 64));
            BoolExpr c4 = ctx.MkEq(ctx.MkNumeral(42, ctx.RealSort), ctx.MkFPToReal(fp_val));
            BoolExpr c5 = ctx.MkAnd(c1, c2, c3, c4);
            Console.WriteLine("c5 = " + c5);

            /* Generic solver */
            Solver s = ctx.MkSolver();
            s.Assert(c5);

            Console.WriteLine(s);

            if (s.Check() != Status.SATISFIABLE)
                throw new TestFailedException();

            Console.WriteLine("OK, model: {0}", s.Model.ToString());
        }
Esempio n. 8
0
        public static void FloatingPointExample1(Context ctx)
        {
            Console.WriteLine("FloatingPointExample1");

            FPSort s = ctx.MkFPSort(11, 53);
            Console.WriteLine("Sort: {0}", s);

            FPNum x = (FPNum)ctx.MkNumeral("-1e1", s); /* -1 * 10^1 = -10 */
            FPNum y = (FPNum)ctx.MkNumeral("-10", s); /* -10 */
            FPNum z = (FPNum)ctx.MkNumeral("-1.25p3", s); /* -1.25 * 2^3 = -1.25 * 8 = -10 */
            Console.WriteLine("x={0}; y={1}; z={2}", x.ToString(), y.ToString(), z.ToString());

            BoolExpr a = ctx.MkAnd(ctx.MkFPEq(x, y), ctx.MkFPEq(y, z));
            Check(ctx, ctx.MkNot(a), Status.UNSATISFIABLE);

            /* nothing is equal to NaN according to floating-point
             * equality, so NaN == k should be unsatisfiable. */
            FPExpr k = (FPExpr)ctx.MkConst("x", s);
            FPExpr nan = ctx.MkFPNaN(s);

            /* solver that runs the default tactic for QF_FP. */
            Solver slvr = ctx.MkSolver("QF_FP");
            slvr.Add(ctx.MkFPEq(nan, k));
            if (slvr.Check() != Status.UNSATISFIABLE)
                throw new TestFailedException();
            Console.WriteLine("OK, unsat:" + Environment.NewLine + slvr);

            /* NaN is equal to NaN according to normal equality. */
            slvr = ctx.MkSolver("QF_FP");
            slvr.Add(ctx.MkEq(nan, nan));
            if (slvr.Check() != Status.SATISFIABLE)
                throw new TestFailedException();
            Console.WriteLine("OK, sat:" + Environment.NewLine + slvr);

            /* Let's prove -1e1 * -1.25e3 == +100 */
            x = (FPNum)ctx.MkNumeral("-1e1", s);
            y = (FPNum)ctx.MkNumeral("-1.25p3", s);
            FPExpr x_plus_y = (FPExpr)ctx.MkConst("x_plus_y", s);
            FPNum r = (FPNum)ctx.MkNumeral("100", s);
            slvr = ctx.MkSolver("QF_FP");

            slvr.Add(ctx.MkEq(x_plus_y, ctx.MkFPMul(ctx.MkFPRoundNearestTiesToAway(), x, y)));
            slvr.Add(ctx.MkNot(ctx.MkFPEq(x_plus_y, r)));
            if (slvr.Check() != Status.UNSATISFIABLE)
                throw new TestFailedException();
            Console.WriteLine("OK, unsat:" + Environment.NewLine + slvr);
        }
Esempio n. 9
0
        public static void FiniteDomainExample(Context ctx)
        {
            Console.WriteLine("FiniteDomainExample");

            FiniteDomainSort s = ctx.MkFiniteDomainSort("S", 10);
            FiniteDomainSort t = ctx.MkFiniteDomainSort("T", 10);
            FiniteDomainNum s1 = (FiniteDomainNum)ctx.MkNumeral(1, s);
            FiniteDomainNum t1 = (FiniteDomainNum)ctx.MkNumeral(1, t);
            Console.WriteLine("{0} of size {1}", s, s.Size);
            Console.WriteLine("{0} of size {1}", t, t.Size);
            Console.WriteLine("{0}", s1);
            Console.WriteLine("{0}", t1);
            Console.WriteLine("{0}", s1.Int);
            Console.WriteLine("{0}", t1.Int);
            // But you cannot mix numerals of different sorts
            // even if the size of their domains are the same:
            // Console.WriteLine("{0}", ctx.MkEq(s1, t1));
        }
Esempio n. 10
0
        /// <summary>
        /// Find x and y such that: x ^ y - 103 == x * y
        /// </summary>
        public static void BitvectorExample2(Context ctx)
        {
            Console.WriteLine("BitvectorExample2");

            /* construct x ^ y - 103 == x * y */
            Sort bv_type = ctx.MkBitVecSort(32);
            BitVecExpr x = ctx.MkBVConst("x", 32);
            BitVecExpr y = ctx.MkBVConst("y", 32);
            BitVecExpr x_xor_y = ctx.MkBVXOR(x, y);
            BitVecExpr c103 = (BitVecNum)ctx.MkNumeral("103", bv_type);
            BitVecExpr lhs = ctx.MkBVSub(x_xor_y, c103);
            BitVecExpr rhs = ctx.MkBVMul(x, y);
            BoolExpr ctr = ctx.MkEq(lhs, rhs);

            Console.WriteLine("find values of x and y, such that x ^ y - 103 == x * y");

            /* find a model (i.e., values for x an y that satisfy the constraint */
            Model m = Check(ctx, ctr, Status.SATISFIABLE);
            Console.WriteLine(m);
        }
Esempio n. 11
0
        /// <summary>
        /// Simple bit-vector example.
        /// </summary>
        /// <remarks>
        /// This example disproves that x - 10 &lt;= 0 IFF x &lt;= 10 for (32-bit) machine integers
        /// </remarks>
        public static void BitvectorExample1(Context ctx)
        {
            Console.WriteLine("BitvectorExample1");

            Sort bv_type = ctx.MkBitVecSort(32);
            BitVecExpr x = (BitVecExpr)ctx.MkConst("x", bv_type);
            BitVecNum zero = (BitVecNum)ctx.MkNumeral("0", bv_type);
            BitVecNum ten = ctx.MkBV(10, 32);
            BitVecExpr x_minus_ten = ctx.MkBVSub(x, ten);
            /* bvsle is signed less than or equal to */
            BoolExpr c1 = ctx.MkBVSLE(x, ten);
            BoolExpr c2 = ctx.MkBVSLE(x_minus_ten, zero);
            BoolExpr thm = ctx.MkIff(c1, c2);
            Console.WriteLine("disprove: x - 10 <= 0 IFF x <= 10 for (32-bit) machine integers");
            Disprove(ctx, thm);
        }
Esempio n. 12
0
        /// <summary>
        /// Show how push & pop can be used to create "backtracking" points.
        /// </summary>
        /// <remarks>This example also demonstrates how big numbers can be
        /// created in ctx.</remarks>
        public static void PushPopExample1(Context ctx)
        {
            Console.WriteLine("PushPopExample1");

            /* create a big number */
            IntSort int_type = ctx.IntSort;
            IntExpr big_number = ctx.MkInt("1000000000000000000000000000000000000000000000000000000");

            /* create number 3 */
            IntExpr three = (IntExpr)ctx.MkNumeral("3", int_type);

            /* create x */
            IntExpr x = ctx.MkIntConst("x");

            Solver solver = ctx.MkSolver();

            /* assert x >= "big number" */
            BoolExpr c1 = ctx.MkGe(x, big_number);
            Console.WriteLine("assert: x >= 'big number'");
            solver.Assert(c1);

            /* create a backtracking point */
            Console.WriteLine("push");
            solver.Push();

            /* assert x <= 3 */
            BoolExpr c2 = ctx.MkLe(x, three);
            Console.WriteLine("assert: x <= 3");
            solver.Assert(c2);

            /* context is inconsistent at this point */
            if (solver.Check() != Status.UNSATISFIABLE)
                throw new TestFailedException();

            /* backtrack: the constraint x <= 3 will be removed, since it was
               asserted after the last ctx.Push. */
            Console.WriteLine("pop");
            solver.Pop(1);

            /* the context is consistent again. */
            if (solver.Check() != Status.SATISFIABLE)
                throw new TestFailedException();

            /* new constraints can be asserted... */

            /* create y */
            IntExpr y = ctx.MkIntConst("y");

            /* assert y > x */
            BoolExpr c3 = ctx.MkGt(y, x);
            Console.WriteLine("assert: y > x");
            solver.Assert(c3);

            /* the context is still consistent. */
            if (solver.Check() != Status.SATISFIABLE)
                throw new TestFailedException();
        }
Esempio n. 13
0
        public static void FiniteDomainExample(Context ctx)
        {
            Console.WriteLine("FiniteDomainExample");

            var s = ctx.MkFiniteDomainSort("S", 10);
            var t = ctx.MkFiniteDomainSort("T", 10);
            var s1 = ctx.MkNumeral(1, s);
            var t1 = ctx.MkNumeral(1, t);
            Console.WriteLine("{0}", s);
            Console.WriteLine("{0}", t);
            Console.WriteLine("{0}", s1);
            Console.WriteLine("{0}", ctx.MkNumeral(2, s));
            Console.WriteLine("{0}", t1);
            // But you cannot mix numerals of different sorts
            // even if the size of their domains are the same:
            // Console.WriteLine("{0}", ctx.MkEq(s1, t1));
        }
Esempio n. 14
0
 public void TestBitVectorOps()
 {
     Context z3 = new Context();
     var bv16 = z3.MkBitVecSort(16);
     var c = (BitVecExpr)z3.MkConst("c",bv16);
     var _3 = (BitVecExpr)z3.MkNumeral(3, bv16);
     var _7 = (BitVecExpr)z3.MkNumeral(7, bv16);
     var _1 = (BitVecExpr)z3.MkNumeral(1, bv16);
     var c_and_7 = z3.MkBVAND(c, _7);
     //((1 + (c & 7)) & 3)
     var t = z3.MkBVAND(z3.MkBVAdd(_1, c_and_7), _3);
     var s = t.Simplify(); //comes out as: (1 + (c & 3))
     var t_neq_s = z3.MkNot(z3.MkEq(t, s));
     var solv =z3.MkSolver();
     solv.Assert(t_neq_s);
     Assert.AreEqual(Status.UNSATISFIABLE, solv.Check());
 }