# 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.

def Stream'.IsSeq {α : Type u} (s : Stream' ()) :

A stream s : Option α is a sequence if s.get n = none implies s.get (n + 1) = none.

Equations
• s.IsSeq = ∀ {n : }, s n = nones (n + 1) = none
Instances For
def Stream'.Seq (α : Type u) :

Seq α is the type of 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
Instances For
def Stream'.Seq1 (α : Type u_1) :
Type u_1

Seq1 α is the type of nonempty sequences.

Equations
Instances For
def Stream'.Seq.nil {α : Type u} :

The empty sequence

Equations
Instances For
instance Stream'.Seq.instInhabited {α : Type u} :
Equations
• Stream'.Seq.instInhabited = { default := Stream'.Seq.nil }
def Stream'.Seq.cons {α : Type u} (a : α) (s : ) :

Prepend an element to a sequence

Equations
Instances For
@[simp]
theorem Stream'.Seq.val_cons {α : Type u} (s : ) (x : α) :
() = Stream'.cons (some x) s
def Stream'.Seq.get? {α : Type u} :

Get the nth element of a sequence (if it exists)

Equations
• Stream'.Seq.get? = Subtype.val
Instances For
@[simp]
theorem Stream'.Seq.get?_mk {α : Type u} (f : Stream' ()) (hf : f.IsSeq) :
Stream'.Seq.get? f, hf = f
@[simp]
theorem Stream'.Seq.get?_nil {α : Type u} (n : ) :
Stream'.Seq.nil.get? n = none
@[simp]
theorem Stream'.Seq.get?_cons_zero {α : Type u} (a : α) (s : ) :
().get? 0 = some a
@[simp]
theorem Stream'.Seq.get?_cons_succ {α : Type u} (a : α) (s : ) (n : ) :
().get? (n + 1) = s.get? n
theorem Stream'.Seq.ext {α : Type u} {s : } {t : } (h : ∀ (n : ), s.get? n = t.get? n) :
s = t
theorem Stream'.Seq.cons_left_injective {α : Type u} (s : ) :
Function.Injective fun (x : α) =>
theorem Stream'.Seq.cons_right_injective {α : Type u} (x : α) :
def Stream'.Seq.TerminatedAt {α : Type u} (s : ) (n : ) :

A sequence has terminated at position n if the value at position n equals none.

Equations
• s.TerminatedAt n = (s.get? n = none)
Instances For
instance Stream'.Seq.terminatedAtDecidable {α : Type u} (s : ) (n : ) :
Decidable (s.TerminatedAt n)

It is decidable whether a sequence terminates at a given position.

Equations
def Stream'.Seq.Terminates {α : Type u} (s : ) :

A sequence terminates if there is some position n at which it has terminated.

Equations
• s.Terminates = ∃ (n : ), s.TerminatedAt n
Instances For
theorem Stream'.Seq.not_terminates_iff {α : Type u} {s : } :
¬s.Terminates ∀ (n : ), (s.get? n).isSome = true
def Stream'.Seq.omap {α : Type u} {β : Type v} {γ : Type w} (f : βγ) :
Option (α × β)Option (α × γ)

Functorial action of the functor Option (α × _)

Equations
• = match x with | none => none | some (a, b) => some (a, f b)
Instances For
def Stream'.Seq.head {α : Type u} (s : ) :

Get the first element of a sequence

Equations
• s.head = s.get? 0
Instances For
def Stream'.Seq.tail {α : Type u} (s : ) :

Get the tail of a sequence (or nil if the sequence is nil)

Equations
• s.tail = (s).tail,
Instances For
def Stream'.Seq.Mem {α : Type u} (a : α) (s : ) :

member definition for Seq

Equations
Instances For
instance Stream'.Seq.instMembership {α : Type u} :
Equations
• Stream'.Seq.instMembership = { mem := Stream'.Seq.Mem }
theorem Stream'.Seq.le_stable {α : Type u} (s : ) {m : } {n : } (h : m n) :
s.get? m = nones.get? n = none
theorem Stream'.Seq.terminated_stable {α : Type u} (s : ) {m : } {n : } :
m ns.TerminatedAt ms.TerminatedAt n

If a sequence terminated at position n, it also terminated at m ≥ n.

theorem Stream'.Seq.ge_stable {α : Type u} (s : ) {aₙ : α} {n : } {m : } (m_le_n : m n) (s_nth_eq_some : s.get? n = some aₙ) :
∃ (aₘ : α), s.get? m = some aₘ

