Debian Codegraph @

src/Codata/Guarded/Stream.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Infinite streams defined as coinductive records
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe --guardedness #-}
8
9 module Codata.Guarded.Stream where
10
11 open import Level hiding (suc)
12 open import Data.Nat.Base using (ℕ; zero; suc)
13 open import Function.Base using (_∘′_; const; id; _∘₂_)
14 open import Data.List.Base as List using (List; []; _∷_)
15 open import Data.Product.Base hiding (map)
16 open import Data.Vec.Base using (Vec; []; _∷_)
17 open import Data.List.NonEmpty.Base as List⁺ using (List⁺; _∷_)
18 open import Algebra.Core using (Op₂)
19 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
20
21 private
22 variable
23 a b c : Level
24 A : Set a
25 B : Set b
26 C : Set c
27
28 ------------------------------------------------------------------------
29 -- Type
30
31 infixr 5 _∷_
32
33 record Stream (A : Set a) : Set a where
34 coinductive
35 constructor _∷_
36 field
37 head : A
38 tail : Stream A
39
40 open Stream public
41
42 ------------------------------------------------------------------------
43 -- Creating streams
44
45 tabulate : (ℕ → A) → Stream A
46 tabulate f .head = f 0
47 tabulate f .tail = tabulate (f ∘′ suc)
48
49 repeat : A → Stream A
50 repeat = tabulate ∘′ const
51
52 infixr 5 _++_
53 _++_ : List A → Stream A → Stream A
54 [] ++ s = s
55 (x ∷ xs) ++ s = x ∷ xs ++ s
56
57 unfold : (A → A × B) → A → Stream B
58 unfold next seed .head = next seed .proj₂
59 unfold next seed .tail = unfold next (next seed .proj₁)
60
61 iterate : (A → A) → A → Stream A
62 iterate f = unfold < f , id >
63
64 nats : Stream ℕ
65 nats = tabulate id
66
67 ------------------------------------------------------------------------
68 -- Lookup
69
70 lookup : Stream A → ℕ → A
71 lookup xs zero = head xs
72 lookup xs (suc n) = lookup (tail xs) n
73
74 infix 4 _[_]
75
76 _[_] : Stream A → ℕ → A
77 _[_] = lookup
78
79 ------------------------------------------------------------------------
80 -- Transforming streams
81
82 map : (A → B) → Stream A → Stream B
83 map f s .head = f (s .head)
84 map f s .tail = map f (s .tail)
85
86 ap : Stream (A → B) → Stream A → Stream B
87 ap fs xs .head = fs .head (xs .head)
88 ap fs xs .tail = ap (fs .tail) (xs .tail)
89
90 scanl : (B → A → B) → B → Stream A → Stream B
91 scanl c n s .head = n
92 scanl c n s .tail = scanl c (c n (s .head)) (s .tail)
93
94 zipWith : (A → B → C) → Stream A → Stream B → Stream C
95 zipWith f s t .head = f (s .head) (t .head)
96 zipWith f s t .tail = zipWith f (s .tail) (t .tail)
97
98 transpose : List (Stream A) → Stream (List A)
99 transpose [] = repeat []
100 transpose (s ∷ ss) = zipWith _∷_ s (transpose ss)
101
102 tails : Stream A → Stream (Stream A)
103 tails s .head = s
104 tails s .tail = tails (s .tail)
105
106 evens : Stream A → Stream A
107 evens s .head = s .head
108 evens s .tail = evens (s .tail .tail)
109
110 odds : Stream A → Stream A
111 odds s = evens (s .tail)
112
113 ------------------------------------------------------------------------
114 -- List⁺-related functions
115
116 infixr 5 _⁺++_
117 _⁺++_ : List⁺ A → Stream A → Stream A
118 (x ∷ xs) ⁺++ ys = x ∷ xs ++ ys
119
120 concat : Stream (List⁺ A) → Stream A
121 concat {A = A} = ++-concat []
122 module Concat where
123 ++-concat : List A → Stream (List⁺ A) → Stream A
124 ++-concat [] s .head = s .head .List⁺.head
125 ++-concat [] s .tail = ++-concat (s .head .List⁺.tail) (s .tail)
126 ++-concat (x ∷ xs) s .head = x
127 ++-concat (x ∷ xs) s .tail = ++-concat xs s
128
129 cycle : List⁺ A → Stream A
130 cycle = concat ∘′ repeat
131
132 transpose⁺ : List⁺ (Stream A) → Stream (List⁺ A)
133 transpose⁺ (s ∷ ss) = zipWith _∷_ s (transpose ss)
134
135 ------------------------------------------------------------------------
136 -- Chunking
137
138 splitAt : ∀ n → Stream A → Vec A n × Stream A
139 splitAt zero s = [] ,′ s
140 splitAt (suc n) s = map₁ (s .head ∷_) (splitAt n (s .tail))
141
142 take : ∀ n → Stream A → Vec A n
143 take = proj₁ ∘₂ splitAt
144
145 drop : ℕ → Stream A → Stream A
146 drop = proj₂ ∘₂ splitAt
147
148 chunksOf : ∀ n → Stream A → Stream (Vec A n)
149 chunksOf n s .head = take n s
150 chunksOf n s .tail = chunksOf n (drop n s)
151
152 ------------------------------------------------------------------------
153 -- Interleaving streams
154
155 -- Interleaving two streams
156 interleave : Op₂ (Stream A)
157 interleave xs ys .head = xs .head
158 interleave xs ys .tail = interleave ys (xs .tail)
159
160 -- Interleaving multiple streams
161 interleave⁺ : List⁺ (Stream A) → Stream A
162 interleave⁺ = concat ∘′ transpose⁺
163
164 -- Interleaving a stream of streams using Cantor's zig zag function
165 -- (inverse of Cantor's pairing function)
166 cantor : Stream (Stream A) → Stream A
167 cantor s .head = s .head .head
168 cantor s .tail = cantor (zipWith _∷_ (s .head .tail) (s .tail))
169
170 -- A version of `bind` using the zig zag function that reaches any
171 -- point of the plane in a finite amout of time
172 plane : {B : A → Set b} → Stream A → (∀ a → Stream (B a)) → Stream (Σ A B)
173 plane xs fs = cantor (map (λ x → map (x ,_) (fs x)) xs)
174
175 -- Here is the beginning of the path we are following:
176 _ : take 21 (plane nats (const nats))
177 ≡ (0 , 0)
178 ∷ (0 , 1) ∷ (1 , 0)
179 ∷ (0 , 2) ∷ (1 , 1) ∷ (2 , 0)
180 ∷ (0 , 3) ∷ (1 , 2) ∷ (2 , 1) ∷ (3 , 0)
181 ∷ (0 , 4) ∷ (1 , 3) ∷ (2 , 2) ∷ (3 , 1) ∷ (4 , 0)
182 ∷ (0 , 5) ∷ (1 , 4) ∷ (2 , 3) ∷ (3 , 2) ∷ (4 , 1) ∷ (5 , 0)
183 ∷ []
184 _ = refl