From ba2b6311f16da542b93522cee14cbaf3393904f8 Mon Sep 17 00:00:00 2001 From: Catarina Gamboa Date: Mon, 30 Nov 2026 20:23:29 +0000 Subject: [PATCH 1/3] Give integer / and % Java's semantics for negative operands Z3's integer div and mod are Euclidean: the remainder is never negative. Java rounds the quotient toward zero and gives the remainder the sign of the dividend, so -7 / 2 was -4 and -7 % 2 was 1 in the solver. Build Java's result from Z3's, which only differs when the dividend is negative and the division is not exact. Longs, which are reals in the solver, now use the same integer division instead of real division. Fixes #564 Co-Authored-By: Claude Opus 5.5 --- .../CorrectArithmeticBinaryOperations.java | 8 ++++ .../java/testSuite/ErrorIntegerDivision.java | 5 +++ .../java/liquidjava/smt/TranslatorToZ3.java | 42 +++++++++++++++---- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/liquidjava-example/src/main/java/testSuite/CorrectArithmeticBinaryOperations.java b/liquidjava-example/src/main/java/testSuite/CorrectArithmeticBinaryOperations.java index 5e2d9c2a2..f848ca4ca 100644 --- a/liquidjava-example/src/main/java/testSuite/CorrectArithmeticBinaryOperations.java +++ b/liquidjava-example/src/main/java/testSuite/CorrectArithmeticBinaryOperations.java @@ -18,5 +18,13 @@ public static void main(String[] args) { int l = k * t; @Refinement("_ > 0") int m = l / 2; + + // Java's / rounds toward zero and % has the sign of the dividend + @Refinement("_ == -3") + int n = -7 / 2; + @Refinement("_ == -1") + int o = -7 % 2; + @Refinement("_ == -3") + long p = -7L / 2L; } } diff --git a/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java b/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java index 7f7f258cc..38e787158 100644 --- a/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java +++ b/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java @@ -8,4 +8,9 @@ public class ErrorIntegerDivision { int half(@Refinement("_ > 0") int x) { return x / 2; // Expect: Refinement Error } + + @Refinement("_ == 0 || _ == 1") + int parity(int x) { + return x % 2; // Expect: Refinement Error + } } diff --git a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java index 30afc9760..c2ac18aa1 100644 --- a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java +++ b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java @@ -8,6 +8,7 @@ import com.microsoft.z3.FuncDecl; import com.microsoft.z3.IntExpr; import com.microsoft.z3.IntNum; +import com.microsoft.z3.IntSort; import com.microsoft.z3.Params; import com.microsoft.z3.RealExpr; import com.microsoft.z3.Solver; @@ -439,23 +440,50 @@ public Expr makeMul(Expr eval, Expr eval2) { return z3.mkMul((ArithExpr) eval, (ArithExpr) eval2); } - @SuppressWarnings({ "unchecked", "rawtypes" }) public Expr makeDiv(Expr eval, Expr eval2) { if (eval instanceof FPExpr || eval2 instanceof FPExpr) return z3.mkFPDiv(z3.mkFPRoundNearestTiesToEven(), toFP(eval), toFP(eval2)); + // longs are reals, but Java divides them as integers if (eval instanceof RealExpr || eval2 instanceof RealExpr) - return z3.mkDiv(toReal(eval), toReal(eval2)); - // Z3 integer division does not model Java's truncation toward zero for negative operands - return z3.mkDiv((ArithExpr) eval, (ArithExpr) eval2); + return z3.mkInt2Real(makeJavaDiv(toInt(eval), toInt(eval2))); + return makeJavaDiv((IntExpr) eval, (IntExpr) eval2); } public Expr makeMod(Expr eval, Expr eval2) { if (eval instanceof FPExpr || eval2 instanceof FPExpr) return z3.mkFPRem(toFP(eval), toFP(eval2)); - // Z3 integer modulo does not model Java's dividend-signed remainder for negative operands if (eval instanceof RealExpr || eval2 instanceof RealExpr) - return z3.mkMod(toInt(eval), toInt(eval2)); - return z3.mkMod((IntExpr) eval, (IntExpr) eval2); + return z3.mkInt2Real(makeJavaMod(toInt(eval), toInt(eval2))); + return makeJavaMod((IntExpr) eval, (IntExpr) eval2); + } + + /** + * Java's integer division, which rounds toward zero. Z3's is Euclidean (its remainder is never negative), which + * only differs from Java's when the dividend is negative and the division is not exact: then Java's quotient is one + * step closer to zero + */ + private Expr makeJavaDiv(IntExpr a, IntExpr b) { + ArithExpr quotient = z3.mkDiv(a, b); + ArithExpr towardZero = z3.mkAdd(quotient, z3.mkITE(isPositive(b), z3.mkInt(1), z3.mkInt(-1))); + return z3.mkITE(euclideanIsJava(a, b), quotient, towardZero); + } + + /** + * Java's integer remainder, which has the sign of the dividend. Z3's is never negative, so when the dividend is + * negative and the division is not exact Java's remainder is Z3's minus the divisor's absolute value + */ + private Expr makeJavaMod(IntExpr a, IntExpr b) { + IntExpr remainder = z3.mkMod(a, b); + Expr dividendSigned = z3.mkITE(isPositive(b), z3.mkSub(remainder, b), z3.mkAdd(remainder, b)); + return z3.mkITE(euclideanIsJava(a, b), remainder, dividendSigned); + } + + private BoolExpr euclideanIsJava(IntExpr a, IntExpr b) { + return z3.mkOr(z3.mkGe(a, z3.mkInt(0)), z3.mkEq(z3.mkMod(a, b), z3.mkInt(0))); + } + + private BoolExpr isPositive(IntExpr e) { + return z3.mkGt(e, z3.mkInt(0)); } private RealExpr toReal(Expr e) { From 3b2918e34b44eee0923b9a8d5f2bc584a9b08e64 Mon Sep 17 00:00:00 2001 From: Catarina Gamboa Date: Mon, 30 Nov 2026 21:04:24 +0000 Subject: [PATCH 2/3] Divide longs by rounding the real quotient toward zero Longs are reals in the solver, which may pick non-integral values for them. Converting the operands to integers first could turn a positive divisor into zero and lose the quotient's sign, so x / y with x < 0 and y > 0 was no longer known to be at most zero. Move the parity check to a fixture no other test counts errors in. Co-Authored-By: Claude Opus 5.5 --- .../java/testSuite/ErrorIntegerDivision.java | 5 ----- .../testSuite/ErrorSpecificArithmetic.java | 5 +++++ .../java/liquidjava/smt/TranslatorToZ3.java | 21 +++++++++++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java b/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java index 38e787158..7f7f258cc 100644 --- a/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java +++ b/liquidjava-example/src/main/java/testSuite/ErrorIntegerDivision.java @@ -8,9 +8,4 @@ public class ErrorIntegerDivision { int half(@Refinement("_ > 0") int x) { return x / 2; // Expect: Refinement Error } - - @Refinement("_ == 0 || _ == 1") - int parity(int x) { - return x % 2; // Expect: Refinement Error - } } diff --git a/liquidjava-example/src/main/java/testSuite/ErrorSpecificArithmetic.java b/liquidjava-example/src/main/java/testSuite/ErrorSpecificArithmetic.java index 296322ecd..e1554dfef 100644 --- a/liquidjava-example/src/main/java/testSuite/ErrorSpecificArithmetic.java +++ b/liquidjava-example/src/main/java/testSuite/ErrorSpecificArithmetic.java @@ -14,4 +14,9 @@ public static void main(String[] args) { @Refinement("_ > 20") int c = b * -1; // Expect: Refinement Error } + + @Refinement("_ == 0 || _ == 1") + static int parity(int x) { + return x % 2; // Expect: Refinement Error + } } diff --git a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java index c2ac18aa1..2fdf5f446 100644 --- a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java +++ b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java @@ -11,6 +11,7 @@ import com.microsoft.z3.IntSort; import com.microsoft.z3.Params; import com.microsoft.z3.RealExpr; +import com.microsoft.z3.RealSort; import com.microsoft.z3.Solver; import com.microsoft.z3.Sort; import com.microsoft.z3.Symbol; @@ -443,17 +444,18 @@ public Expr makeMul(Expr eval, Expr eval2) { public Expr makeDiv(Expr eval, Expr eval2) { if (eval instanceof FPExpr || eval2 instanceof FPExpr) return z3.mkFPDiv(z3.mkFPRoundNearestTiesToEven(), toFP(eval), toFP(eval2)); - // longs are reals, but Java divides them as integers if (eval instanceof RealExpr || eval2 instanceof RealExpr) - return z3.mkInt2Real(makeJavaDiv(toInt(eval), toInt(eval2))); + return makeLongDiv(toReal(eval), toReal(eval2)); return makeJavaDiv((IntExpr) eval, (IntExpr) eval2); } public Expr makeMod(Expr eval, Expr eval2) { if (eval instanceof FPExpr || eval2 instanceof FPExpr) return z3.mkFPRem(toFP(eval), toFP(eval2)); - if (eval instanceof RealExpr || eval2 instanceof RealExpr) - return z3.mkInt2Real(makeJavaMod(toInt(eval), toInt(eval2))); + if (eval instanceof RealExpr || eval2 instanceof RealExpr) { + RealExpr a = toReal(eval), b = toReal(eval2); + return z3.mkSub(a, z3.mkMul(b, makeLongDiv(a, b))); + } return makeJavaMod((IntExpr) eval, (IntExpr) eval2); } @@ -478,6 +480,17 @@ private Expr makeJavaMod(IntExpr a, IntExpr b) { return z3.mkITE(euclideanIsJava(a, b), remainder, dividendSigned); } + /** + * Java's division of longs, which are reals: the real quotient rounded toward zero. Unlike converting the operands + * to integers first, it keeps the quotient's sign when the solver picks non-integral values for them + */ + private RealExpr makeLongDiv(RealExpr a, RealExpr b) { + ArithExpr quotient = z3.mkDiv(a, b); + RealExpr down = z3.mkInt2Real(z3.mkReal2Int(quotient)); + RealExpr up = z3.mkInt2Real(z3.mkReal2Int(z3.mkUnaryMinus(quotient))); + return (RealExpr) z3.mkITE(z3.mkGe(quotient, z3.mkReal(0)), down, z3.mkUnaryMinus(up)); + } + private BoolExpr euclideanIsJava(IntExpr a, IntExpr b) { return z3.mkOr(z3.mkGe(a, z3.mkInt(0)), z3.mkEq(z3.mkMod(a, b), z3.mkInt(0))); } From 56c88b5d458053fd8664e0c0aaed30095a4fde21 Mon Sep 17 00:00:00 2001 From: Catarina Gamboa Date: Mon, 30 Nov 2026 21:37:29 +0000 Subject: [PATCH 3/3] Remove the unused toInt and name the long rounding cases Co-Authored-By: Claude Opus 5.5 --- .../main/java/liquidjava/smt/TranslatorToZ3.java | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java index 2fdf5f446..cd5d42f28 100644 --- a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java +++ b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java @@ -454,6 +454,7 @@ public Expr makeMod(Expr eval, Expr eval2) { return z3.mkFPRem(toFP(eval), toFP(eval2)); if (eval instanceof RealExpr || eval2 instanceof RealExpr) { RealExpr a = toReal(eval), b = toReal(eval2); + // Java defines the remainder by a == (a / b) * b + a % b return z3.mkSub(a, z3.mkMul(b, makeLongDiv(a, b))); } return makeJavaMod((IntExpr) eval, (IntExpr) eval2); @@ -486,9 +487,9 @@ private Expr makeJavaMod(IntExpr a, IntExpr b) { */ private RealExpr makeLongDiv(RealExpr a, RealExpr b) { ArithExpr quotient = z3.mkDiv(a, b); - RealExpr down = z3.mkInt2Real(z3.mkReal2Int(quotient)); - RealExpr up = z3.mkInt2Real(z3.mkReal2Int(z3.mkUnaryMinus(quotient))); - return (RealExpr) z3.mkITE(z3.mkGe(quotient, z3.mkReal(0)), down, z3.mkUnaryMinus(up)); + RealExpr floor = z3.mkInt2Real(z3.mkReal2Int(quotient)); + ArithExpr ceiling = z3.mkUnaryMinus(z3.mkInt2Real(z3.mkReal2Int(z3.mkUnaryMinus(quotient)))); + return (RealExpr) z3.mkITE(z3.mkGe(quotient, z3.mkReal(0)), floor, ceiling); } private BoolExpr euclideanIsJava(IntExpr a, IntExpr b) { @@ -507,14 +508,6 @@ private RealExpr toReal(Expr e) { throw new NotImplementedException(); } - private IntExpr toInt(Expr e) { - if (e instanceof IntExpr) - return (IntExpr) e; - if (e instanceof RealExpr) - return z3.mkReal2Int((RealExpr) e); - throw new NotImplementedException(); - } - private FPExpr toFP(Expr e) { FPExpr f; if (e instanceof FPExpr) {