Skip to content

Commit 1ff296a

Browse files
committed
corrections
1 parent b04b175 commit 1ff296a

File tree

15 files changed

+91
-313
lines changed

15 files changed

+91
-313
lines changed

src/Init/Data/Iterators/Basic.lean

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -222,36 +222,6 @@ theorem IterStep.mapIterator_id {step : IterStep α β} :
222222
step.mapIterator id = step := by
223223
cases step <;> rfl
224224

225-
/--
226-
A variant of `mapIterator` where the mapping function takes a proof that the
227-
given iterator has been obtained with `IterStep.successor`.
228-
229-
If present, applies `f` to the iterator of an `IterStep` and replaces the iterator
230-
with the result of the application of `f`.
231-
-/
232-
@[always_inline, inline, expose]
233-
def IterStep.pmapIterator {α' : Type u'} (step : IterStep α β)
234-
(f : (it : α) → step.successor = some it → α') : IterStep α' β :=
235-
match step with
236-
| .yield it out => .yield (f it rfl) out
237-
| .skip it => .skip (f it rfl)
238-
| .done => .done
239-
240-
@[simp]
241-
theorem IterStep.pmapIterator_yield {α' : Type u'} {it : α} {out : β} {f : (it : α) → _ → α'} :
242-
(IterStep.yield it out).pmapIterator f = IterStep.yield (f it rfl) out :=
243-
rfl
244-
245-
@[simp]
246-
theorem IterStep.pmapIterator_skip {α' : Type u'} {it : α} {f : (it : α) → _ → α'} :
247-
(IterStep.skip it (β := β)).pmapIterator f = IterStep.skip (f it rfl) :=
248-
rfl
249-
250-
@[simp]
251-
theorem IterStep.pmapIterator_done {α' : Type u'} {f : (it : α) → _ → α'} :
252-
(IterStep.done (α := α) (β := β)).pmapIterator f = IterStep.done :=
253-
rfl
254-
255225
/--
256226
A variant of `IterStep` that bundles the step together with a proof that it is "plausible".
257227
The plausibility predicate will later be chosen to assert that a state is a plausible successor
@@ -370,45 +340,6 @@ def IterM.IsPlausibleSuccessorOf {α : Type w} {m : Type w → Type w'} {β : Ty
370340
(it' it : IterM (α := α) m β) : Prop :=
371341
∃ step, step.successor = some it' ∧ it.IsPlausibleStep step
372342

373-
inductive IterM.IsPlausibleIndirectOutput {α β : Type w} {m : Type w → Type w'} [Iterator α m β]
374-
: IterM (α := α) m β → β → Prop where
375-
| direct {it : IterM (α := α) m β} {out : β} : it.IsPlausibleOutput out →
376-
it.IsPlausibleIndirectOutput out
377-
| indirect {it it' : IterM (α := α) m β} {out : β} : it'.IsPlausibleSuccessorOf it →
378-
it'.IsPlausibleIndirectOutput out → it.IsPlausibleIndirectOutput out
379-
380-
-- TODO: remove if unused
381-
def IterM.IsInLineageOf {α : Type w} {m : Type w → Type w'} {β : Type w} [Iterator α m β]
382-
(it' it : IterM (α := α) m β) : Prop :=
383-
it' = it ∨ Relation.TransGen IterM.IsPlausibleSuccessorOf it' it
384-
385-
protected theorem IterM.IsInLineageOf.rfl {α : Type w} {m : Type w → Type w'} {β : Type w}
386-
[Iterator α m β] {it : IterM (α := α) m β} :
387-
it.IsInLineageOf it :=
388-
.inl rfl
389-
390-
protected theorem IterM.IsInLineageOf.single {α : Type w} {m : Type w → Type w'} {β : Type w}
391-
[Iterator α m β] {it' it : IterM (α := α) m β}
392-
(h : it'.IsPlausibleSuccessorOf it) :
393-
it'.IsInLineageOf it :=
394-
.inr <| .single h
395-
396-
protected theorem IterM.IsInLineageOf.trans {α : Type w} {m : Type w → Type w'} {β : Type w}
397-
[Iterator α m β] {it'' it' it : IterM (α := α) m β}
398-
(h' : it''.IsInLineageOf it') (h : it'.IsInLineageOf it) :
399-
it''.IsInLineageOf it := by
400-
cases h
401-
· rename_i h
402-
cases h
403-
exact h'
404-
· rename_i h
405-
cases h'
406-
· rename_i h'
407-
cases h'
408-
exact .inr h
409-
· rename_i h'
410-
exact .inr <| .trans h' h
411-
412343
/--
413344
Asserts that a certain iterator `it'` could plausibly be the directly succeeding iterator of another
414345
given iterator `it` while no value is emitted (see `IterStep.skip`).
@@ -498,34 +429,6 @@ def Iter.IsPlausibleSuccessorOf {α : Type w} {β : Type w} [Iterator α Id β]
498429
(it' it : Iter (α := α) β) : Prop :=
499430
it'.toIterM.IsPlausibleSuccessorOf it.toIterM
500431

501-
inductive Iter.IsPlausibleIndirectOutput {α β : Type w} [Iterator α Id β] :
502-
Iter (α := α) β → β → Prop where
503-
| direct {it : Iter (α := α) β} {out : β} : it.IsPlausibleOutput out →
504-
it.IsPlausibleIndirectOutput out
505-
| indirect {it it' : Iter (α := α) β} {out : β} : it'.IsPlausibleSuccessorOf it →
506-
it'.IsPlausibleIndirectOutput out → it.IsPlausibleIndirectOutput out
507-
508-
theorem Iter.isPlausibleIndirectOutput_iff_isPlausibleIndirectOutput_toIterM {α β : Type w}
509-
[Iterator α Id β] {it : Iter (α := α) β} {out : β} :
510-
it.IsPlausibleIndirectOutput out ↔ it.toIterM.IsPlausibleIndirectOutput out := by
511-
constructor
512-
· intro h
513-
induction h with
514-
| direct h =>
515-
exact .direct h
516-
| indirect h _ ih =>
517-
exact .indirect h ih
518-
· intro h
519-
rw [← Iter.toIter_toIterM (it := it)]
520-
generalize it.toIterM = it at ⊢ h
521-
induction h with
522-
| direct h =>
523-
exact .direct h
524-
| indirect h h' ih =>
525-
rename_i it it' out
526-
replace h : it'.toIter.IsPlausibleSuccessorOf it.toIter := h
527-
exact .indirect (α := α) h ih
528-
529432
/--
530433
Asserts that a certain iterator `it'` could plausibly be the directly succeeding iterator of another
531434
given iterator `it` while no value is emitted (see `IterStep.skip`).

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ namespace Std.Iterators
2626

2727
instance (α : Type w) (β : Type w) (n : Type w → Type w') [Monad n]
2828
[Iterator α Id β] [Finite α Id] [IteratorLoop α Id n] :
29-
ForIn' n (Iter (α := α) β) β ⟨fun it out => it.IsPlausibleIndirectOutput out⟩ where
30-
forIn' it init f :=
31-
IteratorLoop.finiteForIn' (fun δ (c : Id δ) => pure c.run) |>.forIn' it.toIterM init
32-
fun out h acc =>
33-
f out (Iter.isPlausibleIndirectOutput_iff_isPlausibleIndirectOutput_toIterM.mpr h) acc
29+
ForIn n (Iter (α := α) β) β where
30+
forIn it init f :=
31+
IteratorLoop.finiteForIn (fun δ (c : Id δ) => pure c.run) |>.forIn it.toIterM init f
3432

3533
instance (α : Type w) (β : Type w) (n : Type w → Type w') [Monad n]
3634
[Iterator α Id β] [IteratorLoopPartial α Id n] :
@@ -115,14 +113,4 @@ def Iter.Partial.fold {α : Type w} {β : Type w} {γ : Type w} [Iterator α Id
115113
(init : γ) (it : Iter.Partial (α := α) β) : γ :=
116114
ForIn.forIn (m := Id) it init (fun x acc => ForInStep.yield (f acc x))
117115

118-
@[always_inline, inline, inherit_doc IterM.size]
119-
def Iter.size {α : Type w} {β : Type w} [Iterator α Id β] [IteratorSize α Id]
120-
(it : Iter (α := α) β) : Nat :=
121-
(IteratorSize.size it.toIterM).run.down
122-
123-
@[always_inline, inline, inherit_doc IterM.Partial.size]
124-
def Iter.Partial.size {α : Type w} {β : Type w} [Iterator α Id β] [IteratorSizePartial α Id]
125-
(it : Iter (α := α) β) : Nat :=
126-
(IteratorSizePartial.size it.toIterM).run.down
127-
128116
end Std.Iterators

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

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class IteratorLoop (α : Type w) (m : Type w → Type w') {β : Type w} [Iterato
6565
(plausible_forInStep : β → γ → ForInStep γ → Prop) →
6666
IteratorLoop.WellFounded α m plausible_forInStep →
6767
(it : IterM (α := α) m β) → γ →
68-
((b : β) → it.IsPlausibleIndirectOutput b → (c : γ) → n (Subtype (plausible_forInStep b c))) →
68+
((b : β) → (c : γ) → n (Subtype (plausible_forInStep b c))) →
6969
n γ
7070

7171
/--
@@ -80,13 +80,7 @@ class IteratorLoopPartial (α : Type w) (m : Type w → Type w') {β : Type w} [
8080
(n : Type w → Type w'') where
8181
forInPartial : ∀ (_lift : (γ : Type w) → m γ → n γ) {γ : Type w},
8282
(it : IterM (α := α) m β) → γ →
83-
((b : β) → it.IsPlausibleIndirectOutput b → (c : γ) → n (ForInStep γ)) → n γ
84-
85-
class IteratorSize (α : Type w) (m : Type w → Type w') {β : Type w} [Iterator α m β] where
86-
size : IterM (α := α) m β → m (ULift Nat)
87-
88-
class IteratorSizePartial (α : Type w) (m : Type w → Type w') {β : Type w} [Iterator α m β] where
89-
size : IterM (α := α) m β → m (ULift Nat)
83+
((b : β) → (c : γ) → n (ForInStep γ)) → n γ
9084

9185
end Typeclasses
9286

@@ -119,20 +113,20 @@ def IterM.DefaultConsumers.forIn {m : Type w → Type w'} {α : Type w} {β : Ty
119113
(plausible_forInStep : β → γ → ForInStep γ → Prop)
120114
(wf : IteratorLoop.WellFounded α m plausible_forInStep)
121115
(it : IterM (α := α) m β) (init : γ)
122-
(f : (b : β) → it.IsPlausibleIndirectOutput b → (c : γ) → n (Subtype (plausible_forInStep b c))) : n γ :=
116+
(f : (b : β) → (c : γ) → n (Subtype (plausible_forInStep b c))) : n γ :=
123117
haveI : WellFounded _ := wf
124118
letI : MonadLift m n := ⟨fun {γ} => lift γ⟩
125119
do
126120
match ← it.step with
127-
| .yield it' out h =>
128-
match ← f out (.direct ⟨_, h⟩) init with
121+
| .yield it' out _ =>
122+
match ← f out init with
129123
| ⟨.yield c, _⟩ =>
130124
IterM.DefaultConsumers.forIn lift _ plausible_forInStep wf it' c
131-
(fun out h' acc => f out (.indirect ⟨_, rfl, h⟩ h') acc)
125+
(fun out acc => f out acc)
132126
| ⟨.done c, _⟩ => return c
133-
| .skip it' h =>
127+
| .skip it' _ =>
134128
IterM.DefaultConsumers.forIn lift _ plausible_forInStep wf it' init
135-
(fun out h' acc => f out (.indirect ⟨_, rfl, h⟩ h') acc)
129+
(fun out acc => f out acc)
136130
| .done _ => return init
137131
termination_by IteratorLoop.WFRel.mk wf it init
138132
decreasing_by
@@ -167,19 +161,19 @@ partial def IterM.DefaultConsumers.forInPartial {m : Type w → Type w'} {α : T
167161
{n : Type w → Type w''} [Monad n]
168162
(lift : ∀ γ, m γ → n γ) (γ : Type w)
169163
(it : IterM (α := α) m β) (init : γ)
170-
(f : (b : β) → it.IsPlausibleIndirectOutput b → (c : γ) → n (ForInStep γ)) : n γ :=
164+
(f : (b : β) → (c : γ) → n (ForInStep γ)) : n γ :=
171165
letI : MonadLift m n := ⟨fun {γ} => lift γ⟩
172166
do
173167
match ← it.step with
174-
| .yield it' out h =>
175-
match ← f out (.direct ⟨_, h⟩) init with
168+
| .yield it' out _ =>
169+
match ← f out init with
176170
| .yield c =>
177171
IterM.DefaultConsumers.forInPartial lift _ it' c
178-
fun out h' acc => f out (.indirect ⟨_, rfl, h⟩ h') acc
172+
fun out acc => f out acc
179173
| .done c => return c
180-
| .skip it' h =>
174+
| .skip it' _ =>
181175
IterM.DefaultConsumers.forInPartial lift _ it' init
182-
fun out h' acc => f out (.indirect ⟨_, rfl, h⟩ h') acc
176+
fun out acc => f out acc
183177
| .done _ => return init
184178

185179
/--
@@ -214,28 +208,28 @@ theorem IteratorLoop.wellFounded_of_finite {m : Type w → Type w'}
214208
exact WellFoundedRelation.wf
215209

216210
/--
217-
This `ForIn'`-style loop construct traverses a finite iterator using an `IteratorLoop` instance.
211+
This `ForIn`-style loop construct traverses a finite iterator using an `IteratorLoop` instance.
218212
-/
219213
@[always_inline, inline]
220-
def IteratorLoop.finiteForIn' {m : Type w → Type w'} {n : Type w → Type w''}
214+
def IteratorLoop.finiteForIn {m : Type w → Type w'} {n : Type w → Type w''}
221215
{α : Type w} {β : Type w} [Iterator α m β] [Finite α m] [IteratorLoop α m n]
222216
(lift : ∀ γ, m γ → n γ) :
223-
ForIn' n (IterM (α := α) m β) βfun it out => it.IsPlausibleIndirectOutput out⟩ where
224-
forIn' {γ} [Monad n] it init f :=
217+
ForIn n (IterM (α := α) m β) β where
218+
forIn {γ} [Monad n] it init f :=
225219
IteratorLoop.forIn (α := α) (m := m) lift γ (fun _ _ _ => True)
226220
wellFounded_of_finite
227-
it init (fun out h acc => (⟨·, .intro⟩) <$> f out h acc)
221+
it init (fun out acc => (⟨·, .intro⟩) <$> f out acc)
228222

229223
instance {m : Type w → Type w'} {n : Type w → Type w''}
230224
{α : Type w} {β : Type w} [Iterator α m β] [Finite α m] [IteratorLoop α m n]
231225
[MonadLiftT m n] :
232-
ForIn' n (IterM (α := α) m β) βfun it out => it.IsPlausibleIndirectOutput out⟩ :=
233-
IteratorLoop.finiteForIn' (fun _ => monadLift)
226+
ForIn n (IterM (α := α) m β) β :=
227+
IteratorLoop.finiteForIn (fun _ => monadLift)
234228

235229
instance {m : Type w → Type w'} {n : Type w → Type w''}
236230
{α : Type w} {β : Type w} [Iterator α m β] [IteratorLoopPartial α m n] [MonadLiftT m n] :
237-
ForIn' n (IterM.Partial (α := α) m β) βfun it out => it.it.IsPlausibleIndirectOutput out⟩ where
238-
forIn' it init f :=
231+
ForIn n (IterM.Partial (α := α) m β) β where
232+
forIn it init f :=
239233
IteratorLoopPartial.forInPartial (α := α) (m := m) (fun _ => monadLift) it.it init f
240234

241235
instance {m : Type w → Type w'} {n : Type w → Type w''}
@@ -346,42 +340,4 @@ def IterM.Partial.drain {α : Type w} {m : Type w → Type w'} [Monad m] {β : T
346340
m PUnit :=
347341
it.fold (γ := PUnit) (fun _ _ => .unit) .unit
348342

349-
section Size
350-
351-
@[always_inline, inline]
352-
def IterM.DefaultConsumers.size {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w}
353-
[Iterator α m β] [IteratorLoop α m m] [Finite α m] (it : IterM (α := α) m β) :
354-
m (ULift Nat) :=
355-
it.fold (init := .up 0) fun acc _ => .up (acc.down + 1)
356-
357-
@[always_inline, inline]
358-
def IterM.DefaultConsumers.sizePartial {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w}
359-
[Iterator α m β] [IteratorLoopPartial α m m] (it : IterM (α := α) m β) :
360-
m (ULift Nat) :=
361-
it.allowNontermination.fold (init := .up 0) fun acc _ => .up (acc.down + 1)
362-
363-
@[always_inline, inline]
364-
instance IteratorSize.defaultImplementation {α β : Type w} {m : Type w → Type w'} [Monad m]
365-
[Iterator α m β] [Finite α m] [IteratorLoop α m m] :
366-
IteratorSize α m where
367-
size := IterM.DefaultConsumers.size
368-
369-
@[always_inline, inline]
370-
instance IteratorSize.Partial.defaultImplementation {α β : Type w} {m : Type w → Type w'} [Monad m]
371-
[Iterator α m β] [Finite α m] [IteratorLoopPartial α m m] :
372-
IteratorSizePartial α m where
373-
size := IterM.DefaultConsumers.sizePartial
374-
375-
@[always_inline, inline]
376-
def IterM.size {α : Type} {m : TypeType w'} {β : Type} [Iterator α m β] [Monad m]
377-
(it : IterM (α := α) m β) [IteratorSize α m] : m Nat :=
378-
ULift.down <$> IteratorSize.size it
379-
380-
@[always_inline, inline]
381-
def IterM.Partial.size {α : Type} {m : TypeType w'} {β : Type} [Iterator α m β] [Monad m]
382-
(it : IterM.Partial (α := α) m β) [IteratorSizePartial α m] : m Nat :=
383-
ULift.down <$> IteratorSizePartial.size it.it
384-
385-
end Size
386-
387343
end Std.Iterators

src/Init/Data/Iterators/Lemmas/Consumers/Collect.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Authors: Paul Reichert
66
module
77

88
prelude
9-
import all Init.Data.Iterators.Consumers.Access
10-
import all Init.Data.Iterators.Consumers.Collect
119
import Init.Data.Iterators.Lemmas.Basic
1210
import Init.Data.Iterators.Lemmas.Consumers.Monadic.Collect
11+
import all Init.Data.Iterators.Consumers.Access
12+
import all Init.Data.Iterators.Consumers.Collect
1313

1414
namespace Std.Iterators
1515

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,21 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Paul Reichert
55
-/
66
prelude
7-
import Init.Data.List.Control
8-
import Init.Data.Iterators.Lemmas.Basic
97
import Init.Data.Iterators.Lemmas.Consumers.Collect
108
import Init.Data.Iterators.Lemmas.Consumers.Monadic.Loop
11-
import Init.Data.Iterators.Consumers.Collect
129
import Init.Data.Iterators.Consumers.Loop
1310

1411
namespace Std.Iterators
1512

16-
theorem Iter.forIn'_eq {α β : Type w} [Iterator α Id β] [Finite α Id]
17-
{m : Type w → Type w''} [Monad m] [IteratorLoop α Id m] [hl : LawfulIteratorLoop α Id m]
18-
{γ : Type w} {it : Iter (α := α) β} {init : γ}
19-
{f : (b : β) → it.IsPlausibleIndirectOutput b → γ → m (ForInStep γ)} :
20-
ForIn'.forIn' it init f =
21-
IterM.DefaultConsumers.forIn (fun _ c => pure c.run) γ (fun _ _ _ => True)
22-
IteratorLoop.wellFounded_of_finite it.toIterM init
23-
(fun out h acc => (⟨·, .intro⟩) <$>
24-
f out (Iter.isPlausibleIndirectOutput_iff_isPlausibleIndirectOutput_toIterM.mpr h) acc) := by
25-
cases hl.lawful; rfl
26-
2713
theorem Iter.forIn_eq {α β : Type w} [Iterator α Id β] [Finite α Id]
2814
{m : Type w → Type w''} [Monad m] [IteratorLoop α Id m] [hl : LawfulIteratorLoop α Id m]
2915
{γ : Type w} {it : Iter (α := α) β} {init : γ}
30-
{f : β → γ → m (ForInStep γ)} :
16+
{f : (b : β) → γ → m (ForInStep γ)} :
3117
ForIn.forIn it init f =
3218
IterM.DefaultConsumers.forIn (fun _ c => pure c.run) γ (fun _ _ _ => True)
33-
IteratorLoop.wellFounded_of_finite it.toIterM init (fun out _ acc => (⟨·, .intro⟩) <$> f out acc) := by
19+
IteratorLoop.wellFounded_of_finite it.toIterM init
20+
(fun out acc => (⟨·, .intro⟩) <$>
21+
f out acc) := by
3422
cases hl.lawful; rfl
3523

3624
theorem Iter.forIn_eq_forIn_toIterM {α β : Type w} [Iterator α Id β]

src/Init/Data/Iterators/Lemmas/Consumers/Monadic/Collect.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module
77

88
prelude
99
import Init.Data.Array.Lemmas
10-
import all Init.Data.Iterators.Consumers.Monadic.Collect
1110
import Init.Data.Iterators.Lemmas.Monadic.Basic
11+
import all Init.Data.Iterators.Consumers.Monadic.Collect
1212

1313
namespace Std.Iterators
1414

0 commit comments

Comments
 (0)