Z3
 
Loading...
Searching...
No Matches
Data Structures | Public Member Functions
Solver Class Reference
+ Inheritance diagram for Solver:

Public Member Functions

String getHelp ()
 
void setParameters (Params value)
 
ParamDescrs getParameterDescriptions ()
 
int getNumScopes ()
 
void push ()
 
void pop ()
 
void pop (int n)
 
void reset ()
 
void interrupt ()
 
void add (Expr< BoolSort >... constraints)
 
void assertAndTrack (Expr< BoolSort >[] constraints, Expr< BoolSort >[] ps)
 
void assertAndTrack (Expr< BoolSort > constraint, Expr< BoolSort > p)
 
void fromFile (String file)
 Load solver assertions from a file.
 
void fromString (String str)
 Load solver assertions from a string.
 
int getNumAssertions ()
 
BoolExpr[] getAssertions ()
 
final Status check (Expr< BoolSort >... assumptions)
 
Status check ()
 
Status getConsequences (Expr< BoolSort >[] assumptions, Expr<?>[] variables, List< Expr< BoolSort > > consequences)
 
Model getModel ()
 
Expr<?> getProof ()
 
BoolExpr[] getUnsatCore ()
 
BoolExpr[] getUnits ()
 
BoolExpr[] getNonUnits ()
 
BoolExpr[] getTrail ()
 
String getReasonUnknown ()
 
int[] getTrailLevels ()
 
java.util.Iterator< BoolExpr[]> cube (Expr<?>[] vars, int cutoff)
 
Expr<?> congruenceRoot (Expr<?> t)
 
Expr<?> congruenceNext (Expr<?> t)
 
Expr<?> congruenceExplain (Expr<?> a, Expr<?> b)
 
void solveFor (Expr<?>[] variables, Expr<?>[] terms, BoolExpr[] guards)
 
void setInitialValue (Expr<?> var, Expr<?> value)
 
void importModelConverter (Solver src)
 
Solver translate (Context ctx)
 
Solver addSimplifier (Simplifier simplifier)
 
String toDimacs (boolean includeNames)
 
Statistics getStatistics ()
 
String toString ()
 

Additional Inherited Members

- Static Public Member Functions inherited from Z3Object
static long[] arrayToNative (Z3Object[] a)
 
static int arrayLength (Z3Object[] a)
 

Detailed Description

Solvers.

Definition at line 30 of file Solver.java.

Member Function Documentation

◆ add()

void add ( Expr< BoolSort >...  constraints)
inline

Assert a multiple constraints into the solver.

Exceptions
Z3Exception

Definition at line 128 of file Solver.java.

129 {
130 getContext().checkContextMatch(constraints);
131 for (Expr<BoolSort> a : constraints)
132 {
133 Native.solverAssert(getContext().nCtx(), getNativeObject(),
134 a.getNativeObject());
135 }
136 }

Referenced by Solver.__iadd__().

◆ addSimplifier()

Solver addSimplifier ( Simplifier  simplifier)
inline

Create a new solver with pre-processing simplification attached.

Parameters
simplifierThe simplifier to attach for pre-processing
Returns
A new solver with the simplifier applied

Definition at line 578 of file Solver.java.

579 {
580 return new Solver(getContext(), Native.solverAddSimplifier(
581 getContext().nCtx(), getNativeObject(), simplifier.getNativeObject()));
582 }

◆ assertAndTrack() [1/2]

void assertAndTrack ( Expr< BoolSort >  constraint,
Expr< BoolSort >  p 
)
inline

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

Remarks: This API is an alternative to check 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 and the Boolean literals provided using check with assumptions.

See also
Solver::check
Solver::assertAndTrack

Definition at line 184 of file Solver.java.

185 {
186 getContext().checkContextMatch(constraint);
187 getContext().checkContextMatch(p);
188
189 Native.solverAssertAndTrack(getContext().nCtx(), getNativeObject(),
190 constraint.getNativeObject(), p.getNativeObject());
191 }

◆ assertAndTrack() [2/2]

void assertAndTrack ( Expr< BoolSort >[]  constraints,
Expr< BoolSort >[]  ps 
)
inline

Assert multiple constraints into the solver, and track them (in the unsat) core using the Boolean constants in ps.

Remarks: This API is an alternative to check 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 and the Boolean literals provided using check with assumptions.

See also
Solver::check
Solver::assertAndTrack

