0

I'm learning coq and can't figure out why a rewrite doesn't work.

My code looks like this:

Inductive nat : Type :=
| zero
| succ (n : nat)
.

Fixpoint add (a b : nat) : nat :=
  match b with
  | zero => a
  | succ b' => add (succ a) b'
  end.

Theorem add_succ : forall a b : nat,
    add a (succ b) = succ (add a b).

Proof.
  induction b as [ |  b' IHb ].
  - simpl.
    reflexivity.
  -

My current proof state is this:

- a, b' : nat
- IHb : add a (succ b') = succ (add a b')
============================
add a (succ (succ b')) = succ (add a (succ b'))

My expectation is that if I run

rewrite -> IHb.

then, coq will rewrite the left-hand side of my goal to

succ (add a (succ b')

My reason for thinking this is that (succ b') is of type nat and b' is of type nat with no other restriction. So I expect coq to notice that the pattern in IHb is matched by the left-hand side of the goal. But that does not happen.

What am I doing wrong?

azani
  • 486
  • 3
  • 14

2 Answers2

2

Your hypothesis IHb only allows to rewrite the exact term add a (succ b'), because a and b' are variables in your context. You could do the rewrite step you indicate if your hypothesis IHb was quantifying universally over b', e.g. IHb : forall x, add a (succ x) = succ (add a x). Maybe you can modify your proof to obtain a stronger induction hypothesis at that point (relevant tactics to do that might be revert and generalize).

kyo dralliam
  • 1,197
  • 5
  • 6
  • I have updated the question to provide more information about my definitions and previous steps. Could you point out in what way I'm using induction wrong and how to use it right to quantify universally over `b'`? – azani Sep 28 '21 at 21:20
  • As a side-note for anyone reading this in the future, the issue is not that I was using coq wrong. The issue is that my induction hypothesis did not have a universal quantifier because induction hypothesis do not quantify over the set you perform induction over. (My error was mathematical, not programming-related.) – azani Oct 04 '21 at 18:18
0

It's very simple:

Theorem add_succ : forall a b : nat,
    add a (succ b) = succ (add a b).

Proof.
  intros *. revert a. induction b as [|b' IHb].
  - simpl. reflexivity.
  - simpl. intro a'. rewrite <- IHb with (succ a'). reflexivity. 
Qed.
q-dad
  • 1