11import Iterator.Basic
22
3- set_option trace.Elab.Tactic.monotonicity true in
43@[specialize]
54def IterM.DefaultConsumers.forIn {m : Type w → Type w'} {n : Type w → Type w''} [Monad n] [MonadLiftT m n]
6- {α : Type w} {β : Type v} {γ : Type w} [Nonempty (n γ)]
7- [Iterator α m β]
5+ {α : Type w} {β : Type v} {γ : Type w}
6+ [Iterator α m β] [Finite α m]
87 (it : IterM (α := α) m β) (init : γ) {successor_of : β → γ → γ → Prop }
98 (f : (b : β) → (c : γ) → n (Subtype fun s : ForInStep γ => successor_of b c s.value)) : n γ := do
109 match (← it.stepH).inflate with
@@ -14,24 +13,25 @@ def IterM.DefaultConsumers.forIn {m : Type w → Type w'} {n : Type w → Type w
1413 | ⟨.done c, _⟩ => return c
1514 | .skip it' _ => IterM.DefaultConsumers.forIn it' init f
1615 | .done _ => return init
17- partial_fixpoint
16+ termination_by it.finitelyManySteps
1817
1918class IteratorFor (α : Type w) (m : Type w → Type w') {β : Type v} [Iterator α m β] (n : Type w → Type w'') where
2019 forIn : ∀ {γ : Type w}, IterM (α := α) m β → γ → {successor_of : β → γ → γ → Prop } →
2120 ((b : β) → (c : γ) → n (Subtype fun s : ForInStep γ => successor_of b c s.value)) →
22- (IterM.ForInWillTerminate α m successor_of) → n γ
21+ n γ
2322
2423class LawfulIteratorFor (α : Type w) (m : Type w → Type w') (n : Type w → Type w'')
25- [Monad n] [Iterator α m β] [IteratorFor α m n] [MonadLiftT m n] where
24+ [Monad n] [Iterator α m β] [Finite α m] [ IteratorFor α m n] [MonadLiftT m n] where
2625 lawful : ∀ γ, IteratorFor.forIn (α := α) (m := m) (n := n) (γ := γ) =
2726 IterM.DefaultConsumers.forIn (α := α) (m := m) (n := n) (γ := γ)
2827
2928def IteratorFor.defaultImplementation {α : Type w} {m : Type w → Type w'} {n : Type w → Type w'}
30- [Monad m] [Monad n] [MonadLiftT m n] [Iterator α m β] : IteratorFor α m n where
29+ [Monad m] [Monad n] [MonadLiftT m n] [Iterator α m β] [Finite α m] :
30+ IteratorFor α m n where
3131 forIn := IterM.DefaultConsumers.forIn
3232
3333instance (α : Type w) (m : Type w → Type w') (n : Type w → Type w')
34- [Monad m] [Monad n] [MonadLiftT m n] [Iterator α m β] :
34+ [Monad m] [Monad n] [MonadLiftT m n] [Iterator α m β] [Finite α m] :
3535 haveI : IteratorFor α m n := .defaultImplementation
3636 LawfulIteratorFor α m n :=
3737 letI : IteratorFor α m n := .defaultImplementation
@@ -40,21 +40,9 @@ instance (α : Type w) (m : Type w → Type w') (n : Type w → Type w')
4040instance {m : Type w → Type w'} {n : Type w → Type w''}
4141 {α : Type w} {β : Type v} [Iterator α m β] [Finite α m] [IteratorFor α m n] :
4242 ForIn n (IterM (α := α) m β) β where
43- forIn it init stepper := by
44- apply IteratorFor.forIn it init (successor_of := fun _ _ _ => True) (fun b c => (⟨·, True.intro⟩) <$> stepper b c)
45- simp only [IterM.ForInWillTerminate]
46- refine Subrelation.wf (r := InvImage IterM.TerminationMeasures.Finite.rel (fun p => p.1 .finitelyManySteps)) ?_ ?_
47- · intro p' p h
48- cases h
49- · apply IterM.TerminationMeasures.Finite.rel_of_skip
50- rename_i h
51- exact h.1
52- · rename_i h
53- obtain ⟨out, h, _⟩ := h -- Interesting: Moving `obtain` after `apply` leads to failure
54- apply IterM.TerminationMeasures.Finite.rel_of_yield
55- exact h
56- · apply InvImage.wf
57- exact WellFoundedRelation.wf
43+ forIn it init stepper := IteratorFor.forIn it init
44+ (successor_of := fun _ _ _ => True)
45+ (fun b c => (⟨·, True.intro⟩) <$> stepper b c)
5846
5947@[specialize]
6048def IterM.foldM {m : Type w → Type w'} {n : Type w → Type w'} [Monad n]
0 commit comments