3
$\begingroup$

According to the documentation on MakeExpression,

MakeExpression[boxes, form] is the low-level function used in Wolfram System sessions to construct expressions from boxes.

what exactly does "low-level" mean? Do I understand that Mathematica has a parser before feeding the parsed expression to the Kernel for evaluation? Or is MakeExpression handled by the Kernel itself, with a different meaning for "low-level"?


I am asking this because I have discovered two different ways to have custom notation in my package:

I am using the angle bracket characters and to denote inner products. Either I can assign a DownValue to the meaningless function AngleBracket to point to my package function myInnerProduct, or I can use a construct like MakeExpression[boxes_, form_] = MakeExpression[..."myInnerProduct"..., form]. It would be useful to know how MakeExpression is processed by Mathematica before committing to a particular way of implementing the custom notation.

$\endgroup$
  • $\begingroup$ When you enter something in the frontend, the frontend places your input in boxes. When you press shift-enter, these boxes (not your input!) are sent to the kernel for evaluation; the first thing the kernel does is translating the boxes to an expression by calling MakeExpression. You can monitor this with LinkSnooper. MakeExpression is a kernel function like any other. I suppose that low-level here means that normally you do not need to use this function yourself. $\endgroup$ – Fred Simons Mar 10 '16 at 7:11
2
$\begingroup$

First you need to be aware of what boxes are and the fact that MakeExpression works only on boxes.

"Boxes" are Mathematica expressions that represent stuff that appears in a notebook. They are bound to notebooks and the Front End. When you type something in a notebook, it will be stored as boxes. Select a cell and hit Command-Shift-E to see them. MakeExpression is used during the process of converting boxes to expressions, e.g. going from RowBox[{"1", "+", "1"}] to Plus[1,1]. This conversion process is hackable. Users can modify MakeExpression. This is how the Notation package works.

This is different from when you send a plain string to the kernel. If you run the kernel in command line mode, then the input you type is just string. It doesn't get converted to boxes. Instead it gets directly converted to expressions by a hardwired parser. "1+1" goes directly to Plus[1,1]. This process is not customizable.

The implication is that custom notations, such as those created by the Notation package, only work if you are using the Front End and notebooks.

However, the parsing rules to go from "〈x〉" to AngleBracket[x] are already known to Mathematica, and already work in command line mode. You don't need to add a custom notation to work with angle brackets – the notation is already there. You can just create a definition for AngleBracket and it will work whereever.

MakeExpression and MakeBoxes are considered low-level because they are the lowest level user-accessible pieces in the conversion chain between expressions and boxes. These functions are assignable. You can add new definitions to them to teach Mathematica how to do certain parts of the conversion. These definitions will also often involve calling MakeExpression/MakeBoxes again recursively to process sub-pieces.

In contrast, ToExpression/ToBoxes are high level functions which do the complete conversion in one step, and are not modifiable. They call call MakeExpression/MakeBoxes. Use ToExpression/ToBoxes if your goal is to perform the conversion and not to modify the conversion process.

All of this stuff is not well documented and I do not fully understand the details. You can read about it here: http://reference.wolfram.com/language/tutorial/TextualInputAndOutputOverview.html

$\endgroup$

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.