5
$\begingroup$

The head of an expression

In the Wolfram language / Mathematica, "everything is an expression," to quote a tutorial. I think that every expression has a head. According to the tutorial:

The object $f$ in an expression $f[x, y, ...]$ is known as the head of the expression. You can extract it using Head[expr].

Indeed, I can use Head to extract the head of several example expressions:

{myNull, mySymbol, myInteger, myReal, myString, myList, myFunction, myCompositionOfFunctions1, myCompositionOfFunctions2, myRule1, myRule2, myAssociation1, myAssociation2, myExpression1, myExpression2, myMultiplicationExpression1, myMultiplicationExpression2, myEquation1, myEquation2} = {Null, abc, 123, 123.0, "ABC", {1, 2.0, "A"}, f[x, y], f[g[h[x, y]]], Composition[f, g, h][x, y], Rule[a, b], a -> b, <|firstName -> "John", lastName -> "Doe"|>, Association[firstName -> "Jane", lastName -> "Doe"], x + y + z, Plus[x, y, z], a / b, Times[a, Power[b, -1]], a x^2 + b x + c == 0, Equal[Plus[Times[a, Power[x, 2]], Times[b, x], c], 0]}; Head /@ {myNull, mySymbol, myInteger, myReal, myString, myList, myFunction, myCompositionOfFunctions1, myCompositionOfFunctions2, myRule1, myRule2, myAssociation1, myAssociation2, myExpression1, myExpression2, myMultiplicationExpression1, myMultiplicationExpression2, myEquation1, myEquation2} 

which gives this output:

(* {Symbol, Symbol, Integer, Real, String, List, f, f, f, Rule, Rule, Association, Association, Plus, Plus, Times, Times, Equal, Equal} *) 

Visualizing an expression's structure: example 1

We can visualize the structure of an expression in Wolfram language / Mathematica using TreeForm. For example, consider f[g[a, h[x, y], b]]:

TreeForm[f[g[a, h[x, y], b]]] 

which gives this graphical output:

TreeForm example two

(The stick figure is my artistry.) The head of f[g[a, h[x, y], b]] is indeed f:

Head[f[g[a, h[x, y], b]]] (* f *) 

Visualizing an expression's structure: example 2

As another example, the head of {g["a", h[x, y], "b"]} is List:

Head[{g["a", h[x, y], "b"]}] (* List *) 

And the tree representation of {g["a", h[x, y], "b"]} looks like this:

TreeForm[{g["a", h[x, y], "b"]}] 

TreeForm example two

(Again, the stick figure is my artistic handiwork. Also, interestingly, the heads of "a" and "b" are not shown explicitly in the tree representation. Note that Head["a"] and Head["b"] each give String.)

Question

  • Is a head of an expression called a head because in the tree representation of the expression, the expression's head is at the top -- like how my own head is at the top of my body?
  • Or is "head" a term from mathematics or computer science, or short for "header"?
$\endgroup$
6
  • $\begingroup$ Could you call it a Function? $\endgroup$ Commented Nov 29, 2024 at 1:51
  • $\begingroup$ The way I think about it, is that Head of expression is its type. Mathematica has no data types, in the sense of Pascal having data types. So head acts like the type of the expression. expression of head List, means the type of the expression is List. To find if two expressions are of same type, you check to see if the heads are the same, and so on. Yes, in tree form, the head is the top node like you draw it. $\endgroup$ Commented Nov 29, 2024 at 2:13
  • 4
    $\begingroup$ The term Head in Mathematica is analogous to the head of a river. It's the source. It's the beginning. Everything that follows is dependent on it. Instead of seeing your stick figures as cartoon people, think of them as a river system that branches into ever smaller tributaries. The source of that river system is the head. It's not a mysterious math term. We use "head" in this sense all the time in a variety of contexts. From the Latin word for head we get "capital" (and related terms like "decapitate"). $\endgroup$ Commented Nov 29, 2024 at 3:32
  • 1
    $\begingroup$ So, capital, primary, first, the location of authority, etc. "Head" is not short for header, but instead it's the other way around: "header" is derived from "head". The header is the first part of a text--it's at the head. $\endgroup$ Commented Nov 29, 2024 at 3:33
  • 1
    $\begingroup$ See also: en.wikipedia.org/wiki/Head_(linguistics) $\endgroup$ Commented Nov 29, 2024 at 18:11

1 Answer 1

10
$\begingroup$

Rather long for a comment, and maybe close enough of a guess to posit as an answer:

I think of it more as the head of a snake....

HEAD/TAIL (or FIRST/REST and car/cdr in Lisp) is a common way to break down a list in functional programming languages. First[] and Rest[] in Mathematica were taken (or assigned other duties with respect to the arguments), and the design of Mathematica distinguished the part of the expression that is called the head from the arguments. Compare with Lisp below. But "head" typically refers to the leading element in a sequence, and in this sense it will be seen to be a natural choice.

Lisp:

Compare M-expressions and S-expressions in (older) Lisp:

f[e1; e2;...] ↔ (f e1 e2 ...) , 

where e1, e2,... are expressions (technically M-expressions on the left translated to S-expressions on the right). The S-expression (f e1 e2 ...) is conceived of as a list.

Note that the technical definition of an S-expression is either an atom or an expression of the form (x1 . x2) where x1 and x2 are S-expressions. The common syntax (x1 x2 x3 x4) is short for (x1 . (x2 . (x3 . x4))). So the structure of an S-expression is naturally (head . tail).

I don't know that McCarthy ever called the leading element f a "head." That came later.

The suggestion to consider Mathematica expressions as Lisp expressions occurred to me immediately (or perhaps someone mentioned it) as soon as I encountered Mma in 1990/91. Everything is an expression in both languages. Even the head can be an arbitrary expression. It is further reinforced by Mathematica's Part numbering, in which the head of a composite expression is the first part, part 0:

Expression \ Part 0 1 2 3
f[e1, e2, e3] f e1 e2 e3

Mathematica extends the notion of head to atoms (e.g. strings, numbers, etc.), which is a bit unnatural but essential for Mathematica's notion of pattern-matching. Since "a" is an atom, it is not equivalent to the S-expression (String "a"), even though Head[] has been defined for it and other atoms.

The structure of expressions and TreeForm

I've always thought of the basic form of a nonatomic expression in Mathematica as a list of expressions. The tree form is a way to display recursively nested lists. It is not entirely "accurate" or "consistent": Consider that the complete level 2 is not displayed at the same level in the following tree plot.

Level[f[g[e1, e2, e3]], {2}, Heads -> True] TreeForm[f[g[e1, e2, e3]]] 

I'm not disparaging TreeForm. I don't think it can be improved. If level 1 consists of f and g[e1, e2, e3], how do you efficiently represent g[e1, e2, e3]? It has a tooltip to display it over the g (at least since tooltips became available). But I wonder if it's misleading to display just a g at level 1 (the second level of the tree plot), when g is a head in level 2?

$\endgroup$
1
  • 1
    $\begingroup$ TreeForm will be improved using the newer ExpressionTree. TreeForm[expr] effectively formats as ExpressionTree[expr, "Heads", Heads -> False]; i.e. it is showing the tree of heads. I believe what you want to see is given by ExpressionTree[expr, "Subexpressions", Heads -> True]. $\endgroup$ Commented Dec 2, 2024 at 17:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.