InductionProof by Induction

The next line imports all of our definitions from the previous chapter.

Require Export Basics.

For it to work, you need to use coqc to compile Basics.v into Basics.vo. This is like making a .class file from a .java file, or a .o file from a .c file.
Here are two ways to compile your code:
  • In CoqIDE:
    Open Basics.v. In the "Compile" menu, click on "Compile Buffer".
  • From the command line:
    Run coqc Basics.v

Proof by Induction

We proved in the last chapter that 0 is a neutral element for + on the left using an easy argument based on simplification. The fact that it is also a neutral element on the right...

Theorem plus_n_O_firsttry : n:nat,
  n = n + 0.

... cannot be proved in the same simple way. Just applying reflexivity doesn't work, since the n in n + 0 is an arbitrary unknown number, so the match in the definition of + can't be simplified.

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. We could use destruct n' to get one step further, but, since n can be arbitrarily large, if we try to keep on like this we'll never be done.

Theorem plus_n_O_secondtry : n:nat,
  n = n + 0.
Proof.
  intros n. destruct n as [| n'].
  - (* n = 0 *)
    reflexivity. (* so far so good... *)
  - (* n = S n' *)
    simpl. (* ...but here we are stuck again *)
Abort.

To prove interesting facts about numbers, lists, and other inductively defined sets, we usually need a more powerful reasoning principle: induction.
Recall (from high school, a discrete math course, etc.) 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 n, we can reason like this:
  • show that P(O) holds;
  • show that, for any n', if P(n') holds, then so does P(S n');
  • conclude that P(n) holds for all n.
In Coq, the steps are the same but the order is backwards: we begin with the goal of proving P(n) for all n and break it down (by applying the induction tactic) into two separate subgoals: first showing P(O) and then showing P(n') P(S n'). Here's how this works for the theorem at hand:

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

Like destruct, the induction tactic takes an as... clause that specifies the names of the variables to be introduced in the subgoals. In the first branch, n is replaced by 0 and the goal becomes 0 + 0 = 0, which follows by simplification. In the second, n is replaced by S n' and the assumption n' + 0 = n' is added to the context (with the name IHn', i.e., the Induction Hypothesis for n' — notice that this name is explicitly chosen in the as... clause of the call to induction rather than letting Coq choose one arbitrarily). The goal in this case becomes (S n') + 0 = S n', which simplifies to S (n' + 0) = S n', which in turn follows from IHn'.

Theorem minus_diag : n,
  minus n n = 0.
Proof.
  (* WORKED IN CLASS *)
  intros n. induction n as [| n' IHn'].
  - (* n = 0 *)
    simpl. reflexivity.
  - (* n = S n' *)
    simpl. rewrite IHn'. reflexivity. Qed.

Exercise: 2 stars, recommended (basic_induction)

Prove the following using induction. You might need previously proven results.

Theorem mult_0_r : n:nat,
  n * 0 = 0.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem plus_n_Sm : n m : nat,
  S (n + m) = n + (S m).
Proof.
  (* FILL IN HERE *) Admitted.

Theorem plus_comm : n m : nat,
  n + m = m + n.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem plus_assoc : n m p : nat,
  n + (m + p) = (n + m) + p.
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 2 stars (double_plus)

Consider the following function, which doubles its argument:

Fixpoint double (n:nat) :=
  match n with
  | OO
  | S n'S (S (double n'))
  end.

Use induction to prove this simple fact about double:

Lemma double_plus : n, double n = n + n .
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 2 stars, optional (evenb_S)

One inconveninent aspect of our definition of evenb n is that it may need to perform a recursive call on n - 2. This makes proofs about evenb n harder when done by induction on n, since we may need an induction hypothesis about n - 2. The following lemma gives a better characterization of evenb (S n):

Theorem evenb_S : n : nat,
  evenb (S n) = negb (evenb n).
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 1 star (destruct_induction)

Briefly explain the difference between the tactics destruct and induction.
(* FILL IN HERE *)

Proofs Within Proofs

