public MkBV ( int v, uint size ) : |
||
v | int | value of the numeral. |
size | uint | the size of the bit-vector |
Résultat |
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 32); BitVecExpr y = ctx.MkBVConst("y", 32); BitVecExpr two = ctx.MkBV(2, 32); BitVecExpr three = ctx.MkBV(3, 32); BitVecExpr tf = ctx.MkBV(24, 32); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(ctx.MkBVLSHR(x, two), three)); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); s = ctx.MkSolver(); s.Assert(ctx.MkEq(ctx.MkBVSHL(x, two), three)); Console.WriteLine(s.Check()); s = ctx.MkSolver(); s.Assert(ctx.MkEq(ctx.MkBVRotateLeft(x, two), three)); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); s = ctx.MkSolver(); s.Assert(ctx.MkEq(ctx.MkBVSHL(x, two), tf)); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 32); BitVecExpr[] powers = new BitVecExpr[32]; for (uint i = 0; i < 32; i++) powers[i] = ctx.MkBVSHL(ctx.MkBV(1, 32), ctx.MkBV(i, 32)); BoolExpr step_zero = ctx.MkEq(ctx.MkBVAND(x, ctx.MkBVSub(x, ctx.MkBV(1, 32))), ctx.MkBV(0, 32)); BoolExpr fast = ctx.MkAnd(ctx.MkNot(ctx.MkEq(x, ctx.MkBV(0, 32))), step_zero); BoolExpr slow = ctx.MkFalse(); foreach (BitVecExpr p in powers) slow = ctx.MkOr(slow, ctx.MkEq(x, p)); TestDriver.CheckString(fast, "(and (not (= x #x00000000)) (= (bvand x (bvsub x #x00000001)) #x00000000))"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkNot(ctx.MkEq(fast, slow))); TestDriver.CheckUNSAT(s.Check()); s = ctx.MkSolver(); s.Assert(ctx.MkNot(step_zero)); TestDriver.CheckSAT(s.Check()); } }
public void Run() { using (Context ctx = new Context()) { var s = ctx.MkFixedpoint(); BoolSort B = ctx.BoolSort; Sort BV8 = ctx.MkBitVecSort(8); FuncDecl edge = ctx.MkFuncDecl("edge", new Sort[]{BV8, BV8}, B); FuncDecl path = ctx.MkFuncDecl("path", new Sort[]{BV8, BV8}, B); BitVecExpr x = (BitVecExpr)ctx.MkBound(0,BV8); BitVecExpr y = (BitVecExpr)ctx.MkBound(1,BV8); BitVecExpr z = (BitVecExpr)ctx.MkBound(2,BV8); s.RegisterRelation(edge); s.RegisterRelation(path); s.AddRule(ctx.MkImplies((BoolExpr)edge[x,y],(BoolExpr)path[x,y])); s.AddRule(ctx.MkImplies(ctx.MkAnd((BoolExpr)path[x,y],(BoolExpr)path[y,z]), (BoolExpr)path[x,z])); for (int i = 0; i < 128; ++i) { s.AddRule((BoolExpr)edge[ctx.MkBV(i,8),ctx.MkBV(i+1,8)]); } Console.WriteLine(s.Query((BoolExpr)path[ctx.MkBV(0,8),ctx.MkBV(129,8)])); Console.WriteLine(s.GetAnswer()); Console.WriteLine(s.Query((BoolExpr)path[ctx.MkBV(0,8),ctx.MkBV(128,8)])); Console.WriteLine(s.GetAnswer()); Console.WriteLine(s.Query((BoolExpr)path[x,ctx.MkBV(20,8)])); Console.WriteLine(s.GetAnswer()); Console.WriteLine(s.Query(ctx.MkAnd((BoolExpr)path[x,y], (BoolExpr)path[y,ctx.MkBV(20,8)]))); Console.WriteLine(s.GetAnswer()); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 32); BitVecExpr y = ctx.MkBVConst("y", 32); BoolExpr q = ctx.MkAnd(ctx.MkEq(ctx.MkBVAdd(x, y), ctx.MkBV(2, 32)), ctx.MkBVSGT(x, ctx.MkBV(0, 32)), ctx.MkBVSGT(y, ctx.MkBV(0, 32))); Console.WriteLine(q); Solver s = ctx.MkSolver(); s.Assert(q); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); q = ctx.MkEq(ctx.MkBVAND(x, y), ctx.MkBVNeg(y)); Console.WriteLine(q); s = ctx.MkSolver(); s.Assert(q); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); q = ctx.MkBVSLT(x, ctx.MkBV(0, 32)); Console.WriteLine(q); s = ctx.MkSolver(); s.Assert(q); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); q = ctx.MkBVULT(x, ctx.MkBV(0, 32)); Console.WriteLine(q); s = ctx.MkSolver(); s.Assert(q); Console.WriteLine(s.Check()); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 16); BitVecExpr y = ctx.MkBVConst("y", 16); Params p = ctx.MkParams(); p.Add(":mul2concat", true); Tactic t = ctx.Then(ctx.UsingParams(ctx.MkTactic("simplify"), p), ctx.MkTactic("solve-eqs"), ctx.MkTactic("bit-blast"), ctx.MkTactic("aig"), ctx.MkTactic("sat")); Solver s = ctx.MkSolver(t); s.Assert(ctx.MkEq(ctx.MkBVAdd(ctx.MkBVMul(x, ctx.MkBV(32, 16)), y), ctx.MkBV(13, 16))); s.Assert(ctx.MkBVSLT(ctx.MkBVAND(x, y), ctx.MkBV(10, 16))); s.Assert(ctx.MkBVSGT(y, ctx.MkBV(-100, 16))); Console.WriteLine(s.Check()); Model m = s.Model; Console.WriteLine(m); Console.WriteLine(ctx.MkBVAdd(ctx.MkBVMul(x, ctx.MkBV(32, 16)), y) + " == " + m.Evaluate(ctx.MkBVAdd(ctx.MkBVMul(x, ctx.MkBV(32, 16)), y))); Console.WriteLine(ctx.MkBVAND(x, y) + " == " + m.Evaluate(ctx.MkBVAND(x, y)) ); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; Context ctx = new Context(cfg); BitVecNum b = ctx.MkBV(0xbadc0de, 32); Console.WriteLine(b.SExpr()); Console.WriteLine(b); Console.WriteLine(b.Int64); }
public void Run() { using (Context ctx = new Context()) { var s = ctx.MkFixedpoint(); BoolSort B = ctx.BoolSort; Sort BV8 = ctx.MkBitVecSort(8); FuncDecl f = ctx.MkFuncDecl("f", BV8, B); FuncDecl g = ctx.MkFuncDecl("g", BV8, B); BitVecExpr b0 = (BitVecExpr)ctx.MkBound(0,BV8); s.RegisterRelation(f); s.RegisterRelation(g); s.AddRule((BoolExpr)f[b0]); BitVecExpr mask0 = ctx.MkBV(0xFE,8); BoolExpr even = ctx.MkEq(b0,ctx.MkBVAND(b0,mask0)); s.AddRule(ctx.MkImplies(ctx.MkAnd((BoolExpr)f[b0],even), (BoolExpr)g[b0])); Console.WriteLine(s.Query((BoolExpr)g[b0])); Console.WriteLine(s.GetAnswer()); } }
/// <summary> /// Simple bit-vector example. /// </summary> /// <remarks> /// This example disproves that x - 10 <= 0 IFF x <= 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); }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 16); BitVecExpr y = ctx.MkBVConst("y", 16); Tactic t = ctx.Then(ctx.MkTactic("simplify"), ctx.MkTactic("solve-eqs"), ctx.MkTactic("bit-blast"), ctx.MkTactic("sat")); Solver s = ctx.MkSolver(t); s.Assert(ctx.MkEq(ctx.MkBVOR(x, y), ctx.MkBV(13, 16))); s.Assert(ctx.MkBVSGT(x, y)); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 32); BitVecExpr y = ctx.MkBVConst("y", 32); BitVecExpr zero = ctx.MkBV(0, 32); BoolExpr trick = ctx.MkBVSLT(ctx.MkBVXOR(x, y), zero); BoolExpr opposite = ctx.MkOr(ctx.MkAnd(ctx.MkBVSLT(x, zero), ctx.MkBVSGE(y, zero)), ctx.MkAnd(ctx.MkBVSGE(x, zero), ctx.MkBVSLT(y, zero))); Solver s = ctx.MkSolver(); s.Assert(ctx.MkNot(ctx.MkEq(trick, opposite))); Console.WriteLine(s.Check()); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { BitVecExpr x = ctx.MkBVConst("x", 16); BitVecExpr y = ctx.MkBVConst("y", 16); Console.WriteLine(ctx.MkBVAdd(x, ctx.MkBV(2, 16))); Console.WriteLine(ctx.MkBVSub(ctx.MkBVAdd(x, y), ctx.MkBV(1, 16)).Simplify()); BitVecExpr a = ctx.MkBV(-1, 16); BitVecExpr b = ctx.MkBV(65535, 16); Console.WriteLine(ctx.MkEq(a, b).Simplify()); a = ctx.MkBV(-1, 32); b = ctx.MkBV(65535, 32); Console.WriteLine(ctx.MkEq(a, b).Simplify()); } }
/// <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) { } }
/// <summary> /// A simple array example. /// </summary> /// <param name="ctx"></param> static void ArrayExample1(Context ctx) { Console.WriteLine("ArrayExample1"); Goal g = ctx.MkGoal(true); ArraySort asort = ctx.MkArraySort(ctx.IntSort, ctx.MkBitVecSort(32)); ArrayExpr aex = (ArrayExpr)ctx.MkConst(ctx.MkSymbol("MyArray"), asort); Expr sel = ctx.MkSelect(aex, ctx.MkInt(0)); g.Assert(ctx.MkEq(sel, ctx.MkBV(42, 32))); Symbol xs = ctx.MkSymbol("x"); IntExpr xc = (IntExpr)ctx.MkConst(xs, ctx.IntSort); Symbol fname = ctx.MkSymbol("f"); Sort[] domain = { ctx.IntSort }; FuncDecl fd = ctx.MkFuncDecl(fname, domain, ctx.IntSort); Expr[] fargs = { ctx.MkConst(xs, ctx.IntSort) }; IntExpr fapp = (IntExpr)ctx.MkApp(fd, fargs); g.Assert(ctx.MkEq(ctx.MkAdd(xc, fapp), ctx.MkInt(123))); Solver s = ctx.MkSolver(); foreach (BoolExpr a in g.Formulas) s.Assert(a); Console.WriteLine("Solver: " + s); Status q = s.Check(); Console.WriteLine("Status: " + q); if (q != Status.SATISFIABLE) throw new TestFailedException(); Console.WriteLine("Model = " + s.Model); Console.WriteLine("Interpretation of MyArray:\n" + s.Model.FuncInterp(aex.FuncDecl)); Console.WriteLine("Interpretation of x:\n" + s.Model.ConstInterp(xc)); Console.WriteLine("Interpretation of f:\n" + s.Model.FuncInterp(fd)); Console.WriteLine("Interpretation of MyArray as Term:\n" + s.Model.FuncInterp(aex.FuncDecl)); }
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()); }
/// <summary> /// Demonstrate how to use <code>Push</code>and <code>Pop</code>to /// control the size of models. /// </summary> /// <remarks>Note: this test is specialized to 32-bit bitvectors.</remarks> public static void CheckSmall(Context ctx, Solver solver, BitVecExpr[] to_minimize) { Sort bv32 = ctx.MkBitVecSort(32); int num_Exprs = to_minimize.Length; UInt32[] upper = new UInt32[num_Exprs]; UInt32[] lower = new UInt32[num_Exprs]; BitVecExpr[] values = new BitVecExpr[num_Exprs]; for (int i = 0; i < upper.Length; ++i) { upper[i] = UInt32.MaxValue; lower[i] = 0; } bool some_work = true; int last_index = -1; UInt32 last_upper = 0; while (some_work) { solver.Push(); bool check_is_sat = true; while (check_is_sat && some_work) { // Assert all feasible bounds. for (int i = 0; i < num_Exprs; ++i) { solver.Assert(ctx.MkBVULE(to_minimize[i], ctx.MkBV(upper[i], 32))); } check_is_sat = Status.SATISFIABLE == solver.Check(); if (!check_is_sat) { if (last_index != -1) { lower[last_index] = last_upper + 1; } break; } Console.WriteLine("{0}", solver.Model); // narrow the bounds based on the current model. for (int i = 0; i < num_Exprs; ++i) { Expr v = solver.Model.Evaluate(to_minimize[i]); UInt64 ui = ((BitVecNum)v).UInt64; if (ui < upper[i]) { upper[i] = (UInt32)ui; } Console.WriteLine("{0} {1} {2}", i, lower[i], upper[i]); } // find a new bound to add some_work = false; last_index = 0; for (int i = 0; i < num_Exprs; ++i) { if (lower[i] < upper[i]) { last_upper = (upper[i] + lower[i]) / 2; last_index = i; solver.Assert(ctx.MkBVULE(to_minimize[i], ctx.MkBV(last_upper, 32))); some_work = true; break; } } } solver.Pop(); } }
/// <summary> /// Simple bit-vector example. /// </summary> /// <remarks> /// This example disproves that x - 10 <= 0 IFF x <= 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); }