[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.NumberTheory.TsumDivisorsAntidiagonal).

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

```lean
lemma sigmaAntidiagonalEquivProd_symm_apply_snd (x : ℕ+ × ℕ+) :
    (sigmaAntidiagonalEquivProd.symm x).2 = (x.1.1, x.2.1)
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.divisorsAntidiagonalFactors (L31-L35)
def divisorsAntidiagonalFactors (n : ℕ+) : Nat.divisorsAntidiagonal n → ℕ+ × ℕ+ := fun x ↦
⟨⟨x.1.1, Nat.pos_of_mem_divisors (Nat.fst_mem_divisors_of_mem_antidiagonal x.2)⟩,
    (⟨x.1.2, Nat.pos_of_mem_divisors (Nat.snd_mem_divisors_of_mem_antidiagonal x.2)⟩ : ℕ+),
    Nat.pos_of_mem_divisors (Nat.snd_mem_divisors_of_mem_antidiagonal x.2)⟩
```

-- snippet 2
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.divisorsAntidiagonalFactors_eq (L36-L39)
lemma divisorsAntidiagonalFactors_eq {n : ℕ+} (x : Nat.divisorsAntidiagonal n) :
    (divisorsAntidiagonalFactors n x).1.1 * (divisorsAntidiagonalFactors n x).2.1 = n := by
simp [divisorsAntidiagonalFactors, (Nat.mem_divisorsAntidiagonal.mp x.2).1]
```

-- snippet 3
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.divisorsAntidiagonalFactors_one (L40-L47)
lemma divisorsAntidiagonalFactors_one (x : Nat.divisorsAntidiagonal 1) :
    (divisorsAntidiagonalFactors 1 x) = (1, 1) := by
have h := Nat.mem_divisorsAntidiagonal.mp x.2
  simp only [mul_eq_one, ne_eq, one_ne_zero, not_false_eq_true, and_true] at h
  simp [divisorsAntidiagonalFactors, h.1, h.2]

/-- The equivalence from the union over `n` of `Nat.divisorsAntidiagonal n` to `ℕ+ × ℕ+`
given by sending `n = a * b` to `(a, b)`. -/
```

-- snippet 4
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.sigmaAntidiagonalEquivProd (L48-L57)
def sigmaAntidiagonalEquivProd : (Σ n : ℕ+, Nat.divisorsAntidiagonal n) ≃ ℕ+ × ℕ+ where
  toFun x := divisorsAntidiagonalFactors x.1 x.2
invFun x :=
    ⟨⟨x.1.val * x.2.val, mul_pos x.1.2 x.2.2⟩, ⟨x.1, x.2⟩, by simp [Nat.mem_divisorsAntidiagonal]⟩
  left_inv := by
    rintro ⟨n, ⟨k, l⟩, h⟩
    rw [Nat.mem_divisorsAntidiagonal] at h
    ext <;> simp [divisorsAntidiagonalFactors, ← PNat.coe_injective.eq_iff, h.1]
  right_inv _ := rfl
```

-- snippet 5
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.sigmaAntidiagonalEquivProd_symm_apply_fst (L58-L60)
lemma sigmaAntidiagonalEquivProd_symm_apply_fst (x : ℕ+ × ℕ+) :
    (sigmaAntidiagonalEquivProd.symm x).1 = x.1.1 * x.2.1 := rfl
```

