Personal tools

Uniplate

From HaskellWiki

Revision as of 18:45, 23 June 2007 by BrettGiles (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

I am pleased to announce Uniplate (formerly known as Play), a library for boilerplate removal requiring only Haskell 98 (for normal use) and optionally multi-parameter type classes (for more advanced features). This library has been tested with Yhc, Hugs and GHC.

The Uniplate library can do some of the same things as [[Scrap Your Boilerplate]] (SYB), and has functions that can be used in a similar manner to everywhere and everything.

Contents

1 Links

The manual contains a basic overview of some bits of the library, the paper goes into more detail, but is intended to still be a readable introduction to the library.

2 Example

Given an
Int
expression data type:
data Expr  =  Add  Expr  Expr  | Val  Int
           |  Sub  Expr  Expr  | Var  String
           |  Mul  Expr  Expr  | Neg  Expr
           |  Div  Expr  Expr

We can extract all the variables using the function:

variables :: Expr -> [String]
variables (Var  x  ) = [x]
variables (Val  x  ) = []
variables (Neg  x  ) = variables x
variables (Add  x y) = variables x ++ variables y
variables (Sub  x y) = variables x ++ variables y
variables (Mul  x y) = variables x ++ variables y
variables (Div  x y) = variables x ++ variables y

Or we can use the Uniplate library:

variables :: Expr -> [String]
variables x = [y | Var y <- universe x]

3 Thanks to

Colin Runciman, Bjorn Bringert, Jules Bean, Eric Mertens, Tom Shackell and Stefan O'Rear for various help and feedback.

4 Trailer

The Uniplate library has been used in Yhc, Catch, Hoogle and other projects. Please direct any follow-up discussion to haskell-cafe.

Thanks Neil