Chapter�9.�Extending and using GHC as a Library (original) (raw)

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:

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") #-}