A generalized algebraic data type (GADT) is an algebraic data type whose constructors can refine the type parameter to a concrete type. Ordinary constructors may all produce MyGADT a, while GADT constructors can produce MyGADT Bool or MyGADT String. Enable the feature with the GADTs language extension. section 13.1.2
GADTs become useful when a function must remain polymorphic while recovering precise type information:
- Define constructors with precise result types, allowing pattern matching to refine a polymorphic parameter. section 13.1.2
- Return values whose types follow from the matched constructor, as with giveMe :: GiveMe a -> a. section 13.1.2
- Pair a GADT with existential quantification when a container must hide its type parameter while retaining recoverable type information. section 13.1.4
Typed image representations
An Image px GADT stores either 8-bit or 16-bit pixels while recording the pixel type in its result: Image8Bit produces Image P.PixelRGB8, and Image16Bit produces Image P.PixelRGB16. A conversion function can pattern-match safely, transform 8-bit pixels, and leave an already 16-bit image unchanged.

