Library / SDK
teorth/analysis avatar
teorth/analysis

analysis: a Lean companion that drops the book's definitions once Mathlib exists

A Lean companion to Analysis I. For instance, Chapter 2 develops a theory of the natural numbers independent of Mathlib, but all subsequent chapters will use the Mathlib natural numbers instead.

1,987 stars280 forksLeanApache-2.0

At a glance

What is it?
This repository formalizes Analysis I in Lean 4 as an annotated companion rather than a replacement, and it makes a deliberate trade: Chapter 2 builds the natural numbers from nothing, then every later chapter quietly switches to Mathlib's. It also fills in operations the book leaves undefined with junk values, and leaves the exercises as unsolved sorry markers.
Who is it for?
This repository is for two different readers and does not pretend otherwise. For someone working through Analysis I, it is a machine-checked companion that follows the book's order closely enough to navigate by while adding the Lean syntax the text cannot show, and its two epilogues are the part with the clearest payoff, since they prove the hand-built natural numbers and Mathlib's are isomorphic.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 4 days ago.
What is it written in?
Mainly Lean, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 21, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • teorth/analysis1,987 ★ · +149
Star history
Repository2026-07-312026-11-21
teorth/analysis1.8k2k

149 Stars in the last 113 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The formalization abandons the book's definitions partway through

Much of the material in this text already exists in Mathlib, though with slightly different definitions. The stated resolution is a gradual one: the formalization transitions from the textbook-provided definitions to the Mathlib-provided definitions as the reader progresses, and the page names the cost of that choice as sacrificing self-containedness in favour of compatibility. The worked example is the natural numbers. Chapter 2 develops a theory of them independent of Mathlib, and every subsequent chapter uses the Mathlib naturals instead. An epilogue to Chapter 2 is provided to show the two notions are isomorphic, so the switch is bridged rather than silent. The consequence for a reader is concrete: a theorem stated in Chapter 2 and a theorem about the same object in Chapter 4 are not about the same Lean type, and a proof written against one will not compile against the other without going through that epilogue.

Junk values make division by zero and non-Cauchy limits total

Many operations the book leaves undefined are defined here anyway, with a junk value, so the operation becomes total. The two examples the page names are division by zero and taking the formal limit of a sequence that is not Cauchy, and the assigned value in the examples given is 0. The reason is a Lean modelling one, stated at length: Lean has better support for total functions than for partial functions, and indiscriminate use of the latter can lead into what the page calls dependent type hell, where even very basic manipulations require subtle and delicate proofs. The page links a blog post on division by zero in type theory by Kevin Buzzard for the longer discussion. The design consequence is worth naming plainly. A definition that returns 0 for an input it cannot handle is not a weaker version of a partial definition, it is a different function, and any theorem proved about it is a theorem about the padded version.

Sequences start at zero, and the natural numbers are built inductively

Three technical changes are called out as the most notable, and all three exist to match Mathlib rather than the text. Sequences are indexed from zero instead of one, because Mathlib has far more support for zero-based natural numbers than for one-based ones. The Chapter 2 natural numbers are constructed by an inductive type rather than a purely axiomatic approach, and the Peano axioms themselves are then formalized in the epilogue to that chapter, which is the arrangement that makes the later Mathlib comparison possible. The third is the junk-value change described above. Together they add up to a formalization that is faithful in structure and deliberately unfaithful in statement, which is the trade the introduction makes explicit when it says the arrangement of definitions, theorems and proofs closely paraphrases the textbook while the notation does not always match it.

Every exercise stays a sorry and the author is not filling them in

Portions of the text left as exercises to the reader are rendered as sorry markers in the Lean sources. Readers are welcome to fork the repository and try them, and the page is explicit that solutions are not intended to be placed in the repository directly. That is a better position than leaving them silently blank, because a sorry is a checked, greppable marker rather than a comment, and a fork has an obvious merge target. It also means the repository's own build is not a claim of completeness: the text is a companion with gaps in it, and the gaps are at exactly the points where a reader is supposed to do the work. The introduction reinforces the same limit from the other side, saying the formalization should be viewed as an annotated companion to the primary text rather than a replacement for it.

Each section ships as a prose page, generated docs and a source file

