ListsWorking with Structured Data

Overview

In this chapter, we'll introduce some data structures, i.e., ways of storing and arranging data beyond just numbers. We'll focus on the simplest data structure, lists, which are possibly empty ordered sequences. For now, we'll just work with lists of numbers.
We'll find that the ideas from Induction apply very neatly to lists!

From DMFP Require Export Induction.
Module NatList.

Pairs of Numbers

In an Inductive type definition, each constructor can take any number of arguments -- none (as with true and O), one (as with S), or more than one, as here:

Inductive natprod : Type :=
| pair (n1 n2 : nat).
This declaration can be read: "The one and only way to construct a pair of numbers is by applying the constructor pair to two arguments of type nat."

Check (pair 3 5).
Here are two simple functions for extracting the first and second components of a pair. The definitions also illustrate how to do pattern matching on two-argument constructors.

Definition fst (p : natprod) : nat :=
  match p with
  | pair x yx
  end.

Definition snd (p : natprod) : nat :=
  match p with
  | pair x yy
  end.

Compute (fst (pair 3 5)).
(* ===> 3 *)
Since pairs are used quite a bit, it is nice to be able to write them with the standard mathematical notation (x,y) instead of pair x y. We can tell Coq to allow this with a Notation declaration.

Notation "( x , y )" := (pair x y).
The new pair notation can be used both in expressions and in pattern matches (indeed, we've actually seen this already in the Basics chapter, in the definition of the minus function -- this works because the pair notation is also provided as part of the standard library):

Compute (fst (3,5)).

Definition fst' (p : natprod) : nat :=
  match p with
  | (x,y)x
  end.

Definition snd' (p : natprod) : nat :=
  match p with
  | (x,y)y
  end.

Definition swap_pair (p : natprod) : natprod :=
  match p with
  | (x,y)(y,x)
  end.
Let's try to prove a few simple facts about pairs.
If we state things in a particular (and slightly peculiar) way, we can complete proofs with just reflexivity (and its built-in simplification):

Theorem surjective_pairing' : (n m : nat),
  (n,m) = (fst (n,m), snd (n,m)).
Proof.
  reflexivity. Qed.
But reflexivity is not enough if we state the lemma in a more natural way:

Theorem surjective_pairing_stuck : (p : natprod),
  p = (fst p, snd p).
Proof.
  simpl. (* Doesn't reduce anything! *)
Abort.
We have to expose the structure of p so that simpl can perform the pattern match in fst and snd. We can do this with destruct.

Theorem surjective_pairing : (p : natprod),
  p = (fst p, snd p).
Proof.
  intros p. destruct p as [n m]. simpl. reflexivity. Qed.
Notice that, unlike its behavior with nats, destruct generates just one subgoal here. That's because natprods can only be constructed in one way.

Exercise: 1 star, standard (snd_fst_is_swap)

Theorem snd_fst_is_swap : (p : natprod),
  (snd p, fst p) = swap_pair p.
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 1 star, standard, optional (fst_swap_is_snd)

Theorem fst_swap_is_snd : (p : natprod),
  fst (swap_pair p) = snd p.
Proof.
  (* FILL IN HERE *) Admitted.

Lists of Numbers

Generalizing the definition of pairs, we can describe the type of lists of numbers like this: "A list is either the empty list or else a pair of a number and another list."

Inductive natlist : Type :=
  | nil
  | cons (n : nat) (l : natlist).
For example, here is a three-element list:

Definition mylist := cons 1 (cons 2 (cons 3 nil)).
As with pairs, it is more convenient to write lists in familiar programming notation. The following declarations allow us to use :: as an infix cons operator and square brackets as an "outfix" notation for constructing lists.

Notation "x :: l" := (cons x l)
                     (at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y nil) ..).
It is not necessary to understand the details of these declarations, but in case you are interested, here is roughly what's going on. The right associativity annotation tells Coq how to parenthesize expressions involving several uses of :: so that, for example, the next three declarations mean exactly the same thing:

Definition mylist1 := 1 :: (2 :: (3 :: nil)).
Definition mylist2 := 1 :: 2 :: 3 :: nil.
Definition mylist3 := [1;2;3].
The at level 60 part tells Coq how to parenthesize expressions that involve both :: and some other infix operator. For example, since we defined + as infix notation for the plus function at level 50,
  Notation "x + y" := (plus x y)
                      (at level 50, left associativity).
the + operator will bind tighter than ::, so 1 + 2 :: [3] will be parsed, as we'd expect, as (1 + 2) :: [3] rather than 1 + (2 :: [3]).
(Expressions like "1 + 2 :: [3]" can be a little confusing when you read them in a .v file. The inner brackets, around 3, indicate a list, but the outer brackets, which are invisible in the HTML rendering, are there to instruct the "coqdoc" tool that the bracketed part should be displayed as Coq code rather than running text.)
The second and third Notation declarations above introduce the standard square-bracket notation for lists; the right-hand side of the third one illustrates Coq's syntax for declaring n-ary notations and translating them to nested sequences of binary constructors.

Repeat

A number of functions are useful for manipulating lists. For example, the repeat function takes a number n and a count and returns a list of length count where every element is n.

Fixpoint repeat (n count : nat) : natlist :=
  match count with
  | Onil
  | S count'n :: (repeat n count')
  end.

