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.
- 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
1import Mathlib.Tactic23theorem add_comm_example (a b : β) : a + b = b + a := by4 omegaExpected 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Β²
1import Mathlib.Tactic2import Mathlib.Algebra.BigOperators.Group.Finset.Basic34open Finset BigOperators56theorem sum_odd_numbers (n : β) : β i β range n, (2 * i + 1) = n^2 := by7 induction n with8 | zero => simp9 | succ n ih =>10 rw [sum_range_succ, ih]11 ringExpected 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
1import Mathlib.Tactic23theorem incomplete_example (n : β) : n + 0 = n := by4 sorryExpected Result: β Failed β Declaration uses 'sorry'.
Common Tactics
Lean 4 with Mathlib provides powerful tactics for constructing proofs:
| Tactic | Use Case |
|---|---|
| simp | Simplify using simp lemmas (powerful automation) |
| ring | Prove ring equalities (polynomial identities) |
| omega | Integer/natural number arithmetic |
| linarith | Linear arithmetic reasoning |
| norm_num | Numeric computations |
| rfl | Reflexivity (terms are definitionally equal) |
| exact | Provide an exact proof term |
| apply | Apply a lemma or hypothesis |
| induction | Proof by induction |
| cases | Case analysis |
Example: Using Multiple Tactics
Combining Tactics
A proof using simp, ring, and induction together
1import Mathlib.Tactic23-- Sum of first n natural numbers4theorem sum_naturals (n : β) : 2 * (β i β Finset.range (n + 1), i) = n * (n + 1) := by5 induction n with6 | zero => simp7 | succ n ih =>8 simp [Finset.sum_range_succ]9 linarithCommon Imports
Mathlib provides a vast library of mathematical definitions and lemmas. Here are the most commonly used imports:
| Import | Provides |
|---|---|
| Mathlib.Tactic | General tactics (simp, ring, omega, linarith, etc.) |
| Mathlib.Data.Nat.Basic | Natural number operations |
| Mathlib.Data.Real.Basic | Real number operations |
| Mathlib.Data.Nat.Prime.Basic | Prime number definitions |
| Mathlib.Algebra.BigOperators.Group.Finset.Basic | Finite sums and products (β, β) |
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
Proving β(2i+1) = nΒ² for all n:
Lean Approach (Required)
Formal proof by inductionβCAS cannot do this
1import Mathlib.Tactic2import Mathlib.Algebra.BigOperators.Group.Finset.Basic34open Finset BigOperators56theorem sum_odd_eq_square (n : β) : β i β range n, (2 * i + 1) = n^2 := by7 induction n with8 | zero => simp9 | succ n ih => rw [sum_range_succ, ih]; ringError 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
1import Mathlib.Tactic23-- This will fail: can't prove n = n + 14theorem wrong_claim (n : β) : n = n + 1 := by5 rflExpected Error: type mismatch β rfl requires definitionally equal terms.
Tactic Failed
Tactic Failure Example
When a tactic can't solve the goal
1import Mathlib.Tactic23-- omega can't prove this (it's false!)4theorem false_claim (n : β) : n > n := by5 omegaExpected Error: omega could not prove the goal.
Best Practices
simp tactic is incredibly powerful. It can often solve goals automatically or simplify them significantly.ring tactic automatically proves any polynomial identity. It's perfect for algebraic manipulations.#check to see types and example to test small proofs before incorporating them into larger theorems.Reference
Block Syntax
| Attribute | Values | Default |
|---|---|---|
| mode | check | check |
| engine | lean4 | lean4 |
| timeout | integer (ms) | 30000 |
Status Indicators
| Status | Meaning |
|---|---|
| β Verified | Proof type-checks successfully |
| β Failed | Compilation errors (type mismatch, tactic failed, etc.) |
| β±οΈ Timeout | Verification exceeded time limit |
| π Pending | Verification in progress |
Performance
| Scenario | Time |
|---|---|
| First request (cold start) | 50-60 seconds |
| Subsequent requests (warm) | 2-5 seconds |
| Different imports needed | 50-60 seconds (re-initialization) |
Next Steps
Ready to explore more verification options? Check out these related guides:
- CAS Verification Demo β Quick algebraic identity checking with SymPy
- Quadratic Formula Derivation β Step-by-step equation solving with CAS
- KaTeX Syntax Reference β Mathematical notation in Markdown
Related Content
CAS Verification Demo β Automated Math Checking
Interactive examples demonstrating AutEng's Computer Algebra System (CAS) verification. See how mathematical expressions and derivations are automatically verified with SymPy.
Quadratic Formula Derivation β Step-by-Step CAS Verification
Complete derivation of the quadratic formula with automated CAS verification. Learn how mode=solve verifies equation-solving steps and detects lost or extraneous solutions.
KaTeX Syntax Quick Reference β Math Equations in Markdown
Complete reference for KaTeX mathematical notation including common formulas, symbols, and usage patterns for technical documentation.
Calculus in Technical Documentation β Derivatives and Integrals
Master calculus notation in technical docs with KaTeX. Learn derivatives, integrals, limits, and differential equations for software, physics, and engineering documentation.
Set Theory and Logic Notation β Mathematical Foundations
Complete guide to set theory and logic notation in technical documentation. Learn sets, operations, logic symbols, quantifiers, and proof notation with KaTeX.
Ready to Start?
Start creating beautiful technical documentation with AutEng.
Get Started with AutEng