Definition at line 155 of file Solver.java.

156 {
157 getContext().checkContextMatch(constraints);
158 getContext().checkContextMatch(ps);
159 if (constraints.length != ps.length) {
160 throw new Z3Exception("Argument size mismatch");
161 }
162
163 for (int i = 0; i < constraints.length; i++) {
164 Native.solverAssertAndTrack(getContext().nCtx(), getNativeObject(),
165 constraints[i].getNativeObject(), ps[i].getNativeObject());
166 }
167 }

◆ check() [1/2]

Status check ( )
inline

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

See also
Solver::getModel
Solver::getUnsatCore
Solver::getProof

Definition at line 262 of file Solver.java.

263 {
264 return check((Expr[]) null);
265 }

◆ check() [2/2]

final Status check ( Expr< BoolSort >...  assumptions)
inline

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

See also
Solver::getModel
Solver::getUnsatCore
Solver::getProof

Definition at line 240 of file Solver.java.

241 {
242 Z3_lbool r;
243 if (assumptions == null) {
244 r = Z3_lbool.fromInt(Native.solverCheck(getContext().nCtx(),
245 getNativeObject()));
246 } else {
247 r = Z3_lbool.fromInt(Native.solverCheckAssumptions(getContext()
248 .nCtx(), getNativeObject(), assumptions.length, AST
249 .arrayToNative(assumptions)));
250 }
251 return lboolToStatus(r);
252 }
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58

◆ congruenceExplain()

Expr<?> congruenceExplain ( Expr<?>  a,
Expr<?>  b 
)
inline

Return an explanation for why two expressions are congruent.

Parameters
aFirst expression
bSecond expression
Returns
An expression explaining the congruence between a and b
Exceptions
Z3Exception

Definition at line 508 of file Solver.java.

509 {
510 getContext().checkContextMatch(a);
511 getContext().checkContextMatch(b);
512 return Expr.create(getContext(),
513 Native.solverCongruenceExplain(getContext().nCtx(), getNativeObject(),
514 a.getNativeObject(), b.getNativeObject()));
515 }

◆ congruenceNext()

Expr<?> congruenceNext ( Expr<?>  t)
inline

Return the next element in the congruence class of the given expression. The congruence class forms a circular linked list.

Parameters
tThe expression to find the next congruent expression for
Returns
The next expression in the congruence class
Exceptions
Z3Exception

Definition at line 493 of file Solver.java.

494 {
495 getContext().checkContextMatch(t);
496 return Expr.create(getContext(),
497 Native.solverCongruenceNext(getContext().nCtx(), getNativeObject(), t.getNativeObject()));
498 }

◆ congruenceRoot()

Expr<?> congruenceRoot ( Expr<?>  t)
inline

Return the congruence class representative of the given expression. This is useful for querying the equality reasoning performed by the solver.

Parameters
tThe expression to find the congruence root for
Returns
The root expression of the congruence class
Exceptions
Z3Exception

Definition at line 478 of file Solver.java.

479 {
480 getContext().checkContextMatch(t);
481 return Expr.create(getContext(),
482 Native.solverCongruenceRoot(getContext().nCtx(), getNativeObject(), t.getNativeObject()));
483 }

◆ cube()

java.util.Iterator< BoolExpr[]> cube ( Expr<?>[]  vars,
int  cutoff 
)
inline

Return a sequence of cubes (conjunctions of literals) for partitioning the search space. Each cube represents a partial assignment that can be used as a starting point for parallel solving. This is primarily useful for cube-and-conquer parallel SAT solving strategies, where different cubes can be solved independently by different workers. The iterator yields cubes until the search space is exhausted.

Parameters
varsOptional array of variables to use as initial cube variables. If null, solver decides.
cutoffBacktrack level cutoff for cube generation
Returns
Iterator over arrays of Boolean expressions representing cubes
Exceptions
Z3Exception

Definition at line 425 of file Solver.java.

