public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { EnumSort color = ctx.MkEnumSort("Color", new string[] { "red", "green", "blue" }); Expr red = color.Consts[0]; Expr green = color.Consts[1]; Expr blue = color.Consts[2]; Console.WriteLine(ctx.MkEq(green, blue)); Console.WriteLine(ctx.MkEq(green, blue).Simplify()); Expr c = ctx.MkConst("c", color); Solver s = ctx.MkSolver(); s.Assert(ctx.MkNot(ctx.MkEq(c, green))); s.Assert(ctx.MkNot(ctx.MkEq(c, blue))); 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()) { BoolExpr p = ctx.MkBoolConst("p"); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkNot(p)); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1))); Console.WriteLine(ctx.MkAdd(ctx.MkInt(1), x)); Console.WriteLine(ctx.MkAdd(x, y)); Console.WriteLine(ctx.MkMul(ctx.MkInt(2), x)); Console.WriteLine(ctx.MkMul(x, ctx.MkInt(2))); Console.WriteLine(ctx.MkMul(x, y)); Console.WriteLine(ctx.MkDiv(x, y)); Console.WriteLine(ctx.MkMod(x, y)); Console.WriteLine(ctx.MkEq(x, y)); Console.WriteLine(ctx.MkDistinct(x, y, x)); Console.WriteLine(ctx.MkNot(ctx.MkEq(x, y))); Console.WriteLine(ctx.MkEq(x, y)); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1))); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1))); BoolExpr q = ctx.MkBoolConst("q"); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkAnd(p, q)); Console.WriteLine(ctx.MkAnd(p, q)); Console.WriteLine(ctx.MkEq(x, y)); } }
public BoolExpr Conflict(Context ctx, params BoolExpr[] packs) { BoolExpr q = ctx.MkFalse(); foreach (BoolExpr p in packs) q = ctx.MkOr(q, ctx.MkNot(p)); return q; }
static void Prove(Z3.Context ctx, BoolExpr f, bool useMBQI = false, params BoolExpr[] assumptions) { Console.WriteLine("Proving: " + f); Solver s = ctx.MkSolver(); Params p = ctx.MkParams(); p.Add("mbqi", useMBQI); s.Parameters = p; foreach (BoolExpr a in assumptions) { s.Assert(a); } s.Assert(ctx.MkNot(f)); Status q = s.Check(); switch (q) { case Status.UNKNOWN: Console.WriteLine("Unknown because: " + s.ReasonUnknown); break; case Status.SATISFIABLE: throw new Exception("Test failed"); case Status.UNSATISFIABLE: Console.WriteLine("OK, proof: " + s.Proof); break; } }
/// <summary> /// Generates a slightly randomized expression. /// </summary> static BoolExpr MkRandomExpr(Context ctx, System.Random rng) { int limit = 1073741823; Sort i = ctx.IntSort; Sort b = ctx.BoolSort; Symbol sr1 = ctx.MkSymbol(rng.Next(0, limit)); Symbol sr2 = ctx.MkSymbol(rng.Next(0, limit)); Symbol sr3 = ctx.MkSymbol(rng.Next(0, limit)); FuncDecl r1 = ctx.MkFuncDecl(sr1, i, b); FuncDecl r2 = ctx.MkFuncDecl(sr2, i, b); FuncDecl r3 = ctx.MkFuncDecl(sr3, i, b); Symbol s = ctx.MkSymbol(rng.Next(0, limit)); Expr x = ctx.MkConst(s, i); BoolExpr r1x = (BoolExpr)ctx.MkApp(r1, x); BoolExpr r2x = (BoolExpr)ctx.MkApp(r2, x); BoolExpr r3x = (BoolExpr)ctx.MkApp(r3, x); Expr[] vars = { x }; BoolExpr rl1 = ctx.MkForall(vars, ctx.MkImplies(r1x, r2x)); BoolExpr rl2 = ctx.MkForall(vars, ctx.MkImplies(r2x, r1x)); BoolExpr rl3 = (BoolExpr)ctx.MkApp(r1, ctx.MkInt(3)); BoolExpr q = (BoolExpr)ctx.MkApp(r3, ctx.MkInt(2)); BoolExpr a1 = ctx.MkNot(q); BoolExpr q1 = ctx.MkExists(vars, ctx.MkAnd(r3x, r2x)); BoolExpr q2 = ctx.MkExists(vars, ctx.MkAnd(r3x, r1x)); BoolExpr[] all = { a1, q1, q2 }; return ctx.MkAnd(all); }
public void Run() { using (Context ctx = new Context()) { ctx.UpdateParamValue("DL_ENGINE","1"); ctx.UpdateParamValue("DL_PDR_USE_FARKAS","true"); // ctx.UpdateParamValue("VERBOSE","2"); var s = ctx.MkFixedpoint(); BoolSort B = ctx.BoolSort; IntSort I = ctx.IntSort; FuncDecl mc = ctx.MkFuncDecl("mc", new Sort[]{I, I}, B); ArithExpr x = (ArithExpr)ctx.MkBound(0,I); ArithExpr y = (ArithExpr)ctx.MkBound(1,I); ArithExpr z = (ArithExpr)ctx.MkBound(2,I); s.RegisterRelation(mc); BoolExpr gt = ctx.MkGt(x, ctx.MkInt(100)); s.AddRule(ctx.MkImplies(gt,(BoolExpr)mc[x,ctx.MkSub(x,ctx.MkInt(10))])); s.AddRule(ctx.MkImplies(ctx.MkAnd(ctx.MkNot(gt), (BoolExpr) mc[ctx.MkAdd(x,ctx.MkInt(11)),y], (BoolExpr) mc[y,z]), (BoolExpr) mc[x,z])); Console.WriteLine(s.Query(ctx.MkAnd((BoolExpr)mc[x,y], ctx.MkGt(y,ctx.MkInt(100))))); Console.WriteLine(s.GetAnswer()); Console.WriteLine(s.Query(ctx.MkAnd((BoolExpr)mc[x,y], ctx.MkLt(y,ctx.MkInt(91))))); Console.WriteLine(s.GetAnswer()); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { Sort A = ctx.MkUninterpretedSort("A"); Expr x = ctx.MkConst("x", A); Expr y = ctx.MkConst("y", A); FuncDecl f = ctx.MkFuncDecl("f", A, A); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(f[f[x]], x), ctx.MkEq(f[x], y), ctx.MkNot(ctx.MkEq(x, y))); Console.WriteLine(s.Check()); Model m = s.Model; Console.WriteLine(m); Console.WriteLine("interpretation assigned to A: "); foreach (Expr a in m.SortUniverse(A)) Console.WriteLine(a); } }
public void Run() { using (Context ctx = new Context()) { Sort U = ctx.MkUninterpretedSort("U"); FuncDecl f = ctx.MkFuncDecl("f", U, U); Expr a = ctx.MkConst("a", U); Expr b = ctx.MkConst("b", U); Expr c = ctx.MkConst("c", U); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(f[f[a]], b), ctx.MkNot(ctx.MkEq(f[b], c)), ctx.MkEq(f[c], c)); Console.WriteLine(s.Check()); Model m = s.Model; foreach (FuncDecl d in m.Decls) if (d.DomainSize == 0) Console.WriteLine(d.Name + " -> " + m.ConstInterp(d)); else Console.WriteLine(d.Name + " -> " + m.FuncInterp(d)); Console.WriteLine(m.NumSorts); Console.WriteLine(m.Sorts[0]); foreach(Sort srt in m.Sorts) Console.WriteLine(srt); foreach (Expr v in m.SortUniverse(U)) Console.WriteLine(v); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { Sort A = ctx.MkUninterpretedSort("A"); Sort B = ctx.MkUninterpretedSort("B"); FuncDecl f = ctx.MkFuncDecl("f", A, B); Expr a1 = ctx.MkConst("a1", A); Expr a2 = ctx.MkConst("a2", A); Expr b = ctx.MkConst("b", B); Expr x = ctx.MkConst("x", A); Expr y = ctx.MkConst("y", A); Solver s = ctx.MkSolver(); s.Assert(ctx.MkNot(ctx.MkEq(a1, a2))); s.Assert(ctx.MkEq(f[a1], b)); s.Assert(ctx.MkEq(f[a2], b)); s.Assert(ctx.MkForall(new Expr[] { x, y }, ctx.MkImplies(ctx.MkEq(f[x], f[y]), ctx.MkEq(x, y)), 1, new Pattern[] { ctx.MkPattern(f[x], f[y]) })); Console.WriteLine(s); Console.WriteLine(s.Check()); } }
public void Run() { using (Context ctx = new Context()) { BoolExpr p = ctx.MkBoolConst("p"); BoolExpr q = ctx.MkBoolConst("q"); Console.WriteLine(ctx.MkAnd(p, q)); Console.WriteLine(ctx.MkOr(p, q)); Console.WriteLine(ctx.MkAnd(p, ctx.MkTrue())); Console.WriteLine(ctx.MkOr(p, ctx.MkFalse())); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkImplies(p, q)); Console.WriteLine(ctx.MkEq(p, q).Simplify()); Console.WriteLine(ctx.MkEq(p, q)); BoolExpr r = ctx.MkBoolConst("r"); Console.WriteLine(ctx.MkNot(ctx.MkEq(p, ctx.MkNot(ctx.MkEq(q, r))))); Console.WriteLine(ctx.MkNot(ctx.MkEq(ctx.MkNot(ctx.MkEq(p, q)), r))); Console.WriteLine(ctx.MkEq(p, ctx.MkTrue())); Console.WriteLine(ctx.MkEq(p, ctx.MkFalse())); Console.WriteLine(ctx.MkEq(p, ctx.MkTrue()).Simplify()); Console.WriteLine(ctx.MkEq(p, ctx.MkFalse()).Simplify()); Console.WriteLine(ctx.MkEq(p, p).Simplify()); Console.WriteLine(ctx.MkEq(p, q).Simplify()); Console.WriteLine(ctx.MkAnd(p, q, r)); Console.WriteLine(ctx.MkOr(p, q, r)); IntExpr x = ctx.MkIntConst("x"); Console.WriteLine(x is BoolExpr); Console.WriteLine(p is BoolExpr); Console.WriteLine(ctx.MkAnd(p, q) is BoolExpr); Console.WriteLine(p is BoolExpr); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1)) is BoolExpr); Console.WriteLine(p.IsAnd); Console.WriteLine(ctx.MkOr(p, q).IsOr); Console.WriteLine(ctx.MkAnd(p, q).IsAnd); Console.WriteLine(x.IsNot); Console.WriteLine(p.IsNot); Console.WriteLine(ctx.MkNot(p)); Console.WriteLine(ctx.MkNot(p).IsDistinct); Console.WriteLine(ctx.MkEq(p, q).IsDistinct); Console.WriteLine(ctx.MkDistinct(p, q).IsDistinct); Console.WriteLine(ctx.MkDistinct(x, ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkAdd(x, ctx.MkInt(2))).IsDistinct); Console.WriteLine(); Console.WriteLine(ctx.MkBool(true)); Console.WriteLine(ctx.MkBool(false)); Console.WriteLine(ctx.BoolSort); Context ctx1 = new Context(); Console.WriteLine(ctx1.MkBool(true)); Console.WriteLine(ctx1.BoolSort); Console.WriteLine(ctx1.MkBool(true).Sort == ctx1.BoolSort); Console.WriteLine(ctx1.MkBool(true).Sort == ctx.BoolSort); Console.WriteLine(ctx1.MkBool(true).Sort != ctx.BoolSort); } }
public void Run() { using (Context ctx = new Context()) { RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); RealExpr z = ctx.MkRealConst("z"); FuncDecl f = ctx.MkFuncDecl("f", ctx.RealSort, ctx.RealSort); Solver s = ctx.MkSolver(); s.Assert(ctx.MkGt(x, ctx.MkReal(10)), ctx.MkEq(y, ctx.MkAdd(x, ctx.MkReal(3))), ctx.MkLt(y, ctx.MkReal(15)), ctx.MkGt((RealExpr)f[x], ctx.MkReal(2)), ctx.MkNot(ctx.MkEq(f[y], f[x]))); Console.WriteLine(s.Check()); Model m = s.Model; foreach (FuncDecl fd in m.Decls) Console.Write(" " + fd.Name); Console.WriteLine(); foreach (FuncDecl fd in m.Decls) { if (fd.DomainSize == 0) Console.WriteLine(fd.Name + " -> " + m.ConstInterp(fd)); else Console.WriteLine(fd.Name + " -> " + m.FuncInterp(fd)); } Console.WriteLine(m.Evaluate(ctx.MkAdd(z, ctx.MkReal(1)))); Console.WriteLine(m.Evaluate(ctx.MkAdd(z, ctx.MkReal(1)), true)); Console.WriteLine(m.Evaluate(z)); FuncInterp fi = m.FuncInterp(f); Console.WriteLine(fi.Else); Console.WriteLine(fi.NumEntries); Console.WriteLine(fi.Entries[0]); Console.WriteLine(fi.Entries[0].NumArgs); Console.WriteLine(fi.Entries[0].Args[0]); Console.WriteLine(fi.Entries[0].Value); ArrayExpr a = ctx.MkArrayConst("a", ctx.RealSort, ctx.RealSort); s.Assert(ctx.MkGt((RealExpr)ctx.MkSelect(a, x), ctx.MkReal(10)), ctx.MkGt((RealExpr)ctx.MkSelect(a, y), ctx.MkReal(20))); Console.WriteLine(s); Console.WriteLine(s.Check()); Console.WriteLine(s.Model); Console.WriteLine(s.Model.Evaluate(a)); Console.WriteLine(s.Model.FuncInterp(a.FuncDecl)); } }
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); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { Constructor cred = ctx.MkConstructor("red", "is_red"); Constructor cgreen = ctx.MkConstructor("green", "is_green"); Constructor cblue = ctx.MkConstructor("blue", "is_blue"); DatatypeSort color = ctx.MkDatatypeSort("Color", new Constructor[] { cred, cgreen, cblue }); Expr Red = ctx.MkConst(color.Constructors[0]); Expr Green = ctx.MkConst(color.Constructors[1]); Expr Blue = ctx.MkConst(color.Constructors[2]); Expr c = ctx.MkConst("c", color); Solver s = ctx.MkSolver(); s.Assert(ctx.MkNot(ctx.MkOr(ctx.MkEq(c, Red), ctx.MkEq(c, Green), ctx.MkEq(c, Blue)))); Console.WriteLine(s.Check()); // must be unsat BoolExpr c_is_red = (BoolExpr)color.Recognizers[0][c]; BoolExpr c_is_green = (BoolExpr)color.Recognizers[1][c]; BoolExpr c_is_blue = (BoolExpr)color.Recognizers[2][c]; s = ctx.MkSolver(); s.Assert(ctx.MkOr(c_is_red, c_is_green, c_is_blue)); Console.WriteLine(s.Check()); // must be sat s = ctx.MkSolver(); s.Assert(ctx.MkNot(ctx.MkOr(c_is_red, c_is_green, c_is_blue))); Console.WriteLine(s.Check()); // must be unsat } }
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); }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "PROOF_MODE", "2" } }; using (Context ctx = new Context(cfg)) { BoolExpr p = ctx.MkBoolConst("p"); Solver s = ctx.MkSolver(); s.Assert(p); s.Assert(ctx.MkNot(p)); Console.WriteLine(s.Check()); Console.WriteLine(s.Proof); } }
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" }, { "PROOF_MODE", "2" } }; using (Context ctx = new Context(cfg)) { ArrayExpr A = ctx.MkArrayConst("A", ctx.IntSort, ctx.IntSort); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(ctx.MkSelect(A, x), x)); s.Assert(ctx.MkEq(ctx.MkStore(A, x, y), A)); s.Assert(ctx.MkNot(ctx.MkEq(x, y))); Console.WriteLine(s); Console.WriteLine(s.Check()); Console.WriteLine(s.Proof); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "PROOF_MODE", "2" } }; using (Context ctx = new Context(cfg)) { ArrayExpr AllOne = ctx.MkConstArray(ctx.IntSort, ctx.MkInt(1)); IntExpr a = ctx.MkIntConst("a"); IntExpr i = ctx.MkIntConst("i"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkEq(a, ctx.MkSelect(AllOne, i))); Console.WriteLine(s.Check()); s = ctx.MkSolver(); s.Assert(ctx.MkEq(a, ctx.MkSelect(AllOne, i))); s.Assert(ctx.MkNot(ctx.MkEq(a, ctx.MkInt(1)))); Console.WriteLine(s.Check()); } }
/// <summary> /// Some basic expression casting tests. /// </summary> static void CastingTest(Context ctx) { Console.WriteLine("CastingTest"); Sort[] domain = { ctx.BoolSort, ctx.BoolSort }; FuncDecl f = ctx.MkFuncDecl("f", domain, ctx.BoolSort); AST upcast = ctx.MkFuncDecl(ctx.MkSymbol("q"), domain, ctx.BoolSort); try { FuncDecl downcast = (FuncDecl)f; // OK } catch (InvalidCastException) { throw new TestFailedException(); } try { Expr uc = (Expr)upcast; throw new TestFailedException(); // should not be reachable! } catch (InvalidCastException) { } Symbol s = ctx.MkSymbol(42); IntSymbol si = s as IntSymbol; if (si == null) throw new TestFailedException(); try { IntSymbol si2 = (IntSymbol)s; } catch (InvalidCastException) { throw new TestFailedException(); } s = ctx.MkSymbol("abc"); StringSymbol ss = s as StringSymbol; if (ss == null) throw new TestFailedException(); try { StringSymbol ss2 = (StringSymbol)s; } catch (InvalidCastException) { throw new TestFailedException(); } try { IntSymbol si2 = (IntSymbol)s; throw new TestFailedException(); // unreachable } catch { } Sort srt = ctx.MkBitVecSort(32); BitVecSort bvs = null; try { bvs = (BitVecSort)srt; } catch (InvalidCastException) { throw new TestFailedException(); } if (bvs.Size != 32) throw new TestFailedException(); Expr q = ctx.MkAdd(ctx.MkInt(1), ctx.MkInt(2)); Expr q2 = q.Args[1]; Sort qs = q2.Sort; if (qs as IntSort == null) throw new TestFailedException(); try { IntSort isrt = (IntSort)qs; } catch (InvalidCastException) { throw new TestFailedException(); } AST a = ctx.MkInt(42); Expr ae = a as Expr; if (ae == null) throw new TestFailedException(); ArithExpr aae = a as ArithExpr; if (aae == null) throw new TestFailedException(); IntExpr aie = a as IntExpr; if (aie == null) throw new TestFailedException(); IntNum ain = a as IntNum; if (ain == null) throw new TestFailedException(); Expr[][] earr = new Expr[2][]; earr[0] = new Expr[2]; earr[1] = new Expr[2]; earr[0][0] = ctx.MkTrue(); earr[0][1] = ctx.MkTrue(); earr[1][0] = ctx.MkFalse(); earr[1][1] = ctx.MkFalse(); foreach (Expr[] ea in earr) foreach (Expr e in ea) { try { Expr ns = ctx.MkNot((BoolExpr)e); BoolExpr ens = (BoolExpr)ns; } catch (InvalidCastException) { throw new TestFailedException(); } } }
static void Disprove(Context ctx, BoolExpr f, bool useMBQI = false, params BoolExpr[] assumptions) { Console.WriteLine("Disproving: " + f); Solver s = ctx.MkSolver(); Params p = ctx.MkParams(); p.Add("mbqi", useMBQI); s.Parameters = p; foreach (BoolExpr a in assumptions) s.Assert(a); s.Assert(ctx.MkNot(f)); Status q = s.Check(); switch (q) { case Status.UNKNOWN: Console.WriteLine("Unknown because: " + s.ReasonUnknown); break; case Status.SATISFIABLE: Console.WriteLine("OK, model: " + s.Model); break; case Status.UNSATISFIABLE: throw new TestFailedException(); } }
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); }
/// <summary> /// Extract unsatisfiable core example with AssertAndTrack /// </summary> public static void UnsatCoreAndProofExample2(Context ctx) { Console.WriteLine("UnsatCoreAndProofExample2"); Solver solver = ctx.MkSolver(); BoolExpr pa = ctx.MkBoolConst("PredA"); BoolExpr pb = ctx.MkBoolConst("PredB"); BoolExpr pc = ctx.MkBoolConst("PredC"); BoolExpr pd = ctx.MkBoolConst("PredD"); BoolExpr f1 = ctx.MkAnd(new BoolExpr[] { pa, pb, pc }); BoolExpr f2 = ctx.MkAnd(new BoolExpr[] { pa, ctx.MkNot(pb), pc }); BoolExpr f3 = ctx.MkOr(ctx.MkNot(pa), ctx.MkNot(pc)); BoolExpr f4 = pd; BoolExpr p1 = ctx.MkBoolConst("P1"); BoolExpr p2 = ctx.MkBoolConst("P2"); BoolExpr p3 = ctx.MkBoolConst("P3"); BoolExpr p4 = ctx.MkBoolConst("P4"); solver.AssertAndTrack(f1, p1); solver.AssertAndTrack(f2, p2); solver.AssertAndTrack(f3, p3); solver.AssertAndTrack(f4, p4); Status result = solver.Check(); if (result == Status.UNSATISFIABLE) { Console.WriteLine("unsat"); Console.WriteLine("core: "); foreach (Expr c in solver.UnsatCore) { Console.WriteLine("{0}", c); } } }
/// <summary> /// Demonstrate how to use #Eval on tuples. /// </summary> public static void EvalExample2(Context ctx) { Console.WriteLine("EvalExample2"); Sort int_type = ctx.IntSort; TupleSort tuple = ctx.MkTupleSort( ctx.MkSymbol("mk_tuple"), // name of tuple constructor new Symbol[] { ctx.MkSymbol("first"), ctx.MkSymbol("second") }, // names of projection operators new Sort[] { int_type, int_type } // types of projection operators ); FuncDecl first = tuple.FieldDecls[0]; // declarations are for projections FuncDecl second = tuple.FieldDecls[1]; Expr tup1 = ctx.MkConst("t1", tuple); Expr tup2 = ctx.MkConst("t2", tuple); Solver solver = ctx.MkSolver(); /* assert tup1 != tup2 */ solver.Assert(ctx.MkNot(ctx.MkEq(tup1, tup2))); /* assert first tup1 = first tup2 */ solver.Assert(ctx.MkEq(ctx.MkApp(first, tup1), ctx.MkApp(first, tup2))); /* find model for the constraints above */ Model model = null; if (Status.SATISFIABLE == solver.Check()) { model = solver.Model; Console.WriteLine("{0}", model); Console.WriteLine("evaluating tup1 {0}", (model.Evaluate(tup1))); Console.WriteLine("evaluating tup2 {0}", (model.Evaluate(tup2))); Console.WriteLine("evaluating second(tup2) {0}", (model.Evaluate(ctx.MkApp(second, tup2)))); } else { Console.WriteLine("BUG, the constraints are satisfiable."); } }
/// <summary> /// Create a forest of trees. /// </summary> /// <remarks> /// forest ::= nil | cons(tree, forest) /// tree ::= nil | cons(forest, forest) /// </remarks> public static void ForestExample(Context ctx) { Console.WriteLine("ForestExample"); Sort tree, forest; FuncDecl nil1_decl, is_nil1_decl, cons1_decl, is_cons1_decl, car1_decl, cdr1_decl; FuncDecl nil2_decl, is_nil2_decl, cons2_decl, is_cons2_decl, car2_decl, cdr2_decl; Expr nil1, nil2, t1, t2, t3, t4, f1, f2, f3, l1, l2, x, y, u, v; // // Declare the names of the accessors for cons. // Then declare the sorts of the accessors. // For this example, all sorts refer to the new types 'forest' and 'tree' // being declared, so we pass in null for both sorts1 and sorts2. // On the other hand, the sort_refs arrays contain the indices of the // two new sorts being declared. The first element in sort1_refs // points to 'tree', which has index 1, the second element in sort1_refs array // points to 'forest', which has index 0. // Symbol[] head_tail1 = new Symbol[] { ctx.MkSymbol("head"), ctx.MkSymbol("tail") }; Sort[] sorts1 = new Sort[] { null, null }; uint[] sort1_refs = new uint[] { 1, 0 }; // the first item points to a tree, the second to a forest Symbol[] head_tail2 = new Symbol[] { ctx.MkSymbol("car"), ctx.MkSymbol("cdr") }; Sort[] sorts2 = new Sort[] { null, null }; uint[] sort2_refs = new uint[] { 0, 0 }; // both items point to the forest datatype. Constructor nil1_con, cons1_con, nil2_con, cons2_con; Constructor[] constructors1 = new Constructor[2], constructors2 = new Constructor[2]; Symbol[] sort_names = { ctx.MkSymbol("forest"), ctx.MkSymbol("tree") }; /* build a forest */ nil1_con = ctx.MkConstructor(ctx.MkSymbol("nil"), ctx.MkSymbol("is_nil"), null, null, null); cons1_con = ctx.MkConstructor(ctx.MkSymbol("cons1"), ctx.MkSymbol("is_cons1"), head_tail1, sorts1, sort1_refs); constructors1[0] = nil1_con; constructors1[1] = cons1_con; /* build a tree */ nil2_con = ctx.MkConstructor(ctx.MkSymbol("nil2"), ctx.MkSymbol("is_nil2"), null, null, null); cons2_con = ctx.MkConstructor(ctx.MkSymbol("cons2"), ctx.MkSymbol("is_cons2"), head_tail2, sorts2, sort2_refs); constructors2[0] = nil2_con; constructors2[1] = cons2_con; Constructor[][] clists = new Constructor[][] { constructors1, constructors2 }; Sort[] sorts = ctx.MkDatatypeSorts(sort_names, clists); forest = sorts[0]; tree = sorts[1]; // // Now that the datatype has been created. // Query the constructors for the constructor // functions, testers, and field accessors. // nil1_decl = nil1_con.ConstructorDecl; is_nil1_decl = nil1_con.TesterDecl; cons1_decl = cons1_con.ConstructorDecl; is_cons1_decl = cons1_con.TesterDecl; FuncDecl[] cons1_accessors = cons1_con.AccessorDecls; car1_decl = cons1_accessors[0]; cdr1_decl = cons1_accessors[1]; nil2_decl = nil2_con.ConstructorDecl; is_nil2_decl = nil2_con.TesterDecl; cons2_decl = cons2_con.ConstructorDecl; is_cons2_decl = cons2_con.TesterDecl; FuncDecl[] cons2_accessors = cons2_con.AccessorDecls; car2_decl = cons2_accessors[0]; cdr2_decl = cons2_accessors[1]; nil1 = ctx.MkConst(nil1_decl); nil2 = ctx.MkConst(nil2_decl); f1 = ctx.MkApp(cons1_decl, nil2, nil1); t1 = ctx.MkApp(cons2_decl, nil1, nil1); t2 = ctx.MkApp(cons2_decl, f1, nil1); t3 = ctx.MkApp(cons2_decl, f1, f1); t4 = ctx.MkApp(cons2_decl, nil1, f1); f2 = ctx.MkApp(cons1_decl, t1, nil1); f3 = ctx.MkApp(cons1_decl, t1, f1); /* nil != cons(nil,nil) */ Prove(ctx, ctx.MkNot(ctx.MkEq(nil1, f1))); Prove(ctx, ctx.MkNot(ctx.MkEq(nil2, t1))); /* cons(x,u) = cons(x, v) => u = v */ u = ctx.MkConst("u", forest); v = ctx.MkConst("v", forest); x = ctx.MkConst("x", tree); y = ctx.MkConst("y", tree); l1 = ctx.MkApp(cons1_decl, x, u); l2 = ctx.MkApp(cons1_decl, y, v); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(u, v))); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y))); /* is_nil(u) or is_cons(u) */ Prove(ctx, ctx.MkOr((BoolExpr)ctx.MkApp(is_nil1_decl, u), (BoolExpr)ctx.MkApp(is_cons1_decl, u))); /* occurs check u != cons(x,u) */ Prove(ctx, ctx.MkNot(ctx.MkEq(u, l1))); }
public BoolExpr Conflict(Context ctx, BoolExpr p1, BoolExpr p2) { return ctx.MkOr(ctx.MkNot(p1), ctx.MkNot(p2)); }
/// <summary> /// Create a list datatype. /// </summary> public static void ListExample(Context ctx) { Console.WriteLine("ListExample"); Sort int_ty; ListSort int_list; Expr nil, l1, l2, x, y, u, v; BoolExpr fml, fml1; int_ty = ctx.MkIntSort(); int_list = ctx.MkListSort(ctx.MkSymbol("int_list"), int_ty); nil = ctx.MkConst(int_list.NilDecl); l1 = ctx.MkApp(int_list.ConsDecl, ctx.MkInt(1), nil); l2 = ctx.MkApp(int_list.ConsDecl, ctx.MkInt(2), nil); /* nil != cons(1, nil) */ Prove(ctx, ctx.MkNot(ctx.MkEq(nil, l1))); /* cons(2,nil) != cons(1, nil) */ Prove(ctx, ctx.MkNot(ctx.MkEq(l1, l2))); /* cons(x,nil) = cons(y, nil) => x = y */ x = ctx.MkConst("x", int_ty); y = ctx.MkConst("y", int_ty); l1 = ctx.MkApp(int_list.ConsDecl, x, nil); l2 = ctx.MkApp(int_list.ConsDecl, y, nil); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y))); /* cons(x,u) = cons(x, v) => u = v */ u = ctx.MkConst("u", int_list); v = ctx.MkConst("v", int_list); l1 = ctx.MkApp(int_list.ConsDecl, x, u); l2 = ctx.MkApp(int_list.ConsDecl, y, v); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(u, v))); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y))); /* is_nil(u) or is_cons(u) */ Prove(ctx, ctx.MkOr((BoolExpr)ctx.MkApp(int_list.IsNilDecl, u), (BoolExpr)ctx.MkApp(int_list.IsConsDecl, u))); /* occurs check u != cons(x,u) */ Prove(ctx, ctx.MkNot(ctx.MkEq(u, l1))); /* destructors: is_cons(u) => u = cons(head(u),tail(u)) */ fml1 = ctx.MkEq(u, ctx.MkApp(int_list.ConsDecl, ctx.MkApp(int_list.HeadDecl, u), ctx.MkApp(int_list.TailDecl, u))); fml = ctx.MkImplies((BoolExpr)ctx.MkApp(int_list.IsConsDecl, u), fml1); Console.WriteLine("Formula {0}", fml); Prove(ctx, fml); Disprove(ctx, fml1); }
/// <summary> /// Create an enumeration data type. /// </summary> public static void EnumExample(Context ctx) { Console.WriteLine("EnumExample"); Symbol name = ctx.MkSymbol("fruit"); EnumSort fruit = ctx.MkEnumSort(ctx.MkSymbol("fruit"), new Symbol[] { ctx.MkSymbol("apple"), ctx.MkSymbol("banana"), ctx.MkSymbol("orange") }); Console.WriteLine("{0}", (fruit.Consts[0])); Console.WriteLine("{0}", (fruit.Consts[1])); Console.WriteLine("{0}", (fruit.Consts[2])); Console.WriteLine("{0}", (fruit.TesterDecls[0])); Console.WriteLine("{0}", (fruit.TesterDecls[1])); Console.WriteLine("{0}", (fruit.TesterDecls[2])); Expr apple = fruit.Consts[0]; Expr banana = fruit.Consts[1]; Expr orange = fruit.Consts[2]; /* Apples are different from oranges */ Prove(ctx, ctx.MkNot(ctx.MkEq(apple, orange))); /* Apples pass the apple test */ Prove(ctx, (BoolExpr)ctx.MkApp(fruit.TesterDecls[0], apple)); /* Oranges fail the apple test */ Disprove(ctx, (BoolExpr)ctx.MkApp(fruit.TesterDecls[0], orange)); Prove(ctx, (BoolExpr)ctx.MkNot((BoolExpr)ctx.MkApp(fruit.TesterDecls[0], orange))); Expr fruity = ctx.MkConst("fruity", fruit); /* If something is fruity, then it is an apple, banana, or orange */ Prove(ctx, ctx.MkOr(new BoolExpr[] { ctx.MkEq(fruity, apple), ctx.MkEq(fruity, banana), ctx.MkEq(fruity, orange) })); }
/// <summary> /// Prove <tt>x = y implies g(x) = g(y)</tt>, and /// disprove <tt>x = y implies g(g(x)) = g(y)</tt>. /// </summary> /// <remarks>This function demonstrates how to create uninterpreted /// types and functions.</remarks> public static void ProveExample1(Context ctx) { Console.WriteLine("ProveExample1"); /* create uninterpreted type. */ Sort U = ctx.MkUninterpretedSort(ctx.MkSymbol("U")); /* declare function g */ FuncDecl g = ctx.MkFuncDecl("g", U, U); /* create x and y */ Expr x = ctx.MkConst("x", U); Expr y = ctx.MkConst("y", U); /* create g(x), g(y) */ Expr gx = g[x]; Expr gy = g[y]; /* assert x = y */ BoolExpr eq = ctx.MkEq(x, y); /* prove g(x) = g(y) */ BoolExpr f = ctx.MkEq(gx, gy); Console.WriteLine("prove: x = y implies g(x) = g(y)"); Prove(ctx, ctx.MkImplies(eq, f)); /* create g(g(x)) */ Expr ggx = g[gx]; /* disprove g(g(x)) = g(y) */ f = ctx.MkEq(ggx, gy); Console.WriteLine("disprove: x = y implies g(g(x)) = g(y)"); Disprove(ctx, ctx.MkImplies(eq, f)); /* Print the model using the custom model printer */ Model m = Check(ctx, ctx.MkNot(f), Status.SATISFIABLE); Console.WriteLine(m); }
/// <summary> /// Find a model for <tt>x < y + 1, x > 2</tt>. /// Then, assert <tt>not(x = y)</tt>, and find another model. /// </summary> public static void FindModelExample2(Context ctx) { Console.WriteLine("FindModelExample2"); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); IntExpr one = ctx.MkInt(1); IntExpr two = ctx.MkInt(2); ArithExpr y_plus_one = ctx.MkAdd(y, one); BoolExpr c1 = ctx.MkLt(x, y_plus_one); BoolExpr c2 = ctx.MkGt(x, two); BoolExpr q = ctx.MkAnd(c1, c2); Console.WriteLine("model for: x < y + 1, x > 2"); Model model = Check(ctx, q, Status.SATISFIABLE); Console.WriteLine("x = {0}, y = {1}", (model.Evaluate(x)), (model.Evaluate(y))); /* assert not(x = y) */ BoolExpr x_eq_y = ctx.MkEq(x, y); BoolExpr c3 = ctx.MkNot(x_eq_y); q = ctx.MkAnd(q, c3); Console.WriteLine("model for: x < y + 1, x > 2, not(x = y)"); model = Check(ctx, q, Status.SATISFIABLE); Console.WriteLine("x = {0}, y = {1}", (model.Evaluate(x)), (model.Evaluate(y))); }
/// <summary> /// Create a binary tree datatype. /// </summary> public static void TreeExample(Context ctx) { Console.WriteLine("TreeExample"); Sort cell; FuncDecl nil_decl, is_nil_decl, cons_decl, is_cons_decl, car_decl, cdr_decl; Expr nil, l1, l2, x, y, u, v; BoolExpr fml, fml1; string[] head_tail = new string[] { "car", "cdr" }; Sort[] sorts = new Sort[] { null, null }; uint[] sort_refs = new uint[] { 0, 0 }; Constructor nil_con, cons_con; nil_con = ctx.MkConstructor("nil", "is_nil", null, null, null); cons_con = ctx.MkConstructor("cons", "is_cons", head_tail, sorts, sort_refs); Constructor[] constructors = new Constructor[] { nil_con, cons_con }; cell = ctx.MkDatatypeSort("cell", constructors); nil_decl = nil_con.ConstructorDecl; is_nil_decl = nil_con.TesterDecl; cons_decl = cons_con.ConstructorDecl; is_cons_decl = cons_con.TesterDecl; FuncDecl[] cons_accessors = cons_con.AccessorDecls; car_decl = cons_accessors[0]; cdr_decl = cons_accessors[1]; nil = ctx.MkConst(nil_decl); l1 = ctx.MkApp(cons_decl, nil, nil); l2 = ctx.MkApp(cons_decl, l1, nil); /* nil != cons(nil, nil) */ Prove(ctx, ctx.MkNot(ctx.MkEq(nil, l1))); /* cons(x,u) = cons(x, v) => u = v */ u = ctx.MkConst("u", cell); v = ctx.MkConst("v", cell); x = ctx.MkConst("x", cell); y = ctx.MkConst("y", cell); l1 = ctx.MkApp(cons_decl, x, u); l2 = ctx.MkApp(cons_decl, y, v); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(u, v))); Prove(ctx, ctx.MkImplies(ctx.MkEq(l1, l2), ctx.MkEq(x, y))); /* is_nil(u) or is_cons(u) */ Prove(ctx, ctx.MkOr((BoolExpr)ctx.MkApp(is_nil_decl, u), (BoolExpr)ctx.MkApp(is_cons_decl, u))); /* occurs check u != cons(x,u) */ Prove(ctx, ctx.MkNot(ctx.MkEq(u, l1))); /* destructors: is_cons(u) => u = cons(car(u),cdr(u)) */ fml1 = ctx.MkEq(u, ctx.MkApp(cons_decl, ctx.MkApp(car_decl, u), ctx.MkApp(cdr_decl, u))); fml = ctx.MkImplies((BoolExpr)ctx.MkApp(is_cons_decl, u), fml1); Console.WriteLine("Formula {0}", fml); Prove(ctx, fml); Disprove(ctx, fml1); }
public static void not(BoolExpr f1, out BoolExpr e) { e = new BoolExpr(ctx.MkNot(f1.expr)); }