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

Commit 29b35a6

Browse files
committed
provide combinators and some consumers
1 parent 5ff949a commit 29b35a6

File tree

11 files changed

+99
-15
lines changed

11 files changed

+99
-15
lines changed

Iterator/Combinators/Drop.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ _TODO_: prove `Productive`
4444
Currently, this combinator incurs an additional O(1) cost with each output of `it`, even when the iterator
4545
does not drop any elements anymore.
4646
-/
47-
def IterM.drop [Iterator α m β]
48-
(n : Nat) (it : IterM (α := α) m β) :=
47+
def IterM.drop (n : Nat) (it : IterM (α := α) m β) :=
4948
toIter (Drop.mk n it) m β
5049

5150
inductive Drop.PlausibleStep [Iterator α m β] (it : IterM (α := Drop α m β) m β) :

Iterator/Combinators/FlatMap.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ end Dependent
462462

463463
section IterM
464464

465-
set_option pp.universes true in
466465
@[always_inline, inline]
467466
def IterM.flatMapD {α : Type w} {β : Type v} {α₂ : β → Type w}
468467
{γ : Type x} {m : Type w → Type w'}

Iterator/Consumers/Loop.lean

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import Iterator.Basic
44
def IterM.DefaultConsumers.forIn {m : Type w → Type w'} {n : Type w → Type w''} [Monad n] [MonadLiftT m n]
55
{α : Type w} {β : Type v} {γ : Type w}
66
[Iterator α m β] [Finite α m]
7-
(it : IterM (α := α) m β) (init : γ) {successor_of : β → γ → γ → Prop}
8-
(f : (b : β) → (c : γ) → n (Subtype fun s : ForInStep γ => successor_of b c s.value)) : n γ := do
7+
(it : IterM (α := α) m β) (init : γ)
8+
(f : (b : β) → (c : γ) → n (ForInStep γ)) : n γ := do
99
match (← it.stepH).inflate with
1010
| .yield it' out _ =>
1111
match ← f out init with
12-
| .yield c, _⟩ => IterM.DefaultConsumers.forIn it' c f
13-
| .done c, _⟩ => return c
12+
| .yield c => IterM.DefaultConsumers.forIn it' c f
13+
| .done c => return c
1414
| .skip it' _ => IterM.DefaultConsumers.forIn it' init f
1515
| .done _ => return init
1616
termination_by it.finitelyManySteps
1717

1818
class IteratorFor (α : Type w) (m : Type w → Type w') {β : Type v} [Iterator α m β] (n : Type w → Type w'') where
19-
forIn : ∀ {γ : Type w}, IterM (α := α) m β → γ → {successor_of : β → γ → γ → Prop} →
20-
((b : β) → (c : γ) → n (Subtype fun s : ForInStep γ => successor_of b c s.value)) →
19+
forIn : ∀ {γ : Type w}, IterM (α := α) m β → γ →
20+
((b : β) → (c : γ) → n (ForInStep γ)) →
2121
n γ
2222

2323
class LawfulIteratorFor (α : Type w) (m : Type w → Type w') (n : Type w → Type w'')
@@ -38,15 +38,13 @@ instance (α : Type w) (m : Type w → Type w') (n : Type w → Type w')
3838
fun _ => rfl⟩
3939

4040
instance {m : Type w → Type w'} {n : Type w → Type w''}
41-
{α : Type w} {β : Type v} [Iterator α m β] [Finite α m] [IteratorFor α m n] :
41+
{α : Type w} {β : Type v} [Iterator α m β] [IteratorFor α m n] :
4242
ForIn n (IterM (α := α) m β) β where
43-
forIn it init stepper := IteratorFor.forIn it init
44-
(successor_of := fun _ _ _ => True)
45-
(fun b c => (⟨·, True.intro⟩) <$> stepper b c)
43+
forIn := IteratorFor.forIn
4644

4745
@[specialize]
4846
def IterM.foldM {m : Type w → Type w'} {n : Type w → Type w'} [Monad n]
49-
{α : Type w} {β : Type v} {γ : Type w} [Iterator α m β] [Finite α m] [IteratorFor α m n]
47+
{α : Type w} {β : Type v} {γ : Type w} [Iterator α m β] [IteratorFor α m n]
5048
(f : γ → β → n γ) (init : γ) (it : IterM (α := α) m β) : n γ :=
5149
ForIn.forIn it init (fun x acc => ForInStep.yield <$> f acc x)
5250

Iterator/Pure/Basic.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
prelude
12
import Iterator.Basic
23

34
structure Iter {α : Type w} (β : Type v) : Type w where
@@ -39,7 +40,7 @@ def Iter.plausible_skip_successor_of {α : Type w} {β : Type v} [Iterator α Id
3940
it.plausible_step (.skip it')
4041

4142
@[always_inline, inline]
42-
def Iter.step {α : Type w} {β : Type w} [Iterator α Id β] [Functor Id]
43+
def Iter.step {α : Type w} {β : Type v} [Iterator α Id β]
4344
(it : Iter (α := α) β) : it.Step := by
4445
refine it.toIterM.stepH.run.inflate.map IterM.toPureIter id _ ?_
4546
intro step hp
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prelude
2+
import Iterator.Combinators.Drop
3+
import Iterator.Pure.Basic
4+
5+
@[always_inline, inline]
6+
def Iter.drop {α : Type w} {β : Type v} (n : Nat) (it : Iter (α := α) β) :
7+
Iter (α := Drop α Id β) β :=
8+
it.toIterM.drop n |>.toPureIter
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prelude
2+
import Iterator.Combinators.FilterMap
3+
import Iterator.Pure.Basic
4+
5+
-- TODO: consistently require or do not require `Iterator` instances
6+
-- in combinators
7+
@[always_inline, inline]
8+
def Iter.filterMap {α : Type w} {β : Type v} [Iterator α Id β]
9+
(f : β → Option γ) (it : Iter (α := α) β) :=
10+
((it.toIterM.filterMap f).toPureIter : Iter γ)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
prelude
2+
import Iterator.Combinators.FlatMap
3+
import Iterator.Pure.Basic
4+
5+
@[always_inline, inline]
6+
def Iter.flatMap {α : Type w} {β : Type v} {α₂ : Type w} {γ : Type x}
7+
[Iterator α Id β] [Iterator α₂ Id γ] (f : β → Iter (α := α₂) γ)
8+
(it : Iter (α := α) β) :=
9+
((it.toIterM.flatMap (Iter.toIterM ∘ f)).toPureIter : Iter γ)
10+
11+
12+
@[always_inline, inline]
13+
def Iter.flatMapD {α : Type w} {β : Type v} {α₂ : β → Type w} {γ : Type x}
14+
[Iterator α Id β] [∀ b, Iterator (α₂ b) Id γ] (f : (b : β) → Iter (α := α₂ b) γ)
15+
(it : Iter (α := α) β) :=
16+
-- TODO: confusing error message when writing (toIterM ∘ (f b)) (it's fine that it fails)
17+
((it.toIterM.flatMapD (fun b => (f b).toIterM)).toPureIter : Iter γ)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
prelude
2+
import Iterator.Combinators.Take
3+
import Iterator.Pure.Basic
4+
5+
@[always_inline, inline]
6+
def Iter.take {α : Type w} {β : Type v} (n : Nat) (it : Iter (α := α) β) :
7+
Iter (α := Take α Id β) β :=
8+
it.toIterM.take n |>.toPureIter

Iterator/Pure/Combinators/Zip.lean

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
prelude
2+
import Iterator.Combinators.Zip
3+
import Iterator.Pure.Basic
4+
5+
@[always_inline, inline]
6+
def Iter.zip {α₁ : Type w} {β₁: Type v₁} {α₂ : Type w} {β₂ : Type v₂}
7+
[Iterator α₁ Id β₁] [Iterator α₂ Id β₂]
8+
(left : Iter (α := α₁) β₁) (right : Iter (α := α₂) β₂) :=
9+
((left.toIterM.zipH right.toIterM).toPureIter : Iter (β₁ × β₂))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Iterator.Pure.Basic
2+
import Iterator.Consumers.Collect
3+
4+
@[always_inline, inline]
5+
def Iter.toArray {α : Type w} {β : Type w}
6+
[Iterator α Id β] [IteratorToArray α Id] (it : Iter (α := α) β) : Array β :=
7+
it.toIterM.toArray
8+
9+
@[always_inline, inline]
10+
def Iter.reverseToList {α : Type w} {β : Type w}
11+
[Iterator α Id β] [Finite α Id] (it : Iter (α := α) β) : List β :=
12+
it.toIterM.reverseToList
13+
14+
@[always_inline, inline]
15+
def Iter.toList {α : Type w} {β : Type w}
16+
[Iterator α Id β] [IteratorToArray α Id] (it : Iter (α := α) β) : List β :=
17+
it.toIterM.toList

0 commit comments

Comments
 (0)