eBook: [cases — Formalising Mathematics 0.1 documentation](https://b-mehta.github.io/formalising-mathematics-notes/Part_2/tactics/cases.html) ## cases The [[🔩 cases-tactic|cases]]  tactic is used to perform case analysis on terms of an inductive type (such as natural numbers, booleans, lists, disjunctions, or custom inductive types). When you invoke `cases h`, Lean looks at the inductive definition of the type of h and generates a separate subgoal for each possible constructor of that type. ## 5 Examples of Increasing Complexity ### Example 1: Basic Boolean Case Analysis (`Bool`) The simplest use of `cases` is on types with a finite, non-recursive set of constructors, like `Bool`. ```lean example (b : Bool) : (b && true) = b := by cases b · rfl -- case b = false · rfl -- case b = true ``` - **Explanation:** `Bool` has two constructors: `false` and `true`. The `cases b` tactic splits the goal into two branches (represented by the bullets `·`), substituting `false` for `b` in the first branch and `true` for `b` in the second. ### Example 2: Natural Numbers (`Nat`) Natural numbers in Lean are defined inductively with two constructors: `Nat.zero` (or `0`) and `Nat.succ n`. ```lean example (n : Nat) : n = 0 ∨ ∃ k, n = k + 1 := by cases n with | zero => left rfl | succ k => right use k ``` - **Explanation:** Here we use `cases n with` to explicitly name the branches corresponding to `zero` and `succ k`. Notice that `cases` does _not_ give us an inductive hypothesis for k; it just peels off one layer of `succ`. ### Example 3: Disjunction (`Or`) Propositions like `Or` (`∨`) are also inductive types under the hood, having two constructors: `Or.inl` (left injection) and `Or.inr` (right injection). ```lean example (p q : Prop) (h : p ∨ q) : q ∨ p := by cases h with | inl hp => right exact hp | inr hq => left exact hq ``` - **Explanation:** `cases h` breaks apart the disjunction hypothesis `h`. In the first branch, we know p is true (`hp`), so we prove the right side of the goal (`q ∨ p`). In the second branch, we know q is true (`hq`), so we prove the left side. ### Example 4: Lists (`List α`) Lists are recursive inductive types with two constructors: `List.nil` (`[]`) and `List.cons` (`x :: xs`). ```lean example (xs : List α) (h : xs ≠ []) : ∃ y ys, xs = y :: ys := by cases xs with | nil => -- In this case, xs = [], which contradicts h : xs ≠ [] contradiction | cons y ys => -- In this case, xs = y :: ys use y, ys ``` - **Explanation:** This demonstrates how `cases` can help eliminate impossible branches. When `xs` is `nil`, the hypothesis `xs ≠ []` directly contradicts the assumption, and `contradiction` closes the goal instantly. ### Example 5: Advanced `cases` with Equality Substitution (Inversion) `cases` is frequently used for equality hypotheses or constructor inversion. When you run `cases` on an equality or a hypothesis that has a constrained constructor, Lean automatically substitutes equal variables and discards impossible cases. ```lean example (n m : Nat) (h : n + 1 = m + 1) : n = m := by -- We can use `cases h` or injectivity, but `cases` on equality -- of constructors automatically unifies them: cases h rfl ``` Alternatively, consider constructor mismatch elimination: ```lean example (n : Nat) (h : n + 1 = 0) : False := by cases h ``` - **Explanation:** In this example, `n + 1` uses the `succ` constructor, while `0` uses the `zero` constructor. Because `succ` and `zero` are distinct constructors, `cases h` recognizes that this branch is impossible and automatically discharges the goal. --- Unlike the `induction` tactic, `cases` **does not** provide an inductive hypothesis for recursive fields; it simply breaks down the structure into its immediate components. ```