The section index gives every entry three links, and the pattern explains how the project is built. There is a rendered prose page, a generated documentation page, and a link to the Lean source on GitHub. Three artefacts per section means the prose is not a separate document: it lives in the Lean files and is rendered by the Verso tool, with a literate configuration file at the repository root driving the split between narrative and code. The generated documentation is the usual HTML rendering of the same source, so the two are two views of one file rather than two documents that can drift. The build tooling at the root is a Lake file, a Lake manifest and a toolchain file, plus three separate entry points for building, building the web version and serving, and a Python script for the last of those.

Chapter 1 is not formalized and two epilogues are not in the book

The section list starts with Chapter 1: Introduction, marked in italics as not formalized. The rest follows the book's own numbering, with Chapter 2 covering the Peano axioms, addition and multiplication, Chapter 3 covering set theory from fundamentals through Russell's paradox, functions, images and inverse images, Cartesian products and the cardinality of sets, and Chapter 4 beginning with the integers and the rationals. Two of the entries do not correspond to anything in the original text. There is a Chapter 2 epilogue on the isomorphism with the Mathlib natural numbers and a Chapter 3 epilogue on the connections with ZFSet. Both exist for the same reason, which is to connect what the book built to what the library already has, and together they mark the two points where the formalization hands off.

It paraphrases the book on purpose, and is not optimized for speed

Two constraints shape everything else. The first is quotation. The author is refraining from directly quoting material from the textbook and instead provides references to the original text where appropriate, which is why the project describes itself as a paraphrasing that is intended to be as faithful as possible while still showcasing Lean's features and syntax. The practical effect is that the repository is not a substitute for the book and does not try to be one. The second is performance. The formalization is explicitly not optimized for efficiency, and the page says it may in some cases deviate from idiomatic Lean usage. Both constraints are the right call for the stated purpose, which is reading alongside a textbook rather than producing a reusable library, but they mean a proof here is evidence about the mathematics and not a model of how the same mathematics would be written by someone starting from Mathlib.

Editorial conclusion

This repository is for two different readers and does not pretend otherwise. For someone working through Analysis I, it is a machine-checked companion that follows the book's order closely enough to navigate by while adding the Lean syntax the text cannot show, and its two epilogues are the part with the clearest payoff, since they prove the hand-built natural numbers and Mathlib's are isomorphic. For someone learning Mathlib, the early chapters read as an introduction to the library's conventions by example, which is a genuinely different way in than the documentation. The trade is stated rather than hidden. The formalization is not optimized for efficiency, it may depart from idiomatic Lean, it paraphrases instead of quoting so you still need the book, and the exercises stay open. Three things to know before you start. That definitions change under you, since sequences are re-indexed to start at zero and undefined operations gain junk values, so a proof that typechecks here may not match the book's statement. That the natural numbers you build in Chapter 2 are not the ones used from Chapter 3 onward. And that the sorry markers are deliberate, because the author states solutions will not be placed in the repository.

Frequently asked questions

What is the teorth/analysis repository?

A Lean 4 formalization of the book Analysis I, written as an annotated companion rather than a replacement for the text. Every section is published three ways, as a rendered prose page, generated documentation and a Lean source file, and Chapter 2 builds the natural numbers from scratch before later chapters switch to Mathlib's.

Why do some definitions in this Lean formalization return 0 where the book leaves them undefined?

To make the operations total. Division by zero, and taking the formal limit of a sequence that is not Cauchy, are assigned a junk value so the function is defined on every input. The reason given is that Lean supports total functions better than partial ones, since heavy use of partial definitions can lead into dependent type hell.

Are the exercises from Analysis I solved in this repository?

No. Portions of the text left as exercises to the reader are rendered as sorry markers in the Lean sources. The page invites readers to fork the repository and attempt them, and states that solutions are not intended to be placed in the repository.

Are sequences indexed from zero or from one in this formalization?

From zero, unlike the text. The page gives the reason as Mathlib having much more support for zero-based natural numbers than for one-based ones, and lists this as one of the technical changes made to align with Mathlib conventions.

What is in Chapter 1 of this Lean project?

Nothing formalized. The section index lists Chapter 1, Introduction, in italics as not formalized, and the formalized material starts at Chapter 2 with the Peano axioms, addition and multiplication, followed by an epilogue proving an isomorphism with the Mathlib natural numbers.

Official sources

  1. Official documentation
  2. Official README
  3. Project repository
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/teorth-analysis.svg)](https://hysenlabs.com/projects/teorth-analysis)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/teorth-analysis.svg)](https://hysenlabs.com/projects/teorth-analysis)

Was this useful?