Giter VIP home page Giter VIP logo

Comments (13)

vtjnash avatar vtjnash commented on May 26, 2024 1

Those seem to be just examples of equal types. In particular, when the types are equal, the type system has no opinion on which one is "better", and returns the first one.

julia> ==(Tuple{Any}, Tuple{X} where X)
true

julia> ==(Tuple, Tuple{Vararg{X}} where X>:Real)
true

from julia.

vtjnash avatar vtjnash commented on May 26, 2024 1

Ah, that is very different if the third type C in the comparison is Union{}

from julia.

N5N3 avatar N5N3 commented on May 26, 2024

This is not a correctness bug.
As the doc says, typeintersect compute a type that contains the intersection of inputs.
We hope the result to be the smallest set but that's not true in many cases.
The algorithm is allowed to over-estimate when needed.

from julia.

nsajko avatar nsajko commented on May 26, 2024

Is it not possible to guarantee commutativity? It really seems like an essential property for an "intersection" operation.

Anyway, I stumbled upon this while trying to expand the "menagerie" of types used in the subtype test set, where commutativity of intersection is tested:

julia/test/subtype.jl

Lines 1089 to 1092 in 5d4d6ab

S = menagerie[j]
I = _type_intersect(T,S)
I2 = _type_intersect(S,T)
@test isequal_type(I, I2)

from julia.

N5N3 avatar N5N3 commented on May 26, 2024

Is it not possible to guarantee commutativity?

Commutativity doesn't quite make sense considering over-estimation.
At present the algorithm would widen the result to the 2nd input (usually the method signature) if it thinks the union complexity is too big.
But for simple input, we usually give the smallest result thus keeps commutativity.

from julia.

nsajko avatar nsajko commented on May 26, 2024

After taking a quick look at the JuliaHub symbol search, I don't think many are aware of the lack of commutativity of typeintersect.

The noncommutativity should be pointed out in the typeintersect doc string. In particular, people currently do this to check whether a type intersection subtypes some third type: typeintersect(A, B) <: C. But, given that typeintersect isn't commutative, that code should presumably be replaced with (typeintersect(A, B) <: C) || (typeintersect(B, A) <: C), and we could give that example in the doc string. What do you think? EDIT: opened a relevant Discourse thread: https://discourse.julialang.org/t/fr-provide-a-predicate-to-check-whether-a-type-intersection-subtypes-some-type/109303

from julia.

vtjnash avatar vtjnash commented on May 26, 2024

(typeintersect(A, B) <: C) || (typeintersect(B, A) <: C)

This is also incorrect. What packages are trying to use this pattern?

from julia.

vtjnash avatar vtjnash commented on May 26, 2024

We have a menagerie of other cases which may be similar and were noticed to be suboptimal (such that C<:A && C<:B does not hold), such as #36951

from julia.

nsajko avatar nsajko commented on May 26, 2024

What packages are trying to use this pattern?

No packages use that patter. I'm just saying it would be an improvement over the established typeintersect(A, B) <: C pattern.

from julia.

vtjnash avatar vtjnash commented on May 26, 2024

What packages use that pattern? They possibly should not use it, although there are certain circumstances where it is correct (e.g. methods and isambiguous use that pattern quite heavily)

from julia.

nsajko avatar nsajko commented on May 26, 2024

Here are some cases where there's room for making typeintersect more accurate by computing both directional intersections, instead of just one, and then checking for subtyping between them:

julia> function test_property(a, b)
           ab = typeintersect(a, b)::Type
           ba = typeintersect(b, a)::Type
           (ab != ba && (ab <: ba || ba <: ab))::Bool
       end
test_property (generic function with 1 method)

julia> test_property(t) = test_property(t...)
test_property (generic function with 2 methods)

julia> examples = NTuple{2, Type}[
         (Tuple{T, T} where T<:Integer, Tuple{X, X} where Int<:X<:Real),
         (Tuple{T, T} where T<:Integer, Tuple{X, X} where X>:Int),
         (Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int),
         (Tuple{Vararg{X}} where X, Tuple{Any, Vararg{X}} where X),
         (Tuple{Vararg{X}} where X, Tuple{X, X, Vararg{Any}} where X),
       ];

julia> println(test_property.(examples))
Bool[1, 1, 1, 1, 1]

More: examples.jl.txt

from julia.

nsajko avatar nsajko commented on May 26, 2024

OK, I edited the previous message, leaving only the examples where the two type intersections aren't equal.

from julia.

nsajko avatar nsajko commented on May 26, 2024

What packages use that pattern?

Actually, when considering only packages with a significant dependent package count, I think only JSON.jl does that:

https://github.com/JuliaIO/JSON.jl/blob/46579b75938bf5cd2079907499cb8afd8eef7599/src/Parser.jl#L412-L415

from julia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.