If s.get? n = some aₙ for some value aₙ, then there is also some value aₘ such that s.get? = some aₘ for m ≤ n.

theorem Stream'.Seq.not_mem_nil {α : Type u} (a : α) :
¬a Stream'.Seq.nil
theorem Stream'.Seq.mem_cons {α : Type u} (a : α) (s : ) :
a
theorem Stream'.Seq.mem_cons_of_mem {α : Type u} (y : α) {a : α} {s : } :
a sa
theorem Stream'.Seq.eq_or_mem_of_mem_cons {α : Type u} {a : α} {b : α} {s : } :
a a = b a s
@[simp]
theorem Stream'.Seq.mem_cons_iff {α : Type u} {a : α} {b : α} {s : } :
a a = b a s
def Stream'.Seq.destruct {α : Type u} (s : ) :

Destructor for a sequence, resulting in either none (for nil) or some (a, s) (for cons a s).

Equations
• s.destruct = (fun (a' : α) => (a', s.tail)) <$> s.get? 0 Instances For theorem Stream'.Seq.destruct_eq_nil {α : Type u} {s : } : s.destruct = nones = Stream'.Seq.nil theorem Stream'.Seq.destruct_eq_cons {α : Type u} {s : } {a : α} {s' : } : s.destruct = some (a, s')s = @[simp] theorem Stream'.Seq.destruct_nil {α : Type u} : Stream'.Seq.nil.destruct = none @[simp] theorem Stream'.Seq.destruct_cons {α : Type u} (a : α) (s : ) : ().destruct = some (a, s) theorem Stream'.Seq.head_eq_destruct {α : Type u} (s : ) : s.head = Prod.fst <$> s.destruct
@[simp]
theorem Stream'.Seq.head_nil {α : Type u} :
@[simp]
theorem Stream'.Seq.head_cons {α : Type u} (a : α) (s : ) :
().head = some a
@[simp]
theorem Stream'.Seq.tail_nil {α : Type u} :
Stream'.Seq.nil.tail = Stream'.Seq.nil
@[simp]
theorem Stream'.Seq.tail_cons {α : Type u} (a : α) (s : ) :
().tail = s
@[simp]
theorem Stream'.Seq.get?_tail {α : Type u} (s : ) (n : ) :
s.tail.get? n = s.get? (n + 1)
def Stream'.Seq.recOn {α : Type u} {C : Sort v} (s : ) (h1 : C Stream'.Seq.nil) (h2 : (x : α) → (s : ) → C ()) :
C s

Recursion principle for sequences, compare with List.recOn.

Equations
• One or more equations did not get rendered due to their size.
Instances For
theorem Stream'.Seq.mem_rec_on {α : Type u} {C : } {a : α} {s : } (M : a s) (h1 : ∀ (b : α) (s' : ), a = b C s'C ()) :
C s
def Stream'.Seq.Corec.f {α : Type u} {β : Type v} (f : βOption (α × β)) :
×

Corecursor over pairs of Option values

Equations
• = match x with | none => (none, none) | some b => match f b with | none => (none, none) | some (a, b') => (some a, some b')
Instances For
def Stream'.Seq.corec {α : Type u} {β : Type v} (f : βOption (α × β)) (b : β) :

Corecursor for Seq α as a coinductive type. Iterates f to produce new elements of the sequence until none is obtained.

Equations
• = ⟨,
Instances For
@[simp]
theorem Stream'.Seq.corec_eq {α : Type u} {β : Type v} (f : βOption (α × β)) (b : β) :
().destruct = Stream'.Seq.omap (f b)
def Stream'.Seq.BisimO {α : Type u} (R : ) :
Prop

Bisimilarity relation over Option of Seq1 α

Equations
• = match x✝, x with | none, none => True | some (a, s), some (a', s') => a = a' R s s' | x, x_1 => False
Instances For
def Stream'.Seq.IsBisimulation {α : Type u} (R : ) :

a relation is bisimilar if it meets the BisimO test

Equations
Instances For
theorem Stream'.Seq.eq_of_bisim {α : Type u} (R : ) (bisim : ) {s₁ : } {s₂ : } (r : R s₁ s₂) :
s₁ = s₂
theorem Stream'.Seq.coinduction {α : Type u} {s₁ : } {s₂ : } :
s₁.head = s₂.head(∀ (β : Type u) (fr : β), fr s₁ = fr s₂fr s₁.tail = fr s₂.tail)s₁ = s₂
theorem Stream'.Seq.coinduction2 {α : Type u} {β : Type v} (s : ) (f : ) (g : ) (H : ∀ (s : ), Stream'.Seq.BisimO (fun (s1 s2 : ) => ∃ (s : ), s1 = f s s2 = g s) (f s).destruct (g s).destruct) :
f s = g s
def Stream'.Seq.ofList {α : Type u} (l : List α) :

