Copyright | Copyright 2017 Awake Security |
---|---|
License | Apache-2.0 |
Maintainer | opensource@awakesecurity.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
A typeclass for AST nodes that are annotated with a polymorphic field, which provides a canonical lens into that field.
Since: 0.1.0
- class Functor ty => Annotated ty where
- annotation :: Annotated ty => Lens' (ty ann) ann
Documentation
class Functor ty => Annotated ty where Source #
If you have some type that represents an AST node, it is often useful to add a polymorphic "annotation field" to it, which is used for things like source positions.
Specifically, suppose we have the following AST node type:
data Foo = Foo { _fooBar :: !Bar, _fooBaz :: !Baz } deriving (…)
Then an annotation field is added by the following process:
- Add an extra (final) type parameter
ann
to the type. - Add an extra field
_fooAnn :: !ann
. - Derive instances of
Functor
,Foldable
, andTraversable
. - If the type is recursive, add a
Plated
instance. See Language.Ninja.AST.Expr for a complete example of this. - Write an
Annotated
instance with the canonical lens given by the_fooAnn
field. There are plenty of examples around this library.
The end result then looks like:
data Foo ann = Foo { _fooAnn :: !ann , _fooBar :: !Bar , _fooBaz :: !Baz } deriving (…, Functor, Foldable, Traversable) instance Annotated Foo where annotation' = …
Since: 0.1.0
annotation' :: (ann -> ann') -> Lens (ty ann) (ty ann') ann ann' Source #
Given a function that is used when fmap
ing any subterms, return a lens
into the "annotation" field.
When writing an instance, keep in mind that
should
just be the typical definition for a lens into the annotation field.annotation'
id
It should also be true that for any f :: B -> C
and g :: A -> B
,
annotation' (f . g) == annotation' f . annotation' g
Since: 0.1.0
Annotated Expr Source # | The usual definition for Since: 0.1.0 |
Annotated Rule Source # | The usual definition for Since: 0.1.0 |
Annotated Deps Source # | The usual definition for Since: 0.1.0 |
Annotated Build Source # | The usual definition for Since: 0.1.0 |
Annotated Ninja Source # | The usual definition for Since: 0.1.0 |
Annotated LBuild Source # | The usual definition for Since: 0.1.0 |
Annotated LName Source # | The usual definition for Since: 0.1.0 |
Annotated Lexeme Source # | The usual definition for Since: 0.1.0 |
annotation :: Annotated ty => Lens' (ty ann) ann Source #
This is just shorthand for
.annotation'
id
Since: 0.1.0