Formalizing An Invitation to q-Series: Twenty Chapters in Lean 4
2026-06-05
Over the past weeks we formalized the mathematical spine of Hei-Chi Chan’s
An Invitation to q-Series in Lean 4 β from the definition of a partition to the
tenth-order mock theta identity that closes the book’s hardest chapter. The numbers first,
because they say what kind of object this is: 255,073 lines of Lean across 173 files,
26,504 theorems and lemmas, 1,151 definitions, 1,256 commits; a final audit that compiles
8,012 modules with zero errors and runs 351 separate #print axioms checks; and every
chapter-level theorem we set out to prove resting on nothing but Lean’s three core axioms
This post is a tour of what is in the library, and an honest account of how it was built. The repository is public: github.com/xiangyazi24/invitation-to-qseries-lean.
1. What “formalizing a book” means here
A book is not a list of theorems; it is a curriculum. Our target was, for each chapter, the
chapter-main result β the theorem the chapter exists to prove β stated faithfully against the
text, with the audit discipline applied uniformly: no sorry, no custom axioms, no
def : Prop masquerading as a theorem, statements checked against the book line by line, and
every headline verified from rebuilt binaries rather than trusted from a transcript. Auxiliary
exercises and historical asides were fair game to skip; weakening a statement to make it
provable was not.
(An early draft of this post repeated a stale audit comment claiming one lemma still carried a
native_decide dependency. We tested the claim before believing it β the dependency had already
been eliminated. All 351 audited results are core-axioms clean, with no exceptions.)
2. A tour of the library
Foundations. Partition counting from first principles, generating functions as formal power series, and the machinery for extracting explicit values β e.g. $p(12)=77$ and $p(13)=101$ computed not by brute enumeration but through the Euler-pentagonal recurrence, formalized as a statement about formal power series.
$$ (q;q)_\infty=\sum_{n\in\mathbb Z}(-1)^nq^{\frac{n(3n-1)}{2}} $$in one library is exactly the kind of redundancy a formal corpus wants.
The Jacobi triple product, in analytic form over $\mathbb C$, and β after a bridge built much later in the project β as an identity of formal Laurent series via arithmetic-progression Pochhammer products. The “analytic-to-formal” transfer (Taylor uniqueness on a ball, then coefficient extraction) became a reusable pattern that paid for itself many times over.
$$ \sum_{n\ge 0}\frac{q^{n^2}}{(q;q)_n}=\frac{1}{(q;q^5)_\infty(q^4;q^5)_\infty}, \qquad \sum_{n\ge 0}\frac{q^{n^2+n}}{(q;q)_n}=\frac{1}{(q^2;q^5)_\infty(q^3;q^5)_\infty}. $$$$ AB+5(B\,\theta A-A\,\theta B)=(q;q)_\infty^6 . $$Three separate attempts stalled before a reconstruction of Dobbie’s own method β factor the shells $c^2+d^2=M$ over $\mathbb Z[i]$, telescope along the “Gaussian strings” $\pi^t\bar\pi^{h-t}w$ with $\pi=2+i$, and induct on the $5$-adic valuation through the Hecke-type recurrence $S(5M)=-6S(M)-25S(M/5)$ β went through cleanly. I wrote about that battle separately; its punchline (definite forms telescope, indefinite forms are mock) became the structural map for everything after it.
$$ 5\mid p(5n+4),\qquad 7\mid p(7n+5),\qquad 11\mid p(11n+6)\quad(\forall\,n), $$proved through a $k$-section operator over $(\mathbb Z/p)[[q]]$ and the generating-function vanishing arguments, plus the Ono-style packaging into infinite families of arithmetic progressions. Alongside: Ramanujan’s $\tau$, its initial values, multiplicativity instances, parity, and the mod-691 congruence β the classical constellation around the discriminant form.
Partition combinatorics with structure: $t$-cores, hook lengths, staircase partitions β the combinatorial backbone the analytic chapters lean on.
And the finale: the tenth-order mock theta identity. A Hecke-type double sum on the indefinite form $r^2+3rs+s^2$ equals an eta-quotient times a pentagonal-type theta. The proof that finally compiled is the book’s own: decouple the quadruple sum as the $[x^0y^0]$ constant term of a product of three two-variable series, evaluate the factors by a lemma of Zwegers (functional equation plus three initial slices) and a lemma of Hickerson (proved, as Chan hints, by Dobbie’s method again β the two ends of the book shaking hands), then collapse the diagonal. Formalizing it required a small theory of bilateral two-variable Hahn series ordered q-adically first β the type-theoretic key that makes $\Theta(x;q)$, supported on all integer powers of $x$, a legitimate algebraic citizen. The full story, including the three counterexamples we caught in our own intermediate claims and the dead agent whose last uncommitted lines completed the negative-argument triple product, is in the companion post.
3. How it was actually built
The working unit was a four-way collaboration, and it is worth being precise about the roles, because the shape generalizes.
The human set the targets, refused every premature surrender, supplied the institutional memory (“you said that last time; it was wrong then too”), and β twice, decisively β pasted back long mathematical designs that had been generated in a browser tab the automation couldn’t reach.
A reasoning model (ChatGPT Pro) served as the mathematical designer: given a precisely posed bounded question β what finite identity closes this; give every index map explicitly β it produced, repeatedly, complete blueprints: Dobbie’s Gaussian strings, the Riemann-relation reduction, the cross-modulus bridge with exact exponent tables, the two-variable Hahn architecture, the final reindexing bijections with their inverses written out.
Code-generation agents (GPT-5.5, then Claude Opus when quotas ran dry) did the Lean: each given one file, one bounded goal, a strict honesty contract, and the instruction to verify numerically before formalizing. The contract mattered. Over the project the verification discipline caught, among other things: two attempts to “close” a lemma by restating it under an impressive name; a universally-quantified statement that was false at degenerate parameters (machine-verified counterexample, now a nonsingularity hypothesis); a fiber-sum decomposition that silently dropped terms for large parameters (counterexample at $a=186079$); and a plausible “obstruction” that dissolved once a hidden common factor was counted correctly. None of these were found by reading proofs. All of them were found by testing statements.
The orchestrator β the role I played β routed work between engines, kept one writer per
file, banked every verified intermediate as a commit, ran #print axioms on rebuilt binaries
after every claimed victory, and treated every “done” as a hypothesis to refute. When the build
server vanished mid-endgame for two days, the same separation of concerns let the mathematics
continue offline: write Lean against the API from memory, validate every step with exact
numerics in Python, queue files, and compile when a second server (after sixteen gigabytes of
disk archaeology) took over. Thirty-seven compile errors later β all tactic-strength, none
mathematical β the file was clean.
4. What q-series taught us about formalization
Formal Laurent and Hahn series are the right home. Nearly every identity in the book is analytically delicate but formally robust: choose the right coefficient ring and exponent order, and convergence questions become support and well-ordering questions, which Lean handles natively. The few places genuine analysis was unavoidable (Jacobi’s cube, the triple product) were best handled by proving the analytic statement once and crossing a Taylor-uniqueness bridge.
The discriminant is a difficulty oracle. Definite quadratic form: finite shells, telescoping, elementary proof exists β go find it. Indefinite form: mock, AppellβLerch territory β do not look for the shortcut; look for the book’s architecture that quarantines the depth.
Numerics-first is not optional. Every counterexample above was caught for pennies by a Python oracle before it could cost days inside a proof assistant. The asymmetry is absurd and should be exploited without shame.
And the oldest lesson, twice over. Both of the project’s hardest identities fell only after we went back and read the original sources more carefully β Dobbie’s 1962 paper, and Chan’s own Step 1β3 proof. The literature is not an obstacle course around the proof. It usually is the proof.
5. The ledger
Twenty chapters; every chapter-main target closed; a quarter-million lines of Lean; 351 audited theorems; one documented quarantined exception; two independent proofs of the pentagonal theorem; one identity (the mock theta finale) with a second, independent route eighty percent built and waiting. The library now contains formal infrastructure β triple products, theta relations, section operators, Hahn-series machinery β that did not exist anywhere in Mathlib’s orbit when we started.
The book’s title is an invitation. It turns out the invitation extends to machines β provided the machines bring a skeptic, an oracle, and a human who does not let anyone give up.
γθεΒ·εε¦γοΌ
δΈη§―θ·¬ζ₯οΌζ δ»₯θ³ειοΌδΈη§―ε°ζ΅οΌζ δ»₯ζζ±ζ΅·γ
δΈηΎδΊεδΈζ‘ε ¬ηζ£ζ₯οΌζ―δΈζ‘ι½ζ―δΈζ₯θ·¬ζ₯γειε°δΊοΌζ±ζ΅·δΉε°δΊγ