Codata.Musical.Stream
{-# OPTIONS --cubical-compatible --guardedness #-}
module Codata.Musical.Stream where
open import Level using (Level)
open import Codata.Musical.Notation
open import Codata.Musical.Colist using (Colist; []; _β·_)
open import Data.Vec.Base using (Vec; []; _β·_)
open import Data.Nat.Base using (β; zero; suc)
open import Relation.Binary.Definitions
using (Reflexive; Symmetric; Transitive)
open import Relation.Binary.Bundles using (Setoid)
open import Relation.Binary.PropositionalEquality.Core as β‘ using (_β‘_)
private
variable
a b c : Level
A : Set a
B : Set b
C : Set c
infixr 5 _β·_
data Stream (A : Set a) : Set a where
_β·_ : (x : A) (xs : β (Stream A)) β Stream A
{-# FOREIGN GHC
data AgdaStream a = Cons a (MAlonzo.RTE.Inf (AgdaStream a))
type AgdaStream' l a = AgdaStream a
#-}
{-# COMPILE GHC Stream = data AgdaStream' (Cons) #-}
head : Stream A β A
head (x β· xs) = x
tail : Stream A β Stream A
tail (x β· xs) = β xs
map : (A β B) β Stream A β Stream B
map f (x β· xs) = f x β· β― map f (β xs)
zipWith : (A β B β C) β Stream A β Stream B β Stream C
zipWith _β_ (x β· xs) (y β· ys) = (x β y) β· β― zipWith _β_ (β xs) (β ys)
take : β n β Stream A β Vec A n
take zero xs = []
take (suc n) (x β· xs) = x β· take n (β xs)
drop : β β Stream A β Stream A
drop zero xs = xs
drop (suc n) (x β· xs) = drop n (β xs)
repeat : A β Stream A
repeat x = x β· β― repeat x
iterate : (A β A) β A β Stream A
iterate f x = x β· β― iterate f (f x)
infixr 5 _β_
_β_ : Stream A β Stream A β Stream A
(x β· xs) β ys = x β· β― (ys β β xs)
mutual
evens : Stream A β Stream A
evens (x β· xs) = x β· β― odds (β xs)
odds : Stream A β Stream A
odds (x β· xs) = evens (β xs)
toColist : Stream A β Colist A
toColist (x β· xs) = x β· β― toColist (β xs)
lookup : Stream A β β β A
lookup (x β· xs) zero = x
lookup (x β· xs) (suc n) = lookup (β xs) n
infixr 5 _++_
_++_ : β {a} {A : Set a} β Colist A β Stream A β Stream A
[] ++ ys = ys
(x β· xs) ++ ys = x β· β― (β xs ++ ys)
infix 4 _β_
data _β_ {A : Set a} : Stream A β Stream A β Set a where
_β·_ : β {x y xs ys}
(xβ‘ : x β‘ y) (xsβ : β (β xs β β ys)) β x β· xs β y β· ys
infix 4 _β_
data _β_ {A : Set a} : A β Stream A β Set a where
here : β {x xs} β x β x β· xs
there : β {x y xs} (xβxs : x β β xs) β x β y β· xs
infix 4 _β_
data _β_ {A : Set a} : Colist A β Stream A β Set a where
[] : β {ys} β [] β ys
_β·_ : β x {xs ys} (p : β (β xs β β ys)) β x β· xs β x β· ys
setoid : β {a} β Set a β Setoid _ _
setoid A = record
{ Carrier = Stream A
; _β_ = _β_ {A = A}
; isEquivalence = record
{ refl = refl
; sym = sym
; trans = trans
}
}
where
refl : Reflexive _β_
refl {_ β· _} = β‘.refl β· β― refl
sym : Symmetric _β_
sym (xβ‘ β· xsβ) = β‘.sym xβ‘ β· β― sym (β xsβ)
trans : Transitive _β_
trans (xβ‘ β· xsβ) (yβ‘ β· ysβ) = β‘.trans xβ‘ yβ‘ β· β― trans (β xsβ) (β ysβ)
head-cong : {xs ys : Stream A} β xs β ys β head xs β‘ head ys
head-cong (xβ‘ β· _) = xβ‘
tail-cong : {xs ys : Stream A} β xs β ys β tail xs β tail ys
tail-cong (_ β· xsβ) = β xsβ
map-cong : β (f : A β B) {xs ys} β
xs β ys β map f xs β map f ys
map-cong f (xβ‘ β· xsβ) = β‘.cong f xβ‘ β· β― map-cong f (β xsβ)
zipWith-cong : β (_β_ : A β B β C) {xs xsβ² ys ysβ²} β
xs β xsβ² β ys β ysβ² β
zipWith _β_ xs ys β zipWith _β_ xsβ² ysβ²
zipWith-cong _β_ (xβ‘ β· xsβ) (yβ‘ β· ysβ) =
β‘.congβ _β_ xβ‘ yβ‘ β· β― zipWith-cong _β_ (β xsβ) (β ysβ)
infixr 5 _β-cong_
_β-cong_ : {xs xsβ² ys ysβ² : Stream A} β
xs β xsβ² β ys β ysβ² β xs β ys β xsβ² β ysβ²
(x β· xsβ) β-cong ysβ = x β· β― (ysβ β-cong β xsβ)