Z3
 
Loading...
Searching...
No Matches
Public Member Functions | Properties
NativeSolver Class Reference

Solvers. More...

+ Inheritance diagram for NativeSolver:

Public Member Functions

void Set (string name, bool value)
 Sets parameter on the solver.
 
void Set (string name, uint value)
 Sets parameter on the solver.
 
void Set (string name, double value)
 Sets parameter on the solver.
 
void Set (string name, string value)
 Sets parameter on the solver.
 
void Push ()
 Creates a backtracking point.
 
void Pop (uint n=1)
 Backtracks n backtracking points.
 
void Reset ()
 Resets the Solver.
 
void Assert (params Z3_ast[] constraints)
 Assert a constraint (or multiple) into the solver.
 
void Add (params Z3_ast[] constraints)
 Alias for Assert.
 
void Add (IEnumerable< Z3_ast > constraints)
 Alias for Assert.
 
void AssertInjective (Z3_func_decl f)
 Add constraints to ensure the function f can only be injective. Example: for function f : D1 x D2 -> R assert axioms forall (x1 : D1, x2 : D2) x1 = inv1(f(x1,x2)) forall (x1 : D1, x2 : D2) x2 = inv2(f(x1,x2))
 
void AssertAndTrack (Z3_ast[] constraints, Z3_ast[] ps)
 Assert multiple constraints into the solver, and track them (in the unsat) core using the Boolean constants in ps.
 
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.
 
void FromFile (string file)
 Load solver assertions from a file.
 
void FromString (string str)
 Load solver assertions from a string.
 
Z3_ast CongruenceRoot (Z3_ast t)
 Retrieve congruence closure root of the term t relative to the current search state. The function primarily works for SimpleSolver. Terms and variables that are eliminated during pre-processing are not visible to the congruence closure.
 
Z3_ast CongruenceNext (Z3_ast t)
 Retrieve congruence closure sibling of the term t relative to the current search state. The function primarily works for SimpleSolver. Terms and variables that are eliminated during pre-processing are not visible to the congruence closure.
 
Z3_ast CongruenceExplain (Z3_ast a, Z3_ast b)
 Explain congruence of a and b relative to the current search state.
 
Status Check (params Z3_ast[] assumptions)
 Checks whether the assertions in the solver are consistent or not.
 
Status Check (IEnumerable< Z3_ast > assumptions)
 Checks whether the assertions in the solver are consistent or not.
 
NativeSolver Translate (NativeContext ctx)
 Create a clone of the current solver with respect to ctx.
 
void ImportModelConverter (NativeSolver src)
 Import model converter from other solver.
 
override string ToString ()
 A string representation of the solver.
 
void Dispose ()
 Disposes of the underlying native Z3 object.
 

Properties

string Help [get]
 A string that describes all available solver parameters.
 
uint NumScopes [get]
 The current number of backtracking points (scopes).
 
uint NumAssertions [get]
 The number of assertions in the solver.
 
Z3_ast[] Assertions [get]
 The set of asserted formulas.
 
Z3_ast[] Units [get]
 Currently inferred units.
 
Z3_ast[] NonUnits [get]
 Non-unit literals in the solver state.
 
Z3_ast[] Trail [get]
 Trail of the solver state after a check() call.
 
NativeModel Model [get]
 The model of the last Check(params Expr[] assumptions).
 
Z3_ast Proof [get]
 The proof of the last Check(params Expr[] assumptions).
 
Z3_ast[] UnsatCore [get]
 The unsat core of the last Check.
 
string ReasonUnknown [get]
 A brief justification of why the last call to Check returned UNKNOWN.
 
Statistics.Entry[] Statistics [get]
 Solver statistics.
 

Detailed Description

Solvers.

Definition at line 40 of file NativeSolver.cs.

Member Function Documentation

◆ Add() [1/2]

void Add ( IEnumerable< Z3_ast constraints)

Alias for Assert.


◆ Add() [2/2]

void Add ( params Z3_ast[]  constraints)

Alias for Assert.


◆ Assert()

void Assert ( params Z3_ast[]  constraints)
inline

Assert a constraint (or multiple) into the solver.


Definition at line 153 of file NativeSolver.cs.

154 {
155 Debug.Assert(constraints != null);
156 Debug.Assert(constraints.All(c => c != IntPtr.Zero));
157
158 foreach (Z3_ast a in constraints)
159 {
160 Native.Z3_solver_assert(nCtx, z3solver, a);
161 }
162 }
System.IntPtr Z3_ast

Referenced by NativeSolver.AssertInjective(), and NativeSolver.Translate().

◆ AssertAndTrack() [1/2]

void AssertAndTrack ( Z3_ast  constraint,
Z3_ast  p 
)
inline

Assert a constraint into the solver, and track it (in the unsat) core using the Boolean constant p.

This API is an alternative to Check(Z3_ast[]) 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 AssertAndTrack(Z3_ast[],Z3_ast[]) and the Boolean literals provided using Check(Z3_ast[]) with assumptions.


Definition at line 242 of file NativeSolver.cs.

