Skip to content

Commit f7ccbe0

Browse files
gallaisMatthewDaggitt
authored andcommitted
[ new ] functions in Codata.Stream (#516)
1 parent bc49ac7 commit f7ccbe0

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ Deprecated features
6060
Other minor additions
6161
---------------------
6262

63+
* Added new functions to `Codata.Stream`:
64+
```agda
65+
splitAt : (n : ℕ) → Stream A ∞ → Vec A n × Stream A ∞
66+
drop : ℕ → Stream A ∞ → Stream A ∞
67+
interleave : Stream A i → Thunk (Stream A) i → Stream A i
68+
```
69+
70+
* Added new proof to `Codata.Stream.Properties`:
71+
```agda
72+
splitAt-map : splitAt n (map f xs) ≡ map (map f) (map f) (splitAt n xs)
73+
```
74+
6375
* Added new proof to `Algebra.FunctionProperties.Consequences`:
6476
```agda
6577
wlog : Commutative f → Total _R_ → (∀ a b → a R b → P (f a b)) → ∀ a b → P (f a b)

src/Codata/Stream.agda

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ module _ {ℓ} {A : Set ℓ} where
3636
lookup zero xs = head xs
3737
lookup (suc k) xs = lookup k (tail xs)
3838

39+
splitAt : (n : ℕ) Stream A ∞ Vec A n × Stream A ∞
40+
splitAt zero xs = [] , xs
41+
splitAt (suc n) (x ∷ xs) = P.map₁ (x ∷_) (splitAt n (xs .force))
42+
3943
take : (n : ℕ) Stream A ∞ Vec A n
40-
take zero xs = []
41-
take (suc n) xs = head xs ∷ take n (tail xs)
44+
take n xs = proj₁ (splitAt n xs)
45+
46+
drop : Stream A ∞ Stream A ∞
47+
drop n xs = proj₂ (splitAt n xs)
4248

4349
infixr 5 _++_ _⁺++_
4450
_++_ : {i} List A Stream A i Stream A i
@@ -54,6 +60,9 @@ module _ {ℓ} {A : Set ℓ} where
5460
concat : {i} Stream (List⁺ A) i Stream A i
5561
concat (xs ∷ xss) = xs ⁺++ λ where .force concat (xss .force)
5662

63+
interleave : {i} Stream A i Thunk (Stream A) i Stream A i
64+
interleave (x ∷ xs) ys = x ∷ λ where .force interleave (ys .force) xs
65+
5766
module _ {ℓ ℓ′} {A : Set ℓ} {B : Set ℓ′} where
5867

5968
map : {i} (A B) Stream A i Stream B i

src/Codata/Stream/Properties.agda

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
module Codata.Stream.Properties where
88

99
open import Size
10-
open import Data.Nat.Base
11-
import Data.Vec as Vec
1210
open import Codata.Thunk using (Thunk; force)
1311
open import Codata.Stream
1412
open import Codata.Stream.Bisimilarity
13+
14+
open import Data.Nat.Base
15+
import Data.Vec as Vec
16+
import Data.Product as Prod
17+
1518
open import Function
1619
open import Relation.Binary.PropositionalEquality as Eq using (_≡_)
1720

@@ -40,16 +43,27 @@ module _ {a b} {A : Set a} {B : Set b} where
4043
ap-repeat-commute f a = Eq.refl ∷ λ where .force ap-repeat-commute f a
4144

4245

46+
------------------------------------------------------------------------
4347
-- Functor laws
4448

4549
module _ {a} {A : Set a} where
4650

4751
map-identity : (as : Stream A ∞) {i} i ⊢ map id as ≈ as
4852
map-identity (a ∷ as) = Eq.refl ∷ λ where .force map-identity (as .force)
4953

50-
5154
module _ {a b c} {A : Set a} {B : Set b} {C : Set c} where
5255

5356
map-map-fusion : (f : A B) (g : B C) as {i} i ⊢ map g (map f as) ≈ map (g ∘ f) as
5457
map-map-fusion f g (a ∷ as) = Eq.refl ∷ λ where .force map-map-fusion f g (as .force)
5558

59+
60+
------------------------------------------------------------------------
61+
-- splitAt
62+
63+
module _ {a b} {A : Set a} {B : Set b} where
64+
65+
splitAt-map : n (f : A B) xs
66+
splitAt n (map f xs) ≡ Prod.map (Vec.map f) (map f) (splitAt n xs)
67+
splitAt-map zero f xs = Eq.refl
68+
splitAt-map (suc n) f (x ∷ xs) =
69+
Eq.cong (Prod.map₁ (f x Vec.∷_)) (splitAt-map n f (xs .force))

0 commit comments

Comments
 (0)