-- Mathlib/.lake/packages/lean4/src/lean/Init/Data/List/Monadic.lean
theorem forIn'_eq_foldlM [Monad m] [LawfulMonad m]
    (l : List α) (f : (a : α) → a ∈ l → β → m (ForInStep β)) (init : β) :
    forIn' l init f = ForInStep.value <$>
      l.attach.foldlM (fun b ⟨a, m⟩ => match b with
        | .yield b => f a m b
        | .done b => pure (.done b)) (ForInStep.yield init)

Proof state:
case cons
m : Type u_1 → Type u_2
α : Type u_3
β : Type u_1
inst✝¹ : Monad m
inst✝ : LawfulMonad m
a✝ : α
init : β
x : ForInStep β
b : β
a : α
as : List α
ih :
  ∀ (f : (a : α) → a ∈ a✝ :: as → β → m (ForInStep β)),
    pure b =
      ForInStep.value <$>
        List.foldlM
          (fun b x =>
            match b with
            | ForInStep.yield b => f x.val ⋯ b
            | ForInStep.done b => pure (ForInStep.done b))
          (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) as.attach)
f : (a_1 : α) → a_1 ∈ a✝ :: a :: as → β → m (ForInStep β)
⊢ pure b =
    ForInStep.value <$>
      List.foldlM
        (fun b x =>
          match b with
          | ForInStep.yield b => f x.val ⋯ b
          | ForInStep.done b => pure (ForInStep.done b))
        (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) (a :: as).attach)

Tactic:
simp only [attach_cons, map_cons, map_map, Function.comp_def, foldlM_cons, pure_bind]

Result:
case cons
m : Type u_1 → Type u_2
α : Type u_3
β : Type u_1
inst✝¹ : Monad m
inst✝ : LawfulMonad m
a✝ : α
init : β
x : ForInStep β
b : β
a : α
as : List α
ih :
  ∀ (f : (a : α) → a ∈ a✝ :: as → β → m (ForInStep β)),
    pure b =
      ForInStep.value <$>
        List.foldlM
          (fun b x =>
            match b with
            | ForInStep.yield b => f x.val ⋯ b
            | ForInStep.done b => pure (ForInStep.done b))
          (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) as.attach)
f : (a_1 : α) → a_1 ∈ a✝ :: a :: as → β → m (ForInStep β)
⊢ pure b =
    ForInStep.value <$>
      List.foldlM
        (fun b x =>
          match b with
          | ForInStep.yield b => f x.val ⋯ b
          | ForInStep.done b => pure (ForInStep.done b))
        (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) as.attach)

Proof state:
case nil
m : Type u_1 → Type u_2
α : Type u_3
β : Type u_1
inst✝¹ : Monad m
inst✝ : LawfulMonad m
f : (a : α) → a ∈ [] → β → m (ForInStep β)
init : β
⊢ forIn' [] init f =
    ForInStep.value <$>
      List.foldlM
        (fun b x =>
          match x with
          | ⟨a, m_1⟩ =>
            match b with
            | ForInStep.yield b => f a m_1 b
            | ForInStep.done b => pure (ForInStep.done b))
        (ForInStep.yield init) [].attach

Tactic:
simp

Result:
no goals

Proof state:
case cons
m : Type u_1 → Type u_2
α : Type u_3
β : Type u_1
inst✝¹ : Monad m
inst✝ : LawfulMonad m
a✝ : α
init : β
x : ForInStep β
b : β
a : α
as : List α
ih✝ :
  ∀ (f : (a : α) → a ∈ a✝ :: as → β → m (ForInStep β)),
    pure b =
      ForInStep.value <$>
        List.foldlM
          (fun b x =>
            match b with
            | ForInStep.yield b => f x.val ⋯ b
            | ForInStep.done b => pure (ForInStep.done b))
          (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) as.attach)
f : (a_1 : α) → a_1 ∈ a✝ :: a :: as → β → m (ForInStep β)
ih :
  pure b =
    ForInStep.value <$>
      List.foldlM
        (fun b_1 x =>
          match b_1 with
          | ForInStep.yield b_2 => f x.val ⋯ b_2
          | ForInStep.done b => pure (ForInStep.done b))
        (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) as.attach)
⊢ pure b =
    ForInStep.value <$>
      List.foldlM
        (fun b x =>
          match b with
          | ForInStep.yield b => f x.val ⋯ b
          | ForInStep.done b => pure (ForInStep.done b))
        (ForInStep.done b) (map (fun x => ⟨x.val, ⋯⟩) as.attach)

Tactic:
simp [ih, List.foldlM_map]

Result:
no goals

Proof state:
case cons
m : Type u_1 → Type u_2
α : Type u_3
β : Type u_1
inst✝¹ : Monad m
inst✝ : LawfulMonad m
a : α
as : List α
ih :
  ∀ (f : (a : α) → a ∈ as → β → m (ForInStep β)) (init : β),
    forIn' as init f =
      ForInStep.value <$>
        List.foldlM
          (fun b x =>
            match x with
            | ⟨a, m_1⟩ =>
              match b with
              | ForInStep.yield b => f a m_1 b
              | ForInStep.done b => pure (ForInStep.done b))
          (ForInStep.yield init) as.attach
f : (a_1 : α) → a_1 ∈ a :: as → β → m (ForInStep β)
init : β
⊢ (do
      let x ← f a ⋯ init
      match x with
        | ForInStep.done b => pure b
        | ForInStep.yield b => forIn' as b fun a' m b => f a' ⋯ b) =
    do
    let a_1 ← f a ⋯ init
    ForInStep.value <$>
        List.foldlM
          (fun b x =>
            match b with
            | ForInStep.yield b => f x.val ⋯ b
            | ForInStep.done b => pure (ForInStep.done b))
          a_1 (map (fun x => ⟨x.val, ⋯⟩) as.attach)

Tactic:
congr 1

Result:
case cons.e_a
m : Type u_1 → Type u_2
α : Type u_3
β : Type u_1
inst✝¹ : Monad m
inst✝ : LawfulMonad m
a : α
as : List α
ih :
  ∀ (f : (a : α) → a ∈ as → β → m (ForInStep β)) (init : β),
    forIn' as init f =
      ForInStep.value <$>
        List.foldlM
          (fun b x =>
            match x with
            | ⟨a, m_1⟩ =>
              match b with
              | ForInStep.yield b => f a m_1 b
              | ForInStep.done b => pure (ForInStep.done b))
          (ForInStep.yield init) as.attach
f : (a_1 : α) → a_1 ∈ a :: as → β → m (ForInStep β)
init : β
⊢ (fun x =>
      match x with
      | ForInStep.done b => pure b
      | ForInStep.yield b => forIn' as b fun a' m b => f a' ⋯ b) =
    fun a_1 =>
    ForInStep.value <$>
      List.foldlM
        (fun b x =>
          match b with
          | ForInStep.yield b => f x.val ⋯ b
          | ForInStep.done b => pure (ForInStep.done b))
        a_1 (map (fun x => ⟨x.val, ⋯⟩) as.attach)