Proving Theorems with Lean and Machine Learning

Rémy Degenne Univ. Lille, Inria, CNRS, Centrale Lille, CRIStAL; Mathlib maintainer

Wenda Li University of Edinburgh

Université de Lille

Inria

University of Edinburgh

With support from ​ ​

ICARM

This tutorial

  1. Proving (machine learning) theorems with Lean

  2. Machine learning for theorem proving

https://leanmachinelearning.org/icml-tutorial/

Lean is a proof assistant

A programming language in which you can write theorems and proofs.

A compiler that checks that your proofs are correct.

2024

Deepmind silver medal, IMO 2024

with Lean proofs

2025

Harmonic gold medal, IMO 2025

Lean

OpenAI gold medal, IMO 2026

No Lean

2026

Aletheia first proof, 2026

OpenAI Erdős

Now with Lean proof

The challenge

Aletheia Erdős, 2026

https://www.erdosproblems.com/ by Thomas Bloom

Human grading, expert mathematicians are expensive.

Lean

A functional programming language

def helloWorld : IO Unit := IO.println "Hello, world!" #eval helloWorld
Hello, world!

A proof assistant

variable {Ω : Type*} [ : MeasurableSpace Ω] [StandardBorelSpace Ω] {P : Measure Ω} [IsProbabilityMeasure P] {X : Ω } {c : ℝ≥0} { : Filtration } /-- Chernoff tail bound for sub-Gaussian random variables. -/ example (h_adapted : StronglyAdapted X) (h0 : HasSubgaussianMGF (X 0) (c 0) P) (n : ) (h_subG : i < n - 1, HasCondSubgaussianMGF ( i) (.le i) (X (i + 1)) (c (i + 1)) P) {ε : } ( : 0 ε) : P.real {ω | ε i range n, X i ω} exp (-ε ^ 2 / (2 * i range n, c i)) := (HasSubgaussianMGF.sum_of_hasCondSubgaussianMGF h_adapted h0 n h_subG).measure_ge_le

Other proof assistants: Rocq, Isabelle, HOL, Agda, Mizar...

An extensible language

  • Write proofs about programs.

  • Write programs that produce proofs.

And more: these slides are generated from a Lean file!

Formal mathematics with Lean

You want to prove a theorem in Lean

  1. You import some results you need from a library

  2. You write a statement in the Lean language

  3. You write a proof, using tactics (programs)

  4. The Lean kernel checks that your proof is correct

Now your theorem can be used in other proofs, locally.

Next step: you add your result to a library

Notable projects in formal mathematics

Before Lean, before AI.

  • 2005: Four colour theorem (Rocq)

  • 2012: Odd Order Theorem (Rocq)

  • 2014 Kepler's conjecture (Isabelle/HOL Light)

Liquid Tensor Experiment

Challenge by Peter Scholze, 2020

LTE Challenge

LTE Challenge

Completed in 2022. Johan Commelin, Adam Topaz,

Reid Barton, Alex J. Best, Riccardo Brasca, Kevin Buzzard, Yaël Dillies, Floris van Doorn, Fabian Glöckle, Markus Himmel, Heather Macbeth, Patrick Massot, Bhavik Mehta, Kim Morrison, Filippo A. E. Nuccio, Joël Riou, Damiano Testa, Andrew Yang

PFR conjecture

PFR Challenge

Proved by Gowers, Green, Manners and Tao, 2023

example {A : Set G} {K : } (h₀A : A.Nonempty) (hA : Nat.card (A + A) K * Nat.card A) : (H : Submodule (ZMod 2) G) (c : Set G), Nat.card c < 2 * K ^ 12 Nat.card H Nat.card A A c + H := sorry

Formalized, 3 weeks later.

Terence Tao, Yaël Dillies, Rémy Degenne, Sébastien Gouëzel, Kalle Kytölä, Paul Lezeau, Arend Mellendijk, Floris van Doorn, Lawrence Wu, Ben Eltschig, Kim Morrison, Utensil Song, Heather Macbeth, Mantas Bakšys, Jonas Bayer, Mauricio Collares, Patrick Massot, Kyle Miller, Kaiyu Yang, Oliver Nash, Aaron Anderson, Sky Wilshaw, Rob Lewis, Pietro Monticone, Thomas Bloom, Adam Topaz, Kevin Buzzard

CEDAR

A language for access control. Define who is authorized to do what within your application.

Cedar Analysis toolkit by AWS.

  • a compiler implemented in Lean translates Cedar policies into mathematical formulas.

  • prove properties of the policies (e.g. only users with appropriate permissions can access a resource)

SampCert

AWS's SampCert library: verifed differential privacy algorithms deployed in their Clean Rooms service.

