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
