My Lean Work — Proof Systems, Measure Theory, and the Principle of Optimality in Lean4 · Fall 2024

From my Proof Systems presentation and my lean-theorem-proving README — what I built, what I learned, and how I tracked it, in date order. This page summarizes my own works and learning, not a general stance on the subject.

Watch the video: https://youtu.be/2oBB_k_ePuc

2:27 at 1.25× — the version to post. The normal-speed version (3:04) is here, and the subtitles are here. A square 1080 version for Instagram is here.

The story, in order

  1. 10 September – 5 December 2024 — New Hampshire. Volunteering to learn Lean, for the CS department, in Prof. Marek Petrik's RLLab.
  2. 8 October 2024. Proof Systems — what a proof is, and how proof theory meets computation, linguistics, logic, and semiotics.
  3. 4 November 2024. Two MIT courses — Functional Analysis and Measure Theory — to understand Mathlib's conditional expectation.
  4. 7 November 2024. Six steps to plan a proof in Lean.
  5. 5 December 2024. Final presentation for the Lean project.

September 10 – December 5, 2024: Volunteering to Learn Lean

I started as a volunteer, giving knowledge transfer as a courtesy to the computer science department. The RLLab was organized by Professor Marek Petrik, for Fall 2024, and my final presentation was on December 5th, 2024. I am not affiliated with the Reinforcement Lab.

October 8, 2024: Proof Systems

What is a proof · What is a derivation · Logic as a formal system · Propositional logic · First-order logic · Second-order logic · Decidability · Completeness · Soundness · Turing machines · Natural deduction · Tableau method · Sequent calculus · Axiomatic derivations · Lean proof systems

November 4, 2024: Two MIT Courses

To understand how Mathlib implements conditional expectation, I completed two MIT OpenCourseWare courses: “Introduction to Functional Analysis” and “Measure Theory”. Each one is 8–10 hours, with 100+ pages of lecture notes. For Functional Analysis, lectures 1, 2, 4, 6, 7, 8, 9 and 10 are enough.

Lean Builds Bottom-Up

To build a theorem on conditional expectation, define sigma-algebras, measures, and function maps in a measure space. A random variable is a measurable function:

variable {Ω : Type*} [MeasurableSpace Ω] {X : Ω → ℝ} (hX : Measurable X)

Expectation is noncomputable — an integral over theoretical, symbolic functions. A probability mass function maps α → ℝ≥0∞ with Σf = 1.

The Target: E[E[x|y]] = E[x]

In the discrete case, it follows from the law of marginal probability. In the continuous case, from the joint, marginal and conditional densities.

This was a smaller test of how much Lean I had understood — the main project was verifying the Principle of Optimality in Lean4.

November 7, 2024: Six Steps to Plan a Proof

  1. Write down the original proof.
  2. Write down the lemmas and axioms it needs.
  3. Check what Lean already provides.
  4. What we want may differ from what Lean provides.
  5. Writing from scratch may reinvent the wheel.
  6. Tactics matter — prove LHS = RHS and RHS = LHS.

For tactics, I studied RFL — the reflexive tactic, applied on any type, no matter how complex. Marek reviewed my DeterministicDP file, with comments on Finset sums, ranges and products. Further reading: Formalizing the Beginnings of Bayesian Probability Theory in the Lean Theorem Prover, UC San Diego.

Main Project: the Principle of Optimality in Lean4

October 24 – December 1, 2024. The main project was Automated Theorem Proving using Lean4 Prover to verify a foundational theorem of Principle of Optimality in Reinforcement Learning. The goal, set with Prof. Marek Petrik, was to implement deterministic dynamic programming theorems in Lean4 — with the expectation from the start that the proof might not be completed by the December 1 deadline.

The two theorems

Principle of Optimality (Theorem 0.1). If (a*₀, …, a*_{T−1}) is an optimal control sequence and (s*₀, …, s*_T) the corresponding state sequence, then the sub-sequence (a*_k, …, a*_{T−1}) is optimal from s*_k, for each k = 0, …, T−1.

Dynamic Programming Equations (Theorem 0.2). The optimal value function v*_t is the unique value satisfying v*_T(s) = r_T(s) and v*_t(s) = max_a [ r_t(s, a) + v*_{t+1}(f_t(s, a)) ], for t = 0, …, T−1.

The setup

A deterministic dynamic program: a finite state set S, a finite action set A, a horizon T, reward functions r_t: S × A → ℝ and r_T: S → ℝ, a transition function f_t: S × A → S, and an initial state s₀. The value function v_t is the sum of rewards from t to T; the optimal value function v*_t is the maximum of it over all action sequences; the objective is to maximize the return v₀(s₀). The proof is by backward induction on t — the case t = T follows directly from the definition, and assuming v*_{t+1} is the unique solution gives v*_t.

My five-step proof plan

  1. Start with the optimal value function v*_t (6).
  2. Substitute the value function v_t from (3) in the right-hand side of v*_t in (6).
  3. Expand v_t in the right-hand side of (6) with (3) in terms of r_t (4).
  4. Replace the max term in the right-hand side of v*_t with v_{t+1}, i.e. (5).
  5. Replace the right-hand side of (7) in the left-hand side of the same equation, i.e. (7) in the resulting equation in step 4.

The Lean4 implementation — a rough draft

I chose only one theorem for proof, wrote down all the definitions, the proof plan, and then the axioms and functions in Lean4. The draft defines a FiniteProbability structure (a finite set Ω with a probability p — nonneg, total probability 1) and an MDP structure (finite S and A, s_0, horizon T, reward r, transition f), plus the value functions, the optimal value functions, and a max function. The proof attempt is example principle_of_optimality : v_star_t(s) = max_{a_t:A} r_t(s, a_t) + v_star_{t+1}(f_t(s, a_t)) := by rfl.

I used example rather than theorem because theorem is retained in the system and some tactics did not yet apply the underlying proofs — an example is type-checked and then deleted, which makes it a good way to test whether tactics apply correctly. I used the rfl tactic, which has 6 types of conversions, for repeated left-to-right conversions. This is only a rough draft and the proof gives errors — it is not complete, and that was the expectation from the beginning.

Background

Lean (De Moura et al., 2015) is an interactive theorem prover bridging interactive and automated proving; Lean4 (de Moura and Ullrich, 2021) is Lean implemented in itself; Mathlib (Avigad et al., 2020) is for formalizing mathematics in Lean. I drew on proof planning (Bundy, 1998) and the Hitchhiker's Guide to Logical Verification (Baanen et al., 2021). I also found that a lot of theorems on Bayesian probability were missing from Mathlib, which mattered for Markov chains and reinforcement learning — Vaishnav (2022) formalized Bayesian probability in Lean3, but Lean3 and Lean4 are not backward compatible, so that work would need permission and rewriting.

How I tracked it, in order

Write the proof first, check what the prover already has, and do not reinvent the wheel.

Tracking evidence

Proof Systems presentation slides — Google Slides (published) lean-meeting-notes — Google Doc (weekly meetings and Zulip chats) Zulip chats — Google Drive folder (unhlean-status)

My reinforcement learning coursework and mini project now live on a separate page: My RL Work — Homework and Mini Projects.

Resources

cs-851a-rl — mini project and main project repo My lean-theorem-proving README, in full Main project report — Automated Theorem Proving using Lean4 to verify the Principle of Optimality, October 24 – December 1, 2024 (PDF) My works at lean-theorem-proving My work at the University of New Hampshire — research-experience / university-of-new-hampshire