This problem set prepares your capstone (Lecture 14). It has two
halves. The first is a worked example: a small formalization planned with the seven-part
proposal template (Definition 1) and then built sorry-first. Its proofs are your problems,
from the sanity checks (Problem 1, Problem 2) through two lemmas (Problem 3,
Problem 4) to the main theorem (Problem 5). The second half is your own proposal
(Problem 6), written in the same format.
How to work on it. Open the lean/ folder (not the course root) in VS Code with the Lean 4
extension, then open LeanFV/Homework/HW06.lean. It imports Mathlib, and every definition in
it is complete. Each problem below names its theorem in that file, such as HW06.kolm_self.
The file builds now, with one sorry warning per theorem, and is finished when none are left.
The solutions are collapsed under each problem, and LeanFV/Solutions/HW06.lean has the same
statements, proved.
The proposal template
A proposal is short: one or two pages of prose and a Lean file that builds. Its job is to make
the hard decisions before you are deep in a proof.
Definition 1 (Capstone proposal).
A capstone proposal has seven parts.
Problem. The statement in ordinary mathematics, with its source (paper, book,
equation number), and why it is worth formalizing.
Representation. How each object becomes a Lean type, which alternatives you
considered, and what Mathlib or Physlib already provides.
Definitions. The Lean definitions, each with a docstring.
Sanity checks. Small facts the definitions must satisfy, a statement showing that a
hypothesis is needed, and an example showing that all the hypotheses can hold at once.
Main statement. The Lean statement, with an argument that it says what part 1 says.
Blueprint. Five to ten nodes, each a definition or a lemma, with what each one uses.
Risks. The node most likely to fail, why, and what you will do if it does.
Parts 3, 4 and 6 are Lean, stated sorry-first; parts 1, 2, 5 and 7 are prose. The rest of this
page is a complete proposal for a small theorem, written part by part.
A worked example: the classical Fuchs–van de Graaf inequality
1. Problem. For probability vectors on outcomes, the Kolmogorov distance is
and the Bhattacharyya coefficient is
. They satisfy
2. Representation. A probability vector becomes a function Fin n → ℝ together with a
separate hypothesis IsProb p: the entries are nonnegative and sum to 1. This choice is called
unbundled, because the property is not part of the type. The bundled alternative is a type
of vectors that carry their own proof, and Mathlib has one:
import Mathlib#check stdSimplex
Lean prints
stdSimplex.{u_1, u_2} (𝕜 : Type u_2) (ι : Type u_1) [Semiring 𝕜] [PartialOrder 𝕜] [Fintype ι] : Set (ι → 𝕜)
so the standard simplex is a set of functions. A vector p : stdSimplex ℝ (Fin n) would
carry its proof, at the cost of a coercion back to Fin n → ℝ in every formula. For a project
of ten nodes the unbundled version is simpler; for a library it might not be. One junk value
matters here: Real.sqrt returns 0 for a negative number, so the Lean bhatt below is defined
for every vector but means the Bhattacharyya coefficient only when the entries are
nonnegative.
3. Definitions. Real numbers cannot be computed with exactly, so the two definitions that
take square roots or divide are marked noncomputable.
namespace HW06/-- A probability vector on `n` outcomes. -/def IsProb {n : ℕ} (p : Fin n → ℝ) : Prop := (∀ i, 0 ≤ p i) ∧ ∑ i, p i = 1/-- The Bhattacharyya coefficient `B(p, q) = ∑ √(p i * q i)`. -/noncomputable def bhatt {n : ℕ} (p q : Fin n → ℝ) : ℝ := ∑ i, Real.sqrt (p i * q i)/-- The Kolmogorov distance `K(p, q) = (∑ |p i - q i|) / 2`. -/noncomputable def kolm {n : ℕ} (p q : Fin n → ℝ) : ℝ := (∑ i, |p i - q i|) / 2end HW06
4–5. Sanity checks and the main statement, sorry-first. Every remaining node of the
blueprint, stated with sorry. This is exactly the skeleton in HW06.lean. Lean accepts it
with one sorry warning per theorem: it checks that the statements fit together, and nothing
more. Proving them is the problem set below.
namespace HW06theorem kolm_self {n : ℕ} (p : Fin n → ℝ) : kolm p p = 0 := by sorrytheorem bhatt_self {n : ℕ} {p : Fin n → ℝ} (hp : IsProb p) : bhatt p p = 1 := by sorrytheorem hyp_needed : ¬ (1 - bhatt (fun _ : Fin 2 => (0 : ℝ)) (fun _ => 0) ≤ kolm (fun _ : Fin 2 => (0 : ℝ)) (fun _ => 0)) := by sorrytheorem isProb_half : IsProb (fun _ : Fin 2 => (1 / 2 : ℝ)) := by sorrytheorem sq_sqrt_sub_le {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : (Real.sqrt a - Real.sqrt b) ^ 2 ≤ |a - b| := by sorrytheorem sum_sq_sqrt_sub {n : ℕ} {p q : Fin n → ℝ} (hp : IsProb p) (hq : IsProb q) : ∑ i, (Real.sqrt (p i) - Real.sqrt (q i)) ^ 2 = 2 - 2 * bhatt p q := by sorrytheorem one_sub_bhatt_le_kolm {n : ℕ} {p q : Fin n → ℝ} (hp : IsProb p) (hq : IsProb q) : 1 - bhatt p q ≤ kolm p q := by sorryend HW06
Does one_sub_bhatt_le_kolm say what part 1 says? Read it against the paper, piece by piece:
kolm divides by 2, as Definition 5 does. Without the factor, the inequality would be weaker
and still provable, so no proof attempt would ever catch the slip; only reading does.
bhatt takes , as Definition 7 does, not . The two
agree when the entries are nonnegative, which IsProb guarantees.
Both vectors are assumed to be probability vectors, as in the paper. hyp_needed shows the
inequality fails for the zero vectors, so the hypotheses do real work, and isProb_half
shows they can hold, so the theorem is not vacuous.
The subtraction 1 - bhatt p q is in ℝ, so it cannot be cut off at zero, as it would be
in ℕ.
6. Blueprint. Ten nodes. The proof idea fits in one line: for each ,
; summed over , the left side is and
the right side is .
node
Lean name
kind
uses
1
IsProb
definition
2
bhatt
definition
3
kolm
definition
4
kolm_self
sanity check
3
5
bhatt_self
sanity check
1, 2
6
hyp_needed
a hypothesis is needed
2, 3
7
isProb_half
hypotheses can hold
1
8
sq_sqrt_sub_le
lemma: one index
9
sum_sq_sqrt_sub
lemma: the sum is
1, 2
10
one_sub_bhatt_le_kolm
main theorem
1, 2, 3, 8, 9
7. Risks. Node 9 is the riskiest. It is an identity between sums involving square roots,
and each rewrite of as needs at that index.
If it resists, the fallback is to prove it for first, where the sums can be written
out, to separate the algebra from the bookkeeping. Node 8 needs a case split on which root is
larger, the kind of goal where nlinarith succeeds only with the right facts in context. The
capstone version carries a larger risk, the existence of a measurement that achieves the
fidelity, which Lecture 14 discusses under Option B.
Problems
Problem 1 (Sanity checks).
Prove HW06.kolm_self (a distribution is at distance 0 from itself) and HW06.bhatt_self
(a probability vector has Bhattacharyya coefficient 1 with itself).
Hint
simp [kolm] closes the first. For the second, unfold bhatt and rewrite each term with
Real.sqrt_mul_self, which needs 0 ≤ p i: that is hp.1 i.
Solution
namespace HW06theorem kolm_self {n : ℕ} (p : Fin n → ℝ) : kolm p p = 0 := by simp [kolm]theorem bhatt_self {n : ℕ} {p : Fin n → ℝ} (hp : IsProb p) : bhatt p p = 1 := by unfold bhatt simp only [Real.sqrt_mul_self (hp.1 _)] exact hp.2end HW06
After the rewrite, the goal is ∑ i, p i = 1, which is the second half of IsProb p.
Problem 2 (The hypotheses matter, and can hold).
Prove HW06.hyp_needed: for the zero vectors on two outcomes, which are not probability
vectors, is false. Then prove HW06.isProb_half: the uniform distribution on
two outcomes satisfies IsProb.
Hint
For the zero vectors both sums are 0, so the claim becomes ¬ (1 ≤ 0): try
simp [bhatt, kolm]. For isProb_half, split with constructor and use norm_num, with
Fin.sum_univ_two for the sum.
Solution
namespace HW06theorem hyp_needed : ¬ (1 - bhatt (fun _ : Fin 2 => (0 : ℝ)) (fun _ => 0) ≤ kolm (fun _ : Fin 2 => (0 : ℝ)) (fun _ => 0)) := by simp [bhatt, kolm]theorem isProb_half : IsProb (fun _ : Fin 2 => (1 / 2 : ℝ)) := by constructor · intro i norm_num · norm_num [Fin.sum_univ_two]end HW06
Together these two facts are part 4 of the proposal: without the hypotheses the theorem is
false, and with them it is about something that exists.
Problem 3 (One index at a time).
Prove HW06.sq_sqrt_sub_le: for ,
.
Hint
Put Real.sq_sqrt ha, Real.sq_sqrt hb and Real.sqrt_nonneg for both numbers in context
with have. Split on le_total (Real.sqrt b) (Real.sqrt a), remove the absolute value with
abs_of_nonneg or abs_of_nonpos, and finish with nlinarith.
Solution
namespace HW06theorem sq_sqrt_sub_le {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : (Real.sqrt a - Real.sqrt b) ^ 2 ≤ |a - b| := by have hsa := Real.sq_sqrt ha have hsb := Real.sq_sqrt hb have h0a := Real.sqrt_nonneg a have h0b := Real.sqrt_nonneg b rcases le_total (Real.sqrt b) (Real.sqrt a) with h | h · rw [abs_of_nonneg (by nlinarith)] nlinarith · rw [abs_of_nonpos (by nlinarith)] nlinarithend HW06
On paper: if , then
. The two
nlinarith calls find exactly this product.
Problem 4 (The sum identity).
Prove HW06.sum_sq_sqrt_sub: for probability vectors,
.
Hint
First prove, for each i, that the term equals p i + q i - 2 * Real.sqrt (p i * q i)
(Real.sqrt_mul, sub_sq, Real.sq_sqrt, then ring). Then split the sum with
Finset.sum_sub_distrib and Finset.sum_add_distrib, and use hp.2 and hq.2.
Solution
namespace HW06theorem sum_sq_sqrt_sub {n : ℕ} {p q : Fin n → ℝ} (hp : IsProb p) (hq : IsProb q) : ∑ i, (Real.sqrt (p i) - Real.sqrt (q i)) ^ 2 = 2 - 2 * bhatt p q := by have key : ∀ i, (Real.sqrt (p i) - Real.sqrt (q i)) ^ 2 = p i + q i - 2 * Real.sqrt (p i * q i) := by intro i rw [Real.sqrt_mul (hp.1 i), sub_sq, Real.sq_sqrt (hp.1 i), Real.sq_sqrt (hq.1 i)] ring simp only [key, Finset.sum_sub_distrib, Finset.sum_add_distrib, hp.2, hq.2, bhatt, Finset.mul_sum] ringend HW06
The have key line is the risk from part 7, handled index by index: Real.sqrt_mul needs
0 ≤ p i, which hp.1 i supplies.
Problem 5 (The main theorem).
Prove HW06.one_sub_bhatt_le_kolm: for probability vectors ,
.
Hint
Sum Problem 3 over all indices with Finset.sum_le_sum, rewrite the left side with
Problem 4, unfold kolm, and finish with linarith.
Solution
namespace HW06theorem one_sub_bhatt_le_kolm {n : ℕ} {p q : Fin n → ℝ} (hp : IsProb p) (hq : IsProb q) : 1 - bhatt p q ≤ kolm p q := by have h := Finset.sum_le_sum (s := Finset.univ) (fun i _ => sq_sqrt_sub_le (hp.1 i) (hq.1 i)) rw [sum_sq_sqrt_sub hp hq] at h unfold kolm linarithend HW06#print axioms HW06.one_sub_bhatt_le_kolm
The audit line prints 'HW06.one_sub_bhatt_le_kolm' depends on axioms: [propext, Classical.choice, Quot.sound]:
the three standard axioms and nothing else.
Problem 6 (Your capstone proposal).
Write a proposal for your own capstone in the seven parts of the proposal template
(Definition 1). Parts 1, 2, 5 and 7 are one or two pages of prose; parts 3, 4 and 6 are
a Lean file that builds with every proof sorry. Put the Lean file in a project made with lake new … math (Lecture 14
shows how), or at the end of LeanFV/Homework/HW06.lean inside namespace HW06.Mine. The
blueprint has five to ten nodes, and every node is stated in Lean. There is no model solution;
the worked example above is the model.
Before you hand it in
Does the build report one sorry warning per unproved node, and no errors?
Does part 4 show both that a hypothesis is needed and that the hypotheses can all hold?
Would a reader who sees only your definitions and the Lean statement in part 5 agree that
it says what part 1 says? Ask a classmate.
Is there a special case of your riskiest node that you could prove first?