InductionProof by Induction
Separate Compilation
For this Require Export command to work, Coq needs to be
able to find a compiled version of Basics.v, called Basics.vo,
in a directory associated with the prefix LF. This file is
analogous to the .class files compiled from .java source files
and the .o files compiled from .c files.
First create a file named _CoqProject containing the following
line (if you obtained the whole volume "Logical Foundations" as a
single archive, a _CoqProject should already exist and you can
skip this step):
-Q . LF This maps the current directory (".", which contains Basics.v, Induction.v, etc.) to the prefix (or "logical directory") "LF". Proof General and CoqIDE read _CoqProject automatically, so they know to where to look for the file Basics.vo corresponding to the library LF.Basics.
Once _CoqProject is thus created, there are various ways to
build Basics.vo:
If you have trouble (e.g., if you get complaints about missing
identifiers later in the file), it may be because the "load path"
for Coq is not set up correctly. The Print LoadPath. command
may be helpful in sorting out such issues.
In particular, if you see a message like
Compiled library Foo makes inconsistent assumptions over
library Bar check whether you have multiple installations of Coq on your machine. It may be that commands (like coqc) that you execute in a terminal window are getting a different version of Coq than commands executed by Proof General or CoqIDE.
One more tip for CoqIDE users: If you see messages like Error:
Unable to locate library Basics, a likely reason is
inconsistencies between compiling things within CoqIDE vs using
coqc from the command line. This typically happens when there
are two incompatible versions of coqc installed on your
system (one associated with CoqIDE, and one associated with coqc
from the terminal). The workaround for this situation is
compiling using CoqIDE only (i.e. choosing "make" from the menu),
and avoiding using coqc directly at all.
-Q . LF This maps the current directory (".", which contains Basics.v, Induction.v, etc.) to the prefix (or "logical directory") "LF". Proof General and CoqIDE read _CoqProject automatically, so they know to where to look for the file Basics.vo corresponding to the library LF.Basics.
- In Proof General or CoqIDE, the compilation should happen
automatically when you submit the Require line above to PG.
- For VSCode users, open the terminal pane at the bottom
and then use the command line instructions below.
(If you downloaded the project setup .tgz file, just doing `make`
should build all the code.)
- If you want to compile from the command line, generate a
Makefile using the coq_makefile utility, which comes
installed with Coq (if you obtained the whole volume as a
single archive, a Makefile should already exist and you can
skip this step):
coq_makefile -f _CoqProject *.v -o Makefile Note: You should rerun that command whenever you add or remove Coq files to the directory.
make Basics.vo All files in the directory can be compiled by giving no arguments:
make Under the hood, make uses the Coq compiler, coqc. You can also run coqc directly:
coqc -Q . LF Basics.v But make also calculates dependencies between source files to compile them in the right order, so make should generally be preferred over explicit coqc.
Compiled library Foo makes inconsistent assumptions over
library Bar check whether you have multiple installations of Coq on your machine. It may be that commands (like coqc) that you execute in a terminal window are getting a different version of Coq than commands executed by Proof General or CoqIDE.
- Another common reason is that the library Bar was modified and recompiled without also recompiling Foo which depends on it. Recompile Foo, or everything if too many files are affected. (Using the third solution above: make clean; make.)
Proof by Induction
... can't be done 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.
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.
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 could use destruct n' to get one step further, but,
since n can be arbitrarily large, we'll never get all the there
if we just go on like this.
To prove interesting facts about numbers, lists, and other
inductively defined sets, we often 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:
In Coq, the steps are the same: 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: one where we must show P(O)
and another where we must show P(n') → P(S n'). Here's how
this works for the theorem at hand:
- 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.
Theorem add_0_r : ∀ n:nat, n + 0 = n.
Proof.
intros n. induction n as [| n' IHn'].
- (* n = 0 *) reflexivity.
- (* n = S n' *) simpl. rewrite → IHn'. reflexivity. Qed.
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. Since there are two subgoals, the as... clause
has two parts, separated by |. (Strictly speaking, we can omit
the as... clause and Coq will choose names for us. In practice,
this is a bad idea, as Coq's automatic choices tend to be
confusing.)
In the first subgoal, n is replaced by 0. No new variables
are introduced (so the first part of the as... is empty), and
the goal becomes 0 = 0 + 0, which follows by simplification.
In the second subgoal, 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'). These two names
are specified in the second part of the as... clause. The goal
in this case becomes S n' = (S n') + 0, which simplifies to
S n' = S (n' + 0), which in turn follows from IHn'.
Theorem minus_n_n : ∀ 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.
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.
(The use of the intros tactic in these proofs is actually
redundant. When applied to a goal that contains quantified
variables, the induction tactic will automatically move them
into the context as needed.)
Exercise: 2 stars, standard, especially useful (basic_induction)
Prove the following using induction. You might need previously proven results.
Theorem mul_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 add_comm : ∀ n m : nat,
n + m = m + n.
Proof.
(* FILL IN HERE *) Admitted.
Theorem add_assoc : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
(* FILL IN HERE *) Admitted.
☐
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 add_comm : ∀ n m : nat,
n + m = m + n.
Proof.
(* FILL IN HERE *) Admitted.
Theorem add_assoc : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
(* FILL IN HERE *) Admitted.
☐
Exercise: 2 stars, standard (double_plus)
Consider the following function, which doubles its argument:
Use induction to prove this simple fact about double:
Exercise: 2 stars, standard (eqb_refl)
The following theorem relates the computational equality =? on nat with the definitional equality = on bool.Exercise: 1 star, standard (even_S)
One inconvenient aspect of our definition of even n is the recursive call on n - 2. This makes proofs about even n harder when done by induction on n, since we may need an induction hypothesis about n - 2. The following lemma gives an alternative characterization of even (S n) that works better with induction:Proofs Within Proofs
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. }
rewrite → H.
reflexivity. Qed.
(n + 0 + 0) × m = n × m.
Proof.
intros n m.
assert (H: n + 0 + 0 = n).
{ rewrite add_comm. simpl. rewrite add_comm. 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 (n + 0
+ 0 = 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 n + 0 + 0 = 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.
As another 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 (add_comm) to rewrite one into the
other. However, the rewrite tactic is not very smart about
where it applies the rewrite. There are three uses of + here,
and it turns out that doing rewrite → add_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)... seems
like add_comm should do the trick! *)
rewrite add_comm.
(* Doesn't work... Coq rewrites the wrong plus! :-( *)
Abort.
(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.
(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.
Formal vs. Informal Proof
"_Informal proofs are algorithms; formal proofs are code."
Theorem add_assoc' : ∀ n m p : nat,
n + (m + p) = (n + m) + p.
Proof. intros n m p. induction n as [| n' IHn']. reflexivity.
simpl. rewrite IHn'. reflexivity. Qed.
n + (m + p) = (n + m) + p.
Proof. intros n m p. induction n as [| n' IHn']. reflexivity.
simpl. rewrite 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 add_assoc'' : ∀ 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.
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 might 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:
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).
- 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 that
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 now show that
(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.
- First, suppose n = 0. We must show that
More Exercises
Exercise: 3 stars, standard, especially useful (mul_comm)
Use assert to help prove add_shuffle3. You don't need to use induction yet.
Theorem add_shuffle3 : ∀ n m p : nat,
n + (m + p) = m + (n + p).
Proof.
(* FILL IN HERE *) Admitted.
n + (m + p) = m + (n + p).
Proof.
(* FILL IN HERE *) Admitted.
Now prove commutativity of multiplication. You will probably want
to look for (or define and prove) a "helper" theorem to be used in
the proof of this one. Hint: what is n × (1 + k)?
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.
☐
(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.
☐
(* 2023-09-04 16:09 *)