liveBook · Manning

Deriving

As explained in Learn Haskell by Example

A deriving strategy is Haskell syntax that selects how type-class instances are automatically generated for a type.

Deriving adds type-class instances to a data or newtype declaration without writing each instance by hand. It is useful for the routine behavior that lets values be displayed, compared, or combined while the surrounding application stays focused on its own types and operations. section 5.2.3 section 15.1.2

For ordinary data types, `deriving (Show, Eq)` uses the default, or stock, behavior. A derived `Show` instance produces a representation close to Haskell source, which helps GHCi display values; it is not intended as a human-readable output format. A derived `Eq` instance compares constructors and then their fields structurally. section 5.2.3 section 15.1.2

Make map values visible in GHCi

A wrapper around an association list can derive `Show`, allowing inserted and deleted map values to be inspected directly. This works only when the wrapped key and value types themselves have `Show` instances. section 5.2.3

newtype AssocMap k v = AssocMap [(k, v)]
  deriving (Show)

The declaration adds `Show` to a newtype wrapper with the ordinary deriving syntax. section 5.2.3

Deriving strategies make the intended mechanism explicit. `deriving stock` selects the default behavior. `deriving newtype` reuses the underlying type’s instance instead: for a wrapper around `String`, a newtype-derived `Show` uses `String`’s display behavior and does not show the wrapper constructor. section 15.1.2

Newtype derivation avoids repetitive forwarding instances. A `Log` wrapping `[LogEntry]` can keep stock `Show` and `Eq`, while deriving `Semigroup` and `Monoid` from the list. Deriving those latter instances requires `DerivingStrategies` and `GeneralizedNewtypeDeriving`; without the latter, newtype derivation is limited to `Eq`, `Ord`, `Enum`, and `Bounded`. section 15.1.2

Listing 15.5 A type definition for a log that can be used with the WriterT transformer
newtype Log = Log [LogEntry]               #1
  deriving stock (Show, Eq)              #2
  deriving newtype (Semigroup, Monoid)  #3

This log wrapper separates stock instances for inspection and equality from newtype-derived instances for combining log entries. section 15.1.2

A practical first use is deriving `Eq` for an option type, then testing whether a parsed option occurs in a list. The mechanism can be used before its full details are needed: equality supports command-line behavior such as choosing a numbering function for empty lines. section 4.4.1

How Learn Haskell by Example covers it

How deep it goes
Explains stock and newtype strategies, required extensions, and instance reuse; only notes anyclass and via strategies.
Examples
Uses an association-list map, command-line options, runtime statistics, and a file-synchronization log wrapper.
What it assumes
Introduces basic equality use from zero, then relies on familiarity with data types, newtypes, and type classes.
What you can do after
Apply it via GHC language extensions to derive stock or underlying-type instances for application types.
How it connects
Connects type classes with Show, Eq, Semigroup, Monoid, newtype, algebraic data types, and WriterT.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.