public MkForall ( |
||
boundConstants | ||
body | ||
weight | uint | |
patterns | ||
noPatterns | ||
quantifierID | ||
skolemID | ||
Résultat |
/// <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() { Dictionary<string, string> cfg = new Dictionary<string, string>() { }; using (Context ctx = new Context(cfg)) { FuncDecl f = ctx.MkFuncDecl("f", ctx.IntSort, ctx.IntSort); FuncDecl g = ctx.MkFuncDecl("g", ctx.IntSort, ctx.IntSort); IntExpr a = ctx.MkIntConst("a"); IntExpr b = ctx.MkIntConst("b"); IntExpr c = ctx.MkIntConst("c"); IntExpr x = ctx.MkIntConst("x"); Solver s = ctx.MkSolver(); Params p = ctx.MkParams(); p.Add("AUTO_CONFIG", false); p.Add("MBQI", false); s.Parameters = p; s.Assert(ctx.MkForall(new Expr[] { x }, ctx.MkEq(f[g[x]], x), 1, new Pattern[] { ctx.MkPattern(f[g[x]]) })); s.Assert(ctx.MkEq(a, g[b])); s.Assert(ctx.MkEq(b, c)); s.Assert(ctx.MkDistinct(f[a], c)); Console.WriteLine(s); 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 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() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" } }; using (Context ctx = new Context(cfg)) { FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.IntSort }, ctx.IntSort); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Console.WriteLine(ctx.MkForall(new Expr[] { x, y }, ctx.MkEq(f[x, y], ctx.MkInt(0)))); Console.WriteLine(ctx.MkExists(new Expr[] { x }, ctx.MkGe((ArithExpr)f[x, x], ctx.MkInt(0)))); IntExpr a = ctx.MkIntConst("a"); IntExpr b = ctx.MkIntConst("b"); Solver s = ctx.MkSolver(); s.Assert(ctx.MkForall(new Expr[] { x }, ctx.MkEq(f[x, x], ctx.MkInt(0)))); s.Assert(ctx.MkEq(f[a, b], ctx.MkInt(1))); 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)) { FuncDecl f = ctx.MkFuncDecl("f", new Sort[] { ctx.IntSort, ctx.IntSort }, ctx.IntSort); IntExpr x = ctx.MkIntConst("x"); IntExpr y = ctx.MkIntConst("y"); Quantifier qf = ctx.MkForall(new Expr[] { x, y }, ctx.MkEq(f[x, y], ctx.MkInt(0))); Console.WriteLine(qf.Body); Expr v1 = qf.Body.Args[0].Args[0]; Console.WriteLine(v1); Console.WriteLine(v1 == ctx.MkBound(1, ctx.IntSort)); } }
//Constructor public Z3Context() { //Initialize Config and Context _config = new Config(); _config.SetParamValue("MODEL", "true"); // corresponds to /m switch _config.SetParamValue("MACRO_FINDER", "true"); _context = new Context(_config); //Setup custom conversion method BoolToInt (boolean -> integer)---------------------------------------------------------------- FuncDecl boolToInt = _context.MkFuncDecl("BoolToInt", _context.MkBoolSort(), _context.MkIntSort()); Term i = _context.MkConst("i", _context.MkBoolSort()); Term fDef = _context.MkIte(_context.MkEq(i, _context.MkTrue()), _context.MkIntNumeral(1), _context.MkIntNumeral(0)); // x == true => 1, x == false => 0 Term fStatement = _context.MkForall(0, new Term[] { i }, null, _context.MkEq(_context.MkApp(boolToInt, i), fDef)); _context.AssertCnstr(fStatement); // _functions.Add("BoolToInt", new Z3Function(boolToInt)); //----------------------------------------------------------------------------------------------------------------------------- }
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); } }
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> /// 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()); }
/// <summary> /// Create axiom: function f is injective in the i-th argument. /// </summary> /// <remarks> /// The following axiom is produced: /// <c> /// forall (x_0, ..., x_n) finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i /// </c> /// Where, <code>finv</code>is a fresh function declaration. /// </summary> public static BoolExpr InjAxiom(Context ctx, FuncDecl f, int i) { Sort[] domain = f.Domain; uint sz = f.DomainSize; if (i >= sz) { Console.WriteLine("failed to create inj axiom"); return null; } /* declare the i-th inverse of f: finv */ Sort finv_domain = f.Range; Sort finv_range = domain[i]; FuncDecl finv = ctx.MkFuncDecl("f_fresh", finv_domain, finv_range); /* allocate temporary arrays */ Expr[] xs = new Expr[sz]; Symbol[] names = new Symbol[sz]; Sort[] types = new Sort[sz]; /* fill types, names and xs */ for (uint j = 0; j < sz; j++) { types[j] = domain[j]; names[j] = ctx.MkSymbol(String.Format("x_{0}", j)); xs[j] = ctx.MkBound(j, types[j]); } Expr x_i = xs[i]; /* create f(x_0, ..., x_i, ..., x_{n-1}) */ Expr fxs = f[xs]; /* create f_inv(f(x_0, ..., x_i, ..., x_{n-1})) */ Expr finv_fxs = finv[fxs]; /* create finv(f(x_0, ..., x_i, ..., x_{n-1})) = x_i */ Expr eq = ctx.MkEq(finv_fxs, x_i); /* use f(x_0, ..., x_i, ..., x_{n-1}) as the pattern for the quantifier */ Pattern p = ctx.MkPattern(new Expr[] { fxs }); /* create & assert quantifier */ BoolExpr q = ctx.MkForall( types, /* types of quantified variables */ names, /* names of quantified variables */ eq, 1, new Pattern[] { p } /* patterns */); return q; }
public override BoolExpr toZ3Bool(Context ctx) { var qf = quantified_variables.Select(x => x.toZ3(ctx)).ToArray(); if (this.quantifier == Quantifier.Exists) return ctx.MkExists(qf, inner.toZ3Bool(ctx)); else if (this.quantifier == Quantifier.Forall) return ctx.MkForall(qf, inner.toZ3Bool(ctx)); else throw new ArgumentException(); }
private BoolExpr[] GenerateAllConstraints(Context context, Environment environment, IReadOnlyList<LambdaExpression> lambdaExpressions) { var constraints = new List<BoolExpr>(); foreach (LambdaExpression lambdaExpression in lambdaExpressions) { var forAllParameters = new Dictionary<string, Expr>(); foreach (ParameterExpression parameter in lambdaExpression.Parameters) { Sort typeSort; if (!environment.Types.TryGetValue(parameter.Type, out typeSort)) { throw new NotSupportedException("This type was not registered through objectTheorem.CreateInstance<T> :" + parameter.Type); } forAllParameters.Add(parameter.Name, context.MkConst(parameter.Type.ToString(), typeSort)); } var generator = new LambdaExpressionToConstraintGenerator(context, environment); generator.LambdaParameterConstants = forAllParameters; Expr forAllBody = generator.Visit(lambdaExpression); var forAll = context.MkForall(forAllParameters.Values.ToArray(), forAllBody); constraints.Add(forAll); } return constraints.ToArray(); }