annotated-fix-0.1.0.0: A fixpoint of a functor that can be annotated

Safe HaskellSafe
LanguageHaskell2010

Data.Functor.Annotated

Contents

Description

This modules exposes a type representing the fixpoint of a functor type, paired with an annotation. It exports the recursion-schemes instances for this type and a few specific utility functions.

Motivation

Suppose one has an Abstract Syntax Tree (AST) for the lambda calculus:

data Term = Var String | App Term Term | Lam String Term

Such a type can easily be used with the recursion-schemes library, but it is not always convenient to use this type. It is often the case that one wants to add extra informations to every node of an AST, such as location or type information. In this case, instead of adding those informations as an extra-field to all data constructors, one could prefer to represent terms as a record of a descriptor and the information present at every node, like so:

data TermDesc = Var String | App Term Term | Lam String Term
data Term = Term { termDesc :: TermDesc
                 , termTyp  :: Typ
                 , termLoc  :: Loc
                 , ...
                 }

This library implements this general pattern through the Annot type, representing the fixpoint of a functor type, paired with some annotation. In this setting, the above example would be represented like so:

data TermDesc r = Var String | App r r | Lam String r

data TermAnn = TermAnn { termTyp :: Typ
                       , termLoc :: Loc
                       , ...
                       }

type Term = Annot TermDesc TermAnn
Synopsis

Documentation

data Annot f a Source #

The fixpoint type of functor f, with some annotation a.

Constructors

Annot a (f (Annot f a)) 
Instances
Functor f => Functor (Annot f) Source # 
Instance details

Defined in Data.Functor.Annotated

Methods

fmap :: (a -> b) -> Annot f a -> Annot f b #

(<$) :: a -> Annot f b -> Annot f a #

Foldable f => Foldable (Annot f) Source # 
Instance details

Defined in Data.Functor.Annotated

Methods

fold :: Monoid m => Annot f m -> m #

foldMap :: Monoid m => (a -> m) -> Annot f a -> m #

foldr :: (a -> b -> b) -> b -> Annot f a -> b #

foldr' :: (a -> b -> b) -> b -> Annot f a -> b #

foldl :: (b -> a -> b) -> b -> Annot f a -> b #

foldl' :: (b -> a -> b) -> b -> Annot f a -> b #

foldr1 :: (a -> a -> a) -> Annot f a -> a #

foldl1 :: (a -> a -> a) -> Annot f a -> a #

toList :: Annot f a -> [a] #

null :: Annot f a -> Bool #

length :: Annot f a -> Int #

elem :: Eq a => a -> Annot f a -> Bool #

maximum :: Ord a => Annot f a -> a #

minimum :: Ord a => Annot f a -> a #

sum :: Num a => Annot f a -> a #

product :: Num a => Annot f a -> a #

Traversable f => Traversable (Annot f) Source # 
Instance details

Defined in Data.Functor.Annotated

Methods

traverse :: Applicative f0 => (a -> f0 b) -> Annot f a -> f0 (Annot f b) #

sequenceA :: Applicative f0 => Annot f (f0 a) -> f0 (Annot f a) #

mapM :: Monad m => (a -> m b) -> Annot f a -> m (Annot f b) #

sequence :: Monad m => Annot f (m a) -> m (Annot f a) #

Generic (Annot f a) Source # 
Instance details

Defined in Data.Functor.Annotated

Associated Types

type Rep (Annot f a) :: Type -> Type #

Methods

from :: Annot f a -> Rep (Annot f a) x #

to :: Rep (Annot f a) x -> Annot f a #

Functor f => Recursive (Annot f a) Source # 
Instance details

Defined in Data.Functor.Annotated

Methods

project :: Annot f a -> Base (Annot f a) (Annot f a) #

cata :: (Base (Annot f a) a0 -> a0) -> Annot f a -> a0 #

para :: (Base (Annot f a) (Annot f a, a0) -> a0) -> Annot f a -> a0 #

gpara :: (Corecursive (Annot f a), Comonad w) => (forall b. Base (Annot f a) (w b) -> w (Base (Annot f a) b)) -> (Base (Annot f a) (EnvT (Annot f a) w a0) -> a0) -> Annot f a -> a0 #

prepro :: Corecursive (Annot f a) => (forall b. Base (Annot f a) b -> Base (Annot f a) b) -> (Base (Annot f a) a0 -> a0) -> Annot f a -> a0 #

gprepro :: (Corecursive (Annot f a), Comonad w) => (forall b. Base (Annot f a) (w b) -> w (Base (Annot f a) b)) -> (forall c. Base (Annot f a) c -> Base (Annot f a) c) -> (Base (Annot f a) (w a0) -> a0) -> Annot f a -> a0 #

type Rep (Annot f a) Source # 
Instance details

Defined in Data.Functor.Annotated

type Rep (Annot f a) = D1 (MetaData "Annot" "Data.Functor.Annotated" "annotated-fix-0.1.0.0-1iukNPhNY3oIl0on2YwNeK" False) (C1 (MetaCons "Annot" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (f (Annot f a)))))
type Base (Annot f a) Source # 
Instance details

Defined in Data.Functor.Annotated

type Base (Annot f a) = f

Annotating and deannotating

strip :: Annot f a -> f (Annot f a) Source #

Strips one level of annotation

annotation :: Annot f a -> a Source #

Extracts the annotation

annotate :: Functor f => (f (Annot f a) -> a) -> Fix f -> Annot f a Source #

Annotates all the node of a functor's fixpoint value

annotateRec :: Recursive t => (Base t (Annot (Base t) a) -> a) -> t -> Annot (Base t) a Source #

Generalized version of annotate for instances of Recursive

deannotate :: Functor f => Annot f a -> Fix f Source #

Strips all annotations

deannotateCorec :: Corecursive t => Annot (Base t) a -> t Source #

Generalized version of deannotate for instances of Corecursive

Specific schemes

cataAnn :: Functor f => (a -> f b -> b) -> Annot f a -> b Source #

catamorphism with access to the current annotation

paraAnn :: Functor f => (a -> f (Annot f a, b) -> b) -> Annot f a -> b Source #

paramorphism with access to the current annotation