A musing about variables

Introducing the notion of variable, in programming languages as well as in mathematics, is no easy task.

Semantics: what does “variable” mean?

For starters, asking whether variables may vary can come handy, as an awakener: being familiar with a name (here: “variable”) does not mean that one knows what this name means.

The whole thing is actually non-trivial:

  • there are names (e.g., “mileage”);

    Anton: Er, why “mileage”?
    Alfrothul: Your mileage may vary?
    Anton: Harrumph.
  • there are denotations: what the names stand for;

  • distinguishing between name and denotation is not obvious at all:

    • for a monolingual child, a number is its name, especially once they have learned the multiplication tables by singing its successive lines; later in life, having learned a second language, they typically revert to their native language to calculate something, and then translate the result (for example, Queen Margrethe II of Denmark was once recorded playing cards with her family, speaking French; at the end, she tallied the scores in Danish and then announced the results back in French);
    • and for a native speaker who has learned musical notes by singing their names, these names may imprint themselves in such a way that hearing a musical piece comes with the names of each note (which is handy for musical dictations and for playing by ear, but still: TMI the rest of the time, especially when hearing something that is played out of tune);
  • names can be aliases of other names in that different names refer to the same denotation; for example, “one” in English, “un” in French, “eins” in German, and “en” in Danish all denote the number 1, to say nothing of the musical notes, e.g., “do”, “ré”, “mi”, etc. vs. “C”, “D”, “E”, etc. in the fixed-do solfège;
  • names have the weirdest ways to latch onto people’s mind, be it explicitly (mnemonics) or implicitly (for example, some see numbers in color (synesthesia), possibly as a resolution of their aliases for multilingual children).

Besides, sometimes the mapping from name to denotation can be modified, as in Python, C, Java, etc., which seemingly makes it possible for variables, well, yes, to vary. In that light, ML’s take of having immutable names and having a specific kind of mutable values (namely references) is pretty logical: a variable does not vary, but the reference it denotes may mutate. (In denotational-semantics parlance, the environment maps names to locations and the store maps locations to storable values.)

Then there is scope, which determines where to look for to find the denotation of a name in its context: is this context lexical, i.e., solely based on where this name was declared (i.e., on the grammatical structure of the program at hand), or dynamic, i.e., also depending on when this name was declared last (i.e., on the way the program has been executed so far)?

And then there is also the binding strategy (call by value, call by name, call by need, call by reference, and whatnot), which determines the nature of what is denoted: the result of evaluating an expression (call by value), the promise of evaluating an expression (call by name), the promise of evaluating an expression and to remember the result of this evaluation (call by need), or then again the pointer to a denotable value (call by reference), the copy of a denotable value (call by copy/value), etc.

Not being aware of most of these dimensions (nor even of the existence of these dimensions), no wonder that newcomers find variables confusing: no simplistic explanation fits.

Loki: Never mind all of that. Let me tell you about recursive definitions.

Semiotics: meanings in names

In mathematics, a variable stands for all the possible things it might denote: an immensely convenient shorthand. Often, variables are referred to by the name of a representative variable, typically “x”, as in “the x of an equation” and in the title of Steven Strogatz‘s book, “The Joy of x”.

It is often considered good form that the name of a variable reflects the nature of what this variable denotes. That was part of the language definition of Fortran where variables whose name starts with I, J, K, L, M, or N denote integers and all the others denote floating-point numbers. Ditto in OCaml where capitalized names are used for data constructors, which includes exceptions. Adhering to the plural naming convention from Haskell is also considered good form.

And then there are situations where the prototypical name of a variable stands for what this variable denotes: an x-coordinate or a y-coordinate, a delta and an epsilon, etc., which newcomers have to take on faith to bootstrap their understanding.

Postlude

Anton (surprised): And that’s all?

Alfrothul: Nothing about metavariables?

Dana (enjoying herself): And metasyntactic variables?

Acknowledgment

The present musing is the spinoff of an email exchange with Hanne Gottliebsen on 01 Feb 2020.

Version

Added an explanatory quip about “mileage” [16 Apr 2023]

Created [10 Jan 2023]

Table Of Contents

Previous topic

Mutable data in OCaml

Next topic

Sequencing in OCaml