Chapters

In pipeline order. Each chapter explains what a phase does and why it is built the way it is, then points at the source so you can carry on from there.

  1. 01 Parsing Haskell How GHC turns a stream of characters into a syntax tree: the layout algorithm, a grammar that forbids its own ambiguities, and the trick that lets one production parse three different languages at once.
  2. 02 The renamer How every occurrence gets tied to the thing it names, and how the phase that is supposedly "just name resolution" ends up re-associating your operators and quietly desugaring half of Haskell's syntactic sugar.
  3. 03 The typechecker: architecture The largest phase in GHC, and the one shaped most deliberately: it does not type-check your program so much as write down everything that must be true about it, and hand that to a separate solver.
  4. 04 The constraint solver What happens to the pile of facts the typechecker wrote down: a rewriting engine built around a carefully maintained set of "inert" constraints, and the compromises that make it both complete enough and quiet enough to use.
  5. 05 Classes, instances and deriving Where a class declaration becomes a record of functions, an instance becomes a value of that record, and `deriving` writes code you never see: the machinery that makes overloading disappear before Core.
  6. 06 Desugaring to Core Where the whole of Haskell collapses into nine constructors, and where GHC works out, on the way past, whether your patterns were exhaustive.
  7. 07 Core: the language A typed lambda calculus small enough to fit on a page, with a type system strong enough to catch the optimiser's mistakes. Why GHC can be aggressive without being reckless.
  8. 08 The simplifier The densest part of GHC: 450 Notes of inlining, case-of-case, strictness and rewrite rules, run to a fixed point. Where three list traversals become one loop that allocates nothing.
  9. 09 STG and CorePrep Where laziness stops being an abstraction. Every thunk, every closure and every allocation becomes something you can point at in the syntax.
  10. 10 Cmm and code generation The last target-independent form, and the three different back ends that take it the rest of the way, plus the garbage collector's stake in the layout of every stack frame.
  11. 11 The runtime system Compiled Haskell does not run alone. The RTS provides the garbage collector, the scheduler, and the machinery that makes laziness and lightweight threads work: about 180 Notes, written in C.