[swift-evolution] [Proposal] Custom operators

Chris Lattner clattner at apple.com
Fri Apr 8 00:59:49 CDT 2016


> On Apr 7, 2016, at 1:39 PM, Антон Жилин via swift-evolution <swift-evolution at swift.org> wrote:
> 
> First of all, sorry for the delay. I still hope to finish the discussion and push the proposal to review for Swift 3.0.
> Link for newcomers:
> https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md <https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md>
> 
> Sadly, I've moved into the territory opposite to what I had in mind in the beginning: absense of conflict resolution.
> I wanted lightweight directives, but am moving to closed precedence groups.
> 
> It's just IMHO, and I think I just need input on this from more people. I still have not heard anything from Core team.

Hi Антон,

I’m sorry for the delay, I have been out of town recently.  I haven’t read the upstream thread so I hope this isn’t too duplicative.  Here is my 2c:

- I completely agree that numeric precedences are lame, it was always the “plan” that they’d be removed someday, but that obviously still hasn’t happened :-)
- I definitely agree that a partial ordering between precedences is all that we need/want, and that unspecified relations should be an error.

That said, I feel like #operator is a major syntactic regression, both in consistency and predictability.  We use # for two things: directives (like #if) and for expressions (#file).  The #operator is a declaration of an operator, not an expression or a directive.  For declarations, we consistently use a keyword, which allows contextual modifiers before them, along with a body (which is sometimes optional for certain kinds of decls).  I feel like you’re trying to syntactically reduce the weight of something that doesn’t occur very often, which is no real win in expressiveness, and harms consistency.

Likewise #precedence is a relationship between two operators.  I’d suggest putting them into the body of the operator declaration.

OTOH, the stuff inside the current operator declaration is a random series of tokens with no apparent structure.  I think it would be reasonable to end up with something like:

infix operator <> {
  associativity: left
  precedenceLessThan: *
  precedenceEqualTo: -
 }

Or whatever.  The rationale here is that “infix” is primal on the operator decl (and thus is outside the braces) but the rest of the stuff can be omitted, so it goes inside.

Just in terms of the writing of the proposal, in the "Change precedence mechanism” keep in mind that swift code generally doesn’t care about the order of declarations (it doesn’t parse top down in the file like C does) so the example is a bit misleading.

Question for you: have you considered introducing named precedence groups, and having the relationships be between those groups?  For example, I could see something like:

	operator group additive {}
	operator group multiplicative { greaterThan: additive }
	operator group exponential { greaterThan: additive }

Then:

infix operator + {
  associativity: left
  precedence: additive
 }
infix operator - {
  associativity: left
  precedence: additive
 }

etc.

-Chris


> Still, new operators must be able to be added to existing precedence groups.
> Extensions cannot be used, because namespaces of types and precedencegroups will not intersect.
> So I have to return to declaration of operators, if noone finds a better way and if noone objects:
> 
> precedencegroup Additive {
>     members(+, -)
>     associativity(left)
>     precedence(> Comparative)
> }
> infix operator +
> infix operator -
> infix operator &+ { precedencegroup(Additive) }
> 
> All operators must have precedence groups.
> I thought of allowing operators to be single-operator precedence groups, but it wouldn't give any real benefits.
> I also thought of allowing operators without precedence, but almost all operators will want at least `precedence(>Assignment)`.
> 
> Now, what questions did arise from standard library operator declarations?
> 
> 1. All precedence groups have a "parent".
> It means, all operators will want to have precedence higher than Comparative or Ternary, or, at least, Assignment.
> 
> 2. Moreover, I could not find any case where I had to write anything other than precedence(>, ...)
> Of cause, I cheated, because I can control all these declarations.
> Mere people will have to use `<` to say that Additive, for example, should have less priority than their custom operator.
> 
> But... can you build a custom operator where `<` will actually be needed? I have even stronger doubts on `=`.
> Maybe we can even contract this feature to `parent(Comparative)` or something without losing any expressivity?
> 
> 3. Can we allow operators to have less priority than `=`?
> If yes, can you give an example of such operator?
> 
> 4. Operators `is`, `as`, `as?`, `as!`, `?:`, `=` are not proper Swift operators.
> But we can still support these tokens for consistency.
> Their only appearence would be in the standard library.
> Alternatively, we can hide their precedence groups and make them a special case.
> It's more a question of implementation complexity.
> 
> 5. I removed associativity from Ternary, removed BitwiseXor from bitwise hierarchy.
> And made numerous other changes that probably need to be reviewed.
> 
> 2016-04-06 9:17 GMT+03:00 Maximilian Hünenberger <m.huenenberger at me.com <mailto:m.huenenberger at me.com>>:
> 
> 
> Am 05.04.2016 um 22:32 schrieb Антон Жилин <antonyzhilin at gmail.com <mailto:antonyzhilin at gmail.com>>:
> 
>> Added <https://github.com/Anton3/swift-evolution/blob/operator-precedence/proposals/NNNN-operator-precedence.md#use-precedence-groups> group version, "lessThan" problem can be solved nicely. `<`, `=`, `>` signs would be allowed there.
>> 
>> > Should we allow "precedence(... equalTo ...)" for operators if we have precedence groups?
>> I think no.
>> 
>> I have a question to your group syntax.
>> 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?
>> If so, then body of operator declaration won't contain anything, and we can remove it:
>> 
>> precedenceGroup Additive {
>>     associativity(left)
>>     +, -
>> }
>> infix operator +
>> infix operator -
>> 
>> Does this body of precedenceGroup look OK from syntactic PoV?
> 
> Associativity in precedence groups is fine however the operators should then be grouped possibly: "operators(+, -)"
> 
>> 
>> Now, I have another idea.
>> 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.
>> Next, `precedenceGroup` could be as well replaced with `precedenceLevel`, or just `precedence`, and I would not worry about additional keywords.
>> So, our example would look like this:
>> 
>> precedence Additive {
>>     associativity(left)
>>     +, -
>> }
>> precedence Multiplicative {
>>     associativity(left)
>>     *, /
>> }
>> precedence(Additive < Multiplicative)
>> 
>> As a future direction, we could add extensions to precedence levels.
>> 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).
>> 
> 
> 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:
> 
> -3 ^ 3
> 
> "-" has visually the higher precedence and the result would be 9. However the actual result is -9.
> 
> 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)
> 
> Such that this expression is ambiguous to the compiler:
> 
> 3 - -3 // also mathematically incorrect
> // and should be rewritten to
> 3 - (-3)
> // or just
> 3 + 3
> 
>> infix operator Additive {
>>     members(+, -)
>>     associativity(left)
>> }
>> infix operator Multiplicative {
>>     members(*, /)
>>     associativity(left)
>>     precedence(> Additive)
>> }
>> 
>> Some other questions:
>> Do we need transitive precedence propagation?
> 
> Yes because it would be quite a pain to declare every precedence between all precedence groups:
> #needed precedence declarations ~ O(#of precedence groups ^ 2)
> 
>> Do we need resolution of conflicts, i.e. merging multiple definitions of same operators and groups, where possible?
>> 
> 
> 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.
> 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.
> 
> So my current syntax suggestion is:
> 
> infix operator + { associativity(left) }
> prefix operator -
> infix operator && { associativity(left) }
> 
> infix precedenceGroup Additive {
>         associativity(left)
>         members(+)
> }
> 
> infix precedenceGroup Logical {
>         associativity(left)
>         members(&&)
> }
> 
> prefix precedenceGroup Sign {
>         members(-)
> }
> 
> precedence(Additive > Logical)
> precedence(Sign > Logical)
> 
> // warning: duplicate precedence declarations
> precedence(Logical < Additive)
> 
> --------
> 
> I declare associativity in operator declarations and precedence group declarations since it lets the compiler check whether the "members" have the right associativity.
> 
> Best regards
> - Maximilian 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160407/654456fe/attachment.html>


More information about the swift-evolution mailing list