243 {
244 Debug.Assert(constraint != null);
245 Debug.Assert(p != null);
246
247 Native.Z3_solver_assert_and_track(nCtx, z3solver, constraint, p);
248 }

◆ AssertAndTrack() [2/2]

void AssertAndTrack ( Z3_ast[]  constraints,
Z3_ast[]  ps 
)
inline

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 Check(Z3_ast[]) 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 AssertAndTrack(Z3_ast[],Z3_ast[]) and the Boolean literals provided using Check(Z3_ast[]) with assumptions.


Definition at line 219 of file NativeSolver.cs.

220 {
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)
225 throw new Z3Exception("Argument size mismatch");
226
227 for (int i = 0; i < constraints.Length; i++)
228 Native.Z3_solver_assert_and_track(nCtx, z3solver, constraints[i], ps[i]);
229 }

◆ AssertInjective()

void AssertInjective ( Z3_func_decl  f)
inline

Add constraints to ensure the function f can only be injective. Example: for function f : D1 x D2 -> R assert axioms forall (x1 : D1, x2 : D2) x1 = inv1(f(x1,x2)) forall (x1 : D1, x2 : D2) x2 = inv2(f(x1,x2))

Parameters
f

Definition at line 183 of file NativeSolver.cs.

184 {
185 uint arity = Native.Z3_get_arity(nCtx, f);
186 Z3_sort range = Native.Z3_get_range(nCtx, f);
187 Z3_ast[] vars = new Z3_ast[arity];
188 Z3_sort[] sorts = new Z3_sort[arity];
189 Z3_symbol[] names = new Z3_symbol[arity];
190 for (uint i = 0; i < arity; ++i)
191 {
192 Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
193 vars[i] = ntvContext.MkBound(arity - i - 1, domain);
194 sorts[i] = domain;
195 names[i] = Native.Z3_mk_int_symbol(nCtx, (int)i);
196 }
197 Z3_ast app_f = IntPtr.Zero; // Context.MkApp(f, vars);
198 for (uint i = 0; i < arity; ++i)
199 {
200 Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
201 Z3_func_decl proj = ntvContext.MkFreshFuncDecl("inv", new Z3_sort[] { range }, domain);
202 Z3_ast body = ntvContext.MkEq(vars[i], ntvContext.MkApp(proj, app_f));
203 Z3_ast q = ntvContext.MkForall(names, sorts, body);
204 Assert(q);
205 }
206 }
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 .
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 .
void Assert(params Z3_ast[] constraints)
Assert a constraint (or multiple) into the solver.
System.IntPtr Z3_func_decl
System.IntPtr Z3_sort
System.IntPtr Z3_symbol
expr range(expr const &lo, expr const &hi)
Definition z3++.h:4240

◆ Check() [1/2]

Status Check ( IEnumerable< Z3_ast assumptions)
inline

Checks whether the assertions in the solver are consistent or not.

See also
Model, UnsatCore, Proof



Definition at line 350 of file NativeSolver.cs.

351 {
352 Z3_lbool r;
353 Z3_ast[] asms = assumptions.ToArray();
354 if (asms.Length == 0)
355 r = (Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
356 else
357 r = (Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)asms.Length, asms);
358 return lboolToStatus(r);
359 }
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58

◆ Check() [2/2]

Status Check ( params Z3_ast[]  assumptions)
inline

Checks whether the assertions in the solver are consistent or not.

See also
Model, UnsatCore, Proof



Definition at line 332 of file NativeSolver.cs.

333 {
334 Z3_lbool r;
335 if (assumptions == null || assumptions.Length == 0)
336 r = (Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
337 else
338 r = (Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)assumptions.Length, assumptions);
339 return lboolToStatus(r);
340 }

◆ CongruenceExplain()

Z3_ast CongruenceExplain ( Z3_ast  a,
Z3_ast  b 
)
inline

Explain congruence of a and b relative to the current search state.

Definition at line 317 of file NativeSolver.cs.

318 {
319 Debug.Assert(a != IntPtr.Zero);
320 Debug.Assert(b != IntPtr.Zero);
321 return Native.Z3_solver_congruence_explain(nCtx, z3solver, a, b);
322 }

◆ CongruenceNext()

Z3_ast CongruenceNext ( Z3_ast  t)
inline

Retrieve congruence closure sibling of the term t relative to the current search state. The function primarily works for SimpleSolver. Terms and variables that are eliminated during pre-processing are not visible to the congruence closure.

Definition at line 308 of file NativeSolver.cs.

309 {
310 Debug.Assert(t != IntPtr.Zero);
311 return Native.Z3_solver_congruence_next(nCtx, z3solver, t);
312 }

◆ CongruenceRoot()

Z3_ast CongruenceRoot ( Z3_ast  t)
inline

Retrieve congruence closure root of the term t relative to the current search state. The function primarily works for SimpleSolver. Terms and variables that are eliminated during pre-processing are not visible to the congruence closure.

Definition at line 297 of file NativeSolver.cs.

298 {
299 Debug.Assert(t != IntPtr.Zero);
300 return Native.Z3_solver_congruence_root(nCtx, z3solver, t);
301 }

