Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IntSort> makeJavaDiv(IntExpr a, IntExpr b) {
ArithExpr<IntSort> quotient = z3.mkDiv(a, b);
ArithExpr<IntSort> 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<IntSort> makeJavaMod(IntExpr a, IntExpr b) {
IntExpr remainder = z3.mkMod(a, b);
Expr<IntSort> 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<RealSort> quotient = z3.mkDiv(a, b);
RealExpr floor = z3.mkInt2Real(z3.mkReal2Int(quotient));
ArithExpr<RealSort> 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) {
Expand All @@ -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) {
Expand Down
Loading