8 ms·
The ergonomic drawback of concatenative languages is that when you see `a b c d e`, you don’t know whether it means `e(d(c(b(a))))`, or `e(b(a), d(c))`, or `e(a
by layer8 4y ago
The ergonomic drawback of concatenative languages is that when you see `a b c d e`, you don’t know whether it means `e(d(c(b(a))))`, or `e(b(a), d(c))`, or `e(a, b, c, d)`, or `b(a), e(c, d)`, etc. The answer depends on the types of a, b, c, d, and e. The syntax `a b c d e` is a linearization of the call tree, whose node structure can be recovered from the types of the symbols (assuming the language is statically typed). As a human reader, it means you have to know the types to understand the structure of the expression. This imposes a cognitive load that other types of languages don’t.
It also removes redundancy that can be helpful to prevent programming errors. While the type checker verifies that the types match a structuring of the expression, that derived structure is not directly visible to the human programmer, and the programmer cannot easily see whether it actually matches the programmer’s understanding.
- js8 4y agoBut this is true for ML syntax too, isn't it? And ML-likes are widely considered to be readable. There are also features of concatenative languages, like quoting in Joy or Factor, which can help alleviate the problem.
- zetalyrae 4y agoI think currying by default is an antifeature and ML would be better off with f(x,y,z) call syntax. It interacts poorly with type inference: forget a parameter, or swap a parameter, and get an incomprehensible type error message about some function type that has little to do with the actual fault. This is prevented by parenthesizing expressions and using tuples as the sole argument.
- yakubin 4y ago> But this is true for ML syntax too, isn't it? And ML-likes are widely considered to be readable. I don't see how that's true for ML. Could you give an example showing the ambiguity? Edit: I think I can see how the presence of infix syntax may cause that. As in: when you see "a b c" it will read differently based on whether "b" is a function that's declared to be infix by default. Yeah, I think infix notation is a mistake. The more I think about syntax, the more appeal I see in S-expressions.
- js8 4y agoIn ML syntax, you don't know how many arguments a function will take, because of currying, and it's a (useful) feature, not a bug. Similarly, in concatenative languages, there is ambiguity how many parameters will a function take/leave on the stack.
- yakubin 4y ago> In ML syntax, you don't know how many arguments a function will take, because of currying, and it's a (useful) feature, not a bug. Currying doesn't change the picture here from the situation in, let's say, C. If it wasn't for "infixr", there would only be one way to parenthesise an expression. Just like in C, you could apply a function of N arguments to N+2 arguments, and the function call would get interpreted by the compiler as function application to N+2 arguments. It's just that there would be a type error, but gramatically, there is no ambiguity about which symbols are used as called functions, and which as arguments to the functions (and to which functions)... as long as you don't use "infixr". Same with a function of N+2 arguments getting N arguments. At some point you get a type error probably, because you're going to get a function, where you expected a non-function; but the type system doesn't influence the building of AST (in this place at least). Looking at the source you know how many arguments are getting applied to what, without even knowing the definitions of functions, as long as you don't play with infix functions. To make an example, again ignoring infix stuff, given "a b c d e", I know to read the whole expression as application of function a to arguments b, c, d, e. To be even more literal, SML doesn't have multiple-argument functions, all functions take one argument, and I can write it as "((((a b) c) d) e)". Unlike in concatenative languages, the function application doesn't suddenly stop in the middle of this expression, just because a takes fewer arguments than it was given, e.g. this expression can never be interpreted as "((a b) c) (d e)", even if it made more sense from the point of view of types of a, b, c, d, e. But when you do add infix syntax, then yeah, there is ambiguity, and now AST building is driven by the symbol table a bit.
- js8 4y ago> To make an example, again ignoring infix stuff, given "a b c d e", I know to read the whole expression as application of function a to arguments b, c, d, e. Yes, but what does it really tell you? It doesn't tell you that the final result is a primitive value, it might as well be a function again. (We need to be careful how we define an "return argument", whether it is only primitive types, or also function types, and based on the chosen definition, the ambiguity is present in both types of syntax, or in neither.) Similarly, if I have a program "a b c d e" in concatenative language, it is a function from stack to stack, and we don't know, how many arguments it either took from or left at the stack when it's done, without looking at the types. The point is, ambiguity regarding number of processed arguments is there in both cases. Except in concatenative languages, it also applies to the returned arguments (i.e. a "function" in a concatenative language can kinda take negative number of arguments). I think what mainly confuses you is that you really don't need an AST with concatenative languages. And it cannot be created, it's not a tree, more like "abstract syntax sequence".
- layer8 4y ago> But this is true for ML syntax too, isn't it? In ML, function application is left-associative, and therefore `a b c` always means `(a b) c`. It can’t mean `a (b c)`, even if that would fit the types. The expression structure is therefore independent from the types in ML. You can parse the expression tree without a symbol table. In concatenative languages, on the other hand, you need to look up the types, or if the language is dynamically typed, the expression structure only manifests at runtime.
- IHLayman 4y agoHaskell coders have to do this often enough that there is a token that does this grouping… f $ g x = f (g x), so the expression `a b c` is still a source for ambiguity.
- deleted 4y ago[deleted]
- avgcorrection 4y ago`$` is an operator. There is no ambiguity (you might not know the precedence table but that doesn’t make it ambigious).
- pklausler 4y ago`$` = id
- yakubin 4y agoIf b is declared to be infix by default, then it will be an application of function b to arguments a and c though. So you need to know the symbol table anyway it seems.
- js8 4y agoWhat I mean in ML syntax, every function is a curried function of only one argument. So you don't know how many arguments a function has without looking up its type. In concatenative languages, this is similar, every function (with exception of special forms) is a function from stack to stack, and to see how many arguments it has you also have to look at its type. I think concatenative syntax is in fact more elegant than ML syntax, since it also "curries" return values, and is evaluated in the same order as written.
- vitiral 4y agoLike the author said, that is optional. I'm writing a concatenative language with a C-similar syntax GitHub.com/civboot/fngi
- layer8 4y agoIt’s not optional if you want partial application/implicit composition. You can certainly have multiple syntaxes within the same language, but you can’t get the benefits of concatenative languages without also getting the drawbacks.
- vitiral 4y agoInteresting, I hadn't actually considered partial composition, since my language is very "at the metal" and partial composition isn't possible (or rather would have to have a macro and data-structure built around it). That's a good point for a "functional-style" language though.
- adastra22 4y agoPartial compilation is pretty core to concatenative languages. I don’t know how you can call yours one without it.
- vitiral 4y agoYou mean partial _application_. The author called FORTH a concatenative language, and FORTH definitely doesn't have partial application. In fact, FORTH doesn't even have a type system. Perhaps this is a miscategorization? fngi does have a type system which modifies a stack. However, it will be lacking many high-level type system features, being similar (but smaller and lighter-weight) than golang. Not totally sure if it qualifies or not.
- carapace 4y agoIn practice, this rarely comes up. You're almost never looking at ambiguous expressions. Instead the code is written as semantic factoring of the desired "bridge" (program) between the problem domain and solution space. (See "Starting Forth" and especially "Thinking Forth".)
- nmz 4y agoIts seen as bad practice to use anything but one argument per word. There's also quotations, retroforth[1] tends to use a prefix based system for indicating the type of the argument, c:put will print a character, s:put will print a string. https://github.com/crcx/retroforth/blob/master/doc/NamingConventions.md https://github.com/crcx/retroforth/blob/master/doc/NamingCon... https://github.com/crcx/retroforth/blob/master/doc/QuickRef.md https://github.com/crcx/retroforth/blob/master/doc/QuickRef....
- zozbot234 4y agoYes, but this is exactly parent's point. Once you allow for the possibility of procedures taking multiple arguments, one has to deal with call trees which cannot be represented unambiguously and compositionally in linearized form. And if this is extended to multiple return values, the "tree" is replaced by a dataflow or control-flow graph (both are possible, and not directly compatible). The change from an ordinary concatenative language is quite substantial.
- astrobe_ 4y ago"One" would be troublesome... Two is actually common, three is in the red zone, four you refactor. Except in specific cases where 4+ arguments doesn't cause stack juggling. Chuck Moore (inventor of Forth) said once that he don't use stack comments anymore. He claims that the arguments a definition takes should be obvious from the name or the definition (one-liners are common in Forth) or be documented somewhere else than in the code. Moore's claims are often bold and hardly believable. But when you put it to the test yourself, they sometimes do work for you too. This one does work for me. The answer to the "cognitive overload" argument is kind of trivial: "if it hurts, don't do it". The long answer is that concatenative languages have their idiomatic ways of doing things like any other language. Don't be stubborn; practice and embrace it.
- MaxBarraclough 4y ago> Its seen as bad practice to use anything but one argument per word. I've never heard this before. Many of the words in the ANS Forth standard [0] consume more than one element from the data stack, and it's hard to see how else they could be defined. The '+' word consumes 2 elements, as we'd expect. So does '!'. [0] http://lars.nocrew.org/forth2012/alpha.html http://lars.nocrew.org/forth2012/alpha.html
- whartung 4y agoThe other issue, specifically in Forths, is that it's not immediately obvious WHEN an argument is processed. For example, you could have a construct such: ( read first block from FILE.SCR ) OPEN FILE.SCR 1 BLOCK But if you try to define a word to do that: : get-block OPEN FILE.SCR 1 BLOCK ; You will find the second one doesn't work because OPEN is special, reading from the input stream for the filename at RUN TIME, not COMPILE time. The OPEN command is more suited to the immediate "shell" experience, than for use in code. But it's not obvious, certainly not immediately apparent at first sight. Need to dig into the documentation is understand the difference.
- astrobe_ 4y agoNot the best example. The lack of quotes around file.scr is an extra-giant-huge hint that OPEN does some parsing, and therefore you should check the docs before using it in a definition (actually it is so very likely that it "won't work" in a definition that you won't even bother checking the docs). Example aside, this type of word is usually used in a DSL-y way and with parsimony. Confusing uses are to be filed in the misuse/abuse category.