[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.Algebra.Order.Interval.Set.Group).

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

```lean
theorem add_mem_Ioc_iff_right : a + b ∈ Set.Ioc c d ↔ b ∈ Set.Ioc (c - a) (d - a)
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Icc_iff (L32-L34)
theorem inv_mem_Icc_iff : a⁻¹ ∈ Set.Icc c d ↔ a ∈ Set.Icc d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_le' le_inv'
```

-- snippet 2
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Ico_iff (L36-L38)
theorem inv_mem_Ico_iff : a⁻¹ ∈ Set.Ico c d ↔ a ∈ Set.Ioc d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_lt' le_inv'
```

-- snippet 3
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Ioc_iff (L40-L42)
theorem inv_mem_Ioc_iff : a⁻¹ ∈ Set.Ioc c d ↔ a ∈ Set.Ico d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_le' lt_inv'
```

-- snippet 4
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Ioo_iff (L44-L54)
theorem inv_mem_Ioo_iff : a⁻¹ ∈ Set.Ioo c d ↔ a ∈ Set.Ioo d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_lt' lt_inv'

end OrderedCommGroup

section OrderedAddCommGroup

variable [AddCommGroup α] [PartialOrder α] [IsOrderedAddMonoid α] {a b c d : α}

/-! `add_mem_Ixx_iff_left` -/
```

-- snippet 5
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Icc_iff_left (L55-L57)
theorem add_mem_Icc_iff_left : a + b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c - b) (d - b) :=
(and_congr sub_le_iff_le_add le_sub_iff_add_le).symm
```

-- snippet 6
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ico_iff_left (L58-L60)
theorem add_mem_Ico_iff_left : a + b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c - b) (d - b) :=
(and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm
```

-- snippet 7
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ioc_iff_left (L61-L63)
theorem add_mem_Ioc_iff_left : a + b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c - b) (d - b) :=
(and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm
```

-- snippet 8
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ioo_iff_left (L64-L68)
theorem add_mem_Ioo_iff_left : a + b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c - b) (d - b) :=
(and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm

/-! `add_mem_Ixx_iff_right` -/
```

-- snippet 9
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Icc_iff_right (L69-L71)
theorem add_mem_Icc_iff_right : a + b ∈ Set.Icc c d ↔ b ∈ Set.Icc (c - a) (d - a) :=
(and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm
```

-- snippet 10
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ico_iff_right (L72-L74)
theorem add_mem_Ico_iff_right : a + b ∈ Set.Ico c d ↔ b ∈ Set.Ico (c - a) (d - a) :=
(and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm
```

-- snippet 11
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ioo_iff_right (L78-L82)
theorem add_mem_Ioo_iff_right : a + b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (c - a) (d - a) :=
(and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm

/-! `sub_mem_Ixx_iff_left` -/
```

-- snippet 12
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Icc_iff_left (L83-L85)
theorem sub_mem_Icc_iff_left : a - b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c + b) (d + b) :=
and_congr le_sub_iff_add_le sub_le_iff_le_add
```

-- snippet 13
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Ico_iff_left (L86-L88)
theorem sub_mem_Ico_iff_left : a - b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c + b) (d + b) :=
and_congr le_sub_iff_add_le sub_lt_iff_lt_add
```

-- snippet 14
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Ioc_iff_left (L89-L91)
theorem sub_mem_Ioc_iff_left : a - b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c + b) (d + b) :=
and_congr lt_sub_iff_add_lt sub_le_iff_le_add
```

-- snippet 15
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Ioo_iff_left (L92-L96)
theorem sub_mem_Ioo_iff_left : a - b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c + b) (d + b) :=
and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add

/-! `sub_mem_Ixx_iff_right` -/
```

-- snippet 16
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.Simps (L80-L84)
def Simps.apply (f : AbsoluteValue R S) : R → S :=
f

initialize_simps_projections AbsoluteValue (toFun → apply)
```

-- snippet 17
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.sub_le_add (L235-L237)
lemma sub_le_add (a b : R) : abv (a - b) ≤ abv a + abv b := by
simpa only [← sub_eq_add_neg, AbsoluteValue.map_neg] using abv.add_le a (-b)
```

-- snippet 18
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_toMonoidWithZeroHom (L169-L172)
theorem coe_toMonoidWithZeroHom : ⇑abv.toMonoidWithZeroHom = abv :=
rfl

/-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*` and `1`. -/
```

-- snippet 19
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.funLike (L55-L58)
instance funLike : FunLike (AbsoluteValue R S) R S where
  coe f := f.toFun
coe_injective' f g h := by obtain ⟨⟨_, _⟩, _⟩ := f; obtain ⟨⟨_, _⟩, _⟩ := g; congr
```

-- snippet 20
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.mulHomClass (L62-L64)
instance mulHomClass : MulHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.zeroHomClass (R := R) (S := S) with map_mul := fun f => f.map_mul' }
```

-- snippet 21
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.monoidWithZeroHomClass (L159-L164)
instance monoidWithZeroHomClass : MonoidWithZeroHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.mulHomClass with
    map_zero := fun f => f.map_zero
    map_one := fun f => f.map_one }

/-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*`, `0` and `1`. -/
```

-- snippet 22
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.trivial_apply (L319-L334)
lemma trivial_apply {x : R} (hx : x ≠ 0) : AbsoluteValue.trivial (S := S) x = 1 :=
if_neg hx

end trivial

section nontrivial

section OrderedSemiring

variable {R : Type*} [Semiring R] {S : Type*} [Semiring S] [PartialOrder S] [IsOrderedRing S]

/-- An absolute value on a semiring `R` without zero divisors is *nontrivial* if it takes
a value `≠ 1` on a nonzero element.

This has the advantage over `v ≠ .trivial` that it does not require decidability
of `· = 0` in `R`. -/
```

-- snippet 23
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_mk (L72-L74)
theorem coe_mk (f : R →ₙ* S) {h₁ h₂ h₃} : (AbsoluteValue.mk f h₁ h₂ h₃ : R → S) = f :=
rfl
```

-- snippet 24
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.toMonoidHom (L173-L175)
def toMonoidHom : R →* S :=
abv
```

-- snippet 25
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.apply_nat_le_self (L186-L206)
lemma apply_nat_le_self [IsOrderedRing S] (n : ℕ) : abv n ≤ n := by
cases subsingleton_or_nontrivial R
  · simp [Subsingleton.eq_zero (n : R)]
  induction n with
  | zero => simp
  | succ n ih =>
  · grw [Nat.cast_succ, Nat.cast_succ, abv.add_le, abv.map_one, ih]

end IsDomain

end Semiring

end OrderedSemiring

section OrderedRing

section Ring

variable {R S : Type*} [Ring R] [Ring S] [PartialOrder S] [IsOrderedRing S]
  (abv : AbsoluteValue R S)
```

-- snippet 26
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.listSum_le (L102-L106)
lemma listSum_le [AddLeftMono S] (l : List R) : abv l.sum ≤ (l.map abv).sum := by
induction l with
  | nil => simp
  | cons head tail ih => exact (abv.add_le ..).trans <| add_le_add_right ih (abv head)
```

-- snippet 27
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.ext (L76-L79)
theorem ext ⦃f g : AbsoluteValue R S⦄ : (∀ x, f x = g x) → f = g :=
DFunLike.ext _ _

/-- See Note [custom simps projection]. -/
```

-- snippet 28
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.apply_natAbs_eq (L250-L255)
lemma apply_natAbs_eq (x : ℤ) : abv (natAbs x) = abv x := by
obtain ⟨_, rfl | rfl⟩ := Int.eq_nat_or_neg x <;> simp

open Int in
/-- Values of an absolute value coincide on the image of `ℕ` in `R`
if and only if they coincide on the image of `ℤ` in `R`. -/
```

-- snippet 29
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.trivial (L300-L317)
def trivial : AbsoluteValue R S where
  toFun x := if x = 0 then 0 else 1
map_mul' x y := by
    rcases eq_or_ne x 0 with rfl | hx
    · simp
    rcases eq_or_ne y 0 with rfl | hy
    · simp
    simp [hx, hy]
  nonneg' x := by rcases eq_or_ne x 0 with hx | hx <;> simp [hx]
  eq_zero' x := by rcases eq_or_ne x 0 with hx | hx <;> simp [hx]
  add_le' x y := by
    rcases eq_or_ne x 0 with rfl | hx
    · simp
    rcases eq_or_ne y 0 with rfl | hy
    · simp
    simp only [hx, ↓reduceIte, hy, one_add_one_eq_two]
    rcases eq_or_ne (x + y) 0 with hxy | hxy <;> simp [hxy, one_le_two]
```

-- snippet 30
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.nonnegHomClass (L65-L67)
instance nonnegHomClass : NonnegHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.zeroHomClass (R := R) (S := S) with apply_nonneg := fun f => f.nonneg' }
```

-- snippet 31
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.addGroupSeminormClass (L238-L242)
instance addGroupSeminormClass : AddGroupSeminormClass (AbsoluteValue R S) R S where
  toSubadditiveHomClass := AbsoluteValue.subadditiveHomClass
map_zero := AbsoluteValue.map_zero
  map_neg_eq_map f a := AbsoluteValue.map_neg f a
```

-- snippet 32
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.eq_on_nat_iff_eq_on_int (L256-L268)
lemma eq_on_nat_iff_eq_on_int {f g : AbsoluteValue R S} :
    (∀ n : ℕ, f n = g n) ↔ ∀ n : ℤ, f n = g n := by
refine ⟨fun h z ↦ ?_, fun a n ↦ mod_cast a n⟩
  obtain ⟨n, rfl | rfl⟩ := Int.eq_nat_or_neg z <;> simp [h n]

end OrderedCommRing

section LinearOrderedRing

variable {R S : Type*} [Semiring R] [Ring S] [LinearOrder S] [IsStrictOrderedRing S]
  (abv : AbsoluteValue R S)

/-- `AbsoluteValue.abs` is `abs` as a bundled `AbsoluteValue`. -/
```

-- snippet 33
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.map_one_of_isLeftRegular (L124-L126)
theorem map_one_of_isLeftRegular (h : IsLeftRegular (abv 1)) : abv 1 = 1 :=
h <| by simp [← abv.map_mul]
```

