Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prove a = b → a + 1 = b + 1 in lean?

I'm working my way through the chapter 4 of the lean tutorial.

I'd like to be able to prove simple equalities, such as a = b → a + 1 = b + 1 without having to use the calc environment. In other words I'd like to explicitly construct the proof term of:

example (a b : nat) (H1 : a = b) : a + 1 = b + 1 := sorry

My best guess is that I need to use eq.subst and some relevant lemma about equality on natural numbers from the standard library, but I'm at loss. The closest lean example I can find is this:

example (A : Type) (a b : A) (P : A → Prop) (H1 : a = b) (H2 : P a) : P b := eq.subst H1 H2

like image 836
Adam Kurkiewicz Avatar asked Jan 30 '17 22:01

Adam Kurkiewicz


3 Answers

You can use the congr_arg lemma

lemma congr_arg {α : Sort u} {β : Sort v} {a₁ a₂ : α} (f : α → β) :
  a₁ = a₂ → f a₁ = f a₂

which means if you supply equal inputs to a function, the output values will be equal too.

The proof goes like this:

example (a b : nat) (H : a = b) : a + 1 = b + 1 :=
  congr_arg (λ n, n + 1) H

Note, that Lean is able to infer that our function is λ n, n + 1, so the proof can be simplified into congr_arg _ H.

like image 60
Anton Trunov Avatar answered Jan 01 '23 00:01

Anton Trunov


While congr_arg is a good solution in general, this specific example can indeed be solved with eq.subst + higher-order unification (which congr_arg uses internally).

example (a b : nat) (H1 : a = b) : a + 1 = b + 1 :=
eq.subst H1 rfl
like image 35
Sebastian Ullrich Avatar answered Jan 01 '23 02:01

Sebastian Ullrich


Since you have an equality (a = b), you can also rewrite the goal using tactic mode:

example (a b : nat) (H1 : a = b) : a + 1 = b + 1 :=
by rw H1

See Chapter 5 of Theorem Proving in Lean for an introduction to tactics.

like image 34
Lambda Fairy Avatar answered Jan 01 '23 00:01

Lambda Fairy