Your hypothesis is that (typeintersect(A, B) <: C) || (typeintersect(B, A) <: C)
will never be more accurate than typeintersect(A, B) <: C
, right? Here are some examples where the former does turn out to be more accurate:
julia> function test_property(a, b, c)
ab = typeintersect(a, b)::Type
ba = typeintersect(b, a)::Type
x = (ab <: c)::Bool
y = (ba <: c)::Bool
accuracy_improvement = (x != y)::Bool
accuracy_improvement
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{3, Type}[
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X, Vararg{X}} where X<:Real, Tuple{X, X, Vararg{X}} where X),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X, Vararg{X}} where X<:Real, Tuple{X, X, Vararg{X}} where X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X, Tuple{Vararg{X}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X, Tuple{X, Vararg{X}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X, Tuple{X, X, Vararg{Any}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X, Tuple{X, X, Vararg{Real}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X, Tuple{X, X, Vararg{X}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X, Tuple{X, X} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{Vararg{X}} where Int<:X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{Vararg{X}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, Vararg{X}} where Int<:X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, Vararg{X}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{Any}} where Int<:X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{Any}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{Real}} where Int<:X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{Real}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{X}} where Int<:X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{X}} where X>:Int),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X} where Int<:X<:Real),
(Tuple{X, X, Vararg{Any}} where X>:Int, Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int),
(Tuple{X, X, Vararg{Real}} where Int<:X<:Real, Tuple{Any, Vararg{X}} where X, Tuple{Any, Vararg{X}} where X),
(Tuple{X, X} where X<:Real, Tuple{X, X, Vararg{X}} where X>:Int, Tuple{X, X} where X>:Int),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{Vararg{X}} where Int<:X<:Real),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{Vararg{X}} where X>:Int),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, Vararg{X}} where Int<:X<:Real),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, Vararg{X}} where X>:Int),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X, Vararg{Any}} where Int<:X<:Real),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X, Vararg{Any}} where X>:Int),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X, Vararg{Real}} where Int<:X<:Real),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X, Vararg{Real}} where X>:Int),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X, Vararg{X}} where Int<:X<:Real),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X, Vararg{X}} where X>:Int),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X} where Int<:X<:Real),
(Tuple{X, X} where X<:Real, Tuple{X, X} where X>:Int, Tuple{X, X} where X>:Int),
];
julia> println(test_property.(examples))
Bool[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
More examples:
improved_accuracy_examples.jl (2.5 MB)
Breaking down the last example:
julia> const A = Tuple{X, X} where X<:Real
Tuple{X, X} where X<:Real
julia> const B = Tuple{X, X} where X>:Int
Tuple{X, X} where X>:Int64
julia> const AB = typeintersect(A, B)
Tuple{X, X} where X<:Real
julia> const BA = typeintersect(B, A)
Tuple{X, X} where Int64<:X<:Real
julia> AB <: B # suboptimal
false
julia> BA <: B # optimal
true