13

Why aren't parentheses ( ) an expression in Mathematica?

Can I get an expression in a form where parentheses are represented by an expression?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
user
  • 1,877
  • 10
  • 19
  • 8
    Parentheses are used for parsing, to change the relative priorities of parts. Expressions are a result of parsing. So, parentheses simply can't survive the parsing stage, being consumed and thrown away then. It's like asking, why there is no milk in а butter. – Leonid Shifrin May 08 '15 at 03:36
  • 4
    I feel this is a valid question and @leonid's comment makes for a nice answer. – Sjoerd C. de Vries May 08 '15 at 05:54
  • 1
    @Sjoerd Indeed it is a valid question. The language-design tag is intended for the questions such as the first line, and the second line is easily addressed (see my answer). – Mr.Wizard May 08 '15 at 09:09
  • 1
    I don't fully agree with the comments and given answers, I feel this is an important question. Parentheses affect the order of evaluation, though in the FullForm they are interpreted such as to place the square brackets in the right way. Consider the difference between Total@(List@Sequence @@ Range@5) and Total@List@Sequence @@ Range@5. – LLlAMnYP May 08 '15 at 09:43
  • 1
    @LLlAMnYP I am uncertain of your point. The order of evaluation is effected by creating a "FullForm" expression that evaluates according to standard Mathematica rules, is it not? In that sense ( ) can be thought of as a head like Sequence except that it removed after parsing rather than during evaluation I think? – Mr.Wizard May 08 '15 at 09:48
  • 1
    @Mr.Wizard, yes it does seem, that (...) is equivalent to Sequence[...]. If you take my example expressions and wrap them in a Hold[...]//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 the FullForm, if you will). If instead of using @ and @@ we appropriately place [ ], the ( ) become redundant. – LLlAMnYP May 08 '15 at 10:23
  • 2
    I admit, it was a stretch on my part to criticize the answers, but I found Oleksandr's comment overly dismissive. As M. has rules to interpret/parse parentheses, his comparison with the English language is somewhat harsh. "Not fully agree" meant "I don't agree with some points in the comments". The answers, I suppose, are fine. – LLlAMnYP May 08 '15 at 10:38
  • 2
    @LLlAMnYP "In this sense parentheses by themselves are not an expression, but they do affect how input is parsed (or converted to the FullForm, if you will)." - what is being parsed is not yet an expression - it is either a string or a box form. At the point when we start speaking of expressions, parentheses don't exist any more. FullForm merely shows how expressions are represented internally, but they are parsed to internal representation in any case, with or without FullForm in code. It's just that, there is nothing more to it. – Leonid Shifrin May 08 '15 at 13:24
  • 1
    @LeonidShifrin I wholeheartedly agree. At no point did I try to suggest, that FullForm changes 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, that Hold[(a+b)] returns Hold[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
  • 2
    @LLlAMnYP Replacement rules work, because parsing a string to an expression is a two-step process. The string input is first parsed to boxes, which are expressions representing the a lower-level structure that preserves the punctuation (parentheses, things like infix and postfix forms, etc). If you intersept it at that level, you can change how expression will be parsed at the end. This is what was done in that answer you referred to. – Leonid Shifrin May 08 '15 at 15:54
  • 1
    @LLlAMnYP I updated my answer with a section to clarify the mechanism used. – Mr.Wizard May 09 '15 at 00:13
  • @LLlAMnYP the subsequent comments pointed out that this question has a subtlety I did not appreciate at first. (I thought it was asking something like how to produce a parenthesized display form, which may not have been completely inaccurate given the accepted answer, but in any case I felt that the question ought to be clearer in asking for this if that is what was desired.) I now see that my comment was not really appropriate if the question is understood as asking about the structure and parsing of the language, so I deleted it. – Oleksandr R. May 16 '15 at 01:59

2 Answers2

14

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.


Sidebar: Box form manipulation

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
8

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.

djp
  • 1,493
  • 14
  • 18