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]
Downloads
- Released version
- Related blog posts
- Uniform Boilerplate and List Processing - from Haskell Workshop 2007 (abstract)(hide abstract)
Generic traversals over recursive data structures are often referred
to as boilerplate code. The definitions of functions involving such
traversals may repeat very similar patterns, but with variations for
different data types and different functionality. Libraries of operations
abstracting away boilerplate code typically rely on elaborate
types to make operations generic. The motivating observation for
this paper is that most traversals have value-specific behaviour for
just one type. We present the design of a new library exploiting
this assumption. Our library allows concise expression of traversals
with competitive performance.
(bibtex)(hide bibtex)@inproceedings{mitchell:uniplate_2007_9_30
,title = "Uniform Boilerplate and List Processing"
,author = "Neil Mitchell and Colin Runciman"
,year = "2007"
,month = "September"
,day = "30"
,booktitle = "Haskell '07: Proceedings of the ACM SIGPLAN workshop on Haskell workshop"
,pages = "49--60
,location = "Freiburg, Germany"
,doi = "http://doi.acm.org/10.1145/1291201.1291208"
,publisher = "ACM"
,isbn = "978-1-59593-674-5"
,url = "\verb'http://www-users.cs.york.ac.uk/~ndm/downloads/paper-uniform_boilerplate_and_list_processing-30_sep_2007.pdf'"
}
- Playing with Haskell Data - from PLASMA (bibtex)(hide bibtex)
@misc{mitchell:uniplate_2007_1_18
,title = "Playing with {Haskell} Data"
,author = "Neil Mitchell"
,year = "2007"
,month = "January"
,day = "18"
,note = "Presentation from PLASMA"
,url = "\verb'http://www-users.cs.york.ac.uk/~ndm/downloads/slides-playing_with_haskell_data-18_jan_2007.pdf'"
}
Tags: haskell library phd popular released