| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- List-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- Note that the lemmas below could be generalised to work with other |
| 8 |
-- equalities than _≡_. |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
{-# OPTIONS --warning=noUserWarning #-} -- for deprecated scans |
| 12 |
|
| 13 |
module Data.List.Properties where |
| 14 |
|
| 15 |
open import Algebra.Bundles using (Semigroup; Monoid) |
| 16 |
open import Algebra.Consequences.Propositional |
| 17 |
using (selfInverse⇒involutive; selfInverse⇒injective) |
| 18 |
open import Algebra.Definitions as AlgebraicDefinitions using (SelfInverse; Involutive) |
| 19 |
open import Algebra.Morphism.Structures using (IsMagmaHomomorphism; IsMonoidHomomorphism) |
| 20 |
import Algebra.Structures as AlgebraicStructures |
| 21 |
open import Data.Bool.Base using (Bool; false; true; not; if_then_else_) |
| 22 |
open import Data.Fin.Base using (Fin; zero; suc; cast; toℕ) |
| 23 |
open import Data.List.Base as List |
| 24 |
open import Data.List.Relation.Unary.All using (All; []; _∷_) |
| 25 |
open import Data.List.Relation.Unary.Any using (Any; here; there) |
| 26 |
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing) |
| 27 |
open import Data.Maybe.Relation.Unary.Any using (just) renaming (Any to MAny) |
| 28 |
open import Data.Nat.Base |
| 29 |
open import Data.Nat.Properties |
| 30 |
open import Data.Product.Base as Product |
| 31 |
using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; <_,_>) |
| 32 |
import Data.Product.Relation.Unary.All as Product using (All) |
| 33 |
open import Data.Sum using (_⊎_; inj₁; inj₂; isInj₁; isInj₂) |
| 34 |
open import Data.These.Base as These using (These; this; that; these) |
| 35 |
open import Data.Fin.Properties using (toℕ-cast) |
| 36 |
open import Function.Base using (id; _∘_; _∘′_; _∋_; _-⟨_∣; ∣_⟩-_; _$_; const; flip) |
| 37 |
open import Function.Definitions using (Injective) |
| 38 |
open import Level using (Level) |
| 39 |
open import Relation.Binary.Definitions as B using (DecidableEquality) |
| 40 |
import Relation.Binary.Reasoning.Setoid as ≈-Reasoning |
| 41 |
open import Relation.Binary.PropositionalEquality.Core as ≡ |
| 42 |
open import Relation.Binary.PropositionalEquality.Properties as ≡ |
| 43 |
open import Relation.Binary.Core using (Rel) |
| 44 |
open import Relation.Nullary.Reflects using (invert) |
| 45 |
open import Relation.Nullary.Decidable.Core using (Dec; yes; no; _because_; does) |
| 46 |
open import Relation.Nullary.Negation.Core using (contradiction; ¬_) |
| 47 |
open import Relation.Nullary.Decidable as Decidable |
| 48 |
using (isYes; map′; ⌊_⌋; ¬?; _×-dec_; dec-true; dec-false) |
| 49 |
open import Relation.Unary using (Pred; Decidable; ∁; _≐_) |
| 50 |
open import Relation.Unary.Properties using (∁?) |
| 51 |
import Data.Nat.GeneralisedArithmetic as ℕ |
| 52 |
|
| 53 |
open ≡-Reasoning |
| 54 |
|
| 55 |
private |
| 56 |
variable |
| 57 |
a b c d e p q ℓ : Level |
| 58 |
A : Set a |
| 59 |
B : Set b |
| 60 |
C : Set c |
| 61 |
D : Set d |
| 62 |
E : Set e |
| 63 |
x y z w : A |
| 64 |
xs ys zs ws : List A |
| 65 |
|
| 66 |
------------------------------------------------------------------------ |
| 67 |
-- _∷_ |
| 68 |
|
| 69 |
∷-injective : x ∷ xs ≡ y List.∷ ys → x ≡ y × xs ≡ ys |
| 70 |
∷-injective refl = refl , refl |
| 71 |
|
| 72 |
∷-injectiveˡ : x ∷ xs ≡ y List.∷ ys → x ≡ y |
| 73 |
∷-injectiveˡ refl = refl |
| 74 |
|
| 75 |
∷-injectiveʳ : x ∷ xs ≡ y List.∷ ys → xs ≡ ys |
| 76 |
∷-injectiveʳ refl = refl |
| 77 |
|
| 78 |
∷-dec : Dec (x ≡ y) → Dec (xs ≡ ys) → Dec (x ∷ xs ≡ y List.∷ ys) |
| 79 |
∷-dec x≟y xs≟ys = Decidable.map′ (uncurry (cong₂ _∷_)) ∷-injective (x≟y ×-dec xs≟ys) |
| 80 |
|
| 81 |
≡-dec : DecidableEquality A → DecidableEquality (List A) |
| 82 |
≡-dec _≟_ [] [] = yes refl |
| 83 |
≡-dec _≟_ (x ∷ xs) [] = no λ() |
| 84 |
≡-dec _≟_ [] (y ∷ ys) = no λ() |
| 85 |
≡-dec _≟_ (x ∷ xs) (y ∷ ys) = ∷-dec (x ≟ y) (≡-dec _≟_ xs ys) |
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- map |
| 89 |
|
| 90 |
map-id : map id ≗ id {A = List A} |
| 91 |
map-id [] = refl |
| 92 |
map-id (x ∷ xs) = cong (x ∷_) (map-id xs) |
| 93 |
|
| 94 |
map-id-local : ∀ {f : A → A} {xs} → All (λ x → f x ≡ x) xs → map f xs ≡ xs |
| 95 |
map-id-local [] = refl |
| 96 |
map-id-local (fx≡x ∷ pxs) = cong₂ _∷_ fx≡x (map-id-local pxs) |
| 97 |
|
| 98 |
map-++ : ∀ (f : A → B) xs ys → |
| 99 |
map f (xs ++ ys) ≡ map f xs ++ map f ys |
| 100 |
map-++ f [] ys = refl |
| 101 |
map-++ f (x ∷ xs) ys = cong (f x ∷_) (map-++ f xs ys) |
| 102 |
|
| 103 |
map-cong : ∀ {f g : A → B} → f ≗ g → map f ≗ map g |
| 104 |
map-cong f≗g [] = refl |
| 105 |
map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (map-cong f≗g xs) |
| 106 |
|
| 107 |
map-cong-local : ∀ {f g : A → B} {xs} → |
| 108 |
All (λ x → f x ≡ g x) xs → map f xs ≡ map g xs |
| 109 |
map-cong-local [] = refl |
| 110 |
map-cong-local (fx≡gx ∷ fxs≡gxs) = cong₂ _∷_ fx≡gx (map-cong-local fxs≡gxs) |
| 111 |
|
| 112 |
length-map : ∀ (f : A → B) xs → length (map f xs) ≡ length xs |
| 113 |
length-map f [] = refl |
| 114 |
length-map f (x ∷ xs) = cong suc (length-map f xs) |
| 115 |
|
| 116 |
map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f |
| 117 |
map-∘ [] = refl |
| 118 |
map-∘ (x ∷ xs) = cong (_ ∷_) (map-∘ xs) |
| 119 |
|
| 120 |
map-injective : ∀ {f : A → B} → Injective _≡_ _≡_ f → Injective _≡_ _≡_ (map f) |
| 121 |
map-injective finj {[]} {[]} eq = refl |
| 122 |
map-injective finj {x ∷ xs} {y ∷ ys} eq = |
| 123 |
let fx≡fy , fxs≡fys = ∷-injective eq in |
| 124 |
cong₂ _∷_ (finj fx≡fy) (map-injective finj fxs≡fys) |
| 125 |
|
| 126 |
------------------------------------------------------------------------ |
| 127 |
-- _++_ |
| 128 |
|
| 129 |
length-++ : ∀ (xs : List A) {ys} → |
| 130 |
length (xs ++ ys) ≡ length xs + length ys |
| 131 |
length-++ [] = refl |
| 132 |
length-++ (x ∷ xs) = cong suc (length-++ xs) |
| 133 |
|
| 134 |
module _ {A : Set a} where |
| 135 |
|
| 136 |
open AlgebraicDefinitions {A = List A} _≡_ |
| 137 |
|
| 138 |
++-assoc : Associative _++_ |
| 139 |
++-assoc [] ys zs = refl |
| 140 |
++-assoc (x ∷ xs) ys zs = cong (x ∷_) (++-assoc xs ys zs) |
| 141 |
|
| 142 |
++-identityˡ : LeftIdentity [] _++_ |
| 143 |
++-identityˡ xs = refl |
| 144 |
|
| 145 |
++-identityʳ : RightIdentity [] _++_ |
| 146 |
++-identityʳ [] = refl |
| 147 |
++-identityʳ (x ∷ xs) = cong (x ∷_) (++-identityʳ xs) |
| 148 |
|
| 149 |
++-identity : Identity [] _++_ |
| 150 |
++-identity = ++-identityˡ , ++-identityʳ |
| 151 |
|
| 152 |
++-identityʳ-unique : ∀ (xs : List A) {ys} → xs ≡ xs ++ ys → ys ≡ [] |
| 153 |
++-identityʳ-unique [] refl = refl |
| 154 |
++-identityʳ-unique (x ∷ xs) eq = |
| 155 |
++-identityʳ-unique xs (∷-injectiveʳ eq) |
| 156 |
|
| 157 |
++-identityˡ-unique : ∀ {xs} (ys : List A) → xs ≡ ys ++ xs → ys ≡ [] |
| 158 |
++-identityˡ-unique [] _ = refl |
| 159 |
++-identityˡ-unique {xs = x ∷ xs} (y ∷ ys) eq |
| 160 |
with ++-identityˡ-unique (ys ++ [ x ]) (begin |
| 161 |
xs ≡⟨ ∷-injectiveʳ eq ⟩ |
| 162 |
ys ++ x ∷ xs ≡⟨ ++-assoc ys [ x ] xs ⟨ |
| 163 |
(ys ++ [ x ]) ++ xs ∎) |
| 164 |
++-identityˡ-unique {xs = x ∷ xs} (y ∷ [] ) eq | () |
| 165 |
++-identityˡ-unique {xs = x ∷ xs} (y ∷ _ ∷ _) eq | () |
| 166 |
|
| 167 |
++-cancelˡ : LeftCancellative _++_ |
| 168 |
++-cancelˡ [] _ _ ys≡zs = ys≡zs |
| 169 |
++-cancelˡ (x ∷ xs) _ _ x∷xs++ys≡x∷xs++zs = ++-cancelˡ xs _ _ (∷-injectiveʳ x∷xs++ys≡x∷xs++zs) |
| 170 |
|
| 171 |
++-cancelʳ : RightCancellative _++_ |
| 172 |
++-cancelʳ _ [] [] _ = refl |
| 173 |
++-cancelʳ xs [] (z ∷ zs) eq = |
| 174 |
contradiction (trans (cong length eq) (length-++ (z ∷ zs))) (m≢1+n+m (length xs)) |
| 175 |
++-cancelʳ xs (y ∷ ys) [] eq = |
| 176 |
contradiction (trans (sym (length-++ (y ∷ ys))) (cong length eq)) (m≢1+n+m (length xs) ∘ sym) |
| 177 |
++-cancelʳ _ (y ∷ ys) (z ∷ zs) eq = |
| 178 |
cong₂ _∷_ (∷-injectiveˡ eq) (++-cancelʳ _ ys zs (∷-injectiveʳ eq)) |
| 179 |
|
| 180 |
++-cancel : Cancellative _++_ |
| 181 |
++-cancel = ++-cancelˡ , ++-cancelʳ |
| 182 |
|
| 183 |
++-conicalˡ : ∀ (xs ys : List A) → xs ++ ys ≡ [] → xs ≡ [] |
| 184 |
++-conicalˡ [] _ refl = refl |
| 185 |
|
| 186 |
++-conicalʳ : ∀ (xs ys : List A) → xs ++ ys ≡ [] → ys ≡ [] |
| 187 |
++-conicalʳ [] _ refl = refl |
| 188 |
|
| 189 |
++-conical : Conical [] _++_ |
| 190 |
++-conical = ++-conicalˡ , ++-conicalʳ |
| 191 |
|
| 192 |
length-++-sucˡ : ∀ (x : A) (xs ys : List A) → |
| 193 |
length (x ∷ xs ++ ys) ≡ suc (length (xs ++ ys)) |
| 194 |
length-++-sucˡ _ _ _ = refl |
| 195 |
|
| 196 |
length-++-sucʳ : ∀ (xs : List A) (y : A) (ys : List A) → |
| 197 |
length (xs ++ y ∷ ys) ≡ suc (length (xs ++ ys)) |
| 198 |
length-++-sucʳ [] _ _ = refl |
| 199 |
length-++-sucʳ (_ ∷ xs) y ys = cong suc (length-++-sucʳ xs y ys) |
| 200 |
|
| 201 |
length-++-comm : ∀ (xs ys : List A) → |
| 202 |
length (xs ++ ys) ≡ length (ys ++ xs) |
| 203 |
length-++-comm xs [] = cong length (++-identityʳ xs) |
| 204 |
length-++-comm [] (y ∷ ys) = sym (cong length (++-identityʳ (y ∷ ys))) |
| 205 |
length-++-comm (x ∷ xs) (y ∷ ys) = |
| 206 |
begin |
| 207 |
length (x ∷ xs ++ y ∷ ys) |
| 208 |
≡⟨⟩ |
| 209 |
suc (length (xs ++ y ∷ ys)) |
| 210 |
≡⟨ cong suc (length-++-sucʳ xs y ys) ⟩ |
| 211 |
suc (suc (length (xs ++ ys))) |
| 212 |
≡⟨ cong suc (cong suc (length-++-comm xs ys)) ⟩ |
| 213 |
suc (suc (length (ys ++ xs))) |
| 214 |
≡⟨ cong suc (length-++-sucʳ ys x xs) ⟨ |
| 215 |
suc (length (ys ++ x ∷ xs)) |
| 216 |
≡⟨⟩ |
| 217 |
length (y ∷ ys ++ x ∷ xs) |
| 218 |
∎ |
| 219 |
|
| 220 |
length-++-≤ˡ : ∀ (xs : List A) {ys} → |
| 221 |
length xs ≤ length (xs ++ ys) |
| 222 |
length-++-≤ˡ [] = z≤n |
| 223 |
length-++-≤ˡ (x ∷ xs) = s≤s (length-++-≤ˡ xs) |
| 224 |
|
| 225 |
length-++-≤ʳ : ∀ (ys : List A) {xs} → |
| 226 |
length ys ≤ length (xs ++ ys) |
| 227 |
length-++-≤ʳ ys {xs} = ≤-trans (length-++-≤ˡ ys) (≤-reflexive (length-++-comm ys xs)) |
| 228 |
|
| 229 |
module _ {A : Set a} where |
| 230 |
|
| 231 |
open AlgebraicStructures {A = List A} _≡_ |
| 232 |
|
| 233 |
++-isMagma : IsMagma _++_ |
| 234 |
++-isMagma = record |
| 235 |
{ isEquivalence = isEquivalence |
| 236 |
; ∙-cong = cong₂ _++_ |
| 237 |
} |
| 238 |
|
| 239 |
++-isSemigroup : IsSemigroup _++_ |
| 240 |
++-isSemigroup = record |
| 241 |
{ isMagma = ++-isMagma |
| 242 |
; assoc = ++-assoc |
| 243 |
} |
| 244 |
|
| 245 |
++-isMonoid : IsMonoid _++_ [] |
| 246 |
++-isMonoid = record |
| 247 |
{ isSemigroup = ++-isSemigroup |
| 248 |
; identity = ++-identity |
| 249 |
} |
| 250 |
|
| 251 |
module _ (A : Set a) where |
| 252 |
|
| 253 |
++-semigroup : Semigroup a a |
| 254 |
++-semigroup = record |
| 255 |
{ Carrier = List A |
| 256 |
; isSemigroup = ++-isSemigroup |
| 257 |
} |
| 258 |
|
| 259 |
++-monoid : Monoid a a |
| 260 |
++-monoid = record |
| 261 |
{ Carrier = List A |
| 262 |
; isMonoid = ++-isMonoid |
| 263 |
} |
| 264 |
|
| 265 |
module _ (A : Set a) where |
| 266 |
|
| 267 |
length-isMagmaHomomorphism : IsMagmaHomomorphism (++-rawMagma A) +-rawMagma length |
| 268 |
length-isMagmaHomomorphism = record |
| 269 |
{ isRelHomomorphism = record |
| 270 |
{ cong = cong length |
| 271 |
} |
| 272 |
; homo = λ xs ys → length-++ xs {ys} |
| 273 |
} |
| 274 |
|
| 275 |
length-isMonoidHomomorphism : IsMonoidHomomorphism (++-[]-rawMonoid A) +-0-rawMonoid length |
| 276 |
length-isMonoidHomomorphism = record |
| 277 |
{ isMagmaHomomorphism = length-isMagmaHomomorphism |
| 278 |
; ε-homo = refl |
| 279 |
} |
| 280 |
|
| 281 |
------------------------------------------------------------------------ |
| 282 |
-- cartesianProductWith |
| 283 |
|
| 284 |
module _ (f : A → B → C) where |
| 285 |
|
| 286 |
private |
| 287 |
prod = cartesianProductWith f |
| 288 |
|
| 289 |
cartesianProductWith-zeroˡ : ∀ ys → prod [] ys ≡ [] |
| 290 |
cartesianProductWith-zeroˡ _ = refl |
| 291 |
|
| 292 |
cartesianProductWith-zeroʳ : ∀ xs → prod xs [] ≡ [] |
| 293 |
cartesianProductWith-zeroʳ [] = refl |
| 294 |
cartesianProductWith-zeroʳ (x ∷ xs) = cartesianProductWith-zeroʳ xs |
| 295 |
|
| 296 |
cartesianProductWith-distribʳ-++ : ∀ xs ys zs → prod (xs ++ ys) zs ≡ prod xs zs ++ prod ys zs |
| 297 |
cartesianProductWith-distribʳ-++ [] ys zs = refl |
| 298 |
cartesianProductWith-distribʳ-++ (x ∷ xs) ys zs = begin |
| 299 |
prod (x ∷ xs ++ ys) zs ≡⟨⟩ |
| 300 |
map (f x) zs ++ prod (xs ++ ys) zs ≡⟨ cong (map (f x) zs ++_) (cartesianProductWith-distribʳ-++ xs ys zs) ⟩ |
| 301 |
map (f x) zs ++ prod xs zs ++ prod ys zs ≡⟨ ++-assoc (map (f x) zs) (prod xs zs) (prod ys zs) ⟨ |
| 302 |
(map (f x) zs ++ prod xs zs) ++ prod ys zs ≡⟨⟩ |
| 303 |
prod (x ∷ xs) zs ++ prod ys zs ∎ |
| 304 |
|
| 305 |
------------------------------------------------------------------------ |
| 306 |
-- alignWith |
| 307 |
|
| 308 |
module _ {f g : These A B → C} where |
| 309 |
|
| 310 |
alignWith-cong : f ≗ g → ∀ as → alignWith f as ≗ alignWith g as |
| 311 |
alignWith-cong f≗g [] bs = map-cong (f≗g ∘ that) bs |
| 312 |
alignWith-cong f≗g as@(_ ∷ _) [] = map-cong (f≗g ∘ this) as |
| 313 |
alignWith-cong f≗g (a ∷ as) (b ∷ bs) = |
| 314 |
cong₂ _∷_ (f≗g (these a b)) (alignWith-cong f≗g as bs) |
| 315 |
|
| 316 |
module _ {f : These A B → C} where |
| 317 |
|
| 318 |
length-alignWith : ∀ xs ys → |
| 319 |
length (alignWith f xs ys) ≡ length xs ⊔ length ys |
| 320 |
length-alignWith [] ys = length-map (f ∘′ that) ys |
| 321 |
length-alignWith xs@(_ ∷ _) [] = length-map (f ∘′ this) xs |
| 322 |
length-alignWith (x ∷ xs) (y ∷ ys) = cong suc (length-alignWith xs ys) |
| 323 |
|
| 324 |
alignWith-map : (g : D → A) (h : E → B) → |
| 325 |
∀ xs ys → alignWith f (map g xs) (map h ys) ≡ |
| 326 |
alignWith (f ∘′ These.map g h) xs ys |
| 327 |
alignWith-map g h [] ys = sym (map-∘ ys) |
| 328 |
alignWith-map g h xs@(_ ∷ _) [] = sym (map-∘ xs) |
| 329 |
alignWith-map g h (x ∷ xs) (y ∷ ys) = |
| 330 |
cong₂ _∷_ refl (alignWith-map g h xs ys) |
| 331 |
|
| 332 |
map-alignWith : ∀ (g : C → D) → ∀ xs ys → |
| 333 |
map g (alignWith f xs ys) ≡ |
| 334 |
alignWith (g ∘′ f) xs ys |
| 335 |
map-alignWith g [] ys = sym (map-∘ ys) |
| 336 |
map-alignWith g xs@(_ ∷ _) [] = sym (map-∘ xs) |
| 337 |
map-alignWith g (x ∷ xs) (y ∷ ys) = |
| 338 |
cong₂ _∷_ refl (map-alignWith g xs ys) |
| 339 |
|
| 340 |
alignWith-flip : ∀ xs ys → |
| 341 |
alignWith f xs ys ≡ alignWith (f ∘ These.swap) ys xs |
| 342 |
alignWith-flip [] [] = refl |
| 343 |
alignWith-flip [] (y ∷ ys) = refl |
| 344 |
alignWith-flip (x ∷ xs) [] = refl |
| 345 |
alignWith-flip (x ∷ xs) (y ∷ ys) = cong (_ ∷_) (alignWith-flip xs ys) |
| 346 |
|
| 347 |
module _ {f : These A A → B} where |
| 348 |
|
| 349 |
alignWith-comm : f ∘ These.swap ≗ f → |
| 350 |
∀ xs ys → alignWith f xs ys ≡ alignWith f ys xs |
| 351 |
alignWith-comm f-comm xs ys = begin |
| 352 |
alignWith f xs ys ≡⟨ alignWith-flip xs ys ⟩ |
| 353 |
alignWith (f ∘ These.swap) ys xs ≡⟨ alignWith-cong f-comm ys xs ⟩ |
| 354 |
alignWith f ys xs ∎ |
| 355 |
|
| 356 |
------------------------------------------------------------------------ |
| 357 |
-- align |
| 358 |
|
| 359 |
align-map : ∀ (f : A → B) (g : C → D) → |
| 360 |
∀ xs ys → align (map f xs) (map g ys) ≡ |
| 361 |
map (These.map f g) (align xs ys) |
| 362 |
align-map f g xs ys = begin |
| 363 |
align (map f xs) (map g ys) ≡⟨ alignWith-map f g xs ys ⟩ |
| 364 |
alignWith (These.map f g) xs ys ≡⟨ sym (map-alignWith (These.map f g) xs ys) ⟩ |
| 365 |
map (These.map f g) (align xs ys) ∎ |
| 366 |
|
| 367 |
align-flip : ∀ (xs : List A) (ys : List B) → |
| 368 |
align xs ys ≡ map These.swap (align ys xs) |
| 369 |
align-flip xs ys = begin |
| 370 |
align xs ys ≡⟨ alignWith-flip xs ys ⟩ |
| 371 |
alignWith These.swap ys xs ≡⟨ sym (map-alignWith These.swap ys xs) ⟩ |
| 372 |
map These.swap (align ys xs) ∎ |
| 373 |
|
| 374 |
------------------------------------------------------------------------ |
| 375 |
-- zipWith |
| 376 |
|
| 377 |
module _ {f g : A → B → C} where |
| 378 |
|
| 379 |
zipWith-cong : (∀ a b → f a b ≡ g a b) → ∀ as → zipWith f as ≗ zipWith g as |
| 380 |
zipWith-cong f≗g [] bs = refl |
| 381 |
zipWith-cong f≗g as@(_ ∷ _) [] = refl |
| 382 |
zipWith-cong f≗g (a ∷ as) (b ∷ bs) = |
| 383 |
cong₂ _∷_ (f≗g a b) (zipWith-cong f≗g as bs) |
| 384 |
|
| 385 |
module _ (f : A → B → C) where |
| 386 |
|
| 387 |
zipWith-zeroˡ : ∀ xs → zipWith f [] xs ≡ [] |
| 388 |
zipWith-zeroˡ [] = refl |
| 389 |
zipWith-zeroˡ (x ∷ xs) = refl |
| 390 |
|
| 391 |
zipWith-zeroʳ : ∀ xs → zipWith f xs [] ≡ [] |
| 392 |
zipWith-zeroʳ [] = refl |
| 393 |
zipWith-zeroʳ (x ∷ xs) = refl |
| 394 |
|
| 395 |
length-zipWith : ∀ xs ys → |
| 396 |
length (zipWith f xs ys) ≡ length xs ⊓ length ys |
| 397 |
length-zipWith [] [] = refl |
| 398 |
length-zipWith [] (y ∷ ys) = refl |
| 399 |
length-zipWith (x ∷ xs) [] = refl |
| 400 |
length-zipWith (x ∷ xs) (y ∷ ys) = cong suc (length-zipWith xs ys) |
| 401 |
|
| 402 |
zipWith-map : ∀ (g : D → A) (h : E → B) → |
| 403 |
∀ xs ys → zipWith f (map g xs) (map h ys) ≡ |
| 404 |
zipWith (λ x y → f (g x) (h y)) xs ys |
| 405 |
zipWith-map g h [] [] = refl |
| 406 |
zipWith-map g h [] (y ∷ ys) = refl |
| 407 |
zipWith-map g h (x ∷ xs) [] = refl |
| 408 |
zipWith-map g h (x ∷ xs) (y ∷ ys) = |
| 409 |
cong₂ _∷_ refl (zipWith-map g h xs ys) |
| 410 |
|
| 411 |
map-zipWith : ∀ (g : C → D) → ∀ xs ys → |
| 412 |
map g (zipWith f xs ys) ≡ |
| 413 |
zipWith (λ x y → g (f x y)) xs ys |
| 414 |
map-zipWith g [] [] = refl |
| 415 |
map-zipWith g [] (y ∷ ys) = refl |
| 416 |
map-zipWith g (x ∷ xs) [] = refl |
| 417 |
map-zipWith g (x ∷ xs) (y ∷ ys) = |
| 418 |
cong₂ _∷_ refl (map-zipWith g xs ys) |
| 419 |
|
| 420 |
zipWith-flip : ∀ xs ys → zipWith f xs ys ≡ zipWith (flip f) ys xs |
| 421 |
zipWith-flip [] [] = refl |
| 422 |
zipWith-flip [] (x ∷ ys) = refl |
| 423 |
zipWith-flip (x ∷ xs) [] = refl |
| 424 |
zipWith-flip (x ∷ xs) (y ∷ ys) = cong (f x y ∷_) (zipWith-flip xs ys) |
| 425 |
|
| 426 |
module _ (f : A → A → B) where |
| 427 |
|
| 428 |
zipWith-comm : (∀ x y → f x y ≡ f y x) → |
| 429 |
∀ xs ys → zipWith f xs ys ≡ zipWith f ys xs |
| 430 |
zipWith-comm f-comm xs ys = begin |
| 431 |
zipWith f xs ys ≡⟨ zipWith-flip f xs ys ⟩ |
| 432 |
zipWith (flip f) ys xs ≡⟨ zipWith-cong (flip f-comm) ys xs ⟩ |
| 433 |
zipWith f ys xs ∎ |
| 434 |
|
| 435 |
------------------------------------------------------------------------ |
| 436 |
-- zip |
| 437 |
|
| 438 |
zip-map : ∀ (f : A → B) (g : C → D) → |
| 439 |
∀ xs ys → zip (map f xs) (map g ys) ≡ |
| 440 |
map (Product.map f g) (zip xs ys) |
| 441 |
zip-map f g xs ys = begin |
| 442 |
zip (map f xs) (map g ys) ≡⟨ zipWith-map _,_ f g xs ys ⟩ |
| 443 |
zipWith (λ x y → f x , g y) xs ys ≡⟨ sym (map-zipWith _,_ (Product.map f g) xs ys) ⟩ |
| 444 |
map (Product.map f g) (zip xs ys) ∎ |
| 445 |
|
| 446 |
zip-flip : ∀ (xs : List A) (ys : List B) → |
| 447 |
zip xs ys ≡ map Product.swap (zip ys xs) |
| 448 |
zip-flip xs ys = begin |
| 449 |
zip xs ys ≡⟨ zipWith-flip _,_ xs ys ⟩ |
| 450 |
zipWith (flip _,_) ys xs ≡⟨ sym (map-zipWith _,_ Product.swap ys xs) ⟩ |
| 451 |
map Product.swap (zip ys xs) ∎ |
| 452 |
|
| 453 |
------------------------------------------------------------------------ |
| 454 |
-- unalignWith |
| 455 |
|
| 456 |
unalignWith-this : unalignWith ((A → These A B) ∋ this) ≗ (_, []) |
| 457 |
unalignWith-this [] = refl |
| 458 |
unalignWith-this (a ∷ as) = cong (Product.map₁ (a ∷_)) (unalignWith-this as) |
| 459 |
|
| 460 |
unalignWith-that : unalignWith ((B → These A B) ∋ that) ≗ ([] ,_) |
| 461 |
unalignWith-that [] = refl |
| 462 |
unalignWith-that (b ∷ bs) = cong (Product.map₂ (b ∷_)) (unalignWith-that bs) |
| 463 |
|
| 464 |
module _ {f g : C → These A B} where |
| 465 |
|
| 466 |
unalignWith-cong : f ≗ g → unalignWith f ≗ unalignWith g |
| 467 |
unalignWith-cong f≗g [] = refl |
| 468 |
unalignWith-cong f≗g (c ∷ cs) with f c | g c | f≗g c |
| 469 |
... | this a | ._ | refl = cong (Product.map₁ (a ∷_)) (unalignWith-cong f≗g cs) |
| 470 |
... | that b | ._ | refl = cong (Product.map₂ (b ∷_)) (unalignWith-cong f≗g cs) |
| 471 |
... | these a b | ._ | refl = cong (Product.map (a ∷_) (b ∷_)) (unalignWith-cong f≗g cs) |
| 472 |
|
| 473 |
module _ (f : C → These A B) where |
| 474 |
|
| 475 |
unalignWith-map : (g : D → C) → ∀ ds → |
| 476 |
unalignWith f (map g ds) ≡ unalignWith (f ∘′ g) ds |
| 477 |
unalignWith-map g [] = refl |
| 478 |
unalignWith-map g (d ∷ ds) with f (g d) |
| 479 |
... | this a = cong (Product.map₁ (a ∷_)) (unalignWith-map g ds) |
| 480 |
... | that b = cong (Product.map₂ (b ∷_)) (unalignWith-map g ds) |
| 481 |
... | these a b = cong (Product.map (a ∷_) (b ∷_)) (unalignWith-map g ds) |
| 482 |
|
| 483 |
map-unalignWith : (g : A → D) (h : B → E) → |
| 484 |
Product.map (map g) (map h) ∘′ unalignWith f ≗ unalignWith (These.map g h ∘′ f) |
| 485 |
map-unalignWith g h [] = refl |
| 486 |
map-unalignWith g h (c ∷ cs) with f c |
| 487 |
... | this a = cong (Product.map₁ (g a ∷_)) (map-unalignWith g h cs) |
| 488 |
... | that b = cong (Product.map₂ (h b ∷_)) (map-unalignWith g h cs) |
| 489 |
... | these a b = cong (Product.map (g a ∷_) (h b ∷_)) (map-unalignWith g h cs) |
| 490 |
|
| 491 |
unalignWith-alignWith : (g : These A B → C) → f ∘′ g ≗ id → ∀ as bs → |
| 492 |
unalignWith f (alignWith g as bs) ≡ (as , bs) |
| 493 |
unalignWith-alignWith g g∘f≗id [] bs = begin |
| 494 |
unalignWith f (map (g ∘′ that) bs) ≡⟨ unalignWith-map (g ∘′ that) bs ⟩ |
| 495 |
unalignWith (f ∘′ g ∘′ that) bs ≡⟨ unalignWith-cong (g∘f≗id ∘ that) bs ⟩ |
| 496 |
unalignWith that bs ≡⟨ unalignWith-that bs ⟩ |
| 497 |
[] , bs ∎ |
| 498 |
unalignWith-alignWith g g∘f≗id as@(_ ∷ _) [] = begin |
| 499 |
unalignWith f (map (g ∘′ this) as) ≡⟨ unalignWith-map (g ∘′ this) as ⟩ |
| 500 |
unalignWith (f ∘′ g ∘′ this) as ≡⟨ unalignWith-cong (g∘f≗id ∘ this) as ⟩ |
| 501 |
unalignWith this as ≡⟨ unalignWith-this as ⟩ |
| 502 |
as , [] ∎ |
| 503 |
unalignWith-alignWith g g∘f≗id (a ∷ as) (b ∷ bs) |
| 504 |
rewrite g∘f≗id (these a b) = |
| 505 |
cong (Product.map (a ∷_) (b ∷_)) (unalignWith-alignWith g g∘f≗id as bs) |
| 506 |
|
| 507 |
------------------------------------------------------------------------ |
| 508 |
-- unzipWith |
| 509 |
|
| 510 |
module _ {f g : A → B × C} where |
| 511 |
|
| 512 |
unzipWith-cong : f ≗ g → unzipWith f ≗ unzipWith g |
| 513 |
unzipWith-cong f≗g [] = refl |
| 514 |
unzipWith-cong f≗g (x ∷ xs) = |
| 515 |
cong₂ (Product.zip _∷_ _∷_) (f≗g x) (unzipWith-cong f≗g xs) |
| 516 |
|
| 517 |
module _ (f : A → B × C) where |
| 518 |
|
| 519 |
length-unzipWith₁ : ∀ xys → |
| 520 |
length (proj₁ (unzipWith f xys)) ≡ length xys |
| 521 |
length-unzipWith₁ [] = refl |
| 522 |
length-unzipWith₁ (x ∷ xys) = cong suc (length-unzipWith₁ xys) |
| 523 |
|
| 524 |
length-unzipWith₂ : ∀ xys → |
| 525 |
length (proj₂ (unzipWith f xys)) ≡ length xys |
| 526 |
length-unzipWith₂ [] = refl |
| 527 |
length-unzipWith₂ (x ∷ xys) = cong suc (length-unzipWith₂ xys) |
| 528 |
|
| 529 |
zipWith-unzipWith : (g : B → C → A) → uncurry′ g ∘ f ≗ id → |
| 530 |
uncurry′ (zipWith g) ∘ (unzipWith f) ≗ id |
| 531 |
zipWith-unzipWith g g∘f≗id [] = refl |
| 532 |
zipWith-unzipWith g g∘f≗id (x ∷ xs) = |
| 533 |
cong₂ _∷_ (g∘f≗id x) (zipWith-unzipWith g g∘f≗id xs) |
| 534 |
|
| 535 |
unzipWith-zipWith : (g : B → C → A) → f ∘ uncurry′ g ≗ id → |
| 536 |
∀ xs ys → length xs ≡ length ys → |
| 537 |
unzipWith f (zipWith g xs ys) ≡ (xs , ys) |
| 538 |
unzipWith-zipWith g f∘g≗id [] [] l≡l = refl |
| 539 |
unzipWith-zipWith g f∘g≗id (x ∷ xs) (y ∷ ys) l≡l = |
| 540 |
cong₂ (Product.zip _∷_ _∷_) (f∘g≗id (x , y)) |
| 541 |
(unzipWith-zipWith g f∘g≗id xs ys (suc-injective l≡l)) |
| 542 |
|
| 543 |
unzipWith-map : (g : D → A) → unzipWith f ∘ map g ≗ unzipWith (f ∘ g) |
| 544 |
unzipWith-map g [] = refl |
| 545 |
unzipWith-map g (x ∷ xs) = |
| 546 |
cong (Product.zip _∷_ _∷_ (f (g x))) (unzipWith-map g xs) |
| 547 |
|
| 548 |
map-unzipWith : (g : B → D) (h : C → E) → |
| 549 |
Product.map (map g) (map h) ∘ unzipWith f ≗ |
| 550 |
unzipWith (Product.map g h ∘ f) |
| 551 |
map-unzipWith g h [] = refl |
| 552 |
map-unzipWith g h (x ∷ xs) = |
| 553 |
cong (Product.zip _∷_ _∷_ _) (map-unzipWith g h xs) |
| 554 |
|
| 555 |
unzipWith-swap : unzipWith (Product.swap ∘ f) ≗ |
| 556 |
Product.swap ∘ unzipWith f |
| 557 |
unzipWith-swap [] = refl |
| 558 |
unzipWith-swap (x ∷ xs) = |
| 559 |
cong (Product.zip _∷_ _∷_ _) (unzipWith-swap xs) |
| 560 |
|
| 561 |
unzipWith-++ : ∀ xs ys → |
| 562 |
unzipWith f (xs ++ ys) ≡ |
| 563 |
Product.zip _++_ _++_ (unzipWith f xs) (unzipWith f ys) |
| 564 |
unzipWith-++ [] ys = refl |
| 565 |
unzipWith-++ (x ∷ xs) ys = |
| 566 |
cong (Product.zip _∷_ _∷_ (f x)) (unzipWith-++ xs ys) |
| 567 |
|
| 568 |
------------------------------------------------------------------------ |
| 569 |
-- unzip |
| 570 |
|
| 571 |
unzip-map : ∀ (f : A → B) (g : C → D) → |
| 572 |
unzip ∘ map (Product.map f g) ≗ |
| 573 |
Product.map (map f) (map g) ∘ unzip |
| 574 |
unzip-map f g xs = begin |
| 575 |
unzip (map (Product.map f g) xs) ≡⟨ unzipWith-map id (Product.map f g) xs ⟩ |
| 576 |
unzipWith (Product.map f g) xs ≡⟨ sym (map-unzipWith id f g xs) ⟩ |
| 577 |
Product.map (map f) (map g) (unzip xs) ∎ |
| 578 |
|
| 579 |
unzip-swap : unzip ∘ map Product.swap ≗ Product.swap ∘ unzip {A = A} {B = B} |
| 580 |
unzip-swap xs = begin |
| 581 |
unzip (map Product.swap xs) ≡⟨ unzipWith-map id Product.swap xs ⟩ |
| 582 |
unzipWith Product.swap xs ≡⟨ unzipWith-swap id xs ⟩ |
| 583 |
Product.swap (unzip xs) ∎ |
| 584 |
|
| 585 |
zip-unzip : uncurry′ zip ∘ unzip ≗ id {A = List (A × B)} |
| 586 |
zip-unzip = zipWith-unzipWith id _,_ λ _ → refl |
| 587 |
|
| 588 |
unzip-zip : ∀ (xs : List A) (ys : List B) → |
| 589 |
length xs ≡ length ys → unzip (zip xs ys) ≡ (xs , ys) |
| 590 |
unzip-zip = unzipWith-zipWith id _,_ λ _ → refl |
| 591 |
|
| 592 |
------------------------------------------------------------------------ |
| 593 |
-- foldr |
| 594 |
|
| 595 |
foldr-universal : ∀ (h : List A → B) f e → (h [] ≡ e) → |
| 596 |
(∀ x xs → h (x ∷ xs) ≡ f x (h xs)) → |
| 597 |
h ≗ foldr f e |
| 598 |
foldr-universal h f e base step [] = base |
| 599 |
foldr-universal h f e base step (x ∷ xs) = begin |
| 600 |
h (x ∷ xs) ≡⟨ step x xs ⟩ |
| 601 |
f x (h xs) ≡⟨ cong (f x) (foldr-universal h f e base step xs) ⟩ |
| 602 |
f x (foldr f e xs) ∎ |
| 603 |
|
| 604 |
foldr-cong : ∀ {f g : A → B → B} {d e : B} → |
| 605 |
(∀ x y → f x y ≡ g x y) → d ≡ e → |
| 606 |
foldr f d ≗ foldr g e |
| 607 |
foldr-cong f≗g refl [] = refl |
| 608 |
foldr-cong f≗g d≡e (x ∷ xs) rewrite foldr-cong f≗g d≡e xs = f≗g x _ |
| 609 |
|
| 610 |
foldr-fusion : ∀ (h : B → C) {f : A → B → B} {g : A → C → C} (e : B) → |
| 611 |
(∀ x y → h (f x y) ≡ g x (h y)) → |
| 612 |
h ∘ foldr f e ≗ foldr g (h e) |
| 613 |
foldr-fusion h {f} {g} e fuse = |
| 614 |
foldr-universal (h ∘ foldr f e) g (h e) refl |
| 615 |
(λ x xs → fuse x (foldr f e xs)) |
| 616 |
|
| 617 |
id-is-foldr : id {A = List A} ≗ foldr _∷_ [] |
| 618 |
id-is-foldr = foldr-universal id _∷_ [] refl (λ _ _ → refl) |
| 619 |
|
| 620 |
++-is-foldr : (xs ys : List A) → xs ++ ys ≡ foldr _∷_ ys xs |
| 621 |
++-is-foldr xs ys = begin |
| 622 |
xs ++ ys ≡⟨ cong (_++ ys) (id-is-foldr xs) ⟩ |
| 623 |
foldr _∷_ [] xs ++ ys ≡⟨ foldr-fusion (_++ ys) [] (λ _ _ → refl) xs ⟩ |
| 624 |
foldr _∷_ ([] ++ ys) xs ≡⟨⟩ |
| 625 |
foldr _∷_ ys xs ∎ |
| 626 |
|
| 627 |
foldr-++ : ∀ (f : A → B → B) x ys zs → |
| 628 |
foldr f x (ys ++ zs) ≡ foldr f (foldr f x zs) ys |
| 629 |
foldr-++ f x [] zs = refl |
| 630 |
foldr-++ f x (y ∷ ys) zs = cong (f y) (foldr-++ f x ys zs) |
| 631 |
|
| 632 |
map-is-foldr : {f : A → B} → map f ≗ foldr (λ x ys → f x ∷ ys) [] |
| 633 |
map-is-foldr {f = f} xs = begin |
| 634 |
map f xs ≡⟨ cong (map f) (id-is-foldr xs) ⟩ |
| 635 |
map f (foldr _∷_ [] xs) ≡⟨ foldr-fusion (map f) [] (λ _ _ → refl) xs ⟩ |
| 636 |
foldr (λ x ys → f x ∷ ys) [] xs ∎ |
| 637 |
|
| 638 |
foldr-∷ʳ : ∀ (f : A → B → B) x y ys → |
| 639 |
foldr f x (ys ∷ʳ y) ≡ foldr f (f y x) ys |
| 640 |
foldr-∷ʳ f x y [] = refl |
| 641 |
foldr-∷ʳ f x y (z ∷ ys) = cong (f z) (foldr-∷ʳ f x y ys) |
| 642 |
|
| 643 |
foldr-map : ∀ (f : A → B → B) (g : C → A) x xs → foldr f x (map g xs) ≡ foldr (g -⟨ f ∣) x xs |
| 644 |
foldr-map f g x [] = refl |
| 645 |
foldr-map f g x (y ∷ xs) = cong (f (g y)) (foldr-map f g x xs) |
| 646 |
|
| 647 |
-- Interaction with predicates |
| 648 |
|
| 649 |
module _ {P : Pred A p} {f : A → A → A} where |
| 650 |
|
| 651 |
foldr-forcesᵇ : (∀ x y → P (f x y) → P x × P y) → |
| 652 |
∀ e xs → P (foldr f e xs) → All P xs |
| 653 |
foldr-forcesᵇ _ _ [] _ = [] |
| 654 |
foldr-forcesᵇ forces _ (x ∷ xs) Pfold = |
| 655 |
let px , pfxs = forces _ _ Pfold in px ∷ foldr-forcesᵇ forces _ xs pfxs |
| 656 |
|
| 657 |
foldr-preservesᵇ : (∀ {x y} → P x → P y → P (f x y)) → |
| 658 |
∀ {e xs} → P e → All P xs → P (foldr f e xs) |
| 659 |
foldr-preservesᵇ _ Pe [] = Pe |
| 660 |
foldr-preservesᵇ pres Pe (px ∷ pxs) = pres px (foldr-preservesᵇ pres Pe pxs) |
| 661 |
|
| 662 |
foldr-preservesʳ : (∀ x {y} → P y → P (f x y)) → |
| 663 |
∀ {e} → P e → ∀ xs → P (foldr f e xs) |
| 664 |
foldr-preservesʳ pres Pe [] = Pe |
| 665 |
foldr-preservesʳ pres Pe (_ ∷ xs) = pres _ (foldr-preservesʳ pres Pe xs) |
| 666 |
|
| 667 |
foldr-preservesᵒ : (∀ x y → P x ⊎ P y → P (f x y)) → |
| 668 |
∀ e xs → P e ⊎ Any P xs → P (foldr f e xs) |
| 669 |
foldr-preservesᵒ pres e [] (inj₁ Pe) = Pe |
| 670 |
foldr-preservesᵒ pres e (x ∷ xs) (inj₁ Pe) = |
| 671 |
pres _ _ (inj₂ (foldr-preservesᵒ pres e xs (inj₁ Pe))) |
| 672 |
foldr-preservesᵒ pres e (x ∷ xs) (inj₂ (here px)) = pres _ _ (inj₁ px) |
| 673 |
foldr-preservesᵒ pres e (x ∷ xs) (inj₂ (there pxs)) = |
| 674 |
pres _ _ (inj₂ (foldr-preservesᵒ pres e xs (inj₂ pxs))) |
| 675 |
|
| 676 |
------------------------------------------------------------------------ |
| 677 |
-- foldl |
| 678 |
|
| 679 |
foldl-cong : ∀ {f g : B → A → B} → (∀ x y → f x y ≡ g x y) → |
| 680 |
∀ x → foldl f x ≗ foldl g x |
| 681 |
foldl-cong f≗g x [] = refl |
| 682 |
foldl-cong f≗g x (y ∷ xs) rewrite f≗g x y = foldl-cong f≗g _ xs |
| 683 |
|
| 684 |
foldl-++ : ∀ (f : A → B → A) x ys zs → |
| 685 |
foldl f x (ys ++ zs) ≡ foldl f (foldl f x ys) zs |
| 686 |
foldl-++ f x [] zs = refl |
| 687 |
foldl-++ f x (y ∷ ys) zs = foldl-++ f (f x y) ys zs |
| 688 |
|
| 689 |
foldl-∷ʳ : ∀ (f : A → B → A) x y ys → |
| 690 |
foldl f x (ys ∷ʳ y) ≡ f (foldl f x ys) y |
| 691 |
foldl-∷ʳ f x y [] = refl |
| 692 |
foldl-∷ʳ f x y (z ∷ ys) = foldl-∷ʳ f (f x z) y ys |
| 693 |
|
| 694 |
foldl-map : ∀ (f : A → B → A) (g : C → B) x xs → foldl f x (map g xs) ≡ foldl (∣ f ⟩- g) x xs |
| 695 |
foldl-map f g x [] = refl |
| 696 |
foldl-map f g x (y ∷ xs) = foldl-map f g (f x (g y)) xs |
| 697 |
|
| 698 |
------------------------------------------------------------------------ |
| 699 |
-- concat |
| 700 |
|
| 701 |
concat-map : ∀ {f : A → B} → concat ∘ map (map f) ≗ map f ∘ concat |
| 702 |
concat-map {f = f} xss = begin |
| 703 |
concat (map (map f) xss) ≡⟨ cong concat (map-is-foldr xss) ⟩ |
| 704 |
concat (foldr (λ xs → map f xs ∷_) [] xss) ≡⟨ foldr-fusion concat [] (λ _ _ → refl) xss ⟩ |
| 705 |
foldr (λ ys → map f ys ++_) [] xss ≡⟨ sym (foldr-fusion (map f) [] (map-++ f) xss) ⟩ |
| 706 |
map f (concat xss) ∎ |
| 707 |
|
| 708 |
concat-++ : (xss yss : List (List A)) → concat xss ++ concat yss ≡ concat (xss ++ yss) |
| 709 |
concat-++ [] yss = refl |
| 710 |
concat-++ ([] ∷ xss) yss = concat-++ xss yss |
| 711 |
concat-++ ((x ∷ xs) ∷ xss) yss = cong (x ∷_) (concat-++ (xs ∷ xss) yss) |
| 712 |
|
| 713 |
concat-concat : concat {A = A} ∘ map concat ≗ concat ∘ concat |
| 714 |
concat-concat [] = refl |
| 715 |
concat-concat (xss ∷ xsss) = begin |
| 716 |
concat (map concat (xss ∷ xsss)) ≡⟨ cong (concat xss ++_) (concat-concat xsss) ⟩ |
| 717 |
concat xss ++ concat (concat xsss) ≡⟨ concat-++ xss (concat xsss) ⟩ |
| 718 |
concat (concat (xss ∷ xsss)) ∎ |
| 719 |
|
| 720 |
concat-map-[_] : concat {A = A} ∘ map [_] ≗ id |
| 721 |
concat-map-[ [] ] = refl |
| 722 |
concat-map-[ x ∷ xs ] = cong (x ∷_) (concat-map-[ xs ]) |
| 723 |
|
| 724 |
concat-[_] : concat {A = A} ∘ [_] ≗ id |
| 725 |
concat-[ xs ] = ++-identityʳ xs |
| 726 |
|
| 727 |
------------------------------------------------------------------------ |
| 728 |
-- concatMap |
| 729 |
|
| 730 |
concatMap-cong : ∀ {f g : A → List B} → f ≗ g → concatMap f ≗ concatMap g |
| 731 |
concatMap-cong eq = cong concat ∘ map-cong eq |
| 732 |
|
| 733 |
concatMap-pure : concatMap {A = A} [_] ≗ id |
| 734 |
concatMap-pure = concat-map-[_] |
| 735 |
|
| 736 |
concatMap-map : (g : B → List C) → (f : A → B) → |
| 737 |
concatMap g ∘′ (map f) ≗ concatMap (g ∘′ f) |
| 738 |
concatMap-map g f = cong concat ∘ sym ∘ map-∘ |
| 739 |
|
| 740 |
map-concatMap : (f : B → C) (g : A → List B) → |
| 741 |
map f ∘′ concatMap g ≗ concatMap (map f ∘′ g) |
| 742 |
map-concatMap f g xs = begin |
| 743 |
map f (concatMap g xs) |
| 744 |
≡⟨⟩ |
| 745 |
map f (concat (map g xs)) |
| 746 |
≡⟨ concat-map (map g xs) ⟨ |
| 747 |
concat (map (map f) (map g xs)) |
| 748 |
≡⟨ concatMap-map (map f) g xs ⟩ |
| 749 |
concat (map (map f ∘′ g) xs) |
| 750 |
≡⟨⟩ |
| 751 |
concatMap (map f ∘′ g) xs |
| 752 |
∎ |
| 753 |
|
| 754 |
concatMap-++ : ∀ (f : A → List B) xs ys → |
| 755 |
concatMap f (xs ++ ys) ≡ concatMap f xs ++ concatMap f ys |
| 756 |
concatMap-++ f xs ys = begin |
| 757 |
concatMap f (xs ++ ys) ≡⟨⟩ |
| 758 |
concat (map f (xs ++ ys)) ≡⟨ cong concat $ map-++ f xs ys ⟩ |
| 759 |
concat (map f xs ++ map f ys) ≡⟨ concat-++ (map f xs) (map f ys) ⟨ |
| 760 |
concatMap f xs ++ concatMap f ys ∎ where open ≡-Reasoning |
| 761 |
|
| 762 |
------------------------------------------------------------------------ |
| 763 |
-- catMaybes |
| 764 |
|
| 765 |
catMaybes-concatMap : catMaybes {A = A} ≗ concatMap fromMaybe |
| 766 |
catMaybes-concatMap [] = refl |
| 767 |
catMaybes-concatMap (just x ∷ xs) = cong (x ∷_) $ catMaybes-concatMap xs |
| 768 |
catMaybes-concatMap (nothing ∷ xs) = catMaybes-concatMap xs |
| 769 |
|
| 770 |
length-catMaybes : ∀ xs → length (catMaybes {A = A} xs) ≤ length xs |
| 771 |
length-catMaybes [] = ≤-refl |
| 772 |
length-catMaybes (just _ ∷ xs) = s≤s $ length-catMaybes xs |
| 773 |
length-catMaybes (nothing ∷ xs) = m≤n⇒m≤1+n $ length-catMaybes xs |
| 774 |
|
| 775 |
catMaybes-++ : (xs ys : List (Maybe A)) → |
| 776 |
catMaybes (xs ++ ys) ≡ catMaybes xs ++ catMaybes ys |
| 777 |
catMaybes-++ [] _ = refl |
| 778 |
catMaybes-++ (just x ∷ xs) ys = cong (x ∷_) $ catMaybes-++ xs ys |
| 779 |
catMaybes-++ (nothing ∷ xs) ys = catMaybes-++ xs ys |
| 780 |
|
| 781 |
map-catMaybes : (f : A → B) → map f ∘ catMaybes ≗ catMaybes ∘ map (Maybe.map f) |
| 782 |
map-catMaybes _ [] = refl |
| 783 |
map-catMaybes f (just x ∷ xs) = cong (f x ∷_) $ map-catMaybes f xs |
| 784 |
map-catMaybes f (nothing ∷ xs) = map-catMaybes f xs |
| 785 |
|
| 786 |
Any-catMaybes⁺ : ∀ {P : Pred A ℓ} {xs : List (Maybe A)} → |
| 787 |
Any (MAny P) xs → Any P (catMaybes xs) |
| 788 |
Any-catMaybes⁺ {xs = nothing ∷ xs} (there x∈) = Any-catMaybes⁺ x∈ |
| 789 |
Any-catMaybes⁺ {xs = just x ∷ xs} (here (just px)) = here px |
| 790 |
Any-catMaybes⁺ {xs = just x ∷ xs} (there x∈) = there $ Any-catMaybes⁺ x∈ |
| 791 |
|
| 792 |
------------------------------------------------------------------------ |
| 793 |
-- mapMaybe |
| 794 |
|
| 795 |
mapMaybe-cong : {f g : A → Maybe B} → f ≗ g → mapMaybe f ≗ mapMaybe g |
| 796 |
mapMaybe-cong f≗g = cong catMaybes ∘ map-cong f≗g |
| 797 |
|
| 798 |
mapMaybe-just : (xs : List A) → mapMaybe just xs ≡ xs |
| 799 |
mapMaybe-just [] = refl |
| 800 |
mapMaybe-just (x ∷ xs) = cong (x ∷_) (mapMaybe-just xs) |
| 801 |
|
| 802 |
mapMaybe-nothing : (xs : List A) → |
| 803 |
mapMaybe {B = B} (λ _ → nothing) xs ≡ [] |
| 804 |
mapMaybe-nothing [] = refl |
| 805 |
mapMaybe-nothing (x ∷ xs) = mapMaybe-nothing xs |
| 806 |
|
| 807 |
module _ (f : A → Maybe B) where |
| 808 |
|
| 809 |
mapMaybe-concatMap : mapMaybe f ≗ concatMap (fromMaybe ∘ f) |
| 810 |
mapMaybe-concatMap xs = begin |
| 811 |
catMaybes (map f xs) ≡⟨ catMaybes-concatMap (map f xs) ⟩ |
| 812 |
concatMap fromMaybe (map f xs) ≡⟨ concatMap-map fromMaybe f xs ⟩ |
| 813 |
concatMap (fromMaybe ∘ f) xs ∎ |
| 814 |
|
| 815 |
length-mapMaybe : ∀ xs → length (mapMaybe f xs) ≤ length xs |
| 816 |
length-mapMaybe xs = ≤-begin |
| 817 |
length (mapMaybe f xs) ≤⟨ length-catMaybes (map f xs) ⟩ |
| 818 |
length (map f xs) ≤⟨ ≤-reflexive (length-map f xs) ⟩ |
| 819 |
length xs ≤-∎ |
| 820 |
where open ≤-Reasoning renaming (begin_ to ≤-begin_; _∎ to _≤-∎) |
| 821 |
|
| 822 |
mapMaybe-++ : ∀ xs ys → |
| 823 |
mapMaybe f (xs ++ ys) ≡ mapMaybe f xs ++ mapMaybe f ys |
| 824 |
mapMaybe-++ xs ys = begin |
| 825 |
catMaybes (map f (xs ++ ys)) ≡⟨ cong catMaybes (map-++ f xs ys) ⟩ |
| 826 |
catMaybes (map f xs ++ map f ys) ≡⟨ catMaybes-++ (map f xs) (map f ys) ⟩ |
| 827 |
mapMaybe f xs ++ mapMaybe f ys ∎ |
| 828 |
|
| 829 |
module _ (f : B → Maybe C) (g : A → B) where |
| 830 |
|
| 831 |
mapMaybe-map : mapMaybe f ∘ map g ≗ mapMaybe (f ∘ g) |
| 832 |
mapMaybe-map = cong catMaybes ∘ sym ∘ map-∘ |
| 833 |
|
| 834 |
module _ (g : B → C) (f : A → Maybe B) where |
| 835 |
|
| 836 |
map-mapMaybe : map g ∘ mapMaybe f ≗ mapMaybe (Maybe.map g ∘ f) |
| 837 |
map-mapMaybe xs = begin |
| 838 |
map g (catMaybes (map f xs)) ≡⟨ map-catMaybes g (map f xs) ⟩ |
| 839 |
mapMaybe (Maybe.map g) (map f xs) ≡⟨ mapMaybe-map _ f xs ⟩ |
| 840 |
mapMaybe (Maybe.map g ∘ f) xs ∎ |
| 841 |
|
| 842 |
-- embedding-projection pairs |
| 843 |
module _ {proj : B → Maybe A} {emb : A → B} where |
| 844 |
mapMaybe-map-retract : proj ∘ emb ≗ just → mapMaybe proj ∘ map emb ≗ id |
| 845 |
mapMaybe-map-retract retract xs = begin |
| 846 |
mapMaybe proj (map emb xs) ≡⟨ mapMaybe-map _ _ xs ⟩ |
| 847 |
mapMaybe (proj ∘ emb) xs ≡⟨ mapMaybe-cong retract xs ⟩ |
| 848 |
mapMaybe just xs ≡⟨ mapMaybe-just _ ⟩ |
| 849 |
xs ∎ |
| 850 |
|
| 851 |
module _ {proj : C → Maybe B} {emb : A → C} where |
| 852 |
mapMaybe-map-none : proj ∘ emb ≗ const nothing → mapMaybe proj ∘ map emb ≗ const [] |
| 853 |
mapMaybe-map-none retract xs = begin |
| 854 |
mapMaybe proj (map emb xs) ≡⟨ mapMaybe-map _ _ xs ⟩ |
| 855 |
mapMaybe (proj ∘ emb) xs ≡⟨ mapMaybe-cong retract xs ⟩ |
| 856 |
mapMaybe (const nothing) xs ≡⟨ mapMaybe-nothing xs ⟩ |
| 857 |
[] ∎ |
| 858 |
|
| 859 |
-- embedding-projection pairs on sums |
| 860 |
mapMaybeIsInj₁∘mapInj₁ : (xs : List A) → mapMaybe (isInj₁ {B = B}) (map inj₁ xs) ≡ xs |
| 861 |
mapMaybeIsInj₁∘mapInj₁ = mapMaybe-map-retract λ _ → refl |
| 862 |
|
| 863 |
mapMaybeIsInj₁∘mapInj₂ : (xs : List B) → mapMaybe (isInj₁ {A = A}) (map inj₂ xs) ≡ [] |
| 864 |
mapMaybeIsInj₁∘mapInj₂ = mapMaybe-map-none λ _ → refl |
| 865 |
|
| 866 |
mapMaybeIsInj₂∘mapInj₂ : (xs : List B) → mapMaybe (isInj₂ {A = A}) (map inj₂ xs) ≡ xs |
| 867 |
mapMaybeIsInj₂∘mapInj₂ = mapMaybe-map-retract λ _ → refl |
| 868 |
|
| 869 |
mapMaybeIsInj₂∘mapInj₁ : (xs : List A) → mapMaybe (isInj₂ {B = B}) (map inj₁ xs) ≡ [] |
| 870 |
mapMaybeIsInj₂∘mapInj₁ = mapMaybe-map-none λ _ → refl |
| 871 |
|
| 872 |
------------------------------------------------------------------------ |
| 873 |
-- applyUpTo |
| 874 |
|
| 875 |
length-applyUpTo : ∀ (f : ℕ → A) n → length (applyUpTo f n) ≡ n |
| 876 |
length-applyUpTo f zero = refl |
| 877 |
length-applyUpTo f (suc n) = cong suc (length-applyUpTo (f ∘ suc) n) |
| 878 |
|
| 879 |
lookup-applyUpTo : ∀ (f : ℕ → A) n i → lookup (applyUpTo f n) i ≡ f (toℕ i) |
| 880 |
lookup-applyUpTo f (suc n) zero = refl |
| 881 |
lookup-applyUpTo f (suc n) (suc i) = lookup-applyUpTo (f ∘ suc) n i |
| 882 |
|
| 883 |
applyUpTo-∷ʳ : ∀ (f : ℕ → A) n → applyUpTo f n ∷ʳ f n ≡ applyUpTo f (suc n) |
| 884 |
applyUpTo-∷ʳ f zero = refl |
| 885 |
applyUpTo-∷ʳ f (suc n) = cong (f 0 ∷_) (applyUpTo-∷ʳ (f ∘ suc) n) |
| 886 |
|
| 887 |
map-applyUpTo : ∀ (f : ℕ → A) (g : A → B) n → map g (applyUpTo f n) ≡ applyUpTo (g ∘ f) n |
| 888 |
map-applyUpTo f g zero = refl |
| 889 |
map-applyUpTo f g (suc n) = cong (g (f 0) ∷_) (map-applyUpTo (f ∘ suc) g n) |
| 890 |
|
| 891 |
------------------------------------------------------------------------ |
| 892 |
-- applyDownFrom |
| 893 |
|
| 894 |
module _ (f : ℕ → A) where |
| 895 |
|
| 896 |
length-applyDownFrom : ∀ n → length (applyDownFrom f n) ≡ n |
| 897 |
length-applyDownFrom zero = refl |
| 898 |
length-applyDownFrom (suc n) = cong suc (length-applyDownFrom n) |
| 899 |
|
| 900 |
lookup-applyDownFrom : ∀ n i → lookup (applyDownFrom f n) i ≡ f (n ∸ (suc (toℕ i))) |
| 901 |
lookup-applyDownFrom (suc n) zero = refl |
| 902 |
lookup-applyDownFrom (suc n) (suc i) = lookup-applyDownFrom n i |
| 903 |
|
| 904 |
applyDownFrom-∷ʳ : ∀ n → applyDownFrom (f ∘ suc) n ∷ʳ f 0 ≡ applyDownFrom f (suc n) |
| 905 |
applyDownFrom-∷ʳ zero = refl |
| 906 |
applyDownFrom-∷ʳ (suc n) = cong (f (suc n) ∷_) (applyDownFrom-∷ʳ n) |
| 907 |
|
| 908 |
map-applyDownFrom : ∀ (g : A → B) n → map g (applyDownFrom f n) ≡ applyDownFrom (g ∘ f) n |
| 909 |
map-applyDownFrom g zero = refl |
| 910 |
map-applyDownFrom g (suc n) = cong (g (f n) ∷_) (map-applyDownFrom g n) |
| 911 |
|
| 912 |
------------------------------------------------------------------------ |
| 913 |
-- upTo |
| 914 |
|
| 915 |
length-upTo : ∀ n → length (upTo n) ≡ n |
| 916 |
length-upTo = length-applyUpTo id |
| 917 |
|
| 918 |
lookup-upTo : ∀ n i → lookup (upTo n) i ≡ toℕ i |
| 919 |
lookup-upTo = lookup-applyUpTo id |
| 920 |
|
| 921 |
upTo-∷ʳ : ∀ n → upTo n ∷ʳ n ≡ upTo (suc n) |
| 922 |
upTo-∷ʳ = applyUpTo-∷ʳ id |
| 923 |
|
| 924 |
map-upTo : ∀ (f : ℕ → A) n → map f (upTo n) ≡ applyUpTo f n |
| 925 |
map-upTo = map-applyUpTo id |
| 926 |
|
| 927 |
------------------------------------------------------------------------ |
| 928 |
-- downFrom |
| 929 |
|
| 930 |
length-downFrom : ∀ n → length (downFrom n) ≡ n |
| 931 |
length-downFrom = length-applyDownFrom id |
| 932 |
|
| 933 |
lookup-downFrom : ∀ n i → lookup (downFrom n) i ≡ n ∸ (suc (toℕ i)) |
| 934 |
lookup-downFrom = lookup-applyDownFrom id |
| 935 |
|
| 936 |
downFrom-∷ʳ : ∀ n → applyDownFrom suc n ∷ʳ 0 ≡ downFrom (suc n) |
| 937 |
downFrom-∷ʳ = applyDownFrom-∷ʳ id |
| 938 |
|
| 939 |
map-downFrom : ∀ (f : ℕ → A) n → map f (downFrom n) ≡ applyDownFrom f n |
| 940 |
map-downFrom = map-applyDownFrom id |
| 941 |
|
| 942 |
------------------------------------------------------------------------ |
| 943 |
-- tabulate |
| 944 |
|
| 945 |
tabulate-cong : ∀ {n} {f g : Fin n → A} → |
| 946 |
f ≗ g → tabulate f ≡ tabulate g |
| 947 |
tabulate-cong {n = zero} p = refl |
| 948 |
tabulate-cong {n = suc n} p = cong₂ _∷_ (p zero) (tabulate-cong (p ∘ suc)) |
| 949 |
|
| 950 |
tabulate-lookup : ∀ (xs : List A) → tabulate (lookup xs) ≡ xs |
| 951 |
tabulate-lookup [] = refl |
| 952 |
tabulate-lookup (x ∷ xs) = cong (_ ∷_) (tabulate-lookup xs) |
| 953 |
|
| 954 |
length-tabulate : ∀ {n} → (f : Fin n → A) → |
| 955 |
length (tabulate f) ≡ n |
| 956 |
length-tabulate {n = zero} f = refl |
| 957 |
length-tabulate {n = suc n} f = cong suc (length-tabulate (λ z → f (suc z))) |
| 958 |
|
| 959 |
lookup-tabulate : ∀ {n} → (f : Fin n → A) → |
| 960 |
∀ i → let i′ = cast (sym (length-tabulate f)) i |
| 961 |
in lookup (tabulate f) i′ ≡ f i |
| 962 |
lookup-tabulate f zero = refl |
| 963 |
lookup-tabulate f (suc i) = lookup-tabulate (f ∘ suc) i |
| 964 |
|
| 965 |
map-tabulate : ∀ {n} (g : Fin n → A) (f : A → B) → |
| 966 |
map f (tabulate g) ≡ tabulate (f ∘ g) |
| 967 |
map-tabulate {n = zero} g f = refl |
| 968 |
map-tabulate {n = suc n} g f = cong (_ ∷_) (map-tabulate (g ∘ suc) f) |
| 969 |
|
| 970 |
------------------------------------------------------------------------ |
| 971 |
-- _[_]%=_ |
| 972 |
|
| 973 |
length-%= : ∀ xs k (f : A → A) → length (xs [ k ]%= f) ≡ length xs |
| 974 |
length-%= (x ∷ xs) zero f = refl |
| 975 |
length-%= (x ∷ xs) (suc k) f = cong suc (length-%= xs k f) |
| 976 |
|
| 977 |
------------------------------------------------------------------------ |
| 978 |
-- _[_]∷=_ |
| 979 |
|
| 980 |
length-∷= : ∀ xs k (v : A) → length (xs [ k ]∷= v) ≡ length xs |
| 981 |
length-∷= xs k v = length-%= xs k (const v) |
| 982 |
|
| 983 |
map-∷= : ∀ xs k (v : A) (f : A → B) → |
| 984 |
let eq = sym (length-map f xs) in |
| 985 |
map f (xs [ k ]∷= v) ≡ map f xs [ cast eq k ]∷= f v |
| 986 |
map-∷= (x ∷ xs) zero v f = refl |
| 987 |
map-∷= (x ∷ xs) (suc k) v f = cong (f x ∷_) (map-∷= xs k v f) |
| 988 |
|
| 989 |
------------------------------------------------------------------------ |
| 990 |
-- insertAt |
| 991 |
|
| 992 |
length-insertAt : ∀ (xs : List A) (i : Fin (suc (length xs))) v → |
| 993 |
length (insertAt xs i v) ≡ suc (length xs) |
| 994 |
length-insertAt xs zero v = refl |
| 995 |
length-insertAt (x ∷ xs) (suc i) v = cong suc (length-insertAt xs i v) |
| 996 |
|
| 997 |
------------------------------------------------------------------------ |
| 998 |
-- removeAt |
| 999 |
|
| 1000 |
length-removeAt : ∀ (xs : List A) k → length (removeAt xs k) ≡ pred (length xs) |
| 1001 |
length-removeAt (x ∷ xs) zero = refl |
| 1002 |
length-removeAt (x ∷ xs@(_ ∷ _)) (suc k) = cong suc (length-removeAt xs k) |
| 1003 |
|
| 1004 |
length-removeAt′ : ∀ (xs : List A) k → length xs ≡ suc (length (removeAt xs k)) |
| 1005 |
length-removeAt′ xs@(_ ∷ _) k rewrite length-removeAt xs k = refl |
| 1006 |
|
| 1007 |
map-removeAt : ∀ xs k (f : A → B) → |
| 1008 |
let eq = sym (length-map f xs) in |
| 1009 |
map f (removeAt xs k) ≡ removeAt (map f xs) (cast eq k) |
| 1010 |
map-removeAt (x ∷ xs) zero f = refl |
| 1011 |
map-removeAt (x ∷ xs) (suc k) f = cong (f x ∷_) (map-removeAt xs k f) |
| 1012 |
|
| 1013 |
------------------------------------------------------------------------ |
| 1014 |
-- insertAt and removeAt |
| 1015 |
|
| 1016 |
removeAt-insertAt : ∀ (xs : List A) (i : Fin (suc (length xs))) v → |
| 1017 |
removeAt (insertAt xs i v) ((cast (sym (length-insertAt xs i v)) i)) ≡ xs |
| 1018 |
removeAt-insertAt xs zero v = refl |
| 1019 |
removeAt-insertAt (x ∷ xs) (suc i) v = cong (_ ∷_) (removeAt-insertAt xs i v) |
| 1020 |
|
| 1021 |
insertAt-removeAt : (xs : List A) (i : Fin (length xs)) → |
| 1022 |
insertAt (removeAt xs i) (cast (length-removeAt′ xs i) i) (lookup xs i) ≡ xs |
| 1023 |
insertAt-removeAt (x ∷ xs) zero = refl |
| 1024 |
insertAt-removeAt (x ∷ xs) (suc i) = cong (x ∷_) (insertAt-removeAt xs i) |
| 1025 |
|
| 1026 |
------------------------------------------------------------------------ |
| 1027 |
-- take |
| 1028 |
|
| 1029 |
length-take : ∀ n (xs : List A) → length (take n xs) ≡ n ⊓ (length xs) |
| 1030 |
length-take zero xs = refl |
| 1031 |
length-take (suc n) [] = refl |
| 1032 |
length-take (suc n) (x ∷ xs) = cong suc (length-take n xs) |
| 1033 |
|
| 1034 |
-- Take commutes with map. |
| 1035 |
take-map : ∀ {f : A → B} (n : ℕ) xs → take n (map f xs) ≡ map f (take n xs) |
| 1036 |
take-map zero xs = refl |
| 1037 |
take-map (suc s) [] = refl |
| 1038 |
take-map (suc s) (a ∷ xs) = cong (_ ∷_) (take-map s xs) |
| 1039 |
|
| 1040 |
take-suc : (xs : List A) (i : Fin (length xs)) → let m = toℕ i in |
| 1041 |
take (suc m) xs ≡ take m xs ∷ʳ lookup xs i |
| 1042 |
take-suc (x ∷ xs) zero = refl |
| 1043 |
take-suc (x ∷ xs) (suc i) = cong (x ∷_) (take-suc xs i) |
| 1044 |
|
| 1045 |
take-suc-tabulate : ∀ {n} (f : Fin n → A) (i : Fin n) → let m = toℕ i in |
| 1046 |
take (suc m) (tabulate f) ≡ take m (tabulate f) ∷ʳ f i |
| 1047 |
take-suc-tabulate f i rewrite sym (toℕ-cast (sym (length-tabulate f)) i) | sym (lookup-tabulate f i) |
| 1048 |
= take-suc (tabulate f) (cast _ i) |
| 1049 |
|
| 1050 |
-- If you take at least as many elements from a list as it has, you get |
| 1051 |
-- the whole list. |
| 1052 |
take-all : (n : ℕ) (xs : List A) → n ≥ length xs → take n xs ≡ xs |
| 1053 |
take-all zero [] _ = refl |
| 1054 |
take-all (suc _) [] _ = refl |
| 1055 |
take-all (suc n) (x ∷ xs) (s≤s pf) = cong (x ∷_) (take-all n xs pf) |
| 1056 |
|
| 1057 |
-- Taking from an empty list does nothing. |
| 1058 |
take-[] : ∀ m → take {A = A} m [] ≡ [] |
| 1059 |
take-[] zero = refl |
| 1060 |
take-[] (suc m) = refl |
| 1061 |
|
| 1062 |
-- Taking twice takes the minimum of both counts. |
| 1063 |
take-take : ∀ n m (xs : List A) → take n (take m xs) ≡ take (n ⊓ m) xs |
| 1064 |
take-take zero m xs = refl |
| 1065 |
take-take (suc n) zero xs = refl |
| 1066 |
take-take (suc n) (suc m) [] = refl |
| 1067 |
take-take (suc n) (suc m) (x ∷ xs) = cong (x ∷_) (take-take n m xs) |
| 1068 |
|
| 1069 |
-- Dropping m elements and then taking n is the same as |
| 1070 |
-- taking n + m elements and then dropping m. |
| 1071 |
take-drop : ∀ n m (xs : List A) → |
| 1072 |
take n (drop m xs) ≡ drop m (take (m + n) xs) |
| 1073 |
take-drop n zero xs = refl |
| 1074 |
take-drop n (suc m) [] = take-[] n |
| 1075 |
take-drop n (suc m) (x ∷ xs) = take-drop n m xs |
| 1076 |
|
| 1077 |
------------------------------------------------------------------------ |
| 1078 |
-- drop |
| 1079 |
|
| 1080 |
length-drop : ∀ n (xs : List A) → length (drop n xs) ≡ length xs ∸ n |
| 1081 |
length-drop zero xs = refl |
| 1082 |
length-drop (suc n) [] = refl |
| 1083 |
length-drop (suc n) (x ∷ xs) = length-drop n xs |
| 1084 |
|
| 1085 |
-- Drop commutes with map. |
| 1086 |
drop-map : ∀ {f : A → B} (n : ℕ) xs → drop n (map f xs) ≡ map f (drop n xs) |
| 1087 |
drop-map zero xs = refl |
| 1088 |
drop-map (suc n) [] = refl |
| 1089 |
drop-map (suc n) (a ∷ xs) = drop-map n xs |
| 1090 |
|
| 1091 |
-- Dropping from an empty list does nothing. |
| 1092 |
drop-[] : ∀ m → drop {A = A} m [] ≡ [] |
| 1093 |
drop-[] zero = refl |
| 1094 |
drop-[] (suc m) = refl |
| 1095 |
|
| 1096 |
take++drop≡id : ∀ n (xs : List A) → take n xs ++ drop n xs ≡ xs |
| 1097 |
take++drop≡id zero xs = refl |
| 1098 |
take++drop≡id (suc n) [] = refl |
| 1099 |
take++drop≡id (suc n) (x ∷ xs) = cong (x ∷_) (take++drop≡id n xs) |
| 1100 |
|
| 1101 |
drop-take-suc : (xs : List A) (i : Fin (length xs)) → let m = toℕ i in |
| 1102 |
drop m (take (suc m) xs) ≡ [ lookup xs i ] |
| 1103 |
drop-take-suc (x ∷ xs) zero = refl |
| 1104 |
drop-take-suc (x ∷ xs) (suc i) = drop-take-suc xs i |
| 1105 |
|
| 1106 |
drop-take-suc-tabulate : ∀ {n} (f : Fin n → A) (i : Fin n) → let m = toℕ i in |
| 1107 |
drop m (take (suc m) (tabulate f)) ≡ [ f i ] |
| 1108 |
drop-take-suc-tabulate f i rewrite sym (toℕ-cast (sym (length-tabulate f)) i) | sym (lookup-tabulate f i) |
| 1109 |
= drop-take-suc (tabulate f) (cast _ i) |
| 1110 |
|
| 1111 |
-- Dropping m elements and then n elements is same as dropping m+n elements |
| 1112 |
drop-drop : (m n : ℕ) → (xs : List A) → drop n (drop m xs) ≡ drop (m + n) xs |
| 1113 |
drop-drop zero n xs = refl |
| 1114 |
drop-drop (suc m) n [] = drop-[] n |
| 1115 |
drop-drop (suc m) n (x ∷ xs) = drop-drop m n xs |
| 1116 |
|
| 1117 |
drop-all : (n : ℕ) (xs : List A) → n ≥ length xs → drop n xs ≡ [] |
| 1118 |
drop-all n [] _ = drop-[] n |
| 1119 |
drop-all (suc n) (x ∷ xs) p = drop-all n xs (s≤s⁻¹ p) |
| 1120 |
|
| 1121 |
------------------------------------------------------------------------ |
| 1122 |
-- replicate |
| 1123 |
|
| 1124 |
length-replicate : ∀ n {x : A} → length (replicate n x) ≡ n |
| 1125 |
length-replicate zero = refl |
| 1126 |
length-replicate (suc n) = cong suc (length-replicate n) |
| 1127 |
|
| 1128 |
lookup-replicate : ∀ n (x : A) (i : Fin n) → |
| 1129 |
lookup (replicate n x) (cast (sym (length-replicate n)) i) ≡ x |
| 1130 |
lookup-replicate (suc n) x zero = refl |
| 1131 |
lookup-replicate (suc n) x (suc i) = lookup-replicate n x i |
| 1132 |
|
| 1133 |
map-replicate : ∀ (f : A → B) n (x : A) → |
| 1134 |
map f (replicate n x) ≡ replicate n (f x) |
| 1135 |
map-replicate f zero x = refl |
| 1136 |
map-replicate f (suc n) x = cong (_ ∷_) (map-replicate f n x) |
| 1137 |
|
| 1138 |
zipWith-replicate : ∀ n (_⊕_ : A → B → C) (x : A) (y : B) → |
| 1139 |
zipWith _⊕_ (replicate n x) (replicate n y) ≡ replicate n (x ⊕ y) |
| 1140 |
zipWith-replicate zero _⊕_ x y = refl |
| 1141 |
zipWith-replicate (suc n) _⊕_ x y = cong (x ⊕ y ∷_) (zipWith-replicate n _⊕_ x y) |
| 1142 |
|
| 1143 |
------------------------------------------------------------------------ |
| 1144 |
-- iterate |
| 1145 |
|
| 1146 |
length-iterate : ∀ f (x : A) n → length (iterate f x n) ≡ n |
| 1147 |
length-iterate f x zero = refl |
| 1148 |
length-iterate f x (suc n) = cong suc (length-iterate f (f x) n) |
| 1149 |
|
| 1150 |
iterate-id : ∀ (x : A) n → iterate id x n ≡ replicate n x |
| 1151 |
iterate-id x zero = refl |
| 1152 |
iterate-id x (suc n) = cong (_ ∷_) (iterate-id x n) |
| 1153 |
|
| 1154 |
lookup-iterate : ∀ f (x : A) n (i : Fin n) → |
| 1155 |
lookup (iterate f x n) (cast (sym (length-iterate f x n)) i) ≡ ℕ.iterate f x (toℕ i) |
| 1156 |
lookup-iterate f x (suc n) zero = refl |
| 1157 |
lookup-iterate f x (suc n) (suc i) = lookup-iterate f (f x) n i |
| 1158 |
|
| 1159 |
------------------------------------------------------------------------ |
| 1160 |
-- splitAt |
| 1161 |
|
| 1162 |
splitAt-defn : ∀ n → splitAt {A = A} n ≗ < take n , drop n > |
| 1163 |
splitAt-defn zero xs = refl |
| 1164 |
splitAt-defn (suc n) [] = refl |
| 1165 |
splitAt-defn (suc n) (x ∷ xs) = cong (Product.map (x ∷_) id) (splitAt-defn n xs) |
| 1166 |
|
| 1167 |
module _ (f : A → B) where |
| 1168 |
|
| 1169 |
splitAt-map : ∀ n → splitAt n ∘ map f ≗ |
| 1170 |
Product.map (map f) (map f) ∘ splitAt n |
| 1171 |
splitAt-map zero xs = refl |
| 1172 |
splitAt-map (suc n) [] = refl |
| 1173 |
splitAt-map (suc n) (x ∷ xs) = |
| 1174 |
cong (Product.map₁ (f x ∷_)) (splitAt-map n xs) |
| 1175 |
|
| 1176 |
------------------------------------------------------------------------ |
| 1177 |
-- takeWhile, dropWhile, and span |
| 1178 |
|
| 1179 |
module _ {P : Pred A p} (P? : Decidable P) where |
| 1180 |
|
| 1181 |
takeWhile++dropWhile : ∀ xs → takeWhile P? xs ++ dropWhile P? xs ≡ xs |
| 1182 |
takeWhile++dropWhile [] = refl |
| 1183 |
takeWhile++dropWhile (x ∷ xs) with does (P? x) |
| 1184 |
... | true = cong (x ∷_) (takeWhile++dropWhile xs) |
| 1185 |
... | false = refl |
| 1186 |
|
| 1187 |
span-defn : span P? ≗ < takeWhile P? , dropWhile P? > |
| 1188 |
span-defn [] = refl |
| 1189 |
span-defn (x ∷ xs) with does (P? x) |
| 1190 |
... | true = cong (Product.map (x ∷_) id) (span-defn xs) |
| 1191 |
... | false = refl |
| 1192 |
|
| 1193 |
------------------------------------------------------------------------ |
| 1194 |
-- filter |
| 1195 |
|
| 1196 |
module _ {P : Pred A p} (P? : Decidable P) where |
| 1197 |
|
| 1198 |
length-filter : ∀ xs → length (filter P? xs) ≤ length xs |
| 1199 |
length-filter [] = z≤n |
| 1200 |
length-filter (x ∷ xs) with ih ← length-filter xs | does (P? x) |
| 1201 |
... | false = m≤n⇒m≤1+n ih |
| 1202 |
... | true = s≤s ih |
| 1203 |
|
| 1204 |
filter-all : ∀ {xs} → All P xs → filter P? xs ≡ xs |
| 1205 |
filter-all {[]} [] = refl |
| 1206 |
filter-all {x ∷ xs} (px ∷ pxs) with P? x |
| 1207 |
... | no ¬px = contradiction px ¬px |
| 1208 |
... | true because _ = cong (x ∷_) (filter-all pxs) |
| 1209 |
|
| 1210 |
filter-notAll : ∀ xs → Any (∁ P) xs → length (filter P? xs) < length xs |
| 1211 |
filter-notAll (x ∷ xs) (here ¬px) with P? x |
| 1212 |
... | false because _ = s≤s (length-filter xs) |
| 1213 |
... | yes px = contradiction px ¬px |
| 1214 |
filter-notAll (x ∷ xs) (there any) with ih ← filter-notAll xs any | does (P? x) |
| 1215 |
... | false = m≤n⇒m≤1+n ih |
| 1216 |
... | true = s≤s ih |
| 1217 |
|
| 1218 |
filter-some : ∀ {xs} → Any P xs → 0 < length (filter P? xs) |
| 1219 |
filter-some {x ∷ xs} (here px) with P? x |
| 1220 |
... | true because _ = z<s |
| 1221 |
... | no ¬px = contradiction px ¬px |
| 1222 |
filter-some {x ∷ xs} (there pxs) with does (P? x) |
| 1223 |
... | true = m≤n⇒m≤1+n (filter-some pxs) |
| 1224 |
... | false = filter-some pxs |
| 1225 |
|
| 1226 |
filter-none : ∀ {xs} → All (∁ P) xs → filter P? xs ≡ [] |
| 1227 |
filter-none {[]} [] = refl |
| 1228 |
filter-none {x ∷ xs} (¬px ∷ ¬pxs) with P? x |
| 1229 |
... | false because _ = filter-none ¬pxs |
| 1230 |
... | yes px = contradiction px ¬px |
| 1231 |
|
| 1232 |
filter-complete : ∀ {xs} → length (filter P? xs) ≡ length xs → |
| 1233 |
filter P? xs ≡ xs |
| 1234 |
filter-complete {[]} eq = refl |
| 1235 |
filter-complete {x ∷ xs} eq with does (P? x) |
| 1236 |
... | false = contradiction eq (<⇒≢ (s≤s (length-filter xs))) |
| 1237 |
... | true = cong (x ∷_) (filter-complete (suc-injective eq)) |
| 1238 |
|
| 1239 |
filter-accept : ∀ {x xs} → P x → filter P? (x ∷ xs) ≡ x ∷ (filter P? xs) |
| 1240 |
filter-accept {x} Px with P? x |
| 1241 |
... | true because _ = refl |
| 1242 |
... | no ¬Px = contradiction Px ¬Px |
| 1243 |
|
| 1244 |
filter-reject : ∀ {x xs} → ¬ P x → filter P? (x ∷ xs) ≡ filter P? xs |
| 1245 |
filter-reject {x} ¬Px with P? x |
| 1246 |
... | yes Px = contradiction Px ¬Px |
| 1247 |
... | false because _ = refl |
| 1248 |
|
| 1249 |
filter-idem : filter P? ∘ filter P? ≗ filter P? |
| 1250 |
filter-idem [] = refl |
| 1251 |
filter-idem (x ∷ xs) with does (P? x) in eq |
| 1252 |
... | false = filter-idem xs |
| 1253 |
... | true rewrite eq = cong (x ∷_) (filter-idem xs) |
| 1254 |
|
| 1255 |
filter-++ : ∀ xs ys → filter P? (xs ++ ys) ≡ filter P? xs ++ filter P? ys |
| 1256 |
filter-++ [] ys = refl |
| 1257 |
filter-++ (x ∷ xs) ys with ih ← filter-++ xs ys | does (P? x) |
| 1258 |
... | true = cong (x ∷_) ih |
| 1259 |
... | false = ih |
| 1260 |
|
| 1261 |
module _ {P : Pred A p} {Q : Pred A q} (P? : Decidable P) (Q? : Decidable Q) where |
| 1262 |
|
| 1263 |
filter-≐ : P ≐ Q → filter P? ≗ filter Q? |
| 1264 |
filter-≐ P≐Q [] = refl |
| 1265 |
filter-≐ P≐Q (x ∷ xs) with P? x |
| 1266 |
... | yes P[x] = trans (cong (x ∷_) (filter-≐ P≐Q xs)) (sym (filter-accept Q? (proj₁ P≐Q P[x]))) |
| 1267 |
... | no ¬P[x] = trans (filter-≐ P≐Q xs) (sym (filter-reject Q? (¬P[x] ∘ proj₂ P≐Q))) |
| 1268 |
|
| 1269 |
------------------------------------------------------------------------ |
| 1270 |
-- derun and deduplicate |
| 1271 |
|
| 1272 |
module _ {R : Rel A p} (R? : B.Decidable R) where |
| 1273 |
|
| 1274 |
length-derun : ∀ xs → length (derun R? xs) ≤ length xs |
| 1275 |
length-derun [] = ≤-refl |
| 1276 |
length-derun (x ∷ []) = ≤-refl |
| 1277 |
length-derun (x ∷ y ∷ xs) with ih ← length-derun (y ∷ xs) | does (R? x y) |
| 1278 |
... | true = m≤n⇒m≤1+n ih |
| 1279 |
... | false = s≤s ih |
| 1280 |
|
| 1281 |
length-deduplicate : ∀ xs → length (deduplicate R? xs) ≤ length xs |
| 1282 |
length-deduplicate [] = z≤n |
| 1283 |
length-deduplicate (x ∷ xs) = ≤-begin |
| 1284 |
1 + length (filter (¬? ∘ R? x) r) ≤⟨ s≤s (length-filter (¬? ∘ R? x) r) ⟩ |
| 1285 |
1 + length r ≤⟨ s≤s (length-deduplicate xs) ⟩ |
| 1286 |
1 + length xs ≤-∎ |
| 1287 |
where |
| 1288 |
open ≤-Reasoning renaming (begin_ to ≤-begin_; _∎ to _≤-∎) |
| 1289 |
r = deduplicate R? xs |
| 1290 |
|
| 1291 |
derun-reject : ∀ {x y} xs → R x y → derun R? (x ∷ y ∷ xs) ≡ derun R? (y ∷ xs) |
| 1292 |
derun-reject {x} {y} xs Rxy with R? x y |
| 1293 |
... | yes _ = refl |
| 1294 |
... | no ¬Rxy = contradiction Rxy ¬Rxy |
| 1295 |
|
| 1296 |
derun-accept : ∀ {x y} xs → ¬ R x y → derun R? (x ∷ y ∷ xs) ≡ x ∷ derun R? (y ∷ xs) |
| 1297 |
derun-accept {x} {y} xs ¬Rxy with R? x y |
| 1298 |
... | yes Rxy = contradiction Rxy ¬Rxy |
| 1299 |
... | no _ = refl |
| 1300 |
|
| 1301 |
------------------------------------------------------------------------ |
| 1302 |
-- partition |
| 1303 |
|
| 1304 |
module _ {P : Pred A p} (P? : Decidable P) where |
| 1305 |
|
| 1306 |
partition-defn : partition P? ≗ < filter P? , filter (∁? P?) > |
| 1307 |
partition-defn [] = refl |
| 1308 |
partition-defn (x ∷ xs) with ih ← partition-defn xs | does (P? x) |
| 1309 |
... | true = cong (Product.map (x ∷_) id) ih |
| 1310 |
... | false = cong (Product.map id (x ∷_)) ih |
| 1311 |
|
| 1312 |
length-partition : ∀ xs → (let (ys , zs) = partition P? xs) → |
| 1313 |
length ys ≤ length xs × length zs ≤ length xs |
| 1314 |
length-partition [] = z≤n , z≤n |
| 1315 |
length-partition (x ∷ xs) with ih ← length-partition xs | does (P? x) |
| 1316 |
... | true = Product.map s≤s m≤n⇒m≤1+n ih |
| 1317 |
... | false = Product.map m≤n⇒m≤1+n s≤s ih |
| 1318 |
|
| 1319 |
partition-is-foldr : partition P? ≗ foldr |
| 1320 |
(λ x → if does (P? x) then Product.map₁ (x ∷_) else Product.map₂ (x ∷_)) |
| 1321 |
([] , []) |
| 1322 |
partition-is-foldr [] = refl |
| 1323 |
partition-is-foldr (x ∷ xs) with ih ← partition-is-foldr xs | does (P? x) |
| 1324 |
... | true = cong (Product.map₁ (x ∷_)) ih |
| 1325 |
... | false = cong (Product.map₂ (x ∷_)) ih |
| 1326 |
|
| 1327 |
------------------------------------------------------------------------ |
| 1328 |
-- _ʳ++_ |
| 1329 |
|
| 1330 |
ʳ++-defn : ∀ (xs : List A) {ys} → xs ʳ++ ys ≡ reverse xs ++ ys |
| 1331 |
ʳ++-defn [] = refl |
| 1332 |
ʳ++-defn (x ∷ xs) {ys} = begin |
| 1333 |
(x ∷ xs) ʳ++ ys ≡⟨⟩ |
| 1334 |
xs ʳ++ x ∷ ys ≡⟨⟩ |
| 1335 |
xs ʳ++ [ x ] ++ ys ≡⟨ ʳ++-defn xs ⟩ |
| 1336 |
reverse xs ++ [ x ] ++ ys ≡⟨ sym (++-assoc (reverse xs) _ _) ⟩ |
| 1337 |
(reverse xs ++ [ x ]) ++ ys ≡⟨ cong (_++ ys) (sym (ʳ++-defn xs)) ⟩ |
| 1338 |
(xs ʳ++ [ x ]) ++ ys ≡⟨⟩ |
| 1339 |
reverse (x ∷ xs) ++ ys ∎ |
| 1340 |
|
| 1341 |
-- Reverse-append of append is reverse-append after reverse-append. |
| 1342 |
|
| 1343 |
++-ʳ++ : ∀ (xs {ys zs} : List A) → (xs ++ ys) ʳ++ zs ≡ ys ʳ++ xs ʳ++ zs |
| 1344 |
++-ʳ++ [] = refl |
| 1345 |
++-ʳ++ (x ∷ xs) {ys} {zs} = begin |
| 1346 |
(x ∷ xs ++ ys) ʳ++ zs ≡⟨⟩ |
| 1347 |
(xs ++ ys) ʳ++ x ∷ zs ≡⟨ ++-ʳ++ xs ⟩ |
| 1348 |
ys ʳ++ xs ʳ++ x ∷ zs ≡⟨⟩ |
| 1349 |
ys ʳ++ (x ∷ xs) ʳ++ zs ∎ |
| 1350 |
|
| 1351 |
-- Reverse-append of reverse-append is commuted reverse-append after append. |
| 1352 |
|
| 1353 |
ʳ++-ʳ++ : ∀ (xs {ys zs} : List A) → (xs ʳ++ ys) ʳ++ zs ≡ ys ʳ++ xs ++ zs |
| 1354 |
ʳ++-ʳ++ [] = refl |
| 1355 |
ʳ++-ʳ++ (x ∷ xs) {ys} {zs} = begin |
| 1356 |
((x ∷ xs) ʳ++ ys) ʳ++ zs ≡⟨⟩ |
| 1357 |
(xs ʳ++ x ∷ ys) ʳ++ zs ≡⟨ ʳ++-ʳ++ xs ⟩ |
| 1358 |
(x ∷ ys) ʳ++ xs ++ zs ≡⟨⟩ |
| 1359 |
ys ʳ++ (x ∷ xs) ++ zs ∎ |
| 1360 |
|
| 1361 |
-- Length of reverse-append |
| 1362 |
|
| 1363 |
length-ʳ++ : ∀ (xs {ys} : List A) → |
| 1364 |
length (xs ʳ++ ys) ≡ length xs + length ys |
| 1365 |
length-ʳ++ [] = refl |
| 1366 |
length-ʳ++ (x ∷ xs) {ys} = begin |
| 1367 |
length ((x ∷ xs) ʳ++ ys) ≡⟨⟩ |
| 1368 |
length (xs ʳ++ x ∷ ys) ≡⟨ length-ʳ++ xs ⟩ |
| 1369 |
length xs + length (x ∷ ys) ≡⟨ +-suc _ _ ⟩ |
| 1370 |
length (x ∷ xs) + length ys ∎ |
| 1371 |
|
| 1372 |
-- map distributes over reverse-append. |
| 1373 |
|
| 1374 |
map-ʳ++ : (f : A → B) (xs {ys} : List A) → |
| 1375 |
map f (xs ʳ++ ys) ≡ map f xs ʳ++ map f ys |
| 1376 |
map-ʳ++ f [] = refl |
| 1377 |
map-ʳ++ f (x ∷ xs) {ys} = begin |
| 1378 |
map f ((x ∷ xs) ʳ++ ys) ≡⟨⟩ |
| 1379 |
map f (xs ʳ++ x ∷ ys) ≡⟨ map-ʳ++ f xs ⟩ |
| 1380 |
map f xs ʳ++ map f (x ∷ ys) ≡⟨⟩ |
| 1381 |
map f xs ʳ++ f x ∷ map f ys ≡⟨⟩ |
| 1382 |
(f x ∷ map f xs) ʳ++ map f ys ≡⟨⟩ |
| 1383 |
map f (x ∷ xs) ʳ++ map f ys ∎ |
| 1384 |
|
| 1385 |
-- A foldr after a reverse is a foldl. |
| 1386 |
|
| 1387 |
foldr-ʳ++ : ∀ (f : A → B → B) b xs {ys} → |
| 1388 |
foldr f b (xs ʳ++ ys) ≡ foldl (flip f) (foldr f b ys) xs |
| 1389 |
foldr-ʳ++ f b [] {_} = refl |
| 1390 |
foldr-ʳ++ f b (x ∷ xs) {ys} = begin |
| 1391 |
foldr f b ((x ∷ xs) ʳ++ ys) ≡⟨⟩ |
| 1392 |
foldr f b (xs ʳ++ x ∷ ys) ≡⟨ foldr-ʳ++ f b xs ⟩ |
| 1393 |
foldl (flip f) (foldr f b (x ∷ ys)) xs ≡⟨⟩ |
| 1394 |
foldl (flip f) (f x (foldr f b ys)) xs ≡⟨⟩ |
| 1395 |
foldl (flip f) (foldr f b ys) (x ∷ xs) ∎ |
| 1396 |
|
| 1397 |
-- A foldl after a reverse is a foldr. |
| 1398 |
|
| 1399 |
foldl-ʳ++ : ∀ (f : B → A → B) b xs {ys} → |
| 1400 |
foldl f b (xs ʳ++ ys) ≡ foldl f (foldr (flip f) b xs) ys |
| 1401 |
foldl-ʳ++ f b [] {_} = refl |
| 1402 |
foldl-ʳ++ f b (x ∷ xs) {ys} = begin |
| 1403 |
foldl f b ((x ∷ xs) ʳ++ ys) ≡⟨⟩ |
| 1404 |
foldl f b (xs ʳ++ x ∷ ys) ≡⟨ foldl-ʳ++ f b xs ⟩ |
| 1405 |
foldl f (foldr (flip f) b xs) (x ∷ ys) ≡⟨⟩ |
| 1406 |
foldl f (f (foldr (flip f) b xs) x) ys ≡⟨⟩ |
| 1407 |
foldl f (foldr (flip f) b (x ∷ xs)) ys ∎ |
| 1408 |
|
| 1409 |
------------------------------------------------------------------------ |
| 1410 |
-- reverse |
| 1411 |
|
| 1412 |
-- reverse of cons is snoc of reverse. |
| 1413 |
|
| 1414 |
unfold-reverse : ∀ (x : A) xs → reverse (x ∷ xs) ≡ reverse xs ∷ʳ x |
| 1415 |
unfold-reverse x xs = ʳ++-defn xs |
| 1416 |
|
| 1417 |
-- reverse is an anti-homomorphism with respect to append. |
| 1418 |
|
| 1419 |
reverse-++ : (xs ys : List A) → |
| 1420 |
reverse (xs ++ ys) ≡ reverse ys ++ reverse xs |
| 1421 |
reverse-++ xs ys = begin |
| 1422 |
reverse (xs ++ ys) ≡⟨⟩ |
| 1423 |
(xs ++ ys) ʳ++ [] ≡⟨ ++-ʳ++ xs ⟩ |
| 1424 |
ys ʳ++ xs ʳ++ [] ≡⟨⟩ |
| 1425 |
ys ʳ++ reverse xs ≡⟨ ʳ++-defn ys ⟩ |
| 1426 |
reverse ys ++ reverse xs ∎ |
| 1427 |
|
| 1428 |
-- reverse is self-inverse. |
| 1429 |
|
| 1430 |
reverse-selfInverse : SelfInverse {A = List A} _≡_ reverse |
| 1431 |
reverse-selfInverse {x = xs} {y = ys} xs⁻¹≈ys = begin |
| 1432 |
reverse ys ≡⟨⟩ |
| 1433 |
ys ʳ++ [] ≡⟨ cong (_ʳ++ []) xs⁻¹≈ys ⟨ |
| 1434 |
reverse xs ʳ++ [] ≡⟨⟩ |
| 1435 |
(xs ʳ++ []) ʳ++ [] ≡⟨ ʳ++-ʳ++ xs ⟩ |
| 1436 |
[] ʳ++ xs ++ [] ≡⟨⟩ |
| 1437 |
xs ++ [] ≡⟨ ++-identityʳ xs ⟩ |
| 1438 |
xs ∎ |
| 1439 |
|
| 1440 |
-- reverse is involutive. |
| 1441 |
|
| 1442 |
reverse-involutive : Involutive {A = List A} _≡_ reverse |
| 1443 |
reverse-involutive = selfInverse⇒involutive reverse-selfInverse |
| 1444 |
|
| 1445 |
-- reverse is injective. |
| 1446 |
|
| 1447 |
reverse-injective : Injective {A = List A} _≡_ _≡_ reverse |
| 1448 |
reverse-injective = selfInverse⇒injective reverse-selfInverse |
| 1449 |
|
| 1450 |
-- reverse preserves length. |
| 1451 |
|
| 1452 |
length-reverse : ∀ (xs : List A) → length (reverse xs) ≡ length xs |
| 1453 |
length-reverse xs = begin |
| 1454 |
length (reverse xs) ≡⟨⟩ |
| 1455 |
length (xs ʳ++ []) ≡⟨ length-ʳ++ xs ⟩ |
| 1456 |
length xs + 0 ≡⟨ +-identityʳ _ ⟩ |
| 1457 |
length xs ∎ |
| 1458 |
|
| 1459 |
reverse-map : (f : A → B) → map f ∘ reverse ≗ reverse ∘ map f |
| 1460 |
reverse-map f xs = begin |
| 1461 |
map f (reverse xs) ≡⟨⟩ |
| 1462 |
map f (xs ʳ++ []) ≡⟨ map-ʳ++ f xs ⟩ |
| 1463 |
map f xs ʳ++ [] ≡⟨⟩ |
| 1464 |
reverse (map f xs) ∎ |
| 1465 |
|
| 1466 |
reverse-foldr : ∀ (f : A → B → B) b → |
| 1467 |
foldr f b ∘ reverse ≗ foldl (flip f) b |
| 1468 |
reverse-foldr f b xs = foldr-ʳ++ f b xs |
| 1469 |
|
| 1470 |
reverse-foldl : ∀ (f : B → A → B) b xs → |
| 1471 |
foldl f b (reverse xs) ≡ foldr (flip f) b xs |
| 1472 |
reverse-foldl f b xs = foldl-ʳ++ f b xs |
| 1473 |
|
| 1474 |
------------------------------------------------------------------------ |
| 1475 |
-- reverse, applyUpTo, and applyDownFrom |
| 1476 |
|
| 1477 |
reverse-applyUpTo : ∀ (f : ℕ → A) n → reverse (applyUpTo f n) ≡ applyDownFrom f n |
| 1478 |
reverse-applyUpTo f zero = refl |
| 1479 |
reverse-applyUpTo f (suc n) = begin |
| 1480 |
reverse (f 0 ∷ applyUpTo (f ∘ suc) n) ≡⟨ reverse-++ [ f 0 ] (applyUpTo (f ∘ suc) n) ⟩ |
| 1481 |
reverse (applyUpTo (f ∘ suc) n) ∷ʳ f 0 ≡⟨ cong (_∷ʳ f 0) (reverse-applyUpTo (f ∘ suc) n) ⟩ |
| 1482 |
applyDownFrom (f ∘ suc) n ∷ʳ f 0 ≡⟨ applyDownFrom-∷ʳ f n ⟩ |
| 1483 |
applyDownFrom f (suc n) ∎ |
| 1484 |
|
| 1485 |
reverse-upTo : ∀ n → reverse (upTo n) ≡ downFrom n |
| 1486 |
reverse-upTo = reverse-applyUpTo id |
| 1487 |
|
| 1488 |
reverse-applyDownFrom : ∀ (f : ℕ → A) n → reverse (applyDownFrom f n) ≡ applyUpTo f n |
| 1489 |
reverse-applyDownFrom f zero = refl |
| 1490 |
reverse-applyDownFrom f (suc n) = begin |
| 1491 |
reverse (f n ∷ applyDownFrom f n) ≡⟨ reverse-++ [ f n ] (applyDownFrom f n) ⟩ |
| 1492 |
reverse (applyDownFrom f n) ∷ʳ f n ≡⟨ cong (_∷ʳ f n) (reverse-applyDownFrom f n) ⟩ |
| 1493 |
applyUpTo f n ∷ʳ f n ≡⟨ applyUpTo-∷ʳ f n ⟩ |
| 1494 |
applyUpTo f (suc n) ∎ |
| 1495 |
|
| 1496 |
reverse-downFrom : ∀ n → reverse (downFrom n) ≡ upTo n |
| 1497 |
reverse-downFrom = reverse-applyDownFrom id |
| 1498 |
|
| 1499 |
------------------------------------------------------------------------ |
| 1500 |
-- _∷ʳ_ |
| 1501 |
|
| 1502 |
∷ʳ-injective : ∀ xs ys → xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys × x ≡ y |
| 1503 |
∷ʳ-injective [] [] refl = refl , refl |
| 1504 |
∷ʳ-injective (x ∷ xs) (y ∷ ys) eq with refl , eq′ ← ∷-injective eq |
| 1505 |
= Product.map (cong (x ∷_)) id (∷ʳ-injective xs ys eq′) |
| 1506 |
∷ʳ-injective [] (_ ∷ _ ∷ _) () |
| 1507 |
∷ʳ-injective (_ ∷ _ ∷ _) [] () |
| 1508 |
|
| 1509 |
∷ʳ-injectiveˡ : ∀ xs ys → xs ∷ʳ x ≡ ys ∷ʳ y → xs ≡ ys |
| 1510 |
∷ʳ-injectiveˡ xs ys eq = proj₁ (∷ʳ-injective xs ys eq) |
| 1511 |
|
| 1512 |
∷ʳ-injectiveʳ : ∀ xs ys → xs ∷ʳ x ≡ ys ∷ʳ y → x ≡ y |
| 1513 |
∷ʳ-injectiveʳ xs ys eq = proj₂ (∷ʳ-injective xs ys eq) |
| 1514 |
|
| 1515 |
∷ʳ-++ : ∀ xs (a : A) ys → xs ∷ʳ a ++ ys ≡ xs ++ a ∷ ys |
| 1516 |
∷ʳ-++ xs a ys = ++-assoc xs [ a ] ys |
| 1517 |
|
| 1518 |
------------------------------------------------------------------------ |
| 1519 |
-- uncons |
| 1520 |
|
| 1521 |
module _ (f : A → B) where |
| 1522 |
|
| 1523 |
-- 'commute' List.uncons and List.map to obtain a Maybe.map and List.uncons. |
| 1524 |
uncons-map : uncons ∘ map f ≗ Maybe.map (Product.map f (map f)) ∘ uncons |
| 1525 |
uncons-map [] = refl |
| 1526 |
uncons-map (x ∷ xs) = refl |
| 1527 |
|
| 1528 |
------------------------------------------------------------------------ |
| 1529 |
-- head |
| 1530 |
|
| 1531 |
module _ {f : A → B} where |
| 1532 |
|
| 1533 |
-- 'commute' List.head and List.map to obtain a Maybe.map and List.head. |
| 1534 |
head-map : head ∘ map f ≗ Maybe.map f ∘ head |
| 1535 |
head-map [] = refl |
| 1536 |
head-map (_ ∷ _) = refl |
| 1537 |
|
| 1538 |
------------------------------------------------------------------------ |
| 1539 |
-- last |
| 1540 |
|
| 1541 |
module _ (f : A → B) where |
| 1542 |
|
| 1543 |
-- 'commute' List.last and List.map to obtain a Maybe.map and List.last. |
| 1544 |
last-map : last ∘ map f ≗ Maybe.map f ∘ last |
| 1545 |
last-map [] = refl |
| 1546 |
last-map (x ∷ []) = refl |
| 1547 |
last-map (x ∷ xs@(_ ∷ _)) = last-map xs |
| 1548 |
|
| 1549 |
------------------------------------------------------------------------ |
| 1550 |
-- tail |
| 1551 |
|
| 1552 |
module _ (f : A → B) where |
| 1553 |
|
| 1554 |
-- 'commute' List.tail and List.map to obtain a Maybe.map and List.tail. |
| 1555 |
tail-map : tail ∘ map f ≗ Maybe.map (map f) ∘ tail |
| 1556 |
tail-map [] = refl |
| 1557 |
tail-map (x ∷ xs) = refl |
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
------------------------------------------------------------------------ |
| 1563 |
-- DEPRECATED NAMES |
| 1564 |
------------------------------------------------------------------------ |
| 1565 |
-- Please use the new names as continuing support for the old names is |
| 1566 |
-- not guaranteed. |
| 1567 |
|
| 1568 |
-- Version 2.0 |
| 1569 |
|
| 1570 |
map-id₂ = map-id-local |
| 1571 |
{-# WARNING_ON_USAGE map-id₂ |
| 1572 |
"Warning: map-id₂ was deprecated in v2.0. |
| 1573 |
Please use map-id-local instead." |
| 1574 |
#-} |
| 1575 |
|
| 1576 |
map-cong₂ = map-cong-local |
| 1577 |
{-# WARNING_ON_USAGE map-id₂ |
| 1578 |
"Warning: map-cong₂ was deprecated in v2.0. |
| 1579 |
Please use map-cong-local instead." |
| 1580 |
#-} |
| 1581 |
|
| 1582 |
map-compose = map-∘ |
| 1583 |
{-# WARNING_ON_USAGE map-compose |
| 1584 |
"Warning: map-compose was deprecated in v2.0. |
| 1585 |
Please use map-∘ instead." |
| 1586 |
#-} |
| 1587 |
|
| 1588 |
map-++-commute = map-++ |
| 1589 |
{-# WARNING_ON_USAGE map-++-commute |
| 1590 |
"Warning: map-++-commute was deprecated in v2.0. |
| 1591 |
Please use map-++ instead." |
| 1592 |
#-} |
| 1593 |
|
| 1594 |
reverse-map-commute = reverse-map |
| 1595 |
{-# WARNING_ON_USAGE reverse-map-commute |
| 1596 |
"Warning: reverse-map-commute was deprecated in v2.0. |
| 1597 |
Please use reverse-map instead." |
| 1598 |
#-} |
| 1599 |
|
| 1600 |
reverse-++-commute = reverse-++ |
| 1601 |
{-# WARNING_ON_USAGE reverse-++-commute |
| 1602 |
"Warning: reverse-++-commute was deprecated in v2.0. |
| 1603 |
Please use reverse-++ instead." |
| 1604 |
#-} |
| 1605 |
|
| 1606 |
zipWith-identityˡ = zipWith-zeroˡ |
| 1607 |
{-# WARNING_ON_USAGE zipWith-identityˡ |
| 1608 |
"Warning: zipWith-identityˡ was deprecated in v2.0. |
| 1609 |
Please use zipWith-zeroˡ instead." |
| 1610 |
#-} |
| 1611 |
|
| 1612 |
zipWith-identityʳ = zipWith-zeroʳ |
| 1613 |
{-# WARNING_ON_USAGE zipWith-identityʳ |
| 1614 |
"Warning: zipWith-identityʳ was deprecated in v2.0. |
| 1615 |
Please use zipWith-zeroʳ instead." |
| 1616 |
#-} |
| 1617 |
|
| 1618 |
ʳ++-++ = ++-ʳ++ |
| 1619 |
{-# WARNING_ON_USAGE ʳ++-++ |
| 1620 |
"Warning: ʳ++-++ was deprecated in v2.0. |
| 1621 |
Please use ++-ʳ++ instead." |
| 1622 |
#-} |
| 1623 |
|
| 1624 |
take++drop = take++drop≡id |
| 1625 |
{-# WARNING_ON_USAGE take++drop |
| 1626 |
"Warning: take++drop was deprecated in v2.0. |
| 1627 |
Please use take++drop≡id instead." |
| 1628 |
#-} |
| 1629 |
|
| 1630 |
length-─ = length-removeAt |
| 1631 |
{-# WARNING_ON_USAGE length-─ |
| 1632 |
"Warning: length-─ was deprecated in v2.0. |
| 1633 |
Please use length-removeAt instead." |
| 1634 |
#-} |
| 1635 |
|
| 1636 |
map-─ = map-removeAt |
| 1637 |
{-# WARNING_ON_USAGE map-─ |
| 1638 |
"Warning: map-─ was deprecated in v2.0. |
| 1639 |
Please use map-removeAt instead." |
| 1640 |
#-} |
| 1641 |
|
| 1642 |
-- Version 2.1 |
| 1643 |
|
| 1644 |
scanr-defn : ∀ (f : A → B → B) (e : B) → |
| 1645 |
scanr f e ≗ map (foldr f e) ∘ tails |
| 1646 |
scanr-defn f e [] = refl |
| 1647 |
scanr-defn f e (x ∷ []) = refl |
| 1648 |
scanr-defn f e (x ∷ xs@(_ ∷ _)) |
| 1649 |
with eq ← scanr-defn f e xs |
| 1650 |
with ys@(_ ∷ _) ← scanr f e xs |
| 1651 |
= cong₂ (λ z → f x z ∷_) (∷-injectiveˡ eq) eq |
| 1652 |
{-# WARNING_ON_USAGE scanr-defn |
| 1653 |
"Warning: scanr-defn was deprecated in v2.1. |
| 1654 |
Please use Data.List.Scans.Properties.scanr-defn instead." |
| 1655 |
#-} |
| 1656 |
|
| 1657 |
scanl-defn : ∀ (f : A → B → A) (e : A) → |
| 1658 |
scanl f e ≗ map (foldl f e) ∘ inits |
| 1659 |
scanl-defn f e [] = refl |
| 1660 |
scanl-defn f e (x ∷ xs) = cong (e ∷_) (begin |
| 1661 |
scanl f (f e x) xs |
| 1662 |
≡⟨ scanl-defn f (f e x) xs ⟩ |
| 1663 |
map (foldl f (f e x)) (inits xs) |
| 1664 |
≡⟨ refl ⟩ |
| 1665 |
map (foldl f e ∘ (x ∷_)) (inits xs) |
| 1666 |
≡⟨ map-∘ (inits xs) ⟩ |
| 1667 |
map (foldl f e) (map (x ∷_) (inits xs)) |
| 1668 |
∎) |
| 1669 |
{-# WARNING_ON_USAGE scanl-defn |
| 1670 |
"Warning: scanl-defn was deprecated in v2.1. |
| 1671 |
Please use Data.List.Scans.Properties.scanl-defn instead." |
| 1672 |
#-} |
| 1673 |
|
| 1674 |
-- Version 2.2 |
| 1675 |
|
| 1676 |
concat-[-] = concat-map-[_] |
| 1677 |
{-# WARNING_ON_USAGE concat-[-] |
| 1678 |
"Warning: concat-[-] was deprecated in v2.2. |
| 1679 |
Please use concat-map-[_] instead." |
| 1680 |
#-} |
| 1681 |
|
| 1682 |
-- Version 2.3 |
| 1683 |
|
| 1684 |
sum-++ : ∀ xs ys → sum (xs ++ ys) ≡ sum xs + sum ys |
| 1685 |
sum-++ [] ys = refl |
| 1686 |
sum-++ (x ∷ xs) ys = begin |
| 1687 |
x + sum (xs ++ ys) ≡⟨ cong (x +_) (sum-++ xs ys) ⟩ |
| 1688 |
x + (sum xs + sum ys) ≡⟨ +-assoc x _ _ ⟨ |
| 1689 |
(x + sum xs) + sum ys ∎ |
| 1690 |
{-# WARNING_ON_USAGE sum-++ |
| 1691 |
"Warning: sum-++ was deprecated in v2.3. |
| 1692 |
Please use Data.Nat.ListAction.Properties.sum-++ instead." |
| 1693 |
#-} |
| 1694 |
sum-++-commute = sum-++ |
| 1695 |
{-# WARNING_ON_USAGE sum-++-commute |
| 1696 |
"Warning: sum-++-commute was deprecated in v2.0. |
| 1697 |
Please use Data.Nat.ListAction.Properties.sum-++ instead." |
| 1698 |
#-} |
| 1699 |
|
| 1700 |
open import Data.List.Membership.Propositional using (_∈_) |
| 1701 |
open import Data.Nat.Divisibility using (_∣_; m∣m*n; ∣n⇒∣m*n) |
| 1702 |
|
| 1703 |
∈⇒∣product : ∀ {n ns} → n ∈ ns → n ∣ product ns |
| 1704 |
∈⇒∣product {ns = n ∷ ns} (here refl) = m∣m*n (product ns) |
| 1705 |
∈⇒∣product {ns = m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns) |
| 1706 |
{-# WARNING_ON_USAGE ∈⇒∣product |
| 1707 |
"Warning: ∈⇒∣product was deprecated in v2.3. |
| 1708 |
Please use Data.Nat.ListAction.Properties.∈⇒∣product instead." |
| 1709 |
#-} |
| 1710 |
|
| 1711 |
product≢0 : ∀ {ns} → All NonZero ns → NonZero (product ns) |
| 1712 |
product≢0 [] = _ |
| 1713 |
product≢0 {n ∷ ns} (n≢0 ∷ ns≢0) = m*n≢0 n (product ns) {{n≢0}} {{product≢0 ns≢0}} |
| 1714 |
{-# WARNING_ON_USAGE product≢0 |
| 1715 |
"Warning: product≢0 was deprecated in v2.3. |
| 1716 |
Please use Data.Nat.ListAction.Properties.product≢0 instead." |
| 1717 |
#-} |
| 1718 |
|
| 1719 |
∈⇒≤product : ∀ {n ns} → All NonZero ns → n ∈ ns → n ≤ product ns |
| 1720 |
∈⇒≤product {ns = n ∷ ns} (_ ∷ ns≢0) (here refl) = |
| 1721 |
m≤m*n n (product ns) {{product≢0 ns≢0}} |
| 1722 |
∈⇒≤product {ns = n ∷ _} (n≢0 ∷ ns≢0) (there n∈ns) = |
| 1723 |
m≤n⇒m≤o*n n {{n≢0}} (∈⇒≤product ns≢0 n∈ns) |
| 1724 |
{-# WARNING_ON_USAGE ∈⇒≤product |
| 1725 |
"Warning: ∈⇒≤product was deprecated in v2.3. |
| 1726 |
Please use Data.Nat.ListAction.Properties.∈⇒≤product instead." |
| 1727 |
#-} |