Comments (13)
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.
Ah, that is very different if the third type C in the comparison is Union{}
from julia.
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.
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:
Lines 1089 to 1092 in 5d4d6ab
from julia.
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.
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.
(typeintersect(A, B) <: C) || (typeintersect(B, A) <: C)
This is also incorrect. What packages are trying to use this pattern?
from julia.
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.
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.
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.
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.
OK, I edited the previous message, leaving only the examples where the two type intersections aren't equal.
from julia.
What packages use that pattern?
Actually, when considering only packages with a significant dependent package count, I think only JSON.jl does that:
from julia.
Related Issues (20)
- Objects captured by finalizer thunks are finalized early. HOT 27
- Thread pool of main thread varies depending on how Julia is started HOT 3
- llvm-julia-16.0.6-2.tar.gz checksum fail HOT 1
- dot function wrong result HOT 4
- mapreduce with multiple arrays allocates HOT 7
- Make `f.` parse & lower to `Base.Broadcast.BroadcastFunction(f)` HOT 1
- [BUG] Julia Nightly (1.12.0-DEV.49) Issue with HTTP.jl HOT 2
- `copyto!` does not vectorize for views with `UnitRange` indices HOT 2
- regression in correctness of handling typeinf avoidance
- regression in thread-safety correctness of detecting compilation finished
- regression in thread-safety with data-race on mi->inInference field
- regression in gc safety of debuginfo
- `Int8(8) // Int8(100)im == -50im` due to overflow
- `typejoin` does not give the narrowest common type HOT 4
- `cumsum` gives bad error message when collection elements can't be added HOT 6
- Make Base exports yield to package exports HOT 4
- Idiosyncratic failure building Julia on macOS HOT 27
- Inlined dispatch uses incorrect (type-equivalent, but not egal) type HOT 2
- sinc not generic enough for autodiff? HOT 1
- macOS: could not load library "Cocoa.framework/Cocoa" HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from julia.