@@ -10,7 +10,6 @@ public import Init.Data.Iterators.Consumers.Monadic.Partial
1010public import Init.Data.Iterators.Consumers.Monadic.Total
1111public import Init.Data.Iterators.Internal.LawfulMonadLiftFunction
1212public import Init.Internal.ExtrinsicTermination
13- public import Init.Internal.MonadAttach
1413
1514@[expose] public section
1615
@@ -61,6 +60,11 @@ end Typeclasses
6160
6261section ToArray
6362
63+ def IterM.DefaultConsumers.toArrayMapped.RecursionRel {α β : Type w} {m : Type w → Type w'}
64+ [Iterator α m β] {γ : Type w} (x' x : (_ : IterM (α := α) m β) ×' Array γ) : Prop :=
65+ (∃ out, x.1 .IsPlausibleStep (.yield x'.1 out) ∧ ∃ fx, x'.2 = x.2 .push fx) ∨
66+ (x.1 .IsPlausibleStep (.skip x'.1 ) ∧ x'.2 = x.2 )
67+
6468/--
6569This is an internal function used in `IteratorCollect.defaultImplementation`.
6670
@@ -69,7 +73,7 @@ of `f` into an array.
6973-/
7074@[always_inline, no_expose]
7175def IterM.DefaultConsumers.toArrayMapped {α β : Type w} {m : Type w → Type w'}
72- {n : Type w → Type w''} [Monad n] [MonadAttach n] [ Iterator α m β]
76+ {n : Type w → Type w''} [Monad n] [Iterator α m β]
7377 (lift : ⦃α : Type w⦄ → m α → n α) {γ : Type w} (f : β → n γ)
7478 (it : IterM (α := α) m β) : n (Array γ) :=
7579 letI : MonadLift m n := ⟨lift (α := _)⟩
@@ -78,24 +82,43 @@ where
7882 @[always_inline]
7983 go it (acc : Array γ) : n (Array γ) :=
8084 letI : MonadLift m n := ⟨lift (α := _)⟩
81- extrinsicFix₂ (C₂ := fun _ _ => n (Array γ))
82- (fun x' x => (∃ out h, MonadAttach.CanReturn (m := n) x.1 .step (.deflate <| .yield x'.1 out h) ∧ ∃ fx, MonadAttach.CanReturn (f out) fx ∧ x'.2 = x.2 .push fx) ∨ (∃ h, MonadAttach.CanReturn (m := n) x.1 .step (.deflate <| .skip x'.1 h) ∧ x'.2 = x.2 ))
85+ extrinsicFixE₂ (C₂ := fun _ _ => n (Array γ)) (InvImage TerminationMeasures.Finite.Rel (·.1 .finitelyManySteps!))
8386 (fun (it : IterM (α := α) m β) acc recur => do
84- let ⟨step, hs⟩ ← MonadAttach.attach (m := n) it.step
85- match hs' : step.inflate with
87+ match (← it.step).inflate with
8688 | .yield it' out h =>
87- let fx ← MonadAttach.attach (f out)
88- recur it' (acc.push fx.val) (by
89- apply Or.inl
90- have : step = .deflate (.yield it' out h) := by simp [← hs']
91- rw [this] at hs
92- exact ⟨out, h, hs, fx.val, fx.property, rfl⟩)
93- | .skip it' h => recur it' acc (by
94- apply Or.inr
95- have : step = .deflate (.skip it' h) := by simp [← hs']
96- rw [this] at hs
97- exact ⟨h, hs, rfl⟩)
98- | .done h => return acc) it acc
89+ recur it' (acc.push (← f out)) (by exact TerminationMeasures.Finite.rel_of_yield ‹_›)
90+ | .skip it' h => recur it' acc (by exact TerminationMeasures.Finite.rel_of_skip ‹_›)
91+ | .done _ => return acc) it acc
92+
93+ -- @[always_inline, no_expose]
94+ -- def IterM.DefaultConsumers.toArrayMapped {α β : Type w} {m : Type w → Type w'}
95+ -- {n : Type w → Type w''} [Monad n] [MonadAttach n] [Iterator α m β]
96+ -- (lift : ⦃α : Type w⦄ → m α → n α) {γ : Type w} (f : β → n γ)
97+ -- (it : IterM (α := α) m β) : n (Array γ) :=
98+ -- letI : MonadLift m n := ⟨lift (α := _)⟩
99+ -- go it #[]
100+ -- where
101+ -- @[ always_inline ]
102+ -- go it (acc : Array γ) : n (Array γ) :=
103+ -- letI : MonadLift m n := ⟨lift (α := _)⟩
104+ -- extrinsicFixE₂ (C₂ := fun _ _ => n (Array γ))
105+ -- (fun x' x => (∃ out, x.1.IsPlausibleStepE (.yield x'.1 out) ∧ ∃ fx, MonadAttach.CanReturn (f out) fx ∧ x'.2 = x.2.push fx) ∨ (∃ h, MonadAttach.CanReturn (m := n) x.1.step (.deflate <| .skip x'.1 h) ∧ x'.2 = x.2))
106+ -- (fun (it : IterM (α := α) m β) acc recur => do
107+ -- let ⟨step, hs⟩ ← MonadAttach.attach (m := n) it.step
108+ -- match hs' : step.inflate with
109+ -- | .yield it' out h =>
110+ -- let fx ← MonadAttach.attach (f out)
111+ -- recur it' (acc.push fx.val) (by
112+ -- apply Or.inl
113+ -- have : step = .deflate (.yield it' out h) := by simp [← hs']
114+ -- rw [ this ] at hs
115+ -- exact ⟨out, h, hs, fx.val, fx.property, rfl⟩)
116+ -- | .skip it' h => recur it' acc (by
117+ -- apply Or.inr
118+ -- have : step = .deflate (.skip it' h) := by simp [← hs']
119+ -- rw [ this ] at hs
120+ -- exact ⟨h, hs, rfl⟩)
121+ -- | .done h => return acc) it acc
99122
100123/--
101124This is the default implementation of the `IteratorCollect` class.
@@ -105,7 +128,7 @@ used instead.
105128-/
106129@[always_inline]
107130def IteratorCollect.defaultImplementation {α β : Type w} {m : Type w → Type w'}
108- {n : Type w → Type w''} [Monad n] [MonadAttach n] [ Iterator α m β] :
131+ {n : Type w → Type w''} [Monad n] [Iterator α m β] :
109132 IteratorCollect α m n where
110133 toArrayMapped := IterM.DefaultConsumers.toArrayMapped
111134
@@ -115,21 +138,21 @@ Asserts that a given `IteratorCollect` instance is equal to `IteratorCollect.def
115138(Even though equal, the given instance might be vastly more efficient.)
116139-/
117140class LawfulIteratorCollect (α : Type w) (m : Type w → Type w') (n : Type w → Type w'')
118- {β : Type w} [Monad m] [Monad n] [MonadAttach n] [ Iterator α m β] [i : IteratorCollect α m n] where
141+ {β : Type w} [Monad m] [Monad n] [Iterator α m β] [i : IteratorCollect α m n] where
119142 lawful_toArrayMapped : ∀ lift [LawfulMonadLiftFunction lift] [Finite α m],
120143 i.toArrayMapped lift (α := α) (γ := γ)
121144 = IteratorCollect.defaultImplementation.toArrayMapped lift
122145
123146theorem LawfulIteratorCollect.toArrayMapped_eq {α β γ : Type w} {m : Type w → Type w'}
124- {n : Type w → Type w''} [Monad m] [Monad n] [MonadAttach n] [ Iterator α m β] [Finite α m] [IteratorCollect α m n]
147+ {n : Type w → Type w''} [Monad m] [Monad n] [Iterator α m β] [Finite α m] [IteratorCollect α m n]
125148 [hl : LawfulIteratorCollect α m n] {lift : ⦃δ : Type w⦄ → m δ → n δ}
126149 [LawfulMonadLiftFunction lift]
127150 {f : β → n γ} {it : IterM (α := α) m β} :
128151 IteratorCollect.toArrayMapped lift f it (m := m) =
129152 IterM.DefaultConsumers.toArrayMapped lift f it (m := m) := by
130153 rw [lawful_toArrayMapped]; rfl
131154
132- instance (α β : Type w) (m : Type w → Type w') (n : Type w → Type w'') [Monad n] [MonadAttach n]
155+ instance (α β : Type w) (m : Type w → Type w') (n : Type w → Type w'') [Monad n]
133156 [Iterator α m β] [Monad m] [Iterator α m β] [Finite α m] :
134157 haveI : IteratorCollect α m n := .defaultImplementation
135158 LawfulIteratorCollect α m n :=
@@ -185,11 +208,12 @@ def IterM.toListRev {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type
185208where
186209 @[always_inline, inline]
187210 go (it : IterM m β) acc :=
188- extrinsicFix₂ (fun it acc recur => do
189- match (← it.step).inflate with
190- | .yield it' out _ => recur it' (out :: acc)
191- | .skip it' _ => recur it' acc
192- | .done _ => return acc) it acc
211+ extrinsicFixE₂ (InvImage TerminationMeasures.Finite.Rel (·.1 .finitelyManySteps!))
212+ (fun it acc recur => do
213+ match (← it.step).inflate with
214+ | .yield it' out h => recur it' (out :: acc) (TerminationMeasures.Finite.rel_of_yield h)
215+ | .skip it' h => recur it' acc (TerminationMeasures.Finite.rel_of_skip h)
216+ | .done _ => return acc) it acc
193217
194218/--
195219Traverses the given iterator and stores the emitted values in reverse order in a list. Because
0 commit comments