Documentation

Mathlib.Data.Seq.Defs

Possibly infinite lists #

This file provides Stream'.Seq α, a 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.

Main definitions #

One can convert between sequences and other types: List, Stream', MLList using corresponding functions defined in this file.

There are also a number of operations and predicates on sequences mirroring those on lists: Seq.map, Seq.zip, Seq.zipWith, Seq.unzip, Seq.fold, Seq.update, Seq.drop, Seq.splitAt, Seq.append, Seq.join, Seq.enum, Seq.Pairwire, as well as a cases principle Seq.recOn which allows one to reason about sequences by cases (nil and cons).

Main statements #

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

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

Equations
    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.get? {α : Type u} :
              Seq αOption α

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

              Equations
                Instances For
                  @[simp]
                  theorem Stream'.Seq.val_eq_get {α : Type u} (s : Seq α) (n : ) :
                  s n = s.get? n
                  @[simp]
                  theorem Stream'.Seq.get?_mk {α : Type u} (f : Stream' (Option α)) (hf : f.IsSeq) :
                  get? f, hf = f
                  theorem Stream'.Seq.le_stable {α : Type u} (s : Seq α) {m n : } (h : m n) :
                  s.get? m = nones.get? n = none
                  theorem Stream'.Seq.ge_stable {α : Type u} (s : Seq α) {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.ext {α : Type u} {s t : Seq α} (h : ∀ (n : ), s.get? n = t.get? n) :
                  s = t
                  theorem Stream'.Seq.ext_iff {α : Type u} {s t : Seq α} :
                  s = t ∀ (n : ), s.get? n = t.get? n

                  Constructors #

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

                  The empty sequence

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

                      Prepend an element to a sequence

                      Equations
                        Instances For
                          @[simp]
                          theorem Stream'.Seq.val_cons {α : Type u} (s : Seq α) (x : α) :
                          (cons x s) = Stream'.cons (some x) s
                          @[simp]
                          theorem Stream'.Seq.get?_nil {α : Type u} (n : ) :
                          @[simp]
                          theorem Stream'.Seq.get?_zero_eq_none {α : Type u} {s : Seq α} :
                          s.get? 0 = none s = nil
                          @[simp]
                          theorem Stream'.Seq.get?_cons_zero {α : Type u} (a : α) (s : Seq α) :
                          (cons a s).get? 0 = some a
                          @[simp]
                          theorem Stream'.Seq.get?_cons_succ {α : Type u} (a : α) (s : Seq α) (n : ) :
                          (cons a s).get? (n + 1) = s.get? n
                          @[simp]
                          theorem Stream'.Seq.cons_ne_nil {α : Type u} {x : α} {s : Seq α} :
                          @[simp]
                          theorem Stream'.Seq.nil_ne_cons {α : Type u} {x : α} {s : Seq α} :
                          theorem Stream'.Seq.cons_left_injective {α : Type u} (s : Seq α) :
                          Function.Injective fun (x : α) => cons x s
                          @[simp]
                          theorem Stream'.Seq.cons_eq_cons {α : Type u} {x x' : α} {s s' : Seq α} :
                          cons x s = cons x' s' x = x' s = s'

                          Destructors #

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

                          Get the first element of a sequence

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

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

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

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

                                  Equations
                                    Instances For
                                      @[simp]
                                      theorem Stream'.Seq.get?_tail {α : Type u} (s : Seq α) (n : ) :
                                      s.tail.get? n = s.get? (n + 1)
                                      @[simp]
                                      theorem Stream'.Seq.destruct_cons {α : Type u} (a : α) (s : Seq α) :
                                      theorem Stream'.Seq.destruct_eq_cons {α : Type u} {s : Seq α} {a : α} {s' : Seq α} :
                                      s.destruct = some (a, s')s = cons a s'
                                      @[simp]
                                      theorem Stream'.Seq.head_cons {α : Type u} (a : α) (s : Seq α) :
                                      (cons a s).head = some a
                                      @[simp]
                                      theorem Stream'.Seq.tail_cons {α : Type u} (a : α) (s : Seq α) :
                                      (cons a s).tail = s
                                      theorem Stream'.Seq.head_eq_some {α : Type u} {s : Seq α} {x : α} (h : s.head = some x) :
                                      s = cons x s.tail
                                      theorem Stream'.Seq.head_eq_none {α : Type u} {s : Seq α} (h : s.head = none) :
                                      s = nil
                                      @[simp]
                                      theorem Stream'.Seq.head_eq_none_iff {α : Type u} {s : Seq α} :

                                      Recursion and corecursion principles #

                                      def Stream'.Seq.recOn {α : Type u} {motive : Seq αSort v} (s : Seq α) (nil : motive nil) (cons : (x : α) → (s : Seq α) → motive (cons x s)) :
                                      motive s

                                      Recursion principle for sequences, compare with List.recOn.

                                      Equations
                                        Instances For
                                          def Stream'.Seq.omap {α : Type u} {β : Type v} {γ : Type w} (f : βγ) :
                                          Option (α × β)Option (α × γ)

                                          Functorial action of the functor Option (α × _)

                                          Equations
                                            Instances For
                                              def Stream'.Seq.Corec.f {α : Type u} {β : Type v} (f : βOption (α × β)) :
                                              Option βOption α × Option β

                                              Corecursor over pairs of Option values

                                              Equations
                                                Instances For
                                                  def Stream'.Seq.corec {α : Type u} {β : Type v} (f : βOption (α × β)) (b : β) :
                                                  Seq α

                                                  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 : β) :
                                                      (corec f b).destruct = omap (corec f) (f b)
                                                      theorem Stream'.Seq.corec_nil {α : Type u} {β : Type v} (f : βOption (α × β)) (b : β) (h : f b = none) :
                                                      corec f b = nil
                                                      theorem Stream'.Seq.corec_cons {α : Type u} {β : Type v} {f : βOption (α × β)} {b : β} {x : α} {s : β} (h : f b = some (x, s)) :
                                                      corec f b = cons x (corec f s)

                                                      Bisimulation #

                                                      def Stream'.Seq.BisimO {α : Type u} (R : Seq αSeq αProp) :
                                                      Option (Seq1 α)Option (Seq1 α)Prop

                                                      Bisimilarity relation over Option of Seq1 α

                                                      Equations
                                                        Instances For
                                                          def Stream'.Seq.IsBisimulation {α : Type u} (R : Seq αSeq αProp) :

                                                          a relation is bisimilar if it meets the BisimO test

                                                          Equations
                                                            Instances For
                                                              theorem Stream'.Seq.eq_of_bisim {α : Type u} (R : Seq αSeq αProp) (bisim : IsBisimulation R) {s₁ s₂ : Seq α} (r : R s₁ s₂) :
                                                              s₁ = s₂

                                                              If two streams are bisimilar, then they are equal. There are also versions eq_of_bisim' and eq_of_bisim_strong that does not mention IsBisimulation and look more like an induction principles.

                                                              theorem Stream'.Seq.eq_of_bisim' {α : Type u} {s₁ s₂ : Seq α} (motive : Seq αSeq αProp) (base : motive s₁ s₂) (step : ∀ (s₁ s₂ : Seq α), motive s₁ s₂s₁ = nil s₂ = nil ∃ (x : α) (s₁' : Seq α) (s₂' : Seq α), s₁ = cons x s₁' s₂ = cons x s₂' motive s₁' s₂') :
                                                              s₁ = s₂

                                                              Coinductive principle for equality on sequences. This is a version of eq_of_bisim that looks more like an induction principle.

                                                              theorem Stream'.Seq.eq_of_bisim_strong {α : Type u} {s₁ s₂ : Seq α} (motive : Seq αSeq αProp) (base : motive s₁ s₂) (step : ∀ (s₁ s₂ : Seq α), motive s₁ s₂s₁ = s₂ ∃ (x : α) (s₁' : Seq α) (s₂' : Seq α), s₁ = cons x s₁' s₂ = cons x s₂' motive s₁' s₂') :
                                                              s₁ = s₂

                                                              Coinductive principle for equality on sequences. This is a version of eq_of_bisim' that requires proving only s₁ = s₂ instead of s₁ = nil ∧ s₂ = nil in step.

                                                              theorem Stream'.Seq.coinduction {α : Type u} {s₁ s₂ : Seq α} :
                                                              s₁.head = s₂.head(∀ (β : Type u) (fr : Seq αβ), fr s₁ = fr s₂fr s₁.tail = fr s₂.tail)s₁ = s₂
                                                              theorem Stream'.Seq.coinduction2 {α : Type u} {β : Type v} (s : Seq α) (f g : Seq αSeq β) (H : ∀ (s : Seq α), BisimO (fun (s1 s2 : Seq β) => ∃ (s : Seq α), s1 = f s s2 = g s) (f s).destruct (g s).destruct) :
                                                              f s = g s

                                                              Termination #

                                                              def Stream'.Seq.TerminatedAt {α : Type u} (s : Seq α) (n : ) :

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

                                                              Equations
                                                                Instances For
                                                                  instance Stream'.Seq.terminatedAtDecidable {α : Type u} (s : Seq α) (n : ) :

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

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

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

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

                                                                        The length of a terminating sequence.

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

                                                                            The ENat-valued length of a sequence. For non-terminating sequences, it is .

                                                                            Equations
                                                                              Instances For
                                                                                theorem Stream'.Seq.terminated_stable {α : Type u} (s : Seq α) {m n : } :
                                                                                m ns.TerminatedAt ms.TerminatedAt n

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

                                                                                @[simp]
                                                                                theorem Stream'.Seq.cons_not_terminatedAt_zero {α : Type u} {x : α} {s : Seq α} :
                                                                                @[simp]
                                                                                theorem Stream'.Seq.cons_terminatedAt_succ_iff {α : Type u} {x : α} {s : Seq α} {n : } :
                                                                                @[simp]
                                                                                theorem Stream'.Seq.terminates_cons_iff {α : Type u} {x : α} {s : Seq α} :

                                                                                Membership #

                                                                                def Stream'.Seq.Mem {α : Type u} (s : Seq α) (a : α) :

                                                                                member definition for Seq

                                                                                Equations
                                                                                  Instances For
                                                                                    theorem Stream'.Seq.get?_mem {α : Type u} {s : Seq α} {n : } {x : α} (h : s.get? n = some x) :
                                                                                    x s
                                                                                    theorem Stream'.Seq.mem_iff_exists_get? {α : Type u} {s : Seq α} {x : α} :
                                                                                    x s ∃ (i : ), some x = s.get? i
                                                                                    @[simp]
                                                                                    theorem Stream'.Seq.notMem_nil {α : Type u} (a : α) :
                                                                                    anil
                                                                                    theorem Stream'.Seq.mem_cons {α : Type u} (a : α) (s : Seq α) :
                                                                                    a cons a s
                                                                                    theorem Stream'.Seq.mem_cons_of_mem {α : Type u} (y : α) {a : α} {s : Seq α} :
                                                                                    a sa cons y s
                                                                                    theorem Stream'.Seq.eq_or_mem_of_mem_cons {α : Type u} {a b : α} {s : Seq α} :
                                                                                    a cons b sa = b a s
                                                                                    @[simp]
                                                                                    theorem Stream'.Seq.mem_cons_iff {α : Type u} {a b : α} {s : Seq α} :
                                                                                    a cons b s a = b a s
                                                                                    theorem Stream'.Seq.mem_rec_on {α : Type u} {C : Seq αProp} {a : α} {s : Seq α} (M : a s) (h1 : ∀ (b : α) (s' : Seq α), a = b C s'C (cons b s')) :
                                                                                    C s

                                                                                    Converting from/to other types #

                                                                                    def Stream'.Seq.ofList {α : Type u} (l : List α) :
                                                                                    Seq α

                                                                                    Embed a list as a sequence

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

                                                                                          Embed an infinite stream as a sequence

                                                                                          Equations
                                                                                            Instances For
                                                                                              instance Stream'.Seq.coeStream {α : Type u} :
                                                                                              Coe (Stream' α) (Seq α)
                                                                                              Equations
                                                                                                def Stream'.Seq.ofMLList {α : Type u} :
                                                                                                MLList Id αSeq α

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

                                                                                                Equations
                                                                                                  Instances For
                                                                                                    instance Stream'.Seq.coeMLList {α : Type u} :
                                                                                                    Coe (MLList Id α) (Seq α)
                                                                                                    Equations
                                                                                                      unsafe def Stream'.Seq.toMLList {α : Type u} :
                                                                                                      Seq αMLList Id α

                                                                                                      Translate a sequence into a MLList.

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

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

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

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

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

                                                                                                                  Convert a sequence which is known to terminate into a list

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

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

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

                                                                                                                              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
                                                                                                                                Instances For

                                                                                                                                  Operations on sequences #

                                                                                                                                  def Stream'.Seq.append {α : Type u} (s₁ s₂ : Seq α) :
                                                                                                                                  Seq α

                                                                                                                                  Append two sequences. If s₁ is infinite, then s₁ ++ s₂ = s₁, otherwise it puts s₂ at the location of the nil in s₁.

                                                                                                                                  Equations
                                                                                                                                    Instances For
                                                                                                                                      def Stream'.Seq.map {α : Type u} {β : Type v} (f : αβ) :
                                                                                                                                      Seq αSeq β

                                                                                                                                      Map a function over a sequence.

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

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

                                                                                                                                              Remove the first n elements from the sequence.

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

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

                                                                                                                                                  Equations
                                                                                                                                                    Instances For
                                                                                                                                                      def Stream'.Seq.zipWith {α : Type u} {β : Type v} {γ : Type w} (f : αβγ) (s₁ : Seq α) (s₂ : Seq β) :
                                                                                                                                                      Seq γ

                                                                                                                                                      Combine two sequences with a function

                                                                                                                                                      Equations
                                                                                                                                                        Instances For
                                                                                                                                                          def Stream'.Seq.zip {α : Type u} {β : Type v} :
                                                                                                                                                          Seq αSeq βSeq (α × β)

                                                                                                                                                          Pair two sequences into a sequence of pairs

                                                                                                                                                          Equations
                                                                                                                                                            Instances For
                                                                                                                                                              def Stream'.Seq.unzip {α : Type u} {β : Type v} (s : Seq (α × β)) :
                                                                                                                                                              Seq α × Seq β

                                                                                                                                                              Separate a sequence of pairs into two sequences

                                                                                                                                                              Equations
                                                                                                                                                                Instances For

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

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

                                                                                                                                                                      Enumerate a sequence by tagging each element with its index.

                                                                                                                                                                      Equations
                                                                                                                                                                        Instances For
                                                                                                                                                                          def Stream'.Seq.fold {α : Type u} {β : Type v} (s : Seq α) (init : β) (f : βαβ) :
                                                                                                                                                                          Seq β

                                                                                                                                                                          Folds a sequence using f, producing a sequence of intermediate values, i.e. [init, f init s.head, f (f init s.head) s.tail.head, ...].

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

                                                                                                                                                                              Applies f to the nth element of the sequence, if it exists, replacing that element with the result.

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

                                                                                                                                                                                  Sets the value of sequence s at index n to a. If the nth element does not exist (s terminates earlier), the sequence is left unchanged.

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

                                                                                                                                                                                      Pairwise R s means that all the elements with earlier indices are R-related to all the elements with later indices.

                                                                                                                                                                                      Pairwise R [1, 2, 3] ↔ R 1 2 ∧ R 1 3 ∧ R 2 3
                                                                                                                                                                                      

                                                                                                                                                                                      For example if R = (· ≠ ·) then it asserts s has no duplicates, and if R = (· < ·) then it asserts that s is (strictly) sorted.

                                                                                                                                                                                      Equations
                                                                                                                                                                                        Instances For