GADTs
GADTs, or Generalized Algebraic Data Types, are a feature of some statically typed functional programming languages that extend the expressiveness of algebraic data types. In a standard algebraic data type, each constructor produces the same type. GADTs allow constructors to specify the exact result type, which may depend on the constructor. This enables more precise typing and can encode invariants directly in the type system.
In languages that support GADTs, type parameters can be refined by constructors. For example, in Haskell with
Add :: Expr Int -> Expr Int -> Expr Int
IsZero :: Expr Int -> Expr Bool
Here, the type parameter a of Expr a is refined by the constructor, so pattern matching can
Benefits include encoding invariants such as well-typed abstract syntax trees, safe interpreters, and typed stages of
Limitations involve increased type signatures and potential complexity in type checking and inference. They may also
See also: dependent types, type families, indexed types, type safety.