Template Your Boilerplate: Using Template Haskell for Efficient Generic Programming

Michael D. Adams and Thomas M. DuBuisson

Status: Published


Generic programming allows the concise expression of algorithms that would otherwise require large amounts of handwritten code. A number of such systems have been developed over the years, but a common drawback of these systems is poor runtime performance relative to handwritten, non-generic code. Generic-programming systems vary significantly in this regard, but few consistently match the performance of handwritten code. This poses a dilemma for developers. Generic-programming systems offer concision but cost performance. Handwritten code offers performance but costs concision.

This paper explores the use of Template Haskell to achieve the best of both worlds. It presents a generic-programming system for Haskell that provides both the concision of other generic-programming systems and the efficiency of handwritten code. Our system gives the programmer a high-level, generic-programming interface, but uses Template Haskell to generate efficient, non-generic code that outperforms existing generic-programming systems for Haskell.

This paper presents the results of benchmarking our system against both handwritten code and several other generic-programming systems. In these benchmarks, our system matches the performance of handwritten code while other systems average anywhere from two to twenty times slower.


generic programming, scrap your boilerplate, template haskell


Michael D. Adams and Thomas M. DuBuisson. Template your boilerplate: Using Template Haskell for efficient generic programming. In Proceedings of the 2012 ACM SIGPLAN Haskell symposium, Haskell ’12, pages 13–24. ACM, New York, NY, USA, 2012. ISBN 978-1-4503-1574-6. doi: 10.1145/2364506.2364509.

BibTeX Entry

  author = {Adams, Michael D. and DuBuisson, Thomas M.},
  title = {Template Your Boilerplate: Using {T}emplate {H}askell for Efficient Generic Programming},
  booktitle = {Proceedings of the 2012 ACM SIGPLAN Haskell symposium},
  pages = {13--24},
  year = {2012},
  series = {Haskell '12},
  address = {New York, NY, USA},
  publisher = {ACM},
  isbn = {978-1-4503-1574-6},
  doi = {10.1145/2364506.2364509},

Copyright Notice

© ACM, 2012. This is the author’s version of the work. It is posted here by permission of ACM for your personal use. Not for redistribution. The definitive version was published in Proceedings of the 2012 ACM SIGPLAN Haskell symposium, (2012). http://doi.acm.org/10.1145/2364506.2364509.