Int expressions
Inheritance: ArithExpr
Ejemplo n.º 1
0
    public void Run()
    {
        Dictionary<string, string> cfg = new Dictionary<string, string>() {
            { "AUTO_CONFIG", "true" } };

        using (Context ctx = new Context(cfg))
        {
            IntExpr[] x = new IntExpr[20];
            IntExpr[] y = new IntExpr[20];

            for (uint i = 0; i < 20; i++)
            {
                x[i] = ctx.MkIntConst(string.Format("x_{0}", i));
                y[i] = ctx.MkIntConst(string.Format("y_{0}", i));
            }

            BoolExpr f = ctx.MkAnd(ctx.MkGe(ctx.MkAdd(x), ctx.MkInt(0)),
                                   ctx.MkGe(ctx.MkAdd(y), ctx.MkInt(0)));

            Console.WriteLine("now: " + ctx.GetParamValue("PP_MAX_DEPTH"));

            ctx.UpdateParamValue("PP_MAX_DEPTH", "1");
            Console.WriteLine(f);

            ctx.UpdateParamValue("PP_MAX_DEPTH", "100");
            ctx.UpdateParamValue("PP_MAX_NUM_LINES", "10");
            Console.WriteLine(f);

            ctx.UpdateParamValue("PP_MAX_NUM_LINES", "20");
            ctx.UpdateParamValue("PP_MAX_WIDTH", "300");
            Console.WriteLine(f);

            Console.WriteLine("now: " + ctx.GetParamValue("PP_MAX_WIDTH"));
        }
    }
