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 permalink : /Lists
--- ---
This chapter gives examples of other data types in Agda, as well as This chapter discusses the list data type. It uses many of the techniques
introducing polymorphic types and functions. developed for natural numbers in a different context, and provides
examples of polymorphic types and functions.
## Imports ## 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 [_,_,_,_,_,_] : ∀ {A : Set} → A → A → A → A → A → A → List A
[ u , v , w , x , y , z ] = u ∷ v ∷ w ∷ x ∷ y ∷ z ∷ [] [ u , v , w , x , y , z ] = u ∷ v ∷ w ∷ x ∷ y ∷ z ∷ []
\end{code} \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 ## 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 Here is an example, showing how to compute the result
of appending two lists. of appending two lists.
\begin{code} \begin{code}
ex : [ 0 , 1 , 2 ] ++ [ 3 , 4 ] ≡ [ 0 , 1 , 2 , 3 , 4 ] ex : [ 0 , 1 , 2 ] ++ [ 3 , 4 ] ≡ [ 0 , 1 , 2 , 3 , 4 ]
ex = ex =
begin begin
0 ∷ 1 ∷ 2 ∷ [] ++ 3 ∷ 4 ∷ [] 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. Here is an example showing how to compute the length of a list.
\begin{code} \begin{code}
ex : length [ 0 , 1 , 2 ] ≡ 3 ex : length [ 0 , 1 , 2 ] ≡ 3
ex = ex =
begin begin
length (0 ∷ 1 ∷ 2 ∷ []) length (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩ ≡⟨⟩
@ -312,8 +316,8 @@ containing its head.
Here is an example showing how to reverse a list. Here is an example showing how to reverse a list.
\begin{code} \begin{code}
ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ] ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ]
ex = ex =
begin begin
reverse (0 ∷ 1 ∷ 2 ∷ []) reverse (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩ ≡⟨⟩
@ -441,8 +445,8 @@ reverses xs =
Here is an example showing fast reverse of the list `[ 0 , 1 , 2 ]`. Here is an example showing fast reverse of the list `[ 0 , 1 , 2 ]`.
\begin{code} \begin{code}
ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ] ex : reverse [ 0 , 1 , 2 ] ≡ [ 2 , 1 , 0 ]
ex = ex =
begin begin
reverse (0 ∷ 1 ∷ 2 ∷ []) 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. Here is an example showing how to use map to increment every element of a list.
\begin{code} \begin{code}
ex : map suc [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ] ex : map suc [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ]
ex = ex =
begin begin
map suc (0 ∷ 1 ∷ 2 ∷ []) map suc (0 ∷ 1 ∷ 2 ∷ [])
≡⟨⟩ ≡⟨⟩
@ -499,8 +503,8 @@ point applying the resulting function.
sucs : List → List sucs : List → List
sucs = map suc sucs = map suc
ex : sucs [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ] ex : sucs [ 0 , 1 , 2 ] ≡ [ 1 , 2 , 3 ]
ex₇ = ex₆ ex₆ = ex₅
\end{code} \end{code}
Any type that is parameterised on another type, such as lists, has a 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. Here is an example showing how to use fold to find the sum of a list.
\begin{code} \begin{code}
ex : foldr _+_ 0 [ 1 , 2 , 3 , 4 ] ≡ 10 ex : foldr _+_ 0 [ 1 , 2 , 3 , 4 ] ≡ 10
ex = ex =
begin begin
foldr _+_ 0 (1 ∷ 2 ∷ 3 ∷ 4 ∷ []) foldr _+_ 0 (1 ∷ 2 ∷ 3 ∷ 4 ∷ [])
≡⟨⟩ ≡⟨⟩
@ -579,8 +583,8 @@ and at a later point applying the resulting function.
sum : List sum : List
sum = foldr _+_ 0 sum = foldr _+_ 0
ex : sum [ 1 , 2 , 3 , 4 ] ≡ 10 ex : sum [ 1 , 2 , 3 , 4 ] ≡ 10
ex₉ = ex₈ ex₈ = ex₇
\end{code} \end{code}
Just as the list type has two constructors, `[]` and `_∷_`, 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 ≤ `n`, and that if `m≤n` proves `m ≤ n` then `s≤s m≤n` proves `suc m ≤
suc n`, for any `m` and `n`. suc n`, for any `m` and `n`.
\begin{code} \begin{code}
ex₁₀ : All (_≤ 2) [ 0 , 1 , 2 ] ex : All (_≤ 2) [ 0 , 1 , 2 ]
ex₁₀ = z≤n ∷ (s≤s z≤n) ∷ (s≤s (s≤s z≤n)) ∷ [] ex = z≤n ∷ (s≤s z≤n) ∷ (s≤s (s≤s z≤n)) ∷ []
\end{code} \end{code}
Here `_∷_` and `[]` are the constructors of `All P` rather than of `List A`. 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. 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 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. occurrences of zero in the list, as the first element and as the third element.
\begin{code} \begin{code}
ex₁₁ ex₁₂ : 0 ∈ [ 0 , 1 , 0 , 2 ] ex₁₀ ex₁₁ : 0 ∈ [ 0 , 1 , 0 , 2 ]
ex₁ = here refl ex₁ = here refl
ex₁ = there (there (here refl)) ex₁ = there (there (here refl))
\end{code} \end{code}
Further, we can demonstrate that three is not in the list, because Further, we can demonstrate that three is not in the list, because
any possible proof that it is in the list leads to contradiction. any possible proof that it is in the list leads to contradiction.
\begin{code} \begin{code}
ex₁ : 3 ∉ [ 0 , 1 , 0 , 2 ] ex₁ : 3 ∉ [ 0 , 1 , 0 , 2 ]
ex₁ (here ()) ex₁ (here ())
ex₁ (there (here ())) ex₁ (there (here ()))
ex₁ (there (there (here ()))) ex₁ (there (there (here ())))
ex₁ (there (there (there (here ())))) ex₁ (there (there (there (here ()))))
ex₁ (there (there (there (there ())))) ex₁ (there (there (there (there ()))))
\end{code} \end{code}
The five occurrences of `()` attest to the fact that there is no 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 possible evidence for the facts `3 ≡ 0`, `3 ≡ 1`, `3 ≡ 0`, `3 ≡ 2`, and
`3 ∈ []`, respectively. `3 ∈ []`, respectively.
## Any and append ## All and append
A predicate holds for every element of one list appended to another if and 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 only if it holds for every element of each list. Indeed, an even stronger