Embed a list as a sequence

Equations
• l = l.get?,
Instances For
instance Stream'.Seq.coeList {α : Type u} :
Coe (List α) ()
Equations
• Stream'.Seq.coeList = { coe := Stream'.Seq.ofList }
@[simp]
theorem Stream'.Seq.ofList_nil {α : Type u} :
[] = Stream'.Seq.nil
@[simp]
theorem Stream'.Seq.ofList_get {α : Type u} (l : List α) (n : ) :
(l).get? n = l.get? n
@[simp]
theorem Stream'.Seq.ofList_cons {α : Type u} (a : α) (l : List α) :
(a :: l) =
def Stream'.Seq.ofStream {α : Type u} (s : ) :

Embed an infinite stream as a sequence

Equations
Instances For
instance Stream'.Seq.coeStream {α : Type u} :
Coe () ()
Equations
• Stream'.Seq.coeStream = { coe := Stream'.Seq.ofStream }
def Stream'.Seq.ofLazyList {α : Type u} :

Embed a LazyList α as a sequence. Note that even though this is non-meta, it will produce infinite sequences if used with cyclic LazyLists created by meta constructions.

Equations
• Stream'.Seq.ofLazyList = Stream'.Seq.corec fun (l : ) => match l with | LazyList.nil => none | => some (a, l'.get)
Instances For
instance Stream'.Seq.coeLazyList {α : Type u} :
Coe () ()
Equations
• Stream'.Seq.coeLazyList = { coe := Stream'.Seq.ofLazyList }
unsafe def Stream'.Seq.toLazyList {α : Type u} :

Translate a sequence into a LazyList. Since LazyList and List are isomorphic as non-meta types, this function is necessarily meta.

Equations
• x.toLazyList = let s := x; match s.destruct with | none => LazyList.nil | some (a, s') => LazyList.cons a { fn := fun (x : Unit) => s'.toLazyList }
Instances For
unsafe def Stream'.Seq.forceToList {α : Type u} (s : ) :
List α

Translate a sequence to a list. This function will run forever if run on an infinite sequence.

Equations
• s.forceToList = s.toLazyList.toList
Instances For

The sequence of natural numbers some 0, some 1, ...

Equations
Instances For
@[simp]
def Stream'.Seq.append {α : Type u} (s₁ : ) (s₂ : ) :

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
def Stream'.Seq.map {α : Type u} {β : Type v} (f : αβ) :

Map a function over a sequence.

Equations
• = match x with | s, al => ⟨,
Instances For
def Stream'.Seq.join {α : Type u} :

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
def Stream'.Seq.drop {α : Type u} (s : ) :

Remove the first n elements from the sequence.

Equations
• s.drop 0 = s
• s.drop n.succ = (s.drop n).tail
Instances For
def Stream'.Seq.take {α : Type u} :
List α

Take the first n elements of the sequence (producing a list)

Equations
Instances For
def Stream'.Seq.splitAt {α : Type u} :
List α ×

Split a sequence at n, producing a finite initial segment and an infinite tail.

Equations
• = ([], x)
• Stream'.Seq.splitAt n.succ x = match x.destruct with | none => ([], Stream'.Seq.nil) | some (x, s') => match with | (l, r) => (x :: l, r)
Instances For
def Stream'.Seq.zipWith {α : Type u} {β : Type v} {γ : Type w} (f : αβγ) (s₁ : ) (s₂ : ) :

Combine two sequences with a function

Equations
Instances For
@[simp]
theorem Stream'.Seq.get?_zipWith {α : Type u} {β : Type v} {γ : Type w} (f : αβγ) (s : ) (s' : ) (n : ) :
().get? n = Option.map₂ f (s.get? n) (s'.get? n)
def Stream'.Seq.zip {α : Type u} {β : Type v} :
Stream'.Seq (α × β)

Pair two sequences into a sequence of pairs

Equations
Instances For
theorem Stream'.Seq.get?_zip {α : Type u} {β : Type v} (s : ) (t : ) (n : ) :
(s.zip t).get? n = Option.map₂ Prod.mk (s.get? n) (t.get? n)
def Stream'.Seq.unzip {α : Type u} {β : Type v} (s : Stream'.Seq (α × β)) :