Uses Mathlib (Lean's mathematical library), for example the Poisson summation formula.

example {f : } (hc : Continuous f) {b : } (hb : 1 < b) (hf : f =O[cocompact ] fun x |x| ^ (-b)) (hFf : (𝓕 f) =O[cocompact ] fun x |x| ^ (-b)) (x : ) : ∑' n : , f (x + n) = ∑' n : , 𝓕 f n * fourier n (x : UnitAddCircle) := Real.tsum_eq_tsum_fourier_of_rpow_decay hc hb hf hFf x

Why formalize mathematics?

  • verification: check correctness, trust

  • understanding: search the library, explore the proof

  • creation: remove a hypothesis and see what breaks, find generalizations, use automation

  • collaboration: leverage the trust to work together

New reason: check AI output

Collaboration

25 collaborators on PFR. How?

Lean checks the proof: I don't need to know you and trust you're good at maths to believe your proof.

Blueprint

Blueprint tool by Patrick Massot

AI + formal math possibilities

  1. You write a new proof, and write it in Lean

  2. You write a new proof, and an AI checks it with Lean

  3. You ask an AI to prove parts of your argument, and check it with Lean

  4. You research a problem in conversation with an AI with Lean access

  5. You throw money at an AI to prove your theorem, with a Lean proof

Writing proofs in Lean

variable {𝓧 : Type*} [MeasurableSpace 𝓧] {f g : 𝓧 ℝ≥0∞} (hf : Measurable f) (hg : Measurable g) example : Measurable (fun x f x + (g x / 2)) := by
apply Measurable.add hf ?_
apply Measurable.div_const ?_ 2
exact hg
example : Measurable (fun x f x + (g x / 2)) := by exact hf.add (hg.div_const 2)
example : Measurable (fun x f x + (g x / 2)) := by fun_prop
example [TopologicalSpace 𝓧] (f g : 𝓧 ) (hf : Continuous f) (hg : Continuous g) : Continuous (fun x f x + (g x / 2)) := by fun_prop

Writing proofs in Lean

lemma wrong_proof (hf : Measurable f) (hg : Measurable g) : Measurable (fun x f x + (g x / 2)) := by apply Continuous.add
lemma incomplete_lemma (hf : Measurable f) (hg : Measurable g) : Measurable (fun x f x + (g x / 2)) := by sorry
lemma sorryful_lemma (hf : Measurable f) (hg : Measurable g) : Measurable (fun x f x + (g x / 2)) := by exact incomplete_lemma hf hg #print axioms sorryful_lemma

Lean checks the proof, reports errors, emits warnings

Definitions and theorems

/-- My favorite number. -/ def myFavoriteNumber : := 42 theorem myFavoriteNumber_eq_40_plus_2 : myFavoriteNumber = 40 + 2 := by unfold myFavoriteNumber simp theorem even_myFavoriteNumber : Even myFavoriteNumber := by unfold myFavoriteNumber grind #check myFavoriteNumber
myFavoriteNumber :
#check Even myFavoriteNumber
Even myFavoriteNumber : Prop
#check even_myFavoriteNumber
even_myFavoriteNumber : Even myFavoriteNumber

Wrong definitions

def IsSubGaussianProcess (μ : Measure Ω) (X : E Ω ) (σ : ) : Prop := s t : E, l : , ω, exp (l * (X s ω - X t ω)) μ exp (l^2 * σ^2 * (dist s t)^2 / 2)

What if \exp(l (X_s - X_t)) is not integrable?

The integral is 0 by definition. Not what we want.

Lean checks proofs, it does not read your mind.

Lean Mathematical Ecosystem

  • Lean

  • Mathlib: mathematical library

  • Downstream libraries: CSLib, PhysLib, FLT, LML...

Discussions on Zulip: https://leanprover.zulipchat.com

Mathlib

A community-driven library of formalized mathematics in Lean.

  • open source (772 contributors)

  • maintained (29 maintainers), carefully reviewed (58+ reviewers)

  • infrastucture: CI, deprecation cycle, testing, documentation

  • definitions written in the right generality, applicable to many situations

  • definitions written in the right generality, applicable to many situations

Not merely formalized: digested, generalized, organized, reusable.

Formalized matematics in Mathlib

Mathlib contains most of an undergraduate mathematics curriculum.

More in some areas, less in others.

Analysis in Mathlib

Formalizing machine learning

  • Many prerequisites are there, in measure theory, probability theory (discrete time), functional analysis, linear algebra, optimization, ...

  • Everything ML-specific is missing from Mathlib

  • With Paulo Rauber, we formalized basic bandit algorithms and optimization proofs.

  • Others have auto-formalized optimization or stochastic learning theory results (like the IsSubGaussianProcess definition above).

Can I ask my LLM to do it?

Possibly, but.

The Mathlib Halo

Mathlib halo

Beyond the halo, you need a very good harness, domain knowledge and/or lots of resources for your AI to produce working code.

Cheating

/-- Totally legit prime definition. -/ def IsPrime (n : ) : Prop := true
theorem twin_primes : {p : | IsPrime p IsPrime (p + 2)}.Infinite := by simp [IsPrime] exact Set.infinite_univ #print axioms twin_primes

Cheating

Lean is extensible: you can redefine everything.

  • You can change Nat.Prime to always return true.

  • You can redefine #print axioms to hide sorry.

Your LLM can and will cheat.

A solution: use the Comparator tool.

Quality issues

  • restricted definition (perhaps the general one requires too much background)

  • too many hypotheses (need to be checked at every use)

  • slow, inefficient code

  • unmaintainable, brittle code

  • lack of understanding

  • lack of attribution

Piles of definitions

The result of 500k lines of AI formalization:

Object A has property P under assumption H.

A depends on 50 new definitions. Same for P and H.

How do you audit the definitions?

Lean Machine Learning

https://leanmachinelearning.org

A library of formalized machine learning theory definitions

/-- A stochastic, sequential algorithm. -/ structure Algorithm (𝓐 𝓨 : Type*) [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] where /-- Policy: distribution of the next action. -/ policy : (n : ) Kernel (Iic n 𝓐 × 𝓨) 𝓐 [h_policy : n, IsMarkovKernel (policy n)] /-- Distribution of the first action. -/ p0 : Measure 𝓐 [hp0 : IsProbabilityMeasure p0]

Demo: online gradient descent

x_0 \in E, \eta > 0, (g_n) \in E^{\mathbb{N}}

x_{n+1} = x_n - \eta g_n

Goal:

\sum_{i=0}^{n-1} \langle x_i - y, g_i \rangle \leq \frac{1}{2\eta} \|x_0 - y\|^2 + \frac{\eta}{2} \sum_{i=0}^{n-1} \|g_i\|^2

Demo: online gradient descent

open Real Finset open scoped RealInnerProductSpace variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace E] {γ : } {η : } lemma inner_eq_add (x y g : E) ( : 0 < η) : x - y, g = (2 * η)⁻¹ * (x - y ^ 2 - (x - η g) - y ^ 2) + (η / 2) * g ^ 2 := by have hsub : (x - η g) - y = (x - y) - η g := by abel rw [hsub, norm_sub_sq_real (x - y) (η g)] simp only [inner_smul_right, norm_smul, norm_eq_abs, abs_of_pos ] field lemma sum_inner_le_sum' (x y g : E) ( : n, 0 < γ n) (n : ) : i range n, x i - y i, g i i range n, ((2 * γ i)⁻¹ * (x i - y i ^ 2 - (x i - γ i g i) - y i ^ 2) + (γ i / 2) * g i ^ 2) := by gcongr with i hi rw [inner_eq_add (x i) (y i) (g i) ( i)] lemma sum_inner_le_sum (x g : E) (y : E) ( : n, 0 < γ n) (hx : n, x (n + 1) = x n - γ n g n) (n : ) : i range n, x i - y, g i i range n, ((2 * γ i)⁻¹ * (x i - y ^ 2 - x (i + 1) - y ^ 2) + (γ i / 2) * g i ^ 2) := (sum_inner_le_sum' x (fun _ y) g n).trans_eq (by simp [hx]) section ConstantStep lemma sum_inner_le_add (x g : E) (y : E) ( : 0 < η) (hx : n, x (n + 1) = x n - η g n) (n : ) : i range n, x i - y, g i (2 * η)⁻¹ * (x 0 - y ^ 2 - x n - y ^ 2) + (η / 2) * i range n, g i ^ 2 := by refine (sum_inner_le_sum x g y (fun _ ) hx n).trans_eq ?_ rw [sum_add_distrib, mul_sum, mul_sum, sum_range_sub' (fun i x i - y ^ 2) n] /-- Regret bound for gradient descent and linear losses. -/ lemma regret_bound (x g : E) (y : E) (η : ) ( : 0 < η) (hx : n, x (n + 1) = x n - η g n) (n : ) : i range n, x i - y, g i (2 * η)⁻¹ * x 0 - y ^ 2 + (η / 2) * i range n, g i ^ 2 := by grw [sum_inner_le_add x g y hx n] gcongr exact sub_le_self _ (sq_nonneg _) end ConstantStep

proving vs understanding

The product of mathematics is clarity and understanding. Not theorems, by themselves.

William Thurston

We are transitioning in mathematics from an era of proof scarcity to an era of proof abundance, but our mathematical infrastructure and culture has not yet adapted to this. As mentioned previously, there is now a strong (and growing) impedance mismatch between the three core components of mathematical problem solving: proof generation, proof verification, and proof digestion.

Terence Tao

  • There is value in formalizing quickly to be able to check research results

  • There is value in carefully designing a library to be able to further our understanding

Questions?

Formal mathematics is ready for large scale adoption.

https://leanmachinelearning.org/workshop/