-- snippet 34
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_toMulHom (L86-L88)
theorem coe_toMulHom : ⇑abv.toMulHom = abv :=
rfl
```

-- snippet 35
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.zeroHomClass (L59-L61)
instance zeroHomClass : ZeroHomClass (AbsoluteValue R S) R S where
  map_zero f := (f.eq_zero' _).2 rfl
```

-- snippet 36
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.subadditiveHomClass (L68-L70)
instance subadditiveHomClass : SubadditiveHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.zeroHomClass (R := R) (S := S) with map_add_le_add := fun f => f.add_le' }
```

-- snippet 37
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.abs_abv_sub_le_abv_sub (L288-L299)
theorem abs_abv_sub_le_abv_sub (a b : R) : abs (abv a - abv b) ≤ abv (a - b) :=
abs_sub_le_iff.2 ⟨abv.le_sub _ _, by rw [abv.map_sub]; apply abv.le_sub⟩

end LinearOrderedCommRing

section trivial

variable {R : Type*} [Semiring R] [DecidablePred fun x : R ↦ x = 0] [NoZeroDivisors R]
variable {S : Type*} [Semiring S] [PartialOrder S] [IsOrderedRing S] [Nontrivial S]

/-- The *trivial* absolute value takes the value `1` on all nonzero elements. -/
protected
```

-- snippet 38
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_toMonoidHom (L177-L179)
theorem coe_toMonoidHom : ⇑abv.toMonoidHom = abv :=
rfl
```

-- snippet 39
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.map_sub_eq_zero_iff (L141-L154)
theorem map_sub_eq_zero_iff (a b : R) : abv (a - b) = 0 ↔ a = b :=
abv.eq_zero.trans sub_eq_zero

end Ring

section Semiring

section IsDomain

-- all of these are true for `NoZeroDivisors S`; but it doesn't work smoothly with the
-- `IsDomain`/`IsCancelMulZero` API
variable {R S : Type*} [Semiring R] [Semiring S] [PartialOrder S] (abv : AbsoluteValue R S)
variable [IsDomain S] [Nontrivial R]
```

-- snippet 40
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.toMonoidWithZeroHom (L165-L167)
def toMonoidWithZeroHom : R →*₀ S :=
abv
```

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

```lean
theorem add_mem_Ioc_iff_right : a + b ∈ Set.Ioc c d ↔ b ∈ Set.Ioc (c - a) (d - a)
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Icc_iff (L32-L34)
theorem inv_mem_Icc_iff : a⁻¹ ∈ Set.Icc c d ↔ a ∈ Set.Icc d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_le' le_inv'
```

-- snippet 2
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Ico_iff (L36-L38)
theorem inv_mem_Ico_iff : a⁻¹ ∈ Set.Ico c d ↔ a ∈ Set.Ioc d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_lt' le_inv'
```

-- snippet 3
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Ioc_iff (L40-L42)
theorem inv_mem_Ioc_iff : a⁻¹ ∈ Set.Ioc c d ↔ a ∈ Set.Ico d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_le' lt_inv'
```

-- snippet 4
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.inv_mem_Ioo_iff (L44-L54)
theorem inv_mem_Ioo_iff : a⁻¹ ∈ Set.Ioo c d ↔ a ∈ Set.Ioo d⁻¹ c⁻¹ :=
and_comm.trans <| and_congr inv_lt' lt_inv'

end OrderedCommGroup

section OrderedAddCommGroup

variable [AddCommGroup α] [PartialOrder α] [IsOrderedAddMonoid α] {a b c d : α}

/-! `add_mem_Ixx_iff_left` -/
```

-- snippet 5
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Icc_iff_left (L55-L57)
theorem add_mem_Icc_iff_left : a + b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c - b) (d - b) :=
(and_congr sub_le_iff_le_add le_sub_iff_add_le).symm
```

-- snippet 6
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ico_iff_left (L58-L60)
theorem add_mem_Ico_iff_left : a + b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c - b) (d - b) :=
(and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm
```

