public MkConstructor ( |
||
name | constructor name | |
recognizer | name of recognizer function. | |
fieldNames | names of the constructor fields. | |
sorts | Microsoft.Z3.Sort | field sorts, 0 if the field sort refers to a recursive sort. |
sortRefs | uint | reference to datatype sort that is an argument to the constructor; /// if the corresponding sort reference is 0, then the value in sort_refs should be an index /// referring to one of the recursive datatypes that is declared. |
Résultat | Microsoft.Z3.Constructor |
public void Run() { Dictionary<string, string> cfg = new Dictionary<string, string>() { { "AUTO_CONFIG", "true" }, { "MODEL", "true" } }; using (Context ctx = new Context(cfg)) { Constructor c_leaf = ctx.MkConstructor("leaf", "is_leaf", new string[] { "val" }, new Sort[] { ctx.IntSort }); Constructor c_node = ctx.MkConstructor("node", "is_node", new string[] { "left", "right" }, new Sort[] { null, null }, new uint[] { 1, 1 }); Constructor[] constr_1 = new Constructor[] { c_leaf, c_node }; Constructor c_nil = ctx.MkConstructor("nil", "is_nil"); Constructor c_cons = ctx.MkConstructor("cons", "is_cons", new string[] { "car", "cdr" }, new Sort[] { null, null }, new uint[] { 0, 1 }); Constructor[] constr_2 = new Constructor[] { c_nil, c_cons }; DatatypeSort[] ts = ctx.MkDatatypeSorts(new string[] { "Tree", "TreeList" }, new Constructor[][] { constr_1, constr_2 }); DatatypeSort Tree = ts[0]; DatatypeSort TreeList = ts[1]; FuncDecl leaf = Tree.Constructors[0]; FuncDecl node = Tree.Constructors[1]; FuncDecl val = Tree.Accessors[0][0]; FuncDecl nil = TreeList.Constructors[0]; FuncDecl cons = TreeList.Constructors[1]; Expr t1 = leaf[ctx.MkInt(10)]; Expr tl1 = cons[t1, nil.Apply()]; Expr t2 = node[tl1, nil.Apply()]; Console.WriteLine(t2); Console.WriteLine(val.Apply(t1).Simplify()); t1 = ctx.MkConst("t1", TreeList); t2 = ctx.MkConst("t2", TreeList); Expr t3 = ctx.MkConst("t3", TreeList); Solver s = ctx.MkSolver(); s.Assert(ctx.MkDistinct(t1, t2, t3)); 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 } }
/// <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))); }
/// <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); }
private Environment GenerateEnvironment(Context context) { var environment = new Environment(); // Class instances // TODO: support NULL values foreach (var instancesPerClassType in _instances.GroupBy(_ => _.Value.GetType())) { string classType = instancesPerClassType.Key.Name; EnumSort instancesEnumSort = context.MkEnumSort(classType + "_instances", instancesPerClassType.Select(_ => _.Key).ToArray()); environment.Types.Add(instancesPerClassType.Key, instancesEnumSort); Expr[] instancesEnumSortValues = instancesEnumSort.Consts; int instancesIndex = 0; foreach (KeyValuePair<string, object> instance in instancesPerClassType) { environment.Instances.Add(instance.Key, new InstanceInfo(instancesEnumSortValues[instancesIndex++], objectInstance: instance.Value)); } } // Super Types foreach (var superType in _superTypes) { var subTypeSorts = environment.Types.Where(t => t.Key.IsSubclassOf(superType)).Select(t => t.Value).ToList(); if (subTypeSorts.Count == 0) continue; var superTypeConstructors = new List<Constructor>(); foreach (var subTypeSort in subTypeSorts) { var subTypeConstr = context.MkConstructor( name: subTypeSort.Name.ToString(), recognizer: "Is" + subTypeSort.Name, fieldNames: new[] { subTypeSort.Name + "2" + superType.Name }, sorts: new[] { subTypeSort }, sortRefs: null); superTypeConstructors.Add(subTypeConstr); } DatatypeSort superTypeSort = context.MkDatatypeSort(superType.Name, superTypeConstructors.ToArray()); //DatatypeSort = context.MkDatatypeSort("Types", new Constructor[] { // context.MkConstructor("ValTypes", "isValType", new String[] {"Val2Type"}, new Sort[] {ValTypeSort}, null), // context.MkConstructor("RefTypes", "isRefType", new String[] {"Ref2Type"}, new Sort[] {RefTypeSort}, null) //}); environment.Types.Add(superType, superTypeSort); } // Strings if (_possibleStringValues.Any()) { EnumSort enumSort = context.MkEnumSort("Strings", _possibleStringValues.ToArray()); environment.PossibleStringValues = enumSort; } foreach (var member in _members) { LambdaExpressionToConstraintGenerator.GetOrAddMemberAccessFunction(context, environment, member); } return environment; }