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 the type classes:

​  ​ instance ​ ​ Eq ​ ​ Customer ​ ​ where ​
​  (==)
​  (​ Customer ​ name mail email)
​  (​ Customer ​ name' mail' email') =
​  name == name' &&
​  mail == mail' &&
​  email == email'
​  
​  ​ instance ​ ​ Ord ​ ​ Customer ​ ​ where ​
​  compare
​  (​ Customer ​ name mail email)
​  (​ Customer ​ name' mail' email') =
​  compare name name'
​  <> compare mail mail'
​  <> compare email email'
​  
​  ​ instance ​ ​ Show ​ ​ Customer ​ ​ where ​
​  show (​ Customer ​ name mail email) =
​  ​ "Customer {name = " ​ <> show name <> ​ ", " ​
​  <> ​ "mail = " ​ <> show mail <> ​ ", " ​
​  <> ​ "email = " ​ <> show email <> ​ "}" ​

Our manual implementations of the type classes are straightforward, but it can become pretty tedious. Thankfully, for cases like this where the implementation of a type class is fairly straightforward, Haskell has a mechanism to allow us to get an instance of a type class for free, using the deriving keyword.

In most cases, you’ll derive an instance of a type class using the deriving keyword at the end of your data or newtype definition. For instance, in our Customer example we could instead have used automatically derived instances that would behave like our manual implementations:

​  ​ data ​ ​ Customer ​ = ​ Customer ​
​  { name :: ​ String ​
​  , mail :: ​ String ​
​  , email :: ​ String ​
​  } ​ deriving ​ (​ Eq ​, ​ Show ​, ​ Ord ​)

Deriving a type class automatically is a very powerful technique, but it only works in certain circumstances. The first requirement is that the type class has to be derivable. In standards compliant Haskell, only a few type classes are derivable:

  • Eq for types that can be compared for equality
  • Ord for types that have a total ordering
  • Ix for types that support indexing into a range
  • Show for types that can be converted into strings
  • Read for types that can be parsed from strings
  • Enum for types that can be converted to and from integers
  • Bounded for types with finite upper and lower bounds

There are several GHC extensions that give you more options for deriving instances automatically. You’ll learn about a couple of those extensions in this section, and others throughout the book.

Not every derivable class can be derived for every data type or newtype. One of the most common requirements when deriving a type class for a record is that all of the types within the record must also have instances of the type class you are trying to derive. For example, consider if instead of a String , we’d used a newtype wrapper for storing a customer’s name in our earlier example:

​  ​ module ​ ​ DerivingExample ​ ​ where ​
​  
​  ​ newtype ​ ​ Name ​ = ​ Name ​ ​ String ​
​  
​  ​ data ​ ​ Customer ​ = ​ Customer ​
​  { name :: ​ Name ​
​  , mail :: ​ String ​
​  , email :: ​ String ​
​  } ​ deriving ​ (​ Eq ​, ​ Show ​, ​ Ord ​)

If we try to load this module, you’ll see that the compiler will helpfully point out that we can’t derive Eq , Show , or Ord instances for our type classes because the Name type doesn’t have instances for those types:

​  λ :l DerivingExample.hs
​  DerivingExample.hs:9:15: error:
​  * No instance for (Eq Name)
​  arising from the first field of `Customer' (type `Name')
​  Possible fix:
​  use a standalone 'deriving instance' declaration,
​  so you can specify the instance context yourself
​  * When deriving the instance for (Eq Customer)
​  |
​  9 | } deriving (Eq, Show, Ord)
​  | ^^
​  
​  DerivingExample.hs:9:19: error:
​  * No instance for (Show Name)
​  arising from the first field of `Customer' (type `Name')
​  Possible fix:
​  use a standalone 'deriving instance' declaration,
​  so you can specify the instance context yourself
​  * When deriving the instance for (Show Customer)
​  |
​  9 | } deriving (Eq, Show, Ord)
​  | ^^^^
​  
​  DerivingExample.hs:9:25: error:
​  * No instance for (Ord Name)
​  arising from the first field of `Customer' (type `Name')
​  Possible fix:
​  use a standalone 'deriving instance' declaration,
​  so you can specify the instance context yourself
​  * When deriving the instance for (Ord Customer)
​  |
​  9 | } deriving (Eq, Show, Ord)
​  | ^^^
​  Failed, no modules loaded.

Unfortunately, in this case the error message that the compiler gives us is a little bit more general than we really want. The compiler in this case is recommending that you enable the StandaloneDeriving language extension. You’ll learn a bit more about this extension later in the book, but for the moment let’s return to addressing the error at hand. Looking past the compiler’s suggested fix, you can use what you know about deriving to fix the issue at hand by providing instances of the Eq , Show , and Ord type classes for Name :

​  ​ newtype ​ ​ Name ​ = ​ Name ​ ​ String ​ ​ deriving ​ (​ Eq ​, ​ Show ​, ​ Ord ​)
StandaloneDeriving

| images/aside-icons/info.png | The StandaloneDeriving extension has been available since GHC 6.8.1. This extension is enabled by default in GHC2021 but you’ll need to enable it manually if you are using Haskell2010 . This is a safe extension that shouldn’t cause any problems with existing code. |

With this change, you can now load your code. Next, let’s look at some language extensions GHC provides to extend the deriving mechanism to make it.