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"...
Answer
It is well-documented!
According to the Documentation page for StandardForm,
StandardFormgenerates output that gives a unique and unambiguous representation of Wolfram Language expressions, suitable for use as input. »
StandardFormis the standard format type used for both input and output of Wolfram Language expressions in notebooks.
StandardFormis based on boxes.
And then under the "Properties & Relations" section:
Use
ToBoxesto get the box representation of an expression inStandardForm:ToBoxes[x^2 + y^3, StandardForm]RowBox[{SuperscriptBox["x", "2"], "+", SuperscriptBox["y", "3"]}]Use
ToExpressionto convert back:ToExpression[%]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):
MakeBoxesis the low-level function used in Wolfram System sessions to convert expressions into boxes.
MakeExpressionis used whenever boxes are supplied as input to the Wolfram Language.Use
MakeExpressionto obtain the original expression in a held form:MakeBoxes[1 + 1, StandardForm]RowBox[{"1", "+", "1"}]MakeExpression[%, StandardForm]HoldComplete[1 + 1]
Summary
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]];
Now
Nest[(ToExpression[ToBoxes[#]] &), Hold[1/1], 10]
Hold[1 1/1]
and
Nest[(ToExpression[ToBoxes[#]] &), Hold[Divide[1, 1]], 10]
Hold[1/1]
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]];
Now
Hold[1/1]
Hold[1/1]
Nest[(ToExpression[ToBoxes[#]] &), Hold[1/1], 10]
Hold[1/1]
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.
Comments
Post a Comment