[System] You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.Computability.AkraBazzi.GrowsPolynomially).

[User]
The following proof-state transcript was extracted from a real mathlib proof. Use it to answer the question below.

```text
Proof-state transcript for `Mathlib.Computability.AkraBazzi.GrowsPolynomially.GrowsPolynomially`:
State 0: ⊢ lemma GrowsPolynomially.of_isTheta {f g : ℝ → ℝ} (hg : GrowsPolynomially g) (hf : f =Θ[atTop] g)
    (hf' : ∀ᶠ x in atTop, 0 ≤ f x) : GrowsPolynomially f
Tactic: intro b hb
State 1: ⊢ <goal after intro b hb>
Tactic: have hb_pos := hb.1
State 2: ⊢ <goal after have hb_pos := hb.1>
Tactic: have hf_lb := isBigO_iff''.mp hf.isBigO_symm
State 3: ⊢ <goal after have hf_lb := isBigO_iff''.mp hf.isBigO_symm>
Tactic: have hf_ub := isBigO_iff'.mp hf.isBigO
State 4: ⊢ <goal after have hf_ub := isBigO_iff'.mp hf.isBigO>
Tactic: obtain ⟨c₁, hc₁_pos : 0 < c₁, hf_lb⟩ := hf_lb
State 5: ⊢ <goal after obtain ⟨c₁, hc₁_pos : 0 < c₁, hf_lb⟩ := hf_lb>
Tactic: obtain ⟨c₂, hc₂_pos : 0 < c₂, hf_ub⟩ := hf_ub
State 6: ⊢ <goal after obtain ⟨c₂, hc₂_pos : 0 < c₂, hf_ub⟩ := hf_ub>
Tactic: have hg := hg.norm b hb
State 7: ⊢ <goal after have hg := hg.norm b hb>
Tactic: obtain ⟨c₃, hc₃_pos : 0 < c₃, hg⟩ := hg
State 8: ⊢ <goal after obtain ⟨c₃, hc₃_pos : 0 < c₃, hg⟩ := hg>
Tactic: obtain ⟨c₄, hc₄_pos : 0 < c₄, hg⟩ := hg
State 9: ⊢ <goal after obtain ⟨c₄, hc₄_pos : 0 < c₄, hg⟩ := hg>
Tactic: have h_lb_pos : 0 < c₁ * c₂⁻¹ * c₃ := by positivity
State 10: ⊢ <goal after have h_lb_pos : 0 < c₁ * c₂⁻¹ * c₃ := by positivity>
Tactic: have h_ub_pos : 0 < c₂ * c₄ * c₁⁻¹ := by positivity
State 11: ⊢ <goal after have h_ub_pos : 0 < c₂ * c₄ * c₁⁻¹ := by positivity>
Tactic: refine ⟨c₁ * c₂⁻¹ * c₃, h_lb_pos, ?_⟩
State 12: ⊢ <goal after refine ⟨c₁ * c₂⁻¹ * c₃, h_lb_pos, ?_⟩>
Tactic: refine ⟨c₂ * c₄ * c₁⁻¹, h_ub_pos, ?_⟩
State 13: ⊢ <goal after refine ⟨c₂ * c₄ * c₁⁻¹, h_ub_pos, ?_⟩>
Tactic: have c₂_cancel : c₂⁻¹ * c₂ = 1 := inv_mul_cancel₀ (by positivity)
State 14: ⊢ <goal after have c₂_cancel : c₂⁻¹ * c₂ = 1 := inv_mul_cancel₀ (by positivity)>
Tactic: have c₁_cancel : c₁⁻¹ * c₁ = 1 := inv_mul_cancel₀ (by positivity)  filter_upwards [(tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hf',  (tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hf_lb,  (tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hf_ub,  (tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hg,  eventually_ge_atTop 0]  with x hf_pos h_lb h_ub hg_bound hx_pos
State 15: ⊢ <goal after have c₁_cancel : c₁⁻¹ * c₁ = 1 := inv_mul_cancel₀ (by positivity)>
Tactic: intro u hu
State 16: ⊢ <goal after intro u hu>
Tactic: have hbx : b * x ≤ x :=
State 17: ⊢ <goal after have hbx : b * x ≤ x :=>
Tactic: calc b * x ≤ 1 * x := by gcongr; exact le_of_lt hb.2  _ = x := by rw [one_mul]
State 18: ⊢ <goal after calc b * x ≤ 1 * x := by gcongr; exact le_of_lt hb.2>
Tactic: have hg_bound := hg_bound x hbx
State 19: ⊢ <goal after have hg_bound := hg_bound x hbx>
Tactic: refine ⟨?lb, ?ub⟩  case lb => calc  c₁ * c₂⁻¹ * c₃ * f x ≤ c₁ * c₂⁻¹ * c₃ * (c₂ * ‖g x‖) := by
State 20: ⊢ <goal after refine ⟨?lb, ?ub⟩>
Tactic: rw [← Real.norm_of_nonneg (hf_pos x hbx)]; gcongr; exact h_ub x hbx  _ = (c₂⁻¹ * c₂) * c₁ * (c₃ * ‖g x‖) := by ring  _ = c₁ * (c₃ * ‖g x‖) := by simp [c₂_cancel]  _ ≤ c₁ * ‖g u‖ := by gcongr; exact (hg_bound u hu).1  _ ≤ f u := by
State 21: ⊢ <goal after rw [← Real.norm_of_nonneg (hf_pos x hbx)]; gcongr; exact h_ub x hbx>
Tactic: rw [← Real.norm_of_nonneg (hf_pos u hu.1)]
State 22: ⊢ <goal after rw [← Real.norm_of_nonneg (hf_pos u hu.1)]>
Tactic: exact h_lb u hu.1  case ub => calc  f u ≤ c₂ * ‖g u‖ := by rw [← Real.norm_of_nonneg (hf_pos u hu.1)]; exact h_ub u hu.1  _ ≤ c₂ * (c₄ * ‖g x‖) := by gcongr; exact (hg_bound u hu).2  _ = c₂ * c₄ * (c₁⁻¹ * c₁) * ‖g x‖ := by simp [c₁_cancel]; ring  _ = c₂ * c₄ * c₁⁻¹ * (c₁ * ‖g x‖) := by ring  _ ≤ c₂ * c₄ * c₁⁻¹ * f x := by
State 23: ⊢ <goal after exact h_lb u hu.1>
Tactic: gcongr
State 24: ⊢ <goal after gcongr>
Tactic: rw [← Real.norm_of_nonneg (hf_pos x hbx)]
State 25: ⊢ <goal after rw [← Real.norm_of_nonneg (hf_pos x hbx)]>
Tactic: exact h_lb x hbx
```

Question: what is the next tactic after the last recorded state, and why?

<s>[SYSTEM_PROMPT]You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.Computability.AkraBazzi.GrowsPolynomially).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]The following proof-state transcript was extracted from a real mathlib proof. Use it to answer the question below.

```text
Proof-state transcript for `Mathlib.Computability.AkraBazzi.GrowsPolynomially.GrowsPolynomially`:
State 0: ⊢ lemma GrowsPolynomially.of_isTheta {f g : ℝ → ℝ} (hg : GrowsPolynomially g) (hf : f =Θ[atTop] g)
    (hf' : ∀ᶠ x in atTop, 0 ≤ f x) : GrowsPolynomially f
Tactic: intro b hb
State 1: ⊢ <goal after intro b hb>
Tactic: have hb_pos := hb.1
State 2: ⊢ <goal after have hb_pos := hb.1>
Tactic: have hf_lb := isBigO_iff''.mp hf.isBigO_symm
State 3: ⊢ <goal after have hf_lb := isBigO_iff''.mp hf.isBigO_symm>
Tactic: have hf_ub := isBigO_iff'.mp hf.isBigO
State 4: ⊢ <goal after have hf_ub := isBigO_iff'.mp hf.isBigO>
Tactic: obtain ⟨c₁, hc₁_pos : 0 < c₁, hf_lb⟩ := hf_lb
State 5: ⊢ <goal after obtain ⟨c₁, hc₁_pos : 0 < c₁, hf_lb⟩ := hf_lb>
Tactic: obtain ⟨c₂, hc₂_pos : 0 < c₂, hf_ub⟩ := hf_ub
State 6: ⊢ <goal after obtain ⟨c₂, hc₂_pos : 0 < c₂, hf_ub⟩ := hf_ub>
Tactic: have hg := hg.norm b hb
State 7: ⊢ <goal after have hg := hg.norm b hb>
Tactic: obtain ⟨c₃, hc₃_pos : 0 < c₃, hg⟩ := hg
State 8: ⊢ <goal after obtain ⟨c₃, hc₃_pos : 0 < c₃, hg⟩ := hg>
Tactic: obtain ⟨c₄, hc₄_pos : 0 < c₄, hg⟩ := hg
State 9: ⊢ <goal after obtain ⟨c₄, hc₄_pos : 0 < c₄, hg⟩ := hg>
Tactic: have h_lb_pos : 0 < c₁ * c₂⁻¹ * c₃ := by positivity
State 10: ⊢ <goal after have h_lb_pos : 0 < c₁ * c₂⁻¹ * c₃ := by positivity>
Tactic: have h_ub_pos : 0 < c₂ * c₄ * c₁⁻¹ := by positivity
State 11: ⊢ <goal after have h_ub_pos : 0 < c₂ * c₄ * c₁⁻¹ := by positivity>
Tactic: refine ⟨c₁ * c₂⁻¹ * c₃, h_lb_pos, ?_⟩
State 12: ⊢ <goal after refine ⟨c₁ * c₂⁻¹ * c₃, h_lb_pos, ?_⟩>
Tactic: refine ⟨c₂ * c₄ * c₁⁻¹, h_ub_pos, ?_⟩
State 13: ⊢ <goal after refine ⟨c₂ * c₄ * c₁⁻¹, h_ub_pos, ?_⟩>
Tactic: have c₂_cancel : c₂⁻¹ * c₂ = 1 := inv_mul_cancel₀ (by positivity)
State 14: ⊢ <goal after have c₂_cancel : c₂⁻¹ * c₂ = 1 := inv_mul_cancel₀ (by positivity)>
Tactic: have c₁_cancel : c₁⁻¹ * c₁ = 1 := inv_mul_cancel₀ (by positivity)  filter_upwards [(tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hf',  (tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hf_lb,  (tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hf_ub,  (tendsto_id.const_mul_atTop hb_pos).eventually_forall_ge_atTop hg,  eventually_ge_atTop 0]  with x hf_pos h_lb h_ub hg_bound hx_pos
State 15: ⊢ <goal after have c₁_cancel : c₁⁻¹ * c₁ = 1 := inv_mul_cancel₀ (by positivity)>
Tactic: intro u hu
State 16: ⊢ <goal after intro u hu>
Tactic: have hbx : b * x ≤ x :=
State 17: ⊢ <goal after have hbx : b * x ≤ x :=>
Tactic: calc b * x ≤ 1 * x := by gcongr; exact le_of_lt hb.2  _ = x := by rw [one_mul]
State 18: ⊢ <goal after calc b * x ≤ 1 * x := by gcongr; exact le_of_lt hb.2>
Tactic: have hg_bound := hg_bound x hbx
State 19: ⊢ <goal after have hg_bound := hg_bound x hbx>
Tactic: refine ⟨?lb, ?ub⟩  case lb => calc  c₁ * c₂⁻¹ * c₃ * f x ≤ c₁ * c₂⁻¹ * c₃ * (c₂ * ‖g x‖) := by
State 20: ⊢ <goal after refine ⟨?lb, ?ub⟩>
Tactic: rw [← Real.norm_of_nonneg (hf_pos x hbx)]; gcongr; exact h_ub x hbx  _ = (c₂⁻¹ * c₂) * c₁ * (c₃ * ‖g x‖) := by ring  _ = c₁ * (c₃ * ‖g x‖) := by simp [c₂_cancel]  _ ≤ c₁ * ‖g u‖ := by gcongr; exact (hg_bound u hu).1  _ ≤ f u := by
State 21: ⊢ <goal after rw [← Real.norm_of_nonneg (hf_pos x hbx)]; gcongr; exact h_ub x hbx>
Tactic: rw [← Real.norm_of_nonneg (hf_pos u hu.1)]
State 22: ⊢ <goal after rw [← Real.norm_of_nonneg (hf_pos u hu.1)]>
Tactic: exact h_lb u hu.1  case ub => calc  f u ≤ c₂ * ‖g u‖ := by rw [← Real.norm_of_nonneg (hf_pos u hu.1)]; exact h_ub u hu.1  _ ≤ c₂ * (c₄ * ‖g x‖) := by gcongr; exact (hg_bound u hu).2  _ = c₂ * c₄ * (c₁⁻¹ * c₁) * ‖g x‖ := by simp [c₁_cancel]; ring  _ = c₂ * c₄ * c₁⁻¹ * (c₁ * ‖g x‖) := by ring  _ ≤ c₂ * c₄ * c₁⁻¹ * f x := by
State 23: ⊢ <goal after exact h_lb u hu.1>
Tactic: gcongr
State 24: ⊢ <goal after gcongr>
Tactic: rw [← Real.norm_of_nonneg (hf_pos x hbx)]
State 25: ⊢ <goal after rw [← Real.norm_of_nonneg (hf_pos x hbx)]>
Tactic: exact h_lb x hbx
```

Question: what is the next tactic after the last recorded state, and why?[/INST]


-- Mathlib.RingTheory.Ideal.Colon.bot_colon' (L138-L140)
theorem bot_colon' : (⊥ : Submodule R M).colon S = (span R S).annihilator := by
aesop (add simp [mem_colon, mem_annihilator_span])

-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.AssociatedPrimes (L108-L109)
theorem AssociatedPrimes.mem_iff : I ∈ associatedPrimes R M ↔ IsAssociatedPrime I M := Iff.rfl

-- Mathlib.RingTheory.Ideal.Colon.colon_empty (L114-L116)
lemma colon_empty : N.colon (∅ : Set M) = ⊤ := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Colon.colon_inf_eq_left_of_subset (L81-L83)
lemma colon_inf_eq_left_of_subset (h : S ⊆ (N₂ : Set M)) : (N₁ ⊓ N₂).colon S = N₁.colon S := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Cotangent.mem_toCotangent_ker (L58-L61)
theorem mem_toCotangent_ker {x : I} : x ∈ LinearMap.ker I.toCotangent ↔ (x : R) ∈ I ^ 2 := by
rw [← I.map_toCotangent_ker]
  simp

-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_surjective (L68-L69)
theorem toCotangent_surjective : Function.Surjective I.toCotangent := Submodule.mkQ_surjective _

-- Mathlib.RingTheory.Ideal.BigOperators.sum_mem (L29-L33)
theorem sum_mem (I : Ideal α) {ι : Type*} {t : Finset ι} {f : ι → α} :
    (∀ c ∈ t, f c ∈ I) → (∑ i ∈ t, f i) ∈ I :=
Submodule.sum_mem I

end Ideal

-- Mathlib.RingTheory.Ideal.Colon.colon_eq_top_iff_subset (L85-L87)
lemma colon_eq_top_iff_subset (S : Set M) : N.colon S = ⊤ ↔ S ⊆ N := by
aesop (add simp [mem_colon, Ideal.eq_top_iff_one])

-- Mathlib.RingTheory.Ideal.Colon.bot_colon (L64-L67)
theorem bot_colon : colon (⊥ : Submodule R M) (N : Set M) = N.annihilator := by
ext x
  simp [mem_colon, mem_annihilator]

-- Mathlib.RingTheory.Ideal.Colon.top_colon (L102-L104)
lemma top_colon : (⊤ : Submodule R M).colon S = ⊤ := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Colon.iInf_colon (L93-L95)
lemma iInf_colon {ι : Sort*} (f : ι → Submodule R M) : (⨅ i, f i).colon S = ⨅ i, (f i).colon S := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Colon.inf_colon (L89-L91)
lemma inf_colon : (N₁ ⊓ N₂).colon S = N₁.colon S ⊓ N₂.colon S := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_eq_zero (L66-L67)
theorem toCotangent_eq_zero (x : I) : I.toCotangent x = 0 ↔ (x : R) ∈ I ^ 2 := I.mem_toCotangent_ker

-- Mathlib.RingTheory.Ideal.Colon.colon_singleton_zero (L117-L119)
lemma colon_singleton_zero : N.colon {0} = ⊤ := by
simp

-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent (L51-L53)
def toCotangent : I →ₗ[R] I.Cotangent := Submodule.mkQ _
set_option backward.isDefEq.respectTransparency false in

