Illegal deriving item [GHC-11913]

After a datatype declaration, a deriving statement allows programmers to specify type class instances that the compiler should automatically generate. For instance, deriving (Eq, Show) will result in a datatype that can be compared for equality and converted to a string with no further code necessary.

For this to make sense, deriving needs to be provided with valid type class instances. Just as it doesn’t make sense to treat a number as a function, it doesn’t make sense to treat ordinary types as instances.

Examples

Deriving Int

Error Message

DerivingInt.hs:4:12: error: [GHC-11913]
    • Illegal deriving item ‘Int’
    • In the data declaration for ‘Example’
  |
4 |   deriving Int
  |            ^^^

Explanation

In this example, the programmer attempts to deriving Int, which is not a type class.

DerivingInt.hs
Before
module DerivingInt where

data Example = Example
  deriving Int
After
module DerivingInt where

data Example = Example
  deriving Eq
Lowercase type class name

Error message

LowerCase.hs:4:13: error: [GHC-11913]
    • Illegal deriving item ‘show’
    • In the data declaration for ‘Example’
  |
4 |   deriving (show)
  |             ^^^^

Explanation

Type classes in Haskell begin with an uppercase letter. Writing a lowercase name results in a type variable, rather than a type class, which is not a class that can be derived. Fix the situation by capitalizing the class name.

LowerCase.hs
Before
module LowerCase where

data Example = Example
  deriving (show)
After
module LowerCase where

data Example = Example
  deriving (Show)