Why aren't parentheses ( ) an expression in Mathematica?
Can I get an expression in a form where parentheses are represented by an expression?
Why aren't parentheses ( ) an expression in Mathematica?
Can I get an expression in a form where parentheses are represented by an expression?
As djp explains parentheses are unnecessary in the FullForm of an expression; it is logical for superfluous information to be removed.
However if you want parentheses to persist you could use something like this:
$PreRead = # /. RowBox[{"(", body___, ")"}] :> RowBox[{"paren", "[", body, "]"}] &;
MakeBoxes[paren[body___], form_] :=
MakeBoxes[{body}, form] /.
RowBox[{"{", x___, "}"}] :> RowBox[{"(", x, ")"}]
Now:
foo[(bar), (1 + 2) + 5]
foo[(bar), 5 + (3)]
The FullForm of which is: foo[paren[bar], Plus[5, paren[3]]]
Also:
(1, 2, 3)
% // FullForm
(123)paren[1,2,3]
()
% // FullForm
()paren[]
You could add rules as desired to handle the head paren.
Note: before taking this rather unusual step consider using the existing functionality of AngleBracket.
LLlAMnYP commented:
I'm even surprised that the replacement rules in the answer below work at all. It prompts me to ask "how does M even apply replacement rules before parsing the expression?"
To understand what is being done with $PreRead and MakeBoxes and the rules on RowBox one must understand how Box form is used by Mathematica. As the documentation states:
All textual and graphical forms in Mathematica are ultimately represented in terms of nested collections of boxes.
Input is converted into Boxes by the Front End using functionality that may be accessed by this method that John Fultz revealed, which I package as:
parseString[s_String, prep : (True | False) : True] :=
FrontEndExecute[FrontEnd`UndocumentedTestFEParserPacket[s, prep]]
For example:
parseString @ "(1,2,3)"
{BoxData[RowBox[{"(", RowBox[{"1", ",", "2", ",", "3"}], ")"}]], StandardForm}
This Box data is then sent to the Kernel where $PreRead, if defined, is applied before further processing. (Note: CellEvaluationFunction is a lower level hook that is applied before $PreRead.)
Recalling that "all textual and graphical forms ... are ... represented in ... boxes" the output expression must be converted back to Box form before it is sent to the Front End for display, and MakeBoxes lets us attach rules to this process. It is more flexible than Format and more robust when we want to use output as input as Michael Pilat explained.
You can make them an expression if you want. Let par[x.....] represent (x....). For example:
(x+y)*z
The FullForm would be:
Times[par[Plus[x,y]], z]
But in every such expression, the par[..] would only ever have on argument (in the example, Plus[x,y]). It would never modify the meaning of the argument. So in FullForm, there would be no point having an expression representing parentheses.
There is a use to having them in InputForm etc., because it helps communicate with the user, but they are never needed in the internal representation of an expression.
FullFormthey are interpreted such as to place the square brackets in the right way. Consider the difference betweenTotal@(List@Sequence @@ Range@5)andTotal@List@Sequence @@ Range@5. – LLlAMnYP May 08 '15 at 09:43( )can be thought of as a head likeSequenceexcept that it removed after parsing rather than during evaluation I think? – Mr.Wizard May 08 '15 at 09:48(...)is equivalent toSequence[...]. If you take my example expressions and wrap them in aHold[...]//FullForm, it'll become clear, that the grouping of the square brackets will be different in the two cases. In this sense parentheses by themselves are not an expression, but they do affect how input is parsed (or converted to theFullForm, if you will). If instead of using@and@@we appropriately place[ ], the( )become redundant. – LLlAMnYP May 08 '15 at 10:23FullFormmerely shows how expressions are represented internally, but they are parsed to internal representation in any case, with or withoutFullFormin code. It's just that, there is nothing more to it. – Leonid Shifrin May 08 '15 at 13:24FullFormchanges the expression, only that it will help us to see, how Mathematica can parse the (almost) same input in different ways depending on( )placement. It is telling, thatHold[(a+b)]returnsHold[a+b]which further supports your point. After this observation I'm even surprised, that the replacement rules in the answer below work at all. It prompts me to ask "how does M even apply replacement rules before parsing the expression?" – LLlAMnYP May 08 '15 at 13:42