Mathlib's algebraic and order hierarchy
How Mathlib organizes groups, rings, fields, orders, topologies and modules as typeclasses so that one theorem applies everywhere it is true.
On this page15 sections
Every definition and theorem added to Mathlib, and every formalization project built on it, must fit this hierarchy, so its design decides what can be stated cheaply and reused.
How Mathlib arranges groups, rings and fields so one proof works for all of them.
- 1Write each rule family once
- 2Extend families like a family tree
- 3Lean finds the right instance
- 4One proof serves every example
Structures you already use. The same algebra rules work for integers, real numbers, polynomials and square matrices: you can add and multiply, and . Mathematicians name such bundles of rules. A group is a set with one operation that has an identity element, inverses and associativity (the integers under , or the invertible matrices under multiplication). A ring has and with the usual laws, but division may fail (integers, polynomials, square matrices). A field is a ring in which every nonzero element can be divided by (rationals, reals, complex numbers).
Typeclasses. Mathlib, Lean's main library, encodes each bundle as a class: Group G, CommRing R, Field K. A theorem is written once for any type with the needed class, for example theorem foo [CommRing R] (a b : R) : .... The square brackets hold the hypotheses on the structure. When you use the theorem on the integers, Lean searches for an instance, a registered proof that ℤ is a commutative ring, and fills it in. It is like duck typing in Python, except the duck must come with proofs of the laws.
Layers. Classes extend one another: a field extends a commutative ring, which extends a ring, which is built from an additive group and a multiplicative monoid (a monoid is a group without inverses). Every theorem proved at one layer is inherited by everything above it.
A concrete payoff. The ring tactic proves in one line, and the same line works for integers, reals, polynomials and any commutative ring, because the tactic asks only for the class. A library without this design would need a copy of every algebra lemma for each number system, and a fix in one copy would not reach the others.
Diamonds. If a type reaches the same structure along two paths (say the reals as a field and as a normed space, each supplying an addition), the two versions must agree by definition, or proofs break in confusing places. Such conflicts are called diamonds, and spotting them is a large part of reviewing new library code.
Why it is load-bearing. Research formalizations such as the Fermat's Last Theorem project spend much of their effort adding classes to this hierarchy, and AI provers depend on its lemmas being findable. In October 2026 the library held about 139,000 definitions and 290,000 theorems.
Go deeper
The full technical text.
The problem it solves
Mathematics reuses structure constantly. A statement about commutative rings applies to the integers, to polynomials over a field and to continuous real functions. A library has to let one proof serve all of them without a human re-proving it each time and without the checker getting lost in the search for the right instance.
How Mathlib does it
Mathlib encodes structures as typeclasses. Group G, CommRing R, Field K, NormedSpace 𝕜 E and TopologicalSpace X are classes, and a theorem is stated for any type with the needed instances. When a theorem about a ring is applied to the integers, Lean's instance search finds the Ring ℤ instance on its own. The hierarchy is deliberately layered (Mathlib.Algebra.Group.Defs, Mathlib.Algebra.Ring.Defs, Mathlib.Algebra.Field.Defs): a ring extends an additive commutative group and a monoid, a field extends a commutative ring, and so on.
Design points that experts argue about, and that new contributors must learn:
- Bundled versus unbundled. Operations such as
+and*come from small classes (Add,Mul), and structures extend them. The community's account of the trade-offs is in Baanen, Use and abuse of instance parameters in the Lean mathematical library. - Diamonds. If two instance paths give the same structure two different ways, they must agree definitionally, or proofs break in confusing places. Much of the review of a new instance is checking that no bad diamond appears.
- Order and topology mixed with algebra. Ordered fields, normed spaces and topological groups combine several hierarchies, and the combination classes (for example ordered rings or topological rings) are where most of the design friction is.
- Naming. Strict naming conventions make lemmas findable by guessing, which in a library of this size is part of the interface.
Why this entry is load-bearing
Projects that formalize research mathematics, such as the Fermat's Last Theorem project and the Liquid Tensor Experiment, spend a large share of their effort extending this hierarchy: Hecke algebras, deformation rings, condensed abelian groups. Tactics such as ring and linarith work by instance search on these classes. And AI provers read it: a model that proposes exact mul_comm a b is relying on this structure being there to find. The scale is large: the Mathlib statistics page listed 139,391 definitions and 290,502 theorems when read on 17 October 2026.
What to understand first
- What an instance is, and how Lean searches for one.
- Why a structure is declared as extending another rather than containing it.
- What a diamond is and why
rflbetween two instance paths matters. - The overview in the Mathlib community paper, The Lean mathematical library.
Words on this page (7)
- typeclass
- A named bundle of operations and laws that a type can satisfy, used by Lean to reuse theorems.
- instance
- A proof that a particular type satisfies a typeclass, which Lean finds on its own.
- group
- A set with an operation that has an identity and inverses, such as the integers under addition.
- ring
- A set where you can add, subtract and multiply, such as the integers or polynomials.
- field
- A ring where every nonzero element can be divided by, such as the rational or real numbers.
- diamond
- Two different inheritance routes to the same structure that fail to agree by definition.
- Mathlib
- The community-maintained library of formalized mathematics for Lean.
New to this? Read first (3)
- The Lean mathematical library (paper)A readable overview of how Mathlib is organised.
- Use and abuse of instance parameters in the Lean mathematical libraryThe design trade-offs, explained by a maintainer.
- Mathlib documentation: Algebra.Ring.DefsSee the actual definitions of a ring in Lean.
Understanding map
map of the whole wiki →What to read before Mathlib's algebraic and order hierarchy (left), and what builds on it: entries that depend on it and its open problems (right). Every box links to its page.
- Start with
- Step 2
- Mathlib's algebraic and order hierarchyYou are here
- Then
Open problems & questions
No open problems or questions on this entry.
Progress timeline
- Jul 17, 2023Mathlib port to Lean 4 completedsource
The hierarchy was ported to Lean 4 with its design largely intact; development continues in mathlib4.
- Feb 1, 2022Instance-parameter design written upsource
Baanen documents the typeclass design choices and pitfalls of the Lean 3 to Lean 4 era library (arXiv, February 2022).
- Oct 1, 2019Mathlib community papersource
The Lean mathematical library is described in a CPP 2020 paper (arXiv, October 2019), including its approach to the algebraic hierarchy.