| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Streams |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --guardedness #-} |
| 8 |
|
| 9 |
module Codata.Musical.Stream where |
| 10 |
|
| 11 |
open import Level using (Level) |
| 12 |
open import Codata.Musical.Notation |
| 13 |
open import Codata.Musical.Colist using (Colist; []; _∷_) |
| 14 |
open import Data.Vec.Base using (Vec; []; _∷_) |
| 15 |
open import Data.Nat.Base using (ℕ; zero; suc) |
| 16 |
open import Relation.Binary.Definitions |
| 17 |
using (Reflexive; Symmetric; Transitive) |
| 18 |
open import Relation.Binary.Bundles using (Setoid) |
| 19 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 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 |
-- The type |
| 30 |
|
| 31 |
infixr 5 _∷_ |
| 32 |
|
| 33 |
data Stream (A : Set a) : Set a where |
| 34 |
_∷_ : (x : A) (xs : ∞ (Stream A)) → Stream A |
| 35 |
|
| 36 |
{-# FOREIGN GHC |
| 37 |
data AgdaStream a = Cons a (MAlonzo.RTE.Inf (AgdaStream a)) |
| 38 |
type AgdaStream' l a = AgdaStream a |
| 39 |
#-} |
| 40 |
{-# COMPILE GHC Stream = data AgdaStream' (Cons) #-} |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Some operations |
| 44 |
|
| 45 |
head : Stream A → A |
| 46 |
head (x ∷ xs) = x |
| 47 |
|
| 48 |
tail : Stream A → Stream A |
| 49 |
tail (x ∷ xs) = ♭ xs |
| 50 |
|
| 51 |
map : (A → B) → Stream A → Stream B |
| 52 |
map f (x ∷ xs) = f x ∷ ♯ map f (♭ xs) |
| 53 |
|
| 54 |
zipWith : (A → B → C) → Stream A → Stream B → Stream C |
| 55 |
zipWith _∙_ (x ∷ xs) (y ∷ ys) = (x ∙ y) ∷ ♯ zipWith _∙_ (♭ xs) (♭ ys) |
| 56 |
|
| 57 |
take : ∀ n → Stream A → Vec A n |
| 58 |
take zero xs = [] |
| 59 |
take (suc n) (x ∷ xs) = x ∷ take n (♭ xs) |
| 60 |
|
| 61 |
drop : ℕ → Stream A → Stream A |
| 62 |
drop zero xs = xs |
| 63 |
drop (suc n) (x ∷ xs) = drop n (♭ xs) |
| 64 |
|
| 65 |
repeat : A → Stream A |
| 66 |
repeat x = x ∷ ♯ repeat x |
| 67 |
|
| 68 |
iterate : (A → A) → A → Stream A |
| 69 |
iterate f x = x ∷ ♯ iterate f (f x) |
| 70 |
|
| 71 |
-- Interleaves the two streams. |
| 72 |
|
| 73 |
infixr 5 _⋎_ |
| 74 |
|
| 75 |
_⋎_ : Stream A → Stream A → Stream A |
| 76 |
(x ∷ xs) ⋎ ys = x ∷ ♯ (ys ⋎ ♭ xs) |
| 77 |
|
| 78 |
mutual |
| 79 |
|
| 80 |
-- Takes every other element from the stream, starting with the |
| 81 |
-- first one. |
| 82 |
|
| 83 |
evens : Stream A → Stream A |
| 84 |
evens (x ∷ xs) = x ∷ ♯ odds (♭ xs) |
| 85 |
|
| 86 |
-- Takes every other element from the stream, starting with the |
| 87 |
-- second one. |
| 88 |
|
| 89 |
odds : Stream A → Stream A |
| 90 |
odds (x ∷ xs) = evens (♭ xs) |
| 91 |
|
| 92 |
toColist : Stream A → Colist A |
| 93 |
toColist (x ∷ xs) = x ∷ ♯ toColist (♭ xs) |
| 94 |
|
| 95 |
lookup : Stream A → ℕ → A |
| 96 |
lookup (x ∷ xs) zero = x |
| 97 |
lookup (x ∷ xs) (suc n) = lookup (♭ xs) n |
| 98 |
|
| 99 |
infixr 5 _++_ |
| 100 |
|
| 101 |
_++_ : ∀ {a} {A : Set a} → Colist A → Stream A → Stream A |
| 102 |
[] ++ ys = ys |
| 103 |
(x ∷ xs) ++ ys = x ∷ ♯ (♭ xs ++ ys) |
| 104 |
|
| 105 |
------------------------------------------------------------------------ |
| 106 |
-- Equality and other relations |
| 107 |
|
| 108 |
-- xs ≈ ys means that xs and ys are equal. |
| 109 |
|
| 110 |
infix 4 _≈_ |
| 111 |
|
| 112 |
data _≈_ {A : Set a} : Stream A → Stream A → Set a where |
| 113 |
_∷_ : ∀ {x y xs ys} |
| 114 |
(x≡ : x ≡ y) (xs≈ : ∞ (♭ xs ≈ ♭ ys)) → x ∷ xs ≈ y ∷ ys |
| 115 |
|
| 116 |
-- x ∈ xs means that x is a member of xs. |
| 117 |
|
| 118 |
infix 4 _∈_ |
| 119 |
|
| 120 |
data _∈_ {A : Set a} : A → Stream A → Set a where |
| 121 |
here : ∀ {x xs} → x ∈ x ∷ xs |
| 122 |
there : ∀ {x y xs} (x∈xs : x ∈ ♭ xs) → x ∈ y ∷ xs |
| 123 |
|
| 124 |
-- xs ⊑ ys means that xs is a prefix of ys. |
| 125 |
|
| 126 |
infix 4 _⊑_ |
| 127 |
|
| 128 |
data _⊑_ {A : Set a} : Colist A → Stream A → Set a where |
| 129 |
[] : ∀ {ys} → [] ⊑ ys |
| 130 |
_∷_ : ∀ x {xs ys} (p : ∞ (♭ xs ⊑ ♭ ys)) → x ∷ xs ⊑ x ∷ ys |
| 131 |
|
| 132 |
------------------------------------------------------------------------ |
| 133 |
-- Some proofs |
| 134 |
|
| 135 |
setoid : ∀ {a} → Set a → Setoid _ _ |
| 136 |
setoid A = record |
| 137 |
{ Carrier = Stream A |
| 138 |
; _≈_ = _≈_ {A = A} |
| 139 |
; isEquivalence = record |
| 140 |
{ refl = refl |
| 141 |
; sym = sym |
| 142 |
; trans = trans |
| 143 |
} |
| 144 |
} |
| 145 |
where |
| 146 |
refl : Reflexive _≈_ |
| 147 |
refl {_ ∷ _} = ≡.refl ∷ ♯ refl |
| 148 |
|
| 149 |
sym : Symmetric _≈_ |
| 150 |
sym (x≡ ∷ xs≈) = ≡.sym x≡ ∷ ♯ sym (♭ xs≈) |
| 151 |
|
| 152 |
trans : Transitive _≈_ |
| 153 |
trans (x≡ ∷ xs≈) (y≡ ∷ ys≈) = ≡.trans x≡ y≡ ∷ ♯ trans (♭ xs≈) (♭ ys≈) |
| 154 |
|
| 155 |
head-cong : {xs ys : Stream A} → xs ≈ ys → head xs ≡ head ys |
| 156 |
head-cong (x≡ ∷ _) = x≡ |
| 157 |
|
| 158 |
tail-cong : {xs ys : Stream A} → xs ≈ ys → tail xs ≈ tail ys |
| 159 |
tail-cong (_ ∷ xs≈) = ♭ xs≈ |
| 160 |
|
| 161 |
map-cong : ∀ (f : A → B) {xs ys} → |
| 162 |
xs ≈ ys → map f xs ≈ map f ys |
| 163 |
map-cong f (x≡ ∷ xs≈) = ≡.cong f x≡ ∷ ♯ map-cong f (♭ xs≈) |
| 164 |
|
| 165 |
zipWith-cong : ∀ (_∙_ : A → B → C) {xs xs′ ys ys′} → |
| 166 |
xs ≈ xs′ → ys ≈ ys′ → |
| 167 |
zipWith _∙_ xs ys ≈ zipWith _∙_ xs′ ys′ |
| 168 |
zipWith-cong _∙_ (x≡ ∷ xs≈) (y≡ ∷ ys≈) = |
| 169 |
≡.cong₂ _∙_ x≡ y≡ ∷ ♯ zipWith-cong _∙_ (♭ xs≈) (♭ ys≈) |
| 170 |
|
| 171 |
infixr 5 _⋎-cong_ |
| 172 |
|
| 173 |
_⋎-cong_ : {xs xs′ ys ys′ : Stream A} → |
| 174 |
xs ≈ xs′ → ys ≈ ys′ → xs ⋎ ys ≈ xs′ ⋎ ys′ |
| 175 |
(x ∷ xs≈) ⋎-cong ys≈ = x ∷ ♯ (ys≈ ⋎-cong ♭ xs≈) |