Skip to content

Commit 06db5df

Browse files
authored
Fix promotion with nonconcrete types (#166)
* Fix promotion with nonconcrete types * Checkout deps * Fix tests for Julia v1.0 * Revert "Checkout deps" This reverts commit 4f569bb.
1 parent 97cbc9e commit 06db5df

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed

src/promote.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,58 @@ function promote_rule_constant(::Type{S}, PT::Type{<:APL{T}}) where {S, T}
2828
end
2929
Base.promote_rule(::Type{PT}, ::Type{T}) where {T, PT<:APL} = promote_rule_constant(T, PT)
3030
Base.promote_rule(::Type{T}, ::Type{PT}) where {T, PT<:APL} = promote_rule_constant(T, PT)
31+
# Resolve method ambiguity with Base:
32+
Base.promote_rule(::Type{Any}, ::Type{<:APL}) = Any
33+
34+
# We don't have any information on the MultivariatePolynomials implementation,
35+
# so we won't be able to convert the constant to `APL`.
36+
promote_rule_constant(::Type, PT::Type{AbstractMonomialLike}) = Any
37+
promote_rule_constant(::Type, PT::Type{AbstractTermLike{T}}) where {T} = Any
38+
promote_rule_constant(::Type, PT::Type{AbstractTermLike}) = Any
39+
promote_rule_constant(::Type, PT::Type{APL{T}}) where {T} = Any
40+
promote_rule_constant(::Type, PT::Type{APL}) = Any
41+
42+
# AbstractMonomialLike{T}
43+
Base.promote_rule(::Type{AbstractMonomialLike}, ::Type{<:AbstractMonomialLike}) = AbstractMonomialLike
44+
Base.promote_rule(::Type{<:AbstractMonomialLike}, ::Type{AbstractMonomialLike}) = AbstractMonomialLike
45+
Base.promote_rule(::Type{AbstractMonomialLike}, ::Type{<:AbstractTermLike{T}}) where {T} = _atl(Int, T)
46+
Base.promote_rule(::Type{<:AbstractTermLike{T}}, ::Type{AbstractMonomialLike}) where {T} = _atl(Int, T)
47+
Base.promote_rule(::Type{AbstractMonomialLike}, ::Type{AbstractTermLike{T}}) where {T} = _atl(Int, T)
48+
Base.promote_rule(::Type{AbstractTermLike{T}}, ::Type{AbstractMonomialLike}) where {T} = _atl(Int, T)
49+
Base.promote_rule(::Type{AbstractMonomialLike}, ::Type{<:APL{T}}) where {T} = _apl(Int, T)
50+
Base.promote_rule(::Type{<:APL{T}}, ::Type{AbstractMonomialLike}) where {T} = _apl(Int, T)
51+
Base.promote_rule(::Type{AbstractMonomialLike}, ::Type{APL{T}}) where {T} = _apl(Int, T)
52+
Base.promote_rule(::Type{APL{T}}, ::Type{AbstractMonomialLike}) where {T} = _apl(Int, T)
53+
54+
# AbstractTermLike{T}
55+
_atl(::Type{T}, ::Type{T}) where {T} = AbstractTermLike{T}
56+
_atl(::Type, ::Type) = AbstractTermLike
57+
__atl(::Type{T}, ::Type{<:AbstractTermLike{S}}) where {S,T} = _atl(T, S)
58+
__atl(::Type{T}, ::Type{<:APL{S}}) where {S,T} = _apl(T, S)
59+
Base.promote_rule(::Type{AbstractTermLike{T}}, P::Type{<:AbstractTermLike{S}}) where {S,T} = _atl(T, S)
60+
Base.promote_rule(P::Type{<:AbstractTermLike{S}}, ::Type{AbstractTermLike{T}}) where {S,T} = _atl(T, S)
61+
Base.promote_rule(::Type{AbstractTermLike{T}}, P::Type{<:APL{S}}) where {S,T} = _apl(T, S)
62+
Base.promote_rule(P::Type{<:APL{S}}, ::Type{AbstractTermLike{T}}) where {S,T} = _apl(T, S)
63+
Base.promote_rule(::Type{AbstractTermLike{T}}, P::Type{APL{S}}) where {S,T} = _apl(T, S)
64+
Base.promote_rule(P::Type{APL{S}}, ::Type{AbstractTermLike{T}}) where {S,T} = _apl(T, S)
65+
66+
# AbstractTermLike
67+
Base.promote_rule(::Type{AbstractTermLike}, ::Type{<:AbstractTermLike}) = AbstractTermLike
68+
Base.promote_rule(::Type{<:AbstractTermLike}, ::Type{AbstractTermLike}) = AbstractTermLike
69+
Base.promote_rule(::Type{AbstractTermLike}, ::Type{<:APL}) = APL
70+
Base.promote_rule(::Type{<:APL}, ::Type{AbstractTermLike}) = APL
71+
Base.promote_rule(::Type{AbstractTermLike}, ::Type{APL}) = APL
72+
Base.promote_rule(::Type{APL}, ::Type{AbstractTermLike}) = APL
73+
74+
# APL{T}
75+
_apl(::Type{T}, ::Type{T}) where {T} = APL{T}
76+
_apl(::Type, ::Type) = APL
77+
Base.promote_rule(::Type{APL{T}}, ::Type{<:APL{S}}) where {S,T} = _apl(S, T)
78+
Base.promote_rule(::Type{<:APL{S}}, ::Type{APL{T}}) where {S,T} = _apl(S, T)
79+
80+
# APL
81+
Base.promote_rule(::Type{APL}, ::Type{<:APL}) = APL
82+
Base.promote_rule(::Type{<:APL}, ::Type{APL}) = APL
3183

3284
# Rational
3385
promote_rule_constant(::Type{T}, ::Type{RationalPoly{NT, DT}}) where {T, NT, DT} = RationalPoly{promote_type(T, NT), promote_type(DT, termtype(DT))}

test/promote.jl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,73 @@
2222
@test Y[1] == x + 3y
2323
@test Y[2] == 2x + 4y
2424
@test dot(X, [1 2; 3 4] * X) == x^2 + 5x*y + 4y^2
25+
26+
function _t(a, b, T)
27+
if VERSION < v"1.6"
28+
@test typeof(@inferred vcat(a, b)) in [Vector{T}, Vector{Any}]
29+
@test typeof(@inferred vcat(b, a)) in [Vector{T}, Vector{Any}]
30+
else
31+
@test typeof(@inferred vcat(a, b)) == Vector{T}
32+
@test typeof(@inferred vcat(b, a)) == Vector{T}
33+
end
34+
end
35+
36+
function __test(a, PT, TT, MT)
37+
@test typeof(a) == Vector{MT(Int)}
38+
_t(a, 1, Any)
39+
_t(a, x, MT(Int))
40+
_t(a, x^2, MT(Int))
41+
_t(a, 1x, TT(Int))
42+
_t(a, 1.0x, TT())
43+
_t(a, x + y, PT(Int))
44+
_t(a, 1.0x + y, PT())
45+
end
46+
47+
function _test(a, af, TT)
48+
__test(a, apl, TT, TT)
49+
# `x isa APL{Int}` so here we don't have `Float64`:
50+
pt() = apl()
51+
pt(T) = apl()
52+
tt() = TT()
53+
tt(T) = TT()
54+
__test(af, pt, tt, tt)
55+
@test typeof(af) == Vector{TT()}
56+
_t(af, a, TT())
57+
end
58+
59+
apl() = MP.APL
60+
apl(T::Type) = MP.APL{T}
61+
p = [i == 1 ? x + y : x for i in 1:2]
62+
pf = [i == 1 ? 1.0x + y : x for i in 1:2]
63+
_test(p, pf, apl)
64+
65+
atl() = MP.AbstractTermLike
66+
atl(T::Type) = MP.AbstractTermLike{T}
67+
t = [i == 1 ? 2x : x for i in 1:2]
68+
tf = [i == 1 ? 2.0x : x for i in 1:2]
69+
_test(t, tf, atl)
70+
_t(p, t, apl(Int))
71+
_t(pf, t, apl())
72+
_t(p, tf, apl())
73+
_t(pf, tf, apl())
74+
75+
__pt = Base.promote_typejoin(typeof(x + 2), typeof(x + 2.0))
76+
_pt() = __pt
77+
_pt(::Type) = __pt
78+
__test([i == 1 ? x + 2 : x + 2.0 for i in 1:2], _pt, _pt, _pt)
79+
80+
__tt = Base.promote_typejoin(typeof(2x), typeof(2.0x))
81+
_tt() = __tt
82+
_tt(::Type) = __tt
83+
__test([i == 1 ? 2x : 2.0x for i in 1:2], _pt, _tt, _tt)
84+
85+
aml(args...) = MP.AbstractMonomialLike
86+
a = [i == 1 ? x^2 : x for i in 1:2]
87+
__test(a, apl, atl, aml)
88+
_t(a, t, atl(Int))
89+
_t(a, tf, atl())
90+
_t(a, p, apl(Int))
91+
_t(a, pf, apl())
2592
end
2693

2794
struct A end

test/tmp.jl

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)