In Coq, as in informal mathematics, large proofs are often broken into a sequence of theorems, with later proofs referring to earlier theorems. But sometimes a proof will require some miscellaneous fact that is too trivial and of too little general interest to bother giving it its own top-level name. In such cases, it is convenient to be able to simply state and prove the needed "sub-theorem" right at the point where it is used. The assert tactic allows us to do this. For example, our earlier proof of the mult_0_plus theorem referred to a previous theorem named plus_O_n. We could instead use assert to state and prove plus_O_n in-line:

Theorem mult_0_plus' : n m : nat,
  (0 + n) * m = n * m.
Proof.
  intros n m.
  assert (H: 0 + n = n). { reflexivity. }
  rewrite H.
  reflexivity. Qed.

The assert tactic introduces two sub-goals. The first is the assertion itself; by prefixing it with H: we name the assertion H. (We can also name the assertion with as just as we did above with destruct and induction, i.e., assert (0 + n = n) as H.) Note that we surround the proof of this assertion with curly braces { ... }, both for readability and so that, when using Coq interactively, we can see more easily when we have finished this sub-proof. The second goal is the same as the one at the point where we invoke assert except that, in the context, we now have the assumption H that 0 + n = n. That is, assert generates one subgoal where we must prove the asserted fact and a second subgoal where we can use the asserted fact to make progress on whatever we were trying to prove in the first place.
The assert tactic is handy in many sorts of situations. For example, suppose we want to prove that (n + m) + (p + q) = (m + n) + (p + q). The only difference between the two sides of the = is that the arguments m and n to the first inner + are swapped, so it seems we should be able to use the commutativity of addition (plus_comm) to rewrite one into the other. However, the rewrite tactic is a little stupid about where it applies the rewrite. There are three uses of + here, and it turns out that doing rewrite plus_comm will affect only the outer one...

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)...
     it seems like plus_comm should do the trick! *)

  rewrite plus_comm.
  (* Doesn't work...Coq rewrote the wrong plus! *)
Abort.

To get plus_comm to apply at the point where we want it to, 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 plus_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 plus_comm. reflexivity. }
  rewrite H. reflexivity. Qed.

More Exercises

Exercise: 3 stars, recommended (mult_comm)

Use assert to help prove this theorem. You shouldn't need to use induction on plus_swap.

Theorem plus_swap : n m p : nat,
  n + (m + p) = m + (n + p).
Proof.
  (* FILL IN HERE *) Admitted.

Now prove commutativity of multiplication. (You will probably need to define and prove a separate subsidiary theorem to be used in the proof of this one. You may find that plus_swap comes in handy.)

Theorem mult_comm : m n : nat,
  m * n = n * m.
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 3 stars, optional (more_exercises)

Take a piece of paper. For each of the following theorems, first think about whether (a) it can be proved using only simplification and rewriting, (b) it also requires case analysis (destruct), or (c) it also requires induction. Write down your prediction. Then fill in the proof. (There is no need to turn in your piece of paper; this is just to encourage you to reflect before you hack!)

Theorem leb_refl : n:nat,
  true = leb n n.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem zero_nbeq_S : n:nat,
  beq_nat 0 (S n) = false.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem andb_false_r : b : bool,
  andb b false = false.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem plus_ble_compat_l : n m p : nat,
  leb n m = true leb (p + n) (p + m) = true.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem S_nbeq_0 : n:nat,
  beq_nat (S n) 0 = false.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem mult_1_l : n:nat, 1 * n = n.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem all3_spec : b c : bool,
    orb
      (andb b c)
      (orb (negb b)
               (negb c))
  = true.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem mult_plus_distr_r : n m p : nat,
  (n + m) * p = (n * p) + (m * p).
Proof.
  (* FILL IN HERE *) Admitted.

Theorem mult_assoc : n m p : nat,
  n * (m * p) = (n * m) * p.
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 2 stars, optional (beq_nat_refl)

Prove the following theorem. (Putting the true on the left-hand side of the equality may look odd, but this is how the theorem is stated in the Coq standard library, so we follow suit. Rewriting works equally well in either direction, so we will have no problem using the theorem no matter which way we state it.)

