Skip to content

Commit ff7c48c

Browse files
committed
Also RevSplitIterator
1 parent 8cc180a commit ff7c48c

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

src/Init/Data/String/Slice.lean

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,34 @@ Examples:
534534
def endsWith [BackwardPattern ρ] (s : Slice) (pat : ρ) : Bool :=
535535
BackwardPattern.endsWith s pat
536536

537-
inductive RevSplitIterator (ρ : Type) [ToBackwardSearcher ρ σ] where
538-
| operating (s : Slice) (currPos : s.Pos) (searcher : Std.Iter (α := σ s) (SearchStep s))
537+
inductive RevSplitIterator (ρ : Type) (s : Slice) [ToBackwardSearcher ρ σ] where
538+
| operating (currPos : s.Pos) (searcher : Std.Iter (α := σ s) (SearchStep s))
539539
| atEnd
540540
deriving Inhabited
541541

542542
namespace RevSplitIterator
543543

544544
variable [ToBackwardSearcher ρ σ]
545545

546-
instance [Pure m] : Std.Iterators.Iterator (RevSplitIterator ρ) m Slice where
547-
IsPlausibleStep := fun _ _ => True
546+
instance [Pure m] : Std.Iterators.Iterator (RevSplitIterator ρ s) m Slice where
547+
IsPlausibleStep it
548+
| .yield it' out =>
549+
match it.internalState, it'.internalState with
550+
| .operating _ searcher, .operating _ searcher' =>
551+
searcher'.finitelyManySteps.Rel searcher.finitelyManySteps
552+
| .operating _ searcher, .atEnd => True
553+
| .atEnd, _ => False
554+
| .skip _ => False
555+
| .done => True
548556
step := fun ⟨iter⟩ =>
549557
match iter with
550-
| .operating s currPos searcher =>
551-
match Internal.nextMatch searcher with
558+
| .operating currPos searcher =>
559+
match h : Internal.nextMatch searcher with
552560
| some (searcher, startPos, endPos) =>
553561
let slice := s.replaceStartEnd! endPos currPos
554-
let nextIt := ⟨.operating s startPos searcher⟩
555-
pure (.deflate ⟨.yield nextIt slice, by simp⟩)
562+
let nextIt := ⟨.operating startPos searcher⟩
563+
pure (.deflate ⟨.yield nextIt slice,
564+
by simpa [nextIt] using Pattern.Internal.finitelyManySteps_rel_nextMatch h⟩)
556565
| none =>
557566
if currPos ≠ s.startPos then
558567
let slice := s.replaceEnd currPos
@@ -561,19 +570,57 @@ instance [Pure m] : Std.Iterators.Iterator (RevSplitIterator ρ) m Slice where
561570
pure (.deflate ⟨.done, by simp⟩)
562571
| .atEnd => pure (.deflate ⟨.done, by simp⟩)
563572

573+
private def toOption : RevSplitIterator ρ s → Option (Std.Iter (α := σ s) (SearchStep s))
574+
| .operating _ s => some s
575+
| .atEnd => none
576+
577+
private instance {s} [Std.Iterators.Finite (σ s) Id] :
578+
WellFoundedRelation (RevSplitIterator ρ s) where
579+
rel := InvImage (Option.lt (InvImage Std.Iterators.IterM.TerminationMeasures.Finite.Rel
580+
Std.Iterators.Iter.finitelyManySteps)) RevSplitIterator.toOption
581+
wf := by
582+
apply InvImage.wf
583+
apply Option.wellFounded_lt
584+
apply InvImage.wf
585+
exact WellFoundedRelation.wf
586+
587+
private def finitenessRelation [Std.Iterators.Finite (σ s) Id] :
588+
Std.Iterators.FinitenessRelation (RevSplitIterator ρ s) Id where
589+
rel := InvImage WellFoundedRelation.rel (fun it => it.internalState)
590+
wf := InvImage.wf _ WellFoundedRelation.wf
591+
subrelation {it it'} h := by
592+
simp_wf
593+
obtain ⟨step, h, h'⟩ := h
594+
match step with
595+
| .yield it'' out =>
596+
obtain rfl : it' = it'' := by simpa [Std.Iterators.IterStep.successor] using h.symm
597+
simp only [Std.Iterators.IterM.IsPlausibleStep, Std.Iterators.Iterator.IsPlausibleStep] at h'
598+
revert h'
599+
match it.internalState, it'.internalState with
600+
| .operating _ searcher, .operating _ searcher' =>
601+
simp [RevSplitIterator.toOption, Option.lt]
602+
| .operating _ searcher, .atEnd =>
603+
simp [RevSplitIterator.toOption, Option.lt]
604+
| .atEnd, _ => simp
605+
606+
@[no_expose]
607+
instance [Std.Iterators.Finite (σ s) Id] :
608+
Std.Iterators.Finite (RevSplitIterator ρ s) Id :=
609+
.of_finitenessRelation finitenessRelation
610+
564611
-- TODO: Finiteness after we have a notion of lawful searcher
565612

566-
instance [Monad m] [Monad n] : Std.Iterators.IteratorCollect (RevSplitIterator ρ) m n :=
613+
instance [Monad m] [Monad n] : Std.Iterators.IteratorCollect (RevSplitIterator ρ s) m n :=
567614
.defaultImplementation
568615

569616
instance [Monad m] [Monad n] :
570-
Std.Iterators.IteratorCollectPartial (RevSplitIterator ρ) m n :=
617+
Std.Iterators.IteratorCollectPartial (RevSplitIterator ρ s) m n :=
571618
.defaultImplementation
572619

573-
instance [Monad m] [Monad n] : Std.Iterators.IteratorLoop (RevSplitIterator ρ) m n :=
620+
instance [Monad m] [Monad n] : Std.Iterators.IteratorLoop (RevSplitIterator ρ s) m n :=
574621
.defaultImplementation
575622

576-
instance [Monad m] [Monad n] : Std.Iterators.IteratorLoopPartial (RevSplitIterator ρ) m n :=
623+
instance [Monad m] [Monad n] : Std.Iterators.IteratorLoopPartial (RevSplitIterator ρ s) m n :=
577624
.defaultImplementation
578625

579626
end RevSplitIterator
@@ -594,8 +641,8 @@ Examples:
594641
-/
595642
@[specialize pat]
596643
def revSplit [ToBackwardSearcher ρ σ] (s : Slice) (pat : ρ) :
597-
Std.Iter (α := RevSplitIterator ρ) Slice :=
598-
{ internalState := .operating s s.endPos (ToBackwardSearcher.toSearcher s pat) }
644+
Std.Iter (α := RevSplitIterator ρ s) Slice :=
645+
{ internalState := .operating s.endPos (ToBackwardSearcher.toSearcher s pat) }
599646

600647
/--
601648
If {name}`pat` matches a suffix of {name}`s`, returns the remainder. Returns {name}`none` otherwise.

0 commit comments

Comments
 (0)