4 ms·
A lot of domain languages need infix operators though. And even if you understand the domain, it's hard to read and refactor code in the presence of unrestricte
by lmm 11y ago
A lot of domain languages need infix operators though. And even if you understand the domain, it's hard to read and refactor code in the presence of unrestricted macros, whereas in a language where such DSLs are implemented via the type system the tooling will understand that.
- Jtsummers 11y agoWhy do they need infix operators? Most programmers already use prefix notation with function calls. Infix is primarily restricted to mathematical and logical operations, but the prefix (lisp style, taking an arbitrary number of arguments) is pretty clear when written neatly: (and cond1 cond2 cond3) (NB: multiple lines like this would really only be used for a lot of conditions or longer expressions.) We already do something similar with our algol-like languages: if((cond1 || cond2) && (cond3 || cond4) && (cond5 || cond6) && (cond7 || cond8)) In lisp, CL at least, would look like: (if (and (or cond1 cond2) (or cond3 cond4) (or cond5 cond6) (or cond7 cond8))
- lmm 11y agoBut at that point it's no longer the language of the domain. If you're going to support writing mathematics that looks like the mathematics that mathematicians write then you need infix operators, because mathematics is written with infix operators. Similarly for many other domains.
- Jtsummers 11y agoEven still, mathematics isn't entirely infix. And outside computer algebra systems, you'll be hard pressed to find examples of the postfix operations expressed as postfix in programming environments. !, for example. What language allows you to express: n P k = n! / (n - k)! as the above? `n P k` will become something like `nperms n k`, factorial will be moved to prefix as `fact n / fact (n - k)`. We're already diverging from the domain language, there's no reason to consider a more logically consistent framework in this case than one that seems to have arbitrarily moved some things from infix to prefix, postfix almost universally to prefix, and left some infix as infix.
- xrange 11y ago>What language allows you... SML allows you to have an infix function named "P", and Haskell allows something similar, although you have to quote it with backticks and can't use a capital letter to start a function name... https://en.wikibooks.org/wiki/Standard_ML_Programming/Expressions#Infix_function_calls https://en.wikibooks.org/wiki/Standard_ML_Programming/Expres... https://wiki.haskell.org/Infix_operator#Using_prefix_functions_with_infix_notation https://wiki.haskell.org/Infix_operator#Using_prefix_functio...
- Jtsummers 11y agoI did mean to mention that, but how about creating postfix operators?
- xrange 11y agoIn Prolog you have the ability to create new postfix operators. Of course Prolog being a logic language and not a functional language, along with '!' being used for 'cut' opens up another can of worms. http://www.swi-prolog.org/pldoc/man?predicate=op/3 http://www.swi-prolog.org/pldoc/man?predicate=op/3
- lmm 11y agoYou're right as far as you go, but it's not an all-or-nothing thing. Most languages won't let you write mathematics exactly like mathematicians do, but the closer you can get the better. For many cases avoiding infix entirely would be a big cost. (FWIW Scala allows postfix operators, though you'd have to put the n! in brackets)
- sklogic 11y ago> Most languages won't let you write mathematics exactly like mathematicians do, but the closer you can get the better See Wolfram Mathematica for example. Also, one of my usual DSL tricks is to allow arbitrary TeX in identifiers, which, combined with the literate programming tricks, allows to write very idiomatic mathematical expressions as a code.
- kazinator 11y agoThat is incorrect; the notation (op arg1 arg2) isn't the surface syntax that is preferred by some practitioners working in that domain. However, it corresponds 1:1 to its abstract syntax. There are ways to provide infix independently. That is to say, one person can develop this domain specific syntax, and another can develop or customize an infix engine for it. In Common Lisp, there is a well known "infix.cl" module that provides mappings like a[b,c] -> (aref a b c), f(x, y) -> (f x y), a + b -> (+ a b) and so on. This isn't understood as creating a new language as such; it's just a sugar. I think it allows new operators to be added with custom precedence and associativity. Or you can hack it however you want. I've never seen it used in any production code. The main purpose it serves is to satisfy people who want to know that it can be done; after that it turns out that they don't actually want it done. They just don't want to work with a language that can't do it. The only program I'm aware of which actually supports writing mathematics that looks like the mathematics mathematicians write (the actual 2D notation) is Tilton's Algebra: written in Common Lisp. The various mathematics languages out there fail. Oh, and not to mention that mathematicians have been trained to work with this: $\sin{(\frac{\pi}{2}-\theta)} = \cos{\theta}$ That's not such a bad example; I can still sort of see the trig identity in that if I squint my eyes.
- lispm 11y agoYou need infix, but that's not enough. Mathematics isn't written with infix operators alone. Mathematics is written in a fancy 2d notation with all kinds of operator types: infix, prefix, postfix, around-fix, sub-fix, super-fix. If you look at actual software for maths - several famous ones were written in Lisp like Macsyma, Reduce and Axiom - they provide more than infix.
- sklogic 11y agoAnd? DSLs can have any syntax you like. And any type system you want. And DSLs done the right way, via macros, are much better in integrating with tools than any ad hoc interpreted DSLs would ever be able to. You can easily have syntax and semantic highlighting infered, with auto indentation, intellisense and all the bells and whistles. For no extra cost.
- lmm 11y ago> And DSLs done the right way, via macros, are much better in integrating with tools than any ad hoc interpreted DSLs would ever be able to. You can easily have syntax and semantic highlighting infered, with auto indentation, intellisense and all the bells and whistles. For no extra cost. No you can't. If the macro is arbitrary code then no tool can offer those things - there's no way to offer intellisense if you don't know what strings are meaningful in the language, and an unconstrained macro could use anything to mean anything.
- sklogic 11y agoYes you can - as soon as you start wrapping your arbitrarily complex macros into a custom syntax. I am easily doing this stuff with any language I am adding macros and extensible syntax to.
- lomnakkus 11y ago> Yes you can - as soon as you start wrapping your arbitrarily complex macros into a custom syntax. Well, by that definition you get exactly the same if the host language of your DSL is statically typed and doesn't use macros. Custom syntax is custom syntax and whether tools/IDEs understand it has nothing to do with the host language.
- sklogic 11y agoSorry, I did not quite get what you mean. Of course macro+syntax extension got absolutely nothing to do with what you can achieve in a language without macros. And, no, you did not understand. Any custom syntax you're adding (if the right tools are used, like mine, for example) would automatically become available for your IDE and all the other tools, because they're reusing the same compiler front-end.
- AKrumbach 11y agoAll compilers translate a text stream into a tree structure during parsing, so arguing that some language "needs" infix is saying that there is some tree structure out there that can't be traversed depth-first rather than breadth-first. That can only be true if your supposed "tree" actually contains loops. What language do you know of that generates cyclical parse structures?