| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The Colist type and some operations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 8 |
|
| 9 |
module Codata.Sized.Colist where |
| 10 |
|
| 11 |
open import Codata.Sized.Conat as Conat using (Conat ; zero ; suc) |
| 12 |
open import Codata.Sized.Cowriter as CW using (Cowriter; _∷_) |
| 13 |
open import Codata.Sized.Delay as Delay using (Delay ; now ; later) |
| 14 |
open import Codata.Sized.Stream using (Stream ; _∷_) |
| 15 |
open import Codata.Sized.Thunk using (Thunk; force) |
| 16 |
open import Data.List.Base using (List; []; _∷_) |
| 17 |
open import Data.List.NonEmpty as List⁺ using (List⁺; _∷_) |
| 18 |
open import Data.Maybe.Base using (Maybe; nothing; just) |
| 19 |
open import Data.Nat.Base using (ℕ; suc; zero) |
| 20 |
open import Data.Product.Base using (_×_ ; _,_) |
| 21 |
open import Data.These.Base using (These; this; that; these) |
| 22 |
open import Data.Unit.Base using (⊤) |
| 23 |
open import Data.Vec.Base as Vec using (Vec; []; _∷_) |
| 24 |
open import Data.Vec.Bounded.Base as Vec≤ using (Vec≤) |
| 25 |
open import Function.Base using (_$′_; _∘′_; id; _∘_) |
| 26 |
open import Level using (Level) |
| 27 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 28 |
open import Size using (Size; ∞) |
| 29 |
|
| 30 |
private |
| 31 |
variable |
| 32 |
a b c w : Level |
| 33 |
i : Size |
| 34 |
A : Set a |
| 35 |
B : Set b |
| 36 |
C : Set c |
| 37 |
W : Set w |
| 38 |
|
| 39 |
data Colist (A : Set a) (i : Size) : Set a where |
| 40 |
[] : Colist A i |
| 41 |
_∷_ : A → Thunk (Colist A) i → Colist A i |
| 42 |
|
| 43 |
infixr 5 _∷_ |
| 44 |
|
| 45 |
------------------------------------------------------------------------ |
| 46 |
-- Relationship to Cowriter. |
| 47 |
|
| 48 |
fromCowriter : Cowriter W A i → Colist W i |
| 49 |
fromCowriter CW.[ _ ] = [] |
| 50 |
fromCowriter (w ∷ ca) = w ∷ λ where .force → fromCowriter (ca .force) |
| 51 |
|
| 52 |
toCowriter : Colist A i → Cowriter A ⊤ i |
| 53 |
toCowriter [] = CW.[ _ ] |
| 54 |
toCowriter (a ∷ as) = a ∷ λ where .force → toCowriter (as .force) |
| 55 |
|
| 56 |
------------------------------------------------------------------------ |
| 57 |
-- Basic functions. |
| 58 |
|
| 59 |
[_] : A → Colist A ∞ |
| 60 |
[ a ] = a ∷ λ where .force → [] |
| 61 |
|
| 62 |
length : Colist A i → Conat i |
| 63 |
length [] = zero |
| 64 |
length (x ∷ xs) = suc λ where .force → length (xs .force) |
| 65 |
|
| 66 |
replicate : Conat i → A → Colist A i |
| 67 |
replicate zero a = [] |
| 68 |
replicate (suc n) a = a ∷ λ where .force → replicate (n .force) a |
| 69 |
|
| 70 |
infixr 5 _++_ _⁺++_ |
| 71 |
_++_ : Colist A i → Colist A i → Colist A i |
| 72 |
[] ++ ys = ys |
| 73 |
(x ∷ xs) ++ ys = x ∷ λ where .force → xs .force ++ ys |
| 74 |
|
| 75 |
lookup : Colist A ∞ → ℕ → Maybe A |
| 76 |
lookup [] _ = nothing |
| 77 |
lookup (a ∷ as) zero = just a |
| 78 |
lookup (a ∷ as) (suc n) = lookup (as .force) n |
| 79 |
|
| 80 |
colookup : Colist A i → Conat i → Delay (Maybe A) i |
| 81 |
colookup [] _ = now nothing |
| 82 |
colookup (a ∷ as) zero = now (just a) |
| 83 |
colookup (a ∷ as) (suc n) = |
| 84 |
later λ where .force → colookup (as .force) (n .force) |
| 85 |
|
| 86 |
take : (n : ℕ) → Colist A ∞ → Vec≤ A n |
| 87 |
take zero xs = Vec≤.[] |
| 88 |
take n [] = Vec≤.[] |
| 89 |
take (suc n) (x ∷ xs) = x Vec≤.∷ take n (xs .force) |
| 90 |
|
| 91 |
cotake : Conat i → Stream A i → Colist A i |
| 92 |
cotake zero xs = [] |
| 93 |
cotake (suc n) (x ∷ xs) = x ∷ λ where .force → cotake (n .force) (xs .force) |
| 94 |
|
| 95 |
drop : ℕ → Colist A ∞ → Colist A ∞ |
| 96 |
drop zero xs = xs |
| 97 |
drop (suc n) [] = [] |
| 98 |
drop (suc n) (x ∷ xs) = drop n (xs .force) |
| 99 |
|
| 100 |
fromList : List A → Colist A ∞ |
| 101 |
fromList [] = [] |
| 102 |
fromList (x ∷ xs) = x ∷ λ where .force → fromList xs |
| 103 |
|
| 104 |
fromList⁺ : List⁺ A → Colist A ∞ |
| 105 |
fromList⁺ = fromList ∘′ List⁺.toList |
| 106 |
|
| 107 |
_⁺++_ : List⁺ A → Thunk (Colist A) i → Colist A i |
| 108 |
(x ∷ xs) ⁺++ ys = x ∷ λ where .force → fromList xs ++ ys .force |
| 109 |
|
| 110 |
concat : Colist (List⁺ A) i → Colist A i |
| 111 |
concat [] = [] |
| 112 |
concat (as ∷ ass) = as ⁺++ λ where .force → concat (ass .force) |
| 113 |
|
| 114 |
fromStream : Stream A i → Colist A i |
| 115 |
fromStream = cotake Conat.infinity |
| 116 |
|
| 117 |
module ChunksOf (n : ℕ) where |
| 118 |
|
| 119 |
chunksOf : Colist A ∞ → Cowriter (Vec A n) (Vec≤ A n) i |
| 120 |
chunksOfAcc : ∀ m → |
| 121 |
-- We have two continuations but we are only ever going to use one. |
| 122 |
-- If we had linear types, we'd write the type using the & conjunction here. |
| 123 |
(k≤ : Vec≤ A m → Vec≤ A n) → |
| 124 |
(k≡ : Vec A m → Vec A n) → |
| 125 |
-- Finally we chop up the input stream. |
| 126 |
Colist A ∞ → Cowriter (Vec A n) (Vec≤ A n) i |
| 127 |
|
| 128 |
chunksOf = chunksOfAcc n id id |
| 129 |
|
| 130 |
chunksOfAcc zero k≤ k≡ as = k≡ [] ∷ λ where .force → chunksOf as |
| 131 |
chunksOfAcc (suc k) k≤ k≡ [] = CW.[ k≤ Vec≤.[] ] |
| 132 |
chunksOfAcc (suc k) k≤ k≡ (a ∷ as) = |
| 133 |
chunksOfAcc k (k≤ ∘ (a Vec≤.∷_)) (k≡ ∘ (a ∷_)) (as .force) |
| 134 |
|
| 135 |
open ChunksOf using (chunksOf) public |
| 136 |
|
| 137 |
-- Test to make sure that the values are kept in the same order |
| 138 |
_ : chunksOf 3 (fromList (1 ∷ 2 ∷ 3 ∷ 4 ∷ [])) |
| 139 |
≡ (1 ∷ 2 ∷ 3 ∷ []) ∷ _ |
| 140 |
_ = refl |
| 141 |
|
| 142 |
map : (A → B) → Colist A i → Colist B i |
| 143 |
map f [] = [] |
| 144 |
map f (a ∷ as) = f a ∷ λ where .force → map f (as .force) |
| 145 |
|
| 146 |
unfold : (A → Maybe (A × B)) → A → Colist B i |
| 147 |
unfold next seed with next seed |
| 148 |
... | nothing = [] |
| 149 |
... | just (seed′ , b) = b ∷ λ where .force → unfold next seed′ |
| 150 |
|
| 151 |
scanl : (B → A → B) → B → Colist A i → Colist B i |
| 152 |
scanl c n [] = n ∷ λ where .force → [] |
| 153 |
scanl c n (a ∷ as) = n ∷ λ where .force → scanl c (c n a) (as .force) |
| 154 |
|
| 155 |
alignWith : (These A B → C) → Colist A i → Colist B i → Colist C i |
| 156 |
alignWith f [] bs = map (f ∘′ that) bs |
| 157 |
alignWith f as@(_ ∷ _) [] = map (f ∘′ this) as |
| 158 |
alignWith f (a ∷ as) (b ∷ bs) = |
| 159 |
f (these a b) ∷ λ where .force → alignWith f (as .force) (bs .force) |
| 160 |
|
| 161 |
zipWith : (A → B → C) → Colist A i → Colist B i → Colist C i |
| 162 |
zipWith f [] bs = [] |
| 163 |
zipWith f as [] = [] |
| 164 |
zipWith f (a ∷ as) (b ∷ bs) = |
| 165 |
f a b ∷ λ where .force → zipWith f (as .force) (bs .force) |
| 166 |
|
| 167 |
align : Colist A i → Colist B i → Colist (These A B) i |
| 168 |
align = alignWith id |
| 169 |
|
| 170 |
zip : Colist A i → Colist B i → Colist (A × B) i |
| 171 |
zip = zipWith _,_ |
| 172 |
|
| 173 |
ap : Colist (A → B) i → Colist A i → Colist B i |
| 174 |
ap = zipWith _$′_ |