| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Lists, based on the Kleene star and plus, basic types and operations. |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Kleene.Base where |
| 10 |
|
| 11 |
open import Algebra.Core using (Op₂) |
| 12 |
open import Data.Product.Base as Product |
| 13 |
using (_×_; _,_; map₂; map₁; proj₁; proj₂) |
| 14 |
open import Data.Nat.Base as ℕ using (ℕ; suc; zero) |
| 15 |
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; maybe′) |
| 16 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 17 |
open import Function.Base using (id; _$_; _∘_) |
| 18 |
open import Level using (Level) |
| 19 |
|
| 20 |
|
| 21 |
private |
| 22 |
variable |
| 23 |
a b c : Level |
| 24 |
A : Set a |
| 25 |
B : Set b |
| 26 |
C : Set c |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- Definitions |
| 30 |
-- |
| 31 |
-- These lists are exactly equivalent to normal lists, except the "cons" |
| 32 |
-- case is split into its own data type. This lets us write all the same |
| 33 |
-- functions as before, but it has 2 advantages: |
| 34 |
-- |
| 35 |
-- * Some functions are easier to express on the non-empty type. Head, |
| 36 |
-- for instance, has a natural safe implementation. Having the |
| 37 |
-- non-empty type be defined mutually with the normal type makes the |
| 38 |
-- use of this non-empty type occasionally more ergonomic. |
| 39 |
-- * It can make some proofs easier. By using the non-empty type where |
| 40 |
-- possible, we can avoid an extra pattern match, which can really |
| 41 |
-- simplify certain proofs. |
| 42 |
|
| 43 |
infixr 5 _&_ ∹_ |
| 44 |
infixl 4 _+ _* |
| 45 |
|
| 46 |
record _+ {a} (A : Set a) : Set a |
| 47 |
data _* {a} (A : Set a) : Set a |
| 48 |
|
| 49 |
-- Non-Empty Lists |
| 50 |
record _+ A where |
| 51 |
inductive |
| 52 |
constructor _&_ |
| 53 |
field |
| 54 |
head : A |
| 55 |
tail : A * |
| 56 |
|
| 57 |
-- Possibly Empty Lists |
| 58 |
data _* A where |
| 59 |
[] : A * |
| 60 |
∹_ : A + → A * |
| 61 |
open _+ public |
| 62 |
|
| 63 |
------------------------------------------------------------------------ |
| 64 |
-- Uncons |
| 65 |
|
| 66 |
uncons : A * → Maybe (A +) |
| 67 |
uncons [] = nothing |
| 68 |
uncons (∹ xs) = just xs |
| 69 |
|
| 70 |
------------------------------------------------------------------------ |
| 71 |
-- FoldMap |
| 72 |
|
| 73 |
foldMap+ : Op₂ B → (A → B) → A + → B |
| 74 |
foldMap+ _∙_ f (x & []) = f x |
| 75 |
foldMap+ _∙_ f (x & ∹ xs) = f x ∙ foldMap+ _∙_ f xs |
| 76 |
|
| 77 |
foldMap* : Op₂ B → B → (A → B) → A * → B |
| 78 |
foldMap* _∙_ ε f [] = ε |
| 79 |
foldMap* _∙_ ε f (∹ xs) = foldMap+ _∙_ f xs |
| 80 |
|
| 81 |
------------------------------------------------------------------------ |
| 82 |
-- Folds |
| 83 |
|
| 84 |
module _ (f : A → B → B) (b : B) where |
| 85 |
foldr+ : A + → B |
| 86 |
foldr* : A * → B |
| 87 |
|
| 88 |
foldr+ (x & xs) = f x (foldr* xs) |
| 89 |
|
| 90 |
foldr* [] = b |
| 91 |
foldr* (∹ xs) = foldr+ xs |
| 92 |
|
| 93 |
module _ (f : B → A → B) where |
| 94 |
foldl+ : B → A + → B |
| 95 |
foldl* : B → A * → B |
| 96 |
|
| 97 |
foldl+ b (x & xs) = foldl* (f b x) xs |
| 98 |
|
| 99 |
foldl* b [] = b |
| 100 |
foldl* b (∹ xs) = foldl+ b xs |
| 101 |
|
| 102 |
------------------------------------------------------------------------ |
| 103 |
-- Concatenation |
| 104 |
|
| 105 |
module Concat where |
| 106 |
infixr 4 _++++_ _+++*_ _*+++_ _*++*_ |
| 107 |
_++++_ : A + → A + → A + |
| 108 |
_+++*_ : A + → A * → A + |
| 109 |
_*+++_ : A * → A + → A + |
| 110 |
_*++*_ : A * → A * → A * |
| 111 |
|
| 112 |
head (xs +++* ys) = head xs |
| 113 |
tail (xs +++* ys) = tail xs *++* ys |
| 114 |
|
| 115 |
xs *++* ys = foldr* (λ x zs → ∹ x & zs) ys xs |
| 116 |
|
| 117 |
xs ++++ ys = foldr+ (λ x zs → x & ∹ zs) ys xs |
| 118 |
|
| 119 |
[] *+++ ys = ys |
| 120 |
(∹ xs) *+++ ys = xs ++++ ys |
| 121 |
open Concat public using () renaming (_++++_ to _+++_; _*++*_ to _++*_) |
| 122 |
|
| 123 |
------------------------------------------------------------------------ |
| 124 |
-- Mapping |
| 125 |
|
| 126 |
module _ (f : A → B) where |
| 127 |
map+ : A + → B + |
| 128 |
map* : A * → B * |
| 129 |
|
| 130 |
head (map+ xs) = f (head xs) |
| 131 |
tail (map+ xs) = map* (tail xs) |
| 132 |
|
| 133 |
map* [] = [] |
| 134 |
map* (∹ xs) = ∹ map+ xs |
| 135 |
|
| 136 |
module _ (f : A → Maybe B) where |
| 137 |
mapMaybe+ : A + → B * |
| 138 |
mapMaybe* : A * → B * |
| 139 |
|
| 140 |
mapMaybe+ (x & xs) = maybe′ (λ y z → ∹ y & z) id (f x) $ mapMaybe* xs |
| 141 |
|
| 142 |
mapMaybe* [] = [] |
| 143 |
mapMaybe* (∹ xs) = mapMaybe+ xs |
| 144 |
|
| 145 |
------------------------------------------------------------------------ |
| 146 |
-- Applicative Operations |
| 147 |
|
| 148 |
pure+ : A → A + |
| 149 |
head (pure+ x) = x |
| 150 |
tail (pure+ x) = [] |
| 151 |
|
| 152 |
pure* : A → A * |
| 153 |
pure* x = ∹ pure+ x |
| 154 |
|
| 155 |
module Apply where |
| 156 |
_*<*>*_ : (A → B) * → A * → B * |
| 157 |
_+<*>*_ : (A → B) + → A * → B * |
| 158 |
_*<*>+_ : (A → B) * → A + → B * |
| 159 |
_+<*>+_ : (A → B) + → A + → B + |
| 160 |
|
| 161 |
[] *<*>* xs = [] |
| 162 |
(∹ fs) *<*>* xs = fs +<*>* xs |
| 163 |
|
| 164 |
fs +<*>* xs = map* (head fs) xs ++* (tail fs *<*>* xs) |
| 165 |
|
| 166 |
[] *<*>+ xs = [] |
| 167 |
(∹ fs) *<*>+ xs = ∹ fs +<*>+ xs |
| 168 |
|
| 169 |
fs +<*>+ xs = map+ (head fs) xs Concat.+++* (tail fs *<*>+ xs) |
| 170 |
open Apply public using () renaming (_*<*>*_ to _<*>*_; _+<*>+_ to _<*>+_) |
| 171 |
|
| 172 |
------------------------------------------------------------------------ |
| 173 |
-- Monadic Operations |
| 174 |
|
| 175 |
module Bind where |
| 176 |
_+>>=+_ : A + → (A → B +) → B + |
| 177 |
_+>>=*_ : A + → (A → B *) → B * |
| 178 |
_*>>=+_ : A * → (A → B +) → B * |
| 179 |
_*>>=*_ : A * → (A → B *) → B * |
| 180 |
|
| 181 |
(x & xs) +>>=+ k = k x Concat.+++* (xs *>>=+ k) |
| 182 |
|
| 183 |
(x & xs) +>>=* k = k x Concat.*++* (xs *>>=* k) |
| 184 |
|
| 185 |
[] *>>=* k = [] |
| 186 |
(∹ xs) *>>=* k = xs +>>=* k |
| 187 |
|
| 188 |
[] *>>=+ k = [] |
| 189 |
(∹ xs) *>>=+ k = ∹ xs +>>=+ k |
| 190 |
open Bind public using () renaming (_*>>=*_ to _>>=*_; _+>>=+_ to _>>=+_) |
| 191 |
|
| 192 |
------------------------------------------------------------------------ |
| 193 |
-- Scans |
| 194 |
|
| 195 |
module Scanr (f : A → B → B) (b : B) where |
| 196 |
cons : A → B + → B + |
| 197 |
head (cons x xs) = f x (head xs) |
| 198 |
tail (cons x xs) = ∹ xs |
| 199 |
|
| 200 |
scanr+ : A + → B + |
| 201 |
scanr* : A * → B + |
| 202 |
|
| 203 |
scanr* = foldr* cons (b & []) |
| 204 |
scanr+ = foldr+ cons (b & []) |
| 205 |
open Scanr public using (scanr+; scanr*) |
| 206 |
|
| 207 |
module _ (f : B → A → B) where |
| 208 |
scanl* : B → A * → B + |
| 209 |
head (scanl* b xs) = b |
| 210 |
tail (scanl* b []) = [] |
| 211 |
tail (scanl* b (∹ xs)) = ∹ scanl* (f b (head xs)) (tail xs) |
| 212 |
|
| 213 |
scanl+ : B → A + → B + |
| 214 |
head (scanl+ b xs) = b |
| 215 |
tail (scanl+ b xs) = ∹ scanl* (f b (head xs)) (tail xs) |
| 216 |
|
| 217 |
scanl₁ : B → A + → B + |
| 218 |
scanl₁ b xs = scanl* (f b (head xs)) (tail xs) |
| 219 |
|
| 220 |
------------------------------------------------------------------------ |
| 221 |
-- Accumulating maps |
| 222 |
|
| 223 |
module _ (f : B → A → (B × C)) where |
| 224 |
mapAccumˡ* : B → A * → (B × C *) |
| 225 |
mapAccumˡ+ : B → A + → (B × C +) |
| 226 |
|
| 227 |
mapAccumˡ* b [] = b , [] |
| 228 |
mapAccumˡ* b (∹ xs) = map₂ ∹_ (mapAccumˡ+ b xs) |
| 229 |
|
| 230 |
mapAccumˡ+ b (x & xs) = |
| 231 |
let y , ys = f b x |
| 232 |
z , zs = mapAccumˡ* y xs |
| 233 |
in z , ys & zs |
| 234 |
|
| 235 |
module _ (f : A → B → (C × B)) (b : B) where |
| 236 |
mapAccumʳ* : A * → (C * × B) |
| 237 |
mapAccumʳ+ : A + → (C + × B) |
| 238 |
|
| 239 |
mapAccumʳ* [] = [] , b |
| 240 |
mapAccumʳ* (∹ xs) = map₁ ∹_ (mapAccumʳ+ xs) |
| 241 |
|
| 242 |
mapAccumʳ+ (x & xs) = |
| 243 |
let ys , y = mapAccumʳ* xs |
| 244 |
zs , z = f x y |
| 245 |
in zs & ys , z |
| 246 |
|
| 247 |
------------------------------------------------------------------------ |
| 248 |
-- Non-Empty Folds |
| 249 |
|
| 250 |
last : A + → A |
| 251 |
last (x & []) = x |
| 252 |
last (_ & ∹ xs) = last xs |
| 253 |
|
| 254 |
module _ (f : A → A → A) where |
| 255 |
foldr₁ : A + → A |
| 256 |
foldr₁ (x & []) = x |
| 257 |
foldr₁ (x & ∹ xs) = f x (foldr₁ xs) |
| 258 |
|
| 259 |
foldl₁ : A + → A |
| 260 |
foldl₁ (x & xs) = foldl* f x xs |
| 261 |
|
| 262 |
module _ (f : A → Maybe B → B) where |
| 263 |
foldrMaybe* : A * → Maybe B |
| 264 |
foldrMaybe+ : A + → B |
| 265 |
|
| 266 |
foldrMaybe* [] = nothing |
| 267 |
foldrMaybe* (∹ xs) = just (foldrMaybe+ xs) |
| 268 |
|
| 269 |
foldrMaybe+ (x & xs) = f x (foldrMaybe* xs) |
| 270 |
|
| 271 |
------------------------------------------------------------------------ |
| 272 |
-- Indexing |
| 273 |
|
| 274 |
infix 4 _[_]* _[_]+ |
| 275 |
|
| 276 |
_[_]* : A * → ℕ → Maybe A |
| 277 |
_[_]+ : A + → ℕ → Maybe A |
| 278 |
|
| 279 |
[] [ _ ]* = nothing |
| 280 |
(∹ xs) [ i ]* = xs [ i ]+ |
| 281 |
|
| 282 |
xs [ zero ]+ = just (head xs) |
| 283 |
xs [ suc i ]+ = tail xs [ i ]* |
| 284 |
|
| 285 |
applyUpTo* : (ℕ → A) → ℕ → A * |
| 286 |
applyUpTo+ : (ℕ → A) → ℕ → A + |
| 287 |
|
| 288 |
applyUpTo* f zero = [] |
| 289 |
applyUpTo* f (suc n) = ∹ applyUpTo+ f n |
| 290 |
|
| 291 |
head (applyUpTo+ f n) = f zero |
| 292 |
tail (applyUpTo+ f n) = applyUpTo* (f ∘ suc) n |
| 293 |
|
| 294 |
upTo* : ℕ → ℕ * |
| 295 |
upTo* = applyUpTo* id |
| 296 |
|
| 297 |
upTo+ : ℕ → ℕ + |
| 298 |
upTo+ = applyUpTo+ id |
| 299 |
|
| 300 |
------------------------------------------------------------------------ |
| 301 |
-- Manipulation |
| 302 |
|
| 303 |
module ZipWith (f : A → B → C) where |
| 304 |
+zipWith+ : A + → B + → C + |
| 305 |
*zipWith+ : A * → B + → C * |
| 306 |
+zipWith* : A + → B * → C * |
| 307 |
*zipWith* : A * → B * → C * |
| 308 |
|
| 309 |
head (+zipWith+ xs ys) = f (head xs) (head ys) |
| 310 |
tail (+zipWith+ xs ys) = *zipWith* (tail xs) (tail ys) |
| 311 |
|
| 312 |
*zipWith+ [] ys = [] |
| 313 |
*zipWith+ (∹ xs) ys = ∹ +zipWith+ xs ys |
| 314 |
|
| 315 |
+zipWith* xs [] = [] |
| 316 |
+zipWith* xs (∹ ys) = ∹ +zipWith+ xs ys |
| 317 |
|
| 318 |
*zipWith* [] ys = [] |
| 319 |
*zipWith* (∹ xs) ys = +zipWith* xs ys |
| 320 |
open ZipWith public renaming (+zipWith+ to zipWith+; *zipWith* to zipWith*) |
| 321 |
|
| 322 |
module Unzip (f : A → B × C) where |
| 323 |
cons : B × C → B * × C * → B + × C + |
| 324 |
cons = Product.zip′ _&_ _&_ |
| 325 |
|
| 326 |
unzipWith* : A * → B * × C * |
| 327 |
unzipWith+ : A + → B + × C + |
| 328 |
|
| 329 |
unzipWith* = foldr* (λ x xs → Product.map ∹_ ∹_ (cons (f x) xs)) ([] , []) |
| 330 |
|
| 331 |
unzipWith+ xs = cons (f (head xs)) (unzipWith* (tail xs)) |
| 332 |
open Unzip using (unzipWith+; unzipWith*) public |
| 333 |
|
| 334 |
module Partition (f : A → B ⊎ C) where |
| 335 |
cons : B ⊎ C → B * × C * → B * × C * |
| 336 |
proj₁ (cons (inj₁ x) xs) = ∹ x & proj₁ xs |
| 337 |
proj₂ (cons (inj₁ x) xs) = proj₂ xs |
| 338 |
proj₂ (cons (inj₂ x) xs) = ∹ x & proj₂ xs |
| 339 |
proj₁ (cons (inj₂ x) xs) = proj₁ xs |
| 340 |
|
| 341 |
partitionSumsWith* : A * → B * × C * |
| 342 |
partitionSumsWith+ : A + → B * × C * |
| 343 |
|
| 344 |
partitionSumsWith* = foldr* (cons ∘ f) ([] , []) |
| 345 |
|
| 346 |
partitionSumsWith+ = foldr+ (cons ∘ f) ([] , []) |
| 347 |
open Partition using (partitionSumsWith+; partitionSumsWith*) public |
| 348 |
|
| 349 |
tails* : A * → (A +) * |
| 350 |
tails+ : A + → (A +) + |
| 351 |
|
| 352 |
head (tails+ xs) = xs |
| 353 |
tail (tails+ xs) = tails* (tail xs) |
| 354 |
|
| 355 |
tails* [] = [] |
| 356 |
tails* (∹ xs) = ∹ tails+ xs |
| 357 |
|
| 358 |
reverse* : A * → A * |
| 359 |
reverse* = foldl* (λ xs x → ∹ x & xs) [] |
| 360 |
|
| 361 |
reverse+ : A + → A + |
| 362 |
reverse+ (x & xs) = foldl* (λ ys y → y & ∹ ys) (x & []) xs |