Require Import name_case. (* ###################################################################### *) (** * Induction *) (** We proved above that [0] is a neutral element for [+] on the left using a simple partial evaluation argument. The fact that it is also a neutral element on the _right_... *) Theorem plus_0_r_firsttry : forall n:nat, n + 0 = n. (** ... cannot be proved in the same simple way. Just applying [reflexivity] doesn't work: the [n] in [n + 0] is an arbitrary unknown number, so the [match] in the definition of [+] can't be simplified. 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, 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 continue this way we'll never be done. *) Proof. intros n. simpl. (* Does nothing! *) Admitted. (** Case analysis gets us a little further, but not all the way: *) Theorem plus_0_r_secondtry : forall n:nat, n + 0 = n. Proof. intros n. destruct n as [| n']. Case "n = 0". reflexivity. (* so far so good... *) Case "n = S n'". simpl. (* ...but here we are stuck again *) Admitted. (** To prove such facts -- indeed, to prove most interesting facts about numbers, lists, and other inductively defined sets -- we need a more powerful reasoning principle: _induction_. Recall (from high school) 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 we are trying to prove at the moment: *) Theorem plus_0_r : forall n:nat, n + 0 = n. Proof. intros n. induction n as [| n' IHn']. Case "n = 0". reflexivity. Case "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']). The goal in this case becomes [(S n') + 0 = S n'], which simplifies to [S (n' + 0) = S n'], which in turn follows from the induction hypothesis. *) Theorem minus_diag : forall n, minus n n = 0. Proof. intros n. induction n as [| n' IHn']. Case "n = 0". simpl. reflexivity. Case "n = S n'". simpl. rewrite -> IHn'. reflexivity. Qed. (** **** Exercise: 1 star (basic_induction) *) Theorem mult_0_r : forall n:nat, n * 0 = 0. Proof. (* FILL IN HERE *) Admitted. Theorem plus_n_Sm : forall n m : nat, S (n + m) = n + (S m). Proof. (* FILL IN HERE *) Admitted. Theorem plus_comm : forall n m : nat, n + m = m + n. Proof. (* FILL IN HERE *) Admitted. (** [] *) Fixpoint double (n:nat) := match n with | O => O | S n' => S (S (double n')) end. (** **** Exercise: 1 star *) Lemma double_plus : forall n, double n = n + n . Proof. (* FILL IN HERE *) Admitted. (** [] *) (* ###################################################################### *) (** * Formal vs. Informal Proof *) (** The question of what, exactly, constitutes a "proof" of a mathematical claim has challenged philosophers for millenia. A rough and ready definition, though, could be this: a proof of a mathematical proposition [P] is a written (or, sometimes, spoken) text that instills in the reader or hearer the certainty that [P] is true. That is, a proof is an act of communication. Now, 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 a simple mechanical check that [P] can be derived from a certain set of formal logical rules, and the proof is a recipe that guides the program in performing this check. Such recipies are called "formal proof". Alternatively, the reader can be a human being, in which case the proof will be written in English or some other natural language, thus necessarily "informal". Here, the criteria for success are less clearly specified. A "good" 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. One reader 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 another reader, 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, because it is easier to fill in the details for themselves. 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, 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 will be using Coq in this course, we will be working heavily with formal proofs. But this doesn't mean we can ignore the 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 as a proof. For a human, however, it is difficult to make much sense of it. If you're used to Coq you can probably 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. Instead, a mathematician would write it 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'], with [[ 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. [] *) (** The overall form of the proof is basically similar. (This is no accident, of course: 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. *) (** Here is a formal proof that shows the structure 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']. Case "n = 0". reflexivity. Case "n = S n'". simpl. rewrite -> IHn'. reflexivity. Qed. (** **** Exercise: 2 stars, optional (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 (bet_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 *) [] *) (** **** Exercise: 2 stars *) Theorem beq_nat_refl : forall n : nat, true = beq_nat n n. Proof. (* FILL IN HERE *) Admitted. (** [] *) (* ###################################################################### *) (** * Proofs Within Proofs *) (** In Coq, as in informal mathematics, large proofs are very often broken into a sequence of theorems, with later proofs referring to earlier theorems. Occasionally, however, a proof will need 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 can also 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. assert (e: 0 + n = n). Case "Proof of assertion". reflexivity. rewrite -> e. 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]. (Note that we could also name the assertion with [as] just as we did above with [destruct] and [induction], i.e., [assert (0 + n = n) as H]. Also note that we mark the proof of this assertion with a [Case], both for readability and so that, when using Coq interactively, we can see when we're finished proving the assertion by observing when the ["Proof of assertion"] string disappears from the context.) The second goal is the same as the one at the point where we invoke [assert], except that, in the context, we 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. *) (** Actually, [assert] will turn out to be 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 : forall 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! *) Admitted. (** To get [plus_comm] to apply at the point where we want 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 this lemma 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. assert (H: n + m = m + n). Case "Proof of assertion". rewrite -> plus_comm. reflexivity. rewrite -> H. reflexivity. Qed. (** **** Exercise: 3 stars (mult_comm) *) (** Use [assert] to help prove this theorem. You shouldn't need to use induction. *) Theorem plus_swap : forall 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 : forall m n : nat, m * n = n * m. Proof. (* FILL IN HERE *) Admitted. (** [] *) (** **** Exercise: 2 stars, optional *) Theorem evenb_n__oddb_Sn : forall n : nat, evenb n = negb (evenb (S n)). Proof. (* FILL IN HERE *) Admitted. (** [] *) (* ###################################################################### *) (** * Extra Exercises *) (** **** Exercise: 3 stars (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 think before hacking!) *) Theorem ble_nat_refl : forall n:nat, true = ble_nat 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, ble_nat n m = true -> ble_nat (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 *) (** The [replace] tactic allows you specify a particular subterm to rewrite and what you want it rewritten to. More precisely, [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 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: 4 stars (binary) *) (** Consider a different, more efficient representation of natural numbers using a binary rather than unary system. That is, instead of saying that each natural number is either zero or the successor of a natural number, we can say that each binary number is either - zero, - twice a binary number, or - one more than twice a binary number. First, write an inductive definition of the type [bin] corresponding to this description of binary numbers. Next, write an increment function for binary numbers, and a function to convert binary numbers to unary numbers. Finally, prove that your increment and binary-to-unary functions commute: 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. *) (* FILL IN HERE *) (** [] *) (** **** Exercise: 2 stars, optional (decreasing) *) (** The requirement that some argument to each function be "decreasing" is a fundamental feature of Coq's design: In particular, it guarantees that every function that can be defined in Coq will terminate on all inputs. However, because Coq's "decreasing analysis" is not very sophisticated, it is sometimes necessary to write functions in slightly unnatural ways. To get a concrete sense of this, find a way to write a sensible [Fixpoint] definition (of a simple function on numbers, say) that _does_ terminate on all inputs but that Coq will _not_ accept because of this restriction. *) (* FILL IN HERE *)