HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleuniplate-1.6.13Haskell2010

Data.Generics.UniplateStr

DEPRECATED: Use Data.Generics.Uniplate.Operations 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
  • 1 type
  • 1 class
  • 12 values
  • Packageuniplate-1.6.13
  • Exports14
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceUniplateStr.hs
classclass Uniplate on where
#

The standard Uniplate class, all operations require this.

Methods

  • uniplate :: UniplateType on

    The underlying method in the class.

    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)
Instances8Uniplate, …
  • Uniplate IntegerDefined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • Uniplate BoolDefined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • Uniplate CharDefined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • Uniplate DoubleDefined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • Uniplate FloatDefined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • Uniplate IntDefined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • Uniplate ()Defined in uniplate-1.6.13 · Data.Generics.PlateTypeable · orphan
  • (Data a, Typeable a) => Uniplate aDefined in uniplate-1.6.13 · Data.Generics.PlateData · orphan
typetype UniplateType on = on -> (Str on, Str on -> on)
#

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.

valueuniverse :: 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]
valuetransform :: 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
valuerewrite :: 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.

valuedescend :: Uniplate on => (on -> on) -> on -> on
#

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.

valuecontexts :: 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]
valueholes :: 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]
valuepara :: Uniplate on => (on -> [r] -> r) -> on -> r
#

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