MkBoolSort() public method

Create a new Boolean sort.
public MkBoolSort ( ) : BoolSort
return BoolSort
Example #1
0
        //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));
            //-----------------------------------------------------------------------------------------------------------------------------
        }
        internal static FuncDecl GetOrAddMemberAccessFunction(Context context, Environment environment, MemberInfo memberInfo)
        {
            FuncDecl memberFunc;
            if (!environment.Members.TryGetValue(memberInfo, out memberFunc))
            {
                Sort memberTypeSort;
                if (!environment.Types.TryGetValue(memberInfo.DeclaringType, out memberTypeSort))
                {
                    throw new KeyNotFoundException(memberInfo.DeclaringType + " could not be found at environment.Types");
                }

                Sort memberReturnTypeEnumSort;
                var propertyType = ((PropertyInfo)memberInfo).PropertyType;
                if (propertyType == typeof(bool))
                    memberReturnTypeEnumSort = context.MkBoolSort();
                else if (propertyType == typeof(int))
                    memberReturnTypeEnumSort = context.MkIntSort();
                else if (propertyType == typeof(long))
                    memberReturnTypeEnumSort = context.MkRealSort();
                else if (propertyType == typeof(string))
                    memberReturnTypeEnumSort = environment.PossibleStringValues;
                else
                {
                    if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
                    {
                        var listItemType = propertyType.GenericTypeArguments[0];

                        var listSort = context.MkSetSort(environment.Types[listItemType]);

                        memberReturnTypeEnumSort = listSort;

                        // TODO: add TryGetValue
                        environment.Types.Add(propertyType, listSort);
                    }
                    else if (propertyType.IsEnum)
                    {
                        EnumSort enumSort = context.MkEnumSort(propertyType.Name, Enum.GetNames(propertyType));

                        memberReturnTypeEnumSort = enumSort;

                        // TODO: add TryGetValue
                        environment.Types.Add(propertyType, enumSort);
                    }
                    else
                    {
                        // TODO throw exception if type is not supported
                        memberReturnTypeEnumSort = environment.Types[propertyType];
                    }
                }

                memberFunc = context.MkFuncDecl(memberInfo.Name, memberTypeSort, memberReturnTypeEnumSort);
                environment.Members.Add(memberInfo, memberFunc);
            }
            return memberFunc;
        }
Example #3
0
        /// <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)
            {
            }
        }
Example #4
0
        /// <summary>
        /// Show that <code>distinct(a_0, ... , a_n)</code> is
        /// unsatisfiable when <code>a_i</code>'s are arrays from boolean to
        /// boolean and n > 4.
        /// </summary>
        /// <remarks>This example also shows how to use the <code>distinct</code> construct.</remarks>
        public static void ArrayExample3(Context ctx)
        {
            Console.WriteLine("ArrayExample3");

            for (int n = 2; n <= 5; n++)
            {
                Console.WriteLine("n = {0}", n);

                Sort bool_type = ctx.MkBoolSort();
                Sort array_type = ctx.MkArraySort(bool_type, bool_type);
                Expr[] a = new Expr[n];

                /* create arrays */
                for (int i = 0; i < n; i++)
                {
                    a[i] = ctx.MkConst(String.Format("array_{0}", i), array_type);
                }

                /* assert distinct(a[0], ..., a[n]) */
                BoolExpr d = ctx.MkDistinct(a);
                Console.WriteLine("{0}", (d));

                /* context is satisfiable if n < 5 */
                Model model = Check(ctx, d, n < 5 ? Status.SATISFIABLE : Status.UNSATISFIABLE);
                if (n < 5)
                {
                    for (int i = 0; i < n; i++)
                    {
                        Console.WriteLine("{0} = {1}",
                                          a[i],
                                          model.Evaluate(a[i]));
                    }
                }
            }
        }
Example #5
0
    public void Run()
    {
        using (Context ctx = new Context())
        {
            this.ctx = ctx;
            ctx.UpdateParamValue("DL_GENERATE_EXPLANATIONS", "true");

            red_car = new Car(false, 2, 2, 3, "red");
            cars = new Car[]{
                new Car(true, 0, 3, 0, "yellow"),
                new Car(false, 3, 3, 0, "blue"),
                new Car(false, 5, 2, 0, "brown"),
                new Car(false, 0, 2, 1, "lgreen"),
                new Car(true,  1, 2, 1, "light blue"),
                new Car(true,  2, 2, 1, "pink"),
                new Car(true,  2, 2, 4, "dark green"),
                red_car,
                new Car(true,  3, 2, 3, "purple"),
                new Car(false, 5, 2, 3, "light yellow"),
                new Car(true,  4, 2, 0, "orange"),
                new Car(false, 4, 2, 4, "black"),
                new Car(true,  5, 3, 1, "light purple")
                };

            this.num_cars = cars.Length;
            this.B = ctx.MkBoolSort();
            this.bv3 = ctx.MkBitVecSort(3);
            List<Sort> domain = new List<Sort>();
            foreach (var c in cars) domain.Add(bv3);
            this.state = ctx.MkFuncDecl("state", domain.ToArray(), B);
            this.fp = ctx.MkFixedpoint();
            this.fp.RegisterRelation(state);

            // Initial state:

            Expr[] args = new Expr[num_cars];
            for (int i = 0; i < num_cars; ++i) args[i] = num(cars[i].start);
            fp.AddRule((BoolExpr)state[args]);

            // Transitions:
            for (int pos = 0; pos < num_cars; ++pos)
            {
                Car car = cars[pos];
                for (int i = 0; i < dimension; ++i)
                    if (car.is_vertical)
                    {
                        move_down(i, pos, car);
                        move_up(i, pos, car);
                    }
                    else
                    {
                        move_left(i, pos, car);
                        move_right(i, pos, car);
                    }
            }

            // Print the current context:
            Console.WriteLine("{0}", fp);

            // Prepare the query:
            for (int i = 0; i < num_cars; ++i) args[i] = (cars[i] == red_car) ? num(4) : bound(i);
            BoolExpr goal = (BoolExpr)state[args];
            fp.Query(goal);

            // Extract a path:
            get_instructions(fp.GetAnswer());
        }
    }
Example #6
0
        public Z3Context()
        {
            config = new Config();
            config.SetParamValue("MODEL", "true");
            config.SetParamValue("MODEL_V2", "true");
            config.SetParamValue("MODEL_COMPLETION", "true");
            config.SetParamValue("MBQI", "false");
            config.SetParamValue("TYPE_CHECK", "true");
            int timeout = 10000;    // timeout = 10 seconds
            config.SetParamValue("SOFT_TIMEOUT", timeout.ToString());

            context = new Context(config);
            intSort = context.MkIntSort();
            boolSort = context.MkBoolSort();
        }