204

Core is GHC's intermediate language. Reading Core can help you better understand the performance of your program. Someone asked me for documentation or tutorials on reading Core, but I couldn't find much.

What documentation is available for reading GHC Core?

Here's what I've found so far:

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
tibbe
  • 8,809
  • 7
  • 36
  • 64

4 Answers4

282

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.

sjakobi
  • 3,546
  • 1
  • 25
  • 43
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • 65
    Every single answer you give is always ridiculously complete. Have another up-vote and keep it up; I'm leaning heaps. – Robert Massaioli May 26 '11 at 03:46
  • 4
    The amount of CC-wiki documentation that Don and the general Haskell community has released via SO is staggering. Keep up the good Q's and A's, everybody! – Dan Burton May 26 '11 at 22:37
  • 4
    I know it's mentioned, but I think the usefulness of [__ghc-core__](http://hackage.haskell.org/package/ghc-core) should be emphasized in the answer. – Nikita Volkov Apr 29 '13 at 08:57
  • 1
    The link to GHC's `-fext-core` has bitrotted, and it seems that recent GHCs don't have this flag anymore. The latest GHC where I can find this section in the user's guide is 7.8: https://downloads.haskell.org/~ghc/7.8.4/docs/html/users_guide/ext-core.html. What's the best way to update this reference? Link to docs for `-ddump-simpl` instead?! Simply remove the reference?! – sjakobi Mar 07 '22 at 04:07
  • @sjakobi, indeed, that feature was removed a few years ago—the developers decided it wasn't worth the trouble to maintain. – dfeuer Mar 07 '22 at 16:31
25

A tip: If you don't care about type annotations and coercions use -ddump-simpl together with the -dsuppress-all option. The Core output should be much more readable.

nominolo
  • 5,085
  • 2
  • 25
  • 31
  • 5
    `-dsuppress-all` is really useful. You can also use `-dsuppress-coercions` if you only want to get rid of casts (useful when there are lots of newtypes around). – tibbe May 26 '11 at 18:39
  • `-dsuppress-coercions -dsuppress-type-applications` gets rid of the worst of the noise while keeping most of what you want. – dfeuer Mar 07 '22 at 16:55
8

Although not exactly the GHC Core language, as Don mentions the STG language is quite similar. I recently went through the exercise of proving type safety of the STG language + machine, and afterwards I found I could understand Core easily.

The text I used to learn STG is quite accessible: Implementing Lazy Functional Languages on Stock Hardware: The Spineless Tagless G-machine by Simon Peyton-Jones. Much of the paper is concerned with implementation details, but I recommend section 4 in particular as a top-to-bottom explanation of the STG language that gives motivations for some of the counter-intuitive design decisions and provides translations of familiar examples like map.

acfoltzer
  • 5,588
  • 31
  • 48
  • STG is a much lower level than Core. The compilation pipeline is: Haskell -> Core -> STG -> C-- -> Machine Code – Xwtek Oct 01 '19 at 14:58
3

"An External Representation for the GHC Core Language" is a document which can be found in the installation of ghc (share/doc/ghc/core.pdf) or on the internet.

Roman Cheplyaka
  • 37,738
  • 7
  • 72
  • 121