Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        private BoolExpr[] GenerateConstraints(Context context, Environment environment, IReadOnlyList <LambdaExpression> lambdaExpressions)
        {
            var constraints = new List <BoolExpr>();

            foreach (var lambdaExpression in lambdaExpressions)
            {
                var generator = new LambdaExpressionToConstraintGenerator(context, environment);
                constraints.Add(generator.Visit(lambdaExpression));
            }

            return(constraints.ToArray());
        }
Exemplo n.º 3
0
        internal ObjectTheoremResult Solve()
        {
            var settings = new Dictionary <string, string> {
                { "model", "true" },
                { "unsat_core", "true" }
            };

            using (var context = new Context(settings))
            {
                var stopwatch = Stopwatch.StartNew();

                Environment environment = GenerateEnvironment(context);

                BoolExpr[] assertions = GenerateConstraints(context, environment, _assertions);

                BoolExpr[] allAssertions = GenerateAllConstraints(context, environment, _allAssertions);

                Solver solver = context.MkSimpleSolver();

                int assertionCount = 0;
                foreach (var assertion in assertions.Concat(allAssertions))
                {
                    assertionCount++;
                    solver.Assert(assertion);
                }

                int assumptionCount = 0;
                var assumptions     = new Dictionary <LambdaExpression, BoolExpr>();
                foreach (var assumption in _assumptions)
                {
                    var generator = new LambdaExpressionToConstraintGenerator(context, environment);

                    var assumptionExpr = generator.Visit(assumption);

                    var assumptionCheck = context.MkBoolConst(Guid.NewGuid().ToString());
                    solver.Assert(context.MkEq(assumptionExpr, assumptionCheck));

                    assumptionCount++;
                    assumptions.Add(assumption, assumptionCheck);
                }

                stopwatch.Stop();
                var constraintGenerationTimeSpan = stopwatch.Elapsed;

                var solverString          = solver.ToString();
                var totalConstraintsCount = solverString.Split('\n').Count(l => l.StartsWith("  ("));

                Trace.WriteLine("Statistics:");
                Trace.WriteLine("assertionCount: " + assertionCount);
                Trace.WriteLine("assumptionCount: " + assumptionCount);
                Trace.WriteLine("totalConstraintsCount: " + totalConstraintsCount);
                Trace.WriteLine("constraintGenerationTimeSpan: " + constraintGenerationTimeSpan);

                var classCount    = environment.Types.Count(t => !t.Key.IsValueType && t.Key.Name != "String");
                var propertyCount = environment.Types
                                    .Where(t => !t.Key.IsValueType && t.Key.Name != "String")
                                    .Select(p => p.Key.GetProperties())
                                    .Count();

                stopwatch.Restart();
                Microsoft.Z3.Status status = solver.Check(assumptions.Values.ToArray());
                stopwatch.Stop();

                var solvingTimeSpan = stopwatch.Elapsed;
                Trace.WriteLine("solvingTimeSpan: " + solvingTimeSpan);

                if (status != Microsoft.Z3.Status.SATISFIABLE)
                {
                    if (solver.UnsatCore.Length > 0)
                    {
                        var result = new ObjectTheoremResult(context, environment, solver, status);
                        result.SetUnsatCore(assumptions, solver.UnsatCore);
                        return(result);
                    }

                    return(null);
                }

                var modelString = solver.Model.ToString();

                return(new ObjectTheoremResult(context, environment, solver, status));
            }
        }
        internal ObjectTheoremResult Solve()
        {
            var settings = new Dictionary<string, string> {
                { "model", "true" },
                { "unsat_core", "true" }
            };

            using (var context = new Context(settings))
            {
                var stopwatch = Stopwatch.StartNew();

                Environment environment = GenerateEnvironment(context);

                BoolExpr[] assertions = GenerateConstraints(context, environment, _assertions);

                BoolExpr[] allAssertions = GenerateAllConstraints(context, environment, _allAssertions);

                Solver solver = context.MkSimpleSolver();

                int assertionCount = 0;
                foreach (var assertion in assertions.Concat(allAssertions))
                {
                    assertionCount++;
                    solver.Assert(assertion);
                }

                int assumptionCount = 0;
                var assumptions = new Dictionary<LambdaExpression, BoolExpr>();
                foreach (var assumption in _assumptions)
                {
                    var generator = new LambdaExpressionToConstraintGenerator(context, environment);

                    var assumptionExpr = generator.Visit(assumption);

                    var assumptionCheck = context.MkBoolConst(Guid.NewGuid().ToString());
                    solver.Assert(context.MkEq(assumptionExpr, assumptionCheck));

                    assumptionCount++;
                    assumptions.Add(assumption, assumptionCheck);
                }

                stopwatch.Stop();
                var constraintGenerationTimeSpan = stopwatch.Elapsed;

                var solverString = solver.ToString();
                var totalConstraintsCount = solverString.Split('\n').Count(l => l.StartsWith("  ("));

                Trace.WriteLine("Statistics:");
                Trace.WriteLine("assertionCount: " + assertionCount);
                Trace.WriteLine("assumptionCount: " + assumptionCount);
                Trace.WriteLine("totalConstraintsCount: " + totalConstraintsCount);
                Trace.WriteLine("constraintGenerationTimeSpan: " + constraintGenerationTimeSpan);

                var classCount = environment.Types.Count(t => !t.Key.IsValueType && t.Key.Name != "String");
                var propertyCount = environment.Types
                    .Where(t => !t.Key.IsValueType && t.Key.Name != "String")
                    .Select(p => p.Key.GetProperties())
                    .Count();

                stopwatch.Restart();
                Microsoft.Z3.Status status = solver.Check(assumptions.Values.ToArray());
                stopwatch.Stop();

                var solvingTimeSpan = stopwatch.Elapsed;
                Trace.WriteLine("solvingTimeSpan: " + solvingTimeSpan);

                if (status != Microsoft.Z3.Status.SATISFIABLE)
                {
                    if (solver.UnsatCore.Length > 0)
                    {
                        var result = new ObjectTheoremResult(context, environment, solver, status);
                        result.SetUnsatCore(assumptions, solver.UnsatCore);
                        return result;
                    }

                    return null;
                }

                var modelString = solver.Model.ToString();

                return new ObjectTheoremResult(context, environment, solver, status);
            }
        }
        private BoolExpr[] GenerateConstraints(Context context, Environment environment, IReadOnlyList<LambdaExpression> lambdaExpressions)
        {
            var constraints = new List<BoolExpr>();

            foreach (var lambdaExpression in lambdaExpressions)
            {
                var generator = new LambdaExpressionToConstraintGenerator(context, environment);
                constraints.Add(generator.Visit(lambdaExpression));
            }

            return constraints.ToArray();
        }
        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();
        }