◆ Dispose()

void Dispose ( )
inline

Disposes of the underlying native Z3 object.

Definition at line 472 of file NativeSolver.cs.

473 {
474 if (z3solver != IntPtr.Zero)
475 {
476 Native.Z3_solver_dec_ref(nCtx, z3solver);
477 z3solver = IntPtr.Zero;
478 }
479 GC.SuppressFinalize(this);
480 }

◆ FromFile()

void FromFile ( string  file)

Load solver assertions from a file.

◆ FromString()

void FromString ( string  str)

Load solver assertions from a string.

◆ ImportModelConverter()

void ImportModelConverter ( NativeSolver  src)
inline

Import model converter from other solver.

Definition at line 418 of file NativeSolver.cs.

419 {
420 Debug.Assert(src != null);
421
422 Native.Z3_solver_import_model_converter(nCtx, src.z3solver, z3solver);
423 }

◆ Pop()

void Pop ( uint  n = 1)

Backtracks n backtracking points.

Note that an exception is thrown if n is not smaller than NumScopes

See also
Push

◆ Push()

void Push ( )

Creates a backtracking point.

See also
Pop

◆ Reset()

void Reset ( )

Resets the Solver.

This removes all assertions from the solver.

◆ Set() [1/4]

void Set ( string  name,
bool  value 
)
inline

Sets parameter on the solver.

Definition at line 59 of file NativeSolver.cs.

60 {
61 SetParam((Z3_params p) => Native.Z3_params_set_bool(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), (byte)(value ? 1 : 0)));
62 }
System.IntPtr Z3_params

◆ Set() [2/4]

void Set ( string  name,
double  value 
)
inline

Sets parameter on the solver.

Definition at line 75 of file NativeSolver.cs.

76 {
77 SetParam((Z3_params p) => Native.Z3_params_set_double(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value));
78 }

◆ Set() [3/4]

void Set ( string  name,
string  value 
)
inline

Sets parameter on the solver.

Definition at line 83 of file NativeSolver.cs.

84 {
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));
87 }

◆ Set() [4/4]

void Set ( string  name,
uint  value 
)
inline

Sets parameter on the solver.

Definition at line 67 of file NativeSolver.cs.

68 {
69 SetParam((Z3_params p) => Native.Z3_params_set_uint(nCtx, p, Native.Z3_mk_string_symbol(nCtx, name), value));
70 }

◆ ToString()

override string ToString ( )
inline

A string representation of the solver.

Definition at line 440 of file NativeSolver.cs.

441 {
442 return Native.Z3_solver_to_string(nCtx, z3solver);
443 }

◆ Translate()

NativeSolver Translate ( NativeContext  ctx)
inline

Create a clone of the current solver with respect to ctx.

Definition at line 409 of file NativeSolver.cs.

410 {
411 Debug.Assert(ctx != null);
412 return new NativeSolver(ctx, Native.Z3_solver_translate(nCtx, z3solver, ctx.nCtx));
413 }

Property Documentation

◆ Assertions

Z3_ast [] Assertions
get

The set of asserted formulas.

Definition at line 271 of file NativeSolver.cs.

◆ Help

string Help
get

A string that describes all available solver parameters.

Definition at line 45 of file NativeSolver.cs.

◆ Model

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.

Definition at line 368 of file NativeSolver.cs.

369 {
370 get
371 {
372 IntPtr x = Native.Z3_solver_get_model(nCtx, z3solver);
373 return x == IntPtr.Zero
374 ? null
375 : new NativeModel(ntvContext, x);
376 }
377 }

◆ NonUnits

Z3_ast [] NonUnits
get

Non-unit literals in the solver state.

Definition at line 283 of file NativeSolver.cs.

◆ NumAssertions

uint NumAssertions
get

The number of assertions in the solver.

Definition at line 265 of file NativeSolver.cs.

◆ NumScopes

uint NumScopes
get

The current number of backtracking points (scopes).

See also
Pop, Push

Definition at line 129 of file NativeSolver.cs.

◆ Proof

Z3_ast Proof
get

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.

Definition at line 386 of file NativeSolver.cs.

◆ ReasonUnknown

string ReasonUnknown
get

A brief justification of why the last call to Check returned UNKNOWN.

Definition at line 403 of file NativeSolver.cs.

◆ Statistics

Solver statistics.

Definition at line 428 of file NativeSolver.cs.

429 {
430 get
431 {
432 var stats = Native.Z3_solver_get_statistics(nCtx, z3solver);
433 return ntvContext.GetStatistics(stats);
434 }
435 }
Statistics.Entry[] GetStatistics(Z3_stats stats)
Retrieve statistics as an array of entries.

◆ Trail

Z3_ast [] Trail
get

Trail of the solver state after a check() call.

Definition at line 289 of file NativeSolver.cs.

◆ Units

Z3_ast [] Units
get

Currently inferred units.

Definition at line 277 of file NativeSolver.cs.

◆ UnsatCore

Z3_ast [] UnsatCore
get

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.

Definition at line 397 of file NativeSolver.cs.