Module docstring
{"# Ideals generated by a set of elements
This file defines Ideal.span s as the ideal generated by the subset s of the ring.
TODO
Support right ideals, and two-sided ideals over non-commutative rings. "}
{"# Ideals generated by a set of elements
This file defines Ideal.span s as the ideal generated by the subset s of the ring.
Support right ideals, and two-sided ideals over non-commutative rings. "}
IsPrincipalIdealRing
      structure
     (R : Type u) [Semiring R]
        /-- A ring is a principal ideal ring if all (left) ideals are principal. -/
@[mk_iff]
class IsPrincipalIdealRing (R : Type u) [Semiring R] : Prop where
  principal : ∀ S : Ideal R, S.IsPrincipal
        Ideal.span
      definition
     (s : Set α) : Ideal α
        /-- The ideal generated by a subset of a ring -/
def span (s : Set α) : Ideal α :=
  Submodule.span α s
        Ideal.submodule_span_eq
      theorem
     {s : Set α} : Submodule.span α s = Ideal.span s
        @[simp]
theorem submodule_span_eq {s : Set α} : Submodule.span α s = Ideal.span s :=
  rfl
        Ideal.span_empty
      theorem
     : span (∅ : Set α) = ⊥
        @[simp]
theorem span_empty : span (∅ : Set α) = ⊥ :=
  Submodule.span_empty
        Ideal.span_univ
      theorem
     : span (Set.univ : Set α) = ⊤
        
      Ideal.span_union
      theorem
     (s t : Set α) : span (s ∪ t) = span s ⊔ span t
        theorem span_union (s t : Set α) : span (s ∪ t) = spanspan s ⊔ span t :=
  Submodule.span_union _ _
        Ideal.span_iUnion
      theorem
     {ι} (s : ι → Set α) : span (⋃ i, s i) = ⨆ i, span (s i)
        theorem span_iUnion {ι} (s : ι → Set α) : span (⋃ i, s i) = ⨆ i, span (s i) :=
  Submodule.span_iUnion _
        Ideal.mem_span
      theorem
     {s : Set α} (x) : x ∈ span s ↔ ∀ p : Ideal α, s ⊆ p → x ∈ p
        theorem mem_span {s : Set α} (x) : x ∈ span sx ∈ span s ↔ ∀ p : Ideal α, s ⊆ p → x ∈ p :=
  mem_iInter₂
        Ideal.subset_span
      theorem
     {s : Set α} : s ⊆ span s
        theorem subset_span {s : Set α} : s ⊆ span s :=
  Submodule.subset_span
        Ideal.span_le
      theorem
     {s : Set α} {I} : span s ≤ I ↔ s ⊆ I
        theorem span_le {s : Set α} {I} : spanspan s ≤ I ↔ s ⊆ I :=
  Submodule.span_le
        Ideal.span_mono
      theorem
     {s t : Set α} : s ⊆ t → span s ≤ span t
        
      Ideal.span_eq
      theorem
     : span (I : Set α) = I
        @[simp]
theorem span_eq : span (I : Set α) = I :=
  Submodule.span_eq _
        Ideal.span_singleton_one
      theorem
     : span ({ 1 } : Set α) = ⊤
        @[simp]
theorem span_singleton_one : span ({1} : Set α) = ⊤ :=
  (eq_top_iff_one _).2 <| subset_span <| mem_singleton _
        Ideal.isCompactElement_top
      theorem
     : CompleteLattice.IsCompactElement (⊤ : Ideal α)
        theorem isCompactElement_top : CompleteLattice.IsCompactElement (⊤ : Ideal α) := by
  simpa only [← span_singleton_one] using Submodule.singleton_span_isCompactElement 1
        Ideal.mem_span_insert
      theorem
     {s : Set α} {x y} : x ∈ span (insert y s) ↔ ∃ a, ∃ z ∈ span s, x = a * y + z
        theorem mem_span_insert {s : Set α} {x y} :
    x ∈ span (insert y s)x ∈ span (insert y s) ↔ ∃ a, ∃ z ∈ span s, x = a * y + z :=
  Submodule.mem_span_insert
        Ideal.mem_span_singleton'
      theorem
     {x y : α} : x ∈ span ({ y } : Set α) ↔ ∃ a, a * y = x
        theorem mem_span_singleton' {x y : α} : x ∈ span ({y} : Set α)x ∈ span ({y} : Set α) ↔ ∃ a, a * y = x :=
  Submodule.mem_span_singleton
        Ideal.mem_span_singleton_self
      theorem
     (x : α) : x ∈ span ({ x } : Set α)
        theorem mem_span_singleton_self (x : α) : x ∈ span ({x} : Set α) :=
  Submodule.mem_span_singleton_self x
        Ideal.span_singleton_le_iff_mem
      theorem
     {x : α} : span { x } ≤ I ↔ x ∈ I
        theorem span_singleton_le_iff_mem {x : α} : spanspan {x} ≤ I ↔ x ∈ I :=
  Submodule.span_singleton_le_iff_mem _ _
        Ideal.span_singleton_mul_left_unit
      theorem
     {a : α} (h2 : IsUnit a) (x : α) : span ({a * x} : Set α) = span { x }
        theorem span_singleton_mul_left_unit {a : α} (h2 : IsUnit a) (x : α) :
    span ({a * x} : Set α) = span {x} := by
  apply le_antisymm <;> rw [span_singleton_le_iff_mem, mem_span_singleton']
  exacts [⟨a, rfl⟩, ⟨_, h2.unit.inv_mul_cancel_left x⟩]
        Ideal.span_insert
      theorem
     (x) (s : Set α) : span (insert x s) = span ({ x } : Set α) ⊔ span s
        theorem span_insert (x) (s : Set α) : span (insert x s) = spanspan ({x} : Set α) ⊔ span s :=
  Submodule.span_insert x s
        Ideal.span_eq_bot
      theorem
     {s : Set α} : span s = ⊥ ↔ ∀ x ∈ s, (x : α) = 0
        theorem span_eq_bot {s : Set α} : spanspan s = ⊥ ↔ ∀ x ∈ s, (x : α) = 0 :=
  Submodule.span_eq_bot
        Ideal.span_singleton_eq_bot
      theorem
     {x} : span ({ x } : Set α) = ⊥ ↔ x = 0
        @[simp]
theorem span_singleton_eq_bot {x} : spanspan ({x} : Set α) = ⊥ ↔ x = 0 :=
  Submodule.span_singleton_eq_bot
        Ideal.span_singleton_ne_top
      theorem
     {α : Type*} [CommSemiring α] {x : α} (hx : ¬IsUnit x) : Ideal.span ({ x } : Set α) ≠ ⊤
        theorem span_singleton_ne_top {α : Type*} [CommSemiring α] {x : α} (hx : ¬IsUnit x) :
    Ideal.spanIdeal.span ({x} : Set α) ≠ ⊤ :=
  (Ideal.ne_top_iff_one _).mpr fun h1 =>
    let ⟨y, hy⟩ := Ideal.mem_span_singleton'.mp h1
    hx ⟨⟨x, y, mul_comm y x ▸ hy, hy⟩, rfl⟩
        Ideal.span_zero
      theorem
     : span (0 : Set α) = ⊥
        @[simp]
theorem span_zero : span (0 : Set α) = ⊥ := by rw [← Set.singleton_zero, span_singleton_eq_bot]
        Ideal.span_one
      theorem
     : span (1 : Set α) = ⊤
        @[simp]
theorem span_one : span (1 : Set α) = ⊤ := by rw [← Set.singleton_one, span_singleton_one]
        Ideal.span_eq_top_iff_finite
      theorem
     (s : Set α) : span s = ⊤ ↔ ∃ s' : Finset α, ↑s' ⊆ s ∧ span (s' : Set α) = ⊤
        
      Ideal.mem_span_singleton_sup
      theorem
     {x y : α} {I : Ideal α} : x ∈ Ideal.span { y } ⊔ I ↔ ∃ a : α, ∃ b ∈ I, a * y + b = x
        theorem mem_span_singleton_sup {x y : α} {I : Ideal α} :
    x ∈ Ideal.span {y} ⊔ Ix ∈ Ideal.span {y} ⊔ I ↔ ∃ a : α, ∃ b ∈ I, a * y + b = x := by
  rw [Submodule.mem_sup]
  constructor
  · rintro ⟨ya, hya, b, hb, rfl⟩
    obtain ⟨a, rfl⟩ := mem_span_singleton'.mp hya
    exact ⟨a, b, hb, rfl⟩
  · rintro ⟨a, b, hb, rfl⟩
    exact ⟨a * y, Ideal.mem_span_singleton'.mpr ⟨a, rfl⟩, b, hb, rfl⟩
        Ideal.ofRel
      definition
     (r : α → α → Prop) : Ideal α
        /-- The ideal generated by an arbitrary binary relation.
-/
def ofRel (r : α → α → Prop) : Ideal α :=
  Submodule.span α { x | ∃ a b, r a b ∧ x + b = a }
        Ideal.zero_ne_one_of_proper
      theorem
     {I : Ideal α} (h : I ≠ ⊤) : (0 : α) ≠ 1
        theorem zero_ne_one_of_proper {I : Ideal α} (h : I ≠ ⊤) : (0 : α) ≠ 1 := fun hz =>
  I.ne_top_iff_one.1 h <| hz ▸ I.zero_mem
        Ideal.span_pair_comm
      theorem
     {x y : α} : (span { x, y } : Ideal α) = span { y, x }
        theorem span_pair_comm {x y : α} : (span {x, y} : Ideal α) = span {y, x} := by
  simp only [span_insert, sup_comm]
        Ideal.mem_span_pair
      theorem
     {x y z : α} : z ∈ span ({ x, y } : Set α) ↔ ∃ a b, a * x + b * y = z
        theorem mem_span_pair {x y z : α} : z ∈ span ({x, y} : Set α)z ∈ span ({x, y} : Set α) ↔ ∃ a b, a * x + b * y = z :=
  Submodule.mem_span_pair
        Ideal.span_pair_add_mul_left
      theorem
     {R : Type u} [CommRing R] {x y : R} (z : R) : (span {x + y * z, y} : Ideal R) = span { x, y }
        @[simp]
theorem span_pair_add_mul_left {R : Type u} [CommRing R] {x y : R} (z : R) :
    (span {x + y * z, y} : Ideal R) = span {x, y} := by
  ext
  rw [mem_span_pair, mem_span_pair]
  exact
    ⟨fun ⟨a, b, h⟩ =>
      ⟨a, b + a * z, by
        rw [← h]
        ring1⟩,
      fun ⟨a, b, h⟩ =>
      ⟨a, b - a * z, by
        rw [← h]
        ring1⟩⟩
        Ideal.span_pair_add_mul_right
      theorem
     {R : Type u} [CommRing R] {x y : R} (z : R) : (span {x, y + x * z} : Ideal R) = span { x, y }
        @[simp]
theorem span_pair_add_mul_right {R : Type u} [CommRing R] {x y : R} (z : R) :
    (span {x, y + x * z} : Ideal R) = span {x, y} := by
  rw [span_pair_comm, span_pair_add_mul_left, span_pair_comm]
        Ideal.mem_span_singleton
      theorem
     {x y : α} : x ∈ span ({ y } : Set α) ↔ y ∣ x
        theorem mem_span_singleton {x y : α} : x ∈ span ({y} : Set α)x ∈ span ({y} : Set α) ↔ y ∣ x :=
  mem_span_singleton'.trans <| exists_congr fun _ => by rw [eq_comm, mul_comm]
        Ideal.span_singleton_le_span_singleton
      theorem
     {x y : α} : span ({ x } : Set α) ≤ span ({ y } : Set α) ↔ y ∣ x
        theorem span_singleton_le_span_singleton {x y : α} :
    spanspan ({x} : Set α) ≤ span ({y} : Set α) ↔ y ∣ x :=
  span_le.trans <| singleton_subset_iff.trans mem_span_singleton
        Ideal.span_singleton_eq_span_singleton
      theorem
     {α : Type u} [CommSemiring α] [IsDomain α] {x y : α} : span ({ x } : Set α) = span ({ y } : Set α) ↔ Associated x y
        theorem span_singleton_eq_span_singleton {α : Type u} [CommSemiring α] [IsDomain α] {x y : α} :
    spanspan ({x} : Set α) = span ({y} : Set α) ↔ Associated x y := by
  rw [← dvd_dvd_iff_associated, le_antisymm_iff, and_comm]
  apply and_congr <;> rw [span_singleton_le_span_singleton]
        Ideal.span_singleton_mul_right_unit
      theorem
     {a : α} (h2 : IsUnit a) (x : α) : span ({x * a} : Set α) = span { x }
        theorem span_singleton_mul_right_unit {a : α} (h2 : IsUnit a) (x : α) :
    span ({x * a} : Set α) = span {x} := by rw [mul_comm, span_singleton_mul_left_unit h2]
        Ideal.span_singleton_eq_top
      theorem
     {x} : span ({ x } : Set α) = ⊤ ↔ IsUnit x
        @[simp]
theorem span_singleton_eq_top {x} : spanspan ({x} : Set α) = ⊤ ↔ IsUnit x := by
  rw [isUnit_iff_dvd_one, ← span_singleton_le_span_singleton, span_singleton_one, eq_top_iff]
        Ideal.factors_decreasing
      theorem
     [IsDomain α] (b₁ b₂ : α) (h₁ : b₁ ≠ 0) (h₂ : ¬IsUnit b₂) : span ({b₁ * b₂} : Set α) < span { b₁ }
        theorem factors_decreasing [IsDomain α] (b₁ b₂ : α) (h₁ : b₁ ≠ 0) (h₂ : ¬IsUnit b₂) :
    span ({b₁ * b₂} : Set α) < span {b₁} :=
  lt_of_le_not_le
    (Ideal.span_le.2 <| singleton_subset_iff.2 <| Ideal.mem_span_singleton.2 ⟨b₂, rfl⟩) fun h =>
    h₂ <| isUnit_of_dvd_one <|
        (mul_dvd_mul_iff_left h₁).1 <| by rwa [mul_one, ← Ideal.span_singleton_le_span_singleton]
        Ideal.mem_span_insert'
      theorem
     {s : Set α} {x y} : x ∈ span (insert y s) ↔ ∃ a, x + a * y ∈ span s
        theorem mem_span_insert' {s : Set α} {x y} : x ∈ span (insert y s)x ∈ span (insert y s) ↔ ∃ a, x + a * y ∈ span s :=
  Submodule.mem_span_insert'
        Ideal.span_singleton_neg
      theorem
     (x : α) : (span {-x} : Ideal α) = span { x }
        @[simp]
theorem span_singleton_neg (x : α) : (span {-x} : Ideal α) = span {x} := by
  ext
  simp only [mem_span_singleton']
  exact ⟨fun ⟨y, h⟩ => ⟨-y, h ▸ neg_mul_comm y x⟩, fun ⟨y, h⟩ => ⟨-y, h ▸ neg_mul_neg y x⟩⟩
        IsIdempotentElem.ker_toSpanSingleton_eq_span
      theorem
     : LinearMap.ker (LinearMap.toSpanSingleton R R e) = Ideal.span {1 - e}
        theorem ker_toSpanSingleton_eq_span :
    LinearMap.ker (LinearMap.toSpanSingleton R R e) = Ideal.span {1 - e} := SetLike.ext fun x ↦ by
  rw [Ideal.mem_span_singleton']
  refine ⟨fun h ↦ ⟨x, by rw [mul_sub, show x * e = 0 from h, mul_one, sub_zero]⟩, fun h ↦ ?_⟩
  obtain ⟨x, rfl⟩ := h
  show x * (1 - e) * e = 0
  rw [mul_assoc, sub_mul, one_mul, he, sub_self, mul_zero]
        IsIdempotentElem.ker_toSpanSingleton_one_sub_eq_span
      theorem
     : LinearMap.ker (LinearMap.toSpanSingleton R R (1 - e)) = Ideal.span { e }
        theorem ker_toSpanSingleton_one_sub_eq_span :
    LinearMap.ker (LinearMap.toSpanSingleton R R (1 - e)) = Ideal.span {e} := by
  rw [ker_toSpanSingleton_eq_span he.one_sub, sub_sub_cancel]