Skip to content

Empty sums #44

Closed
Closed

Description

@odow

Despite this comment:
https://github.com/JuliaOpt/MutableArithmetics.jl/blob/9b6f9a0206fd3f423122749077b4dd9fed2532ba/src/rewrite.jl#L48

julia> @rewrite(1 .+ sum(1 for i in 1:0) * 1^2)
ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
 [1] _empty_reduce_error() at ./reduce.jl:216
 [2] mapreduce_empty_iter(::Function, ::Function, ::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##31#32"))}, ::Base.EltypeUnknown) at ./reduce.jl:261
 [3] mapfoldl_impl at ./reduce.jl:57 [inlined]
 [4] #mapfoldl#187 at ./reduce.jl:72 [inlined]
 [5] mapfoldl at ./reduce.jl:72 [inlined]
 [6] #mapreduce#191 at ./reduce.jl:205 [inlined]
 [7] mapreduce at ./reduce.jl:205 [inlined]
 [8] sum at ./reduce.jl:399 [inlined]
 [9] sum(::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##31#32"))}) at ./reduce.jl:416
 [10] top-level scope at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:224

There is also this nasty bug:

julia> @rewrite(sum(1 for i in 1:0) .- 0)
ERROR: MethodError: no method matching -(::MutableArithmetics.Zero, ::Int64)
Closest candidates are:
  -(::Complex{Bool}, ::Real) at complex.jl:298
  -(::Missing, ::Number) at missing.jl:97
  -(::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:107
  ...
Stacktrace:
 [1] sub_mul(::MutableArithmetics.Zero, ::Int64) at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/MutableArithmetics.jl:31
 [2] _broadcast_getindex_evalf at ./broadcast.jl:578 [inlined]
 [3] _broadcast_getindex at ./broadcast.jl:551 [inlined]
 [4] getindex at ./broadcast.jl:511 [inlined]
 [5] copy at ./broadcast.jl:763 [inlined]
 [6] materialize at ./broadcast.jl:753 [inlined]
 [7] broadcast(::typeof(MutableArithmetics.sub_mul), ::MutableArithmetics.Zero, ::Int64) at ./broadcast.jl:707
 [8] broadcast_fallback!(::MutableArithmetics.NotMutable, ::Function, ::MutableArithmetics.Zero, ::Int64) at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/broadcast.jl:85
 [9] broadcast!(::Function, ::MutableArithmetics.Zero, ::Int64) at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/broadcast.jl:77
 [10] top-level scope at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:224

julia> @rewrite(0 .- sum(1 for i in 1:0))
0

Distilled from: jump-dev/JuMP.jl#2195

Activity

  1. reopened this on Apr 3, 2020
  2. odow commented on Apr 3, 2020

    @odow
    MemberAuthor

    Not actually fixed by #45

  3. added
    bug
    on Jan 14, 2021
  4. odow commented on Jan 14, 2021

    @odow
    MemberAuthor

    I took a look into this. @rewrite(1 .+ sum(1 for i in 1:0) * 1^2) is still an issue. But it's not an easy fix, so I'm in favor of closing as "won't-fix".

    Julia 1.6 introduces an init keyword that can be used as the default when the collection is empty. We should encourage this instead.

    julia> sum(i^2 for i in 1:0; init = 0)
    0

    Base Julia is also inconsistent in when it throws an error:

    julia> sum(i for i in 1:0)
    0
    
    julia> sum(1 for i in 1:0)
    ERROR: ArgumentError: reducing over an empty collection is not allowed
    Stacktrace:
      [1] _empty_reduce_error()
        @ Base ./reduce.jl:299
      [2] mapreduce_empty(f::Function, op::Base.BottomRF{typeof(Base.add_sum)}, T::Type)
        @ Base ./reduce.jl:342
      [3] reduce_empty(op::Base.MappingRF{var"#1#2", Base.BottomRF{typeof(Base.add_sum)}}, #unused#::Type{Int64})
        @ Base ./reduce.jl:329
      [4] reduce_empty_iter
        @ ./reduce.jl:355 [inlined]
      [5] reduce_empty_iter
        @ ./reduce.jl:354 [inlined]
      [6] foldl_impl
        @ ./reduce.jl:49 [inlined]
      [7] mapfoldl_impl
        @ ./reduce.jl:44 [inlined]
      [8] #mapfoldl#211
        @ ./reduce.jl:160 [inlined]
      [9] mapfoldl
        @ ./reduce.jl:160 [inlined]
     [10] #mapreduce#215
        @ ./reduce.jl:287 [inlined]
     [11] mapreduce
        @ ./reduce.jl:287 [inlined]
     [12] #sum#218
        @ ./reduce.jl:501 [inlined]
     [13] sum
        @ ./reduce.jl:501 [inlined]
     [14] #sum#219
        @ ./reduce.jl:528 [inlined]
     [15] sum(a::Base.Generator{UnitRange{Int64}, var"#1#2"})
        @ Base ./reduce.jl:528
     [16] top-level scope
        @ REPL[1]:1
  5. removed
    bug
    on Jan 14, 2021
  6. added
    bug
    on May 2, 2021
  7. blegat commented on May 2, 2021

    @blegat
    Member

    What would be the fix ? Consistently throwing the error or consistenstly returning MA.Zero() ? It seems that MA.Zero() would always be a correct answer but it might cause type unstability.

  8. odow commented on May 2, 2021

    @odow
    MemberAuthor

    Returning MA.Zero seems like the way to go. Or we should decide that throwing the reducing not allowed error is okay, and document that in JuMP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions