Named precedence levels
evincarofautumn opened this issue · comments
There aren’t many operators defined in the common vocabulary yet, so we still have the chance to figure out the ergonomics of custom operators. Instead of the current 0–9 precedence levels, it might be better to use named precedence levels, under the assumption that most operators could (and should) fall into a clear family, such as:
Number | Name | Examples |
---|---|---|
9 | ? | |
8 | tight exponential |
^ * |
7 | tight multiplicative |
* / % |
6 | tight additive |
+ - |
5 | ? | ~ ** |
4 | relational |
< > <= >= = <> |
3 | loose multiplicative |
& && |
2 | loose additive |
| || |
1 | ? | --> ---> |
0 | ? |
* not yet in the common vocab
** strict difference (xor)
Pros and cons:
-
With names, it’s easier to decide which precedence level to use for an operator
-
With names, we can easily add additional precedence levels later on between existing levels
-
With numbers, it’s easier to know which operator binds tighter
Why don't just use an operator as a precedence level? Something like infixl ÷ (/)
.
That could work, and it’s been done in a few languages, at least Perl6 and Fortress. I just figured it would come out more organised to use names for the groups, provided we can come up with sensible names.
I just figured it would come out more organised to use names for the groups, provided we can come up with sensible names.
To be honest, the concept of precedence levels by itself feels like a kludge. Even though it is a most simple and direct way to implement precedences.
Interesting alternative approach: https://youtu.be/EZD3Scuv02g?t=31m42s
Its may be worth to do operator precedence like in Agda: you can only declare fixity and the fact that the operator in question binds tighter that some other (and the relation is not transitive).
Using operators with no order known without putting one of subexpr in ()
is a suntax error.
Then you have to construct a lattice, of course.