426 {
427 ASTVector cubeVars = new ASTVector(getContext());
428 if (vars != null) {
429 for (Expr<?> v : vars) {
430 cubeVars.push(v);
431 }
432 }
433
434 return new java.util.Iterator<BoolExpr[]>() {
435 private BoolExpr[] nextCube = computeNext();
436
437 private BoolExpr[] computeNext() {
438 ASTVector result = new ASTVector(getContext(),
439 Native.solverCube(getContext().nCtx(), getNativeObject(),
440 cubeVars.getNativeObject(), cutoff));
441 BoolExpr[] cube = result.ToBoolExprArray();
442
443 // Check for termination conditions
444 if (cube.length == 1 && cube[0].isFalse()) {
445 return null; // No more cubes
446 }
447 if (cube.length == 0) {
448 return null; // Search space exhausted
449 }
450 return cube;
451 }
452
453 @Override
454 public boolean hasNext() {
455 return nextCube != null;
456 }
457
458 @Override
459 public BoolExpr[] next() {
460 if (nextCube == null) {
461 throw new java.util.NoSuchElementException();
462 }
463 BoolExpr[] current = nextCube;
464 nextCube = computeNext();
465 return current;
466 }
467 };
468 }
java.util.Iterator< BoolExpr[]> cube(Expr<?>[] vars, int cutoff)
Definition Solver.java:425

◆ fromFile()

void fromFile ( String  file)
inline

Load solver assertions from a file.

Definition at line 196 of file Solver.java.

197 {
198 Native.solverFromFile(getContext().nCtx(), getNativeObject(), file);
199 }

◆ fromString()

void fromString ( String  str)
inline

Load solver assertions from a string.

Definition at line 204 of file Solver.java.

205 {
206 Native.solverFromString(getContext().nCtx(), getNativeObject(), str);
207 }

◆ getAssertions()

BoolExpr[] getAssertions ( )
inline

The set of asserted formulas.

Exceptions
Z3Exception

Definition at line 226 of file Solver.java.

227 {
228 ASTVector assrts = new ASTVector(getContext(), Native.solverGetAssertions(getContext().nCtx(), getNativeObject()));
229 return assrts.ToBoolExprArray();
230 }

◆ getConsequences()

