@@ -416,6 +416,169 @@ def IterM.Partial.drain {α : Type w} {m : Type w → Type w'} [Monad m] {β : T
416416 m PUnit :=
417417 it.fold (γ := PUnit) (fun _ _ => .unit) .unit
418418
419+ set_option doc.verso true in
420+ /--
421+ Returns {lean}`ULift.up true` if the monadic predicate {name}`p` returns {lean}`ULift.up true` for
422+ any element emitted by the iterator {name}`it`.
423+
424+ {lit}`O(|xs|)`. Short-circuits upon encountering the first match. The elements in {name}`it` are
425+ examined in order of iteration.
426+
427+ This function requires a {name}`Finite` instance proving that the iterator will finish after a
428+ finite number of steps. If the iterator is not finite or such an instance is not available,
429+ consider using {lit}`it.allowNontermination.anyM` instead of {lean}`it.anyM`. However, it is not
430+ possible to formally verify the behavior of the partial variant.
431+ -/
432+ @[specialize]
433+ def IterM.anyM {α β : Type w} {m : Type w → Type w'} [Monad m]
434+ [Iterator α m β] [IteratorLoop α m m] [Finite α m]
435+ (p : β → m (ULift Bool)) (it : IterM (α := α) m β) : m (ULift Bool) :=
436+ ForIn.forIn it (ULift.up false ) (fun x _ => do
437+ if (← p x).down then
438+ return .done (.up true )
439+ else
440+ return .yield (.up false ))
441+
442+ set_option doc.verso true in
443+ /--
444+ Returns {lean}`ULift.up true` if the monadic predicate {name}`p` returns {lean}`ULift.up true` for
445+ any element emitted by the iterator {name}`it`.
446+
447+ {lit}`O(|xs|)`. Short-circuits upon encountering the first match. The elements in {name}`it` are
448+ examined in order of iteration.
449+
450+ This is a partial, potentially nonterminating, function. It is not possible to formally verify
451+ its behavior. If the iterator has a {name}`Finite` instance, consider using {name}`IterM.anyM`
452+ instead.
453+ -/
454+ @[specialize]
455+ def IterM.Partial.anyM {α β : Type w} {m : Type w → Type w'} [Monad m]
456+ [Iterator α m β] [IteratorLoopPartial α m m]
457+ (p : β → m (ULift Bool)) (it : IterM.Partial (α := α) m β) : m (ULift Bool) :=
458+ ForIn.forIn it (ULift.up false ) (fun x _ => do
459+ if (← p x).down then
460+ return .done (.up true )
461+ else
462+ return .yield (.up false ))
463+
464+ set_option doc.verso true in
465+ /--
466+ Returns {lean}`ULift.up true` if the pure predicate {name}`p` returns {lean}`true` for
467+ any element emitted by the iterator {name}`it`.
468+
469+ {lit}`O(|xs|)`. Short-circuits upon encountering the first match. The elements in {name}`it` are
470+ examined in order of iteration.
471+
472+ This function requires a {name}`Finite` instance proving that the iterator will finish after a
473+ finite number of steps. If the iterator is not finite or such an instance is not available,
474+ consider using {lit}`it.allowNontermination.any` instead of {lean}`it.any`. However, it is not
475+ possible to formally verify the behavior of the partial variant.
476+ -/
477+ @[inline]
478+ def IterM.any {α β : Type w} {m : Type w → Type w'} [Monad m]
479+ [Iterator α m β] [IteratorLoop α m m] [Finite α m]
480+ (p : β → Bool) (it : IterM (α := α) m β) : m (ULift Bool) := do
481+ it.anyM (fun x => pure (.up (p x)))
482+
483+ set_option doc.verso true in
484+ /--
485+ Returns {lean}`ULift.up true` if the pure predicate {name}`p` returns {lean}`true` for
486+ any element emitted by the iterator {name}`it`.
487+
488+ {lit}`O(|xs|)`. Short-circuits upon encountering the first match. The elements in {name}`it` are
489+ examined in order of iteration.
490+
491+ This is a partial, potentially nonterminating, function. It is not possible to formally verify
492+ its behavior. If the iterator has a {name}`Finite` instance, consider using {name}`IterM.any`
493+ instead.
494+ -/
495+ @[inline]
496+ def IterM.Partial.any {α β : Type w} {m : Type w → Type w'} [Monad m]
497+ [Iterator α m β] [IteratorLoopPartial α m m]
498+ (p : β → Bool) (it : IterM.Partial (α := α) m β) : m (ULift Bool) := do
499+ it.anyM (fun x => pure (.up (p x)))
500+
501+ set_option doc.verso true in
502+ /--
503+ Returns {lean}`ULift.up true` if the monadic predicate {name}`p` returns {lean}`ULift.up true` for
504+ all elements emitted by the iterator {name}`it`.
505+
506+ {lit}`O(|xs|)`. Short-circuits upon encountering the first mismatch. The elements in {name}`it` are
507+ examined in order of iteration.
508+
509+ This function requires a {name}`Finite` instance proving that the iterator will finish after a
510+ finite number of steps. If the iterator is not finite or such an instance is not available,
511+ consider using {lit}`it.allowNontermination.allM` instead of {lean}`it.allM`. However, it is not
512+ possible to formally verify the behavior of the partial variant.
513+ -/
514+ @[specialize]
515+ def IterM.allM {α β : Type w} {m : Type w → Type w'} [Monad m]
516+ [Iterator α m β] [IteratorLoop α m m] [Finite α m]
517+ (p : β → m (ULift Bool)) (it : IterM (α := α) m β) : m (ULift Bool) := do
518+ ForIn.forIn it (ULift.up true ) (fun x _ => do
519+ if (← p x).down then
520+ return .yield (.up true )
521+ else
522+ return .done (.up false ))
523+ set_option doc.verso true in
524+ /--
525+ Returns {lean}`ULift.up true` if the monadic predicate {name}`p` returns {lean}`ULift.up true` for
526+ all elements emitted by the iterator {name}`it`.
527+
528+ {lit}`O(|xs|)`. Short-circuits upon encountering the first mismatch. The elements in {name}`it` are
529+ examined in order of iteration.
530+
531+ This is a partial, potentially nonterminating, function. It is not possible to formally verify
532+ its behavior. If the iterator has a {name}`Finite` instance, consider using {name}`IterM.allM`
533+ instead.
534+ -/
535+ @[specialize]
536+ def IterM.Partial.allM {α β : Type w} {m : Type w → Type w'} [Monad m]
537+ [Iterator α m β] [IteratorLoopPartial α m m]
538+ (p : β → m (ULift Bool)) (it : IterM.Partial (α := α) m β) : m (ULift Bool) := do
539+ ForIn.forIn it (ULift.up true ) (fun x _ => do
540+ if (← p x).down then
541+ return .yield (.up true )
542+ else
543+ return .done (.up false ))
544+
545+ set_option doc.verso true in
546+ /--
547+ Returns {lean}`ULift.up true` if the pure predicate {name}`p` returns {lean}`true` for
548+ all elements emitted by the iterator {name}`it`.
549+
550+ {lit}`O(|xs|)`. Short-circuits upon encountering the first mismatch. The elements in {name}`it` are
551+ examined in order of iteration.
552+
553+ This function requires a {name}`Finite` instance proving that the iterator will finish after a
554+ finite number of steps. If the iterator is not finite or such an instance is not available,
555+ consider using {lit}`it.allowNontermination.all` instead of {lean}`it.all`. However, it is not
556+ possible to formally verify the behavior of the partial variant.
557+ -/
558+ @[inline]
559+ def IterM.all {α β : Type w} {m : Type w → Type w'} [Monad m]
560+ [Iterator α m β] [IteratorLoop α m m] [Finite α m]
561+ (p : β → Bool) (it : IterM (α := α) m β) : m (ULift Bool) := do
562+ it.allM (fun x => pure (.up (p x)))
563+
564+ set_option doc.verso true in
565+ /--
566+ Returns {lean}`ULift.up true` if the pure predicate {name}`p` returns {lean}`true` for
567+ all elements emitted by the iterator {name}`it`.
568+
569+ {lit}`O(|xs|)`. Short-circuits upon encountering the first mismatch. The elements in {name}`it` are
570+ examined in order of iteration.
571+
572+ This is a partial, potentially nonterminating, function. It is not possible to formally verify
573+ its behavior. If the iterator has a {name}`Finite` instance, consider using {name}`IterM.all`
574+ instead.
575+ -/
576+ @[inline]
577+ def IterM.Partial.all {α β : Type w} {m : Type w → Type w'} [Monad m]
578+ [Iterator α m β] [IteratorLoopPartial α m m]
579+ (p : β → Bool) (it : IterM.Partial (α := α) m β) : m (ULift Bool) := do
580+ it.allM (fun x => pure (.up (p x)))
581+
419582section Size
420583
421584/--
0 commit comments