Z3
Public Member Functions | Properties
Solver Class Reference

Solvers. More...

+ Inheritance diagram for Solver:

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 Set (string name, Symbol value)
 Sets parameter on the solver More...
 
void Set (Symbol name, bool value)
 Sets parameter on the solver More...
 
void Set (Symbol name, uint value)
 Sets parameter on the solver More...
 
void Set (Symbol name, double value)
 Sets parameter on the solver More...
 
void Set (Symbol name, string value)
 Sets parameter on the solver More...
 
void Set (Symbol name, Symbol 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 BoolExpr[] constraints)
 Assert a constraint (or multiple) into the solver. More...
 
void Add (params BoolExpr[] constraints)
 Alias for Assert. More...
 
void Add (IEnumerable< BoolExpr > constraints)
 Alias for Assert. More...
 
void AssertAndTrack (BoolExpr[] constraints, BoolExpr[] ps)
 Assert multiple constraints into the solver, and track them (in the unsat) core using the Boolean constants in ps. More...
 
void AssertAndTrack (BoolExpr constraint, BoolExpr 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 Expr[] assumptions)
 Checks whether the assertions in the solver are consistent or not. More...
 
Status Check (IEnumerable< BoolExpr > assumptions)
 Checks whether the assertions in the solver are consistent or not. More...
 
Status Consequences (IEnumerable< BoolExpr > assumptions, IEnumerable< Expr > variables, out BoolExpr[] consequences)
 Retrieve fixed assignments to the set of variables in the form of consequences. Each consequence is an implication of the form More...
 
IEnumerable< BoolExpr[]> Cube ()
 Return a set of cubes. More...
 
Solver Translate (Context ctx)
 Create a clone of the current solver with respect to ctx. More...
 
void ImportModelConverter (Solver src)
 Import model converter from other solver. More...
 
override string ToString ()
 A string representation of the solver. More...
 
- Public Member Functions inherited from Z3Object
void Dispose ()
 Disposes of the underlying native Z3 object. More...
 

Properties

string Help [get]
 A string that describes all available solver parameters. More...
 
Params Parameters [set]
 Sets the solver parameters. More...
 
ParamDescrs ParameterDescriptions [get]
 Retrieves parameter descriptions for solver. More...
 
uint NumScopes [get]
 The current number of backtracking points (scopes). More...
 
uint NumAssertions [get]
 The number of assertions in the solver. More...
 
BoolExpr[] Assertions [get]
 The set of asserted formulas. More...
 
BoolExpr[] Units [get]
 Currently inferred units. More...
 
Model Model [get]
 The model of the last Check(params Expr[] assumptions). More...
 
Expr Proof [get]
 The proof of the last Check(params Expr[] assumptions). More...
 
BoolExpr[] 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...
 
uint BacktrackLevel [get, set]
 Backtrack level that can be adjusted by conquer process More...
 
BoolExpr[] CubeVariables [get, set]
 Variables available and returned by the cuber. More...
 
Statistics Statistics [get]
 Solver statistics. More...
 
- Properties inherited from Z3Object
Context Context [get]
 Access Context object More...
 

Detailed Description

Solvers.

Definition at line 30 of file Solver.cs.

Member Function Documentation

◆ Add() [1/2]

void Add ( IEnumerable< BoolExpr constraints)
inline

Alias for Assert.


Definition at line 223 of file Solver.cs.

224  {
225  Assert(constraints.ToArray());
226  }
void Assert(params BoolExpr[] constraints)
Assert a constraint (or multiple) into the solver.
Definition: Solver.cs:200

◆ Add() [2/2]

void Add ( params BoolExpr[]  constraints)
inline

Alias for Assert.


Definition at line 215 of file Solver.cs.

216  {
217  Assert(constraints);
218  }

◆ Assert()

void Assert ( params BoolExpr[]  constraints)
inline

Assert a constraint (or multiple) into the solver.


Definition at line 200 of file Solver.cs.

201  {
202  Debug.Assert(constraints != null);
203  Debug.Assert(constraints.All(c => c != null));
204 
205  Context.CheckContextMatch<BoolExpr>(constraints);
206  foreach (BoolExpr a in constraints)
207  {
208  Native.Z3_solver_assert(Context.nCtx, NativeObject, a.NativeObject);
209  }
210  }
Context Context
Access Context object
Definition: Z3Object.cs:111

