Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Last active July 22, 2018 23:16
Show Gist options
  • Select an option

  • Save jtpaasch/286fd990fe6654d9d990cb3685aae320 to your computer and use it in GitHub Desktop.

Select an option

Save jtpaasch/286fd990fe6654d9d990cb3685aae320 to your computer and use it in GitHub Desktop.
Software Foundations, Logical Foundations, Induction.v
(** * Induction: Proof by Induction *)
(** Before getting started, we need to import all of our
definitions from the previous chapter: *)
Require Export Basics.
(** For the [Require Export] to work, you first 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. There are two ways to do it:
- In CoqIDE:
Open [Basics.v]. In the "Compile" menu, click on "Compile
Buffer".
- From the command line: Either
[make Basics.vo]
(assuming you've downloaded the whole LF directory and have a
working [make] command) or
[coqc Basics.v]
(which should work from any terminal window).
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 Coq.Init.Bar]
you should check whether you have multiple installations of Coq on
your machine. If so, 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. *)
(* ################################################################# *)
(** * 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. We also observed that proving the fact that it is
also a neutral element on the _right_... *)
Theorem plus_n_O_firsttry : forall n:nat,
n = n + 0.
(** ... 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.
(** 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 plus_n_O_secondtry : forall 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.
(** We could use [destruct n'] to get one step further, but,
since [n] can be arbitrarily large, if we just go on like this
we'll never finish. *)
(** 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: 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: *)
Theorem plus_n_O : forall 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. 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_diag : forall 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.
(** (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, recommended (basic_induction) *)
(** Prove the following using induction. You might need previously
proven results. *)
Theorem mult_0_r : forall n:nat,
n * 0 = 0.
Proof.
Print mult. (* The definition of [mult] (which is [*] is [Nat.mul]. *)
Print Nat.mul. (* We can see by the definition of [Nat.mul] that it handles [n * m]
by matching [n] against two cases:
- if [n] has the shape [0], then it just returns [m].
- if [n] has the shape of a successor [S] of some number [p], then it
returns [m + mult p m]. *)
intros n. (* Suppose [n] is a fixed number. *)
induction n as [| n' IHn']. (* Let's proceed by induction. To prove the theorem,
we'll prove that it holds for all the shapes [n] can take.
[n] can take two shapes: it can be [0], or it can be the
successor [S] of some number [n']. *)
- (* Case: [n = 0]. *)
(* We must show that [0 * 0 = 0]. *)
simpl. (* [simpl] can look at the definition of [*] and match the case where the first
argument is [0]. That just returns [m], which in this case is [0]. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case: [n = S n']. *)
(* We must show that [S n' * 0 = 0], with the induction hypothesis [n' * 0 = 0]. *)
simpl. (* Here [simpl] can match the second case of [*]'s definition, so it will return
[m + mult p m], which in this case is [0 + mult n' 0]. Then, [simpl] can look
at the definition of [+] and see that if you add [0 + x], it will just return [x].
So, in this case, it returns [mult n' 0], i.e., [n' * 0]. *)
rewrite -> IHn'. (* The induction hypothesis says that [n' * 0] is the same as [0],
so we can replace [n' * 0] in the goal with [0]. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
(* Informal proof:
Theorem: [forall n : nat, n * 0 = 0.
Proof: by induction on [n].
- Suppose [n] is a fixed number.
- Let's proceed by induction on [n].
- First, let [n = 0]. We must show that [0 * 0 = 0].
- This follows by the definition of [*], which says that [0 * m = m].
In this case, that yields [0 = 0].
- So the theorem holds for this case, by the reflexivity of equality.
- Second, let [n] be the successor [S] of some number [n'],
with an induction hypothesis:
[n' * 0 = 0].
We must show that [S n' * 0 = 0].
- the definition of [*], which says that [S n' * m = m + mult n' m].
In this case, that yields [0 + mult n' 0 = 0]. Furher, by the definition of [+],
[0 + m = m], which in this case yields [mult n' 0 = 0], or [n' * 0 = 0].
- The induction hypothesis says that [n' * 0] is the same as [0], so we can
replace [n' * 0] on the left hand side of the equation with [0], which
reduces the goal to [0 = 0].
- So the theorem holds for this case too, by the reflexivity of equality.
- We have shown that the theorem holds for this case too, by the reflexivity of equality.
- Since we've shown that the theorem holds for an arbitrary [n], we've shown that
it holds for all [n]s. []
*)
Theorem plus_n_Sm : forall n m : nat,
S (n + m) = n + (S m).
Proof.
intros n m. (* Suppose [n] and [m] are fixed numbers. *)
induction n as [|n' IHn']. (* Let's proceed by induction on [n]. *)
- (* Case: [n = 0]. *)
(* We must show that [S (0 + m) = 0 + (S m)]. *)
Print plus. Print Nat.add.
simpl. (* By the definition of [+], we can drop the first [0] on each side. *)
reflexivity. (* Both sides of the equation are now the same. *)
- (* Case: [n = S n']. *)
(* We must show that [S (S n' + m) = S n' + (S m)], assuming that [S (n' + m) = n' + (S m)]. *)
Print Nat.add. (* If [n] in [n + m] has the shape [S p], then [+] returns [S (add p m)]. *)
simpl. (* [simpl] will unfold that case in the definition, and replace [S n' + m] with
[S (n' + m)] on the left side of the equation, and it will replace [S n' + S m] with
[S (n' + S m)] on the right hand side of the equation. *)
rewrite -> IHn'. (* The induction hypothesis says that [S (n' + m)] is the same as
[n' + S m], so let's replace [S (n' + m)] with [n' + S m] on the
left hand side of the equation. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
Theorem plus_comm : forall n m : nat,
n + m = m + n.
Proof.
intros n m. (* Suppose [n] and [m] are each a fixed number. *)
induction n as [| n' IHn']. (* Let's proceed by induction. *)
- (* Case: [n = 0]. *)
(* We must show that [0 + m = m + 0]. *)
simpl. (* By the definition of [+], [0 + m] is the same as [m]. So [simpl] will replace
[0 + m] with [m] on the left hand side of the equation. *)
Print plus_n_O. (* This says [n = n + 0], i.e., [n] is the same as [n + 0], for any [n]. *)
rewrite <- plus_n_O. (* We can use [plus_n_O] to replace [m + 0] with [m]. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case: [n = S n']. *)
(* We must show that [S n' + m = m + S n'], with the induction hypothesis [n' + m = m + n']. *)
Print Nat.add. (* S p + m => S (p + m). *)
simpl. (* [simpl] will use the definition of [+] to replace [S n' + m] on the left hand side
of the equation with [S (n' + m)]. *)
rewrite -> IHn'. (* The induction hypothesis says that [n' + m] is the same as [m + n']. So
we can replace [n' + m] with [m + n'] in the goal. *)
Print plus_n_Sm. (* [forall n m : nat, S (n + m) = n + S m. *)
rewrite -> plus_n_Sm. (* [plus_n_Sm] says that [S (n + m)] is the same as [n + S m], so let's
relpace [S (m + n')] on the left hand side with [m + S n']. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
Theorem plus_assoc : forall n m p : nat,
n + (m + p) = (n + m) + p.
Proof.
intros n m p. (* Suppose [n], [m], and [p] are each fixed numbers. *)
induction n as [| n' IHn']. (* Let's try induction over [n]. *)
- (* Case: [n = 0]. *)
(* We must show that [0 + (m + p) = 0 + m + p]. *)
simpl. (* By the definition of [+], the [0]s will be removed. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case: [n = S n']. *)
(* We must show that [S n' + (m + p) = S n' + m + p], with an induction hypothesis
that [n' + (m + p) = n' + m + p]. *)
Print Nat.add. (* [S p + m => S (p + m)] *)
simpl. (* [simpl] will use the definition of [+]. That says that [S n' + m] is the same as
[S (n + m)]. So [simpl] will replace [S n' + (m + p)] with [S (n' + (m + p))] on
the left hand side of the equation. It will also replace [S n' + m + p] (which is
really [(S n' + m) + p]) on the right hand side of the equation with
[S (n' + m) + p], which is really [S (n' + m + p)]. *)
rewrite -> IHn'. (* The inductive hypothesis says that [n' + (m + p)] is the same as
[n' + m + p], so let's replace [n' + (m + p)] on the left hand side of the
equation with [n' + m + p]. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
(** [] *)
(** **** Exercise: 2 stars (double_plus) *)
(** Consider the following function, which doubles its argument: *)
Fixpoint double (n:nat) :=
match n with
| O => O (* If [n] is [0], doubling [0] is still [0]. *)
| S n' => S (S (double n')) (* If [n] is the successor [S] of some number [n'], then doubling [n]
will be the same as the successor [S] of the successor [S] of
doubling [n']. *)
end.
(** Use induction to prove this simple fact about [double]: *)
Lemma double_plus : forall n, double n = n + n.
Proof.
intros n. (* Suppose [n] is a fixed number. *)
induction n as [| n' IHn']. (* Let's try induction on [n]. *)
- (* Case: [n = 0]. *)
(* We must show that [double 0 = 0 + 0]. *)
simpl. (* By the definition of [double], [double 0] is the same as [0], so [simpl] will
replace the left hand side of the equation with [0]. By the definition of [+],
[0 + m = m], so [simpl] will replace the right hand side with [0]. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case: [n = S n']. *)
(* We must show that [double S n' = S n' + S n'],
with the induction hypothesis [double n' = n' + n']. *)
simpl. (* According to the definition of [double], [double S n'] is the same as [S (S double n')].
So [simpl] will replace [double (S n')] on the left hand side of the equation with
[S (S double n')]. According to the definition of [+], [S n' + m] is the same as
[S (n' + m)], so [simpl] will replace [S n' + S n'] on the right hand side of the
equation with [S (n' + Sn')]. *)
rewrite -> IHn'. (* The induction hypothesis says [double n'] is the same as [n' + n']. So let's
replace [double n'] with [n' + n'] on the left hand side of the equation. *)
Search "plus_n". (* Didn't we use a theorem before with the form [S (a + b) = a + S b]? *)
Print plus_n_Sm. (* [forall n m : nat, S (n + m) = n + S m. *)
rewrite -> plus_n_Sm. (* [plus_n_Sm] says that [S (n + m)] is the same as [n + S m], so let's
relpace [S (n' + n')] on the left hand side with [n' + S n']. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
(** [] *)
(** **** Exercise: 2 stars, optional (evenb_S) *)
(** One inconvenient aspect of our definition of [evenb n] is the
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 an
alternative characterization of [evenb (S n)] that works better
with induction: *)
Theorem evenb_S : forall n : nat,
evenb (S n) = negb (evenb n).
Proof.
intros n. (* Suppose [n] is a fixed number. *)
induction n as [| n' IHn']. (* Let's try induction on [n]. *)
- (* Case [n = 0]. *)
(* We must show that [evenb (S 0) = negb (evenb 0)]. *)
Print evenb. (* [evenb 0 => true], [evenb 1 = evenb S n = false]. [evenb S (S n') = evenb n'. *)
simpl. (* [simpl] will replace [evenb (S 0)] on the left hand side of the equation with [false],
and it will replace [evenb 0] on the right hand side with [true]. Then it will use
the definition of [negb] to replace [negb true] with [false]. *)
reflexivity. (* Now both sides of the equation are the same. *)
- (* Case [n = S n']. *)
(* We must show that [evenb (S (S n')) = negb (evenb S n')], with an
induction hypothesis [evenb (S n') = negb (evenb n')]. *)
Print evenb.
rewrite -> IHn'.
simpl. (* [simpl] will replace [evenb (S (S n'))] on the left hand side of the equation
with [evenb n']. *)
Search "negb". (* Wasn't there a lemma that says [negb (negb b) = b]? *)
Print negb_involutive. (* [negb (negb b)] is the same as [b]. *)
rewrite -> negb_involutive. (* So replace [negb (negb (evenb n'))] with [evenb n']. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
(** [] *)
(** **** Exercise: 1 star (destruct_induction) *)
(** Briefly explain the difference between the tactics [destruct]
and [induction].
Both [destruct x] and [induction x] will consider all possible shapes that [x] can take.
However, [induction] will also generate an induction hypothesis. This is useful for
proving facts about inductive data types that have recursive definitions. *)
(* Do not modify the following line: *)
Definition manual_grade_for_destruct_induction : option (prod nat string) := None.
(** [] *)
(* ################################################################# *)
(** * 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' : forall n m : nat,
(0 + n) * m = n * m.
Proof.
intros n m. (* Suppose [n] and [m] are each a fixed number. *)
assert (H: 0 + n = n).
{
(* The goal is to prove [H], namely that [0 + n = n]. *)
reflexivity. (* [reflexivity] can simplify [H] with the def. of [+]. *)
}
(* Now we return to proving our original goal, but
we have [H : 0 + n = n] as a hypothesis in the proof context. *)
rewrite -> H. (* [H] says that [0 + n] and [n] are the same, so replace [0 + n] on the left
hand side fo the equation with [n]. *)
reflexivity. (* Now both sides of the equation are the same. *)
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. *)
(** Another example of [assert]... *)
(** 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 not very smart 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 : forall n m p q : nat,
(n + m) + (p + q) = (m + n) + (p + q).
Proof.
intros n m p q. (* Suppose [n], [m], [p], and [q] are each a fixed number. *)
(* We just need to swap (n + m) for (m + n)... seems
like plus_comm should do the trick! *)
Print plus_comm. (* forall n m : nat, n + m = m + n *)
rewrite -> plus_comm. (* On the left hand side, this swaps [n + m] with [(p + q)].
Why didin't it flip anything on the right hand side of the equation?
And why didn't it just flip [n + m] itself? I presume the reason is
that Coq parses equations from the main connective first, which in
this case is the [+] in the very middle of each side of the equation.
So it initially sees the equation like this: [A + B = C + B], before it
dives down into [A], where it can see that [A] is [n + m], and then
[B], which is [(p + q)], and so on.
At this highest level of looking at the equation, Coq can indeed match
[A + B], or [C + B], with the theorem [plus_comm]. So, it can use
[plus_comm] on either one. Why does it pick [A + B] to flip?
I presume it just picks the first one it can match. *)
(* Doesn't work...Coq rewrote the wrong plus! *)
Abort.
(** To use [plus_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
[plus_comm], and then use it to do the desired rewrite. *)
Theorem plus_rearrange : forall n m p q : nat,
(n + m) + (p + q) = (m + n) + (p + q).
Proof.
intros n m p q. (* Suppose [n], [m], [p], and [q] are each a fixed number. *)
assert (H: n + m = m + n). (* When we declare this lemma, we're not using a [forall n m : nat].
We're only using the fixed [n] and [m] that we have in this context. *)
{
(* Our first goal is to prove [H], namely [n + m = m + n]. *)
rewrite -> plus_comm. (* [plus_comm] says [n + m] is the same as [m + n], for any [n].
So we can replace [n + m] on the left hand side of the equation
with [m + n]. *)
reflexivity. (* And now both sides of the equation are the same. *)
}
(* Now that we've proved [H], we have [H : n + m = m + n] in our context as a hypothesis.
This is different from [plus_comm], again, because [H] applies to the fixed [n + m],
not any [n + m]. *)
rewrite -> H. (* So now, when Coq looks to apply [H], it doesn't match the whole [A + B = C + B]
pattern, since [A] is [n + m + (p + q)], which doesn't match the fixed [n + m].
The only fixed [n + m] that matches is the one that appears right at the front
of the left side of the equation. So this replaces [m + n] with [n + m]. *)
(* Note that you could do [rewrite <- H]. That would match the [m + n] right at the beginning
of the right hand side. *)
reflexivity. (* Now both sides of the equation are the same. *)
Qed.
(* ################################################################# *)
(** * Formal vs. Informal Proof *)
(** "_Informal proofs are algorithms; formal proofs are code_." *)
(** What constitutes a successful proof of a mathematical claim?
The question has challenged philosophers for millennia, but a
rough and ready definition could be this: A proof of a
mathematical proposition [P] is a written (or spoken) text that
instills in the reader or hearer the certainty that [P] is true --
an unassailable argument for the truth of [P]. That is, a proof
is an act of communication.
Acts of communication may involve different sorts of readers. On
one hand, the "reader" can be a program like Coq, in which case
the "belief" that is instilled is that [P] can be mechanically
derived from a certain set of formal logical rules, and the proof
is a recipe that guides the program in checking this fact. Such
recipes are _formal_ proofs.
Alternatively, the reader can be a human being, in which case the
proof will be written in English or some other natural language,
and will thus necessarily be _informal_. Here, the criteria for
success are less clearly specified. A "valid" proof is one that
makes the reader believe [P]. But the same proof may be read by
many different readers, some of whom may be convinced by a
particular way of phrasing the argument, while others may not be.
Some readers may be particularly pedantic, inexperienced, or just
plain thick-headed; the only way to convince them will be to make
the argument in painstaking detail. But other readers, more
familiar in the area, may find all this detail so overwhelming
that they lose the overall thread; all they want is to be told the
main ideas, since it is easier for them to fill in the details for
themselves than to wade through a written presentation of them.
Ultimately, there is no universal standard, because there is no
single way of writing an informal proof that is guaranteed to
convince every conceivable reader.
In practice, however, mathematicians have developed a rich set of
conventions and idioms for writing about complex mathematical
objects that -- at least within a certain community -- make
communication fairly reliable. The conventions of this stylized
form of communication give a fairly clear standard for judging
proofs good or bad.
Because we are using Coq in this course, we will be working
heavily with formal proofs. But this doesn't mean we can
completely forget about informal ones! Formal proofs are useful
in many ways, but they are _not_ very efficient ways of
communicating ideas between human beings. *)
(** For example, here is a proof that addition is associative: *)
Theorem plus_assoc' : forall 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.
(** 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.
[forall n m : nat, n + m = m + n]
Proof: By induction on [n].
- Suppose [n] and [m] are each a fixed number.
- First, suppose [n = 0]. We must show [0 + m = m + 0].
- [0 + m] is the same as [0], by the definition of [+].
- [m + 0] is the same as [0], by the lemma [plus_n_0].
- So the theorem holds for this case, by the reflexivity of equality.
- Next, suppose [n] is the successor [S] of some number [n'],
with an induction hypothesis:
[n' + m = m + n'].
We must show that [S n' + m = m + S n'].
- By the definition of [+], [S n' + m] is the same as [S (n' + m)].
- By the induction hypothesis, [n' + m] is the same as [m + n'],
so that yields [S (m + n')].
- By the lemma [plus_n_Sm], [S (m + n')] is the same as [m + S n'].
- So the theorem holds for this case too, by the reflexivity of equality.
- We've shown that the theorem holds for all shapes [n] can take.
- Since we've shown the theorem holds for an arbitrary [n], we've shown that
it holds for all [n]s.
*)
(* Do not modify the following line: *)
Definition manual_grade_for_plus_comm_informal : option (prod nat string) := None.
(** [] *)
Print beq_nat.
(** **** 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: By induction on [n].
- Suppose [n] is a fixed number.
- First, suppose [n = 0]. We must show that [true = beq_nat 0 0].
- This follows immediately from the fact that [beq_nat 0 0] is true.
- Next, suppose [n] is the successor [S] of some number [n'], with
an induction hypothesis:
[true = beq_nat n' n'].
We must show that [true = beq_nat (S n') (S n')].
- By the definition of [beq_nat], if [n] is [S n'] and [m] is [S m'],
then [beq_nat (S n') (S m')] is the same as [beq_nat n' m'].
- But [beq_nat n' m'] is [true], by the induction hypothesis.
- So the theorem holds for this case too.
- We've shown that the theorem holds for all shapes [n] can take.
- Since we've shown that the theorem holds for an arbitrary [n],
we've shown that it holds for all [n]s.
*)
(** [] *)
(* ################################################################# *)
(** * 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 : forall n m p : nat,
n + (m + p) = m + (n + p).
Proof.
intros n m p. (* Suppose [n], [m], and [p] are each a fixed number. *)
rewrite -> plus_comm. (* [plus_comm] says [a + b] is the same as [b + a]. So we can replace
[n + (m + p)] with [(m + p) + n] on the left hand side of the equation. *)
assert (H: p + n = n + p). (* I want to match [(n + p)] on the right hand side of the equation,
and then replace it with [p + n]. *)
{
(* The goal is to prove [p + n = n + p]. *)
rewrite -> plus_comm. (* [plus_comm] does exactly what I need. *)
reflexivity. (* Now both sides of the equation are the same. *)
}
(* We return to proving our original goal, but now we have [H] in the proof context. *)
rewrite <- H. (* [H] says [n + p] is the same as [p + n], so we can replace [(n + p)] on the
right hand side of the equation with [p + n]. *)
rewrite -> plus_assoc. (* [plus_assoc] says [(a + b) + c)] (which Coq renders as [a + b + c])
is the same as [a + (b + c)]. So we can replace [(m + p) + n)] (which
Coq renders as [m + p + n]) with [m + (p + n)] on the left hand side
of the equation. *)
reflexivity. (* Both sides of the equation are the same now. *)
Qed.
(** 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 : forall 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!) *)
Check leb.
Theorem leb_refl : forall n:nat,
true = leb n n.
Proof.
(* FILL IN HERE *) Admitted.
Theorem zero_nbeq_S : forall n:nat,
beq_nat 0 (S n) = false.
Proof.
(* FILL IN HERE *) Admitted.
Theorem andb_false_r : forall b : bool,
andb b false = false.
Proof.
(* FILL IN HERE *) Admitted.
Theorem plus_ble_compat_l : forall n m p : nat,
leb n m = true -> leb (p + n) (p + m) = true.
Proof.
(* FILL IN HERE *) Admitted.
Theorem S_nbeq_0 : forall n:nat,
beq_nat (S n) 0 = false.
Proof.
(* FILL IN HERE *) Admitted.
Theorem mult_1_l : forall n:nat, 1 * n = n.
Proof.
(* FILL IN HERE *) Admitted.
Theorem all3_spec : forall b c : bool,
orb
(andb b c)
(orb (negb b)
(negb c))
= true.
Proof.
(* FILL IN HERE *) Admitted.
Theorem mult_plus_distr_r : forall n m p : nat,
(n + m) * p = (n * p) + (m * p).
Proof.
(* FILL IN HERE *) Admitted.
Theorem mult_assoc : forall 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 : forall 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' : forall n m p : nat,
n + (m + p) = m + (n + p).
Proof.
(* FILL IN HERE *) Admitted.
(** [] *)
(** **** Exercise: 3 stars, recommended (binary_commute) *)
(** Recall the [incr] and [bin_to_nat] functions that you
wrote for the [binary] exercise in the [Basics] chapter. Prove
that the following diagram commutes:
incr
bin ----------------------> bin
| |
bin_to_nat | | bin_to_nat
| |
v v
nat ----------------------> nat
S
That is, incrementing a binary number and then converting it to
a (unary) natural number yields the same result as first converting
it to a natural number and then incrementing.
Name your theorem [bin_to_nat_pres_incr] ("pres" for "preserves").
Before you start working on this exercise, copy the definitions
from your solution to the [binary] exercise here so that this file
can be graded on its own. If you want to change your original
definitions to make the property easier to prove, feel free to
do so! *)
(* FILL IN HERE *)
(* Do not modify the following line: *)
Definition manual_grade_for_binary_commute : option (prod nat string) := None.
(** [] *)
(** **** 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; please copy them to this file to make
it self contained for grading.
(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 *)
(* Do not modify the following line: *)
Definition manual_grade_for_binary_inverse : option (prod nat string) := None.
(** [] *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment