/-- If `x` belongs to the ideal generated by generators in `s`, then every element of the support of
`x` factors through an element of `s`.

We could spell `∃ d, m = d * m` as `MulOpposite.op m' ∣ MulOpposite.op m` but this would be worse.
-/
theorem MonoidAlgebra.mem_ideal_span_of_image [Monoid G] [Semiring k] {s : Set G}
    {x : MonoidAlgebra k G} :
    x ∈ Ideal.span (MonoidAlgebra.of k G '' s) ↔ ∀ m ∈ x.support, ∃ m' ∈ s, ∃ d, m = d * m' := by
  let RHS : Ideal (MonoidAlgebra k G) :=
    { carrier := { p | ∀ m : G, m ∈ p.support → ∃ m' ∈ s, ∃ d, m = d * m' }
      add_mem' := fun {x y} hx hy m hm => by
        classical exact (Finset.mem_union.1 <| Finsupp.support_add hm).elim (hx m) (hy m)
      zero_mem' := fun m hm => by cases hm
      smul_mem' := fun x y hy m hm => by
        classical
        replace hm := Finset.mem_biUnion.mp (Finsupp.support_sum hm)
        obtain ⟨xm, -, hm⟩ := hm
        replace hm := Finset.mem_biUnion.mp (Finsupp.support_sum hm)
        obtain ⟨ym, hym, hm⟩ := hm
        replace hm := Finset.mem_singleton.mp (Finsupp.support_single_subset hm)
        obtain rfl := hm
        -- porting note: changed `Exists.imp` to `And.imp_right` due to change in `∃ x ∈ s`
        -- elaboration
        refine' (hy _ hym).imp fun sm p => And.imp_right _ p
        rintro ⟨d, rfl⟩
        exact ⟨xm * d, (mul_assoc _ _ _).symm⟩ }
  change _ ↔ x ∈ RHS
  constructor
  · revert x
    rw [← SetLike.le_def] -- porting note: refine needs this even though it's defeq?
    refine Ideal.span_le.2 ?_
    rintro _ ⟨i, hi, rfl⟩ m hm
    refine' ⟨_, hi, 1, _⟩
    obtain rfl := Finset.mem_singleton.mp (Finsupp.support_single_subset hm)
    exact (one_mul _).symm
  · intro hx
    rw [← Finsupp.sum_single x]
    refine Ideal.sum_mem _ fun i hi => ?_  -- porting note: changed `apply` to `refine`
    obtain ⟨d, hd, d2, rfl⟩ := hx _ hi
    convert Ideal.mul_mem_left _ (id <| Finsupp.single d2 <| x (d2 * d) : MonoidAlgebra k G) _
    pick_goal 3
    refine' Ideal.subset_span ⟨_, hd, rfl⟩
    

Proof state:
case h.e'_4
k : Type u_1
A : Type u_2
G : Type u_3
inst✝¹ : Monoid G
inst✝ : Semiring k
s : Set G
x : MonoidAlgebra k G
RHS : Ideal (MonoidAlgebra k G) :=
  {
    toAddSubmonoid :=
      {
        toAddSubsemigroup :=
          { carrier := {p | ∀ m ∈ p.support, ∃ m' ∈ s, ∃ d, m = d * m'},
            add_mem' :=
              (_ :
                ∀ {x y : MonoidAlgebra k G},
                  x ∈ {p | ∀ m ∈ p.support, ∃ m' ∈ s, ∃ d, m = d * m'} →
                    y ∈ {p | ∀ m ∈ p.support, ∃ m' ∈ s, ∃ d, m = d * m'} →
                      ∀ m ∈ (x + y).support, ∃ m' ∈ s, ∃ d, m = d * m') },
        zero_mem' := (_ : ∀ m ∈ 0.support, ∃ m' ∈ s, ∃ d, m = d * m') },
    smul_mem' :=
      (_ :
        ∀ (x y : MonoidAlgebra k G),
          y ∈
              {
                    toAddSubsemigroup :=
                      { carrier := {p | ∀ m ∈ p.support, ∃ m' ∈ s, ∃ d, m = d * m'},
                        add_mem' :=
                          (_ :
                            ∀ {x y : MonoidAlgebra k G},
                              x ∈ {p | ∀ m ∈ p.support, ∃ m' ∈ s, ∃ d, m = d * m'} →
                                y ∈ {p | ∀ m ∈ p.support, ∃ m' ∈ s, ∃ d, m = d * m'} →
                                  ∀ m ∈ (x + y).support, ∃ m' ∈ s, ∃ d, m = d * m') },
                    zero_mem' := (_ : ∀ m ∈ 0.support, ∃ m' ∈ s, ∃ d, m = d * m') }.toAddSubsemigroup.carrier →
            ∀ m ∈ (x • y).support, ∃ m' ∈ s, ∃ d, m = d * m') }
hx : x ∈ RHS
d : G
hd : d ∈ s
d2 : G
hi : d2 * d ∈ x.support
⊢ (fun₀ | d2 * d => x (d2 * d)) = (id fun₀ | d2 => x (d2 * d)) * (of k G) d

Next tactic:
rw [id.def, MonoidAlgebra.of_apply, MonoidAlgebra.single_mul_single, mul_one]