■ Change #47 · wiki.edited
Mathlib's algebraic and order hierarchy: Normalize Markdown to canonical form
explainer.student
− **Structures you already use.** The same algebra rules work for integers, real numbers, polynomials and square matrices: you can add and multiply, and $a(b+c)=ab+ac$. 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 $n\times n$ matrices under multiplication). A *ring* has $+$ and $\times$ 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).+ **Structures you already use.** The same algebra rules work for integers, real numbers, polynomials and square matrices: you can add and multiply, and $a(b+c)=ab+ac$. 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 $n\times n$ matrices under multiplication). A _ring_ has $+$ and $\times$ 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.+ **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.+ **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 $(a+b)^2=a^2+2ab+b^2$ 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.+ **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.
hash 759a5f0d7e4a9d1099e36940c92c9666d7f9ca492a1c46709355e3fb7e808b20
previous a80894c446030d8a6af5e04c310f0da4f19031d3867b31998c8ad8351ff2f29f