Theorem beq_nat_refl : n : nat,
  true = beq_nat n n.
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 2 stars, optional (plus_swap')

The replace tactic allows you to specify a particular subterm to rewrite and what you want it rewritten to: replace (t) with (u) replaces (all copies of) expression t in the goal by expression u, and generates t = u as an additional subgoal. This is often useful when a plain rewrite acts on the wrong part of the goal.
Use the replace tactic to do a proof of plus_swap', just like plus_swap but without needing assert (n + m = m + n).

Theorem plus_swap' : n m p : nat,
  n + (m + p) = m + (n + p).
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 3 stars, recommended (binary_commute)

Recall the increment and binary-to-unary functions that you wrote for the binary exercise in the Basics chapter. Prove that the following diagram commutes:
             binnat ------- incr ------> binnat
                |                           |
            bin2unary                    bin2unary
                |                           |
                v                           v
               nat ---------- S ---------> nat
That is, incrementing a binary number and then converting it to unary yields the same result as first converting it to unary and then incrementing. Name your theorem bin_to_nat_pres_incr.
Before you start working on this exercise, please copy the definitions from your solution to the binary exercise here so that this file can be graded on its own. If you find yourself wanting to change your original definitions to make the property easier to prove, feel free to do so!

(* FILL IN HERE *)

Exercise: 5 stars, advanced (binary_inverse)

This exercise is a continuation of the previous exercise about binary numbers. You will need your definitions and theorems from there to complete this one.
(a) First, write a function to convert natural numbers to binary numbers. Then prove that starting with any natural number, converting to binary, then converting back yields the same natural number you started with.
(b) You might naturally think that we should also prove the opposite direction: that starting with a binary number, converting to a natural, and then back to binary yields the same number we started with. However, this is not true! Explain what the problem is.
(c) Define a "direct" normalization function — i.e., a function normalize from binary numbers to binary numbers such that, for any binary number b, converting to a natural and then back to binary yields (normalize b). Prove it. (Warning: This part is tricky!)
Again, feel free to change your earlier definitions if this helps here.

(* FILL IN HERE *)

Formal vs. Informal Proof (Optional)

IHn'. reflexivity. Qed. (** Coq is perfectly happy with this. For a human, however, it is difficult to make much sense of it. We can use comments and bullets to show the structure a little more clearly... *) Theorem plus_assoc'' : forall n m p : nat, n + (m + p) = (n + m) + p. Proof. intros n m p. induction n as [| n' IHn']. - (* n = 0 *) reflexivity. - (* n = S n' *) simpl. rewrite -> IHn'. reflexivity. Qed. (** ... and if you're used to Coq you may be able to step through the tactics one after the other in your mind and imagine the state of the context and goal stack at each point, but if the proof were even a little bit more complicated this would be next to impossible. A (pedantic) mathematician might write the proof something like this: *) (** - _Theorem_: For any [n], [m] and [p], [[ n + (m + p) = (n + m) + p. ]] _Proof_: By induction on [n]. - First, suppose [n = 0]. We must show [[ 0 + (m + p) = (0 + m) + p. ]] This follows directly from the definition of [+]. - Next, suppose [n = S n'], where [[ n' + (m + p) = (n' + m) + p. ]] We must show [[ (S n') + (m + p) = ((S n') + m) + p. ]] By the definition of [+], this follows from [[ S (n' + (m + p)) = S ((n' + m) + p), ]] which is immediate from the induction hypothesis. _Qed_. *) (** The overall form of the proof is basically similar, and of course this is no accident: Coq has been designed so that its [induction] tactic generates the same sub-goals, in the same order, as the bullet points that a mathematician would write. But there are significant differences of detail: the formal proof is much more explicit in some ways (e.g., the use of [reflexivity]) but much less explicit in others (in particular, the "proof state" at any given point in the Coq proof is completely implicit, whereas the informal proof reminds the reader several times where things stand). *) (** **** Exercise: 2 stars, advanced, recommended (plus_comm_informal) *) (** Translate your solution for [plus_comm] into an informal proof. *) (** Theorem: Addition is commutative. Proof: (* FILL IN HERE *) *) (** [] *) (** **** Exercise: 2 stars, optional (beq_nat_refl_informal) *) (** Write an informal proof of the following theorem, using the informal proof of [plus_assoc] as a model. Don't just paraphrase the Coq tactics into English! Theorem: [true = beq_nat n n] for any [n]. Proof: (* FILL IN HERE *) [] *) (** $Date: 2016-02-17 14:39:13 -0800 (Wed, 17 Feb 2016) $ *)