Status getConsequences ( Expr< BoolSort >[]  assumptions,
Expr<?>[]  variables,
List< Expr< BoolSort > >  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.

Definition at line 278 of file Solver.java.

279 {
280 ASTVector result = new ASTVector(getContext());
281 ASTVector asms = new ASTVector(getContext());
282 ASTVector vars = new ASTVector(getContext());
283 for (Expr<BoolSort> asm : assumptions) asms.push(asm);
284 for (Expr<?> v : variables) vars.push(v);
285 int r = Native.solverGetConsequences(getContext().nCtx(), getNativeObject(), asms.getNativeObject(), vars.getNativeObject(), result.getNativeObject());
286 for (int i = 0; i < result.size(); ++i) consequences.add((BoolExpr) Expr.create(getContext(), result.get(i).getNativeObject()));
287 return lboolToStatus(Z3_lbool.fromInt(r));
288 }

◆ getHelp()

String getHelp ( )
inline

A string that describes all available solver parameters.

Definition at line 34 of file Solver.java.

35 {
36 return Native.solverGetHelp(getContext().nCtx(), getNativeObject());
37 }

◆ getModel()

Model getModel ( )
inline

The model of the last Check. Remarks: The result is null if Check was not invoked before, if its results was not SATISFIABLE, or if model production is not enabled.

Exceptions
Z3Exception

Definition at line 300 of file Solver.java.

301 {
302 long x = Native.solverGetModel(getContext().nCtx(), getNativeObject());
303 if (x == 0) {
304 return null;
305 } else {
306 return new Model(getContext(), x);
307 }
308 }
Model(ctx=None, eval={})
Definition z3py.py:7470

◆ getNonUnits()

BoolExpr[] getNonUnits ( )
inline

Retrieve non-unit atomic formulas in the solver state. Remarks: This retrieves atomic formulas that are not units after a check call.

Returns
An array of Boolean expressions representing the non-unit formulas
Exceptions
Z3Exception

Definition at line 366 of file Solver.java.

367 {
368 ASTVector nonUnits = new ASTVector(getContext(), Native.solverGetNonUnits(getContext().nCtx(), getNativeObject()));
369 return nonUnits.ToBoolExprArray();
370 }

◆ getNumAssertions()

int getNumAssertions ( )
inline

The number of assertions in the solver.

Exceptions
Z3Exception

Definition at line 215 of file Solver.java.

216 {
217 ASTVector assrts = new ASTVector(getContext(), Native.solverGetAssertions(getContext().nCtx(), getNativeObject()));
218 return assrts.size();
219 }

◆ getNumScopes()

int getNumScopes ( )
inline

The current number of backtracking points (scopes).

See also
Solver::pop
Solver::push

Definition at line 67 of file Solver.java.

68 {
69 return Native
70 .solverGetNumScopes(getContext().nCtx(), getNativeObject());
71 }

◆ getParameterDescriptions()

ParamDescrs getParameterDescriptions ( )
inline

Retrieves parameter descriptions for solver.

Exceptions
Z3Exception

Definition at line 56 of file Solver.java.

57 {
58 return new ParamDescrs(getContext(), Native.solverGetParamDescrs(
59 getContext().nCtx(), getNativeObject()));
60 }

◆ getProof()

Expr<?> getProof ( )
inline

The proof of the last Check. Remarks: The result is null if Check was not invoked before, if its results was not UNSATISFIABLE, or if proof production is disabled.

Exceptions
Z3Exception

Definition at line 319 of file Solver.java.

320 {
321 long x = Native.solverGetProof(getContext().nCtx(), getNativeObject());
322 if (x == 0) {
323 return null;
324 } else {
325 return Expr.create(getContext(), x);
326 }
327 }

◆ getReasonUnknown()

String getReasonUnknown ( )
inline

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

Definition at line 391 of file Solver.java.

392 {
393 return Native.solverGetReasonUnknown(getContext().nCtx(),
394 getNativeObject());
395 }

◆ getStatistics()

Statistics getStatistics ( )
inline

Solver statistics.

Exceptions
Z3Exception

Definition at line 600 of file Solver.java.

601 {
602 return new Statistics(getContext(), Native.solverGetStatistics(
603 getContext().nCtx(), getNativeObject()));
604 }

◆ getTrail()

BoolExpr[] getTrail ( )
inline

Retrieve the solver decision trail. Remarks: This retrieves the trail of decisions made by the solver after a check call. The trail represents the sequence of Boolean literals (decisions and propagations) in the order they were assigned.

Returns
An array of Boolean expressions representing the decision trail
Exceptions
Z3Exception

Definition at line 381 of file Solver.java.

382 {
383 ASTVector trail = new ASTVector(getContext(), Native.solverGetTrail(getContext().nCtx(), getNativeObject()));
384 return trail.ToBoolExprArray();
385 }

◆ getTrailLevels()

int[] getTrailLevels ( )
inline

Retrieve the decision levels for each literal in the solver's trail after a check call. The trail contains Boolean literals (decisions and propagations) in the order they were assigned. The returned array has one entry per trail literal, indicating at which decision level it was assigned.

Returns
An array where element i contains the decision level for the i-th trail literal
Exceptions
Z3Exception

Definition at line 405 of file Solver.java.

406 {
407 ASTVector trailVector = new ASTVector(getContext(), Native.solverGetTrail(getContext().nCtx(), getNativeObject()));
408 int[] levels = new int[trailVector.size()];
409 Native.solverGetLevels(getContext().nCtx(), getNativeObject(), trailVector.getNativeObject(), trailVector.size(), levels);
410 return levels;
411 }

◆ getUnits()

BoolExpr[] getUnits ( )
inline

Retrieve currently inferred units. Remarks: This retrieves the set of literals that the solver has inferred at the current decision level after a check call.

Returns
An array of Boolean expressions representing the inferred units
Exceptions
Z3Exception

Definition at line 353 of file Solver.java.

354 {
355 ASTVector units = new ASTVector(getContext(), Native.solverGetUnits(getContext().nCtx(), getNativeObject()));
356 return units.ToBoolExprArray();
357 }

◆ getUnsatCore()

BoolExpr[] getUnsatCore ( )
inline

The unsat core of the last Check. Remarks: 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.

Exceptions
Z3Exception

Definition at line 338 of file Solver.java.

339 {
340
341 ASTVector core = new ASTVector(getContext(), Native.solverGetUnsatCore(getContext().nCtx(), getNativeObject()));
342 return core.ToBoolExprArray();
343 }

◆ importModelConverter()

void importModelConverter ( Solver  src)
inline

Import model converter from other solver.

Parameters
srcThe solver to import the model converter from

Definition at line 559 of file Solver.java.

560 {
561 Native.solverImportModelConverter(getContext().nCtx(), src.getNativeObject(), getNativeObject());
562 }

◆ interrupt()

void interrupt ( )
inline

Interrupt the execution of the solver object. Remarks: This ensures that the interrupt applies only to the given solver object and it applies only if it is running.

Definition at line 118 of file Solver.java.

119 {
120 Native.solverInterrupt(getContext().nCtx(), getNativeObject());
121 }

◆ pop() [1/2]

void pop ( )
inline

Backtracks one backtracking point. Remarks: .

Definition at line 86 of file Solver.java.

87 {
88 pop(1);
89 }

Referenced by Solver.__exit__().

◆ pop() [2/2]

void pop ( int  n)
inline

Backtracks n backtracking points. Remarks: Note that an exception is thrown if n is not smaller than NumScopes

See also
Solver::push

Definition at line 98 of file Solver.java.

99 {
100 Native.solverPop(getContext().nCtx(), getNativeObject(), n);
101 }

Referenced by Solver.__exit__().

◆ push()

void push ( )
inline

Creates a backtracking point.

See also
Solver::pop

Definition at line 77 of file Solver.java.

78 {
79 Native.solverPush(getContext().nCtx(), getNativeObject());
80 }

Referenced by Solver.__enter__().

◆ reset()

void reset ( )
inline

Resets the Solver. Remarks: This removes all assertions from the solver.

Definition at line 108 of file Solver.java.

109 {
110 Native.solverReset(getContext().nCtx(), getNativeObject());
111 }

◆ setInitialValue()

void setInitialValue ( Expr<?>  var,
Expr<?>  value 
)
inline

Set an initial value for a variable to guide the solver's search heuristics. This can improve performance when good initial values are known for the problem domain.

Parameters
varThe variable to set an initial value for
valueThe initial value for the variable
Exceptions
Z3Exception

Definition at line 546 of file Solver.java.

547 {
548 getContext().checkContextMatch(var);
549 getContext().checkContextMatch(value);
550 Native.solverSetInitialValue(getContext().nCtx(), getNativeObject(),
551 var.getNativeObject(), value.getNativeObject());
552 }

◆ setParameters()

void setParameters ( Params  value)
inline

Sets the solver parameters.

Exceptions
Z3Exception

Definition at line 44 of file Solver.java.

45 {
46 getContext().checkContextMatch(value);
47 Native.solverSetParams(getContext().nCtx(), getNativeObject(),
48 value.getNativeObject());
49 }

◆ solveFor()

void solveFor ( Expr<?>[]  variables,
Expr<?>[]  terms,
BoolExpr[]  guards 
)
inline

Solve constraints for given variables, replacing their occurrences by terms. Guards are used to guard substitutions.

Parameters
variablesArray of variables to solve for
termsArray of terms to substitute for the variables
guardsArray of Boolean guards for the substitutions
Exceptions
Z3Exception

Definition at line 526 of file Solver.java.

527 {
528 ASTVector vars = new ASTVector(getContext());
529 ASTVector termVec = new ASTVector(getContext());
530 ASTVector guardVec = new ASTVector(getContext());
531 for (Expr<?> v : variables) vars.push(v);
532 for (Expr<?> t : terms) termVec.push(t);
533 for (BoolExpr g : guards) guardVec.push(g);
534 Native.solverSolveFor(getContext().nCtx(), getNativeObject(),
535 vars.getNativeObject(), termVec.getNativeObject(), guardVec.getNativeObject());
536 }

◆ toDimacs()

String toDimacs ( boolean  includeNames)
inline

Convert the solver's Boolean formula to DIMACS CNF format.

Parameters
includeNamesIf true, include variable names in the DIMACS output
Returns
A string containing the DIMACS CNF representation

Definition at line 590 of file Solver.java.

591 {
592 return Native.solverToDimacsString(getContext().nCtx(), getNativeObject(), includeNames);
593 }

◆ toString()

String toString ( )
inline

A string representation of the solver.

Definition at line 610 of file Solver.java.

611 {
612 return Native
613 .solverToString(getContext().nCtx(), getNativeObject());
614 }

◆ translate()

Solver translate ( Context  ctx)
inline

Create a clone of the current solver with respect toctx.

Definition at line 567 of file Solver.java.

568 {
569 return new Solver(ctx, Native.solverTranslate(getContext().nCtx(), getNativeObject(), ctx.nCtx()));
570 }

Referenced by AstRef.__copy__(), Goal.__copy__(), AstVector.__copy__(), FuncInterp.__copy__(), ModelRef.__copy__(), Goal.__deepcopy__(), AstVector.__deepcopy__(), FuncInterp.__deepcopy__(), and ModelRef.__deepcopy__().