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

missing ambiguity warning for Integer vs. Union{Int,Float64}? #16489

Closed
jiahao opened this issue May 20, 2016 · 5 comments
Closed

missing ambiguity warning for Integer vs. Union{Int,Float64}? #16489

jiahao opened this issue May 20, 2016 · 5 comments
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@jiahao
Copy link
Member

jiahao commented May 20, 2016

In a meeting with @stevengj, @ivirshup, and @janvitek, we were surprised that the following code did not yield an ambiguity warning:

foo(x::Integer) = 7
foo(x::Union{Int,Float64}) = 8
foo(3) # returns 8
@jiahao jiahao added the types and dispatch Types, subtyping and method dispatch label May 20, 2016
@jiahao
Copy link
Member Author

jiahao commented May 20, 2016

More specifically, we'd like to think of Integer as an implicit union of all its subtypes, so it should behave similarly to

foo(x::Union{Int,ASCIIString}) = 7
foo(x::Union{Int,Float64}) = 8
foo(3) # ambiguous

@JeffBezanson
Copy link
Member

Int is strictly more specific than Integer, and we allow adding Float64 without interfering with that. Giving priority to declared subtypes of abstract types, when possible, is an important priority for specificity rules.

@vtjnash vtjnash closed this as completed May 20, 2016
@jiahao
Copy link
Member Author

jiahao commented May 20, 2016

We couldn't find an update to the type ordering rules since the first Julia paper. If we followed the rules exactly as laid out there, the result is ambiguous because the intersection of Integer and Union{Int,Float64} is Int, which is strictly more specific than both.

In Section 2.10, p. 8, the only applicable rule for determining specificity is

Rule 3. The intersection of A and B is nonempty, more specific than B, and not equal to B, and B is not more specific than A.

The first three clauses are true when A = Integer and B = Union{Int,Float64} or vice versa. The last clause is undecidable because you have recursively go back to Rule 3. So strictly following this specification, we cannot conclude which is more specific.

So, is there an updated specification of the type ordering?

@jiahao
Copy link
Member Author

jiahao commented May 20, 2016

In Jeff's PhD thesis there is also a description of specificity here, but here none of rules are applicable to this case.

@JeffBezanson
Copy link
Member

Yes, there are extra rules regarding unions here:
https://github.com/JeffBezanson/phdthesis/blob/876be73a5aab9b034fac3eb9ea9d8f96713f786f/chap4.tex#L880
These rules are in fact applicable here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

3 participants