# Is expression in Mathematica well defined?

According to Mathematica, everything is an expression. So an atom is also an expression. But in other parts of documentation, they say that an expression is of form Head[e1,e2,..]. This is a contradiction since an atom does not have that form. Confused.

To express more clearly what I mean look at the following statements.

1. An atom is an expression.

2. All expressions have the form Head[e1,e2,..]

3. Expressions of type Head[e1,e2,..] have parts

4. An atom does not have parts.

If 2. would say that Some expressions have the form Head[e1,e2,..], then there would not be a problem. Then we could say that some expressions are atomic (like number 7) and some (like Plus[2,3] are nested (or molecular or whatever is the right word).

In that case I would express the whole situation as follows:

We should distinguish between two types of expressions: atomic expressions and nested expressions.

An expression is a tree structure. An atom is leaf ( a node with no children) while nested expressions have also branches (a node with children).

Mathematica comes with a bunch of primitive elements. About a dozen atoms (like numbers, strings, etc) and several thousends of built in functions (like Plus, Plot, Module, etc).

The main purpose of nested expressions is to COMBINE atoms and other nested expressions into new expressions. Since an argument of a nested expression can itself be an expression it gives us enormous combinatorial power to build new expressions (that themselves can behave like built in functions, inside our session, or otherwise taking proper care to put them into packages that can be imported).

• In your example you use Rational and Complex, but the same does not work for Integer, Real, String and Symbol. For instance AtomQ[Integer[3]] gives false, and Integer[3] does not evaluate to 3, but Rational[1,2] does evaluate to 1/2. Still confused. – Bob Ueland Nov 14 '17 at 13:28
• I would love to link official and up to date atomic expressions tutorial... – Kuba Nov 14 '17 at 13:35
• I think there's not much practical relevance to a debate on this. Everything is an expression, either atomic or compound. Atoms are indivisible. Compound expressions (but not atoms!) look like e0[e1, e2, ...], where each of e0, e1, ..., is itself an expression. Sometimes atoms are shown as if they were compound, e.g. Complex[1,2], but you can think of this as just an illusion. They are still atoms. Head returns a result for every expression, even atoms. This does not mean that atoms have a separate, extractable head. It's just a data type marker. – Szabolcs Nov 16 '17 at 14:18
• @gwr Aren't CompoundExpressions themselves non-atomic expressions? (Also, I think @Szabolcs was referring to all non-atomic expressions when saying 'compound expressions' -- it seems natural (at least in English) to refer to something non-atomic as 'compound', but alas MMA already uses CompoundExpression. Ah, the imprecision of human language!) – jjc385 Nov 16 '17 at 17:40
• @gwr Unbelievable. The docs for LeafCount say LeafCount "gives the total number of indivisible subexpressions in expr." They're clearly saying certain atoms (such as Rational and Complex) are divisible! – jjc385 Nov 17 '17 at 15:50

John Doty's answer says it very well:

Even atoms have heads, they just aren't normally displayed, even in FullForm.

I'll also respond to OP's comment:

J.M.: Look at e.g. AtomQ[Rational[1, 2]] or AtomQ[Complex[1, 3]]

OP: In your example you use Rational and Complex, but the same does not work for Integer, Real, String and Symbol. For instance AtomQ[Integer[3]] gives false, and Integer[3] does not evaluate to 3, but Rational[1,2] does evaluate to 1/2. Still confused.

I think the issue is that certain 'atomic heads' act as constructors. This is certainly true for things like Graph and Association, and appears to be true for Complex and Rational as well. Such constructors take certain inputs and turn them into atoms

Complex[1,2] // AtomQ  (* True -- proper input converted to atom *)
Complex[a,b] // AtomQ  (* False -- improper input not converted *)


It should be emphasized that an expression which has an 'atomic head' is not necessarily an atom.

As JM implicitly notes in a comment, Symbol acts as a constructor, but it expects a string -- e.g., Symbol["x"].

Your comment boils down to the fact that Integer, Real, and String do not act as constructors. This is presumably because it's sufficiently nice to input these objects already, though I agree that it doesn't seem entirely consistent.

Slight aside: Note that while Rational[1,2] appears to evaluate to 1/2, its FullForm remains Rational[1,2] (as opposed to something involving Times).

References: WReach's answer discussing pitfals of Association acting both as an atomic head and a constructor.

• Well, that's one more answer I don't need to write anymore... :D (IOU one upvote.) – J. M. will be back soon Nov 14 '17 at 15:27
• I'm glad you approve! Feel free to edit as desired. – jjc385 Nov 14 '17 at 15:28
• @J.M. Also, why the IOU's? – jjc385 Nov 14 '17 at 15:29
• I expended my 30-vote quota hours ago, so I won't be able to upvote your fine answer until a new UTC day comes in. – J. M. will be back soon Nov 14 '17 at 15:30

Even atoms have heads, they just aren't normally displayed, even in FullForm.

Head[1]
(* Integer *)
(* Symbol *)

• Ah, of course! I didn't realize this when I commented above (now deleted) -- I always get tricked when FullForm suppresses things. +1 – jjc385 Nov 14 '17 at 15:04

As @jjc385 nicely writes, the confusing part is, that some atomic heads serve as constructors. In the end the main feature of any atomic expression is, that while it may well be of the form $h[e_1, e_2]$ (e.g. the case with Rational[ 1, 2 ]), we can only extract the head and we cannot replace its parts (cf. Tutorial "Basic Objects: Atomic Objects", where special selectors are given to extract parts of atoms, e.g. IntegerDigits.)

So note:

Rational[ 1, 2 ][[2]]


Part specification is longer than depth of object.

expr[ 1, 2 ][[2]]


2

Rational[ 1, 2 ] /. Rational -> Complex


$\frac{1}{2}$

expr[ 1, 2 ] /. expr-> Complex


$1 + 2i$

a = Rational[ 1, 2];
FullForm[ a ] /. Rational -> Complex


Rational[ 1, 2 ]

b = expr[ 1, 2 ];
FullForm[ b ] /. expr-> Complex


Complex[ 1, 2 ]

But since the constructors have to be evaluated before we get an atomic expression, we can do:

HoldForm[ Rational[1, 2] ] /. Rational -> Complex  // ReleaseHold


$1 + 2i$

Update

With more elaborate atomic expressions like Association the above given indications for an atomic expression (e.g. no extraction of parts) are blurred:

assoc = Association[ "1" -> e1, "2" -> e2 ]; AtomQ @ assoc


True

But now:

assoc[[1]]


e1

So here Part is a valid selector for the atomic expression Association. It may thus be indivisibility which is the more general attribute for an atomic expression?

Partition[ h[ e1, e2], {1} ]


h[ h[e1], h[e2] ]

Partition[ assoc, {1} ]


The expression [...] cannot be partitioned.

TL;DR

It seems, that atomic expressions may sometimes (especially with later releases of Mathematica) be of the form $h[e_1, e_2, ...]$ and so have parts, but the subcomponents ($e_1, e_2, \ldots$) generally cannot be extracted by using functions like Part or subdivided using functions like Partition. Instead extraction and subdivision then have to be handled by dedicated selectors.

• Richard J. Gaylord does a good job explaining this in his tutorial: "Wolfram Programming Language Fundamentals" (pp. 2 ff.). – gwr Nov 16 '17 at 14:12
• Minor nitpick: "while it may well be of the form h[e1,e2]" -- in light of Szabolcs comment on the OP, it might be more helpful to say "while it may well be entered or displayed as h[e1,e2]". (+1, of course) – jjc385 Nov 16 '17 at 17:01
• @jjc385 I would counter that it still is its form in some cases, as that is what will be given by applying FullForm? Thanks for +1. – gwr Nov 16 '17 at 17:05
• I see your point -- I almost said this is probably a matter of point of view. I'd argue that while this is the displayed FullForm, that FullForm isn't necessarily meaningful. Consider FullForm[1], which evaluates to 1, not Integer[1]. I've lately taken the point of view that FullForm isn't always trustworthy, particularly when it comes to atoms. (There are also other cases where FullForm fails to give the full picture, such as packed arrays.) – jjc385 Nov 16 '17 at 17:19
• Moreover, since the parts of an atomic expression cannot be accessed (which is the important thing here, and a fact on which we agree), I'm hesitant to say an atomic expression has the form h[e1,e2] -- rather, I view it as a way of displaying the information. – jjc385 Nov 16 '17 at 17:19

"Everything is an expression."1. "Expressions can be written in the form h[e1,e2,…]. The object h is known generically as the head of the expression. The ei are termed the elements of the expression. Both the head and the elements may themselves be expressions"2, meaning that "all expressions—whatever they may represent—ultimately have a uniform tree-like structure"3 whose internal nodes are heads and external nodes (a.k.a. leaves) are atomic objects.

In more details, "all expressions in the Wolfram Language are ultimately made up from a small number of basic or atomic types of objects" (Symbol, String, Integer, Real, Rational, Complex).4 "These objects have heads that are symbols that can be thought of as "tagging" their types. The objects contain "raw data", which can usually be accessed only by functions specific to the particular type of object. You can extract the head of the object using Head, but you cannot directly extract any of its other parts."4 The elements of an atomic object (the raw data) are "stored directly as a pattern of bits in computer memory."5

In addition to bolding some text above, to answer your question directly, I would add that an atomic object is an indivisible expression (in our expressionism formalism) whose elements consist of raw data tagged by the head of the expression enabling higher level heads to interpret and manipulate the mentioned raw data. In this formalism, heads are symbolic representations of computation.

References

1. Everything Is an Expression
2. Basic Objects > Expresion
3. Expression Structure
4. Basic Objects > Atomic Objects
5. Basic Internal Architecture
• What is the h[e1,e2,...] structure of 1? (btw, +1, especially for the direct quotes and references) – jjc385 Nov 30 '17 at 16:08
• @jjc385, thank you! To answer your question, I have added a new info (with reference) at the end of the second paragraph. I'm glad you found this useful. – Miladiouss Dec 2 '17 at 0:03
• Also, I don't think this answers my question. I interpret the second sentence of your answer to say that all "expressions can be written in the form h[e1,e2,...]", and since 1 is an expression, this begs the question of what precisely its h[e1,e2,...]` form is. – jjc385 Dec 2 '17 at 0:27
• 2nd comment: 1 is Integer[some sort of pointer to some bits on your memory]. It's a little more complicated than that, but that is the gist of it. – Miladiouss Dec 4 '17 at 23:20
• 2nd comment: I'm not entirely sure I reach the same conclusion, but this seems like a decent way to look at it. Thanks for the discussion. – jjc385 Dec 5 '17 at 0:16