Haskell's deriving construct is both wonderful and magical at the same time: it is wonderful, because with just a single line of code, you can obtain functionality such as(de)serialization, comparison and traversal functions for your newly defined datatype; it is magical because all of this happens behind the scenes, by code deeply built into the compiler.
But does it have to be magic? Since recently, the Glasgow Haskell Compiler (GHC) supports a feature that lets you define your own derivable functionality! And it is not difficult to use, either. All you have to do is to understand a little bit about how Haskell datatypes can be uniformly represented using a limited set of simple datatypes. Then you define a class plus instances for this limited set of types, and get all the rest for free.
So-called datatype-generic functions let you properly express your code in terms of specific, user-defined datatypes without paying a price: for your new datatypes, you can still use lots of pre-defined functionality, simply by deriving it.
YOU MAY ALSO LIKE:
- Deriving Via (SkillsCast recorded in October 2018)
- Haskell Fundamentals (4-Day Course) with Alejandro Serrano (Online Course on 19th - 22nd April 2021)
- FP in Kotlin with Arrow with Jorge Castillo (Online Course on 24th - 27th May 2021)
- F# eXchange 2021 (Online Conference on 20th - 21st October 2021)
- Haskell eXchange 2021 (Online Conference on 16th - 17th November 2021)
- Theorems for Free (SkillsCast recorded in November 2020)
- Comparing Strict and Lazy (SkillsCast recorded in November 2020)
Datatype-Generic Programming in Haskell
Andres Löh
Andres Löh is a Haskell consultant and co-owner of Well-Typed LLP. He is based in Regensburg, Germany. He started using Haskell in 1997, when being an undergraduate student of mathematics in Konstanz and has been an enthusiastic functional programmer ever since. Andres obtained a PhD in Computer Science from Utrecht University in 2004, on extending the Haskell language with capabilities for datatype-generic programming. After having been a university lecturer for several years, he joined Well-Typed in 2010.