Skip to content

Commit bbe298c

Browse files
Try Z3's simple solver before the default one
The default solver's preprocessing tactics take most of the time of the small queries the verifier asks (about 5 ms each, against well under 1 ms for the simple solver). The simple solver now answers first, with a deterministic work budget, and only the queries it cannot decide go to the default solver as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0d489e1 commit bbe298c

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎liquidjava-verifier/src/main/java/liquidjava/smt/SMTEvaluator.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ public SMTResult verifySubtype(Predicate subRef, Predicate supRef, Context conte
4040
try (TranslatorToZ3 tz3 = new TranslatorToZ3(context)) {
4141
ExpressionToZ3Visitor visitor = new ExpressionToZ3Visitor(tz3);
4242
Expr<?> e = exp.accept(visitor);
43-
Solver solver = tz3.makeSolverForExpression(e);
43+
Solver solver = tz3.makeSimpleSolverForExpression(e);
4444
Status result = solver.check();
45+
if (result.equals(Status.UNKNOWN)) {
46+
solver = tz3.makeSolverForExpression(e);
47+
result = solver.check();
48+
}
4549

4650
// subRef is not a subtype of supRef
4751
if (result.equals(Status.SATISFIABLE)) {
@@ -78,8 +82,10 @@ public boolean isUnsatisfiable(Predicate predicate, Context context) throws Exce
7882
try (TranslatorToZ3 tz3 = new TranslatorToZ3(context)) {
7983
ExpressionToZ3Visitor visitor = new ExpressionToZ3Visitor(tz3);
8084
Expr<?> e = exp.accept(visitor);
81-
Solver solver = tz3.makeSolverForExpression(e);
82-
return solver.check().equals(Status.UNSATISFIABLE);
85+
Status result = tz3.makeSimpleSolverForExpression(e).check();
86+
if (result.equals(Status.UNKNOWN))
87+
result = tz3.makeSolverForExpression(e).check();
88+
return result.equals(Status.UNSATISFIABLE);
8389
}
8490
} catch (Z3Exception e) {
8591
throw new Z3Exception(e.getLocalizedMessage());

‎liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.microsoft.z3.FuncDecl;
99
import com.microsoft.z3.IntExpr;
1010
import com.microsoft.z3.IntNum;
11+
import com.microsoft.z3.Params;
1112
import com.microsoft.z3.RealExpr;
1213
import com.microsoft.z3.Solver;
1314
import com.microsoft.z3.Sort;
@@ -36,6 +37,12 @@
3637

3738
public class TranslatorToZ3 implements AutoCloseable {
3839

40+
/**
41+
* Deterministic work budget of the simple solver. Queries it decides use at most about a thousand units; ones it
42+
* cannot decide can take millions, and minutes, before it gives up
43+
*/
44+
private static final int SIMPLE_SOLVER_RLIMIT = 100_000;
45+
3946
private final com.microsoft.z3.Context z3 = new com.microsoft.z3.Context();
4047
private final Map<String, Expr<?>> varTranslation = new HashMap<>();
4148
private final Map<String, List<Expr<?>>> varSuperTypes = new HashMap<>();
@@ -66,9 +73,26 @@ public TranslatorToZ3(liquidjava.processor.context.Context context) {
6673
TranslatorContextToZ3.addGhostStates(z3, context.getGhostStates(), funcTranslation);
6774
}
6875

69-
@SuppressWarnings("unchecked")
7076
public Solver makeSolverForExpression(Expr<?> e) {
71-
Solver solver = z3.mkSolver();
77+
return addToSolver(z3.mkSolver(), e);
78+
}
79+
80+
/**
81+
* A solver for {@code e} without the preprocessing tactics of the default solver, which take most of the time of
82+
* the small queries the verifier asks. It decides fewer queries and can get stuck on the ones it cannot decide, so
83+
* it gives up after {@link #SIMPLE_SOLVER_RLIMIT} and an unknown result should be retried with
84+
* {@link #makeSolverForExpression}
85+
*/
86+
public Solver makeSimpleSolverForExpression(Expr<?> e) {
87+
Solver solver = z3.mkSimpleSolver();
88+
Params params = z3.mkParams();
89+
params.add("rlimit", SIMPLE_SOLVER_RLIMIT);
90+
solver.setParameters(params);
91+
return addToSolver(solver, e);
92+
}
93+
94+
@SuppressWarnings("unchecked")
95+
private Solver addToSolver(Solver solver, Expr<?> e) {
7296
for (BoolExpr axiom : staticConstantAxioms)
7397
solver.add(axiom);
7498
for (BoolExpr axiom : referenceIdentityAxioms)

0 commit comments

Comments
 (0)