(home)

⚛️
GHC Core


The most popular Haskell implementation is the Glasgow Haskell Compiler (GHC). Core is one of it's intermediate representations.

👤 Simon Marlow and 👤 Simon Peyton Jones in The Architecture of Open Source Applications (Volume 2):

  • Haskell is a very large source language. The data type representing its syntax tree has literally hundreds of constructors.
  • In contrast Core is a tiny, principled, lambda calculus. It has extremely few syntactic forms, yet we can translate all of Haskell into Core.

...

All of GHC's analysis and optimisation passes work on Core. This is great: because Core is such a tiny language an optimisation has only a few cases to deal with. Although Core is small, it is extremely expressive—System F was, after all, originally developed as a foundational calculus for typed computation. When new language features are added to the source language (and that happens all the time) the changes are usually restricted to the front end; Core stays unchanged, and hence so does most of the compiler.

The code itself, docs and source:

data Expr b
  = Var   Id
  | Lit   Literal
  | App   (Expr b) (Arg b)
  | Lam   b (Expr b)
  | Let   (Bind b) (Expr b)
  | Case  (Expr b) b Type [Alt b]
  | Cast  (Expr b) CoercionR
  | Tick  CoreTickish (Expr b)
  | Type  Type
  | Coercion Coercion

Some referenced definitions (I've dropped some comments and deriving clauses from both code snippets):

type Arg b = Expr b

data Alt b
    = Alt AltCon [b] (Expr b)

data AltCon
  = DataAlt DataCon   --  ^ A plain data constructor: @case e of { Foo x -> ... }@.

  | LitAlt  Literal   -- ^ A literal: @case e of { 1 -> ... }@

  | DEFAULT           -- ^ Trivial alternative: @case e of { _ -> ... }@

Backlinks