(This article is continued in Metaphors in computing science II)
Michael Barr recently told me of a transcription of a talk by Edsger Dijkstra dissing the use of metaphors in teaching programming and advocating that every program be written together with a proof that it works. This led me to think about the metaphors used in computing science, and that is what this post is about. It is not a direct answer to what Dijkstra said.
We understand almost anything by using metaphors. This is a broader sense of metaphor than that thing in English class where you had to say "my love is a red red rose" instead of "my love is like a red red rose". Here I am talking about conceptual metaphors (see references at the end of the post).
Metaphor: A program is a set of instructions
You can think of a program as a list of instructions that you can read and, if it is not very complicated, understand how to carry them out. This metaphor comes from your experience with directions on how to do something (like directions from Google Maps or for assembling a toy). In the case of a program, you can visualize doing what the program says to do and coming out with the expected output. This is one of the fundamental metaphors for programs.
Such a program may be informal text or it may be written in a computer language.
Example
A description of how to calculate $n!$ in English could be: "Multiply the integers $1$ through $n$". In Mathematica, you could define the factorial function this way:
fac[n_] := Apply[Times, Table[i, {i, 1, n}]]
This more or less directly copies the English definition, which could have been reworded as "Apply the Times function to the integers from $1$ to $n$ inclusive." Mathematica programmers customarily use the abbreviation "@@" for Apply because it is more convenient:
Fac[n_]:=Times @@ Table[i, {i, 1, 6}]
As far as I know, C does not have list operations built in. This simple program gives you the factorial function evaluated at $n$:
j=1; for (i=2; i<=n; i++) j=j*i; return j;
This does the calculation in a different way: it goes through the numbers $1, 2,\ldots,n$ and multiplies the result-so-far by the new number. If you are old enough to remember Pascal or Basic, you will see that there you could use a DO loop to accomplish the same thing.
What this metaphor makes you think of
Every metaphor suggests both correct and incorrect ideas about the concept.
- If you think of a list of instructions, you typically think that you should carry out the instructions in order. (If they are Ikea instructions, your experience may have taught you that you must carry out the instructions in order.)
- In fact, you don't have to "multiply the numbers from $1$ to $n$" in order at all: You could break the list of numbers into several lists and give each one to a different person to do, and they would give their answers to you and you would multiply them together.
- The instructions for calculating the factorial can be translated directly into Mathematica instructions, which does not specify an order. When $n$ is large enough, Mathematica would in fact do something like the process of giving it to several different people (well, processors) to speed things up.
- I had hoped that Wolfram alpha would answer "720" if I wrote "multiply the numbers from $1$ to $6$" in its box, but it didn't work. If it had worked, the instruction in English would not be translated at all. (Note added 7 July 2012: Wolfram has repaired this.)
- The example program for C that I gave above explicitly multiplies the numbers together in order from little to big. That is the way it is usually taught in class. In fact, you could program a package for lists using pointers (a process taught in class!) and then use your package to write a C program that looks like the "multiply the numbers from $1$ to $n$" approach. I don't know much about C; a reader could probably tell me other better ways to do it.
So notice what happened:
- You can translate the "multiply the numbers from $1$ to $n$" directly into Mathematica.
- For C, you have to write a program that implements multiplying the numbers from $1$ to $n$. Implementation in this sense doesn't seem to come up when we think about instruction sets for putting furniture together. It is sort of like: Build a robot to insert & tighten all the screws.
Thus the concept of program in computing science comes with the idea of translating the program instruction set into another instruction set.
- The translation provided above for Mathematica resembles translating the instruction set into another language.
- The two translations I suggested for C (the program and the definition of a list package to be used in the translation) are not like translating from English to another language. They involve a conceptual reconstruction of the set of instructions.
Similarly, a compiler translates a program in a computer language into machine code, which involves automated conceptual reconstruction on a vast scale.
Other metaphors
In writing about this, I have brought in other metaphors, for example:
- C or Mathematica as like a natural language in some ways
- Compiling (or interpreting) as translation
Computing science has used other VIM's (Very Important Metaphors) that I need to write about later:
- Semantics (metaphor: meaning)
- Program as text — this allows you to treat the program as a mathematical object
- Program as machine, with states and actions like automata and Turing machines.
- Specification of a program. You can regard "the product of the numbers from $1$ to $n$" as a specification. Notice that saying "the product" instead of "multiply" changes the metaphor from "instruction" to "specification".
References
Conceptual metaphors (Wikipedia)
Images and Metaphors (article in abstractmath)
Images and Metaphors for Sets (article in abstractmath)
Images and Metaphors for Functions (incomplete article in abstractmath)
Send to Kindle