






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Each constructor of an inductive type can take any number of parameters— none (as with true and O), one (as with S), or more than one: Inductive natprod : Set := pair : nat → nat → natprod. 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.” Here are some simple function definitions illustrating pattern matching on two-argument constructors: 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. 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 instruct Coq to allow this with a Notation declaration. Notation "( x , y )" := (pair x y). The new notation is supported both in expressions like fst (3,4) and in pattern matches:
26 3 Pairs and Lists Definition swap_pair (p : natprod) : natprod := match p with | (x,y) => (y,x) end. 3.1.1 EXERCISE [!!]: Prove snd_fst_is_swap in Lists.v. 3.1.2 EXERCISE [!!, OPTIONAL]: Prove fst_swap_is_snd in Lists.v.
Generalizing the definition of pairs a little, we can describe the type of lists of numbers like this: “A list can be either the empty list or else a pair of a number and another list.” Inductive natlist : Set := | nil : natlist | cons : nat → natlist → natlist. For example, here is a three-element list: Definition l_123 := cons 1 (cons 2 (cons 3 nil)). As with pairs, it is more convenient to write lists in familiar mathematical 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 "[ x , .. , y ]" := (cons x .. (cons y nil) ..). It is not necessary to fully understand the second line of the first declaration (or any of the right-hand side of the second), 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 l_123′^ := 1 :: (2 :: (3 :: nil)). Definition l_123′′^ := 1 :: 2 :: 3 :: nil. Definition l_123′′′^ := [1,2,3]. The at level 60 part tells Coq how to parenthesize expressions that involve both :: and some other infix operator. For example, if we define + as infix notation for the plus function at level 50,
28 3 Pairs and Lists | nil => 0 (* arbitrarily (^) *) | h :: t => h end. Definition tl (l:natlist) : natlist := match l with | nil => nil | h :: t => t end. 3.2.1 EXERCISE [!!, OPTIONAL]: A bag (or multiset) is a set where each element can appear any finite number of times. One reasonable implementation of bags is to represent a bag of numbers as a list. Definition bag := natlist. Stubs for several bag-manipulating functions (count, union, etc.) can be found in Lists.v. Complete them.
Just as with numbers, simple facts about list-processing functions can some- times be proved entirely by simplification. For example, simplification is enough for this theorem... Theorem nil_app : forall l:natlist, [] ++ l = l. ... because the [] is substituted into the match position in the definition of app, allowing the match itself to be simplified. Also like numbers, it is some- times helpful to perform case analysis on the possible shapes (empty or non- empty) of an unknown list. Theorem tl_length_pred : forall 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.! 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).
3.3 Reasoning About Lists 29 Usually, though, interesting theorems about lists require induction for their proofs. Proofs by induction over non-numeric data types are perhaps a little less familiar than natural number induction, but the basic idea is equally simple. Each Inductive declaration defines a set of data values that can be built up from the declared constructors: a number can be either O or S applied to a number; a boolean can be either true or false; 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′, asssuming that P is true for l′. Since larger lists can only be built up from smaller ones, stopping eventually with nil, these two things together establish the truth of P for all lists l. For example, the associativity of the ++ operation can be shown by induc- tion on l1: Theorem ass_app : forall 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.! 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. A human-readable (informal) proof needs to include more explicit—in particular, it helps the reader a lot to be reminded exactly what the induction hypothesis is in the second case. 3.3.1 THEOREM: For all l1, l2, and l3, l1 ++ (l2 ++ l3) = (l1 ++ l2) ++ l3.
3.3 Reasoning About Lists 31 We can now prove that reversing a list doesn’t change it’s length as follows. First we prove a lemma relating length and snoc. 3.3.4 LEMMA: For all numbers n and lists l, length (snoc l n) = S (length l). Proof: By induction on l.
32 3 Pairs and Lists
34 3 Pairs and Lists Example test_index1 : index 0 [4,5,6,7] = Some 4. Example test_index2 : index 3 [4,5,6,7] = Some 7. Example test_index3 : index 10 [4,5,6,7] = None. 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) {struct l} : 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 in every other language, with one small generalization. Since the boolean type is not built in, Coq actually al- lows 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. 3.4.1 EXERCISE [!!]: Complete the definition of hd_opt in Lists.v. 3.4.2 EXERCISE [!!]: Prove option_elim_hd in Lists.v. 3.4.3 EXERCISE [!!]: Define a function beq_natlist for comparing lists of numbers for equality. Prove that beq_natlist l l yields true for every list l. (Stubs are provided in Lists.v.)
This section still needs to be written. Please have a look at the corresponding material in Lists.v, where you’ll find plenty of descriptive text... 3.5.1 EXERCISE [!!]: Prove silly_ex in Lists.v without using the simpl tactic. 3.5.2 EXERCISE [!!!]: Prove rev_exercise1 and beq_nat_sym in Lists.v. 3.5.3 EXERCISE [!!!]: Provide an informal proof of beq_nat_sym in Lists.v. 3.5.4 EXERCISE [!]: Briefly explain the difference between the tactics apply and rewrite. Are there situations where either one can be applied?
3.6 Varying the Induction Hypothesis 35
One subtlety in these inductive proofs is worth noticing here. For example, look back at the proof of the app_ass theorem. The induction hypothesis (in the second subgoal generated by the induction tactic) is l1′^ ++ l2 ++ l3 = (l1′^ ++ l2) ++ l3. (Note that, because we’ve defined ++ to be right associative, the expression on the left of the = is the same as writing l1′^ ++ (l2 ++ l3).) That is it makes a statement about l1′^ together with the particular lists l2 and l3. The lists l2 and l3, which were introduced into the context by the intros at the top of the proof, are “held constant” in the induction hypothesis. If we set up the proof slightly differently by introducing just n into the context at the top, then we get an induction hypothesis that makes a stronger claim: forall l2 l3, l1′^ ++ l2 ++ l3 = (l1′^ ++ l2) ++ l3. (Use Coq to see the difference for yourself.) In the present case, the differ- ence between the two proofs is minor, since the definition of the ++ function just examines its first argument and doesn’t do anything interesting with its second argument. But we’ll soon come to situations where setting up the in- duction hypothesis one way or the other can make the difference between a proof working and failing. 3.6.1 EXERCISE [!!]: Finish the proof of ass_app′^ at the end of Lists.v.
3.7.1 EXERCISE [!]: Briefly explain the difference between the tactics destruct and induction. [Make sure that the answer is given in the text! Also, perhaps