Skip to content

Commit b3e6ea4

Browse files
committed
introduce default size instances
1 parent 308473e commit b3e6ea4

File tree

9 files changed

+79
-3
lines changed

9 files changed

+79
-3
lines changed

src/Init/Data/Iterators/Consumers/Monadic/Loop.lean

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,8 @@ It simply iterates using `IteratorLoopPartial` and counts the elements.
411411
For certain iterators, more efficient implementations are possible and should be used instead.
412412
-/
413413
@[always_inline, inline]
414-
instance IteratorSize.Partial.defaultImplementation {α β : Type w} {m : Type w → Type w'} [Monad m]
415-
[Iterator α m β] [Finite α m] [IteratorLoopPartial α m m] :
414+
instance IteratorSizePartial.defaultImplementation {α β : Type w} {m : Type w → Type w'} [Monad m]
415+
[Iterator α m β] [IteratorLoopPartial α m m] :
416416
IteratorSizePartial α m where
417417
size := IterM.DefaultConsumers.sizePartial
418418

@@ -422,6 +422,10 @@ are caused by calling `size`, and if the monad is nondeterministic, it is also u
422422
returned value should be. The reference implementation, `IteratorSize.defaultImplementation`,
423423
simply iterates over the whole iterator monadically, counting the number of emitted values.
424424
An `IteratorSize` instance is considered lawful if it is equal to the reference implementation.
425+
426+
**Performance**:
427+
428+
Default performance is linear in the number of steps taken by the iterator.
425429
-/
426430
@[always_inline, inline]
427431
def IterM.size {α : Type} {m : TypeType w'} {β : Type} [Iterator α m β] [Monad m]
@@ -439,6 +443,10 @@ An `IteratorSize` instance is considered lawful if it is equal to the reference
439443
440444
This is the partial version of `size`. It does not require a proof of finiteness and might loop
441445
forever. It is not possible to verify the behavior in Lean because it uses `partial`.
446+
447+
**Performance**:
448+
449+
Default performance is linear in the number of steps taken by the iterator.
442450
-/
443451
@[always_inline, inline]
444452
def IterM.Partial.size {α : Type} {m : TypeType w'} {β : Type} [Iterator α m β] [Monad m]

src/Std/Data/Iterators/Combinators/Monadic/Drop.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,12 @@ instance Drop.instIteratorLoopPartial [Monad m] [Monad n] [Iterator α m β] :
166166
IteratorLoopPartial (Drop α m β) m n :=
167167
.defaultImplementation
168168

169+
instance {α : Type w} [Monad m] [Iterator α m β] [Finite α m] [IteratorLoop α m m] :
170+
IteratorSize (Drop α m β) m :=
171+
.defaultImplementation
172+
173+
instance {α : Type w} [Monad m] [Iterator α m β] [IteratorLoopPartial α m m] :
174+
IteratorSizePartial (Drop α m β) m :=
175+
.defaultImplementation
176+
169177
end Std.Iterators

src/Std/Data/Iterators/Combinators/Monadic/DropWhile.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,12 @@ instance DropWhile.instIteratorForPartial [Monad m] [Monad n] [Iterator α m β]
286286
IteratorLoopPartial (DropWhile α m β P) m n :=
287287
.defaultImplementation
288288

289+
instance {α : Type w} [Monad m] [Iterator α m β] [Finite α m] [IteratorLoop α m m] {P} :
290+
IteratorSize (DropWhile α m β P) m :=
291+
.defaultImplementation
292+
293+
instance {α : Type w} [Monad m] [Iterator α m β] [IteratorLoopPartial α m m] {P} :
294+
IteratorSizePartial (DropWhile α m β P) m :=
295+
.defaultImplementation
296+
289297
end Std.Iterators

src/Std/Data/Iterators/Combinators/Monadic/FilterMap.lean

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ instance FilterMap.instIteratorLoop {α β γ : Type w} {m : Type w → Type w'}
232232
instance FilterMap.instIteratorLoopPartial {α β γ : Type w} {m : Type w → Type w'}
233233
{n : Type w → Type w''} {o : Type w → Type w'''}
234234
[Monad n] [Monad o] [Iterator α m β] {lift : ⦃α : Type w⦄ → m α → n α}
235-
{f : β → PostconditionT n (Option γ)} [Finite α m] :
235+
{f : β → PostconditionT n (Option γ)} :
236236
IteratorLoopPartial (FilterMap α m n lift f) n o :=
237237
.defaultImplementation
238238

@@ -597,4 +597,16 @@ def IterM.filter {α β : Type w} {m : Type w → Type w'} [Iterator α m β] [M
597597
(f : β → Bool) (it : IterM (α := α) m β) :=
598598
(it.filterMap (fun b => if f b then some b else none) : IterM m β)
599599

600+
instance {α β γ : Type w} {m : Type w → Type w'}
601+
{n : Type w → Type w''} [Monad n] [Iterator α m β] {lift : ⦃α : Type w⦄ → m α → n α}
602+
{f : β → PostconditionT n (Option γ)} [Finite α m] :
603+
IteratorSize (FilterMap α m n lift f) n :=
604+
.defaultImplementation
605+
606+
instance {α β γ : Type w} {m : Type w → Type w'}
607+
{n : Type w → Type w''} [Monad n] [Iterator α m β] {lift : ⦃α : Type w⦄ → m α → n α}
608+
{f : β → PostconditionT n (Option γ)} :
609+
IteratorSizePartial (FilterMap α m n lift f) n :=
610+
.defaultImplementation
611+
600612
end Std.Iterators

src/Std/Data/Iterators/Combinators/Monadic/Take.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,12 @@ instance Take.instIteratorLoopPartial [Monad m] [Monad n] [Iterator α m β]
148148
IteratorLoopPartial (Take α m β) m n :=
149149
.defaultImplementation
150150

151+
instance {α : Type w} [Monad m] [Iterator α m β] [Finite α m] [IteratorLoop α m m] :
152+
IteratorSize (Take α m β) m :=
153+
.defaultImplementation
154+
155+
instance {α : Type w} [Monad m] [Iterator α m β] [IteratorLoopPartial α m m] :
156+
IteratorSizePartial (Take α m β) m :=
157+
.defaultImplementation
158+
151159
end Std.Iterators

src/Std/Data/Iterators/Combinators/Monadic/TakeWhile.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,12 @@ instance TakeWhile.instIteratorForPartial [Monad m] [Monad n] [Iterator α m β]
242242
IteratorLoopPartial (TakeWhile α m β P) m n :=
243243
.defaultImplementation
244244

245+
instance {α : Type w} [Monad m] [Iterator α m β] [Finite α m] [IteratorLoop α m m] {P} :
246+
IteratorSize (TakeWhile α m β P) m :=
247+
.defaultImplementation
248+
249+
instance {α : Type w} [Monad m] [Iterator α m β] [IteratorLoopPartial α m m] {P} :
250+
IteratorSizePartial (TakeWhile α m β P) m :=
251+
.defaultImplementation
252+
245253
end Std.Iterators

src/Std/Data/Iterators/Combinators/Monadic/Zip.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,12 @@ instance Zip.instIteratorLoopPartial [Monad m] [Monad n] :
392392
IteratorLoopPartial (Zip α₁ m α₂ β₂) m n :=
393393
.defaultImplementation
394394

395+
instance Zip.instIteratorSize [Monad m] [Finite (Zip α₁ m α₂ β₂) m] :
396+
IteratorSize (Zip α₁ m α₂ β₂) m :=
397+
.defaultImplementation
398+
399+
instance Zip.instIteratorSizePartial [Monad m] :
400+
IteratorSizePartial (Zip α₁ m α₂ β₂) m :=
401+
.defaultImplementation
402+
395403
end Std.Iterators

src/Std/Data/Iterators/Producers/Monadic/Array.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,12 @@ instance {α : Type w} [Monad m] [Monad n] : IteratorLoop (ArrayIterator α) m n
125125
instance {α : Type w} [Monad m] [Monad n] : IteratorLoopPartial (ArrayIterator α) m n :=
126126
.defaultImplementation
127127

128+
@[always_inline, inline]
129+
instance {α : Type w} [Monad m] : IteratorSize (ArrayIterator α) m :=
130+
.defaultImplementation
131+
132+
@[always_inline, inline]
133+
instance {α : Type w} [Monad m] : IteratorSizePartial (ArrayIterator α) m :=
134+
.defaultImplementation
135+
128136
end Std.Iterators

src/Std/Data/Iterators/Producers/Monadic/List.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,12 @@ instance {α : Type w} [Monad m] [Monad n] : IteratorLoop (ListIterator α) m n
7474
instance {α : Type w} [Monad m] [Monad n] : IteratorLoopPartial (ListIterator α) m n :=
7575
.defaultImplementation
7676

77+
@[always_inline, inline]
78+
instance {α : Type w} [Monad m] : IteratorSize (ListIterator α) m :=
79+
.defaultImplementation
80+
81+
@[always_inline, inline]
82+
instance {α : Type w} [Monad m] : IteratorSizePartial (ListIterator α) m :=
83+
.defaultImplementation
84+
7785
end Std.Iterators

0 commit comments

Comments
 (0)