Applying ToExpression and ToBoxes repeatedly to Hold[1/1] gives many 1, which seems strange.

In[11]:= Nest[(ToExpression[ToBoxes[#]] &), Hold[1/1], 10]

Out[11]= Hold[1 (1 (1 (1 (1 (1 (1 (1 (1 (1 (1 x 1/1))))))))))]

This only happens with a denominator of 1. Is it a bug? I'm using Mathematica 10.0 for Microsoft Windows (64-bit) (June 30, 2014).

EDIT: A better question would be: how can I avoid this accumulation? But presumably the answer would just be "use a pattern to detect that special case"...

  • 4
    $\begingroup$ You're right, Hold[1/1] is treated differently, apparently multiplied by 1 (checked for version 8). But is there any place in the docs that says ToExpression and ToBoxes are inverses of each other? I don't think that's what defines them. $\endgroup$ – Jens Apr 12 '16 at 2:33
  • 1
    $\begingroup$ Yes, I don't believe this is ever claimed in the docs. $\endgroup$ – M.R. Apr 12 '16 at 4:08
  • $\begingroup$ Perhaps the question makes no sense. $\endgroup$ – Bruno Le Floch Apr 12 '16 at 7:19
  • $\begingroup$ @AlexeyPopkov I already upvoted the first version... $\endgroup$ – Jens Apr 13 '16 at 6:44

It is well-documented!

According to the Documentation page for StandardForm,

  • StandardForm generates output that gives a unique and unambiguous representation of Wolfram Language expressions, suitable for use as input. »

  • StandardForm is the standard format type used for both input and output of Wolfram Language expressions in notebooks.

  • StandardForm is based on boxes.

And then under the "Properties & Relations" section:

Use ToBoxes to get the box representation of an expression in StandardForm:

ToBoxes[x^2 + y^3, StandardForm]
RowBox[{SuperscriptBox["x", "2"], "+", SuperscriptBox["y", "3"]}]

Use ToExpression to convert back:

x^2 + y^3

Based on the above I would say that it is well-documented that ToExpression and ToBoxes are inverses of each other when we work exclusively in the StandardForm (and the expression won't change during evaluation).

If one still isn't completely convinced, I would cite also Documentation pages for MakeExpression (which ToExpression uses under the hood) and MakeBoxes (which ToBoxes uses under the hood):

  • MakeBoxes is the low-level function used in Wolfram System sessions to convert expressions into boxes.

  • MakeExpression is used whenever boxes are supplied as input to the Wolfram Language.

Use MakeExpression to obtain the original expression in a held form:

MakeBoxes[1 + 1, StandardForm]
RowBox[{"1", "+", "1"}]
MakeExpression[%, StandardForm]
HoldComplete[1 + 1]


StandardForm is the default form used in Wolfram System notebooks which provides a unique and unambiguous representation of Wolfram Language expressions, suitable for use as input. On the low level it is based on boxes and one can convert any WL expression into boxes using MakeBoxes and then get the original expression backward in the held form using MakeExpression.

So the actual functions which (when working exclusively in the StandardForm) are exact inverses for each other are MakeBoxes and MakeExpression. Their only difference from ToBoxes and ToExpression is that they don't evaluate the expression.

Your particular issue

The issue you have encountered is indeed a minor bug.

You can fix it by making the following specific definition for MakeExpression:

MakeExpression[FractionBox["1", "1"], StandardForm] = HoldComplete[Divide[1, 1]];


Nest[(ToExpression[ToBoxes[#]] &), Hold[1/1], 10]
Hold[1 1/1]


Nest[(ToExpression[ToBoxes[#]] &), Hold[Divide[1, 1]], 10]

If you wish to fix also the conversion of Hold[1/1] input to Hold[1 1/1], you can add to the above a definition which will treat the 1/1 input in a special way:

MakeExpression[RowBox[{"1", "/", "1"}], StandardForm] = HoldComplete[Divide[1, 1]];


Nest[(ToExpression[ToBoxes[#]] &), Hold[1/1], 10]

A word of warning: I'm not absolutely sure that this solution is free from unexpected side-effects and won't break something in the system. If you encounter any issue, please let me know.

  • 1
    $\begingroup$ I wouldn't go so far as to make the claim that ToBoxes and ToExpression are inverse operations. I wouldn't even make that claim about MakeBoxes and MakeExpression. Expressions can have many representations in StandardForm, but necessarily, MakeBoxes must choose one canonical representation. Also, it's easy to create things which are not inverses, so even some sort of weaker statement must emphasize that this is convention, not a dictate. Finally, there are some boxes, most notably TemplateBox, which require FE secret sauce to properly convert into expressions. $\endgroup$ – John Fultz Sep 7 '16 at 13:48
  • $\begingroup$ @JohnFultz In which situations we can be sure that MakeBoxes and MakeExpression are inverses for each other (technically, i.e. in the sense of the original question)? $\endgroup$ – Alexey Popkov Sep 7 '16 at 14:29
  • 1
    $\begingroup$ There are simply no guarantees. Anybody can make any MakeBoxes and MakeExpression rules they want. The system enforces no contracts here. So, at best, your question only makes sense for what is typically true for built-in rules. Working under that assumption, there's still no guarantee that MakeExpression/MakeBoxes applied to a given set of boxes will return those boxes. There can't be. There's too many variations of boxes that produce a given expression (whitespace, comments, operators vs. fullform, prefix/infix/postfix, etc.). $\endgroup$ – John Fultz Sep 12 '16 at 7:15
  • 1
    $\begingroup$ So the only sensible variation of this question which can be asked is, are there conventions that guarantee, for a given expression, that MakeBoxes/MakeExpression will return the same expression. No, it is not guaranteed, although we do try. TemplateBox is a problem because only the FE authoritatively knows about them, but we do build in rules that do sensible things (and, in response to this question, we're updating those rules for the next release). But there are other cases, like size-limited output, or boxes with elided output (e.g., InterpolatingFunction) which still fail. $\endgroup$ – John Fultz Sep 12 '16 at 7:18

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.