Referenced by Solver.Add(), Context.MkSolver(), and Solver.Translate().

◆ AssertAndTrack() [1/2]

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


Definition at line 264 of file Solver.cs.

265  {
266  Debug.Assert(constraint != null);
267  Debug.Assert(p != null);
268  Context.CheckContextMatch(constraint);
269  Context.CheckContextMatch(p);
270 
271  Native.Z3_solver_assert_and_track(Context.nCtx, NativeObject, constraint.NativeObject, p.NativeObject);
272  }

◆ AssertAndTrack() [2/2]

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


Definition at line 239 of file Solver.cs.

240  {
241  Debug.Assert(constraints != null);
242  Debug.Assert(constraints.All(c => c != null));
243  Debug.Assert(ps.All(c => c != null));
244  Context.CheckContextMatch<BoolExpr>(constraints);
245  Context.CheckContextMatch<BoolExpr>(ps);
246  if (constraints.Length != ps.Length)
247  throw new Z3Exception("Argument size mismatch");
248 
249  for (int i = 0 ; i < constraints.Length; i++)
250  Native.Z3_solver_assert_and_track(Context.nCtx, NativeObject, constraints[i].NativeObject, ps[i].NativeObject);
251  }

◆ Check() [1/2]

Status Check ( IEnumerable< BoolExpr assumptions)
inline

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

See also
Model, UnsatCore, Proof



Definition at line 354 of file Solver.cs.

355  {
356  Z3_lbool r;
357  BoolExpr[] asms = assumptions.ToArray();
358  if (asms.Length == 0)
359  r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
360  else
361  r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)asms.Length, AST.ArrayToNative(asms));
362  return lboolToStatus(r);
363  }
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition: z3_api.h:60

◆ Check() [2/2]

Status Check ( params Expr[]  assumptions)
inline

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

See also
Model, UnsatCore, Proof



Definition at line 336 of file Solver.cs.

337  {
338  Z3_lbool r;
339  if (assumptions == null || assumptions.Length == 0)
340  r = (Z3_lbool)Native.Z3_solver_check(Context.nCtx, NativeObject);
341  else
342  r = (Z3_lbool)Native.Z3_solver_check_assumptions(Context.nCtx, NativeObject, (uint)assumptions.Length, AST.ArrayToNative(assumptions));
343  return lboolToStatus(r);
344  }

◆ Consequences()

Status Consequences ( IEnumerable< BoolExpr assumptions,
IEnumerable< Expr variables,
out BoolExpr[]  consequences 
)
inline

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.

See also
Model, UnsatCore, Proof



Definition at line 380 of file Solver.cs.

381  {
382  using ASTVector result = new ASTVector(Context);
383  using ASTVector asms = new ASTVector(Context);
384  using ASTVector vars = new ASTVector(Context);
385  foreach (var asm in assumptions) asms.Push(asm);
386  foreach (var v in variables) vars.Push(v);
387  Z3_lbool r = (Z3_lbool)Native.Z3_solver_get_consequences(Context.nCtx, NativeObject, asms.NativeObject, vars.NativeObject, result.NativeObject);
388  consequences = result.ToBoolExprArray();
389  return lboolToStatus(r);
390  }

◆ Cube()

IEnumerable<BoolExpr[]> Cube ( )
inline

Return a set of cubes.

Definition at line 474 of file Solver.cs.

475  {
476  using ASTVector cv = new ASTVector(Context);
477  if (CubeVariables != null)
478  foreach (var b in CubeVariables) cv.Push(b);
479 
480  while (true) {
481  var lvl = BacktrackLevel;
482  BacktrackLevel = uint.MaxValue;
483  using ASTVector r = new ASTVector(Context, Native.Z3_solver_cube(Context.nCtx, NativeObject, cv.NativeObject, lvl));
484  var v = r.ToBoolExprArray();
485  CubeVariables = cv.ToBoolExprArray();
486  if (v.Length == 1 && v[0].IsFalse) {
487  break;
488  }
489  yield return v;
490  if (v.Length == 0) {
491  break;
492  }
493  }
494  }
uint BacktrackLevel
Backtrack level that can be adjusted by conquer process
Definition: Solver.cs:463
BoolExpr[] CubeVariables
Variables available and returned by the cuber.
Definition: Solver.cs:468

◆ FromFile()

void FromFile ( string  file)
inline

Load solver assertions from a file.

Definition at line 277 of file Solver.cs.

