| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Documentation for the List type |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
module README.Data.List where |
| 8 |
|
| 9 |
open import Data.Nat.Base using (ℕ; _+_) |
| 10 |
open import Data.Nat.ListAction using (sum) |
| 11 |
open import Relation.Binary.PropositionalEquality using (_≡_; refl) |
| 12 |
|
| 13 |
------------------------------------------------------------------------ |
| 14 |
-- 1. Basics |
| 15 |
------------------------------------------------------------------------ |
| 16 |
-- The `List` datatype is exported by the following file: |
| 17 |
|
| 18 |
open import Data.List |
| 19 |
using |
| 20 |
(List |
| 21 |
; []; _∷_ |
| 22 |
; map; take; reverse; _++_; drop |
| 23 |
) |
| 24 |
|
| 25 |
-- Lists are built using the "[]" and "_∷_" constructors. |
| 26 |
|
| 27 |
list₁ : List ℕ |
| 28 |
list₁ = 3 ∷ 1 ∷ 2 ∷ [] |
| 29 |
|
| 30 |
-- Basic operations over lists are also exported by the same file. |
| 31 |
|
| 32 |
lem₁ : sum list₁ ≡ 6 |
| 33 |
lem₁ = refl |
| 34 |
|
| 35 |
lem₂ : map (_+ 2) list₁ ≡ 5 ∷ 3 ∷ 4 ∷ [] |
| 36 |
lem₂ = refl |
| 37 |
|
| 38 |
lem₃ : take 2 list₁ ≡ 3 ∷ 1 ∷ [] |
| 39 |
lem₃ = refl |
| 40 |
|
| 41 |
lem₄ : reverse list₁ ≡ 2 ∷ 1 ∷ 3 ∷ [] |
| 42 |
lem₄ = refl |
| 43 |
|
| 44 |
lem₅ : list₁ ++ list₁ ≡ 3 ∷ 1 ∷ 2 ∷ 3 ∷ 1 ∷ 2 ∷ [] |
| 45 |
lem₅ = refl |
| 46 |
|
| 47 |
-- Various basic properties of these operations can be found in: |
| 48 |
|
| 49 |
open import Data.List.Properties |
| 50 |
|
| 51 |
lem₆ : ∀ n (xs : List ℕ) → take n xs ++ drop n xs ≡ xs |
| 52 |
lem₆ = take++drop≡id |
| 53 |
|
| 54 |
lem₇ : ∀ (xs : List ℕ) → reverse (reverse xs) ≡ xs |
| 55 |
lem₇ = reverse-involutive |
| 56 |
|
| 57 |
lem₈ : ∀ (xs ys zs : List ℕ) → (xs ++ ys) ++ zs ≡ xs ++ (ys ++ zs) |
| 58 |
lem₈ = ++-assoc |
| 59 |
|
| 60 |
|
| 61 |
------------------------------------------------------------------------ |
| 62 |
-- 2. Unary relations over lists |
| 63 |
------------------------------------------------------------------------ |
| 64 |
|
| 65 |
-- Unary relations in `Data.List.Relation.Unary` are used to reason |
| 66 |
-- about the properties of an individual list. |
| 67 |
|
| 68 |
------------------------------------------------------------------------ |
| 69 |
-- Any |
| 70 |
|
| 71 |
-- The predicate `Any` encodes the idea of at least one element of a |
| 72 |
-- given list satisfying a given property (or more formally a |
| 73 |
-- predicate, see the `Pred` type in `Relation.Unary`). |
| 74 |
|
| 75 |
import README.Data.List.Relation.Unary.Any |
| 76 |
|
| 77 |
------------------------------------------------------------------------ |
| 78 |
-- All |
| 79 |
|
| 80 |
-- The dual to `Any` is the predicate `All` which encodes the idea that |
| 81 |
-- every element in a given list satisfies a given property. |
| 82 |
|
| 83 |
import README.Data.List.Relation.Unary.All |
| 84 |
|
| 85 |
------------------------------------------------------------------------ |
| 86 |
-- Other unary relations |
| 87 |
|
| 88 |
-- There exist many other unary relations in the |
| 89 |
-- `Data.List.Relation.Unary` folder, including: |
| 90 |
|
| 91 |
-- 1. lists with every pair of elements related |
| 92 |
|
| 93 |
import Data.List.Relation.Unary.AllPairs |
| 94 |
|
| 95 |
-- 2. lists with only unique elements |
| 96 |
|
| 97 |
import Data.List.Relation.Unary.Unique.Setoid |
| 98 |
|
| 99 |
-- 3. lists with each pair of neighbouring elements related |
| 100 |
|
| 101 |
import Data.List.Relation.Unary.Linked |
| 102 |
|
| 103 |
|
| 104 |
------------------------------------------------------------------------ |
| 105 |
-- 3. Binary relations over lists |
| 106 |
------------------------------------------------------------------------ |
| 107 |
|
| 108 |
-- Binary relations relate two different lists, and are found in the |
| 109 |
-- folder `Data.List.Relation.Binary`. |
| 110 |
|
| 111 |
------------------------------------------------------------------------ |
| 112 |
-- Pointwise |
| 113 |
|
| 114 |
-- One of the most basic ways to form a binary relation between two |
| 115 |
-- lists of type `List A`, given a binary relation over `A`, is to say |
| 116 |
-- that two lists are related if they are the same length and: |
| 117 |
-- i) the first elements in the lists are related |
| 118 |
-- ii) the second elements in the lists are related |
| 119 |
-- iii) the third elements in the lists are related etc. |
| 120 |
-- etc. |
| 121 |
|
| 122 |
-- This is known as the pointwise lifting of a relation |
| 123 |
|
| 124 |
import README.Data.List.Relation.Binary.Pointwise |
| 125 |
|
| 126 |
------------------------------------------------------------------------ |
| 127 |
-- Equality |
| 128 |
|
| 129 |
-- There are many different options for what it means for two |
| 130 |
-- different lists of type `List A` to be "equal". We will initially |
| 131 |
-- consider notions of equality that require the list elements to be |
| 132 |
-- pointwise equal. |
| 133 |
|
| 134 |
import README.Data.List.Relation.Binary.Equality |
| 135 |
|
| 136 |
------------------------------------------------------------------------ |
| 137 |
-- Permutations |
| 138 |
|
| 139 |
-- Alternatively you might consider two lists to be equal if they |
| 140 |
-- contain the same elements regardless of the order of the elements. |
| 141 |
-- This is known as either "set equality" or a "permutation". |
| 142 |
|
| 143 |
import README.Data.List.Relation.Binary.Permutation |
| 144 |
|
| 145 |
------------------------------------------------------------------------ |
| 146 |
-- Subsets |
| 147 |
|
| 148 |
-- Instead one might want to order lists by the subset relation which |
| 149 |
-- forms a partial order over lists. One list is a subset of another if |
| 150 |
-- every element in the first list occurs at least once in the second. |
| 151 |
|
| 152 |
import README.Data.List.Relation.Binary.Subset |
| 153 |
|
| 154 |
------------------------------------------------------------------------ |
| 155 |
-- Other binary relations |
| 156 |
|
| 157 |
-- There exist many other binary relations in the |
| 158 |
-- `Data.List.Relation.Binary` folder, including: |
| 159 |
|
| 160 |
-- 1. lexicographic orderings |
| 161 |
|
| 162 |
import Data.List.Relation.Binary.Lex.Strict |
| 163 |
|
| 164 |
-- 2. bag/multiset equality |
| 165 |
|
| 166 |
import Data.List.Relation.Binary.BagAndSetEquality |
| 167 |
|
| 168 |
-- 3. the sublist relations |
| 169 |
|
| 170 |
import Data.List.Relation.Binary.Sublist.Propositional |
| 171 |
|
| 172 |
|
| 173 |
------------------------------------------------------------------------ |
| 174 |
-- 4. Ternary relations over lists |
| 175 |
------------------------------------------------------------------------ |
| 176 |
|
| 177 |
-- Ternary relations relate three different lists, and are found in the |
| 178 |
-- folder `Data.List.Relation.Ternary`. |
| 179 |
|
| 180 |
------------------------------------------------------------------------ |
| 181 |
-- Interleaving |
| 182 |
|
| 183 |
-- Given two lists, a third list is an `Interleaving` of them if there |
| 184 |
-- exists an order preserving partition of it that reconstructs the |
| 185 |
-- original two lists. |
| 186 |
|
| 187 |
import README.Data.List.Relation.Ternary.Interleaving |
| 188 |
|
| 189 |
|
| 190 |
------------------------------------------------------------------------ |
| 191 |
-- 5. Membership |
| 192 |
------------------------------------------------------------------------ |
| 193 |
|
| 194 |
-- Although simply a specialisation of the unary predicate `Any`, |
| 195 |
-- membership of a list is not strictly a unary or a binary relation |
| 196 |
-- over lists. Therefore it lives it it's own top-level folder. |
| 197 |
|
| 198 |
import README.Data.List.Membership |