@@ -26,13 +26,6 @@ def ωSequence (α : Type u) := ℕ → α
2626
2727namespace ωSequence
2828
29- /-- Prepend an element to an ω-sequence. -/
30- def cons (a : α) (s : ωSequence α) : ωSequence α
31- | 0 => a
32- | n + 1 => s n
33-
34- @[inherit_doc] scoped infixr :67 " :: " => cons
35-
3629/-- Head of an ω-sequence: `ωSequence.head s = ωSequence s 0`. -/
3730abbrev head (s : ωSequence α) : α := s 0
3831
@@ -51,22 +44,22 @@ def take : ℕ → ωSequence α → List α
5144def extract (xs : ωSequence α) (m n : ℕ) : List α :=
5245 take (n - m) (xs.drop m)
5346
47+ /-- Prepend an element to an ω-sequence. -/
48+ def cons (a : α) (s : ωSequence α) : ωSequence α
49+ | 0 => a
50+ | n + 1 => s n
51+
52+ @[inherit_doc] scoped infixr :67 " :: " => cons
53+
5454/-- Append an ω-sequence to a list. -/
5555def appendωSequence : List α → ωSequence α → ωSequence α
5656 | [], s => s
5757 | List.cons a l, s => a::appendωSequence l s
5858
5959@[inherit_doc] infixl :65 " ++ω " => appendωSequence
6060
61- /-- Proposition saying that all elements of an ω-sequence satisfy a predicate. -/
62- def All (p : α → Prop ) (s : ωSequence α) := ∀ n, p (s n)
63-
64- /-- Proposition saying that at least one element of an ω-sequence satisfies a predicate. -/
65- def Any (p : α → Prop ) (s : ωSequence α) := ∃ n, p (s n)
66-
67- /-- `a ∈ s` means that `a = ωSequence n s` for some `n`. -/
68- instance : Membership α (ωSequence α) :=
69- ⟨fun s a => Any (fun b => a = b) s⟩
61+ /-- The constant ω-sequence: `ωSequence n (ωSequence.const a) = a`. -/
62+ def const (a : α) : ωSequence α := fun _ => a
7063
7164/-- Apply a function `f` to all elements of an ω-sequence `s`. -/
7265def map (f : α → β) (s : ωSequence α) : ωSequence β := fun n => f (s n)
@@ -76,100 +69,9 @@ def map (f : α → β) (s : ωSequence α) : ωSequence β := fun n => f (s n)
7669def zip (f : α → β → δ) (s₁ : ωSequence α) (s₂ : ωSequence β) : ωSequence δ :=
7770 fun n => f (s₁ n) (s₂ n)
7871
79- /-- The ω-sequence of natural numbers: `ωSequence n ωSequence.nats = n`. -/
80- def nats : ωSequence ℕ := fun n => n
81-
82- /-- Enumerate an ω-sequence by tagging each element with its index. -/
83- def enum (s : ωSequence α) : ωSequence (ℕ × α) := fun n => (n, s n)
84-
85- /-- The constant ω-sequence: `ωSequence n (ωSequence.const a) = a`. -/
86- def const (a : α) : ωSequence α := fun _ => a
87-
8872/-- Iterates of a function as an ω-sequence. -/
8973def iterate (f : α → α) (a : α) : ωSequence α
9074 | 0 => a
9175 | n + 1 => f (iterate f a n)
9276
93- /-- Given functions `f : α → β` and `g : α → α`, `corec f g` creates an ω-sequence by:
94- 1. Starting with an initial value `a : α`
95- 2. Applying `g` repeatedly to get an ω-sequence of α values
96- 3. Applying `f` to each value to convert them to β
97- -/
98- def corec (f : α → β) (g : α → α) : α → ωSequence β := fun a => map f (iterate g a)
99-
100- /-- Given an initial value `a : α` and functions `f : α → β` and `g : α → α`,
101- `corecOn a f g` creates an ω-sequence by repeatedly:
102- 1. Applying `f` to the current value to get the next ω-sequence element
103- 2. Applying `g` to get the next value to process
104- This is equivalent to `corec f g a`. -/
105- def corecOn (a : α) (f : α → β) (g : α → α) : ωSequence β :=
106- corec f g a
107-
108- /-- Given a function `f : α → β × α`, `corec' f` creates an ω-sequence by repeatedly:
109- 1. Starting with an initial value `a : α`
110- 2. Applying `f` to get both the next ω-sequence element (β) and next state value (α)
111- This is a more convenient form when the next element and state are computed together. -/
112- def corec' (f : α → β × α) : α → ωSequence β :=
113- corec (Prod.fst ∘ f) (Prod.snd ∘ f)
114-
115- /-- Use a state monad to generate an ω-sequence through corecursion -/
116- def corecState {σ α} (cmd : StateM σ α) (s : σ) : ωSequence α :=
117- corec Prod.fst (cmd.run ∘ Prod.snd) (cmd.run s)
118-
119- -- corec is also known as unfolds
120- abbrev unfolds (g : α → β) (f : α → α) (a : α) : ωSequence β :=
121- corec g f a
122-
123- /-- Interleave two ω-sequences. -/
124- def interleave (s₁ s₂ : ωSequence α) : ωSequence α :=
125- corecOn (s₁, s₂) (fun ⟨s₁, _⟩ => head s₁) fun ⟨s₁, s₂⟩ => (s₂, tail s₁)
126-
127- @[inherit_doc] infixl :65 " ⋈ " => interleave
128-
129- /-- Elements of an ω-sequence with even indices. -/
130- def even (s : ωSequence α) : ωSequence α :=
131- corec head (fun s => tail (tail s)) s
132-
133- /-- Elements of an ω-sequence with odd indices. -/
134- def odd (s : ωSequence α) : ωSequence α :=
135- even (tail s)
136-
137- /-- An auxiliary definition for `ωSequence.cycle` corecursive def -/
138- protected def cycleF : α × List α × α × List α → α
139- | (v, _, _, _) => v
140-
141- /-- An auxiliary definition for `ωSequence.cycle` corecursive def -/
142- protected def cycleG : α × List α × α × List α → α × List α × α × List α
143- | (_, [], v₀, l₀) => (v₀, l₀, v₀, l₀)
144- | (_, List.cons v₂ l₂, v₀, l₀) => (v₂, l₂, v₀, l₀)
145-
146- /-- Interpret a nonempty list as a cyclic ω-sequence. -/
147- def cycle : ∀ l : List α, l ≠ [] → ωSequence α
148- | [], h => absurd rfl h
149- | List.cons a l, _ => corec ωSequence.cycleF ωSequence.cycleG (a, l, a, l)
150-
151- /-- Tails of an ω-sequence, starting with `ωSequence.tail s`. -/
152- def tails (s : ωSequence α) : ωSequence (ωSequence α) :=
153- corec id tail (tail s)
154-
155- /-- An auxiliary definition for `ωSequence.inits`. -/
156- def initsCore (l : List α) (s : ωSequence α) : ωSequence (List α) :=
157- corecOn (l, s) (fun ⟨a, _⟩ => a) fun p =>
158- match p with
159- | (l', s') => (l' ++ [head s'], tail s')
160-
161- /-- Nonempty initial segments of an ω-sequence. -/
162- def inits (s : ωSequence α) : ωSequence (List α) :=
163- initsCore [head s] (tail s)
164-
165- /-- A constant ω-sequence, same as `ωSequence.const`. -/
166- def pure (a : α) : ωSequence α :=
167- const a
168-
169- /-- Given an ω-sequence of functions and an ω-sequence of values,
170- apply `n`-th function to `n`-th value. -/
171- def apply (f : ωSequence (α → β)) (s : ωSequence α) : ωSequence β := fun n => (f n) (s n)
172-
173- @[inherit_doc] infixl :75 " ⊛ " => apply -- input as `\circledast`
174-
17577end ωSequence
0 commit comments