Reading GHC Core

GHC Core is the System FC language into which all Haskell is translated. The (approximate) grammar for Core is given by:

enter image description here

Core is closely related to the simpler and better known System F. All transformations GHC does on the Core level are type-preserving refactorings of this Core representation, to improve performance. And, not so well known, you can write directly in Core to program GHC.

GHC Core fits in the compiler pipeline (as it was in 2002, sans-LLVM and CMM):

enter image description here

The primary documents to learn about GHC Core are:

Related material that can aid understanding:

Core in turn is translated into STG code, which looks something like:

enter image description here

The funny names in Core are encoded in the “Z-encoding”:

enter image description here

GHC Core’s types and kinds (from Tolmach’s paper):

enter image description here

Finally, GHC’s primops appear regularly in GHC Core output, when you have optimized your Haskell down to the basic instructions GHC knows about. The primop set is given as a set of Core functions in a pre-processed file.

Leave a Comment