4 ms·
>That syntax doesn't work along with using parentheses for function application unless you do something like making the parser context-sensitive (so function fo
by alphaalpha101 9y ago
>That syntax doesn't work along with using parentheses for function application unless you do something like making the parser context-sensitive (so function followed by '(' means application but value followed by '(' means multiplication) or treat whitespace as significant (so "f(x+1)" is application but "f (x+1)" is multiplication). This is why Mathematica does Sin[22] instead of Sin(22).
I'm not so sure that's true? You can just parse 'a b c d e' as
Apply (Apply (Apply (Apply a b) c) d) e)
and then you can just say that Apply is overloaded to mean multiplication for numbers, just like + is overloaded to mean concatenation for strings in most languages, but addition for numbers. It doesn't actually change the parsing, your parsing doesn't become context-sensitive. 'Apply x y' would mean multiplication if the arguments are numbers and application if the left argument is a function.
Even if you allow multiplication of functions by scalars (which you probably shouldn't, IMO) you can easily say that 'x f' means 'x times f' while 'f x' means 'f applied to x'.
>I know you can type superscripts, subscripts, fractions, etc. using shortcuts. If you meant "as you type it" as in Mathematica will reformat what you type in more traditional notation as you type it, then I am wondering where you can enable that.
Well you can write :alpha: and it will make it an actual alpha character, you can write superscripts and it will superscript them, it will make fractions readable, etc. It will turn -> into a unicode right arrow, that sort of thing.
- kmill 9y agoThat's altering the syntax for function application by allowing f x to mean f(x), which is fine but changing the rules. If we do go with that, then "a b c" should be "Apply a (Apply b c)" because of the associativity of application --- traditional math tends not to have functions which return functions. There is an ambiguity which comes up when you allow both f x and f(x) syntax, which is you cannot tell the difference between f(x,y) and f((x,y)) if your language has tuples. (One solution: make tuples be like Mathematica's Sequence, thus establishing the associativity of Cartesian products once and for all.) A gotcha is that you can't write f(x)(y) if your function does return a function, since this will parse as f(x y). You would need to write (f(x))(y) instead. (If you insist the associativity rule should be the other way, then 3f(x) would be (3f)x, which is possibly ok, and sin cos x would be (sin(cos))(x).)
- yorwba 9y agoI don't think you can have all of traditional mathematical notation (which is often highly overloaded) in a programming language while only using syntax for disambiguation. Mathematicians mostly depend on the semantics of an expression to disambiguate it. You could get something close by making use of a type system to filter all possible parses. For example, sin cos x would parse as sin(cos(x)), (sin(cos))(x), (sin * cos)(x), ... and most of these would be discarded by the type checker. If you do it naively, the complexity will be exponential, but I think you could use dynamic programming to handle that. In my example, it would be more problematic that (sin * cos) might be interpreted as multiplication of functions, but this could be solved by having separate operators for numbers and other types, and only allowing some to be represented by juxtaposition. Overall, I'm not convinced that such a highly ambiguous system would be beneficial in a general purpose programming language, but when you just want to type in a specific mathematical formula, disambiguate it once, and then work with the unambiguous representation (maybe using a pretty-printer), I think it would be useful.
- alphaalpha101 9y ago>If you do it naively, the complexity will be exponential Hindley-Milner type inference is doubly exponential already but is still widely used. If people never actually use extremely nested expressions in real code that require this sort of disambiguation then it will probably be completely fine.
- kmill 9y ago> I don't think you can have all of traditional mathematical notation (which is often highly overloaded) in a programming language while only using syntax for disambiguation. I think it is reasonable to be able to handle certain subsets of mathematical notation, but yeah, in total it seems to be context dependent. (I've tried designing languages to deal with this before, and my previous comments were about actual issues I had encountered making it all consistent.) > For example, sin cos x would parse as sin(cos(x)), (sin(cos))(x), (sin * cos)(x), ... and most of these would be discarded by the type checker Unfortunately, all of those make some sense! The first is what anyone who took precalculus would read, but the second could be substituting cos into the power series of sin, and the third could be the pointwise product (sin * cos)(x) := sin(x) * cos(x), which is particularly important for the ring of functions on the real line. > Overall, I'm not convinced that such a highly ambiguous system would be beneficial in a general purpose programming language, but when you just want to type in a specific mathematical formula, disambiguate it once, and then work with the unambiguous representation (maybe using a pretty-printer), I think it would be useful. One thing I've been enjoying about Mathematica is the ability to have the source code be written in 2D graphical notation. I'm not convinced that it would be useful to have a special way to input and disambiguate mathematical formulae for a general purpose programming language because 1) actual formulae are infrequent and 2) it tends not to be too challenging to translate a formula into the syntax of the programming language. I think the main use case for trying to interpret traditional math notation is for exploratory calculating/graphing/programming where you would only ever use the equation at most a few times.