Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit aa4054d

Browse files
committed
zip lemmas and getAtIdx? consumer
1 parent 7f3bdba commit aa4054d

File tree

8 files changed

+431
-2
lines changed

8 files changed

+431
-2
lines changed

Iterator/Combinators/Monadic/Zip.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ instance Zip.instIterator [Monad m] :
5959
pure <| .done (.doneRight hm hp)
6060

6161
@[inline]
62-
def IterM.zip [Monad m]
62+
def IterM.zip
6363
(left : IterM (α := α₁) m β₁) (right : IterM (α := α₂) m β₂) :
6464
IterM (α := Zip α₁ m α₂ β₂) m (β₁ × β₂) :=
6565
toIter ⟨left, none, right⟩ m _

Iterator/Consumers.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Authors: Paul Reichert
55
-/
66
prelude
77
import Iterator.Consumers.Monadic
8+
import Iterator.Consumers.Access
89
import Iterator.Consumers.Collect
910
import Iterator.Consumers.Loop
1011
import Iterator.Consumers.Partial

Iterator/Consumers/Access.lean

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/-
2+
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Paul Reichert
5+
-/
6+
prelude
7+
import Iterator.Consumers.Monadic.Access
8+
import Iterator.Consumers.Partial
9+
10+
@[specialize]
11+
def Iter.getAtIdx? {α β} [Iterator α Id β] [Productive α Id]
12+
(n : Nat) (it : Iter (α := α) β) : Option β :=
13+
match it.step with
14+
| .yield it' out _ =>
15+
match n with
16+
| 0 => some out
17+
| k + 1 => it'.getAtIdx? k
18+
| .skip it' _ => it'.getAtIdx? n
19+
| .done _ => none
20+
termination_by (n, it.finitelyManySkips)
21+
22+
@[specialize]
23+
partial def Iter.Partial.getAtIdx? {α β} [Iterator α Id β] [Monad Id]
24+
(n : Nat) (it : Iter.Partial (α := α) β) : Option β := do
25+
match it.it.step with
26+
| .yield it' out _ =>
27+
match n with
28+
| 0 => some out
29+
| k + 1 => (⟨it'⟩ : Iter.Partial (α := α) β).getAtIdx? k
30+
| .skip it' _ => (⟨it'⟩ : Iter.Partial (α := α) β).getAtIdx? n
31+
| .done _ => none
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/-
2+
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Paul Reichert
5+
-/
6+
prelude
7+
import Iterator.Basic
8+
import Iterator.Consumers.Monadic.Partial
9+
10+
@[specialize]
11+
def IterM.getAtIdx? {α m β} [Iterator α m β] [Productive α m] [Monad m]
12+
(n : Nat) (it : IterM (α := α) m β) : m (Option β) := do
13+
match ← it.step with
14+
| .yield it' out _ =>
15+
match n with
16+
| 0 => return some out
17+
| k + 1 => it'.getAtIdx? k
18+
| .skip it' _ => it'.getAtIdx? n
19+
| .done _ => return none
20+
termination_by (n, it.finitelyManySkips)
21+
22+
@[specialize]
23+
partial def IterM.Partial.getAtIdx? {α m β} [Iterator α m β] [Monad m]
24+
(n : Nat) (it : IterM.Partial (α := α) m β) : m (Option β) := do
25+
match ← it.it.step with
26+
| .yield it' out _ =>
27+
match n with
28+
| 0 => return some out
29+
| k + 1 => (⟨it'⟩ : IterM.Partial (α := α) m β).getAtIdx? k
30+
| .skip it' _ => (⟨it'⟩ : IterM.Partial (α := α) m β).getAtIdx? n
31+
| .done _ => return none

Iterator/Lemmas/Combinators/Take.lean

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Authors: Paul Reichert
55
-/
66
prelude
77
import Iterator.Combinators.Take
8+
import Iterator.Consumers.Access
89
import Iterator.Lemmas.Monadic.Take
910
import Iterator.Lemmas.Consumers
1011

@@ -52,3 +53,26 @@ theorem Iter.toList_take_of_finite {α β} [Iterator α Id β] {n : Nat}
5253
· simp [ihy h]
5354
· simp [ihs h]
5455
· simp
56+
57+
theorem Iter.getAtIdx?_take {α β}
58+
[Iterator α Id β] [Productive α Id] {k l : Nat}
59+
{it : Iter (α := α) β} :
60+
(it.take k).getAtIdx? l = if l < k then it.getAtIdx? l else none := by
61+
revert k
62+
fun_induction it.getAtIdx? l
63+
case case1 it it' out h h' =>
64+
intro k
65+
simp [getAtIdx?.eq_def (it := it.take k), getAtIdx?.eq_def (it := it), step_take, h']
66+
cases k <;> simp
67+
case case2 it it' out h h' l ih =>
68+
intro k
69+
simp [getAtIdx?.eq_def (it := it.take k), getAtIdx?.eq_def (it := it), step_take, h']
70+
cases k <;> cases l <;> simp [ih]
71+
case case3 l it it' h h' ih =>
72+
intro k
73+
simp [getAtIdx?.eq_def (it := it.take k), getAtIdx?.eq_def (it := it), step_take, h']
74+
cases k <;> cases l <;> simp [ih]
75+
case case4 l it h h' =>
76+
intro k
77+
simp only [getAtIdx?.eq_def (it := it.take k), getAtIdx?.eq_def (it := it), step_take, h']
78+
cases k <;> cases l <;> simp

0 commit comments

Comments
 (0)