Z3
 
Loading...
Searching...
No Matches
Solver.java
Go to the documentation of this file.
1
19package com.microsoft.z3;
20
21import com.microsoft.z3.enumerations.Z3_lbool;
22
23import java.lang.ref.ReferenceQueue;
24import java.util.*;
25
29@SuppressWarnings("unchecked")
30public class Solver extends Z3Object {
34 public String getHelp()
35 {
36 return Native.solverGetHelp(getContext().nCtx(), getNativeObject());
37 }
38
44 public void setParameters(Params value)
45 {
46 getContext().checkContextMatch(value);
47 Native.solverSetParams(getContext().nCtx(), getNativeObject(),
48 value.getNativeObject());
49 }
50
57 {
58 return new ParamDescrs(getContext(), Native.solverGetParamDescrs(
59 getContext().nCtx(), getNativeObject()));
60 }
61
67 public int getNumScopes()
68 {
69 return Native
70 .solverGetNumScopes(getContext().nCtx(), getNativeObject());
71 }
72
77 public void push()
78 {
79 Native.solverPush(getContext().nCtx(), getNativeObject());
80 }
81
86 public void pop()
87 {
88 pop(1);
89 }
90
98 public void pop(int n)
99 {
100 Native.solverPop(getContext().nCtx(), getNativeObject(), n);
101 }
102
108 public void reset()
109 {
110 Native.solverReset(getContext().nCtx(), getNativeObject());
111 }
112
118 public void interrupt()
119 {
120 Native.solverInterrupt(getContext().nCtx(), getNativeObject());
121 }
122
128 public void add(Expr<BoolSort>... constraints)
129 {
130 getContext().checkContextMatch(constraints);
131 for (Expr<BoolSort> a : constraints)
132 {
133 Native.solverAssert(getContext().nCtx(), getNativeObject(),
134 a.getNativeObject());
135 }
136 }
137
138
155 public void assertAndTrack(Expr<BoolSort>[] constraints, Expr<BoolSort>[] ps)
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 }
168
185 {
186 getContext().checkContextMatch(constraint);
187 getContext().checkContextMatch(p);
188
189 Native.solverAssertAndTrack(getContext().nCtx(), getNativeObject(),
190 constraint.getNativeObject(), p.getNativeObject());
191 }
192
196 public void fromFile(String file)
197 {
198 Native.solverFromFile(getContext().nCtx(), getNativeObject(), file);
199 }
200
204 public void fromString(String str)
205 {
206 Native.solverFromString(getContext().nCtx(), getNativeObject(), str);
207 }
208
209
215 public int getNumAssertions()
216 {
217 ASTVector assrts = new ASTVector(getContext(), Native.solverGetAssertions(getContext().nCtx(), getNativeObject()));
218 return assrts.size();
219 }
220
227 {
228 ASTVector assrts = new ASTVector(getContext(), Native.solverGetAssertions(getContext().nCtx(), getNativeObject()));
229 return assrts.ToBoolExprArray();
230 }
231
239 @SafeVarargs
240 public final Status check(Expr<BoolSort>... assumptions)
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 }
253
261 @SuppressWarnings("rawtypes")
262 public Status check()
263 {
264 return check((Expr[]) null);
265 }
266
278 public Status getConsequences(Expr<BoolSort>[] assumptions, Expr<?>[] variables, List<Expr<BoolSort>> consequences)
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 }
289
290
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 }
309
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 }
328
339 {
340
341 ASTVector core = new ASTVector(getContext(), Native.solverGetUnsatCore(getContext().nCtx(), getNativeObject()));
342 return core.ToBoolExprArray();
343 }
344
354 {
355 ASTVector units = new ASTVector(getContext(), Native.solverGetUnits(getContext().nCtx(), getNativeObject()));
356 return units.ToBoolExprArray();
357 }
358
367 {
368 ASTVector nonUnits = new ASTVector(getContext(), Native.solverGetNonUnits(getContext().nCtx(), getNativeObject()));
369 return nonUnits.ToBoolExprArray();
370 }
371
382 {
383 ASTVector trail = new ASTVector(getContext(), Native.solverGetTrail(getContext().nCtx(), getNativeObject()));
384 return trail.ToBoolExprArray();
385 }
386
391 public String getReasonUnknown()
392 {
393 return Native.solverGetReasonUnknown(getContext().nCtx(),
394 getNativeObject());
395 }
396
405 public int[] getTrailLevels()
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 }
412
425 public java.util.Iterator<BoolExpr[]> cube(Expr<?>[] vars, int cutoff)
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 }
469
479 {
480 getContext().checkContextMatch(t);
481 return Expr.create(getContext(),
482 Native.solverCongruenceRoot(getContext().nCtx(), getNativeObject(), t.getNativeObject()));
483 }
484
494 {
495 getContext().checkContextMatch(t);
496 return Expr.create(getContext(),
497 Native.solverCongruenceNext(getContext().nCtx(), getNativeObject(), t.getNativeObject()));
498 }
499
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 }
516
526 public void solveFor(Expr<?>[] variables, Expr<?>[] terms, BoolExpr[] guards)
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 }
537
546 public void setInitialValue(Expr<?> var, Expr<?> value)
547 {
548 getContext().checkContextMatch(var);
549 getContext().checkContextMatch(value);
550 Native.solverSetInitialValue(getContext().nCtx(), getNativeObject(),
551 var.getNativeObject(), value.getNativeObject());
552 }
553
560 {
561 Native.solverImportModelConverter(getContext().nCtx(), src.getNativeObject(), getNativeObject());
562 }
563
568 {
569 return new Solver(ctx, Native.solverTranslate(getContext().nCtx(), getNativeObject(), ctx.nCtx()));
570 }
571
578 public Solver addSimplifier(Simplifier simplifier)
579 {
580 return new Solver(getContext(), Native.solverAddSimplifier(
581 getContext().nCtx(), getNativeObject(), simplifier.getNativeObject()));
582 }
583
590 public String toDimacs(boolean includeNames)
591 {
592 return Native.solverToDimacsString(getContext().nCtx(), getNativeObject(), includeNames);
593 }
594
601 {
602 return new Statistics(getContext(), Native.solverGetStatistics(
603 getContext().nCtx(), getNativeObject()));
604 }
605
609 @Override
610 public String toString()
611 {
612 return Native
613 .solverToString(getContext().nCtx(), getNativeObject());
614 }
615
619 private Status lboolToStatus(Z3_lbool r)
620 {
621 switch (r)
622 {
623 case Z3_L_TRUE:
624 return Status.SATISFIABLE;
625 case Z3_L_FALSE:
626 return Status.UNSATISFIABLE;
627 default:
628 return Status.UNKNOWN;
629 }
630 }
631
632 Solver(Context ctx, long obj)
633 {
634 super(ctx, obj);
635 }
636
637 @Override
638 void incRef() {
639 Native.solverIncRef(getContext().nCtx(), getNativeObject());
640 }
641
642 @Override
643 void addToReferenceQueue() {
644 getContext().getReferenceQueue().storeReference(this, SolverRef::new);
645 }
646
647 private static class SolverRef extends Z3ReferenceQueue.Reference<Solver> {
648
649 private SolverRef(Solver referent, ReferenceQueue<Z3Object> q) {
650 super(referent, q);
651 }
652
653 @Override
654 void decRef(Context ctx, long z3Obj) {
655 Native.solverDecRef(ctx.nCtx(), z3Obj);
656 }
657 }
658}
boolean isFalse()
Definition Expr.java:365
void setInitialValue(Expr<?> var, Expr<?> value)
Definition Solver.java:546
void solveFor(Expr<?>[] variables, Expr<?>[] terms, BoolExpr[] guards)
Definition Solver.java:526
Solver translate(Context ctx)
Definition Solver.java:567
Status getConsequences(Expr< BoolSort >[] assumptions, Expr<?>[] variables, List< Expr< BoolSort > > consequences)
Definition Solver.java:278
ParamDescrs getParameterDescriptions()
Definition Solver.java:56
String toDimacs(boolean includeNames)
Definition Solver.java:590
final Status check(Expr< BoolSort >... assumptions)
Definition Solver.java:240
void assertAndTrack(Expr< BoolSort > constraint, Expr< BoolSort > p)
Definition Solver.java:184
Expr<?> congruenceExplain(Expr<?> a, Expr<?> b)
Definition Solver.java:508
Expr<?> congruenceNext(Expr<?> t)
Definition Solver.java:493
void setParameters(Params value)
Definition Solver.java:44
BoolExpr[] getUnsatCore()
Definition Solver.java:338
BoolExpr[] getUnits()
Definition Solver.java:353
void importModelConverter(Solver src)
Definition Solver.java:559
java.util.Iterator< BoolExpr[]> cube(Expr<?>[] vars, int cutoff)
Definition Solver.java:425
Expr<?> congruenceRoot(Expr<?> t)
Definition Solver.java:478
Solver addSimplifier(Simplifier simplifier)
Definition Solver.java:578
BoolExpr[] getAssertions()
Definition Solver.java:226
void fromString(String str)
Load solver assertions from a string.
Definition Solver.java:204
BoolExpr[] getNonUnits()
Definition Solver.java:366
void add(Expr< BoolSort >... constraints)
Definition Solver.java:128
BoolExpr[] getTrail()
Definition Solver.java:381
void fromFile(String file)
Load solver assertions from a file.
Definition Solver.java:196
void assertAndTrack(Expr< BoolSort >[] constraints, Expr< BoolSort >[] ps)
Definition Solver.java:155
Statistics getStatistics()
Definition Solver.java:600
static long[] arrayToNative(Z3Object[] a)
Definition Z3Object.java:73
static Status fromInt(int v)
Definition Status.java:41
Z3_lbool
Lifted Boolean type: false, undefined, true.
Definition z3_api.h:58
@ Z3_L_TRUE
Definition z3_api.h:61
@ Z3_L_FALSE
Definition z3_api.h:59