# Chapter 16: Introduction to Theorem Proving

Part I treated First as a programming language. Part II uses it as a **proof assistant** — a tool for stating and machine-verifying mathematical theorems.

The key insight: in First, a **theorem** is just a function that returns a proposition, and **proving** it means providing a body the type checker accepts. All proofs are erased at compile time (they emit a constant `1` at runtime), so there is zero overhead for shipping verified code.

---

## Why theorem proving in a programming language?

Proof assistants like Lean 4, Coq, and Agda live separately from the languages used to build systems. First unifies them: you write your algorithm, state its correctness theorem, and prove both in the same file, with the same compiler.

This is useful for:
- **Cryptographic libraries** — formally verify security properties
- **Numerical algorithms** — prove convergence or error bounds
- **Data structures** — prove invariants (a sorted array stays sorted after insert)
- **Compilers / interpreters** — prove preservation and progress
- **Mathematics** — develop library theorems for reuse

---

## Propositions and proofs

In First's type theory, a **proposition** is a type, and a **proof** is a value of that type. The simplest proposition is `Eq(a, b)` — propositional equality.

```first
// Eq(a, b) is the type "a equals b"
// A proof of Eq(a, b) is a value of type Eq(a, b)
```

A `theorem` declaration says: "I claim this proposition holds, and here is a proof."

```first
// Claim: 1 + 1 = 2
theorem one_plus_one() -> Eq(1 + 1, 2) by {
    refl;
}
```

The `by { ... }` block is a **tactic proof** — a sequence of proof steps that the compiler executes to verify the claim. `refl` succeeds because `1 + 1` reduces to `2`, making both sides identical.

---

## Your first theorem: refl

`refl` (reflexivity) proves `Eq(a, a)` — any value equals itself, or any expression equal to itself after reduction.

```first
theorem three_times_two() -> Eq(3 * 2, 6) by {
    refl;
}

theorem string_eq() -> Eq("hello", "hello") by {
    refl;
}
```

`refl` works whenever the two sides are **definitionally equal** — either literally the same, or one reduces to the other by computation.

---

## Proof irrelevance

**All proofs of the same proposition are considered equal.** This is called proof irrelevance, and it is fundamental to First's design.

```first
// Two different proof terms for the same theorem
theorem add_zero_v1(n: Int) -> Eq(n + 0, n) by { ring; }
theorem add_zero_v2(n: Int) -> Eq(n + 0, n) by { omega; }

// Both have the same type. At runtime, both emit the constant 1.
// The choice of proof strategy is an implementation detail.
```

Proof irrelevance means:
1. You can always replace one proof with another proof of the same statement.
2. You can store a proof in a variable and pass it around without caring which specific proof object it is.
3. The compiler only checks **that** you proved something, not **how**.

---

## Using Eq

`Eq` is both a proposition and a usable value. When you have a proof `h: Eq(a, b)`, you can use it in further reasoning.

```first
// Use h to rewrite in a proof
theorem double(n: Int) -> Eq(n + n, 2 * n) by {
    ring;
}

// Pass a proof as an argument
theorem helper(n: Int, h: Eq(n, 5)) -> Eq(n + 1, 6) by {
    have : Eq(5 + 1, 6) by { refl; }
    rw [h];
    exact this;
}
```

---

## Theorems are exported and reused

Like functions, theorems can be `export`ed and used in proofs elsewhere:

```first
// In Int.first
export theorem add_comm(a: Int, b: Int) -> Eq(a + b, b + a) by { ring; }

// In your file
import "Int";

theorem my_theorem(x: Int, y: Int) -> Eq(x + y + 1, y + x + 1) by {
    have h := add_comm(x, y);
    rw [h];
    refl;
}
```

This is how `first-mathlib` works: it's a collection of exported theorems you can apply by name.

---

## The five tiers of tactics

First's tactics are organised into five tiers, from simplest to most powerful:

| Tier | Tactics | Good for |
|------|---------|----------|
| 1 — Trivial | `refl`, `rfl`, `decide`, `trivial` | Definitional equality, decidable props |
| 2 — Arithmetic | `ring`, `omega`, `linarith`, `norm_num` | Algebraic identities, integer arithmetic |
| 3 — Automation | `simp`, `simp [h]`, `exact h`, `assumption` | Rewriting with known lemmas |
| 4 — Structural | `induction`, `cases`, `have`, `obtain` | Inductive proofs, case analysis |
| 5 — Calc | `calc`, `by_contra`, `push_neg`, `contrapose` | Multi-step chains, classical logic |

Chapter 17 covers Tiers 1–3 in depth. Chapter 18 covers Tiers 4–5.

---

## Axioms — trusted propositions

Not everything can be proved from first principles within the language. For foundational mathematical truths, First provides the `axiom` keyword:

```first
// axiom: a trusted statement with no proof body
export axiom real_add_comm(a: Real, b: Real) -> Eq(a + b, b + a);
```

An `axiom` compiles exactly like a `theorem`: the return type is a proposition, the body emits `i64 1`, and callers can use it in `exact` / `apply`. The difference is that the compiler emits a **warning** for every `axiom`, reminding you it is trusted rather than proved.

A consistent set of axioms is fine. The danger is adding contradictory axioms (which would let you prove `False` and then anything). The standard library's axioms in `Real.first`, `Rat.first`, etc., are well-known mathematical truths.

---

## sorry — placeholder proofs

`sorry` is a tactic that proves anything, without checking the proof. It is useful when you are exploring a proof strategy:

```first
theorem hard_conjecture(n: Int) -> Eq(n * n - n + 41, ???) by {
    sorry;   // placeholder — will come back to this
}
```

The compiler accepts `sorry` but emits a warning: `warning: proof uses sorry`. Never ship code with `sorry` in a theorem you care about — it makes the proof worthless.

---

## Propositions, theorems, axioms — a summary

| Keyword | Has proof body? | Compiler checks proof? | Warning? |
|---------|----------------|----------------------|---------|
| `theorem` | Yes (`by { ... }`) | Yes (full verification) | No |
| `axiom` | No | No (trusted) | Yes: axiom warning |
| `theorem` with `sorry` | Yes (uses sorry) | No for that step | Yes: sorry warning |

---

## A complete worked example

```first
import "Int";

// Lemma: (a + b)^2 = a^2 + 2ab + b^2
theorem expand_square(a: Int, b: Int) ->
    Eq((a + b) * (a + b), a * a + 2 * a * b + b * b) by {
    ring;
}

// Theorem: the square of any integer is non-negative
// (requires the Real or Int order axioms)
theorem sq_nonneg(a: Int) -> Eq(a * a >= 0, true) by {
    omega;
}
```

---

## Summary

- A `theorem` is a function returning a proposition, proved with tactics
- `Eq(a, b)` is the fundamental proposition: propositional equality
- **Proof irrelevance**: all proofs of the same proposition are equal; they emit `1` at runtime
- `axiom` — trusted statement; compiles like a theorem; emits a warning
- `sorry` — placeholder proof; always emits a warning
- Five tiers of tactics, from `refl` to `calc` (Chapters 17–18)

Chapter 17 dives into the first three tiers of tactics.
