RECOMMENDATION Use Data.Generics.UniplateStr instead.
This is the main Uniplate module, which defines all the essential operations in a Haskell 98 compatible manner.
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
- type UniplateType on = on -> ([on], [on] -> on)
- class Uniplate on where
- universe :: Uniplate on => on -> [on]
- children :: Uniplate on => on -> [on]
- transform :: Uniplate on => (on -> on) -> on -> on
- transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on
- rewrite :: Uniplate on => (on -> Maybe on) -> on -> on
- rewriteM :: (Monad m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on
- descend :: Uniplate on => (on -> on) -> on -> on
- descendM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on
- contexts :: Uniplate on => on -> [(on, on -> on)]
- holes :: Uniplate on => on -> [(on, on -> on)]
- para :: Uniplate on => (on -> [r] -> r) -> on -> r
The type of replacing all the children of a node
Taking a value, the function should return all the immediate children of the same type, and a function to replace them.
The standard Uniplate class, all operations require this
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 = [Val i | i <- universe x]
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
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))
transform is more appropriate, but
rewrite can give better
compositionality. Given two single transformations
g, you can
f which performs both rewrites until a fixed point.
Monadic variant of
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.
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
propChildren x = children x == map fst (holes x) propId x = all (== x) [b a | (a,b) <- holes x]