3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-25 01:55:32 +00:00

Mixing Integers and Rational in the new Java API #5085 (#5098)

* Added covariance to arithmetic operations

* Added distillSort

* Update JavaGenericExample.java

Co-authored-by: Alexander Kreuzer <alexander.kreuzer@sap.com>
Co-authored-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Alexander Kreuzer 2021-03-16 05:24:23 -07:00 committed by GitHub
parent ee614c2e46
commit dc5fa89de3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 12 deletions

View file

@ -829,6 +829,37 @@ class JavaGenericExample
} catch (Z3Exception ignored)
{
}
// Coercing type change in Z3
Expr<IntSort> integerDivision = ctx.mkDiv(ctx.mkInt(1), ctx.mkInt(2));
System.out.printf("%s -> %s%n", integerDivision, integerDivision.simplify()); // (div 1 2) -> 0
Expr<RealSort> realDivision = ctx.mkDiv(ctx.mkReal(1), ctx.mkReal(2));
System.out.printf("%s -> %s%n", realDivision, realDivision.simplify()); // (/ 1.0 2.0) -> 1/2
Expr<ArithSort> mixedDivision1 = ctx.mkDiv(ctx.mkReal(1), ctx.mkInt(2));
Expr<ArithSort> tmp = mixedDivision1;
// the return type is a Expr<ArithSort> here but since we know it is a
// real view it as such.
Expr<RealSort> mixedDivision2 = mixedDivision1.distillSort(RealSort.class);
System.out.printf("%s -> %s%n", mixedDivision2, mixedDivision2.simplify()); // (/ 1.0 (to_real 2)) -> 1/2
// empty distillSort
mixedDivision1.distillSort(ArithSort.class);
try {
mixedDivision1.distillSort(IntSort.class);
throw new TestFailedException(); // unreachable
} catch (Z3Exception exception) {
System.out.println(exception); // com.microsoft.z3.Z3Exception: Cannot cast expression of sort
// com.microsoft.z3.RealSort to com.microsoft.z3.IntSort.
}
Expr<BoolSort> eq1 = ctx.mkEq(realDivision, integerDivision);
System.out.printf("%s -> %s%n", eq1, eq1.simplify()); // (= (/ 1.0 2.0) (to_real (div 1 2))) -> false
Expr<BoolSort> eq2 = ctx.mkEq(realDivision, mixedDivision2);
System.out.printf("%s -> %s%n", eq2, eq2.simplify()); // (= (/ 1.0 2.0) (/ 1.0 (to_real 2))) -> true
}
// / Shows how to use Solver(logic)