278  {
279  Native.Z3_solver_from_file(Context.nCtx, NativeObject, file);
280  }

◆ FromString()

void FromString ( string  str)
inline

Load solver assertions from a string.

Definition at line 285 of file Solver.cs.

286  {
287  Native.Z3_solver_from_string(Context.nCtx, NativeObject, str);
288  }

◆ ImportModelConverter()

void ImportModelConverter ( Solver  src)
inline

Import model converter from other solver.

Definition at line 508 of file Solver.cs.

509  {
510  Native.Z3_solver_import_model_converter(Context.nCtx, src.NativeObject, NativeObject);
511  }

◆ Pop()

void Pop ( uint  n = 1)
inline

Backtracks n backtracking points.

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

See also
Push

Definition at line 183 of file Solver.cs.

184  {
185  Native.Z3_solver_pop(Context.nCtx, NativeObject, n);
186  }

◆ Push()

void Push ( )
inline

Creates a backtracking point.

See also
Pop

Definition at line 173 of file Solver.cs.

174  {
175  Native.Z3_solver_push(Context.nCtx, NativeObject);
176  }

◆ Reset()

void Reset ( )
inline

Resets the Solver.

This removes all assertions from the solver.

Definition at line 192 of file Solver.cs.

193  {
194  Native.Z3_solver_reset(Context.nCtx, NativeObject);
195  }

◆ Set() [1/10]

void Set ( string  name,
bool  value 
)
inline

Sets parameter on the solver

Definition at line 61 of file Solver.cs.

62  {
63  using var parameters = Context.MkParams().Add(name, value);
64  Parameters = parameters;
65  }
Params MkParams()
Creates a new ParameterSet.
Definition: Context.cs:3460
Params Add(Symbol name, bool value)
Adds a parameter setting.
Definition: Params.cs:33
Params Parameters
Sets the solver parameters.
Definition: Solver.cs:48

◆ Set() [2/10]

void Set ( string  name,
double  value 
)
inline

Sets parameter on the solver

Definition at line 79 of file Solver.cs.

80  {
81  using var parameters = Context.MkParams().Add(name, value);
82  Parameters = parameters;
83  }

◆ Set() [3/10]

void Set ( string  name,
string  value 
)
inline

Sets parameter on the solver

Definition at line 88 of file Solver.cs.

89  {
90  using var parameters = Context.MkParams().Add(name, value);
91  Parameters = parameters;
92  }

◆ Set() [4/10]

void Set ( string  name,
Symbol  value 
)
inline

Sets parameter on the solver

Definition at line 97 of file Solver.cs.

98  {
99  using var parameters = Context.MkParams().Add(name, value);
100  Parameters = parameters;
101  }

◆ Set() [5/10]

void Set ( string  name,
uint  value 
)
inline

Sets parameter on the solver

Definition at line 70 of file Solver.cs.

71  {
72  using var parameters = Context.MkParams().Add(name, value);
73  Parameters = parameters;
74  }

◆ Set() [6/10]

void Set ( Symbol  name,
bool  value 
)
inline

Sets parameter on the solver

Definition at line 106 of file Solver.cs.

107  {
108  using var parameters = Context.MkParams().Add(name, value);
109  Parameters = parameters;
110  }

◆ Set() [7/10]

void Set ( Symbol  name,
double  value 
)
inline

Sets parameter on the solver

Definition at line 124 of file Solver.cs.

125  {
126  using var parameters = Context.MkParams().Add(name, value);
127  Parameters = parameters;
128  }

◆ Set() [8/10]

void Set ( Symbol  name,
string  value 
)
inline

Sets parameter on the solver

Definition at line 133 of file Solver.cs.

134  {
135  using var parameters = Context.MkParams().Add(name, value);
136  Parameters = parameters;
137  }

◆ Set() [9/10]

void Set ( Symbol  name,
Symbol  value 
)
inline

Sets parameter on the solver

Definition at line 142 of file Solver.cs.

143  {
144  using var parameters = Context.MkParams().Add(name, value);
145  Parameters = parameters;
146  }

◆ Set() [10/10]

void Set ( Symbol  name,
uint  value 
)
inline

Sets parameter on the solver

Definition at line 115 of file Solver.cs.

116  {
117  using var parameters = Context.MkParams().Add(name, value);
118  Parameters = parameters;
119  }

◆ ToString()

override string ToString ( )
inline

A string representation of the solver.