Length

The length function calculates the length of a list.

Fixpoint length (l:natlist) : nat :=
  match l with
  | nilO
  | h :: tS (length t)
  end.

Append

The app function concatenates (appends) two lists.

Fixpoint app (l1 l2 : natlist) : natlist :=
  match l1 with
  | nill2
  | h :: th :: (app t l2)
  end.
Actually, app will be used a lot in some parts of what follows, so it is convenient to have an infix operator for it.

Notation "x ++ y" := (app x y)
                     (right associativity, at level 60).

Example test_app1: [1;2;3] ++ [4;5] = [1;2;3;4;5].
Proof. reflexivity. Qed.
Example test_app2: nil ++ [4;5] = [4;5].
Proof. reflexivity. Qed.
Example test_app3: [1;2;3] ++ nil = [1;2;3].
Proof. reflexivity. Qed.

Head (with default) and Tail

Here are two smaller examples of programming with lists. The hd function returns the first element (the "head") of the list, while tl returns everything but the first element (the "tail"). Of course, the empty list has no first element, so we must pass a default value to be returned in that case.

Definition hd (default:nat) (l:natlist) : nat :=
  match l with
  | nildefault
  | h :: th
  end.

Definition tl (l:natlist) : natlist :=
  match l with
  | nilnil
  | h :: tt
  end.

Example test_hd1: hd 0 [1;2;3] = 1.
Proof. reflexivity. Qed.
Example test_hd2: hd 0 [] = 0.
Proof. reflexivity. Qed.
Example test_tl: tl [1;2;3] = [2;3].
Proof. reflexivity. Qed.

Exercises

Exercise: 2 stars, standard, recommended (list_funs)

Complete the definitions of nonzeros, oddmembers and countoddmembers below. Have a look at the tests to understand what these functions should do.

