4
$\begingroup$

expr//HoldForm usually returns identical expressions as input, but sometimes parenthesis are inserted. I tried to interpret this behavior as an attempt to eliminate ambiguity, but some observation shows that it does not seem to be the real purpose.

For some long expressions (that is not trivial to see the real evaluation order), it does not add any parenthesis at all, and removes parenthesis which is unnecessary.

For this case:

0 ..

(0) ..

It could relate to this issue:https://github.com/halirutan/Mathematica-IntelliJ-Plugin/issues/31

a .. a //HoldForm

(a..) a

Adds parenthesis which doesn't seem to be able to cause any ambiguity without them. Even if they are, as long as the input is valid, it is not necessary.

My question is: Is there some rule Mathematica will insert/remove parenthesis in an expression? And what is the purpose of doing so?

$\endgroup$
5
$\begingroup$

Remember that the internal form of an expression is often not the same as what you type as input or is displayed as output. For example the input 0.. is interpreted as Repeated[0] and Repeated[0] is displayed in output as (0)..

Hold and HoldForm prevent evaluation but do not prevent parsing of input and formatting of output, so it is no surprise that HoldForm can produce an output which is not identical to the input. For example HoldForm[Factorial[5]] will output 5!

Parentheses are used in input parsing and output formatting, but they are not part of the internal expression. So it's not really the case that Mathematica is inserting or removing parentheses in the expression, it's just that sometimes you enter expressions in a different form to the standard output format.

$\endgroup$
  • $\begingroup$ I am only using HoldForm to prevent the order change of Times expression. I just wonder why the standard form included some unnecessary parenthesis. $\endgroup$ – happy fish May 8 '16 at 10:35
  • $\begingroup$ Just for clarity I think, so that first period doesn't get confused as part of the real number 0. $\endgroup$ – Simon Woods May 8 '16 at 10:42

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.