Definition of stream
and functions on streams #
THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.
A stream stream α
is an infinite sequence of elements of α
. One can also think about it as an
infinite list. In this file we define stream
and some functions that take and/or return streams.
A stream stream α
is an infinite sequence of elements of α
.
Instances for stream
Head of a stream: stream.head s = stream.nth 0 s
.
Tail of a stream: stream.tail (h :: t) = t
.
Drop first n
elements of a stream.
Equations
- stream.drop n s = λ (i : ℕ), s (i + n)
Proposition saying that all elements of a stream satisfy a predicate.
Equations
- stream.all p s = ∀ (n : ℕ), p (s.nth n)
a ∈ s
means that a = stream.nth n s
for some n
.
Equations
- stream.has_mem = {mem := λ (a : α) (s : stream α), stream.any (λ (b : α), a = b) s}
Zip two streams using a binary operation:
stream.nth n (stream.zip f s₁ s₂) = f (stream.nth s₁) (stream.nth s₂)
.
Equations
- stream.zip f s₁ s₂ = λ (n : ℕ), f (s₁.nth n) (s₂.nth n)
The constant stream: stream.nth n (stream.const a) = a
.
Equations
- stream.const a = λ (n : ℕ), a
Iterates of a function as a stream.
Equations
- stream.iterate f a = λ (n : ℕ), n.rec_on a (λ (n : ℕ) (r : α), f r)
Equations
- stream.corec f g = λ (a : α), stream.map f (stream.iterate g a)
Equations
- stream.corec_on a f g = stream.corec f g a
Equations
- stream.corec' f = stream.corec (prod.fst ∘ f) (prod.snd ∘ f)
Use a state monad to generate a stream through corecursion
Equations
- stream.corec_state cmd s = stream.corec prod.fst (cmd.run ∘ prod.snd) (cmd.run s)
Equations
- stream.unfolds g f a = stream.corec g f a
Interleave two streams.
take n s
returns a list of the n
first elements of stream s
Equations
- stream.take (n + 1) s = s.head :: stream.take n s.tail
- stream.take 0 s = list.nil
An auxiliary definition for stream.cycle
corecursive def
Equations
- stream.cycle_f (v, _x, _x_1, _x_2) = v
Interpret a nonempty list as a cyclic stream.
Equations
- stream.cycle (a :: l) h = stream.corec stream.cycle_f stream.cycle_g (a, l, a, l)
- stream.cycle list.nil h = absurd stream.cycle._main._proof_1 h
Tails of a stream, starting with stream.tail s
.
Equations
- s.tails = stream.corec id stream.tail s.tail
An auxiliary definition for stream.inits
.
A constant stream, same as stream.const
.
Equations
- stream.pure a = stream.const a
The stream of natural numbers: stream.nth n stream.nats = n
.
Equations
- stream.nats = λ (n : ℕ), n