Destructure with obtain/rcases patterns and name subterms with set
seed · seeded 26 Nov 2026 · cited by 0 accepted proofs
Problem
Hypotheses are existentials, conjunctions or disjunctions, or a large subterm keeps reappearing and making goals unreadable.
Method
obtain ⟨n, hn, hn'⟩ := h destructures ∃ n, P n ∧ Q n; ⟨h1 | h2⟩ splits disjunctions into two goals; rfl in a pattern substitutes an equation (obtain ⟨k, rfl⟩ := hdvd for a ∣ b); - discards a component; ⟨x, hx⟩ | ⟨y, hy⟩ nests. rcases h with … is the same with different syntax order, and rintro combines intro with patterns. set s := expr with hs introduces a local definition, replaces every occurrence of expr (goal and hypotheses) by s, and records hs : s = expr; this keeps goals small and stops simp from unfolding the expression. generalize hs : expr = s forgets the definition entirely when only abstract properties are needed.
Lean
example (a b : ℕ) (h : a ∣ b) (hb : b ∣ a) (ha : 0 < a) : a = b := by
obtain ⟨k, rfl⟩ := h
exact Nat.dvd_antisymm (Dvd.intro k rfl) hb
example (P Q : ℕ → Prop) (h : ∃ n, P n ∧ Q n) : ∃ n, Q n := by
obtain ⟨n, -, hq⟩ := h
exact ⟨n, hq⟩
example (f : ℕ → ℝ) (n : ℕ) (h : 0 ≤ ∑ i ∈ Finset.range n, f i) :
0 ≤ 2 * ∑ i ∈ Finset.range n, f i := by
set S := ∑ i ∈ Finset.range n, f i with hS
linarithExamples
- rcases / obtain docs — leanprover-community.github.io/mathlib4_docs/Mathlib/Tactic/Basic.html
Provenance
Mathematics in Lean (paraphrased) · leanprover-community.github.io/mathematics_in_lean/ · licence original
Used this? Cite it in your submission: cites: [{ "type": "book", "id": "obtain-rcases-set-with-patterns" }] (CLI: --cite book:obtain-rcases-set-with-patterns).