Skip to content

Commit d637fd4

Browse files
Hackeython smt mod (KeYProject#3418)
2 parents ed673b0 + d15f5d3 commit d637fd4

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

key.core/src/main/java/de/uka/ilkd/key/smt/newsmt2/IntegerOpHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void init(MasterHandler masterHandler, Services services, Properties hand
4848
supportedOperators.put(mul, "*");
4949
supportedOperators.put(integerLDT.getSub(), "-");
5050
supportedOperators.put(integerLDT.getDiv(), "div");
51+
supportedOperators.put(integerLDT.getMod(), "mod");
5152
supportedOperators.put(integerLDT.getNeg(), "-");
5253

5354
supportedOperators.put(integerLDT.getLessOrEquals(), "<=");
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* This file is part of KeY - https://key-project.org
2+
* KeY is licensed under the GNU General Public License Version 2
3+
* SPDX-License-Identifier: GPL-2.0-only */
4+
package de.uka.ilkd.key.smt.newsmt2;
5+
6+
import java.io.File;
7+
8+
import de.uka.ilkd.key.control.DefaultUserInterfaceControl;
9+
import de.uka.ilkd.key.control.KeYEnvironment;
10+
import de.uka.ilkd.key.proof.Goal;
11+
import de.uka.ilkd.key.proof.Proof;
12+
import de.uka.ilkd.key.proof.io.ProblemLoaderException;
13+
import de.uka.ilkd.key.smt.SMTProblem;
14+
import de.uka.ilkd.key.smt.SMTSolverResult;
15+
import de.uka.ilkd.key.smt.SMTTestSettings;
16+
import de.uka.ilkd.key.smt.SolverLauncher;
17+
import de.uka.ilkd.key.smt.solvertypes.SolverType;
18+
import de.uka.ilkd.key.smt.solvertypes.SolverTypeImplementation;
19+
import de.uka.ilkd.key.smt.solvertypes.SolverTypes;
20+
21+
import org.key_project.util.helper.FindResources;
22+
23+
import org.junit.Test;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
/**
30+
* This class is for testing the translation of modulo from KeY to SMT
31+
*
32+
* @author Nils Buchholz, 2024
33+
*/
34+
public class TestSMTMod {
35+
36+
private static final Logger LOGGER = LoggerFactory.getLogger(TestSMTMod.class);
37+
38+
private static final File testCaseDirectory = FindResources.getTestCasesDirectory();
39+
40+
private static final SolverType Z3_SOLVER = SolverTypes.getSolverTypes().stream()
41+
.filter(it -> it.getClass().equals(SolverTypeImplementation.class)
42+
&& it.getName().equals("Z3"))
43+
.findFirst().orElse(null);
44+
45+
private static final SolverType CVC4_SOLVER = SolverTypes.getSolverTypes().stream()
46+
.filter(it -> it.getClass().equals(SolverTypeImplementation.class)
47+
&& it.getName().equals("CVC4"))
48+
.findFirst().orElse(null);
49+
50+
/**
51+
* This tests if x mod y is non-negative and x mod y < |y| for y != 0
52+
* thus satisfying the definition of euclidean modulo
53+
* Tests for Z3 and CVC4
54+
*
55+
* @throws ProblemLoaderException Occured Exception during load of problem file
56+
*/
57+
@Test
58+
public void testModSpec() throws ProblemLoaderException {
59+
KeYEnvironment<DefaultUserInterfaceControl> env =
60+
KeYEnvironment.load(new File(testCaseDirectory, "smt/modSpec.key"));
61+
try {
62+
Proof proof = env.getLoadedProof();
63+
assertNotNull(proof);
64+
assertEquals(1, proof.openGoals().size());
65+
Goal g = proof.openGoals().iterator().next();
66+
SMTSolverResult result;
67+
if (Z3_SOLVER.isInstalled(true)) {
68+
result = checkGoal(g, Z3_SOLVER);
69+
assertSame(SMTSolverResult.ThreeValuedTruth.VALID, result.isValid());
70+
} else {
71+
LOGGER.warn("Warning:Z3 solver not installed, tests skipped.");
72+
}
73+
if (CVC4_SOLVER.isInstalled(true)) {
74+
result = checkGoal(g, CVC4_SOLVER);
75+
assertSame(SMTSolverResult.ThreeValuedTruth.VALID, result.isValid());
76+
} else {
77+
LOGGER.warn("Warning:CVC4 solver not installed, tests skipped.");
78+
}
79+
} finally {
80+
env.dispose();
81+
}
82+
}
83+
84+
private SMTSolverResult checkGoal(Goal g, SolverType solverType) {
85+
SolverLauncher launcher = new SolverLauncher(new SMTTestSettings());
86+
SMTProblem problem = new SMTProblem(g);
87+
launcher.launch(problem, g.proof().getServices(), solverType);
88+
return problem.getFinalResult();
89+
}
90+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\functions {
2+
int y;
3+
int x;
4+
}
5+
6+
\problem {
7+
((y>0) -> x % y >= 0 & x % y < y) & ((y<0) -> x%y >= 0 & x % y < -y)
8+
}
9+

0 commit comments

Comments
 (0)