Fixpoint nonzeros (l:natlist) : natlist
  (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_nonzeros:
  nonzeros [0;1;0;2;3;0;0] = [1;2;3].
  (* FILL IN HERE *) Admitted.

Fixpoint oddmembers (l:natlist) : natlist
  (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_oddmembers:
  oddmembers [0;1;0;2;3;0;0] = [1;3].
  (* FILL IN HERE *) Admitted.

Definition countoddmembers (l:natlist) : nat
  (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_countoddmembers1:
  countoddmembers [1;0;3;1;4;5] = 4.
  (* FILL IN HERE *) Admitted.

Example test_countoddmembers2:
  countoddmembers [0;2;4] = 0.
  (* FILL IN HERE *) Admitted.

Example test_countoddmembers3:
  countoddmembers nil = 0.
  (* FILL IN HERE *) Admitted.

Exercise: 3 stars, advanced (alternate)

Complete the definition of alternate, which "zips up" two lists into one, alternating between elements taken from the first list and elements from the second. See the tests below for more specific examples.
Note: one natural and elegant way of writing alternate will fail to satisfy Coq's requirement that all Fixpoint definitions be "obviously terminating." If you find yourself in this rut, look for a slightly more verbose solution that considers elements of both lists at the same time. (One possible solution requires defining a new kind of pairs, but this is not the only way.)

Fixpoint alternate (l1 l2 : natlist) : natlist
  (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_alternate1:
  alternate [1;2;3] [4;5;6] = [1;4;2;5;3;6].
  (* FILL IN HERE *) Admitted.

Example test_alternate2:
  alternate [1] [4;5;6] = [1;4;5;6].
  (* FILL IN HERE *) Admitted.

Example test_alternate3:
  alternate [1;2;3] [4] = [1;4;2;3].
  (* FILL IN HERE *) Admitted.

Example test_alternate4:
  alternate [] [20;30] = [20;30].
  (* FILL IN HERE *) Admitted.

Reasoning About Lists

As with numbers, simple facts about list-processing functions can sometimes be proved entirely by simplification. For example, the simplification performed by reflexivity is enough for this theorem...

Theorem nil_app : l:natlist,
  [] ++ l = l.
Proof. reflexivity. Qed.
... because the [] is substituted into the "scrutinee" (the expression whose value is being "scrutinized" by the match) in the definition of app, allowing the match itself to be simplified.
Also, as with numbers, it is sometimes helpful to perform case analysis on the possible shapes (empty or non-empty) of an unknown list.

Theorem tl_length_pred : l:natlist,
  pred (length l) = length (tl l).
Proof.
  intros l. destruct l as [| n l'].
  - (* l = nil *)
    reflexivity.
  - (* l = cons n l' *)
    reflexivity. Qed.
Here, the nil case works because we've chosen to define tl nil = nil. Notice that the as annotation on the destruct tactic here introduces two names, n and l', corresponding to the fact that the cons constructor for lists takes two arguments (the head and tail of the list it is constructing).
Usually, though, interesting theorems about lists require induction for their proofs.

Micro-Sermon

Simply reading example proof scripts will not get you very far! It is important to work through the details of each one, using Coq and thinking about what each step achieves. Otherwise it is more or less guaranteed that the exercises will make no sense when you get to them. 'Nuff said.

Induction on Lists

Proofs by induction over datatypes like natlist are a little less familiar than standard natural number induction, but the idea is equally simple. Each Inductive declaration defines a set of data values that can be built up using the declared constructors: a boolean can be either true or false; a number can be either O or S applied to another number; a list can be either nil or cons applied to a number and a list.
Moreover, applications of the declared constructors to one another are the only possible shapes that elements of an inductively defined set can have, and this fact directly gives rise to a way of reasoning about inductively defined sets: a number is either O or else it is S applied to some smaller number; a list is either nil or else it is cons applied to some number and some smaller list; etc. So, if we have in mind some proposition P that mentions a list l and we want to argue that P holds for all lists, we can reason as follows:
  • First, show that P is true of l when l is nil.
  • Then show that P is true of l when l is cons n l' for some number n and some smaller list l', assuming that P is true for l'.
Since larger lists can only be built up from smaller ones, eventually reaching nil, these two arguments together establish the truth of P for all lists l. Here's a concrete example:

Theorem app_assoc : l1 l2 l3 : natlist,
  (l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof.
  intros l1 l2 l3. induction l1 as [| n l1' IHl1'].
  - (* l1 = nil *)
    reflexivity.
  - (* l1 = cons n l1' *)
    simpl. rewriteIHl1'. reflexivity. Qed.
Notice that, as when doing induction on natural numbers, the as... clause provided to the induction tactic gives a name to the induction hypothesis corresponding to the smaller list l1' in the cons case. Once again, this Coq proof is not especially illuminating as a static written document -- it is easy to see what's going on if you are reading the proof in an interactive Coq session and you can see the current goal and context at each point, but this state is not visible in the written-down parts of the Coq proof. So a natural-language proof -- one written for human readers -- will need to include more explicit signposts; in particular, it will help the reader stay oriented if we remind them exactly what the induction hypothesis is in the second case.
For comparison, here is an informal proof of the same theorem.
Theorem: For all lists l1, l2, and l3, (l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof: By induction on l1.
  • First, suppose l1 = []. We must show
           ([] ++ l2) ++ l3 = [] ++ (l2 ++ l3),
    which follows directly from the definition of ++.
  • Next, suppose l1 = n::l1', with
           (l1' ++ l2) ++ l3 = l1' ++ (l2 ++ l3)
    (the induction hypothesis). We must show
           ((n :: l1') ++ l2) ++ l3 = (n :: l1') ++ (l2 ++ l3).
    By the definition of ++, this follows from
           n :: ((l1' ++ l2) ++ l3) = n :: (l1' ++ (l2 ++ l3)),
    which is immediate from the induction hypothesis.

Reversing a List

For a slightly more involved example of inductive proof over lists, suppose we use app to define a list-reversing function rev:

Fixpoint rev (l:natlist) : natlist :=
  match l with
  | nilnil
  | h :: trev t ++ [h]
  end.

Example test_rev1: rev [1;2;3] = [3;2;1].
Proof. reflexivity. Qed.
Example test_rev2: rev nil = nil.
Proof. reflexivity. Qed.

Properties of rev

Now let's prove some theorems about our newly defined rev. For something a bit more challenging than what we've seen, let's prove that reversing a list does not change its length. Our first attempt gets stuck in the successor case...

Theorem rev_length_firsttry : l : natlist,
  length (rev l) = length l.
Proof.
  intros l. induction l as [| n l' IHl'].
  - (* l =  *)
    reflexivity.
  - (* l = n :: l' *)
    (* This is the tricky case.  Let's begin as usual
       by simplifying. *)

    simpl.
    (* Now we seem to be stuck: the goal is an equality
       involving ++, but we don't have any useful equations
       in either the immediate context or in the global
       environment!  We can make a little progress by using
       the IH to rewrite the goal... *)

    rewrite <- IHl'.
    (* ... but now we can't go any further. *)
Abort.
So let's take the equation relating ++ and length that would have enabled us to make progress and prove it as a separate lemma.

Theorem app_length : l1 l2 : natlist,
  length (l1 ++ l2) = (length l1) + (length l2).
Proof.
  (* WORKED IN CLASS *)
  intros l1 l2. induction l1 as [| n l1' IHl1'].
  - (* l1 = nil *)
    reflexivity.
  - (* l1 = cons *)
    simpl. rewriteIHl1'. reflexivity. Qed.
Note that, to make the lemma as general as possible, we quantify over all natlists, not just those that result from an application of rev. This should seem natural, because the truth of the goal clearly doesn't depend on the list having been reversed. Moreover, it is easier to prove the more general property.
Now we can complete the original proof.

Theorem rev_length : l : natlist,
  length (rev l) = length l.
Proof.
  intros l. induction l as [| n l' IHl'].
  - (* l = nil *)
    reflexivity.
  - (* l = cons *)
    simpl. rewriteapp_length, plus_comm.
    simpl. rewriteIHl'. reflexivity. Qed.
For comparison, here are informal proofs of these two theorems:
Theorem: For all lists l1 and l2, length (l1 ++ l2) = length l1 + length l2.
Proof: By induction on l1.
  • First, suppose l1 = []. We must show
            length ([] ++ l2) = length [] + length l2,
    which follows directly from the definitions of length and ++.
  • Next, suppose l1 = n::l1', with
            length (l1' ++ l2) = length l1' + length l2.
    We must show
            length ((n::l1') ++ l2) = length (n::l1') + length l2).
    This follows directly from the definitions of length and ++ together with the induction hypothesis. Qed
Theorem: For all lists l, length (rev l) = length l.
Proof: By induction on l.
  • First, suppose l = []. We must show
              length (rev []) = length [],
    which follows directly from the definitions of length and rev.
  • Next, suppose l = n::l', with
              length (rev l') = length l'.
    We must show
              length (rev (n :: l')) = length (n :: l').
    By the definition of rev, this follows from
              length ((rev l') ++ [n]) = S (length l')
    which, by the previous lemma, is the same as
              length (rev l') + length [n] = S (length l').
    This follows directly from the induction hypothesis and the definition of length. Qed
The style of these proofs is rather longwinded and pedantic. After the first few, we might find it easier to follow proofs that give fewer details (which can easily work out in our own minds or on scratch paper if necessary) and just highlight the non-obvious steps. In this more compressed style, the above proof might look like this:
Theorem: For all lists l, length (rev l) = length l.
Proof: First, observe that length (l ++ [n]) = S (length l) for any l (this follows by a straightforward induction on l). The main property again follows by induction on l, using the observation together with the induction hypothesis in the case where l = n'::l'.
Which style is preferable in a given situation depends on the sophistication of the expected audience and how similar the proof at hand is to ones that the audience will already be familiar with. The more pedantic style is a good default for our present purposes---please stick with it for now on exercises.

Search

We've seen that proofs can make use of other theorems we've already proved, e.g., using rewrite. But in order to refer to a theorem, we need to know its name! Indeed, it is often hard even to remember what theorems have been proven, much less what they are called.
Coq's Search command is quite helpful with this. Typing Search foo will cause Coq to display a list of all theorems involving foo. For example, try uncommenting the following line to see a list of theorems that we have proved about rev:

(*  Search rev. *)
Keep Search in mind as you do the following exercises and throughout the rest of the book; it can save you a lot of time!
If you are using ProofGeneral, you can run Search with C-c C-a C-a. Pasting its response into your buffer can be accomplished with C-c C-;.

List Exercises, Part 1

Exercise: 3 stars, standard (list_exercises)

More practice with lists:

Theorem app_nil_r : l : natlist,
  l ++ [] = l.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem rev_app_distr: l1 l2 : natlist,
  rev (l1 ++ l2) = rev l2 ++ rev l1.
Proof.
  (* FILL IN HERE *) Admitted.

Theorem rev_involutive : l : natlist,
  rev (rev l) = l.
Proof.
  (* FILL IN HERE *) Admitted.
There is a short solution to the next one. If you find yourself getting tangled up, step back and try to look for a simpler way.

Theorem app_assoc4 : l1 l2 l3 l4 : natlist,
  l1 ++ (l2 ++ (l3 ++ l4)) = ((l1 ++ l2) ++ l3) ++ l4.
Proof.
  (* FILL IN HERE *) Admitted.
An exercise about your implementation of nonzeros:

Lemma nonzeros_app : l1 l2 : natlist,
  nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 2 stars, standard (eqblist)

Fill in the definition of eqblist, which compares lists of numbers for equality. Prove that eqblist l l yields true for every list l.

Fixpoint eqblist (l1 l2 : natlist) : bool
  (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_eqblist1 :
  (eqblist nil nil = true).
 (* FILL IN HERE *) Admitted.

Example test_eqblist2 :
  eqblist [1;2;3] [1;2;3] = true.
(* FILL IN HERE *) Admitted.

Example test_eqblist3 :
  eqblist [1;2;3] [1;2;4] = false.
 (* FILL IN HERE *) Admitted.

Theorem eqblist_refl : l:natlist,
  true = eqblist l l.
Proof.
  (* FILL IN HERE *) Admitted.

Sums

Now that we can talk about lists of numbers, we can talk about the mathematical concept of sums.
As a first draft, let's consider the following mathematical utterance:
     Σ_i=1^n i
The capital greek letter sigma is just short for "sum". We say we'll be summing over a variable i, starting from 1 and going up to n. For each such i, we'll add i. That is,
     Σ_i=0^n i = i[i=0] + i[i=1] + i[i=2] + ... + i[i=n-1] + i[i=n]
               =     0  +     1  +     2  + ... +     n-1  +     n
But we could phrase the sum differently: we're computing the sum of the list 0,1,2,...,n-1,n. Here's a function that can sum a list:

Fixpoint sum_natlist (l : natlist) : nat :=
  match l with
  | nil ⇒ 0
  | cons n l'n + sum_natlist l'
  end.

Fixpoint ones (n : nat) : natlist :=
  match n with
  | Onil
  | S n'cons 1 (ones n')
  end.

Lemma sum_natlist_ones : n : nat,
    sum_natlist (ones n) = n.
Proof.
  (* WORKED IN CLASS *)
  induction n as [|n' IHn'].
  - reflexivity.
  - simpl. rewriteIHn'. reflexivity.
Qed.

Fixpoint odds (n : nat) : natlist :=
  match n with
  | O[1]
  | S n' ⇒ 2 × (S n') + 1 :: odds n'
  end.

Lemma sum_natlist_odds : n,
    sum_natlist (odds n) = (n+1)*(n+1).
Proof.
  (* WORKED IN CLASS *)
  intros n. induction n as [|n' IHn'].
  - reflexivity.
  - simpl. rewriteIHn'. simpl.
    rewrite <- plus_n_Sm. rewrite <- plus_n_Sm. rewrite <- plus_n_Sm.
    rewrite <- plus_n_O. rewrite <- plus_n_O.
    simpl.
    rewrite <- plus_n_Sm. rewrite <- plus_n_Sm.
    rewrite <- plus_n_Sm.
    rewrite <- plus_assoc.
    assert (n' × S (S n') = n' + n' × S n').
    { rewrite <- mult_n_Sm. rewrite plus_comm. reflexivity. }
    rewriteH.
    reflexivity.
Qed.
Your choices in proofs matter a lot! You might need to do substantially more work if you pick a hard way to go.
The bad news is that there's no easy rule for predicting which way will be harder or easier. But the good news is that you'll improve a lot with mindful practice.

Lemma sum_natlist_odds' : n,
    sum_natlist (odds n) = (n+1)*(n+1).
Proof.
  intros n. induction n as [|n' IHn'].
  - reflexivity.
  - rewrite plus_comm.
    assert ((1 + S n') × (1 + S n') = 1 + S n' + S n' + (S n') × (S n')).
    { rewrite <- mult_n_Sm. rewrite plus_comm.
      simpl. rewrite <- plus_n_Sm. rewrite <- plus_n_Sm.
      rewrite <- plus_n_Sm. rewrite <- plus_n_Sm.
      rewrite <- plus_n_Sm. rewriteplus_assoc.
      rewriteplus_assoc. rewriteplus_assoc.
      simpl. reflexivity. }
    rewrite H.
    assert (S n' × S n' = (n' + 1) × (n' + 1)).
    { rewrite plus_comm. reflexivity. }
    rewrite H0.
    rewrite <- IHn'.
    simpl. rewrite <- plus_n_Sm.
    rewrite <- plus_n_O.
    rewrite <- plus_n_O.
    simpl. reflexivity.
Qed.

Exercise: 2 stars, standard, recommended (sum_natlist_ntoone)

Show the following property, or, as it's better known:
      Σ_i=0^n i = 0 + 1 + 2 + 3 + ... + n-1 + n = n*(n+1)/2
You'll have to do a lot of arithmetic rewriting. Why not try it on the board, first? Don't forget about assert!
Fixpoint ntoone (n:nat) : natlist :=
  match n with
  | 0 ⇒ nil
  | S n'n :: (ntoone n')
  end.

Lemma sum_natlist_ntoone : n : nat,
    2 × sum_natlist (ntoone n) = n × (n + 1).
Proof.
  (* FILL IN HERE *) Admitted.

Exercise: 3 stars, standard (sum_natlist_rev)

You may have noticed that we didn't exactly prove the sum we wanted to. First, we proved
      2 × Σ_i=1^n i = n*(n+1)
rather than
      Σ_i=1^n i = n*(n+1)/2.
We deliberately avoided division. How come? So far, every Coq function we've written has been total, that is, defined on all inputs. Things have gotten a little bit weird for predecessor and division, but not hopelessly so. But defining division is pretty tricky---what should happen if we divide by zero? It's much easier to prove the version of the theorem that uses multiplication in Coq.
There's another reason we didn't exactly prove the theorem we wanted to: the list of summands we provided goes from n down to 0, i.e., we've shown that 2 × (sum_natlist [n,n-1,...,2,1,0]) = n*(n+1), but the informal statment used the list 0,1,2,...,n-1,n.
Prove the following theorem, sum_natlist_rev, showing that it doesn't matter what order we sum in. You may need to come up with a subsidiary lemma.

Lemma sum_natlist_rev : l : natlist,
    sum_natlist l = sum_natlist (rev l).
Proof.
  (* FILL IN HERE *) Admitted.
Next, let's prove that
      Σ_i=1^n n = n×n.
Here's a first attempt:
Lemma sum_natlist_repeat_n : n : nat,
      sum_natlist (repeat n n) = n × n.
Proof.
  induction n as [|n' IHn'].
  - reflexivity.
  - (* our IH won't help _at all_! *)
Abort.
What went wrong? Since n appeared more than once in the proposition we were proving---our goal---when we went by induction, we updated n in both cases.
The solution here is generalizing the induction hypothesis: we prove a slightly more general property that will imply the one we want. Here, it's a simple generalization: separate out the number we're adding and the number of times we add it to itself.

Exercise: 2 stars, standard (sum_natlist_repeat_n)

Lemma sum_natlist_repeat_n_m : n m : nat,
    sum_natlist (repeat n m) = n × m.
Proof.
  (* FILL IN HERE *) Admitted.

Lemma sum_natlist_repeat_n : n : nat,
    sum_natlist (repeat n n) = n × n.
Proof.
  (* FILL IN HERE *) Admitted.

Options

Suppose we want to write a function that returns the nth element of some list. If we give it type nat natlist nat, then we'll have to choose some number to return when the list is too short...

Fixpoint nth_bad (l:natlist) (n:nat) : nat :=
  match l with
  | nil ⇒ 42 (* arbitrary! *)
  | a :: l'match eqb n O with
               | truea
               | falsenth_bad l' (pred n)
               end
  end.
This solution is not so good: If nth_bad returns 42, we can't tell whether that value actually appears on the input without further processing. A better alternative is to change the return type of nth_bad to include an error value as a possible outcome. We call this type natoption.

Inductive natoption : Type :=
  | Some (n : nat)
  | None.
We can then change the above definition of nth_bad to return None when the list is too short and Some a when the list has enough members and a appears at position n. We call this new function nth_error to indicate that it may result in an error.

Fixpoint nth_error (l:natlist) (n:nat) : natoption :=
  match l with
  | nilNone
  | a :: l'match eqb n O with
               | trueSome a
               | falsenth_error l' (pred n)
               end
  end.

Example test_nth_error1 : nth_error [4;5;6;7] 0 = Some 4.
Proof. reflexivity. Qed.
Example test_nth_error2 : nth_error [4;5;6;7] 3 = Some 7.
Proof. reflexivity. Qed.
Example test_nth_error3 : nth_error [4;5;6;7] 9 = None.
Proof. reflexivity. Qed.
(In the HTML version, the boilerplate proofs of these examples are elided. Click on a box if you want to see one.)
This example is also an opportunity to introduce one more small feature of Coq's programming language: conditional expressions...

Fixpoint nth_error' (l:natlist) (n:nat) : natoption :=
  match l with
  | nilNone
  | a :: l'if eqb n O then Some a
               else nth_error' l' (pred n)
  end.
Coq's conditionals are exactly like those found in any other language, with one small generalization. Since the boolean type is not built in, Coq actually supports conditional expressions over any inductively defined type with exactly two constructors. The guard is considered true if it evaluates to the first constructor in the Inductive definition and false if it evaluates to the second.
The function below pulls the nat out of a natoption, returning a supplied default in the None case.

Definition option_elim (d : nat) (o : natoption) : nat :=
  match o with
  | Some n'n'
  | Noned
  end.

Exercise: 2 stars, standard (hd_error)

Using the same idea, fix the hd function from earlier so we don't have to pass a default element for the nil case.

Definition hd_error (l : natlist) : natoption
  (* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_hd_error1 : hd_error [] = None.
 (* FILL IN HERE *) Admitted.

Example test_hd_error2 : hd_error [1] = Some 1.
 (* FILL IN HERE *) Admitted.

Example test_hd_error3 : hd_error [5;6] = Some 5.
 (* FILL IN HERE *) Admitted.

Exercise: 1 star, standard, optional (option_elim_hd)

This exercise relates your new hd_error to the old hd.

Theorem option_elim_hd : (l:natlist) (default:nat),
  hd default l = option_elim default (hd_error l).
Proof.
  (* FILL IN HERE *) Admitted.

End NatList.

(* Mon Apr 6 09:16:54 PDT 2020 *)