| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of operations on the Colist type |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 8 |
|
| 9 |
module Codata.Sized.Colist.Properties where |
| 10 |
|
| 11 |
open import Level using (Level) |
| 12 |
open import Size using (Size; ∞) |
| 13 |
open import Codata.Sized.Thunk as Thunk using (Thunk; force) |
| 14 |
open import Codata.Sized.Colist |
| 15 |
open import Codata.Sized.Colist.Bisimilarity |
| 16 |
open import Codata.Sized.Conat using |
| 17 |
(Conat; zero; suc; _+_; _⊔_; _⊓_; _∸_; fromℕ; _ℕ+_) |
| 18 |
open import Codata.Sized.Conat.Bisimilarity as Conat using (zero; suc) |
| 19 |
import Codata.Sized.Conat.Properties as Conat using (0∸m≈0) |
| 20 |
open import Codata.Sized.Cowriter as Cowriter using ([_]; _∷_) |
| 21 |
open import Codata.Sized.Cowriter.Bisimilarity as Cowriter using ([_]; _∷_) |
| 22 |
open import Codata.Sized.Stream as Stream using (Stream; _∷_) |
| 23 |
open import Data.Vec.Bounded as Vec≤ using (Vec≤) |
| 24 |
open import Data.List.Base as List using (List; []; _∷_) |
| 25 |
open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_) |
| 26 |
import Data.List.Scans.Base as Scans using (scanl) |
| 27 |
open import Data.List.Relation.Binary.Equality.Propositional using (≋-refl) |
| 28 |
open import Data.Maybe.Base as Maybe using (Maybe; nothing; just) |
| 29 |
import Data.Maybe.Properties as Maybe using (map-nothing; map-just) |
| 30 |
open import Data.Maybe.Relation.Unary.All using (All; nothing; just) |
| 31 |
open import Data.Nat.Base as ℕ using (zero; suc; z≤n; s≤s) |
| 32 |
open import Data.Product.Base as Product using (_×_; _,_; uncurry) |
| 33 |
open import Data.These.Base as These using (These; this; that; these) |
| 34 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_) |
| 35 |
open import Function.Base using (const; id; _∘_; _∘′_; flip; _on_) |
| 36 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 37 |
|
| 38 |
private |
| 39 |
variable |
| 40 |
a b c d : Level |
| 41 |
A : Set a |
| 42 |
B : Set b |
| 43 |
C : Set c |
| 44 |
D : Set d |
| 45 |
i : Size |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Functor laws |
| 49 |
|
| 50 |
map-id : ∀ (as : Colist A ∞) → i ⊢ map id as ≈ as |
| 51 |
map-id [] = [] |
| 52 |
map-id (a ∷ as) = ≡.refl ∷ λ where .force → map-id (as .force) |
| 53 |
|
| 54 |
map-∘ : ∀ (f : A → B) (g : B → C) as {i} → |
| 55 |
i ⊢ map g (map f as) ≈ map (g ∘ f) as |
| 56 |
map-∘ f g [] = [] |
| 57 |
map-∘ f g (a ∷ as) = |
| 58 |
≡.refl ∷ λ where .force → map-∘ f g (as .force) |
| 59 |
|
| 60 |
------------------------------------------------------------------------ |
| 61 |
-- Relation to Cowriter |
| 62 |
|
| 63 |
fromCowriter∘toCowriter≗id : ∀ (as : Colist A ∞) → |
| 64 |
i ⊢ fromCowriter (toCowriter as) ≈ as |
| 65 |
fromCowriter∘toCowriter≗id [] = [] |
| 66 |
fromCowriter∘toCowriter≗id (a ∷ as) = |
| 67 |
≡.refl ∷ λ where .force → fromCowriter∘toCowriter≗id (as .force) |
| 68 |
|
| 69 |
------------------------------------------------------------------------ |
| 70 |
-- Properties of length |
| 71 |
|
| 72 |
length-∷ : ∀ (a : A) as → i Conat.⊢ length (a ∷ as) ≈ 1 ℕ+ length (as .force) |
| 73 |
length-∷ a as = suc (λ where .force → Conat.refl) |
| 74 |
|
| 75 |
length-replicate : ∀ n (a : A) → i Conat.⊢ length (replicate n a) ≈ n |
| 76 |
length-replicate zero a = zero |
| 77 |
length-replicate (suc n) a = suc λ where .force → length-replicate (n .force) a |
| 78 |
|
| 79 |
length-++ : (as bs : Colist A ∞) → |
| 80 |
i Conat.⊢ length (as ++ bs) ≈ length as + length bs |
| 81 |
length-++ [] bs = Conat.refl |
| 82 |
length-++ (a ∷ as) bs = suc λ where .force → length-++ (as .force) bs |
| 83 |
|
| 84 |
length-map : ∀ (f : A → B) as → i Conat.⊢ length (map f as) ≈ length as |
| 85 |
length-map f [] = zero |
| 86 |
length-map f (a ∷ as) = suc λ where .force → length-map f (as .force) |
| 87 |
|
| 88 |
------------------------------------------------------------------------ |
| 89 |
-- Properties of replicate |
| 90 |
|
| 91 |
replicate-+ : ∀ m n (a : A) → |
| 92 |
i ⊢ replicate (m + n) a ≈ replicate m a ++ replicate n a |
| 93 |
replicate-+ zero n a = refl |
| 94 |
replicate-+ (suc m) n a = ≡.refl ∷ λ where .force → replicate-+ (m .force) n a |
| 95 |
|
| 96 |
map-replicate : ∀ (f : A → B) n a → |
| 97 |
i ⊢ map f (replicate n a) ≈ replicate n (f a) |
| 98 |
map-replicate f zero a = [] |
| 99 |
map-replicate f (suc n) a = |
| 100 |
≡.refl ∷ λ where .force → map-replicate f (n .force) a |
| 101 |
|
| 102 |
lookup-replicate : ∀ k n (a : A) → All (a ≡_) (lookup (replicate n a) k) |
| 103 |
lookup-replicate k zero a = nothing |
| 104 |
lookup-replicate zero (suc n) a = just ≡.refl |
| 105 |
lookup-replicate (suc k) (suc n) a = lookup-replicate k (n .force) a |
| 106 |
|
| 107 |
------------------------------------------------------------------------ |
| 108 |
-- Properties of unfold |
| 109 |
|
| 110 |
map-unfold : ∀ (f : B → C) (alg : A → Maybe (A × B)) a → |
| 111 |
i ⊢ map f (unfold alg a) ≈ unfold (Maybe.map (Product.map₂ f) ∘ alg) a |
| 112 |
map-unfold f alg a with alg a |
| 113 |
... | nothing = [] |
| 114 |
... | just (a′ , b) = ≡.refl ∷ λ where .force → map-unfold f alg a′ |
| 115 |
|
| 116 |
module _ {alg : A → Maybe (A × B)} {a} where |
| 117 |
|
| 118 |
unfold-nothing : alg a ≡ nothing → unfold alg a ≡ [] |
| 119 |
unfold-nothing eq with alg a |
| 120 |
... | nothing = ≡.refl |
| 121 |
|
| 122 |
unfold-just : ∀ {a′ b} → alg a ≡ just (a′ , b) → |
| 123 |
i ⊢ unfold alg a ≈ b ∷ λ where .force → unfold alg a′ |
| 124 |
unfold-just eq with alg a |
| 125 |
unfold-just ≡.refl | just (a′ , b) = ≡.refl ∷ λ where .force → refl |
| 126 |
|
| 127 |
------------------------------------------------------------------------ |
| 128 |
-- Properties of scanl |
| 129 |
|
| 130 |
length-scanl : ∀ (c : B → A → B) n as → |
| 131 |
i Conat.⊢ length (scanl c n as) ≈ 1 ℕ+ length as |
| 132 |
length-scanl c n [] = suc λ where .force → zero |
| 133 |
length-scanl c n (a ∷ as) = suc λ { .force → begin |
| 134 |
length (scanl c (c n a) (as .force)) |
| 135 |
≈⟨ length-scanl c (c n a) (as .force) ⟩ |
| 136 |
1 ℕ+ length (as .force) |
| 137 |
≈⟨ length-∷ a as ⟨ |
| 138 |
length (a ∷ as) ∎ } where open Conat.≈-Reasoning |
| 139 |
|
| 140 |
module _ (cons : C → B → C) (alg : A → Maybe (A × B)) where |
| 141 |
|
| 142 |
private |
| 143 |
alg′ : (A × C) → Maybe ((A × C) × C) |
| 144 |
alg′ (a , c) = Maybe.map (uncurry step) (alg a) where |
| 145 |
step = λ a′ b → let b′ = cons c b in (a′ , b′) , b′ |
| 146 |
|
| 147 |
scanl-unfold : ∀ nil a → i ⊢ scanl cons nil (unfold alg a) |
| 148 |
≈ nil ∷ (λ where .force → unfold alg′ (a , nil)) |
| 149 |
scanl-unfold nil a with alg a in eq |
| 150 |
... | nothing = ≡.refl ∷ λ { .force → |
| 151 |
sym (fromEq (unfold-nothing (Maybe.map-nothing eq))) } |
| 152 |
... | just (a′ , b) = ≡.refl ∷ λ { .force → begin |
| 153 |
scanl cons (cons nil b) (unfold alg a′) |
| 154 |
≈⟨ scanl-unfold (cons nil b) a′ ⟩ |
| 155 |
(cons nil b ∷ _) |
| 156 |
≈⟨ ≡.refl ∷ (λ where .force → refl) ⟩ |
| 157 |
(cons nil b ∷ _) |
| 158 |
≈⟨ unfold-just (Maybe.map-just eq) ⟨ |
| 159 |
unfold alg′ (a , nil) ∎ } where open ≈-Reasoning |
| 160 |
|
| 161 |
------------------------------------------------------------------------ |
| 162 |
-- Properties of alignwith |
| 163 |
|
| 164 |
map-alignWith : ∀ (f : C → D) (al : These A B → C) as bs → |
| 165 |
i ⊢ map f (alignWith al as bs) ≈ alignWith (f ∘ al) as bs |
| 166 |
map-alignWith f al [] bs = map-∘ (al ∘′ that) f bs |
| 167 |
map-alignWith f al as@(_ ∷ _) [] = map-∘ (al ∘′ this) f as |
| 168 |
map-alignWith f al (a ∷ as) (b ∷ bs) = |
| 169 |
≡.refl ∷ λ where .force → map-alignWith f al (as .force) (bs .force) |
| 170 |
|
| 171 |
length-alignWith : ∀ (al : These A B → C) as bs → |
| 172 |
i Conat.⊢ length (alignWith al as bs) ≈ length as ⊔ length bs |
| 173 |
length-alignWith al [] bs = length-map (al ∘ that) bs |
| 174 |
length-alignWith al as@(_ ∷ _) [] = length-map (al ∘ this) as |
| 175 |
length-alignWith al (a ∷ as) (b ∷ bs) = |
| 176 |
suc λ where .force → length-alignWith al (as .force) (bs .force) |
| 177 |
|
| 178 |
------------------------------------------------------------------------ |
| 179 |
-- Properties of zipwith |
| 180 |
|
| 181 |
map-zipWith : ∀ (f : C → D) (zp : A → B → C) as bs → |
| 182 |
i ⊢ map f (zipWith zp as bs) ≈ zipWith (λ a → f ∘ zp a) as bs |
| 183 |
map-zipWith f zp [] _ = [] |
| 184 |
map-zipWith f zp (_ ∷ _) [] = [] |
| 185 |
map-zipWith f zp (a ∷ as) (b ∷ bs) = |
| 186 |
≡.refl ∷ λ where .force → map-zipWith f zp (as .force) (bs .force) |
| 187 |
|
| 188 |
length-zipWith : ∀ (zp : A → B → C) as bs → |
| 189 |
i Conat.⊢ length (zipWith zp as bs) ≈ length as ⊓ length bs |
| 190 |
length-zipWith zp [] bs = zero |
| 191 |
length-zipWith zp as@(_ ∷ _) [] = zero |
| 192 |
length-zipWith zp (a ∷ as) (b ∷ bs) = |
| 193 |
suc λ where .force → length-zipWith zp (as .force) (bs .force) |
| 194 |
|
| 195 |
------------------------------------------------------------------------ |
| 196 |
-- Properties of drop |
| 197 |
|
| 198 |
drop-nil : ∀ m → i ⊢ drop {A = A} m [] ≈ [] |
| 199 |
drop-nil zero = [] |
| 200 |
drop-nil (suc m) = [] |
| 201 |
|
| 202 |
drop-drop : ∀ m n (as : Colist A ∞) → |
| 203 |
i ⊢ drop n (drop m as) ≈ drop (m ℕ.+ n) as |
| 204 |
drop-drop zero n as = refl |
| 205 |
drop-drop (suc m) n [] = drop-nil n |
| 206 |
drop-drop (suc m) n (a ∷ as) = drop-drop m n (as .force) |
| 207 |
|
| 208 |
map-drop : ∀ (f : A → B) m as → i ⊢ map f (drop m as) ≈ drop m (map f as) |
| 209 |
map-drop f zero as = refl |
| 210 |
map-drop f (suc m) [] = [] |
| 211 |
map-drop f (suc m) (a ∷ as) = map-drop f m (as .force) |
| 212 |
|
| 213 |
length-drop : ∀ m (as : Colist A ∞) → i Conat.⊢ length (drop m as) ≈ length as ∸ m |
| 214 |
length-drop zero as = Conat.refl |
| 215 |
length-drop (suc m) [] = Conat.sym (Conat.0∸m≈0 m) |
| 216 |
length-drop (suc m) (a ∷ as) = length-drop m (as .force) |
| 217 |
|
| 218 |
drop-fromList-++-identity : ∀ (as : List A) bs → |
| 219 |
drop (List.length as) (fromList as ++ bs) ≡ bs |
| 220 |
drop-fromList-++-identity [] bs = ≡.refl |
| 221 |
drop-fromList-++-identity (a ∷ as) bs = drop-fromList-++-identity as bs |
| 222 |
|
| 223 |
drop-fromList-++-≤ : ∀ (as : List A) bs {m} → m ℕ.≤ List.length as → |
| 224 |
drop m (fromList as ++ bs) ≡ fromList (List.drop m as) ++ bs |
| 225 |
drop-fromList-++-≤ [] bs z≤n = ≡.refl |
| 226 |
drop-fromList-++-≤ (a ∷ as) bs z≤n = ≡.refl |
| 227 |
drop-fromList-++-≤ (a ∷ as) bs (s≤s p) = drop-fromList-++-≤ as bs p |
| 228 |
|
| 229 |
drop-fromList-++-≥ : ∀ (as : List A) bs {m} → m ℕ.≥ List.length as → |
| 230 |
drop m (fromList as ++ bs) ≡ drop (m ℕ.∸ List.length as) bs |
| 231 |
drop-fromList-++-≥ [] bs z≤n = ≡.refl |
| 232 |
drop-fromList-++-≥ (a ∷ as) bs (s≤s p) = drop-fromList-++-≥ as bs p |
| 233 |
|
| 234 |
drop-⁺++-identity : ∀ (as : List⁺ A) bs → |
| 235 |
drop (List⁺.length as) (as ⁺++ bs) ≡ bs .force |
| 236 |
drop-⁺++-identity (a ∷ as) bs = drop-fromList-++-identity as (bs .force) |
| 237 |
|
| 238 |
------------------------------------------------------------------------ |
| 239 |
-- Properties of cotake |
| 240 |
|
| 241 |
length-cotake : ∀ n (as : Stream A ∞) → i Conat.⊢ length (cotake n as) ≈ n |
| 242 |
length-cotake zero as = zero |
| 243 |
length-cotake (suc n) (a ∷ as) = |
| 244 |
suc λ where .force → length-cotake (n .force) (as .force) |
| 245 |
|
| 246 |
map-cotake : ∀ (f : A → B) n as → |
| 247 |
i ⊢ map f (cotake n as) ≈ cotake n (Stream.map f as) |
| 248 |
map-cotake f zero as = [] |
| 249 |
map-cotake f (suc n) (a ∷ as) = |
| 250 |
≡.refl ∷ λ where .force → map-cotake f (n .force) (as .force) |
| 251 |
|
| 252 |
------------------------------------------------------------------------ |
| 253 |
-- Properties of chunksOf |
| 254 |
|
| 255 |
module Map-ChunksOf (f : A → B) n where |
| 256 |
|
| 257 |
open ChunksOf n using (chunksOfAcc) |
| 258 |
|
| 259 |
map-chunksOf : ∀ as → |
| 260 |
i Cowriter.⊢ Cowriter.map (Vec.map f) (Vec≤.map f) (chunksOf n as) |
| 261 |
≈ chunksOf n (map f as) |
| 262 |
map-chunksOfAcc : ∀ m as {k≤ k≡ k≤′ k≡′} → |
| 263 |
(∀ vs → Vec≤.map f (k≤ vs) ≡ k≤′ (Vec≤.map f vs)) → |
| 264 |
(∀ vs → Vec.map f (k≡ vs) ≡ k≡′ (Vec.map f vs)) → |
| 265 |
i Cowriter.⊢ Cowriter.map (Vec.map f) (Vec≤.map f) |
| 266 |
(chunksOfAcc m k≤ k≡ as) |
| 267 |
≈ chunksOfAcc m k≤′ k≡′ (map f as) |
| 268 |
|
| 269 |
map-chunksOf as = map-chunksOfAcc n as (λ vs → ≡.refl) (λ vs → ≡.refl) |
| 270 |
|
| 271 |
map-chunksOfAcc zero as eq-≤ eq-≡ = |
| 272 |
eq-≡ [] ∷ λ where .force → map-chunksOf as |
| 273 |
map-chunksOfAcc (suc m) [] eq-≤ eq-≡ = Cowriter.[ eq-≤ Vec≤.[] ] |
| 274 |
map-chunksOfAcc (suc m) (a ∷ as) eq-≤ eq-≡ = |
| 275 |
map-chunksOfAcc m (as .force) (eq-≤ ∘ (a Vec≤.∷_)) (eq-≡ ∘ (a Vec.∷_)) |
| 276 |
|
| 277 |
open Map-ChunksOf using (map-chunksOf) public |
| 278 |
|
| 279 |
------------------------------------------------------------------------ |
| 280 |
-- Properties of fromList |
| 281 |
|
| 282 |
fromList-++ : (as bs : List A) → |
| 283 |
i ⊢ fromList (as List.++ bs) ≈ fromList as ++ fromList bs |
| 284 |
fromList-++ [] bs = refl |
| 285 |
fromList-++ (a ∷ as) bs = ≡.refl ∷ λ where .force → fromList-++ as bs |
| 286 |
|
| 287 |
fromList-scanl : ∀ (c : B → A → B) n as → |
| 288 |
i ⊢ fromList (Scans.scanl c n as) ≈ scanl c n (fromList as) |
| 289 |
fromList-scanl c n [] = ≡.refl ∷ λ where .force → refl |
| 290 |
fromList-scanl c n (a ∷ as) = |
| 291 |
≡.refl ∷ λ where .force → fromList-scanl c (c n a) as |
| 292 |
|
| 293 |
map-fromList : ∀ (f : A → B) as → |
| 294 |
i ⊢ map f (fromList as) ≈ fromList (List.map f as) |
| 295 |
map-fromList f [] = [] |
| 296 |
map-fromList f (a ∷ as) = ≡.refl ∷ λ where .force → map-fromList f as |
| 297 |
|
| 298 |
length-fromList : (as : List A) → |
| 299 |
i Conat.⊢ length (fromList as) ≈ fromℕ (List.length as) |
| 300 |
length-fromList [] = zero |
| 301 |
length-fromList (a ∷ as) = suc (λ where .force → length-fromList as) |
| 302 |
|
| 303 |
------------------------------------------------------------------------ |
| 304 |
-- Properties of fromStream |
| 305 |
|
| 306 |
fromStream-++ : ∀ (as : List A) bs → |
| 307 |
i ⊢ fromStream (as Stream.++ bs) ≈ fromList as ++ fromStream bs |
| 308 |
fromStream-++ [] bs = refl |
| 309 |
fromStream-++ (a ∷ as) bs = ≡.refl ∷ λ where .force → fromStream-++ as bs |
| 310 |
|
| 311 |
fromStream-⁺++ : ∀ (as : List⁺ A) bs → |
| 312 |
i ⊢ fromStream (as Stream.⁺++ bs) |
| 313 |
≈ fromList⁺ as ++ fromStream (bs .force) |
| 314 |
fromStream-⁺++ (a ∷ as) bs = |
| 315 |
≡.refl ∷ λ where .force → fromStream-++ as (bs .force) |
| 316 |
|
| 317 |
fromStream-concat : (ass : Stream (List⁺ A) ∞) → |
| 318 |
i ⊢ concat (fromStream ass) ≈ fromStream (Stream.concat ass) |
| 319 |
fromStream-concat (as@(a ∷ _) ∷ ass) = begin |
| 320 |
concat (fromStream (as ∷ ass)) |
| 321 |
≈⟨ ≡.refl ∷ (λ { .force → ++⁺ ≋-refl (fromStream-concat (ass .force))}) ⟩ |
| 322 |
a ∷ _ |
| 323 |
≈⟨ sym (fromStream-⁺++ as _) ⟩ |
| 324 |
fromStream (Stream.concat (as ∷ ass)) ∎ where open ≈-Reasoning |
| 325 |
|
| 326 |
fromStream-scanl : ∀ (c : B → A → B) n as → |
| 327 |
i ⊢ scanl c n (fromStream as) |
| 328 |
≈ fromStream (Stream.scanl c n as) |
| 329 |
fromStream-scanl c n (a ∷ as) = |
| 330 |
≡.refl ∷ λ where .force → fromStream-scanl c (c n a) (as .force) |
| 331 |
|
| 332 |
map-fromStream : ∀ (f : A → B) as → |
| 333 |
i ⊢ map f (fromStream as) ≈ fromStream (Stream.map f as) |
| 334 |
map-fromStream f (a ∷ as) = |
| 335 |
≡.refl ∷ λ where .force → map-fromStream f (as .force) |
| 336 |
|
| 337 |
------------------------------------------------------------------------ |
| 338 |
-- DEPRECATED |
| 339 |
------------------------------------------------------------------------ |
| 340 |
-- Please use the new names as continuing support for the old names is |
| 341 |
-- not guaranteed. |
| 342 |
|
| 343 |
-- Version 2.0 |
| 344 |
|
| 345 |
map-identity = map-id |
| 346 |
{-# WARNING_ON_USAGE map-identity |
| 347 |
"Warning: map-identity was deprecated in v2.0. |
| 348 |
Please use map-id instead." |
| 349 |
#-} |
| 350 |
|
| 351 |
map-map-fusion = map-∘ |
| 352 |
{-# WARNING_ON_USAGE map-map-fusion |
| 353 |
"Warning: map-map-fusion was deprecated in v2.0. |
| 354 |
Please use map-∘ instead." |
| 355 |
#-} |
| 356 |
|
| 357 |
drop-drop-fusion = drop-drop |
| 358 |
{-# WARNING_ON_USAGE drop-drop-fusion |
| 359 |
"Warning: drop-drop-fusion was deprecated in v2.0. |
| 360 |
Please use drop-drop instead." |
| 361 |
#-} |