| |||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||
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 | |||||||||||||||||||||||||||||||||||||||||||||
class Uniplate on where | |||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||
class Uniplate to => Biplate from to where | |||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||
Single Type Operations | |||||||||||||||||||||||||||||||||||||||||||||
Queries | |||||||||||||||||||||||||||||||||||||||||||||
universe :: Uniplate on => on -> [on] | |||||||||||||||||||||||||||||||||||||||||||||
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] | |||||||||||||||||||||||||||||||||||||||||||||
children :: Uniplate on => on -> [on] | |||||||||||||||||||||||||||||||||||||||||||||
Get the direct children of a node. Usually using universe is more appropriate. | |||||||||||||||||||||||||||||||||||||||||||||
Transformations | |||||||||||||||||||||||||||||||||||||||||||||
transform :: Uniplate on => (on -> on) -> on -> on | |||||||||||||||||||||||||||||||||||||||||||||
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 | |||||||||||||||||||||||||||||||||||||||||||||
transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on | |||||||||||||||||||||||||||||||||||||||||||||
Monadic variant of transform | |||||||||||||||||||||||||||||||||||||||||||||
rewrite :: Uniplate on => (on -> Maybe on) -> on -> on | |||||||||||||||||||||||||||||||||||||||||||||
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. | |||||||||||||||||||||||||||||||||||||||||||||
rewriteM :: (Monad m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on | |||||||||||||||||||||||||||||||||||||||||||||
Monadic variant of rewrite | |||||||||||||||||||||||||||||||||||||||||||||
Others | |||||||||||||||||||||||||||||||||||||||||||||
contexts :: Uniplate on => on -> [(on, on -> on)] | |||||||||||||||||||||||||||||||||||||||||||||
Return all the contexts and holes. propUniverse x = universe x == map fst (contexts x) propId x = all (== x) [b a | (a,b) <- contexts x] | |||||||||||||||||||||||||||||||||||||||||||||
holes :: Uniplate on => on -> [(on, on -> on)] | |||||||||||||||||||||||||||||||||||||||||||||
The one depth version of contexts propChildren x = children x == map fst (holes x) propId x = all (== x) [b a | (a,b) <- holes x] | |||||||||||||||||||||||||||||||||||||||||||||
para :: Uniplate on => (on -> [r] -> r) -> on -> r | |||||||||||||||||||||||||||||||||||||||||||||
Perform a fold-like computation on each value, technically a paramorphism | |||||||||||||||||||||||||||||||||||||||||||||
Multiple Type Operations | |||||||||||||||||||||||||||||||||||||||||||||
Queries | |||||||||||||||||||||||||||||||||||||||||||||
universeBi :: Biplate from to => from -> [to] | |||||||||||||||||||||||||||||||||||||||||||||
childrenBi :: Biplate from to => from -> [to] | |||||||||||||||||||||||||||||||||||||||||||||
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 | |||||||||||||||||||||||||||||||||||||||||||||
transformBiM :: (Monad m, Biplate from to) => (to -> m to) -> from -> m from | |||||||||||||||||||||||||||||||||||||||||||||
rewriteBi :: Biplate from to => (to -> Maybe to) -> from -> from | |||||||||||||||||||||||||||||||||||||||||||||
rewriteBiM :: (Monad m, Biplate from to) => (to -> m (Maybe to)) -> from -> m from | |||||||||||||||||||||||||||||||||||||||||||||
Others | |||||||||||||||||||||||||||||||||||||||||||||
contextsBi :: Biplate from to => from -> [(to, to -> from)] | |||||||||||||||||||||||||||||||||||||||||||||
holesBi :: Biplate from to => from -> [(to, to -> from)] | |||||||||||||||||||||||||||||||||||||||||||||
Produced by Haddock version 2.6.0 |