MkLe() public method

Create an expression representing t1 <= t2
public MkLe ( ArithExpr t1, ArithExpr t2 ) : BoolExpr
t1 ArithExpr
t2 ArithExpr
return BoolExpr
Esempio n. 1
0
    public void Run()
    {
        Dictionary<string, string> settings = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } };

        using (Context ctx = new Context(settings))
        {
            IntExpr a = ctx.MkIntConst("a");
            IntExpr b = ctx.MkIntConst("b");
            IntExpr c = ctx.MkIntConst("c");
            RealExpr d = ctx.MkRealConst("d");
            RealExpr e = ctx.MkRealConst("e");

            BoolExpr q = ctx.MkAnd(
                ctx.MkGt(a, ctx.MkAdd(b, ctx.MkInt(2))),
                ctx.MkEq(a, ctx.MkAdd(ctx.MkMul(ctx.MkInt(2), c), ctx.MkInt(10))),
                ctx.MkLe(ctx.MkAdd(c, b), ctx.MkInt(1000)),
                ctx.MkGe(d, e));

            Solver s = ctx.MkSolver();
            s.Assert(q);

            Console.WriteLine(s.Check());

            Console.WriteLine(s.Model);
        }
    }
Esempio n. 2
0
    public void Run()
    {
        Dictionary<string, string> cfg = new Dictionary<string, string>() {
            { "AUTO_CONFIG", "true" } };

        using (Context ctx = new Context(cfg))
        {
            ArithExpr[] Q = new ArithExpr[8];
            for (uint i = 0; i < 8; i++)
                Q[i] = ctx.MkIntConst(string.Format("Q_{0}", i + 1));

            BoolExpr[] val_c = new BoolExpr[8];
            for (uint i = 0; i < 8; i++)
                val_c[i] = ctx.MkAnd(ctx.MkLe(ctx.MkInt(1), Q[i]),
                                     ctx.MkLe(Q[i], ctx.MkInt(8)));

            BoolExpr col_c = ctx.MkDistinct(Q);

            BoolExpr[][] diag_c = new BoolExpr[8][];
            for (uint i = 0; i < 8; i++)
            {
                diag_c[i] = new BoolExpr[i];
                for (uint j = 0; j < i; j++)
                    diag_c[i][j] = (BoolExpr)ctx.MkITE(ctx.MkEq(ctx.MkInt(i), ctx.MkInt(j)),
                                             ctx.MkTrue(),
                                             ctx.MkAnd(ctx.MkNot(ctx.MkEq(ctx.MkSub(Q[i], Q[j]),
                                                                          ctx.MkSub(ctx.MkInt(i), ctx.MkInt(j)))),
                                                       ctx.MkNot(ctx.MkEq(ctx.MkSub(Q[i], Q[j]),
                                                                          ctx.MkSub(ctx.MkInt(j), ctx.MkInt(i))))));
            }

            Solver s = ctx.MkSolver();
            s.Assert(val_c);
            s.Assert(col_c);
            foreach (var c in diag_c)
                s.Assert(c);

            Console.WriteLine(s.Check());
            Console.WriteLine(s.Model);
        }
    }
Esempio n. 3
0
        public static void ProveExample2(Context ctx)
        {
            Console.WriteLine("ProveExample2");

            /* declare function g */
            Sort I = ctx.IntSort;

            FuncDecl g = ctx.MkFuncDecl("g", I, I);

            /* create x, y, and z */
            IntExpr x = ctx.MkIntConst("x");
            IntExpr y = ctx.MkIntConst("y");
            IntExpr z = ctx.MkIntConst("z");

            /* create gx, gy, gz */
            Expr gx = ctx.MkApp(g, x);
            Expr gy = ctx.MkApp(g, y);
            Expr gz = ctx.MkApp(g, z);

            /* create zero */
            IntExpr zero = ctx.MkInt(0);

            /* assert not(g(g(x) - g(y)) = g(z)) */
            ArithExpr gx_gy = ctx.MkSub((IntExpr)gx, (IntExpr)gy);
            Expr ggx_gy = ctx.MkApp(g, gx_gy);
            BoolExpr eq = ctx.MkEq(ggx_gy, gz);
            BoolExpr c1 = ctx.MkNot(eq);

            /* assert x + z <= y */
            ArithExpr x_plus_z = ctx.MkAdd(x, z);
            BoolExpr c2 = ctx.MkLe(x_plus_z, y);

            /* assert y <= x */
            BoolExpr c3 = ctx.MkLe(y, x);

            /* prove z < 0 */
            BoolExpr f = ctx.MkLt(z, zero);
            Console.WriteLine("prove: not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < 0");
            Prove(ctx, f, c1, c2, c3);

            /* disprove z < -1 */
            IntExpr minus_one = ctx.MkInt(-1);
            f = ctx.MkLt(z, minus_one);
            Console.WriteLine("disprove: not(g(g(x) - g(y)) = g(z)), x + z <= y <= x implies z < -1");
            Disprove(ctx, f, c1, c2, c3);
        }
Esempio n. 4
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();
            }
        }
