diff --git a/liquidjava-example/src/main/java/testSuite/CorrectArithmeticBinaryOperations.java b/liquidjava-example/src/main/java/testSuite/CorrectArithmeticBinaryOperations.java index 5e2d9c2a..f848ca4c 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/ErrorSpecificArithmetic.java b/liquidjava-example/src/main/java/testSuite/ErrorSpecificArithmetic.java index 296322ec..e1554dfe 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 30afc976..cd5d42f2 100644 --- a/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java +++ b/liquidjava-verifier/src/main/java/liquidjava/smt/TranslatorToZ3.java @@ -8,8 +8,10 @@ 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.RealSort; import com.microsoft.z3.Solver; import com.microsoft.z3.Sort; import com.microsoft.z3.Symbol; @@ -439,23 +441,63 @@ 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)); 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 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)); - // 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); + 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); + } + + /** + * 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); + } + + /** + * 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 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) { + 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) { @@ -466,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) {