<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div></div><div><br></div><div><br>Am 05.04.2016 um 22:32 schrieb Антон Жилин <<a href="mailto:antonyzhilin@gmail.com">antonyzhilin@gmail.com</a>>:<br><br></div><blockquote type="cite"><div><div dir="ltr"><a href="https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md#use-precedence-groups">Added</a> group version, "lessThan" problem can be solved nicely. `<`, `=`, `>` signs would be allowed there.<div><br><div>> <span style="font-size:12.8px">Should we allow "precedence(... equalTo ...)" for operators if we have precedence groups?</span></div></div><div><span style="font-size:12.8px">I think no.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">I have a question to your group syntax.</span></div><div><span style="font-size:12.8px">Since all operators in a precedence group must have equal associativity for parsing to work and look logically (right?), wouldn't it be better to declare associativity in groups?</span></div><div><span style="font-size:12.8px">If so, then body of operator declaration won't contain anything, and we can remove it:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">precedenceGroup Additive {</span></div><div><span style="font-size:12.8px"> associativity(left)</span></div><div><span style="font-size:12.8px"> +, -</span></div><div><span style="font-size:12.8px">}</span></div><div><span style="font-size:12.8px">infix operator +</span></div><div><span style="font-size:12.8px">infix operator -</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Does this body of precedenceGroup look OK from syntactic PoV?</span></div></div></div></blockquote><div><br></div><div>Associativity in precedence groups is fine however the operators should then be grouped possibly: "operators(+, -)"</div><br><blockquote type="cite"><div><div dir="ltr"><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Now, I have another idea.</span></div><div><span style="font-size:12.8px">As operator declarations themselves don't contain anything anymore, remove operator declarations at all. We don't need to pre-declare function names, for example.</span></div><div><span style="font-size:12.8px">Next, `precedenceGroup` could be as well replaced with `precedenceLevel`, or just `precedence`, and I would not worry about additional keywords.</span></div><div><span style="font-size:12.8px">So, our example would look like this:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">precedence Additive {</span></div><div><span style="font-size:12.8px"> associativity(left)</span></div><div><span style="font-size:12.8px"> +, -</span></div><div><span style="font-size:12.8px">}</span></div><div>precedence Multiplicative {</div><div> associativity(left)</div><div> *, /</div><div>}</div><div>precedence(Additive < Multiplicative)</div><div><br></div><div>As a future direction, we could add extensions to precedence levels.</div><div>We could go further and replace `precedence` with `operator`, abandoning the idea of priority for prefix and postfix operators (that I honestly don't like).</div><div><br></div></div></div></blockquote><div><br></div><div>Regarding pre- and postfix operators: there was a separate thread which discussed exactly this. The biggest problem was that if a prefix "-" has lower precedence than an infix operator like "^" this calculation is ambiguous from a human perspective:</div><div><br></div><div>-3 ^ 3</div><div><br></div><div>"-" has visually the higher precedence and the result would be 9. However the actual result is -9.</div><div><br></div><div>If we have precedence on pre- and postfix operators we would break existing code. A migratory could then enforce the old precedence levels with braces. But then we can resolve existing (visual) ambiguities and "mathematical incorrectness" by making the precedence of prefix "-" higher than the current comparative operators and not declare its precedence to higher precedence operators (precedence > 140)</div><div><br></div><div>Such that this expression is ambiguous to the compiler:</div><div><br></div><div>3 - -3 // also mathematically incorrect</div><div>// and should be rewritten to</div><div>3 - (-3)</div><div>// or just</div><div>3 + 3</div><br><blockquote type="cite"><div><div dir="ltr"><div>infix operator Additive {</div> members(+, -)<div> associativity(left)</div><div>}</div><div>infix operator Multiplicative {</div><div> members(*, /)</div><div> associativity(left)</div><div> precedence(> Additive)</div><div>}</div><div><br></div><div>Some other questions:</div><div>Do we need transitive precedence propagation?</div></div></div></blockquote><div><br></div><div>Yes because it would be quite a pain to declare every precedence between all precedence groups:</div><div>#needed precedence declarations ~ O(#of precedence groups ^ 2)</div><br><blockquote type="cite"><div><div dir="ltr"><div>Do we need resolution of conflicts, i.e. merging multiple definitions of same operators and groups, where possible?</div></div><div class="gmail_extra"><div class="gmail_quote"><br></div></div>
</div></blockquote><br></div><div>I think we shouldn't define operators in a precedence group because if we want to have an operator in two different groups then we have two operator definitions which can result in a conflict.</div><div>I'm not sure if we need the same operator in different groups. Therefore I'd suggest to declare all standard library operators in this form in order to see if we need this.</div><div><br></div><div>So my current syntax suggestion is:</div><div><br></div><div>infix operator + { <span style="background-color: rgba(255, 255, 255, 0);">associativity(left) }</span></div><div>prefix operator -</div><div>infix operator && { <span style="background-color: rgba(255, 255, 255, 0);">associativity(left) }</span></div><div><br></div><div>infix precedenceGroup Additive {</div><div> associativity(left)</div><div> members(+)</div><div>}</div><div><br></div><div><div><span style="background-color: rgba(255, 255, 255, 0);">infix precedenceGroup Logical {</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> associativity(left)</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> members(&&)</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">}</span></div></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">prefix precedenceGroup Sign {</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> members(-)</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">precedence(Additive > Logical)</span></div><div>precedence(Sign > Logical)</div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">// warning: duplicate precedence declarations</span></div><div><span style="background-color: rgba(255, 255, 255, 0);">precedence(Logical < Additive)</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div>--------</div><div><br></div><div>I declare associativity in operator declarations and precedence group declarations since it lets the compiler check whether the "members" have the right associativity.</div><div><br></div><div>Best regards</div><div>- Maximilian </div></body></html>