Definition at line 528 of file Solver.cs.

529  {
530  return Native.Z3_solver_to_string(Context.nCtx, NativeObject);
531  }

◆ Translate()

Solver Translate ( Context  ctx)
inline

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

Definition at line 499 of file Solver.cs.

500  {
501  Debug.Assert(ctx != null);
502  return new Solver(ctx, Native.Z3_solver_translate(Context.nCtx, NativeObject, ctx.nCtx));
503  }

Property Documentation

◆ Assertions

BoolExpr [] Assertions
get

The set of asserted formulas.

Definition at line 305 of file Solver.cs.

306  {
307  get
308  {
309 
310  using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
311  return assertions.ToBoolExprArray();
312  }
313  }

◆ BacktrackLevel

uint BacktrackLevel
getset

Backtrack level that can be adjusted by conquer process

Definition at line 463 of file Solver.cs.

463 { get; set; }

Referenced by Solver.Cube().

◆ CubeVariables

BoolExpr [] CubeVariables
getset

Variables available and returned by the cuber.

Definition at line 468 of file Solver.cs.

468 { get; set; }

Referenced by Solver.Cube().

◆ Help

string Help
get

A string that describes all available solver parameters.

Definition at line 35 of file Solver.cs.

36  {
37  get
38  {
39 
40  return Native.Z3_solver_get_help(Context.nCtx, NativeObject);
41  }
42  }

◆ Model

Model Model
get

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 399 of file Solver.cs.

400  {
401  get
402  {
403  IntPtr x = Native.Z3_solver_get_model(Context.nCtx, NativeObject);
404  if (x == IntPtr.Zero)
405  return null;
406  else
407  return new Model(Context, x);
408  }
409  }
Model Model
The model of the last Check(params Expr[] assumptions).
Definition: Solver.cs:400

◆ NumAssertions

uint NumAssertions
get

The number of assertions in the solver.

Definition at line 293 of file Solver.cs.

294  {
295  get
296  {
297  using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
298  return assertions.Size;
299  }
300  }

◆ NumScopes

uint NumScopes
get

The current number of backtracking points (scopes).

See also
Pop, Push

Definition at line 164 of file Solver.cs.

165  {
166  get { return Native.Z3_solver_get_num_scopes(Context.nCtx, NativeObject); }
167  }

◆ ParameterDescriptions

ParamDescrs ParameterDescriptions
get

Retrieves parameter descriptions for solver.

Definition at line 153 of file Solver.cs.

154  {
155  get { return new ParamDescrs(Context, Native.Z3_solver_get_param_descrs(Context.nCtx, NativeObject)); }
156  }

◆ Parameters

Params Parameters
set

Sets the solver parameters.

Definition at line 47 of file Solver.cs.

48  {
49  set
50  {
51  Debug.Assert(value != null);
52 
53  Context.CheckContextMatch(value);
54  Native.Z3_solver_set_params(Context.nCtx, NativeObject, value.NativeObject);
55  }
56  }

Referenced by Solver.Set().

◆ Proof

Expr 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 418 of file Solver.cs.

419  {
420  get
421  {
422  IntPtr x = Native.Z3_solver_get_proof(Context.nCtx, NativeObject);
423  if (x == IntPtr.Zero)
424  return null;
425  else
426  return Expr.Create(Context, x);
427  }
428  }

◆ ReasonUnknown

string ReasonUnknown
get

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

Definition at line 451 of file Solver.cs.

452  {
453  get
454  {
455 
456  return Native.Z3_solver_get_reason_unknown(Context.nCtx, NativeObject);
457  }
458  }

◆ Statistics

Solver statistics.

Definition at line 516 of file Solver.cs.

517  {
518  get
519  {
520 
521  return new Statistics(Context, Native.Z3_solver_get_statistics(Context.nCtx, NativeObject));
522  }
523  }
Statistics Statistics
Solver statistics.
Definition: Solver.cs:517

◆ Units

BoolExpr [] Units
get

Currently inferred units.

Definition at line 318 of file Solver.cs.

319  {
320  get
321  {
322 
323  using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_units(Context.nCtx, NativeObject));
324  return assertions.ToBoolExprArray();
325  }
326  }

◆ UnsatCore

BoolExpr [] 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 438 of file Solver.cs.

439  {
440  get
441  {
442 
443  using ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));
444  return core.ToBoolExprArray();
445  }
446  }