r/ProgrammingLanguages • u/FlamingBudder • Apr 16 '26
Discussion Combining prefix and postfix function application concrete syntax
I am just thinking about how syntax would work if you took a symmetric approach to function application syntax where prefix and postfix application are both allowed at the same time for any function. Let me know your thoughts on this syntax!
I distinguish between 2 orthogonal axes, a fixity axis is (prefix < and postfix >), and an associativity axis (left 1 and right 2 associative). The direction of the inequality is the direction in which the argument is fed into the function so f < x and x > f for feeding x into f.
< means prefix and left associative
<< means prefix and right associative
> means postfix and left associative
>> means postfix and right associative
For the examples below, I define how these operators work by converting the operator into an abstract syntax AP(f, x) operator meaning function f is applied to input x. AP is strictly prefix with parenthesized arguments to be fully unambiguous. f, g, h ... range over functions. a, b, c, ... range over expressions of any type.
Curried application: f < a < b == b >> a >> f == AP(AP(f, a), b)
Composition/Pipelining: a > f > g == g << f << a == AP(g, AP(f, a))
I think it would be logical to bias left associativity with either fixity because naturally we read left to right, and so this ordering makes sense.
For curried application f a b c d e ... prefix notation makes a lot of sense because the successive arguments are fed into the function from left to right order. f : A -> B -> C -> D -> ... and a : A, b : B, ...
With function composition in a lot of functional languages and in category theory we take (Haskell) g $ f a to mean a : A => f : A -> B => g : B -> C. The standard ordering g f x is a bad ordering because chronologically you start with x, and then f goes first and then g, but with my syntax the pipelining/function composition would be nice and chronological a > f > g. Meanwhile alternative right associative ordering is still available but not as prioritized.
In general a : A, f : A -> B, g : B -> C, h : C -> D, i : D -> E, ....
I believe (?) that this combined prefix and postfix application should be unambiguous. Just make < and > have greater precedence than << and >>, so for example A < B >> C == (A < B) >> C. Combining < and > is fine because they both associate to the left, and same for << and >>.
View all comments
Show parent comment
1
u/FlamingBudder Apr 16 '26 edited Apr 16 '26
yes, exactly. Although since function composition without an arg is nice to have, you can allow the user to define an infix operator such as . as well as how it associates (in this case it doesn't matter because function composition is associative) and its precedence. The operator then will just desugar during parse time: f . g ==> compose < f < g
Also the compose implementation is the canonical way to implement composition without making it an ad hoc builtin so it’s not necessarily being “simulated” rather it is not builtin and can be defined with base constructs of the language which you can't define via something else (function abstraction and application)