Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any() and all() functions broken for empty collections #18073

Closed
dbeach24 opened this issue Aug 17, 2016 · 7 comments · Fixed by #18140
Closed

any() and all() functions broken for empty collections #18073

dbeach24 opened this issue Aug 17, 2016 · 7 comments · Fixed by #18140
Labels
good first issue Indicates a good issue for first-time contributors to Julia status:help wanted Indicates that a maintainer wants help on an issue or pull request
Milestone

Comments

@dbeach24
Copy link
Contributor

dbeach24 commented Aug 17, 2016

Julia raises an exception when invoking the any() and all() functions with empty collections.

In Julia 0.5.0-RC2, I get:

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http:https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.5.0-pre+5606 (2016-07-21 19:23 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 5fe9e53* (26 days old master)
|__/                   |  x86_64-apple-darwin13.4.0

julia> any([])
ERROR: ArgumentError: reducing over an empty collection is not allowed
 in _mapreduce(::Base.#identity, ::Base.#or_bool_only, ::Base.LinearFast, ::Array{Any,1}) at ./reduce.jl:148
 in any(::Array{Any,1}) at ./reduce.jl:365
 in eval(::Module, ::Any) at ./boot.jl:234
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:46

julia> all([])
ERROR: ArgumentError: reducing over an empty collection is not allowed
 in _mapreduce(::Base.#identity, ::Base.#and_bool_only, ::Base.LinearFast, ::Array{Any,1}) at ./reduce.jl:148
 in all(::Array{Any,1}) at ./reduce.jl:366
 in eval(::Module, ::Any) at ./boot.jl:234
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:46

The behavior I would expect:

@assert any([]) == false
@assert all([]) == true

Note that this behavior is generally useful in most logic programming and matches Python:

Python 2.7.10 (default, Sep 22 2015, 12:25:10) 
Type "copyright", "credits" or "license" for more information.

IPython 3.2.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: any([])
Out[1]: False

In [2]: all([])
Out[2]: True
@dbeach24
Copy link
Contributor Author

dbeach24 commented Aug 17, 2016

Update:

This is actually an inconsistency between any(iter) and all(iter) and their two argument counterparts that accept a predicate any(pred, iter) and all(pred, iter):

These two argument forms return the expected result in the case of an empty collection:

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http:https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.5.0-pre+5606 (2016-07-21 19:23 UTC)
 _/ |\__'_|_|_|\__'_|  |  Commit 5fe9e53* (26 days old master)
|__/                   |  x86_64-apple-darwin13.4.0

julia> any(x -> x > 1, [])
false

julia> all(x -> x > 1, [])
true

@yuyichao
Copy link
Contributor

FWIW

julia> all(Bool[])
true

julia> any(Bool[])
false

@dbeach24
Copy link
Contributor Author

@yuyichao
Interesting. Perhaps this is only a bug for empty collections where eltype(coll) == Any ?

@TotalVerb
Copy link
Contributor

Similar issue exists for sum and prod... I wonder if they should just return Bool on Any[] arrays. That case is not type stable anyway.

@eschnett
Copy link
Contributor

I guess any and all should call mapreduce with the additional argument specifying the value for empty collections.

@stevengj
Copy link
Member

stevengj commented Aug 17, 2016

@dbeach24, no, that would destroy type-stability of the sum function. We've had many discussions of sum, see e.g. #8092, #12461, #6994, #6069.

Let's keep this issue focused on the case at hand. any and all always return Bool, so there is no type-stability issue. It is clear that they should return false and true, respectively, for empty arrays of any type. It should be quite a simple PR to fix this.

@stevengj stevengj added status:help wanted Indicates that a maintainer wants help on an issue or pull request good first issue Indicates a good issue for first-time contributors to Julia labels Aug 17, 2016
@dbeach24
Copy link
Contributor Author

@stevengj Thanks for the background. (Deleting my earlier comment as it is not pertinent to this issue.)

dbeach24 added a commit to dbeach24/julia that referenced this issue Aug 18, 2016
@StefanKarpinski StefanKarpinski added this to the 0.5.x milestone Aug 18, 2016
dbeach24 added a commit to dbeach24/julia that referenced this issue Aug 18, 2016
Add default (true/false) argument to reduce argument so that
any([]) == false and all([]) == true.
(Previously these raised an exception due to reducing an empty list.)
StefanKarpinski pushed a commit that referenced this issue Aug 18, 2016
fix #18073: `any([])` and `all([])` (#18105)
tkelman pushed a commit that referenced this issue Aug 19, 2016
fix #18073: `any([])` and `all([])` (#18105)
(cherry picked from commit d259be5)
JeffBezanson added a commit that referenced this issue Aug 19, 2016
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
fix JuliaLang#18073: `any([])` and `all([])` (JuliaLang#18105)
(cherry picked from commit d259be5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia status:help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants