All Downloads are FREE. Search and download functionalities are using the official Maven repository.

z3-z3-4.13.0.src.api.dotnet.Solver.cs Maven / Gradle / Ivy

The newest version!
/*++
Copyright (c) 2012 Microsoft Corporation

Module Name:

    Solver.cs

Abstract:

    Z3 Managed API: Solvers

Author:

    Christoph Wintersteiger (cwinter) 2012-03-22

Notes:
    
--*/

using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;

namespace Microsoft.Z3
{
    /// 
    /// Solvers.
    /// 
    public class Solver : Z3Object
    {
        /// 
        /// A string that describes all available solver parameters.
        /// 
        public string Help
        {
            get
            {

                return Native.Z3_solver_get_help(Context.nCtx, NativeObject);
            }
        }

        /// 
        /// Sets the solver parameters.
        /// 
        public Params Parameters
        {
            set
            {
                Debug.Assert(value != null);

                Context.CheckContextMatch(value);
                Native.Z3_solver_set_params(Context.nCtx, NativeObject, value.NativeObject);
            }
        }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(string name, bool value) 
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(string name, uint value)
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(string name, double value)
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(string name, string value)
    {
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(string name, Symbol value)
    {
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(Symbol name, bool value)
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(Symbol name, uint value)
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(Symbol name, double value)
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(Symbol name, string value)
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }

	/// 
	/// Sets parameter on the solver
	/// 
	public void Set(Symbol name, Symbol value) 
    { 
        using var parameters = Context.MkParams().Add(name, value);
        Parameters = parameters; 
    }



        /// 
        /// Retrieves parameter descriptions for solver.
        /// 
        public ParamDescrs ParameterDescriptions
        {
            get { return new ParamDescrs(Context, Native.Z3_solver_get_param_descrs(Context.nCtx, NativeObject)); }
        }


        /// 
        /// The current number of backtracking points (scopes).
        /// 
        /// 
        /// 
        public uint NumScopes
        {
            get { return Native.Z3_solver_get_num_scopes(Context.nCtx, NativeObject); }
        }

        /// 
        /// Creates a backtracking point.
        /// 
        /// 
        public void Push()
        {
            Native.Z3_solver_push(Context.nCtx, NativeObject);
        }

        /// 
        /// Backtracks  backtracking points.
        /// 
        /// Note that an exception is thrown if  is not smaller than NumScopes
        /// 
        public void Pop(uint n = 1)
        {
            Native.Z3_solver_pop(Context.nCtx, NativeObject, n);
        }

        /// 
        /// Resets the Solver.
        /// 
        /// This removes all assertions from the solver.
        public void Reset()
        {
            Native.Z3_solver_reset(Context.nCtx, NativeObject);
        }

        /// 
        /// Assert a constraint (or multiple) into the solver.
        ///         
        public void Assert(params BoolExpr[] constraints)
        {
            Debug.Assert(constraints != null);
            Debug.Assert(constraints.All(c => c != null));

            Context.CheckContextMatch(constraints);
            foreach (BoolExpr a in constraints)
            {
                Native.Z3_solver_assert(Context.nCtx, NativeObject, a.NativeObject);
            }
        }

        /// 
        /// Alias for Assert.
        ///         
        public void Add(params BoolExpr[] constraints)
        {
            Assert(constraints);
        }

        /// 
        /// Alias for Assert.
        ///         
        public void Add(IEnumerable constraints)
        {
            Assert(constraints.ToArray());
        }

        /// 
        /// Assert multiple constraints into the solver, and track them (in the unsat) core 
        /// using the Boolean constants in ps. 
        /// 
        /// 
        /// This API is an alternative to  with assumptions for extracting unsat cores.
        /// Both APIs can be used in the same solver. The unsat core will contain a combination
        /// of the Boolean variables provided using  
        /// and the Boolean literals
        /// provided using  with assumptions.
        ///         
        public void AssertAndTrack(BoolExpr[] constraints, BoolExpr[] ps)
        {
            Debug.Assert(constraints != null);
            Debug.Assert(constraints.All(c => c != null));
            Debug.Assert(ps.All(c => c != null));
            Context.CheckContextMatch(constraints);
            Context.CheckContextMatch(ps);
            if (constraints.Length != ps.Length)
                throw new Z3Exception("Argument size mismatch");
            
            for (int i = 0 ; i < constraints.Length; i++)
                Native.Z3_solver_assert_and_track(Context.nCtx, NativeObject, constraints[i].NativeObject, ps[i].NativeObject);
        }

        /// 
        /// Assert a constraint into the solver, and track it (in the unsat) core 
        /// using the Boolean constant p. 
        /// 
        /// 
        /// This API is an alternative to  with assumptions for extracting unsat cores.
        /// Both APIs can be used in the same solver. The unsat core will contain a combination
        /// of the Boolean variables provided using  
        /// and the Boolean literals
        /// provided using  with assumptions.
        ///         
        public void AssertAndTrack(BoolExpr constraint, BoolExpr p)
        {
            Debug.Assert(constraint != null);
            Debug.Assert(p != null);
            Context.CheckContextMatch(constraint);
            Context.CheckContextMatch(p);
                        
            Native.Z3_solver_assert_and_track(Context.nCtx, NativeObject, constraint.NativeObject, p.NativeObject);
        }

	/// 
	/// Load solver assertions from a file.
	/// 
	public void FromFile(string file) 
        {
	     Native.Z3_solver_from_file(Context.nCtx, NativeObject, file);	
	}

	/// 
	/// Load solver assertions from a string.
	/// 
	public void FromString(string str) 
        {
	     Native.Z3_solver_from_string(Context.nCtx, NativeObject, str);	
	}

        /// 
        /// The number of assertions in the solver.
        /// 
        public uint NumAssertions
        {
            get
            {
                using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
                return assertions.Size;
            }
        }

        /// 
        /// The set of asserted formulas.
        /// 
        public BoolExpr[] Assertions
        {
            get
            {

                using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
                return assertions.ToBoolExprArray();
            }
        }

        /// 
        /// Currently inferred units.
        /// 
        public BoolExpr[] Units
        {
            get
            {

                using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_units(Context.nCtx, NativeObject));
                return assertions.ToBoolExprArray();
            }
        }

