Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

12. Dependent types and validated boundaries

A dependent type can mention a value. Fin n, for example, contains a natural number together with evidence that it is below n. The value of n affects which values inhabit the type.

This lets you move a validation condition into an interface. After checking an untrusted input, the rest of the program can consume a value whose type records the established condition.

Build a bounded value once

The successful branch stores evidence of the bound in Fin limit. Callers can use that evidence directly.

def bounded? (limit value : Nat) : Option (Fin limit) :=
  if h : value < limit then some ⟨value, h⟩ else none

example (i : Fin 10) : i.val < 10 := i.isLt

#eval (bounded? 10 7).map Fin.val
#eval (bounded? 10 12).map Fin.val

The named condition h is a proof available in the successful branch. The constructor ⟨value, h⟩ packages the value and its bound. The failed branch returns none because no bounded value can be constructed from that input.

A subtype {x : α // P x} similarly contains a value and a proof about it. A structure is often clearer when several fields and relationships belong together:

structure CheckedWithdrawal (balance : Nat) where
  amount : Nat
  affordable : amount ≤ balance

def remaining {balance : Nat} (request : CheckedWithdrawal balance) : Nat :=
  balance - request.amount

theorem remainingAccounts {balance : Nat}
    (request : CheckedWithdrawal balance) :
    remaining request + request.amount = balance := by
  have h := request.affordable
  simp only [remaining]
  omega

To construct a request, a caller supplies a proof that the amount is affordable. Functions receiving the request can use that proof directly. Compilation erases the proof field and retains the amount as runtime data.

Indexed families

A parameter stays uniform throughout an inductive family. An index may vary between constructors. This small vector type keeps its element type as a parameter and its length as an index:

inductive Vec (α : Type) : Nat → Type where
  | nil : Vec α 0
  | cons : α → Vec α n → Vec α (n + 1)

def Vec.head {α : Type} {n : Nat} : Vec α (n + 1) → α
  | .cons x _ => x

def Vec.map {α β : Type} (f : α → β) : Vec α n → Vec β n
  | .nil => .nil
  | .cons x xs => .cons (f x) (Vec.map f xs)

Vec.head accepts only vectors whose length is at least 1. Vec.map records that transformation preserves length. Matching refines the index so each branch can construct the required result type.

Mathematically equal index expressions can require a proof of equality. Combining lengths may therefore require arithmetic proofs and transport, which uses an equality to move a value between indexed types.

Start with ordinary data and separate theorems when dependent indices would complicate routine transformations. Use indices when they rule out errors that matter to callers.

Existentials and dependent pairs

An existential in Prop expresses that a witness exists. A dependent pair in Type, such as Sigma, stores a witness for computation along with data whose type depends on it.

Use a dependent pair when a program needs to inspect the witness at runtime. Lean erases proofs and restricts which values a program can extract from propositions.

Exercises

  1. Construct a value of Fin 5 containing 3.
  2. Write positive? : Nat → Option {n : Nat // 0 < n}.
  3. Define Vec.toList and prove or explain why Vec.head cannot be called on Vec.nil.