# PolyPolymorphism and Higher-Order Functions

# Polymorphism

*polymorphism*(abstracting functions over the types of the data they manipulate) and

*higher-order functions*(treating functions as data). We begin with polymorphism.

## Polymorphic Lists

*could*just define a new inductive datatype for each of these, for example...

... but this would quickly become tedious, partly because we
have to make up different constructor names for each datatype, but
mostly because we would also need to define new versions of all
our list manipulating functions (length, rev, etc.) and all
their properties (rev_length, app_assoc, etc.) for each
new datatype definition.
To avoid all this repetition, Coq supports

*polymorphic*inductive type definitions. For example, here is a*polymorphic list*datatype.
This is exactly like the definition of natlist from the
previous chapter, except that the nat argument to the cons
constructor has been replaced by an arbitrary type X, a binding
for X has been added to the function header on the first line,
and the occurrences of natlist in the types of the constructors
have been replaced by list X.
What sort of thing is list itself? A good way to think about it
is that the definition of list is a

*function*from Types to Inductive definitions; or, to put it more concisely, list is a function from Types to Types. For any particular type X, the type list X is the Inductively defined set of lists whose elements are of type X.
The X in the definition of list automatically becomes a
parameter to the constructors nil and cons -- that is, nil
and cons are now polymorphic constructors; when we use them, we
must now provide a first argument that is the type of the list
they are building. For example, nil nat constructs the empty
list of type nat.

Similarly, cons nat adds an element of type nat to a list of
type list nat. Here is an example of forming a list containing
just the natural number 3.

What might the type of nil be? We can read off the type
list X from the definition, but this omits the binding for X
which is the parameter to list. Type → list X does not
explain the meaning of X. (X : Type) → list X comes
closer. Coq's notation for this situation is ∀ X : Type,
list X.

Similarly, the type of cons from the definition looks like
X → list X → list X, but using this convention to explain the
meaning of X results in the type ∀ X, X → list X → list
X.

(A side note on notations: In .v files, the "forall"
quantifier is spelled out in letters. In the corresponding HTML
files (and in the way some IDEs show .v files, depending on the
settings of their display controls), ∀ is usually typeset
as the standard mathematical "upside down A," though you'll still
see the spelled-out "forall" in a few places. This is just a
quirk of typesetting -- there is no difference in meaning.)
Having to supply a type argument for every single use of a
list constructor would be rather burdensome; we will soon see ways
of reducing this annotation burden.

We can now go back and make polymorphic versions of all the
list-processing functions that we wrote before. Here is repeat,
for example:

Fixpoint repeat (X : Type) (x : X) (count : nat) : list X :=

match count with

| 0 ⇒ nil X

| S count' ⇒ cons X x (repeat X x count')

end.

match count with

| 0 ⇒ nil X

| S count' ⇒ cons X x (repeat X x count')

end.

As with nil and cons, we can use repeat by applying it
first to a type and then to an element of this type (and a number):

To use repeat to build other kinds of lists, we simply
instantiate it with an appropriate type parameter:

### Type Annotation Inference

Fixpoint repeat' X x count : list X :=

match count with

| 0 ⇒ nil X

| S count' ⇒ cons X x (repeat' X x count')

end.

match count with

| 0 ⇒ nil X

| S count' ⇒ cons X x (repeat' X x count')

end.

Indeed it will. Let's see what type Coq has assigned to repeat'...

It has exactly the same type as repeat. Coq was able to
use
This powerful facility means we don't always have to write
explicit type annotations everywhere, although explicit type
annotations can still be quite useful as documentation and sanity
checks, so we will continue to use them much of the time.

*type inference*to deduce what the types of X, x, and count must be, based on how they are used. For example, since X is used as an argument to cons, it must be a Type, since cons expects a Type as its first argument; matching count with 0 and S means it must be a nat; and so on.### Type Argument Synthesis

*unify*all locally available information -- the type of the function being applied, the types of the other arguments, and the type expected by the context in which the application appears -- to determine what concrete type should replace the _.

repeat' X x count : list X := we can also replace the types with holes

repeat' (X : _) (x : _) (count : _) : list X := to tell Coq to attempt to infer the missing information.

Fixpoint repeat'' X x count : list X :=

match count with

| 0 ⇒ nil _

| S count' ⇒ cons _ x (repeat'' _ x count')

end.

