Definition of Stream'
and functions on streams #
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.
Note that we already have Stream
to represent a similar object, hence the awkward naming.
Prepend an element to a stream.
Equations
- Stream'.cons a s x = match x with | 0 => a | Nat.succ n => s n
Instances For
Equations
- Stream'.«term_::_» = Lean.ParserDescr.trailingNode `Stream'.term_::_ 67 68 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " :: ") (Lean.ParserDescr.cat `term 67))
Instances For
Head of a stream: Stream'.head s = Stream'.get s 0
.
Equations
- Stream'.head s = Stream'.get s 0
Instances For
Tail of a stream: Stream'.tail (h :: t) = t
.
Equations
- Stream'.tail s i = Stream'.get s (i + 1)
Instances For
Proposition saying that all elements of a stream satisfy a predicate.
Equations
- Stream'.All p s = ∀ (n : ℕ), p (Stream'.get s n)
Instances For
Proposition saying that at least one element of a stream satisfies a predicate.
Equations
- Stream'.Any p s = ∃ (n : ℕ), p (Stream'.get s n)
Instances For
a ∈ s
means that a = Stream'.get n s
for some n
.
Equations
- Stream'.instMembershipStream' = { mem := fun (a : α) (s : Stream' α) => Stream'.Any (fun (b : α) => a = b) s }
Apply a function f
to all elements of a stream s
.
Equations
- Stream'.map f s n = f (Stream'.get s n)
Instances For
Zip two streams using a binary operation:
Stream'.get n (Stream'.zip f s₁ s₂) = f (Stream'.get s₁) (Stream'.get s₂)
.
Equations
- Stream'.zip f s₁ s₂ n = f (Stream'.get s₁ n) (Stream'.get s₂ n)
Instances For
Enumerate a stream by tagging each element with its index.
Equations
- Stream'.enum s n = (n, Stream'.get s n)
Instances For
The constant stream: Stream'.get n (Stream'.const a) = a
.
Equations
- Stream'.const a x = a
Instances For
Iterates of a function as a stream.
Equations
- Stream'.iterate f a 0 = a
- Stream'.iterate f a (Nat.succ n) = f (Stream'.iterate f a n)
Instances For
Equations
- Stream'.corec f g a = Stream'.map f (Stream'.iterate g a)
Instances For
Equations
- Stream'.corecOn a f g = Stream'.corec f g a
Instances For
Equations
- Stream'.corec' f = Stream'.corec (Prod.fst ∘ f) (Prod.snd ∘ f)
Instances For
Use a state monad to generate a stream through corecursion
Equations
- Stream'.corecState cmd s = Stream'.corec Prod.fst (StateT.run cmd ∘ Prod.snd) (StateT.run cmd s)
Instances For
Equations
- Stream'.«term_⋈_» = Lean.ParserDescr.trailingNode `Stream'.term_⋈_ 65 65 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ⋈ ") (Lean.ParserDescr.cat `term 66))
Instances For
Elements of a stream with even indices.
Equations
- Stream'.even s = Stream'.corec (fun (s : Stream' α) => Stream'.head s) (fun (s : Stream' α) => Stream'.tail (Stream'.tail s)) s
Instances For
Elements of a stream with odd indices.
Equations
- Stream'.odd s = Stream'.even (Stream'.tail s)
Instances For
Equations
- Stream'.«term_++ₛ_» = Lean.ParserDescr.trailingNode `Stream'.term_++ₛ_ 65 65 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ++ₛ ") (Lean.ParserDescr.cat `term 66))
Instances For
take n s
returns a list of the n
first elements of stream s
Equations
- Stream'.take 0 x = []
- Stream'.take (Nat.succ n) x = Stream'.head x :: Stream'.take n (Stream'.tail x)
Instances For
An auxiliary definition for Stream'.cycle
corecursive def
Equations
- Stream'.cycleF x = match x with | (v, fst, fst_1, snd) => v
Instances For
Interpret a nonempty list as a cyclic stream.
Equations
- Stream'.cycle x✝ x = match x✝, x with | [], h => absurd (_ : [] = []) h | a :: l, x => Stream'.corec Stream'.cycleF Stream'.cycleG (a, l, a, l)
Instances For
Tails of a stream, starting with Stream'.tail s
.
Equations
- Stream'.tails s = Stream'.corec id Stream'.tail (Stream'.tail s)
Instances For
An auxiliary definition for Stream'.inits
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Nonempty initial segments of a stream.
Equations
- Stream'.inits s = Stream'.initsCore [Stream'.head s] (Stream'.tail s)
Instances For
Given a stream of functions and a stream of values, apply n
-th function to n
-th value.
Equations
- (f ⊛ s) n = Stream'.get f n (Stream'.get s n)
Instances For
Equations
- Stream'.«term_⊛_» = Lean.ParserDescr.trailingNode `Stream'.term_⊛_ 75 75 (Lean.ParserDescr.binary `andthen (Lean.ParserDescr.symbol " ⊛ ") (Lean.ParserDescr.cat `term 76))
Instances For
The stream of natural numbers: Stream'.get n Stream'.nats = n
.
Equations
- Stream'.nats n = n