Esempio n. 5
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. 6
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(" }");
        }
    }
 public override BoolExpr toZ3Bool(Context ctx)
 {
     switch (this.comparison_operator)
     {
         case ComparisonOperator.EQ: return ctx.MkEq(this.arithmetic_operand1.toZ3Int(ctx), this.arithmetic_operand2.toZ3Int(ctx));
         case ComparisonOperator.NEQ: return ctx.MkNot(ctx.MkEq(this.arithmetic_operand1.toZ3Int(ctx), this.arithmetic_operand2.toZ3Int(ctx)));
         case ComparisonOperator.LEQ: return ctx.MkLe(this.arithmetic_operand1.toZ3Int(ctx), this.arithmetic_operand2.toZ3Int(ctx));
         case ComparisonOperator.LT: return ctx.MkLt(this.arithmetic_operand1.toZ3Int(ctx), this.arithmetic_operand2.toZ3Int(ctx));
         case ComparisonOperator.GEQ: return ctx.MkGe(this.arithmetic_operand1.toZ3Int(ctx), this.arithmetic_operand2.toZ3Int(ctx));
         case ComparisonOperator.GT: return ctx.MkGt(this.arithmetic_operand1.toZ3Int(ctx), this.arithmetic_operand2.toZ3Int(ctx));
         default: throw new ArgumentOutOfRangeException();
     }
 }
    // CMW: get_implied_equalities is deprecated.
    ///*!
    //   \brief Extract implied equalities.
    //*/
    public void get_implied_equalities_example()
    {
        if (this.z3 != null)
        {
            this.z3.Dispose();
        }
        Config p = new Config();
        p.SetParam("ARITH_EQ_BOUNDS","true");
        this.z3 = new Context(p);

        Sort int_sort = z3.MkIntSort();
        Expr a = mk_int_var("a");
        Expr b = mk_int_var("b");
        Expr c = mk_int_var("c");
        Expr d = mk_int_var("d");
        FuncDecl f = z3.MkFuncDecl("f", int_sort, int_sort);
        Expr fa = z3.MkApp(f,a);
        Expr fb = z3.MkApp(f,b);
        Expr fc = z3.MkApp(f,c);
        Expr[] Exprs = new Expr[]{ a, b, c, d, fa, fb, fc };
        uint[] class_ids;

        solver.Assert(z3.MkEq(a, b));
        solver.Assert(z3.MkEq(b, c));
        solver.Assert(z3.MkLe((ArithExpr)fc, (ArithExpr)a));
        solver.Assert(z3.MkLe((ArithExpr)b, (ArithExpr)fb));
        int num_Exprs = Exprs.Length;

        z3.GetImpliedEqualities(Exprs, out class_ids);
        for (int i = 0; i < num_Exprs; ++i) {
            Console.WriteLine("Class {0} |-> {1}", Exprs[i], class_ids[i]);
        }
    }
 public override void ToSMTConstraints(Context z3Context, Solver z3Solver, int alphabetSize, VariableCache variableGenerator)
 {
     this.constraintVariable = variableGenerator.GetFreshVariableName();
     ArithExpr myVariable = z3Context.MkIntConst(this.constraintVariable);
     z3Solver.Assert(z3Context.MkLe(z3Context.MkInt(0), myVariable));
     z3Solver.Assert(z3Context.MkLe(myVariable, z3Context.MkInt(1)));
 }
 public void ToSMTConstraints(Context z3Context, Solver z3Solver, int alphabetSize, VariableCache variableGenerator)
 {
     this.constraintVariable = variableGenerator.GetCharChoiceVariable(this.originalValue);
     ArithExpr myVariable = z3Context.MkIntConst(this.constraintVariable);
     z3Solver.Assert(z3Context.MkLe(z3Context.MkInt(0), myVariable));
     z3Solver.Assert(z3Context.MkLe(myVariable, z3Context.MkInt(alphabetSize - 1)));
 }
        public void ToSMTConstraints(Context z3Context, Solver z3Solver, int alphabetSize, VariableCache variableGenerator)
        {
            this.constraintVariable = variableGenerator.GetIntegerChoiceVariable(this.originalValue);
            ArithExpr myVariable = z3Context.MkIntConst(this.constraintVariable);
            // For now, just concretize in the range [floor(origVal / 2), ceil(origVal * 1.5)]
            z3Solver.Assert(z3Context.MkLe(z3Context.MkInt((int)(this.originalValue * 0.5)), myVariable));
            z3Solver.Assert(z3Context.MkLe(myVariable, z3Context.MkInt((int)(this.originalValue * 1.5))));

            if (this.includeZero == false)
            {
                z3Solver.Assert(z3Context.MkNot(z3Context.MkEq(z3Context.MkInt(0), myVariable)));
            }
        }
 public void ToSMTConstraints(Context z3Context, Solver z3Solver, int alphabetSize, VariableCache variableGenerator)
 {
     this.constraintVariable = variableGenerator.GetStringChoiceVariable(this.originalValue);
     ArithExpr myVariable = z3Context.MkIntConst(this.constraintVariable);
     z3Solver.Assert(z3Context.MkLe(z3Context.MkInt(0), myVariable));
     /* We have |s| * |\Sigma| options for concretizing this string
      * Thus, since we are 0-based: originalValue.Length * alphabet - 1 */
     z3Solver.Assert(z3Context.MkLe(myVariable, z3Context.MkInt(this.originalValue.Length * alphabetSize - 1)));
 }
Esempio n. 13
0
        public static Expr LeAndEe(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.MkLe((ArithExpr)a, (ArithExpr)b), ctx.MkEq((ArithExpr)c, (ArithExpr)d)));
                s.Check();

                BoolExpr testing = ctx.MkAnd(ctx.MkLe((ArithExpr)a, (ArithExpr)b), ctx.MkEq((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;
        }