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
10 changes: 10 additions & 0 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

abstract type AbstractMutable end

# Zero arithmetic methods for AbstractMutable types.
# The main Zero arithmetic is defined in rewrite.jl with Number/AbstractArray;
# these methods extend it to AbstractMutable.
Base.:*(z::Zero, ::AbstractMutable) = z
Base.:*(::AbstractMutable, z::Zero) = z
Base.:+(::Zero, x::AbstractMutable) = copy_if_mutable(x)
Base.:+(x::AbstractMutable, ::Zero) = copy_if_mutable(x)
Base.:-(::Zero, x::AbstractMutable) = operate(-, x)
Base.:-(x::AbstractMutable, ::Zero) = copy_if_mutable(x)

function Base.sum(
a::AbstractArray{T};
dims = :,
Expand Down
25 changes: 18 additions & 7 deletions src/rewrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,31 @@ broadcast!!(::Union{typeof(add_mul),typeof(+)}, ::Zero, x) = copy_if_mutable(x)
broadcast!!(::typeof(add_mul), ::Zero, x, y) = x * y

# Needed in `@rewrite(1 .+ sum(1 for i in 1:0) * 1^2)`
Base.:*(z::Zero, ::Any) = z
Base.:*(::Any, z::Zero) = z
# These methods are narrowed to `Number` and `AbstractArray` to avoid invalidating
# the very broad `+(x, y)`, `*(x, y)` fallbacks in Base, which causes thousands of
# method invalidations across the ecosystem. Downstream packages that define custom
# types participating in MutableArithmetics rewrites should define their own
# `+(::MyType, ::Zero)` etc. methods.
Base.:*(z::Zero, ::Number) = z
Base.:*(::Number, z::Zero) = z
Base.:*(z::Zero, ::AbstractArray) = z
Base.:*(::AbstractArray, z::Zero) = z
Base.:*(z::Zero, ::Zero) = z
Base.:+(::Zero, x::Any) = x
Base.:+(x::Any, ::Zero) = x
Base.:+(::Zero, x::Number) = x
Base.:+(x::Number, ::Zero) = x
Base.:+(::Zero, x::AbstractArray) = x
Base.:+(x::AbstractArray, ::Zero) = x
Base.:+(z::Zero, ::Zero) = z
Base.:-(::Zero, x::Any) = -x
Base.:-(x::Any, ::Zero) = x
Base.:-(::Zero, x::Number) = -x
Base.:-(x::Number, ::Zero) = x
Base.:-(::Zero, x::AbstractArray) = -x
Base.:-(x::AbstractArray, ::Zero) = x
Base.:-(z::Zero, ::Zero) = z
Base.:-(z::Zero) = z
Base.:+(z::Zero) = z
Base.:*(z::Zero) = z

function Base.:/(z::Zero, x::Any)
function Base.:/(z::Zero, x::Number)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add integration tests for downstream packages to CI?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have something similar for MathOptInterface

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if iszero(x)
throw(DivideError())
else
Expand Down
Loading