Reading Cupid in a day
Cupid is about 44,000 lines of C. You can't read that in a day, and this page doesn't suggest you try. What a day can get you, if you know some C and some Haskell, is the shape of the thing: what the main stages are, and one execution path through them, from the bytes of a small program to its output. You'll know what the one data structure is, where a type class goes when it disappears, and what the machine does when it runs, and you'll be able to say which file does each step.
That is all this page promises. You won't understand the type checker at the end of it, and you won't yet be someone who can safely change the code. Some of the code is also harder to read than it should be: much of the C is Hugs's, tidied rather than rewritten. What makes the day possible is that there's only one way through. There is one representation, one pipeline and one machine, and no flags to choose between paths that would otherwise have to be read twice.
No one has yet timed this route with a new reader, so treat it as a proposal.
Before you start
You'll want an interpreter that can show you what it's doing. The checks
build option adds the +D toggle, which prints generated code and traces
evaluation. From the top of the source tree:
meson setup build-checks -Dchecks=true
ninja -C build-checks
export CUPIDDIR="$PWD/build-checks/libraries/cupiddir"
build-checks/src/cupidCupid> :set +D
Cupid> 1 + 2
You should see a page of instructions and Entering name lines, with a 3
somewhere among them. Build Cupid on Linux and
macOS has the prerequisites. The
file to keep open beside everything else is src/storage.h.
The first day
This is five chapters of the sixteen, in this order, three of them only in part. Read the chapter first and then the code, because the chapters tell you what to look for.
-
The whole path. How Cupid runs a program, all but its "Try it", with the example running under
+D. ThenaddScriptinscript.candrunPrograminevaluator.c, which are short. You don't need to understand everything in the listing yet. -
The data. Cells, text and tables, with
storage.hopen, andpairandmarkSlotinstorage.c. This is the investment that pays for the rest of the day. Once you can see thatfst,sndandhdwork on anything, and thatwhatIsis how code decides what a cell is, most of the code becomes approachable. -
The front end, in part. From How source is read, the opening example, "Indentation becomes punctuation", "Expressions and types are trees", and the operator grouping at the start of "What the grammar leaves to later". Find the layout code in
yylex(search forgoOffside) and theendrule inparser.y, then readcheckDefnsinstatic.cline by line without following the calls. -
Types, in part. How types are inferred starts by inferring small functions by hand. Read those opening examples, then "Types are a skeleton and an offset" and "The expression is rewritten in place" to connect the ideas to Cupid's representation and dictionary arguments. The implementation of generalisation and the details of binding groups can wait. Read
typeExprandtypeApintype.cfar enough to seeassumeEvidcalled, andoverEvidinpreds.c. This is the hardest part of the day, so give it a bounded amount of time and move on even if you're lost. The next step needs only the idea of a dictionary. -
Compiling and the machine, in part. From How code is compiled and run, the opening on reduction, "Reading a listing" and "The machine". Read
compileGlobalFunctionincompiler.cto see the order of the passes, without following them.evalandruninmachine.care short enough to read whole.
That opens about 16,000 lines, of which you'll read a small part closely.
What can wait
All of this is optional, and most of it is a better second day than a longer first one:
- Deeper into the type checker: the rest of How types are
inferred,
kind.c, which is kind inference and makes a gentle introduction to the machinery. How constraints are solved starts with examples of dictionaries supplied by a caller, a superclass or an instance; then followentailinpreds.cand the binding-group work inunrestricted. - The rest of the compiler: the sections of How code is compiled and
run on reducing the language, matching,
lambda lifting and generating code, with their examples, and
match,lift,makeandbuildAp. - Exercises: "Try it" in the first chapter and the exercises that end the compiler chapter. The last of those changes the source and measures the result.
How to tell whether it worked
If the day went well, you can answer these without looking anything up, or at least you'll know exactly where to look:
- What is
NIL, as a C value? What doesisPair(c)test, and how can a single comparison answer that question? - Why can't Cupid use a copying garbage collector, and why are fifteen of its files compiled without optimisation?
- The parser sees
{and;that weren't in the file. Who put them there? And when the lexer can't decide where a block ends, who decides? - Why does the parser leave
a + b * cas a flat list, and which pass finishes the job? - A function has type
Num a => a -> a. How many arguments does its compiled code take? Where does the extra one come from at a call site? - What does
x + ycompile to, if there's no instruction for adding? When does the addition happen? - Why is
f x = g xno deeper on the C stack thang xitself?
The complete tour
The sixteen chapters, read in order, are the longer way through. They put the design right after this page, take in everything the first day skipped, and go on to the parts around the pipeline: the running interpreter, modules, foreign functions, character encoding, the build and how the C is written. They end with why the language is what it is and the history.
The map
For when you want to know where something lives, here is src/, grouped by what
it does, with approximate sizes and leaving out a few small utility files.
| Part | Files | Lines | Chapter |
|---|---|---|---|
| The data | storage.h, storage.c, sizes.h | 4,800 | Cells, text and tables |
| Reading source | input.c, parser.y, fixity.c | 3,600 | How source is read |
| Static analysis | static.c, binding.c, depend.c, scc.c, pattern.c | 2,000 | How source is read |
| Declarations | tycon.c, class.c, typeexp.c, kind.c, deriving.c | 5,500 | Mostly none yet |
| Modules | module.c | 1,200 | Modules, files and the search path |
| Types | type.c, subst.c, preds.c | 5,300 | How types are inferred, How constraints are solved |
| Compiling | compiler.c | 1,800 | How code is compiled and run |
| The machine | machine.c | 1,800 | How code is compiled and run |
| Primitives | prims/ | 7,200 | Mostly none yet |
| The driver | cupid.c, run.c, evaluator.c, script.c, opts.c, edit.c, goal.c, errors.c, output.c | 3,900 | The running interpreter |
| The platform | machdep.c, sysdeps.h, char.c, unitable.c | 3,600 | Platforms and the build, Character encoding |
| Foreign functions | ffi.c, HsFFI.h | 1,500 | Foreign functions |
The headers beside the static-analysis files are short and show what each exposes;
connect.h declares the entry points of the big stages, such as type.c,
compiler.c and machine.c.
Static analysis and the declarations are spread over many files because each stage
that checkDefns runs has its own: tycon.c and class.c check the data, class,
instance and default declarations, typeexp.c resolves the type expressions in
them, kind.c infers their kinds, pattern.c turns parsed expressions into
patterns, binding.c turns equations into bindings, and depend.c resolves names
and sorts the bindings into dependency groups, with scc.c doing the
strongly-connected-components sort. deriving.c writes the derived instances,
ffi.c checks the foreign declarations, and module.c works out what a module
exports and what an import brings into scope.
The primitives are what the Prelude stands on: prims/builtin.c has arithmetic,
characters and the pieces of the FFI, prims/array.c arrays, prims/iomonad.c the
IO monad and exceptions, and prims/bignums.c the Integer type. Unicode
classification and the character encodings are in char.c.
How the output here was made
These chapters quote the interpreter in three different ways.
Raw output is what a terminal shows, apart from the banner: the sessions, and $ cupid Total.hs.
Normalised output is real output tidied so that it doesn't change for incidental
reasons. A listing's addresses are gone, its jump targets are labels, L1, L2
and so on, and the address of the shared failure instruction is FAIL. The names
the compiler invents depend on what has been compiled so far, so a listing in these
pages writes them descriptively, by hand: dNumInt is the dictionary for Num Int,
which a raw listing prints as v87 at the revision below, and go' is the lifted
form of a local function called go. What follows a ; is a comment added by hand,
a line of ... stands for lines left out, and traces show only the names entered.
Schematic forms were written by hand to show a shape, and no command prints them: the trees and stack pictures, and the "as if you had written" Haskell.
Everything was produced from src/ and libraries/ as of commit 7a38d5c, on
x86-64 Linux with gcc, built as above with -Dreadline=disabled as well. The
programs are in tests/doc-sessions/. With CUPIDDIR set as above, this shows a raw
listing, the tokens and cells from running the program under gdb, and a listing's
normalised form:
cd tests/doc-sessions
printf ':set +D\n:load Total.hs\n:quit\n' | ../../build-checks/src/cupid
gdb -q -batch -x show-stages.py --args ../../build-checks/src/cupid Total.hs
cd ../..
tests/check-doc-sessions build-checks/src/cupid \
'build-checks/libraries/cupiddir/packages/*' . --show total-listing
meson test runs tests/check-doc-sessions, which compares each marked block in
these pages with the normalised output of the current tree, and fails if they differ,
so those blocks don't depend on the revision above. It checks that each invented name
is written consistently, though not that the descriptive name is the right one.
Marked C and grammar excerpts are checked against their source files too.
Listings and traces need a checks build. The tokens and cells need gdb and a build
with -Dbuildtype=debug, since optimising can leave gdb unable to read a function's
arguments, so meson test leaves them out of an optimised build. Blocks that can't run
are counted as skipped, but a gdb that runs and fails is a failure. One CI job, Linux
with gcc, runs the check on an unoptimised checks build with --require-all, which
allows no skips.
The counts quoted in Counting the work
and What survives a collection do
change with the libraries, so they belong to that revision. The test checks three things
about them: that head (queens 8) takes under a tenth of the reductions of length (queens 8); that repeating either in a session costs fewer reductions; and that forcing table
leaves between 50,000 and 65,000 fewer cells free, which a further list doesn't add to. It
also checks that total on 16,000 elements gives an answer and on 20,000 reports an overflow.