Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java type generics #4832

Merged
merged 27 commits into from
Nov 30, 2020
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
19d83e3
Generify, needs some testing and review
addisoncrump Nov 28, 2020
da9c2ed
Remove unnecessary change
addisoncrump Nov 28, 2020
7c30c50
Whoops, ? capture that type
addisoncrump Nov 28, 2020
c1096ad
Misread the docs, whoops
addisoncrump Nov 28, 2020
9173296
More permissible arithmetic operations
addisoncrump Nov 28, 2020
9ad727d
Implement believed Optimize generics
addisoncrump Nov 28, 2020
b6b42c9
Missed a few generics
addisoncrump Nov 28, 2020
8904764
More permissible expr for arrays in parameters
addisoncrump Nov 28, 2020
92f5f2d
More permissible expr for bitvecs in parameters
addisoncrump Nov 28, 2020
9d4aa7c
More permissible expr for bools in parameters
addisoncrump Nov 28, 2020
dd80510
More permissible expr for fps in parameters
addisoncrump Nov 28, 2020
e154a47
More permissible expr for fprms in parameters
addisoncrump Nov 28, 2020
37da41d
More permissible expr for ints in parameters
addisoncrump Nov 28, 2020
f7f43a8
More permissible expr for reals in parameters
addisoncrump Nov 28, 2020
e3d7a9b
Undo breaking name conflict due to type erasure; see notes
addisoncrump Nov 28, 2020
372de12
Whoops, fix typing of ReExpr
addisoncrump Nov 28, 2020
7894e69
Sort corrections for Re, Seq
addisoncrump Nov 28, 2020
5f58655
More permissible expr for regular expressions in parameters
addisoncrump Nov 28, 2020
67e4ef8
Fix name conflict between sequences and regular expressions; see notes
addisoncrump Nov 28, 2020
714e8f7
Minor typo, big implications!
addisoncrump Nov 28, 2020
e47e996
Make Constructor consistent, associate captured types with other unkn…
addisoncrump Nov 29, 2020
d975295
More expressive; outputs of multiple datatype definitions are only kn…
addisoncrump Nov 29, 2020
cf0289e
Be less dumb and just type it a little differently
addisoncrump Nov 29, 2020
c5a1737
Update examples, make sure to type Expr and FuncDecl sort returns
addisoncrump Nov 29, 2020
ca8a595
General fixups
addisoncrump Nov 29, 2020
4e14a1c
Downgrade java version, make it only for the generic support, remove …
addisoncrump Nov 30, 2020
82a0d4f
Turns out Java 8 hadn't figured out how to do stream generics yet. Di…
addisoncrump Nov 30, 2020
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
39 changes: 20 additions & 19 deletions src/api/java/Optimize.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Nikolaj Bjorner (nbjorner) 2015-07-16
/**
* Object for managing optimization context
**/
@SuppressWarnings("unchecked")
public class Optimize extends Z3Object {

/**
Expand Down Expand Up @@ -75,7 +76,7 @@ public void Add(BoolExpr ... constraints)
/**
* Handle to objectives returned by objective functions.
**/
public static class Handle {
public static class Handle<R extends Sort> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be more narrowly defined to R extends ArithSort?


private final Optimize opt;
private final int handle;
Expand All @@ -89,15 +90,15 @@ public static class Handle {
/**
* Retrieve a lower bound for the objective handle.
**/
public Expr getLower()
public Expr<R> getLower()
{
return opt.GetLower(handle);
}

/**
* Retrieve an upper bound for the objective handle.
**/
public Expr getUpper()
public Expr<R> getUpper()
{
return opt.GetUpper(handle);
}
Expand All @@ -110,7 +111,7 @@ public Expr getUpper()
* 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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be typed to R instead of ??

{
return opt.GetUpperAsVector(handle);
}
Expand All @@ -120,15 +121,15 @@ public Expr[] getUpperAsVector()
*
* <p>See {@link #getUpperAsVector()} for triple semantics.
*/
public Expr[] getLowerAsVector()
public Expr<?>[] getLowerAsVector()
{
return opt.GetLowerAsVector(handle);
}

/**
* Retrieve the value of an objective.
**/
public Expr getValue()
public Expr<R> getValue()
{
return getLower();
}
Expand Down Expand Up @@ -243,42 +244,42 @@ public BoolExpr[] getUnsatCore()
* 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));
}

/**
* @return Triple representing the upper bound for the objective handle.
*
* <p>See {@link Handle#getUpperAsVector}.
*/
private Expr[] GetUpperAsVector(int index) {
private Expr<?>[] GetUpperAsVector(int index) {
return unpackObjectiveValueVector(
Native.optimizeGetUpperAsVector(
getContext().nCtx(), getNativeObject(), index
Expand All @@ -291,20 +292,20 @@ private Expr[] GetUpperAsVector(int index) {
*
* <p>See {@link Handle#getLowerAsVector}.
*/
private Expr[] GetLowerAsVector(int index) {
private Expr<?>[] GetLowerAsVector(int index) {
return unpackObjectiveValueVector(
Native.optimizeGetLowerAsVector(
getContext().nCtx(), getNativeObject(), index
)
);
}

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)
};

}
Expand Down Expand Up @@ -355,7 +356,7 @@ public BoolExpr[] getAssertions()
/**
* The set of asserted formulas.
*/
public Expr[] getObjectives()
public Expr<?>[] getObjectives()
{
ASTVector objectives = new ASTVector(getContext(), Native.optimizeGetObjectives(getContext().nCtx(), getNativeObject()));
return objectives.ToExprArray();
Expand Down