Lean 4 Theorem Proving β€” Formal Proof Verification

Machine-checked mathematical proofs using Lean 4 and Mathlib. Learn how to write and verify formal proofs with induction, case analysis, and powerful tactics.

Introduction

AutEng integrates Lean 4 with Mathlib for formal theorem proving. Unlike CAS verification (which uses heuristic symbolic computation), Lean provides machine-checked proofsβ€”if your proof compiles, it's mathematically correct.

Every Lean proof is either verified (βœ…), has errors (❌), or is pending (πŸ”„). There's no "unknown" stateβ€”Lean's type system guarantees correctness.

When to Use Lean vs CAS
  • CAS (SymPy): Quick algebraic identity checks (e.g., sinΒ²x + cosΒ²x = 1)
  • Lean 4: Formal proofs requiring induction, case analysis, or Mathlib lemmas

Quick Start

Wrap your Lean code in a :::lean block. The system will compile and verify it using Lean 4 + Mathlib.

Example 1: Simple Theorem βœ…

A basic proof using the omega tactic:

Addition Commutativity

Proving that addition is commutative using the omega tactic

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2
3theorem add_comm_example (a b : β„•) : a + b = b + a := by
4 omega

Expected Result: βœ… Verified β€” The proof type-checks successfully.

Example 2: Proof by Induction βœ…

A classic result: the sum of the first n odd numbers equals nΒ². This requires induction and the ring tactic.

Sum of Odd Numbers

1 + 3 + 5 + ... + (2n-1) = nΒ²

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2import Mathlib.Algebra.BigOperators.Group.Finset.Basic
3
4open Finset BigOperators
5
6theorem sum_odd_numbers (n : β„•) : βˆ‘ i ∈ range n, (2 * i + 1) = n^2 := by
7 induction n with
8 | zero => simp
9 | succ n ih =>
10 rw [sum_range_succ, ih]
11 ring

Expected Result: βœ… Verified β€” The induction proof is complete.

Example 3: Incomplete Proof ❌

Using sorry marks a proof as incomplete. Lean will report this as an error:

Incomplete Proof

The sorry tactic marks unfinished proofs

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2
3theorem incomplete_example (n : β„•) : n + 0 = n := by
4 sorry

Expected Result: ❌ Failed β€” Declaration uses 'sorry'.

Common Tactics

Lean 4 with Mathlib provides powerful tactics for constructing proofs:

TacticUse Case
simpSimplify using simp lemmas (powerful automation)
ringProve ring equalities (polynomial identities)
omegaInteger/natural number arithmetic
linarithLinear arithmetic reasoning
norm_numNumeric computations
rflReflexivity (terms are definitionally equal)
exactProvide an exact proof term
applyApply a lemma or hypothesis
inductionProof by induction
casesCase analysis

Example: Using Multiple Tactics

Combining Tactics

A proof using simp, ring, and induction together

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2
3-- Sum of first n natural numbers
4theorem sum_naturals (n : β„•) : 2 * (βˆ‘ i ∈ Finset.range (n + 1), i) = n * (n + 1) := by
5 induction n with
6 | zero => simp
7 | succ n ih =>
8 simp [Finset.sum_range_succ]
9 linarith

Common Imports

Mathlib provides a vast library of mathematical definitions and lemmas. Here are the most commonly used imports:

ImportProvides
Mathlib.TacticGeneral tactics (simp, ring, omega, linarith, etc.)
Mathlib.Data.Nat.BasicNatural number operations
Mathlib.Data.Real.BasicReal number operations
Mathlib.Data.Nat.Prime.BasicPrime number definitions
Mathlib.Algebra.BigOperators.Group.Finset.BasicFinite sums and products (βˆ‘, ∏)
Import Loading Time
The first verification request may take 50-60 seconds while Mathlib imports are loaded. Subsequent requests complete in 2-5 seconds as the server keeps imports cached.

CAS vs Lean: When to Use Each

Both CAS and Lean verify mathematical correctness, but they serve different purposes:

CAS (SymPy)

  • βœ“Algebraic identity verification
  • βœ“Symbolic simplification
  • βœ“Equation solving steps
  • βœ“Fast (~1-5 seconds)
  • βœ—Heuristic (can return UNKNOWN)

Lean 4 + Mathlib

  • βœ“Induction proofs
  • βœ“Case analysis
  • βœ“Complex reasoning chains
  • βœ“Absolute correctness guarantee
  • βœ—Slower first request (~50-60s cold)

Example Comparison

Verifying sinΒ²x + cosΒ²x = 1:

CAS Approach (Recommended)

Quick algebraic verification with SymPy

Not Verified
sin⁑2(x)+cos⁑2(x)\sin^2(x) + \cos^2(x)
=
11

Proving βˆ‘(2i+1) = nΒ² for all n:

Lean Approach (Required)

Formal proof by inductionβ€”CAS cannot do this

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2import Mathlib.Algebra.BigOperators.Group.Finset.Basic
3
4open Finset BigOperators
5
6theorem sum_odd_eq_square (n : β„•) : βˆ‘ i ∈ range n, (2 * i + 1) = n^2 := by
7 induction n with
8 | zero => simp
9 | succ n ih => rw [sum_range_succ, ih]; ring

Error Messages

When a proof fails, Lean provides detailed error messages with line numbers and suggestions:

Type Mismatch

Type Error Example

Lean catches type mismatches at compile time

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2
3-- This will fail: can't prove n = n + 1
4theorem wrong_claim (n : β„•) : n = n + 1 := by
5 rfl

Expected Error: type mismatch β€” rfl requires definitionally equal terms.

Tactic Failed

Tactic Failure Example

When a tactic can't solve the goal

Not Verified
Lean 4 + Mathlib
1import Mathlib.Tactic
2
3-- omega can't prove this (it's false!)
4theorem false_claim (n : β„•) : n > n := by
5 omega

Expected Error: omega could not prove the goal.

Best Practices

Start with import Mathlib.Tactic
This single import gives you access to most common tactics (simp, ring, omega, linarith, norm_num, etc.).
Use simp for Simplification
The simp tactic is incredibly powerful. It can often solve goals automatically or simplify them significantly.
Break Complex Proofs into Lemmas
If a proof is getting long, extract intermediate results as separate lemmas. This makes proofs more readable and reusable.
Use ring for Polynomial Identities
The ring tactic automatically proves any polynomial identity. It's perfect for algebraic manipulations.
Check the Goal State
Use #check to see types and example to test small proofs before incorporating them into larger theorems.

Reference

Block Syntax

AttributeValuesDefault
modecheckcheck
enginelean4lean4
timeoutinteger (ms)30000

Status Indicators

StatusMeaning
βœ… VerifiedProof type-checks successfully
❌ FailedCompilation errors (type mismatch, tactic failed, etc.)
⏱️ TimeoutVerification exceeded time limit
πŸ”„ PendingVerification in progress

Performance

ScenarioTime
First request (cold start)50-60 seconds
Subsequent requests (warm)2-5 seconds
Different imports needed50-60 seconds (re-initialization)

Next Steps

Ready to explore more verification options? Check out these related guides:

Related Content

Ready to Start?

Start creating beautiful technical documentation with AutEng.

Get Started with AutEng