Z3
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 More...
 
void Set (string name, uint value)
 Sets parameter on the solver More...
 
void Set (string name, double value)
 Sets parameter on the solver More...
 
void Set (string name, string value)
 Sets parameter on the solver More...
 
void Push ()
 Creates a backtracking point. More...
 
void Pop (uint n=1)
 Backtracks n backtracking points. More...
 
void Reset ()
 Resets the Solver. More...
 
void Assert (params Z3_ast[] constraints)
 Assert a constraint (or multiple) into the solver. More...
 
void Add (params Z3_ast[] constraints)
 Alias for Assert. More...
 
void Add (IEnumerable< Z3_ast > constraints)
 Alias for Assert. More...
 
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)) More...
 
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. More...
 
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. More...
 
void FromFile (string file)
 Load solver assertions from a file. More...
 
void FromString (string str)
 Load solver assertions from a string. More...
 
Status Check (params Z3_ast[] assumptions)
 Checks whether the assertions in the solver are consistent or not. More...
 
Status Check (IEnumerable< Z3_ast > assumptions)
 Checks whether the assertions in the solver are consistent or not. More...
 
NativeSolver Translate (NativeContext ctx)
 Create a clone of the current solver with respect to ctx. More...
 
void ImportModelConverter (NativeSolver src)
 Import model converter from other solver. More...
 
override string ToString ()
 A string representation of the solver. More...
 
void Dispose ()
 Disposes of the underlying native Z3 object. More...
 

Properties

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

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:4136

◆ 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 306 of file NativeSolver.cs.

307  {
308  Z3_lbool r;
309  Z3_ast[] asms = assumptions.ToArray();
310  if (asms.Length == 0)
311  r = (Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
312  else
313  r = (Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)asms.Length, asms);
314  return lboolToStatus(r);
315  }
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition: z3_api.h:61

◆ 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 288 of file NativeSolver.cs.

289  {
290  Z3_lbool r;
291  if (assumptions == null || assumptions.Length == 0)
292  r = (Z3_lbool)Native.Z3_solver_check(nCtx, z3solver);
293  else
294  r = (Z3_lbool)Native.Z3_solver_check_assumptions(nCtx, z3solver, (uint)assumptions.Length, assumptions);
295  return lboolToStatus(r);
296  }

◆ Dispose()

void Dispose ( )
inline

Disposes of the underlying native Z3 object.

Definition at line 428 of file NativeSolver.cs.

429  {
430  if (z3solver != IntPtr.Zero)
431  {
432  Native.Z3_solver_dec_ref(nCtx, z3solver);
433  z3solver = IntPtr.Zero;
434  }
435  GC.SuppressFinalize(this);
436  }

◆ 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 374 of file NativeSolver.cs.

375  {
376  Debug.Assert(src != null);
377 
378  Native.Z3_solver_import_model_converter(nCtx, src.z3solver, z3solver);
379  }

◆ 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
Definition: NativeSolver.cs:32

◆ 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 396 of file NativeSolver.cs.

397  {
398  return Native.Z3_solver_to_string(nCtx, z3solver);
399  }

◆ Translate()

NativeSolver Translate ( NativeContext  ctx)
inline

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

Definition at line 365 of file NativeSolver.cs.

366  {
367  Debug.Assert(ctx != null);
368  return new NativeSolver(ctx, Native.Z3_solver_translate(nCtx, z3solver, ctx.nCtx));
369  }

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 324 of file NativeSolver.cs.

325  {
326  get
327  {
328  IntPtr x = Native.Z3_solver_get_model(nCtx, z3solver);
329  return x == IntPtr.Zero
330  ? null
331  : new NativeModel(ntvContext, x);
332  }
333  }

◆ 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 342 of file NativeSolver.cs.

◆ ReasonUnknown

string ReasonUnknown
get

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

Definition at line 359 of file NativeSolver.cs.

◆ Statistics

Solver statistics.

Definition at line 384 of file NativeSolver.cs.

385  {
386  get
387  {
388  var stats = Native.Z3_solver_get_statistics(nCtx, z3solver);
389  return ntvContext.GetStatistics(stats);
390  }
391  }
Statistics.Entry[] GetStatistics(Z3_stats stats)
Retrieve statistics as an array of entries

◆ 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 353 of file NativeSolver.cs.