minor fixes to Lists

This commit is contained in:
wadler 2018-02-15 09:40:27 -02:00
parent f939eebf5b
commit a6e1179622

View file

@ -4,8 +4,9 @@ layout : page
permalink : /Lists
---
This chapter gives examples of other data types in Agda, as well as
introducing polymorphic types and functions.
This chapter discusses the list data type. It uses many of the techniques
developed for natural numbers in a different context, and provides
examples of polymorphic types and functions.
## Imports
@ -102,6 +103,9 @@ We can write lists more conveniently by introducing the following definitions.
[_,_,_,_,_,_] : ∀ {A : Set} → A → A → A → A → A → A → List A
[ u , v , w , x , y , z ] = u ∷ v ∷ w ∷ x ∷ y ∷ z ∷ []
\end{code}
Agda recognises that this is a bracketing notation,
and hence we can write things like `length [ 0 , 1 , 2 ]`
rather than `length ([ 0 , 1 , 2 ])`.
## Append
@ -125,8 +129,8 @@ tail the same as the tail of the first list appended to the second list.
Here is an example, showing how to compute the result
of appending two lists.
\begin{code}
ex : [ 0 , 1 , 2 ] ++ [ 3 , 4 ] ≡ [ 0 , 1 , 2 , 3 , 4 ]
ex =
ex : [ 0 , 1 , 2 ] ++ [ 3 , 4 ] ≡ [ 0 , 1 , 2 , 3 , 4 ]
ex =
begin
0 ∷ 1 ∷ 2 ∷ [] ++ 3 ∷ 4 ∷ []
≡⟨⟩
@ -236,8 +240,8 @@ is one greater than the length of the tail of the list.
Here is an example showing how to compute the length of a list.
\begin{code}
ex : length [ 0 , 1 , 2 ] ≡ 3
ex =
ex : length [ 0 , 1 , 2 ] ≡ 3
ex =
begin
length (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩
@ -312,8 +316,8 @@ containing its head.
Here is an example showing how to reverse a list.
\begin{code}
ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ]
ex =
ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ]
ex =
begin
reverse (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩
@ -441,8 +445,8 @@ reverses xs =
Here is an example showing fast reverse of the list `[ 0 , 1 , 2 ]`.
\begin{code}
ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ]
ex =
ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ]
ex =
begin
reverse (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩
@ -476,8 +480,8 @@ and tail the same as map of the function applied to the tail of the given list.
Here is an example showing how to use map to increment every element of a list.
\begin{code}
ex : map suc [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ]
ex =
ex : map suc [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ]
ex =
begin
map suc (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩
@ -499,8 +503,8 @@ point applying the resulting function.
sucs : List → List
sucs = map suc
ex : sucs [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ]
ex₇ = ex₆
ex : sucs [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ]
ex₆ = ex₅
\end{code}
Any type that is parameterised on another type, such as lists, has a
@ -554,8 +558,8 @@ the head of the list and the fold of the tail of the list.
Here is an example showing how to use fold to find the sum of a list.
\begin{code}
ex : foldr _+_ 0 [ 1 , 2 , 3 , 4 ] ≡ 10
ex =
ex : foldr _+_ 0 [ 1 , 2 , 3 , 4 ] ≡ 10
ex =
begin
foldr _+_ 0 (1 ∷ 2 ∷ 3 ∷ 4 ∷ [])
≡⟨⟩
@ -579,8 +583,8 @@ and at a later point applying the resulting function.
sum : List
sum = foldr _+_ 0
ex : sum [ 1 , 2 , 3 , 4 ] ≡ 10
ex₉ = ex₈
ex : sum [ 1 , 2 , 3 , 4 ] ≡ 10
ex₈ = ex₇
\end{code}
Just as the list type has two constructors, `[]` and `_∷_`,
@ -725,8 +729,8 @@ than or equal to two. Recall that `z≤n` proves `zero ≤ n` for any
`n`, and that if `m≤n` proves `m ≤ n` then `s≤s m≤n` proves `suc m ≤
suc n`, for any `m` and `n`.
\begin{code}
ex₁₀ : All (_≤ 2) [ 0 , 1 , 2 ]
ex₁₀ = z≤n ∷ (s≤s z≤n) ∷ (s≤s (s≤s z≤n)) ∷ []
ex : All (_≤ 2) [ 0 , 1 , 2 ]
ex = z≤n ∷ (s≤s z≤n) ∷ (s≤s (s≤s z≤n)) ∷ []
\end{code}
Here `_∷_` and `[]` are the constructors of `All P` rather than of `List A`.
The three items are proofs of `0 ≤ 2`, `1 ≤ 2`, and `2 ≤ 2`, respectively.
@ -755,25 +759,25 @@ For example, zero is an element of the list `[ 0 , 1 , 0 , 2 ]`. Indeed, we can
this fact in two different ways, corresponding to the two different
occurrences of zero in the list, as the first element and as the third element.
\begin{code}
ex₁₁ ex₁₂ : 0 ∈ [ 0 , 1 , 0 , 2 ]
ex₁ = here refl
ex₁ = there (there (here refl))
ex₁₀ ex₁₁ : 0 ∈ [ 0 , 1 , 0 , 2 ]
ex₁ = here refl
ex₁ = there (there (here refl))
\end{code}
Further, we can demonstrate that three is not in the list, because
any possible proof that it is in the list leads to contradiction.
\begin{code}
ex₁ : 3 ∉ [ 0 , 1 , 0 , 2 ]
ex₁ (here ())
ex₁ (there (here ()))
ex₁ (there (there (here ())))
ex₁ (there (there (there (here ()))))
ex₁ (there (there (there (there ()))))
ex₁ : 3 ∉ [ 0 , 1 , 0 , 2 ]
ex₁ (here ())
ex₁ (there (here ()))
ex₁ (there (there (here ())))
ex₁ (there (there (there (here ()))))
ex₁ (there (there (there (there ()))))
\end{code}
The five occurrences of `()` attest to the fact that there is no
possible evidence for the facts `3 ≡ 0`, `3 ≡ 1`, `3 ≡ 0`, `3 ≡ 2`, and
`3 ∈ []`, respectively.
## Any and append
## All and append
A predicate holds for every element of one list appended to another if and
only if it holds for every element of each list. Indeed, an even stronger