Library Lists
Pairs of Numbers
This declaration can be read: "There is just one way to
construct a pair of numbers: by applying the constructor pair to
two arguments of type nat."
We can construct an element of natprod like this:
Definition fst (p : natprod) : nat :=
match p with
| pair x y ⇒ x
end.
Definition snd (p : natprod) : nat :=
match p with
| pair x y ⇒ y
end.
Eval compute in (fst (pair 3 5)).
The new notation can be used both in expressions and in
pattern matches (indeed, we've seen it already in the previous
chapter -- this notation is provided as part of the standard
library):
Eval compute in (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.
Theorem surjective_pairing' : ∀ (n m : nat),
(n,m) = (fst (n,m), snd (n,m)).
Proof.
reflexivity. Qed.
Note that reflexivity is not enough if we state the lemma in a
more natural way:
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.
☐
Lists of Numbers
For example, here is a three-element list:
As with pairs, it is more convenient to write lists in familiar programming notation. The following two 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 fully understand 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]).
(By the way, it's worth noting in passing that 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
| O ⇒ nil
| S count' ⇒ n :: (repeat n count')
end.
Fixpoint app (l1 l2 : natlist) : natlist :=
match l1 with
| nil ⇒ l2
| h :: t ⇒ h :: (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.
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.
Head (with default) and Tail
Definition hd (default:nat) (l:natlist) : nat :=
match l with
| nil ⇒ default
| h :: t ⇒ h
end.
Definition tl (l:natlist) : natlist :=
match l with
| nil ⇒ nil
| h :: t ⇒ t
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.
match l with
| nil ⇒ default
| h :: t ⇒ h
end.
Definition tl (l:natlist) : natlist :=
match l with
| nil ⇒ nil
| h :: t ⇒ t
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.
Exercise: 2 stars (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 :=
admit.
Example test_nonzeros: nonzeros [0;1;0;2;3;0;0] = [1;2;3].
Admitted.
Fixpoint oddmembers (l:natlist) : natlist :=
admit.
Example test_oddmembers: oddmembers [0;1;0;2;3;0;0] = [1;3].
Admitted.
Fixpoint countoddmembers (l:natlist) : nat :=
admit.
Example test_countoddmembers1: countoddmembers [1;0;3;1;4;5] = 4.
Admitted.
Example test_countoddmembers2: countoddmembers [0;2;4] = 0.
Admitted.
Example test_countoddmembers3: countoddmembers nil = 0.
Admitted.
☐
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.)
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.Fixpoint alternate (l1 l2 : natlist) : natlist :=
admit.
Example test_alternate1: alternate [1;2;3] [4;5;6] = [1;4;2;5;3;6].
Admitted.
Example test_alternate2: alternate [1] [4;5;6] = [1;4;5;6].
Admitted.
Example test_alternate3: alternate [1;2;3] [4] = [1;4;2;3].
Admitted.
Example test_alternate4: alternate [] [20;30] = [20;30].
Admitted.
☐
Bags via Lists
Exercise: 3 stars (bag_functions)
Complete the following definitions for the functions count, sum, add, and member for bags.
All these proofs can be done just by reflexivity.
Example test_count1: count 1 [1;2;3;1;4;1] = 3.
Admitted.
Example test_count2: count 6 [1;2;3;1;4;1] = 0.
Admitted.
Multiset sum is similar to set union: sum a b contains
all the elements of a and of b. (Mathematicians usually
define union on multisets a little bit differently, which
is why we don't use that name for this operation.)
For sum we're giving you a header that does not give explicit
names to the arguments. Moreover, it uses the keyword
Definition instead of Fixpoint, so even if you had names for
the arguments, you wouldn't be able to process them recursively.
The point of stating the question this way is to encourage you to
think about whether sum can be implemented in another way --
perhaps by using functions that have already been defined.
Definition sum : bag → bag → bag :=
admit.
Example test_sum1: count 1 (sum [1;2;3] [1;4;1]) = 3.
Admitted.
Definition add (v:nat) (s:bag) : bag :=
admit.
Example test_add1: count 1 (add 1 [1;4;1]) = 3.
Admitted.
Example test_add2: count 5 (add 1 [1;4;1]) = 0.
Admitted.
Definition member (v:nat) (s:bag) : bool :=
admit.
Example test_member1: member 1 [1;4;1] = true.
Admitted.
Example test_member2: member 2 [1;4;1] = false.
Admitted.
☐
Exercise: 3 stars, optional (bag_more_functions)
Here are some more bag functions for you to practice with.Fixpoint remove_one (v:nat) (s:bag) : bag :=
admit.
Example test_remove_one1: count 5 (remove_one 5 [2;1;5;4;1]) = 0.
Admitted.
Example test_remove_one2: count 5 (remove_one 5 [2;1;4;1]) = 0.
Admitted.
Example test_remove_one3: count 4 (remove_one 5 [2;1;4;5;1;4]) = 2.
Admitted.
Example test_remove_one4: count 5 (remove_one 5 [2;1;5;4;5;1;4]) = 1.
Admitted.
Fixpoint remove_all (v:nat) (s:bag) : bag :=
admit.
Example test_remove_all1: count 5 (remove_all 5 [2;1;5;4;1]) = 0.
Admitted.
Example test_remove_all2: count 5 (remove_all 5 [2;1;4;1]) = 0.
Admitted.
Example test_remove_all3: count 4 (remove_all 5 [2;1;4;5;1;4]) = 2.
Admitted.
Example test_remove_all4: count 5 (remove_all 5 [2;1;5;4;5;1;4;5;1;4]) = 0.
Admitted.
Fixpoint subset (s1:bag) (s2:bag) : bool :=
admit.
Example test_subset1: subset [1;2] [2;1;4;1] = true.
Admitted.
Example test_subset2: subset [1;2;2] [2;1;4;1] = false.
Admitted.
☐
Exercise: 3 stars (bag_theorem)
Write down an interesting theorem about bags involving the functions count and add, and prove it. Note that, since this problem is somewhat open-ended, it's possible that you may come up with a theorem which is true, but whose proof requires techniques you haven't learned yet. Feel free to ask for help if you get stuck!
☐
Reasoning About Lists
... because the [] is substituted into the match position
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'].
Case "l = nil".
reflexivity.
Case "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
Induction on Lists
- 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'.
Theorem app_assoc : ∀ l1 l2 l3 : natlist,
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof.
intros l1 l2 l3. induction l1 as [| n l1'].
Case "l1 = nil".
reflexivity.
Case "l1 = cons n l1'".
simpl. rewrite → IHl1'. reflexivity. Qed.
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.
Theorem: For all lists l1, l2, and l3,
(l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3).
Proof: By induction on l1.
Here is a similar example to be worked together in class:
Informal version
- 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).
Another example
Theorem app_length : ∀ l1 l2 : natlist,
length (l1 ++ l2) = (length l1) + (length l2).
Proof.
intros l1 l2. induction l1 as [| n l1'].
Case "l1 = nil".
reflexivity.
Case "l1 = cons".
simpl. rewrite → IHl1'. reflexivity. Qed.
Reversing a list
For a slightly more involved example of an inductive proof over lists, suppose we define a "cons on the right" function snoc like this...Fixpoint snoc (l:natlist) (v:nat) : natlist :=
match l with
| nil ⇒ [v]
| h :: t ⇒ h :: (snoc t v)
end.
... and use it to define a list-reversing function rev
like this:
Fixpoint rev (l:natlist) : natlist :=
match l with
| nil ⇒ nil
| h :: t ⇒ snoc (rev 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.
Proofs about reverse
Now let's prove some more list theorems using our newly defined snoc and rev. For something a little more challenging than the inductive proofs we've seen so far, let's prove that reversing a list does not change its length. Our first attempt at this proof 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'].
Case "l = []".
reflexivity.
Case "l = n :: l'".
simpl.
rewrite <- IHl'.
Abort.
So let's take the equation about snoc that would have
enabled us to make progress and prove it as a separate lemma.
Theorem length_snoc : ∀ n : nat, ∀ l : natlist,
length (snoc l n) = S (length l).
Proof.
intros n l. induction l as [| n' l'].
Case "l = nil".
reflexivity.
Case "l = cons n' l'".
simpl. rewrite → IHl'. reflexivity. Qed.
Note that we make the lemma as general as possible: in particular,
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 much 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'].
Case "l = nil".
reflexivity.
Case "l = cons".
simpl. rewrite → length_snoc.
rewrite → IHl'. reflexivity. Qed.
For comparison, here are informal proofs of these two theorems:
Theorem: For all numbers n and lists l,
length (snoc l n) = S (length l).
Proof: By induction on l.
Theorem: For all lists l, length (rev l) = length l.
Proof: By induction on l.
Obviously, 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 (since we can easily work
them 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 more like this:
Theorem:
For all lists l, length (rev l) = length l.
Proof: First, observe that
length (snoc l n) = S (length l)
for any l. This follows by a straightforward induction on l.
The main property now follows by another straightforward
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 on 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
present purposes.
- First, suppose l = []. We must show
length (snoc ☐ n) = S (length ☐),
which follows directly from the definitions of
length and snoc.
- Next, suppose l = n'::l', with length (snoc l' n) = S (length l'). We must show length (snoc (n' :: l') n) = S (length (n' :: l')). By the definitions of length and snoc, this follows from S (length (snoc l' n)) = S (S (length 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 (snoc (rev l') n) = S (length l') which, by the previous lemma, is the same as S (length (rev l')) = S (length l'). This is immediate from the induction hypothesis. ☐
SearchAbout
Keep SearchAbout in mind as you do the following exercises and
throughout the rest of the course; it can save you a lot of time!
Also, if you are using ProofGeneral, you can run SearchAbout
with C-c C-a C-a. Pasting its response into your buffer can be
accomplished with C-c C-;.
Theorem app_nil_end : ∀ l : natlist,
l ++ [] = l.
Proof.
Admitted.
Theorem rev_involutive : ∀ l : natlist,
rev (rev l) = l.
Proof.
Admitted.
There is a short solution to the next exercise. 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.
Admitted.
Theorem snoc_append : ∀ (l:natlist) (n:nat),
snoc l n = l ++ [n].
Proof.
Admitted.
Theorem distr_rev : ∀ l1 l2 : natlist,
rev (l1 ++ l2) = (rev l2) ++ (rev l1).
Proof.
Admitted.
An exercise about your implementation of nonzeros:
Lemma nonzeros_app : ∀ l1 l2 : natlist,
nonzeros (l1 ++ l2) = (nonzeros l1) ++ (nonzeros l2).
Proof.
Admitted.
☐
Exercise: 2 stars (beq_natlist)
Fill in the definition of beq_natlist, which compares lists of numbers for equality. Prove that beq_natlist l l yields true for every list l.Fixpoint beq_natlist (l1 l2 : natlist) : bool :=
admit.
Example test_beq_natlist1 : (beq_natlist nil nil = true).
Admitted.
Example test_beq_natlist2 : beq_natlist [1;2;3] [1;2;3] = true.
Admitted.
Example test_beq_natlist3 : beq_natlist [1;2;3] [1;2;4] = false.
Admitted.
Theorem beq_natlist_refl : ∀ l:natlist,
true = beq_natlist l l.
Proof.
Admitted.
☐
☐
Exercise: 3 stars, advanced (bag_proofs)
Here are a couple of little theorems to prove about your definitions about bags earlier in the file.
The following lemma about ble_nat might help you in the next proof.
Theorem ble_n_Sn : ∀ n,
ble_nat n (S n) = true.
Proof.
intros n. induction n as [| n'].
Case "0".
simpl. reflexivity.
Case "S n'".
simpl. rewrite IHn'. reflexivity. Qed.
Theorem remove_decreases_count: ∀ (s : bag),
ble_nat (count 0 (remove_one 0 s)) (count 0 s) = true.
Proof.
Admitted.
☐
Exercise: 3 stars, optional (bag_count_sum)
Write down an interesting theorem about bags involving the functions count and sum, and prove it.
☐
forall (l1 l2 : natlist), rev l1 = rev l2 -> l1 = l2.
There is a hard way and an easy way to solve this exercise.
Exercise: 4 stars, advanced (rev_injective)
Prove that the rev function is injective, that is,
☐
Options
Fixpoint index_bad (n:nat) (l:natlist) : nat :=
match l with
| nil ⇒ 42
| a :: l' ⇒ match beq_nat n O with
| true ⇒ a
| false ⇒ index_bad (pred n) l'
end
end.
On the other hand, if we give it type nat → natlist → natoption, then we can return None when the list is too short and Some a when the list has enough members and a appears at position n.
Inductive natoption : Type :=
| Some : nat → natoption
| None : natoption.
Fixpoint index (n:nat) (l:natlist) : natoption :=
match l with
| nil ⇒ None
| a :: l' ⇒ match beq_nat n O with
| true ⇒ Some a
| false ⇒ index (pred n) l'
end
end.
Example test_index1 : index 0 [4;5;6;7] = Some 4.
Proof. reflexivity. Qed.
Example test_index2 : index 3 [4;5;6;7] = Some 7.
Proof. reflexivity. Qed.
Example test_index3 : index 10 [4;5;6;7] = None.
Proof. reflexivity. Qed.
This example is also an opportunity to introduce one more
small feature of Coq's programming language: conditional
expressions...
Fixpoint index' (n:nat) (l:natlist) : natoption :=
match l with
| nil ⇒ None
| a :: l' ⇒ if beq_nat n O then Some a else index' (pred n) l'
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 allows 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'
| None ⇒ d
end.
Exercise: 2 stars (hd_opt)
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_opt (l : natlist) : natoption :=
admit.
Example test_hd_opt1 : hd_opt [] = None.
Admitted.
Example test_hd_opt2 : hd_opt [1] = Some 1.
Admitted.
Example test_hd_opt3 : hd_opt [5;6] = Some 5.
Admitted.
Theorem option_elim_hd : ∀ (l:natlist) (default:nat),
hd default l = option_elim default (hd_opt l).
Proof.
Admitted.
☐
Dictionaries
Module Dictionary.
Inductive dictionary : Type :=
| empty : dictionary
| record : nat → nat → dictionary → dictionary.
This declaration can be read: "There are two ways to construct a
dictionary: either using the constructor empty to represent an
empty dictionary, or by applying the constructor record to
a key, a value, and an existing dictionary to construct a
dictionary with an additional key to value mapping."
Here is a function find that searches a dictionary for a
given key. It evaluates evaluates to None if the key was not
found and Some val if the key was mapped to val in the
dictionary. If the same key is mapped to multiple values, find
will return the first one it finds.
Fixpoint find (key : nat) (d : dictionary) : natoption :=
match d with
| empty ⇒ None
| record k v d' ⇒ if (beq_nat key k)
then (Some v)
else (find key d')
end.
Theorem dictionary_invariant1' : ∀ (d : dictionary) (k v: nat),
(find k (insert k v d)) = Some v.
Proof.
Admitted.
Theorem dictionary_invariant2' : ∀ (d : dictionary) (m n o: nat),
beq_nat m n = false → find m d = find m (insert n o d).
Proof.
Admitted.
☐