uniplate-1.2: Uniform type generic traversals.




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


The Class

type UniplateType on = on -> (Str on, Str on -> on)Source

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.

class Uniplate on whereSource

The standard Uniplate class, all operations require this. Must define one of uniplate or uniplateStr, uniplateStr is recommended


uniplate :: UniplateType onSource

The underlying method in the class

 uniplateStr (Add (Val 1) (Neg (Val 2)))
    = (Two (One (Val 1)) (One (Neg (Val 2)))], \(Two (One a) (One b)) -> Add a b)
 uniplateStr (Val 1)
    = (Zero                                  , \Zero                  -> Val 1  )

uniplateList :: Uniplate on => on -> ([on], [on] -> on)Source

Compatibility method, for direct users of the uniplate function

The Operations


universe :: Uniplate on => on -> [on]Source

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]

children :: Uniplate on => on -> [on]Source

Get the direct children of a node. Usually using universe is more appropriate.

children = fst . uniplate


transform :: Uniplate on => (on -> on) -> on -> onSource

Transform every element in the tree, in a bottom-up manner.

For example, replacing negative literals with literals:

 negLits = trasform f
    where f (Neg (Lit i)) = Lit (negate i)
          f x = x

transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m onSource

Monadic variant of transform

rewrite :: Uniplate on => (on -> Maybe on) -> on -> onSource

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 onSource

Monadic variant of rewrite

descend :: Uniplate on => (on -> on) -> on -> onSource

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, Uniplate on) => (on -> m on) -> on -> m onSource

Monadic variant of descend


contexts :: Uniplate on => on -> [(on, on -> on)]Source

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)]Source

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 -> rSource

Perform a fold-like computation on each value, technically a paramorphism