2.4. Structured Proofs and Dependent Types

In the previous chapter we learned to write backward proofs: we start from the goal and decompose it with tactics such as intro and apply until we reach hypotheses. In this chapter we study the complementary style, that of forward proofs, in which we start from the hypotheses and build the conclusion step by step, naming intermediate results. This style is closer to the way we write proofs in mathematics.

The two approaches complement each other and are frequently combined:

  • backward: from the goal toward the hypotheses, with intro and apply;

  • forward: from the hypotheses toward the goal, with have, show, and calc.

The main theoretical novelty of the chapter, however, is the PAT principle (Propositions as Types, Proofs as Terms):

\text{Propositions} = \text{Types} \qquad \text{Proofs} = \text{Terms}

It reveals that proving and programming are the same activity, and that structured proofs and tactic proofs are merely convenient ways of writing lambda terms. To state it precisely, we will need to extend the simple type theory of Chapter 8 with dependent types.

2.4.1. Structured proofs

A structured proof is syntactic sugar over lambda terms: more readable than the raw term, but equally formal. The starting point is the observation that the name of a theorem is already a proof term. Consider addition and its commutativity (proved in the previous chapter):

namespace Ch11 def add : ℕ → ℕ → ℕ | m, Nat.zero => m | m, Nat.succ n => Nat.succ (add m n) theorem add_zero (n : ℕ) : add 0 n = n := n:ℕ⊢ add 0 n = n induction n with ⊢ add 0 0 = 0 All goals completed! 🐙 n:ℕih:add 0 n = n⊢ add 0 (n + 1) = n + 1 All goals completed! 🐙 theorem add_succ (m n : ℕ) : add (Nat.succ m) n = Nat.succ (add m n) := m:ℕn:ℕ⊢ add m.succ n = (add m n).succ induction n with m:ℕ⊢ add m.succ 0 = (add m 0).succ All goals completed! 🐙 m:ℕn:ℕih:add m.succ n = (add m n).succ⊢ add m.succ (n + 1) = (add m (n + 1)).succ All goals completed! 🐙 theorem add_comm (m n : ℕ) : add m n = add n m := m:ℕn:ℕ⊢ add m n = add n m induction n with m:ℕ⊢ add m 0 = add 0 m All goals completed! 🐙 m:ℕn:ℕih:add m n = add n m⊢ add m (n + 1) = add (n + 1) m All goals completed! 🐙

The theorem add_comm has type ∀ m n : ℕ, add m n = add n m, and so it behaves like a function: applied to 0 and n, it produces a proof of add 0 n = add n 0.

theorem add_comm_zero_left (n : ℕ) : add 0 n = add n 0 := add_comm 0 n

2.4.1.1. Introducing variables and hypotheses

