Chapter 9. Extending and using GHC as a Library

Table of Contents

9.1. Source annotations
9.1.1. Annotating values
9.1.2. Annotating types
9.1.3. Annotating modules
9.2. Using GHC as a Library
9.3. Compiler Plugins
9.3.1. Using compiler plugins
9.3.2. Writing compiler plugins
9.3.2.1. CoreToDo in more detail
9.3.2.2. Manipulating bindings
9.3.2.3. Using Annotations

GHC exposes its internal APIs to users through the built-in ghc package. It allows you to write programs that leverage GHC's entire compilation driver, in order to analyze or compile Haskell code programmatically. Furthermore, GHC gives users the ability to load compiler plugins during compilation - modules which are allowed to view and change GHC's internal intermediate representation, Core. Plugins are suitable for things like experimental optimizations or analysis, and offer a lower barrier of entry to compiler development for many common cases.

Furthermore, GHC offers a lightweight annotation mechanism that you can use to annotate your source code with metadata, which you can later inspect with either the compiler API or a compiler plugin.

9.1. Source annotations

Annotations are small pragmas that allow you to attach data to identifiers in source code, which are persisted when compiled. These pieces of data can then inspected and utilized when using GHC as a library or writing a compiler plugin.

9.1.1. Annotating values

Any expression that has both Typeable and Data instances may be attached to a top-level value binding using an ANN pragma. In particular, this means you can use ANN to annotate data constructors (e.g. Just) as well as normal values (e.g. take). By way of example, to annotate the function foo with the annotation Just "Hello" you would do this:

{-# ANN foo (Just "Hello") #-}
foo = ...

A number of restrictions apply to use of annotations:

  • The binder being annotated must be at the top level (i.e. no nested binders)

  • The binder being annotated must be declared in the current module

  • The expression you are annotating with must have a type with Typeable and Data instances

  • The Template Haskell staging restrictions apply to the expression being annotated with, so for example you cannot run a function from the module being compiled.

    To be precise, the annotation {-# ANN x e #-} is well staged if and only if $(e) would be (disregarding the usual type restrictions of the splice syntax, and the usual restriction on splicing inside a splice - $([|1|]) is fine as an annotation, albeit redundant).

If you feel strongly that any of these restrictions are too onerous, please give the GHC team a shout.

However, apart from these restrictions, many things are allowed, including expressions which are not fully evaluated! Annotation expressions will be evaluated by the compiler just like Template Haskell splices are. So, this annotation is fine:

{-# ANN f SillyAnnotation { foo = (id 10) + $([| 20 |]), bar = 'f } #-}
f = ...

9.1.2. Annotating types

You can annotate types with the ANN pragma by using the type keyword. For example:

{-# ANN type Foo (Just "A `Maybe String' annotation") #-}
data Foo = ...

9.1.3. Annotating modules

You can annotate modules with the ANN pragma by using the module keyword. For example:

{-# ANN module (Just "A `Maybe String' annotation") #-}