4 | import public Data.Zippable
11 | drop : (n : Nat) -> Stream a -> Stream a
13 | drop (S k) (x::xs) = drop k xs
17 | repeat : a -> Stream a
18 | repeat x = x :: repeat x
24 | iterate : (f : a -> a) -> (x : a) -> Stream a
25 | iterate f x = x :: iterate f (f x)
28 | unfoldr : (b -> (a, b)) -> b -> Stream a
29 | unfoldr f c = let (a, n) = f c in a :: unfoldr f n
38 | index : Nat -> Stream a -> a
40 | index (S k) (x::xs) = index k xs
47 | Zippable Stream where
48 | zipWith f (x :: xs) (y :: ys) = f x y :: zipWith f xs ys
50 | zipWith3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: zipWith3 f xs ys zs
52 | unzipWith f xs = unzip (map f xs)
54 | unzip xs = (map fst xs, map snd xs)
56 | unzipWith3 f xs = unzip3 (map f xs)
58 | unzip3 xs = (map (\(x, _, _) => x) xs, map (\(_, x, _) => x) xs, map (\(_, _, x) => x) xs)
61 | zipWithIndexLinear : (0 f : _) -> (xs, ys : Stream a) -> (i : Nat) -> index i (zipWith f xs ys) = f (index i xs) (index i ys)
62 | zipWithIndexLinear f (_::xs) (_::ys) Z = Refl
63 | zipWithIndexLinear f (_::xs) (_::ys) (S k) = zipWithIndexLinear f xs ys k
66 | zipWith3IndexLinear : (0 f : _) -> (xs, ys, zs : Stream a) -> (i : Nat) -> index i (zipWith3 f xs ys zs) = f (index i xs) (index i ys) (index i zs)
67 | zipWith3IndexLinear f (_::xs) (_::ys) (_::zs) Z = Refl
68 | zipWith3IndexLinear f (_::xs) (_::ys) (_::zs) (S k) = zipWith3IndexLinear f xs ys zs k
72 | diag : Stream (Stream a) -> Stream a
73 | diag ((x::xs)::xss) = x :: diag (map tail xss)
80 | scanl : (f : a -> b -> a) -> (acc : a) -> (xs : Stream b) -> Stream a
81 | scanl f acc (x :: xs) = acc :: scanl f (f acc x) xs
87 | cycle : (xs : List a) -> {auto 0 ok : NonEmpty xs} -> Stream a
88 | cycle (x :: xs) = x :: cycle' xs
89 | where cycle' : List a -> Stream a
90 | cycle' [] = x :: cycle' xs
91 | cycle' (y :: ys) = y :: cycle' ys
102 | zig : List1 (Stream a) -> Stream (Stream a) -> Stream a
105 | zag : List1 a -> List1 (Stream a) -> Stream (Stream a) -> Stream a
107 | zig xs = zag (head <$> xs) (tail <$> xs)
109 | zag (x ::: []) zs (l :: ls) = x :: zig (l ::: forget zs) ls
110 | zag (x ::: (y :: xs)) zs ls = x :: zag (y ::: xs) zs ls
113 | cantor : Stream (Stream a) -> Stream a
114 | cantor (l :: ls) = zig (l ::: []) ls
121 | planeWith : {0 p : a -> Type} ->
122 | ((x : a) -> p x -> c) ->
123 | Stream a -> ((x : a) -> Stream (p x)) ->
125 | planeWith k as f = cantor (map (\ x => map (k x) (f x)) as)
130 | plane : {0 p : a -> Type} ->
131 | Stream a -> ((x : a) -> Stream (p x)) ->
132 | Stream (x : a ** p x)
133 | plane = planeWith (\ x, prf => (
x ** prf)
)
140 | planeWith : (a -> b -> c) ->
141 | Stream a -> (a -> Stream b) ->
143 | planeWith k as f = cantor (map (\ x => map (k x) (f x)) as)
148 | plane : Stream a -> (a -> Stream b) -> Stream (a, b)
149 | plane = Pair.planeWith (,)
158 | take 10 (plane Stream.nats (const Stream.nats))
161 | , (2, 0), (1, 1), (0, 2)
162 | , (3, 0), (2, 1), (1, 2), (0, 3)
171 | Applicative Stream where
173 | (<*>) = zipWith apply
177 | s >>= f = diag (map f s)
183 | lengthTake : (n : Nat) -> (xs : Stream a) -> length (take n xs) = n
184 | lengthTake Z _ = Refl
185 | lengthTake (S n) (x :: xs) = cong S (lengthTake n xs)