Programming Paradigms Session 3 · Wed 16 September 2026

← all sessions

Session 3

Algebraic Data Types and Objects

Two ways to be a value: a closed list of shapes the compiler reads, or a bundle of operations the value carries. Algebraic data types and objects, and what each can grow.

Lecture

Lab

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.

Three things to take away

A type can invent values

type note = C | D | E | F | G | A | B creates seven values that did not exist before, distinct by construction. C is not 0; nothing is inside it. Sums add alternatives, products glue fields, and recursion lets a type mention itself. Every value is then built in exactly one way — which is why match can take it apart again, and why the compiler can tell us when a case is missing.

Kinds × operations

Lay the kinds of a type along one side and its operations along the other. An algebraic data type fills the table a column at a time: one function per operation, every kind inside it. An object fills it a row at a time: one class per kind, every operation inside it. Each way makes one direction of growth free and the other expensive. That is the expression problem, and both sides of it are checked by a compiler — each blind exactly where the other sees.

Languages choose a default

OCaml, Haskell, Rust and Swift start from the closed list of shapes; Java, Smalltalk, Python and Go start from the object. Each can say the other thing — OCaml has object … end, Java has sealed, record and pattern switch — but the friction of saying it measures the default. Paradigm is not language.

The lecture, in order

  1. The question. What is a datatype, what is an object, what is an algebraic data type — asked, not answered.
  2. Two ways to be a set. One interface, two implementations in JavaScript, three extensions that tell them apart. Code runs inside the slides.
  3. Algebraic data types. Enumerations, carried values, products and sums, why the word is “algebraic”, recursion, constructors as functions that lose no information, matching as their inverse, and a timeline from Hope in 1980 to Java 21.
  4. Objects. The same set as OCaml objects and as a Java abstract class; what a class bundles, and how five languages bundle it differently.
  5. Abstract data types versus objects. One abstraction barrier placed two ways, Reynolds and Cook’s comparison, the kinds × operations matrix, exhaustiveness in three languages, the two dual checks, the visitor pattern as matching by hand, and where each way is used.
  6. Conclusion. The default each language chooses, the same question in sessions 8, 9, 11 and 12, and the lab’s type.

The slides are a web page rather than a PDF this time, because the JavaScript on them runs. Arrow keys move one step; T lists the parts; Esc shows an overview.

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.

WWarm-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.
ATwo numbers. Count the notes; measure the length in beats. Seq adds, Par takes the maximum — the constructor decides the operation.
BFrom 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.
COperations 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.

Homework

  • Reading. CS3110, chapter 3: sections 3.1, 3.5, 3.6, 3.8, 3.11 and 3.12. The lecture gives the idea; the reading gives the language. Session 4 writes OCaml from the first minute, and the lab’s Part C goes much quicker after these sections.
  • Finish the lab. Any part left open, in order.
  • Three questions to think about. How would we model a tie between two notes? What would an empty melody be, and which constructors would need it? Where does pretty live if melody is written in Java as an abstract class with subclasses — and where when it is a sealed interface with records?

Languages this session

LanguageRole
JavaScriptread the two sets and their extensions, run live on the slides
OCamlread in the lecture, write in the lab — our first type declarations and our first recursion
Javaread the abstract class, the sealed interface, the visitor
Smalltalk, Python, Goread one specimen each

Further reading

None of this is required.

  • William Cook, “On understanding data abstraction, revisited” — OOPSLA 2009 (PDF). The essay this session’s part 5 follows: the set example, the comparison table, and the claim that objects and abstract data types are different things rather than one with a feature missing.
  • John Reynolds, “User-defined types and procedural data structures as complementary approaches to data abstraction” (1975). Where the duality was first stated, in the vocabulary of its time.
  • Philip Wadler, “The expression problem” — a 1998 mailing-list post that named the kinds × operations matrix. Its resolutions are session 11.
  • Burstall, MacQueen and Sannella, “Hope: an experimental applicative language” (1980). The language that fixed the closed datatype declaration, type and constructors together, as the ML family still writes it; Burstall’s NPL (1977) had constructor patterns before it.
  • Sylvain Conchon & Jean-Christophe Filliâtre, Learn Programming with OCaml — free PDF. Chapter 5 is trees as algebraic data types, in the same style as the lab.