/*++
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.Contracts;
namespace Microsoft.Z3
{
///
/// Solvers.
///
[ContractVerification(true)]
public class Solver : Z3Object
{
///
/// A string that describes all available solver parameters.
///
public string Help
{
get
{
Contract.Ensures(Contract.Result() != null);
return Native.Z3_solver_get_help(Context.nCtx, NativeObject);
}
}
///
/// Sets the solver parameters.
///
public Params Parameters
{
set
{
Contract.Requires(value != null);
Context.CheckContextMatch(value);
Native.Z3_solver_set_params(Context.nCtx, NativeObject, value.NativeObject);
}
}
///
/// 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)
{
Contract.Requires(constraints != null);
Contract.Requires(Contract.ForAll(constraints, c => c != null));
Context.CheckContextMatch(constraints);
foreach (BoolExpr a in constraints)
{
Native.Z3_solver_assert(Context.nCtx, NativeObject, a.NativeObject);
}
}
///
/// The number of assertions in the solver.
///
public uint NumAssertions
{
get
{
ASTVector ass = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
return ass.Size;
}
}
///
/// The set of asserted formulas.
///
public BoolExpr[] Assertions
{
get
{
Contract.Ensures(Contract.Result() != null);
ASTVector ass = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
uint n = ass.Size;
BoolExpr[] res = new BoolExpr[n];
for (uint i = 0; i < n; i++)
res[i] = new BoolExpr(Context, ass[i].NativeObject);
return res;
}
}
///
/// Checks whether the assertions in the solver are consistent or not.
///
///
///
///
///
///
public Status Check(params Expr[] assumptions)
{
Z3_lbool r;
if (assumptions == null)
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));
switch (r)
{
case Z3_lbool.Z3_L_TRUE: return Status.SATISFIABLE;
case Z3_lbool.Z3_L_FALSE: return Status.UNSATISFIABLE;
default: return Status.UNKNOWN;
}
}
///
/// The model of the last Check.
///
///
/// The result is null if Check 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.
///
///
/// The result is null if Check 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 Expr[] UnsatCore
{
get
{
Contract.Ensures(Contract.Result() != null);
ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));
uint n = core.Size;
Expr[] res = new Expr[n];
for (uint i = 0; i < n; i++)
res[i] = Expr.Create(Context, core[i].NativeObject);
return res;
}
}
///
/// A brief justification of why the last call to Check returned UNKNOWN.
///
public string ReasonUnknown
{
get
{
Contract.Ensures(Contract.Result() != null);
return Native.Z3_solver_get_reason_unknown(Context.nCtx, NativeObject);
}
}
///
/// Solver statistics.
///
public Statistics Statistics
{
get
{
Contract.Ensures(Contract.Result() != null);
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)
{
Contract.Requires(ctx != null);
}
internal class DecRefQueue : Z3.DecRefQueue
{
public override void IncRef(Context ctx, IntPtr obj)
{
Native.Z3_solver_inc_ref(ctx.nCtx, obj);
}
public override void DecRef(Context ctx, IntPtr obj)
{
Native.Z3_solver_dec_ref(ctx.nCtx, obj);
}
};
internal override void IncRef(IntPtr o)
{
Context.Solver_DRQ.IncAndClear(Context, o);
base.IncRef(o);
}
internal override void DecRef(IntPtr o)
{
Context.Solver_DRQ.Add(o);
base.DecRef(o);
}
#endregion
}
}