Ejemplo n.º 2
0
    public void Run()
    {
        Dictionary<string, string> cfg = new Dictionary<string, string>() {
            { "AUTO_CONFIG", "true" } };

        using (Context ctx = new Context(cfg))
        {
            IntExpr[] X = new IntExpr[5];
            for (uint i = 0; i < 5; i++)
                X[i] = ctx.MkIntConst(string.Format("x_{0}", i));

            RealExpr[] Y = new RealExpr[5];
            for (uint i = 0; i < 5; i++)
                Y[i] = ctx.MkRealConst(string.Format("y_{0}", i));

            BoolExpr[] P = new BoolExpr[5];
            for (uint i = 0; i < 5; i++)
                P[i] = ctx.MkBoolConst(string.Format("p_{0}", i));

            foreach (Expr x in X)
                Console.WriteLine(x);
            foreach (Expr x in Y)
                Console.WriteLine(x);
            foreach (Expr x in P)
                Console.WriteLine(x);

            foreach (ArithExpr y in Y)
                Console.WriteLine(ctx.MkPower(y, ctx.MkReal(2)));

            ArithExpr[] Yp = new ArithExpr[Y.Length];
            for (uint i = 0; i < Y.Length; i++)
                Yp[i] = ctx.MkPower(Y[i], ctx.MkReal(2));
            Console.WriteLine(ctx.MkAdd(Yp));
        }
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Translates an ASTVector into a IntExpr[]
        /// </summary>
        public IntExpr[] ToIntExprArray()
        {
            uint n = Size;

            IntExpr[] res = new IntExpr[n];
            for (uint i = 0; i < n; i++)
            {
                res[i] = (IntExpr)Expr.Create(this.Context, this[i].NativeObject);
            }
            return(res);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create an expression representing <c>t1 rem t2</c>.
        /// </summary>
        /// <remarks>The arguments must have int type.</remarks>
        public IntExpr MkRem(IntExpr t1, IntExpr t2)
        {
            Contract.Requires(t1 != null);
            Contract.Requires(t2 != null);
            Contract.Ensures(Contract.Result<IntExpr>() != null);

            CheckContextMatch(t1);
            CheckContextMatch(t2);
            return new IntExpr(this, Native.Z3_mk_rem(nCtx, t1.NativeObject, t2.NativeObject));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Coerce an integer to a real.
        /// </summary>
        /// <remarks>
        /// There is also a converse operation exposed. It follows the semantics prescribed by the SMT-LIB standard.
        ///
        /// You can take the floor of a real by creating an auxiliary integer Term <c>k</c> and
        /// and asserting <c>MakeInt2Real(k) &lt;= t1 &lt; MkInt2Real(k)+1</c>.
        /// The argument must be of integer sort.
        /// </remarks>
        public RealExpr MkInt2Real(IntExpr t)
        {
            Contract.Requires(t != null);
            Contract.Ensures(Contract.Result<RealExpr>() != null);

            CheckContextMatch(t);
            return new RealExpr(this, Native.Z3_mk_int2real(nCtx, t.NativeObject));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Create an <paramref name="n"/> bit bit-vector from the integer argument <paramref name="t"/>.
        /// </summary>
        /// <remarks>
        /// NB. This function is essentially treated as uninterpreted. 
        /// So you cannot expect Z3 to precisely reflect the semantics of this function
        /// when solving constraints with this function.
        /// 
        /// The argument must be of integer sort.
        /// </remarks>
        public BitVecExpr MkInt2BV(uint n, IntExpr t)
        {
            Contract.Requires(t != null);
            Contract.Ensures(Contract.Result<BitVecExpr>() != null);

            CheckContextMatch(t);
            return new BitVecExpr(this, Native.Z3_mk_int2bv(nCtx, n, t.NativeObject));
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Conversion of a real-sorted significand and an integer-sorted exponent into a term of FloatingPoint sort.
 /// </summary>
 /// <remarks>
 /// Produces a term that represents the conversion of sig * 2^exp into a 
 /// floating-point term of sort s. If necessary, the result will be rounded
 /// according to rounding mode rm.
 /// </remarks>
 /// <param name="rm">RoundingMode term.</param>
 /// <param name="exp">Exponent term of Int sort.</param>
 /// <param name="sig">Significand term of Real sort.</param>        
 /// <param name="s">FloatingPoint sort.</param>
 public BitVecExpr MkFPToFP(FPRMExpr rm, IntExpr exp, RealExpr sig, FPSort s)
 {
     Contract.Ensures(Contract.Result<BitVecExpr>() != null);
     return new BitVecExpr(this, Native.Z3_mk_fpa_to_fp_int_real(this.nCtx, rm.NativeObject, exp.NativeObject, sig.NativeObject, s.NativeObject));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Extract subsequence.
 /// </summary>
 public SeqExpr MkExtract(SeqExpr s, IntExpr offset, IntExpr length)
 {
     Contract.Requires(s != null);
     Contract.Requires(offset != null);
     Contract.Requires(length != null);
     Contract.Ensures(Contract.Result<SeqExpr>() != null);
     CheckContextMatch(s, offset, length);
     return new SeqExpr(this, Native.Z3_mk_seq_extract(nCtx, s.NativeObject, offset.NativeObject, length.NativeObject));
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Retrieve sequence of length one at index.
 /// </summary>
 public SeqExpr MkAt(SeqExpr s, IntExpr index)
 {
     Contract.Requires(s != null);
     Contract.Requires(index != null);
     Contract.Ensures(Contract.Result<SeqExpr>() != null);
     CheckContextMatch(s, index);
     return new SeqExpr(this, Native.Z3_mk_seq_at(nCtx, s.NativeObject, index.NativeObject));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// A basic example of how to use quantifiers.
        /// </summary>
        static void QuantifierExample1(Context ctx)
        {
            Console.WriteLine("QuantifierExample");

            Sort[] types = new Sort[3];
            IntExpr[] xs = new IntExpr[3];
            Symbol[] names = new Symbol[3];
            IntExpr[] vars = new IntExpr[3];

            for (uint j = 0; j < 3; j++)
            {
                types[j] = ctx.IntSort;
                names[j] = ctx.MkSymbol(String.Format("x_{0}", j));
                xs[j] = (IntExpr)ctx.MkConst(names[j], types[j]);
                vars[j] = (IntExpr)ctx.MkBound(2 - j, types[j]); // <-- vars reversed!
            }

            Expr body_vars = ctx.MkAnd(ctx.MkEq(ctx.MkAdd(vars[0], ctx.MkInt(1)), ctx.MkInt(2)),
                                        ctx.MkEq(ctx.MkAdd(vars[1], ctx.MkInt(2)),
                                                       ctx.MkAdd(vars[2], ctx.MkInt(3))));

            Expr body_const = ctx.MkAnd(ctx.MkEq(ctx.MkAdd(xs[0], ctx.MkInt(1)), ctx.MkInt(2)),
                                        ctx.MkEq(ctx.MkAdd(xs[1], ctx.MkInt(2)),
                                                       ctx.MkAdd(xs[2], ctx.MkInt(3))));

            Expr x = ctx.MkForall(types, names, body_vars, 1, null, null, ctx.MkSymbol("Q1"), ctx.MkSymbol("skid1"));
            Console.WriteLine("Quantifier X: " + x.ToString());

            Expr y = ctx.MkForall(xs, body_const, 1, null, null, ctx.MkSymbol("Q2"), ctx.MkSymbol("skid2"));
            Console.WriteLine("Quantifier Y: " + y.ToString());
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Sudoku solving example.
        /// </summary>
        static void SudokuExample(Context ctx)
        {
            Console.WriteLine("SudokuExample");

            // 9x9 matrix of integer variables
            IntExpr[][] X = new IntExpr[9][];
            for (uint i = 0; i < 9; i++)
            {
                X[i] = new IntExpr[9];
                for (uint j = 0; j < 9; j++)
                    X[i][j] = (IntExpr)ctx.MkConst(ctx.MkSymbol("x_" + (i + 1) + "_" + (j + 1)), ctx.IntSort);
            }

            // each cell contains a value in {1, ..., 9}
            Expr[][] cells_c = new Expr[9][];
            for (uint i = 0; i < 9; i++)
            {
                cells_c[i] = new BoolExpr[9];
                for (uint j = 0; j < 9; j++)
                    cells_c[i][j] = ctx.MkAnd(ctx.MkLe(ctx.MkInt(1), X[i][j]),
                                              ctx.MkLe(X[i][j], ctx.MkInt(9)));
            }

            // each row contains a digit at most once
            BoolExpr[] rows_c = new BoolExpr[9];
            for (uint i = 0; i < 9; i++)
                rows_c[i] = ctx.MkDistinct(X[i]);

            // each column contains a digit at most once
            BoolExpr[] cols_c = new BoolExpr[9];
            for (uint j = 0; j < 9; j++)
            {
                IntExpr[] column = new IntExpr[9];
                for (uint i = 0; i < 9; i++)
                    column[i] = X[i][j];

                cols_c[j] = ctx.MkDistinct(column);
            }

            // each 3x3 square contains a digit at most once
            BoolExpr[][] sq_c = new BoolExpr[3][];
            for (uint i0 = 0; i0 < 3; i0++)
            {
                sq_c[i0] = new BoolExpr[3];
                for (uint j0 = 0; j0 < 3; j0++)
                {
                    IntExpr[] square = new IntExpr[9];
                    for (uint i = 0; i < 3; i++)
                        for (uint j = 0; j < 3; j++)
                            square[3 * i + j] = X[3 * i0 + i][3 * j0 + j];
                    sq_c[i0][j0] = ctx.MkDistinct(square);
                }
            }

            BoolExpr sudoku_c = ctx.MkTrue();
            foreach (BoolExpr[] t in cells_c)
                sudoku_c = ctx.MkAnd(ctx.MkAnd(t), sudoku_c);
            sudoku_c = ctx.MkAnd(ctx.MkAnd(rows_c), sudoku_c);
            sudoku_c = ctx.MkAnd(ctx.MkAnd(cols_c), sudoku_c);
            foreach (BoolExpr[] t in sq_c)
                sudoku_c = ctx.MkAnd(ctx.MkAnd(t), sudoku_c);

            // sudoku instance, we use '0' for empty cells
            int[,] instance = {{0,0,0,0,9,4,0,3,0},
                               {0,0,0,5,1,0,0,0,7},
                               {0,8,9,0,0,0,0,4,0},
                               {0,0,0,0,0,0,2,0,8},
                               {0,6,0,2,0,1,0,5,0},
                               {1,0,2,0,0,0,0,0,0},
                               {0,7,0,0,0,0,5,2,0},
                               {9,0,0,0,6,5,0,0,0},
                               {0,4,0,9,7,0,0,0,0}};

            BoolExpr instance_c = ctx.MkTrue();
            for (uint i = 0; i < 9; i++)
                for (uint j = 0; j < 9; j++)
                    instance_c = ctx.MkAnd(instance_c,
                        (BoolExpr)
                        ctx.MkITE(ctx.MkEq(ctx.MkInt(instance[i, j]), ctx.MkInt(0)),
                                    ctx.MkTrue(),
                                    ctx.MkEq(X[i][j], ctx.MkInt(instance[i, j]))));

            Solver s = ctx.MkSolver();
            s.Assert(sudoku_c);
            s.Assert(instance_c);

            if (s.Check() == Status.SATISFIABLE)
            {
                Model m = s.Model;
                Expr[,] R = new Expr[9, 9];
                for (uint i = 0; i < 9; i++)
                    for (uint j = 0; j < 9; j++)
                        R[i, j] = m.Evaluate(X[i][j]);
                Console.WriteLine("Sudoku solution:");
                for (uint i = 0; i < 9; i++)
                {
                    for (uint j = 0; j < 9; j++)
                        Console.Write(" " + R[i, j]);
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("Failed to solve sudoku");
                throw new TestFailedException();
            }
        }
Ejemplo n.º 12
0
    protected void Clique(Context ctx, Edge[] edges, uint n)
    {
        uint num = 0;
        foreach (Edge e in edges)
        {
            if (e.v0 >= num)
                num = e.v0 + 1;
            if (e.v1 >= num)
                num = e.v1 + 1;
        }

        Solver S = ctx.MkSolver();

        IntExpr [] In = new IntExpr[num];

        for (uint i = 0; i < num; i++)
        {
            In[i] = ctx.MkIntConst(String.Format("in_{0}", i));
            S.Assert(ctx.MkLe(ctx.MkInt(0), In[i]));
            S.Assert(ctx.MkLe(In[i], ctx.MkInt(1)));
        }

        ArithExpr sum = ctx.MkInt(0);
        foreach (IntExpr e in In)
            sum = ctx.MkAdd(sum, e);
        S.Assert(ctx.MkGe(sum, ctx.MkInt(n)));

        IntNum[][] matrix = new IntNum[num][];

        for (uint i = 0; i < num; i++)
        {
            matrix[i] = new IntNum[i];
            for (uint j = 0; j < i; j++)
                matrix[i][j] = ctx.MkInt(0);
        }

        foreach (Edge e in edges)
        {
            uint s = e.v0;
            uint t = e.v1;
            if (s < t) {
                s = e.v1;
                t = e.v0;
            }
            matrix[s][t] = ctx.MkInt(1);
        }

        for (uint i = 0; i < num; i++)
            for (uint j = 0; j < i; j++)
                if (i != j)
                    if (matrix[i][j].Int == 0)
                        S.Assert(ctx.MkOr(ctx.MkEq(In[i], ctx.MkInt(0)),
                                          ctx.MkEq(In[j], ctx.MkInt(0))));

        Status r = S.Check();
        if (r == Status.UNSATISFIABLE)
            Console.WriteLine("no solution");
        else if (r == Status.UNKNOWN)
        {
            Console.Write("failed");
            Console.WriteLine(S.ReasonUnknown);
        }
        else
        {
            Console.WriteLine("solution found");
            Model m = S.Model;

            Console.Write("{ ");
            foreach (FuncDecl cfd in m.ConstDecls)
            {
                IntNum q = (IntNum)m.ConstInterp(cfd);
                if (q.Int == 1)
                    Console.Write(" " + cfd.Name);
            }
            Console.WriteLine(" }");

            Console.Write("{ ");
            for (uint i = 0; i < num; i++)
            {
                IntNum q = (IntNum)m.Evaluate(In[i]);
                if (q.Int == 1)
                    Console.Write(" " + In[i]);
            }
            Console.WriteLine(" }");
        }
    }
Ejemplo n.º 13
0
 /// <summary>
 /// Translates an ASTVector into a IntExpr[]
 /// </summary>    
 public IntExpr[] ToIntExprArray()
 {
     uint n = Size;
     IntExpr[] res = new IntExpr[n];
     for (uint i = 0; i < n; i++)
         res[i] = (IntExpr)Expr.Create(this.Context, this[i].NativeObject);
     return res;
 }