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

Improve inferrability of JuMP functions #1846

Merged
merged 3 commits into from
Feb 12, 2019
Merged

Improve inferrability of JuMP functions #1846

merged 3 commits into from
Feb 12, 2019

Conversation

blegat
Copy link
Member

@blegat blegat commented Feb 10, 2019

As the MOI backend does not have a concrete type, Julia is not able to infer the result of the MOI.get calls. This PR adds type asserts to help inferrability and adds tests to unsure that the functions are now inferrable.

Closes #1824

@mlubin
Copy link
Member

mlubin commented Feb 10, 2019

There's a subtle difference in regular type assertions and return value type assertions:

julia> f() = 1
f (generic function with 1 method)

julia> function g()
           return f()::Float64
       end
g (generic function with 1 method)

julia> function h()::Float64
           return f()
       end
h (generic function with 1 method)

julia> g()
ERROR: TypeError: in g, in typeassert, expected Float64, got Int64
Stacktrace:
 [1] g() at ./REPL[2]:2
 [2] top-level scope at none:0

julia> h()
1.0

Which should we prefer?

Also we should distinguish the cases where MOI defines or should define an expected return type like SolverName and NumberOfVariables (jump-dev/MathOptInterface.jl#31) and the cases where we're asserting beyond what MOI implies because JuMP assumes Float64.

@odow
Copy link
Member

odow commented Feb 10, 2019

Also we should distinguish the cases where MOI defines or should define an expected return type like SolverName and NumberOfVariables (JuliaOpt/MathOptInterface.jl#31) and the cases where we're asserting beyond what MOI implies because JuMP assumes Float64.

Use a type assertion when MOI defines what type is returned.

function objective_sense(model::Model)
    return MOI.get(model, MOI.ObjectiveSense())::MOI.OptimizationSense
end

Use a return type assertion for JuMP-specific values.

function objective_value(model::Model)::Float64
    return MOI.get(model, MOI.ObjectiveValue())
end

@blegat
Copy link
Member Author

blegat commented Feb 10, 2019

I agree with @odow suggestion. Some solvers might return a Float32 instead of a Float64 and it's better to do the conversion in JuMP rather than forcing the solver wrappers to do these corrections.

@blegat
Copy link
Member Author

blegat commented Feb 11, 2019

I have move type asserts to return value type assertion when appropriate

Copy link
Member

@mlubin mlubin left a comment

Choose a reason for hiding this comment

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

Larger architectural question: which functions are we guaranteeing are type stable and which are we not, and how do we document this?

src/JuMP.jl Outdated
@@ -384,7 +384,7 @@ end

Returns number of variables in `model`.
"""
num_variables(model::Model) = MOI.get(model, MOI.NumberOfVariables())
num_variables(model::Model) = MOI.get(model, MOI.NumberOfVariables())::Int64
Copy link
Member

Choose a reason for hiding this comment

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

What if a solver returns an Int32?

Copy link
Member Author

Choose a reason for hiding this comment

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

We should use a conversion here, that is also consistent with num_constraint in #1850

@blegat
Copy link
Member Author

blegat commented Feb 11, 2019

Larger architectural question: which functions are we guaranteeing are type stable and which are we not, and how do we document this?

I think that by default all jump function should be type stable (or with a Union with Nothing) and we should explictly document it when it's not the case

@blegat blegat merged commit 6c1720c into master Feb 12, 2019
@odow odow deleted the bl/inferrability branch February 12, 2019 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants