Search Uniplate API

Uniplate

This library (formerly known as 'Play') provides a framework for generic traversals, and is intended to be used in a similar manner to the original Scrap Your Boilerplate paper.

An example of how Uniplate can reduce boilerplate:

data Expr  =  Add  Expr  Expr  | Val  Int
           |  Sub  Expr  Expr  | Var  String
           |  Mul  Expr  Expr  | Neg  Expr
           |  Div  Expr  Expr

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

The code for variables is not very nice, however, using Uniplate we can rewrite this as:

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

Related work

Downloads

Tags: haskell library phd popular released