Volume P0 · Guide
Lean for readers
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Contents
Enough Lean to read a statement
A Lean statement is a precise sentence of mathematics. It is longer than the sentence of a book, because it says everything: the type of each variable, the range of each sum, the meaning of each symbol. This section gives the parts that you meet most often.
Types
Every object in Lean has a type. The types of numbers that the project uses most are these:
| Lean | Meaning |
|---|---|
ℕ |
the natural numbers 0, 1, 2, ... |
ℤ |
the integers |
ℚ |
the rational numbers |
ℝ |
the real numbers |
ℂ |
the complex numbers |
Bool |
the two values true and false |
Prop |
the type of statements |
The notation (n : ℕ) means " is a natural number". The notation (x : ℝ) means " is a real number". A number such as (3 : ℚ) is the rational number 3. The same symbol 3 can live in several types, and Lean always knows which one is meant.
Example. In ℕ, subtraction cannot go below 0. So 3 - 5 = 0 in ℕ. In ℤ, 3 - 5 = -2. Division in ℕ rounds down: 7 / 2 = 3. These rules matter when you read a statement about binomial coefficients, where appears. The project writes such statements over ℤ or ℚ, or it says why the subtraction cannot go below 0 (spec 06.1).
Example. In Lean, division by zero gives zero: (5 : ℚ) / 0 = 0. This is a convention, not a theorem of arithmetic. It makes Knuth's rule come out by itself: the project defines TAOCP.kmod x y as , and for this is .
Functions
A function from ℕ to ℚ has the type ℕ → ℚ. You apply a function by writing its argument after it, with a space: f n, not f(n). A function of two arguments is written f m n. Several functions are written after their argument with a dot: n.choose k is and n.factorial is .
Example. Mathlib defines the harmonic numbers as a function harmonic : ℕ → ℚ. So harmonic 3 is .
Example. Nat.fib 10 is the Fibonacci number . The project adds TAOCP.fibInt : ℤ → ℤ for Knuth's with negative , because Mathlib's function takes only natural numbers.
Theorems
A theorem has a name, its variables, a colon, and the claim:
theorem Nat.sum_range_choose (n : ℕ) :
(∑ m ∈ Finset.range (n + 1), n.choose m) = 2 ^ n
Read it like this: "For every natural number , the sum of over from 0 to is ." The variables before the colon are quantified for all values. The proof comes after := or by, and the handbook usually does not show it.
Quantifiers and connectives
| Lean | Meaning |
|---|---|
∀ n, P n |
for every , |
∃ n, P n |
there is an with |
P ∧ Q |
and |
P ∨ Q |
or |
¬P |
not |
P → Q |
if then |
P ↔ Q |
if and only if |
Example. Mathlib has the theorem Nat.exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ Nat.Prime p. Read: "for every , there is a prime with ". This says that there are infinitely many primes.
Example. The statement ∀ n : ℕ, 0 < n → n.factorial ≥ n reads: "for every natural number , if then ". The arrow after a hypothesis is "if ... then".
Sums and products
A sum is written ∑ i ∈ s, f i, where s is a finite set. The set Finset.range n is . It does not contain . A product is written in the same way with ∏.
theorem Finset.sum_range_id_mul_two (n : ℕ) :
(∑ i ∈ Finset.range n, i) * 2 = n * (n - 1)
This is the sum , times 2. It equals . The subtraction is in ℕ, so for it is , and both sides are 0.
Example. For , the left side is and the right side is .
Example. Knuth writes sums such as . In Lean this is ∑ k ∈ Finset.Icc 1 n, a k, where Finset.Icc 1 n is the set . A reader must check the range of each sum: range n starts at 0 and stops before , Icc 1 n starts at 1 and stops at .
From the book to a Lean statement
Each item of TAOCP that claims something gets a Lean statement. The statement has a name that comes from the number of the item in the book, so you can find it from the number alone. This section gives the naming rules and three worked examples.
The names
The Lean files of the project are in the repository tamnd/taocp-lean, under the root TAOCP (spec 06.8). The names follow the numbers of the book:
| Item in the book | Lean name |
|---|---|
| Section 1.2.6 | the namespace TAOCP.S1_2_6 |
| equation (21) of Section 1.2.6 | TAOCP.S1_2_6.eq21 |
| Theorem A of Section 5.1.4 | TAOCP.S5_1_4.theoremA |
| Algorithm E of Section 1.1 | the namespace TAOCP.S1_1.E, with the method TAOCP.S1_1.E.method |
| Program M of Section 1.3.2 | TAOCP.S1_3_2.programM |
| exercise 42 of Section 1.2.6 | the namespace TAOCP.S1_2_6.Ex42 |
| part (a) of that exercise | TAOCP.S1_2_6.Ex42.a |
A section number such as 1.2.6 becomes S1_2_6 in a name, because a dot in a Lean name has another meaning.
The problem and its solution are in two files. The file TAOCP/Problems/S1_2_6/Ex42.lean has the docstring, the answer (when there is one) and the statement. The file TAOCP/Solutions/S1_2_6/Ex42.lean has the proof, theorem solved : statement. So you can read a problem without seeing its proof, and the statement is fixed before anybody writes a proof.
The docstring gives the exercise in the project's own words, with its number and its rating. It never copies the sentence of the book.
The functions of Knuth's notation that Mathlib does not have are in TAOCP/Notation. The most common ones are these:
| Lean | Knuth's notation | Book |
|---|---|---|
TAOCP.kmod x y |
for real and | 1.2.4 |
TAOCP.binom r k |
for any and integer | 1.2.6 |
TAOCP.iverson P |
, which is 1 when is true and 0 otherwise | 1.2.3 |
TAOCP.harmonicPow r n |
1.2.7 | |
TAOCP.fibInt n |
for integer | 1.2.8 |
TAOCP.stirling1 n k, TAOCP.stirling2 n k |
the Stirling numbers | 1.2.6 |
TAOCP.eulerian n k |
the Eulerian numbers | 5.1.3 |
Example. Mathlib's Nat.choose n k takes two natural numbers. For and it gives 0, as the book does. But Knuth also writes and . These need TAOCP.binom. The value of TAOCP.binom (-1 : ℚ) 3 is . The value of TAOCP.binom (1/2 : ℚ) 2 is .
Example. For a negative lower index, TAOCP.binom r k is 0, whatever is. The lemma TAOCP.binom_of_neg says this. The bridge lemma TAOCP.binom_natCast says that TAOCP.binom and Nat.choose agree on natural numbers.
First worked example: an identity of Section 1.2.6
Equation 1.2.6(21) is Vandermonde's convolution. In the book it holds for all real and and every integer . The spec sketches its statement like this (spec 06.10):
/-- Vandermonde's convolution, 1.2.6-(21), for real upper indices. -/
theorem TAOCP.S1_2_6.eq21 (r s : ℝ) (n : ℤ) :
∑ᶠ k : ℤ, binom r k * binom s (n - k) = binom (r + s) n
Read it part by part.
(r s : ℝ): the upper indices are real numbers, not only natural numbers. A statement overℕwould be weaker than the book. A fidelity check (F1, Section 5.4) looks for this kind of weakening.(n : ℤ): the lower index is any integer. For , both sides are 0.∑ᶠ k : ℤ: a sum over all integers . This is Mathlib'sfinsum. It is the sum of the terms that are not zero, when there are only finitely many of them. Here the term is 0 when (the first factor) and when (the second factor). So only from 0 to count. This matches Knuth's convention that a sum over with no range is over all integers.
Mathlib proves the special case for natural numbers as Nat.add_choose_eq. The general statement is a separate theorem of the project, and a bridge lemma links the two.
Example. Take , and . The left side is . The right side is .
Example. Take , and . Every term of the left side has a factor with or a factor with . So the left side is 0. The right side is .
Second worked example: Algorithm 1.1E
Algorithm 1.1E is Euclid's algorithm. The file TAOCP/V1/S1_1.lean writes it with the command knuth_algorithm, in the step format of the book:
knuth_algorithm E (m n : ℕ) : ℕ requires 0 < m ∧ 0 < n where
var r : ℕ := 0
E1 "Divide." r := m % n
E2 "Test the remainder." if r = 0 then halt n
E3 "Shift." m := n; n := r; goto E1
Each line after var is one step of the book. The command makes the namespace TAOCP.S1_1.E. A state is a pair of a step label (E1, E2, E3 or done) and a store with the variables , and and the output result. The computational method is TAOCP.S1_1.E.method. The first state for the inputs and is TAOCP.S1_1.E.start m n, and TAOCP.S1_1.E.run x k is the state after steps from the state x.
The correctness of the algorithm is the theorem TAOCP.S1_1.E.correct:
theorem correct (m n : ℕ) (hn : 0 < n) (h : method.Terminates (start m n)) :
output (start m n) h = Nat.gcd m n
Read it: for natural numbers and with , if the computation from and stops, then its output is . The proof h that the computation stops is an argument, because the output is defined only for a computation that stops. The termination is a separate theorem, TAOCP.S1_1.E.terminates. It says that E.method is an algorithm: from each input that satisfies the requirement, the computation reaches done. The theorem TAOCP.S1_1.E.steps_start also gives the number of steps, , where is the number of times step E1 runs.
Example. Start with and . The steps are E1 (), E2, E3 (, ), E1 (), E2, E3 (, ), E1 (), E2. After 8 steps the state is at done with store , , and result . Here , so . And . In Lean, #eval TAOCP.S1_1.E.run (TAOCP.S1_1.E.start 255 105) 8 prints this state.
Example. The command asks for and , as the book does, so the inputs of the method are only those states. The theorem correct needs only . Without it, step E1 would compute . In Lean m % 0 = m, so the step does not fail. But the book does not define the algorithm for that input, and no theorem may claim anything about it.
Third worked example: a problem file
Here is a problem file of the K3 kind, which asks for a value. It is an illustration for this guide, not a node of the map.
import TAOCP.Notation
/-!
# Illustration (K3)
Find a closed form for the sum of `k * C(n, k)` over `k` from 0 to `n`.
-/
namespace TAOCP.Demo.SumKChoose
/-- The closed form `n 2^(n-1)`, written as `n 2^n / 2` to avoid
truncated subtraction. -/
def answer (n : ℕ) : ℚ := n * 2 ^ n / 2
def statement : Prop :=
∀ n : ℕ, (∑ k ∈ Finset.range (n + 1), (k : ℚ) * n.choose k) = answer n
end TAOCP.Demo.SumKChoose
The file has no proof. The proof goes to TAOCP/Solutions/... as theorem solved : statement. Note three details. The sum is over range (n + 1), so goes from 0 to . The terms are cast to ℚ, so no subtraction is truncated. And the answer is written as in ℚ, which is for and 0 for .
Example. For , the sum is , and answer 3 is .
Example. If answer were written as n * 2 ^ (n - 1) over ℕ, then answer 0 would be , which is also right. But a reader must check this by hand. The form over ℚ needs no check. This is the kind of detail that the fidelity battery examines (Section 5.4).
What answer, statement and solved mean
Every exercise of TAOCP with formal content gets a namespace with up to three names: answer, statement and solved. Each name makes one promise to a reader. This section says what each promise is and what it is not.
The three names
| Name | Where | What it is | What it promises |
|---|---|---|---|
answer |
the problem file | the value that the exercise asks for | the value is in closed form, and it agrees with small cases and with Knuth's answer |
statement |
the problem file | a Prop: the claim of the exercise, with answer in it |
the claim says what the book says |
solved |
the solution file | theorem solved : statement |
the Lean kernel checked a proof of the claim |
The kind of the exercise (Section 2.5) fixes which names occur:
| Kind | answer |
statement |
solved |
|---|---|---|---|
| K1 prove | no | yes | yes |
| K2 decide | yes, of type Bool |
yes | yes |
| K3 find a value, K4 find an estimate, K8 compute by machine | yes | yes | yes |
| K5 to K7 | sometimes | yes | yes |
| K9 research | no | yes | never, while the problem is open |
| informal | no | no | no |
The promise of statement
A statement is a precise claim. It can be wrong in two ways. It can be false, and then no proof exists. Or it can be true but say something other than the book. The second error is the dangerous one, because a proof does not find it. A proof of a wrong statement is a correct proof of the wrong thing.
So each statement goes through the fidelity battery before its status becomes faithful (spec 06.6, and Section 5.4 of this guide). Only after that does the promise hold. The statement is then pinned. If it changes, its status goes back to stated, and the battery runs again.
Example. An exercise asks to prove a fact for every byte size of MIX. A statement that proves it only for byte size 64 is weaker than the book. Check F1 looks for a parameter that the book lets vary but the statement fixes.
Example. An exercise asks to prove a bound for . A statement with the hypothesis and the extra hypothesis is true and easy, but it is not the book's claim. Check F5 catches this: a model writes the Lean statement back in English, and a second reader compares the two.
The promise of answer
For K2 to K4 and K8, the answer is the value that the exercise asks for. The battery checks it in three ways. F2 computes small cases by brute force and compares. F3 compares with Knuth's answer in the book. F4 checks that the answer is in closed form: it uses only an allowed list of functions, and it does not just repeat the quantity that it should give.
Example. For the quantity , the answer is in closed form. The answer fun n => ∑ k ∈ Finset.range (n + 1), k * n.choose k is true but empty: it is the quantity itself. F4 rejects it.
Example. For a K2 exercise, the answer is true or false. Take the claim "if is prime, then is prime". The statement has the form
def answer : Bool := false
def statement : Prop :=
(∀ n : ℕ, Nat.Prime n → Nat.Prime (2 ^ n - 1)) ↔ answer = true
The answer is false, because . The statement is then "the claim is equivalent to false = true", which says that the claim is false. A proof of solved must show this, for example with the counterexample .
The promise of solved
solved is a theorem in the solution file. Lean has checked it. Its type is exactly statement, so the proof proves the claim with the answer in it. A proof that uses a value other than answer does not type check.
The promise has one more condition: the proof uses only the standard axioms. Section 4.4 shows how to check this. A proof that uses compiled evaluation is proved-native (Section 4.5). A proof that takes a published result as a hypothesis is proved-mod (Section 4.6). The handbook and the site never call these two just "proved".
Example. When the status of an exercise is proved, the handbook prints the Lean statement and a link to the solution file. You can read the statement, look up the axioms, and trust the result as far as you trust the Lean kernel and the fidelity checks.
Example. When the status is stated, there is a statement and no proof. The handbook may still give a proof in words. The text then says that the Lean proof is not done, and it never says "proved in Lean" (spec 11.3).
Reading the output of print axioms
A Lean proof rests on the rules of the Lean kernel and on a list of axioms. The command #print axioms shows the list for one theorem. It is the fastest way to see whether a proof is clean. The project runs it on every solution (spec 14.1, row Q2), but you can run it yourself.
The command and its output
You write the command after the theorem, in the same file or in a file that imports it:
#print axioms TAOCP.S1_2_6.Ex42.solved
Lean answers in one of two forms. Either "the theorem does not depend on any axioms", or "the theorem depends on axioms" followed by a list in square brackets. The output for a typical proof that uses Mathlib is:
'TAOCP.S1_2_6.Ex42.solved' depends on axioms: [propext, Classical.choice, Quot.sound]
This line is an illustration, and that solution may not exist yet. The form of the line is what Lean prints.
The three standard axioms
Almost every proof in Mathlib uses three axioms. The project accepts exactly these three for the status proved.
| Axiom | What it says, in plain words |
|---|---|
propext |
two statements that imply each other are equal |
Classical.choice |
from a proof that a type has an element, you can pick an element |
Quot.sound |
two elements that are related are equal in the quotient by the relation |
From Classical.choice, Lean derives the law of the excluded middle: every statement is true or false. From Quot.sound, it derives that two functions with the same values are equal. Ordinary mathematics uses both facts all the time, often without notice. So a list with some or all of these three is normal.
What other entries mean
| Entry in the list | Meaning | Status |
|---|---|---|
| none, or some of the three standard axioms | a clean proof | proved |
sorryAx |
the proof has a gap marked sorry |
not proved; never in the main tree |
a name with _native in it |
the proof uses compiled evaluation (Section 4.5) | proved-native |
| any other name | the proof uses a new axiom | rejected by CI (spec 09.2) |
A named hypothesis does not show up here. It is a hypothesis of the theorem, not an axiom, so it is in the statement, where you can read it (Section 4.6).
Example. Here are four small theorems and the output of #print axioms for each, run with Lean v4.36.0, the version that the project uses:
theorem t1 : 2 + 2 = 4 := rfl
theorem t2 (p : Prop) : p ∨ ¬p := Classical.em p
theorem t3 : Nat.gcd 255 105 = 15 := by decide
theorem t5 (n : Nat) : n + 0 = n := by sorry
't1' does not depend on any axioms
't2' depends on axioms: [propext, Classical.choice, Quot.sound]
't3' does not depend on any axioms
't5' depends on axioms: [sorryAx]
The proof of t1 and of t3 is a computation in the kernel, and it needs no axiom. The excluded middle in t2 needs all three. And t5 is not proved at all, even though Lean accepts the file with a warning.
Example. Suppose that you read a solution file, and its theorem has the list [propext, Classical.choice, Quot.sound, TAOCP.myFact]. The file declares a new axiom called myFact. Such a proof proves nothing until myFact is proved. The CI of the project fails on a new axiom, so this cannot happen in the main tree. But it can happen in a file that somebody sends you, so the check is worth the ten seconds.
What the check does not tell you
A clean list says that the proof is complete. It does not say that the statement is the right one. That is the job of the fidelity battery (Section 5.4). The two checks together give the status of a node.
What proved-native means
Some results of TAOCP come from a large computation: a count of objects, a table of values, a search that shows that no small object exists. Lean can check such a result in two ways. This section explains the difference and the status that each way gives.
Two ways to compute
The first way is computation in the kernel. Tactics such as decide and rfl reduce the claim to true with the same rules that check every other proof. The kernel is small and has been checked carefully, so this way adds no trust. The status is proved. The kernel has fast arithmetic for natural numbers, so a moderately large computation is fine.
The second way is compiled evaluation. Tactics such as native_decide and bv_decide compile the claim to machine code, run it, and then accept the result as an axiom. This way is much faster. But it trusts more: the Lean compiler, the code that it calls, and the machine that runs it. A bug in any of these could make a false claim pass. The status is proved-native.
Example. The claim can be proved in both ways:
theorem t3 : Nat.gcd 255 105 = 15 := by decide
theorem t4 : Nat.gcd 255 105 = 15 := by native_decide
With Lean v4.36.0, #print axioms gives:
't3' does not depend on any axioms
't4' depends on axioms: [t4._native.native_decide.ax_1_1]
The tactic native_decide added a new axiom, which says that the compiled code returned true. The name of that axiom contains _native. That is how a reader, and the scan of the project, sees that the proof is native.
Example. Older versions of Lean showed the same fact in another way: the list contained Lean.ofReduceBool or Lean.trustCompiler. The spec of the project names those two axioms (spec 06.7). With the pinned version v4.36.0, the list contains an axiom with _native in its name instead. Both forms mean the same thing: the proof trusts the compiler.
When the project accepts a native proof
A native proof is allowed only under these rules (spec 06.7 and 09.2):
- It is in a file of
Solutions/orCertificate/, and that file is listed inmanifests/native.yaml. - The node has a lab record of the computation: what was computed, on which machine, and how long it took.
- When possible, an independent program computes the result again. For a SAT result, the verified checker
cake_lprchecks the proof certificate. - The docstring of the proof says that it uses a computation, with the lab record and the time.
A kernel proof is always preferred. A native proof is accepted when the kernel computation is too slow for the build, and then the project keeps looking for a faster kernel proof or a certificate that the kernel can check.
How the status is shown
The site and the handbook print "proved with compiled evaluation" for such a node, with the computation, its time and the result of the independent recount (spec 14.3). The word "proved" never stands alone for it. No count on the site adds proved and proved-native without saying so.
Example. An exercise of Volume 4 might ask for the number of Boolean functions of five variables with a given cost. A program can count them. In the kernel, the count may take hours. With native_decide, it may take a minute. The node is then proved-native. Its page shows the count, the minute, and a second count by an independent program in C.
Named hypotheses and proved-mod
Some proofs need a published result that nobody has written in Lean yet. Examples are general theorems of complex analysis that the analysis of an algorithm uses. The project does not stop and wait for them. It also does not add them as axioms. It states each one as a named hypothesis, and the theorems that use it take it as an assumption. This section explains how that works and what the status proved-mod means.
A hypothesis, not an axiom
A named hypothesis is a definition of a statement, of type Prop, in the file TAOCP/Library/Hypotheses.lean (spec 09.2). Its docstring gives the source in the literature. A theorem that needs it takes a proof of it as a hypothesis:
/-- Source: a published paper or book, cited in full. -/
def Hyp.someResult : Prop := ∀ n : ℕ, ...
theorem foo (h : Hyp.someResult) : ... := ...
This is different from an axiom in three ways. First, the hypothesis is in the statement of foo, so every reader sees it. Second, #print axioms foo stays clean, because nothing new was assumed globally. Third, when somebody later proves Hyp.someResult in Lean, the proof of foo applied to that proof gives the result with no hypothesis, and nothing else changes.
Example. Bertrand's postulate says that for every there is a prime with . Suppose that Lean did not have it. Then the project would write
/-- Bertrand's postulate. Source: Chebyshev (1852). -/
def Hyp.bertrand : Prop :=
∀ n : ℕ, n ≠ 0 → ∃ p, Nat.Prime p ∧ n < p ∧ p ≤ 2 * n
A theorem that needs a prime between and would take (h : Hyp.bertrand). In fact Mathlib proves this statement as Nat.exists_prime_lt_and_le_two_mul, also known as Nat.bertrand. So the project does not need this hypothesis. The example only shows the shape.
Example. For the postulate gives a prime in : there are four of them, 11, 13, 17 and 19. For it gives .
What proved-mod means
A node is proved-mod when its proof is clean and one of two things holds (spec 08.3):
- The proof takes a named hypothesis.
- The proof uses another node of the map that is not proved yet.
In both cases the result is proved "modulo" something: if the missing part holds, then the result holds. The site and the handbook print the hypotheses under the result, each with its source, and they name each unproved node that the proof uses (spec 14.3).
Example. Suppose that the proof of a node in Section 1.2.10 uses an identity of Section 1.2.7, and that identity is stated but not yet proved. Then the node of 1.2.10 is proved-mod, and its page names the identity of 1.2.7. When the identity becomes proved, the node of 1.2.10 becomes proved with no change to its own proof.
Example. A K4 exercise asks for an asymptotic form. Its proof uses a transfer theorem from analytic combinatorics in a general form that Mathlib lacks. The node is proved-mod. Its page prints the transfer theorem in words, cites its source, and links to the row of the table of black boxes (Chapter 6).
How many hypotheses
The project keeps the list short. Each hypothesis is a row of the table of black boxes, and each row is a task: somebody should prove it in Lean, ideally for Mathlib. The first expected rows are analytic results used by the HM exercises (spec 09.2). Chapter 6 shows the table.
Checking a proof on your own machine
You do not have to trust the site. You can get the Lean files and check any proof yourself. This section gives the steps. You need a computer with several gigabytes of free disk space and a network connection for the first download.
The steps
- Install
elan, the installer of Lean. It is atgithub.com/leanprover/elan, and the page gives one command for each system.elanreads the filelean-toolchainof a project and installs the right version of Lean by itself. - Get the files:
git clone https://github.com/tamnd/taocp-lean, then go into the new folder. - Get the compiled Mathlib:
lake exe cache get. This downloads the compiled files of Mathlib, so that you do not compile Mathlib yourself. It takes several gigabytes. - Build the project:
lake build. This compiles the files ofTAOCP. When it ends with no error, every proof in the main tree has been checked by the Lean kernel on your machine. - Check the escape hatches:
sh scripts/scan.sh. It looks forsorry, a newaxiom,native_decideand the other forbidden forms outside the allowed files. It prints nothing and exits with code 0 when the tree is clean.
The project is pinned to Lean v4.36.0 and Mathlib v4.36.0 (spec 09.3). The file lean-toolchain holds the version, and lake-manifest.json holds the exact commit of Mathlib. Do not change them. A different version of Mathlib may rename lemmas, and then some files do not build.
Checking one theorem
To check one theorem, write a small file, for example Check.lean, at the top of the folder:
import TAOCP
#check TAOCP.binom_natCast
#print axioms TAOCP.binom_natCast
Then run lake env lean Check.lean. The command #check prints the statement. The command #print axioms prints the list of Section 4.4. You can also open the folder in an editor with the Lean extension, such as VS Code, and put the cursor on a line to see the output.
Example. The command #eval computes a value. Knuth defines so that the result has the sign of . So . The line #eval TAOCP.kmod (7 : ℚ) (-3) prints -2. The integer % of Lean is Int.emod, and it gives (7 : ℤ) % (-3) = 1, because its result is never negative. Lean also has Int.fmod, and Int.fmod 7 (-3) gives -2, as Knuth does. This is why the project has its own TAOCP.kmod.
Example. For a positive divisor, the three agree. , and (-7 : ℤ) % 3 and Int.fmod (-7) 3 are also 2. The lemma TAOCP.kmod_intCast_natCast says that TAOCP.kmod and % agree when the divisor is a natural number.
What to do when a build fails
A failed build on the pinned versions is a bug of the project. Note the file, the line and the error, and open an issue in tamnd/taocp-lean. Section 3.6 says how to report an error in the handbook itself.
Things to try
- Read the statement of
Nat.sum_range_choosein Section 4.1, and write it as a sentence. Then check it by hand for . P2 teaches the identity. - Write the Lean name of exercise 3 of Section 1.2.4, and the paths of its problem file and its solution file (Section 4.2).
- Run Algorithm 1.1E by hand on and , step by step, in the form of the example of Section 4.2. Count the steps until
done. P7 teaches the step format. - For each kind K1 to K9, say which of
answer,statementandsolvedits namespace has (Section 4.3). - Compute , and with Knuth's definition, then with
Int.emod. P2 Chapter 4 teaches the floor and mod functions. - If you have the disk space, follow the steps of this section and run
#print axiomson one theorem ofTAOCP/Notation. Theorem Proving in Lean 4 teaches more Lean.
The project states the items of TAOCP in Lean, a language in which a computer checks every step of a proof. This chapter teaches you to read those statements. It does not teach you to write proofs. For that, read Theorem Proving in Lean 4 and Mathematics in Lean. P7 uses Lean to state and prove facts about programs.
Section 4.1 gives enough Lean to read a statement: types, functions, theorems, quantifiers, sums and products. Section 4.2 shows how a result of TAOCP becomes a Lean statement, with three worked examples. Section 4.3 explains the three names answer, statement and solved. Section 4.4 shows how to read the output of #print axioms. Section 4.5 explains the status proved-native. Section 4.6 explains named hypotheses and the status proved-mod. Section 4.7 says how to get the Lean files and check a proof on your own machine.
Every section of the handbook has a Lean part. The path "Lean contributor" reads this chapter and Chapter 5. Other readers can read Sections 4.1 to 4.3 and skip the rest until they need it.
The source of this page. Something is wrong? Report it.