The question
What is a datatype? The answers we give first — a set of numbers,
several values glued together, values with code attached — each
describe one aspect. This session takes one aspect, how a datatype
organises its values, and finds that there are two answers to it,
and that they are opposites.
We discover both in JavaScript, on a set of integers with four operations.
In the first version a set is data: an array, and every
operation inspects it. In the second a set is a function
that answers is x inside?, and every operation can only call it.
Client code cannot tell them apart — until we extend the interface.
A new operation is one line for the first version and rebuilds every set in
the second; a new kind of set is one line for the second and touches every
operation of the first.
Then the names. The first way, made precise, is the algebraic
data type: a closed list of shapes, written where the compiler
can read it. The second is the object: a value used only
through the operations it carries. Reynolds saw in 1975 that they are
complementary; Cook gave the comparison its rows in 2009; Wadler named the
tension between them in 1998.
Warm-up, then the quest, A to C
A tiny language for melodies as an OCaml type, then the programs that
process it. Every function is one match on one type, and the
lecture’s thesis is tested in code: when we add a constructor, the
compiler lists every function we have to fix; when we add a function, we
touch nothing else. Nothing needs an ear: the tests are on counts, beats
and event lists. The piece is Bach’s Invention no. 1 in C major,
BWV 772 — two voices, 467 notes, written out in
tunes.ml.
| W | Warm-up, before the music.
One sheet: on paper, a notification by email, SMS or push as a
type — write it, count its values, find the missing and the
unreachable case, fill the kinds × operations grid —
then five small types at the keyboard in warmup.ml.
Plain exercises, without a story. |
| A | Two numbers. Count the
notes; measure the length in beats. Seq adds,
Par takes the maximum — the constructor decides
the operation. |
| B | From a melody to
sound. Compile the tree to timed events and write a
.wav; we hear the two voices together. Then add a constructor,
Repeat, and read the compiler’s list of what to
fix — that list is the point of the session. |
| C | Operations on melodies.
Transpose, print, reverse, simplify: four new functions, and
nothing else changes. Reversing swaps a Seq but not a
Par — time reverses, simultaneity does not. |
Nothing is handed in. What is left open is finished at home,
with the checks in main.ml saying which parts remain.