contrapose
Transform goals and hypotheses using contraposition.contrapose switches between "P → Q" and "¬Q → ¬P".
❗
contrapose requires Mathlib. Addimport Mathlib.Tactic.Contrapose orimport Mathlib.Tactic.Basic Usage
lean
1import Mathlib.Tactic.Contrapose23-- contrapose transforms: prove P → Q by proving ¬Q → ¬P4example (h : P) : Q → P := by5 intro _6 exact h78-- Same proof using contrapose9example : (n = 0 → n * n = 0) := by10 intro h11 simp [h]1213example : (n * n ≠ 0 → n ≠ 0) := by14 contrapose -- Changes goal to: n = 0 → n * n = 015 intro h16 simp [h]Key Takeaway
contrapose transforms a goalP → Q into ¬Q → ¬P. Often the contrapositive is easier to prove directly.contrapose!
lean
1import Mathlib.Tactic.Contrapose2import Mathlib.Tactic.PushNeg34-- contrapose! also applies push_neg5example : (∀ n : Nat, n > 0 → n ≥ 1) := by6 intro n7 contrapose!8 -- Goal changes from: n > 0 → n ≥ 19 -- to: n < 1 → n ≤ 0 (with push_neg applied)10 intro h11 omega💡
Use
contrapose! (with bang) to automatically apply push_neg after the contraposition, simplifying negations.When Contraposition Helps
lean
1import Mathlib.Tactic.Contrapose23-- Sometimes the contrapositive is more natural4-- "If n² is even, then n is even"5-- Easier as: "If n is odd, then n² is odd"67example (n : Nat) : (∃ k, n = 2 * k) → ∃ k, n * n = 2 * k := by8 intro ⟨k, hk⟩9 use 2 * k * k10 omega1112-- The contrapositive might be easier in some cases13-- But for this example, direct is fineWith Hypothesis
lean
1import Mathlib.Tactic.Contrapose23-- contrapose can work on hypotheses too4example (h : P → Q) (hnq : ¬Q) : ¬P := by5 exact fun hp => hnq (h hp)67-- Using contrapose directly on hypothesis8example (h : P → Q) : ¬Q → ¬P := by9 intro hnq hp10 exact hnq (h hp)Deep Dive: Contraposition Logic
Contraposition is based on the logical equivalence:
lean
1-- (P → Q) ↔ (¬Q → ¬P)23-- Proof:4-- If P → Q, and we know ¬Q, then assuming P gives Q,5-- contradicting ¬Q. So we must have ¬P.67-- This is classical logic - it requires excluded middle8-- for the full equivalence.Proof by Contradiction vs Contraposition
lean
1import Mathlib.Tactic.Contrapose23-- Proof by contradiction: assume ¬conclusion, derive False4example (h : P) : ¬¬P := by5 intro hnp6 exact hnp h78-- Contraposition: swap hypothesis and conclusion, negate both9-- To prove P → Q, prove ¬Q → ¬P1011-- They're related but different techniques!12-- Contradiction: assumes negation of what you want13-- Contraposition: changes the structure of implicationReal-World Examples
Divisibility
lean
1import Mathlib.Tactic.Contrapose23-- If a divides bc and gcd(a,b) = 1, then a divides c4-- Contrapositive: If a doesn't divide c, then...5-- (Sometimes the direct proof is still easier)Injectivity
lean
1import Mathlib.Tactic.Contrapose23-- f is injective iff f(x) = f(y) → x = y4-- Contrapositive: x ≠ y → f(x) ≠ f(y)56example (f : Nat → Nat) (hf : ∀ x y, f x = f y → x = y) :7 ∀ x y, x ≠ y → f x ≠ f y := by8 intro x y hne hfeq9 exact hne (hf x y hfeq)Irrationality Proofs
lean
1import Mathlib.Tactic.Contrapose23-- Classic: √2 is irrational4-- Contrapositive: If √2 = p/q (rational), derive contradiction5-- This is actually proof by contradiction, but contraposition6-- can help structure parts of the argument78example (h : ¬(∃ p q : Nat, q ≠ 0 ∧ p * p = 2 * q * q)) :9 ¬(∃ r : Rat, r * r = 2) := by10 intro ⟨r, hr⟩11 sorry -- Full proof needs more machineryCommon Patterns
lean
1import Mathlib.Tactic.Contrapose23-- Pattern: prove implication by contrapositive4-- When conclusion is "positive" and hypothesis is "negative"5-- Or when negated forms are simpler67-- Example: If f(x) ≠ 0 for all x > 0, then f has no positive roots8-- Contrapositive: If f has a positive root, then f(x) = 0 for some x > 09example (f : Nat → Int) : 10 (∃ x, x > 0 ∧ f x = 0) → ¬(∀ x, x > 0 → f x ≠ 0) := by11 intro ⟨x, hx_pos, hfx⟩12 push_neg13 exact ⟨x, hx_pos, hfx⟩contrapose vs by_contra
lean
1import Mathlib.Tactic.Contrapose2import Mathlib.Tactic.ByContra34-- by_contra assumes ¬goal and tries to derive False5example (h : P) : P := by6 by_contra hnp7 exact hnp h89-- contrapose changes the structure of an implication goal10-- They're complementary techniques!