-- Mathlib/.lake/packages/lean4/src/lean/Init/Data/List/Attach.lean
theorem attach_filterMap {l : List α} {f : α → Option β} :
    (l.filterMap f).attach = l.attach.filterMap
      fun ⟨x, h⟩ => (f x).pbind (fun b m => some ⟨b, mem_filterMap.mpr ⟨x, h, m⟩⟩)

Proof state:
case cons.h_2
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
b✝ :
  { x_1 //
    x_1 ∈
      match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs }
h : ((f x).pbind fun a h => some ⟨a, ⋯⟩) = some b✝
⊢ (match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs).attach =
    b✝ :: filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Tactic:
simp only [Option.pbind_eq_some_iff] at h

Result:
case cons.h_2
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
b✝ :
  { x_1 //
    x_1 ∈
      match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs }
h : ∃ a h, some ⟨a, ⋯⟩ = some b✝
⊢ (match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs).attach =
    b✝ :: filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Proof state:
case cons.h_1
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
h : f x = none
⊢ (match f x with
    | none => filterMap f xs
    | some b => b :: filterMap f xs) =
    ?m.60801

Tactic:
simp only [h]

Result:
case cons.h_1
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
h : f x = none
⊢ filterMap f xs = ?m.60801

Proof state:
case cons.h_2.intro.intro
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
a : β
h : a ∈ f x
⊢ (match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs).attach =
    ⟨a, ⋯⟩ :: filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Tactic:
simp only [Option.mem_def] at h

Result:
case cons.h_2.intro.intro
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
a : β
h✝ : a ∈ f x
h : f x = some a
⊢ (match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs).attach =
    ⟨a, ⋯⟩ :: filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Proof state:
case cons.h_1
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
h : f x = none
⊢ map (fun x_1 => ⟨x_1.val, ⋯⟩) (filterMap f xs).attach =
    filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Tactic:
rw [ih]

Result:
case cons.h_1
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
h : f x = none
⊢ map (fun x_1 => ⟨x_1.val, ⋯⟩)
      (filterMap
        (fun x =>
          match x with
          | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
        xs.attach) =
    filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Proof state:
case cons.h_2.intro.intro
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
b✝ :
  { x_1 //
    x_1 ∈
      match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs }
a : β
h : a ∈ f x
w : some ⟨a, ⋯⟩ = some b✝
⊢ (match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs).attach =
    b✝ :: filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach

Tactic:
simp only [Option.some.injEq] at w

Result:
case cons.h_2.intro.intro
α : Type u_1
β : Type u_2
f : α → Option β
x : α
xs : List α
ih :
  (filterMap f xs).attach =
    filterMap
      (fun x =>
        match x with
        | ⟨x, h⟩ => (f x).pbind fun b m => some ⟨b, ⋯⟩)
      xs.attach
x✝ :
  Option
    { x_1 //
      x_1 ∈
        match f x with
        | none => filterMap f xs
        | some b => b :: filterMap f xs }
b✝ :
  { x_1 //
    x_1 ∈
      match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs }
a : β
h : a ∈ f x
w : ⟨a, ⋯⟩ = b✝
⊢ (match f x with
      | none => filterMap f xs
      | some b => b :: filterMap f xs).attach =
    b✝ :: filterMap ((fun x_1 => (f x_1.val).pbind fun a h => some ⟨a, ⋯⟩) ∘ fun x_1 => ⟨x_1.val, ⋯⟩) xs.attach