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 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() { 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 (uint i = 0; i < 128; ++i) { s.AddFact(edge, i, i+1); } 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() { 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); } }
/// <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 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; }
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); } }
public void Run() { using (Context ctx = new Context()) { Sort U = ctx.MkUninterpretedSort("U"); Console.WriteLine(U); Expr a = ctx.MkConst("a", U); a = ctx.MkConst("a", U); Expr b = ctx.MkConst("b", U); Expr c = ctx.MkConst("c", U); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Console.WriteLine(ctx.MkAnd(ctx.MkEq(a, b), ctx.MkEq(a, c))); Console.WriteLine(U == ctx.IntSort); Sort U2 = ctx.MkUninterpretedSort("U"); Console.WriteLine(U == U2); U2 = ctx.MkUninterpretedSort("U2"); Console.WriteLine(U == U2); Console.WriteLine(ctx.MkDistinct(a, b, c)); FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { U, U }, U); Console.WriteLine(ctx.MkEq(f[a,b], b)); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { RealExpr x = ctx.MkRealConst("x"); RealExpr y = ctx.MkRealConst("y"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkAnd(ctx.MkEq(ctx.MkAdd(x, ctx.MkReal("10000000000000000000000")), y), ctx.MkGt(y, ctx.MkReal("20000000000000000")))); s.Check(); Console.WriteLine(s.Model); Expr q = ctx.MkAdd(ctx.MkPower(ctx.MkReal(2), ctx.MkReal(1, 2)), ctx.MkPower(ctx.MkReal(3), ctx.MkReal(1, 2))); Console.WriteLine(q); AlgebraicNum an = (AlgebraicNum)q.Simplify(); Console.WriteLine(an); Console.WriteLine("[" + an.ToLower(10) + "," + an.ToUpper(10) + "]"); Console.WriteLine(an.ToDecimal(10)); } }
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")); } }
public BoolExpr DependsOn(Context ctx, BoolExpr pack, BoolExpr[] deps) { BoolExpr[] q = new BoolExpr[deps.Length]; for (uint i = 0; i < deps.Length; i++) q[i] = ctx.MkImplies(pack, deps[i]); return ctx.MkAnd(q); }
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() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { ArithExpr[] a = new ArithExpr[5]; for (uint x = 0; x < 5; x++) a[x] = ctx.MkInt(x+1); foreach (Expr e in a) Console.WriteLine(e); ArithExpr[] X = new ArithExpr[5]; for (uint i = 0; i < 5; i++) X[i] = ctx.MkIntConst(string.Format("x{0}", i)); ArithExpr[] Y = new ArithExpr[5]; for (uint i = 0; i < 5; i++) Y[i] = ctx.MkIntConst(string.Format("y{0}", i)); foreach (Expr e in X) Console.WriteLine(e); ArithExpr[] X_plus_Y = new ArithExpr[5]; for (uint i = 0; i < 5; i++) X_plus_Y[i] = ctx.MkAdd(X[i], Y[i]); foreach (Expr e in X_plus_Y) Console.WriteLine(e); BoolExpr[] X_gt_Y = new BoolExpr[5]; for (uint i = 0; i < 5; i++) X_gt_Y[i] = ctx.MkGt(X[i], Y[i]); foreach (Expr e in X_gt_Y) Console.WriteLine(e); Console.WriteLine(ctx.MkAnd(X_gt_Y)); Expr[][] matrix = new Expr[3][]; for (uint i = 0; i < 3; i++) { matrix[i] = new Expr[3]; for (uint j = 0; j < 3; j++) matrix[i][j] = ctx.MkIntConst(string.Format("x_{0}_{1}", i + 1, j + 1)); } foreach(Expr[] row in matrix) { foreach(Expr e in row) Console.Write(" " + e); Console.WriteLine(); } } }
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() { using (Context ctx = new Context()) { FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.RealSort }, ctx.IntSort); try { Console.WriteLine(f.Domain[3]); } catch (IndexOutOfRangeException ex) { Console.WriteLine("failed: " + ex.Message); } IntExpr x = ctx.MkIntConst("x"); Console.WriteLine(f[ctx.MkInt(1), ctx.MkReal(1)]); Console.WriteLine(f[ctx.MkInt(1), ctx.MkReal(1)].Sort); Console.WriteLine(f[ctx.MkInt(1), ctx.MkReal(1)].NumArgs); foreach (Expr e in f[ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkReal(1)].Args) Console.WriteLine(e); Console.WriteLine(f[ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkReal(1)].Args[0]); Console.WriteLine(f[ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkReal(1)].Args[0].Equals(ctx.MkAdd(x, ctx.MkInt(1)))); Console.WriteLine(f[ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkReal(1)].FuncDecl[ctx.MkInt(2), ctx.MkInt2Real((IntExpr)ctx.MkAdd(x, ctx.MkInt(1)))]); Console.WriteLine(ctx.MkInt(1).IsExpr); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1)).IsExpr); Console.WriteLine(ctx.MkForall(new Expr[] { x }, ctx.MkGt(x, ctx.MkInt(0))).IsExpr); Console.WriteLine(ctx.MkInt(1).IsConst); Console.WriteLine(x.IsConst); Console.WriteLine(ctx.MkAdd(x, ctx.MkInt(1)).IsConst); Console.WriteLine(ctx.MkForall(new Expr[] { x }, ctx.MkGt(x, ctx.MkInt(0))).IsConst); Console.WriteLine(ctx.MkForall(new Expr[] { x }, ctx.MkGt(x, ctx.MkInt(0))).Body.Args[0]); Console.WriteLine(ctx.MkForall(new Expr[] { x }, ctx.MkGt(x, ctx.MkInt(0))).Body.Args[0].IsExpr); Console.WriteLine(ctx.MkForall(new Expr[] { x }, ctx.MkGt(x, ctx.MkInt(0))).Body.Args[0].IsConst); Console.WriteLine(ctx.MkForall(new Expr[] { x }, ctx.MkGt(x, ctx.MkInt(0))).Body.Args[0].IsVar); Console.WriteLine(x.IsVar); Console.WriteLine(ctx.MkITE(ctx.MkTrue(), x, ctx.MkAdd(x, ctx.MkInt(1)))); Context ctx1 = new Context(); Console.WriteLine(ctx1.MkITE(ctx1.MkTrue(), x.Translate(ctx1), ctx.MkAdd(x, ctx.MkInt(1)).Translate(ctx1))); Console.WriteLine(ctx.MkITE(ctx.MkTrue(), ctx.MkInt(1), ctx.MkInt(1))); Console.WriteLine(ctx.MkDistinct(x, ctx.MkAdd(x, ctx.MkInt(1)), ctx.MkAdd(x, ctx.MkInt(2)))); Console.WriteLine(ctx1.MkAnd(ctx1.MkDistinct(x.Translate(ctx1), ctx1.MkInt(1)), ctx1.MkGt((IntExpr)x.Translate(ctx1), ctx1.MkInt(0)))); } }
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)) { Sort T = ctx.MkUninterpretedSort("Type"); FuncDecl subtype = ctx.MkFuncDecl("subtype", new Sort[] { T, T }, ctx.BoolSort); FuncDecl array_of = ctx.MkFuncDecl("array_of", T, T); Expr root = ctx.MkConst("root", T); Expr x = ctx.MkConst("x", T); Expr y = ctx.MkConst("y", T); Expr z = ctx.MkConst("z", T); BoolExpr[] axioms = new BoolExpr[] { ctx.MkForall(new Expr[] { x }, subtype[x, x]), ctx.MkForall(new Expr[] { x, y , z }, ctx.MkImplies(ctx.MkAnd((BoolExpr)subtype[x,y], (BoolExpr)subtype[y,z]), (BoolExpr)subtype[x,z])), ctx.MkForall(new Expr[] { x, y }, ctx.MkImplies(ctx.MkAnd((BoolExpr)subtype[x, y], (BoolExpr)subtype[y,x]), ctx.MkEq(x, y))), ctx.MkForall(new Expr[] { x, y, z }, ctx.MkImplies(ctx.MkAnd((BoolExpr)subtype[x,y],(BoolExpr)subtype[x,z]), ctx.MkOr((BoolExpr)subtype[y,z], (BoolExpr)subtype[z,y]))), ctx.MkForall(new Expr[] { x, y }, ctx.MkImplies((BoolExpr)subtype[x,y], (BoolExpr)subtype[array_of[x], array_of[y]])), ctx.MkForall(new Expr[] { x }, (BoolExpr) subtype[root, x]) }; Solver s = ctx.MkSolver(); s.Assert(axioms); Console.WriteLine(s); Console.WriteLine(s.Check()); Expr[] universe = s.Model.SortUniverse(T); foreach (var e in universe) Console.WriteLine(e); Console.WriteLine(s.Model); } }
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { Expr x = ctx.MkConst("x", ctx.IntSort); Console.WriteLine(x == ctx.MkIntConst("x")); BoolExpr a = (BoolExpr)ctx.MkConst("a", ctx.BoolSort); BoolExpr b = (BoolExpr)ctx.MkConst("b", ctx.BoolSort); Console.WriteLine(ctx.MkAnd(a, b)); } }
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() { 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()); } }
static void QuantifierExample2(Context ctx) { Console.WriteLine("QuantifierExample2"); Expr q1, q2; FuncDecl f = ctx.MkFuncDecl("f", ctx.IntSort, ctx.IntSort); FuncDecl g = ctx.MkFuncDecl("g", ctx.IntSort, ctx.IntSort); // Quantifier with Exprs as the bound variables. { Expr x = ctx.MkConst("x", ctx.IntSort); Expr y = ctx.MkConst("y", ctx.IntSort); Expr f_x = ctx.MkApp(f, x); Expr f_y = ctx.MkApp(f, y); Expr g_y = ctx.MkApp(g, y); Pattern[] pats = new Pattern[] { ctx.MkPattern(new Expr[] { f_x, g_y }) }; Expr[] no_pats = new Expr[] { f_y }; Expr[] bound = new Expr[2] { x, y }; Expr body = ctx.MkAnd(ctx.MkEq(f_x, f_y), ctx.MkEq(f_y, g_y)); q1 = ctx.MkForall(bound, body, 1, null, no_pats, ctx.MkSymbol("q"), ctx.MkSymbol("sk")); Console.WriteLine("{0}", q1); } // Quantifier with de-Brujin indices. { Expr x = ctx.MkBound(1, ctx.IntSort); Expr y = ctx.MkBound(0, ctx.IntSort); Expr f_x = ctx.MkApp(f, x); Expr f_y = ctx.MkApp(f, y); Expr g_y = ctx.MkApp(g, y); Pattern[] pats = new Pattern[] { ctx.MkPattern(new Expr[] { f_x, g_y }) }; Expr[] no_pats = new Expr[] { f_y }; Symbol[] names = new Symbol[] { ctx.MkSymbol("x"), ctx.MkSymbol("y") }; Sort[] sorts = new Sort[] { ctx.IntSort, ctx.IntSort }; Expr body = ctx.MkAnd(ctx.MkEq(f_x, f_y), ctx.MkEq(f_y, g_y)); q2 = ctx.MkForall(sorts, names, body, 1, null, // pats, no_pats, ctx.MkSymbol("q"), ctx.MkSymbol("sk") ); Console.WriteLine("{0}", q2); } Console.WriteLine("{0}", (q1.Equals(q2))); }
/// <summary> /// Shows how to read an SMT1 file. /// </summary> static void SMT1FileTest(string filename) { Console.Write("SMT File test "); using (Context ctx = new Context(new Dictionary<string, string>() { { "MODEL", "true" } })) { ctx.ParseSMTLIBFile(filename); BoolExpr a = ctx.MkAnd(ctx.SMTLIBFormulas); Console.WriteLine("read formula: " + a); } }
/// <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(); } }
/// <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()); }
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()); }
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> /// Demonstrates how to use the SMTLIB parser. /// </summary> public static void ParserExample1(Context ctx) { Console.WriteLine("ParserExample1"); ctx.ParseSMTLIBString("(benchmark tst :extrafuns ((x Int) (y Int)) :formula (> x y) :formula (> x 0))"); foreach (BoolExpr f in ctx.SMTLIBFormulas) Console.WriteLine("formula {0}", f); Model m = Check(ctx, ctx.MkAnd(ctx.SMTLIBFormulas), Status.SATISFIABLE); }
public static void and(BoolExpr f1, BoolExpr f2, out BoolExpr e) { e = new BoolExpr(ctx.MkAnd(f1.expr, f2.expr)); }
/// <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> /// Extract unsatisfiable core example /// </summary> public static void UnsatCoreAndProofExample() { Console.WriteLine("UnsatCoreAndProofExample"); Dictionary<string, string> cfg = new Dictionary<string, string>() { { "PROOF_MODE", "2" } }; using (Context ctx = new Context(cfg)) { 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 p1 = ctx.MkBoolConst("P1"); BoolExpr p2 = ctx.MkBoolConst("P2"); BoolExpr p3 = ctx.MkBoolConst("P3"); BoolExpr p4 = ctx.MkBoolConst("P4"); BoolExpr[] assumptions = new BoolExpr[] { ctx.MkNot(p1), ctx.MkNot(p2), ctx.MkNot(p3), ctx.MkNot(p4) }; 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; solver.Assert(ctx.MkOr(f1, p1)); solver.Assert(ctx.MkOr(f2, p2)); solver.Assert(ctx.MkOr(f3, p3)); solver.Assert(ctx.MkOr(f4, p4)); Status result = solver.Check(assumptions); if (result == Status.UNSATISFIABLE) { Console.WriteLine("unsat"); Console.WriteLine("proof: {0}", solver.Proof); Console.WriteLine("core: "); foreach (Expr c in solver.UnsatCore) { Console.WriteLine("{0}", c); } } } }