Why does unary function application require parentheses? #3379
gwhitney
started this conversation in
Design decisions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is a companion discussion to #3358, in that it suggests (an) additional way(s) to invoke functions in mathjs. There is a sort of inconsistency between current implicit multiplication parsing and what one commonly sees in mathematical writing, in that if$x\cdot sin$ . And while it is true that if a function $3\cdot f$ but they would write
x
is a number, thensin x
is what you will typically see forsin(x)
, and no mathematician would take that forf
has been defined for the moment in some mathematical writing, it would be very unusual to seef 3
forf(3)
, it's also the case that nobody would ever writef 3
for3f
for this. So we could consider a couple of different possible extensions/alterations to the parsing and/or evaluation algorithm that would provide more fidelity to common mathematical usage as well as more convenience for formula authors.In the below concepts, we are just considering parsing
EXPRA EXPRB
where EXPRA and EXPRB are both unparenthesized and separated by whitespace (critically, because nobody ever writessinx
orcos1
and those would be tokenized as single symbols anyway). So some options of what to do with this construct:x 3
that one is very unlikely to see "in the wild". But at least it's clear and explicit.sin x
correct, for example. And it is just as clear and explicit as the current behavior, just different. However, it removes the possibility of writingx y
which is useful in multivariate polynomials, like(x+y)^2 == x^2 + 2 x y + y^2
. So this is likely too far in the other direction.sin x
andx y
correct. However, it would be the first example of semantic-directed (or at least type-directed) parsing for mathjs, and those are murky waters that mathjs may well not want to wander into. (But I do think this is how humans are parsing this kind of construct.)In fact, if something in the vicinity of 4 were adopted, it could potentially be extended to many other instances that currently parse just as implicit multiplication:
(f(x)=x+2)(5)
could be parsed as this "ImFaNode" and would then succeed. In fact, I think any(EXPRA)(EXPRB)
could be reasonably parsed to an ImFaNode, and it would generally do what the author intended. And it would eliminate a significant fraction of the need for #3378, especially the part that allows argument lists like(3,)
. (The only parts that I would suggest be kept are things like(EXPR)()
being function invocation with no arguments (what else could it be?) and similarly with something like(EXPR)(2, 3, true)
.)Conversely, I would say that
(EXPR)blah
where blah is unparenthesized should only be parsed as implicit multiplication, because almost nobody (except some group theorists) would ever write function application this way, and that the rules forblah(EXPR)
be left exactly as they are.If these ideas strike a positive chord, I'd be happy to close #3378 and supply an alternate PR with ImFaNodes (perhaps under a better name) and not allowing the dangling comma in argument lists, but allowing function applications like
EXPR()
andEXPR(x, y)
regardless of what sort of expression EXPR is. Or, #3378 can be evaluated on its own merits, accepted/rejected/merged with changes, and the ideas here could percolate for a while or be rejected altogether.I am just trying to make the mathjs expression language as convenient and powerful as possible, and always looking for ideas from mainstream math-community notation. Thanks for considering.
Beta Was this translation helpful? Give feedback.
All reactions