The Journey: From Axioms to Proof
This is the story of formalizing the Riemann inequality in Lean 4 — a journey through 75 cycles of development, multiple dead ends, surprising breakthroughs, and ultimately, victory. What started as an ambitious attempt at the full Riemann-Roch theorem evolved into a deep dive into the elegant connection between valuation theory, module dimension, and algebraic geometry.
The Goal: What is Riemann-Roch?
The Riemann-Roch theorem is one of the crown jewels of algebraic geometry. For a divisor D on a smooth curve, it relates four quantities: the dimension l(D) of functions with bounded poles, the degree of D, the genus g of the curve, and a mysterious "dual" term l(K-D) involving differentials.
The Riemann inequality is the "easy half" — a one-sided bound that doesn't require the duality machinery:
But even this "easy" half required 75 cycles of careful proof engineering to formalize from first principles!
The Timeline: Key Moments
The Axiom Era
We started by axiomatizing Riemann-Roch: assume it holds, then derive consequences like Clifford's theorem, genus formulas, and behavior under twisting. This produced 70+ lemmas but hit a ceiling — we couldn't make the axioms constructive.
The Pivot: Dedekind Domains
Key insight: work with Dedekind domains instead of abstract curves. Points become height-1 primes, divisors become finitely-supported integer functions on primes, and L(D) becomes a concrete submodule of the fraction field. This was the foundation that made everything constructive.
The Main Theorems (Conditionally)
With the new foundation, we proved both Riemann inequalities — but they depended on a
typeclass called LocalGapBound. The theorems were correct; we just needed to
instantiate the typeclass for Dedekind domains. This became the central challenge.
The Infrastructure Grind
Thirty cycles of building valuation infrastructure: uniformizers, DVR instances, valuation ring equivalences, residue field bridges. Every lemma had to navigate between different representations — HeightOneSpectrum valuations vs DVR valuations vs localization. Many dead ends, much sorry-driven exploration.
The mapEquiv Discovery
After 20+ cycles wrestling with residue field surjectivity, we found
IsLocalRing.ResidueField.mapEquiv buried in mathlib. One line replaced weeks
of complex argument! This proved the residue field bridge and unblocked everything downstream.
The Kernel Proof
The hardest proof: showing that the kernel of evaluation L(D+v) → κ(v) is exactly L(D). This required careful WithZero arithmetic, discrete valuation step-down lemmas, and precise handling of the multiplicative group structure. Six cycles of detailed work.
Victory: LocalGapBound Instance
With the kernel characterized, Module.length additivity on exact sequences gave us
l(D+v) ≤ l(D) + 1. The LocalGapBound instance was complete. Both Riemann
inequalities became unconditionally proved!
Cleanup and Victory Lap
Refactored the codebase, removed dead ends, documented everything. Final result: 2493 lines of sorry-free Lean 4, cleanly organized into 8 modules with a clear proof architecture.
How the Proof Actually Works
The cleaned-up proof has a beautiful structure. Here's the core idea:
The Induction Strategy
We prove l(D) ≤ deg(D) + 1 by induction on the degree of effective divisors.
Base case: If deg(D) = 0, then D = 0 (the zero divisor). The space L(0) consists of functions with no poles — just constants. So l(0) = 1 ≤ 0 + 1. ✓
Inductive step: If deg(D) = n+1 > 0, decompose D = D' + v where v is a point with D(v) > 0. Then deg(D') = n, so by induction l(D') ≤ n + 1.
The key is bounding how much l increases when we add a single point:
The Evaluation Map: Where the Magic Happens
The evaluation map is where algebra meets geometry. For f ∈ L(D+v), we:
- Shift: Multiply f by πD(v)+1, where π is a uniformizer at v
- Land in Ov: The shifted element has valuation ≤ 1, so it lives in the valuation ring
- Project: Take the image in the residue field κ(v) = Ov/mv
The beautiful fact: f ∈ L(D) (without the extra point) if and only if the shifted element lands in the maximal ideal mv, i.e., evaluates to zero. This is the kernel characterization, and it took 6 cycles to prove rigorously!
Why This Works
The discrete valuation structure is key. Because v is a discrete valuation:
• Valuations are integers (with ∞ for zero)
• v(fπn) = v(f) · v(π)n = v(f) · e-n
• If v(f) ≤ eD(v)+1 (f ∈ L(D+v)) then v(fπD(v)+1) ≤ 1
The step from "strictly less than 1" to "in the maximal ideal" uses the discrete nature — there are no values between e-1 and 1.
What Made This Hard?
Formalizing "obvious" mathematics in a proof assistant reveals hidden complexity:
Type-Level Challenges
- Three different residue field constructions that need bridging
- WithZero multiplicative integers for valuations
- Distinguishing v.valuation K vs DVR's valuation
- Subtype vs submodule representations of L(D)
Mathematical Subtleties
- Affine vs projective: HeightOneSpectrum misses ∞
- The uniformizer must be chosen consistently
- Module.length vs vector space dimension
- Casting between ℤ, ℕ, and WithZero ℤ
What We Learned
This project taught us several lessons about formal mathematics:
- Mathlib spelunking pays off. The mapEquiv discovery saved 20 cycles of work.
- Sorry-driven development works. Sketch the structure, then fill in the gaps.
- Type mismatches are hints. They often reveal hidden mathematical distinctions.
- Clean architecture matters. Modularization in Cycle 40 made the endgame tractable.
- LLM collaboration accelerates exploration but requires verification against actual code.
What's Left for Full Riemann-Roch?
The Riemann inequality is the "easy half." The full theorem requires:
- Canonical divisor K: Defined via differential forms
- Serre duality: An isomorphism relating L(D) to differentials
- Residue theorem: Sum of residues equals zero
- Genus computation: Connecting g to differential forms
These require substantial algebraic geometry infrastructure that mathlib doesn't yet have. But the foundation is laid — the exact sequence machinery and Module.length framework would carry over directly.