22using System.Diagnostics;
23using System.Collections.Generic;
29 using Z3_ast = System.IntPtr;
45 public string Help => Native.Z3_solver_get_help(nCtx, z3solver);
47 private void SetParam(Action<Z3_params> setter)
50 Native.Z3_params_inc_ref(nCtx, p);
52 Native.Z3_solver_set_params(nCtx, z3solver, p);
53 Native.Z3_params_dec_ref(nCtx, p);
59 public void Set(
string name,
bool value)
61 SetParam((
Z3_params p) => Native.Z3_params_set_bool(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), (
byte)(value ? 1 : 0)));
67 public void Set(
string name, uint value)
69 SetParam((
Z3_params p) => Native.Z3_params_set_uint(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value));
75 public void Set(
string name,
double value)
77 SetParam((
Z3_params p) => Native.Z3_params_set_double(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value));
83 public void Set(
string name,
string value)
85 var value_sym = Native.Z3_mk_string_symbol(nCtx, value);
86 SetParam((
Z3_params p) => Native.Z3_params_set_symbol(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value_sym));
97 public void Set(Symbol name,
bool value) { Parameters = Context.MkParams().
Add(name, value); }
101 public void Set(Symbol name, uint value) { Parameters = Context.MkParams().
Add(name, value); }
105 public void Set(Symbol name,
double value) { Parameters = Context.MkParams().
Add(name, value); }
109 public void Set(Symbol name,
string value) { Parameters = Context.MkParams().
Add(name, value); }
113 public void Set(Symbol name, Symbol value) { Parameters = Context.MkParams().
Add(name, value); }
118 public ParamDescrs ParameterDescriptions
120 get {
return new ParamDescrs(Context, Native.Z3_solver_get_param_descrs(nCtx, NativeObject)); }
129 public uint
NumScopes => Native.Z3_solver_get_num_scopes(nCtx, z3solver);
135 public void Push() => Native.Z3_solver_push(nCtx, z3solver);
142 public void Pop(uint n = 1) => Native.Z3_solver_pop(nCtx, z3solver, n);
148 public void Reset() => Native.Z3_solver_reset(nCtx, z3solver);
155 Debug.Assert(constraints !=
null);
156 Debug.Assert(constraints.All(c => c != IntPtr.Zero));
158 foreach (
Z3_ast a
in constraints)
160 Native.Z3_solver_assert(nCtx, z3solver, a);
172 public void Add(IEnumerable<Z3_ast> constraints) =>
Assert(constraints.ToArray());
185 uint arity = Native.Z3_get_arity(nCtx, f);
186 Z3_sort range = Native.Z3_get_range(nCtx, f);
190 for (uint i = 0; i < arity; ++i)
192 Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
193 vars[i] = ntvContext.
MkBound(arity - i - 1, domain);
195 names[i] = Native.Z3_mk_int_symbol(nCtx, (
int)i);
197 Z3_ast app_f = IntPtr.Zero;
198 for (uint i = 0; i < arity; ++i)
200 Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
221 Debug.Assert(constraints !=
null);
222 Debug.Assert(constraints.All(c => c != IntPtr.Zero));
223 Debug.Assert(ps.All(c => c != IntPtr.Zero));
224 if (constraints.Length != ps.Length)
227 for (
int i = 0; i < constraints.Length; i++)
228 Native.Z3_solver_assert_and_track(nCtx, z3solver, constraints[i], ps[i]);
244 Debug.Assert(constraint !=
null);
245 Debug.Assert(p !=
null);
247 Native.Z3_solver_assert_and_track(nCtx, z3solver, constraint, p);
254 => Native.Z3_solver_from_file(nCtx, z3solver, file);
260 => Native.Z3_solver_from_string(nCtx, z3solver, str);
266 => (uint)ntvContext.ToArray(Native.Z3_solver_get_assertions(nCtx, z3solver)).Length;
272 => ntvContext.ToArray(Native.Z3_solver_get_assertions(nCtx, z3solver));
278 => ntvContext.ToArray(Native.Z3_solver_get_units(nCtx, z3solver));
284 => ntvContext.ToArray(Native.Z3_solver_get_non_units(nCtx, z3solver));
290 => ntvContext.ToArray(Native.Z3_solver_get_trail(nCtx, z3solver));
299 Debug.Assert(t != IntPtr.Zero);
300 return Native.Z3_solver_congruence_root(nCtx, z3solver, t);
310 Debug.Assert(t != IntPtr.Zero);
311 return Native.Z3_solver_congruence_next(nCtx, z3solver, t);
319 Debug.Assert(a != IntPtr.Zero);
320 Debug.Assert(b != IntPtr.Zero);
321 return Native.Z3_solver_congruence_explain(nCtx, z3solver, a, b);
335 if (assumptions ==
null || assumptions.Length == 0)
336 r = (
Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
338 r = (
Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)assumptions.Length, assumptions);
339 return lboolToStatus(r);
353 Z3_ast[] asms = assumptions.ToArray();
354 if (asms.Length == 0)
355 r = (
Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
357 r = (
Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)asms.Length, asms);
358 return lboolToStatus(r);
372 IntPtr x = Native.Z3_solver_get_model(nCtx, z3solver);
373 return x == IntPtr.Zero
387 => Native.Z3_solver_get_proof(nCtx, z3solver);
398 => ntvContext.ToArray(Native.Z3_solver_get_unsat_core(nCtx, z3solver));
404 => Native.Z3_solver_get_reason_unknown(nCtx, z3solver);
411 Debug.
Assert(ctx !=
null);
412 return new NativeSolver(ctx, Native.Z3_solver_translate(nCtx, z3solver, ctx.nCtx));
420 Debug.Assert(src !=
null);
422 Native.Z3_solver_import_model_converter(nCtx, src.z3solver, z3solver);
432 var stats = Native.Z3_solver_get_statistics(nCtx, z3solver);
442 return Native.Z3_solver_to_string(nCtx, z3solver);
452 Debug.
Assert(nativeCtx !=
null);
453 Debug.Assert(z3solver != IntPtr.Zero);
455 this.ntvContext = nativeCtx;
456 this.z3solver = z3solver;
458 Native.Z3_solver_inc_ref(nCtx, z3solver);
474 if (z3solver != IntPtr.Zero)
476 Native.Z3_solver_dec_ref(nCtx, z3solver);
477 z3solver = IntPtr.Zero;
479 GC.SuppressFinalize(
this);
489 default:
return Status.UNKNOWN;
The main interaction with Z3 happens via the Context.
Params MkParams()
Creates a new ParameterSet.
A Model contains interpretations (assignments) of constants and functions.
The main interaction with Z3 happens via the Context. NativeContext allows for efficient wrapper-redu...
Z3_ast MkApp(Z3_func_decl f, params Z3_ast[] args)
Create a new function application.
Z3_ast MkEq(Z3_ast x, Z3_ast y)
Creates the equality x = y .
Statistics.Entry[] GetStatistics(Z3_stats stats)
Retrieve statistics as an array of entries.
Z3_ast MkBound(uint index, Z3_sort sort)
Creates a new bound variable.
Z3_ast MkForall(Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight=1, Z3_ast[] patterns=null, Z3_ast[] noPatterns=null, Symbol quantifierID=null, Symbol skolemID=null)
Create a universal Quantifier.
Z3_func_decl MkFreshFuncDecl(string prefix, Z3_sort[] domain, Z3_sort range)
Creates a fresh function declaration with a name prefixed with prefix .
A Model contains interpretations (assignments) of constants and functions.
void Set(string name, uint value)
Sets parameter on the solver.
Z3_ast CongruenceRoot(Z3_ast t)
Retrieve congruence closure root of the term t relative to the current search state....
void ImportModelConverter(NativeSolver src)
Import model converter from other solver.
Status Check(params Z3_ast[] assumptions)
Checks whether the assertions in the solver are consistent or not.
void Set(string name, double value)
Sets parameter on the solver.
string Help
A string that describes all available solver parameters.
void Set(string name, string value)
Sets parameter on the solver.
void Pop(uint n=1)
Backtracks n backtracking points.
void Add(IEnumerable< Z3_ast > constraints)
Alias for Assert.
void Reset()
Resets the Solver.
void Add(params Z3_ast[] constraints)
Alias for Assert.
void Push()
Creates a backtracking point.
void FromString(string str)
Load solver assertions from a string.
uint NumAssertions
The number of assertions in the solver.
Z3_ast[] UnsatCore
The unsat core of the last Check.
uint NumScopes
The current number of backtracking points (scopes).
void Dispose()
Disposes of the underlying native Z3 object.
void AssertAndTrack(Z3_ast[] constraints, Z3_ast[] ps)
Assert multiple constraints into the solver, and track them (in the unsat) core using the Boolean con...
Z3_ast Proof
The proof of the last Check(params Expr[] assumptions).
void Assert(params Z3_ast[] constraints)
Assert a constraint (or multiple) into the solver.
Z3_ast[] Assertions
The set of asserted formulas.
override string ToString()
A string representation of the solver.
Z3_ast CongruenceExplain(Z3_ast a, Z3_ast b)
Explain congruence of a and b relative to the current search state.
void FromFile(string file)
Load solver assertions from a file.
void Set(string name, bool value)
Sets parameter on the solver.
string ReasonUnknown
A brief justification of why the last call to Check returned UNKNOWN.
Status Check(IEnumerable< Z3_ast > assumptions)
Checks whether the assertions in the solver are consistent or not.
Z3_ast CongruenceNext(Z3_ast t)
Retrieve congruence closure sibling of the term t relative to the current search state....
void AssertAndTrack(Z3_ast constraint, Z3_ast p)
Assert a constraint into the solver, and track it (in the unsat) core using the Boolean constant p.
Z3_ast[] NonUnits
Non-unit literals in the solver state.
void AssertInjective(Z3_func_decl f)
Add constraints to ensure the function f can only be injective. Example: for function f : D1 x D2 -> ...
Z3_ast[] Trail
Trail of the solver state after a check() call.
NativeSolver Translate(NativeContext ctx)
Create a clone of the current solver with respect to ctx.
Z3_ast[] Units
Currently inferred units.
Params Add(Symbol name, bool value)
Adds a parameter setting.
Statistical data is organized into pairs of [Key, Entry], where every Entry is either a DoubleEntry o...
Objects of this class track statistical information about solvers.
Symbols are used to name several term and type constructors.
The exception base class for error reporting from Z3.
Z3_lbool
Lifted Boolean type: false, undefined, true.
System.IntPtr Z3_func_decl