        /// 
        /// Checks whether the assertions in the solver are consistent or not.
        /// 
        /// 
        /// 
        /// 
        ///     
        ///     
        public Status Check(params Expr[] assumptions)
        {
            Z3_lbool r;
            if (assumptions == null || assumptions.Length == 0)
                r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
            else
                r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
            return lboolToStatus(r);
        }

        /// 
        /// Checks whether the assertions in the solver are consistent or not.
        /// 
        /// 
        /// 
        /// 
        ///     
        ///     
        public Status Check(IEnumerable assumptions)
        {
            Z3_lbool r;
            BoolExpr[] asms = assumptions.ToArray();
            if (asms.Length == 0)
                r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
            else
                r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)asms.Length, AST.ArrayToNative(asms));
            return lboolToStatus(r);
        }

        /// 
        /// Retrieve fixed assignments to the set of variables in the form of consequences.
        /// Each consequence is an implication of the form 
        ///
        ///       relevant-assumptions Implies variable = value
        /// 
        /// where the relevant assumptions is a subset of the assumptions that are passed in
        /// and the equality on the right side of the implication indicates how a variable
        /// is fixed.
        /// 
        /// 
        /// 
        /// 
        ///     
        ///     
        public Status Consequences(IEnumerable assumptions, IEnumerable variables, out BoolExpr[] consequences) 
        {
            using ASTVector result = new ASTVector(Context);
            using ASTVector asms = new ASTVector(Context);
            using ASTVector vars = new ASTVector(Context);
            foreach (var asm in assumptions) asms.Push(asm);
            foreach (var v in variables) vars.Push(v);
            Z3_lbool r = (Z3_lbool)Native.Z3_solver_get_consequences(Context.nCtx, NativeObject, asms.NativeObject, vars.NativeObject, result.NativeObject);
            consequences = result.ToBoolExprArray();
            return lboolToStatus(r);
        }

        /// 
        /// The model of the last Check(params Expr[] assumptions).
        /// 
        /// 
        /// The result is null if Check(params Expr[] assumptions) was not invoked before,
        /// if its results was not SATISFIABLE, or if model production is not enabled.
        /// 
        public Model Model
        {
            get
            {
                IntPtr x = Native.Z3_solver_get_model(Context.nCtx, NativeObject);
                if (x == IntPtr.Zero)
                    return null;
                else
                    return new Model(Context, x);
            }
        }

        /// 
        /// The proof of the last Check(params Expr[] assumptions).
        /// 
        ///     
        /// The result is null if Check(params Expr[] assumptions) was not invoked before,
        /// if its results was not UNSATISFIABLE, or if proof production is disabled.
        /// 
        public Expr Proof
        {
            get
            {
                IntPtr x = Native.Z3_solver_get_proof(Context.nCtx, NativeObject);
                if (x == IntPtr.Zero)
                    return null;
                else
                    return Expr.Create(Context, x);
            }
        }

        /// 
        /// The unsat core of the last Check.
        /// 
        /// 
        /// The unsat core is a subset of Assertions
        /// The result is empty if Check was not invoked before,
        /// if its results was not UNSATISFIABLE, or if core production is disabled.
        /// 
        public BoolExpr[] UnsatCore
        {
            get
            {

                using ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));                
                return core.ToBoolExprArray();
            }
        }

        /// 
        /// A brief justification of why the last call to Check returned UNKNOWN.
        /// 
        public string ReasonUnknown
        {
            get
            {

                return Native.Z3_solver_get_reason_unknown(Context.nCtx, NativeObject);
            }
        }

	/// 
	/// Backtrack level that can be adjusted by conquer process
	/// 
        public uint BacktrackLevel { get; set; }

	/// 
	/// Variables available and returned by the cuber.
	/// 
	public BoolExpr[] CubeVariables { get; set; }
        

	/// 
	/// Return a set of cubes.
	/// 
	public IEnumerable Cube()
	{
             using ASTVector cv = new ASTVector(Context);
             if (CubeVariables != null) 
                foreach (var b in CubeVariables) cv.Push(b);

	     while (true) {
                var lvl = BacktrackLevel;
                BacktrackLevel = uint.MaxValue;
                using ASTVector r = new ASTVector(Context, Native.Z3_solver_cube(Context.nCtx, NativeObject, cv.NativeObject, lvl));
                var v = r.ToBoolExprArray();
                CubeVariables = cv.ToBoolExprArray();
                if (v.Length == 1 && v[0].IsFalse) {
                   break;
                }
                yield return v; 
                if (v.Length == 0) {
                   break;
                }
	     }
	}

        /// 
        /// Create a clone of the current solver with respect to ctx.
        /// 
        public Solver Translate(Context ctx) 
        {
             Debug.Assert(ctx != null);
             return new Solver(ctx, Native.Z3_solver_translate(Context.nCtx, NativeObject, ctx.nCtx));
        }

	/// 
	/// Import model converter from other solver. 
	/// 
	public void ImportModelConverter(Solver src) 
	{
	     Native.Z3_solver_import_model_converter(Context.nCtx, src.NativeObject, NativeObject);
	}

        /// 
        /// Solver statistics.
        /// 
        public Statistics Statistics
        {
            get
            {

                return new Statistics(Context, Native.Z3_solver_get_statistics(Context.nCtx, NativeObject));
            }
        }

        /// 
        /// A string representation of the solver.
        /// 
        public override string ToString()
        {
            return Native.Z3_solver_to_string(Context.nCtx, NativeObject);
        }

        #region Internal
        internal Solver(Context ctx, IntPtr obj)
            : base(ctx, obj)
        {
            Debug.Assert(ctx != null);
            this.BacktrackLevel = uint.MaxValue;
        }

        internal override void IncRef(IntPtr o)
        {
            Native.Z3_solver_inc_ref(Context.nCtx, o);
        }

        internal override void DecRef(IntPtr o)
        {
            lock (Context)
            {
                if (Context.nCtx != IntPtr.Zero)
                    Native.Z3_solver_dec_ref(Context.nCtx, o);
            }
        }

        private Status lboolToStatus(Z3_lbool r) 
        {
            switch (r)
            {
                case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE;
                case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE;
                default: return Status.UNKNOWN;
            }
        }

        #endregion
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy