kitten icon indicating copy to clipboard operation
kitten copied to clipboard

Named precedence levels

Open evincarofautumn opened this issue 7 years ago • 5 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

evincarofautumn avatar Jun 19 '17 07:06 evincarofautumn

Why don't just use an operator as a precedence level? Something like infixl ÷ (/).

suhr avatar Jun 19 '17 12:06 suhr

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.

evincarofautumn avatar Jun 19 '17 14:06 evincarofautumn

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.

suhr avatar Jun 19 '17 15:06 suhr

Interesting alternative approach: https://youtu.be/EZD3Scuv02g?t=31m42s

sullyj3 avatar Jun 27 '17 11:06 sullyj3

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.

Heimdell avatar Nov 14 '17 10:11 Heimdell