concept GeneralizedNewtypeDeriving in category haskell

appears as: GeneralizedNewtypeDeriving
Haskell in Depth

This is an excerpt from Manning's book Haskell in Depth.

The runApp field is the monad stack itself. The deriving clause generates all the functionality required. Deriving is implemented by the GeneralizedNewtypeDeriving GHC extension, it must be enabled to compile this code. The idea behind it is rather simple. For example, to provide the tell method for the MyApp s monad the deriving machinery generates a method body with something like lift . tell (one level down along the monad stack to get to the Writer monad). The method body for get becomes lift . lift . get to reach the State monad. We’ll discuss other details on instance deriving later in this book.

Note that the GeneralizedNewtypeDeriving GHC extension fails to generate instances if there are no appropriate Coercible instances around. The whole roles and Coercible idea have arisen from the following bug in the older GHC versions. Suppose we have the following type family: