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

empty function composition ∘() should return Base.identity? #52831

Closed
stevengj opened this issue Jan 9, 2024 · 2 comments
Closed

empty function composition ∘() should return Base.identity? #52831

stevengj opened this issue Jan 9, 2024 · 2 comments

Comments

@stevengj
Copy link
Member

stevengj commented Jan 9, 2024

Currently, ∘() gives a MethodError:

julia> (sin, cos, tan)
sin  cos  tan

julia> (sin, cos)
sin  cos

julia> (sin,)
sin (generic function with 14 methods)

julia> ()
ERROR: MethodError: no method matching ()

whereas it should arguably return Base.identity.

Similarly, reduce_empty(∘) should return Base.identity so that empty reductions work:

julia> foldl(, (sin, cos, tan))
sin  cos  tan

julia> foldl(, (sin, cos))
sin  cos

julia> foldl(, (sin,))
sin (generic function with 14 methods)

julia> foldl(, ())
ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer
Stacktrace:
 [1] reduce_empty(op::Base.BottomRF{typeof(∘)}, ::Type{Union{}})
   @ Base ./reduce.jl:360

Update: The 1-arg method was added in #34251, and I see that at this point the possibility for a zero-argument version ∘() = identity was explicitly proposed, and rejected on the grounds that might be applicable to more than just Functions, in which case the identity element becomes ambiguous:

That's based on the desire to have ∘ for other "morphisms" than functions, which has been requested before, e.g. in the discussion where compose was requested as an ASCII name for ∘.

@stevengj stevengj added bug Indicates an unexpected problem or unintended behavior and removed bug Indicates an unexpected problem or unintended behavior labels Jan 9, 2024
@stevengj
Copy link
Member Author

stevengj commented Jan 9, 2024

Closing as a duplicate of the discussion in #34251.

There is also an explicit comment about this in the source code:

julia/test/operators.jl

Lines 171 to 174 in c0c676b

# Like +() and *() we leave ∘() undefined.
# While `∘() = identity` is a reasonable definition for functions, this
# would cause headaches for composition of user defined morphisms.
# See also #34251

@stevengj stevengj closed this as completed Jan 9, 2024
@stevengj
Copy link
Member Author

stevengj commented Jan 9, 2024

Also duplicate of #52436.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant