-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Parse 1 + 2 + 3 as +(1, +(2, 3)) #34999
Comments
It doesn't :)
What are the 16? How do you get that number? I agree this is more trouble than it's worth though, and I'd rather it parse 2 arguments at a time like most infix operators. What about |
At 16 |
This doesn't seem like something we could change before 2.0 without breaking quite a bit. |
One potential reason to prefer parsing as I guess one can just make |
A cute thing you can do with the current parsing is to change the associativity of julia> module RightAssociative
*(a) = Base.:*(a)
*(a, b) = Base.:*(a, b)
*(args...) = *(args[1:end-2]..., *(args[end-1], args[end]))
end
Main.RightAssociative
julia> using .RightAssociative: *
julia> Base.:*(x::Symbol, y) = Symbol("($x * $y)")
julia> :a * :b * :c
Symbol("(a * (b * c))") Something like static arrays probably can do more advanced things by looking at exact sizes and picking up a good grouping. BTW, I suppose 16 is just a heuristics to optimize the compile time: Lines 518 to 522 in 4d9a334
Is it possible to increase this threshold? |
Surely parsing does not imply implementation, nothing prevents a macro or optimizer from following the spine of a |
The existing parsing exposes potential optimization opportunities for operations over collections. For example, with the present parsing and for matrix |
Yes, directly optimizing chained operations was the motivation for this current parsing. For chains of matrix and vector multiplications, this can be a major optimizations. Of course, we don't currently do such optimizations for chains of matrix multiplication. |
https://discourse.julialang.org/t/speeding-up-solution-to-the-large-system-of-non-linear-complex-valued-odes/39075/2 is where this is showing up in practice. |
If this is change should it not be that |
A trick is to use |
I definitely support such a change. Situations where N-ary uses of The status quo can occasionally cause nontrivial ugliness like JuliaLang/LinearAlgebra.jl#1043. And even in that situation of chained matrix multiplication, it's not a better implementation than can be achieved with the proper choice of binary reduction order. Dynamic reassociation seems like it should be the domain of packages anyway (although
Maybe I overstep, but had I assumed the title's suggestion of right-to-left associativity was merely a typo and that focus was on defaulting to purely binary reductions. Left-to-right is much more common and also is the documented behavior, with some caveats. I also interpreted this discussion to apply to the other N-ary-parsing operators |
@ChrisRackauckas and I was just talking and we were wondering why we are parsing
1 + 2 + 3
asExpr(:+, 1, 2, 3)
.While I can see how that makes the parsers job easier it seems to make it harder for type-inference since instead of just
having on
+
method for integers we now have ~16, added to that is Chris infamous tendencies to create very long expressionswhich causes things to break down one you reach a limit. (and as demonstrated before no matter how large
N
at some point it will be to small).Is there a strong reason that I am missing for why we keep parsing it as a single expression? Macro-writers already need to program defensively
against both forms and it seems to me normalizing earlier would be better.
The text was updated successfully, but these errors were encountered: