Deriving Instances

You’ve seen how type classes allow you to create abstractions in your programs. A problem with type classes that you might have already run into is that most of the time you’re doing a lot of work to create instances, but the code is uninteresting and often redundant. As an example, let’s imagine a simple record to hold some customer info:

 data​ ​Customer​ = ​Customer
  { name :: ​String
  , mail :: ​String
  , email :: ​String
  }

For our application, we’d like to provide implementations for three common type classes, Show, which will allow us to convert a customer record to a string, Eq, which will let us test equality between two records, and Ord, which will allow us to sort our records. We can manually implement ...

Get Effective Haskell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.