Separate a sequence of pairs into two sequences

Equations
Instances For
def Stream'.Seq.enum {α : Type u} (s : ) :

Enumerate a sequence by tagging each element with its index.

Equations
Instances For
@[simp]
theorem Stream'.Seq.get?_enum {α : Type u} (s : ) (n : ) :
s.enum.get? n = Option.map () (s.get? n)
@[simp]
theorem Stream'.Seq.enum_nil {α : Type u} :
Stream'.Seq.nil.enum = Stream'.Seq.nil
def Stream'.Seq.toList {α : Type u} (s : ) (h : s.Terminates) :
List α

Convert a sequence which is known to terminate into a list

Equations
• s.toList h =
Instances For
def Stream'.Seq.toStream {α : Type u} (s : ) (h : ¬s.Terminates) :

Convert a sequence which is known not to terminate into a stream

Equations
• s.toStream h n = (s.get? n).get
Instances For
def Stream'.Seq.toListOrStream {α : Type u} (s : ) [Decidable s.Terminates] :
List α

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
• s.toListOrStream = if h : s.Terminates then Sum.inl (s.toList h) else Sum.inr (s.toStream h)
Instances For
@[simp]
theorem Stream'.Seq.nil_append {α : Type u} (s : ) :
Stream'.Seq.nil.append s = s
@[simp]
theorem Stream'.Seq.cons_append {α : Type u} (a : α) (s : ) (t : ) :
().append t = Stream'.Seq.cons a (s.append t)
@[simp]
theorem Stream'.Seq.append_nil {α : Type u} (s : ) :
s.append Stream'.Seq.nil = s
@[simp]
theorem Stream'.Seq.append_assoc {α : Type u} (s : ) (t : ) (u : ) :
(s.append t).append u = s.append (t.append u)
@[simp]
theorem Stream'.Seq.map_nil {α : Type u} {β : Type v} (f : αβ) :
Stream'.Seq.map f Stream'.Seq.nil = Stream'.Seq.nil
@[simp]
theorem Stream'.Seq.map_cons {α : Type u} {β : Type v} (f : αβ) (a : α) (s : ) :
@[simp]
theorem Stream'.Seq.map_id {α : Type u} (s : ) :
= s
@[simp]
theorem Stream'.Seq.map_tail {α : Type u} {β : Type v} (f : αβ) (s : ) :
Stream'.Seq.map f s.tail = ().tail
theorem Stream'.Seq.map_comp {α : Type u} {β : Type v} {γ : Type w} (f : αβ) (g : βγ) (s : ) :
@[simp]
theorem Stream'.Seq.map_append {α : Type u} {β : Type v} (f : αβ) (s : ) (t : ) :
Stream'.Seq.map f (s.append t) = ().append ()
@[simp]
theorem Stream'.Seq.map_get? {α : Type u} {β : Type v} (f : αβ) (s : ) (n : ) :
().get? n = Option.map f (s.get? n)
Equations
Equations
@[simp]
theorem Stream'.Seq.join_nil {α : Type u} :
Stream'.Seq.nil.join = Stream'.Seq.nil
theorem Stream'.Seq.join_cons_nil {α : Type u} (a : α) (S : ) :
(Stream'.Seq.cons (a, Stream'.Seq.nil) S).join = Stream'.Seq.cons a S.join
theorem Stream'.Seq.join_cons_cons {α : Type u} (a : α) (b : α) (s : ) (S : ) :
(Stream'.Seq.cons (a, ) S).join = Stream'.Seq.cons a (Stream'.Seq.cons (b, s) S).join
@[simp]
theorem Stream'.Seq.join_cons {α : Type u} (a : α) (s : ) (S : ) :
(Stream'.Seq.cons (a, s) S).join = Stream'.Seq.cons a (s.append S.join)
@[simp]
theorem Stream'.Seq.join_append {α : Type u} (S : ) (T : ) :
(S.append T).join = S.join.append T.join
@[simp]
theorem Stream'.Seq.ofStream_cons {α : Type u} (a : α) (s : ) :
() =
@[simp]
theorem Stream'.Seq.ofList_append {α : Type u} (l : List α) (l' : List α) :
(l ++ l') = (l).append l'
@[simp]
theorem Stream'.Seq.ofStream_append {α : Type u} (l : List α) (s : ) :
(l ++ₛ s) = (l).append s
def Stream'.Seq.toList' {α : Type u_1} (s : ) :

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
theorem Stream'.Seq.dropn_add {α : Type u} (s : ) (m : ) (n : ) :
s.drop (m + n) = (s.drop m).drop n
theorem Stream'.Seq.dropn_tail {α : Type u} (s : ) (n : ) :
s.tail.drop n = s.drop (n + 1)
@[simp]
theorem Stream'.Seq.head_dropn {α : Type u} (s : ) (n : ) :
(s.drop n).head = s.get? n
theorem Stream'.Seq.mem_map {α : Type u} {β : Type v} (f : αβ) {a : α} {s : } :
a sf a
theorem Stream'.Seq.exists_of_mem_map {α : Type u} {β : Type v} {f : αβ} {b : β} {s : } :
b ∃ (a : α), a s f a = b
theorem Stream'.Seq.of_mem_append {α : Type u} {s₁ : } {s₂ : } {a : α} (h : a s₁.append s₂) :
a s₁ a s₂
theorem Stream'.Seq.mem_append_left {α : Type u} {s₁ : } {s₂ : } {a : α} (h : a s₁) :
a s₁.append s₂
@[simp]
theorem Stream'.Seq.enum_cons {α : Type u} (s : ) (x : α) :
().enum = Stream'.Seq.cons (0, x) (Stream'.Seq.map () s.enum)
def Stream'.Seq1.toSeq {α : Type u} :

Convert a Seq1 to a sequence.

Equations
• x.toSeq = match x with | (a, s) =>
Instances For
instance Stream'.Seq1.coeSeq {α : Type u} :
Coe () ()
Equations
• Stream'.Seq1.coeSeq = { coe := Stream'.Seq1.toSeq }
def Stream'.Seq1.map {α : Type u} {β : Type v} (f : αβ) :

Map a function on a Seq1

Equations
• = match x with | (a, s) => (f a, )
Instances For
theorem Stream'.Seq1.map_pair {α : Type u} {β : Type v} {f : αβ} {a : α} {s : } :
Stream'.Seq1.map f (a, s) = (f a, )
theorem Stream'.Seq1.map_id {α : Type u} (s : ) :
= s
def Stream'.Seq1.join {α : Type u} :

Flatten a nonempty sequence of nonempty sequences

Equations
• x.join = match x with | ((a, s), S) => match s.destruct with | none => (a, S.join) | some s' => (a, ().join)
Instances For
@[simp]
theorem Stream'.Seq1.join_nil {α : Type u} (a : α) (S : ) :
Stream'.Seq1.join ((a, Stream'.Seq.nil), S) = (a, S.join)
@[simp]
theorem Stream'.Seq1.join_cons {α : Type u} (a : α) (b : α) (s : ) (S : ) :
Stream'.Seq1.join ((a, ), S) = (a, (Stream'.Seq.cons (b, s) S).join)
def Stream'.Seq1.ret {α : Type u} (a : α) :

