Every type we built in Part 3 held values of a fixed type like nat. Now we pass a type itself as a parameter to build polymorphic types and functions that work across any data type.
Run the option example first in a new Lists.v with rocq compile Lists.v. Append the length and concatenation examples, then compile the same file again to see those two results at the end.
1. Implicit Type Arguments {A : Type} and option
A function that extracts the first element of a list never performs arithmetic on that element. Because it does the exact same job regardless of the element type, we name the element type A and declare it in curly braces as {A : Type} so Rocq infers the type automatically from the value arguments at each call site.
Below, head_or returns a caller-supplied fallback on the empty list, while safe_head uses option A to report directly whether a first element was present. First we import List from the standard library and run Import ListNotations. to enable bracket notation such as [10; 20; 30].
From Stdlib Require Import List. loads the library definitions and functions. Import ListNotations. brings short forms such as [10; 20; 30] into scope. To test safe_head on [], whose elements carry no type information, annotate it as ([] : list nat) or pass the normally implicit type yourself with Compute @safe_head nat [].. Both give None : option nat.
From Stdlib Require Import List.
Import ListNotations.
Definition head_or {A : Type} (xs : list A) (fallback : A) : A :=
match xs with
| [] => fallback
| x :: _ => x
end.
Definition safe_head {A : Type} (xs : list A) : option A :=
match xs with
| [] => None
| x :: _ => Some x
end.
Compute head_or [10; 20; 30] 0.
Compute head_or ([] : list bool) false.
Compute safe_head [10; 20; 30].
Compute safe_head ([] : list nat).Output:
= 10 : nat
= false : bool
= Some 10 : option nat
= None : option natFour results come back from Compute. First, head_or [10; 20; 30] 0 extracts the front element = 10 : nat, and on the second line head_or ([] : list bool) false sees an empty boolean list and returns the fallback = false : bool. Third, safe_head [10; 20; 30] wraps the found head element 10 inside the Some constructor as = Some 10 : option nat, whereas on the fourth line safe_head ([] : list nat) returns = None : option nat to signal that the list was empty.
Keep one syntax rule in mind here: in Rocq’s ListNotations, list elements are separated by semicolons (;) rather than commas ([10; 20; 30])! The empty list is [] (or nil), and the constructor that prepends a head element x onto a tail list rest is x :: rest (or cons x rest).1
Looking at how values are constructed makes the difference between option and list clear at a glance. Here is the element type and is the single empty branch:
The second branch of option holds a single element of type , whereas the second branch of list holds both a head element and the remaining list. So safe_head reports only whether that head element exists, while head_or fills the empty branch with a fallback value supplied by the caller.23
2. Fixpoint Recursive Functions on Polymorphic Lists
Because both functions below peel the first list one element at a time, we declare them with Fixpoint. Counting the length does not use the head element’s value, so we ignore it with _ :: rest; appending two lists must keep the head element in the result, so we bind it with x :: rest.
Concatenation stops when the first list runs out and returns the second list, ys. As the calls return, :: prepends each saved x in turn. That puts the first list back in its original order.
Let denote list length and denote the function that joins two lists. The head element is unused when counting length, but is prepended back onto the front when building the concatenated list:
In both functions, the decreasing argument is the first list, which steps down to its tail . The second list is simply attached at the end, so it is passed along unchanged at every step.4
From Stdlib Require Import List.
Import ListNotations.
Fixpoint my_length {A : Type} (xs : list A) : nat :=
match xs with
| [] => 0
| _ :: rest => S (my_length rest)
end.
Fixpoint my_app {A : Type} (xs ys : list A) : list A :=
match xs with
| [] => ys
| x :: rest => x :: my_app rest ys
end.
Compute my_length [10; 20; 30; 40].
Compute my_app [1; 2] [3; 4; 5].Output:
= 4 : nat
= [1; 2; 3; 4; 5] : list natUnfolding the recursion step by step explains both results. First, my_length [10; 20; 30; 40] wraps one S constructor around the outside for each head element it peels off, producing S (S (S (S (my_length [])))), which is S (S (S (S 0))) = 4 : nat.
Second, my_app [1; 2] [3; 4; 5] leaves the second list [3; 4; 5] untouched and unfolds the first list step by step as 1 :: (2 :: my_app [] [3; 4; 5]) 1 :: 2 :: [3; 4; 5] to return = [1; 2; 3; 4; 5] : list nat. When we prove in Part 6 that appending two lists adds their lengths, we will follow this exact recursion direction and induct on the first list xs.
Footnotes
- Rocq Core Library 9.2. Corelib.Init.Datatypes. Shows the definitions and constructors of
optionandlist. ↩ - Rocq Core Library 9.2. Corelib.Init.Datatypes. Defines
NoneandSomeforoptionas well asnil,cons, and::forlist. ↩ - The Rocq Prover Reference Manual 9.2. Implicit Arguments: Implicit Argument Binders. Explains placing type parameters like
Ain curly braces for automatic inference. ↩ - The Rocq Prover Reference Manual 9.2. Inductive Types: Top-Level Recursive Functions · Inductive Types: The match … with … end Construction. Explains recursing on the tail and rebuilding results with constructors. ↩