Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ is_commutative(p::_APL) = is_commutative(typeof(p))
is_commutative(v::AbstractVector) = is_commutative(eltype(v))
is_commutative(::Type{V}) where {V<:AbstractVector} = is_commutative(eltype(V))
is_commutative(v::Tuple) = all(is_commutative, v)
is_commutative(::Type{T}) where {T<:Tuple} = all(is_commutative, fieldtypes(T))
# Recursive dispatch allows the compiler to constant-fold for concrete Tuple types,
# enabling type-stable branches on `is_commutative(V)` in downstream packages.
is_commutative(::Type{Tuple{}}) = true
function is_commutative(::Type{T}) where {T<:Tuple}
return is_commutative(Base.tuple_type_head(T)) &&
is_commutative(Base.tuple_type_tail(T))
end

"""
name(v::AbstractVariable)::AbstractString
Expand Down
4 changes: 2 additions & 2 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"

[compat]
DynamicPolynomials = "0.6"
DynamicPolynomials = "0.6.5"
StarAlgebras = "0.3"
TypedPolynomials = "0.4.2"
TypedPolynomials = "0.4.4"
4 changes: 4 additions & 0 deletions test/commutative/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import MultivariatePolynomials:
_is_comm_test(x^2)
_is_comm_test(2x^2)
_is_comm_test(sum(y))
# Test that `is_commutative` is constant-folded for variable types,
# so that branches on it are eliminated at compile time.
_comm_branch(::Type{V}) where {V} = MP.is_commutative(V) ? Int : String
@test @inferred(_comm_branch(typeof(variables(x * y[1])))) === Int
end
@testset "polyvar macro index set" begin
Mod.@polyvar x y z
Expand Down
Loading