kellysmith12.21
2020-12-3 05:09:09

I’m making a little #lang, and I thought it’d be nice to have an infix form. I’ve thought about two ways that I’ve seen precedence handled (numerical precedence, à la Haskell; precedence groups, à la Swift). I’m not satisfied with either approach for these reasons:

• à la Haskell: using a natural number for precedence conveys nothing about the relationship between operators and means that there is no upper limit to the number of precedences that the programmer has to consider when reading an expression. • à la Swift: the precedence groups provides a better clue to the relationships between operators, but it’s still a vague relation. There is an unbounded number of precedences to consider, and the programmer now must consider a lattice of precedences, instead of a list, when reading an expression. In practice, both approaches seem to work well enough, but I’m trying to see if there’s a simpler option that still feels flexible.


kellysmith12.21
2020-12-3 05:16:41

I have an idea that I think is reasonable: an “operator group” can be defined, which associates a set of function bindings with associativities and precedences. There are two important parts of this:

  1. An infix form can contain only infix operators from the same operator group. To use operators from another group, another infix form (meaning explicit parens) is necessary.
  2. Once defined, an operator group cannot be extended. Both of these points (I think) will help make it easier to read infix expressions and help to emphasize the semantic relationships between the operators in a group.

sorawee
2020-12-3 05:17:10

Have you seen Hackett? I think the approach is very simple and looks nice.


sorawee
2020-12-3 05:17:31

(define x {1 + 2})



kellysmith12.21
2020-12-3 05:19:43

The approach in Hacket is simple, which I like, but it only allows the same operator to be used multiple times in an infix expression.


kellysmith12.21
2020-12-3 05:20:15

(Sorry, I should’ve mentioned Hackett!)


samdphillips
2020-12-3 05:21:10

I know it rubs some people wrong, but I like Smalltalk’s system of having no precedence and everything evaluated left to right.


kellysmith12.21
2020-12-3 05:28:13

One goal for this lang is to reduce the number of parens, while still using an s-expr syntax. I’ve managed to do that with several syntactic forms, but I’ve yet to tackle infix functions because of the precedence issue.


kellysmith12.21
2020-12-3 05:31:11

Are there any immediate impressions of my idea of “operator groups”? It’s the best compromise I could think of.


soegaard2
2020-12-3 07:17:20

The Mathematica language has a surprising number of operators. Here is how they handle precedence: https://reference.wolfram.com/language/tutorial/OperatorInputForms.html


soegaard2
2020-12-3 07:18:50

I was inspired by the Mathematica syntax, when I wrote Infix, but I hardcoded the precedence into the grammar. https://docs.racket-lang.org/infix-manual/index.html?q=infix#%28part._top%29