3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-28 00:48:45 +00:00

Java type generics (#4832)

* Generify, needs some testing and review

* Remove unnecessary change

* Whoops, ? capture that type

* Misread the docs, whoops

* More permissible arithmetic operations

* Implement believed Optimize generics

* Missed a few generics

* More permissible expr for arrays in parameters

* More permissible expr for bitvecs in parameters

* More permissible expr for bools in parameters

* More permissible expr for fps in parameters

* More permissible expr for fprms in parameters

* More permissible expr for ints in parameters

* More permissible expr for reals in parameters

* Undo breaking name conflict due to type erasure; see notes

* Whoops, fix typing of ReExpr

* Sort corrections for Re, Seq

* More permissible expr for regular expressions in parameters

* Fix name conflict between sequences and regular expressions; see notes

* Minor typo, big implications!

* Make Constructor consistent, associate captured types with other unknown capture types for datatype consistency

* More expressive; outputs of multiple datatype definitions are only known to be sort, not capture, and Constructor.of should make a capture

* Be less dumb and just type it a little differently

* Update examples, make sure to type Expr and FuncDecl sort returns

* General fixups

* Downgrade java version, make it only for the generic support, remove var and Expr[]::new construction

* Turns out Java 8 hadn't figured out how to do stream generics yet. Didn't even show up in my IDE, weird
This commit is contained in:
Addison Crump 2020-11-30 12:04:54 -06:00 committed by GitHub
parent bb24b3f2be
commit 3bca1fbcd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 2724 additions and 588 deletions

View file

@ -24,6 +24,7 @@ import com.microsoft.z3.enumerations.Z3_lbool;
/**
* Object for managing optimization context
**/
@SuppressWarnings("unchecked")
public class Optimize extends Z3Object {
/**
@ -55,10 +56,10 @@ public class Optimize extends Z3Object {
/**
* Assert a constraint (or multiple) into the optimize solver.
**/
public void Assert(BoolExpr ... constraints)
public void Assert(Expr<BoolSort> ... constraints)
{
getContext().checkContextMatch(constraints);
for (BoolExpr a : constraints)
for (Expr<BoolSort> a : constraints)
{
Native.optimizeAssert(getContext().nCtx(), getNativeObject(), a.getNativeObject());
}
@ -67,7 +68,7 @@ public class Optimize extends Z3Object {
/**
* Alias for Assert.
**/
public void Add(BoolExpr ... constraints)
public void Add(Expr<BoolSort> ... constraints)
{
Assert(constraints);
}
@ -75,7 +76,7 @@ public class Optimize extends Z3Object {
/**
* Handle to objectives returned by objective functions.
**/
public static class Handle {
public static class Handle<R extends Sort> {
private final Optimize opt;
private final int handle;
@ -89,7 +90,7 @@ public class Optimize extends Z3Object {
/**
* Retrieve a lower bound for the objective handle.
**/
public Expr getLower()
public Expr<R> getLower()
{
return opt.GetLower(handle);
}
@ -97,7 +98,7 @@ public class Optimize extends Z3Object {
/**
* Retrieve an upper bound for the objective handle.
**/
public Expr getUpper()
public Expr<R> getUpper()
{
return opt.GetUpper(handle);
}
@ -110,7 +111,7 @@ public class Optimize extends Z3Object {
* and otherwise is represented by the expression {@code value + eps * EPSILON},
* where {@code EPSILON} is an arbitrarily small real number.
*/
public Expr[] getUpperAsVector()
public Expr<?>[] getUpperAsVector()
{
return opt.GetUpperAsVector(handle);
}
@ -120,7 +121,7 @@ public class Optimize extends Z3Object {
*
* <p>See {@link #getUpperAsVector()} for triple semantics.
*/
public Expr[] getLowerAsVector()
public Expr<?>[] getLowerAsVector()
{
return opt.GetLowerAsVector(handle);
}
@ -128,7 +129,7 @@ public class Optimize extends Z3Object {
/**
* Retrieve the value of an objective.
**/
public Expr getValue()
public Expr<R> getValue()
{
return getLower();
}
@ -149,11 +150,11 @@ public class Optimize extends Z3Object {
* Return an objective which associates with the group of constraints.
*
**/
public Handle AssertSoft(BoolExpr constraint, int weight, String group)
public Handle<?> AssertSoft(Expr<BoolSort> constraint, int weight, String group)
{
getContext().checkContextMatch(constraint);
Symbol s = getContext().mkSymbol(group);
return new Handle(this, Native.optimizeAssertSoft(getContext().nCtx(), getNativeObject(), constraint.getNativeObject(), Integer.toString(weight), s.getNativeObject()));
return new Handle<>(this, Native.optimizeAssertSoft(getContext().nCtx(), getNativeObject(), constraint.getNativeObject(), Integer.toString(weight), s.getNativeObject()));
}
/**
@ -161,7 +162,7 @@ public class Optimize extends Z3Object {
* Produce a model that (when the objectives are bounded and
* don't use strict inequalities) meets the objectives.
**/
public Status Check(Expr... assumptions)
public Status Check(Expr<BoolSort>... assumptions)
{
Z3_lbool r;
if (assumptions == null) {
@ -243,34 +244,34 @@ public class Optimize extends Z3Object {
* Return a handle to the objective. The handle is used as
* to retrieve the values of objectives after calling Check.
**/
public Handle MkMaximize(Expr e)
public <R extends Sort> Handle<R> MkMaximize(Expr<R> e)
{
return new Handle(this, Native.optimizeMaximize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
return new Handle<>(this, Native.optimizeMaximize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
}
/**
* Declare an arithmetical minimization objective.
* Similar to MkMaximize.
**/
public Handle MkMinimize(Expr e)
public <R extends Sort> Handle<R> MkMinimize(Expr<R> e)
{
return new Handle(this, Native.optimizeMinimize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
return new Handle<>(this, Native.optimizeMinimize(getContext().nCtx(), getNativeObject(), e.getNativeObject()));
}
/**
* Retrieve a lower bound for the objective handle.
**/
private Expr GetLower(int index)
private <R extends Sort> Expr<R> GetLower(int index)
{
return Expr.create(getContext(), Native.optimizeGetLower(getContext().nCtx(), getNativeObject(), index));
return (Expr<R>) Expr.create(getContext(), Native.optimizeGetLower(getContext().nCtx(), getNativeObject(), index));
}
/**
* Retrieve an upper bound for the objective handle.
**/
private Expr GetUpper(int index)
private <R extends Sort> Expr<R> GetUpper(int index)
{
return Expr.create(getContext(), Native.optimizeGetUpper(getContext().nCtx(), getNativeObject(), index));
return (Expr<R>) Expr.create(getContext(), Native.optimizeGetUpper(getContext().nCtx(), getNativeObject(), index));
}
/**
@ -278,7 +279,7 @@ public class Optimize extends Z3Object {
*
* <p>See {@link Handle#getUpperAsVector}.
*/
private Expr[] GetUpperAsVector(int index) {
private Expr<?>[] GetUpperAsVector(int index) {
return unpackObjectiveValueVector(
Native.optimizeGetUpperAsVector(
getContext().nCtx(), getNativeObject(), index
@ -291,7 +292,7 @@ public class Optimize extends Z3Object {
*
* <p>See {@link Handle#getLowerAsVector}.
*/
private Expr[] GetLowerAsVector(int index) {
private Expr<?>[] GetLowerAsVector(int index) {
return unpackObjectiveValueVector(
Native.optimizeGetLowerAsVector(
getContext().nCtx(), getNativeObject(), index
@ -299,12 +300,12 @@ public class Optimize extends Z3Object {
);
}
private Expr[] unpackObjectiveValueVector(long nativeVec) {
private Expr<?>[] unpackObjectiveValueVector(long nativeVec) {
ASTVector vec = new ASTVector(
getContext(), nativeVec
);
return new Expr[] {
(Expr) vec.get(0), (Expr) vec.get(1), (Expr) vec.get(2)
(Expr<?>) vec.get(0), (Expr<?>) vec.get(1), (Expr<?>) vec.get(2)
};
}
@ -355,7 +356,7 @@ public class Optimize extends Z3Object {
/**
* The set of asserted formulas.
*/
public Expr[] getObjectives()
public Expr<?>[] getObjectives()
{
ASTVector objectives = new ASTVector(getContext(), Native.optimizeGetObjectives(getContext().nCtx(), getNativeObject()));
return objectives.ToExprArray();