The return operator for the Seq1 monad, which produces a singleton sequence.

Equations
• = (a, Stream'.Seq.nil)
Instances For
instance Stream'.Seq1.instInhabited {α : Type u} [] :
Equations
def Stream'.Seq1.bind {α : Type u} {β : Type v} (s : ) (f : α) :

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 = ().join
Instances For
@[simp]
theorem Stream'.Seq1.join_map_ret {α : Type u} (s : ) :
(Stream'.Seq.map Stream'.Seq1.ret s).join = s
@[simp]
theorem Stream'.Seq1.bind_ret {α : Type u} {β : Type v} (f : αβ) (s : ) :
s.bind (Stream'.Seq1.ret f) =
@[simp]
theorem Stream'.Seq1.ret_bind {α : Type u} {β : Type v} (a : α) (f : α) :
().bind f = f a
@[simp]
theorem Stream'.Seq1.map_join' {α : Type u} {β : Type v} (f : αβ) (S : ) :
Stream'.Seq.map f S.join = ().join
@[simp]
theorem Stream'.Seq1.map_join {α : Type u} {β : Type v} (f : αβ) (S : ) :
Stream'.Seq1.map f S.join = ().join
@[simp]
theorem Stream'.Seq1.join_join {α : Type u} (SS : ) :
SS.join.join = (Stream'.Seq.map Stream'.Seq1.join SS).join
@[simp]
theorem Stream'.Seq1.bind_assoc {α : Type u} {β : Type v} {γ : Type w} (s : ) (f : α) (g : β) :
(s.bind f).bind g = s.bind fun (x : α) => (f x).bind g
Equations
Equations