Debian Codegraph @

src/Codata/Guarded/Stream/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of infinite streams defined as coinductive records
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --safe --cubical-compatible --guardedness #-}
8
9 module Codata.Guarded.Stream.Properties where
10
11 open import Codata.Guarded.Stream
12 open import Codata.Guarded.Stream.Relation.Binary.Pointwise
13 as B using (_≈_; head; tail; module ≈-Reasoning)
14 open import Data.List.Base as List using (List; []; _∷_)
15 open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_)
16 open import Data.Nat.Base using (ℕ; zero; suc; _+_; _*_)
17 import Data.Nat.GeneralisedArithmetic as ℕ
18 open import Data.Product.Base as Prod using (_×_; _,_; proj₁; proj₂)
19 open import Data.Vec.Base as Vec using (Vec; _∷_)
20 open import Function.Base using (const; flip; id; _∘′_; _$′_; _⟨_⟩_; _∘₂′_)
21 open import Level using (Level)
22 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong; cong₂)
23 open import Relation.Binary.PropositionalEquality.Properties
24 using (module ≡-Reasoning)
25
26 private
27 variable
28 a b c d : Level
29 A : Set a
30 B : Set b
31 C : Set c
32 D : Set d
33
34 ------------------------------------------------------------------------
35 -- Congruence
36
37 cong-lookup : ∀ {as bs : Stream A} → as ≈ bs → ∀ n → lookup as n ≡ lookup bs n
38 cong-lookup = B.lookup⁺
39
40 cong-take : ∀ n {as bs : Stream A} → as ≈ bs → take n as ≡ take n bs
41 cong-take zero as≈bs = refl
42 cong-take (suc n) as≈bs = cong₂ _∷_ (as≈bs .head) (cong-take n (as≈bs .tail))
43
44 cong-drop : ∀ n {as bs : Stream A} → as ≈ bs → drop n as ≈ drop n bs
45 cong-drop = B.drop⁺
46
47 -- This is not map⁺ because the propositional equality relation is
48 -- not the same on the input and output
49 cong-map : ∀ (f : A → B) {as bs} → as ≈ bs → map f as ≈ map f bs
50 cong-map f as≈bs .head = cong f (as≈bs .head)
51 cong-map f as≈bs .tail = cong-map f (as≈bs .tail)
52
53 cong-zipWith : ∀ (f : A → B → C) {as bs cs ds} → as ≈ bs → cs ≈ ds →
54 zipWith f as cs ≈ zipWith f bs ds
55 cong-zipWith f as≈bs cs≈ds .head = cong₂ f (as≈bs .head) (cs≈ds .head)
56 cong-zipWith f as≈bs cs≈ds .tail = cong-zipWith f (as≈bs .tail) (cs≈ds .tail)
57
58 cong-concat : {ass bss : Stream (List⁺ A)} → ass ≈ bss → concat ass ≈ concat bss
59 cong-concat ass≈bss = cong-++-concat [] ass≈bss
60 where
61 open Concat
62 cong-++-concat : ∀ (as : List A) {ass bss} → ass ≈ bss → ++-concat as ass ≈ ++-concat as bss
63 cong-++-concat [] ass≈bss .head = cong List⁺.head (ass≈bss .head)
64 cong-++-concat [] ass≈bss .tail rewrite ass≈bss .head = cong-++-concat _ (ass≈bss .tail)
65 cong-++-concat (a ∷ as) ass≈bss .head = refl
66 cong-++-concat (a ∷ as) ass≈bss .tail = cong-++-concat as ass≈bss
67
68 cong-interleave : {as bs cs ds : Stream A} → as ≈ bs → cs ≈ ds →
69 interleave as cs ≈ interleave bs ds
70 cong-interleave as≈bs cs≈ds .head = as≈bs .head
71 cong-interleave as≈bs cs≈ds .tail = cong-interleave cs≈ds (as≈bs .tail)
72
73 cong-chunksOf : ∀ n {as bs : Stream A} → as ≈ bs → chunksOf n as ≈ chunksOf n bs
74 cong-chunksOf n as≈bs .head = cong-take n as≈bs
75 cong-chunksOf n as≈bs .tail = cong-chunksOf n (cong-drop n as≈bs)
76
77 ------------------------------------------------------------------------
78 -- Properties of repeat
79
80 lookup-repeat : ∀ n (a : A) → lookup (repeat a) n ≡ a
81 lookup-repeat zero a = refl
82 lookup-repeat (suc n) a = lookup-repeat n a
83
84 splitAt-repeat : ∀ n (a : A) → splitAt n (repeat a) ≡ (Vec.replicate n a , repeat a)
85 splitAt-repeat zero a = refl
86 splitAt-repeat (suc n) a = cong (Prod.map₁ (a ∷_)) (splitAt-repeat n a)
87
88 take-repeat : ∀ n (a : A) → take n (repeat a) ≡ Vec.replicate n a
89 take-repeat n a = cong proj₁ (splitAt-repeat n a)
90
91 drop-repeat : ∀ n (a : A) → drop n (repeat a) ≡ repeat a
92 drop-repeat n a = cong proj₂ (splitAt-repeat n a)
93
94 map-repeat : ∀ (f : A → B) a → map f (repeat a) ≈ repeat (f a)
95 map-repeat f a .head = refl
96 map-repeat f a .tail = map-repeat f a
97
98 ap-repeat : ∀ (f : A → B) a → ap (repeat f) (repeat a) ≈ repeat (f a)
99 ap-repeat f a .head = refl
100 ap-repeat f a .tail = ap-repeat f a
101
102 ap-repeatˡ : ∀ (f : A → B) as → ap (repeat f) as ≈ map f as
103 ap-repeatˡ f as .head = refl
104 ap-repeatˡ f as .tail = ap-repeatˡ f (as .tail)
105
106 ap-repeatʳ : ∀ (fs : Stream (A → B)) a → ap fs (repeat a) ≈ map (_$′ a) fs
107 ap-repeatʳ fs a .head = refl
108 ap-repeatʳ fs a .tail = ap-repeatʳ (fs .tail) a
109
110 interleave-repeat : (a : A) → interleave (repeat a) (repeat a) ≈ repeat a
111 interleave-repeat a .head = refl
112 interleave-repeat a .tail = interleave-repeat a
113
114 zipWith-repeat : ∀ (f : A → B → C) a b →
115 zipWith f (repeat a) (repeat b) ≈ repeat (f a b)
116 zipWith-repeat f a b .head = refl
117 zipWith-repeat f a b .tail = zipWith-repeat f a b
118
119 chunksOf-repeat : ∀ n (a : A) → chunksOf n (repeat a) ≈ repeat (Vec.replicate n a)
120 chunksOf-repeat n a = begin go where
121
122 open ≈-Reasoning
123
124 go : chunksOf n (repeat a) ≈∞ repeat (Vec.replicate n a)
125 go .head = take-repeat n a
126 go .tail =
127 chunksOf n (drop n (repeat a)) ≡⟨ cong (chunksOf n) (drop-repeat n a) ⟩
128 chunksOf n (repeat a) ↺⟨ go ⟩
129 repeat (Vec.replicate n a) ∎
130
131 ------------------------------------------------------------------------
132 -- Properties of map
133
134 map-const : (a : A) (bs : Stream B) → map (const a) bs ≈ repeat a
135 map-const a bs .head = refl
136 map-const a bs .tail = map-const a (bs .tail)
137
138 map-id : (as : Stream A) → map id as ≈ as
139 map-id as .head = refl
140 map-id as .tail = map-id (as .tail)
141
142 map-∘ : ∀ (g : B → C) (f : A → B) as → map g (map f as) ≈ map (g ∘′ f) as
143 map-∘ g f as .head = refl
144 map-∘ g f as .tail = map-∘ g f (as .tail)
145
146 map-unfold : ∀ (g : B → C) (f : A → A × B) a →
147 map g (unfold f a) ≈ unfold (Prod.map₂ g ∘′ f) a
148 map-unfold g f a .head = refl
149 map-unfold g f a .tail = map-unfold g f (proj₁ (f a))
150
151 map-drop : ∀ (f : A → B) n as → map f (drop n as) ≡ drop n (map f as)
152 map-drop f zero as = refl
153 map-drop f (suc n) as = map-drop f n (as .tail)
154
155 map-zipWith : ∀ (g : C → D) (f : A → B → C) as bs →
156 map g (zipWith f as bs) ≈ zipWith (g ∘₂′ f) as bs
157 map-zipWith g f as bs .head = refl
158 map-zipWith g f as bs .tail = map-zipWith g f (as .tail) (bs .tail)
159
160 map-interleave : ∀ (f : A → B) as bs →
161 map f (interleave as bs) ≈ interleave (map f as) (map f bs)
162 map-interleave f as bs .head = refl
163 map-interleave f as bs .tail = map-interleave f bs (as .tail)
164
165 map-concat : ∀ (f : A → B) ass → map f (concat ass) ≈ concat (map (List⁺.map f) ass)
166 map-concat f ass = map-++-concat [] ass
167 where
168 open Concat
169 map-++-concat : ∀ acc ass → map f (++-concat acc ass) ≈ ++-concat (List.map f acc) (map (List⁺.map f) ass)
170 map-++-concat [] ass .head = refl
171 map-++-concat [] ass .tail = map-++-concat (ass .head .List⁺.tail) (ass .tail)
172 map-++-concat (a ∷ as) ass .head = refl
173 map-++-concat (a ∷ as) ass .tail = map-++-concat as ass
174
175 map-cycle : ∀ (f : A → B) as → map f (cycle as) ≈ cycle (List⁺.map f as)
176 map-cycle f as = run
177 (map f (cycle as) ≡⟨⟩
178 map f (concat (repeat as)) ≈⟨ map-concat f (repeat as) ⟩
179 concat (map (List⁺.map f) (repeat as)) ≈⟨ cong-concat (map-repeat (List⁺.map f) as) ⟩
180 concat (repeat (List⁺.map f as)) ≡⟨⟩
181 cycle (List⁺.map f as) ∎)
182 where open ≈-Reasoning
183
184 ------------------------------------------------------------------------
185 -- Properties of lookup
186
187 lookup-drop : ∀ m (as : Stream A) n → lookup (drop m as) n ≡ lookup as (m + n)
188 lookup-drop zero as n = refl
189 lookup-drop (suc m) as n = lookup-drop m (as .tail) n
190
191 lookup-map : ∀ n (f : A → B) as → lookup (map f as) n ≡ f (lookup as n)
192 lookup-map zero f as = refl
193 lookup-map (suc n) f as = lookup-map n f (as . tail)
194
195 lookup-iterate : ∀ n f (x : A) → lookup (iterate f x) n ≡ ℕ.iterate f x n
196 lookup-iterate zero f x = refl
197 lookup-iterate (suc n) f x = lookup-iterate n f (f x)
198
199 lookup-zipWith : ∀ n (f : A → B → C) as bs →
200 lookup (zipWith f as bs) n ≡ f (lookup as n) (lookup bs n)
201 lookup-zipWith zero f as bs = refl
202 lookup-zipWith (suc n) f as bs = lookup-zipWith n f (as .tail) (bs .tail)
203
204 lookup-unfold : ∀ n (f : A → A × B) a →
205 lookup (unfold f a) n ≡ proj₂ (f (ℕ.iterate (proj₁ ∘′ f) a n))
206 lookup-unfold zero f a = refl
207 lookup-unfold (suc n) f a = lookup-unfold n f (proj₁ (f a))
208
209 lookup-tabulate : ∀ n (f : ℕ → A) → lookup (tabulate f) n ≡ f n
210 lookup-tabulate zero f = refl
211 lookup-tabulate (suc n) f = lookup-tabulate n (f ∘′ suc)
212
213 lookup-transpose : ∀ n (ass : List (Stream A)) →
214 lookup (transpose ass) n ≡ List.map (flip lookup n) ass
215 lookup-transpose n [] = lookup-repeat n []
216 lookup-transpose n (as ∷ ass) = begin
217 lookup (transpose (as ∷ ass)) n ≡⟨⟩
218 lookup (zipWith _∷_ as (transpose ass)) n ≡⟨ lookup-zipWith n _∷_ as (transpose ass) ⟩
219 lookup as n ∷ lookup (transpose ass) n ≡⟨ cong (lookup as n ∷_) (lookup-transpose n ass) ⟩
220 lookup as n ∷ List.map (flip lookup n) ass ≡⟨⟩
221 List.map (flip lookup n) (as ∷ ass) ∎
222 where open ≡-Reasoning
223
224 lookup-transpose⁺ : ∀ n (ass : List⁺ (Stream A)) →
225 lookup (transpose⁺ ass) n ≡ List⁺.map (flip lookup n) ass
226 lookup-transpose⁺ n (as ∷ ass) = begin
227 lookup (transpose⁺ (as ∷ ass)) n ≡⟨⟩
228 lookup (zipWith _∷_ as (transpose ass)) n ≡⟨ lookup-zipWith n _∷_ as (transpose ass) ⟩
229 lookup as n ∷ lookup (transpose ass) n ≡⟨ cong (lookup as n ∷_) (lookup-transpose n ass) ⟩
230 lookup as n ∷ List.map (flip lookup n) ass ≡⟨⟩
231 List⁺.map (flip lookup n) (as ∷ ass) ∎
232 where open ≡-Reasoning
233
234 lookup-tails : ∀ n (as : Stream A) → lookup (tails as) n ≈ ℕ.iterate tail as n
235 lookup-tails zero as = B.refl
236 lookup-tails (suc n) as = lookup-tails n (as .tail)
237
238 lookup-evens : ∀ n (as : Stream A) → lookup (evens as) n ≡ lookup as (n * 2)
239 lookup-evens zero as = refl
240 lookup-evens (suc n) as = lookup-evens n (as .tail .tail)
241
242 lookup-odds : ∀ n (as : Stream A) → lookup (odds as) n ≡ lookup as (suc (n * 2))
243 lookup-odds zero as = refl
244 lookup-odds (suc n) as = lookup-odds n (as .tail .tail)
245
246 lookup-interleave-even : ∀ n (as bs : Stream A) →
247 lookup (interleave as bs) (n * 2) ≡ lookup as n
248 lookup-interleave-even zero as bs = refl
249 lookup-interleave-even (suc n) as bs = lookup-interleave-even n (as .tail) (bs .tail)
250
251 lookup-interleave-odd : ∀ n (as bs : Stream A) →
252 lookup (interleave as bs) (suc (n * 2)) ≡ lookup bs n
253 lookup-interleave-odd zero as bs = refl
254 lookup-interleave-odd (suc n) as bs = lookup-interleave-odd n (as .tail) (bs .tail)
255
256 ------------------------------------------------------------------------
257 -- Properties of take
258
259 take-iterate : ∀ n f (x : A) → take n (iterate f x) ≡ Vec.iterate f x n
260 take-iterate zero f x = refl
261 take-iterate (suc n) f x = cong (x ∷_) (take-iterate n f (f x))
262
263 take-zipWith : ∀ n (f : A → B → C) as bs →
264 take n (zipWith f as bs) ≡ Vec.zipWith f (take n as) (take n bs)
265 take-zipWith zero f as bs = refl
266 take-zipWith (suc n) f as bs =
267 cong (f (as .head) (bs .head) ∷_) (take-zipWith n f (as .tail) (bs . tail))
268
269 ------------------------------------------------------------------------
270 -- Properties of drop
271
272 drop-drop : ∀ m n (as : Stream A) → drop n (drop m as) ≡ drop (m + n) as
273 drop-drop zero n as = refl
274 drop-drop (suc m) n as = drop-drop m n (as .tail)
275
276 drop-zipWith : ∀ n (f : A → B → C) as bs →
277 drop n (zipWith f as bs) ≡ zipWith f (drop n as) (drop n bs)
278 drop-zipWith zero f as bs = refl
279 drop-zipWith (suc n) f as bs = drop-zipWith n f (as .tail) (bs .tail)
280
281 drop-ap : ∀ n (fs : Stream (A → B)) as →
282 drop n (ap fs as) ≡ ap (drop n fs) (drop n as)
283 drop-ap zero fs as = refl
284 drop-ap (suc n) fs as = drop-ap n (fs .tail) (as .tail)
285
286 drop-iterate : ∀ n f (x : A) → drop n (iterate f x) ≡ iterate f (ℕ.iterate f x n)
287 drop-iterate zero f x = refl
288 drop-iterate (suc n) f x = drop-iterate n f (f x)
289
290 ------------------------------------------------------------------------
291 -- Properties of zipWith
292
293 zipWith-defn : ∀ (f : A → B → C) as bs →
294 zipWith f as bs ≈ (repeat f ⟨ ap ⟩ as ⟨ ap ⟩ bs)
295 zipWith-defn f as bs .head = refl
296 zipWith-defn f as bs .tail = zipWith-defn f (as .tail) (bs .tail)
297
298 zipWith-const : (as : Stream A) (bs : Stream B) →
299 zipWith const as bs ≈ as
300 zipWith-const as bs .head = refl
301 zipWith-const as bs .tail = zipWith-const (as .tail) (bs .tail)
302
303 zipWith-flip : ∀ (f : A → B → C) as bs →
304 zipWith (flip f) as bs ≈ zipWith f bs as
305 zipWith-flip f as bs .head = refl
306 zipWith-flip f as bs .tail = zipWith-flip f (as .tail) (bs. tail)
307
308 ------------------------------------------------------------------------
309 -- Properties of interleave
310
311 interleave-evens-odds : (as : Stream A) → interleave (evens as) (odds as) ≈ as
312 interleave-evens-odds as .head = refl
313 interleave-evens-odds as .tail .head = refl
314 interleave-evens-odds as .tail .tail = interleave-evens-odds (as .tail .tail)
315
316 ------------------------------------------------------------------------
317 -- DEPRECATED
318 ------------------------------------------------------------------------
319 -- Please use the new names as continuing support for the old names is
320 -- not guaranteed.
321
322 -- Version 2.0
323
324 map-identity = map-id
325 {-# WARNING_ON_USAGE map-identity
326 "Warning: map-identity was deprecated in v2.0.
327 Please use map-id instead."
328 #-}
329
330 map-fusion = map-∘
331 {-# WARNING_ON_USAGE map-fusion
332 "Warning: map-fusion was deprecated in v2.0.
333 Please use map-∘ instead."
334 #-}
335
336 drop-fusion = drop-drop
337 {-# WARNING_ON_USAGE drop-fusion
338 "Warning: drop-fusion was deprecated in v2.0.
339 Please use drop-drop instead."
340 #-}