I'm trying to learn lean (version 4) by proving some basic facts about the natural numbers. Please feel free to critique my code if you see have general comments, but I also have a specific question involving a proof below. Here is my nat
type:
-- Axiom 2.1. 0 is a natural number.
-- Axiom 2.2. If n is a natural number, then n++ is also a natural number.
inductive nat : Type | zero : nat | succ : nat → nat
I also have this axiom:
-- Axiom 2.4. Different natural numbers must have
-- different successors; i.e., if n, m are natural
-- numbers and n ≠ m, then n++ ≠ m++. Equivalently,
-- if n++ = m++, then we must have n = m.
axiom succ_inj : ∀ n : nat, ∀ m : nat, n ≠ m → succ n ≠ succ m
I am using classical mode:
open Classical
I would like to use the contrapositive of axiom 2.4 (or a proof by contradiction) to prove the inductive step of this proposition
-- Proposition 2.2.6 (Cancellation law). Let a,b,c be natural
-- numbers such that a+b=a+c. Then we have b=c.
theorem cancellation_law (a b c : nat) : (a + b) = (a + c) → b = c := by
intro h
induction a with
| zero =>
have h1 : b = zero + b := by rfl
have h2 : c = zero + c := by rfl
rw [h1, h2]
exact h
| succ a ih =>
have h1 : succ a + b = succ (a + b) := by rfl
have h2 : succ a + c = succ (a + c) := by rfl
rw [h1,h2] at h
So now my lean state looks like this:
> case succ
bca: nat
ih: a + b = a + c → b = c
h: succ (a + b) = succ (a + c)
h1: succ a + b = succ (a + b)
h2: succ a + c = succ (a + c)
⊢ b = c
So now I would like to use the fact that
succ (a + b) = succ (a + c) -> a + b = a + c
which follows from axiom 2.4, and then apply the induction hypothesis. But I can't seem to figure out how to do a proof by contradiction. I continue the proof of proposition 2.2.6 with something like
have h3: a + b = a + c := by
...
...
contradiction
The theorem proving in lean 4 book says the contradiction
tactic looks for a contradiction in the hypotheses introduced, but how do I introduce an assumption for the purpose of contradiction? What is a good/canonical way to write this proof?
Here is a MWE:
open Classical
-- Section 2.1 The Peano axioms
-- Axiom 2.1. 0 is a natural number.
-- Axiom 2.2. If n is a natural number, then n++ is also a natural number.
inductive nat : Type | zero : nat | succ : nat → nat
namespace nat
-- Axiom 2.3. 0 is not the successor of any natural number; i.e.,
-- we have n++ ̸= 0 for every natural number n.
axiom zero_not_succ : ∀ n : nat, zero ≠ succ n
-- Axiom 2.4. Different natural numbers must have
-- different successors; i.e., if n, m are natural
-- numbers and n ≠ m, then n++ ≠ m++. Equivalently,
-- if n++ = m++, then we must have n = m.
axiom succ_inj : ∀ n : nat, ∀ m : nat, n ≠ m → succ n ≠ succ m
-- Section 2.2 Addition
def add : nat → nat → nat
| zero, m => m
| succ n, m => succ (add n m)
-- Get to use the plus operator
instance : Add nat where add := add
-- Proposition 2.2.6 (Cancellation law). Let a,b,c be natural
-- numbers such that a+b=a+c. Then we have b=c.
theorem cancellation_law (a b c : nat) : (a + b) = (a + c) → b = c := by
intro h
induction a with
| zero =>
have h1 : b = zero + b := by rfl
have h2 : c = zero + c := by rfl
rw [h1, h2]
exact h
| succ a ih =>
have h1 : succ a + b = succ (a + b) := by rfl
have h2 : succ a + c = succ (a + c) := by rfl
rw [h1,h2] at h
/-
> case succ
bca: nat
ih: a + b = a + c → b = c
h: succ (a + b) = succ (a + c)
h1: succ a + b = succ (a + b)
h2: succ a + c = succ (a + c)
⊢ b = c
-/
end nat
+
. This makes it possible for someone to copy-paste your code into Lean. $\endgroup$succ_inj
written as it is, because that is more true to how its stated in the text, and I felt it should be possible to use the statements as given in Lean. I will update the question with a MWE, even though you have already answered it. $\endgroup$