Skip to content

Chapter 4. Equality and Rewriting

This chapter isolates the operational core of reasoning in Type Theory as implemented by Lean: equality and its use as a transport mechanism.

This chapter isolates the operational core of reasoning in Type Theory as implemented by Lean: equality and its use as a transport mechanism. Most nontrivial proofs in Lean reduce to a controlled sequence of rewrites. The objective here is to make rewriting predictable, terminating, and compositional.

Lean exposes two layers of equality. Definitional equality is handled by the kernel through computation. Expressions that reduce to the same normal form are treated as equal without requiring proof. This includes beta reduction, delta unfolding, and other normalization steps. You do not invoke it directly; you design definitions so that it fires where you need it. In contrast, propositional equality is an explicit value of type a = b. It must be constructed and then consumed by rewriting. The boundary between these two layers determines when rfl succeeds and when a proof term is required.

The primitive proof of equality is rfl. It closes goals where both sides are definitionally equal after normalization. From this base, the system builds congruence, symmetry, and transitivity. Congruence lifts equality through contexts such as function application. Symmetry and transitivity allow you to orient and compose equalities. The chapter shows how these principles appear in practice through rewriting tactics and through term-level combinators.

Rewriting is the act of replacing one expression with another using a proof of equality. In tactic mode, rw applies a lemma in a chosen direction and location. In term mode, the same effect is achieved by applying transport along equality. The direction matters: many goals require the inverse orientation of a lemma. The chapter emphasizes explicit control of direction, scope, and order of rewrites, since uncontrolled rewriting leads to divergence or loss of structure.

A central tool is simp, a normalization engine built on a curated set of rewrite rules. Unlike rw, which performs a single directed step, simp performs repeated rewriting until no rule applies. Its behavior depends entirely on the simp set. The chapter develops criteria for good simp lemmas: left-hand sides that strictly decrease a measure, right-hand sides in canonical form, and absence of overlapping rules that create loops. You will see how to register, localize, and audit simp rules so that normalization remains fast and terminating.

Rewriting under binders introduces dependence. When variables appear in types, replacing a term can change the type of subsequent expressions. Lean supports dependent rewriting through specialized combinators and tactics that respect binders. The chapter shows how to move equalities across lambdas, quantifiers, and structures without breaking typing, and how to diagnose failures that arise from hidden dependencies.

Transport is the general mechanism behind rewriting. Given h : a = b, you can move data or proofs indexed by a to ones indexed by b. In simple cases this appears as substitution. In dependent settings it becomes explicit transport across families. Understanding this mechanism clarifies why some rewrites require auxiliary lemmas and why others are automatic.

Function equality is treated via extensionality. Two functions are equal when they agree on all inputs. Lean provides extensionality lemmas that reduce function equality to pointwise goals. This reduction is often necessary before rewriting can proceed, since rewriting acts on concrete applications rather than abstract functions.

The chapter also addresses heterogeneous equality, which relates terms whose types differ but are connected by an equality of indices. While less common in routine proofs, it appears in dependent developments and in low-level manipulations. The practical guidance is to avoid it when a homogeneous formulation is available, and to use standard bridges when it is not.

Throughout, the focus is on building a stable rewrite system for your development. You will learn to choose canonical forms, orient lemmas consistently, and separate local rewrites from global normalization. The result is a proof style where most steps are mechanical, failures are localized, and large proofs remain maintainable.

4.1 Definitional EqualityDefinitional equality is the built-in notion of equality used by the kernel of Lean.
3 min
4.2 Propositional EqualityPropositional equality is the explicit notion of equality in Lean.
3 min
4.3 Reflexivity, Symmetry, and TransitivityPropositional equality in Lean is generated from a small set of core operations.
3 min
4.4 Congruence and Contextual RewritingEquality becomes useful when it propagates through larger expressions.
3 min
4.5 Directed Rewriting with `rw`Rewriting is the primary way to apply propositional equalities in Lean.
3 min
4.6 Simplification with `simp`The tactic `simp` performs normalization by repeated rewriting using a curated set of lemmas.
3 min
4.7 Controlling the `simp` SetThe behavior of `simp` depends entirely on the set of rewrite rules it uses.
3 min
4.8 Case Analysis and RewritingCase analysis splits a goal according to the structure of a value.
3 min
4.9 Rewriting with HypothesesIn most proofs, equalities come from the local context.
3 min
4.10 Rewriting with LemmasLemmas provide reusable equalities that drive most rewriting.
3 min
4.11 Chained Rewriting and `calc`Complex equalities are rarely achieved in a single step.
3 min
4.12 Rewriting Under BindersRewriting becomes more subtle when the target term appears inside a binder.
3 min
4.13 Substitution and `subst`Substitution is the direct elimination of equalities from the context.
3 min
4.14 Dependent Rewriting and TransportWhen types depend on values, rewriting affects both terms and their types.
3 min
4.15 Avoiding Rewrite Loops and NonterminationRewriting systems can diverge if rules are poorly oriented or interact cyclically.
3 min
4.16 Rewriting with EquivalencesEqualities are often too strict.
3 min
4.17 Rewriting in Structures and RecordsStructures package multiple fields into a single value.
3 min
4.18 Rewriting of Functions and ExtensionalityEquality between functions requires a different treatment than equality between values.
3 min
4.19 Proof Irrelevance and Equality of ProofsIn Lean, propositions live in `Prop`, a universe where proof irrelevance holds.
3 min
4.20 Decidable Equality and Boolean BridgesReasoning often alternates between propositional equality `a = b` and boolean equality `a == b`.
3 min
4.21 Rewriting in Pattern MatchingPattern matching performs case analysis by selecting a branch based on the shape of a value.
3 min
4.22 Rewriting with `conv`The `conv` tactic provides fine-grained control over rewriting.
3 min
4.23 Rewriting in Goals vs HypothesesRewriting can target either the goal or the local context.
3 min
4.24 Rewriting Strategies and HeuristicsRewriting is most effective when guided by a small set of consistent strategies.
3 min
4.25 Common Pitfalls and DebuggingRewriting failures in Lean usually come from a small set of recurring issues.
3 min