Search code examples
coqtheorem-provingcoq-tactic

How does one properly simplify a coq goal with a previous lemma?


Why doesn't the last line of my proof shed a successor instead of adding one. Note: i'm doing these excercises outside of a classroom setting and don't condone people using it to cheat on hw, I just don't know where else to ask it. From the tactics chapter in Pierce.

Theorem plus_n_n_injective : forall n m,
     n + n = m + m ->
     n = m.
Proof.
  intros n. induction n as [| n'].
  intros.
  simpl in H.
  destruct m.
  reflexivity.
  discriminate.
  intros.
  rewrite <- plus_n_Sm in H.
  destruct m.
  discriminate.
  rewrite <- plus_n_Sm in H.
  apply S_injective in H.
  simpl in H.
  apply S_injective in H.
  apply S_injective.

where these auxiliary lemmas are used

Theorem S_injective : forall (n m : nat),
  S n = S m ->
  n = m.
Proof.
  intros n m H1.
  assert (H2: n = pred (S n)). { reflexivity. }
  rewrite H2. rewrite H1. reflexivity.
Qed.
Theorem plus_n_Sm : forall n m : nat,
    S (n + m) = n + (S m).
Proof.
  intros n m. induction n as [| n' IHn'].
  simpl.
  reflexivity.
  simpl.
  rewrite -> IHn'.
  reflexivity.
Qed.

Solution

  • If you have a look at the statement of S_injective:

    Theorem S_injective : forall (n m : nat),
      S n = S m ->
      n = m.
    

    you will see it says that to prove n = m it is enough to prove S n = S m. Before you apply it, you have to prove S n' = S m, and then you say you only need to prove S (S n') = S (S m). It's because apply in the goal is doing some backward thinking.

    What you want instead is being able to say n = m -> S n = S m. You can prove the lemma by hand like you did, or you can use the f_equal tactic which works in general to prove f n = f m from n = m for any f (roughly).