rljacobson / FoxySheep

Compiler Technologies for Mathematica/Wolfram Language.
BSD 2-Clause "Simplified" License
36 stars 3 forks source link

Differentiating between ReplaceAll and Division (Times [ , Power]) #3

Open rocky opened 3 years ago

rocky commented 3 years ago

In https://github.com/mathics/Mathics/issues/989 it was noticed that 1/.2 Is comes out ReplaceAll[1,2] rather than Times[1,Power[.2,-1]].

The fix there was to add a lookahead pattern for a number to distinguish these.

ANTLR4 has such a notion too which I think is called a semantic predicate or {...}? which might help the parser here.

rljacobson commented 3 years ago

The rules for how . is parsed are arcane. Here are my private notes on the subject from last summer:

1//..9 is ReplaceRepeated[1, 0.9`] 1//.9 is ReplaceRepeated[1, 9] should be 0.9[1] 1/.9 is Times[1, Power[0.9`, -1]] 1...9 is 9 ((1)...) or Hold[Times[RepeatedNull[1], 9]] 7//.9 is ReplaceRepeated[7, 9] 7// .9 is 0.9`[7] 1/.1->2 is 10. -> 2

7// .9 is 0.9`[7]

b; 7`b is 7. b _.1 is Times[Optional[Blank[]], 1]

Rule: If . can be interpreted as part of an operator, it will be. The only exceptions are:

    b=.1
    a/:b=.1
    a/.1
    a.b (* The Dot operator. *)

This places the precedence of the decimal point between //. and /.—excepting for the Dot operator itself.

rljacobson commented 3 years ago

Pingback to #2 for reference, which references the edge case x_.. discussed here.