In the literature (in particular in the book The Hitchhiker's Guide to Logical Verification, which we follow closely) one finds the constructs fix x : T, which introduces a universally quantified variable, and assume h : P, which introduces the hypothesis of a goal P → Q. They come from Lean 3 and from teaching libraries; they do not exist in standard Lean 4. Their role is played by the abstraction fun itself:In short: fix x : T and assume h : P correspond to fun (x : T) ↦ … and fun (h : P) ↦ … in term mode, or to intro x and intro h in tactic mode.

  • fun x : T ↦ … introduces a variable to prove ∀ x : T, …;

  • fun h : P ↦ … introduces a hypothesis to prove P → ….

This is no coincidence: as we will see, proving ∀ or → is building a function.

2.4.1.2. show and have

The construct show T from proof documents the current goal and supplies its proof. It is useful for making a proof readable, and also when the goal can be restated up to definitional equality (Lean checks that T is definitionally equal to the goal). Thus the proof of \forall a\; b : \mathsf{Prop},\; a \to b \to a becomes:

theorem fst_of_two_props : ∀ a b : Prop, a → b → a := fun a b : Prop ↦ fun ha : a ↦ fun hb : b ↦ show a from ha

The construct have h : P := proof proves an intermediate result P and names it h for later use. Composition of implications, which we proved backward in the previous chapter, looks like this when written forward:

theorem prop_comp (a b c : Prop) (hab : a → b) (hbc : b → c) : a → c := fun ha : a ↦ have hb : b := hab ha have hc : c := hbc hb show c from hc

Read it from top to bottom: we have a; hence we have b; hence we have c. The intermediate steps can be inlined when the proof is short:

theorem prop_comp_inline (a b c : Prop) (hab : a → b) (hbc : b → c) : a → c := fun ha : a ↦ show c from hbc (hab ha)

When the variables and hypotheses already appear in the theorem's signature, to the left of the :, they are already in the context and need not be introduced. The two proofs below are equivalent:

theorem fst (a b : Prop) : a → b → a := fun ha : a ↦ fun _ : b ↦ show a from ha theorem fst' (a b : Prop) (ha : a) (_ : b) : a := ha

The name _ indicates a hypothesis that will not be used.

2.4.1.3. Connectives in structured proofs

The introduction and elimination rules for the connectives (Chapter 10) are Lean constants and can be used directly in terms. Swapping a conjunction, written forward, extracts each component and then recombines them:

theorem And_swap (a b : Prop) : a ∧ b → b ∧ a := fun hab : a ∧ b ↦ have ha : a := And.left hab have hb : b := And.right hab show b ∧ a from And.intro hb ha

To swap a disjunction, we eliminate ∨ with Or.elim, supplying one function for each case:

theorem Or_swap (a b : Prop) : a ∨ b → b ∨ a := fun hab : a ∨ b ↦ show b ∨ a from Or.elim hab (fun ha : a ↦ Or.inr ha) (fun hb : b ↦ Or.inl hb)

For an existential, we supply the witness and the proof of the property. Note the use of by rfl inside a term: tactics may appear at any point of a structured proof, by way of by.

def double (n : ℕ) : ℕ := n + n theorem Nat_exists_double_iden : ∃ n : ℕ, double n = n := Exists.intro 0 (show double 0 = 0 from ⊢ double 0 = 0 All goals completed! 🐙)

Mixing styles is very common. In the example below, the overall shape is structured (an equivalence proved through its two directions), while small steps are discharged by tactics:

theorem Forall.one_point {α : Type} (t : α) (P : α → Prop) : (∀ x, x = t → P x) ↔ P t := Iff.intro (fun hall : ∀ x, x = t → P x ↦ show P t from α:Typet:αP:α → Prophall:∀ (x : α), x = t → P x⊢ P t α:Typet:αP:α → Prophall:∀ (x : α), x = t → P x⊢ t = t All goals completed! 🐙) (fun hp : P t ↦ fun x : α ↦ fun heq : x = t ↦ show P x from α:Typet:αP:α → Prophp:P tx:αheq:x = t⊢ P x α:Typet:αP:α → Prophp:P tx:αheq:x = t⊢ P t All goals completed! 🐙)

2.4.1.4. Structured proofs in tactic mode

The constructs have and show also exist as tactics, so the forward style can be used inside by blocks. In this case, intro plays the role of fix/assume:

theorem prop_comp_tactical (a b c : Prop) (hab : a → b) (hbc : b → c) : a → c := a:Propb:Propc:Prophab:a → bhbc:b → c⊢ a → c a:Propb:Propc:Prophab:a → bhbc:b → cha:a⊢ c a:Propb:Propc:Prophab:a → bhbc:b → cha:ahb:b⊢ c a:Propb:Propc:Prophab:a → bhbc:b → cha:ahb:bhc:c⊢ c All goals completed! 🐙

Two other tactics round out the repertoire. The tactic show T merely confirms (or restates, up to definitional equality) the current goal, serving as documentation. The tactic suffices h : P by tac reasons "backward" inside a forward proof: it says that it suffices to prove P, shows with tac how the goal follows from h : P, and leaves P as the new goal.

theorem prop_comp_suffices (a b c : Prop) (hab : a → b) (hbc : b → c) : a → c := a:Propb:Propc:Prophab:a → bhbc:b → c⊢ a → c a:Propb:Propc:Prophab:a → bhbc:b → cha:a⊢ c suffices hb : b a:Propb:Propc:Prophab:a → bhbc:b → cha:ahb:b⊢ c All goals completed! 🐙 a:Propb:Propc:Prophab:a → bhbc:b → cha:a⊢ b All goals completed! 🐙

To use existential or conjunctive hypotheses in the forward style, the obtain tactic decomposes a hypothesis into its parts, naming them:

theorem And_swap_obtain (a b : Prop) : a ∧ b → b ∧ a := a:Propb:Prop⊢ a ∧ b → b ∧ a a:Propb:Prophab:a ∧ b⊢ b ∧ a a:Propb:Propha:ahb:b⊢ b ∧ a All goals completed! 🐙

The notation ⟨hb, ha⟩ is the anonymous constructor: since the expected type is a conjunction, it abbreviates And.intro hb ha.

2.4.2. Calculational proofs

Many mathematical proofs are chains of equalities or inequalities: a = b = c \le d. The calc construct lets us write this kind of proof directly, chaining steps by transitivity:

calc
  term₀ op₁ term₁ := proof₁
  _     op₂ term₂ := proof₂
  ...
  _     opN termN := proofN

Each _ denotes the term on the right-hand side of the previous line. Lean checks that adjacent relations compose transitively (for example, = followed by ≤ yields ≤). For instance, to prove 2m + n = m + n + m:

theorem two_mul_example (m n : ℕ) : 2 * m + n = m + n + m := calc 2 * m + n = m + m + n := m:ℕn:ℕ⊢ 2 * m + n = m + m + n All goals completed! 🐙 _ = m + n + m := m:ℕn:ℕ⊢ m + m + n = m + n + m All goals completed! 🐙

calc is syntactic sugar: the same result can be obtained with have and Eq.trans:

theorem two_mul_example_have (m n : ℕ) : 2 * m + n = m + n + m := have h1 : 2 * m + n = m + m + n := m:ℕn:ℕ⊢ 2 * m + n = m + m + n All goals completed! 🐙 have h2 : m + m + n = m + n + m := m:ℕn:ℕh1:2 * m + n = m + m + n⊢ m + m + n = m + n + m All goals completed! 🐙 show _ from Eq.trans h1 h2

Chains may mix equalities and inequalities:

theorem calc_le (a b c : ℕ) (hab : a ≤ b) : a + c ≤ c + b + 0 := calc a + c ≤ b + c := Nat.add_le_add_right hab c _ = c + b := Nat.add_comm b c _ = c + b + 0 := a:ℕb:ℕc:ℕhab:a ≤ b⊢ c + b = c + b + 0 All goals completed! 🐙

Like have, calc can also be used as a tactic inside by blocks.

2.4.3. Dependent types

A dependent type is a type that depends on a term. Consider a function pick that takes a natural number n and returns a natural number bounded by n:

\mathsf{pick} : (n : \mathbb{N}) \to \{i : \mathbb{N} \mid i \leq n\} \qquad\qquad \mathsf{pick}\; 5 : \{i : \mathbb{N} \mid i \leq 5\}

The type of the result varies with the value of the argument. In Lean, the type \{i : \mathbb{N} \mid i \le n\} is written {i : ℕ // i ≤ n} (a subtype): its elements are pairs consisting of a natural number and a proof that it is less than or equal to n.

def pick (n : ℕ) : {i : ℕ // i ≤ n} := ⟨n, Nat.le_refl n⟩ Ch11.pick (n : ℕ) : { i // i ≤ n }#check pick pick 5 : { i // i ≤ 5 }#check pick 5 5#eval (pick 5).val
Ch11.pick (n : ℕ) : { i // i ≤ n }
pick 5 : { i // i ≤ 5 }
5

2.4.3.1. Four forms of dependency

In an abstraction fun x ↦ t, both the parameter x and the body t may be terms or types. This gives four combinations:

  • the body is a term depending on a term: simply typed functions, such as fun n : ℕ ↦ n + 1;

  • the body is a type depending on a term: dependent types proper, such as fun n : ℕ ↦ {i : ℕ // i ≤ n};

  • the body is a term depending on a type: polymorphism, such as the identity fun α : Type ↦ fun x : α ↦ x;

  • the body is a type depending on a type: type constructors, such as List = fun α ↦ List α.

fun n => n + 1 : ℕ → ℕ#check fun n : ℕ ↦ n + 1 fun n => { i // i ≤ n } : ℕ → Type#check fun n : ℕ ↦ {i : ℕ // i ≤ n} fun α x => x : (α : Type) → α → α#check fun (α : Type) (x : α) ↦ x List.{u} (α : Type u) : Type u#check List
fun n => n + 1 : ℕ → ℕ
fun n => { i // i ≤ n } : ℕ → Type
fun α x => x : (α : Type) → α → α
List.{u} (α : Type u) : Type u

Notice the type of the polymorphic identity: (α : Type) → α → α. It is a dependent function type: the types of the second argument and of the result depend on the value of the first.

2.4.3.2. Extended typing rules

The App and Fun rules of Chapter 8 need to be generalized. We use the notation (x : \sigma) \to \tau[x] for the type of dependent functions, in which the result type \tau[x] may mention the argument x. In the application rule, the result type is obtained by substituting the actual argument for x:

\dfrac{C \vdash t : (x : \sigma) \to \tau[x] \qquad C \vdash u : \sigma}{C \vdash t\; u : \tau[u]}\;\text{(App')}

In the abstraction rule, the type of the body may mention the parameter:

\dfrac{C,\, x : \sigma \vdash t : \tau[x]}{C \vdash (\mathbf{fun}\; x{:}\sigma \mapsto t) : (x : \sigma) \to \tau[x]}\;\text{(Fun')}

When x \notin \mathsf{fv}(\tau), that is, when the result type does not depend on x, we simply write \sigma \to \tau, and the rules reduce to App and Fun. For example, pick 5 : {i : ℕ // i ≤ 5} is obtained by App' with \tau[n] = \{i : \mathbb{N} \mid i \le n\} and u = 5.

2.4.3.3. Universal quantification is a dependent function

The crucial point is that universal quantification is a dependent function type:

\forall x : \sigma,\; \tau \;\stackrel{\text{def}}{=}\; (x : \sigma) \to \tau

This is not merely an analogy or a notation: in Lean they are the same construction. The equality below is proved by rfl, because both sides are literally the same term:

example : (∀ n : ℕ, add 0 n = n) = ((n : ℕ) → add 0 n = n) := rfl

2.4.4. The PAT principle

The PAT principle (Propositions as Types / Proofs as Terms), also known as the Curry-Howard isomorphism, which we already encountered in the first part of the course, establishes a correspondence between types and propositions, and between terms and proofs. The operator → plays a double role:

  • as a type, \sigma \to \tau is the type of total functions from \sigma to \tau;

  • as a proposition, P \to Q ("P implies Q") is the type of functions that transform proofs of P into proofs of Q.

On the side of types and propositions, the correspondence is:

  • the function type \sigma \to \tau corresponds to the implication P \to Q;

  • the dependent type (x : \sigma) \to \tau[x] corresponds to the universal quantification \forall x : \sigma,\; Q[x].

On the side of terms and proofs:

  • a constant corresponds to the name of a theorem or axiom;

  • a variable corresponds to a hypothesis;

  • an application H\;t, with t a term, instantiates the \forall of H with t;

  • an application H\;G, with G a proof, discharges the premise of H using G (this is modus ponens);

  • an abstraction \mathbf{fun}\; h{:}P \mapsto H[h] proves P \to Q;

  • an abstraction \mathbf{fun}\; x{:}\sigma \mapsto H[x] proves \forall x : \sigma,\; Q[x].

Thus the rules App' and Fun' are simultaneously typing rules and inference rules: App' is the elimination of \to/\forall and Fun' is its introduction. Checking a proof is checking the type of a term.

2.4.4.1. Raw proof terms

Under PAT, every proof is a lambda term, and we can write it down directly, without have, show, or tactics. Let us write the conjunction swap in three styles: as a raw term, as a structured proof (done above, in And_swap), and as a tactic proof.

theorem And_swap_raw (a b : Prop) : a ∧ b → b ∧ a := fun hab : a ∧ b ↦ And.intro (And.right hab) (And.left hab) theorem And_swap_tactical (a b : Prop) : a ∧ b → b ∧ a := a:Propb:Prop⊢ a ∧ b → b ∧ a a:Propb:Prophab:a ∧ b⊢ b ∧ a a:Propb:Prophab:a ∧ b⊢ ba:Propb:Prophab:a ∧ b⊢ a a:Propb:Prophab:a ∧ b⊢ b All goals completed! 🐙 a:Propb:Prophab:a ∧ b⊢ a All goals completed! 🐙

Lean accepts all three styles, but the kernel's type checker sees only the underlying lambda term. The #print command lets us inspect it:

theorem Ch11.And_swap_raw : ∀ (a b : Prop), a ∧ b → b ∧ a := fun a b hab => ⟨hab.right, hab.left⟩#print And_swap_raw theorem Ch11.And_swap_tactical : ∀ (a b : Prop), a ∧ b → b ∧ a := fun a b hab => ⟨hab.right, hab.left⟩#print And_swap_tactical
theorem Ch11.And_swap_raw : ∀ (a b : Prop), a ∧ b → b ∧ a :=
fun a b hab => ⟨hab.right, hab.left⟩
theorem Ch11.And_swap_tactical : ∀ (a b : Prop), a ∧ b → b ∧ a :=
fun a b hab => ⟨hab.right, hab.left⟩

The tactic proof produced exactly the same term that we wrote by hand (here ⟨x, y⟩ is notation for And.intro x y, and hab.right for And.right hab). In the structured proof, the haves produce small applied abstractions, which reduce to the same term.

The proof of double-negation introduction as a raw term is particularly instructive. Recalling that ¬ a is a → False, a proof of a → ¬¬ a is a function that takes ha : a and returns a function from ¬ a to False:

theorem not_not_intro (a : Prop) : a → ¬¬ a := fun ha : a ↦ fun hna : ¬ a ↦ hna ha

Here hna : ¬ a is the same as hna : a → False, and therefore hna ha : False.

2.4.5. Induction by pattern matching and recursion

By PAT, a proof of \forall n : \mathbb{N},\; P\,n is a function that assigns to each n a proof of P n. And how do we define functions on inductive types? By pattern matching and recursion! A proof by induction is therefore a recursive term.

Instead of the induction tactic, we can define the theorem by pattern matching, exactly like a function. The recursive calls are the induction hypotheses. Let us start with add_zero:

theorem add_zero' : ∀ n : ℕ, add 0 n = n ⊢ add 0 0 = 0 ⊢ add 0 0 = 0 All goals completed! 🐙 n:ℕ⊢ add 0 (n + 1) = n + 1 n:ℕ⊢ add 0 (n + 1) = n + 1 All goals completed! 🐙

In the second case, add_zero' n : add 0 n = n is the induction hypothesis: a recursive call on a structurally smaller argument. The termination checker guarantees that this "circular reasoning" is legitimate, exactly as it guarantees that recursive functions terminate. An ill-founded recursion would be a circular proof, and that is why it is rejected.

For lists, let us return to the reversal function of Chapter 9:

def reverse {α : Type} : List α → List α | [] => [] | x :: xs => reverse xs ++ [x] theorem reverse_append {α : Type} : ∀ xs ys : List α, reverse (xs ++ ys) = reverse ys ++ reverse xs α:Typeys:List α⊢ reverse ([] ++ ys) = reverse ys ++ reverse [] α:Typeys:List α⊢ reverse ([] ++ ys) = reverse ys ++ reverse [] All goals completed! 🐙 α:Typex:αxs:List αys:List α⊢ reverse (x :: xs ++ ys) = reverse ys ++ reverse (x :: xs) α:Typex:αxs:List αys:List α⊢ reverse (x :: xs ++ ys) = reverse ys ++ reverse (x :: xs) All goals completed! 🐙

The expression reverse_append xs in the recursive call is the induction hypothesis for xs (still quantified over ys). Likewise:

theorem reverse_reverse {α : Type} : ∀ xs : List α, reverse (reverse xs) = xs α:Type⊢ reverse (reverse []) = [] α:Type⊢ reverse (reverse []) = [] All goals completed! 🐙 α:Typex:αxs:List α⊢ reverse (reverse (x :: xs)) = x :: xs α:Typex:αxs:List α⊢ reverse (reverse (x :: xs)) = x :: xs All goals completed! 🐙

We can even dispense with tactics altogether and write the recursive proof term using the equality combinators. The function congrArg f h turns h : a = b into f a = f b:

theorem add_zero_raw : ∀ n : ℕ, add 0 n = n | 0 => rfl | n + 1 => congrArg Nat.succ (add_zero_raw n)

This is a recursive program that, for each n, builds a proof of add 0 n = n: in the base case, rfl; in the step, it applies Nat.succ to both sides of the proof for n. The n + 1 case works because add 0 (n + 1) reduces, by definition, to Nat.succ (add 0 n).

Comparing the two styles of induction:

  • with the induction tactic, the induction hypothesis receives a generated or chosen name (ih); with pattern matching, it is the theorem's own name applied to the smaller argument;

  • the induction tactic tends to be clearer for beginners, whereas pattern matching is more compact;

  • pattern matching gives explicit control over the structure of the proof, allowing, for example, nested patterns (n + 2) and simultaneous induction on several arguments, which require more work with the tactic.

end Ch11

2.4.6. Summary

The structured-proof constructs seen in this chapter are:

  • fun x : T ↦ … (the fix of the literature; intro x in tactics): introduces a universally quantified variable;

  • fun h : P ↦ … (the assume; intro h in tactics): introduces a hypothesis;

  • show T from p (tactic show T): documents the goal and supplies its proof;

  • have h : P := p: proves an intermediate lemma and names it;

  • suffices h : P by …: reduces the goal to P;

  • calc: chains equations and inequalities.

On the theoretical side:

  • a dependent type is a type that depends on a term;

  • ∀ x : σ, τ is the same as (x : σ) → τ;

  • App' and Fun' are the typing rules extended to dependent types;

  • PAT: propositions are types and proofs are terms;

  • a raw proof term is a lambda term that serves directly as a proof;

  • in induction by recursion, the theorem is defined by patterns and the recursive calls are the induction hypotheses.

References: Avigad et al., The Hitchhiker's Guide to Logical Verification (2025), ch. 4; Howard, The formulae-as-types notion of construction (1980); Sørensen and Urzyczyn, Lectures on the Curry-Howard Isomorphism (2006); Pierce, Types and Programming Languages (2002), chs. 29–30.

2.4.7. Exercises

  1. Prove the theorems below in three ways: as a raw term, as a structured proof (with have/show), and with tactics. Compare the terms using #print.

    theorem I (a : Prop) : a → a := sorry
    theorem K (a b : Prop) : a → b → b := sorry
    theorem C (a b c : Prop) : (a → b → c) → b → a → c := sorry
    theorem contrapositive (a b : Prop) :
        (a → b) → ¬ b → ¬ a := sorry
    
  2. Write a structured proof of ∀ (α : Type) (p q : α → Prop), (∀ x, p x ∧ q x) → (∀ x, p x).

  3. Use calc to prove, for natural numbers a and b:

    theorem sq_sum (a b : ℕ) :
        (a + b) * (a + b) = a * a + 2 * a * b + b * b := sorry
    

    (Hint: Nat.add_mul, Nat.mul_add, Nat.two_mul, ring for individual steps.)

  4. Determine the types of the terms below using the rules App' and Fun', and check them with #check:

    fun (α : Type) (f : α → α) (x : α) ↦ f (f x)
    fun (n : ℕ) (h : 0 < n) ↦ (⟨0, h⟩ : Fin n)
    
  5. Prove, by pattern matching and recursion (without the induction tactic), the theorems add_succ and add_comm of this chapter.

  6. Consider the map function from Chapter 9. Prove by pattern matching:

    theorem map_comp {α β γ : Type} (f : α → β) (g : β → γ) :
        ∀ xs : List α, map g (map f xs) = map (fun x ↦ g (f x)) xs
      := sorry
    
  7. Explain, in light of PAT, why a "proof" that used a non-terminating recursion (for example, theorem bad : False := bad) would make the logic inconsistent, and how Lean's termination checker prevents this.