Actionable: Support direct function application of a result via (expr)(arg), but not yet unary application w/o parens #3379
Replies: 3 comments 7 replies
-
Interesting to think this through, thanks! I agree that (2) wouldn't work as we want for common cases like About (3): right now, there is a clear separation between parsing an expression and evaluating it. Ther parser is not aware of which functions and variables are available and parses purely based on the syntax. If this changes to the parser requiring to look up the defined functions, we cannot separate parse/compile/evaluate anymore: the parser must already know the scope and math namespace. I think that would be a serious loss, because right now you can compile once (slow) and evaluate repeately (fast). Besides that, it helps making the behavior of the parser predictable when it is not coupled with defined functions (which can be built-in and user defined). So I think (3) is not a good option because of that. I like the idea of (4). It's a bit odd but very pragmatic :). So this boils down to adding a signature I'm not sure if there is an actual use case for expressions like |
Beta Was this translation helpful? Give feedback.
-
|
I suspect that some simple name like Another plausible option that occurs to me: just use an OperatorNode with the empty string as the operator name. After all, in some cases putting things side by side is called something like the "empty operator", and some programming languages even let you overload it. If we provide a way for the evaluation process to look up the correct special (typed-)function to use when the operator is the empty string, this scheme would probably take very little code to implement, and moreover, clients of the library could extend the semantics of juxtaposition just by importing more implementations to that typed-function. So I think this option is definitely worth consideration, and probably what I'd recommend just for ease of implementation. But if you think it might be confusing when looking at parse trees to see an OperatorNode with an empty string name, and prefer to have a distinct Node type to represent the situation of juxtaposition that syntactically has not been definitely determined between implicit multiplication or function application, then I'm fine with that too, and would suggest a "structural" name like JuxtapositionNode or AdjacentNode. |
Beta Was this translation helpful? Give feedback.
-
|
Zooming out a bit: because we support implicit multiplication, some other things become impossible (or very hard) to support, like unary functions without parenthesis and directly invoking a function returned by a function. That is unfortunate, but I think we have to accept that and not try to have it both ways. So, let's not go into the direction of a
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
xis a number, thensin xis what you will typically see forsin(x), and no mathematician would take that forfhas been defined for the moment in some mathematical writing, it would be very unusual to seef 3forf(3), it's also the case that nobody would ever writef 3for3ffor 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 EXPRBwhere EXPRA and EXPRB are both unparenthesized and separated by whitespace (critically, because nobody ever writessinxorcos1and those would be tokenized as single symbols anyway). So some options of what to do with this construct:x 3that one is very unlikely to see "in the wild". But at least it's clear and explicit.sin xcorrect, for example. And it is just as clear and explicit as the current behavior, just different. However, it removes the possibility of writingx ywhich 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 xandx ycorrect. 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)blahwhere 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