enso-org / enso

Hybrid visual and textual functional programming.
https://enso.org
Apache License 2.0
7.3k stars 317 forks source link

Investigate How Changing Space Operator Precedence For Maths #10366

Open jdunkerley opened 3 days ago

jdunkerley commented 3 days ago

Want to evaluate the complexity of making it so whitespace not affect the precedence of mathematical operators (arithmetic, bitwise and logical ops).

We would keep the space precedence for the other operators like . and ->.

a + b>4 . floor == ((a+b)>4).floor NOT (a+(b>4)).floor a-> b-> a+b

Goal is to understand how hard to change on the engine/parser side and how hard to update all the library code needed.

wdanilo commented 2 days ago

My idea would be to introduce another precedence level in parser. I don't think this involves any part of engine beside parser (please correct me if I'm wrong). Now, we hierarchical precedence discovery - first level is "spaced" and "non-spaced". Then in these groups we apply precedences of operators. We could add another group to the top level, so we will have "spaced", "non-spaced", "math" and then just resolve precedences in these groups. Just my idea, might not be the best one, but I shared it because maybe it will be useful to someone :)

kazcw commented 2 days ago
ExpressionInterpretation (current)Interpretation (Solution 1)Interpretation (Solution 2)
`a * b+c . floor` `a * ((b + c) . floor)` `((a * b) + c) . floor`[^1]
`a * b+c.floor` `a * (b + (c . floor))` `((a * b) + (c . floor))`[^1]
`x+y * z` `(x + y) * z` `x + (y * z)`[^1]
`f x+y * z` `(f (x + y)) * z` `f (x + (y * z))`[^1] *unchanged*
`f x + y` `(f x) + y` `f (x + y)`[^2] *unchanged*
`f +y` `f (+y)` *unchanged*
`f x +y` `((f x) (+y))` *unchanged*

[^1]: In this case a warning may be emitted that spacing is inconsistent with effective precedence. [^2]: The interpretation of this expression has changed, but no warning is emitted. This may take some getting used to for library developers.

kazcw commented 2 days ago

Solution 0: Math-consistent spacing

Maintain the uniform application of space-precedence rules, but warn if the result is inconsistent with mathematical precedence.

Solution 1: High precedence math

Place math operators in a precedence level between unspaced non-math and spaced non-math.

Current logic:

New logic:

Solution 2: Space-invariant math precedence

Redefine the effect of spacing in terms of precedence-modification.

Current logic (after refactoring):

New logic:

Discussion

Complexity for language users:

Implementation

Parsing changes

Warnings

Library changes

parse_all_enso_files.sh can be used to detect expressions whose interpretation have been changed. If there are few they could be updated by hand; if there are many, I could add refactoring suggestions to the warnings, and create a tool that can apply them (2 points).

Total (excluding compiler changes to print warnings)