Possibly infinite lists #
This file provides a Seq α
type representing possibly infinite lists (referred here as sequences).
It is encoded as an infinite stream of options such that if f n = none
, then
f m = none
for all m ≥ n
.
Equations
- Stream'.Seq.instInhabited = { default := Stream'.Seq.nil }
Prepend an element to a sequence
Equations
- Stream'.Seq.cons a s = ⟨Stream'.cons (some a) ↑s, ⋯⟩
Instances For
Get the nth element of a sequence (if it exists)
Equations
- Stream'.Seq.get? = Subtype.val
Instances For
A sequence has terminated at position n
if the value at position n
equals none
.
Instances For
It is decidable whether a sequence terminates at a given position.
Equations
- s.terminatedAtDecidable n = decidable_of_iff' ((s.get? n).isNone = true) ⋯
A sequence terminates if there is some position n
at which it has terminated.
Instances For
member definition for Seq
Instances For
Equations
- Stream'.Seq.instMembership = { mem := Stream'.Seq.Mem }
If a sequence terminated at position n
, it also terminated at m ≥ n
.
Recursion principle for sequences, compare with List.recOn
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Corecursor over pairs of Option
values
Equations
- Stream'.Seq.Corec.f f none = (none, none)
- Stream'.Seq.Corec.f f (some b) = match f b with | none => (none, none) | some (a, b') => (some a, some b')
Instances For
Corecursor for Seq α
as a coinductive type. Iterates f
to produce new elements
of the sequence until none
is obtained.
Equations
- Stream'.Seq.corec f b = ⟨Stream'.corec' (Stream'.Seq.Corec.f f) (some b), ⋯⟩
Instances For
Bisimilarity relation over Option
of Seq1 α
Equations
- Stream'.Seq.BisimO R none none = True
- Stream'.Seq.BisimO R (some (a, s)) (some (a', s')) = (a = a' ∧ R s s')
- Stream'.Seq.BisimO R x✝¹ x✝ = False
Instances For
a relation is bisimilar if it meets the BisimO
test
Equations
- Stream'.Seq.IsBisimulation R = ∀ ⦃s₁ s₂ : Stream'.Seq α⦄, R s₁ s₂ → Stream'.Seq.BisimO R s₁.destruct s₂.destruct
Instances For
Equations
- Stream'.Seq.coeList = { coe := Stream'.Seq.ofList }
Equations
- Stream'.Seq.coeStream = { coe := Stream'.Seq.ofStream }
Alias of Stream'.Seq.ofMLList
.
Embed a MLList α
as a sequence. Note that even though this
is non-meta, it will produce infinite sequences if used with
cyclic MLList
s created by meta constructions.
Instances For
Equations
- Stream'.Seq.coeMLList = { coe := Stream'.Seq.ofMLList }
Translate a sequence into a MLList
.
Equations
- x✝.toMLList = match x✝.destruct with | none => MLList.nil | some (a, s') => MLList.cons a s'.toMLList
Instances For
Alias of Stream'.Seq.toMLList
.
Translate a sequence into a MLList
.
Instances For
Translate a sequence to a list. This function will run forever if run on an infinite sequence.
Equations
- s.forceToList = s.toMLList.force
Instances For
The sequence of natural numbers some 0, some 1, ...
Equations
Instances For
Append two sequences. If s₁
is infinite, then s₁ ++ s₂ = s₁
,
otherwise it puts s₂
at the location of the nil
in s₁
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Map a function over a sequence.
Equations
- Stream'.Seq.map f ⟨s, al⟩ = ⟨Stream'.map (Option.map f) s, ⋯⟩
Instances For
Flatten a sequence of sequences. (It is required that the
sequences be nonempty to ensure productivity; in the case
of an infinite sequence of nil
, the first element is never
generated.)
Equations
- One or more equations did not get rendered due to their size.
Instances For
Remove the first n
elements from the sequence.
Instances For
Take the first n
elements of the sequence (producing a list)
Equations
- Stream'.Seq.take 0 x✝ = []
- Stream'.Seq.take n.succ x✝ = match x✝.destruct with | none => [] | some (x, r) => x :: Stream'.Seq.take n r
Instances For
Split a sequence at n
, producing a finite initial segment
and an infinite tail.
Equations
- Stream'.Seq.splitAt 0 x✝ = ([], x✝)
- Stream'.Seq.splitAt n.succ x✝ = match x✝.destruct with | none => ([], Stream'.Seq.nil) | some (x, s') => match Stream'.Seq.splitAt n s' with | (l, r) => (x :: l, r)
Instances For
Combine two sequences with a function
Equations
- Stream'.Seq.zipWith f s₁ s₂ = ⟨fun (n : ℕ) => Option.map₂ f (s₁.get? n) (s₂.get? n), ⋯⟩
Instances For
Pair two sequences into a sequence of pairs
Equations
- Stream'.Seq.zip = Stream'.Seq.zipWith Prod.mk
Instances For
Separate a sequence of pairs into two sequences
Equations
- s.unzip = (Stream'.Seq.map Prod.fst s, Stream'.Seq.map Prod.snd s)
Instances For
Enumerate a sequence by tagging each element with its index.
Equations
- s.enum = Stream'.Seq.nats.zip s
Instances For
The length of a terminating sequence.
Instances For
Convert a sequence which is known to terminate into a list
Equations
- s.toList h = Stream'.Seq.take (s.length h) s
Instances For
Convert a sequence which is known not to terminate into a stream
Equations
- s.toStream h n = (s.get? n).get ⋯
Instances For
Convert a sequence into either a list or a stream depending on whether it is finite or infinite. (Without decidability of the infiniteness predicate, this is not constructively possible.)
Equations
Instances For
The statement of length_le_iff'
does not assume that the sequence terminates. For a
simpler statement of the theorem where the sequence is known to terminate see length_le_iff
The statement of length_le_iff
assumes that the sequence terminates. For a
statement of the where the sequence is not known to terminate see length_le_iff'
The statement of lt_length_iff'
does not assume that the sequence terminates. For a
simpler statement of the theorem where the sequence is known to terminate see lt_length_iff
The statement of length_le_iff
assumes that the sequence terminates. For a
statement of the where the sequence is not known to terminate see length_le_iff'
Equations
- Stream'.Seq.instFunctor = { map := @Stream'.Seq.map, mapConst := fun {α β : Type ?u.8} => Stream'.Seq.map ∘ Function.const β }
Convert a sequence into a list, embedded in a computation to allow for the possibility of infinite sequences (in which case the computation never returns anything).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Stream'.Seq1.coeSeq = { coe := Stream'.Seq1.toSeq }
Map a function on a Seq1
Equations
- Stream'.Seq1.map f (a, s) = (f a, Stream'.Seq.map f s)
Instances For
Flatten a nonempty sequence of nonempty sequences
Equations
- Stream'.Seq1.join ((a, s), S) = match s.destruct with | none => (a, S.join) | some s' => (a, (Stream'.Seq.cons s' S).join)
Instances For
The return
operator for the Seq1
monad,
which produces a singleton sequence.
Equations
- Stream'.Seq1.ret a = (a, Stream'.Seq.nil)
Instances For
Equations
- Stream'.Seq1.instInhabited = { default := Stream'.Seq1.ret default }
The bind
operator for the Seq1
monad,
which maps f
on each element of s
and appends the results together.
(Not all of s
may be evaluated, because the first few elements of s
may already produce an infinite result.)
Equations
- s.bind f = (Stream'.Seq1.map f s).join