|
Data.Generics.Uniplate.Operations |
|
|
|
|
Description |
Definitions of Uniplate and Biplate classes, along with all the standard operations.
Import this module directly only if you are defining new Uniplate operations, otherwise
import one of Data.Generics.Uniplate.Direct, Data.Generics.Uniplate.Typeable or
Data.Generics.Uniplate.Data.
Most functions have an example of a possible use for the function.
To illustate, I have used the Expr type as below:
data Expr = Val Int
| Neg Expr
| Add Expr Expr
|
|
Synopsis |
|
|
|
|
The Classes
|
|
|
The standard Uniplate class, all operations require this.
| | Methods | | The underlying method in the class.
Taking a value, the function should return all the immediate children
of the same type, and a function to replace them.
Given uniplate x = (cs, gen)
cs should be a Str on, constructed of Zero, One and Two,
containing all x's direct children of the same type as x. gen
should take a Str on with exactly the same structure as cs,
and generate a new element with the children replaced.
Example instance:
instance Uniplate Expr where
uniplate (Val i ) = (Zero , \Zero -> Val i )
uniplate (Neg a ) = (One a , \(One a) -> Neg a )
uniplate (Add a b) = (Two (One a) (One b), \(Two (One a) (One b)) -> Add a b)
| | descend :: (on -> on) -> on -> on | Source |
| Perform a transformation on all the immediate children, then combine them back.
This operation allows additional information to be passed downwards, and can be
used to provide a top-down transformation.
| | descendM :: Monad m => (on -> m on) -> on -> m on | Source |
| Monadic variant of descend
|
| | Instances | |
|
|
|
Children are defined as the top-most items of type to
starting at the root.
| | Methods | | Return all the top most children of type to within from.
If from == to then this function should return the root as the single
child.
| | descendBi :: (to -> to) -> from -> from | Source |
| | descendBiM :: Monad m => (to -> m to) -> from -> m from | Source |
|
| | Instances | |
|
|
Single Type Operations
|
|
Queries
|
|
|
Get all the children of a node, including itself and all children.
universe (Add (Val 1) (Neg (Val 2))) =
[Add (Val 1) (Neg (Val 2)), Val 1, Neg (Val 2), Val 2]
This method is often combined with a list comprehension, for example:
vals x = [i | Val i <- universe x]
|
|
|
Get the direct children of a node. Usually using universe is more appropriate.
|
|
Transformations
|
|
|
Transform every element in the tree, in a bottom-up manner.
For example, replacing negative literals with literals:
negLits = transform f
where f (Neg (Lit i)) = Lit (negate i)
f x = x
|
|
|
Monadic variant of transform
|
|
|
Rewrite by applying a rule everywhere you can. Ensures that the rule cannot
be applied anywhere in the result:
propRewrite r x = all (isNothing . r) (universe (rewrite r x))
Usually transform is more appropriate, but rewrite can give better
compositionality. Given two single transformations f and g, you can
construct f mplus g which performs both rewrites until a fixed point.
|
|
|
Monadic variant of rewrite
|
|
Others
|
|
|
Return all the contexts and holes.
propUniverse x = universe x == map fst (contexts x)
propId x = all (== x) [b a | (a,b) <- contexts x]
|
|
|
The one depth version of contexts
propChildren x = children x == map fst (holes x)
propId x = all (== x) [b a | (a,b) <- holes x]
|
|
|
Perform a fold-like computation on each value,
technically a paramorphism
|
|
Multiple Type Operations
|
|
Queries
|
|
|
|
|
Return the children of a type. If to == from then it returns the
original element (in contrast to children)
|
|
Transformations
|
|
transformBi :: Biplate from to => (to -> to) -> from -> from | Source |
|
|
|
|
|
|
|
|
Others
|
|
contextsBi :: Biplate from to => from -> [(to, to -> from)] | Source |
|
|
|
|
Produced by Haddock version 2.4.2 |