---
marp: true
paginate: true
---

# Formal proofs

Something we can *really* throw tokens at

---

# Content

1. "Verified in Lean" means?
2. What can go wrong?
3. OK. So? Do we care?

---

# 1. "Verified in Lean" means?

Lean is *an advanced type checker*

---

# Proofs are programs

- A **theorem** is a type
- A **proof** is a program (a term, a function) of that type
- The type checker verifies that the proof **actually** has the claimed type

```coq
Theorem left_or (A B : Prop) : A -> A \/ B.   (* theorem: the type *)
Proof (fun a => or_introl a).                 (* proof: a term of that type *)
```

---

# Proving A ∧ B

A proof is a pair: a proof of A and a proof of B.

```
     a : A        b : B
──────────────────────────
   conj a b : A /\ B
```

---

# Proving A ∨ B

A flag saying which side, plus a proof of that side.

```
        a : A                         b : B
────────────────────────      ────────────────────────
or_introl a : A \/ B          or_intror b : A \/ B
```

```coq
or_introl : forall A B : Prop, A -> A \/ B
or_intror : forall A B : Prop, B -> A \/ B
```

---

# Proving ∀ a : A, B a

A function that, for each `a`, constructs a proof of `B a`.

Example: A → A ∨ B

```coq
Theorem left_or : forall A B : Prop, A -> A \/ B.
Proof (fun A B a => or_introl a).
```

---

# Proving ∃ a : A, B a

A concrete witness, plus a proof that it works.

```coq
Theorem twelve : exists n : nat, n + n = 24.
Proof (ex_intro _ 12 eq_refl).
```

---

# Proof *assistant*

<style scoped>
.cols { display: grid; grid-template-columns: 1.15fr 1fr; gap: 1.2em; }
pre { font-size: 0.62em; }
.cols p, .cols li { font-size: 0.8em; }
</style>

You write **tactics**, interactively. Try it: [jscoq.github.io](https://jscoq.github.io)

<div class="cols">
<div>

```coq
Theorem plus_zero : forall n : nat, n + 0 = n.
Proof.
  intros n.
  induction n.
  - (* goal: 0 + 0 = 0 *)
    reflexivity.
  - (* IHn:  n + 0 = n
       goal: S n + 0 = S n *)
    transitivity (S (n + 0)).
    + (* S n + 0 = S (n + 0) *)
      reflexivity.
    + (* S (n + 0) = S n *)
      rewrite IHn.
      reflexivity.
Qed.
```

</div>
<div>

**What does `+` mean?**

```coq
Locate "+".
Print Nat.add.
```

**What term did we build?**

```coq
Show Proof.        (* mid-proof *)
Print plus_zero.   (* after Qed *)
Check nat_ind.
```

</div>
</div>

---

# Bragging

My formalization

[madiot.fr/rocq100](https://madiot.fr/rocq100/)

2000 loc

---

# 2. What can go wrong?

Do we still need humans?

---

# 2.1 The proof does not typecheck

---

# 2.2 Proving the wrong thing

```lean
def FermatsLastTheorem : Prop :=
  ∀ a b c n : ℕ, n ≥ 3 → a^n + b^n ≠ c^n
```

Looks right, but it is false: ℕ contains 0, and 0³ + 0³ = 0³.

OpenAI released 290 theorems with Lean code. In ~38 of them, the Lean code does not state the paper's main theorem.

---

# 2.3 Assuming something invalid

At first glance it looks OK:

```lean
axiom helpful_lemma : (1 : ℕ) = 2

theorem anything (P : Prop) : P :=
  absurd helpful_lemma (by decide)
```

`#print axioms anything` reveals it.

---

# 2.4 The type checker has a bug

The kernel (the "compiler" of proofs) can have bugs, and so can anything else you choose to trust.

- Lean ≤ 4.33.1: a "checked" proof of Fermat's Last Theorem, via a string bug in compiled code trusted by `native_decide` (Trail of Bits, Sep 2026)
- Mitigation: several independent checkers (`lake comparator --paranoid`)

---

# 3. OK. So? Do we care?

~~Proving software is correct~~

Writing software in Lean/Rocq

```coq
Definition compile (src : string) : option string := (* … *).

Theorem compile_correct : forall src : string,
  IsValidC src ->
  exists asm, compile src = Some asm /\ SameSemantics src asm.
```

---

# Typically

1. Implement the program in Lean / Rocq
2. Prove its properties
3. Export the algorithm to ordinary code (Rocq → OCaml / Haskell); the proofs are erased

```coq
Require Extraction.
Extraction Language OCaml.
Extraction compile.
```

---

# In practice

A bunch of software **has** been formally verified:

- **CompCert**: a C compiler (Rocq, exported to OCaml)
- **seL4**: an operating system kernel (Isabelle)
- **CakeML**: an ML compiler (HOL4)
- **Cedar**: AWS's authorization language (Lean)

---

# Proving things about EXISTING code

You need the language's full semantics, at least the parts you use.

- Rust: Aeneas translates (safe) Rust into Lean
- C: CompCert's C semantics, VST (Rocq)
- Python: …

---

# Fancy terminology

| Term | Means |
|---|---|
| **Lambda calculus** | this notation for writing programs |
| **Per Martin-Löf type theory** | this way of formulating math |
| **Curry–Howard correspondence** | programs are proofs, statements are types |
| **Intuitionistic** | maximally painful |
| **Calculus of Inductive Constructions** | ask me later |
