ArticleslgStudy

mathematics

Generalized algebraic data type

Generalized algebraic data type is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Generalized algebraic data type rather than just read about it. In short: In functional programming, a generalized algebraic data type (GADT, also first-class phantom type, guarded recursive datatype, or equality-qualified type) is a generalization of a parametric algebraic data type (ADT). Overview In a GADT, the product constructors (called data constructors in Haskell) can provide an explicit instantiation of the ADT as the type instantiation of their return value.

Key takeaways

  • Generalized algebraic data type belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Generalized algebraic data type to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Generalized algebraic data type from memory before moving on to harder problems.

Reference excerpt

In functional programming, a generalized algebraic data type (GADT, also first-class phantom type, guarded recursive datatype, or equality-qualified type) is a generalization of a parametric algebraic data type (ADT).

Overview In a GADT, the product constructors (called data constructors in Haskell) can provide an explicit instantiation of the ADT as the type instantiation of their return value. This allows defining functions with a more advanced type behaviour. For a data constructor of Haskell 2010, the return value has the type instantiation implied by the instantiation of the ADT parameters at the constructor's application.

They are currently implemented in the Glasgow Haskell Compiler (GHC) as a non-standard extension, used by, among others, Pugs and Darcs. OCaml supports GADT natively since version 4.00. The GHC implementation provides support for existentially quantified type parameters and for local constraints.

History An early version of generalized algebraic data types were described by Augustsson & Petersson (1994) and based on pattern matching in ALF. Generalized algebraic data types were introduced independently by Cheney & Hinze (2003) and prior by Xi, Chen & Chen (2003) as extensions to the algebraic data types of ML and Haskell. Both are essentially equivalent to each other. They are similar to the inductive families of data types (or inductive datatypes) found in Rocq's Calculus of Inductive Constructions and other dependently typed languages, modulo the dependent types and except that the latter have an additional positivity restriction which is not enforced in GADTs. Sulzmann, Wazny & Stuckey (2006) introduced extended algebraic data types which combine GADTs together with the existential data types and type class constraints. Type inference in the absence of any programmer supplied type annotation, is undecidable and functions defined over GADTs do not admit principal types in general. Type reconstruction requires several design trade-offs and is an area of active research (Peyton Jones, Washburn & Weirich 2004; Peyton Jones et al. 2006). In spring 2021, Scala 3.0 was released. This major update of Scala introduced the possibility to write GADTs with the same syntax as algebraic data types, which is not the case in other programming languages according to Martin Odersky.

Applications Applications of GADTs include generic programming, modelling programming languages (higher-order abstract syntax), maintaining invariants in data structures, expressing constraints in embedded domain-specific languages, and modelling objects.

Higher-order abstract syntax

An important application of GADTs is to embed higher-order abstract syntax in a type safe fashion. Here is an embedding of the simply typed lambda calculus with an arbitrary collection of base types, product types (tuples) and a fixed point combinator:

And a type safe evaluation function:

The factorial function can now be written as:

Problems would have occurred using regular algebraic data types. Dropping the type parameter would have made the lifted base types existentially quantified, making it impossible to write the evaluator. With a type parameter, it is still restricted to one base type. Further, ill-formed expressions such as App (Lam (\x -> Lam (\y -> App x y))) (Lift True) would have been possible to construct, while they are type incorrect using the GADT. A well-formed analogue is App (Lam (\x -> Lam (\y -> App x y))) (Lift (\z -> True)). This is because the type of x is Lam (a -> b), inferred from the type of the Lam data constructor.

See also Type variable

Notes

Further reading

External links

Generalised Algebraic Datatype Page on the Haskell wiki Generalised Algebraic Data Types in the GHC Users' Guide Generalized Algebraic Data Types and Object-Oriented Programming GADTs – Haskell Prime – Trac Archived 2019-04-04 at the Wayback Machine Papers about type inference for GADTs, bibliography by Simon Peyton Jones Type inference with constraints, bibliography by Simon Peyton Jones Emulating GADTs in Java via the Yoneda lemma

Worked examples

Example 1 — a first encounter with Generalized algebraic data type

Start with the simplest possible case. Write down what Generalized algebraic data type claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Generalized algebraic data type before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Generalized algebraic data type ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Generalized algebraic data type

In research
Generalized algebraic data type appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Generalized algebraic data type in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Generalized algebraic data type is common in secondary-school and first-year university syllabi. It links to neighbouring topics Composite data types, Data types, Dependently typed programming, so understanding it makes those chapters shorter.
In everyday life
Look for Generalized algebraic data type outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Generalized algebraic data type” →

How to study Generalized algebraic data type in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Generalized algebraic data type means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Generalized algebraic data type out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Generalized algebraic data type in simple terms?

In functional programming, a generalized algebraic data type (GADT, also first-class phantom type, guarded recursive datatype, or equality-qualified type) is a generalization of a parametric algebraic data type (ADT). Overview In a GADT, the product constructors (called data constructors in Haskell…

Why does Generalized algebraic data type matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Generalized algebraic data type?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Generalized algebraic data type.

Tags

  • Composite data types
  • Data types
  • Dependently typed programming
  • Functional programming
  • Type theory

Keep exploring