Abstract
Haskell's deriving mechanism supports the automatic generation of instances for a number of functions. The Haskell 98 Report only specifies how to generate instances for the Eq, Ord, Enum, Bounded, Show, and Read classes. The description of how to generate instances is largely informal. The generation of instances imposes restrictions on the shape of datatypes, depending on the particular class to derive. As a consequence, the portability of instances across different compilers is not guaranteed.
We propose a new approach to Haskell's deriving mechanism, which allows users to specify how to derive arbitrary class instances using standard datatype-generic programming techniques. Generic functions, including the methods from six standard Haskell 98 derivable classes, can be specified entirely within Haskell 98 plus multi-parameter type classes, making them lightweight and portable. We can also express Functor, Typeable, and many other derivable classes with our technique. We implemented our deriving mechanism together with many new derivable classes in the Utrecht Haskell Compiler.
Supplemental Material
- }}Artem Alimarine and Rinus Plasmeijer. A Generic Programming Extension for Clean. In IFL'01, pages 168--185. Springer-Verlag, 2001. Google Scholar
Digital Library
- }}Roland Backhouse, Patrik Jansson, Johan Jeuring, and Lambert Meertens. Generic programming - an introduction. In AFP'98, volume 1608 of LNCS, pages 28--115. Springer, 1999.Google Scholar
- }}Manuel M. T. Chakravarty, Gabriel C. Ditu, and Roman Leshchinskiy. Instant generics: Fast and easy, 2009. Draft version.Google Scholar
- }}Atze Dijkstra, Jeroen Fokker, and S. Doaitse Swierstra. The architecture of the Utrecht Haskell compiler. In Haskell'09, pages 93--104. ACM, 2009. Google Scholar
Digital Library
- }}Erik Hesselink. Generic programming with fixed points for parametrized datatypes. Master's thesis, Utrecht University, 2009.Google Scholar
- }}Ralf Hinze. Polytypic values possess polykinded types. SCP, 43(2-3):129--159, 2002.Google Scholar
- }}Ralf Hinze and Andres Löh. Generic programming in 3D. SCP, 74(8): 590--628, 2009. Google Scholar
Digital Library
- }}Ralf Hinze and Simon Peyton Jones. Derivable type classes. Electronic Notes in Theoretical Computer Science, 41(1):5--35, 2001.Google Scholar
Cross Ref
- }}Ralf Hinze, Johan Jeuring, and Andres Löh. Type-indexed data types. In MPC'02, volume 2386 of LNCS, pages 148--174. Springer, 2002. Google Scholar
Digital Library
- }}Ralf Hinze, Johan Jeuring, and Andres Löh. Comparing approches to generic programming in Haskell. In Datatype-Generic Programming, volume 4719 of LNCS, pages 72--149. Springer, 2007. Google Scholar
Digital Library
- }}Gérard Huet. The zipper. JFP, 7(5):549--554, 1997. Google Scholar
Digital Library
- }}Patrik Jansson and Johan Jeuring. PolyP - a polytypic programming language extension. In POPL'97, pages 470--482. ACM, 1997. Google Scholar
Digital Library
- }}Mark Jones. Type classes with functional dependencies. In ESOP'00, volume 1782 of LNCS, pages 230--244. Springer, 2000. Google Scholar
Digital Library
- }}Ralf Lämmel and Simon Peyton Jones. Scrap your boilerplate: a practical approach to generic programming. In TLDI'03, pages 26--37, 2003.Google Scholar
Digital Library
- }}Ralf Lämmel and Simon Peyton Jones. Scrap more boilerplate: reflection, zips, and generalised casts. In ICFP'04, pages 244--255. ACM, 2004. Google Scholar
Digital Library
- }}Andres Löh. Exploring Generic Haskell. PhD thesis, Utrecht University, 2004.Google Scholar
- }}José Pedro Magalhães, Stefan Holdermans, Johan Jeuring, and Andres Löh. Optimizing generics is easy! In PEPM'10, pages 33--42. ACM, 2010. Google Scholar
Digital Library
- }}Thomas van Noort, Alexey Rodriguez Yakushev, Stefan Holdermans, Johan Jeuring, and Bastiaan Heeren. A lightweight approach to datatypegeneric rewriting. In WGP'08, pages 13--24. ACM, 2008. Google Scholar
Digital Library
- }}Bruno C. d. S. Oliveira, Ralf Hinze, and Andres Löh. Extensible and modular Bruno C. d. S. Oliveira, Ralf Hinze, and Andres Löh. Extensible and modular generics for the masses. In TFP'06, pages 199--216. Intellect, 2007.Google Scholar
- }}Simon Peyton Jones et al. Haskell 98, Language and Libraries. The Revised Report. Cambridge University Press, 2003. A special issue of JFP.Google Scholar
- }}Alexey Rodriguez Yakushev, Johan Jeuring, Patrik Jansson, Alex Gerdes, Oleg Kiselyov, and Bruno C. d. S. Oliveira. Comparing libraries for generic programming in Haskell. In Haskell'08, pages 111--122. ACM, 2008. Google Scholar
Digital Library
- }}Tom Schrijvers, Simon Peyton Jones, Manuel M. T. Chakravarty, and Martin Sulzmann. Type checking with open type functions. In ICFP'08, pages 51--62. ACM, 2008. Google Scholar
Digital Library
- }}Tim Sheard and Simon Peyton Jones. Template metaprogramming for Haskell. In Haskell'02, pages 1--16. ACM, 2002. Google Scholar
Digital Library
- }}Malcom Wallace et al. Derived instances - Haskell Prime. http://hackage.haskell.org/trac/haskell-prime/wiki/derivedInstances, April 2007. {Online; accessed 07-June-2010}.Google Scholar
- }}Noel Winstanley and John Meacham. DrIFT user guide. http://repetae.net/computer/haskell/DrIFT/drift.html, February 2008. {Online; accessed 07-June-2010}.Google Scholar
Index Terms
A generic deriving mechanism for Haskell
Recommendations
A generic deriving mechanism for Haskell
Haskell '10: Proceedings of the third ACM Haskell symposium on HaskellHaskell's deriving mechanism supports the automatic generation of instances for a number of functions. The Haskell 98 Report only specifies how to generate instances for the Eq, Ord, Enum, Bounded, Show, and Read classes. The description of how to ...
Type classes in Haskell
This article defines a set of type inference rules for resolving overloading introduced by type classes, as used in the functional programming language Haskell. Programs including type classes are transformed into ones which may be typed by standard ...
Polymorphic variants in Haskell
Haskell '06: Proceedings of the 2006 ACM SIGPLAN workshop on HaskellIn languages that support polymorphic variants, a single variant value can be passed to many contexts that accept different sets of constructors. Polymorphic variants can be used in order to introduce extensible algebraic datatypes into functional ...







Comments