r/ProgrammingLanguages • u/Dobias • Aug 27 '24
Idea: "ubiquefix" function-call syntax (prefix, infix, and postfix notation combined); Is it any good?
Recently, while thinking about programming languages, I had an idea for a (maybe) novel function-call syntax, which generalizes prefix, infix, and postfix notation.
I've written the following explanation: https://gist.github.com/Dobiasd/bb9d38a027cf3164e66996dd9e955481
Since I'm not experienced in language design, it would be great if you could give me some feedback. I'm also happy to learn why this idea is nonsense, in case it is. :)
40
Upvotes
25
u/hugogrant Aug 27 '24
I like the idea but kinda think Haskell's
$and.are good enough (and you can use back ticks at times too).This probably would not support variadic functions. Might be inconvenient for
-.Function overloading might also bring in ambiguities.
Actually:
Is the result 42 or 34?
Agda has mixfix which might also be interesting: https://agda.readthedocs.io/en/v2.5.2/language/mixfix-operators.html
Minor naming thought: I wonder if ubifix would work better?
23
u/hellotanjent Aug 27 '24
So if I see "f g", I have to go look up the definition of f and g to see if it means "f(g)" or "g(f)"?
And if someone changes the type signature of f or g, it could silently swap between those two options?
5
u/Aaron1924 Aug 27 '24
I think if you have two functions
f, g : A -> int, whereAis a generic type parameter, thenf gcould either be interpreted asf(g)org(f)4
u/Lvl999Noob Aug 27 '24
What about polymorphism? If you have an expression
f gwhere both of them take anobjector similar type as first arg, how would you resolve that? Or would it be a type error?3
u/cbarrick Aug 27 '24
Prolog has this.
All predicates can be called like this:
Then you can register the fixity, associativity, and precedence of symbols to interpret them as operators. Prolog supports infix, prefix, and postfix operators.
There are 7 possible combinations of fixity and associativity in Prolog:
The
findicates the position of the operator (fixity), and thexandyindicate the position of the arguments. Theyarguments are allowed to be terms of equal or less precedence, while thexarguments must be terms of strictly less precedence. (Note that Prolog's definition of "precedence" may be inverted from your own.) So together thexandyspecifiers describe the associativity of the argument.So, for example, the
+operator is described as:To have a precedence of 500 and type
yfx(left-associative infix).In Prolog, you have to explicitly describe which symbols may be used as operators, along with the fixity, associativity, and precedence. Otherwise, parsing would be ambiguous.
I can't quickly find a good doc on Prolog operators, so I'm just going to link Markus Triska's video: https://youtu.be/DepPPfDVSpw
5
u/erez27 Aug 27 '24
How would you interpret
a+b*c?2
u/metazip Aug 27 '24
Represent as a tree?
2
u/rhet0rica Aug 27 '24
The problems others have noted are valid objections, but I don't think they get at the heart of why this idea is fundamentally doomed.
The ability to recognize and conserve word order in utterances is a key function of human intelligence that other animals (including higher primates like gorillas and chimpanzees) lack. This faculty not only lets us bind arguments (noun predicates) to functions (verbs) in a specific order (which your ubiquefix spec permits through left-association), but also to detect illegal sentences. The best and most thoroughly trained animals will attempt something similar to the scheme you have here, except using intuition to hammer arguments into their most likely positions, obliterating the actual syntax. Anything beyond the most trivial sentence will just cause confusion and misunderstanding.
While a formal parse tree is an idealization of how humans understand language, it reflects important truths about the mental tests we perform on statements in order to comprehend them. In this process, failure—the possibility of recognizing a sentence as illegal, ambiguous, or incoherent—is just as important as success. When we detect a malformed sentence we can stop listening or reading, and reason abstractly about what the sentence was supposed to mean, or why it might be flawed, and, as a measure of last resort, we can ask the speaker to rephrase or restate it until communication is achieved.
This is what a normal parser is doing when it throws an error: it's saying, "Hey, you typed something I didn't understand. It's probably a mistake. Can you tell me what you meant to say?"
By removing some of the restrictions that define the correctness of a sentence, you're increasing the number of situations in which erroneous statements can be made. This criticism isn't unique to ubiquefix; we can also demonstrate it exists in most popular languages.
Consider a language where every function that takes more than one argument uses named parameters exclusively when they aren't interchangeable. For example, a
String -> String -> Listfunctionsplit(haystack, needle)must be called assplit(haystack=foo, needle=bar). Th equivalent of a split-on-separator string function appears in many languages, and has no canonical order for its arguments; PHP'sexplode()takes the separator as the first argument rather than the second. Detecting mistakes in these argument orders is beyond the scope of most parsers, and personally I find I often have to re-learn the syntax of functions likesplit(),substr(), andstrpos()whenever I switch between environments.This is the same problem that ubiquefix has—where a weak parser fails to catch programmer errors—but we tolerate it so long as the argument list is short and the function is well-known. These properties make underspecification acceptable, because we don't want to write out argument names for every single call (they're frequently-used functions in many applications, so this would be tedious) and we don't have to look up the parameter list every single time we use them (because they're so common, we can easily memorize them.)
The same cannot be said of ubiquefix, as it wants to extend underspecified syntax to not only rarely-used functions, but entire statements at once. The programmer must know the function signatures of every single function in a line of code to be able to read what it's doing. (So, too, does the parser, and therefore even error messages would be less helpful.)
At one point you mentioned adding traits to reduce the space of possible assignments. To the reader (think maintenance programmer) this would amount to silently adding named arguments—the interpreter would have an easier time nailing down a correct parse, but not the poor human who cannot actually see these magic bits of annotation.
Finally, to make one more appeal to natural languages: although there are many (mostly older) languages that have variable word order, they require marking arguments with different cases, which is the natlang equivalent of named parameters. Also, they pretty much all have a standard word order, and readers get angry if anyone deviates from it except when writing a poem—it still sounds like Yoda.
2
u/Mercerenies Aug 31 '24
I think this could work in a small-scale scripting language. Rebol is already part of the way there. In Rebol, everything is written in prefix notation, but a function's arity is always known static. The expression
f g hcould mean either of the following (using traditional JavaScript notation for clarity)f(g(h()))iffandgboth have arity 1 andhhas arity 0.f(g, h)iffhas arity 2 andgandhhave arity 0.And if
gis an infix operator (which, if I recall correctly, is determined purely by syntax, not scoping rules), then it could also bef()gh()assumingfandhhave arity zero andghas arity 2.The issue is that you need to know the static arity of every function up-front. That immediately precludes us from having functions which take optional or variadic arguments. And it's going to severely hamper type inference, since even a single term anywhere in an expression whose type is unknown will pretty much prevent parsing of that expression. Finally, as you've already noticed in your examples, this is going to get much harder if you want to add support for higher-order functions.
2
u/AntonLorenzen Aug 31 '24
What do you think of the WASM-style syntax:
The way to think about this is that it pushes
inputon a stack, then runs(split ',')with the last item on the stack and puts the result on the stack and so forth.A main benefit of this is that the stack does not have to be empty after a function call. Consider:
This code splits
inputby commas and appends the result to the end oftable. In contrast, it seems that in your proposed syntax this can not be expressed since a function takes as many arguments from the stack as it can:would split
tablebyinputand then append a single comma to the result. Similarlytable input append split ','would first appendtableandinputand then split both by commas.12
u/AlmusDives Aug 27 '24
I can see the appeal of this syntax, but I'm not sure I agree that it's particularly readable.
Your example: "input split ',' map stringToInteger map square sum" took me a moment to figure out what was an argument and what is a function. I can imagine things getting pretty confusing if it's not clear how many arguments a function takes.
9
u/mx00s Aug 27 '24
Reminds me a bit of mixfix operators, e.g. https://agda.readthedocs.io/en/latest/language/mixfix-operators.html.
5
u/WittyStick Aug 27 '24 edited Aug 27 '24
IMO it's bad idea. Syntax should be context-free. It's clearly not when the syntax depends on types of functions.
This seems like it would be completely incompatible with partial application and would cause too many issues with higher order functions.
Trivial example:
Could be interpreted many different ways:
Although in this example we're very lucky. They all produce the same result!
We may not be so fortunate if we have something less trivial, if for example
appendwas replaced by something which is a quasigroup rather than a semigroup. Consider for exampleintersect.If we give a, b, c, d some example values so we can see the difference in results:
4
u/va1en0k Aug 27 '24
check out Lambek calculus https://en.wikipedia.org/wiki/Categorial_grammar
3
u/brunogadaleta Aug 28 '24
https://ryelang.org/ has an interesting take on this subject: use dot or pipe prefix to call with arg on the left.
3
u/[deleted] Aug 28 '24
Noulith does something very similar, where any function or operator can be applied like
add(1, 2)or1 add 2or+(1, 2)or1 + 2(also supporting partial application), so your example can be written asinput split ',' map stringToInteger map square reduce +I like the idea, but it seems like having the AST depend on types would cause big problems both to the compiler writers (you have to somehow extract the type information before parsing the statements, which means you have to constrain the syntax in other ways) and to the users as well (good example of the dreaded "action at a distance"). Maybe some tweaks are necessary to get rid of that dependency.
5
u/Kaisha001 Aug 27 '24
It's a clever idea. I like the 'shift in' idea to handle infix.
There might be some issues with your ambiguity resolution in that I can imagine places where both cases would be valid.
I think the FP guys will love it, as they love the obscure, no punctuation, syntax. The imperative guys probably won't. I know I personally find it harder to read dense code if there is no punctuation.
2
u/SoInsightful Aug 27 '24
I have thought about ubiquefix syntax before, but this would be a nightmare to work with. Just trying to parse basic code like
create_server handle_requestand having to figure out which function applies to which sounds like a bad time.I think having the different -fixes can be a good idea, but they would need different syntaxes to prevent programmers from going crazy.
2
u/blue__sky Aug 27 '24
I think having a default associativity and a common style would make most expressions fairly easy to read. I came to a similar idea because I like the subject, verb, object (SVO) style. F# and OCaml both have a common idiom using SVO in function chaining, but require a chaining operator.
The OP's doc has an example. This "sentence" would be subject, verb, object and then chaining verb, object -> verb, object -> verb. Many complex expression would look like this:
In F# and OCaml it would look like this:
I guess you could use parens to make it more obvious, but I think after a short while the top statement would be easy for a human to read.
2
u/2skip Aug 28 '24
Wolfram can handle different *fix in an expression:
https://reference.wolfram.com/language/tutorial/Expressions.html#27714
f[x,y] | standard form for f[x,y]
fx@ | prefix form for f[x]
xf// | postfix form for f[x]
xfy~ ~ | infix form for f[x,y]
1
u/2skip Aug 28 '24
Wolfram can handle different *fix in an expression:
https://reference.wolfram.com/language/tutorial/Expressions.html#27714
|| || |f[x,y]|standard form for f[x,y]| |fx@ |prefix form for f[x]| |xf// |postfix form for f[x]| |xfy~ ~ |infix form for f[x,y]|
2
u/[deleted] Aug 28 '24
[removed] — view removed comment