-- Mathlib.RingTheory.Ideal.Colon.colon_union (L106-L108)
lemma colon_union : N.colon (S₁ ∪ S₂) = N.colon S₁ ⊓ N.colon S₂ := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Colon.mem_colon_singleton (L48-L50)
theorem mem_colon_singleton {x : M} {r : R} : r ∈ N.colon {x} ↔ r • x ∈ N := by
simp [mem_colon, forall_eq]

-- Mathlib.RingTheory.Ideal.Colon.colon_iUnion (L110-L112)
lemma colon_iUnion {ι : Sort*} (f : ι → Set M) : N.colon (⋃ i, f i) = ⨅ i, N.colon (f i) := by
aesop (add simp mem_colon)

-- Mathlib.RingTheory.Ideal.Colon.mem_colon (L45-L46)
theorem mem_colon {r} : r ∈ N.colon S ↔ ∀ s ∈ S, r • s ∈ N := Set.smul_set_subset_iff

-- Mathlib.RingTheory.Ideal.Colon.mem_colon' (L131-L133)
theorem mem_colon' {r} : r ∈ N.colon S ↔ S ≤ comap (r • (LinearMap.id : M →ₗ[R] M)) N :=
mem_colon

-- Mathlib.RingTheory.Ideal.Colon.mem_colon_span_singleton (L154-L156)
theorem mem_colon_span_singleton {x : M} {r : R} : r ∈ N.colon (span R {x}) ↔ r • x ∈ N := by
simp

-- Mathlib.RingTheory.Ideal.Basic.mem_pi (L56-L58)
theorem mem_pi (r : Π i, R i) : r ∈ pi I ↔ ∀ i, r i ∈ I i :=
Iff.rfl

-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_eq (L62-L65)
theorem toCotangent_eq {x y : I} : I.toCotangent x = I.toCotangent y ↔ (x - y : R) ∈ I ^ 2 := by
rw [← sub_eq_zero]
  exact I.mem_toCotangent_ker

lemma Ideal.height_le_ringKrullDim_quotient_add_one {r : R} {p : Ideal R} [p.IsPrime]
    (hrp : r ∈ p) : p.height ≤ ringKrullDim (R ⧸ span {r}) + 1

[System] You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.RingTheory.Ideal.KrullsHeightTheorem).

[User]
Prove the following theorem. The retrieved context below may help.

```lean
lemma Ideal.height_le_ringKrullDim_quotient_add_one {r : R} {p : Ideal R} [p.IsPrime]
    (hrp : r ∈ p) : p.height ≤ ringKrullDim (R ⧸ span {r}) + 1
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.IsLocalRing (L48-L58)
lemma IsLocalRing.quotient_artinian_of_mem_minimalPrimes_of_isLocalRing
    [IsLocalRing R] (I : Ideal R) (hp : IsLocalRing.maximalIdeal R ∈ I.minimalPrimes) :
    IsArtinianRing (R ⧸ I) :=
have : Ring.KrullDimLE 0 (R ⧸ I) := Ring.krullDimLE_zero_iff.mpr fun J prime ↦
    Ideal.isMaximal_of_isIntegral_of_isMaximal_comap _ <| by
      convert IsLocalRing.maximalIdeal.isMaximal R
      rw [Ideal.minimalPrimes, Set.mem_setOf] at hp
      have := prime.comap (Ideal.Quotient.mk I)
      exact hp.eq_of_le ⟨this, .trans (by simp) (Ideal.ker_le_comap _)⟩ (le_maximalIdeal this.1)
  IsNoetherianRing.isArtinianRing_of_krullDimLE_zero
```

-- snippet 2
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.height_le_ringKrullDim_quotient_add_spanFinrank (L343-L351)
lemma height_le_ringKrullDim_quotient_add_spanFinrank {p I : Ideal R} [p.IsPrime] (h : I ≤ p) :
    p.height ≤ ringKrullDim (R ⧸ I) + I.spanFinrank := by
trans (p.map (Ideal.Quotient.mk I)).height + I.spanFinrank
  · norm_cast; exact Ideal.height_le_height_add_spanFinrank_of_le h
  · gcongr
    have : (Ideal.map (Ideal.Quotient.mk I) p).IsPrime :=
      Ideal.isPrime_map_quotientMk_of_isPrime h
    exact Ideal.height_le_ringKrullDim_of_ne_top Ideal.IsPrime.ne_top'
```

-- snippet 3
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_ringKrullDim_quotient_add_spanFinrank (L352-L362)
lemma ringKrullDim_le_ringKrullDim_quotient_add_spanFinrank (I : Ideal R)
    (h : I ≤ Ring.jacobson R) :
    ringKrullDim R ≤ ringKrullDim (R ⧸ I) + I.spanFinrank := by
nontriviality R
  rw [ringKrullDim_le_iff_isMaximal_height_le]
  intro m hm
  exact height_le_ringKrullDim_quotient_add_spanFinrank <|
    le_trans h <| Ring.jacobson_le_of_isMaximal m

/-- If `p` is a prime ideal containing `s`, the height of `p` is bounded
by the sum of the height of the image of `p` in `R ⧸ (s)` and the cardinality of `s`. -/
```

-- snippet 4
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_ringKrullDim_quotient_add_encard (L386-L391)
lemma ringKrullDim_le_ringKrullDim_quotient_add_encard (s : Set R) (hs : s ⊆ Ring.jacobson R) :
    ringKrullDim R ≤ ringKrullDim (R ⧸ Ideal.span s) + s.encard := by
refine le_trans (ringKrullDim_le_ringKrullDim_quotient_add_spanFinrank (Ideal.span s) ?_) ?_
  · simpa [Ideal.span_le]
  · gcongr; norm_cast; exact Submodule.spanFinrank_span_le_encard _
```

-- snippet 5
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_ringKrullDim_quotient_add_card (L392-L407)
lemma ringKrullDim_le_ringKrullDim_quotient_add_card (s : Finset R)
    (hs : (s : Set R) ⊆ Ring.jacobson R) :
    ringKrullDim R ≤ ringKrullDim (R ⧸ Ideal.span (s : Set R)) + s.card := by
convert ringKrullDim_le_ringKrullDim_quotient_add_encard s hs
  norm_cast

section Algebra

variable {S : Type*} [CommRing S] [Algebra R S]

/--
If `P` lies over `p`, the height of `P` is bounded by the height of `p` plus
the height of the image of `P` in `S ⧸ p S`.
Equality holds if `S` satisfies going-down as an `R`-algebra
(see `Ideal.height_eq_height_add_of_liesOver_of_hasGoingDown`).
-/
```

-- snippet 6
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_spanFinrank_maximalIdeal (L474-L480)
lemma ringKrullDim_le_spanFinrank_maximalIdeal [IsLocalRing R] :
    ringKrullDim R ≤ (IsLocalRing.maximalIdeal R).spanFinrank :=
