# 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&apos;s mathematically correct.

Every Lean proof is either **verified** (✅), has **errors** (❌), or is **pending** (🔄). There&apos;s no &quot;unknown&quot; state—Lean&apos;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:

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic

theorem add_comm_example (a b : ℕ) : a + b = b + a := by
  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.

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic
import Mathlib.Algebra.BigOperators.Group.Finset.Basic

open Finset BigOperators

theorem sum_odd_numbers (n : ℕ) : ∑ i ∈ range n, (2 * i + 1) = n^2 := by
  induction n with
  | zero => simp
  | succ n ih =>
    rw [sum_range_succ, ih]
    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:

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic

theorem incomplete_example (n : ℕ) : n + 0 = n := by
  sorry

:::

```

**Expected Result:** ❌ Failed — Declaration uses &apos;sorry&apos;.

## 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

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic

-- Sum of first n natural numbers
theorem sum_naturals (n : ℕ) : 2 * (∑ i ∈ Finset.range (n + 1), i) = n * (n + 1) := by
  induction n with
  | zero => simp
  | succ n ih =>
    simp [Finset.sum_range_succ]
    linarith

:::

```

## Common 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 (∑, ∏) |

> **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:

- ✓ Algebraic identity verification
- ✓ Symbolic simplification
- ✓ Equation solving steps
- ✓ Fast (~1-5 seconds)
- ✗ Heuristic (can return UNKNOWN)

- ✓ Induction proofs
- ✓ Case analysis
- ✓ Complex reasoning chains
- ✓ Absolute correctness guarantee
- ✗ Slower first request (~50-60s cold)

### Example Comparison

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

```markdown

:::cas mode=equivalence engine=sympy
$$ \\sin^2(x) + \\cos^2(x) = 1 $$
:::

```

**Proving ∑(2i+1) = n² for all n:**

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic
import Mathlib.Algebra.BigOperators.Group.Finset.Basic

open Finset BigOperators

theorem sum_odd_eq_square (n : ℕ) : ∑ i ∈ range n, (2 * i + 1) = n^2 := by
  induction n with
  | zero => simp
  | 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

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic

-- This will fail: can't prove n = n + 1
theorem wrong_claim (n : ℕ) : n = n + 1 := by
  rfl

:::

```

**Expected Error:** type mismatch — rfl requires definitionally equal terms.

### Tactic Failed

```markdown

:::lean mode=check engine=lean4

import Mathlib.Tactic

-- omega can't prove this (it's false!)
theorem false_claim (n : ℕ) : n > n := by
  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&apos;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

| 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](/docs/math/verification/cas-demo) — Quick algebraic identity checking with SymPy
- [Quadratic Formula Derivation](/docs/math/verification/quadratic-formula-derivation) — Step-by-step equation solving with CAS
- [KaTeX Syntax Reference](/docs/math/notation/katex-syntax) — Mathematical notation in Markdown
