3
$\begingroup$

I'm trying to deepen my understanding of the mathematica language after having messed around with it for a long time.

I'm reading the book "mathematica programming Intro". Unfortunately, I don't really get its explanation of single brackets $[ \quad]$.

What exactly does a statement like f[x] represent? I understand that you can use this to call functions or indexed variables, but I don't really understand what's happening under the hood, in terms of mathematica's symbolic language. in other words, how are the brackets interpreted syntactically by mathematica?

EDIT: Perhaps a better formulation of my question is: How are these statements parsed by the mathematica compiler?

  • f[5]
  • f[5][3]
$\endgroup$
  • $\begingroup$ @Kuba, I'm not sure if I'm "confused". I just don't know what the brackets are in the language. maybe you can just state in the most literalist way possible, exactly how mathematica interprets the brackets? how are they parsed, exactly? I may have skipped over the explanation in the documentation, but I could not find it. The documentation simply starts talking about expressions that contain brackets, without literally stating how they are parsed. $\endgroup$ – user56834 Jan 25 '18 at 7:26
  • $\begingroup$ @Programmer2134 do you mean how does Mathematica use them to build an AST? Or do you want to know what the WVM byte code for them looks like? The first you can get a sense for by looking at TreeForm, but I'll warn you now that that will be unsatisfying to you if you're not comfortable with the code-as-data paradigm. $\endgroup$ – b3m2a1 Jan 25 '18 at 7:46
  • $\begingroup$ So I answered, but it is quite condensed :P $\endgroup$ – Kuba Jan 25 '18 at 9:20
2
$\begingroup$

The general form of a Mathematica expression is

head[part...]

The expression you show are like any other expressions. Therefore, you can examine with Part. Like so.

This gives the head.

f[5][[0]]

f

This gives the 1st part (aka 'argument' if f has a function definition).

f[5][[1]]

5

In 2nd case, the head is itself an expression.

f[5][3][[0]]

f[5]

You get at 1st part of this head with

f[5][3][[0, 1]]

5

f[5][3][[1]]

3

There is no magic unless the expression has been processed by constructs that make it special such as SetDelayed or SetAttributes.

$\endgroup$
2
$\begingroup$

f[x, y, ...] is an abstract representation (AR from now on) * of an expression with head f and arguments x, y, .... (expressions, levels)

You can call 'head' a 'function' or call 'arguments' 'elements', does not really matter. (meaning of expressions) You only need to remember that both the head and each of arguments can be an expression too e.g. f[x][g[y], h[z1][z2][z3]]. For simplicity we will assume that there are no definitions associated with f, x,y,.. so it will stay inert here.

It is not parsed (in WL meaning) to anything, it is what the WL input is parsed to. The so called full form of the expression. Of course it is represented somehow in a memory but that is too deep, out of scope here (I don't know either :)). (e.g. memory management for large arrays)

It undergoes evaluation which is outlined e.g. in tutorial/Evaluation

(*) I said an AR because there are several 'stages' we can confuse.

  • 'what it really is': ????: internal/low level representation in (?)C (as I said, out of scope here)

  • 'how to think about it': f[x]: AR / full form of an expression (see note about context at the bottom)

  • 'what we see'/'what we input': f[x]: piece of screen rendered by FrontEnd (terminal/Mathematica FE/CloudFE) which represents input/output of the Kernel, in a specific form or styles.

    That rendered piece is really a cell expression: "f[x]" or RowBox[{"f", "[","x", "]"}] (or an html in case of cloud FE)

    So f[x] as an AR of an expression is not parsed but an expression input e.g. RowBox[{"f", "[","x", "]"}] (rendered as f[x]) is parsed to our AR and then evaluated.

I need to mention that I intentionally ignored contexts to not confuse a reader. But AR is not just f[x], every symbol has context which we should keep in mind.

That is an input is parsed to something like System`f[Global`x]. See FullForm with context for each symbol?.

For completeness, f @ x / x // f do not live 'below' input/output stage. Boxes/Strings of what you see as f @ x are parsed to System`f[Global`x].

$\endgroup$
  • $\begingroup$ Feedback about wording appreciated. $\endgroup$ – Kuba Jan 25 '18 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.