le_of_eq_of_le IsLocalRing.maximalIdeal_height_eq_ringKrullDim.symm
    (WithBot.coe_le_coe.mpr (Ideal.height_le_spanFinrank (IsLocalRing.maximalIdeal R)
      Ideal.IsPrime.ne_top'))

end Algebra
```

-- snippet 7
```lean
-- Mathlib.RingTheory.Ideal.Basic.single_mem_pi (L67-L72)
theorem single_mem_pi [DecidableEq ι] {i : ι} {r : R i} (hr : r ∈ I i) : Pi.single i r ∈ pi I := by
intro j
  obtain rfl | ne := eq_or_ne i j
  · simpa
  · simp [ne]
```

-- snippet 8
```lean
-- Mathlib.RingTheory.Ideal.Colon.bot_colon' (L138-L140)
theorem bot_colon' : (⊥ : Submodule R M).colon S = (span R S).annihilator := by
aesop (add simp [mem_colon, mem_annihilator_span])
```

-- snippet 9
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.AssociatedPrimes (L108-L109)
theorem AssociatedPrimes.mem_iff : I ∈ associatedPrimes R M ↔ IsAssociatedPrime I M := Iff.rfl
```

-- snippet 10
```lean
-- Mathlib.RingTheory.Ideal.Basic.isField_iff_maximal_bot (L259-L264)
theorem isField_iff_maximal_bot [Nontrivial R] : IsField R ↔ (⊥ : Ideal R).IsMaximal := by
refine ⟨fun h ↦ let := h.toSemifield; bot_isMaximal, fun hmax ↦ ?_⟩
  by_contra hf
  obtain ⟨x, hx0, hxu⟩ := exists_not_isUnit_of_not_isField hf
  exact hx0 <| span_singleton_eq_bot.mp (hmax.eq_of_le (span_singleton_ne_top hxu) bot_le).symm
```

-- snippet 11
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.subset_union_of_exact (L171-L204)
theorem subset_union_of_exact (hf : Function.Injective f) (hfg : Function.Exact f g) :
    associatedPrimes R M' ⊆ associatedPrimes R M ∪ associatedPrimes R M'' := by
rintro p ⟨_, x, hx⟩
  by_cases! h : ∃ a ∈ p.primeCompl, ∃ y : M, ∃ k, f y = a ^ k • x
  · obtain ⟨a, ha, y, k, h⟩ := h
    left
    refine ⟨‹_›, y, le_antisymm (fun b hb ↦ ?_) (fun b ⟨n, hb⟩ ↦ ?_)⟩
    · rw [hx] at hb
      obtain ⟨n, hb⟩ := hb
      use n
      rw [mem_colon_singleton, mem_bot] at hb ⊢
      apply_fun _ using hf
      rw [map_smul, h, smul_comm, hb, smul_zero, map_zero]
    · rw [mem_colon_singleton, mem_bot] at hb
      apply_fun f at hb
      rw [map_smul, map_zero, h, ← mul_smul, ← mem_bot R, ← mem_colon_singleton] at hb
      replace hb := hx.ge (Ideal.le_radical hb)
      contrapose hb
      exact p.primeCompl.mul_mem (p.primeCompl.pow_mem hb n) (p.primeCompl.pow_mem ha k)
  · right
    refine ⟨‹_›, g x, le_antisymm (fun b hb ↦ ?_) (fun b ⟨n, hb⟩ ↦ ?_)⟩
    · rw [hx] at hb
      refine Ideal.radical_mono (fun b hb ↦ ?_) hb
      rw [mem_colon_singleton, mem_bot] at hb ⊢
      rw [← map_smul, hb, map_zero]
    · rw [mem_colon_singleton, mem_bot, ← map_smul, ← LinearMap.mem_ker,
        hfg.linearMap_ker_eq] at hb
      obtain ⟨y, hy⟩ := hb
      by_contra H
      exact h b H y n hy

variable (R M M') in
/-- The set of associated primes of the product of two modules is equal to
the union of those of the two modules. -/
```

-- snippet 12
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_empty (L114-L116)
lemma colon_empty : N.colon (∅ : Set M) = ⊤ := by
aesop (add simp mem_colon)
```

-- snippet 13
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.map_toCotangent_ker (L54-L57)
theorem map_toCotangent_ker : (LinearMap.ker I.toCotangent).map I.subtype = I ^ 2 := by
rw [Ideal.toCotangent, Submodule.ker_mkQ, pow_two, Submodule.map_smul'' I ⊤ (Submodule.subtype I),
    smul_eq_mul, Submodule.map_subtype_top]
```

-- snippet 14
```lean
-- Mathlib.RingTheory.Ideal.Basic.exists_maximal_of_not_isField (L265-L270)
theorem exists_maximal_of_not_isField [Nontrivial R] (h : ¬ IsField R) :
    ∃ p : Ideal R, p ≠ ⊥ ∧ p.IsMaximal := by
contrapose! h
  simp only [← bot_lt_iff_ne_bot] at h
  refine isField_iff_maximal_bot.mpr ⟨⟨bot_ne_top, Ideal.maximal_of_no_maximal h⟩⟩
```

-- snippet 15
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_span (L142-L149)
theorem colon_span : N.colon (span R S) = N.colon S := by
refine (colon_mono le_rfl subset_span).antisymm fun r h ↦ mem_colon.mpr fun s hs ↦ ?_
  induction hs using Submodule.span_induction with
  | mem => aesop (add simp mem_colon)
  | zero => simp
  | add => aesop
  | smul => simp_all [smul_mem, smul_comm r]
```

-- snippet 16
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_add_pred_mem_of_pow_mem_of_commute (L97-L117)
theorem add_pow_add_pred_mem_of_pow_mem_of_commute {m n : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hab : Commute a b) :
    (a + b) ^ (m + n - 1) ∈ I :=
I.add_pow_mem_of_pow_mem_of_le_of_commute ha hb (by rw [← Nat.sub_le_iff_le_add]) hab

end Commute

end Ideal

end Semiring

section CommSemiring

variable {a b : α}

-- A separate namespace definition is needed because the variables were historically in a different
-- order.
namespace Ideal

variable [CommSemiring α] (I : Ideal α)
```

-- snippet 17
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.biUnion_associatedPrimes_eq_zero_divisors (L229-L239)
theorem biUnion_associatedPrimes_eq_zero_divisors [IsNoetherianRing R] :
    ⋃ p ∈ associatedPrimes R M, p = { r : R | ∃ x : M, x ≠ 0 ∧ r • x = 0 } := by
simp only [AssociatedPrimes.mem_iff, isAssociatedPrime_iff]
  refine subset_antisymm (Set.iUnion₂_subset ?_) ?_
  · rintro _ ⟨h, x, ⟨⟩⟩ r h'
    exact ⟨x, by simpa using h.ne_top, by simpa using h'⟩
  · intro r ⟨x, h, h'⟩
    obtain ⟨P, hP, hx⟩ := exists_le_isAssociatedPrime_of_isNoetherianRing R x h
    rw [isAssociatedPrime_iff] at hP
    exact Set.mem_iUnion₂_of_mem hP (hx (by rwa [mem_colon_singleton]))
```

-- snippet 18
```lean
-- Mathlib.RingTheory.Ideal.Colon.annihilator_map_mkQ_eq_colon (L169-L174)
lemma annihilator_map_mkQ_eq_colon : annihilator (P.map N.mkQ) = N.colon (P : Set M) := by
ext
  rw [mem_annihilator, mem_colon]
  exact ⟨fun H p hp ↦ (Quotient.mk_eq_zero N).1 (H (Quotient.mk p) (mem_map_of_mem hp)),
    fun H _ ⟨p, hp, hpm⟩ ↦ hpm ▸ ((Quotient.mk_eq_zero N).2 <| H p hp)⟩
```

-- snippet 19
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon (L35-L44)
def colon (N : Submodule R M) (S : Set M) : Ideal R where
  carrier := {r : R | (r • S : Set M) ⊆ N}
add_mem' ha hb :=
    (Set.add_smul_subset _ _ _).trans ((Set.add_subset_add ha hb).trans_eq (by simp))
  zero_mem' := (Set.zero_smul_set_subset S).trans (by simp)
  smul_mem' r := by
    simp only [Set.mem_setOf_eq, smul_eq_mul, mul_smul, Set.smul_set_subset_iff]
    intro x hx y hy
    exact N.smul_mem _ (hx hy)
```

-- snippet 20
```lean
-- Mathlib.RingTheory.Ideal.Basis.Basis (L55-L61)
theorem Basis.mem_ideal_iff {ι R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S]
    {I : Ideal S} (b : Basis ι R I) {x : S} :
    x ∈ I ↔ ∃ c : ι →₀ R, x = Finsupp.sum c fun i x => x • (b i : S) :=
(b.map ((I.restrictScalarsEquiv R _ _).restrictScalars R).symm).mem_submodule_iff

/-- If `I : Ideal S` has a finite basis over `R`,
`x ∈ I` iff it is a linear combination of basis vectors. -/
```

-- snippet 21
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.comap_mem_associatedPrimes_of_mem_associatedPrimes_of_isLocalizedModule_of_fg (L84-L120)
lemma comap_mem_associatedPrimes_of_mem_associatedPrimes_of_isLocalizedModule_of_fg (p : Ideal R')
    (ass : p ∈ associatedPrimes R' M') (fg : (p.comap (algebraMap R R')).FG) :
    p.comap (algebraMap R R') ∈ associatedPrimes R M := by
rcases ass with ⟨hp, x, hx⟩
  rcases fg with ⟨T, hT⟩
  rcases IsLocalizedModule.mk'_surjective S f x with ⟨⟨m, s⟩, rfl⟩
  simp only [Function.uncurry_apply_pair] at hx
  have mem (a : T) : algebraMap R R' a ∈ p := by
    simpa [← Ideal.mem_comap, ← hT] using Ideal.subset_span a.2
  simp only [hx, Ideal.mem_radical_iff, mem_bot, mem_colon_singleton, ← map_pow, algebraMap_smul,
    ← IsLocalizedModule.mk'_smul, IsLocalizedModule.mk'_eq_zero' f] at mem
  choose e g hg using mem
  refine ⟨.under R p, (∏ a, g a).1 • m, le_antisymm ?_ fun r hr ↦ ?_⟩
  · rw [← hT, Ideal.span_le]
    intro a ha
    simp only [SetLike.mem_coe, Ideal.mem_radical_iff, mem_bot, mem_colon_singleton]
    obtain ⟨u, hu⟩ : g ⟨a, ha⟩ ∣ (∏ a, g a) := by
      apply Finset.dvd_prod_of_mem g (Finset.mem_univ ⟨a, ha⟩)
    use e ⟨a, ha⟩
    rw [hu, Submonoid.coe_mul, smul_smul, ← mul_assoc, mul_comm, ← smul_smul, mul_comm, ← smul_smul]
    exact smul_eq_zero_of_right u.1 (hg ⟨a, ha⟩)
  · simp only [Ideal.mem_radical_iff, mem_bot, mem_colon_singleton, smul_smul] at hr
    obtain ⟨k, hk⟩ := hr
    have mem : r ^ k * (∏ a, g a).1 ∈ Ideal.comap (algebraMap R R') p := by
      rw [hx]
      use 1
      simp_rw [pow_one, mem_colon_singleton, algebraMap_smul, ← IsLocalizedModule.mk'_smul,
        hk, IsLocalizedModule.mk'_zero, mem_bot]
    apply hp.mem_of_pow_mem k
    rw [← map_pow]
    exact ((hp.under R).mul_mem_iff_mem_or_mem.mp mem).resolve_right
      (Set.disjoint_left.mp ((IsLocalization.disjoint_comap_iff S R' p).mpr hp.1) (∏ a, g a).2)

variable (R')

include S f in
open Set in
```

-- snippet 22
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_univ (L57-L60)
theorem colon_univ {I : Ideal R} [I.IsTwoSided] : I.colon Set.univ = I := by
simp_rw [SetLike.ext_iff, mem_colon, smul_eq_mul]
  exact fun x ↦ ⟨fun h ↦ mul_one x ▸ h 1 trivial, fun h _ _ ↦ I.mul_mem_right _ h⟩
```

-- snippet 23
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_inf_eq_left_of_subset (L81-L83)
lemma colon_inf_eq_left_of_subset (h : S ⊆ (N₂ : Set M)) : (N₁ ⊓ N₂).colon S = N₁.colon S := by
aesop (add simp mem_colon)
```

-- snippet 24
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.exists_le_isAssociatedPrime_of_isNoetherianRing (L139-L163)
theorem exists_le_isAssociatedPrime_of_isNoetherianRing [H : IsNoetherianRing R] (x : M)
    (hx : x ≠ 0) : ∃ P : Ideal R, IsAssociatedPrime P M ∧ (⊥ : Submodule R M).colon {x} ≤ P := by
simp only [isAssociatedPrime_iff]
  obtain ⟨P, ⟨l, h₁, y, rfl⟩, h₃⟩ :=
    set_has_maximal_iff_noetherian.mpr H
      { P | (⊥ : Submodule R M).colon {x} ≤ P ∧ P ≠ ⊤ ∧ ∃ y : M, P = (⊥ : Submodule R M).colon {y} }
      ⟨_, rfl.le, by simpa, x, rfl⟩
  refine ⟨_, ⟨⟨h₁, ?_⟩, y, rfl⟩, l⟩
  intro a b hab
  rw [or_iff_not_imp_left]
  intro ha
  rw [mem_colon_singleton] at ha hab
  have H₁ : (⊥ : Submodule R M).colon {y} ≤ (⊥ : Submodule R M).colon {a • y} := by
    intro c hc
    rw [mem_colon_singleton, mem_bot] at hc ⊢
    rw [smul_comm, hc, smul_zero]
  rwa [H₁.eq_of_not_lt (h₃ _ ⟨l.trans H₁, by simpa, _, rfl⟩),
    mem_colon_singleton, smul_comm, smul_smul]

namespace associatedPrimes

variable {f} {M'' : Type*} [AddCommMonoid M''] [Module R M''] {g : M' →ₗ[R] M''}

/-- If `M → M'` is injective, then the set of associated primes of `M` is
contained in that of `M'`. -/
```

-- snippet 25
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.mem_associatedPrimes_atPrime_of_mem_associatedPrimes (L70-L77)
lemma mem_associatedPrimes_atPrime_of_mem_associatedPrimes
    {p : Ideal R} [p.IsPrime] (ass : p ∈ associatedPrimes R M) :
    maximalIdeal (Localization.AtPrime p) ∈
    associatedPrimes (Localization.AtPrime p) (LocalizedModule.AtPrime p M) := by
apply mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule
    p.primeCompl (LocalizedModule.mkLinearMap p.primeCompl M)
  simpa [Localization.AtPrime.comap_maximalIdeal] using ass
```

-- snippet 26
```lean
-- Mathlib.RingTheory.Ideal.Basic.sum_pow_mem_span_pow (L155-L160)
theorem sum_pow_mem_span_pow {ι} (s : Finset ι) (f : ι → α) (n : ℕ) :
    (∑ i ∈ s, f i) ^ (s.card * n + 1) ∈ span ((fun i => f i ^ (n + 1)) '' s) := by
classical
  simpa only [Multiset.card_map, Multiset.map_map, comp_apply, Multiset.toFinset_map,
    Finset.coe_image, Finset.val_toFinset] using pow_multiset_sum_mem_span_pow (s.1.map f) n
```

-- snippet 27
```lean
-- Mathlib.RingTheory.Ideal.Basic.not_isField_iff_exists_prime (L282-L291)
theorem not_isField_iff_exists_prime [Nontrivial R] :
    ¬IsField R ↔ ∃ p : Ideal R, p ≠ ⊥ ∧ p.IsPrime := by
refine ⟨fun h ↦ ?_, fun ⟨I, h_bot, h_top⟩ ↦ not_isField_of_ne_of_ne h_bot h_top.ne_top⟩
  obtain ⟨I, hI, hIm⟩ := exists_maximal_of_not_isField h
  exact ⟨I, hI, hIm.isPrime⟩

/-- Also see `Ideal.isSimpleOrder` for the forward direction as an instance when `R` is a
division (semi)ring.

This result actually holds for all division semirings, but we lack the predicate to state it. -/
```

-- snippet 28
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.isAssociatedPrime_iff_exists_injective_linearMap (L257-L267)
theorem isAssociatedPrime_iff_exists_injective_linearMap [IsNoetherianRing R] :
    IsAssociatedPrime I M ↔ I.IsPrime ∧ ∃ (f : R ⧸ I →ₗ[R] M), Function.Injective f := by
rw [isAssociatedPrime_iff, and_congr_right_iff]
  refine fun _ ↦ ⟨fun ⟨x, h⟩ ↦ ?_, fun ⟨f, h⟩ ↦ ⟨(f ∘ₗ mkQ I) 1, ?_⟩⟩
  · replace h : I = ker (toSpanSingleton R M x) := by simp [h, SetLike.ext_iff]
    exact ⟨liftQ _ _ h.le, ker_eq_bot.mp (ker_liftQ_eq_bot' _ _ h)⟩
  · conv_lhs => rw [← I.ker_mkQ, ← ker_comp_of_ker_eq_bot (mkQ I) (ker_eq_bot_of_injective h)]
    simp [SetLike.ext_iff, ← Ideal.Quotient.algebraMap_eq, Algebra.algebraMap_eq_smul_one]

variable {I J M}
```

-- snippet 29
```lean
-- Mathlib.RingTheory.Ideal.Basis.basisSpanSingleton_apply (L39-L44)
theorem basisSpanSingleton_apply (b : Basis ι R S) {x : S} (hx : x ≠ 0) (i : ι) :
    (basisSpanSingleton b hx i : S) = x * b i := by
simp only [basisSpanSingleton, Basis.map_apply, LinearEquiv.trans_apply,
    Submodule.restrictScalarsEquiv_apply, LinearEquiv.ofInjective_apply, LinearEquiv.coe_ofEq_apply,
    LinearEquiv.restrictScalars_apply, LinearMap.mulLeft_apply]
```

-- snippet 30
```lean
-- Mathlib.RingTheory.Ideal.Basic.pi (L50-L55)
def pi : Ideal (Π i, R i) where
  carrier := { r | ∀ i, r i ∈ I i }
zero_mem' i := (I i).zero_mem
  add_mem' ha hb i := (I i).add_mem (ha i) (hb i)
  smul_mem' a _b hb i := (I i).mul_mem_left (a i) (hb i)
```

-- snippet 31
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.subset_of_injective (L165-L169)
theorem subset_of_injective (hf : Function.Injective f) :
    associatedPrimes R M ⊆ associatedPrimes R M' := fun _I h => h.map_of_injective f hf
/-- If `0 → M → M' → M''` is an exact sequence, then the set of associated primes of `M'` is
contained in the union of those of `M` and `M''`. -/
```

-- snippet 32
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.mem_toCotangent_ker (L58-L61)
theorem mem_toCotangent_ker {x : I} : x ∈ LinearMap.ker I.toCotangent ↔ (x : R) ∈ I ^ 2 := by
rw [← I.map_toCotangent_ker]
  simp
```

-- snippet 33
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon_iff_le (L134-L137)
theorem mem_colon_iff_le {r} : r ∈ N.colon N' ↔ r • N' ≤ N := by
aesop (add simp SetLike.coe_subset_coe)

/-- A variant for arbitrary sets in commutative semirings -/
```

-- snippet 34
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_surjective (L68-L69)
theorem toCotangent_surjective : Function.Surjective I.toCotangent := Submodule.mkQ_surjective _
```

-- snippet 35
```lean
-- Mathlib.RingTheory.Ideal.Basic.not_isField_iff_exists_ideal_bot_lt_and_lt_top (L276-L281)
theorem not_isField_iff_exists_ideal_bot_lt_and_lt_top [Nontrivial R] :
    ¬IsField R ↔ ∃ I : Ideal R, ⊥ < I ∧ I < ⊤ := by
refine ⟨fun h ↦ ?_, fun ⟨I, h_bot, h_top⟩ ↦ not_isField_of_ne_of_ne h_bot.ne' h_top.ne⟩
  obtain ⟨I, hI, hIm⟩ := exists_maximal_of_not_isField h
  exact ⟨I, bot_lt_iff_ne_bot.mpr hI, lt_top_iff_ne_top.mpr hIm.ne_top⟩
```

-- snippet 36
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_bot (L120-L129)
lemma colon_bot : N.colon ((⊥ : Submodule R M) : Set M) = ⊤ := by
simp

end Semiring

section CommSemiring

variable [CommSemiring R] [AddCommMonoid M] [Module R M]
variable {N N' : Submodule R M} {S : Set M}
```

-- snippet 37
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_finsetInf (L97-L100)
lemma colon_finsetInf {ι : Type*} (s : Finset ι) (f : ι → Submodule R M) :
    (s.inf f).colon S = s.inf (fun i ↦ (f i).colon S) := by
aesop (add simp mem_colon)
```

-- snippet 38
```lean
-- Mathlib.RingTheory.Ideal.BigOperators.sum_mem (L29-L33)
theorem sum_mem (I : Ideal α) {ι : Type*} {t : Finset ι} {f : ι → α} :
    (∀ c ∈ t, f c ∈ I) → (∑ i ∈ t, f i) ∈ I :=
Submodule.sum_mem I

end Ideal
```

-- snippet 39
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.preimage_comap_associatedPrimes_eq_associatedPrimes_of_isLocalizedModule (L122-L130)
lemma preimage_comap_associatedPrimes_eq_associatedPrimes_of_isLocalizedModule
    [IsNoetherianRing R] :
    (Ideal.comap (algebraMap R R')) ⁻¹' (associatedPrimes R M) = associatedPrimes R' M' := by
ext p
  exact ⟨mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule S f p,
    fun h ↦ comap_mem_associatedPrimes_of_mem_associatedPrimes_of_isLocalizedModule_of_fg S f p h
    ((isNoetherianRing_iff_ideal_fg R).mp ‹_› _)⟩

variable (R M) in
```

-- snippet 40
```lean
-- Mathlib.RingTheory.Ideal.Basic.span_range_pow_eq_top (L186-L195)
theorem span_range_pow_eq_top (s : Set α) (hs : span s = ⊤) (n : s → ℕ) :
    span (Set.range fun x ↦ x.1 ^ n x) = ⊤ := by
have ⟨t, hts, mem⟩ := Submodule.mem_span_finite_of_mem_span ((eq_top_iff_one _).mp hs)
  refine top_unique ((span_pow_eq_top _ ((eq_top_iff_one _).mpr mem) <|
    t.attach.sup fun x ↦ n ⟨x, hts x.2⟩).ge.trans <| span_le.mpr ?_)
  rintro _ ⟨x, hxt, rfl⟩
  rw [← Nat.sub_add_cancel (Finset.le_sup <| t.mem_attach ⟨x, hxt⟩)]
  simp_rw [pow_add]
  exact mul_mem_left _ _ (subset_span ⟨_, rfl⟩)
```

-- snippet 41
```lean
-- Mathlib.RingTheory.Ideal.Basic.pow_multiset_sum_mem_span_pow (L128-L154)
theorem pow_multiset_sum_mem_span_pow [DecidableEq α] (s : Multiset α) (n : ℕ) :
    s.sum ^ (Multiset.card s * n + 1) ∈
    span ((s.map fun (x : α) ↦ x ^ (n + 1)).toFinset : Set α) := by
induction s using Multiset.induction_on with
  | empty => simp
  | cons a s hs => ?_
  simp only [Finset.coe_insert, Multiset.map_cons, Multiset.toFinset_cons, Multiset.sum_cons,
    Multiset.card_cons, add_pow]
  refine Submodule.sum_mem _ ?_
  intro c _hc
  rw [mem_span_insert]
  by_cases! h : n + 1 ≤ c
  · refine ⟨a ^ (c - (n + 1)) * s.sum ^ ((Multiset.card s + 1) * n + 1 - c) *
      ((Multiset.card s + 1) * n + 1).choose c, 0, Submodule.zero_mem _, ?_⟩
    rw [mul_comm _ (a ^ (n + 1))]
    simp_rw [← mul_assoc]
    rw [← pow_add, add_zero, add_tsub_cancel_of_le h]
  · use 0
    simp_rw [zero_mul, zero_add]
    refine ⟨_, ?_, rfl⟩
    replace h : c ≤ n := Nat.lt_succ_iff.mp h
    have : (Multiset.card s + 1) * n + 1 - c = Multiset.card s * n + 1 + (n - c) := by
      rw [add_mul, one_mul, add_assoc, add_comm n 1, ← add_assoc, add_tsub_assoc_of_le h]
    rw [this, pow_add]
    simp_rw [mul_assoc, mul_comm (s.sum ^ (Multiset.card s * n + 1)), ← mul_assoc]
    exact mul_mem_left _ _ hs
```

-- snippet 42
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_eq_top_iff_subset (L85-L87)
lemma colon_eq_top_iff_subset (S : Set M) : N.colon S = ⊤ ↔ S ⊆ N := by
aesop (add simp [mem_colon, Ideal.eq_top_iff_one])
```

-- snippet 43
```lean
-- Mathlib.RingTheory.Ideal.Basic.span_single_eq_top (L203-L232)
lemma span_single_eq_top {ι : Type*} [DecidableEq ι] [Finite ι] (R : ι → Type*)
    [∀ i, Semiring (R i)] : Ideal.span (Set.range fun i ↦ (Pi.single i 1 : Π i, R i)) = ⊤ := by
rw [_root_.eq_top_iff]
  rintro x -
  induction x using Pi.single_induction with
  | zero => simp
  | add f g hf hg => exact Ideal.add_mem _ hf hg
  | single i r =>
      rw [show Pi.single i r = Pi.single i r * Pi.single i 1 by simp [← Pi.single_mul_left]]
      exact Ideal.mul_mem_left _ _ (Ideal.subset_span ⟨i, rfl⟩)

end Ideal

end CommSemiring

section DivisionSemiring

variable {K : Type*} [DivisionSemiring K] (I : Ideal K)

namespace Ideal

variable (K) in
/-- A bijection between (left) ideals of a division ring and `{0, 1}`, sending `⊥` to `0`
and `⊤` to `1`. -/
noncomputable def equivFinTwo : Ideal K ≃ Fin 2 where
  toFun := fun I ↦ if I = ⊥ then 0 else 1
  invFun := ![⊥, ⊤]
  left_inv := fun I ↦ by rcases eq_bot_or_top I with rfl | rfl <;> simp
  right_inv := fun i ↦ by fin_cases i <;> simp
```

-- snippet 44
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.IsAssociatedPrime (L98-L102)
def IsAssociatedPrime : Prop :=
(⊥ : Submodule R M).IsAssociatedPrime I

variable (R) in
/-- The set of associated primes of a module. -/
```

-- snippet 45
```lean
-- Mathlib.RingTheory.Ideal.Colon.bot_colon (L64-L67)
theorem bot_colon : colon (⊥ : Submodule R M) (N : Set M) = N.annihilator := by
ext x
  simp [mem_colon, mem_annihilator]
```

-- snippet 46
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.biUnion_associatedPrimes_eq_compl_nonZeroDivisors (L240-L246)
theorem biUnion_associatedPrimes_eq_compl_nonZeroDivisors [IsNoetherianRing R] :
    ⋃ p ∈ associatedPrimes R R, p = (nonZeroDivisors R : Set R)ᶜ :=
(biUnion_associatedPrimes_eq_zero_divisors R R).trans <| by
    ext; simp [← nonZeroDivisorsLeft_eq_nonZeroDivisors, and_comm]

variable {R M}
```

-- snippet 47
```lean
-- Mathlib.RingTheory.Ideal.Colon.top_colon (L102-L104)
lemma top_colon : (⊤ : Submodule R M).colon S = ⊤ := by
aesop (add simp mem_colon)
```

-- snippet 48
```lean
-- Mathlib.RingTheory.Ideal.Colon.iInf_colon (L93-L95)
lemma iInf_colon {ι : Sort*} (f : ι → Submodule R M) : (⨅ i, f i).colon S = ⨅ i, (f i).colon S := by
aesop (add simp mem_colon)
```

-- snippet 49
```lean
-- Mathlib.RingTheory.Ideal.Colon.inf_colon (L89-L91)
lemma inf_colon : (N₁ ⊓ N₂).colon S = N₁.colon S ⊓ N₂.colon S := by
aesop (add simp mem_colon)
```

-- snippet 50
```lean
-- Mathlib.RingTheory.Ideal.Basic.prod_mem (L196-L202)
theorem prod_mem {ι : Type*} {f : ι → α} {s : Finset ι}
    (I : Ideal α) {i : ι} (hi : i ∈ s) (hfi : f i ∈ I) :
    ∏ i ∈ s, f i ∈ I := by
classical
  rw [Finset.prod_eq_prod_diff_singleton_mul hi]
  exact Ideal.mul_mem_left _ _ hfi
```

-- snippet 51
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_eq_zero (L66-L67)
theorem toCotangent_eq_zero (x : I) : I.toCotangent x = 0 ↔ (x : R) ∈ I ^ 2 := I.mem_toCotangent_ker
```

-- snippet 52
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.LinearEquiv (L128-L132)
theorem LinearEquiv.isAssociatedPrime_iff (l : M ≃ₗ[R] M') :
    IsAssociatedPrime I M ↔ IsAssociatedPrime I M' :=
⟨fun h => h.map_of_injective l l.injective,
    fun h => h.map_of_injective l.symm l.symm.injective⟩
```

-- snippet 53
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Finiteness.associatedPrimes (L158-L169)
theorem associatedPrimes.finite : (associatedPrimes A M).Finite := by
induction ‹Module.Finite A M› using
    IsNoetherianRing.induction_on_isQuotientEquivQuotientPrime A with
  | subsingleton N => simp [associatedPrimes.eq_empty_of_subsingleton]
  | quotient N p f =>
    have := associatedPrimes.eq_singleton_of_isPrimary p.2.isPrimary
    simp [LinearEquiv.AssociatedPrimes.eq f, this]
  | exact N₁ N₂ N₃ f g hf _ hfg h₁ h₃ =>
    exact (h₁.union h₃).subset (associatedPrimes.subset_union_of_exact hf hfg)

/-- Every maximal ideal of a commutative Noetherian total ring of fractions `A` is
an associated prime of the `A`-module `A`. -/
```

-- snippet 54
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_add_pred_mem_of_pow_mem (L123-L127)
theorem add_pow_add_pred_mem_of_pow_mem {m n : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) :
    (a + b) ^ (m + n - 1) ∈ I :=
I.add_pow_add_pred_mem_of_pow_mem_of_commute ha hb (Commute.all ..)
```

-- snippet 55
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule (L40-L69)
lemma mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule
    (p : Ideal R') (ass : p.comap (algebraMap R R') ∈ associatedPrimes R M) :
    p ∈ associatedPrimes R' M' := by
rcases ass with ⟨hp, x, hx⟩
  constructor
  · refine (IsLocalization.isPrime_iff_isPrime_disjoint S _ _).mpr
      ⟨hp, (IsLocalization.disjoint_comap_iff S R' p).mpr ?_⟩
    by_contra eqtop
    simp [eqtop, Ideal.comap_top, Ideal.isPrime_iff] at hp
  · use f x
    ext t
    rcases IsLocalization.exists_mk'_eq S t with ⟨r, s, hrs⟩
    simp_rw [← hrs, Ideal.mem_radical_iff, mem_colon_singleton, ← IsLocalizedModule.mk'_one S f,
      ← IsLocalization.mk'_pow, IsLocalizedModule.mk'_smul_mk', mul_one, mem_bot,
      IsLocalizedModule.mk'_eq_zero']
    refine ⟨fun h ↦ exists_comm.mp ⟨1, ?_⟩, fun ⟨n, t, ht⟩ ↦ ?_⟩
    · simp only [← mem_colon_singleton, one_smul, ← mem_bot R, ← hx, ← Ideal.mem_radical_iff]
      exact hSR'.mk'_mem_iff.mp h
    · have : hSR'.mk' R' r s = hSR'.mk' R' (t.1 * r) 1 * hSR'.mk' R' 1 (t * s) := by
        rw [← hSR'.mk'_mul, mul_one, one_mul, ← sub_eq_zero, ← hSR'.mk'_sub, Submonoid.coe_mul]
        simp [← mul_assoc, mul_comm r t.1, IsLocalization.mk'_zero]
      rw [this]
      apply Ideal.IsTwoSided.mul_mem_of_left
      rw [IsLocalization.mk'_one, ← Ideal.mem_comap, hx]
      rcases eq_zero_or_pos n with rfl | hn
      · exact Ideal.IsTwoSided.mul_mem_of_left _ ⟨1, by simpa using ht⟩
      · use n
        rw [mem_colon_singleton, mul_pow, mul_smul, ← mem_colon_singleton]
        exact Ideal.pow_mem_of_mem _ (by simpa using ht) n hn
```

-- snippet 56
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.isAssociatedPrime_iff (L117-L120)
theorem isAssociatedPrime_iff [IsNoetherianRing R] :
    IsAssociatedPrime I M ↔ I.IsPrime ∧ ∃ x : M, I = colon ⊥ {x} :=
(⊥ : Submodule R M).isAssociatedPrime_iff
```

-- snippet 57
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_mem_of_pow_mem_of_le_of_commute (L83-L96)
theorem add_pow_mem_of_pow_mem_of_le_of_commute {m n k : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hk : m + n ≤ k + 1)
    (hab : Commute a b) :
    (a + b) ^ k ∈ I := by
simp_rw [hab.add_pow, ← Nat.cast_comm]
  apply I.sum_mem
  intro c _
  apply mul_mem_left
  by_cases h : m ≤ c
  · rw [hab.pow_pow]
    exact I.mul_mem_left _ (I.pow_mem_of_pow_mem ha h)
  · refine I.mul_mem_left _ (I.pow_mem_of_pow_mem hb ?_)
    lia
```

-- snippet 58
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_mem_of_pow_mem_of_le (L118-L122)
theorem add_pow_mem_of_pow_mem_of_le {m n k : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hk : m + n ≤ k + 1) :
    (a + b) ^ k ∈ I :=
I.add_pow_mem_of_pow_mem_of_le_of_commute ha hb hk (Commute.all ..)
```

-- snippet 59
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_singleton_zero (L117-L119)
lemma colon_singleton_zero : N.colon {0} = ⊤ := by
simp
```

-- snippet 60
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent (L51-L53)
def toCotangent : I →ₗ[R] I.Cotangent := Submodule.mkQ _
set_option backward.isDefEq.respectTransparency false in
```

-- snippet 61
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_union (L106-L108)
lemma colon_union : N.colon (S₁ ∪ S₂) = N.colon S₁ ⊓ N.colon S₂ := by
aesop (add simp mem_colon)
```

-- snippet 62
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon_singleton (L48-L50)
theorem mem_colon_singleton {x : M} {r : R} : r ∈ N.colon {x} ↔ r • x ∈ N := by
simp [mem_colon, forall_eq]
```

-- snippet 63
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.associatedPrimes (L103-L107)
def associatedPrimes : Set (Ideal R) :=
{ I | IsAssociatedPrime I M }

variable {I J M} {M' : Type*} [AddCommMonoid M'] [Module R M'] (f : M →ₗ[R] M')
```

-- snippet 64
```lean
-- Mathlib.RingTheory.Ideal.Basic.span_pow_eq_top (L161-L185)
theorem span_pow_eq_top (s : Set α) (hs : span s = ⊤) (n : ℕ) :
    span ((fun (x : α) => x ^ n) '' s) = ⊤ := by
rw [eq_top_iff_one]
  rcases n with - | n
  · obtain rfl | ⟨x, hx⟩ := eq_empty_or_nonempty s
    · rw [Set.image_empty, hs]
      trivial
    · exact subset_span ⟨_, hx, pow_zero _⟩
  rw [eq_top_iff_one, span, Finsupp.mem_span_iff_linearCombination] at hs
  rcases hs with ⟨f, hf⟩
  simp only [Finsupp.linearCombination, Finsupp.coe_lsum, Finsupp.sum, LinearMap.coe_smulRight,
    LinearMap.id_coe, id_eq, smul_eq_mul] at hf
  have := sum_pow_mem_span_pow f.support (fun a => f a * a) n
  rw [hf, one_pow] at this
  refine span_le.mpr ?_ this
  rintro _ hx
  simp_rw [Set.mem_image] at hx
  rcases hx with ⟨x, _, rfl⟩
  have : span ({(x : α) ^ (n + 1)} : Set α) ≤ span ((fun x : α => x ^ (n + 1)) '' s) := by
    rw [span_le, Set.singleton_subset_iff]
    exact subset_span ⟨x, x.prop, rfl⟩
  refine this ?_
  rw [mul_pow, mem_span_singleton]
  exact ⟨f x ^ (n + 1), mul_comm _ _⟩
```

-- snippet 65
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_iUnion (L110-L112)
lemma colon_iUnion {ι : Sort*} (f : ι → Set M) : N.colon (⋃ i, f i) = ⨅ i, N.colon (f i) := by
aesop (add simp mem_colon)
```

-- snippet 66
```lean
-- Mathlib.RingTheory.Ideal.Basic.isField_iff_isSimpleOrder_ideal (L292-L302)
theorem isField_iff_isSimpleOrder_ideal : IsField R ↔ IsSimpleOrder (Ideal R) := by
cases subsingleton_or_nontrivial R
  · exact
      ⟨fun h => (not_isField_of_subsingleton _ h).elim, fun h =>
        (false_of_nontrivial_of_subsingleton <| Ideal R).elim⟩
  rw [← not_iff_not, Ring.not_isField_iff_exists_ideal_bot_lt_and_lt_top]
  contrapose! +distrib
  simp_rw [not_lt_top_iff, not_bot_lt_iff]
  exact ⟨fun h => ⟨h⟩, fun h => h.2⟩

/-- When a ring is not a field, the maximal ideals are nontrivial. -/
```

-- snippet 67
```lean
-- Mathlib.RingTheory.Ideal.Basic.isSimpleOrder (L237-L249)
instance isSimpleOrder : IsSimpleOrder (Ideal K) :=
⟨eq_bot_or_top⟩

end Ideal

end DivisionSemiring

-- TODO: consider moving the lemmas below out of the `Ring` namespace since they are
-- about `CommSemiring`s.
namespace Ring

variable {R : Type*} [CommSemiring R]
```

-- snippet 68
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon (L45-L46)
theorem mem_colon {r} : r ∈ N.colon S ↔ ∀ s ∈ S, r • s ∈ N := Set.smul_set_subset_iff
```

-- snippet 69
```lean
-- Mathlib.RingTheory.Ideal.Basic.ne_bot_of_isMaximal_of_not_isField (L303-L314)
theorem ne_bot_of_isMaximal_of_not_isField [Nontrivial R] {M : Ideal R} (max : M.IsMaximal)
    (not_field : ¬IsField R) : M ≠ ⊥ := by
rintro rfl
  obtain ⟨I, hIbot, hItop⟩ := not_isField_iff_exists_ideal_bot_lt_and_lt_top.mp not_field
  exact hIbot.ne (max.eq_of_le hItop.ne bot_le)

end Ring

namespace Ideal

variable {R : Type*} [CommSemiring R] [Nontrivial R]
```

-- snippet 70
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.not_isAssociatedPrime_of_subsingleton (L133-L138)
theorem not_isAssociatedPrime_of_subsingleton [Subsingleton M] : ¬IsAssociatedPrime I M := by
rintro ⟨hI, x, hx⟩
  apply hI.ne_top
  simp [hx, Subsingleton.elim x 0]

variable (R) in
```

-- snippet 71
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.prod (L206-L211)
theorem prod : associatedPrimes R (M × M') = associatedPrimes R M ∪ associatedPrimes R M' :=
(subset_union_of_exact LinearMap.inl_injective .inl_snd).antisymm (Set.union_subset_iff.2
    ⟨subset_of_injective LinearMap.inl_injective, subset_of_injective LinearMap.inr_injective⟩)

end associatedPrimes
```

-- snippet 72
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Finiteness.Submodule (L51-L55)
def Submodule.IsQuotientEquivQuotientPrime (N₁ N₂ : Submodule A M) :=
N₁ ≤ N₂ ∧ ∃ (p : PrimeSpectrum A), Nonempty ((↥N₂ ⧸ N₁.submoduleOf N₂) ≃ₗ[A] A ⧸ p.1)

set_option backward.isDefEq.respectTransparency false in
open LinearMap in
```

-- snippet 73
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.Cotangent (L41-L49)
def Cotangent : Type _ := I ⧸ (I • ⊤ : Submodule R I)
deriving Inhabited, AddCommGroup, Module (R ⧸ I)

deriving instance Module S, IsScalarTower S S' for Cotangent I

variable [IsNoetherian R I] in
deriving instance IsNoetherian R for Cotangent I

/-- The quotient map from `I` to `I ⧸ I ^ 2`. -/
```

-- snippet 74
```lean
-- Mathlib.RingTheory.Ideal.Colon.annihilator_quotient (L175-L181)
theorem annihilator_quotient : Module.annihilator R (M ⧸ N) = N.colon Set.univ := by
ext r
  have htop : (⊤ : Submodule R (M ⧸ N)) = (⊤ : Submodule R M).map N.mkQ := by
    simpa [map_top] using (LinearMap.range_eq_top.mpr (mkQ_surjective N)).symm
  rw [← annihilator_top (R := R) (M := M ⧸ N), htop,
    annihilator_map_mkQ_eq_colon (N := N) (P := ⊤), Submodule.top_coe]
```

-- snippet 75
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_mono (L68-L70)
theorem colon_mono (hn : N₁ ≤ N₂) (hs : S₁ ⊆ S₂) : N₁.colon S₂ ≤ N₂.colon S₁ :=
fun _ hrns ↦ mem_colon.mpr fun s₁ hs₁ ↦ hn <| mem_colon.mp hrns s₁ <| hs hs₁
```

-- snippet 76
```lean
-- Mathlib.RingTheory.Ideal.Basis.constr_basisSpanSingleton (L46-L54)
theorem constr_basisSpanSingleton {N : Type*} [Semiring N] [Module N S] [SMulCommClass R N S]
    (b : Basis ι R S) {x : S} (hx : x ≠ 0) :
    (b.constr N).toFun (((↑) : _ → S) ∘ (basisSpanSingleton b hx)) = Algebra.lmul R S x :=
b.ext fun i => by simp

end Ideal

/-- If `I : Ideal S` has a basis over `R`,
`x ∈ I` iff it is a linear combination of basis vectors. -/
```

-- snippet 77
```lean
-- Mathlib.RingTheory.Ideal.Basic.exists_not_isUnit_of_not_isField (L250-L258)
theorem exists_not_isUnit_of_not_isField [Nontrivial R] (hf : ¬IsField R) :
    ∃ (x : R) (_hx : x ≠ (0 : R)), ¬IsUnit x := by
have : ¬_ := fun h => hf ⟨exists_pair_ne R, mul_comm, h⟩
  simp_rw [isUnit_iff_exists_inv]
  push Not at this ⊢
  obtain ⟨x, hx, not_unit⟩ := this
  exact ⟨x, hx, not_unit⟩

open Ideal in
```

-- snippet 78
```lean
-- Mathlib.RingTheory.Ideal.Basic.bot_lt_of_maximal (L315-L318)
theorem bot_lt_of_maximal (M : Ideal R) [hm : M.IsMaximal] (non_field : ¬IsField R) : ⊥ < M :=
(Ring.ne_bot_of_isMaximal_of_not_isField hm non_field).bot_lt

end Ideal
```

-- snippet 79
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon' (L131-L133)
theorem mem_colon' {r} : r ∈ N.colon S ↔ S ≤ comap (r • (LinearMap.id : M →ₗ[R] M)) N :=
mem_colon
```

-- snippet 80
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.minimalPrimes_annihilator_subset_associatedPrimes (L131-L150)
lemma minimalPrimes_annihilator_subset_associatedPrimes [IsNoetherianRing R] [Module.Finite R M] :
    (Module.annihilator R M).minimalPrimes ⊆ associatedPrimes R M := by
intro p hp
  have prime := hp.1.1
  let Rₚ := Localization.AtPrime p
  have : Nontrivial (LocalizedModule p.primeCompl M) := by
    simpa [← Module.mem_support_iff (p := ⟨p, prime⟩), Module.support_eq_zeroLocus] using hp.1.2
  rcases associatedPrimes.nonempty Rₚ (LocalizedModule p.primeCompl M) with ⟨q, hq⟩
  have q_prime : q.IsPrime := IsAssociatedPrime.isPrime hq
  simp only [← preimage_comap_associatedPrimes_eq_associatedPrimes_of_isLocalizedModule p.primeCompl
    Rₚ (LocalizedModule.mkLinearMap p.primeCompl M), Set.mem_preimage] at hq
  have ann_le : Module.annihilator R M ≤ Ideal.comap (algebraMap R Rₚ) q :=
    le_of_eq_of_le Submodule.annihilator_top.symm (IsAssociatedPrime.annihilator_le hq)
  have le : Ideal.comap (algebraMap R Rₚ) q ≤ p := by
    have := (IsLocalization.disjoint_comap_iff p.primeCompl Rₚ q).mpr q_prime.ne_top
    simpa only [Ideal.primeCompl, Submonoid.coe_set_mk, Subsemigroup.coe_set_mk,
      Set.disjoint_compl_left_iff_subset] using this
  simpa [Minimal.eq_of_le hp.out ⟨IsAssociatedPrime.isPrime hq, ann_le⟩ le] using hq

end Module.associatedPrimes
```

-- snippet 81
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon_span_singleton (L154-L156)
theorem mem_colon_span_singleton {x : M} {r : R} : r ∈ N.colon (span R {x}) ↔ r • x ∈ N := by
simp
```

-- snippet 82
```lean
-- Mathlib.RingTheory.Ideal.Basic.mem_pi (L56-L58)
theorem mem_pi (r : Π i, R i) : r ∈ pi I ↔ ∀ i, r i ∈ I i :=
Iff.rfl
```

-- snippet 83
```lean
-- Mathlib.RingTheory.Ideal.Colon.iInf_colon_iUnion (L74-L77)
theorem iInf_colon_iUnion (ι₁ : Sort*) (f : ι₁ → Submodule R M) (ι₂ : Sort*) (g : ι₂ → Set M) :
    (⨅ i, f i).colon (⋃ j, g j) = ⨅ (i) (j), (f i).colon (g j) := by
aesop (add simp mem_colon)
```

-- snippet 84
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_eq (L62-L65)
theorem toCotangent_eq {x y : I} : I.toCotangent x = I.toCotangent y ↔ (x - y : R) ∈ I ^ 2 := by
rw [← sub_eq_zero]
  exact I.mem_toCotangent_ker
```

-- snippet 85
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Finiteness.IsNoetherianRing (L92-L117)
theorem IsNoetherianRing.exists_relSeries_isQuotientEquivQuotientPrime :
    ∃ s : RelSeries {(N₁, N₂) | Submodule.IsQuotientEquivQuotientPrime (A := A) (M := M) N₁ N₂},
s.head = ⊥ ∧ s.last = ⊤ := by
  refine WellFoundedGT.induction_top ⟨⊥, .singleton _ ⊥, rfl, rfl⟩ ?_
  rintro N hN ⟨s, hs₁, hs₂⟩
  have := Submodule.Quotient.nontrivial_iff.mpr hN
  obtain ⟨p, hp⟩ := associatedPrimes.nonempty A (M ⧸ N)
  rw [AssociatedPrimes.mem_iff, isAssociatedPrime_iff] at hp
  obtain ⟨hp, x, rfl⟩ := hp
  obtain ⟨x, rfl⟩ := Submodule.mkQ_surjective _ x
  have hxN : x ∉ N := fun h ↦ hp.ne_top (by rw [show N.mkQ x = 0 by simpa]; simp)
  have := Submodule.isQuotientEquivQuotientPrime_iff.mpr ⟨x, hp, rfl⟩
  refine ⟨_, by simpa [hs₂], s.snoc _ (hs₂ ▸ this), by simpa, rfl⟩

/-- If a property on finitely generated modules over a Noetherian ring satisfies that:

- it holds for zero module (it's formalized as it holds for any module which is subsingleton),
- it holds for `A ⧸ p` for every prime ideal `p` of `A` (to avoid universe problem,
  it's formalized as it holds for any module isomorphic to `A ⧸ p`),
- it is stable by short exact sequences,

then the property holds for every finitely generated modules.

NOTE: This should be the induction principle for `M`, but due to the bug
https://github.com/leanprover/lean4/issues/4246
currently it is induction for `Module.Finite A M`. -/
```

-- snippet 86
```lean
-- Mathlib.RingTheory.Ideal.Basic.not_isField_of_ne_of_ne (L271-L275)
theorem not_isField_of_ne_of_ne [Nontrivial R] {I : Ideal R} (h_bot : I ≠ ⊥) (h_top : I ≠ ⊤) :
    ¬ IsField R := by
contrapose! h_bot
  exact ((isField_iff_maximal_bot.mp h_bot).eq_of_le h_top bot_le).symm
```

<s>[SYSTEM_PROMPT]You are Leanstral, a Lean 4 proof assistant. Complete the task below using the provided Lean source context. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.RingTheory.Ideal.KrullsHeightTheorem).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]Prove the following theorem. The retrieved context below may help.

```lean
lemma Ideal.height_le_ringKrullDim_quotient_add_one {r : R} {p : Ideal R} [p.IsPrime]
    (hrp : r ∈ p) : p.height ≤ ringKrullDim (R ⧸ span {r}) + 1
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.IsLocalRing (L48-L58)
lemma IsLocalRing.quotient_artinian_of_mem_minimalPrimes_of_isLocalRing
    [IsLocalRing R] (I : Ideal R) (hp : IsLocalRing.maximalIdeal R ∈ I.minimalPrimes) :
    IsArtinianRing (R ⧸ I) :=
have : Ring.KrullDimLE 0 (R ⧸ I) := Ring.krullDimLE_zero_iff.mpr fun J prime ↦
    Ideal.isMaximal_of_isIntegral_of_isMaximal_comap _ <| by
      convert IsLocalRing.maximalIdeal.isMaximal R
      rw [Ideal.minimalPrimes, Set.mem_setOf] at hp
      have := prime.comap (Ideal.Quotient.mk I)
      exact hp.eq_of_le ⟨this, .trans (by simp) (Ideal.ker_le_comap _)⟩ (le_maximalIdeal this.1)
  IsNoetherianRing.isArtinianRing_of_krullDimLE_zero
```

-- snippet 2
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.height_le_ringKrullDim_quotient_add_spanFinrank (L343-L351)
lemma height_le_ringKrullDim_quotient_add_spanFinrank {p I : Ideal R} [p.IsPrime] (h : I ≤ p) :
    p.height ≤ ringKrullDim (R ⧸ I) + I.spanFinrank := by
trans (p.map (Ideal.Quotient.mk I)).height + I.spanFinrank
  · norm_cast; exact Ideal.height_le_height_add_spanFinrank_of_le h
  · gcongr
    have : (Ideal.map (Ideal.Quotient.mk I) p).IsPrime :=
      Ideal.isPrime_map_quotientMk_of_isPrime h
    exact Ideal.height_le_ringKrullDim_of_ne_top Ideal.IsPrime.ne_top'
```

-- snippet 3
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_ringKrullDim_quotient_add_spanFinrank (L352-L362)
lemma ringKrullDim_le_ringKrullDim_quotient_add_spanFinrank (I : Ideal R)
    (h : I ≤ Ring.jacobson R) :
    ringKrullDim R ≤ ringKrullDim (R ⧸ I) + I.spanFinrank := by
nontriviality R
  rw [ringKrullDim_le_iff_isMaximal_height_le]
  intro m hm
  exact height_le_ringKrullDim_quotient_add_spanFinrank <|
    le_trans h <| Ring.jacobson_le_of_isMaximal m

/-- If `p` is a prime ideal containing `s`, the height of `p` is bounded
by the sum of the height of the image of `p` in `R ⧸ (s)` and the cardinality of `s`. -/
```

-- snippet 4
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_ringKrullDim_quotient_add_encard (L386-L391)
lemma ringKrullDim_le_ringKrullDim_quotient_add_encard (s : Set R) (hs : s ⊆ Ring.jacobson R) :
    ringKrullDim R ≤ ringKrullDim (R ⧸ Ideal.span s) + s.encard := by
refine le_trans (ringKrullDim_le_ringKrullDim_quotient_add_spanFinrank (Ideal.span s) ?_) ?_
  · simpa [Ideal.span_le]
  · gcongr; norm_cast; exact Submodule.spanFinrank_span_le_encard _
```

-- snippet 5
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_ringKrullDim_quotient_add_card (L392-L407)
lemma ringKrullDim_le_ringKrullDim_quotient_add_card (s : Finset R)
    (hs : (s : Set R) ⊆ Ring.jacobson R) :
    ringKrullDim R ≤ ringKrullDim (R ⧸ Ideal.span (s : Set R)) + s.card := by
convert ringKrullDim_le_ringKrullDim_quotient_add_encard s hs
  norm_cast

section Algebra

variable {S : Type*} [CommRing S] [Algebra R S]

/--
If `P` lies over `p`, the height of `P` is bounded by the height of `p` plus
the height of the image of `P` in `S ⧸ p S`.
Equality holds if `S` satisfies going-down as an `R`-algebra
(see `Ideal.height_eq_height_add_of_liesOver_of_hasGoingDown`).
-/
```

-- snippet 6
```lean
-- Mathlib.RingTheory.Ideal.KrullsHeightTheorem.ringKrullDim_le_spanFinrank_maximalIdeal (L474-L480)
lemma ringKrullDim_le_spanFinrank_maximalIdeal [IsLocalRing R] :
    ringKrullDim R ≤ (IsLocalRing.maximalIdeal R).spanFinrank :=
le_of_eq_of_le IsLocalRing.maximalIdeal_height_eq_ringKrullDim.symm
    (WithBot.coe_le_coe.mpr (Ideal.height_le_spanFinrank (IsLocalRing.maximalIdeal R)
      Ideal.IsPrime.ne_top'))

end Algebra
```

-- snippet 7
```lean
-- Mathlib.RingTheory.Ideal.Basic.single_mem_pi (L67-L72)
theorem single_mem_pi [DecidableEq ι] {i : ι} {r : R i} (hr : r ∈ I i) : Pi.single i r ∈ pi I := by
intro j
  obtain rfl | ne := eq_or_ne i j
  · simpa
  · simp [ne]
```

-- snippet 8
```lean
-- Mathlib.RingTheory.Ideal.Colon.bot_colon' (L138-L140)
theorem bot_colon' : (⊥ : Submodule R M).colon S = (span R S).annihilator := by
aesop (add simp [mem_colon, mem_annihilator_span])
```

-- snippet 9
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.AssociatedPrimes (L108-L109)
theorem AssociatedPrimes.mem_iff : I ∈ associatedPrimes R M ↔ IsAssociatedPrime I M := Iff.rfl
```

-- snippet 10
```lean
-- Mathlib.RingTheory.Ideal.Basic.isField_iff_maximal_bot (L259-L264)
theorem isField_iff_maximal_bot [Nontrivial R] : IsField R ↔ (⊥ : Ideal R).IsMaximal := by
refine ⟨fun h ↦ let := h.toSemifield; bot_isMaximal, fun hmax ↦ ?_⟩
  by_contra hf
  obtain ⟨x, hx0, hxu⟩ := exists_not_isUnit_of_not_isField hf
  exact hx0 <| span_singleton_eq_bot.mp (hmax.eq_of_le (span_singleton_ne_top hxu) bot_le).symm
```

-- snippet 11
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.subset_union_of_exact (L171-L204)
theorem subset_union_of_exact (hf : Function.Injective f) (hfg : Function.Exact f g) :
    associatedPrimes R M' ⊆ associatedPrimes R M ∪ associatedPrimes R M'' := by
rintro p ⟨_, x, hx⟩
  by_cases! h : ∃ a ∈ p.primeCompl, ∃ y : M, ∃ k, f y = a ^ k • x
  · obtain ⟨a, ha, y, k, h⟩ := h
    left
    refine ⟨‹_›, y, le_antisymm (fun b hb ↦ ?_) (fun b ⟨n, hb⟩ ↦ ?_)⟩
    · rw [hx] at hb
      obtain ⟨n, hb⟩ := hb
      use n
      rw [mem_colon_singleton, mem_bot] at hb ⊢
      apply_fun _ using hf
      rw [map_smul, h, smul_comm, hb, smul_zero, map_zero]
    · rw [mem_colon_singleton, mem_bot] at hb
      apply_fun f at hb
      rw [map_smul, map_zero, h, ← mul_smul, ← mem_bot R, ← mem_colon_singleton] at hb
      replace hb := hx.ge (Ideal.le_radical hb)
      contrapose hb
      exact p.primeCompl.mul_mem (p.primeCompl.pow_mem hb n) (p.primeCompl.pow_mem ha k)
  · right
    refine ⟨‹_›, g x, le_antisymm (fun b hb ↦ ?_) (fun b ⟨n, hb⟩ ↦ ?_)⟩
    · rw [hx] at hb
      refine Ideal.radical_mono (fun b hb ↦ ?_) hb
      rw [mem_colon_singleton, mem_bot] at hb ⊢
      rw [← map_smul, hb, map_zero]
    · rw [mem_colon_singleton, mem_bot, ← map_smul, ← LinearMap.mem_ker,
        hfg.linearMap_ker_eq] at hb
      obtain ⟨y, hy⟩ := hb
      by_contra H
      exact h b H y n hy

variable (R M M') in
/-- The set of associated primes of the product of two modules is equal to
the union of those of the two modules. -/
```

-- snippet 12
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_empty (L114-L116)
lemma colon_empty : N.colon (∅ : Set M) = ⊤ := by
aesop (add simp mem_colon)
```

-- snippet 13
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.map_toCotangent_ker (L54-L57)
theorem map_toCotangent_ker : (LinearMap.ker I.toCotangent).map I.subtype = I ^ 2 := by
rw [Ideal.toCotangent, Submodule.ker_mkQ, pow_two, Submodule.map_smul'' I ⊤ (Submodule.subtype I),
    smul_eq_mul, Submodule.map_subtype_top]
```

-- snippet 14
```lean
-- Mathlib.RingTheory.Ideal.Basic.exists_maximal_of_not_isField (L265-L270)
theorem exists_maximal_of_not_isField [Nontrivial R] (h : ¬ IsField R) :
    ∃ p : Ideal R, p ≠ ⊥ ∧ p.IsMaximal := by
contrapose! h
  simp only [← bot_lt_iff_ne_bot] at h
  refine isField_iff_maximal_bot.mpr ⟨⟨bot_ne_top, Ideal.maximal_of_no_maximal h⟩⟩
```

-- snippet 15
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_span (L142-L149)
theorem colon_span : N.colon (span R S) = N.colon S := by
refine (colon_mono le_rfl subset_span).antisymm fun r h ↦ mem_colon.mpr fun s hs ↦ ?_
  induction hs using Submodule.span_induction with
  | mem => aesop (add simp mem_colon)
  | zero => simp
  | add => aesop
  | smul => simp_all [smul_mem, smul_comm r]
```

-- snippet 16
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_add_pred_mem_of_pow_mem_of_commute (L97-L117)
theorem add_pow_add_pred_mem_of_pow_mem_of_commute {m n : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hab : Commute a b) :
    (a + b) ^ (m + n - 1) ∈ I :=
I.add_pow_mem_of_pow_mem_of_le_of_commute ha hb (by rw [← Nat.sub_le_iff_le_add]) hab

end Commute

end Ideal

end Semiring

section CommSemiring

variable {a b : α}

-- A separate namespace definition is needed because the variables were historically in a different
-- order.
namespace Ideal

variable [CommSemiring α] (I : Ideal α)
```

-- snippet 17
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.biUnion_associatedPrimes_eq_zero_divisors (L229-L239)
theorem biUnion_associatedPrimes_eq_zero_divisors [IsNoetherianRing R] :
    ⋃ p ∈ associatedPrimes R M, p = { r : R | ∃ x : M, x ≠ 0 ∧ r • x = 0 } := by
simp only [AssociatedPrimes.mem_iff, isAssociatedPrime_iff]
  refine subset_antisymm (Set.iUnion₂_subset ?_) ?_
  · rintro _ ⟨h, x, ⟨⟩⟩ r h'
    exact ⟨x, by simpa using h.ne_top, by simpa using h'⟩
  · intro r ⟨x, h, h'⟩
    obtain ⟨P, hP, hx⟩ := exists_le_isAssociatedPrime_of_isNoetherianRing R x h
    rw [isAssociatedPrime_iff] at hP
    exact Set.mem_iUnion₂_of_mem hP (hx (by rwa [mem_colon_singleton]))
```

-- snippet 18
```lean
-- Mathlib.RingTheory.Ideal.Colon.annihilator_map_mkQ_eq_colon (L169-L174)
lemma annihilator_map_mkQ_eq_colon : annihilator (P.map N.mkQ) = N.colon (P : Set M) := by
ext
  rw [mem_annihilator, mem_colon]
  exact ⟨fun H p hp ↦ (Quotient.mk_eq_zero N).1 (H (Quotient.mk p) (mem_map_of_mem hp)),
    fun H _ ⟨p, hp, hpm⟩ ↦ hpm ▸ ((Quotient.mk_eq_zero N).2 <| H p hp)⟩
```

-- snippet 19
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon (L35-L44)
def colon (N : Submodule R M) (S : Set M) : Ideal R where
  carrier := {r : R | (r • S : Set M) ⊆ N}
add_mem' ha hb :=
    (Set.add_smul_subset _ _ _).trans ((Set.add_subset_add ha hb).trans_eq (by simp))
  zero_mem' := (Set.zero_smul_set_subset S).trans (by simp)
  smul_mem' r := by
    simp only [Set.mem_setOf_eq, smul_eq_mul, mul_smul, Set.smul_set_subset_iff]
    intro x hx y hy
    exact N.smul_mem _ (hx hy)
```

-- snippet 20
```lean
-- Mathlib.RingTheory.Ideal.Basis.Basis (L55-L61)
theorem Basis.mem_ideal_iff {ι R S : Type*} [CommSemiring R] [Semiring S] [Algebra R S]
    {I : Ideal S} (b : Basis ι R I) {x : S} :
    x ∈ I ↔ ∃ c : ι →₀ R, x = Finsupp.sum c fun i x => x • (b i : S) :=
(b.map ((I.restrictScalarsEquiv R _ _).restrictScalars R).symm).mem_submodule_iff

/-- If `I : Ideal S` has a finite basis over `R`,
`x ∈ I` iff it is a linear combination of basis vectors. -/
```

-- snippet 21
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.comap_mem_associatedPrimes_of_mem_associatedPrimes_of_isLocalizedModule_of_fg (L84-L120)
lemma comap_mem_associatedPrimes_of_mem_associatedPrimes_of_isLocalizedModule_of_fg (p : Ideal R')
    (ass : p ∈ associatedPrimes R' M') (fg : (p.comap (algebraMap R R')).FG) :
    p.comap (algebraMap R R') ∈ associatedPrimes R M := by
rcases ass with ⟨hp, x, hx⟩
  rcases fg with ⟨T, hT⟩
  rcases IsLocalizedModule.mk'_surjective S f x with ⟨⟨m, s⟩, rfl⟩
  simp only [Function.uncurry_apply_pair] at hx
  have mem (a : T) : algebraMap R R' a ∈ p := by
    simpa [← Ideal.mem_comap, ← hT] using Ideal.subset_span a.2
  simp only [hx, Ideal.mem_radical_iff, mem_bot, mem_colon_singleton, ← map_pow, algebraMap_smul,
    ← IsLocalizedModule.mk'_smul, IsLocalizedModule.mk'_eq_zero' f] at mem
  choose e g hg using mem
  refine ⟨.under R p, (∏ a, g a).1 • m, le_antisymm ?_ fun r hr ↦ ?_⟩
  · rw [← hT, Ideal.span_le]
    intro a ha
    simp only [SetLike.mem_coe, Ideal.mem_radical_iff, mem_bot, mem_colon_singleton]
    obtain ⟨u, hu⟩ : g ⟨a, ha⟩ ∣ (∏ a, g a) := by
      apply Finset.dvd_prod_of_mem g (Finset.mem_univ ⟨a, ha⟩)
    use e ⟨a, ha⟩
    rw [hu, Submonoid.coe_mul, smul_smul, ← mul_assoc, mul_comm, ← smul_smul, mul_comm, ← smul_smul]
    exact smul_eq_zero_of_right u.1 (hg ⟨a, ha⟩)
  · simp only [Ideal.mem_radical_iff, mem_bot, mem_colon_singleton, smul_smul] at hr
    obtain ⟨k, hk⟩ := hr
    have mem : r ^ k * (∏ a, g a).1 ∈ Ideal.comap (algebraMap R R') p := by
      rw [hx]
      use 1
      simp_rw [pow_one, mem_colon_singleton, algebraMap_smul, ← IsLocalizedModule.mk'_smul,
        hk, IsLocalizedModule.mk'_zero, mem_bot]
    apply hp.mem_of_pow_mem k
    rw [← map_pow]
    exact ((hp.under R).mul_mem_iff_mem_or_mem.mp mem).resolve_right
      (Set.disjoint_left.mp ((IsLocalization.disjoint_comap_iff S R' p).mpr hp.1) (∏ a, g a).2)

variable (R')

include S f in
open Set in
```

-- snippet 22
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_univ (L57-L60)
theorem colon_univ {I : Ideal R} [I.IsTwoSided] : I.colon Set.univ = I := by
simp_rw [SetLike.ext_iff, mem_colon, smul_eq_mul]
  exact fun x ↦ ⟨fun h ↦ mul_one x ▸ h 1 trivial, fun h _ _ ↦ I.mul_mem_right _ h⟩
```

-- snippet 23
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_inf_eq_left_of_subset (L81-L83)
lemma colon_inf_eq_left_of_subset (h : S ⊆ (N₂ : Set M)) : (N₁ ⊓ N₂).colon S = N₁.colon S := by
aesop (add simp mem_colon)
```

-- snippet 24
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.exists_le_isAssociatedPrime_of_isNoetherianRing (L139-L163)
theorem exists_le_isAssociatedPrime_of_isNoetherianRing [H : IsNoetherianRing R] (x : M)
    (hx : x ≠ 0) : ∃ P : Ideal R, IsAssociatedPrime P M ∧ (⊥ : Submodule R M).colon {x} ≤ P := by
simp only [isAssociatedPrime_iff]
  obtain ⟨P, ⟨l, h₁, y, rfl⟩, h₃⟩ :=
    set_has_maximal_iff_noetherian.mpr H
      { P | (⊥ : Submodule R M).colon {x} ≤ P ∧ P ≠ ⊤ ∧ ∃ y : M, P = (⊥ : Submodule R M).colon {y} }
      ⟨_, rfl.le, by simpa, x, rfl⟩
  refine ⟨_, ⟨⟨h₁, ?_⟩, y, rfl⟩, l⟩
  intro a b hab
  rw [or_iff_not_imp_left]
  intro ha
  rw [mem_colon_singleton] at ha hab
  have H₁ : (⊥ : Submodule R M).colon {y} ≤ (⊥ : Submodule R M).colon {a • y} := by
    intro c hc
    rw [mem_colon_singleton, mem_bot] at hc ⊢
    rw [smul_comm, hc, smul_zero]
  rwa [H₁.eq_of_not_lt (h₃ _ ⟨l.trans H₁, by simpa, _, rfl⟩),
    mem_colon_singleton, smul_comm, smul_smul]

namespace associatedPrimes

variable {f} {M'' : Type*} [AddCommMonoid M''] [Module R M''] {g : M' →ₗ[R] M''}

/-- If `M → M'` is injective, then the set of associated primes of `M` is
contained in that of `M'`. -/
```

-- snippet 25
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.mem_associatedPrimes_atPrime_of_mem_associatedPrimes (L70-L77)
lemma mem_associatedPrimes_atPrime_of_mem_associatedPrimes
    {p : Ideal R} [p.IsPrime] (ass : p ∈ associatedPrimes R M) :
    maximalIdeal (Localization.AtPrime p) ∈
    associatedPrimes (Localization.AtPrime p) (LocalizedModule.AtPrime p M) := by
apply mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule
    p.primeCompl (LocalizedModule.mkLinearMap p.primeCompl M)
  simpa [Localization.AtPrime.comap_maximalIdeal] using ass
```

-- snippet 26
```lean
-- Mathlib.RingTheory.Ideal.Basic.sum_pow_mem_span_pow (L155-L160)
theorem sum_pow_mem_span_pow {ι} (s : Finset ι) (f : ι → α) (n : ℕ) :
    (∑ i ∈ s, f i) ^ (s.card * n + 1) ∈ span ((fun i => f i ^ (n + 1)) '' s) := by
classical
  simpa only [Multiset.card_map, Multiset.map_map, comp_apply, Multiset.toFinset_map,
    Finset.coe_image, Finset.val_toFinset] using pow_multiset_sum_mem_span_pow (s.1.map f) n
```

-- snippet 27
```lean
-- Mathlib.RingTheory.Ideal.Basic.not_isField_iff_exists_prime (L282-L291)
theorem not_isField_iff_exists_prime [Nontrivial R] :
    ¬IsField R ↔ ∃ p : Ideal R, p ≠ ⊥ ∧ p.IsPrime := by
refine ⟨fun h ↦ ?_, fun ⟨I, h_bot, h_top⟩ ↦ not_isField_of_ne_of_ne h_bot h_top.ne_top⟩
  obtain ⟨I, hI, hIm⟩ := exists_maximal_of_not_isField h
  exact ⟨I, hI, hIm.isPrime⟩

/-- Also see `Ideal.isSimpleOrder` for the forward direction as an instance when `R` is a
division (semi)ring.

This result actually holds for all division semirings, but we lack the predicate to state it. -/
```

-- snippet 28
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.isAssociatedPrime_iff_exists_injective_linearMap (L257-L267)
theorem isAssociatedPrime_iff_exists_injective_linearMap [IsNoetherianRing R] :
    IsAssociatedPrime I M ↔ I.IsPrime ∧ ∃ (f : R ⧸ I →ₗ[R] M), Function.Injective f := by
rw [isAssociatedPrime_iff, and_congr_right_iff]
  refine fun _ ↦ ⟨fun ⟨x, h⟩ ↦ ?_, fun ⟨f, h⟩ ↦ ⟨(f ∘ₗ mkQ I) 1, ?_⟩⟩
  · replace h : I = ker (toSpanSingleton R M x) := by simp [h, SetLike.ext_iff]
    exact ⟨liftQ _ _ h.le, ker_eq_bot.mp (ker_liftQ_eq_bot' _ _ h)⟩
  · conv_lhs => rw [← I.ker_mkQ, ← ker_comp_of_ker_eq_bot (mkQ I) (ker_eq_bot_of_injective h)]
    simp [SetLike.ext_iff, ← Ideal.Quotient.algebraMap_eq, Algebra.algebraMap_eq_smul_one]

variable {I J M}
```

-- snippet 29
```lean
-- Mathlib.RingTheory.Ideal.Basis.basisSpanSingleton_apply (L39-L44)
theorem basisSpanSingleton_apply (b : Basis ι R S) {x : S} (hx : x ≠ 0) (i : ι) :
    (basisSpanSingleton b hx i : S) = x * b i := by
simp only [basisSpanSingleton, Basis.map_apply, LinearEquiv.trans_apply,
    Submodule.restrictScalarsEquiv_apply, LinearEquiv.ofInjective_apply, LinearEquiv.coe_ofEq_apply,
    LinearEquiv.restrictScalars_apply, LinearMap.mulLeft_apply]
```

-- snippet 30
```lean
-- Mathlib.RingTheory.Ideal.Basic.pi (L50-L55)
def pi : Ideal (Π i, R i) where
  carrier := { r | ∀ i, r i ∈ I i }
zero_mem' i := (I i).zero_mem
  add_mem' ha hb i := (I i).add_mem (ha i) (hb i)
  smul_mem' a _b hb i := (I i).mul_mem_left (a i) (hb i)
```

-- snippet 31
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.subset_of_injective (L165-L169)
theorem subset_of_injective (hf : Function.Injective f) :
    associatedPrimes R M ⊆ associatedPrimes R M' := fun _I h => h.map_of_injective f hf
/-- If `0 → M → M' → M''` is an exact sequence, then the set of associated primes of `M'` is
contained in the union of those of `M` and `M''`. -/
```

-- snippet 32
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.mem_toCotangent_ker (L58-L61)
theorem mem_toCotangent_ker {x : I} : x ∈ LinearMap.ker I.toCotangent ↔ (x : R) ∈ I ^ 2 := by
rw [← I.map_toCotangent_ker]
  simp
```

-- snippet 33
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon_iff_le (L134-L137)
theorem mem_colon_iff_le {r} : r ∈ N.colon N' ↔ r • N' ≤ N := by
aesop (add simp SetLike.coe_subset_coe)

/-- A variant for arbitrary sets in commutative semirings -/
```

-- snippet 34
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_surjective (L68-L69)
theorem toCotangent_surjective : Function.Surjective I.toCotangent := Submodule.mkQ_surjective _
```

-- snippet 35
```lean
-- Mathlib.RingTheory.Ideal.Basic.not_isField_iff_exists_ideal_bot_lt_and_lt_top (L276-L281)
theorem not_isField_iff_exists_ideal_bot_lt_and_lt_top [Nontrivial R] :
    ¬IsField R ↔ ∃ I : Ideal R, ⊥ < I ∧ I < ⊤ := by
refine ⟨fun h ↦ ?_, fun ⟨I, h_bot, h_top⟩ ↦ not_isField_of_ne_of_ne h_bot.ne' h_top.ne⟩
  obtain ⟨I, hI, hIm⟩ := exists_maximal_of_not_isField h
  exact ⟨I, bot_lt_iff_ne_bot.mpr hI, lt_top_iff_ne_top.mpr hIm.ne_top⟩
```

-- snippet 36
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_bot (L120-L129)
lemma colon_bot : N.colon ((⊥ : Submodule R M) : Set M) = ⊤ := by
simp

end Semiring

section CommSemiring

variable [CommSemiring R] [AddCommMonoid M] [Module R M]
variable {N N' : Submodule R M} {S : Set M}
```

-- snippet 37
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_finsetInf (L97-L100)
lemma colon_finsetInf {ι : Type*} (s : Finset ι) (f : ι → Submodule R M) :
    (s.inf f).colon S = s.inf (fun i ↦ (f i).colon S) := by
aesop (add simp mem_colon)
```

-- snippet 38
```lean
-- Mathlib.RingTheory.Ideal.BigOperators.sum_mem (L29-L33)
theorem sum_mem (I : Ideal α) {ι : Type*} {t : Finset ι} {f : ι → α} :
    (∀ c ∈ t, f c ∈ I) → (∑ i ∈ t, f i) ∈ I :=
Submodule.sum_mem I

end Ideal
```

-- snippet 39
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.preimage_comap_associatedPrimes_eq_associatedPrimes_of_isLocalizedModule (L122-L130)
lemma preimage_comap_associatedPrimes_eq_associatedPrimes_of_isLocalizedModule
    [IsNoetherianRing R] :
    (Ideal.comap (algebraMap R R')) ⁻¹' (associatedPrimes R M) = associatedPrimes R' M' := by
ext p
  exact ⟨mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule S f p,
    fun h ↦ comap_mem_associatedPrimes_of_mem_associatedPrimes_of_isLocalizedModule_of_fg S f p h
    ((isNoetherianRing_iff_ideal_fg R).mp ‹_› _)⟩

variable (R M) in
```

-- snippet 40
```lean
-- Mathlib.RingTheory.Ideal.Basic.span_range_pow_eq_top (L186-L195)
theorem span_range_pow_eq_top (s : Set α) (hs : span s = ⊤) (n : s → ℕ) :
    span (Set.range fun x ↦ x.1 ^ n x) = ⊤ := by
have ⟨t, hts, mem⟩ := Submodule.mem_span_finite_of_mem_span ((eq_top_iff_one _).mp hs)
  refine top_unique ((span_pow_eq_top _ ((eq_top_iff_one _).mpr mem) <|
    t.attach.sup fun x ↦ n ⟨x, hts x.2⟩).ge.trans <| span_le.mpr ?_)
  rintro _ ⟨x, hxt, rfl⟩
  rw [← Nat.sub_add_cancel (Finset.le_sup <| t.mem_attach ⟨x, hxt⟩)]
  simp_rw [pow_add]
  exact mul_mem_left _ _ (subset_span ⟨_, rfl⟩)
```

-- snippet 41
```lean
-- Mathlib.RingTheory.Ideal.Basic.pow_multiset_sum_mem_span_pow (L128-L154)
theorem pow_multiset_sum_mem_span_pow [DecidableEq α] (s : Multiset α) (n : ℕ) :
    s.sum ^ (Multiset.card s * n + 1) ∈
    span ((s.map fun (x : α) ↦ x ^ (n + 1)).toFinset : Set α) := by
induction s using Multiset.induction_on with
  | empty => simp
  | cons a s hs => ?_
  simp only [Finset.coe_insert, Multiset.map_cons, Multiset.toFinset_cons, Multiset.sum_cons,
    Multiset.card_cons, add_pow]
  refine Submodule.sum_mem _ ?_
  intro c _hc
  rw [mem_span_insert]
  by_cases! h : n + 1 ≤ c
  · refine ⟨a ^ (c - (n + 1)) * s.sum ^ ((Multiset.card s + 1) * n + 1 - c) *
      ((Multiset.card s + 1) * n + 1).choose c, 0, Submodule.zero_mem _, ?_⟩
    rw [mul_comm _ (a ^ (n + 1))]
    simp_rw [← mul_assoc]
    rw [← pow_add, add_zero, add_tsub_cancel_of_le h]
  · use 0
    simp_rw [zero_mul, zero_add]
    refine ⟨_, ?_, rfl⟩
    replace h : c ≤ n := Nat.lt_succ_iff.mp h
    have : (Multiset.card s + 1) * n + 1 - c = Multiset.card s * n + 1 + (n - c) := by
      rw [add_mul, one_mul, add_assoc, add_comm n 1, ← add_assoc, add_tsub_assoc_of_le h]
    rw [this, pow_add]
    simp_rw [mul_assoc, mul_comm (s.sum ^ (Multiset.card s * n + 1)), ← mul_assoc]
    exact mul_mem_left _ _ hs
```

-- snippet 42
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_eq_top_iff_subset (L85-L87)
lemma colon_eq_top_iff_subset (S : Set M) : N.colon S = ⊤ ↔ S ⊆ N := by
aesop (add simp [mem_colon, Ideal.eq_top_iff_one])
```

-- snippet 43
```lean
-- Mathlib.RingTheory.Ideal.Basic.span_single_eq_top (L203-L232)
lemma span_single_eq_top {ι : Type*} [DecidableEq ι] [Finite ι] (R : ι → Type*)
    [∀ i, Semiring (R i)] : Ideal.span (Set.range fun i ↦ (Pi.single i 1 : Π i, R i)) = ⊤ := by
rw [_root_.eq_top_iff]
  rintro x -
  induction x using Pi.single_induction with
  | zero => simp
  | add f g hf hg => exact Ideal.add_mem _ hf hg
  | single i r =>
      rw [show Pi.single i r = Pi.single i r * Pi.single i 1 by simp [← Pi.single_mul_left]]
      exact Ideal.mul_mem_left _ _ (Ideal.subset_span ⟨i, rfl⟩)

end Ideal

end CommSemiring

section DivisionSemiring

variable {K : Type*} [DivisionSemiring K] (I : Ideal K)

namespace Ideal

variable (K) in
/-- A bijection between (left) ideals of a division ring and `{0, 1}`, sending `⊥` to `0`
and `⊤` to `1`. -/
noncomputable def equivFinTwo : Ideal K ≃ Fin 2 where
  toFun := fun I ↦ if I = ⊥ then 0 else 1
  invFun := ![⊥, ⊤]
  left_inv := fun I ↦ by rcases eq_bot_or_top I with rfl | rfl <;> simp
  right_inv := fun i ↦ by fin_cases i <;> simp
```

-- snippet 44
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.IsAssociatedPrime (L98-L102)
def IsAssociatedPrime : Prop :=
(⊥ : Submodule R M).IsAssociatedPrime I

variable (R) in
/-- The set of associated primes of a module. -/
```

-- snippet 45
```lean
-- Mathlib.RingTheory.Ideal.Colon.bot_colon (L64-L67)
theorem bot_colon : colon (⊥ : Submodule R M) (N : Set M) = N.annihilator := by
ext x
  simp [mem_colon, mem_annihilator]
```

-- snippet 46
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.biUnion_associatedPrimes_eq_compl_nonZeroDivisors (L240-L246)
theorem biUnion_associatedPrimes_eq_compl_nonZeroDivisors [IsNoetherianRing R] :
    ⋃ p ∈ associatedPrimes R R, p = (nonZeroDivisors R : Set R)ᶜ :=
(biUnion_associatedPrimes_eq_zero_divisors R R).trans <| by
    ext; simp [← nonZeroDivisorsLeft_eq_nonZeroDivisors, and_comm]

variable {R M}
```

-- snippet 47
```lean
-- Mathlib.RingTheory.Ideal.Colon.top_colon (L102-L104)
lemma top_colon : (⊤ : Submodule R M).colon S = ⊤ := by
aesop (add simp mem_colon)
```

-- snippet 48
```lean
-- Mathlib.RingTheory.Ideal.Colon.iInf_colon (L93-L95)
lemma iInf_colon {ι : Sort*} (f : ι → Submodule R M) : (⨅ i, f i).colon S = ⨅ i, (f i).colon S := by
aesop (add simp mem_colon)
```

-- snippet 49
```lean
-- Mathlib.RingTheory.Ideal.Colon.inf_colon (L89-L91)
lemma inf_colon : (N₁ ⊓ N₂).colon S = N₁.colon S ⊓ N₂.colon S := by
aesop (add simp mem_colon)
```

-- snippet 50
```lean
-- Mathlib.RingTheory.Ideal.Basic.prod_mem (L196-L202)
theorem prod_mem {ι : Type*} {f : ι → α} {s : Finset ι}
    (I : Ideal α) {i : ι} (hi : i ∈ s) (hfi : f i ∈ I) :
    ∏ i ∈ s, f i ∈ I := by
classical
  rw [Finset.prod_eq_prod_diff_singleton_mul hi]
  exact Ideal.mul_mem_left _ _ hfi
```

-- snippet 51
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_eq_zero (L66-L67)
theorem toCotangent_eq_zero (x : I) : I.toCotangent x = 0 ↔ (x : R) ∈ I ^ 2 := I.mem_toCotangent_ker
```

-- snippet 52
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.LinearEquiv (L128-L132)
theorem LinearEquiv.isAssociatedPrime_iff (l : M ≃ₗ[R] M') :
    IsAssociatedPrime I M ↔ IsAssociatedPrime I M' :=
⟨fun h => h.map_of_injective l l.injective,
    fun h => h.map_of_injective l.symm l.symm.injective⟩
```

-- snippet 53
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Finiteness.associatedPrimes (L158-L169)
theorem associatedPrimes.finite : (associatedPrimes A M).Finite := by
induction ‹Module.Finite A M› using
    IsNoetherianRing.induction_on_isQuotientEquivQuotientPrime A with
  | subsingleton N => simp [associatedPrimes.eq_empty_of_subsingleton]
  | quotient N p f =>
    have := associatedPrimes.eq_singleton_of_isPrimary p.2.isPrimary
    simp [LinearEquiv.AssociatedPrimes.eq f, this]
  | exact N₁ N₂ N₃ f g hf _ hfg h₁ h₃ =>
    exact (h₁.union h₃).subset (associatedPrimes.subset_union_of_exact hf hfg)

/-- Every maximal ideal of a commutative Noetherian total ring of fractions `A` is
an associated prime of the `A`-module `A`. -/
```

-- snippet 54
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_add_pred_mem_of_pow_mem (L123-L127)
theorem add_pow_add_pred_mem_of_pow_mem {m n : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) :
    (a + b) ^ (m + n - 1) ∈ I :=
I.add_pow_add_pred_mem_of_pow_mem_of_commute ha hb (Commute.all ..)
```

-- snippet 55
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule (L40-L69)
lemma mem_associatedPrimes_of_comap_mem_associatedPrimes_of_isLocalizedModule
    (p : Ideal R') (ass : p.comap (algebraMap R R') ∈ associatedPrimes R M) :
    p ∈ associatedPrimes R' M' := by
rcases ass with ⟨hp, x, hx⟩
  constructor
  · refine (IsLocalization.isPrime_iff_isPrime_disjoint S _ _).mpr
      ⟨hp, (IsLocalization.disjoint_comap_iff S R' p).mpr ?_⟩
    by_contra eqtop
    simp [eqtop, Ideal.comap_top, Ideal.isPrime_iff] at hp
  · use f x
    ext t
    rcases IsLocalization.exists_mk'_eq S t with ⟨r, s, hrs⟩
    simp_rw [← hrs, Ideal.mem_radical_iff, mem_colon_singleton, ← IsLocalizedModule.mk'_one S f,
      ← IsLocalization.mk'_pow, IsLocalizedModule.mk'_smul_mk', mul_one, mem_bot,
      IsLocalizedModule.mk'_eq_zero']
    refine ⟨fun h ↦ exists_comm.mp ⟨1, ?_⟩, fun ⟨n, t, ht⟩ ↦ ?_⟩
    · simp only [← mem_colon_singleton, one_smul, ← mem_bot R, ← hx, ← Ideal.mem_radical_iff]
      exact hSR'.mk'_mem_iff.mp h
    · have : hSR'.mk' R' r s = hSR'.mk' R' (t.1 * r) 1 * hSR'.mk' R' 1 (t * s) := by
        rw [← hSR'.mk'_mul, mul_one, one_mul, ← sub_eq_zero, ← hSR'.mk'_sub, Submonoid.coe_mul]
        simp [← mul_assoc, mul_comm r t.1, IsLocalization.mk'_zero]
      rw [this]
      apply Ideal.IsTwoSided.mul_mem_of_left
      rw [IsLocalization.mk'_one, ← Ideal.mem_comap, hx]
      rcases eq_zero_or_pos n with rfl | hn
      · exact Ideal.IsTwoSided.mul_mem_of_left _ ⟨1, by simpa using ht⟩
      · use n
        rw [mem_colon_singleton, mul_pow, mul_smul, ← mem_colon_singleton]
        exact Ideal.pow_mem_of_mem _ (by simpa using ht) n hn
```

-- snippet 56
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.isAssociatedPrime_iff (L117-L120)
theorem isAssociatedPrime_iff [IsNoetherianRing R] :
    IsAssociatedPrime I M ↔ I.IsPrime ∧ ∃ x : M, I = colon ⊥ {x} :=
(⊥ : Submodule R M).isAssociatedPrime_iff
```

-- snippet 57
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_mem_of_pow_mem_of_le_of_commute (L83-L96)
theorem add_pow_mem_of_pow_mem_of_le_of_commute {m n k : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hk : m + n ≤ k + 1)
    (hab : Commute a b) :
    (a + b) ^ k ∈ I := by
simp_rw [hab.add_pow, ← Nat.cast_comm]
  apply I.sum_mem
  intro c _
  apply mul_mem_left
  by_cases h : m ≤ c
  · rw [hab.pow_pow]
    exact I.mul_mem_left _ (I.pow_mem_of_pow_mem ha h)
  · refine I.mul_mem_left _ (I.pow_mem_of_pow_mem hb ?_)
    lia
```

-- snippet 58
```lean
-- Mathlib.RingTheory.Ideal.Basic.add_pow_mem_of_pow_mem_of_le (L118-L122)
theorem add_pow_mem_of_pow_mem_of_le {m n k : ℕ}
    (ha : a ^ m ∈ I) (hb : b ^ n ∈ I) (hk : m + n ≤ k + 1) :
    (a + b) ^ k ∈ I :=
I.add_pow_mem_of_pow_mem_of_le_of_commute ha hb hk (Commute.all ..)
```

-- snippet 59
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_singleton_zero (L117-L119)
lemma colon_singleton_zero : N.colon {0} = ⊤ := by
simp
```

-- snippet 60
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent (L51-L53)
def toCotangent : I →ₗ[R] I.Cotangent := Submodule.mkQ _
set_option backward.isDefEq.respectTransparency false in
```

-- snippet 61
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_union (L106-L108)
lemma colon_union : N.colon (S₁ ∪ S₂) = N.colon S₁ ⊓ N.colon S₂ := by
aesop (add simp mem_colon)
```

-- snippet 62
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon_singleton (L48-L50)
theorem mem_colon_singleton {x : M} {r : R} : r ∈ N.colon {x} ↔ r • x ∈ N := by
simp [mem_colon, forall_eq]
```

-- snippet 63
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.associatedPrimes (L103-L107)
def associatedPrimes : Set (Ideal R) :=
{ I | IsAssociatedPrime I M }

variable {I J M} {M' : Type*} [AddCommMonoid M'] [Module R M'] (f : M →ₗ[R] M')
```

-- snippet 64
```lean
-- Mathlib.RingTheory.Ideal.Basic.span_pow_eq_top (L161-L185)
theorem span_pow_eq_top (s : Set α) (hs : span s = ⊤) (n : ℕ) :
    span ((fun (x : α) => x ^ n) '' s) = ⊤ := by
rw [eq_top_iff_one]
  rcases n with - | n
  · obtain rfl | ⟨x, hx⟩ := eq_empty_or_nonempty s
    · rw [Set.image_empty, hs]
      trivial
    · exact subset_span ⟨_, hx, pow_zero _⟩
  rw [eq_top_iff_one, span, Finsupp.mem_span_iff_linearCombination] at hs
  rcases hs with ⟨f, hf⟩
  simp only [Finsupp.linearCombination, Finsupp.coe_lsum, Finsupp.sum, LinearMap.coe_smulRight,
    LinearMap.id_coe, id_eq, smul_eq_mul] at hf
  have := sum_pow_mem_span_pow f.support (fun a => f a * a) n
  rw [hf, one_pow] at this
  refine span_le.mpr ?_ this
  rintro _ hx
  simp_rw [Set.mem_image] at hx
  rcases hx with ⟨x, _, rfl⟩
  have : span ({(x : α) ^ (n + 1)} : Set α) ≤ span ((fun x : α => x ^ (n + 1)) '' s) := by
    rw [span_le, Set.singleton_subset_iff]
    exact subset_span ⟨x, x.prop, rfl⟩
  refine this ?_
  rw [mul_pow, mem_span_singleton]
  exact ⟨f x ^ (n + 1), mul_comm _ _⟩
```

-- snippet 65
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_iUnion (L110-L112)
lemma colon_iUnion {ι : Sort*} (f : ι → Set M) : N.colon (⋃ i, f i) = ⨅ i, N.colon (f i) := by
aesop (add simp mem_colon)
```

-- snippet 66
```lean
-- Mathlib.RingTheory.Ideal.Basic.isField_iff_isSimpleOrder_ideal (L292-L302)
theorem isField_iff_isSimpleOrder_ideal : IsField R ↔ IsSimpleOrder (Ideal R) := by
cases subsingleton_or_nontrivial R
  · exact
      ⟨fun h => (not_isField_of_subsingleton _ h).elim, fun h =>
        (false_of_nontrivial_of_subsingleton <| Ideal R).elim⟩
  rw [← not_iff_not, Ring.not_isField_iff_exists_ideal_bot_lt_and_lt_top]
  contrapose! +distrib
  simp_rw [not_lt_top_iff, not_bot_lt_iff]
  exact ⟨fun h => ⟨h⟩, fun h => h.2⟩

/-- When a ring is not a field, the maximal ideals are nontrivial. -/
```

-- snippet 67
```lean
-- Mathlib.RingTheory.Ideal.Basic.isSimpleOrder (L237-L249)
instance isSimpleOrder : IsSimpleOrder (Ideal K) :=
⟨eq_bot_or_top⟩

end Ideal

end DivisionSemiring

-- TODO: consider moving the lemmas below out of the `Ring` namespace since they are
-- about `CommSemiring`s.
namespace Ring

variable {R : Type*} [CommSemiring R]
```

-- snippet 68
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon (L45-L46)
theorem mem_colon {r} : r ∈ N.colon S ↔ ∀ s ∈ S, r • s ∈ N := Set.smul_set_subset_iff
```

-- snippet 69
```lean
-- Mathlib.RingTheory.Ideal.Basic.ne_bot_of_isMaximal_of_not_isField (L303-L314)
theorem ne_bot_of_isMaximal_of_not_isField [Nontrivial R] {M : Ideal R} (max : M.IsMaximal)
    (not_field : ¬IsField R) : M ≠ ⊥ := by
rintro rfl
  obtain ⟨I, hIbot, hItop⟩ := not_isField_iff_exists_ideal_bot_lt_and_lt_top.mp not_field
  exact hIbot.ne (max.eq_of_le hItop.ne bot_le)

end Ring

namespace Ideal

variable {R : Type*} [CommSemiring R] [Nontrivial R]
```

-- snippet 70
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.not_isAssociatedPrime_of_subsingleton (L133-L138)
theorem not_isAssociatedPrime_of_subsingleton [Subsingleton M] : ¬IsAssociatedPrime I M := by
rintro ⟨hI, x, hx⟩
  apply hI.ne_top
  simp [hx, Subsingleton.elim x 0]

variable (R) in
```

-- snippet 71
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Basic.prod (L206-L211)
theorem prod : associatedPrimes R (M × M') = associatedPrimes R M ∪ associatedPrimes R M' :=
(subset_union_of_exact LinearMap.inl_injective .inl_snd).antisymm (Set.union_subset_iff.2
    ⟨subset_of_injective LinearMap.inl_injective, subset_of_injective LinearMap.inr_injective⟩)

end associatedPrimes
```

-- snippet 72
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Finiteness.Submodule (L51-L55)
def Submodule.IsQuotientEquivQuotientPrime (N₁ N₂ : Submodule A M) :=
N₁ ≤ N₂ ∧ ∃ (p : PrimeSpectrum A), Nonempty ((↥N₂ ⧸ N₁.submoduleOf N₂) ≃ₗ[A] A ⧸ p.1)

set_option backward.isDefEq.respectTransparency false in
open LinearMap in
```

-- snippet 73
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.Cotangent (L41-L49)
def Cotangent : Type _ := I ⧸ (I • ⊤ : Submodule R I)
deriving Inhabited, AddCommGroup, Module (R ⧸ I)

deriving instance Module S, IsScalarTower S S' for Cotangent I

variable [IsNoetherian R I] in
deriving instance IsNoetherian R for Cotangent I

/-- The quotient map from `I` to `I ⧸ I ^ 2`. -/
```

-- snippet 74
```lean
-- Mathlib.RingTheory.Ideal.Colon.annihilator_quotient (L175-L181)
theorem annihilator_quotient : Module.annihilator R (M ⧸ N) = N.colon Set.univ := by
ext r
  have htop : (⊤ : Submodule R (M ⧸ N)) = (⊤ : Submodule R M).map N.mkQ := by
    simpa [map_top] using (LinearMap.range_eq_top.mpr (mkQ_surjective N)).symm
  rw [← annihilator_top (R := R) (M := M ⧸ N), htop,
    annihilator_map_mkQ_eq_colon (N := N) (P := ⊤), Submodule.top_coe]
```

-- snippet 75
```lean
-- Mathlib.RingTheory.Ideal.Colon.colon_mono (L68-L70)
theorem colon_mono (hn : N₁ ≤ N₂) (hs : S₁ ⊆ S₂) : N₁.colon S₂ ≤ N₂.colon S₁ :=
fun _ hrns ↦ mem_colon.mpr fun s₁ hs₁ ↦ hn <| mem_colon.mp hrns s₁ <| hs hs₁
```

-- snippet 76
```lean
-- Mathlib.RingTheory.Ideal.Basis.constr_basisSpanSingleton (L46-L54)
theorem constr_basisSpanSingleton {N : Type*} [Semiring N] [Module N S] [SMulCommClass R N S]
    (b : Basis ι R S) {x : S} (hx : x ≠ 0) :
    (b.constr N).toFun (((↑) : _ → S) ∘ (basisSpanSingleton b hx)) = Algebra.lmul R S x :=
b.ext fun i => by simp

end Ideal

/-- If `I : Ideal S` has a basis over `R`,
`x ∈ I` iff it is a linear combination of basis vectors. -/
```

-- snippet 77
```lean
-- Mathlib.RingTheory.Ideal.Basic.exists_not_isUnit_of_not_isField (L250-L258)
theorem exists_not_isUnit_of_not_isField [Nontrivial R] (hf : ¬IsField R) :
    ∃ (x : R) (_hx : x ≠ (0 : R)), ¬IsUnit x := by
have : ¬_ := fun h => hf ⟨exists_pair_ne R, mul_comm, h⟩
  simp_rw [isUnit_iff_exists_inv]
  push Not at this ⊢
  obtain ⟨x, hx, not_unit⟩ := this
  exact ⟨x, hx, not_unit⟩

open Ideal in
```

-- snippet 78
```lean
-- Mathlib.RingTheory.Ideal.Basic.bot_lt_of_maximal (L315-L318)
theorem bot_lt_of_maximal (M : Ideal R) [hm : M.IsMaximal] (non_field : ¬IsField R) : ⊥ < M :=
(Ring.ne_bot_of_isMaximal_of_not_isField hm non_field).bot_lt

end Ideal
```

-- snippet 79
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon' (L131-L133)
theorem mem_colon' {r} : r ∈ N.colon S ↔ S ≤ comap (r • (LinearMap.id : M →ₗ[R] M)) N :=
mem_colon
```

-- snippet 80
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Localization.minimalPrimes_annihilator_subset_associatedPrimes (L131-L150)
lemma minimalPrimes_annihilator_subset_associatedPrimes [IsNoetherianRing R] [Module.Finite R M] :
    (Module.annihilator R M).minimalPrimes ⊆ associatedPrimes R M := by
intro p hp
  have prime := hp.1.1
  let Rₚ := Localization.AtPrime p
  have : Nontrivial (LocalizedModule p.primeCompl M) := by
    simpa [← Module.mem_support_iff (p := ⟨p, prime⟩), Module.support_eq_zeroLocus] using hp.1.2
  rcases associatedPrimes.nonempty Rₚ (LocalizedModule p.primeCompl M) with ⟨q, hq⟩
  have q_prime : q.IsPrime := IsAssociatedPrime.isPrime hq
  simp only [← preimage_comap_associatedPrimes_eq_associatedPrimes_of_isLocalizedModule p.primeCompl
    Rₚ (LocalizedModule.mkLinearMap p.primeCompl M), Set.mem_preimage] at hq
  have ann_le : Module.annihilator R M ≤ Ideal.comap (algebraMap R Rₚ) q :=
    le_of_eq_of_le Submodule.annihilator_top.symm (IsAssociatedPrime.annihilator_le hq)
  have le : Ideal.comap (algebraMap R Rₚ) q ≤ p := by
    have := (IsLocalization.disjoint_comap_iff p.primeCompl Rₚ q).mpr q_prime.ne_top
    simpa only [Ideal.primeCompl, Submonoid.coe_set_mk, Subsemigroup.coe_set_mk,
      Set.disjoint_compl_left_iff_subset] using this
  simpa [Minimal.eq_of_le hp.out ⟨IsAssociatedPrime.isPrime hq, ann_le⟩ le] using hq

end Module.associatedPrimes
```

-- snippet 81
```lean
-- Mathlib.RingTheory.Ideal.Colon.mem_colon_span_singleton (L154-L156)
theorem mem_colon_span_singleton {x : M} {r : R} : r ∈ N.colon (span R {x}) ↔ r • x ∈ N := by
simp
```

-- snippet 82
```lean
-- Mathlib.RingTheory.Ideal.Basic.mem_pi (L56-L58)
theorem mem_pi (r : Π i, R i) : r ∈ pi I ↔ ∀ i, r i ∈ I i :=
Iff.rfl
```

-- snippet 83
```lean
-- Mathlib.RingTheory.Ideal.Colon.iInf_colon_iUnion (L74-L77)
theorem iInf_colon_iUnion (ι₁ : Sort*) (f : ι₁ → Submodule R M) (ι₂ : Sort*) (g : ι₂ → Set M) :
    (⨅ i, f i).colon (⋃ j, g j) = ⨅ (i) (j), (f i).colon (g j) := by
aesop (add simp mem_colon)
```

-- snippet 84
```lean
-- Mathlib.RingTheory.Ideal.Cotangent.toCotangent_eq (L62-L65)
theorem toCotangent_eq {x y : I} : I.toCotangent x = I.toCotangent y ↔ (x - y : R) ∈ I ^ 2 := by
rw [← sub_eq_zero]
  exact I.mem_toCotangent_ker
```

-- snippet 85
```lean
-- Mathlib.RingTheory.Ideal.AssociatedPrime.Finiteness.IsNoetherianRing (L92-L117)
theorem IsNoetherianRing.exists_relSeries_isQuotientEquivQuotientPrime :
    ∃ s : RelSeries {(N₁, N₂) | Submodule.IsQuotientEquivQuotientPrime (A := A) (M := M) N₁ N₂},
s.head = ⊥ ∧ s.last = ⊤ := by
  refine WellFoundedGT.induction_top ⟨⊥, .singleton _ ⊥, rfl, rfl⟩ ?_
  rintro N hN ⟨s, hs₁, hs₂⟩
  have := Submodule.Quotient.nontrivial_iff.mpr hN
  obtain ⟨p, hp⟩ := associatedPrimes.nonempty A (M ⧸ N)
  rw [AssociatedPrimes.mem_iff, isAssociatedPrime_iff] at hp
  obtain ⟨hp, x, rfl⟩ := hp
  obtain ⟨x, rfl⟩ := Submodule.mkQ_surjective _ x
  have hxN : x ∉ N := fun h ↦ hp.ne_top (by rw [show N.mkQ x = 0 by simpa]; simp)
  have := Submodule.isQuotientEquivQuotientPrime_iff.mpr ⟨x, hp, rfl⟩
  refine ⟨_, by simpa [hs₂], s.snoc _ (hs₂ ▸ this), by simpa, rfl⟩

/-- If a property on finitely generated modules over a Noetherian ring satisfies that:

- it holds for zero module (it's formalized as it holds for any module which is subsingleton),
- it holds for `A ⧸ p` for every prime ideal `p` of `A` (to avoid universe problem,
  it's formalized as it holds for any module isomorphic to `A ⧸ p`),
- it is stable by short exact sequences,

then the property holds for every finitely generated modules.

NOTE: This should be the induction principle for `M`, but due to the bug
https://github.com/leanprover/lean4/issues/4246
currently it is induction for `Module.Finite A M`. -/
```

-- snippet 86
```lean
-- Mathlib.RingTheory.Ideal.Basic.not_isField_of_ne_of_ne (L271-L275)
theorem not_isField_of_ne_of_ne [Nontrivial R] {I : Ideal R} (h_bot : I ≠ ⊥) (h_top : I ≠ ⊤) :
    ¬ IsField R := by
contrapose! h_bot
  exact ((isField_iff_maximal_bot.mp h_bot).eq_of_le h_top bot_le).symm
```
[/INST]


[System] You are Leanstral, a Lean 4 proof assistant. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.Analysis.Normed.Operator.NormedSpace).

[User]
Complete the following proof.

Open namespaces: Topology, scoped, NNReal, Metric, ContinuousLinearMap, Set, Real

## Surrounding context in Mathlib/Analysis/Normed/Operator/NormedSpace.lean
```lean
-- Mathlib.Analysis.Normed.Operator.NormedSpace.bound_of_shell (L45-L53)
theorem bound_of_shell [RingHomIsometric σ₁₂] (f : E →ₛₗ[σ₁₂] F) {ε C : ℝ} (ε_pos : 0 < ε) {c : 𝕜}
    (hc : 1 < ‖c‖) (hf : ∀ x, ε / ‖c‖ ≤ ‖x‖ → ‖x‖ < ε → ‖f x‖ ≤ C * ‖x‖) (x : E) :
    ‖f x‖ ≤ C * ‖x‖

-- Mathlib.Analysis.Normed.Operator.NormedSpace.bound_of_ball_bound (L54-L67)
theorem bound_of_ball_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] Fₗ)
    (h : ∀ z ∈ Metric.ball (0 : E) r, ‖f z‖ ≤ c) : ∃ C, ∀ z : E, ‖f z‖ ≤ C * ‖z‖

-- Mathlib.Analysis.Normed.Operator.NormedSpace.antilipschitz_of_comap_nhds_le (L68-L98)
theorem antilipschitz_of_comap_nhds_le [h : RingHomIsometric σ₁₂] (f : E →ₛₗ[σ₁₂] F)
    (hf : (𝓝 0).comap f ≤ 𝓝 0) : ∃ K, AntilipschitzWith K f
```

```lean
lemma LinearIsometry.inr_apply [SeminormedAddCommGroup E] [NormedSpace 𝕜 E]
    [SeminormedAddCommGroup F] [NormedSpace 𝕜 F] (y : F) :
    LinearIsometry.inr 𝕜 E F y = (0, y)
```

Provide a complete Lean proof for the statement above. Do not include the statement in your answer.

You are Leanstral, a Lean 4 proof assistant. The target mathlib commit is 5e932f97dd25535344f80f9dd8da3aab83df0fe6 (module Mathlib.Analysis.Normed.Operator.NormedSpace).

Complete the following proof.

Open namespaces: Topology, scoped, NNReal, Metric, ContinuousLinearMap, Set, Real

## Surrounding context in Mathlib/Analysis/Normed/Operator/NormedSpace.lean
```lean
-- Mathlib.Analysis.Normed.Operator.NormedSpace.bound_of_shell (L45-L53)
theorem bound_of_shell [RingHomIsometric σ₁₂] (f : E →ₛₗ[σ₁₂] F) {ε C : ℝ} (ε_pos : 0 < ε) {c : 𝕜}
    (hc : 1 < ‖c‖) (hf : ∀ x, ε / ‖c‖ ≤ ‖x‖ → ‖x‖ < ε → ‖f x‖ ≤ C * ‖x‖) (x : E) :
    ‖f x‖ ≤ C * ‖x‖

-- Mathlib.Analysis.Normed.Operator.NormedSpace.bound_of_ball_bound (L54-L67)
theorem bound_of_ball_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] Fₗ)
    (h : ∀ z ∈ Metric.ball (0 : E) r, ‖f z‖ ≤ c) : ∃ C, ∀ z : E, ‖f z‖ ≤ C * ‖z‖

-- Mathlib.Analysis.Normed.Operator.NormedSpace.antilipschitz_of_comap_nhds_le (L68-L98)
theorem antilipschitz_of_comap_nhds_le [h : RingHomIsometric σ₁₂] (f : E →ₛₗ[σ₁₂] F)
    (hf : (𝓝 0).comap f ≤ 𝓝 0) : ∃ K, AntilipschitzWith K f
```

```lean
lemma LinearIsometry.inr_apply [SeminormedAddCommGroup E] [NormedSpace 𝕜 E]
    [SeminormedAddCommGroup F] [NormedSpace 𝕜 F] (y : F) :
    LinearIsometry.inr 𝕜 E F y = (0, y)
```

Provide a complete Lean proof for the statement above. Do not include the statement in your answer.