InductionProof by Induction

Review

To prove the following theorem, which tactics will we need besides intros and reflexivity? (1) none, (2) rewrite, (3) destruct, (4) both rewrite and destruct, or (5) can't be done with the tactics we've seen.
    Theorem review1: (orb true false) = true.
What about the next one?
    Theorem review2: b, (orb true b) = true. Which tactics do we need besides intros and reflexivity? (1) none (2) rewrite, (3) destruct, (4) both rewrite and destruct, or (5) can't be done with the tactics we've seen.
What if we change the order of the arguments of orb?
    Theorem review3: b, (orb b true) = true. Which tactics do we need besides intros and reflexivity? (1) none (2) rewrite, (3) destruct, (4) both rewrite and destruct, or (5) can't be done with the tactics we've seen.
What about this one?
    Theorem review4 : n : nat, n = 0 + n. (1) none, (2) rewrite, (3) destruct, (4) both rewrite and destruct, or (5) can't be done with the tactics we've seen.
What about this?
    Theorem review5 : n : nat, n = n + 0. (1) none, (2) rewrite, (3) destruct, (4) both rewrite and destruct, or (5) can't be done with the tactics we've seen.

Separate Compilation

Coq will first need to compile Basics.v into Basics.vo so it can be imported here -- detailed instructions are in the full version of this chapter...
From LF Require Export Basics.

Proof by Induction

We can prove that 0 is a neutral element for + on the left using just reflexivity. But the proof that it is also a neutral element on the right ...
Theorem add_0_r_firsttry : n:nat,
  n + 0 = n.
... gets stuck.
Proof.
  intros n.
  simpl. (* Does nothing! *)
Abort.

And reasoning by cases using destruct n doesn't get us much further: the branch of the case analysis where we assume n = 0 goes through fine, but in the branch where n = S n' for some n' we get stuck in exactly the same way.
Theorem add_0_r_secondtry : n:nat,
  n + 0 = n.
Proof.
  intros n. destruct n as [| n'] eqn:E.
  - (* n = 0 *)
    reflexivity. (* so far so good... *)
  - (* n = S n' *)
    simpl. (* ...but here we are stuck again *)
Abort.

We need a bigger hammer: the principle of induction over natural numbers...
  • If P(n) is some proposition involving a natural number n, and we want to show that P holds for all numbers, we can reason like this:
    • show that P(O) holds
    • show that, if P(n') holds, then so does P(S n')
    • conclude that P(n) holds for all n.
For example...

Theorem add_0_r : n:nat, n + 0 = n.
Proof.
  intros n. induction n as [| n' IHn'].
  - (* n = 0 *) reflexivity.
  - (* n = S n' *) simpl. rewriteIHn'. reflexivity. Qed.

Let's try this one together:
Theorem minus_n_n : n,
  minus n n = 0.
Proof.
  (* WORK IN CLASS *) Admitted.

Here's another related fact about addition, which we'll need later. (The proof is left as an exercise.)
Theorem add_comm : n m : nat,
  n + m = m + n.
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 2 stars, standard (eqb_refl)

The following theorem relates the computational equality =? on nat with the definitional equality = on bool.
Theorem eqb_refl : n : nat,
  (n =? n) = true.
Proof.
  (* FILL IN HERE *) Admitted.

Proofs Within Proofs

Here's a way to use an in-line assertion instead of a separate lemma.
New tactic: assert.
Theorem mult_0_plus' : n m : nat,
  (n + 0 + 0) × m = n × m.
Proof.
  intros n m.
  assert (H: n + 0 + 0 = n).
    { rewrite add_comm. simpl. rewrite add_comm. reflexivity. }
  rewriteH.
  reflexivity. Qed.

Theorem plus_rearrange_firsttry : n m p q : nat,
  (n + m) + (p + q) = (m + n) + (p + q).
Proof.
  intros n m p q.
  (* We just need to swap (n + m) for (m + n)... seems
     like add_comm should do the trick! *)

  rewrite add_comm.
  (* Doesn't work... Coq rewrites the wrong plus! :-( *)
Abort.

To use add_comm at the point where we need it, we can introduce a local lemma stating that n + m = m + n (for the particular m and n that we are talking about here), prove this lemma using add_comm, and then use it to do the desired rewrite.
Theorem plus_rearrange : n m p q : nat,
  (n + m) + (p + q) = (m + n) + (p + q).
Proof.
  intros n m p q.
  assert (H: n + m = m + n).
  { rewrite add_comm. reflexivity. }
  rewrite H. reflexivity. Qed.