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