-- snippet 6
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.summable_norm_pow_mul_geometric_div_one_sub (L69-L98)
lemma summable_norm_pow_mul_geometric_div_one_sub (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    Summable fun n : ℕ ↦ n ^ k * r ^ n / (1 - r ^ n) := by
simp only [div_eq_mul_one_div (_ * _ ^ _)]
  apply Summable.mul_tendsto_const (c := 1 / (1 - 0))
    (by simpa using summable_norm_pow_mul_geometric_of_norm_lt_one k hr)
  simpa only [Nat.cofinite_eq_atTop] using
   tendsto_const_nhds.div ((tendsto_pow_atTop_nhds_zero_of_norm_lt_one hr).const_sub 1) (by simp)

private lemma summable_divisorsAntidiagonal_aux (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    Summable fun c : (n : ℕ+) × {x // x ∈ (n : ℕ).divisorsAntidiagonal} ↦
    (c.2.1.2) ^ k * (r ^ (c.2.1.1 * c.2.1.2)) := by
  apply Summable.of_norm
  rw [summable_sigma_of_nonneg (fun a ↦ by positivity)]
  constructor
  · exact fun n ↦ (hasSum_fintype _).summable
  · simp only [norm_mul, norm_pow, tsum_fintype, Finset.univ_eq_attach]
    apply Summable.of_nonneg_of_le (f := fun c : ℕ+ ↦ ‖(c : 𝕜) ^ (k + 1) * r ^ (c : ℕ)‖)
      (fun b ↦ Finset.sum_nonneg (fun _ _ ↦ mul_nonneg (by simp) (by simp))) (fun b ↦ ?_)
      (by apply (summable_norm_pow_mul_geometric_of_norm_lt_one (k + 1) hr).subtype)
    transitivity ∑ _ ∈ (b : ℕ).divisors, ‖(b : 𝕜)‖ ^ k * ‖r ^ (b : ℕ)‖
    · rw [(b : ℕ).divisorsAntidiagonal.sum_attach (fun x ↦ ‖(x.2 : 𝕜)‖ ^ _ * _ ^ (x.1 * x.2)),
          sum_divisorsAntidiagonal ((fun x y ↦ ‖(y : 𝕜)‖ ^ k * _ ^ (x * y)))]
      gcongr with i hi
      · simpa using le_of_dvd b.2 (div_dvd_of_dvd (dvd_of_mem_divisors hi))
      · rw [norm_pow, mul_comm, Nat.div_mul_cancel (dvd_of_mem_divisors hi)]
    · simp only [norm_pow, Finset.sum_const, nsmul_eq_mul, ← mul_assoc, add_comm k 1, pow_add,
        pow_one, norm_mul]
      gcongr
      simpa using Nat.card_divisors_le_self b
```

-- snippet 7
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.summable_prod_mul_pow (L99-L105)
theorem summable_prod_mul_pow (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    Summable fun c : (ℕ+ × ℕ+) ↦ c.2 ^ k * (r ^ (c.1 * c.2 : ℕ)) := by
simpa [sigmaAntidiagonalEquivProd.summable_iff.symm] using summable_divisorsAntidiagonal_aux k hr

-- access notation `σ`
open scoped sigma
```

-- snippet 8
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.tsum_prod_pow_eq_tsum_sigma (L106-L118)
theorem tsum_prod_pow_eq_tsum_sigma (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    ∑' d : ℕ+, ∑' c : ℕ+, c ^ k * r ^ (d * c : ℕ) = ∑' e : ℕ+, σ k e * r ^ (e : ℕ) := by
suffices ∑' c : ℕ+ × ℕ+, c.2 ^ k * r ^ (c.1 * c.2 : ℕ) =
    ∑' e : ℕ+, σ k e * r ^ (e : ℕ) by rwa [← (summable_prod_mul_pow k hr).tsum_prod]
  simp only [← sigmaAntidiagonalEquivProd.tsum_eq, sigmaAntidiagonalEquivProd,
    divisorsAntidiagonalFactors, PNat.mk_coe, Equiv.coe_fn_mk, sigma_eq_sum_div, cast_sum,
    cast_pow, Summable.tsum_sigma (summable_divisorsAntidiagonal_aux k hr)]
  refine tsum_congr fun n ↦ ?_
  simpa [tsum_fintype, Finset.sum_mul,
    (n : ℕ).divisorsAntidiagonal.sum_attach fun x : ℕ × ℕ ↦ x.2 ^ k * r ^ (x.1 * x.2),
    sum_divisorsAntidiagonal fun x y ↦ y ^ k * r ^ (x * y)]
      using Finset.sum_congr rfl fun i hi ↦ by rw [Nat.mul_div_cancel' (dvd_of_mem_divisors hi)]
```

-- snippet 9
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.tsum_pow_div_one_sub_eq_tsum_sigma (L119-L130)
lemma tsum_pow_div_one_sub_eq_tsum_sigma {r : 𝕜} (hr : ‖r‖ < 1) (k : ℕ) :
    ∑' n : ℕ+, n ^ k * r ^ (n : ℕ) / (1 - r ^ (n : ℕ)) = ∑' n : ℕ+, σ k n * r ^ (n : ℕ) := by
have (m : ℕ) [NeZero m] := tsum_geometric_of_norm_lt_one (ξ := r ^ m)
    (by simpa using pow_lt_one₀ (by simp) hr (NeZero.ne _))
  simp only [div_eq_mul_inv, ← this, ← tsum_mul_left, mul_assoc, ← _root_.pow_succ',
    ← fun (n : ℕ) ↦ tsum_pnat_eq_tsum_succ (f := fun m ↦ n ^ k * (r ^ n) ^ m)]
  have h00 := tsum_prod_pow_eq_tsum_sigma k hr
  rw [Summable.tsum_comm (by apply (summable_prod_mul_pow k hr).prod_symm)] at h00
  rw [← h00]
  exact tsum_congr₂ <| fun b c ↦ by simp [mul_comm c.val b.val, pow_mul]

omit [CompleteSpace 𝕜] [NormSMulClass ℤ 𝕜] in
```

-- snippet 10
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.tendsto_zero_geometric_tsum_pnat (L131-L143)
lemma tendsto_zero_geometric_tsum_pnat {r : 𝕜} (hr : ‖r‖ < 1) :
    Tendsto (fun m : ℕ+ ↦ ∑' n : ℕ+, r ^ (n * m : ℕ)) atTop (𝓝 0) := by
have h1 (m : ℕ+) : ‖r ^ (m : ℕ)‖ < 1 := by
    rwa [norm_pow, pow_lt_one_iff_of_nonneg (norm_nonneg _) (NeZero.ne _)]
  have h2 (m : ℕ+) : ∑' n : ℕ+, r ^ (n * m : ℕ) = (1 - r ^ (m : ℕ))⁻¹ - 1 := by
    have := tsum_geometric_of_norm_lt_one (h1 m)
    rw [← tsum_zero_pnat_eq_tsum_nat (summable_geometric_of_norm_lt_one (h1 m))] at this
    simp_rw [← this, pow_zero, add_sub_cancel_left, mul_comm, pow_mul]
  rw [funext h2, (by simp : 𝓝 (0 : 𝕜) = 𝓝 ((1 - 0)⁻¹ - 1)), tendsto_sub_const_iff,
    tendsto_inv_iff₀ (by simp), tendsto_const_sub_iff]
  exact (tendsto_pow_atTop_nhds_zero_of_norm_lt_one hr).comp <| tendsto_PNat_val_atTop_atTop

end tsum
```

<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.NumberTheory.TsumDivisorsAntidiagonal).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]Prove the following theorem. The retrieved context below may help.

```lean
lemma sigmaAntidiagonalEquivProd_symm_apply_snd (x : ℕ+ × ℕ+) :
    (sigmaAntidiagonalEquivProd.symm x).2 = (x.1.1, x.2.1)
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.divisorsAntidiagonalFactors (L31-L35)
def divisorsAntidiagonalFactors (n : ℕ+) : Nat.divisorsAntidiagonal n → ℕ+ × ℕ+ := fun x ↦
⟨⟨x.1.1, Nat.pos_of_mem_divisors (Nat.fst_mem_divisors_of_mem_antidiagonal x.2)⟩,
    (⟨x.1.2, Nat.pos_of_mem_divisors (Nat.snd_mem_divisors_of_mem_antidiagonal x.2)⟩ : ℕ+),
    Nat.pos_of_mem_divisors (Nat.snd_mem_divisors_of_mem_antidiagonal x.2)⟩
```

-- snippet 2
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.divisorsAntidiagonalFactors_eq (L36-L39)
lemma divisorsAntidiagonalFactors_eq {n : ℕ+} (x : Nat.divisorsAntidiagonal n) :
    (divisorsAntidiagonalFactors n x).1.1 * (divisorsAntidiagonalFactors n x).2.1 = n := by
simp [divisorsAntidiagonalFactors, (Nat.mem_divisorsAntidiagonal.mp x.2).1]
```

-- snippet 3
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.divisorsAntidiagonalFactors_one (L40-L47)
lemma divisorsAntidiagonalFactors_one (x : Nat.divisorsAntidiagonal 1) :
    (divisorsAntidiagonalFactors 1 x) = (1, 1) := by
have h := Nat.mem_divisorsAntidiagonal.mp x.2
  simp only [mul_eq_one, ne_eq, one_ne_zero, not_false_eq_true, and_true] at h
  simp [divisorsAntidiagonalFactors, h.1, h.2]

/-- The equivalence from the union over `n` of `Nat.divisorsAntidiagonal n` to `ℕ+ × ℕ+`
given by sending `n = a * b` to `(a, b)`. -/
```

-- snippet 4
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.sigmaAntidiagonalEquivProd (L48-L57)
def sigmaAntidiagonalEquivProd : (Σ n : ℕ+, Nat.divisorsAntidiagonal n) ≃ ℕ+ × ℕ+ where
  toFun x := divisorsAntidiagonalFactors x.1 x.2
invFun x :=
    ⟨⟨x.1.val * x.2.val, mul_pos x.1.2 x.2.2⟩, ⟨x.1, x.2⟩, by simp [Nat.mem_divisorsAntidiagonal]⟩
  left_inv := by
    rintro ⟨n, ⟨k, l⟩, h⟩
    rw [Nat.mem_divisorsAntidiagonal] at h
    ext <;> simp [divisorsAntidiagonalFactors, ← PNat.coe_injective.eq_iff, h.1]
  right_inv _ := rfl
```

-- snippet 5
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.sigmaAntidiagonalEquivProd_symm_apply_fst (L58-L60)
lemma sigmaAntidiagonalEquivProd_symm_apply_fst (x : ℕ+ × ℕ+) :
    (sigmaAntidiagonalEquivProd.symm x).1 = x.1.1 * x.2.1 := rfl
```

-- snippet 6
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.summable_norm_pow_mul_geometric_div_one_sub (L69-L98)
lemma summable_norm_pow_mul_geometric_div_one_sub (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    Summable fun n : ℕ ↦ n ^ k * r ^ n / (1 - r ^ n) := by
simp only [div_eq_mul_one_div (_ * _ ^ _)]
  apply Summable.mul_tendsto_const (c := 1 / (1 - 0))
    (by simpa using summable_norm_pow_mul_geometric_of_norm_lt_one k hr)
  simpa only [Nat.cofinite_eq_atTop] using
   tendsto_const_nhds.div ((tendsto_pow_atTop_nhds_zero_of_norm_lt_one hr).const_sub 1) (by simp)

private lemma summable_divisorsAntidiagonal_aux (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    Summable fun c : (n : ℕ+) × {x // x ∈ (n : ℕ).divisorsAntidiagonal} ↦
    (c.2.1.2) ^ k * (r ^ (c.2.1.1 * c.2.1.2)) := by
  apply Summable.of_norm
  rw [summable_sigma_of_nonneg (fun a ↦ by positivity)]
  constructor
  · exact fun n ↦ (hasSum_fintype _).summable
  · simp only [norm_mul, norm_pow, tsum_fintype, Finset.univ_eq_attach]
    apply Summable.of_nonneg_of_le (f := fun c : ℕ+ ↦ ‖(c : 𝕜) ^ (k + 1) * r ^ (c : ℕ)‖)
      (fun b ↦ Finset.sum_nonneg (fun _ _ ↦ mul_nonneg (by simp) (by simp))) (fun b ↦ ?_)
      (by apply (summable_norm_pow_mul_geometric_of_norm_lt_one (k + 1) hr).subtype)
    transitivity ∑ _ ∈ (b : ℕ).divisors, ‖(b : 𝕜)‖ ^ k * ‖r ^ (b : ℕ)‖
    · rw [(b : ℕ).divisorsAntidiagonal.sum_attach (fun x ↦ ‖(x.2 : 𝕜)‖ ^ _ * _ ^ (x.1 * x.2)),
          sum_divisorsAntidiagonal ((fun x y ↦ ‖(y : 𝕜)‖ ^ k * _ ^ (x * y)))]
      gcongr with i hi
      · simpa using le_of_dvd b.2 (div_dvd_of_dvd (dvd_of_mem_divisors hi))
      · rw [norm_pow, mul_comm, Nat.div_mul_cancel (dvd_of_mem_divisors hi)]
    · simp only [norm_pow, Finset.sum_const, nsmul_eq_mul, ← mul_assoc, add_comm k 1, pow_add,
        pow_one, norm_mul]
      gcongr
      simpa using Nat.card_divisors_le_self b
```

-- snippet 7
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.summable_prod_mul_pow (L99-L105)
theorem summable_prod_mul_pow (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    Summable fun c : (ℕ+ × ℕ+) ↦ c.2 ^ k * (r ^ (c.1 * c.2 : ℕ)) := by
simpa [sigmaAntidiagonalEquivProd.summable_iff.symm] using summable_divisorsAntidiagonal_aux k hr

-- access notation `σ`
open scoped sigma
```

-- snippet 8
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.tsum_prod_pow_eq_tsum_sigma (L106-L118)
theorem tsum_prod_pow_eq_tsum_sigma (k : ℕ) {r : 𝕜} (hr : ‖r‖ < 1) :
    ∑' d : ℕ+, ∑' c : ℕ+, c ^ k * r ^ (d * c : ℕ) = ∑' e : ℕ+, σ k e * r ^ (e : ℕ) := by
suffices ∑' c : ℕ+ × ℕ+, c.2 ^ k * r ^ (c.1 * c.2 : ℕ) =
    ∑' e : ℕ+, σ k e * r ^ (e : ℕ) by rwa [← (summable_prod_mul_pow k hr).tsum_prod]
  simp only [← sigmaAntidiagonalEquivProd.tsum_eq, sigmaAntidiagonalEquivProd,
    divisorsAntidiagonalFactors, PNat.mk_coe, Equiv.coe_fn_mk, sigma_eq_sum_div, cast_sum,
    cast_pow, Summable.tsum_sigma (summable_divisorsAntidiagonal_aux k hr)]
  refine tsum_congr fun n ↦ ?_
  simpa [tsum_fintype, Finset.sum_mul,
    (n : ℕ).divisorsAntidiagonal.sum_attach fun x : ℕ × ℕ ↦ x.2 ^ k * r ^ (x.1 * x.2),
    sum_divisorsAntidiagonal fun x y ↦ y ^ k * r ^ (x * y)]
      using Finset.sum_congr rfl fun i hi ↦ by rw [Nat.mul_div_cancel' (dvd_of_mem_divisors hi)]
```

-- snippet 9
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.tsum_pow_div_one_sub_eq_tsum_sigma (L119-L130)
lemma tsum_pow_div_one_sub_eq_tsum_sigma {r : 𝕜} (hr : ‖r‖ < 1) (k : ℕ) :
    ∑' n : ℕ+, n ^ k * r ^ (n : ℕ) / (1 - r ^ (n : ℕ)) = ∑' n : ℕ+, σ k n * r ^ (n : ℕ) := by
have (m : ℕ) [NeZero m] := tsum_geometric_of_norm_lt_one (ξ := r ^ m)
    (by simpa using pow_lt_one₀ (by simp) hr (NeZero.ne _))
  simp only [div_eq_mul_inv, ← this, ← tsum_mul_left, mul_assoc, ← _root_.pow_succ',
    ← fun (n : ℕ) ↦ tsum_pnat_eq_tsum_succ (f := fun m ↦ n ^ k * (r ^ n) ^ m)]
  have h00 := tsum_prod_pow_eq_tsum_sigma k hr
  rw [Summable.tsum_comm (by apply (summable_prod_mul_pow k hr).prod_symm)] at h00
  rw [← h00]
  exact tsum_congr₂ <| fun b c ↦ by simp [mul_comm c.val b.val, pow_mul]

omit [CompleteSpace 𝕜] [NormSMulClass ℤ 𝕜] in
```

-- snippet 10
```lean
-- Mathlib.NumberTheory.TsumDivisorsAntidiagonal.tendsto_zero_geometric_tsum_pnat (L131-L143)
lemma tendsto_zero_geometric_tsum_pnat {r : 𝕜} (hr : ‖r‖ < 1) :
    Tendsto (fun m : ℕ+ ↦ ∑' n : ℕ+, r ^ (n * m : ℕ)) atTop (𝓝 0) := by
have h1 (m : ℕ+) : ‖r ^ (m : ℕ)‖ < 1 := by
    rwa [norm_pow, pow_lt_one_iff_of_nonneg (norm_nonneg _) (NeZero.ne _)]
  have h2 (m : ℕ+) : ∑' n : ℕ+, r ^ (n * m : ℕ) = (1 - r ^ (m : ℕ))⁻¹ - 1 := by
    have := tsum_geometric_of_norm_lt_one (h1 m)
    rw [← tsum_zero_pnat_eq_tsum_nat (summable_geometric_of_norm_lt_one (h1 m))] at this
    simp_rw [← this, pow_zero, add_sub_cancel_left, mul_comm, pow_mul]
  rw [funext h2, (by simp : 𝓝 (0 : 𝕜) = 𝓝 ((1 - 0)⁻¹ - 1)), tendsto_sub_const_iff,
    tendsto_inv_iff₀ (by simp), tendsto_const_sub_iff]
  exact (tendsto_pow_atTop_nhds_zero_of_norm_lt_one hr).comp <| tendsto_PNat_val_atTop_atTop

end tsum
```
[/INST]

mistral-common:Tekken v15 (Mistral chat template, [INST]/[SYSTEM_PROMPT])


[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.Topology.Baire.BaireMeasurable).

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

```lean
theorem MeasurableSet.residualEq_isOpen [MeasurableSpace α] [BorelSpace α] (h : MeasurableSet s) :
    ∃ u : Set α, IsOpen u ∧ s =ᵇ u
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.coborder_mem_residual (L50-L52)
theorem coborder_mem_residual {s : Set α} (hs : IsLocallyClosed s) : coborder s ∈ residual α :=
residual_of_dense_open hs.isOpen_coborder dense_coborder
```

-- snippet 2
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.closure_residualEq (L53-L63)
theorem closure_residualEq {s : Set α} (hs : IsLocallyClosed s) : closure s =ᵇ s := by
rw [Filter.eventuallyEq_set]
  filter_upwards [coborder_mem_residual hs] with x hx
  nth_rewrite 2 [← closure_inter_coborder (s := s)]
  simp [hx]

/-- We say a set is a `BaireMeasurableSet` if it differs from some Borel set by
a meager set. This forms a σ-algebra.

It is equivalent, and a more standard definition, to say that the set differs from
some *open* set by a meager set. See `BaireMeasurableSet.iff_residualEq_isOpen` -/
```

-- snippet 3
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L64-L70)
def BaireMeasurableSet (s : Set α) : Prop :=
@MeasurableSet _ (eventuallyMeasurableSpace (borel _) (residual _)) s

variable {s t : Set α}

namespace BaireMeasurableSet
```

-- snippet 4
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.of_mem_residual (L71-L73)
theorem of_mem_residual (h : s ∈ residual _) : BaireMeasurableSet s :=
eventuallyMeasurableSet_of_mem_filter (α := α) h
```

-- snippet 5
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.compl (L83-L84)
theorem compl (h : BaireMeasurableSet s) : BaireMeasurableSet sᶜ := MeasurableSet.compl h
```

-- snippet 6
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.of_compl (L85-L86)
theorem of_compl (h : BaireMeasurableSet sᶜ) : BaireMeasurableSet s := MeasurableSet.of_compl h
```

-- snippet 7
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.iUnion (L90-L93)
theorem iUnion {ι : Sort*} [Countable ι] {s : ι → Set α}
    (h : ∀ i, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋃ i, s i) :=
MeasurableSet.iUnion h
```

-- snippet 8
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.biUnion (L94-L97)
theorem biUnion {ι : Type*} {s : ι → Set α} {t : Set ι} (ht : t.Countable)
    (h : ∀ i ∈ t, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋃ i ∈ t, s i) :=
MeasurableSet.biUnion ht h
```

-- snippet 9
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.sUnion (L98-L101)
theorem sUnion {s : Set (Set α)} (hs : s.Countable)
    (h : ∀ t ∈ s, BaireMeasurableSet t) : BaireMeasurableSet (⋃₀ s) :=
MeasurableSet.sUnion hs h
```

-- snippet 10
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.iInter (L102-L105)
theorem iInter {ι : Sort*} [Countable ι] {s : ι → Set α}
    (h : ∀ i, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋂ i, s i) :=
MeasurableSet.iInter h
```

-- snippet 11
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.biInter (L106-L109)
theorem biInter {ι : Type*} {s : ι → Set α} {t : Set ι} (ht : t.Countable)
    (h : ∀ i ∈ t, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋂ i ∈ t, s i) :=
MeasurableSet.biInter ht h
```

-- snippet 12
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.sInter (L110-L113)
theorem sInter {s : Set (Set α)} (hs : s.Countable)
    (h : ∀ t ∈ s, BaireMeasurableSet t) : BaireMeasurableSet (⋂₀ s) :=
MeasurableSet.sInter hs h
```

-- snippet 13
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.union (L114-L117)
theorem union (hs : BaireMeasurableSet s) (ht : BaireMeasurableSet t) :
    BaireMeasurableSet (s ∪ t) :=
MeasurableSet.union hs ht
```

-- snippet 14
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.inter (L118-L121)
theorem inter (hs : BaireMeasurableSet s) (ht : BaireMeasurableSet t) :
    BaireMeasurableSet (s ∩ t) :=
MeasurableSet.inter hs ht
```

-- snippet 15
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.diff (L122-L125)
theorem diff (hs : BaireMeasurableSet s) (ht : BaireMeasurableSet t) :
    BaireMeasurableSet (s \ t) :=
MeasurableSet.diff hs ht
```

-- snippet 16
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.congr (L126-L133)
theorem congr (hs : BaireMeasurableSet s) (h : s =ᵇ t) : BaireMeasurableSet t :=
EventuallyMeasurableSet.congr (α := α) hs h.symm

end BaireMeasurableSet

open Filter

/-- Any Borel set differs from some open set by a meager set. -/
```

-- snippet 17
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L147-L154)
theorem BaireMeasurableSet.residualEq_isOpen (h : BaireMeasurableSet s) :
    ∃ u : Set α, (IsOpen u) ∧ s =ᵇ u := by
borelize α
  rcases h with ⟨t, ht, hst⟩
  rcases ht.residualEq_isOpen with ⟨u, hu, htu⟩
  exact ⟨u, hu, hst.trans htu⟩

/-- A set is Baire measurable if and only if it differs from some open set by a meager set. -/
```

-- snippet 18
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L155-L164)
theorem BaireMeasurableSet.iff_residualEq_isOpen :
    BaireMeasurableSet s ↔ ∃ u : Set α, (IsOpen u) ∧ s =ᵇ u :=
⟨fun h => h.residualEq_isOpen, fun ⟨_, uo, ueq⟩ => uo.baireMeasurableSet.congr ueq.symm⟩

section Map

open Set

variable {f : α → β}
```

-- snippet 19
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.tendsto_residual_of_isOpenMap (L165-L171)
theorem tendsto_residual_of_isOpenMap (hc : Continuous f) (ho : IsOpenMap f) :
    Tendsto f (residual α) (residual β) := by
apply le_countableGenerate_iff_of_countableInterFilter.mpr
  rintro t ⟨ht, htd⟩
  exact residual_of_dense_open (ht.preimage hc) (htd.preimage ho)

/-- The preimage of a meager set under a continuous open map is meager. -/
```

-- snippet 20
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.IsMeagre (L172-L176)
theorem IsMeagre.preimage_of_isOpenMap (hc : Continuous f) (ho : IsOpenMap f)
    {s : Set β} (h : IsMeagre s) : IsMeagre (f ⁻¹' s) :=
tendsto_residual_of_isOpenMap hc ho h

/-- The preimage of a `BaireMeasurableSet` under a continuous open map is Baire measurable. -/
```

-- snippet 21
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L177-L183)
theorem BaireMeasurableSet.preimage (hc : Continuous f) (ho : IsOpenMap f)
    {s : Set β} (h : BaireMeasurableSet s) : BaireMeasurableSet (f ⁻¹' s) := by
rcases h with ⟨u, hu, hsu⟩
  refine ⟨f ⁻¹' u, ?_, hsu.filter_mono <| tendsto_residual_of_isOpenMap hc ho⟩
  borelize α β
  exact hc.measurable hu
```

-- snippet 22
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.Homeomorph (L184-L189)
theorem Homeomorph.residual_map_eq (h : α ≃ₜ β) : (residual α).map h = residual β := by
refine le_antisymm (tendsto_residual_of_isOpenMap h.continuous h.isOpenMap) (le_map ?_)
  simp_rw [← preimage_symm]
  exact tendsto_residual_of_isOpenMap h.symm.continuous h.symm.isOpenMap

end Map
```

-- snippet 23
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_sInter_of_Gδ (L165-L171)
theorem dense_sInter_of_Gδ {S : Set (Set X)} (ho : ∀ s ∈ S, IsGδ s) (hS : S.Countable)
    (hd : ∀ s ∈ S, Dense s) : Dense (⋂₀ S) :=
dense_of_mem_residual ((countable_sInter_mem hS).mpr
    (fun _ hs => residual_of_dense_Gδ (ho _ hs) (hd _ hs)))

/-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with
an index set which is a countable type. -/
```

-- snippet 24
```lean
-- Mathlib.Topology.Baire.Lemmas.Set (L48-L57)
theorem Set.Finite.dense_sInter {s : Set (Set X)} (hs : s.Finite)
    (ho : ∀ t ∈ s, IsOpen t) (hd : ∀ t ∈ s, Dense t) : Dense (⋂₀ s) := by
induction s, hs using Set.Finite.induction_on with
  | empty => simp [sInter_empty]
  | insert ha hsf ih =>
    simp only [sInter_insert, forall_mem_insert] at hd ⊢
    refine hd.1.inter_of_isOpen_right ?_ (hsf.isOpen_sInter (fun y hy => ho y (Or.inr hy)))
    exact ih ((fun y hy => ho y (Or.inr hy))) (fun y hy => hd.2 y hy)

/-- A finite set is Baire. -/
```

-- snippet 25
```lean
-- Mathlib.Topology.Baire.Lemmas.IsGδ (L69-L80)
theorem IsGδ.baireSpace_of_dense {s : Set X} (hG : IsGδ s) (hd : Dense s) : BaireSpace s := by
constructor
  intro f hof hdf
  obtain ⟨V, hV⟩ : ∃ V : ℕ → Set X, (∀ n, IsOpen (V n)) ∧ s = ⋂ n, V n := eq_iInter_nat hG
  choose g hg1 hg2 hg3 using fun n => exists_open_dense_of_open_dense_subtype hd (hof n) (hdf n)
  have h_inter_dense : Dense (⋂ n, g n ∩ V n) := BaireSpace.baire_property (fun n ↦ g n ∩ V n)
    (fun n => (hg1 n).inter (hV.1 n))
    (fun n => (hg2 n).inter_of_isOpen_left (hd.mono (by simp [hV.2, iInter_subset])) (hg1 n))
  have h_inter_eq : ⋂ n, g n ∩ V n = ⋂ n, f n := by ext; simp_all; grind
  exact Subtype.dense_iff.mpr fun a _ ↦ h_inter_eq ▸ h_inter_dense a

/-- If `p : Y → X` is an open embedding and `X` is a Baire space, then `Y` is a Baire space. -/
```

-- snippet 26
```lean
-- Mathlib.Topology.Baire.Lemmas.eventually_residual (L145-L149)
theorem eventually_residual {p : X → Prop} :
    (∀ᶠ x in residual X, p x) ↔ ∃ t : Set X, IsGδ t ∧ Dense t ∧ ∀ x ∈ t, p x := by
simp only [Filter.Eventually, mem_residual, subset_def, mem_setOf_eq]
  tauto
```

-- snippet 27
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iInter_of_isOpen_nat (L64-L68)
theorem dense_iInter_of_isOpen_nat {f : ℕ → Set X} (ho : ∀ n, IsOpen (f n))
    (hd : ∀ n, Dense (f n)) : Dense (⋂ n, f n) :=
BaireSpace.baire_property f ho hd

/-- A dense Gδ subset of a Baire space is Baire. -/
```

-- snippet 28
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iInter_of_isOpen (L130-L134)
theorem dense_iInter_of_isOpen [Countable ι] {f : ι → Set X} (ho : ∀ i, IsOpen (f i))
    (hd : ∀ i, Dense (f i)) : Dense (⋂ s, f s) :=
dense_sInter_of_isOpen (forall_mem_range.2 ho) (countable_range _) (forall_mem_range.2 hd)

/-- A set is residual (comeagre) if and only if it includes a dense `Gδ` set. -/
```

-- snippet 29
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_of_mem_residual (L150-L157)
theorem dense_of_mem_residual {s : Set X} (hs : s ∈ residual X) : Dense s :=
let ⟨_, hts, _, hd⟩ := mem_residual.1 hs
  hd.mono hts

/--
In a Baire space, every nonempty open set is non‐meagre,
that is, it cannot be written as a countable union of nowhere‐dense sets.
-/
```

-- snippet 30
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_biInter_of_isOpen (L123-L129)
theorem dense_biInter_of_isOpen {S : Set α} {f : α → Set X} (ho : ∀ s ∈ S, IsOpen (f s))
    (hS : S.Countable) (hd : ∀ s ∈ S, Dense (f s)) : Dense (⋂ s ∈ S, f s) := by
rw [← sInter_image]
  refine dense_sInter_of_isOpen ?_ (hS.image _) ?_ <;> rwa [forall_mem_image]

/-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with
an index set which is a countable type. -/
```

-- snippet 31
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iInter_of_Gδ (L172-L177)
theorem dense_iInter_of_Gδ [Countable ι] {f : ι → Set X} (ho : ∀ s, IsGδ (f s))
    (hd : ∀ s, Dense (f s)) : Dense (⋂ s, f s) :=
dense_sInter_of_Gδ (forall_mem_range.2 ‹_›) (countable_range _) (forall_mem_range.2 ‹_›)

/-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with
an index set which is a countable set in any type. -/
```

-- snippet 32
```lean
-- Mathlib.Topology.Baire.Lemmas.baire_of_finite (L58-L63)
theorem baire_of_finite [Finite X] : BaireSpace X where
  baire_property f _ _ := sInter_range f ▸ (toFinite (range f)).dense_sInter (by grind) (by grind)
variable [BaireSpace X]

/-- Definition of a Baire space. -/
```

-- snippet 33
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_biUnion_interior_of_closed (L225-L230)
theorem dense_biUnion_interior_of_closed {S : Set α} {f : α → Set X} (hc : ∀ s ∈ S, IsClosed (f s))
    (hS : S.Countable) (hU : ⋃ s ∈ S, f s = univ) : Dense (⋃ s ∈ S, interior (f s)) :=
IsGδ.univ.dense_biUnion_interior_of_closed dense_univ hS hc hU.ge

/-- Baire theorem: if countably many closed sets cover the whole space, then their interiors
are dense. Formulated here with `⋃₀`. -/
```

-- snippet 34
```lean
-- Mathlib.Topology.Baire.Lemmas.not_isMeagre_of_isOpen (L158-L164)
theorem not_isMeagre_of_isOpen {s : Set X} (hs : IsOpen s) (hne : s.Nonempty) : ¬ IsMeagre s := by
intro h
  obtain ⟨x, hx, hxc⟩ :=
    (dense_of_mem_residual (by rwa [IsMeagre] at h)).inter_open_nonempty s hs hne
  exact hxc hx

/-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with ⋂₀. -/
```

-- snippet 35
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_biInter_of_Gδ (L178-L184)
theorem dense_biInter_of_Gδ {S : Set α} {f : ∀ x ∈ S, Set X} (ho : ∀ s (H : s ∈ S), IsGδ (f s H))
    (hS : S.Countable) (hd : ∀ s (H : s ∈ S), Dense (f s H)) : Dense (⋂ s ∈ S, f s ‹_›) := by
rw [biInter_eq_iInter]
  haveI := hS.to_subtype
  exact dense_iInter_of_Gδ (fun s => ho s s.2) fun s => hd s s.2

/-- Baire theorem: the intersection of two dense Gδ sets is dense. -/
```

-- snippet 36
```lean
-- Mathlib.Topology.Baire.Lemmas.not_isMeagre_of_mem_residual (L258-L264)
theorem not_isMeagre_of_mem_residual {s : Set X} (hs : s ∈ residual X) :
    ¬ IsMeagre s := by
rcases (mem_residual (X := X)).1 hs with ⟨t, ht_sub, htGδ, ht_dense⟩
  intro hs_meagre
  exact not_isMeagre_of_isGδ_of_dense (X := X) htGδ ht_dense (hs_meagre.mono ht_sub)

end BaireTheorem
```

-- snippet 37
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iUnion_interior_of_closed (L237-L244)
theorem dense_iUnion_interior_of_closed [Countable ι] {f : ι → Set X} (hc : ∀ i, IsClosed (f i))
    (hU : ⋃ i, f i = univ) : Dense (⋃ i, interior (f i)) :=
IsGδ.univ.dense_iUnion_interior_of_closed dense_univ hc hU.ge

variable [Nonempty X]

/-- One of the most useful consequences of Baire theorem: if a countable union of closed sets
covers the space, then one of the sets has nonempty interior. -/
```

-- snippet 38
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_sInter_of_isOpen (L114-L122)
theorem dense_sInter_of_isOpen {S : Set (Set X)} (ho : ∀ s ∈ S, IsOpen s) (hS : S.Countable)
    (hd : ∀ s ∈ S, Dense s) : Dense (⋂₀ S) := by
rcases S.eq_empty_or_nonempty with h | h
  · simp [h]
  · rcases hS.exists_eq_range h with ⟨f, rfl⟩
    exact dense_iInter_of_isOpen_nat (forall_mem_range.1 ho) (forall_mem_range.1 hd)

/-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with
an index set which is a countable set in any type. -/
```

-- snippet 39
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_sUnion_interior_of_closed (L231-L236)
theorem dense_sUnion_interior_of_closed {S : Set (Set X)} (hc : ∀ s ∈ S, IsClosed s)
    (hS : S.Countable) (hU : ⋃₀ S = univ) : Dense (⋃ s ∈ S, interior s) :=
IsGδ.univ.dense_sUnion_interior_of_closed dense_univ hS hc hU.ge

/-- Baire theorem: if countably many closed sets cover the whole space, then their interiors
are dense. Formulated here with an index set which is a countable type. -/
```

-- snippet 40
```lean
-- Mathlib.Topology.Baire.Lemmas.mem_residual (L135-L144)
theorem mem_residual {s : Set X} : s ∈ residual X ↔ ∃ t ⊆ s, IsGδ t ∧ Dense t := by
constructor
  · rw [mem_residual_iff]
    rintro ⟨S, hSo, hSd, Sct, Ss⟩
    refine ⟨_, Ss, ⟨_, fun t ht => hSo _ ht, Sct, rfl⟩, ?_⟩
    exact dense_sInter_of_isOpen hSo Sct hSd
  rintro ⟨t, ts, ho, hd⟩
  exact mem_of_superset (residual_of_dense_Gδ ho hd) ts

/-- A property holds on a residual (comeagre) set if and only if it holds on some dense `Gδ` set. -/
```

-- snippet 41
```lean
-- Mathlib.Topology.Baire.Lemmas.not_isMeagre_of_isGδ_of_dense (L250-L257)
theorem not_isMeagre_of_isGδ_of_dense {s : Set X} (hs : IsGδ s) (hd : Dense s) :
    ¬ IsMeagre s := by
intro h
  rcases mem_residual.1 h with ⟨t, hts, htG, hd'⟩
  rcases (hd.inter_of_Gδ hs htG hd').nonempty with ⟨x, hx₁, hx₂⟩
  exact hts hx₂ hx₁

/-- In a nonempty Baire space, a residual set is not meagre. -/
```

-- snippet 42
```lean
-- Mathlib.Topology.Baire.LocallyCompactRegular.IsGδ (L62-L72)
theorem IsGδ.of_t2Space_locallyCompactSpace (hG : IsGδ s) : BaireSpace s := by
have : BaireSpace (closure s) := by
    convert BaireSpace.of_t2Space_locallyCompactSpace using 1
    · infer_instance
    · exact isClosed_closure.locallyCompactSpace
  have : BaireSpace ((↑) ⁻¹' s : Set (closure s)) :=
    (isGδ_induced continuous_subtype_val hG).baireSpace_of_dense
    (by simp [Subtype.dense_iff, inter_eq_right.mpr subset_closure])
  have h_homeo : Homeomorph ((↑) ⁻¹' s : Set (closure s)) s := ⟨⟨fun x => ⟨x, x.2⟩,
    fun x => ⟨⟨x, subset_closure x.2⟩, x.2⟩, by grind, by grind⟩, by fun_prop, by fun_prop⟩
  exact h_homeo.baireSpace
```

-- snippet 43
```lean
-- Mathlib.Topology.Baire.Lemmas.IsOpen (L110-L113)
theorem IsOpen.baireSpace {s : Set X} (hO : IsOpen s) : BaireSpace s :=
hO.isOpenEmbedding_subtypeVal.baireSpace

/-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with ⋂₀. -/
```

-- snippet 44
```lean
-- Mathlib.Topology.Baire.Lemmas.Dense (L185-L191)
theorem Dense.inter_of_Gδ {s t : Set X} (hs : IsGδ s) (ht : IsGδ t) (hsc : Dense s)
    (htc : Dense t) : Dense (s ∩ t) := by
rw [inter_eq_iInter]
  apply dense_iInter_of_Gδ <;> simp [Bool.forall_bool, *]

/-- If a countable family of closed sets cover a dense `Gδ` set, then the union of their interiors
is dense. Formulated here with `⋃`. -/
```

-- snippet 45
```lean
-- Mathlib.Topology.Baire.Lemmas.nonempty_interior_of_iUnion_of_closed (L245-L249)
theorem nonempty_interior_of_iUnion_of_closed [Countable ι] {f : ι → Set X}
    (hc : ∀ i, IsClosed (f i)) (hU : ⋃ i, f i = univ) : ∃ i, (interior <| f i).Nonempty := by
simpa using (dense_iUnion_interior_of_closed hc hU).nonempty

/-- In a nonempty Baire space, any dense `Gδ` set is not meagre. -/
```

-- snippet 46
```lean
-- Mathlib.Topology.Baire.Lemmas.Topology (L81-L109)
theorem Topology.IsOpenEmbedding.baireSpace {Y : Type*} [TopologicalSpace Y] {p : Y → X}
    (hp : Topology.IsOpenEmbedding p) : BaireSpace Y := by
constructor
  intro f hof hdf
  let s := range p
  let c := fun n : ℕ => p '' f n ∪ (closure s)ᶜ
  have c_open (n : ℕ) : IsOpen (c n) := IsOpen.union (hp.isOpenMap (f n) (hof n))
    isClosed_closure.isOpen_compl
  have c_dense (n : ℕ) : Dense (c n) := by
    rw [dense_iff_closure_eq, subset_antisymm_iff]
    have : univ ⊆ closure (c n) := calc
      _ ⊆ (interior (closure s)) ∪ (interior (closure s))ᶜ := by grind
      _ ⊆ closure s ∪ (interior (closure s))ᶜ := by gcongr; exact interior_subset
      _ ⊆ closure (p '' f n) ∪ (interior (closure s))ᶜ := union_subset_union
          (closure_minimal (hp.continuous.range_subset_closure_image_dense (hdf n))
          isClosed_closure) (subset_refl (interior (closure s))ᶜ)
      _ ⊆ closure (p '' f n) ∪ closure ((closure s)ᶜ) := union_subset_union (by simp) (by simp)
      _ = closure (c n) := closure_union.symm
    grind
  have c_inter_dense : Dense (⋂ n, c n) := dense_iInter_of_isOpen_nat c_open c_dense
  have c_inter_eq : ⋂ n, f n = p ⁻¹' (⋂ n, c n) := by
    ext x
    simp only [mem_iInter, mem_preimage, mem_union, mem_compl_iff, c]
    refine ⟨fun h i => by grind, fun h i => ?_⟩
    exact hp.injective.mem_set_image.mp (imp_iff_or_not.mpr (h i)
      (subset_closure (mem_range_self x)))
  exact c_inter_eq ▸ Dense.preimage c_inter_dense hp.isOpenMap

/-- An open subset of a Baire space is Baire. -/
```

<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.Topology.Baire.BaireMeasurable).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]Prove the following theorem. The retrieved context below may help.

```lean
theorem MeasurableSet.residualEq_isOpen [MeasurableSpace α] [BorelSpace α] (h : MeasurableSet s) :
    ∃ u : Set α, IsOpen u ∧ s =ᵇ u
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.coborder_mem_residual (L50-L52)
theorem coborder_mem_residual {s : Set α} (hs : IsLocallyClosed s) : coborder s ∈ residual α :=
residual_of_dense_open hs.isOpen_coborder dense_coborder
```

-- snippet 2
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.closure_residualEq (L53-L63)
theorem closure_residualEq {s : Set α} (hs : IsLocallyClosed s) : closure s =ᵇ s := by
rw [Filter.eventuallyEq_set]
  filter_upwards [coborder_mem_residual hs] with x hx
  nth_rewrite 2 [← closure_inter_coborder (s := s)]
  simp [hx]

/-- We say a set is a `BaireMeasurableSet` if it differs from some Borel set by
a meager set. This forms a σ-algebra.

It is equivalent, and a more standard definition, to say that the set differs from
some *open* set by a meager set. See `BaireMeasurableSet.iff_residualEq_isOpen` -/
```

-- snippet 3
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L64-L70)
def BaireMeasurableSet (s : Set α) : Prop :=
@MeasurableSet _ (eventuallyMeasurableSpace (borel _) (residual _)) s

variable {s t : Set α}

namespace BaireMeasurableSet
```

-- snippet 4
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.of_mem_residual (L71-L73)
theorem of_mem_residual (h : s ∈ residual _) : BaireMeasurableSet s :=
eventuallyMeasurableSet_of_mem_filter (α := α) h
```

-- snippet 5
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.compl (L83-L84)
theorem compl (h : BaireMeasurableSet s) : BaireMeasurableSet sᶜ := MeasurableSet.compl h
```

-- snippet 6
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.of_compl (L85-L86)
theorem of_compl (h : BaireMeasurableSet sᶜ) : BaireMeasurableSet s := MeasurableSet.of_compl h
```

-- snippet 7
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.iUnion (L90-L93)
theorem iUnion {ι : Sort*} [Countable ι] {s : ι → Set α}
    (h : ∀ i, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋃ i, s i) :=
MeasurableSet.iUnion h
```

-- snippet 8
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.biUnion (L94-L97)
theorem biUnion {ι : Type*} {s : ι → Set α} {t : Set ι} (ht : t.Countable)
    (h : ∀ i ∈ t, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋃ i ∈ t, s i) :=
MeasurableSet.biUnion ht h
```

-- snippet 9
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.sUnion (L98-L101)
theorem sUnion {s : Set (Set α)} (hs : s.Countable)
    (h : ∀ t ∈ s, BaireMeasurableSet t) : BaireMeasurableSet (⋃₀ s) :=
MeasurableSet.sUnion hs h
```

-- snippet 10
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.iInter (L102-L105)
theorem iInter {ι : Sort*} [Countable ι] {s : ι → Set α}
    (h : ∀ i, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋂ i, s i) :=
MeasurableSet.iInter h
```

-- snippet 11
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.biInter (L106-L109)
theorem biInter {ι : Type*} {s : ι → Set α} {t : Set ι} (ht : t.Countable)
    (h : ∀ i ∈ t, BaireMeasurableSet (s i)) : BaireMeasurableSet (⋂ i ∈ t, s i) :=
MeasurableSet.biInter ht h
```

-- snippet 12
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.sInter (L110-L113)
theorem sInter {s : Set (Set α)} (hs : s.Countable)
    (h : ∀ t ∈ s, BaireMeasurableSet t) : BaireMeasurableSet (⋂₀ s) :=
MeasurableSet.sInter hs h
```

-- snippet 13
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.union (L114-L117)
theorem union (hs : BaireMeasurableSet s) (ht : BaireMeasurableSet t) :
    BaireMeasurableSet (s ∪ t) :=
MeasurableSet.union hs ht
```

-- snippet 14
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.inter (L118-L121)
theorem inter (hs : BaireMeasurableSet s) (ht : BaireMeasurableSet t) :
    BaireMeasurableSet (s ∩ t) :=
MeasurableSet.inter hs ht
```

-- snippet 15
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.diff (L122-L125)
theorem diff (hs : BaireMeasurableSet s) (ht : BaireMeasurableSet t) :
    BaireMeasurableSet (s \ t) :=
MeasurableSet.diff hs ht
```

-- snippet 16
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.congr (L126-L133)
theorem congr (hs : BaireMeasurableSet s) (h : s =ᵇ t) : BaireMeasurableSet t :=
EventuallyMeasurableSet.congr (α := α) hs h.symm

end BaireMeasurableSet

open Filter

/-- Any Borel set differs from some open set by a meager set. -/
```

-- snippet 17
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L147-L154)
theorem BaireMeasurableSet.residualEq_isOpen (h : BaireMeasurableSet s) :
    ∃ u : Set α, (IsOpen u) ∧ s =ᵇ u := by
borelize α
  rcases h with ⟨t, ht, hst⟩
  rcases ht.residualEq_isOpen with ⟨u, hu, htu⟩
  exact ⟨u, hu, hst.trans htu⟩

/-- A set is Baire measurable if and only if it differs from some open set by a meager set. -/
```

-- snippet 18
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L155-L164)
theorem BaireMeasurableSet.iff_residualEq_isOpen :
    BaireMeasurableSet s ↔ ∃ u : Set α, (IsOpen u) ∧ s =ᵇ u :=
⟨fun h => h.residualEq_isOpen, fun ⟨_, uo, ueq⟩ => uo.baireMeasurableSet.congr ueq.symm⟩

section Map

open Set

variable {f : α → β}
```

-- snippet 19
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.tendsto_residual_of_isOpenMap (L165-L171)
theorem tendsto_residual_of_isOpenMap (hc : Continuous f) (ho : IsOpenMap f) :
    Tendsto f (residual α) (residual β) := by
apply le_countableGenerate_iff_of_countableInterFilter.mpr
  rintro t ⟨ht, htd⟩
  exact residual_of_dense_open (ht.preimage hc) (htd.preimage ho)

/-- The preimage of a meager set under a continuous open map is meager. -/
```

-- snippet 20
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.IsMeagre (L172-L176)
theorem IsMeagre.preimage_of_isOpenMap (hc : Continuous f) (ho : IsOpenMap f)
    {s : Set β} (h : IsMeagre s) : IsMeagre (f ⁻¹' s) :=
tendsto_residual_of_isOpenMap hc ho h

/-- The preimage of a `BaireMeasurableSet` under a continuous open map is Baire measurable. -/
```

-- snippet 21
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.BaireMeasurableSet (L177-L183)
theorem BaireMeasurableSet.preimage (hc : Continuous f) (ho : IsOpenMap f)
    {s : Set β} (h : BaireMeasurableSet s) : BaireMeasurableSet (f ⁻¹' s) := by
rcases h with ⟨u, hu, hsu⟩
  refine ⟨f ⁻¹' u, ?_, hsu.filter_mono <| tendsto_residual_of_isOpenMap hc ho⟩
  borelize α β
  exact hc.measurable hu
```

-- snippet 22
```lean
-- Mathlib.Topology.Baire.BaireMeasurable.Homeomorph (L184-L189)
theorem Homeomorph.residual_map_eq (h : α ≃ₜ β) : (residual α).map h = residual β := by
refine le_antisymm (tendsto_residual_of_isOpenMap h.continuous h.isOpenMap) (le_map ?_)
  simp_rw [← preimage_symm]
  exact tendsto_residual_of_isOpenMap h.symm.continuous h.symm.isOpenMap

end Map
```

-- snippet 23
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_sInter_of_Gδ (L165-L171)
theorem dense_sInter_of_Gδ {S : Set (Set X)} (ho : ∀ s ∈ S, IsGδ s) (hS : S.Countable)
    (hd : ∀ s ∈ S, Dense s) : Dense (⋂₀ S) :=
dense_of_mem_residual ((countable_sInter_mem hS).mpr
    (fun _ hs => residual_of_dense_Gδ (ho _ hs) (hd _ hs)))

/-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with
an index set which is a countable type. -/
```

-- snippet 24
```lean
-- Mathlib.Topology.Baire.Lemmas.Set (L48-L57)
theorem Set.Finite.dense_sInter {s : Set (Set X)} (hs : s.Finite)
    (ho : ∀ t ∈ s, IsOpen t) (hd : ∀ t ∈ s, Dense t) : Dense (⋂₀ s) := by
induction s, hs using Set.Finite.induction_on with
  | empty => simp [sInter_empty]
  | insert ha hsf ih =>
    simp only [sInter_insert, forall_mem_insert] at hd ⊢
    refine hd.1.inter_of_isOpen_right ?_ (hsf.isOpen_sInter (fun y hy => ho y (Or.inr hy)))
    exact ih ((fun y hy => ho y (Or.inr hy))) (fun y hy => hd.2 y hy)

/-- A finite set is Baire. -/
```

-- snippet 25
```lean
-- Mathlib.Topology.Baire.Lemmas.IsGδ (L69-L80)
theorem IsGδ.baireSpace_of_dense {s : Set X} (hG : IsGδ s) (hd : Dense s) : BaireSpace s := by
constructor
  intro f hof hdf
  obtain ⟨V, hV⟩ : ∃ V : ℕ → Set X, (∀ n, IsOpen (V n)) ∧ s = ⋂ n, V n := eq_iInter_nat hG
  choose g hg1 hg2 hg3 using fun n => exists_open_dense_of_open_dense_subtype hd (hof n) (hdf n)
  have h_inter_dense : Dense (⋂ n, g n ∩ V n) := BaireSpace.baire_property (fun n ↦ g n ∩ V n)
    (fun n => (hg1 n).inter (hV.1 n))
    (fun n => (hg2 n).inter_of_isOpen_left (hd.mono (by simp [hV.2, iInter_subset])) (hg1 n))
  have h_inter_eq : ⋂ n, g n ∩ V n = ⋂ n, f n := by ext; simp_all; grind
  exact Subtype.dense_iff.mpr fun a _ ↦ h_inter_eq ▸ h_inter_dense a

/-- If `p : Y → X` is an open embedding and `X` is a Baire space, then `Y` is a Baire space. -/
```

-- snippet 26
```lean
-- Mathlib.Topology.Baire.Lemmas.eventually_residual (L145-L149)
theorem eventually_residual {p : X → Prop} :
    (∀ᶠ x in residual X, p x) ↔ ∃ t : Set X, IsGδ t ∧ Dense t ∧ ∀ x ∈ t, p x := by
simp only [Filter.Eventually, mem_residual, subset_def, mem_setOf_eq]
  tauto
```

-- snippet 27
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iInter_of_isOpen_nat (L64-L68)
theorem dense_iInter_of_isOpen_nat {f : ℕ → Set X} (ho : ∀ n, IsOpen (f n))
    (hd : ∀ n, Dense (f n)) : Dense (⋂ n, f n) :=
BaireSpace.baire_property f ho hd

/-- A dense Gδ subset of a Baire space is Baire. -/
```

-- snippet 28
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iInter_of_isOpen (L130-L134)
theorem dense_iInter_of_isOpen [Countable ι] {f : ι → Set X} (ho : ∀ i, IsOpen (f i))
    (hd : ∀ i, Dense (f i)) : Dense (⋂ s, f s) :=
dense_sInter_of_isOpen (forall_mem_range.2 ho) (countable_range _) (forall_mem_range.2 hd)

/-- A set is residual (comeagre) if and only if it includes a dense `Gδ` set. -/
```

-- snippet 29
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_of_mem_residual (L150-L157)
theorem dense_of_mem_residual {s : Set X} (hs : s ∈ residual X) : Dense s :=
let ⟨_, hts, _, hd⟩ := mem_residual.1 hs
  hd.mono hts

/--
In a Baire space, every nonempty open set is non‐meagre,
that is, it cannot be written as a countable union of nowhere‐dense sets.
-/
```

-- snippet 30
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_biInter_of_isOpen (L123-L129)
theorem dense_biInter_of_isOpen {S : Set α} {f : α → Set X} (ho : ∀ s ∈ S, IsOpen (f s))
    (hS : S.Countable) (hd : ∀ s ∈ S, Dense (f s)) : Dense (⋂ s ∈ S, f s) := by
rw [← sInter_image]
  refine dense_sInter_of_isOpen ?_ (hS.image _) ?_ <;> rwa [forall_mem_image]

/-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with
an index set which is a countable type. -/
```

-- snippet 31
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iInter_of_Gδ (L172-L177)
theorem dense_iInter_of_Gδ [Countable ι] {f : ι → Set X} (ho : ∀ s, IsGδ (f s))
    (hd : ∀ s, Dense (f s)) : Dense (⋂ s, f s) :=
dense_sInter_of_Gδ (forall_mem_range.2 ‹_›) (countable_range _) (forall_mem_range.2 ‹_›)

/-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with
an index set which is a countable set in any type. -/
```

-- snippet 32
```lean
-- Mathlib.Topology.Baire.Lemmas.baire_of_finite (L58-L63)
theorem baire_of_finite [Finite X] : BaireSpace X where
  baire_property f _ _ := sInter_range f ▸ (toFinite (range f)).dense_sInter (by grind) (by grind)
variable [BaireSpace X]

/-- Definition of a Baire space. -/
```

-- snippet 33
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_biUnion_interior_of_closed (L225-L230)
theorem dense_biUnion_interior_of_closed {S : Set α} {f : α → Set X} (hc : ∀ s ∈ S, IsClosed (f s))
    (hS : S.Countable) (hU : ⋃ s ∈ S, f s = univ) : Dense (⋃ s ∈ S, interior (f s)) :=
IsGδ.univ.dense_biUnion_interior_of_closed dense_univ hS hc hU.ge

/-- Baire theorem: if countably many closed sets cover the whole space, then their interiors
are dense. Formulated here with `⋃₀`. -/
```

-- snippet 34
```lean
-- Mathlib.Topology.Baire.Lemmas.not_isMeagre_of_isOpen (L158-L164)
theorem not_isMeagre_of_isOpen {s : Set X} (hs : IsOpen s) (hne : s.Nonempty) : ¬ IsMeagre s := by
intro h
  obtain ⟨x, hx, hxc⟩ :=
    (dense_of_mem_residual (by rwa [IsMeagre] at h)).inter_open_nonempty s hs hne
  exact hxc hx

/-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with ⋂₀. -/
```

-- snippet 35
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_biInter_of_Gδ (L178-L184)
theorem dense_biInter_of_Gδ {S : Set α} {f : ∀ x ∈ S, Set X} (ho : ∀ s (H : s ∈ S), IsGδ (f s H))
    (hS : S.Countable) (hd : ∀ s (H : s ∈ S), Dense (f s H)) : Dense (⋂ s ∈ S, f s ‹_›) := by
rw [biInter_eq_iInter]
  haveI := hS.to_subtype
  exact dense_iInter_of_Gδ (fun s => ho s s.2) fun s => hd s s.2

/-- Baire theorem: the intersection of two dense Gδ sets is dense. -/
```

-- snippet 36
```lean
-- Mathlib.Topology.Baire.Lemmas.not_isMeagre_of_mem_residual (L258-L264)
theorem not_isMeagre_of_mem_residual {s : Set X} (hs : s ∈ residual X) :
    ¬ IsMeagre s := by
rcases (mem_residual (X := X)).1 hs with ⟨t, ht_sub, htGδ, ht_dense⟩
  intro hs_meagre
  exact not_isMeagre_of_isGδ_of_dense (X := X) htGδ ht_dense (hs_meagre.mono ht_sub)

end BaireTheorem
```

-- snippet 37
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_iUnion_interior_of_closed (L237-L244)
theorem dense_iUnion_interior_of_closed [Countable ι] {f : ι → Set X} (hc : ∀ i, IsClosed (f i))
    (hU : ⋃ i, f i = univ) : Dense (⋃ i, interior (f i)) :=
IsGδ.univ.dense_iUnion_interior_of_closed dense_univ hc hU.ge

variable [Nonempty X]

/-- One of the most useful consequences of Baire theorem: if a countable union of closed sets
covers the space, then one of the sets has nonempty interior. -/
```

-- snippet 38
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_sInter_of_isOpen (L114-L122)
theorem dense_sInter_of_isOpen {S : Set (Set X)} (ho : ∀ s ∈ S, IsOpen s) (hS : S.Countable)
    (hd : ∀ s ∈ S, Dense s) : Dense (⋂₀ S) := by
rcases S.eq_empty_or_nonempty with h | h
  · simp [h]
  · rcases hS.exists_eq_range h with ⟨f, rfl⟩
    exact dense_iInter_of_isOpen_nat (forall_mem_range.1 ho) (forall_mem_range.1 hd)

/-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with
an index set which is a countable set in any type. -/
```

-- snippet 39
```lean
-- Mathlib.Topology.Baire.Lemmas.dense_sUnion_interior_of_closed (L231-L236)
theorem dense_sUnion_interior_of_closed {S : Set (Set X)} (hc : ∀ s ∈ S, IsClosed s)
    (hS : S.Countable) (hU : ⋃₀ S = univ) : Dense (⋃ s ∈ S, interior s) :=
IsGδ.univ.dense_sUnion_interior_of_closed dense_univ hS hc hU.ge

/-- Baire theorem: if countably many closed sets cover the whole space, then their interiors
are dense. Formulated here with an index set which is a countable type. -/
```

-- snippet 40
```lean
-- Mathlib.Topology.Baire.Lemmas.mem_residual (L135-L144)
theorem mem_residual {s : Set X} : s ∈ residual X ↔ ∃ t ⊆ s, IsGδ t ∧ Dense t := by
constructor
  · rw [mem_residual_iff]
    rintro ⟨S, hSo, hSd, Sct, Ss⟩
    refine ⟨_, Ss, ⟨_, fun t ht => hSo _ ht, Sct, rfl⟩, ?_⟩
    exact dense_sInter_of_isOpen hSo Sct hSd
  rintro ⟨t, ts, ho, hd⟩
  exact mem_of_superset (residual_of_dense_Gδ ho hd) ts

/-- A property holds on a residual (comeagre) set if and only if it holds on some dense `Gδ` set. -/
```

-- snippet 41
```lean
-- Mathlib.Topology.Baire.Lemmas.not_isMeagre_of_isGδ_of_dense (L250-L257)
theorem not_isMeagre_of_isGδ_of_dense {s : Set X} (hs : IsGδ s) (hd : Dense s) :
    ¬ IsMeagre s := by
intro h
  rcases mem_residual.1 h with ⟨t, hts, htG, hd'⟩
  rcases (hd.inter_of_Gδ hs htG hd').nonempty with ⟨x, hx₁, hx₂⟩
  exact hts hx₂ hx₁

/-- In a nonempty Baire space, a residual set is not meagre. -/
```

-- snippet 42
```lean
-- Mathlib.Topology.Baire.LocallyCompactRegular.IsGδ (L62-L72)
theorem IsGδ.of_t2Space_locallyCompactSpace (hG : IsGδ s) : BaireSpace s := by
have : BaireSpace (closure s) := by
    convert BaireSpace.of_t2Space_locallyCompactSpace using 1
    · infer_instance
    · exact isClosed_closure.locallyCompactSpace
  have : BaireSpace ((↑) ⁻¹' s : Set (closure s)) :=
    (isGδ_induced continuous_subtype_val hG).baireSpace_of_dense
    (by simp [Subtype.dense_iff, inter_eq_right.mpr subset_closure])
  have h_homeo : Homeomorph ((↑) ⁻¹' s : Set (closure s)) s := ⟨⟨fun x => ⟨x, x.2⟩,
    fun x => ⟨⟨x, subset_closure x.2⟩, x.2⟩, by grind, by grind⟩, by fun_prop, by fun_prop⟩
  exact h_homeo.baireSpace
```

-- snippet 43
```lean
-- Mathlib.Topology.Baire.Lemmas.IsOpen (L110-L113)
theorem IsOpen.baireSpace {s : Set X} (hO : IsOpen s) : BaireSpace s :=
hO.isOpenEmbedding_subtypeVal.baireSpace

/-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with ⋂₀. -/
```

-- snippet 44
```lean
-- Mathlib.Topology.Baire.Lemmas.Dense (L185-L191)
theorem Dense.inter_of_Gδ {s t : Set X} (hs : IsGδ s) (ht : IsGδ t) (hsc : Dense s)
    (htc : Dense t) : Dense (s ∩ t) := by
rw [inter_eq_iInter]
  apply dense_iInter_of_Gδ <;> simp [Bool.forall_bool, *]

/-- If a countable family of closed sets cover a dense `Gδ` set, then the union of their interiors
is dense. Formulated here with `⋃`. -/
```

-- snippet 45
```lean
-- Mathlib.Topology.Baire.Lemmas.nonempty_interior_of_iUnion_of_closed (L245-L249)
theorem nonempty_interior_of_iUnion_of_closed [Countable ι] {f : ι → Set X}
    (hc : ∀ i, IsClosed (f i)) (hU : ⋃ i, f i = univ) : ∃ i, (interior <| f i).Nonempty := by
simpa using (dense_iUnion_interior_of_closed hc hU).nonempty

/-- In a nonempty Baire space, any dense `Gδ` set is not meagre. -/
```

-- snippet 46
```lean
-- Mathlib.Topology.Baire.Lemmas.Topology (L81-L109)
theorem Topology.IsOpenEmbedding.baireSpace {Y : Type*} [TopologicalSpace Y] {p : Y → X}
    (hp : Topology.IsOpenEmbedding p) : BaireSpace Y := by
constructor
  intro f hof hdf
  let s := range p
  let c := fun n : ℕ => p '' f n ∪ (closure s)ᶜ
  have c_open (n : ℕ) : IsOpen (c n) := IsOpen.union (hp.isOpenMap (f n) (hof n))
    isClosed_closure.isOpen_compl
  have c_dense (n : ℕ) : Dense (c n) := by
    rw [dense_iff_closure_eq, subset_antisymm_iff]
    have : univ ⊆ closure (c n) := calc
      _ ⊆ (interior (closure s)) ∪ (interior (closure s))ᶜ := by grind
      _ ⊆ closure s ∪ (interior (closure s))ᶜ := by gcongr; exact interior_subset
      _ ⊆ closure (p '' f n) ∪ (interior (closure s))ᶜ := union_subset_union
          (closure_minimal (hp.continuous.range_subset_closure_image_dense (hdf n))
          isClosed_closure) (subset_refl (interior (closure s))ᶜ)
      _ ⊆ closure (p '' f n) ∪ closure ((closure s)ᶜ) := union_subset_union (by simp) (by simp)
      _ = closure (c n) := closure_union.symm
    grind
  have c_inter_dense : Dense (⋂ n, c n) := dense_iInter_of_isOpen_nat c_open c_dense
  have c_inter_eq : ⋂ n, f n = p ⁻¹' (⋂ n, c n) := by
    ext x
    simp only [mem_iInter, mem_preimage, mem_union, mem_compl_iff, c]
    refine ⟨fun h i => by grind, fun h i => ?_⟩
    exact hp.injective.mem_set_image.mp (imp_iff_or_not.mpr (h i)
      (subset_closure (mem_range_self x)))
  exact c_inter_eq ▸ Dense.preimage c_inter_dense hp.isOpenMap

/-- An open subset of a Baire space is Baire. -/
```
[/INST]


[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.Order.Closure).

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

```lean
theorem closure_sup_closure (x y : α) : u (l (u (l x) ⊔ u (l y))) = u (l (x ⊔ y))
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Order.Closure.conjBy (L87-L97)
def conjBy {α β} [Preorder α] [Preorder β] (c : ClosureOperator α)
    (e : α ≃o β) : ClosureOperator β where
  toFun := e.conj c
IsClosed b := c.IsClosed (e.symm b)
  monotone' _ _ h :=
    (map_le_map_iff e).mpr <| c.monotone <| (map_le_map_iff e.symm).mpr h
  le_closure' _ := e.symm_apply_le.mp (c.le_closure' _)
  idempotent' _ :=
    congrArg e <| Eq.trans (congrArg c (e.symm_apply_apply _)) (c.idempotent' _)
  isClosed_iff := Iff.trans c.isClosed_iff e.eq_symm_apply
```

-- snippet 2
```lean
-- Mathlib.Order.Closure.conjBy_refl (L98-L100)
lemma conjBy_refl {α} [Preorder α] (c : ClosureOperator α) :
    c.conjBy (OrderIso.refl α) = c := rfl
```

-- snippet 3
```lean
-- Mathlib.Order.Closure.conjBy_trans (L101-L109)
lemma conjBy_trans {α β γ} [Preorder α] [Preorder β] [Preorder γ]
    (e₁ : α ≃o β) (e₂ : β ≃o γ) (c : ClosureOperator α) :
    c.conjBy (e₁.trans e₂) = (c.conjBy e₁).conjBy e₂ := rfl
section PartialOrder

variable [PartialOrder α]

/-- The identity function as a closure operator. -/
```

-- snippet 4
```lean
-- Mathlib.Order.Closure.id (L111-L116)
def id : ClosureOperator α where
  toOrderHom := OrderHom.id
le_closure' _ := le_rfl
  idempotent' _ := rfl
  IsClosed _ := True
```

-- snippet 5
```lean
-- Mathlib.Order.Closure.ext (L124-L127)
theorem ext : ∀ c₁ c₂ : ClosureOperator α, (∀ x, c₁ x = c₂ x) → c₁ = c₂ :=
DFunLike.ext

/-- Constructor for a closure operator using the weaker idempotency axiom: `f (f x) ≤ f x`. -/
```

-- snippet 6
```lean
-- Mathlib.Order.Closure.mk' (L129-L137)
def mk' (f : α → α) (hf₁ : Monotone f) (hf₂ : ∀ x, x ≤ f x) (hf₃ : ∀ x, f (f x) ≤ f x) :
    ClosureOperator α where
  toFun := f
monotone' := hf₁
  le_closure' := hf₂
  idempotent' x := (hf₃ x).antisymm (hf₁ (hf₂ x))

/-- Convenience constructor for a closure operator using the weaker minimality axiom:
`x ≤ f y → f x ≤ f y`, which is sometimes easier to prove in practice. -/
```

-- snippet 7
```lean
-- Mathlib.Order.Closure.mk₂ (L139-L147)
def mk₂ (f : α → α) (hf : ∀ x, x ≤ f x) (hmin : ∀ ⦃x y⦄, x ≤ f y → f x ≤ f y) :
    ClosureOperator α where
  toFun := f
monotone' _ y hxy := hmin (hxy.trans (hf y))
  le_closure' := hf
  idempotent' _ := (hmin le_rfl).antisymm (hf _)

/-- Construct a closure operator from an inflationary function `f` and a "closedness" predicate `p`
witnessing minimality of `f x` among closed elements greater than `x`. -/
```

-- snippet 8
```lean
-- Mathlib.Order.Closure.ofPred (L149-L154)
def ofPred (f : α → α) (p : α → Prop) (hf : ∀ x, x ≤ f x) (hfp : ∀ x, p (f x))
    (hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y) : ClosureOperator α where
  __ := mk₂ f hf fun _ y hxy => hmin hxy (hfp y)
IsClosed := p
  isClosed_iff := ⟨fun hx ↦ (hmin le_rfl hx).antisymm <| hf _, fun hx ↦ hx ▸ hfp _⟩
```

-- snippet 9
```lean
-- Mathlib.Order.Closure.monotone (L156-L160)
theorem monotone : Monotone c :=
c.monotone'

/-- Every element is less than its closure. This property is sometimes referred to as extensivity or
inflationarity. -/
```

-- snippet 10
```lean
-- Mathlib.Order.Closure.le_closure (L161-L163)
theorem le_closure (x : α) : x ≤ c x :=
c.le_closure' x
```

-- snippet 11
```lean
-- Mathlib.Order.Closure.idempotent (L165-L167)
theorem idempotent (x : α) : c (c x) = c x :=
c.idempotent' x
```

-- snippet 12
```lean
-- Mathlib.Order.Closure.Closeds (L171-L173)
abbrev Closeds := {x // c.IsClosed x}
/-- Send an element to a closed element (by taking the closure). -/
```

-- snippet 13
```lean
-- Mathlib.Order.Closure.toCloseds (L174-L177)
def toCloseds (x : α) : c.Closeds := ⟨c x, c.isClosed_closure x⟩
variable {c} {x y : α}
```

-- snippet 14
```lean
-- Mathlib.Order.Closure.IsClosed (L178-L179)
theorem IsClosed.closure_eq : c.IsClosed x → c x = x := c.isClosed_iff.1
```

-- snippet 15
```lean
-- Mathlib.Order.Closure.isClosed_iff_closure_le (L180-L183)
theorem isClosed_iff_closure_le : c.IsClosed x ↔ c x ≤ x :=
⟨fun h ↦ h.closure_eq.le, fun h ↦ c.isClosed_iff.2 <| h.antisymm <| c.le_closure x⟩

/-- The set of closed elements for `c` is exactly its range. -/
```

-- snippet 16
```lean
-- Mathlib.Order.Closure.setOf_isClosed_eq_range_closure (L184-L186)
theorem setOf_isClosed_eq_range_closure : {x | c.IsClosed x} = Set.range c := by
ext x; exact ⟨fun hx ↦ ⟨x, hx.closure_eq⟩, by rintro ⟨y, rfl⟩; exact c.isClosed_closure _⟩
```

-- snippet 17
```lean
-- Mathlib.Order.Closure.le_closure_iff (L187-L189)
theorem le_closure_iff : x ≤ c y ↔ c x ≤ c y :=
⟨fun h ↦ c.idempotent y ▸ c.monotone h, (c.le_closure x).trans⟩
```

-- snippet 18
```lean
-- Mathlib.Order.Closure.IsClosed (L191-L193)
theorem IsClosed.closure_le_iff (hy : c.IsClosed y) : c x ≤ y ↔ x ≤ y := by
rw [← hy.closure_eq, ← le_closure_iff]
```

-- snippet 19
```lean
-- Mathlib.Order.Closure.closure_min (L194-L195)
lemma closure_min (hxy : x ≤ y) (hy : c.IsClosed y) : c x ≤ y := hy.closure_le_iff.2 hxy
```

-- snippet 20
```lean
-- Mathlib.Order.Closure.closure_isGLB (L196-L199)
lemma closure_isGLB (x : α) : IsGLB { y | x ≤ y ∧ c.IsClosed y } (c x) where
  left _ := and_imp.mpr closure_min
right _ h := h ⟨c.le_closure x, c.isClosed_closure x⟩
```

-- snippet 21
```lean
-- Mathlib.Order.Closure.ext_isClosed (L200-L205)
theorem ext_isClosed (c₁ c₂ : ClosureOperator α)
    (h : ∀ x, c₁.IsClosed x ↔ c₂.IsClosed x) : c₁ = c₂ :=
ext c₁ c₂ <| fun x => IsGLB.unique (c₁.closure_isGLB x) <| by simpa [h] using c₂.closure_isGLB x

/-- A closure operator is equal to the closure operator obtained by feeding `c.closed` into the
`ofPred` constructor. -/
```

-- snippet 22
```lean
-- Mathlib.Order.Closure.eq_ofPred_closed (L206-L218)
theorem eq_ofPred_closed (c : ClosureOperator α) :
    c = ofPred c c.IsClosed c.le_closure c.isClosed_closure fun _ _ ↦ closure_min := by
ext
  simp

end PartialOrder

variable {α}

section OrderTop

variable [PartialOrder α] [OrderTop α] (c : ClosureOperator α)
```

-- snippet 23
```lean
-- Mathlib.Order.Closure.closure_top (L220-L222)
theorem closure_top : c ⊤ = ⊤ :=
le_top.antisymm (c.le_closure _)
```

-- snippet 24
```lean
-- Mathlib.Order.Closure.closure_inf_le (L227-L234)
theorem closure_inf_le [SemilatticeInf α] (c : ClosureOperator α) (x y : α) :
    c (x ⊓ y) ≤ c x ⊓ c y :=
c.monotone.map_inf_le _ _

section SemilatticeSup

variable [SemilatticeSup α] (c : ClosureOperator α)
```

-- snippet 25
```lean
-- Mathlib.Order.Closure.closure_sup_closure_le (L235-L237)
theorem closure_sup_closure_le (x y : α) : c x ⊔ c y ≤ c (x ⊔ y) :=
c.monotone.le_map_sup _ _
```

<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.Order.Closure).[/SYSTEM_PROMPT][MODEL_SETTINGS]{"reasoning_effort": "none"}[/MODEL_SETTINGS][INST]Prove the following theorem. The retrieved context below may help.

```lean
theorem closure_sup_closure (x y : α) : u (l (u (l x) ⊔ u (l y))) = u (l (x ⊔ y))
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Order.Closure.conjBy (L87-L97)
def conjBy {α β} [Preorder α] [Preorder β] (c : ClosureOperator α)
    (e : α ≃o β) : ClosureOperator β where
  toFun := e.conj c
IsClosed b := c.IsClosed (e.symm b)
  monotone' _ _ h :=
    (map_le_map_iff e).mpr <| c.monotone <| (map_le_map_iff e.symm).mpr h
  le_closure' _ := e.symm_apply_le.mp (c.le_closure' _)
  idempotent' _ :=
    congrArg e <| Eq.trans (congrArg c (e.symm_apply_apply _)) (c.idempotent' _)
  isClosed_iff := Iff.trans c.isClosed_iff e.eq_symm_apply
```

-- snippet 2
```lean
-- Mathlib.Order.Closure.conjBy_refl (L98-L100)
lemma conjBy_refl {α} [Preorder α] (c : ClosureOperator α) :
    c.conjBy (OrderIso.refl α) = c := rfl
```

-- snippet 3
```lean
-- Mathlib.Order.Closure.conjBy_trans (L101-L109)
lemma conjBy_trans {α β γ} [Preorder α] [Preorder β] [Preorder γ]
    (e₁ : α ≃o β) (e₂ : β ≃o γ) (c : ClosureOperator α) :
    c.conjBy (e₁.trans e₂) = (c.conjBy e₁).conjBy e₂ := rfl
section PartialOrder

variable [PartialOrder α]

/-- The identity function as a closure operator. -/
```

-- snippet 4
```lean
-- Mathlib.Order.Closure.id (L111-L116)
def id : ClosureOperator α where
  toOrderHom := OrderHom.id
le_closure' _ := le_rfl
  idempotent' _ := rfl
  IsClosed _ := True
```

-- snippet 5
```lean
-- Mathlib.Order.Closure.ext (L124-L127)
theorem ext : ∀ c₁ c₂ : ClosureOperator α, (∀ x, c₁ x = c₂ x) → c₁ = c₂ :=
DFunLike.ext

/-- Constructor for a closure operator using the weaker idempotency axiom: `f (f x) ≤ f x`. -/
```

-- snippet 6
```lean
-- Mathlib.Order.Closure.mk' (L129-L137)
def mk' (f : α → α) (hf₁ : Monotone f) (hf₂ : ∀ x, x ≤ f x) (hf₃ : ∀ x, f (f x) ≤ f x) :
    ClosureOperator α where
  toFun := f
monotone' := hf₁
  le_closure' := hf₂
  idempotent' x := (hf₃ x).antisymm (hf₁ (hf₂ x))

/-- Convenience constructor for a closure operator using the weaker minimality axiom:
`x ≤ f y → f x ≤ f y`, which is sometimes easier to prove in practice. -/
```

-- snippet 7
```lean
-- Mathlib.Order.Closure.mk₂ (L139-L147)
def mk₂ (f : α → α) (hf : ∀ x, x ≤ f x) (hmin : ∀ ⦃x y⦄, x ≤ f y → f x ≤ f y) :
    ClosureOperator α where
  toFun := f
monotone' _ y hxy := hmin (hxy.trans (hf y))
  le_closure' := hf
  idempotent' _ := (hmin le_rfl).antisymm (hf _)

/-- Construct a closure operator from an inflationary function `f` and a "closedness" predicate `p`
witnessing minimality of `f x` among closed elements greater than `x`. -/
```

-- snippet 8
```lean
-- Mathlib.Order.Closure.ofPred (L149-L154)
def ofPred (f : α → α) (p : α → Prop) (hf : ∀ x, x ≤ f x) (hfp : ∀ x, p (f x))
    (hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y) : ClosureOperator α where
  __ := mk₂ f hf fun _ y hxy => hmin hxy (hfp y)
IsClosed := p
  isClosed_iff := ⟨fun hx ↦ (hmin le_rfl hx).antisymm <| hf _, fun hx ↦ hx ▸ hfp _⟩
```

-- snippet 9
```lean
-- Mathlib.Order.Closure.monotone (L156-L160)
theorem monotone : Monotone c :=
c.monotone'

/-- Every element is less than its closure. This property is sometimes referred to as extensivity or
inflationarity. -/
```

-- snippet 10
```lean
-- Mathlib.Order.Closure.le_closure (L161-L163)
theorem le_closure (x : α) : x ≤ c x :=
c.le_closure' x
```

-- snippet 11
```lean
-- Mathlib.Order.Closure.idempotent (L165-L167)
theorem idempotent (x : α) : c (c x) = c x :=
c.idempotent' x
```

-- snippet 12
```lean
-- Mathlib.Order.Closure.Closeds (L171-L173)
abbrev Closeds := {x // c.IsClosed x}
/-- Send an element to a closed element (by taking the closure). -/
```

-- snippet 13
```lean
-- Mathlib.Order.Closure.toCloseds (L174-L177)
def toCloseds (x : α) : c.Closeds := ⟨c x, c.isClosed_closure x⟩
variable {c} {x y : α}
```

-- snippet 14
```lean
-- Mathlib.Order.Closure.IsClosed (L178-L179)
theorem IsClosed.closure_eq : c.IsClosed x → c x = x := c.isClosed_iff.1
```

-- snippet 15
```lean
-- Mathlib.Order.Closure.isClosed_iff_closure_le (L180-L183)
theorem isClosed_iff_closure_le : c.IsClosed x ↔ c x ≤ x :=
⟨fun h ↦ h.closure_eq.le, fun h ↦ c.isClosed_iff.2 <| h.antisymm <| c.le_closure x⟩

/-- The set of closed elements for `c` is exactly its range. -/
```

-- snippet 16
```lean
-- Mathlib.Order.Closure.setOf_isClosed_eq_range_closure (L184-L186)
theorem setOf_isClosed_eq_range_closure : {x | c.IsClosed x} = Set.range c := by
ext x; exact ⟨fun hx ↦ ⟨x, hx.closure_eq⟩, by rintro ⟨y, rfl⟩; exact c.isClosed_closure _⟩
```

-- snippet 17
```lean
-- Mathlib.Order.Closure.le_closure_iff (L187-L189)
theorem le_closure_iff : x ≤ c y ↔ c x ≤ c y :=
⟨fun h ↦ c.idempotent y ▸ c.monotone h, (c.le_closure x).trans⟩
```

-- snippet 18
```lean
-- Mathlib.Order.Closure.IsClosed (L191-L193)
theorem IsClosed.closure_le_iff (hy : c.IsClosed y) : c x ≤ y ↔ x ≤ y := by
rw [← hy.closure_eq, ← le_closure_iff]
```

-- snippet 19
```lean
-- Mathlib.Order.Closure.closure_min (L194-L195)
lemma closure_min (hxy : x ≤ y) (hy : c.IsClosed y) : c x ≤ y := hy.closure_le_iff.2 hxy
```

-- snippet 20
```lean
-- Mathlib.Order.Closure.closure_isGLB (L196-L199)
lemma closure_isGLB (x : α) : IsGLB { y | x ≤ y ∧ c.IsClosed y } (c x) where
  left _ := and_imp.mpr closure_min
right _ h := h ⟨c.le_closure x, c.isClosed_closure x⟩
```

-- snippet 21
```lean
-- Mathlib.Order.Closure.ext_isClosed (L200-L205)
theorem ext_isClosed (c₁ c₂ : ClosureOperator α)
    (h : ∀ x, c₁.IsClosed x ↔ c₂.IsClosed x) : c₁ = c₂ :=
ext c₁ c₂ <| fun x => IsGLB.unique (c₁.closure_isGLB x) <| by simpa [h] using c₂.closure_isGLB x

/-- A closure operator is equal to the closure operator obtained by feeding `c.closed` into the
`ofPred` constructor. -/
```

-- snippet 22
```lean
-- Mathlib.Order.Closure.eq_ofPred_closed (L206-L218)
theorem eq_ofPred_closed (c : ClosureOperator α) :
    c = ofPred c c.IsClosed c.le_closure c.isClosed_closure fun _ _ ↦ closure_min := by
ext
  simp

end PartialOrder

variable {α}

section OrderTop

variable [PartialOrder α] [OrderTop α] (c : ClosureOperator α)
```

-- snippet 23
```lean
-- Mathlib.Order.Closure.closure_top (L220-L222)
theorem closure_top : c ⊤ = ⊤ :=
le_top.antisymm (c.le_closure _)
```

-- snippet 24
```lean
-- Mathlib.Order.Closure.closure_inf_le (L227-L234)
theorem closure_inf_le [SemilatticeInf α] (c : ClosureOperator α) (x y : α) :
    c (x ⊓ y) ≤ c x ⊓ c y :=
c.monotone.map_inf_le _ _

section SemilatticeSup

variable [SemilatticeSup α] (c : ClosureOperator α)
```

-- snippet 25
```lean
-- Mathlib.Order.Closure.closure_sup_closure_le (L235-L237)
theorem closure_sup_closure_le (x y : α) : c x ⊔ c y ≤ c (x ⊔ y) :=
c.monotone.le_map_sup _ _
```
[/INST]