-- snippet 7
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ioc_iff_left (L61-L63)
theorem add_mem_Ioc_iff_left : a + b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c - b) (d - b) :=
(and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm
```

-- snippet 8
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ioo_iff_left (L64-L68)
theorem add_mem_Ioo_iff_left : a + b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c - b) (d - b) :=
(and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm

/-! `add_mem_Ixx_iff_right` -/
```

-- snippet 9
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Icc_iff_right (L69-L71)
theorem add_mem_Icc_iff_right : a + b ∈ Set.Icc c d ↔ b ∈ Set.Icc (c - a) (d - a) :=
(and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm
```

-- snippet 10
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ico_iff_right (L72-L74)
theorem add_mem_Ico_iff_right : a + b ∈ Set.Ico c d ↔ b ∈ Set.Ico (c - a) (d - a) :=
(and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm
```

-- snippet 11
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.add_mem_Ioo_iff_right (L78-L82)
theorem add_mem_Ioo_iff_right : a + b ∈ Set.Ioo c d ↔ b ∈ Set.Ioo (c - a) (d - a) :=
(and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm

/-! `sub_mem_Ixx_iff_left` -/
```

-- snippet 12
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Icc_iff_left (L83-L85)
theorem sub_mem_Icc_iff_left : a - b ∈ Set.Icc c d ↔ a ∈ Set.Icc (c + b) (d + b) :=
and_congr le_sub_iff_add_le sub_le_iff_le_add
```

-- snippet 13
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Ico_iff_left (L86-L88)
theorem sub_mem_Ico_iff_left : a - b ∈ Set.Ico c d ↔ a ∈ Set.Ico (c + b) (d + b) :=
and_congr le_sub_iff_add_le sub_lt_iff_lt_add
```

-- snippet 14
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Ioc_iff_left (L89-L91)
theorem sub_mem_Ioc_iff_left : a - b ∈ Set.Ioc c d ↔ a ∈ Set.Ioc (c + b) (d + b) :=
and_congr lt_sub_iff_add_lt sub_le_iff_le_add
```

-- snippet 15
```lean
-- Mathlib.Algebra.Order.Interval.Set.Group.sub_mem_Ioo_iff_left (L92-L96)
theorem sub_mem_Ioo_iff_left : a - b ∈ Set.Ioo c d ↔ a ∈ Set.Ioo (c + b) (d + b) :=
and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add

/-! `sub_mem_Ixx_iff_right` -/
```

-- snippet 16
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.Simps (L80-L84)
def Simps.apply (f : AbsoluteValue R S) : R → S :=
f

initialize_simps_projections AbsoluteValue (toFun → apply)
```

-- snippet 17
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.sub_le_add (L235-L237)
lemma sub_le_add (a b : R) : abv (a - b) ≤ abv a + abv b := by
simpa only [← sub_eq_add_neg, AbsoluteValue.map_neg] using abv.add_le a (-b)
```

-- snippet 18
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_toMonoidWithZeroHom (L169-L172)
theorem coe_toMonoidWithZeroHom : ⇑abv.toMonoidWithZeroHom = abv :=
rfl

/-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*` and `1`. -/
```

-- snippet 19
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.funLike (L55-L58)
instance funLike : FunLike (AbsoluteValue R S) R S where
  coe f := f.toFun
coe_injective' f g h := by obtain ⟨⟨_, _⟩, _⟩ := f; obtain ⟨⟨_, _⟩, _⟩ := g; congr
```

-- snippet 20
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.mulHomClass (L62-L64)
instance mulHomClass : MulHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.zeroHomClass (R := R) (S := S) with map_mul := fun f => f.map_mul' }
```

-- snippet 21
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.monoidWithZeroHomClass (L159-L164)
instance monoidWithZeroHomClass : MonoidWithZeroHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.mulHomClass with
    map_zero := fun f => f.map_zero
    map_one := fun f => f.map_one }

/-- Absolute values from a nontrivial `R` to a linear ordered ring preserve `*`, `0` and `1`. -/
```

-- snippet 22
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.trivial_apply (L319-L334)
lemma trivial_apply {x : R} (hx : x ≠ 0) : AbsoluteValue.trivial (S := S) x = 1 :=
if_neg hx

end trivial

section nontrivial

section OrderedSemiring

variable {R : Type*} [Semiring R] {S : Type*} [Semiring S] [PartialOrder S] [IsOrderedRing S]

/-- An absolute value on a semiring `R` without zero divisors is *nontrivial* if it takes
a value `≠ 1` on a nonzero element.

This has the advantage over `v ≠ .trivial` that it does not require decidability
of `· = 0` in `R`. -/
```

-- snippet 23
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_mk (L72-L74)
theorem coe_mk (f : R →ₙ* S) {h₁ h₂ h₃} : (AbsoluteValue.mk f h₁ h₂ h₃ : R → S) = f :=
rfl
```

-- snippet 24
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.toMonoidHom (L173-L175)
def toMonoidHom : R →* S :=
abv
```

-- snippet 25
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.apply_nat_le_self (L186-L206)
lemma apply_nat_le_self [IsOrderedRing S] (n : ℕ) : abv n ≤ n := by
cases subsingleton_or_nontrivial R
  · simp [Subsingleton.eq_zero (n : R)]
  induction n with
  | zero => simp
  | succ n ih =>
  · grw [Nat.cast_succ, Nat.cast_succ, abv.add_le, abv.map_one, ih]

end IsDomain

end Semiring

end OrderedSemiring

section OrderedRing

section Ring

variable {R S : Type*} [Ring R] [Ring S] [PartialOrder S] [IsOrderedRing S]
  (abv : AbsoluteValue R S)
```

-- snippet 26
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.listSum_le (L102-L106)
lemma listSum_le [AddLeftMono S] (l : List R) : abv l.sum ≤ (l.map abv).sum := by
induction l with
  | nil => simp
  | cons head tail ih => exact (abv.add_le ..).trans <| add_le_add_right ih (abv head)
```

-- snippet 27
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.ext (L76-L79)
theorem ext ⦃f g : AbsoluteValue R S⦄ : (∀ x, f x = g x) → f = g :=
DFunLike.ext _ _

/-- See Note [custom simps projection]. -/
```

-- snippet 28
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.apply_natAbs_eq (L250-L255)
lemma apply_natAbs_eq (x : ℤ) : abv (natAbs x) = abv x := by
obtain ⟨_, rfl | rfl⟩ := Int.eq_nat_or_neg x <;> simp

open Int in
/-- Values of an absolute value coincide on the image of `ℕ` in `R`
if and only if they coincide on the image of `ℤ` in `R`. -/
```

-- snippet 29
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.trivial (L300-L317)
def trivial : AbsoluteValue R S where
  toFun x := if x = 0 then 0 else 1
map_mul' x y := by
    rcases eq_or_ne x 0 with rfl | hx
    · simp
    rcases eq_or_ne y 0 with rfl | hy
    · simp
    simp [hx, hy]
  nonneg' x := by rcases eq_or_ne x 0 with hx | hx <;> simp [hx]
  eq_zero' x := by rcases eq_or_ne x 0 with hx | hx <;> simp [hx]
  add_le' x y := by
    rcases eq_or_ne x 0 with rfl | hx
    · simp
    rcases eq_or_ne y 0 with rfl | hy
    · simp
    simp only [hx, ↓reduceIte, hy, one_add_one_eq_two]
    rcases eq_or_ne (x + y) 0 with hxy | hxy <;> simp [hxy, one_le_two]
```

-- snippet 30
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.nonnegHomClass (L65-L67)
instance nonnegHomClass : NonnegHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.zeroHomClass (R := R) (S := S) with apply_nonneg := fun f => f.nonneg' }
```

-- snippet 31
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.addGroupSeminormClass (L238-L242)
instance addGroupSeminormClass : AddGroupSeminormClass (AbsoluteValue R S) R S where
  toSubadditiveHomClass := AbsoluteValue.subadditiveHomClass
map_zero := AbsoluteValue.map_zero
  map_neg_eq_map f a := AbsoluteValue.map_neg f a
```

-- snippet 32
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.eq_on_nat_iff_eq_on_int (L256-L268)
lemma eq_on_nat_iff_eq_on_int {f g : AbsoluteValue R S} :
    (∀ n : ℕ, f n = g n) ↔ ∀ n : ℤ, f n = g n := by
refine ⟨fun h z ↦ ?_, fun a n ↦ mod_cast a n⟩
  obtain ⟨n, rfl | rfl⟩ := Int.eq_nat_or_neg z <;> simp [h n]

end OrderedCommRing

section LinearOrderedRing

variable {R S : Type*} [Semiring R] [Ring S] [LinearOrder S] [IsStrictOrderedRing S]
  (abv : AbsoluteValue R S)

/-- `AbsoluteValue.abs` is `abs` as a bundled `AbsoluteValue`. -/
```

-- snippet 33
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.map_one_of_isLeftRegular (L124-L126)
theorem map_one_of_isLeftRegular (h : IsLeftRegular (abv 1)) : abv 1 = 1 :=
h <| by simp [← abv.map_mul]
```

-- snippet 34
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_toMulHom (L86-L88)
theorem coe_toMulHom : ⇑abv.toMulHom = abv :=
rfl
```

-- snippet 35
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.zeroHomClass (L59-L61)
instance zeroHomClass : ZeroHomClass (AbsoluteValue R S) R S where
  map_zero f := (f.eq_zero' _).2 rfl
```

-- snippet 36
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.subadditiveHomClass (L68-L70)
instance subadditiveHomClass : SubadditiveHomClass (AbsoluteValue R S) R S :=
{ AbsoluteValue.zeroHomClass (R := R) (S := S) with map_add_le_add := fun f => f.add_le' }
```

-- snippet 37
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.abs_abv_sub_le_abv_sub (L288-L299)
theorem abs_abv_sub_le_abv_sub (a b : R) : abs (abv a - abv b) ≤ abv (a - b) :=
abs_sub_le_iff.2 ⟨abv.le_sub _ _, by rw [abv.map_sub]; apply abv.le_sub⟩

end LinearOrderedCommRing

section trivial

variable {R : Type*} [Semiring R] [DecidablePred fun x : R ↦ x = 0] [NoZeroDivisors R]
variable {S : Type*} [Semiring S] [PartialOrder S] [IsOrderedRing S] [Nontrivial S]

/-- The *trivial* absolute value takes the value `1` on all nonzero elements. -/
protected
```

-- snippet 38
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.coe_toMonoidHom (L177-L179)
theorem coe_toMonoidHom : ⇑abv.toMonoidHom = abv :=
rfl
```

-- snippet 39
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.map_sub_eq_zero_iff (L141-L154)
theorem map_sub_eq_zero_iff (a b : R) : abv (a - b) = 0 ↔ a = b :=
abv.eq_zero.trans sub_eq_zero

end Ring

section Semiring

section IsDomain

-- all of these are true for `NoZeroDivisors S`; but it doesn't work smoothly with the
-- `IsDomain`/`IsCancelMulZero` API
variable {R S : Type*} [Semiring R] [Semiring S] [PartialOrder S] (abv : AbsoluteValue R S)
variable [IsDomain S] [Nontrivial R]
```

-- snippet 40
```lean
-- Mathlib.Algebra.Order.AbsoluteValue.Basic.toMonoidWithZeroHom (L165-L167)
def toMonoidWithZeroHom : R →*₀ S :=
abv
```
[/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.BooleanAlgebra.Basic).

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

```lean
instance OrderDual.instBooleanAlgebra : BooleanAlgebra αᵒᵈ where
  __
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sup_inf_sdiff (L53-L55)
theorem sup_inf_sdiff (x y : α) : x ⊓ y ⊔ x \ y = x :=
GeneralizedBooleanAlgebra.sup_inf_sdiff _ _
```

-- snippet 2
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_inf_sdiff (L57-L59)
theorem inf_inf_sdiff (x y : α) : x ⊓ y ⊓ x \ y = ⊥ :=
GeneralizedBooleanAlgebra.inf_inf_sdiff _ _
```

-- snippet 3
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sup_sdiff_inf (L61-L62)
theorem sup_sdiff_inf (x y : α) : x \ y ⊔ x ⊓ y = x := by rw [sup_comm, sup_inf_sdiff]
```

-- snippet 4
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_sdiff_inf (L64-L66)
theorem inf_sdiff_inf (x y : α) : x \ y ⊓ (x ⊓ y) = ⊥ := by rw [inf_comm, inf_inf_sdiff]
-- see Note [lower instance priority]
```

-- snippet 5
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_inf_sdiff (L73-L76)
theorem disjoint_inf_sdiff : Disjoint (x ⊓ y) (x \ y) :=
disjoint_iff_inf_le.mpr (inf_inf_sdiff x y).le

-- TODO: in distributive lattices, relative complements are unique when they exist
```

-- snippet 6
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sdiff_unique (L77-L97)
theorem sdiff_unique (s : x ⊓ y ⊔ z = x) (i : x ⊓ y ⊓ z = ⊥) : x \ y = z := by
conv_rhs at s => rw [← sup_inf_sdiff x y, sup_comm]
  rw [sup_comm] at s
  conv_rhs at i => rw [← inf_inf_sdiff x y, inf_comm]
  rw [inf_comm] at i
  exact (eq_of_inf_eq_sup_eq i s).symm

-- Use `sdiff_le`
private theorem sdiff_le' : x \ y ≤ x :=
  calc
    x \ y ≤ x ⊓ y ⊔ x \ y := le_sup_right
    _ = x := sup_inf_sdiff x y

set_option backward.privateInPublic true in
-- Use `sdiff_sup_self`
private theorem sdiff_sup_self' : y \ x ⊔ x = y ⊔ x :=
  calc
    y \ x ⊔ x = y \ x ⊔ (x ⊔ x ⊓ y) := by rw [sup_inf_self]
    _ = y ⊓ x ⊔ y \ x ⊔ x := by ac_rfl
    _ = y ⊔ x := by rw [sup_inf_sdiff]
```

-- snippet 7
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sdiff_inf_sdiff (L99-L109)
theorem sdiff_inf_sdiff : x \ y ⊓ y \ x = ⊥ :=
Eq.symm <|
    calc
      ⊥ = x ⊓ (y ⊓ x ⊔ y \ x) ⊓ x \ y := by rw [← inf_inf_sdiff, sup_inf_sdiff]
      _ = (x ⊓ (y ⊓ x) ⊔ x ⊓ y \ x) ⊓ x \ y := by rw [inf_sup_left]
      _ = (y ⊓ (x ⊓ x) ⊔ x ⊓ y \ x) ⊓ x \ y := by ac_rfl
      _ = x ⊓ y \ x ⊓ x \ y := by
          rw [inf_idem, inf_sup_right, ← inf_comm x y, inf_inf_sdiff, bot_sup_eq]
      _ = x ⊓ x \ y ⊓ y \ x := by ac_rfl
      _ = x \ y ⊓ y \ x := by rw [inf_of_le_right sdiff_le']
```

-- snippet 8
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_sdiff (L110-L112)
theorem disjoint_sdiff_sdiff : Disjoint (x \ y) (y \ x) :=
disjoint_iff_inf_le.mpr sdiff_inf_sdiff.le
```

-- snippet 9
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_sdiff_self_right (L114-L118)
theorem inf_sdiff_self_right : x ⊓ y \ x = ⊥ :=
calc
    x ⊓ y \ x = (x ⊓ y ⊔ x \ y) ⊓ y \ x := by rw [sup_inf_sdiff]
    _ = ⊥ := by rw [inf_sup_right, inf_comm x y, inf_inf_sdiff, sdiff_inf_sdiff, bot_sup_eq]
```

-- snippet 10
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_sdiff_self_left (L120-L124)
theorem inf_sdiff_self_left : y \ x ⊓ x = ⊥ := by rw [inf_comm, inf_sdiff_self_right]
set_option backward.privateInPublic true in
set_option backward.privateInPublic.warn false in
-- see Note [lower instance priority]
```

-- snippet 11
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_self_left (L145-L147)
theorem disjoint_sdiff_self_left : Disjoint (y \ x) x :=
disjoint_iff_inf_le.mpr inf_sdiff_self_left.le
```

-- snippet 12
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_self_right (L148-L150)
theorem disjoint_sdiff_self_right : Disjoint x (y \ x) :=
disjoint_iff_inf_le.mpr inf_sdiff_self_right.le
```

-- snippet 13
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.le_sdiff (L151-L154)
lemma le_sdiff : x ≤ y \ z ↔ x ≤ y ∧ Disjoint x z :=
⟨fun h ↦ ⟨h.trans sdiff_le, disjoint_sdiff_self_left.mono_left h⟩, fun h ↦
    by rw [← h.2.sdiff_eq_left]; exact sdiff_le_sdiff_right h.1⟩
```

-- snippet 14
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.Disjoint (L160-L173)
theorem Disjoint.sdiff_eq_of_sup_eq (hi : Disjoint x z) (hs : x ⊔ z = y) : y \ x = z :=
have h : y ⊓ x = x := inf_eq_right.2 <| le_sup_left.trans hs.le
  sdiff_unique (by rw [h, hs]) (by rw [h, hi.eq_bot])

protected theorem Disjoint.sdiff_unique (hd : Disjoint x z) (hz : z ≤ y) (hs : y ≤ x ⊔ z) :
    y \ x = z :=
  sdiff_unique
    (by
      rw [← inf_eq_right] at hs
      rwa [sup_inf_right, inf_sup_right, sup_comm x, inf_sup_self, inf_comm, sup_comm z,
        hs, sup_eq_left])
    (by rw [inf_assoc, hd.eq_bot, inf_bot_eq])

-- cf. `IsCompl.disjoint_left_iff` and `IsCompl.disjoint_right_iff`
```

-- snippet 15
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_iff_le (L174-L183)
theorem disjoint_sdiff_iff_le (hz : z ≤ y) (hx : x ≤ y) : Disjoint z (y \ x) ↔ z ≤ x :=
⟨fun H =>
    le_of_inf_le_sup_le (le_trans H.le_bot bot_le)
      (by
        rw [sup_sdiff_cancel_right hx]
        grw [sdiff_le]
        rw [sup_eq_right.2 hz]),
    fun H => disjoint_sdiff_self_right.mono_left H⟩

-- cf. `IsCompl.le_left_iff` and `IsCompl.le_right_iff`
```

-- snippet 16
```lean
-- Mathlib.Order.BooleanAlgebra.Set.sdiff_inter_right_comm (L47-L48)
lemma sdiff_inter_right_comm (s t u : Set α) : s \ t ∩ u = (s ∩ u) \ t := sdiff_inf_right_comm ..
```

-- snippet 17
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_univ (L106-L108)
theorem compl_univ : (univ : Set α)ᶜ = ∅ :=
compl_top
```

-- snippet 18
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_diff_eq_sdiff_union (L58-L59)
theorem diff_diff_eq_sdiff_union (h : u ⊆ s) : s \ (t \ u) = s \ t ∪ u := sdiff_sdiff_eq_sdiff_sup h
```

-- snippet 19
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_diff_distrib_left (L60-L62)
theorem inter_diff_distrib_left (s t u : Set α) : s ∩ (t \ u) = (s ∩ t) \ (s ∩ u) :=
inf_sdiff_distrib_left _ _ _
```

-- snippet 20
```lean
-- Mathlib.Order.BooleanAlgebra.Set.notMem_compl_iff (L83-L85)
theorem notMem_compl_iff {x : α} : x ∉ sᶜ ↔ x ∈ s :=
not_not
```

-- snippet 21
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_def (L71-L73)
theorem compl_def (s : Set α) : sᶜ = { x | x ∉ s } :=
rfl
```

-- snippet 22
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_inter (L102-L104)
theorem compl_inter (s t : Set α) : (s ∩ t)ᶜ = sᶜ ∪ tᶜ :=
compl_inf
```

-- snippet 23
```lean
-- Mathlib.Order.BooleanAlgebra.Set.mem_compl (L74-L76)
theorem mem_compl {s : Set α} {x : α} (h : x ∉ s) : x ∈ sᶜ :=
h
```

-- snippet 24
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_diff_assoc (L44-L46)
lemma inter_diff_assoc (a b c : Set α) : (a ∩ b) \ c = a ∩ (b \ c) := inf_sdiff_assoc ..
/-- See also `Set.inter_diff_assoc`. -/
```

-- snippet 25
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_setOf (L77-L79)
theorem compl_setOf {α} (p : α → Prop) : { a | p a }ᶜ = { a | ¬p a } :=
rfl
```

-- snippet 26
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_compl_self (L87-L89)
theorem inter_compl_self (s : Set α) : s ∩ sᶜ = ∅ :=
inf_compl_eq_bot
```

-- snippet 27
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.PUnit (L148-L163)
instance PUnit.instBooleanAlgebra : BooleanAlgebra PUnit where
  __ := PUnit.instBiheytingAlgebra
le_sup_inf := by simp
  inf_compl_le_bot _ := trivial
  top_le_sup_compl _ := trivial

namespace DistribLattice

variable (α) [DistribLattice α]

/--
An alternative constructor for Boolean algebras:
a distributive lattice that is complemented is a Boolean algebra.

This is not an instance, because it creates data using choice.
-/
```

-- snippet 28
```lean
-- Mathlib.Order.BooleanAlgebra.Set.instBooleanAlgebra (L40-L43)
instance instBooleanAlgebra : BooleanAlgebra (Set α) :=
fast_instance% { (inferInstance : BooleanAlgebra (α → Prop)) with }

/-- See also `Set.sdiff_inter_right_comm`. -/
```

-- snippet 29
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.Prop (L126-L133)
instance Prop.instBooleanAlgebra : BooleanAlgebra Prop where
  __ := Prop.instHeytingAlgebra
__ := GeneralizedHeytingAlgebra.toDistribLattice
  compl := Not
  himp_eq _ _ := propext imp_iff_or_not
  inf_compl_le_bot _ H := H.2 H.1
  top_le_sup_compl p _ := Classical.em p
```

-- snippet 30
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_sdiff_left_comm (L49-L50)
lemma inter_sdiff_left_comm (s t u : Set α) : s ∩ (t \ u) = t ∩ (s \ u) := inf_sdiff_left_comm ..
```

-- snippet 31
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_union_diff_cancel' (L55-L57)
theorem diff_union_diff_cancel' (hi : s ∩ u ⊆ t) (hu : t ⊆ s ∪ u) : (s \ t) ∪ (t \ u) = s \ u :=
sdiff_sup_sdiff_cancel' hi hu
```

-- snippet 32
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.booleanAlgebraOfComplemented (L166-L172)
def booleanAlgebraOfComplemented [BoundedOrder α] [ComplementedLattice α] : BooleanAlgebra α where
  __ := ((inferInstance : BoundedOrder α))
compl a := Classical.choose <| exists_isCompl a
  inf_compl_le_bot a := (Classical.choose_spec (exists_isCompl a)).disjoint.le_bot
  top_le_sup_compl a := (Classical.choose_spec (exists_isCompl a)).codisjoint.top_le

end DistribLattice
```

-- snippet 33
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_union (L99-L101)
theorem compl_union (s t : Set α) : (s ∪ t)ᶜ = sᶜ ∩ tᶜ :=
compl_sup
```

-- snippet 34
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_union_diff_cancel (L51-L54)
theorem diff_union_diff_cancel (hts : t ⊆ s) (hut : u ⊆ t) : s \ t ∪ t \ u = s \ u :=
sdiff_sup_sdiff_cancel hts hut

/-- A version of `diff_union_diff_cancel` with more general hypotheses. -/
```

-- snippet 35
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_inter_self (L91-L93)
theorem compl_inter_self (s : Set α) : sᶜ ∩ s = ∅ :=
compl_inf_eq_bot
```

-- snippet 36
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_inter_distrib_right (L66-L70)
theorem diff_inter_distrib_right (s t r : Set α) : (t ∩ r) \ s = (t \ s) ∩ (r \ s) :=
inf_sdiff

/-! ### Lemmas about complement -/
```

-- snippet 37
```lean
-- Mathlib.Order.BooleanAlgebra.Set.notMem_of_mem_compl (L80-L82)
theorem notMem_of_mem_compl {s : Set α} {x : α} (h : x ∈ sᶜ) : x ∉ s :=
h
```

-- snippet 38
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_empty (L95-L97)
theorem compl_empty : (∅ : Set α)ᶜ = univ :=
compl_bot
```

-- snippet 39
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.Bool (L134-L139)
instance Bool.instBooleanAlgebra : BooleanAlgebra Bool where
  __ := instBoundedOrder
compl := not
  inf_compl_le_bot a := a.and_not_self.le
  top_le_sup_compl a := a.or_not_self.ge
```

-- snippet 40
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_diff_distrib_right (L63-L65)
theorem inter_diff_distrib_right (s t u : Set α) : (s \ t) ∩ u = (s ∩ u) \ (t ∩ u) :=
inf_sdiff_distrib_right _ _ _
```

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

```lean
instance OrderDual.instBooleanAlgebra : BooleanAlgebra αᵒᵈ where
  __
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sup_inf_sdiff (L53-L55)
theorem sup_inf_sdiff (x y : α) : x ⊓ y ⊔ x \ y = x :=
GeneralizedBooleanAlgebra.sup_inf_sdiff _ _
```

-- snippet 2
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_inf_sdiff (L57-L59)
theorem inf_inf_sdiff (x y : α) : x ⊓ y ⊓ x \ y = ⊥ :=
GeneralizedBooleanAlgebra.inf_inf_sdiff _ _
```

-- snippet 3
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sup_sdiff_inf (L61-L62)
theorem sup_sdiff_inf (x y : α) : x \ y ⊔ x ⊓ y = x := by rw [sup_comm, sup_inf_sdiff]
```

-- snippet 4
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_sdiff_inf (L64-L66)
theorem inf_sdiff_inf (x y : α) : x \ y ⊓ (x ⊓ y) = ⊥ := by rw [inf_comm, inf_inf_sdiff]
-- see Note [lower instance priority]
```

-- snippet 5
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_inf_sdiff (L73-L76)
theorem disjoint_inf_sdiff : Disjoint (x ⊓ y) (x \ y) :=
disjoint_iff_inf_le.mpr (inf_inf_sdiff x y).le

-- TODO: in distributive lattices, relative complements are unique when they exist
```

-- snippet 6
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sdiff_unique (L77-L97)
theorem sdiff_unique (s : x ⊓ y ⊔ z = x) (i : x ⊓ y ⊓ z = ⊥) : x \ y = z := by
conv_rhs at s => rw [← sup_inf_sdiff x y, sup_comm]
  rw [sup_comm] at s
  conv_rhs at i => rw [← inf_inf_sdiff x y, inf_comm]
  rw [inf_comm] at i
  exact (eq_of_inf_eq_sup_eq i s).symm

-- Use `sdiff_le`
private theorem sdiff_le' : x \ y ≤ x :=
  calc
    x \ y ≤ x ⊓ y ⊔ x \ y := le_sup_right
    _ = x := sup_inf_sdiff x y

set_option backward.privateInPublic true in
-- Use `sdiff_sup_self`
private theorem sdiff_sup_self' : y \ x ⊔ x = y ⊔ x :=
  calc
    y \ x ⊔ x = y \ x ⊔ (x ⊔ x ⊓ y) := by rw [sup_inf_self]
    _ = y ⊓ x ⊔ y \ x ⊔ x := by ac_rfl
    _ = y ⊔ x := by rw [sup_inf_sdiff]
```

-- snippet 7
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.sdiff_inf_sdiff (L99-L109)
theorem sdiff_inf_sdiff : x \ y ⊓ y \ x = ⊥ :=
Eq.symm <|
    calc
      ⊥ = x ⊓ (y ⊓ x ⊔ y \ x) ⊓ x \ y := by rw [← inf_inf_sdiff, sup_inf_sdiff]
      _ = (x ⊓ (y ⊓ x) ⊔ x ⊓ y \ x) ⊓ x \ y := by rw [inf_sup_left]
      _ = (y ⊓ (x ⊓ x) ⊔ x ⊓ y \ x) ⊓ x \ y := by ac_rfl
      _ = x ⊓ y \ x ⊓ x \ y := by
          rw [inf_idem, inf_sup_right, ← inf_comm x y, inf_inf_sdiff, bot_sup_eq]
      _ = x ⊓ x \ y ⊓ y \ x := by ac_rfl
      _ = x \ y ⊓ y \ x := by rw [inf_of_le_right sdiff_le']
```

-- snippet 8
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_sdiff (L110-L112)
theorem disjoint_sdiff_sdiff : Disjoint (x \ y) (y \ x) :=
disjoint_iff_inf_le.mpr sdiff_inf_sdiff.le
```

-- snippet 9
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_sdiff_self_right (L114-L118)
theorem inf_sdiff_self_right : x ⊓ y \ x = ⊥ :=
calc
    x ⊓ y \ x = (x ⊓ y ⊔ x \ y) ⊓ y \ x := by rw [sup_inf_sdiff]
    _ = ⊥ := by rw [inf_sup_right, inf_comm x y, inf_inf_sdiff, sdiff_inf_sdiff, bot_sup_eq]
```

-- snippet 10
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.inf_sdiff_self_left (L120-L124)
theorem inf_sdiff_self_left : y \ x ⊓ x = ⊥ := by rw [inf_comm, inf_sdiff_self_right]
set_option backward.privateInPublic true in
set_option backward.privateInPublic.warn false in
-- see Note [lower instance priority]
```

-- snippet 11
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_self_left (L145-L147)
theorem disjoint_sdiff_self_left : Disjoint (y \ x) x :=
disjoint_iff_inf_le.mpr inf_sdiff_self_left.le
```

-- snippet 12
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_self_right (L148-L150)
theorem disjoint_sdiff_self_right : Disjoint x (y \ x) :=
disjoint_iff_inf_le.mpr inf_sdiff_self_right.le
```

-- snippet 13
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.le_sdiff (L151-L154)
lemma le_sdiff : x ≤ y \ z ↔ x ≤ y ∧ Disjoint x z :=
⟨fun h ↦ ⟨h.trans sdiff_le, disjoint_sdiff_self_left.mono_left h⟩, fun h ↦
    by rw [← h.2.sdiff_eq_left]; exact sdiff_le_sdiff_right h.1⟩
```

-- snippet 14
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.Disjoint (L160-L173)
theorem Disjoint.sdiff_eq_of_sup_eq (hi : Disjoint x z) (hs : x ⊔ z = y) : y \ x = z :=
have h : y ⊓ x = x := inf_eq_right.2 <| le_sup_left.trans hs.le
  sdiff_unique (by rw [h, hs]) (by rw [h, hi.eq_bot])

protected theorem Disjoint.sdiff_unique (hd : Disjoint x z) (hz : z ≤ y) (hs : y ≤ x ⊔ z) :
    y \ x = z :=
  sdiff_unique
    (by
      rw [← inf_eq_right] at hs
      rwa [sup_inf_right, inf_sup_right, sup_comm x, inf_sup_self, inf_comm, sup_comm z,
        hs, sup_eq_left])
    (by rw [inf_assoc, hd.eq_bot, inf_bot_eq])

-- cf. `IsCompl.disjoint_left_iff` and `IsCompl.disjoint_right_iff`
```

-- snippet 15
```lean
-- Mathlib.Order.BooleanAlgebra.Basic.disjoint_sdiff_iff_le (L174-L183)
theorem disjoint_sdiff_iff_le (hz : z ≤ y) (hx : x ≤ y) : Disjoint z (y \ x) ↔ z ≤ x :=
⟨fun H =>
    le_of_inf_le_sup_le (le_trans H.le_bot bot_le)
      (by
        rw [sup_sdiff_cancel_right hx]
        grw [sdiff_le]
        rw [sup_eq_right.2 hz]),
    fun H => disjoint_sdiff_self_right.mono_left H⟩

-- cf. `IsCompl.le_left_iff` and `IsCompl.le_right_iff`
```

-- snippet 16
```lean
-- Mathlib.Order.BooleanAlgebra.Set.sdiff_inter_right_comm (L47-L48)
lemma sdiff_inter_right_comm (s t u : Set α) : s \ t ∩ u = (s ∩ u) \ t := sdiff_inf_right_comm ..
```

-- snippet 17
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_univ (L106-L108)
theorem compl_univ : (univ : Set α)ᶜ = ∅ :=
compl_top
```

-- snippet 18
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_diff_eq_sdiff_union (L58-L59)
theorem diff_diff_eq_sdiff_union (h : u ⊆ s) : s \ (t \ u) = s \ t ∪ u := sdiff_sdiff_eq_sdiff_sup h
```

-- snippet 19
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_diff_distrib_left (L60-L62)
theorem inter_diff_distrib_left (s t u : Set α) : s ∩ (t \ u) = (s ∩ t) \ (s ∩ u) :=
inf_sdiff_distrib_left _ _ _
```

-- snippet 20
```lean
-- Mathlib.Order.BooleanAlgebra.Set.notMem_compl_iff (L83-L85)
theorem notMem_compl_iff {x : α} : x ∉ sᶜ ↔ x ∈ s :=
not_not
```

-- snippet 21
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_def (L71-L73)
theorem compl_def (s : Set α) : sᶜ = { x | x ∉ s } :=
rfl
```

-- snippet 22
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_inter (L102-L104)
theorem compl_inter (s t : Set α) : (s ∩ t)ᶜ = sᶜ ∪ tᶜ :=
compl_inf
```

-- snippet 23
```lean
-- Mathlib.Order.BooleanAlgebra.Set.mem_compl (L74-L76)
theorem mem_compl {s : Set α} {x : α} (h : x ∉ s) : x ∈ sᶜ :=
h
```

-- snippet 24
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_diff_assoc (L44-L46)
lemma inter_diff_assoc (a b c : Set α) : (a ∩ b) \ c = a ∩ (b \ c) := inf_sdiff_assoc ..
/-- See also `Set.inter_diff_assoc`. -/
```

-- snippet 25
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_setOf (L77-L79)
theorem compl_setOf {α} (p : α → Prop) : { a | p a }ᶜ = { a | ¬p a } :=
rfl
```

-- snippet 26
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_compl_self (L87-L89)
theorem inter_compl_self (s : Set α) : s ∩ sᶜ = ∅ :=
inf_compl_eq_bot
```

-- snippet 27
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.PUnit (L148-L163)
instance PUnit.instBooleanAlgebra : BooleanAlgebra PUnit where
  __ := PUnit.instBiheytingAlgebra
le_sup_inf := by simp
  inf_compl_le_bot _ := trivial
  top_le_sup_compl _ := trivial

namespace DistribLattice

variable (α) [DistribLattice α]

/--
An alternative constructor for Boolean algebras:
a distributive lattice that is complemented is a Boolean algebra.

This is not an instance, because it creates data using choice.
-/
```

-- snippet 28
```lean
-- Mathlib.Order.BooleanAlgebra.Set.instBooleanAlgebra (L40-L43)
instance instBooleanAlgebra : BooleanAlgebra (Set α) :=
fast_instance% { (inferInstance : BooleanAlgebra (α → Prop)) with }

/-- See also `Set.sdiff_inter_right_comm`. -/
```

-- snippet 29
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.Prop (L126-L133)
instance Prop.instBooleanAlgebra : BooleanAlgebra Prop where
  __ := Prop.instHeytingAlgebra
__ := GeneralizedHeytingAlgebra.toDistribLattice
  compl := Not
  himp_eq _ _ := propext imp_iff_or_not
  inf_compl_le_bot _ H := H.2 H.1
  top_le_sup_compl p _ := Classical.em p
```

-- snippet 30
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_sdiff_left_comm (L49-L50)
lemma inter_sdiff_left_comm (s t u : Set α) : s ∩ (t \ u) = t ∩ (s \ u) := inf_sdiff_left_comm ..
```

-- snippet 31
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_union_diff_cancel' (L55-L57)
theorem diff_union_diff_cancel' (hi : s ∩ u ⊆ t) (hu : t ⊆ s ∪ u) : (s \ t) ∪ (t \ u) = s \ u :=
sdiff_sup_sdiff_cancel' hi hu
```

-- snippet 32
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.booleanAlgebraOfComplemented (L166-L172)
def booleanAlgebraOfComplemented [BoundedOrder α] [ComplementedLattice α] : BooleanAlgebra α where
  __ := ((inferInstance : BoundedOrder α))
compl a := Classical.choose <| exists_isCompl a
  inf_compl_le_bot a := (Classical.choose_spec (exists_isCompl a)).disjoint.le_bot
  top_le_sup_compl a := (Classical.choose_spec (exists_isCompl a)).codisjoint.top_le

end DistribLattice
```

-- snippet 33
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_union (L99-L101)
theorem compl_union (s t : Set α) : (s ∪ t)ᶜ = sᶜ ∩ tᶜ :=
compl_sup
```

-- snippet 34
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_union_diff_cancel (L51-L54)
theorem diff_union_diff_cancel (hts : t ⊆ s) (hut : u ⊆ t) : s \ t ∪ t \ u = s \ u :=
sdiff_sup_sdiff_cancel hts hut

/-- A version of `diff_union_diff_cancel` with more general hypotheses. -/
```

-- snippet 35
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_inter_self (L91-L93)
theorem compl_inter_self (s : Set α) : sᶜ ∩ s = ∅ :=
compl_inf_eq_bot
```

-- snippet 36
```lean
-- Mathlib.Order.BooleanAlgebra.Set.diff_inter_distrib_right (L66-L70)
theorem diff_inter_distrib_right (s t r : Set α) : (t ∩ r) \ s = (t \ s) ∩ (r \ s) :=
inf_sdiff

/-! ### Lemmas about complement -/
```

-- snippet 37
```lean
-- Mathlib.Order.BooleanAlgebra.Set.notMem_of_mem_compl (L80-L82)
theorem notMem_of_mem_compl {s : Set α} {x : α} (h : x ∈ sᶜ) : x ∉ s :=
h
```

-- snippet 38
```lean
-- Mathlib.Order.BooleanAlgebra.Set.compl_empty (L95-L97)
theorem compl_empty : (∅ : Set α)ᶜ = univ :=
compl_bot
```

-- snippet 39
```lean
-- Mathlib.Order.BooleanAlgebra.Defs.Bool (L134-L139)
instance Bool.instBooleanAlgebra : BooleanAlgebra Bool where
  __ := instBoundedOrder
compl := not
  inf_compl_le_bot a := a.and_not_self.le
  top_le_sup_compl a := a.or_not_self.ge
```

-- snippet 40
```lean
-- Mathlib.Order.BooleanAlgebra.Set.inter_diff_distrib_right (L63-L65)
theorem inter_diff_distrib_right (s t u : Set α) : (s \ t) ∩ u = (s ∩ u) \ (t ∩ u) :=
inf_sdiff_distrib_right _ _ _
```
[/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.Algebra.Module.WeakDual).

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

```lean
def toWeakSpaceCLM : E →L[𝕜] WeakSpace 𝕜 E where
  __
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.WeakDual (L61-L76)
def WeakDual (𝕜 E : Type*) [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
    [ContinuousConstSMul 𝕜 𝕜] [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E] :=
WeakBilin (topDualPairing 𝕜 E)
deriving AddCommMonoid, Module 𝕜, TopologicalSpace, ContinuousAdd, Inhabited,
  FunLike, ContinuousLinearMapClass

namespace WeakDual

section Semiring

variable [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
variable [ContinuousConstSMul 𝕜 𝕜]
variable [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E]

/-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with
multiplication on `𝕜`, then it acts on `WeakDual 𝕜 E`. -/
```

-- snippet 2
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instMulAction (L77-L82)
instance instMulAction (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [ContinuousConstSMul M 𝕜] : MulAction M (WeakDual 𝕜 E) :=
inferInstanceAs <| MulAction M (E →L[𝕜] 𝕜)

/-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with
multiplication on `𝕜`, then it acts distributively on `WeakDual 𝕜 E`. -/
```

-- snippet 3
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instDistribMulAction (L83-L88)
instance instDistribMulAction (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [ContinuousConstSMul M 𝕜] : DistribMulAction M (WeakDual 𝕜 E) :=
inferInstanceAs <| DistribMulAction M (E →L[𝕜] 𝕜)

/-- If `𝕜` is a topological module over a semiring `R` and scalar multiplication commutes with the
multiplication on `𝕜`, then `WeakDual 𝕜 E` is a module over `R`. -/
```

-- snippet 4
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instModule' (L89-L92)
instance instModule' (R) [Semiring R] [Module R 𝕜] [SMulCommClass 𝕜 R 𝕜] [ContinuousConstSMul R 𝕜] :
    Module R (WeakDual 𝕜 E) :=
inferInstanceAs <| Module R (E →L[𝕜] 𝕜)
```

-- snippet 5
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instContinuousConstSMul (L93-L99)
instance instContinuousConstSMul (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [ContinuousConstSMul M 𝕜] : ContinuousConstSMul M (WeakDual 𝕜 E) :=
⟨fun m =>
    continuous_induced_rng.2 <| (WeakBilin.coeFn_continuous (topDualPairing 𝕜 E)).const_smul m⟩

/-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with
multiplication on `𝕜`, then it continuously acts on `WeakDual 𝕜 E`. -/
```

-- snippet 6
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instContinuousSMul (L100-L104)
instance instContinuousSMul (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [TopologicalSpace M] [ContinuousSMul M 𝕜] : ContinuousSMul M (WeakDual 𝕜 E) :=
⟨continuous_induced_rng.2 <|
      continuous_fst.smul ((WeakBilin.coeFn_continuous (topDualPairing 𝕜 E)).comp continuous_snd)⟩
```

-- snippet 7
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.coeFn_continuous (L105-L107)
theorem coeFn_continuous : Continuous fun (x : WeakDual 𝕜 E) y => x y :=
continuous_induced_dom
```

-- snippet 8
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.eval_continuous (L108-L110)
theorem eval_continuous (y : E) : Continuous fun x : WeakDual 𝕜 E => x y :=
continuous_pi_iff.mp coeFn_continuous y
```

-- snippet 9
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.continuous_of_continuous_eval (L111-L114)
theorem continuous_of_continuous_eval [TopologicalSpace α] {g : α → WeakDual 𝕜 E}
    (h : ∀ y, Continuous fun a => (g a) y) : Continuous g :=
continuous_induced_rng.2 (continuous_pi_iff.mpr h)
```

-- snippet 10
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instT2Space (L115-L124)
instance instT2Space [T2Space 𝕜] : T2Space (WeakDual 𝕜 E) :=
(WeakBilin.isEmbedding ContinuousLinearMap.coe_injective).t2Space

end Semiring

section Ring

variable [CommRing 𝕜] [TopologicalSpace 𝕜] [IsTopologicalAddGroup 𝕜] [ContinuousConstSMul 𝕜 𝕜]
variable [AddCommGroup E] [Module 𝕜 E] [TopologicalSpace E] [IsTopologicalAddGroup E]
```

-- snippet 11
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instAddCommGroup (L125-L127)
instance instAddCommGroup : AddCommGroup (WeakDual 𝕜 E) :=
inferInstanceAs <| AddCommGroup (WeakBilin (topDualPairing 𝕜 E))
```

-- snippet 12
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instIsTopologicalAddGroup (L128-L136)
instance instIsTopologicalAddGroup : IsTopologicalAddGroup (WeakDual 𝕜 E) :=
WeakBilin.instIsTopologicalAddGroup (topDualPairing 𝕜 E)

end Ring

end WeakDual

/-- The weak topology is the topology coarsest topology on `E` such that all functionals
`fun x => v x` are continuous. -/
```

-- snippet 13
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.WeakSpace (L137-L149)
def WeakSpace (𝕜 E) [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
    [ContinuousConstSMul 𝕜 𝕜] [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E] :=
WeakBilin (topDualPairing 𝕜 E).flip
deriving AddCommMonoid, Module 𝕜, TopologicalSpace, ContinuousAdd

section Semiring

variable [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
variable [ContinuousConstSMul 𝕜 𝕜]
variable [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E]

namespace WeakSpace
```

-- snippet 14
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instModule' (L150-L152)
instance instModule' [CommSemiring 𝕝] [Module 𝕝 E] : Module 𝕝 (WeakSpace 𝕜 E) :=
inferInstanceAs <| Module 𝕝 (WeakBilin (topDualPairing 𝕜 E).flip)
```

-- snippet 15
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instIsScalarTower (L153-L156)
instance instIsScalarTower [CommSemiring 𝕝] [Module 𝕝 𝕜] [Module 𝕝 E] [IsScalarTower 𝕝 𝕜 E] :
    IsScalarTower 𝕝 𝕜 (WeakSpace 𝕜 E) :=
WeakBilin.instIsScalarTower (topDualPairing 𝕜 E).flip
```

-- snippet 16
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearMap_toContinuousLinearEquiv_eq (L140-L143)
theorem toContinuousLinearMap_toContinuousLinearEquiv_eq (e : A ≃A[R] B) :
    e.toContinuousLinearEquiv.toContinuousLinearMap
    = e.toContinuousAlgHom.toContinuousLinearMap := rfl
```

-- snippet 17
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.image_closure (L151-L153)
theorem image_closure (e : A ≃A[R] B) (S : Set A) : e '' closure S = closure (e '' S) :=
e.toHomeomorph.image_closure S
```

-- snippet 18
```lean
-- Mathlib.Topology.Algebra.Affine.lineMap_continuous (L66-L75)
theorem lineMap_continuous {p q : P} :
    Continuous (lineMap p q : R →ᵃ[R] P) := by
fun_prop

open Topology Filter

section Tendsto

variable {α : Type*} {l : Filter α}
```

-- snippet 19
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearEquiv_apply (L137-L139)
theorem toContinuousLinearEquiv_apply (e : A ≃A[R] B) (a : A) :
    e.toContinuousLinearEquiv a = e a := rfl
```

-- snippet 20
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.ext (L111-L113)
theorem ext {f g : A ≃A[R] B} (h : ⇑f = ⇑g) : f = g :=
toAlgEquiv_injective <| AlgEquiv.ext <| congr_fun h
```

-- snippet 21
```lean
-- Mathlib.Topology.Algebra.Affine.continuous_linear_iff (L35-L45)
theorem continuous_linear_iff {f : P →ᵃ[R] Q} : Continuous f.linear ↔ Continuous f := by
inhabit P
  have :
    (f.linear : V → W) =
      (Homeomorph.vaddConst <| f default).symm ∘ f ∘ (Homeomorph.vaddConst default) := by
    ext v
    simp
  rw [this]
  simp only [Homeomorph.comp_continuous_iff, Homeomorph.comp_continuous_iff']

/-- If `f` is an affine map, then its linear part is an open map iff `f` is an open map. -/
```

-- snippet 22
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_toAlgEquiv (L122-L125)
theorem coe_toAlgEquiv (e : A ≃A[R] B) : ⇑e.toAlgEquiv = e := rfl
/-- The natural coercion from a continuous algebra isomorphism
to a continuous linear isomorphism. -/
```

-- snippet 23
```lean
-- Mathlib.Topology.Algebra.AffineSubspace.subtypeA (L33-L36)
def subtypeA (s : AffineSubspace R P) [Nonempty s] : s →ᴬ[R] P where
  toAffineMap := s.subtype
cont := continuous_subtype_val
```

-- snippet 24
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_injective (L114-L116)
theorem coe_injective : Function.Injective ((↑) : (A ≃A[R] B) → A →A[R] B) :=
fun _ _ h => ext <| funext <| ContinuousAlgHom.ext_iff.1 h
```

-- snippet 25
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_apply (L99-L100)
theorem coe_apply (e : A ≃A[R] B) (a : A) : (e : A →A[R] B) a = e a := rfl
```

-- snippet 26
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearEquiv (L127-L129)
def toContinuousLinearEquiv (e : A ≃A[R] B) : A ≃L[R] B :=
{ e with __ := e.toLinearEquiv }
```

-- snippet 27
```lean
-- Mathlib.Topology.Algebra.Affine.lineMap_continuous_uncurry (L60-L65)
theorem lineMap_continuous_uncurry :
    Continuous (fun pqt : P × P × R ↦ lineMap pqt.1 pqt.2.1 pqt.2.2) := by
simp only [coe_lineMap]
  fun_prop

/-- The line map is continuous. -/
```

-- snippet 28
```lean
-- Mathlib.Topology.Algebra.AffineSubspace.isClosed_direction_iff (L54-L61)
theorem isClosed_direction_iff [T1Space V] (s : AffineSubspace R P) :
    IsClosed (s.direction : Set V) ↔ IsClosed (s : Set P) := by
rcases s.eq_bot_or_nonempty with (rfl | ⟨x, hx⟩); · simp
  rw [← (Homeomorph.vaddConst x).symm.isClosed_image,
    AffineSubspace.coe_direction_eq_vsub_set_right hx]
  simp only [Homeomorph.vaddConst_symm_apply]

end AffineSubspace
```

-- snippet 29
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_inj (L118-L120)
theorem coe_inj {f g : A ≃A[R] B} : (f : A →A[R] B) = g ↔ f = g :=
coe_injective.eq_iff
```

-- snippet 30
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.continuousAlgEquivClass (L92-L98)
instance continuousAlgEquivClass : ContinuousAlgEquivClass (A ≃A[R] B) R A B where
  map_add f := f.map_add'
map_mul f := f.map_mul'
  commutes f := f.commutes'
  map_continuous := continuous_toFun
  inv_continuous := continuous_invFun
```

-- snippet 31
```lean
-- Mathlib.Topology.Algebra.Affine.homothety_isOpenMap (L146-L152)
theorem homothety_isOpenMap (x : P) (t : R) (ht : t ≠ 0) : IsOpenMap <| homothety x t := by
apply IsOpenMap.of_inverse (homothety_continuous x t⁻¹) <;> intro e <;>
    simp [← AffineMap.comp_apply, ← homothety_mul, ht]

end Field

end AffineMap
```

-- snippet 32
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_coe (L104-L105)
theorem coe_coe (e : A ≃A[R] B) : ⇑(e : A →A[R] B) = e := rfl
```

-- snippet 33
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.equivLike (L80-L91)
instance equivLike : EquivLike (A ≃A[R] B) A B where
  coe f := f.toFun
inv f := f.invFun
  coe_injective' f g h₁ h₂ := by
    obtain ⟨f', _⟩ := f
    obtain ⟨g', _⟩ := g
    rcases f' with ⟨⟨_, _⟩, _⟩
    rcases g' with ⟨⟨_, _⟩, _⟩
    congr
  left_inv f := f.left_inv
  right_inv f := f.right_inv
```

-- snippet 34
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearEquiv_toLinearEquiv_eq (L144-L147)
theorem toContinuousLinearEquiv_toLinearEquiv_eq (e : A ≃A[R] B) :
    e.toContinuousLinearEquiv.toLinearEquiv
    = e.toAlgEquiv.toLinearEquiv := rfl
```

-- snippet 35
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe (L78-L79)
instance coe : Coe (A ≃A[R] B) (A →A[R] B) := ⟨toContinuousAlgHom⟩
```

-- snippet 36
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.isOpenMap (L148-L150)
theorem isOpenMap (e : A ≃A[R] B) : IsOpenMap e :=
e.toHomeomorph.isOpenMap
```

-- snippet 37
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toAlgEquiv_injective (L106-L109)
theorem toAlgEquiv_injective : Function.Injective (toAlgEquiv : (A ≃A[R] B) → A ≃ₐ[R] B) := by
rintro ⟨e, _, _⟩ ⟨e', _, _⟩ rfl
  rfl
```

-- snippet 38
```lean
-- Mathlib.Topology.Algebra.Affine.isOpenMap_linear_iff (L46-L58)
theorem isOpenMap_linear_iff {f : P →ᵃ[R] Q} : IsOpenMap f.linear ↔ IsOpenMap f := by
inhabit P
  have :
    (f.linear : V → W) =
      (Homeomorph.vaddConst <| f default).symm ∘ f ∘ (Homeomorph.vaddConst default) := by
    ext v
    simp
  rw [this]
  simp only [Homeomorph.comp_isOpenMap_iff, Homeomorph.comp_isOpenMap_iff']

variable [TopologicalSpace R] [ContinuousSMul R V]

/-- The line map is continuous in all arguments. -/
```

-- snippet 39
```lean
-- Mathlib.Topology.Algebra.Affine.homothety_continuous (L118-L125)
theorem homothety_continuous (x : P) (t : R) : Continuous <| homothety x t := by
rw [coe_homothety]
  fun_prop

variable (R) [TopologicalSpace R] [Module R W] [ContinuousSMul R W] (x : Q) {s : Set Q}

open Topology
```

-- snippet 40
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousAlgHom (L74-L77)
def toContinuousAlgHom (e : A ≃A[R] B) : A →A[R] B where
  __ := e.toAlgHom
cont := e.continuous_toFun
```

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

```lean
def toWeakSpaceCLM : E →L[𝕜] WeakSpace 𝕜 E where
  __
```

## Retrieved context
-- snippet 1
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.WeakDual (L61-L76)
def WeakDual (𝕜 E : Type*) [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
    [ContinuousConstSMul 𝕜 𝕜] [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E] :=
WeakBilin (topDualPairing 𝕜 E)
deriving AddCommMonoid, Module 𝕜, TopologicalSpace, ContinuousAdd, Inhabited,
  FunLike, ContinuousLinearMapClass

namespace WeakDual

section Semiring

variable [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
variable [ContinuousConstSMul 𝕜 𝕜]
variable [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E]

/-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with
multiplication on `𝕜`, then it acts on `WeakDual 𝕜 E`. -/
```

-- snippet 2
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instMulAction (L77-L82)
instance instMulAction (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [ContinuousConstSMul M 𝕜] : MulAction M (WeakDual 𝕜 E) :=
inferInstanceAs <| MulAction M (E →L[𝕜] 𝕜)

/-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with
multiplication on `𝕜`, then it acts distributively on `WeakDual 𝕜 E`. -/
```

-- snippet 3
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instDistribMulAction (L83-L88)
instance instDistribMulAction (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [ContinuousConstSMul M 𝕜] : DistribMulAction M (WeakDual 𝕜 E) :=
inferInstanceAs <| DistribMulAction M (E →L[𝕜] 𝕜)

/-- If `𝕜` is a topological module over a semiring `R` and scalar multiplication commutes with the
multiplication on `𝕜`, then `WeakDual 𝕜 E` is a module over `R`. -/
```

-- snippet 4
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instModule' (L89-L92)
instance instModule' (R) [Semiring R] [Module R 𝕜] [SMulCommClass 𝕜 R 𝕜] [ContinuousConstSMul R 𝕜] :
    Module R (WeakDual 𝕜 E) :=
inferInstanceAs <| Module R (E →L[𝕜] 𝕜)
```

-- snippet 5
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instContinuousConstSMul (L93-L99)
instance instContinuousConstSMul (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [ContinuousConstSMul M 𝕜] : ContinuousConstSMul M (WeakDual 𝕜 E) :=
⟨fun m =>
    continuous_induced_rng.2 <| (WeakBilin.coeFn_continuous (topDualPairing 𝕜 E)).const_smul m⟩

/-- If a monoid `M` distributively continuously acts on `𝕜` and this action commutes with
multiplication on `𝕜`, then it continuously acts on `WeakDual 𝕜 E`. -/
```

-- snippet 6
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instContinuousSMul (L100-L104)
instance instContinuousSMul (M) [Monoid M] [DistribMulAction M 𝕜] [SMulCommClass 𝕜 M 𝕜]
    [TopologicalSpace M] [ContinuousSMul M 𝕜] : ContinuousSMul M (WeakDual 𝕜 E) :=
⟨continuous_induced_rng.2 <|
      continuous_fst.smul ((WeakBilin.coeFn_continuous (topDualPairing 𝕜 E)).comp continuous_snd)⟩
```

-- snippet 7
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.coeFn_continuous (L105-L107)
theorem coeFn_continuous : Continuous fun (x : WeakDual 𝕜 E) y => x y :=
continuous_induced_dom
```

-- snippet 8
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.eval_continuous (L108-L110)
theorem eval_continuous (y : E) : Continuous fun x : WeakDual 𝕜 E => x y :=
continuous_pi_iff.mp coeFn_continuous y
```

-- snippet 9
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.continuous_of_continuous_eval (L111-L114)
theorem continuous_of_continuous_eval [TopologicalSpace α] {g : α → WeakDual 𝕜 E}
    (h : ∀ y, Continuous fun a => (g a) y) : Continuous g :=
continuous_induced_rng.2 (continuous_pi_iff.mpr h)
```

-- snippet 10
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instT2Space (L115-L124)
instance instT2Space [T2Space 𝕜] : T2Space (WeakDual 𝕜 E) :=
(WeakBilin.isEmbedding ContinuousLinearMap.coe_injective).t2Space

end Semiring

section Ring

variable [CommRing 𝕜] [TopologicalSpace 𝕜] [IsTopologicalAddGroup 𝕜] [ContinuousConstSMul 𝕜 𝕜]
variable [AddCommGroup E] [Module 𝕜 E] [TopologicalSpace E] [IsTopologicalAddGroup E]
```

-- snippet 11
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instAddCommGroup (L125-L127)
instance instAddCommGroup : AddCommGroup (WeakDual 𝕜 E) :=
inferInstanceAs <| AddCommGroup (WeakBilin (topDualPairing 𝕜 E))
```

-- snippet 12
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instIsTopologicalAddGroup (L128-L136)
instance instIsTopologicalAddGroup : IsTopologicalAddGroup (WeakDual 𝕜 E) :=
WeakBilin.instIsTopologicalAddGroup (topDualPairing 𝕜 E)

end Ring

end WeakDual

/-- The weak topology is the topology coarsest topology on `E` such that all functionals
`fun x => v x` are continuous. -/
```

-- snippet 13
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.WeakSpace (L137-L149)
def WeakSpace (𝕜 E) [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
    [ContinuousConstSMul 𝕜 𝕜] [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E] :=
WeakBilin (topDualPairing 𝕜 E).flip
deriving AddCommMonoid, Module 𝕜, TopologicalSpace, ContinuousAdd

section Semiring

variable [CommSemiring 𝕜] [TopologicalSpace 𝕜] [ContinuousAdd 𝕜]
variable [ContinuousConstSMul 𝕜 𝕜]
variable [AddCommMonoid E] [Module 𝕜 E] [TopologicalSpace E]

namespace WeakSpace
```

-- snippet 14
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instModule' (L150-L152)
instance instModule' [CommSemiring 𝕝] [Module 𝕝 E] : Module 𝕝 (WeakSpace 𝕜 E) :=
inferInstanceAs <| Module 𝕝 (WeakBilin (topDualPairing 𝕜 E).flip)
```

-- snippet 15
```lean
-- Mathlib.Topology.Algebra.Module.WeakDual.instIsScalarTower (L153-L156)
instance instIsScalarTower [CommSemiring 𝕝] [Module 𝕝 𝕜] [Module 𝕝 E] [IsScalarTower 𝕝 𝕜 E] :
    IsScalarTower 𝕝 𝕜 (WeakSpace 𝕜 E) :=
WeakBilin.instIsScalarTower (topDualPairing 𝕜 E).flip
```

-- snippet 16
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearMap_toContinuousLinearEquiv_eq (L140-L143)
theorem toContinuousLinearMap_toContinuousLinearEquiv_eq (e : A ≃A[R] B) :
    e.toContinuousLinearEquiv.toContinuousLinearMap
    = e.toContinuousAlgHom.toContinuousLinearMap := rfl
```

-- snippet 17
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.image_closure (L151-L153)
theorem image_closure (e : A ≃A[R] B) (S : Set A) : e '' closure S = closure (e '' S) :=
e.toHomeomorph.image_closure S
```

-- snippet 18
```lean
-- Mathlib.Topology.Algebra.Affine.lineMap_continuous (L66-L75)
theorem lineMap_continuous {p q : P} :
    Continuous (lineMap p q : R →ᵃ[R] P) := by
fun_prop

open Topology Filter

section Tendsto

variable {α : Type*} {l : Filter α}
```

-- snippet 19
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearEquiv_apply (L137-L139)
theorem toContinuousLinearEquiv_apply (e : A ≃A[R] B) (a : A) :
    e.toContinuousLinearEquiv a = e a := rfl
```

-- snippet 20
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.ext (L111-L113)
theorem ext {f g : A ≃A[R] B} (h : ⇑f = ⇑g) : f = g :=
toAlgEquiv_injective <| AlgEquiv.ext <| congr_fun h
```

-- snippet 21
```lean
-- Mathlib.Topology.Algebra.Affine.continuous_linear_iff (L35-L45)
theorem continuous_linear_iff {f : P →ᵃ[R] Q} : Continuous f.linear ↔ Continuous f := by
inhabit P
  have :
    (f.linear : V → W) =
      (Homeomorph.vaddConst <| f default).symm ∘ f ∘ (Homeomorph.vaddConst default) := by
    ext v
    simp
  rw [this]
  simp only [Homeomorph.comp_continuous_iff, Homeomorph.comp_continuous_iff']

/-- If `f` is an affine map, then its linear part is an open map iff `f` is an open map. -/
```

-- snippet 22
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_toAlgEquiv (L122-L125)
theorem coe_toAlgEquiv (e : A ≃A[R] B) : ⇑e.toAlgEquiv = e := rfl
/-- The natural coercion from a continuous algebra isomorphism
to a continuous linear isomorphism. -/
```

-- snippet 23
```lean
-- Mathlib.Topology.Algebra.AffineSubspace.subtypeA (L33-L36)
def subtypeA (s : AffineSubspace R P) [Nonempty s] : s →ᴬ[R] P where
  toAffineMap := s.subtype
cont := continuous_subtype_val
```

-- snippet 24
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_injective (L114-L116)
theorem coe_injective : Function.Injective ((↑) : (A ≃A[R] B) → A →A[R] B) :=
fun _ _ h => ext <| funext <| ContinuousAlgHom.ext_iff.1 h
```

-- snippet 25
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_apply (L99-L100)
theorem coe_apply (e : A ≃A[R] B) (a : A) : (e : A →A[R] B) a = e a := rfl
```

-- snippet 26
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearEquiv (L127-L129)
def toContinuousLinearEquiv (e : A ≃A[R] B) : A ≃L[R] B :=
{ e with __ := e.toLinearEquiv }
```

-- snippet 27
```lean
-- Mathlib.Topology.Algebra.Affine.lineMap_continuous_uncurry (L60-L65)
theorem lineMap_continuous_uncurry :
    Continuous (fun pqt : P × P × R ↦ lineMap pqt.1 pqt.2.1 pqt.2.2) := by
simp only [coe_lineMap]
  fun_prop

/-- The line map is continuous. -/
```

-- snippet 28
```lean
-- Mathlib.Topology.Algebra.AffineSubspace.isClosed_direction_iff (L54-L61)
theorem isClosed_direction_iff [T1Space V] (s : AffineSubspace R P) :
    IsClosed (s.direction : Set V) ↔ IsClosed (s : Set P) := by
rcases s.eq_bot_or_nonempty with (rfl | ⟨x, hx⟩); · simp
  rw [← (Homeomorph.vaddConst x).symm.isClosed_image,
    AffineSubspace.coe_direction_eq_vsub_set_right hx]
  simp only [Homeomorph.vaddConst_symm_apply]

end AffineSubspace
```

-- snippet 29
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_inj (L118-L120)
theorem coe_inj {f g : A ≃A[R] B} : (f : A →A[R] B) = g ↔ f = g :=
coe_injective.eq_iff
```

-- snippet 30
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.continuousAlgEquivClass (L92-L98)
instance continuousAlgEquivClass : ContinuousAlgEquivClass (A ≃A[R] B) R A B where
  map_add f := f.map_add'
map_mul f := f.map_mul'
  commutes f := f.commutes'
  map_continuous := continuous_toFun
  inv_continuous := continuous_invFun
```

-- snippet 31
```lean
-- Mathlib.Topology.Algebra.Affine.homothety_isOpenMap (L146-L152)
theorem homothety_isOpenMap (x : P) (t : R) (ht : t ≠ 0) : IsOpenMap <| homothety x t := by
apply IsOpenMap.of_inverse (homothety_continuous x t⁻¹) <;> intro e <;>
    simp [← AffineMap.comp_apply, ← homothety_mul, ht]

end Field

end AffineMap
```

-- snippet 32
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe_coe (L104-L105)
theorem coe_coe (e : A ≃A[R] B) : ⇑(e : A →A[R] B) = e := rfl
```

-- snippet 33
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.equivLike (L80-L91)
instance equivLike : EquivLike (A ≃A[R] B) A B where
  coe f := f.toFun
inv f := f.invFun
  coe_injective' f g h₁ h₂ := by
    obtain ⟨f', _⟩ := f
    obtain ⟨g', _⟩ := g
    rcases f' with ⟨⟨_, _⟩, _⟩
    rcases g' with ⟨⟨_, _⟩, _⟩
    congr
  left_inv f := f.left_inv
  right_inv f := f.right_inv
```

-- snippet 34
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousLinearEquiv_toLinearEquiv_eq (L144-L147)
theorem toContinuousLinearEquiv_toLinearEquiv_eq (e : A ≃A[R] B) :
    e.toContinuousLinearEquiv.toLinearEquiv
    = e.toAlgEquiv.toLinearEquiv := rfl
```

-- snippet 35
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.coe (L78-L79)
instance coe : Coe (A ≃A[R] B) (A →A[R] B) := ⟨toContinuousAlgHom⟩
```

-- snippet 36
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.isOpenMap (L148-L150)
theorem isOpenMap (e : A ≃A[R] B) : IsOpenMap e :=
e.toHomeomorph.isOpenMap
```

-- snippet 37
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toAlgEquiv_injective (L106-L109)
theorem toAlgEquiv_injective : Function.Injective (toAlgEquiv : (A ≃A[R] B) → A ≃ₐ[R] B) := by
rintro ⟨e, _, _⟩ ⟨e', _, _⟩ rfl
  rfl
```

-- snippet 38
```lean
-- Mathlib.Topology.Algebra.Affine.isOpenMap_linear_iff (L46-L58)
theorem isOpenMap_linear_iff {f : P →ᵃ[R] Q} : IsOpenMap f.linear ↔ IsOpenMap f := by
inhabit P
  have :
    (f.linear : V → W) =
      (Homeomorph.vaddConst <| f default).symm ∘ f ∘ (Homeomorph.vaddConst default) := by
    ext v
    simp
  rw [this]
  simp only [Homeomorph.comp_isOpenMap_iff, Homeomorph.comp_isOpenMap_iff']

variable [TopologicalSpace R] [ContinuousSMul R V]

/-- The line map is continuous in all arguments. -/
```

-- snippet 39
```lean
-- Mathlib.Topology.Algebra.Affine.homothety_continuous (L118-L125)
theorem homothety_continuous (x : P) (t : R) : Continuous <| homothety x t := by
rw [coe_homothety]
  fun_prop

variable (R) [TopologicalSpace R] [Module R W] [ContinuousSMul R W] (x : Q) {s : Set Q}

open Topology
```

-- snippet 40
```lean
-- Mathlib.Topology.Algebra.Algebra.Equiv.toContinuousAlgHom (L74-L77)
def toContinuousAlgHom (e : A ≃A[R] B) : A →A[R] B where
  __ := e.toAlgHom
cont := e.continuous_toFun
```
[/INST]