| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The Stream type and some operations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 8 |
|
| 9 |
module Codata.Sized.Stream where |
| 10 |
|
| 11 |
open import Codata.Sized.Thunk as Thunk using (Thunk; force) |
| 12 |
open import Data.Nat.Base using (ℕ; suc; zero) |
| 13 |
open import Data.List.Base using (List; []; _∷_) |
| 14 |
open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_; _∷⁺_) |
| 15 |
open import Data.Vec.Base using (Vec; []; _∷_) |
| 16 |
open import Data.Product.Base as P using (Σ; _×_; _,_; <_,_>; proj₁; proj₂) |
| 17 |
open import Function.Base using (id; _∘_) |
| 18 |
open import Level using (Level) |
| 19 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 20 |
open import Size using (Size; ∞; Size<_) |
| 21 |
|
| 22 |
private |
| 23 |
variable |
| 24 |
a b c : Level |
| 25 |
A : Set a |
| 26 |
B : Set b |
| 27 |
C : Set c |
| 28 |
i : Size |
| 29 |
|
| 30 |
------------------------------------------------------------------------ |
| 31 |
-- Type |
| 32 |
|
| 33 |
data Stream (A : Set a) (i : Size) : Set a where |
| 34 |
_∷_ : A → Thunk (Stream A) i → Stream A i |
| 35 |
|
| 36 |
infixr 5 _∷_ |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- Creating streams |
| 40 |
|
| 41 |
repeat : A → Stream A i |
| 42 |
repeat a = a ∷ λ where .force → repeat a |
| 43 |
|
| 44 |
infixr 5 _++_ _⁺++_ |
| 45 |
_++_ : List A → Stream A i → Stream A i |
| 46 |
[] ++ ys = ys |
| 47 |
(x ∷ xs) ++ ys = x ∷ λ where .force → xs ++ ys |
| 48 |
|
| 49 |
unfold : (A → A × B) → A → Stream B i |
| 50 |
unfold next seed = |
| 51 |
let (seed′ , b) = next seed in |
| 52 |
b ∷ λ where .force → unfold next seed′ |
| 53 |
|
| 54 |
iterate : (A → A) → A → Stream A ∞ |
| 55 |
iterate f = unfold < f , id > |
| 56 |
|
| 57 |
nats : Stream ℕ ∞ |
| 58 |
nats = iterate suc zero |
| 59 |
|
| 60 |
------------------------------------------------------------------------ |
| 61 |
-- Looking at streams |
| 62 |
|
| 63 |
head : Stream A i → A |
| 64 |
head (x ∷ xs) = x |
| 65 |
|
| 66 |
tail : {j : Size< i} → Stream A i → Stream A j |
| 67 |
tail (x ∷ xs) = xs .force |
| 68 |
|
| 69 |
lookup : Stream A ∞ → ℕ → A |
| 70 |
lookup xs zero = head xs |
| 71 |
lookup xs (suc k) = lookup (tail xs) k |
| 72 |
|
| 73 |
------------------------------------------------------------------------ |
| 74 |
-- Transforming streams |
| 75 |
|
| 76 |
map : (A → B) → Stream A i → Stream B i |
| 77 |
map f (x ∷ xs) = f x ∷ λ where .force → map f (xs .force) |
| 78 |
|
| 79 |
ap : Stream (A → B) i → Stream A i → Stream B i |
| 80 |
ap (f ∷ fs) (x ∷ xs) = f x ∷ λ where .force → ap (fs .force) (xs .force) |
| 81 |
|
| 82 |
scanl : (B → A → B) → B → Stream A i → Stream B i |
| 83 |
scanl c n (x ∷ xs) = n ∷ λ where .force → scanl c (c n x) (xs .force) |
| 84 |
|
| 85 |
zipWith : (A → B → C) → Stream A i → Stream B i → Stream C i |
| 86 |
zipWith f (a ∷ as) (b ∷ bs) = f a b ∷ λ where .force → zipWith f (as .force) (bs .force) |
| 87 |
|
| 88 |
------------------------------------------------------------------------ |
| 89 |
-- List⁺-related functions |
| 90 |
|
| 91 |
_⁺++_ : List⁺ A → Thunk (Stream A) i → Stream A i |
| 92 |
(x ∷ xs) ⁺++ ys = x ∷ λ where .force → xs ++ ys .force |
| 93 |
|
| 94 |
cycle : List⁺ A → Stream A i |
| 95 |
cycle xs = xs ⁺++ λ where .force → cycle xs |
| 96 |
|
| 97 |
concat : Stream (List⁺ A) i → Stream A i |
| 98 |
concat (xs ∷ xss) = xs ⁺++ λ where .force → concat (xss .force) |
| 99 |
|
| 100 |
------------------------------------------------------------------------ |
| 101 |
-- Chunking |
| 102 |
|
| 103 |
splitAt : (n : ℕ) → Stream A ∞ → Vec A n × Stream A ∞ |
| 104 |
splitAt zero xs = [] , xs |
| 105 |
splitAt (suc n) (x ∷ xs) = P.map₁ (x ∷_) (splitAt n (xs .force)) |
| 106 |
|
| 107 |
take : (n : ℕ) → Stream A ∞ → Vec A n |
| 108 |
take n xs = proj₁ (splitAt n xs) |
| 109 |
|
| 110 |
drop : ℕ → Stream A ∞ → Stream A ∞ |
| 111 |
drop n xs = proj₂ (splitAt n xs) |
| 112 |
|
| 113 |
chunksOf : (n : ℕ) → Stream A ∞ → Stream (Vec A n) ∞ |
| 114 |
chunksOf n = chunksOfAcc n id module ChunksOf where |
| 115 |
|
| 116 |
chunksOfAcc : ∀ k (acc : Vec A k → Vec A n) → |
| 117 |
Stream A ∞ → Stream (Vec A n) i |
| 118 |
chunksOfAcc zero acc xs = acc [] ∷ λ where .force → chunksOfAcc n id xs |
| 119 |
chunksOfAcc (suc k) acc (x ∷ xs) = chunksOfAcc k (acc ∘ (x ∷_)) (xs .force) |
| 120 |
|
| 121 |
------------------------------------------------------------------------ |
| 122 |
-- Interleaving streams |
| 123 |
|
| 124 |
-- The most basic of interleaving strategies is to take two streams and |
| 125 |
-- alternate between emitting values from one and the other. |
| 126 |
interleave : Stream A i → Thunk (Stream A) i → Stream A i |
| 127 |
interleave (x ∷ xs) ys = x ∷ λ where .force → interleave (ys .force) xs |
| 128 |
|
| 129 |
-- This interleaving strategy can be generalised to an arbitrary |
| 130 |
-- non-empty list of streams |
| 131 |
interleave⁺ : List⁺ (Stream A i) → Stream A i |
| 132 |
interleave⁺ xss = |
| 133 |
List⁺.map head xss |
| 134 |
⁺++ λ where .force → interleave⁺ (List⁺.map tail xss) |
| 135 |
|
| 136 |
-- To generalise this further to a stream of streams however we have to |
| 137 |
-- adopt a different strategy: if we were to start with *all* the heads |
| 138 |
-- then we would never reach any of the second elements in the streams. |
| 139 |
|
| 140 |
-- Here we use Cantor's zig zag function to explore the plane defined by |
| 141 |
-- the function `(i,j) ↦ lookup (lookup xss i) j‵ mapping coordinates to |
| 142 |
-- values in a way that guarantees that any point is reached in a finite |
| 143 |
-- amount of time. The definition is taken from the paper: |
| 144 |
-- Applications of Applicative Proof Search by Liam O'Connor |
| 145 |
|
| 146 |
cantor : Stream (Stream A ∞) ∞ → Stream A ∞ |
| 147 |
cantor (l ∷ ls) = zig (l ∷ []) (ls .force) module Cantor where |
| 148 |
|
| 149 |
zig : List⁺ (Stream A ∞) → Stream (Stream A ∞) ∞ → Stream A i |
| 150 |
zag : List⁺ A → List⁺ (Stream A ∞) → Stream (Stream A ∞) ∞ → Stream A i |
| 151 |
|
| 152 |
zig xss = zag (List⁺.map head xss) (List⁺.map tail xss) |
| 153 |
|
| 154 |
zag (x ∷ []) zs (l ∷ ls) = x ∷ λ where .force → zig (l ∷⁺ zs) (ls .force) |
| 155 |
zag (x ∷ (y ∷ xs)) zs ls = x ∷ λ where .force → zag (y ∷ xs) zs ls |
| 156 |
|
| 157 |
-- We can use the Cantor zig zag function to define a form of `bind' |
| 158 |
-- that reaches any point of the plane in a finite amount of time. |
| 159 |
plane : {B : A → Set b} → Stream A ∞ → ((a : A) → Stream (B a) ∞) → |
| 160 |
Stream (Σ A B) ∞ |
| 161 |
plane as bs = cantor (map (λ a → map (a ,_) (bs a)) as) |
| 162 |
|
| 163 |
-- Here is the beginning of the path we are following: |
| 164 |
_ : take 21 (plane nats (λ _ → nats)) |
| 165 |
≡ (0 , 0) |
| 166 |
∷ (1 , 0) ∷ (0 , 1) |
| 167 |
∷ (2 , 0) ∷ (1 , 1) ∷ (0 , 2) |
| 168 |
∷ (3 , 0) ∷ (2 , 1) ∷ (1 , 2) ∷ (0 , 3) |
| 169 |
∷ (4 , 0) ∷ (3 , 1) ∷ (2 , 2) ∷ (1 , 3) ∷ (0 , 4) |
| 170 |
∷ (5 , 0) ∷ (4 , 1) ∷ (3 , 2) ∷ (2 , 3) ∷ (1 , 4) ∷ (0 , 5) |
| 171 |
∷ [] |
| 172 |
_ = refl |