match count with

| 0 ⇒ nil _

| S count' ⇒ cons _ x (repeat'' _ x count')

end.

In this instance, we don't save much by writing _ instead of
X. But in many cases the difference in both keystrokes and
readability is nontrivial. For example, suppose we want to write
down a list containing the numbers 1, 2, and 3. Instead of
this...

...we can use holes to write this:

### Implicit Arguments

*always*to infer the type argument(s) of a given function.

Now we don't have to supply any type arguments at all in the example:

Alternatively, we can declare an argument to be implicit
when defining the function itself, by surrounding it in curly
braces instead of parens. For example:

Fixpoint repeat''' {X : Type} (x : X) (count : nat) : list X :=

match count with

| 0 ⇒ nil

| S count' ⇒ cons x (repeat''' x count')

end.

match count with

| 0 ⇒ nil

| S count' ⇒ cons x (repeat''' x count')

end.

(Note that we didn't even have to provide a type argument to the
recursive call to repeat'''. Indeed, it would be invalid to
provide one, because Coq is not expecting it.)
We will use the latter style whenever possible, but we will
continue to use explicit Argument declarations for Inductive
constructors. The reason for this is that marking the parameter
of an inductive type as implicit causes it to become implicit for
the type itself, not just for its constructors. For instance,
consider the following alternative definition of the list
type:

Because X is declared as implicit for the
Let's finish by re-implementing a few other standard list
functions on our new polymorphic lists...

*entire*inductive definition including list' itself, we now have to write just list' whether we are talking about lists of numbers or booleans or anything else, rather than list' nat or list' bool or whatever; this is a step too far.
Fixpoint app {X : Type} (l

match l

| nil ⇒ l

| cons h t ⇒ cons h (app t l

end.

Fixpoint rev {X:Type} (l:list X) : list X :=

match l with

| nil ⇒ nil

| cons h t ⇒ app (rev t) (cons h nil)

end.

Fixpoint length {X : Type} (l : list X) : nat :=

match l with

| nil ⇒ 0

| cons _ l' ⇒ S (length l')

end.

Example test_rev1 :

rev (cons 1 (cons 2 nil)) = (cons 2 (cons 1 nil)).

Example test_rev2:

rev (cons true nil) = cons true nil.

Example test_length1: length (cons 1 (cons 2 (cons 3 nil))) = 3.

_{1}l_{2}: list X) : list X :=match l

_{1}with| nil ⇒ l

_{2}| cons h t ⇒ cons h (app t l

_{2})end.

Fixpoint rev {X:Type} (l:list X) : list X :=

match l with

| nil ⇒ nil

| cons h t ⇒ app (rev t) (cons h nil)

end.

Fixpoint length {X : Type} (l : list X) : nat :=

match l with

| nil ⇒ 0

| cons _ l' ⇒ S (length l')

end.

Example test_rev1 :

rev (cons 1 (cons 2 nil)) = (cons 2 (cons 1 nil)).

Proof. reflexivity. Qed.

Example test_rev2:

rev (cons true nil) = cons true nil.

Proof. reflexivity. Qed.

Example test_length1: length (cons 1 (cons 2 (cons 3 nil))) = 3.

Proof. reflexivity. Qed.

### Supplying Type Arguments Explicitly

(The Fail qualifier that appears before Definition can be
used with
Here, Coq gives us an error because it doesn't know what type
argument to supply to nil. We can help it by providing an
explicit type declaration (so that Coq has more information
available when it gets to the "application" of nil):

*any*command, and is used to ensure that that command indeed fails when executed. If the command does fail, Coq prints the corresponding error message, but continues processing the rest of the file.)
Alternatively, we can force the implicit arguments to be explicit by
prefixing the function name with @.

Using argument synthesis and implicit arguments, we can
define convenient notation for lists, as before. Since we have
made the constructor type arguments implicit, Coq will know to
automatically infer these when we use the notations.

Notation "x :: y" := (cons x y)

(at level 60, right associativity).

Notation "[ ]" := nil.

Notation "[ x ; .. ; y ]" := (cons x .. (cons y []) ..).

Notation "x ++ y" := (app x y)

(at level 60, right associativity).

(at level 60, right associativity).

Notation "[ ]" := nil.

Notation "[ x ; .. ; y ]" := (cons x .. (cons y []) ..).

Notation "x ++ y" := (app x y)

(at level 60, right associativity).

Now lists can be written just the way we'd hope:

### Exercises

#### Exercise: 2 stars, standard (poly_exercises)

Here are a few simple exercises, just like ones in the Lists chapter, for practice with polymorphism. Complete the proofs below.
Theorem app_nil_r : ∀ (X:Type), ∀ l:list X,

l ++ [] = l.

Proof.

(* FILL IN HERE *) Admitted.

Theorem app_assoc : ∀ A (l m n:list A),

l ++ m ++ n = (l ++ m) ++ n.

Proof.

(* FILL IN HERE *) Admitted.

Lemma app_length : ∀ (X:Type) (l

length (l

Proof.

(* FILL IN HERE *) Admitted.

☐

l ++ [] = l.

Proof.

(* FILL IN HERE *) Admitted.

Theorem app_assoc : ∀ A (l m n:list A),

l ++ m ++ n = (l ++ m) ++ n.

Proof.

(* FILL IN HERE *) Admitted.

Lemma app_length : ∀ (X:Type) (l

_{1}l_{2}: list X),length (l

_{1}++ l_{2}) = length l_{1}+ length l_{2}.Proof.

(* FILL IN HERE *) Admitted.

☐

Theorem rev_app_distr: ∀ X (l

rev (l

Proof.

(* FILL IN HERE *) Admitted.

Theorem rev_involutive : ∀ X : Type, ∀ l : list X,

rev (rev l) = l.

Proof.

(* FILL IN HERE *) Admitted.

☐

_{1}l_{2}: list X),rev (l

_{1}++ l_{2}) = rev l_{2}++ rev l_{1}.Proof.

(* FILL IN HERE *) Admitted.

Theorem rev_involutive : ∀ X : Type, ∀ l : list X,

rev (rev l) = l.

Proof.

(* FILL IN HERE *) Admitted.

☐

## Polymorphic Pairs

*polymorphic pairs*, often called

*products*:

As with lists, we make the type arguments implicit and define the
familiar concrete notation.

We can also use the Notation mechanism to define the standard
notation for

*product types*(i.e., the types of pairs):
(The annotation : type_scope tells Coq that this abbreviation
should only be used when parsing types, not when parsing
expressions. This avoids a clash with the multiplication
symbol.)
It is easy at first to get (x,y) and X×Y confused.
Remember that (x,y) is a
The first and second projection functions now look pretty
much as they would in any functional programming language.

*value*built from two other values, while X×Y is a*type*built from two other types. If x has type X and y has type Y, then (x,y) has type X×Y.
Definition fst {X Y : Type} (p : X × Y) : X :=

match p with

| (x, y) ⇒ x

end.

Definition snd {X Y : Type} (p : X × Y) : Y :=

match p with

| (x, y) ⇒ y

end.

match p with

| (x, y) ⇒ x

end.

Definition snd {X Y : Type} (p : X × Y) : Y :=

match p with

| (x, y) ⇒ y

end.

The following function takes two lists and combines them
into a list of pairs. In other functional languages, it is often
called zip; we call it combine for consistency with Coq's
standard library.

Fixpoint combine {X Y : Type} (lx : list X) (ly : list Y)

: list (X×Y) :=

match lx, ly with

| [], _ ⇒ []

| _, [] ⇒ []

| x :: tx, y :: ty ⇒ (x, y) :: (combine tx ty)

end.

: list (X×Y) :=

match lx, ly with

| [], _ ⇒ []

| _, [] ⇒ []

| x :: tx, y :: ty ⇒ (x, y) :: (combine tx ty)

end.

#### Exercise: 2 stars, standard, especially useful (split)

The function split is the right inverse of combine: it takes a list of pairs and returns a pair of lists. In many functional languages, it is called unzip.
Fixpoint split {X Y : Type} (l : list (X×Y)) : (list X) × (list Y)

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_split:

split [(1,false);(2,false)] = ([1;2],[false;false]).

Proof.

(* FILL IN HERE *) Admitted.

☐

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_split:

split [(1,false);(2,false)] = ([1;2],[false;false]).

Proof.

(* FILL IN HERE *) Admitted.

☐

## Polymorphic Options

*polymorphic options*, which generalize natoption from the previous chapter. (We put the definition inside a module because the standard library already defines option and it's this one that we want to use below.)

Module OptionPlayground.

Inductive option (X:Type) : Type :=

| Some (x : X)

| None.

Arguments Some {X}.

Arguments None {X}.

End OptionPlayground.

Inductive option (X:Type) : Type :=

| Some (x : X)

| None.

Arguments Some {X}.

Arguments None {X}.

End OptionPlayground.

We can now rewrite the nth_error function so that it works
with any type of lists.

Fixpoint nth_error {X : Type} (l : list X) (n : nat)

: option X :=

match l with

| nil ⇒ None

| a :: l' ⇒ match n with

| O ⇒ Some a

| S n' ⇒ nth_error l' n'

end

end.

Example test_nth_error1 : nth_error [4;5;6;7] 0 = Some 4.

: option X :=

match l with

| nil ⇒ None

| a :: l' ⇒ match n with

| O ⇒ Some a

| S n' ⇒ nth_error l' 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 [[1];[2]] 1 = Some [2].
Proof. reflexivity. Qed.

Example test_nth_error3 : nth_error [true] 2 = None.
Proof. reflexivity. Qed.

# Functions as Data

## Higher-Order Functions

*higher-order*functions. Here's a simple one:

The argument f here is itself a function (from X to
X); the body of doit3times applies f three times to some
value n.

Check @doit3times : ∀ X : Type, (X → X) → X → X.

Example test_doit3times: doit3times minustwo 9 = 3.

Example test_doit3times': doit3times negb true = false.

Example test_doit3times: doit3times minustwo 9 = 3.

Proof. reflexivity. Qed.

Example test_doit3times': doit3times negb true = false.

Proof. reflexivity. Qed.

## Filter

*predicate*on X (a function from X to bool) and "filtering" the list, returning a new list containing just those elements for which the predicate returns true.

Fixpoint filter {X:Type} (test: X→bool) (l:list X) : list X :=

match l with

| [] ⇒ []

| h :: t ⇒

if test h then h :: (filter test t)

else filter test t

end.

match l with

| [] ⇒ []

| h :: t ⇒

if test h then h :: (filter test t)

else filter test t

end.

For example, if we apply filter to the predicate even
and a list of numbers l, it returns a list containing just the
even members of l.

Example test_filter1: filter even [1;2;3;4] = [2;4].

Definition length_is_1 {X : Type} (l : list X) : bool :=

(length l) =? 1.

Example test_filter2:

filter length_is_1

[ [1; 2]; [3]; [4]; [5;6;7]; []; [8] ]

= [ [3]; [4]; [8] ].

Proof. reflexivity. Qed.

Definition length_is_1 {X : Type} (l : list X) : bool :=

(length l) =? 1.

Example test_filter2:

filter length_is_1

[ [1; 2]; [3]; [4]; [5;6;7]; []; [8] ]

= [ [3]; [4]; [8] ].

Proof. reflexivity. Qed.

Definition countoddmembers' (l:list nat) : nat :=

length (filter odd l).

Example test_countoddmembers'1: countoddmembers' [1;0;3;1;4;5] = 4.

length (filter odd l).

Example test_countoddmembers'1: countoddmembers' [1;0;3;1;4;5] = 4.

Proof. reflexivity. Qed.

Example test_countoddmembers'2: countoddmembers' [0;2;4] = 0.
Proof. reflexivity. Qed.

Example test_countoddmembers'3: countoddmembers' nil = 0.
Proof. reflexivity. Qed.

## Anonymous Functions

The expression (fun n ⇒ n × n) can be read as "the function
that, given a number n, yields n × n."
Here is the filter example, rewritten to use an anonymous
function.

Example test_filter2':

filter (fun l ⇒ (length l) =? 1)

[ [1; 2]; [3]; [4]; [5;6;7]; []; [8] ]

= [ [3]; [4]; [8] ].

filter (fun l ⇒ (length l) =? 1)

[ [1; 2]; [3]; [4]; [5;6;7]; []; [8] ]

= [ [3]; [4]; [8] ].

Proof. reflexivity. Qed.

#### Exercise: 2 stars, standard (filter_even_gt_{7})

Use filter (instead of Fixpoint) to write a Coq function
filter_even_gt_{7}that takes a list of natural numbers as input and returns a list of just those that are even and greater than 7.

Definition filter_even_gt

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_filter_even_gt7_1 :

filter_even_gt

(* FILL IN HERE *) Admitted.

Example test_filter_even_gt7_2 :

filter_even_gt

(* FILL IN HERE *) Admitted.

☐

_{7}(l : list nat) : list nat(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_filter_even_gt7_1 :

filter_even_gt

_{7}[1;2;6;9;10;3;12;8] = [10;12;8].(* FILL IN HERE *) Admitted.

Example test_filter_even_gt7_2 :

filter_even_gt

_{7}[5;2;6;19;129] = [].(* FILL IN HERE *) Admitted.

☐

#### Exercise: 3 stars, standard (partition)

Use filter to write a Coq function partition:partition : ∀ X : Type,

(X → bool) → list X → list X × list X Given a set X, a predicate of type X → bool and a list X, partition should return a pair of lists. The first member of the pair is the sublist of the original list containing the elements that satisfy the test, and the second is the sublist containing those that fail the test. The order of elements in the two sublists should be the same as their order in the original list.

Definition partition {X : Type}

(test : X → bool)

(l : list X)

: list X × list X

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_partition1: partition odd [1;2;3;4;5] = ([1;3;5], [2;4]).

(* FILL IN HERE *) Admitted.

Example test_partition2: partition (fun x ⇒ false) [5;9;0] = ([], [5;9;0]).

(* FILL IN HERE *) Admitted.

☐

(test : X → bool)

(l : list X)

: list X × list X

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_partition1: partition odd [1;2;3;4;5] = ([1;3;5], [2;4]).

(* FILL IN HERE *) Admitted.

Example test_partition2: partition (fun x ⇒ false) [5;9;0] = ([], [5;9;0]).

(* FILL IN HERE *) Admitted.

☐

Fixpoint map {X Y : Type} (f : X→Y) (l : list X) : list Y :=

match l with

| [] ⇒ []

| h :: t ⇒ (f h) :: (map f t)

end.

match l with

| [] ⇒ []

| h :: t ⇒ (f h) :: (map f t)

end.

It takes a function f and a list l = [n

_{1}, n_{2}, n_{3}, ...] and returns the list [f n_{1}, f n_{2}, f n_{3},...] , where f has been applied to each element of l in turn. For example:
The element types of the input and output lists need not be
the same, since map takes

*two*type arguments, X and Y; it can thus be applied to a list of numbers and a function from numbers to booleans to yield a list of booleans:
It can even be applied to a list of numbers and
a function from numbers to

*lists*of booleans to yield a*list of lists*of booleans:
Example test_map3:

map (fun n ⇒ [even n;odd n]) [2;1;2;5]

= [[true;false];[false;true];[true;false];[false;true]].

map (fun n ⇒ [even n;odd n]) [2;1;2;5]

= [[true;false];[false;true];[true;false];[false;true]].

Proof. reflexivity. Qed.

### Exercises

#### Exercise: 3 stars, standard (map_rev)

Show that map and rev commute. You may need to define an auxiliary lemma.
Theorem map_rev : ∀ (X Y : Type) (f : X → Y) (l : list X),

map f (rev l) = rev (map f l).

Proof.

(* FILL IN HERE *) Admitted.

☐

map f (rev l) = rev (map f l).

Proof.

(* FILL IN HERE *) Admitted.

☐

#### Exercise: 2 stars, standard, especially useful (flat_map)

The function map maps a list X to a list Y using a function of type X → Y. We can define a similar function, flat_map, which maps a list X to a list Y using a function f of type X → list Y. Your definition should work by 'flattening' the results of f, like so:flat_map (fun n ⇒ [n;n+1;n+2]) [1;5;10]

= [1; 2; 3; 5; 6; 7; 10; 11; 12].

Fixpoint flat_map {X Y: Type} (f: X → list Y) (l: list X)

: list Y

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_flat_map1:

flat_map (fun n ⇒ [n;n;n]) [1;5;4]

= [1; 1; 1; 5; 5; 5; 4; 4; 4].

(* FILL IN HERE *) Admitted.

☐

: list Y

(* REPLACE THIS LINE WITH ":= _your_definition_ ." *). Admitted.

Example test_flat_map1:

flat_map (fun n ⇒ [n;n;n]) [1;5;4]

= [1; 1; 1; 5; 5; 5; 4; 4; 4].

(* FILL IN HERE *) Admitted.

☐

Definition option_map {X Y : Type} (f : X → Y) (xo : option X)

: option Y :=

match xo with

| None ⇒ None

| Some x ⇒ Some (f x)

end.

: option Y :=

match xo with

| None ⇒ None

| Some x ⇒ Some (f x)

end.

## Fold

Fixpoint fold {X Y: Type} (f : X→Y→Y) (l : list X) (b : Y)

: Y :=

match l with

| nil ⇒ b

| h :: t ⇒ f h (fold f t b)

end.

: Y :=

match l with

| nil ⇒ b

| h :: t ⇒ f h (fold f t b)

end.

Intuitively, the behavior of the fold operation is to
insert a given binary operator f between every pair of elements
in a given list. For example, fold plus [1;2;3;4] intuitively
means 1+2+3+4. To make this precise, we also need a "starting
element" that serves as the initial second input to f. So, for
example,

fold plus [1;2;3;4] 0 yields

1 + (2 + (3 + (4 + 0))). Some more examples:

fold plus [1;2;3;4] 0 yields

1 + (2 + (3 + (4 + 0))). Some more examples:

Check (fold andb) : list bool → bool → bool.

Example fold_example1 :

fold mult [1;2;3;4] 1 = 24.

Example fold_example2 :

fold andb [true;true;false;true] true = false.

Example fold_example3 :

fold app [[1];[];[2;3];[4]] [] = [1;2;3;4].

Example fold_example1 :

fold mult [1;2;3;4] 1 = 24.

Proof. reflexivity. Qed.

Example fold_example2 :

fold andb [true;true;false;true] true = false.

Proof. reflexivity. Qed.

Example fold_example3 :

fold app [[1];[];[2;3];[4]] [] = [1;2;3;4].

Proof. reflexivity. Qed.

## Functions That Construct Functions

*returning*functions as the results of other functions. To begin, here is a function that takes a value x (drawn from some type X) and returns a function from nat to X that yields x whenever it is called, ignoring its nat argument.

Definition constfun {X: Type} (x: X) : nat → X :=

fun (k:nat) ⇒ x.

Definition ftrue := constfun true.

Example constfun_example1 : ftrue 0 = true.

Example constfun_example2 : (constfun 5) 99 = 5.

fun (k:nat) ⇒ x.

Definition ftrue := constfun true.

Example constfun_example1 : ftrue 0 = true.

Proof. reflexivity. Qed.

Example constfun_example2 : (constfun 5) 99 = 5.

Proof. reflexivity. Qed.

In fact, the multiple-argument functions we have already
seen are also examples of passing functions as data. To see why,
recall the type of plus.

Each → in this expression is actually a

*binary*operator on types. This operator is*right-associative*, so the type of plus is really a shorthand for nat → (nat → nat) -- i.e., it can be read as saying that "plus is a one-argument function that takes a nat and returns a one-argument function that takes another nat and returns a nat." In the examples above, we have always applied plus to both of its arguments at once, but if we like we can supply just the first. This is called*partial application*.
Definition plus3 := plus 3.

Check plus3 : nat → nat.

Example test_plus3 : plus3 4 = 7.

Check plus3 : nat → nat.

Example test_plus3 : plus3 4 = 7.

Proof. reflexivity. Qed.

Example test_plus3' : doit3times plus3 0 = 9.
Proof. reflexivity. Qed.

Example test_plus3'' : doit3times (plus 3) 0 = 9.
Proof. reflexivity. Qed.

#### Exercise: 2 stars, standard (fold_length)

Many common functions on lists can be implemented in terms of fold. For example, here is an alternative definition of length:
Definition fold_length {X : Type} (l : list X) : nat :=

fold (fun _ n ⇒ S n) l 0.

Example test_fold_length1 : fold_length [4;7;0] = 3.

fold (fun _ n ⇒ S n) l 0.

Example test_fold_length1 : fold_length [4;7;0] = 3.

Proof. reflexivity. Qed.

Prove the correctness of fold_length. (Hint: It may help to
know that reflexivity simplifies expressions a bit more
aggressively than simpl does -- i.e., you may find yourself in a
situation where simpl does nothing but reflexivity solves the
goal.)

Theorem fold_length_correct : ∀ X (l : list X),

fold_length l = length l.

Proof.

(* FILL IN HERE *) Admitted.

☐

fold_length l = length l.

Proof.

(* FILL IN HERE *) Admitted.

☐