Skip to content

Commit 48b66d4

Browse files
committed
Z3 Model: reduce internal variables in the model.
It seems as if Z3 produces n^2 additional variables in the model, if arrays are represented. Let's reduce this number and only list the relevant variables.
1 parent 0488a99 commit 48b66d4

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/org/sosy_lab/java_smt/solvers/z3/Z3Model.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.microsoft.z3.Z3Exception;
1717
import com.microsoft.z3.enumerations.Z3_decl_kind;
1818
import com.microsoft.z3.enumerations.Z3_sort_kind;
19+
import com.microsoft.z3.enumerations.Z3_symbol_kind;
1920
import java.util.ArrayList;
2021
import java.util.Collection;
2122
import java.util.HashSet;
@@ -83,8 +84,13 @@ private boolean isInternalSymbol(long funcDecl) {
8384
case Z3_OP_ARRAY_EXT:
8485
return true;
8586
default:
87+
long declName = Native.getDeclName(z3context, funcDecl);
88+
Z3_symbol_kind kind = Z3_symbol_kind.fromInt(Native.getSymbolKind(z3context, declName));
89+
if (kind == Z3_symbol_kind.Z3_INT_SYMBOL) { // bound variables
90+
return true;
91+
}
8692
return Z3_IRRELEVANT_MODEL_TERM_PATTERN
87-
.matcher(z3creator.symbolToString(Native.getDeclName(z3context, funcDecl)))
93+
.matcher(z3creator.symbolToString(declName))
8894
.matches();
8995
}
9096
}

src/org/sosy_lab/java_smt/test/ModelTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.Iterables;
22+
import com.google.common.collect.Range;
2223
import java.io.IOException;
2324
import java.math.BigInteger;
2425
import java.nio.file.Files;
@@ -1837,10 +1838,16 @@ public void testArrayWithManyValues() throws SolverException, InterruptedExcepti
18371838
}
18381839
BooleanFormula query = amgr.equivalence(array, storedArray);
18391840

1840-
testModelIterator(query);
1841+
List<ValueAssignment> assignments = testModelIterator(query);
1842+
1843+
// Check that we only provide relevant assignments and ignore solver-internal variables.
1844+
// Allow about 100 assignments plus small overhead, for arr[x] := x for 0 to 99.
1845+
// Some solvers return additional assignments for constant array assignments.
1846+
assertThat(assignments.size()).isIn(Range.closedOpen(99, 102));
18411847
}
18421848

1843-
private void testModelIterator(BooleanFormula f) throws SolverException, InterruptedException {
1849+
private ImmutableList<ValueAssignment> testModelIterator(BooleanFormula f)
1850+
throws SolverException, InterruptedException {
18441851
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
18451852
prover.push(f);
18461853

@@ -1851,6 +1858,7 @@ private void testModelIterator(BooleanFormula f) throws SolverException, Interru
18511858
// Check that we can iterate through with no crashes.
18521859
}
18531860
assertThat(prover.getModelAssignments()).containsExactlyElementsIn(m).inOrder();
1861+
return m.asList();
18541862
}
18551863
}
18561864
}

0 commit comments

Comments
 (0)