Skip to content

Commit 6198a81

Browse files
committed
Remove inner loop
1 parent ff7c48c commit 6198a81

File tree

5 files changed

+155
-202
lines changed

5 files changed

+155
-202
lines changed

src/Init/Data/Iterators/Basic.lean

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ def IterStep.successor : IterStep α β → Option α
241241
| .skip it => some it
242242
| .done => none
243243

244+
@[simp]
245+
theorem IterStep.successor_yield {it : α} {out : β} :
246+
(IterStep.yield it out).successor = some it := rfl
247+
248+
@[simp]
249+
theorem IterStep.successor_skip {it : α} : (IterStep.skip (β := β) it).successor = some it := rfl
250+
251+
@[simp]
252+
theorem IterStep.successor_done : (IterStep.done (α := α) (β := β)).successor = none := rfl
253+
244254
/--
245255
If present, applies `f` to the iterator of an `IterStep` and replaces the iterator
246256
with the result of the application of `f`.
@@ -543,6 +553,10 @@ def Iter.IsPlausibleSuccessorOf {α : Type w} {β : Type w} [Iterator α Id β]
543553
(it' it : Iter (α := α) β) : Prop :=
544554
it'.toIterM.IsPlausibleSuccessorOf it.toIterM
545555

556+
theorem Iter.isPlausibleSuccessorOf_eq_invImage {α : Type w} {β : Type w} [Iterator α Id β] :
557+
IsPlausibleSuccessorOf (α := α) (β := β) =
558+
InvImage (IterM.IsPlausibleSuccessorOf (α := α) (β := β) (m := Id)) Iter.toIterM := rfl
559+
546560
theorem Iter.isPlausibleSuccessorOf_iff_exists {α : Type w} {β : Type w} [Iterator α Id β]
547561
{it' it : Iter (α := α) β} :
548562
it'.IsPlausibleSuccessorOf it ↔ ∃ step, step.successor = some it' ∧ it.IsPlausibleStep step := by
@@ -555,6 +569,16 @@ theorem Iter.isPlausibleSuccessorOf_iff_exists {α : Type w} {β : Type w} [Iter
555569
exact ⟨step.mapIterator Iter.toIterM,
556570
by cases step <;> simp_all [IterStep.successor, Iter.IsPlausibleStep]⟩
557571

572+
theorem Iter.IsPlausibleStep.isPlausibleSuccessor_of_yield {α : Type w} {β : Type w}
573+
[Iterator α Id β] {it' it : Iter (α := α) β} {out : β}
574+
(h : it.IsPlausibleStep (.yield it' out)) : it'.IsPlausibleSuccessorOf it := by
575+
simpa [isPlausibleSuccessorOf_iff_exists] using ⟨.yield it' out, by simp [h]⟩
576+
577+
theorem Iter.IsPlausibleStep.isPlausibleSuccessor_of_skip {α : Type w} {β : Type w}
578+
[Iterator α Id β] {it' it : Iter (α := α) β} (h : it.IsPlausibleStep (.skip it')) :
579+
it'.IsPlausibleSuccessorOf it := by
580+
simpa [isPlausibleSuccessorOf_iff_exists] using ⟨.skip it', by simp [h]⟩
581+
558582
/--
559583
Asserts that a certain iterator `it` could plausibly yield the value `out` after an arbitrary
560584
number of steps.
@@ -656,6 +680,10 @@ Given this typeclass, termination proofs for well-founded recursion over an iter
656680
class Finite (α : Type w) (m : Type w → Type w') {β : Type w} [Iterator α m β] : Prop where
657681
wf : WellFounded (IterM.IsPlausibleSuccessorOf (α := α) (m := m))
658682

683+
theorem Finite.wf_of_id {α : Type w} {β : Type w} [Iterator α Id β] [Finite α Id] :
684+
WellFounded (Iter.IsPlausibleSuccessorOf (α := α)) := by
685+
simpa [Iter.isPlausibleSuccessorOf_eq_invImage] using InvImage.wf _ Finite.wf
686+
659687
/--
660688
This type is a wrapper around `IterM` so that it becomes a useful termination measure for
661689
recursion over finite iterators. See also `IterM.finitelyManySteps` and `Iter.finitelyManySteps`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ it.filterMapWithPostcondition ---a'-----c'-------⊥
9494
**Termination properties:**
9595
9696
* `Finite` instance: only if `it` is finite
97-
* `Productive` instance: only if `it` is finite`
97+
* `Productive` instance: only if `it` is finite
9898
9999
For certain mapping functions `f`, the resulting iterator will be finite (or productive) even though
100100
no `Finite` (or `Productive`) instance is provided. For example, if `f` never returns `none`, then

src/Init/Data/String/Pattern/Basic.lean

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -99,62 +99,6 @@ where
9999
simp at h ⊢
100100
omega
101101

102-
variable {ρ : Type} {σ : Slice → Type}
103-
variable [∀ s, Std.Iterators.Iterator (σ s) Id (SearchStep s)]
104-
variable [∀ s, Std.Iterators.Finite (σ s) Id]
105-
106-
/--
107-
Tries to skip the {name}`searcher` until the next {name}`SearchStep.matched` and return it. If no
108-
match is found until the end returns {name}`none`.
109-
-/
110-
@[inline]
111-
def nextMatch (searcher : Std.Iter (α := σ s) (SearchStep s)) :
112-
Option (Std.Iter (α := σ s) (SearchStep s) × s.Pos × s.Pos) :=
113-
go searcher
114-
where
115-
go [∀ s, Std.Iterators.Finite (σ s) Id] (searcher : Std.Iter (α := σ s) (SearchStep s)) :
116-
Option (Std.Iter (α := σ s) (SearchStep s) × s.Pos × s.Pos) :=
117-
match searcher.step with
118-
| .yield it (.matched startPos endPos) _ => some (it, startPos, endPos)
119-
| .yield it (.rejected ..) _ | .skip it .. => go it
120-
| .done .. => none
121-
termination_by Std.Iterators.Iter.finitelyManySteps searcher
122-
123-
theorem finitelyManySteps_rel_nextMatch {s : Slice} {searcher : Std.Iter (α := σ s) (SearchStep s)}
124-
{next : Std.Iter (α := σ s) (SearchStep s)} {p q : s.Pos} :
125-
nextMatch searcher = some (next, p, q) →
126-
next.finitelyManySteps.Rel searcher.finitelyManySteps := by
127-
rw [nextMatch]
128-
fun_induction nextMatch.go with
129-
| case1 start middle p' q' hstart hstep =>
130-
simp only [Option.some.injEq, Prod.mk.injEq, and_imp]
131-
rintro rfl rfl rfl
132-
exact Std.Iterators.Iter.TerminationMeasures.Finite.rel_of_yield hstart
133-
| case2 start middle p' q' hstart hstep ih =>
134-
refine (Std.Iterators.IterM.TerminationMeasures.Finite.rel_trans · ?_) ∘ ih
135-
exact Std.Iterators.Iter.TerminationMeasures.Finite.rel_of_yield hstart
136-
| case3 start middle hstart hstep ih =>
137-
refine (Std.Iterators.IterM.TerminationMeasures.Finite.rel_trans · ?_) ∘ ih
138-
exact Std.Iterators.Iter.TerminationMeasures.Finite.rel_of_skip hstart
139-
| case4 => simp_all
140-
141-
/--
142-
Tries to skip the {name}`searcher` until the next {name}`SearchStep.rejected` and return it. If no
143-
reject is found until the end returns {name}`none`.
144-
-/
145-
@[inline]
146-
def nextReject (searcher : Std.Iter (α := σ s) (SearchStep s)) :
147-
Option (Std.Iter (α := σ s) (SearchStep s) × s.Pos × s.Pos) :=
148-
go searcher
149-
where
150-
go [∀ s, Std.Iterators.Finite (σ s) Id] (searcher : Std.Iter (α := σ s) (SearchStep s)) :
151-
Option (Std.Iter (α := σ s) (SearchStep s) × s.Pos × s.Pos) :=
152-
match searcher.step with
153-
| .yield it (.rejected startPos endPos) _ => some (it, startPos, endPos)
154-
| .yield it (.matched ..) _ | .skip it .. => go it
155-
| .done .. => none
156-
termination_by Std.Iterators.Iter.finitelyManySteps searcher
157-
158102
end Internal
159103

160104
namespace ForwardPattern

0 commit comments

Comments
 (0)