🏛️ Atlas✒️Formalizations.NumberTheory.NumberField.InvariantOrders.Discriminant view on GitHub ↗224 theorems
/-Copyright (c) 2026 Carles Marín. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles Marín-/module public import Formalizations.NumberTheory.NumberField.InvariantOrders.BiquadraticModelpublic import Formalizations.NumberTheory.NumberField.InvariantOrders.TriangularBasispublic import Mathlib.LinearAlgebra.FreeModule.Finite.CardQuotientpublic import Mathlib.LinearAlgebra.Matrix.Adjugatepublic import Mathlib.GroupTheory.Indexpublic import Mathlib.RingTheory.Int.Basicpublic import Mathlib.Data.Nat.Factorization.Basic /-!# The discriminant and the invariant factors of `ℤ[conj α]` (biquadratic case) In the model `V4Data O` of `BiquadraticModel`, `A = ℤ[x₀, …, x₃]` is the subring generated by theconjugates (`Aring`), `ℤ[α] = ℤ + ℤα + ℤα² + ℤα³`, indices are `AddSubgroup.relIndex`, and`D(ω) = det[γ_i(ω_l)]`. The triangular basis of `A` (Louboutin 2020, Prop. 10; Cohen 1993,Cor. 4.7.6) enters as the structure `Louboutin.Triang` for the image of `A` in `ℚ⁴`; it is usedonly for `d₁, d₂, d₃` and the isomorphism. `d₁ = 1` is proved, not assumed. The route: `[A : L] = 2` for `L = ⟨1, η₁, η₂, η₃⟩` is shown by exhibiting `A = L + ℤy` throughring closure, where each product has an odd multiple in `L + ℤy` (a 2-adic computation) and apower-of-2 multiple in `⟨1, ϑ_j⟩`, glued by Bézout; indices are computed by determinants(`AddSubgroup.relIndex_eq_natAbs_det`); `d₂` is read from coordinates in the `ϑ`-basis, a globalreplacement of a local Nakayama argument. ## Main results * `Louboutin.DiscV4.thm_disc`: the discriminant theorem, with `(n_j, t_j)`, `h₀`, `m₀`, `g`.* `Louboutin.DiscV4.Dat.prop_discA`: `g ∣ Δ`, `[A : ℤ[α]] = |Δ|/g`, `g² = Δ₁Δ₂Δ₃`, and  `D(b)² = g²` for every basis `b` of `A` (so `disc A = g²`).* `Louboutin.DiscV4.Dat.prop_smith`: `h₀ ∈ {1, 2}`, `h₀ ∣ m₀`, `d₁ = 1`, `d₂ = m₀/h₀`,  `d₃ = I h₀/m₀`, `A/ℤ[α] ≅ ℤ/(m₀/h₀) × ℤ/(I h₀/m₀)`, and `{1, α, α², ω}` is a basis iff `m₀ = h₀`. ## References * S. R. Louboutin, *On the order generated by the conjugates of an algebraic integer*, in:  Algebra, Logic and Number Theory, Banach Center Publ. 121 (2020), 107–124, doi:10.4064/bc121-10.* S. R. Louboutin, *When is the order generated by a cubic, quartic or quintic algebraic unit Galois  invariant: three conjectures*, Czechoslovak Math. J. 70 (2020), 905–919,  doi:10.21136/CMJ.2020.0019-19.* C. Marín, *Orders generated by conjugates of Galois quartic integers*, preprint (2026).-/ @[expose] public section namespace Louboutin.DiscV4 set_option linter.unusedSectionVars false /-! ## Part 1. Pure integer core -/ /-- Integer data extracted from the model (see `Dat.toZD`). -/structure ZD where  a : ℤ  n₁ : ℤ  n₂ : ℤ  n₃ : ℤ  t₁ : ℤ  t₂ : ℤ  t₃ : ℤ  e₁ : ℤ  e₂ : ℤ  e₃ : ℤ  Δ₁ : ℤ  Δ₂ : ℤ  Δ₃ : ℤ  Θ : ℤ  δ : ℤ  cop₁ : IsCoprime n₁ t₁  cop₂ : IsCoprime n₂ t₂  cop₃ : IsCoprime n₃ t₃  I₁ : n₂ * n₃ * Θ = (a * n₁ - 4 * t₁) * Δ₁  I₂ : n₁ * n₃ * Θ = (a * n₂ - 4 * t₂) * Δ₂  I₃ : n₁ * n₂ * Θ = (a * n₃ - 4 * t₃) * Δ₃  hΘ2 : Θ * Θ = Δ₁ * Δ₂ * Δ₃  Θ0 : Θ ≠ 0  nz₁ : ¬(n₂ = 0 ∧ n₃ = 0)  nz₂ : ¬(n₁ = 0 ∧ n₃ = 0)  nz₃ : ¬(n₁ = 0 ∧ n₂ = 0)  sq₁ : 4 ∣ Δ₁ - e₁ ^ 2  sq₂ : 4 ∣ Δ₂ - e₂ ^ 2  sq₃ : 4 ∣ Δ₃ - e₃ ^ 2  asq₁ : 4 ∣ a ^ 2 - n₁ ^ 2 * Δ₁  F1 : 2 ∣ n₁ → 2 ∣ n₂ → 2 ∣ n₃ → 4 ∣ a - (n₁ * e₁ + n₂ * e₂ + n₃ * e₃)  minors : Θ * ((n₁ * t₂ - n₂ * t₁) * (n₂ * t₃ - n₃ * t₂) * (n₃ * t₁ - n₁ * t₃))    = δ * (n₁ * n₂ * n₃)  δ0 : δ ≠ 0 namespace ZD variable (Z : ZD) theorem Δ0₁ : Z.Δ₁ ≠ 0 := by  intro h; apply Z.Θ0; have := Z.hΘ2; rw [h] at this; simpa using thistheorem Δ0₂ : Z.Δ₂ ≠ 0 := by  intro h; apply Z.Θ0; have := Z.hΘ2; rw [h] at this; simpa using thistheorem Δ0₃ : Z.Δ₃ ≠ 0 := by  intro h; apply Z.Θ0; have := Z.hΘ2; rw [h] at this; simpa using this /-- pair identities `P_l`: n_j n_k n_l² Δ_l = c_j c_k. -/theorem P₃ : Z.n₁ * Z.n₂ * Z.n₃ ^ 2 * Z.Δ₃ = (Z.a * Z.n₁ - 4 * Z.t₁) * (Z.a * Z.n₂ - 4 * Z.t₂) := by  apply mul_left_cancel₀ (mul_ne_zero Z.Δ0₁ Z.Δ0₂)  linear_combination (-(Z.n₁ * Z.n₂ * Z.n₃ ^ 2)) * Z.hΘ2 + (Z.n₁ * Z.n₃ * Z.Θ) * Z.I₁    + ((Z.a * Z.n₁ - 4 * Z.t₁) * Z.Δ₁) * Z.I₂ theorem P₂ : Z.n₁ * Z.n₃ * Z.n₂ ^ 2 * Z.Δ₂ = (Z.a * Z.n₁ - 4 * Z.t₁) * (Z.a * Z.n₃ - 4 * Z.t₃) := by  apply mul_left_cancel₀ (mul_ne_zero Z.Δ0₁ Z.Δ0₃)  linear_combination (-(Z.n₁ * Z.n₃ * Z.n₂ ^ 2)) * Z.hΘ2 + (Z.n₁ * Z.n₂ * Z.Θ) * Z.I₁    + ((Z.a * Z.n₁ - 4 * Z.t₁) * Z.Δ₁) * Z.I₃ theorem P₁ : Z.n₂ * Z.n₃ * Z.n₁ ^ 2 * Z.Δ₁ = (Z.a * Z.n₂ - 4 * Z.t₂) * (Z.a * Z.n₃ - 4 * Z.t₃) := by  apply mul_left_cancel₀ (mul_ne_zero Z.Δ0₂ Z.Δ0₃)  linear_combination (-(Z.n₂ * Z.n₃ * Z.n₁ ^ 2)) * Z.hΘ2 + (Z.n₁ * Z.n₂ * Z.Θ) * Z.I₂    + ((Z.a * Z.n₂ - 4 * Z.t₂) * Z.Δ₂) * Z.I₃ /-- triple identity: (n₁n₂n₃)² Θ = c₁c₂c₃. -/theorem triple : (Z.n₁ * Z.n₂ * Z.n₃) ^ 2 * Z.Θ    = (Z.a * Z.n₁ - 4 * Z.t₁) * (Z.a * Z.n₂ - 4 * Z.t₂) * (Z.a * Z.n₃ - 4 * Z.t₃) := by  apply mul_left_cancel₀ (mul_ne_zero Z.Θ0 Z.Θ0)  linear_combination (Z.n₁ * Z.n₃ * Z.Θ * (Z.n₁ * Z.n₂ * Z.Θ)) * Z.I₁    + ((Z.a * Z.n₁ - 4 * Z.t₁) * Z.Δ₁ * (Z.n₁ * Z.n₂ * Z.Θ)) * Z.I₂    + ((Z.a * Z.n₁ - 4 * Z.t₁) * Z.Δ₁ * ((Z.a * Z.n₂ - 4 * Z.t₂) * Z.Δ₂)) * Z.I₃    - ((Z.a * Z.n₁ - 4 * Z.t₁) * (Z.a * Z.n₂ - 4 * Z.t₂) * (Z.a * Z.n₃ - 4 * Z.t₃)) * Z.hΘ2 end ZD /-! ### Parity helpers -/ theorem not_dvd_t {p n t : ℤ} (hp : Prime p) (h : IsCoprime n t) (hn : p ∣ n) : ¬ p ∣ t :=  fun ht => hp.not_isUnit (h.isUnit_of_dvd' hn ht) theorem c_not_dvd {p : ℤ} (hp : Prime p) (hp2 : ¬ p ∣ 2) {a n t : ℤ} (h : IsCoprime n t)    (hn : p ∣ n) : ¬ p ∣ a * n - 4 * t := by  intro hc  have h4t : p ∣ 4 * t := by    have e : 4 * t = a * n - (a * n - 4 * t) := by ring    rw [e]; exact dvd_sub (dvd_mul_of_dvd_right hn a) hc  rcases hp.dvd_or_dvd h4t with h4 | ht  · apply hp2    have e : (4 : ℤ) = 2 * 2 := by norm_num    rw [e] at h4    rcases hp.dvd_or_dvd h4 with h | h <;> exact h  · exact not_dvd_t hp h hn ht theorem odd_of_cop_even {n t : ℤ} (h : IsCoprime n t) (hn : 2 ∣ n) : ¬ Even t := by  rw [Int.not_even_iff_odd, ← Int.isCoprime_two_left]  exact h.of_isCoprime_of_dvd_left hn theorem sq_mod4_odd {e : ℤ} (he : ¬ Even e) : ¬ (4 : ℤ) ∣ e ^ 2 := by  rw [Int.not_even_iff_odd] at he  obtain ⟨k, rfl⟩ := he  rintro ⟨m, hm⟩  have : (1 : ℤ) = 4 * (m - k ^ 2 - k) := by linear_combination hm  omega theorem even_iff_of_dvd4 {Δ e : ℤ} (h : 4 ∣ Δ - e ^ 2) : (Even Δ ↔ Even e) := by  have h2 : Even (Δ - e ^ 2) := even_iff_two_dvd.mpr (dvd_trans ⟨2, by norm_num⟩ h)  rw [Int.even_sub, Int.even_pow] at h2  simpa using h2 theorem even_of_dvd4 {Δ e : ℤ} (h : 4 ∣ Δ - e ^ 2) (hΔ : 4 ∣ Δ) : Even e := by  by_contra he  apply sq_mod4_odd he  have : e ^ 2 = Δ - (Δ - e ^ 2) := by ring  rw [this]; exact dvd_sub hΔ h theorem cop4_of_odd {u : ℤ} (hu : ¬ Even u) : IsCoprime (4 : ℤ) u := by  have : IsCoprime (2 : ℤ) u := Int.isCoprime_two_left.mpr (Int.not_even_iff_odd.mp hu)  have h22 : IsCoprime ((2 : ℤ) ^ 2) u := this.pow_left  norm_num at h22  exact h22 /-- parity as a Boolean (for `decide`). -/def ev (x : ℤ) : Bool := decide (Even x) theorem ev_true {x : ℤ} : ev x = true ↔ Even x := by simp [ev]theorem ev_false {x : ℤ} : ev x = false ↔ ¬ Even x := by simp [ev]theorem ev_iff {x y : ℤ} (h : Even x ↔ Even y) : ev x = ev y := by simp [ev, h]theorem ev_mul (x y : ℤ) : ev (x * y) = (ev x || ev y) := by  simp only [ev, Int.even_mul]; by_cases hx : Even x <;> by_cases hy : Even y <;> simp [hx, hy]theorem ev_add (x y : ℤ) : ev (x + y) = (ev x == ev y) := by  simp only [ev, Int.even_add]; by_cases hx : Even x <;> by_cases hy : Even y <;> simp [hx, hy]theorem ev_sub (x y : ℤ) : ev (x - y) = (ev x == ev y) := by  simp only [ev, Int.even_sub]; by_cases hx : Even x <;> by_cases hy : Even y <;> simp [hx, hy]theorem ev_sq (x : ℤ) : ev (x ^ 2) = ev x := by  simp [ev, Int.even_pow] namespace ZD variable (Z : ZD) /-- the minors d_ij = n_i t_j − n_j t_i and the note's h₀, m₀. -/def d₁₂ : ℤ := Z.n₁ * Z.t₂ - Z.n₂ * Z.t₁def d₁₃ : ℤ := Z.n₁ * Z.t₃ - Z.n₃ * Z.t₁def d₂₃ : ℤ := Z.n₂ * Z.t₃ - Z.n₃ * Z.t₂def h0 : ℕ := Int.gcd (Int.gcd Z.n₁ Z.n₂) Z.n₃def m0 : ℕ := Int.gcd (Int.gcd Z.d₁₂ Z.d₁₃) Z.d₂₃ theorem h0_dvd₁ : (Z.h0 : ℤ) ∣ Z.n₁ := (Int.gcd_dvd_left ..).trans (Int.gcd_dvd_left ..)theorem h0_dvd₂ : (Z.h0 : ℤ) ∣ Z.n₂ := (Int.gcd_dvd_left ..).trans (Int.gcd_dvd_right ..)theorem h0_dvd₃ : (Z.h0 : ℤ) ∣ Z.n₃ := Int.gcd_dvd_right ..theorem m0_dvd₁₂ : (Z.m0 : ℤ) ∣ Z.d₁₂ := (Int.gcd_dvd_left ..).trans (Int.gcd_dvd_left ..)theorem m0_dvd₁₃ : (Z.m0 : ℤ) ∣ Z.d₁₃ := (Int.gcd_dvd_left ..).trans (Int.gcd_dvd_right ..)theorem m0_dvd₂₃ : (Z.m0 : ℤ) ∣ Z.d₂₃ := Int.gcd_dvd_right .. theorem dvd_h0 {k : ℤ} (h1 : k ∣ Z.n₁) (h2 : k ∣ Z.n₂) (h3 : k ∣ Z.n₃) : k ∣ (Z.h0 : ℤ) :=  Int.dvd_coe_gcd (Int.dvd_coe_gcd h1 h2) h3 theorem dvd_m0 {k : ℤ} (h1 : k ∣ Z.d₁₂) (h2 : k ∣ Z.d₁₃) (h3 : k ∣ Z.d₂₃) : k ∣ (Z.m0 : ℤ) :=  Int.dvd_coe_gcd (Int.dvd_coe_gcd h1 h2) h3 /-- No odd prime divides n₁, n₂, n₃ (proof of prop:smith, third item). -/theorem no_odd_prime {p : ℤ} (hp : Prime p) (hp2 : ¬ p ∣ 2) :    ¬ (p ∣ Z.n₁ ∧ p ∣ Z.n₂ ∧ p ∣ Z.n₃) := by  rintro ⟨h1, h2, h3⟩  have hL : p ∣ (Z.n₁ * Z.n₂ * Z.n₃) ^ 2 * Z.Θ :=    dvd_mul_of_dvd_left (dvd_pow (dvd_mul_of_dvd_left (dvd_mul_of_dvd_left h1 _) _) two_ne_zero) _  rw [Z.triple] at hL  rcases hp.dvd_or_dvd hL with h12 | h3'  · rcases hp.dvd_or_dvd h12 with h1' | h2'    · exact c_not_dvd hp hp2 Z.cop₁ h1 h1'    · exact c_not_dvd hp hp2 Z.cop₂ h2 h2'  · exact c_not_dvd hp hp2 Z.cop₃ h3 h3' theorem a_even (h1 : 2 ∣ Z.n₁) : 2 ∣ Z.a := by  have h4 : (4 : ℤ) ∣ Z.n₁ ^ 2 * Z.Δ₁ := by    obtain ⟨k, hk⟩ := h1; exact ⟨k ^ 2 * Z.Δ₁, by rw [hk]; ring⟩  have h4a : (4 : ℤ) ∣ Z.a ^ 2 := by    have := dvd_add Z.asq₁ h4    rwa [sub_add_cancel] at this  exact Int.prime_two.dvd_of_dvd_pow (dvd_trans ⟨2, by norm_num⟩ h4a) /-- Boolean parity core of the all-even case (2⁷ cases; A = a₀, M_j = m_j, E_j = e_j even). -/theorem parity_core : ∀ A M1 M2 M3 E1 E2 E3 : Bool,    (((M1 || M2) || M3) || E3) = (((A || M1) == false) || ((A || M2) == false)) →    (((M1 || M3) || M2) || E2) = (((A || M1) == false) || ((A || M3) == false)) →    (((M2 || M3) || M1) || E1) = (((A || M2) == false) || ((A || M3) == false)) →    (A == (((M1 || E1) == (M2 || E2)) == (M3 || E3))) = true →    (M1 = false → M2 = false → M3 = false → A = false → E1 = true ∧ E2 = true ∧ E3 = true) →    A = false ∧      ((M1 = false ∧ M2 = false ∧ M3 = true ∧ (E1 == E2) = false) ∨       (M1 = false ∧ M2 = true ∧ M3 = false ∧ (E1 == E3) = false) ∨       (M1 = true ∧ M2 = false ∧ M3 = false ∧ (E2 == E3) = false)) := by  intro A M1 M2 M3 E1 E2 E3  cases A <;> cases M1 <;> cases M2 <;> cases M3 <;> cases E1 <;> cases E2 <;> cases E3 <;> simp /-- The all-even case (Lemma `lem:alleven`, its first two steps, and the proof of prop:discA):    a = 2a₀ with a₀ odd, n_j = 2m_j, exactly one m_j even, and e_i + e_j odd for the other two. -/theorem even_case (h1 : 2 ∣ Z.n₁) (h2 : 2 ∣ Z.n₂) (h3 : 2 ∣ Z.n₃) :    ∃ a0 m₁ m₂ m₃ : ℤ, Z.a = 2 * a0 ∧ Z.n₁ = 2 * m₁ ∧ Z.n₂ = 2 * m₂ ∧ Z.n₃ = 2 * m₃ ∧      ¬ Even a0 ∧      ((¬ Even m₁ ∧ ¬ Even m₂ ∧ Even m₃ ∧ ¬ Even (Z.e₁ + Z.e₂)) ∨       (¬ Even m₁ ∧ Even m₂ ∧ ¬ Even m₃ ∧ ¬ Even (Z.e₁ + Z.e₃)) ∨       (Even m₁ ∧ ¬ Even m₂ ∧ ¬ Even m₃ ∧ ¬ Even (Z.e₂ + Z.e₃))) := by  obtain ⟨a0, ha⟩ := Z.a_even h1  have ot₁ := odd_of_cop_even Z.cop₁ h1  have ot₂ := odd_of_cop_even Z.cop₂ h2  have ot₃ := odd_of_cop_even Z.cop₃ h3  have hF := Z.F1 h1 h2 h3  obtain ⟨m₁, hm₁⟩ := h1  obtain ⟨m₂, hm₂⟩ := h2  obtain ⟨m₃, hm₃⟩ := h3  refine ⟨a0, m₁, m₂, m₃, ha, hm₁, hm₂, hm₃, ?_⟩  have Q₃ : m₁ * m₂ * m₃ ^ 2 * Z.Δ₃ = (a0 * m₁ - Z.t₁) * (a0 * m₂ - Z.t₂) := by    have := Z.P₃    rw [ha, hm₁, hm₂, hm₃] at this    apply mul_left_cancel₀ (show (16 : ℤ) ≠ 0 by norm_num)    linear_combination this  have Q₂ : m₁ * m₃ * m₂ ^ 2 * Z.Δ₂ = (a0 * m₁ - Z.t₁) * (a0 * m₃ - Z.t₃) := by    have := Z.P₂    rw [ha, hm₁, hm₂, hm₃] at this    apply mul_left_cancel₀ (show (16 : ℤ) ≠ 0 by norm_num)    linear_combination this  have Q₁ : m₂ * m₃ * m₁ ^ 2 * Z.Δ₁ = (a0 * m₂ - Z.t₂) * (a0 * m₃ - Z.t₃) := by    have := Z.P₁    rw [ha, hm₁, hm₂, hm₃] at this    apply mul_left_cancel₀ (show (16 : ℤ) ≠ 0 by norm_num)    linear_combination this  have hF2 : Even (a0 - (m₁ * Z.e₁ + m₂ * Z.e₂ + m₃ * Z.e₃)) := by    rw [ha, hm₁, hm₂, hm₃] at hF    obtain ⟨k, hk⟩ := hF    refine ⟨k, ?_⟩    have : (2 : ℤ) * (a0 - (m₁ * Z.e₁ + m₂ * Z.e₂ + m₃ * Z.e₃)) = 2 * (k + k) := by      linear_combination hk    exact mul_left_cancel₀ two_ne_zero this  -- the all-odd branch with a₀ odd: c'_j even ⇒ 4 ∣ Δ_j ⇒ e_j even  have h4 : ¬ Even m₁ → ¬ Even m₂ → ¬ Even m₃ → ¬ Even a0 →      Even Z.e₁ ∧ Even Z.e₂ ∧ Even Z.e₃ := by    intro o₁ o₂ o₃ ha0    have c4 : ∀ {m t m' t' : ℤ}, ¬ Even m → ¬ Even t → ¬ Even m' → ¬ Even t' →        (4 : ℤ) ∣ (a0 * m - t) * (a0 * m' - t') := by      intro m t m' t' hm ht hm' ht'      rw [Int.not_even_iff_odd] at hm ht hm' ht' ha0      have e1 : Even (a0 * m - t) := (ha0.mul hm).sub_odd ht      have e2 : Even (a0 * m' - t') := (ha0.mul hm').sub_odd ht'      obtain ⟨k, hk⟩ := e1      obtain ⟨k', hk'⟩ := e2      exact ⟨k * k', by rw [hk, hk']; ring⟩    have q₃ : (4 : ℤ) ∣ Z.Δ₃ := by      have h := c4 o₁ ot₁ o₂ ot₂      rw [← Q₃] at h      refine (cop4_of_odd (u := m₁ * m₂ * m₃ ^ 2) ?_).dvd_of_dvd_mul_left h      simp only [Int.even_mul, Int.even_pow]; tauto    have q₂ : (4 : ℤ) ∣ Z.Δ₂ := by      have h := c4 o₁ ot₁ o₃ ot₃      rw [← Q₂] at h      refine (cop4_of_odd (u := m₁ * m₃ * m₂ ^ 2) ?_).dvd_of_dvd_mul_left h      simp only [Int.even_mul, Int.even_pow]; tauto    have q₁ : (4 : ℤ) ∣ Z.Δ₁ := by      have h := c4 o₂ ot₂ o₃ ot₃      rw [← Q₁] at h      refine (cop4_of_odd (u := m₂ * m₃ * m₁ ^ 2) ?_).dvd_of_dvd_mul_left h      simp only [Int.even_mul, Int.even_pow]; tauto    exact ⟨even_of_dvd4 Z.sq₁ q₁, even_of_dvd4 Z.sq₂ q₂, even_of_dvd4 Z.sq₃ q₃⟩  -- translate to Booleans and apply `parity_core`  have b₃ : ev (m₁ * m₂ * m₃ ^ 2 * Z.Δ₃) = ev ((a0 * m₁ - Z.t₁) * (a0 * m₂ - Z.t₂)) := by    rw [Q₃]  have b₂ : ev (m₁ * m₃ * m₂ ^ 2 * Z.Δ₂) = ev ((a0 * m₁ - Z.t₁) * (a0 * m₃ - Z.t₃)) := by    rw [Q₂]  have b₁ : ev (m₂ * m₃ * m₁ ^ 2 * Z.Δ₁) = ev ((a0 * m₂ - Z.t₂) * (a0 * m₃ - Z.t₃)) := by    rw [Q₁]  have bF : ev (a0 - (m₁ * Z.e₁ + m₂ * Z.e₂ + m₃ * Z.e₃)) = true := ev_true.mpr hF2  simp only [ev_mul, ev_sub, ev_add, ev_sq, ev_false.mpr ot₁, ev_false.mpr ot₂,    ev_false.mpr ot₃] at b₁ b₂ b₃ bF  rw [ev_iff (even_iff_of_dvd4 Z.sq₁)] at b₁  rw [ev_iff (even_iff_of_dvd4 Z.sq₂)] at b₂  rw [ev_iff (even_iff_of_dvd4 Z.sq₃)] at b₃  have key := parity_core (ev a0) (ev m₁) (ev m₂) (ev m₃)    (ev Z.e₁) (ev Z.e₂) (ev Z.e₃) b₃ b₂ b₁ bF    (fun o₁ o₂ o₃ oa => by      obtain ⟨f₁, f₂, f₃⟩ := h4 (ev_false.mp o₁) (ev_false.mp o₂) (ev_false.mp o₃) (ev_false.mp oa)      exact ⟨ev_true.mpr f₁, ev_true.mpr f₂, ev_true.mpr f₃⟩)  simp only [← ev_add, ev_false, ev_true] at key  exact key /-- If 4 divides all n_j: contradiction (two of the m_j = n_j/2 are odd). -/theorem not_four_dvd_all (h1 : 4 ∣ Z.n₁) (h2 : 4 ∣ Z.n₂) (h3 : 4 ∣ Z.n₃) : False := by  have t1 : (2 : ℤ) ∣ Z.n₁ := dvd_trans ⟨2, by norm_num⟩ h1  have t2 : (2 : ℤ) ∣ Z.n₂ := dvd_trans ⟨2, by norm_num⟩ h2  have t3 : (2 : ℤ) ∣ Z.n₃ := dvd_trans ⟨2, by norm_num⟩ h3  obtain ⟨a0, m₁, m₂, m₃, -, hm₁, hm₂, hm₃, -, hc⟩ := Z.even_case t1 t2 t3  have e : ∀ {n m : ℤ}, 4 ∣ n → n = 2 * m → Even m := by    intro n m h hm    obtain ⟨k, hk⟩ := h    exact ⟨k, by linarith⟩  have := e h1 hm₁  have := e h2 hm₂  have := e h3 hm₃  tauto theorem h0_ne_zero : Z.h0 ≠ 0 := by  intro h  have h' : ((Z.h0 : ℕ) : ℤ) = 0 := by rw [h]; rfl  apply Z.nz₁  constructor  · exact zero_dvd_iff.mp (h' ▸ Z.h0_dvd₂)  · exact zero_dvd_iff.mp (h' ▸ Z.h0_dvd₃) /-- prop:smith: h₀ ∈ {1, 2}. -/theorem h0_one_or_two : Z.h0 = 1 ∨ Z.h0 = 2 := by  obtain ⟨k, q, hq, hkq⟩ := Nat.exists_eq_two_pow_mul_odd Z.h0_ne_zero  have hq1 : q = 1 := by    by_contra hne    obtain ⟨p, hp, hpq⟩ := Nat.exists_prime_and_dvd hne    have hp2 : p ≠ 2 := by      rintro rfl      exact (Nat.not_even_iff_odd.mpr hq) (even_iff_two_dvd.mpr hpq)    have hph : (p : ℤ) ∣ (Z.h0 : ℤ) := by      rw [hkq]; exact_mod_cast dvd_mul_of_dvd_right hpq _    have hpz : Prime (p : ℤ) := Nat.prime_iff_prime_int.mp hp    apply Z.no_odd_prime hpz    · intro h2      have : p ∣ 2 := by exact_mod_cast h2      exact hp2 ((Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp this)    · exact ⟨hph.trans Z.h0_dvd₁, hph.trans Z.h0_dvd₂, hph.trans Z.h0_dvd₃⟩  subst hq1  rcases Nat.lt_or_ge k 2 with hk | hk  · interval_cases k <;> simp_all  · exfalso    have h4 : (4 : ℤ) ∣ (Z.h0 : ℤ) := by      rw [hkq, mul_one]      have : (4 : ℕ) ∣ 2 ^ k := by        have := Nat.pow_dvd_pow 2 hk        simpa using this      exact_mod_cast this    exact Z.not_four_dvd_all (h4.trans Z.h0_dvd₁) (h4.trans Z.h0_dvd₂) (h4.trans Z.h0_dvd₃) /-- prop:smith: h₀ ∣ m₀. -/theorem h0_dvd_m0 : Z.h0 ∣ Z.m0 := by  have h := Z.dvd_m0 (k := (Z.h0 : ℤ))    (dvd_sub (dvd_mul_of_dvd_left Z.h0_dvd₁ _) (dvd_mul_of_dvd_left Z.h0_dvd₂ _))    (dvd_sub (dvd_mul_of_dvd_left Z.h0_dvd₁ _) (dvd_mul_of_dvd_left Z.h0_dvd₃ _))    (dvd_sub (dvd_mul_of_dvd_left Z.h0_dvd₂ _) (dvd_mul_of_dvd_left Z.h0_dvd₃ _))  exact_mod_cast h theorem unit_of_cop_zero {t : ℤ} (h : IsCoprime 0 t) : t = 1 ∨ t = -1 :=  Int.isUnit_iff.mp (isCoprime_zero_left.mp h) /-- prop:smith, case U = 0: if some n_j = 0 then m₀ = h₀. -/theorem m0_eq_h0_of_zero (h0N : Z.n₁ * Z.n₂ * Z.n₃ = 0) : Z.m0 = Z.h0 := by  apply Nat.dvd_antisymm _ Z.h0_dvd_m0  have key : (Z.m0 : ℤ) ∣ Z.h0 := by    rcases mul_eq_zero.mp h0N with h12 | h3    · rcases mul_eq_zero.mp h12 with h1 | h2      · -- n₁ = 0: d₁₂ = −n₂t₁, d₁₃ = −n₃t₁, t₁ = ±1        have ht := unit_of_cop_zero (h1 ▸ Z.cop₁)        have e2 : Z.d₁₂ = -(Z.n₂ * Z.t₁) := by simp [d₁₂, h1]        have e3 : Z.d₁₃ = -(Z.n₃ * Z.t₁) := by simp [d₁₃, h1]        have m2 : (Z.m0 : ℤ) ∣ Z.n₂ := by          have := Z.m0_dvd₁₂; rw [e2] at this          rcases ht with h | h <;> rw [h] at this <;> simpa using this        have m3 : (Z.m0 : ℤ) ∣ Z.n₃ := by          have := Z.m0_dvd₁₃; rw [e3] at this          rcases ht with h | h <;> rw [h] at this <;> simpa using this        exact Z.dvd_h0 (by rw [h1]; exact dvd_zero _) m2 m3      · have ht := unit_of_cop_zero (h2 ▸ Z.cop₂)        have e1 : Z.d₁₂ = Z.n₁ * Z.t₂ := by simp [d₁₂, h2]        have e3 : Z.d₂₃ = -(Z.n₃ * Z.t₂) := by simp [d₂₃, h2]        have m1 : (Z.m0 : ℤ) ∣ Z.n₁ := by          have := Z.m0_dvd₁₂; rw [e1] at this          rcases ht with h | h <;> rw [h] at this <;> simpa using this        have m3 : (Z.m0 : ℤ) ∣ Z.n₃ := by          have := Z.m0_dvd₂₃; rw [e3] at this          rcases ht with h | h <;> rw [h] at this <;> simpa using this        exact Z.dvd_h0 m1 (by rw [h2]; exact dvd_zero _) m3    · have ht := unit_of_cop_zero (h3 ▸ Z.cop₃)      have e1 : Z.d₁₃ = Z.n₁ * Z.t₃ := by simp [d₁₃, h3]      have e2 : Z.d₂₃ = Z.n₂ * Z.t₃ := by simp [d₂₃, h3]      have m1 : (Z.m0 : ℤ) ∣ Z.n₁ := by        have := Z.m0_dvd₁₃; rw [e1] at this        rcases ht with h | h <;> rw [h] at this <;> simpa using this      have m2 : (Z.m0 : ℤ) ∣ Z.n₂ := by        have := Z.m0_dvd₂₃; rw [e2] at this        rcases ht with h | h <;> rw [h] at this <;> simpa using this      exact Z.dvd_h0 m1 m2 (by rw [h3]; exact dvd_zero _)  exact_mod_cast key /-- prop:smith: m₀ > 0. -/theorem m0_pos : 0 < Z.m0 := by  rcases eq_or_ne (Z.n₁ * Z.n₂ * Z.n₃) 0 with h | h  · rw [Z.m0_eq_h0_of_zero h]; exact Nat.pos_of_ne_zero Z.h0_ne_zero  · apply Nat.pos_of_ne_zero    intro hm    have hm' : ((Z.m0 : ℕ) : ℤ) = 0 := by rw [hm]; rfl    have z12 : Z.d₁₂ = 0 := zero_dvd_iff.mp (hm' ▸ Z.m0_dvd₁₂)    have hmin := Z.minors    have e : (Z.n₁ * Z.t₂ - Z.n₂ * Z.t₁) = 0 := z12    rw [e] at hmin    simp only [zero_mul, mul_zero] at hmin    exact mul_ne_zero Z.δ0 h hmin.symm theorem h0_one_of_odd (h : ¬ 2 ∣ Z.n₁ ∨ ¬ 2 ∣ Z.n₂ ∨ ¬ 2 ∣ Z.n₃) : Z.h0 = 1 := by  rcases Z.h0_one_or_two with h1 | h2  · exact h1  · exfalso    have h2' : ((Z.h0 : ℕ) : ℤ) = 2 := by rw [h2]; rfl    rcases h with h | h | h    · exact h (h2' ▸ Z.h0_dvd₁)    · exact h (h2' ▸ Z.h0_dvd₂)    · exact h (h2' ▸ Z.h0_dvd₃) theorem h0_two_of_even (h1 : 2 ∣ Z.n₁) (h2 : 2 ∣ Z.n₂) (h3 : 2 ∣ Z.n₃) : Z.h0 = 2 := by  rcases Z.h0_one_or_two with h | h  · exfalso    have := Z.dvd_h0 h1 h2 h3    rw [h] at this    norm_num at this  · exact h /-- p ∣ n, p ∤ n', p ∣ n t' − n' t with gcd(n, t) = 1: impossible. -/theorem pair_contra {p n t n' t' : ℤ} (hp : Prime p) (hc : IsCoprime n t) (hn : p ∣ n)    (hn' : ¬ p ∣ n') (hd : p ∣ n * t' - n' * t) : False := by  have h1 : p ∣ n' * t := by    have e : n' * t = n * t' - (n * t' - n' * t) := by ring    rw [e]; exact dvd_sub (dvd_mul_of_dvd_left hn _) hd  rcases hp.dvd_or_dvd h1 with h | h  · exact hn' h  · exact not_dvd_t hp hc hn h /-- gcd(m₀/h₀, n₁n₂n₃) = 1 (proof of prop:smith, the bullet items). -/theorem coprime_e_N :    IsCoprime ((Z.m0 / Z.h0 : ℕ) : ℤ) (Z.n₁ * Z.n₂ * Z.n₃) := by  rw [Int.isCoprime_iff_gcd_eq_one, Int.gcd_eq_natAbs, Int.natAbs_natCast]  apply Nat.coprime_of_dvd  intro p hp hpe hpN  have hpz : Prime (p : ℤ) := Nat.prime_iff_prime_int.mp hp  have hpN' : (p : ℤ) ∣ Z.n₁ * Z.n₂ * Z.n₃ := Int.natCast_dvd.mpr hpN  have hpm : (p : ℤ) ∣ (Z.m0 : ℤ) := by    have : p ∣ Z.m0 := hpe.trans (Nat.div_dvd_of_dvd Z.h0_dvd_m0)    exact_mod_cast this  have q12 := hpm.trans Z.m0_dvd₁₂  have q13 := hpm.trans Z.m0_dvd₁₃  have q23 := hpm.trans Z.m0_dvd₂₃  have q21 : (p : ℤ) ∣ Z.n₂ * Z.t₁ - Z.n₁ * Z.t₂ := by    have := dvd_neg.mpr q12; unfold d₁₂ at this; rwa [neg_sub] at this  have q31 : (p : ℤ) ∣ Z.n₃ * Z.t₁ - Z.n₁ * Z.t₃ := by    have := dvd_neg.mpr q13; unfold d₁₃ at this; rwa [neg_sub] at this  unfold d₁₂ at q12  unfold d₁₃ at q13  unfold d₂₃ at q23  by_cases a1 : (p : ℤ) ∣ Z.n₁ <;> by_cases a2 : (p : ℤ) ∣ Z.n₂ <;> by_cases a3 : (p : ℤ) ∣ Z.n₃  · -- p divides all n_j: p = 2, h₀ = 2, and 4 ∣ m₀ is impossible    have hp2 : (p : ℤ) ∣ 2 := by      by_contra hp2; exact Z.no_odd_prime hpz hp2 ⟨a1, a2, a3⟩    have hp2' : p = 2 := by      have : p ∣ 2 := by exact_mod_cast hp2      exact (Nat.prime_dvd_prime_iff_eq hp Nat.prime_two).mp this    subst hp2'    have e1 : (2 : ℤ) ∣ Z.n₁ := by exact_mod_cast a1    have e2 : (2 : ℤ) ∣ Z.n₂ := by exact_mod_cast a2    have e3 : (2 : ℤ) ∣ Z.n₃ := by exact_mod_cast a3    have hh := Z.h0_two_of_even e1 e2 e3    rw [hh] at hpe    have h4 : (4 : ℤ) ∣ (Z.m0 : ℤ) := by      obtain ⟨k, hk⟩ := hpe      have hm : Z.m0 = 2 * (2 * k) := by        have := Nat.div_mul_cancel (hh ▸ Z.h0_dvd_m0 : 2 ∣ Z.m0)        rw [hk] at this; linarith      exact ⟨k, by rw [hm]; push_cast; ring⟩    obtain ⟨a0, m₁, m₂, m₃, -, hm₁, hm₂, hm₃, -, hc⟩ := Z.even_case e1 e2 e3    have ot₁ := odd_of_cop_even Z.cop₁ e1    have ot₂ := odd_of_cop_even Z.cop₂ e2    have ot₃ := odd_of_cop_even Z.cop₃ e3    have bad : ∀ {m t m' t' : ℤ}, ¬ Even m → ¬ Even t' → Even m' →        ¬ (4 : ℤ) ∣ 2 * m * t' - 2 * m' * t := by      intro m t m' t' hm ht' hm' h4'      obtain ⟨k, hk⟩ := h4'      have : Even (m * t' - m' * t) := ⟨k, by linarith⟩      rw [Int.even_sub, Int.even_mul, Int.even_mul] at this      tauto    rcases hc with ⟨o1, o2, ev3, -⟩ | ⟨o1, ev2, o3, -⟩ | ⟨ev1, o2, o3, -⟩    · exact bad o1 ot₃ ev3 (by rw [← hm₁, ← hm₃]; exact h4.trans Z.m0_dvd₁₃)    · exact bad o1 ot₂ ev2 (by rw [← hm₁, ← hm₂]; exact h4.trans Z.m0_dvd₁₂)    · have m21 : (Z.m0 : ℤ) ∣ Z.n₂ * Z.t₁ - Z.n₁ * Z.t₂ := by        have := dvd_neg.mpr Z.m0_dvd₁₂; unfold d₁₂ at this; rwa [neg_sub] at this      exact bad o2 ot₁ ev1 (by rw [← hm₂, ← hm₁]; exact h4.trans m21)  · exact pair_contra hpz Z.cop₁ a1 a3 q13  · exact pair_contra hpz Z.cop₁ a1 a2 q12  · exact pair_contra hpz Z.cop₁ a1 a2 q12  · exact pair_contra hpz Z.cop₂ a2 a1 q21  · exact pair_contra hpz Z.cop₂ a2 a1 q21  · exact pair_contra hpz Z.cop₃ a3 a1 q31  · -- p divides no n_j but divides the product    rcases hpz.dvd_or_dvd hpN' with h12 | h3    · rcases hpz.dvd_or_dvd h12 with h1 | h2      · exact a1 h1      · exact a2 h2    · exact a3 h3 end ZD /-! ## Part 2. The V4 model: the note's pairs (n_j, t_j) and the elements ϑ_j -/ open Louboutin.Conj61 Louboutin.Conj61.V4Data /-- The note's n_j from (D_j, E_j): D/gcd(D, E) if D ≠ 0, else 0 (prop:smith). -/def ntN (D E : ℤ) : ℤ := if D = 0 then 0 else D / (Int.gcd D E : ℤ)/-- The note's t_j from (D_j, E_j): E/gcd(D, E) if D ≠ 0, else 1 (prop:smith). -/def ntT (D E : ℤ) : ℤ := if D = 0 then 1 else E / (Int.gcd D E : ℤ) section model variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] /-- With r² = D and rq = E (integers), the note's coprime pair (n, t) and some ϑ ∈ O satisfy    r = nϑ, q = tϑ (proof of prop:smith, first paragraph; cf. `split_pair` in `Conj61Model`). -/theorem split_note {r q : O} {D E : ℤ} (hD : r * r = D) (hE : r * q = E) :    IsCoprime (ntN D E) (ntT D E) ∧ ∃ ϑ : O, r = ntN D E * ϑ ∧ q = ntT D E * ϑ := by  by_cases h0 : D = 0  · have hr : r = 0 := by      have : r * r = 0 := by rw [hD, h0, Int.cast_zero]      exact mul_self_eq_zero.mp this    simp only [ntN, ntT, h0, ite_true]    exact ⟨isCoprime_zero_left.mpr isUnit_one, q, by simp [hr], by simp⟩  · simp only [ntN, ntT, h0, ite_false]    have hr : r ≠ 0 := by      rintro rfl      apply h0      have : (D : O) = 0 := by rw [← hD]; ring      exact_mod_cast this    have hcop : IsCoprime (D / (Int.gcd D E : ℤ)) (E / (Int.gcd D E : ℤ)) :=      Int.isCoprime_iff_gcd_eq_one.mpr (Int.gcd_ediv_gcd_ediv_gcd_of_ne_zero_left h0)    have eA : D / (Int.gcd D E : ℤ) * (Int.gcd D E : ℤ) = D :=      Int.ediv_mul_cancel (Int.gcd_dvd_left ..)    have eB : E / (Int.gcd D E : ℤ) * (Int.gcd D E : ℤ) = E :=      Int.ediv_mul_cancel (Int.gcd_dvd_right ..)    set h : ℤ := (Int.gcd D E : ℤ)    set a' := D / h    set b' := E / h    have e1 : (b' : O) * (D : O) = (a' : O) * (E : O) := by      have : b' * D = a' * E := by        calc b' * D = b' * (a' * h) := by rw [eA]          _ = a' * (b' * h) := by ring          _ = a' * E := by rw [eB]      exact_mod_cast this    have key : (b' : O) * r = (a' : O) * q := by      apply mul_left_cancel₀ hr      linear_combination (b' : O) * hD - (a' : O) * hE + e1    refine ⟨hcop, ?_⟩    obtain ⟨u, v, huv⟩ := hcop    have huv' : (u : O) * a' + v * b' = 1 := by exact_mod_cast huv    refine ⟨u * r + v * q, ?_, ?_⟩    · linear_combination (-r) * huv' + (v : O) * key    · linear_combination (-q) * huv' - (u : O) * key /-- A coprime splitting inherits the sign of the pair: τ r = e r, τ q = e q ⇒ τ ϑ = e ϑ. -/theorem theta_sign (σ : O →+* O) {r q ϑ e : O} {n t u v : ℤ} (hb : u * n + v * t = 1)    (hr : r = n * ϑ) (hq : q = t * ϑ) (hσr : σ r = e * r) (hσq : σ q = e * q) :    σ ϑ = e * ϑ := by  have hϑ : ϑ = u * r + v * q := by    have hb' : ((u * n + v * t : ℤ) : O) = 1 := by rw [hb]; simp    push_cast at hb'    rw [hr, hq]; linear_combination (-ϑ) * hb'  rw [hϑ]; exact lin_sign σ u v hσr hσq variable (M : V4Data O) /-- The data of the note's proof of prop:discA / prop:smith in the model: the integers    a, b, c, d, z_j, Δ = δ, the coprime pairs (n_j, t_j) with Bézout coefficients (u_j, v_j),    the elements ϑ_j with r_j = n_j ϑ_j, q_j = t_j ϑ_j, Δ_j = ϑ_j², Θ = ϑ₁ϑ₂ϑ₃. -/structure Dat where  a : ℤ  b : ℤ  c : ℤ  d : ℤ  ha : ea M.x = a  hb : eb M.x = b  hc : ec M.x = c  hd : ed M.x = d  z₁ : ℤ  z₂ : ℤ  z₃ : ℤ  hz₁ : z1 M.x = z₁  hz₂ : z2 M.x = z₂  hz₃ : z3 M.x = z₃  δ : ℤ  hδ : Vand M.x = δ  n₁ : ℤ  n₂ : ℤ  n₃ : ℤ  t₁ : ℤ  t₂ : ℤ  t₃ : ℤ  u₁ : ℤ  u₂ : ℤ  u₃ : ℤ  v₁ : ℤ  v₂ : ℤ  v₃ : ℤ  bez₁ : u₁ * n₁ + v₁ * t₁ = 1  bez₂ : u₂ * n₂ + v₂ * t₂ = 1  bez₃ : u₃ * n₃ + v₃ * t₃ = 1  ϑ₁ : O  ϑ₂ : O  ϑ₃ : O  hr₁ : r1 M.x = n₁ * ϑ₁  hr₂ : r2 M.x = n₂ * ϑ₂  hr₃ : r3 M.x = n₃ * ϑ₃  hq₁ : dQ1 M.x = t₁ * ϑ₁  hq₂ : dQ2 M.x = t₂ * ϑ₂  hq₃ : dQ3 M.x = t₃ * ϑ₃  Δ₁ : ℤ  Δ₂ : ℤ  Δ₃ : ℤ  hΔ₁ : ϑ₁ * ϑ₁ = Δ₁  hΔ₂ : ϑ₂ * ϑ₂ = Δ₂  hΔ₃ : ϑ₃ * ϑ₃ = Δ₃  Θ : ℤ  hΘ : ϑ₁ * ϑ₂ * ϑ₃ = Θ /-- Existence of the data with the note's (n_j, t_j) = ntN/ntT (D_j, E_j),    D_j = a² − 4b + 4z_j, E_j = a z_j − 2c. -/theorem Dat.exists_note {a b c d z₁ z₂ z₃ δ : ℤ} (ha : ea M.x = a) (hb : eb M.x = b)    (hc : ec M.x = c) (hd : ed M.x = d) (hz₁ : z1 M.x = z₁) (hz₂ : z2 M.x = z₂)    (hz₃ : z3 M.x = z₃) (hδ : Vand M.x = δ) :    ∃ P : Dat M, P.a = a ∧ P.b = b ∧ P.c = c ∧ P.d = d ∧ P.z₁ = z₁ ∧ P.z₂ = z₂ ∧      P.z₃ = z₃ ∧ P.δ = δ ∧      P.n₁ = ntN (a ^ 2 - 4 * b + 4 * z₁) (a * z₁ - 2 * c) ∧      P.t₁ = ntT (a ^ 2 - 4 * b + 4 * z₁) (a * z₁ - 2 * c) ∧      P.n₂ = ntN (a ^ 2 - 4 * b + 4 * z₂) (a * z₂ - 2 * c) ∧      P.t₂ = ntT (a ^ 2 - 4 * b + 4 * z₂) (a * z₂ - 2 * c) ∧      P.n₃ = ntN (a ^ 2 - 4 * b + 4 * z₃) (a * z₃ - 2 * c) ∧      P.t₃ = ntT (a ^ 2 - 4 * b + 4 * z₃) (a * z₃ - 2 * c) := by  obtain ⟨rs₁, rs₂, rs₃⟩ := rsq M.x  obtain ⟨rq₁, rq₂, rq₃⟩ := rq_eq M.x  have D₁ : r1 M.x * r1 M.x = ((a ^ 2 - 4 * b + 4 * z₁ : ℤ) : O) := by    push_cast; rw [← ha, ← hb, ← hz₁, ← rs₁]; ring  have D₂ : r2 M.x * r2 M.x = ((a ^ 2 - 4 * b + 4 * z₂ : ℤ) : O) := by    push_cast; rw [← ha, ← hb, ← hz₂, ← rs₂]; ring  have D₃ : r3 M.x * r3 M.x = ((a ^ 2 - 4 * b + 4 * z₃ : ℤ) : O) := by    push_cast; rw [← ha, ← hb, ← hz₃, ← rs₃]; ring  have E₁ : r1 M.x * dQ1 M.x = ((a * z₁ - 2 * c : ℤ) : O) := by    push_cast; rw [← ha, ← hc, ← hz₁, rq₁]  have E₂ : r2 M.x * dQ2 M.x = ((a * z₂ - 2 * c : ℤ) : O) := by    push_cast; rw [← ha, ← hc, ← hz₂, rq₂]  have E₃ : r3 M.x * dQ3 M.x = ((a * z₃ - 2 * c : ℤ) : O) := by    push_cast; rw [← ha, ← hc, ← hz₃, rq₃]  obtain ⟨⟨u₁, v₁, bz₁⟩, ϑ₁, hr₁, hq₁⟩ := split_note D₁ E₁  obtain ⟨⟨u₂, v₂, bz₂⟩, ϑ₂, hr₂, hq₂⟩ := split_note D₂ E₂  obtain ⟨⟨u₃, v₃, bz₃⟩, ϑ₃, hr₃, hq₃⟩ := split_note D₃ E₃  obtain ⟨a1, b1, a2, b2, a3, b3⟩ := M.sgn₁  obtain ⟨c1, d1, c2, d2, c3, d3⟩ := M.sgn₂  obtain ⟨f1, g1, f2, g2, f3, g3⟩ := M.sgn₃  have s11 := theta_sign M.τ₁ bz₁ hr₁ hq₁ a1 b1  have s12 := theta_sign M.τ₁ bz₂ hr₂ hq₂ a2 b2  have s13 := theta_sign M.τ₁ bz₃ hr₃ hq₃ a3 b3  have s21 := theta_sign M.τ₂ bz₁ hr₁ hq₁ c1 d1  have s22 := theta_sign M.τ₂ bz₂ hr₂ hq₂ c2 d2  have s23 := theta_sign M.τ₂ bz₃ hr₃ hq₃ c3 d3  have s31 := theta_sign M.τ₃ bz₁ hr₁ hq₁ f1 g1  have s32 := theta_sign M.τ₃ bz₂ hr₂ hq₂ f2 g2  have s33 := theta_sign M.τ₃ bz₃ hr₃ hq₃ f3 g3  obtain ⟨Δ₁, hΔ₁⟩ := M.fix (ϑ₁ * ϑ₁) (by rw [map_mul, s11]; ring) (by rw [map_mul, s21]; ring)    (by rw [map_mul, s31]; ring)  obtain ⟨Δ₂, hΔ₂⟩ := M.fix (ϑ₂ * ϑ₂) (by rw [map_mul, s12]; ring) (by rw [map_mul, s22]; ring)    (by rw [map_mul, s32]; ring)  obtain ⟨Δ₃, hΔ₃⟩ := M.fix (ϑ₃ * ϑ₃) (by rw [map_mul, s13]; ring) (by rw [map_mul, s23]; ring)    (by rw [map_mul, s33]; ring)  obtain ⟨Θ, hΘ⟩ := M.fix (ϑ₁ * ϑ₂ * ϑ₃) (by rw [map_mul, map_mul, s11, s12, s13]; ring)    (by rw [map_mul, map_mul, s21, s22, s23]; ring) (by rw [map_mul, map_mul, s31, s32, s33]; ring)  exact ⟨⟨a, b, c, d, ha, hb, hc, hd, z₁, z₂, z₃, hz₁, hz₂, hz₃, δ, hδ, _, _, _, _, _, _,    u₁, u₂, u₃, v₁, v₂, v₃, bz₁, bz₂, bz₃, ϑ₁, ϑ₂, ϑ₃, hr₁, hr₂, hr₃, hq₁, hq₂, hq₃,    Δ₁, Δ₂, Δ₃, hΔ₁, hΔ₂, hΔ₃, Θ, hΘ⟩,    rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ end model /-! ## Part 3. Identities in the model -/ namespace Dat variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- e_j = u_j a + v_j z_j (so that ϑ_j = 2η_j − e_j). -/def e₁ : ℤ := P.u₁ * P.a + P.v₁ * P.z₁def e₂ : ℤ := P.u₂ * P.a + P.v₂ * P.z₂def e₃ : ℤ := P.u₃ * P.a + P.v₃ * P.z₃ /-- η_j = u_j S_j + v_j Q_j with S_j = α + τ_jα, Q_j = α·τ_jα: the generator of    C_j = ℤ + ℤS_j + ℤQ_j (Lemma `lem:Lambda`), with ϑ_j = 2η_j − e_j. -/def η₁ : O := (P.u₁ : O) * (M.x 0 + M.x 1) + (P.v₁ : O) * (M.x 0 * M.x 1)def η₂ : O := (P.u₂ : O) * (M.x 0 + M.x 2) + (P.v₂ : O) * (M.x 0 * M.x 2)def η₃ : O := (P.u₃ : O) * (M.x 0 + M.x 3) + (P.v₃ : O) * (M.x 0 * M.x 3) theorem ϑ_eq₁ : P.ϑ₁ = P.u₁ * r1 M.x + P.v₁ * dQ1 M.x := by  have hb' : ((P.u₁ * P.n₁ + P.v₁ * P.t₁ : ℤ) : O) = 1 := by rw [P.bez₁]; simp  push_cast at hb'  rw [P.hr₁, P.hq₁]; linear_combination (-P.ϑ₁) * hb'theorem ϑ_eq₂ : P.ϑ₂ = P.u₂ * r2 M.x + P.v₂ * dQ2 M.x := by  have hb' : ((P.u₂ * P.n₂ + P.v₂ * P.t₂ : ℤ) : O) = 1 := by rw [P.bez₂]; simp  push_cast at hb'  rw [P.hr₂, P.hq₂]; linear_combination (-P.ϑ₂) * hb'theorem ϑ_eq₃ : P.ϑ₃ = P.u₃ * r3 M.x + P.v₃ * dQ3 M.x := by  have hb' : ((P.u₃ * P.n₃ + P.v₃ * P.t₃ : ℤ) : O) = 1 := by rw [P.bez₃]; simp  push_cast at hb'  rw [P.hr₃, P.hq₃]; linear_combination (-P.ϑ₃) * hb' theorem two_η₁ : 2 * P.η₁ - (P.e₁ : O) = P.ϑ₁ := by  rw [P.ϑ_eq₁]; simp only [η₁, e₁]; push_cast; rw [← P.ha, ← P.hz₁]  simp only [r1, dQ1, ea, z1]; ringtheorem two_η₂ : 2 * P.η₂ - (P.e₂ : O) = P.ϑ₂ := by  rw [P.ϑ_eq₂]; simp only [η₂, e₂]; push_cast; rw [← P.ha, ← P.hz₂]  simp only [r2, dQ2, ea, z2]; ringtheorem two_η₃ : 2 * P.η₃ - (P.e₃ : O) = P.ϑ₃ := by  rw [P.ϑ_eq₃]; simp only [η₃, e₃]; push_cast; rw [← P.ha, ← P.hz₃]  simp only [r3, dQ3, ea, z3]; ring /-- 4α = a + n₁ϑ₁ + n₂ϑ₂ + n₃ϑ₃, and its conjugates. -/theorem four_x0 : 4 * M.x 0 = P.a + P.n₁ * P.ϑ₁ + P.n₂ * P.ϑ₂ + P.n₃ * P.ϑ₃ := by  rw [← P.hr₁, ← P.hr₂, ← P.hr₃, ← P.ha]; simp only [r1, r2, r3, ea]; ringtheorem four_x1 : 4 * M.x 1 = P.a + P.n₁ * P.ϑ₁ - P.n₂ * P.ϑ₂ - P.n₃ * P.ϑ₃ := by  rw [← P.hr₁, ← P.hr₂, ← P.hr₃, ← P.ha]; simp only [r1, r2, r3, ea]; ringtheorem four_x2 : 4 * M.x 2 = P.a - P.n₁ * P.ϑ₁ + P.n₂ * P.ϑ₂ - P.n₃ * P.ϑ₃ := by  rw [← P.hr₁, ← P.hr₂, ← P.hr₃, ← P.ha]; simp only [r1, r2, r3, ea]; ringtheorem four_x3 : 4 * M.x 3 = P.a - P.n₁ * P.ϑ₁ - P.n₂ * P.ϑ₂ + P.n₃ * P.ϑ₃ := by  rw [← P.hr₁, ← P.hr₂, ← P.hr₃, ← P.ha]; simp only [r1, r2, r3, ea]; ring /-- r_k r_l = a r_j − 4q_j, i.e. n_k n_l ϑ_k ϑ_l = (a n_j − 4 t_j) ϑ_j (proof of `lem:Lodd`). -/theorem prod₁ : (P.n₂ * P.n₃ : O) * (P.ϑ₂ * P.ϑ₃) = (P.a * P.n₁ - 4 * P.t₁ : O) * P.ϑ₁ := by  have h : r2 M.x * r3 M.x = ea M.x * r1 M.x - 4 * dQ1 M.x := by    simp only [r1, r2, r3, dQ1, ea]; ring  rw [P.hr₁, P.hr₂, P.hr₃, P.hq₁, P.ha] at h  linear_combination htheorem prod₂ : (P.n₁ * P.n₃ : O) * (P.ϑ₁ * P.ϑ₃) = (P.a * P.n₂ - 4 * P.t₂ : O) * P.ϑ₂ := by  have h : r1 M.x * r3 M.x = ea M.x * r2 M.x - 4 * dQ2 M.x := by    simp only [r1, r2, r3, dQ2, ea]; ring  rw [P.hr₁, P.hr₂, P.hr₃, P.hq₂, P.ha] at h  linear_combination htheorem prod₃ : (P.n₁ * P.n₂ : O) * (P.ϑ₁ * P.ϑ₂) = (P.a * P.n₃ - 4 * P.t₃ : O) * P.ϑ₃ := by  have h : r1 M.x * r2 M.x = ea M.x * r3 M.x - 4 * dQ3 M.x := by    simp only [r1, r2, r3, dQ3, ea]; ring  rw [P.hr₁, P.hr₂, P.hr₃, P.hq₃, P.ha] at h  linear_combination h /-- ϑ_j ≠ 0: (α − x_k)(α − x_l) = α r_j − q_j = (α n_j − t_j) ϑ_j. -/theorem ϑ₁_ne : P.ϑ₁ ≠ 0 := by  intro h  have e : (M.x 0 - M.x 2) * (M.x 0 - M.x 3) = M.x 0 * r1 M.x - dQ1 M.x := by    simp only [r1, dQ1]; ring  rw [P.hr₁, P.hq₁, h] at e  simp only [mul_zero, sub_zero] at e  rcases mul_eq_zero.mp e with h' | h'  · exact absurd (M.inj (sub_eq_zero.mp h')) (by decide)  · exact absurd (M.inj (sub_eq_zero.mp h')) (by decide)theorem ϑ₂_ne : P.ϑ₂ ≠ 0 := by  intro h  have e : (M.x 0 - M.x 1) * (M.x 0 - M.x 3) = M.x 0 * r2 M.x - dQ2 M.x := by    simp only [r2, dQ2]; ring  rw [P.hr₂, P.hq₂, h] at e  simp only [mul_zero, sub_zero] at e  rcases mul_eq_zero.mp e with h' | h'  · exact absurd (M.inj (sub_eq_zero.mp h')) (by decide)  · exact absurd (M.inj (sub_eq_zero.mp h')) (by decide)theorem ϑ₃_ne : P.ϑ₃ ≠ 0 := by  intro h  have e : (M.x 0 - M.x 1) * (M.x 0 - M.x 2) = M.x 0 * r3 M.x - dQ3 M.x := by    simp only [r3, dQ3]; ring  rw [P.hr₃, P.hq₃, h] at e  simp only [mul_zero, sub_zero] at e  rcases mul_eq_zero.mp e with h' | h'  · exact absurd (M.inj (sub_eq_zero.mp h')) (by decide)  · exact absurd (M.inj (sub_eq_zero.mp h')) (by decide) theorem Θ_ne : P.Θ ≠ 0 := by  intro h  have := P.hΘ  rw [h, Int.cast_zero] at this  exact mul_ne_zero (mul_ne_zero P.ϑ₁_ne P.ϑ₂_ne) P.ϑ₃_ne this /-- integer identities I_j: n_k n_l Θ = (a n_j − 4t_j) Δ_j. -/theorem I₁ : P.n₂ * P.n₃ * P.Θ = (P.a * P.n₁ - 4 * P.t₁) * P.Δ₁ := by  have h : ((P.n₂ * P.n₃ * P.Θ : ℤ) : O) = ((P.a * P.n₁ - 4 * P.t₁) * P.Δ₁ : ℤ) := by    push_cast; rw [← P.hΘ, ← P.hΔ₁]; linear_combination P.ϑ₁ * P.prod₁  exact_mod_cast htheorem I₂ : P.n₁ * P.n₃ * P.Θ = (P.a * P.n₂ - 4 * P.t₂) * P.Δ₂ := by  have h : ((P.n₁ * P.n₃ * P.Θ : ℤ) : O) = ((P.a * P.n₂ - 4 * P.t₂) * P.Δ₂ : ℤ) := by    push_cast; rw [← P.hΘ, ← P.hΔ₂]; linear_combination P.ϑ₂ * P.prod₂  exact_mod_cast htheorem I₃ : P.n₁ * P.n₂ * P.Θ = (P.a * P.n₃ - 4 * P.t₃) * P.Δ₃ := by  have h : ((P.n₁ * P.n₂ * P.Θ : ℤ) : O) = ((P.a * P.n₃ - 4 * P.t₃) * P.Δ₃ : ℤ) := by    push_cast; rw [← P.hΘ, ← P.hΔ₃]; linear_combination P.ϑ₃ * P.prod₃  exact_mod_cast h theorem hΘ2 : P.Θ * P.Θ = P.Δ₁ * P.Δ₂ * P.Δ₃ := by  have h : ((P.Θ * P.Θ : ℤ) : O) = ((P.Δ₁ * P.Δ₂ * P.Δ₃ : ℤ) : O) := by    push_cast; rw [← P.hΘ, ← P.hΔ₁, ← P.hΔ₂, ← P.hΔ₃]; ring  exact_mod_cast h /-- At most one n_j vanishes (proof of prop:smith, Preliminaries). -/theorem nz₁ : ¬(P.n₂ = 0 ∧ P.n₃ = 0) := by  rintro ⟨h2, h3⟩  have e : r2 M.x - r3 M.x = 2 * (M.x 2 - M.x 3) := by simp only [r2, r3]; ring  rw [P.hr₂, P.hr₃, h2, h3] at e  simp only [Int.cast_zero, zero_mul, sub_zero] at e  have : M.x 2 - M.x 3 = 0 := by    rcases mul_eq_zero.mp e.symm with h | h    · exact absurd h two_ne_zero    · exact h  exact absurd (M.inj (sub_eq_zero.mp this)) (by decide)theorem nz₂ : ¬(P.n₁ = 0 ∧ P.n₃ = 0) := by  rintro ⟨h1, h3⟩  have e : r1 M.x - r3 M.x = 2 * (M.x 1 - M.x 3) := by simp only [r1, r3]; ring  rw [P.hr₁, P.hr₃, h1, h3] at e  simp only [Int.cast_zero, zero_mul, sub_zero] at e  have : M.x 1 - M.x 3 = 0 := by    rcases mul_eq_zero.mp e.symm with h | h    · exact absurd h two_ne_zero    · exact h  exact absurd (M.inj (sub_eq_zero.mp this)) (by decide)theorem nz₃ : ¬(P.n₁ = 0 ∧ P.n₂ = 0) := by  rintro ⟨h1, h2⟩  have e : r1 M.x - r2 M.x = 2 * (M.x 1 - M.x 2) := by simp only [r1, r2]; ring  rw [P.hr₁, P.hr₂, h1, h2] at e  simp only [Int.cast_zero, zero_mul, sub_zero] at e  have : M.x 1 - M.x 2 = 0 := by    rcases mul_eq_zero.mp e.symm with h | h    · exact absurd h two_ne_zero    · exact h  exact absurd (M.inj (sub_eq_zero.mp this)) (by decide) /-- Δ_j ≡ e_j² (mod 4): Δ_j − e_j² = 4(η_j² − e_j η_j). -/theorem sq₁ : 4 ∣ P.Δ₁ - P.e₁ ^ 2 := by  apply M.sat (y := P.η₁ * P.η₁ - P.e₁ * P.η₁) (by norm_num)  push_cast; rw [← P.hΔ₁, ← P.two_η₁]; ringtheorem sq₂ : 4 ∣ P.Δ₂ - P.e₂ ^ 2 := by  apply M.sat (y := P.η₂ * P.η₂ - P.e₂ * P.η₂) (by norm_num)  push_cast; rw [← P.hΔ₂, ← P.two_η₂]; ringtheorem sq₃ : 4 ∣ P.Δ₃ - P.e₃ ^ 2 := by  apply M.sat (y := P.η₃ * P.η₃ - P.e₃ * P.η₃) (by norm_num)  push_cast; rw [← P.hΔ₃, ← P.two_η₃]; ring /-- a² − n₁²Δ₁ = 4(b − z₁) (from r₁² = a² − 4b + 4z₁). -/theorem asq₁ : 4 ∣ P.a ^ 2 - P.n₁ ^ 2 * P.Δ₁ := by  refine ⟨P.b - P.z₁, ?_⟩  have h : ((P.a ^ 2 - P.n₁ ^ 2 * P.Δ₁ : ℤ) : O) = ((4 * (P.b - P.z₁) : ℤ) : O) := by    push_cast    rw [← P.ha, ← P.hb, ← P.hz₁, ← P.hΔ₁]    have := (rsq M.x).1    rw [P.hr₁] at this    linear_combination -this  exact_mod_cast h /-- 2α − Σ n_j η_j is an integer s with a − Σ n_j e_j = 2s; if all n_j are even, 4 ∣ a − Σ n_j e_j. -/theorem F1 : 2 ∣ P.n₁ → 2 ∣ P.n₂ → 2 ∣ P.n₃ → 4 ∣ P.a - (P.n₁ * P.e₁ + P.n₂ * P.e₂ + P.n₃ * P.e₃) := by  rintro ⟨m₁, h1⟩ ⟨m₂, h2⟩ ⟨m₃, h3⟩  apply M.sat (y := M.x 0 - (m₁ * P.η₁ + m₂ * P.η₂ + m₃ * P.η₃)) (by norm_num)  have := P.four_x0  rw [← P.two_η₁, ← P.two_η₂, ← P.two_η₃, h1, h2, h3] at this  rw [h1, h2, h3]  push_cast at this ⊢  linear_combination -this /-- Θ·d₁₂d₂₃d₃₁ = Δ·n₁n₂n₃ (from `cross_prod`; proof of `cor:weak`). -/theorem minors : P.Θ * ((P.n₁ * P.t₂ - P.n₂ * P.t₁) * (P.n₂ * P.t₃ - P.n₃ * P.t₂) *    (P.n₃ * P.t₁ - P.n₁ * P.t₃)) = P.δ * (P.n₁ * P.n₂ * P.n₃) := by  apply mul_left_cancel₀ P.Θ_ne  have key := cross_prod M.x  rw [P.hr₁, P.hr₂, P.hr₃, P.hq₁, P.hq₂, P.hq₃, P.hδ] at key  have h : ((P.Θ * (P.Θ * ((P.n₁ * P.t₂ - P.n₂ * P.t₁) * (P.n₂ * P.t₃ - P.n₃ * P.t₂) *      (P.n₃ * P.t₁ - P.n₁ * P.t₃))) : ℤ) : O)      = ((P.Θ * (P.δ * (P.n₁ * P.n₂ * P.n₃)) : ℤ) : O) := by    push_cast; rw [← P.hΘ]; linear_combination key  exact_mod_cast h /-- The integer data of the model. -/def toZD : ZD where  a := P.a  n₁ := P.n₁  n₂ := P.n₂  n₃ := P.n₃  t₁ := P.t₁  t₂ := P.t₂  t₃ := P.t₃  e₁ := P.e₁  e₂ := P.e₂  e₃ := P.e₃  Δ₁ := P.Δ₁  Δ₂ := P.Δ₂  Δ₃ := P.Δ₃  Θ := P.Θ  δ := P.δ  cop₁ := ⟨P.u₁, P.v₁, P.bez₁⟩  cop₂ := ⟨P.u₂, P.v₂, P.bez₂⟩  cop₃ := ⟨P.u₃, P.v₃, P.bez₃⟩  I₁ := P.I₁  I₂ := P.I₂  I₃ := P.I₃  hΘ2 := P.hΘ2  Θ0 := P.Θ_ne  nz₁ := P.nz₁  nz₂ := P.nz₂  nz₃ := P.nz₃  sq₁ := P.sq₁  sq₂ := P.sq₂  sq₃ := P.sq₃  asq₁ := P.asq₁  F1 := P.F1  minors := P.minors  δ0 := by    intro h; apply M.Vand_ne_zero; rw [P.hδ, h, Int.cast_zero] end Dat /-! ## Part 4. Lattices in O: the determinant `D` (the note's detG), indices, Cramer -/ section lattice open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] (M : V4Data O) /-- D(ω) = det[γ_i(ω_l)], the note's detG (γ₀ = id, γ_j = τ_j). -/noncomputable def D (w : Fin 4 → O) : O := (Matrix.of fun i l => M.ρ i (w l)).det /-- Lemma `lem:sub` (general form): if ω'_l = Σ_k C_lk ω_k with C integral, D(ω') = D(ω) det C. -/theorem D_comb (v w : Fin 4 → O) (C : Matrix (Fin 4) (Fin 4) ℤ)    (h : ∀ l, w l = ∑ k, (C l k : O) * v k) : D M w = D M v * (C.det : O) := by  have hE : (Matrix.of fun i l => M.ρ i (w l))      = (Matrix.of fun i l => M.ρ i (v l)) * (C.map (Int.castRingHom O))ᵀ := by    ext i l    simp only [Matrix.of_apply, Matrix.mul_apply, Matrix.transpose_apply, Matrix.map_apply, h l,      map_sum, map_mul, map_intCast, eq_intCast]    exact Finset.sum_congr rfl fun k _ => by ring  rw [D, D, hE, Matrix.det_mul, Matrix.det_transpose]  congr 1  rw [show ((C.det : ℤ) : O) = Int.castRingHom O C.det from rfl, RingHom.map_det]  rfl /-- D(ω) ≠ 0 ⇒ ω₁, …, ω₄ are ℤ-independent. -/theorem linIndep_of_D {v : Fin 4 → O} (hv : D M v ≠ 0) : LinearIndependent ℤ v := by  rw [Fintype.linearIndependent_iff]  intro g hg  set A := Matrix.of fun i l => M.ρ i (v l) with hAdef  have hA : A *ᵥ (fun l => (g l : O)) = 0 := by    funext i    simp only [hAdef, Matrix.mulVec, dotProduct, Matrix.of_apply, Pi.zero_apply]    have := congrArg (M.ρ i) hg    rw [map_sum, map_zero] at this    rw [← this]    refine Finset.sum_congr rfl fun l _ => ?_    rw [zsmul_eq_mul, map_mul, map_intCast, mul_comm]  have h2 : A.det • (fun l => (g l : O)) = 0 := by    have := congrArg (fun u => A.adjugate *ᵥ u) hA    simp only [Matrix.mulVec_mulVec, Matrix.adjugate_mul, Matrix.mulVec_zero] at this    rwa [Matrix.smul_mulVec, Matrix.one_mulVec] at this  intro l  have := congrFun h2 l  simp only [Pi.smul_apply, smul_eq_mul, Pi.zero_apply] at this  rcases mul_eq_zero.mp this with h | h  · exact absurd h hv  · exact_mod_cast h /-- The lattice spanned by a family. -/abbrev Lat (v : Fin 4 → O) : AddSubgroup O := (Submodule.span ℤ (Set.range v)).toAddSubgroup /-- Index of lattices by determinants (`AddSubgroup.relIndex_eq_natAbs_det`):    [⟨v⟩ : ⟨w⟩] = |det C| where w = C v, and D(w) = D(v) det C. -/theorem relIndex_D {v w : Fin 4 → O} (hv : D M v ≠ 0) (hw : D M w ≠ 0)    (hwv : ∀ l, w l ∈ Submodule.span ℤ (Set.range v)) :    ∃ C : Matrix (Fin 4) (Fin 4) ℤ, D M w = D M v * (C.det : O) ∧      (Lat w).relIndex (Lat v) = C.det.natAbs := by  choose c hc using fun l => (Submodule.mem_span_range_iff_exists_fun ℤ).mp (hwv l)  refine ⟨Matrix.of c, D_comb M v w (Matrix.of c) (fun l => by    rw [← hc l]; simp [zsmul_eq_mul]), ?_⟩  have hle : Lat w ≤ Lat v := by    show Submodule.span ℤ (Set.range w) ≤ Submodule.span ℤ (Set.range v)    exact Submodule.span_le.mpr (Set.range_subset_iff.mpr hwv)  let bv := Basis.span (linIndep_of_D M hv)  let bw := Basis.span (linIndep_of_D M hw)  let b₂ := bv.map (LinearEquiv.ofEq _ _ (Submodule.toAddSubgroup_toIntSubmodule _).symm)  let b₁ := bw.map (LinearEquiv.ofEq _ _ (Submodule.toAddSubgroup_toIntSubmodule _).symm)  rw [AddSubgroup.relIndex_eq_natAbs_det _ _ hle b₁ b₂, Basis.det_apply]  have hT : b₂.toMatrix (fun i => (⟨b₁ i, hle (SetLike.coe_mem _)⟩ :      AddSubgroup.toIntSubmodule (Lat v))) = (Matrix.of c)ᵀ := by    ext i j    rw [Basis.toMatrix_apply]    have hb1 : ((b₁ j : AddSubgroup.toIntSubmodule (Lat w)) : O) = w j := by      simp only [b₁, Basis.map_apply, LinearEquiv.coe_ofEq_apply]      exact Basis.coe_span_apply _ j    have hsum : (⟨b₁ j, hle (SetLike.coe_mem _)⟩ : AddSubgroup.toIntSubmodule (Lat v))        = LinearEquiv.ofEq _ _ (Submodule.toAddSubgroup_toIntSubmodule _).symm          (∑ k, c j k • bv k) := by      apply Subtype.ext      simp only [LinearEquiv.coe_ofEq_apply, Submodule.coe_sum, Submodule.coe_smul, bv,        Basis.coe_span_apply]      rw [hb1, ← hc j]    rw [hsum]    simp only [b₂, Basis.map_repr, LinearEquiv.trans_apply, LinearEquiv.symm_apply_apply,      Basis.repr_sum_self, Matrix.transpose_apply, Matrix.of_apply]  rw [hT, Matrix.det_transpose] end lattice /-! ## Part 5. The order A = ℤ[x₁, …, x₄], Cramer's rule, and the closure arguments -/ section order open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] (M : V4Data O) /-- A = ℤ[conj(α)] = ℤ[x₁, x₂, x₃, x₄], the subring generated by the conjugates. -/def Aring : Subring O := Subring.closure (Set.range M.x) theorem x_mem_A (k : Fin 4) : M.x k ∈ Aring M := Subring.subset_closure ⟨k, rfl⟩ theorem ρ_x (a k : Fin 4) : M.ρ a (M.x k) = M.x (xt a k) := by  fin_cases a <;> fin_cases k <;>    simp [V4Data.ρ, xt, M.h₁.1, M.h₁.2.1, M.h₁.2.2.1, M.h₁.2.2.2, M.h₂.1, M.h₂.2.1, M.h₂.2.2.1,      M.h₂.2.2.2, M.h₃.1, M.h₃.2.1, M.h₃.2.2.1, M.h₃.2.2.2] theorem xt_assoc : ∀ a i k : Fin 4, xt a (xt i k) = xt (xt a i) k := by decide /-- The elements on which the γ's compose as the group V₄ (γ_a γ_i = γ_{a xor i}). -/def compSub : Subring O where  carrier := {y | ∀ a i, M.ρ a (M.ρ i y) = M.ρ (xt a i) y}  mul_mem' := by    intro y z hy hz a i    simp only [Set.mem_ofPred_eq] at hy hz    rw [map_mul, map_mul, map_mul, hy, hz]  one_mem' := by intro a i; simp  add_mem' := by    intro y z hy hz a i    simp only [Set.mem_ofPred_eq] at hy hz    rw [map_add, map_add, map_add, hy, hz]  zero_mem' := by intro a i; simp  neg_mem' := by    intro y hy a i    simp only [Set.mem_ofPred_eq] at hy    rw [map_neg, map_neg, map_neg, hy] theorem comp_of_mem {y : O} (hy : y ∈ Aring M) (a i : Fin 4) :    M.ρ a (M.ρ i y) = M.ρ (xt a i) y := by  have hle : Aring M ≤ compSub M := by    apply Subring.closure_le.mpr    rintro _ ⟨k, rfl⟩ a i    rw [ρ_x, ρ_x, ρ_x, xt_assoc]  exact hle hy a i /-- the permutation `k ↦ a xor k`. -/def xtPerm (a : Fin 4) : Equiv.Perm (Fin 4) where  toFun := xt a  invFun := xt a  left_inv := by intro k; revert a k; decide  right_inv := by intro k; revert a k; decide theorem sign_xtPerm (a : Fin 4) : Equiv.Perm.sign (xtPerm a) = 1 := by  revert a; decide /-- Cramer's rule with integer numerators (Lemma `lem:cramer`, first part, for every y ∈ A):    Δ·y ∈ ℤ[α]. -/theorem cramer_mem {y : O} (hy : y ∈ Aring M) : Vand M.x * y ∈ M.Zα := by  set V := Matrix.vandermonde M.x  set b : Fin 4 → O := fun i => M.ρ i y  have hrow : ∑ j : Fin 4, M.x 0 ^ (j : ℕ) * cramer V b j = Vand M.x * y := by    have h := congrFun (mulVec_cramer V b) 0    simp only [mulVec, dotProduct, Pi.smul_apply, smul_eq_mul] at h    rw [vand4] at h    simpa [V, b, vandermonde_apply, V4Data.ρ] using h  have hint : ∀ j, ∃ n : ℤ, cramer V b j = n := by    intro j    rw [cramer_apply]    have inv : ∀ a : Fin 4, M.ρ a (V.updateCol j b).det = (V.updateCol j b).det := by      intro a      rw [RingHom.map_det]      have hsub : (M.ρ a).mapMatrix (V.updateCol j b) = (V.updateCol j b).submatrix (xtPerm a) id := by        ext i k        simp only [RingHom.mapMatrix_apply, Matrix.map_apply, Matrix.submatrix_apply, id]        rw [updateCol_apply, updateCol_apply]        split_ifs        · simp only [b]; rw [comp_of_mem M hy]; rfl        · simp only [V, vandermonde_apply, map_pow, ρ_x]; rfl      rw [hsub, det_permute, sign_xtPerm]      simp    exact M.fix _ (inv 1) (inv 2) (inv 3)  choose n hn using hint  rw [← hrow]  refine Submodule.sum_mem _ fun j _ => ?_  rw [hn j, mul_comm]  exact M.intmul_mem _ (M.pow_mem j) /-- If 1 ∈ N and N is stable under multiplication by every conjugate, then A ⊆ N. -/theorem A_le_of_stable (N : Submodule ℤ O) (h1 : (1 : O) ∈ N)    (hmul : ∀ m, ∀ w ∈ N, M.x m * w ∈ N) : ∀ y ∈ Aring M, y ∈ N := by  let S : Subring O :=    { carrier := {y | ∀ w ∈ N, y * w ∈ N}      mul_mem' := fun {y z} hy hz w hw => by        rw [mul_assoc]; exact hy _ (hz w hw)      one_mem' := fun w hw => by rw [one_mul]; exact hw      add_mem' := fun {y z} hy hz w hw => by rw [add_mul]; exact N.add_mem (hy w hw) (hz w hw)      zero_mem' := fun w _ => by rw [zero_mul]; exact N.zero_mem      neg_mem' := fun {y} hy w hw => by rw [neg_mul]; exact N.neg_mem (hy w hw) }  have hle : Aring M ≤ S := by    apply Subring.closure_le.mpr    rintro _ ⟨k, rfl⟩ w hw    exact hmul k w hw  intro y hy  have := hle hy 1 h1  rwa [mul_one] at this /-- Stability can be checked on generators. -/theorem stable_of_gens (G : Set O) (m : Fin 4)    (h : ∀ g ∈ G, M.x m * g ∈ Submodule.span ℤ G) :    ∀ w ∈ Submodule.span ℤ G, M.x m * w ∈ Submodule.span ℤ G := by  intro w hw  induction hw using Submodule.span_induction with  | mem g hg => exact h g hg  | zero => simp  | add y z _ _ hy hz => rw [mul_add]; exact Submodule.add_mem _ hy hz  | smul n y _ hy => rw [mul_smul_comm]; exact Submodule.smul_mem _ n hy /-- Local-global by Bézout: s·z ∈ N with s odd and 2ᵏ·z ∈ N give z ∈ N. -/theorem bezout_mem (N : Submodule ℤ O) {z : O} {s : ℤ} (hs : ¬ Even s) (k : ℕ)    (h1 : (s : O) * z ∈ N) (h2 : (2 : O) ^ k * z ∈ N) : z ∈ N := by  have hc : IsCoprime s (2 ^ k) := by    have : IsCoprime (2 : ℤ) s := Int.isCoprime_two_left.mpr (Int.not_even_iff_odd.mp hs)    exact (this.pow_left (m := k)).symm  obtain ⟨p, q, hpq⟩ := hc  have e : z = p • ((s : O) * z) + q • ((2 : O) ^ k * z) := by    have hpq' : ((p * s + q * 2 ^ k : ℤ) : O) = 1 := by rw [hpq]; simp    push_cast at hpq'    simp only [zsmul_eq_mul]    linear_combination (-z) * hpq'  rw [e]  exact N.add_mem (N.smul_mem p h1) (N.smul_mem q h2) end order /-! ## Part 6. The lattices M = ⟨1, ϑ_j⟩ ⊆ L = ⟨1, η_j⟩ ⊆ A, and ℤ[α] -/ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) theorem ϑ₁_mem : P.ϑ₁ ∈ Aring M := by  rw [P.ϑ_eq₁]; simp only [r1, dQ1]  have := x_mem_A M  exact add_mem (mul_mem (intCast_mem _ _) (sub_mem (sub_mem (add_mem (this 0) (this 1)) (this 2))    (this 3))) (mul_mem (intCast_mem _ _) (sub_mem (mul_mem (this 0) (this 1)) (mul_mem (this 2) (this 3))))theorem ϑ₂_mem : P.ϑ₂ ∈ Aring M := by  rw [P.ϑ_eq₂]; simp only [r2, dQ2]  have := x_mem_A M  exact add_mem (mul_mem (intCast_mem _ _) (sub_mem (sub_mem (add_mem (this 0) (this 2)) (this 1))    (this 3))) (mul_mem (intCast_mem _ _) (sub_mem (mul_mem (this 0) (this 2)) (mul_mem (this 1) (this 3))))theorem ϑ₃_mem : P.ϑ₃ ∈ Aring M := by  rw [P.ϑ_eq₃]; simp only [r3, dQ3]  have := x_mem_A M  exact add_mem (mul_mem (intCast_mem _ _) (sub_mem (sub_mem (add_mem (this 0) (this 3)) (this 1))    (this 2))) (mul_mem (intCast_mem _ _) (sub_mem (mul_mem (this 0) (this 3)) (mul_mem (this 1) (this 2))))theorem η₁_mem : P.η₁ ∈ Aring M := by  have := x_mem_A M  exact add_mem (mul_mem (intCast_mem _ _) (add_mem (this 0) (this 1)))    (mul_mem (intCast_mem _ _) (mul_mem (this 0) (this 1)))theorem η₂_mem : P.η₂ ∈ Aring M := by  have := x_mem_A M  exact add_mem (mul_mem (intCast_mem _ _) (add_mem (this 0) (this 2)))    (mul_mem (intCast_mem _ _) (mul_mem (this 0) (this 2)))theorem η₃_mem : P.η₃ ∈ Aring M := by  have := x_mem_A M  exact add_mem (mul_mem (intCast_mem _ _) (add_mem (this 0) (this 3)))    (mul_mem (intCast_mem _ _) (mul_mem (this 0) (this 3))) /-- the action of V₄ on the ϑ_j (each is an eigenvector of a nontrivial character). -/theorem sgnϑ : M.τ₁ P.ϑ₁ = P.ϑ₁ ∧ M.τ₁ P.ϑ₂ = -P.ϑ₂ ∧ M.τ₁ P.ϑ₃ = -P.ϑ₃ ∧    M.τ₂ P.ϑ₁ = -P.ϑ₁ ∧ M.τ₂ P.ϑ₂ = P.ϑ₂ ∧ M.τ₂ P.ϑ₃ = -P.ϑ₃ ∧    M.τ₃ P.ϑ₁ = -P.ϑ₁ ∧ M.τ₃ P.ϑ₂ = -P.ϑ₂ ∧ M.τ₃ P.ϑ₃ = P.ϑ₃ := by  obtain ⟨a1, b1, a2, b2, a3, b3⟩ := M.sgn₁  obtain ⟨c1, d1, c2, d2, c3, d3⟩ := M.sgn₂  obtain ⟨f1, g1, f2, g2, f3, g3⟩ := M.sgn₃  refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩  · simpa using theta_sign M.τ₁ P.bez₁ P.hr₁ P.hq₁ a1 b1  · simpa using theta_sign M.τ₁ P.bez₂ P.hr₂ P.hq₂ a2 b2  · simpa using theta_sign M.τ₁ P.bez₃ P.hr₃ P.hq₃ a3 b3  · simpa using theta_sign M.τ₂ P.bez₁ P.hr₁ P.hq₁ c1 d1  · simpa using theta_sign M.τ₂ P.bez₂ P.hr₂ P.hq₂ c2 d2  · simpa using theta_sign M.τ₂ P.bez₃ P.hr₃ P.hq₃ c3 d3  · simpa using theta_sign M.τ₃ P.bez₁ P.hr₁ P.hq₁ f1 g1  · simpa using theta_sign M.τ₃ P.bez₂ P.hr₂ P.hq₂ f2 g2  · simpa using theta_sign M.τ₃ P.bez₃ P.hr₃ P.hq₃ f3 g3 /-- the families 1, ϑ_j; 1, η_j; Δ, Δϑ_j; and 1, α, α², α³. -/def vϑ : Fin 4 → O := ![1, P.ϑ₁, P.ϑ₂, P.ϑ₃]def vη : Fin 4 → O := ![1, P.η₁, P.η₂, P.η₃]def vX : Fin 4 → O := ![(P.δ : O), P.δ * P.ϑ₁, P.δ * P.ϑ₂, P.δ * P.ϑ₃]def vpow (M : V4Data O) : Fin 4 → O := fun k => M.x 0 ^ (k : ℕ) /-- D(1, ϑ₁, ϑ₂, ϑ₃) = −16Θ (the ϑ_j are eigenvectors of the three nontrivial characters). -/theorem D_vϑ : D M P.vϑ = -16 * P.Θ := by  obtain ⟨s1, s2, s3, s4, s5, s6, s7, s8, s9⟩ := P.sgnϑ  rw [D, det4]  simp [vϑ, V4Data.ρ, s1, s2, s3, s4, s5, s6, s7, s8, s9]  rw [← P.hΘ]  simp only [det4e]  ring theorem D_vpow : D M (vpow M) = Vand M.x := by  rw [D, ← vand4]  congr 1  ext i l  simp [vpow, Matrix.vandermonde_apply, map_pow, ρ_x M i 0]  fin_cases i <;> rfl theorem det_C8 (e₁ e₂ e₃ : ℤ) :    (!![1, 0, 0, 0; -e₁, 2, 0, 0; -e₂, 0, 2, 0; -e₃, 0, 0, 2] : Matrix (Fin 4) (Fin 4) ℤ).det = 8 := by  rw [det4]; simp [det4e] theorem det_Cδ (δ : ℤ) :    (!![δ, 0, 0, 0; 0, δ, 0, 0; 0, 0, δ, 0; 0, 0, 0, δ] : Matrix (Fin 4) (Fin 4) ℤ).det = δ ^ 4 := by  rw [det4]; simp [det4e]; ring theorem D_vη : D M P.vη = -2 * P.Θ := by  have h := D_comb M P.vη P.vϑ    (!![1, 0, 0, 0; -P.e₁, 2, 0, 0; -P.e₂, 0, 2, 0; -P.e₃, 0, 0, 2]) (by      intro l      fin_cases l <;> simp [vϑ, vη, Fin.sum_univ_four, ← P.two_η₁, ← P.two_η₂, ← P.two_η₃] <;> ring)  rw [det_C8, P.D_vϑ] at h  push_cast at h  apply mul_left_cancel₀ (show (8 : O) ≠ 0 by norm_num)  linear_combination -h theorem D_vX : D M P.vX = P.δ ^ 4 * (-16 * P.Θ) := by  have h := D_comb M P.vϑ P.vX    (!![P.δ, 0, 0, 0; 0, P.δ, 0, 0; 0, 0, P.δ, 0; 0, 0, 0, P.δ]) (by      intro l      fin_cases l <;> simp [vϑ, vX, Fin.sum_univ_four])  rw [det_Cδ, P.D_vϑ] at h  rw [h]; push_cast; ring end Dat /-! ## Part 7. M[1/2] is a ring containing A; the indices of M, L, X, ℤ[α] -/ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- 4ϑ_j = (4u_j + a v_j) n_j ϑ_j − v_j n_k n_l ϑ_k ϑ_l (from 4q_j = a r_j − r_k r_l). -/theorem four_ϑ₁ : 4 * P.ϑ₁ = (4 * P.u₁ + P.a * P.v₁) * P.n₁ * P.ϑ₁ - P.v₁ * P.n₂ * P.n₃ * (P.ϑ₂ * P.ϑ₃) := by  have q : 4 * dQ1 M.x = ea M.x * r1 M.x - r2 M.x * r3 M.x := by    simp only [dQ1, r1, r2, r3, ea]; ring  rw [P.ha, P.hr₁, P.hr₂, P.hr₃] at q  have h := P.ϑ_eq₁  rw [P.hr₁] at h  linear_combination 4 * h + (P.v₁ : O) * qtheorem four_ϑ₂ : 4 * P.ϑ₂ = (4 * P.u₂ + P.a * P.v₂) * P.n₂ * P.ϑ₂ - P.v₂ * P.n₁ * P.n₃ * (P.ϑ₁ * P.ϑ₃) := by  have q : 4 * dQ2 M.x = ea M.x * r2 M.x - r1 M.x * r3 M.x := by    simp only [dQ2, r1, r2, r3, ea]; ring  rw [P.ha, P.hr₁, P.hr₂, P.hr₃] at q  have h := P.ϑ_eq₂  rw [P.hr₂] at h  linear_combination 4 * h + (P.v₂ : O) * qtheorem four_ϑ₃ : 4 * P.ϑ₃ = (4 * P.u₃ + P.a * P.v₃) * P.n₃ * P.ϑ₃ - P.v₃ * P.n₁ * P.n₂ * (P.ϑ₁ * P.ϑ₂) := by  have q : 4 * dQ3 M.x = ea M.x * r3 M.x - r1 M.x * r2 M.x := by    simp only [dQ3, r1, r2, r3, ea]; ring  rw [P.ha, P.hr₁, P.hr₂, P.hr₃] at q  have h := P.ϑ_eq₃  rw [P.hr₃] at h  linear_combination 4 * h + (P.v₃ : O) * q /-- 16 ϑ_i ϑ_j ∈ ℤ ϑ_k (so M[1/2] is a ring; cf. the proof of `lem:Lodd`). -/theorem sixteen₁₂ : ∃ K : ℤ, 16 * (P.ϑ₁ * P.ϑ₂) = K * P.ϑ₃ := by  set A₁ : ℤ := 4 * P.u₁ + P.a * P.v₁  set A₂ : ℤ := 4 * P.u₂ + P.a * P.v₂  set c₃ : ℤ := P.a * P.n₃ - 4 * P.t₃  refine ⟨(A₁ * A₂ + P.v₁ * P.v₂ * P.n₃ ^ 2 * P.Δ₃) * c₃    - (A₁ * P.v₂ * P.n₁ ^ 2 * P.Δ₁ + P.v₁ * A₂ * P.n₂ ^ 2 * P.Δ₂) * P.n₃, ?_⟩  have e1 := P.four_ϑ₁  have e2 := P.four_ϑ₂  have p3 := P.prod₃  calc 16 * (P.ϑ₁ * P.ϑ₂) = (4 * P.ϑ₁) * (4 * P.ϑ₂) := by ring    _ = _ := by rw [e1, e2]    _ = _ := by      simp only [A₁, A₂, c₃]; push_cast      linear_combination ((4 * P.u₁ + P.a * P.v₁) * (4 * P.u₂ + P.a * P.v₂)          + P.v₁ * P.v₂ * P.n₃ ^ 2 * P.ϑ₃ ^ 2 : O) * p3        - ((4 * P.u₁ + P.a * P.v₁) * P.v₂ * P.n₁ ^ 2 * P.n₃ * P.ϑ₃ : O) * P.hΔ₁        - (P.v₁ * (4 * P.u₂ + P.a * P.v₂) * P.n₂ ^ 2 * P.n₃ * P.ϑ₃ : O) * P.hΔ₂        + (P.v₁ * P.v₂ * P.n₃ ^ 2 * (P.a * P.n₃ - 4 * P.t₃) * P.ϑ₃ : O) * P.hΔ₃ theorem sixteen₁₃ : ∃ K : ℤ, 16 * (P.ϑ₁ * P.ϑ₃) = K * P.ϑ₂ := by  set A₁ : ℤ := 4 * P.u₁ + P.a * P.v₁  set A₃ : ℤ := 4 * P.u₃ + P.a * P.v₃  set c₂ : ℤ := P.a * P.n₂ - 4 * P.t₂  refine ⟨(A₁ * A₃ + P.v₁ * P.v₃ * P.n₂ ^ 2 * P.Δ₂) * c₂    - (A₁ * P.v₃ * P.n₁ ^ 2 * P.Δ₁ + P.v₁ * A₃ * P.n₃ ^ 2 * P.Δ₃) * P.n₂, ?_⟩  have e1 := P.four_ϑ₁  have e3 := P.four_ϑ₃  have p2 := P.prod₂  calc 16 * (P.ϑ₁ * P.ϑ₃) = (4 * P.ϑ₁) * (4 * P.ϑ₃) := by ring    _ = _ := by rw [e1, e3]    _ = _ := by      simp only [A₁, A₃, c₂]; push_cast      linear_combination ((4 * P.u₁ + P.a * P.v₁) * (4 * P.u₃ + P.a * P.v₃)          + P.v₁ * P.v₃ * P.n₂ ^ 2 * P.ϑ₂ ^ 2 : O) * p2        - ((4 * P.u₁ + P.a * P.v₁) * P.v₃ * P.n₁ ^ 2 * P.n₂ * P.ϑ₂ : O) * P.hΔ₁        - (P.v₁ * (4 * P.u₃ + P.a * P.v₃) * P.n₃ ^ 2 * P.n₂ * P.ϑ₂ : O) * P.hΔ₃        + (P.v₁ * P.v₃ * P.n₂ ^ 2 * (P.a * P.n₂ - 4 * P.t₂) * P.ϑ₂ : O) * P.hΔ₂ theorem sixteen₂₃ : ∃ K : ℤ, 16 * (P.ϑ₂ * P.ϑ₃) = K * P.ϑ₁ := by  set A₂ : ℤ := 4 * P.u₂ + P.a * P.v₂  set A₃ : ℤ := 4 * P.u₃ + P.a * P.v₃  set c₁ : ℤ := P.a * P.n₁ - 4 * P.t₁  refine ⟨(A₂ * A₃ + P.v₂ * P.v₃ * P.n₁ ^ 2 * P.Δ₁) * c₁    - (A₂ * P.v₃ * P.n₂ ^ 2 * P.Δ₂ + P.v₂ * A₃ * P.n₃ ^ 2 * P.Δ₃) * P.n₁, ?_⟩  have e2 := P.four_ϑ₂  have e3 := P.four_ϑ₃  have p1 := P.prod₁  calc 16 * (P.ϑ₂ * P.ϑ₃) = (4 * P.ϑ₂) * (4 * P.ϑ₃) := by ring    _ = _ := by rw [e2, e3]    _ = _ := by      simp only [A₂, A₃, c₁]; push_cast      linear_combination ((4 * P.u₂ + P.a * P.v₂) * (4 * P.u₃ + P.a * P.v₃)          + P.v₂ * P.v₃ * P.n₁ ^ 2 * P.ϑ₁ ^ 2 : O) * p1        - ((4 * P.u₂ + P.a * P.v₂) * P.v₃ * P.n₂ ^ 2 * P.n₁ * P.ϑ₁ : O) * P.hΔ₂        - (P.v₂ * (4 * P.u₃ + P.a * P.v₃) * P.n₃ ^ 2 * P.n₁ * P.ϑ₁ : O) * P.hΔ₃        + (P.v₂ * P.v₃ * P.n₁ ^ 2 * (P.a * P.n₁ - 4 * P.t₁) * P.ϑ₁ : O) * P.hΔ₁ end Dat namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- M = ℤ + ℤϑ₁ + ℤϑ₂ + ℤϑ₃ (as a ℤ-submodule of O). -/abbrev Mℓ : Submodule ℤ O := Submodule.span ℤ (Set.range P.vϑ)/-- L = ℤ + ℤη₁ + ℤη₂ + ℤη₃. -/abbrev Lℓ : Submodule ℤ O := Submodule.span ℤ (Set.range P.vη) theorem mem_Mℓ (k : Fin 4) : P.vϑ k ∈ P.Mℓ := Submodule.subset_span ⟨k, rfl⟩theorem mem_Lℓ (k : Fin 4) : P.vη k ∈ P.Lℓ := Submodule.subset_span ⟨k, rfl⟩ theorem smul_mem' (N : Submodule ℤ O) (n : ℤ) {y : O} (hy : y ∈ N) : (n : O) * y ∈ N := by  rw [← zsmul_eq_mul]; exact N.smul_mem n hy theorem one_Mℓ : (1 : O) ∈ P.Mℓ := by simpa [vϑ] using P.mem_Mℓ 0theorem ϑ₁_Mℓ : P.ϑ₁ ∈ P.Mℓ := by simpa [vϑ] using P.mem_Mℓ 1theorem ϑ₂_Mℓ : P.ϑ₂ ∈ P.Mℓ := by simpa [vϑ] using P.mem_Mℓ 2theorem ϑ₃_Mℓ : P.ϑ₃ ∈ P.Mℓ := by simpa [vϑ] using P.mem_Mℓ 3theorem one_Lℓ : (1 : O) ∈ P.Lℓ := by simpa [vη] using P.mem_Lℓ 0theorem η₁_Lℓ : P.η₁ ∈ P.Lℓ := by simpa [vη] using P.mem_Lℓ 1theorem η₂_Lℓ : P.η₂ ∈ P.Lℓ := by simpa [vη] using P.mem_Lℓ 2theorem η₃_Lℓ : P.η₃ ∈ P.Lℓ := by simpa [vη] using P.mem_Lℓ 3 theorem gen16 (a b : Fin 4) : 16 * (P.vϑ a * P.vϑ b) ∈ P.Mℓ := by  have sm := smul_mem' P.Mℓ  obtain ⟨K₃, h₃⟩ := P.sixteen₁₂  obtain ⟨K₂, h₂⟩ := P.sixteen₁₃  obtain ⟨K₁, h₁⟩ := P.sixteen₂₃  have s1 : ∀ {y : O}, y ∈ P.Mℓ → (16 : O) * (1 * y) ∈ P.Mℓ := fun hy => by    rw [one_mul]; exact_mod_cast sm 16 hy  have s2 : ∀ {y : O}, y ∈ P.Mℓ → (16 : O) * (y * 1) ∈ P.Mℓ := fun hy => by    rw [mul_one]; exact_mod_cast sm 16 hy  have sq : ∀ {y : O} {d : ℤ}, y * y = d → (16 : O) * (y * y) ∈ P.Mℓ := fun {y} {d} hd => by    rw [hd]; have := sm (16 * d) P.one_Mℓ; push_cast at this; simpa using this  fin_cases a <;> fin_cases b <;> simp only [vϑ, Fin.zero_eta, Fin.mk_one, Fin.reduceFinMk,    Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val]  · exact s1 P.one_Mℓ  · exact s1 P.ϑ₁_Mℓ  · exact s1 P.ϑ₂_Mℓ  · exact s1 P.ϑ₃_Mℓ  · exact s2 P.ϑ₁_Mℓ  · exact sq P.hΔ₁  · rw [h₃]; exact sm _ P.ϑ₃_Mℓ  · rw [h₂]; exact sm _ P.ϑ₂_Mℓ  · exact s2 P.ϑ₂_Mℓ  · rw [show (16 : O) * (P.ϑ₂ * P.ϑ₁) = 16 * (P.ϑ₁ * P.ϑ₂) by ring, h₃]; exact sm _ P.ϑ₃_Mℓ  · exact sq P.hΔ₂  · rw [h₁]; exact sm _ P.ϑ₁_Mℓ  · exact s2 P.ϑ₃_Mℓ  · rw [show (16 : O) * (P.ϑ₃ * P.ϑ₁) = 16 * (P.ϑ₁ * P.ϑ₃) by ring, h₂]; exact sm _ P.ϑ₂_Mℓ  · rw [show (16 : O) * (P.ϑ₃ * P.ϑ₂) = 16 * (P.ϑ₂ * P.ϑ₃) by ring, h₁]; exact sm _ P.ϑ₁_Mℓ  · exact sq P.hΔ₃ /-- 16·M·M ⊆ M. -/theorem Mℓ16 {p q : O} (hp : p ∈ P.Mℓ) (hq : q ∈ P.Mℓ) : 16 * (p * q) ∈ P.Mℓ := by  induction hp using Submodule.span_induction with  | mem g hg =>    obtain ⟨a, rfl⟩ := hg    induction hq using Submodule.span_induction with    | mem g' hg' => obtain ⟨b, rfl⟩ := hg'; exact P.gen16 a b    | zero => simp    | add y z _ _ hy hz => rw [mul_add, mul_add]; exact Submodule.add_mem _ hy hz    | smul n y _ hy =>      rw [mul_smul_comm, mul_smul_comm]; exact Submodule.smul_mem _ n hy  | zero => simp  | add y z _ _ hy hz => rw [add_mul, mul_add]; exact Submodule.add_mem _ hy hz  | smul n y _ hy => rw [smul_mul_assoc, mul_smul_comm]; exact Submodule.smul_mem _ n hy /-- 4x_m ∈ M for every conjugate. -/theorem four_x_Mℓ (m : Fin 4) : 4 * M.x m ∈ P.Mℓ := by  have sm := smul_mem' P.Mℓ  have base : ∀ (s₁ s₂ s₃ : ℤ), (P.a : O) + s₁ * P.n₁ * P.ϑ₁ + s₂ * P.n₂ * P.ϑ₂ + s₃ * P.n₃ * P.ϑ₃      ∈ P.Mℓ := by    intro s₁ s₂ s₃    refine add_mem (add_mem (add_mem ?_ ?_) ?_) ?_    · simpa using sm P.a P.one_Mℓ    · have := sm (s₁ * P.n₁) P.ϑ₁_Mℓ; push_cast at this; exact this    · have := sm (s₂ * P.n₂) P.ϑ₂_Mℓ; push_cast at this; exact this    · have := sm (s₃ * P.n₃) P.ϑ₃_Mℓ; push_cast at this; exact this  fin_cases m  · have := base 1 1 1; simp only [Int.cast_one, one_mul] at this    rw [show ((⟨0, by norm_num⟩ : Fin 4)) = 0 from rfl, P.four_x0]; exact this  · have := base 1 (-1) (-1); push_cast at this    rw [show ((⟨1, by norm_num⟩ : Fin 4)) = 1 from rfl, P.four_x1]    convert this using 1; ring  · have := base (-1) 1 (-1); push_cast at this    rw [show ((⟨2, by norm_num⟩ : Fin 4)) = 2 from rfl, P.four_x2]    convert this using 1; ring  · have := base (-1) (-1) 1; push_cast at this    rw [show ((⟨3, by norm_num⟩ : Fin 4)) = 3 from rfl, P.four_x3]    convert this using 1; ring /-- A ⊆ M[1/2]: every y ∈ A has 2ᴷ y ∈ M (proof of `lem:Lodd`, globally). -/theorem half_loc {y : O} (hy : y ∈ Aring M) : ∃ K : ℕ, (2 : O) ^ K * y ∈ P.Mℓ := by  have sm := smul_mem' P.Mℓ  let S : Subring O :=    { carrier := {y | ∃ K : ℕ, (2 : O) ^ K * y ∈ P.Mℓ}      mul_mem' := fun {y z} ⟨K, hK⟩ ⟨K', hK'⟩ => ⟨K + K' + 4, by        have := P.Mℓ16 hK hK'        have e : (2 : O) ^ (K + K' + 4) * (y * z) = 16 * ((2 ^ K * y) * (2 ^ K' * z)) := by ring        rw [e]; exact this⟩      one_mem' := ⟨0, by simpa using P.one_Mℓ⟩      add_mem' := fun {y z} ⟨K, hK⟩ ⟨K', hK'⟩ => ⟨K + K', by        have e : (2 : O) ^ (K + K') * (y + z)            = ((2 ^ K' : ℤ) : O) * (2 ^ K * y) + ((2 ^ K : ℤ) : O) * (2 ^ K' * z) := by          push_cast; ring        rw [e]; exact add_mem (sm _ hK) (sm _ hK')⟩      zero_mem' := ⟨0, by simp⟩      neg_mem' := fun {y} ⟨K, hK⟩ => ⟨K, by rw [mul_neg]; exact neg_mem hK⟩ }  have hle : Aring M ≤ S := by    apply Subring.closure_le.mpr    rintro _ ⟨m, rfl⟩    exact ⟨2, by rw [show (2 : O) ^ 2 = 4 by norm_num]; exact P.four_x_Mℓ m⟩  exact hle hy /-! ### The indices -/ theorem Mℓ_le_Lℓ : P.Mℓ ≤ P.Lℓ := by  apply Submodule.span_le.mpr  rintro _ ⟨k, rfl⟩  have sm := smul_mem' P.Lℓ  fin_cases k  · simpa [vϑ] using P.one_Lℓ  · show P.ϑ₁ ∈ P.Lℓ    rw [← P.two_η₁]    exact sub_mem (by exact_mod_cast sm 2 P.η₁_Lℓ) (by simpa using sm P.e₁ P.one_Lℓ)  · show P.ϑ₂ ∈ P.Lℓ    rw [← P.two_η₂]    exact sub_mem (by exact_mod_cast sm 2 P.η₂_Lℓ) (by simpa using sm P.e₂ P.one_Lℓ)  · show P.ϑ₃ ∈ P.Lℓ    rw [← P.two_η₃]    exact sub_mem (by exact_mod_cast sm 2 P.η₃_Lℓ) (by simpa using sm P.e₃ P.one_Lℓ) /-- a span of elements of A lies in A. -/theorem span_le_A {v : Fin 4 → O} (hv : ∀ k, v k ∈ Aring M) {y : O}    (hy : y ∈ Submodule.span ℤ (Set.range v)) : y ∈ Aring M := by  induction hy using Submodule.span_induction with  | mem g hg => obtain ⟨k, rfl⟩ := hg; exact hv k  | zero => exact zero_mem _  | add _ _ _ _ h1 h2 => exact add_mem h1 h2  | smul n _ _ h => exact zsmul_mem h n theorem vη_mem_A (k : Fin 4) : P.vη k ∈ Aring M := by  fin_cases k  · exact one_mem _  · exact P.η₁_mem  · exact P.η₂_mem  · exact P.η₃_mem end Dat namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) theorem D_vϑ_ne : D M P.vϑ ≠ 0 := by  rw [P.D_vϑ]  exact mul_ne_zero (by norm_num) (Int.cast_ne_zero.mpr P.Θ_ne)theorem D_vη_ne : D M P.vη ≠ 0 := by  rw [P.D_vη]  exact mul_ne_zero (by norm_num) (Int.cast_ne_zero.mpr P.Θ_ne)theorem δ_ne : P.δ ≠ 0 := by  intro h; apply M.Vand_ne_zero; rw [P.hδ, h, Int.cast_zero]theorem D_vX_ne : D M P.vX ≠ 0 := by  rw [P.D_vX]  exact mul_ne_zero (pow_ne_zero _ (Int.cast_ne_zero.mpr P.δ_ne))    (mul_ne_zero (by norm_num) (Int.cast_ne_zero.mpr P.Θ_ne))theorem D_vpow_ne : D M (vpow M) ≠ 0 := by  rw [D_vpow]; exact M.Vand_ne_zero /-- [L : M] = 8 (proof of prop:discA). -/theorem relIndex_Mℓ_Lℓ : (Lat P.vϑ).relIndex (Lat P.vη) = 8 := by  obtain ⟨C, hD, hidx⟩ := relIndex_D M P.D_vη_ne P.D_vϑ_ne    (fun l => P.Mℓ_le_Lℓ (P.mem_Mℓ l))  rw [P.D_vϑ, P.D_vη] at hD  have h8 : C.det = 8 := by    have h' : ((-2 * P.Θ * 8 : ℤ) : O) = ((-2 * P.Θ * C.det : ℤ) : O) := by      simp only [Int.cast_mul, Int.cast_neg, Int.cast_ofNat]; linear_combination hD    have h'' : -2 * P.Θ * 8 = -2 * P.Θ * C.det := Int.cast_injective h'    exact (mul_left_cancel₀ (mul_ne_zero (by norm_num) P.Θ_ne) h'').symm  rw [hidx, h8]; rfl theorem vX_mem (l : Fin 4) : P.vX l ∈ P.Mℓ := by  have sm := smul_mem' P.Mℓ  fin_cases l  · simpa [vX] using sm P.δ P.one_Mℓ  · exact sm P.δ P.ϑ₁_Mℓ  · exact sm P.δ P.ϑ₂_Mℓ  · exact sm P.δ P.ϑ₃_Mℓ /-- [M : X] = |Δ|⁴ for X = ΔM. -/theorem relIndex_X_Mℓ : (Lat P.vX).relIndex (Lat P.vϑ) = P.δ.natAbs ^ 4 := by  obtain ⟨C, hD, hidx⟩ := relIndex_D M P.D_vϑ_ne P.D_vX_ne P.vX_mem  rw [P.D_vϑ, P.D_vX] at hD  have h4 : C.det = P.δ ^ 4 := by    have h' : ((-16 * P.Θ * P.δ ^ 4 : ℤ) : O) = ((-16 * P.Θ * C.det : ℤ) : O) := by      simp only [Int.cast_mul, Int.cast_neg, Int.cast_ofNat, Int.cast_pow]; linear_combination hD    have h'' : -16 * P.Θ * P.δ ^ 4 = -16 * P.Θ * C.det := Int.cast_injective h'    exact (mul_left_cancel₀ (mul_ne_zero (by norm_num) P.Θ_ne) h'').symm  rw [hidx, h4, Int.natAbs_pow] theorem vX_mem_Zα (l : Fin 4) : P.vX l ∈ Submodule.span ℤ (Set.range (vpow M)) := by  show P.vX l ∈ M.Zα  have hc : ∀ {y : O}, y ∈ Aring M → (P.δ : O) * y ∈ M.Zα := fun hy => by    rw [← P.hδ]; exact cramer_mem M hy  fin_cases l  · simpa [vX] using M.int_mem P.δ  · exact hc P.ϑ₁_mem  · exact hc P.ϑ₂_mem  · exact hc P.ϑ₃_mem /-- [ℤ[α] : X] = 16 |Δ|³ |Θ|. -/theorem relIndex_X_Z : (Lat P.vX).relIndex (Lat (vpow M)) = 16 * P.δ.natAbs ^ 3 * P.Θ.natAbs := by  obtain ⟨C, hD, hidx⟩ := relIndex_D M (D_vpow_ne (M := M)) P.D_vX_ne P.vX_mem_Zα  rw [D_vpow, P.D_vX, P.hδ] at hD  have hC : C.det = -16 * P.δ ^ 3 * P.Θ := by    have h' : ((P.δ * (-16 * P.δ ^ 3 * P.Θ) : ℤ) : O) = ((P.δ * C.det : ℤ) : O) := by      simp only [Int.cast_mul, Int.cast_neg, Int.cast_ofNat, Int.cast_pow]; linear_combination hD    have h'' : P.δ * (-16 * P.δ ^ 3 * P.Θ) = P.δ * C.det := Int.cast_injective h'    exact (mul_left_cancel₀ P.δ_ne h'').symm  rw [hidx, hC, Int.natAbs_mul, Int.natAbs_mul, Int.natAbs_pow]  rfl /-- k·y ∈ L ⟺ k even, when 2y ∈ L and y ∉ L. -/theorem zsmul_mem_iff (L : Submodule ℤ O) {y : O} (h2y : 2 * y ∈ L) (hny : y ∉ L) (k : ℤ) :    k • y ∈ L ↔ Even k := by  constructor  · intro hk    by_contra hodd    rw [Int.not_even_iff_odd] at hodd    obtain ⟨j, rfl⟩ := hodd    apply hny    have e : y = (2 * j + 1) • y - j • (2 * y) := by      rw [zsmul_eq_mul, zsmul_eq_mul]; push_cast; ring    rw [e]; exact L.sub_mem hk (L.smul_mem j h2y)  · rintro ⟨j, rfl⟩    have e : (j + j) • y = j • (2 * y) := by rw [zsmul_eq_mul, zsmul_eq_mul]; push_cast; ring    rw [e]; exact L.smul_mem j h2y /-- [A : L] = 2 when A = L + ℤy with 2y ∈ L, y ∉ L. -/theorem relIndex_two (L : Submodule ℤ O) {y : O} (hyA : y ∈ Aring M) (h2y : 2 * y ∈ L) (hny : y ∉ L)    (hAN : ∀ z ∈ Aring M, ∃ l ∈ L, ∃ k : ℤ, z = l + k • y) :    L.toAddSubgroup.relIndex (Aring M).toAddSubgroup = 2 := by  rw [AddSubgroup.relIndex_eq_two_iff]  refine ⟨y, hyA, fun b hb => ?_⟩  obtain ⟨l, hl, k, rfl⟩ := hAN b hb  have hk := zsmul_mem_iff L h2y hny k  have hk1 := zsmul_mem_iff L h2y hny (k + 1)  have e1 : l + k • y + y = l + (k + 1) • y := by rw [add_zsmul, one_zsmul]; abel  change Xor (l + k • y + y ∈ L) (l + k • y ∈ L)  rw [e1]  have m1 : l + (k + 1) • y ∈ L ↔ (k + 1) • y ∈ L := L.add_mem_iff_right hl  have m2 : l + k • y ∈ L ↔ k • y ∈ L := L.add_mem_iff_right hl  rw [m1, m2, hk, hk1, Int.even_add_one]  by_cases h : Even k <;> simp [h] /-- prop:discA, the index: from [A : L] = 2, [A : ℤ[α]] · |Θ| = |Δ|. -/theorem index_of_two (h2 : P.Lℓ.toAddSubgroup.relIndex (Aring M).toAddSubgroup = 2) :    (Lat (vpow M)).relIndex (Aring M).toAddSubgroup * P.Θ.natAbs = P.δ.natAbs := by  have hXZ : Lat P.vX ≤ Lat (vpow M) :=    Submodule.span_le.mpr (Set.range_subset_iff.mpr P.vX_mem_Zα)  have hZA : Lat (vpow M) ≤ (Aring M).toAddSubgroup := by    intro y hy    exact span_le_A (fun k => pow_mem (x_mem_A M 0) _) hy  have hXM : Lat P.vX ≤ Lat P.vϑ := Submodule.span_le.mpr (Set.range_subset_iff.mpr P.vX_mem)  have hML : Lat P.vϑ ≤ Lat P.vη := P.Mℓ_le_Lℓ  have hLA : Lat P.vη ≤ (Aring M).toAddSubgroup := fun y hy => span_le_A P.vη_mem_A hy  have e1 := AddSubgroup.relIndex_mul_relIndex _ _ _ hXZ hZA  have e2 := AddSubgroup.relIndex_mul_relIndex _ _ _ hXM (hML.trans hLA)  have e3 := AddSubgroup.relIndex_mul_relIndex _ _ _ hML hLA  rw [P.relIndex_X_Z] at e1  rw [P.relIndex_X_Mℓ, ← e3, P.relIndex_Mℓ_Lℓ, h2, ← e1] at e2  have hδ0 : P.δ.natAbs ≠ 0 := Int.natAbs_ne_zero.mpr P.δ_ne  apply mul_left_cancel₀ (show 16 * P.δ.natAbs ^ 3 ≠ 0 by positivity)  calc 16 * P.δ.natAbs ^ 3 * ((Lat (vpow M)).relIndex (Aring M).toAddSubgroup * P.Θ.natAbs)      = 16 * P.δ.natAbs ^ 3 * P.Θ.natAbs * (Lat (vpow M)).relIndex (Aring M).toAddSubgroup := by ring    _ = P.δ.natAbs ^ 4 * (8 * 2) := e2.symm    _ = 16 * P.δ.natAbs ^ 3 * P.δ.natAbs := by ring end Dat /-! ## Part 8. [A : L] = 2 (proof of prop:discA) -/ /-- An element w ∈ O with 2w ∈ ℤ is an integer (O ∩ ℚ = ℤ in the model). -/theorem half_int {O : Type*} [CommRing O] [IsDomain O] [CharZero O] (M : V4Data O) {k : ℤ} {w : O}    (h : (k : O) = 2 * w) : ∃ m : ℤ, w = m := by  obtain ⟨m, rfl⟩ : (2 : ℤ) ∣ k := M.sat (by norm_num) (by push_cast; exact h)  refine ⟨m, ?_⟩  have : (2 : O) * w = 2 * m := by rw [← h]; push_cast; ring  exact (mul_left_cancel₀ two_ne_zero this) namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- N = L + ℤy. -/abbrev Nℓ (y : O) : Submodule ℤ O := Submodule.span ℤ (Set.range P.vη ∪ {y}) theorem Nℓ_eq (y : O) : P.Nℓ y = P.Lℓ ⊔ Submodule.span ℤ {y} := Submodule.span_union _ _ theorem Lℓ_le_N (y : O) : P.Lℓ ≤ P.Nℓ y := by rw [Nℓ_eq]; exact le_sup_left theorem y_mem_N (y : O) : y ∈ P.Nℓ y :=  Submodule.subset_span (Set.mem_union_right _ (Set.mem_singleton y)) theorem decomp {y z : O} (hz : z ∈ P.Nℓ y) : ∃ l ∈ P.Lℓ, ∃ k : ℤ, z = l + k • y := by  rw [Nℓ_eq, Submodule.mem_sup] at hz  obtain ⟨l, hl, w, hw, rfl⟩ := hz  obtain ⟨k, rfl⟩ := Submodule.mem_span_singleton.mp hw  exact ⟨l, hl, k, rfl⟩ /-- The closure scheme: if every product x_m·g (g a generator of N = L + ℤy) has an odd multiple    in N, then A ⊆ N (the 2-power multiple is supplied by `half_loc`; Bézout glues). -/theorem A_le_N {y : O} (hyA : y ∈ Aring M) {s : ℤ} (hs : ¬ Even s)    (hodd : ∀ m, ∀ g ∈ Set.range P.vη ∪ {y}, (s : O) * (M.x m * g) ∈ P.Nℓ y) :    ∀ z ∈ Aring M, z ∈ P.Nℓ y := by  apply A_le_of_stable M (P.Nℓ y) (P.Lℓ_le_N y P.one_Lℓ)  intro m  apply stable_of_gens M _ m  intro g hg  have hgA : g ∈ Aring M := by    rcases hg with ⟨k, rfl⟩ | hg    · exact P.vη_mem_A k    · rw [Set.mem_singleton_iff.mp hg]; exact hyA  obtain ⟨K, hK⟩ := P.half_loc (mul_mem (x_mem_A M m) hgA)  exact bezout_mem _ hs K (hodd m g hg) (P.Lℓ_le_N y (P.Mℓ_le_Lℓ hK)) /-- Coefficients in the ℤ-basis 1, η₁, η₂, η₃ of L are unique. -/theorem coeff_η {c d : Fin 4 → ℤ} (h : ∑ k, c k • P.vη k = ∑ k, d k • P.vη k) : c = d := by  have hli := Fintype.linearIndependent_iff.mp (linIndep_of_D M P.D_vη_ne) (c - d) (by    simp only [Pi.sub_apply, sub_smul, Finset.sum_sub_distrib, h, sub_self])  funext k  exact sub_eq_zero.mp (hli k) theorem mem_Lℓ_iff {z : O} : z ∈ P.Lℓ ↔ ∃ c : Fin 4 → ℤ, ∑ k, c k • P.vη k = z :=  Submodule.mem_span_range_iff_exists_fun ℤ theorem sum_vη (c : Fin 4 → ℤ) : ∑ k, c k • P.vη k    = (c 0 : O) + c 1 * P.η₁ + c 2 * P.η₂ + c 3 * P.η₃ := by  simp [Fin.sum_univ_four, vη, zsmul_eq_mul] /-- 2α = Σ n_j η_j + s with s ∈ ℤ (proof of prop:discA). -/theorem two_x0 : ∃ s : ℤ, 2 * M.x 0 = s + P.n₁ * P.η₁ + P.n₂ * P.η₂ + P.n₃ * P.η₃ := by  obtain ⟨s, hs⟩ := half_int M (k := P.a - (P.n₁ * P.e₁ + P.n₂ * P.e₂ + P.n₃ * P.e₃))    (w := 2 * M.x 0 - (P.n₁ * P.η₁ + P.n₂ * P.η₂ + P.n₃ * P.η₃)) (by      have := P.four_x0      rw [← P.two_η₁, ← P.two_η₂, ← P.two_η₃] at this      push_cast; linear_combination -this)  exact ⟨s, by linear_combination hs⟩ /-- S_j = n_j η_j + s_j, Q_j = t_j η_j + s'_j (`lem:Lambda`: C_j = ℤ + ℤη_j). -/theorem S₁_eq : ∃ s : ℤ, M.x 0 + M.x 1 = P.n₁ * P.η₁ + s := by  obtain ⟨s, hs⟩ := half_int M (k := P.a - P.n₁ * P.e₁) (w := M.x 0 + M.x 1 - P.n₁ * P.η₁) (by    have h : 2 * (M.x 0 + M.x 1) = ea M.x + r1 M.x := by simp only [ea, r1]; ring    rw [P.ha, P.hr₁, ← P.two_η₁] at h    push_cast; linear_combination -h)  exact ⟨s, by linear_combination hs⟩theorem S₂_eq : ∃ s : ℤ, M.x 0 + M.x 2 = P.n₂ * P.η₂ + s := by  obtain ⟨s, hs⟩ := half_int M (k := P.a - P.n₂ * P.e₂) (w := M.x 0 + M.x 2 - P.n₂ * P.η₂) (by    have h : 2 * (M.x 0 + M.x 2) = ea M.x + r2 M.x := by simp only [ea, r2]; ring    rw [P.ha, P.hr₂, ← P.two_η₂] at h    push_cast; linear_combination -h)  exact ⟨s, by linear_combination hs⟩theorem S₃_eq : ∃ s : ℤ, M.x 0 + M.x 3 = P.n₃ * P.η₃ + s := by  obtain ⟨s, hs⟩ := half_int M (k := P.a - P.n₃ * P.e₃) (w := M.x 0 + M.x 3 - P.n₃ * P.η₃) (by    have h : 2 * (M.x 0 + M.x 3) = ea M.x + r3 M.x := by simp only [ea, r3]; ring    rw [P.ha, P.hr₃, ← P.two_η₃] at h    push_cast; linear_combination -h)  exact ⟨s, by linear_combination hs⟩theorem Q₁_eq : ∃ s : ℤ, M.x 0 * M.x 1 = P.t₁ * P.η₁ + s := by  obtain ⟨s, hs⟩ := half_int M (k := P.z₁ - P.t₁ * P.e₁) (w := M.x 0 * M.x 1 - P.t₁ * P.η₁) (by    have h : 2 * (M.x 0 * M.x 1) = z1 M.x + dQ1 M.x := by simp only [z1, dQ1]; ring    rw [P.hz₁, P.hq₁, ← P.two_η₁] at h    push_cast; linear_combination -h)  exact ⟨s, by linear_combination hs⟩theorem Q₂_eq : ∃ s : ℤ, M.x 0 * M.x 2 = P.t₂ * P.η₂ + s := by  obtain ⟨s, hs⟩ := half_int M (k := P.z₂ - P.t₂ * P.e₂) (w := M.x 0 * M.x 2 - P.t₂ * P.η₂) (by    have h : 2 * (M.x 0 * M.x 2) = z2 M.x + dQ2 M.x := by simp only [z2, dQ2]; ring    rw [P.hz₂, P.hq₂, ← P.two_η₂] at h    push_cast; linear_combination -h)  exact ⟨s, by linear_combination hs⟩theorem Q₃_eq : ∃ s : ℤ, M.x 0 * M.x 3 = P.t₃ * P.η₃ + s := by  obtain ⟨s, hs⟩ := half_int M (k := P.z₃ - P.t₃ * P.e₃) (w := M.x 0 * M.x 3 - P.t₃ * P.η₃) (by    have h : 2 * (M.x 0 * M.x 3) = z3 M.x + dQ3 M.x := by simp only [z3, dQ3]; ring    rw [P.hz₃, P.hq₃, ← P.two_η₃] at h    push_cast; linear_combination -h)  exact ⟨s, by linear_combination hs⟩ /-- n_j Q_j = t_j S_j + ℓ_j with ℓ_j ∈ ℤ (n_j ϑ_j and t_j ϑ_j give r_j t_j = q_j n_j). -/theorem nQ₁ : ∃ l : ℤ, P.n₁ * (M.x 0 * M.x 1) = P.t₁ * (M.x 0 + M.x 1) + l := by  obtain ⟨l, hl⟩ := half_int M (k := P.n₁ * P.z₁ - P.t₁ * P.a)    (w := P.n₁ * (M.x 0 * M.x 1) - P.t₁ * (M.x 0 + M.x 1)) (by      have h1 : 2 * (M.x 0 * M.x 1) = z1 M.x + dQ1 M.x := by simp only [z1, dQ1]; ring      have h2 : 2 * (M.x 0 + M.x 1) = ea M.x + r1 M.x := by simp only [ea, r1]; ring      rw [P.hz₁, P.hq₁] at h1      rw [P.ha, P.hr₁] at h2      push_cast; linear_combination (P.t₁ : O) * h2 - (P.n₁ : O) * h1)  exact ⟨l, by linear_combination hl⟩theorem nQ₂ : ∃ l : ℤ, P.n₂ * (M.x 0 * M.x 2) = P.t₂ * (M.x 0 + M.x 2) + l := by  obtain ⟨l, hl⟩ := half_int M (k := P.n₂ * P.z₂ - P.t₂ * P.a)    (w := P.n₂ * (M.x 0 * M.x 2) - P.t₂ * (M.x 0 + M.x 2)) (by      have h1 : 2 * (M.x 0 * M.x 2) = z2 M.x + dQ2 M.x := by simp only [z2, dQ2]; ring      have h2 : 2 * (M.x 0 + M.x 2) = ea M.x + r2 M.x := by simp only [ea, r2]; ring      rw [P.hz₂, P.hq₂] at h1      rw [P.ha, P.hr₂] at h2      push_cast; linear_combination (P.t₂ : O) * h2 - (P.n₂ : O) * h1)  exact ⟨l, by linear_combination hl⟩theorem nQ₃ : ∃ l : ℤ, P.n₃ * (M.x 0 * M.x 3) = P.t₃ * (M.x 0 + M.x 3) + l := by  obtain ⟨l, hl⟩ := half_int M (k := P.n₃ * P.z₃ - P.t₃ * P.a)    (w := P.n₃ * (M.x 0 * M.x 3) - P.t₃ * (M.x 0 + M.x 3)) (by      have h1 : 2 * (M.x 0 * M.x 3) = z3 M.x + dQ3 M.x := by simp only [z3, dQ3]; ring      have h2 : 2 * (M.x 0 + M.x 3) = ea M.x + r3 M.x := by simp only [ea, r3]; ring      rw [P.hz₃, P.hq₃] at h1      rw [P.ha, P.hr₃] at h2      push_cast; linear_combination (P.t₃ : O) * h2 - (P.n₃ : O) * h1)  exact ⟨l, by linear_combination hl⟩ end Dat /-! ### Case I: some n_i odd, A = L + ℤα -/ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) theorem int_N (y : O) (k : ℤ) : (k : O) ∈ P.Nℓ y := by  simpa using smul_mem' _ k (P.Lℓ_le_N y P.one_Lℓ)theorem η₁_N (y : O) : P.η₁ ∈ P.Nℓ y := P.Lℓ_le_N y P.η₁_Lℓtheorem η₂_N (y : O) : P.η₂ ∈ P.Nℓ y := P.Lℓ_le_N y P.η₂_Lℓtheorem η₃_N (y : O) : P.η₃ ∈ P.Nℓ y := P.Lℓ_le_N y P.η₃_Lℓ /-- In N = L + ℤα: all x_m and all products x_a x_b (squares included) lie in N. -/theorem I_x (m : Fin 4) : M.x m ∈ P.Nℓ (M.x 0) := by  have N := P.Nℓ (M.x 0)  have sm := smul_mem' (P.Nℓ (M.x 0))  have hx0 := P.y_mem_N (M.x 0)  obtain ⟨s₁, h₁⟩ := P.S₁_eq  obtain ⟨s₂, h₂⟩ := P.S₂_eq  obtain ⟨s₃, h₃⟩ := P.S₃_eq  fin_cases m  · exact hx0  · show M.x 1 ∈ _    rw [show M.x 1 = (M.x 0 + M.x 1) - M.x 0 by ring, h₁]    exact sub_mem (add_mem (sm _ (P.η₁_N _)) (P.int_N _ _)) hx0  · show M.x 2 ∈ _    rw [show M.x 2 = (M.x 0 + M.x 2) - M.x 0 by ring, h₂]    exact sub_mem (add_mem (sm _ (P.η₂_N _)) (P.int_N _ _)) hx0  · show M.x 3 ∈ _    rw [show M.x 3 = (M.x 0 + M.x 3) - M.x 0 by ring, h₃]    exact sub_mem (add_mem (sm _ (P.η₃_N _)) (P.int_N _ _)) hx0 theorem I_pairs : M.x 0 * M.x 1 ∈ P.Nℓ (M.x 0) ∧ M.x 0 * M.x 2 ∈ P.Nℓ (M.x 0) ∧    M.x 0 * M.x 3 ∈ P.Nℓ (M.x 0) ∧ M.x 2 * M.x 3 ∈ P.Nℓ (M.x 0) ∧    M.x 1 * M.x 3 ∈ P.Nℓ (M.x 0) ∧ M.x 1 * M.x 2 ∈ P.Nℓ (M.x 0) := by  have sm := smul_mem' (P.Nℓ (M.x 0))  obtain ⟨s₁, h₁⟩ := P.Q₁_eq  obtain ⟨s₂, h₂⟩ := P.Q₂_eq  obtain ⟨s₃, h₃⟩ := P.Q₃_eq  have q1 : M.x 0 * M.x 1 ∈ P.Nℓ (M.x 0) := by    rw [h₁]; exact add_mem (sm _ (P.η₁_N _)) (P.int_N _ _)  have q2 : M.x 0 * M.x 2 ∈ P.Nℓ (M.x 0) := by    rw [h₂]; exact add_mem (sm _ (P.η₂_N _)) (P.int_N _ _)  have q3 : M.x 0 * M.x 3 ∈ P.Nℓ (M.x 0) := by    rw [h₃]; exact add_mem (sm _ (P.η₃_N _)) (P.int_N _ _)  refine ⟨q1, q2, q3, ?_, ?_, ?_⟩  · rw [show M.x 2 * M.x 3 = z1 M.x - M.x 0 * M.x 1 by simp only [z1]; ring, P.hz₁]    exact sub_mem (P.int_N _ _) q1  · rw [show M.x 1 * M.x 3 = z2 M.x - M.x 0 * M.x 2 by simp only [z2]; ring, P.hz₂]    exact sub_mem (P.int_N _ _) q2  · rw [show M.x 1 * M.x 2 = z3 M.x - M.x 0 * M.x 3 by simp only [z3]; ring, P.hz₃]    exact sub_mem (P.int_N _ _) q3 theorem I_deg2 (a b : Fin 4) : M.x a * M.x b ∈ P.Nℓ (M.x 0) := by  obtain ⟨p01, p02, p03, p23, p13, p12⟩ := P.I_pairs  have sm := smul_mem' (P.Nℓ (M.x 0))  have hx := P.I_x  have sq : ∀ m : Fin 4, M.x m * M.x m = ea M.x * M.x m - M.x m * (ea M.x - M.x m) := by    intro m; ring  have e0 : M.x 0 * M.x 0 = P.a * M.x 0 - (M.x 0 * M.x 1 + M.x 0 * M.x 2 + M.x 0 * M.x 3) := by    rw [← P.ha]; simp only [ea]; ring  have e1 : M.x 1 * M.x 1 = P.a * M.x 1 - (M.x 0 * M.x 1 + M.x 1 * M.x 2 + M.x 1 * M.x 3) := by    rw [← P.ha]; simp only [ea]; ring  have e2 : M.x 2 * M.x 2 = P.a * M.x 2 - (M.x 0 * M.x 2 + M.x 1 * M.x 2 + M.x 2 * M.x 3) := by    rw [← P.ha]; simp only [ea]; ring  have e3 : M.x 3 * M.x 3 = P.a * M.x 3 - (M.x 0 * M.x 3 + M.x 1 * M.x 3 + M.x 2 * M.x 3) := by    rw [← P.ha]; simp only [ea]; ring  clear sq  fin_cases a <;> fin_cases b <;> simp only [Fin.zero_eta, Fin.mk_one, Fin.reduceFinMk]  · rw [e0]; exact sub_mem (sm _ (hx 0)) (add_mem (add_mem p01 p02) p03)  · exact p01  · exact p02  · exact p03  · rw [mul_comm]; exact p01  · rw [e1]; exact sub_mem (sm _ (hx 1)) (add_mem (add_mem p01 p12) p13)  · exact p12  · exact p13  · rw [mul_comm]; exact p02  · rw [mul_comm]; exact p12  · rw [e2]; exact sub_mem (sm _ (hx 2)) (add_mem (add_mem p02 p12) p23)  · exact p23  · rw [mul_comm]; exact p03  · rw [mul_comm]; exact p13  · rw [mul_comm]; exact p23  · rw [e3]; exact sub_mem (sm _ (hx 3)) (add_mem (add_mem p03 p13) p23) /-- From the four triple products to all products x_m·g, g ∈ {1, η_j, α} (Case I). -/theorem I_odd_of_triples {s : ℤ}    (T012 : (s : O) * (M.x 0 * M.x 1 * M.x 2) ∈ P.Nℓ (M.x 0))    (T013 : (s : O) * (M.x 0 * M.x 1 * M.x 3) ∈ P.Nℓ (M.x 0))    (T023 : (s : O) * (M.x 0 * M.x 2 * M.x 3) ∈ P.Nℓ (M.x 0))    (T123 : (s : O) * (M.x 1 * M.x 2 * M.x 3) ∈ P.Nℓ (M.x 0)) :    ∀ m, ∀ g ∈ Set.range P.vη ∪ {M.x 0}, (s : O) * (M.x m * g) ∈ P.Nℓ (M.x 0) := by  have sm := smul_mem' (P.Nℓ (M.x 0))  have d2 := P.I_deg2  have hx := P.I_x  -- s·x_m·(x₀x_j) ∈ N for all m and j = 1, 2, 3  have cub : ∀ m : Fin 4, (s : O) * (M.x m * (M.x 0 * M.x 1)) ∈ P.Nℓ (M.x 0) ∧      (s : O) * (M.x m * (M.x 0 * M.x 2)) ∈ P.Nℓ (M.x 0) ∧      (s : O) * (M.x m * (M.x 0 * M.x 3)) ∈ P.Nℓ (M.x 0) := by    have z1e : M.x 0 * M.x 1 = P.z₁ - M.x 2 * M.x 3 := by rw [← P.hz₁]; simp only [z1]; ring    have z2e : M.x 0 * M.x 2 = P.z₂ - M.x 1 * M.x 3 := by rw [← P.hz₂]; simp only [z2]; ring    have z3e : M.x 0 * M.x 3 = P.z₃ - M.x 1 * M.x 2 := by rw [← P.hz₃]; simp only [z3]; ring    have lin : ∀ (k : ℤ) (m : Fin 4), (s : O) * (k * M.x m) ∈ P.Nℓ (M.x 0) := by      intro k m; have := sm (s * k) (hx m); push_cast at this; rw [← mul_assoc]; exact this    intro m    fin_cases m <;> simp only [Fin.zero_eta, Fin.mk_one, Fin.reduceFinMk]    · refine ⟨?_, ?_, ?_⟩      · rw [z1e, show (s : O) * (M.x 0 * (P.z₁ - M.x 2 * M.x 3))          = s * (P.z₁ * M.x 0) - s * (M.x 0 * M.x 2 * M.x 3) by ring]        exact sub_mem (lin _ 0) T023      · rw [z2e, show (s : O) * (M.x 0 * (P.z₂ - M.x 1 * M.x 3))          = s * (P.z₂ * M.x 0) - s * (M.x 0 * M.x 1 * M.x 3) by ring]        exact sub_mem (lin _ 0) T013      · rw [z3e, show (s : O) * (M.x 0 * (P.z₃ - M.x 1 * M.x 2))          = s * (P.z₃ * M.x 0) - s * (M.x 0 * M.x 1 * M.x 2) by ring]        exact sub_mem (lin _ 0) T012    · refine ⟨?_, ?_, ?_⟩      · rw [z1e, show (s : O) * (M.x 1 * (P.z₁ - M.x 2 * M.x 3))          = s * (P.z₁ * M.x 1) - s * (M.x 1 * M.x 2 * M.x 3) by ring]        exact sub_mem (lin _ 1) T123      · rw [show (s : O) * (M.x 1 * (M.x 0 * M.x 2)) = s * (M.x 0 * M.x 1 * M.x 2) by ring]        exact T012      · rw [show (s : O) * (M.x 1 * (M.x 0 * M.x 3)) = s * (M.x 0 * M.x 1 * M.x 3) by ring]        exact T013    · refine ⟨?_, ?_, ?_⟩      · rw [show (s : O) * (M.x 2 * (M.x 0 * M.x 1)) = s * (M.x 0 * M.x 1 * M.x 2) by ring]        exact T012      · rw [z2e, show (s : O) * (M.x 2 * (P.z₂ - M.x 1 * M.x 3))          = s * (P.z₂ * M.x 2) - s * (M.x 1 * M.x 2 * M.x 3) by ring]        exact sub_mem (lin _ 2) T123      · rw [show (s : O) * (M.x 2 * (M.x 0 * M.x 3)) = s * (M.x 0 * M.x 2 * M.x 3) by ring]        exact T023    · refine ⟨?_, ?_, ?_⟩      · rw [show (s : O) * (M.x 3 * (M.x 0 * M.x 1)) = s * (M.x 0 * M.x 1 * M.x 3) by ring]        exact T013      · rw [show (s : O) * (M.x 3 * (M.x 0 * M.x 2)) = s * (M.x 0 * M.x 2 * M.x 3) by ring]        exact T023      · rw [z3e, show (s : O) * (M.x 3 * (P.z₃ - M.x 1 * M.x 2))          = s * (P.z₃ * M.x 3) - s * (M.x 1 * M.x 2 * M.x 3) by ring]        exact sub_mem (lin _ 3) T123  intro m g hg  rcases hg with ⟨k, rfl⟩ | hg  · fin_cases k    · simpa [vη] using sm s (hx m)    · show (s : O) * (M.x m * P.η₁) ∈ _      rw [show (s : O) * (M.x m * P.η₁) = (s * P.u₁ : ℤ) * (M.x m * M.x 0 + M.x m * M.x 1)        + P.v₁ * (s * (M.x m * (M.x 0 * M.x 1))) by simp only [η₁]; push_cast; ring]      exact add_mem (sm _ (add_mem (d2 m 0) (d2 m 1))) (sm _ (cub m).1)    · show (s : O) * (M.x m * P.η₂) ∈ _      rw [show (s : O) * (M.x m * P.η₂) = (s * P.u₂ : ℤ) * (M.x m * M.x 0 + M.x m * M.x 2)        + P.v₂ * (s * (M.x m * (M.x 0 * M.x 2))) by simp only [η₂]; push_cast; ring]      exact add_mem (sm _ (add_mem (d2 m 0) (d2 m 2))) (sm _ (cub m).2.1)    · show (s : O) * (M.x m * P.η₃) ∈ _      rw [show (s : O) * (M.x m * P.η₃) = (s * P.u₃ : ℤ) * (M.x m * M.x 0 + M.x m * M.x 3)        + P.v₃ * (s * (M.x m * (M.x 0 * M.x 3))) by simp only [η₃]; push_cast; ring]      exact add_mem (sm _ (add_mem (d2 m 0) (d2 m 3))) (sm _ (cub m).2.2)  · rw [Set.mem_singleton_iff.mp hg]    exact sm s (d2 m 0) /-- n Q = t S + ℓ gives n·x_m·Q ∈ N and n·x_m·(z − Q) ∈ N. -/theorem I_xQ {n t l : ℤ} {i : Fin 4} (h : (n : O) * (M.x 0 * M.x i) = t * (M.x 0 + M.x i) + l)    (m : Fin 4) : (n : O) * (M.x m * (M.x 0 * M.x i)) ∈ P.Nℓ (M.x 0) ∧      ∀ z : ℤ, (n : O) * (M.x m * (z - M.x 0 * M.x i)) ∈ P.Nℓ (M.x 0) := by  have sm := smul_mem' (P.Nℓ (M.x 0))  have h1 : (n : O) * (M.x m * (M.x 0 * M.x i)) = t * (M.x m * M.x 0 + M.x m * M.x i) + l * M.x m := by    linear_combination (M.x m) * h  refine ⟨?_, fun z => ?_⟩  · rw [h1]; exact add_mem (sm _ (add_mem (P.I_deg2 m 0) (P.I_deg2 m i))) (sm _ (P.I_x m))  · rw [show (n : O) * (M.x m * (z - M.x 0 * M.x i))      = (n * z : ℤ) * M.x m - n * (M.x m * (M.x 0 * M.x i)) by push_cast; ring, h1]    exact sub_mem (sm _ (P.I_x m))      (add_mem (sm _ (add_mem (P.I_deg2 m 0) (P.I_deg2 m i))) (sm _ (P.I_x m))) /-- Case I of prop:discA: if some n_i is odd, A ⊆ L + ℤα. -/theorem caseI_A_le (hodd : ¬ Even P.n₁ ∨ ¬ Even P.n₂ ∨ ¬ Even P.n₃) :    ∀ z ∈ Aring M, z ∈ P.Nℓ (M.x 0) := by  have z1e : P.z₁ - M.x 0 * M.x 1 = M.x 2 * M.x 3 := by rw [← P.hz₁]; simp only [z1]; ring  have z2e : P.z₂ - M.x 0 * M.x 2 = M.x 1 * M.x 3 := by rw [← P.hz₂]; simp only [z2]; ring  have z3e : P.z₃ - M.x 0 * M.x 3 = M.x 1 * M.x 2 := by rw [← P.hz₃]; simp only [z3]; ring  -- the odd multiplier s and the four triples  obtain ⟨s, hs, T012, T013, T023, T123⟩ : ∃ s : ℤ, ¬ Even s ∧      (s : O) * (M.x 0 * M.x 1 * M.x 2) ∈ P.Nℓ (M.x 0) ∧      (s : O) * (M.x 0 * M.x 1 * M.x 3) ∈ P.Nℓ (M.x 0) ∧      (s : O) * (M.x 0 * M.x 2 * M.x 3) ∈ P.Nℓ (M.x 0) ∧      (s : O) * (M.x 1 * M.x 2 * M.x 3) ∈ P.Nℓ (M.x 0) := by    rcases hodd with h | h | h    · obtain ⟨l, hl⟩ := P.nQ₁      refine ⟨P.n₁, h, ?_, ?_, ?_, ?_⟩      · rw [show M.x 0 * M.x 1 * M.x 2 = M.x 2 * (M.x 0 * M.x 1) by ring]        exact (P.I_xQ hl 2).1      · rw [show M.x 0 * M.x 1 * M.x 3 = M.x 3 * (M.x 0 * M.x 1) by ring]        exact (P.I_xQ hl 3).1      · rw [show M.x 0 * M.x 2 * M.x 3 = M.x 0 * (P.z₁ - M.x 0 * M.x 1) by rw [z1e]; ring]        exact (P.I_xQ hl 0).2 _      · rw [show M.x 1 * M.x 2 * M.x 3 = M.x 1 * (P.z₁ - M.x 0 * M.x 1) by rw [z1e]; ring]        exact (P.I_xQ hl 1).2 _    · obtain ⟨l, hl⟩ := P.nQ₂      refine ⟨P.n₂, h, ?_, ?_, ?_, ?_⟩      · rw [show M.x 0 * M.x 1 * M.x 2 = M.x 1 * (M.x 0 * M.x 2) by ring]        exact (P.I_xQ hl 1).1      · rw [show M.x 0 * M.x 1 * M.x 3 = M.x 0 * (P.z₂ - M.x 0 * M.x 2) by rw [z2e]; ring]        exact (P.I_xQ hl 0).2 _      · rw [show M.x 0 * M.x 2 * M.x 3 = M.x 3 * (M.x 0 * M.x 2) by ring]        exact (P.I_xQ hl 3).1      · rw [show M.x 1 * M.x 2 * M.x 3 = M.x 2 * (P.z₂ - M.x 0 * M.x 2) by rw [z2e]; ring]        exact (P.I_xQ hl 2).2 _    · obtain ⟨l, hl⟩ := P.nQ₃      refine ⟨P.n₃, h, ?_, ?_, ?_, ?_⟩      · rw [show M.x 0 * M.x 1 * M.x 2 = M.x 0 * (P.z₃ - M.x 0 * M.x 3) by rw [z3e]; ring]        exact (P.I_xQ hl 0).2 _      · rw [show M.x 0 * M.x 1 * M.x 3 = M.x 1 * (M.x 0 * M.x 3) by ring]        exact (P.I_xQ hl 1).1      · rw [show M.x 0 * M.x 2 * M.x 3 = M.x 2 * (M.x 0 * M.x 3) by ring]        exact (P.I_xQ hl 2).1      · rw [show M.x 1 * M.x 2 * M.x 3 = M.x 3 * (P.z₃ - M.x 0 * M.x 3) by rw [z3e]; ring]        exact (P.I_xQ hl 3).2 _  exact P.A_le_N (x_mem_A M 0) hs (P.I_odd_of_triples T012 T013 T023 T123) /-- Case I of prop:discA: if some n_i is odd, A = L + ℤα with 2α ∈ L, α ∉ L. -/theorem caseI (hodd : ¬ Even P.n₁ ∨ ¬ Even P.n₂ ∨ ¬ Even P.n₃) :    M.x 0 ∈ Aring M ∧ 2 * M.x 0 ∈ P.Lℓ ∧ M.x 0 ∉ P.Lℓ ∧ ∀ z ∈ Aring M, z ∈ P.Nℓ (M.x 0) := by  have hA := P.caseI_A_le hodd  obtain ⟨s0, h2⟩ := P.two_x0  have h2L : 2 * M.x 0 ∈ P.Lℓ := by    rw [P.mem_Lℓ_iff]    exact ⟨![s0, P.n₁, P.n₂, P.n₃], by rw [P.sum_vη, h2]; simp⟩  have hnL : M.x 0 ∉ P.Lℓ := by    intro hx    obtain ⟨c, hc⟩ := P.mem_Lℓ_iff.mp hx    have hcd := P.coeff_η (c := fun k => 2 * c k) (d := ![s0, P.n₁, P.n₂, P.n₃]) (by      rw [P.sum_vη, P.sum_vη]; rw [P.sum_vη] at hc      simp only [Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val]      push_cast      linear_combination 2 * hc + h2)    have e1 := congrFun hcd 1    have e2 := congrFun hcd 2    have e3 := congrFun hcd 3    simp only [Matrix.cons_val_one, Matrix.cons_val] at e1 e2 e3    rcases hodd with h | h | h    · exact h ⟨c 1, by linarith⟩    · exact h ⟨c 2, by linarith⟩    · exact h ⟨c 3, by linarith⟩  exact ⟨x_mem_A M 0, h2L, hnL, hA⟩ end Dat /-! ### Case II: all n_j even, A = L + ℤ η_i η_j -/ /-- Products in L·N for the configuration (i, j, k) with c η_k = 2m_im_j η_iη_j − … (an odd    multiple of every product lands in N = L + ℤ η_iη_j). Abstract over a commutative ring. -/theorem caseII_odd {R : Type*} [CommRing R] (N : Submodule ℤ R) {ηi ηj ηk : R}    {ei ej ek Ci Cj Ck mi mj c K : ℤ}    (hi : ηi * ηi = ei * ηi + Ci) (hj : ηj * ηj = ej * ηj + Cj) (hk : ηk * ηk = ek * ηk + Ck)    (hE : (c : R) * ηk = 2 * mi * mj * (ηi * ηj) - mi * mj * ej * ηi - mi * mj * ei * ηj + K)    (n1 : (1 : R) ∈ N) (ni : ηi ∈ N) (nj : ηj ∈ N) (nk : ηk ∈ N) (nij : ηi * ηj ∈ N) :    ∀ a ∈ ({1, ηi, ηj, ηk} : Set R), ∀ g ∈ ({1, ηi, ηj, ηk, ηi * ηj} : Set R),      (c : R) * (a * g) ∈ N := by  have sm : ∀ (n : ℤ) {y : R}, y ∈ N → (n : R) * y ∈ N := fun n y hy => by    rw [← zsmul_eq_mul]; exact N.smul_mem n hy  have comb : ∀ k0 k1 k2 k3 k4 : ℤ,      (k0 : R) + k1 * ηi + k2 * ηj + k3 * ηk + k4 * (ηi * ηj) ∈ N := by    intro k0 k1 k2 k3 k4    exact add_mem (add_mem (add_mem (add_mem (by simpa using sm k0 n1) (sm k1 ni)) (sm k2 nj))      (sm k3 nk)) (sm k4 nij)  intro a ha g hg  simp only [Set.mem_insert_iff, Set.mem_singleton_iff] at ha hg  rcases ha with h | h | h | h <;> rcases hg with h' | h' | h' | h' | h' <;> rw [h, h']  -- a = 1  · have := comb c 0 0 0 0; convert this using 1; push_cast; ring  · have := comb 0 c 0 0 0; convert this using 1; push_cast; ring  · have := comb 0 0 c 0 0; convert this using 1; push_cast; ring  · have := comb 0 0 0 c 0; convert this using 1; push_cast; ring  · have := comb 0 0 0 0 c; convert this using 1; push_cast; ring  -- a = η_i  · have := comb 0 c 0 0 0; convert this using 1; push_cast; ring  · have := comb (c * Ci) (c * ei) 0 0 0; convert this using 1; push_cast    linear_combination (c : R) * hi  · have := comb 0 0 0 0 c; convert this using 1; push_cast; ring  · have := comb (-(mi * mj * ej * Ci)) (K - mi * mj * ei * ej) (2 * mi * mj * Ci) 0 (mi * mj * ei)    convert this using 1; push_cast    linear_combination ηi * hE + (2 * (mi : R) * mj * ηj - mi * mj * ej) * hi  · have := comb 0 0 (c * Ci) 0 (c * ei); convert this using 1; push_cast    linear_combination (c : R) * ηj * hi  -- a = η_j  · have := comb 0 0 c 0 0; convert this using 1; push_cast; ring  · have := comb 0 0 0 0 c; convert this using 1; push_cast; ring  · have := comb (c * Cj) 0 (c * ej) 0 0; convert this using 1; push_cast    linear_combination (c : R) * hj  · have := comb (-(mi * mj * ei * Cj)) (2 * mi * mj * Cj) (K - mi * mj * ei * ej) 0 (mi * mj * ej)    convert this using 1; push_cast    linear_combination ηj * hE + (2 * (mi : R) * mj * ηi - mi * mj * ei) * hj  · have := comb 0 (c * Cj) 0 0 (c * ej); convert this using 1; push_cast    linear_combination (c : R) * ηi * hj  -- a = η_k  · have := comb 0 0 0 c 0; convert this using 1; push_cast; ring  · have := comb (-(mi * mj * ej * Ci)) (K - mi * mj * ei * ej) (2 * mi * mj * Ci) 0 (mi * mj * ei)    convert this using 1; push_cast    linear_combination ηi * hE + (2 * (mi : R) * mj * ηj - mi * mj * ej) * hi  · have := comb (-(mi * mj * ei * Cj)) (2 * mi * mj * Cj) (K - mi * mj * ei * ej) 0 (mi * mj * ej)    convert this using 1; push_cast    linear_combination ηj * hE + (2 * (mi : R) * mj * ηi - mi * mj * ei) * hj  · have := comb (c * Ck) 0 0 (c * ek) 0; convert this using 1; push_cast    linear_combination (c : R) * hk  · have := comb (2 * mi * mj * Ci * Cj) (mi * mj * ei * Cj) (mi * mj * Ci * ej) 0 K    convert this using 1; push_cast    linear_combination (ηi * ηj) * hE + (2 * (mi : R) * mj * ηj * ηj - mi * mj * ej * ηj) * hi      + (2 * (mi : R) * mj * (ei * ηi + Ci) - mi * mj * ei * ηi) * hj /-- η_iη_j ∉ ℤ + ℤη_i + ℤη_j + ℤη_k when c is odd (comparing η_k-coordinates). -/theorem caseII_notin {R : Type*} [CommRing R] {ηi ηj ηk : R} {ei ej mi mj c K : ℤ}    (hli : LinearIndependent ℤ ![1, ηi, ηj, ηk])    (hE : (c : R) * ηk = 2 * mi * mj * (ηi * ηj) - mi * mj * ej * ηi - mi * mj * ei * ηj + K)    (hc : ¬ Even c) : ηi * ηj ∉ Submodule.span ℤ (Set.range ![1, ηi, ηj, ηk]) := by  intro hmem  obtain ⟨d, hd⟩ := (Submodule.mem_span_range_iff_exists_fun ℤ).mp hmem  simp only [Fin.sum_univ_four, Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val,    zsmul_eq_mul, mul_one] at hd  have h0 := Fintype.linearIndependent_iff.mp hli    ![2 * mi * mj * d 0 + K, 2 * mi * mj * d 1 - mi * mj * ej, 2 * mi * mj * d 2 - mi * mj * ei,      2 * mi * mj * d 3 - c] (by      simp only [Fin.sum_univ_four, Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val,        zsmul_eq_mul, mul_one]      push_cast      linear_combination (2 * (mi : R) * mj) * hd - hE)  have h3 := h0 3  simp only [Matrix.cons_val] at h3  exact hc ⟨mi * mj * d 3, by linarith⟩ /-- An element w ∈ O with g·w ∈ ℤ (g ≠ 0) is an integer. -/theorem div_int {O : Type*} [CommRing O] [IsDomain O] [CharZero O] (M : V4Data O) {g k : ℤ}    (hg : g ≠ 0) {w : O} (h : (k : O) = g * w) : ∃ m : ℤ, w = m := by  obtain ⟨m, rfl⟩ : g ∣ k := M.sat hg h  refine ⟨m, ?_⟩  have h' : (g : O) * w = g * m := by rw [← h]; push_cast; ring  exact mul_left_cancel₀ (Int.cast_ne_zero.mpr hg) h' /-- η² = eη + C with C = (Δ − e²)/4 ∈ ℤ, when ϑ = 2η − e and ϑ² = Δ (C_j = ℤ[η_j] is a ring). -/theorem eta_sq {O : Type*} [CommRing O] [IsDomain O] [CharZero O] (M : V4Data O) {η ϑ : O}    {e Δ : ℤ} (hη : 2 * η - e = ϑ) (hΔ : ϑ * ϑ = Δ) : ∃ C : ℤ, η * η = e * η + C := by  have key : ((Δ - e ^ 2 : ℤ) : O) = (4 : ℤ) * (η * η - e * η) := by    push_cast; rw [← hΔ, ← hη]; ring  obtain ⟨C, hC⟩ := div_int M (by norm_num) key  exact ⟨C, by linear_combination hC⟩ theorem range_four {R : Type*} (a b c d : R) : Set.range ![a, b, c, d] = {a, b, c, d} := by  ext z  constructor  · rintro ⟨k, rfl⟩    fin_cases k <;> simp  · rintro (rfl | rfl | rfl | rfl)    exacts [⟨0, rfl⟩, ⟨1, rfl⟩, ⟨2, rfl⟩, ⟨3, rfl⟩] /-- From m_i m_j ϑ_i ϑ_j = c ϑ_k and ϑ = 2η − e: c η_k = 2m_im_j η_iη_j − m_im_j e_j η_i    − m_im_j e_i η_j + K with K ∈ ℤ (proof of prop:discA, all-even case). -/theorem eta_rel {O : Type*} [CommRing O] [IsDomain O] [CharZero O] (M : V4Data O)    {ηi ηj ηk ϑi ϑj ϑk : O} {ei ej ek mi mj c : ℤ}    (hi : 2 * ηi - ei = ϑi) (hj : 2 * ηj - ej = ϑj) (hk : 2 * ηk - ek = ϑk)    (h : (mi * mj : O) * (ϑi * ϑj) = c * ϑk) :    ∃ K : ℤ, (c : O) * ηk = 2 * mi * mj * (ηi * ηj) - mi * mj * ej * ηi - mi * mj * ei * ηj + K := by  obtain ⟨K, hK⟩ := half_int M (k := mi * mj * ei * ej + c * ek)    (w := c * ηk - 2 * mi * mj * (ηi * ηj) + mi * mj * ej * ηi + mi * mj * ei * ηj) (by      rw [← hi, ← hj, ← hk] at h      push_cast; linear_combination h)  exact ⟨K, by linear_combination hK⟩ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- If all n_j are even, every conjugate lies in L (α = s + Σ m_j η_j). -/theorem x_in_L (h1 : 2 ∣ P.n₁) (h2 : 2 ∣ P.n₂) (h3 : 2 ∣ P.n₃) (m : Fin 4) : M.x m ∈ P.Lℓ := by  obtain ⟨m₁, hm₁⟩ := h1  obtain ⟨m₂, hm₂⟩ := h2  obtain ⟨m₃, hm₃⟩ := h3  have sm := smul_mem' P.Lℓ  obtain ⟨s0, hs0⟩ := P.two_x0  obtain ⟨k, hk⟩ := half_int M (k := s0) (w := M.x 0 - (m₁ * P.η₁ + m₂ * P.η₂ + m₃ * P.η₃)) (by    rw [hm₁, hm₂, hm₃] at hs0; push_cast at hs0; linear_combination -hs0)  have hx0 : M.x 0 ∈ P.Lℓ := by    rw [show M.x 0 = (k : O) + m₁ * P.η₁ + m₂ * P.η₂ + m₃ * P.η₃ by linear_combination hk]    exact add_mem (add_mem (add_mem (by simpa using sm k P.one_Lℓ) (sm _ P.η₁_Lℓ))      (sm _ P.η₂_Lℓ)) (sm _ P.η₃_Lℓ)  obtain ⟨s₁, e₁⟩ := P.S₁_eq  obtain ⟨s₂, e₂⟩ := P.S₂_eq  obtain ⟨s₃, e₃⟩ := P.S₃_eq  fin_cases m  · exact hx0  · show M.x 1 ∈ _    rw [show M.x 1 = (M.x 0 + M.x 1) - M.x 0 by ring, e₁]    exact sub_mem (add_mem (sm _ P.η₁_Lℓ) (by simpa using sm s₁ P.one_Lℓ)) hx0  · show M.x 2 ∈ _    rw [show M.x 2 = (M.x 0 + M.x 2) - M.x 0 by ring, e₂]    exact sub_mem (add_mem (sm _ P.η₂_Lℓ) (by simpa using sm s₂ P.one_Lℓ)) hx0  · show M.x 3 ∈ _    rw [show M.x 3 = (M.x 0 + M.x 3) - M.x 0 by ring, e₃]    exact sub_mem (add_mem (sm _ P.η₃_Lℓ) (by simpa using sm s₃ P.one_Lℓ)) hx0 /-- Case II of prop:discA for one configuration (i, j, k): A = L + ℤ η_iη_j, 2η_iη_j ∈ L, η_iη_j ∉ L. -/theorem caseII_gen {ηi ηj ηk : O} {ei ej ek mi mj c K : ℤ}    (hset : Set.range P.vη = {1, ηi, ηj, ηk})    (hli : LinearIndependent ℤ ![1, ηi, ηj, ηk])    (hiA : ηi ∈ Aring M) (hjA : ηj ∈ Aring M)    (hi : ∃ C : ℤ, ηi * ηi = ei * ηi + C) (hj : ∃ C : ℤ, ηj * ηj = ej * ηj + C)    (hk : ∃ C : ℤ, ηk * ηk = ek * ηk + C)    (hE : (c : O) * ηk = 2 * mi * mj * (ηi * ηj) - mi * mj * ej * ηi - mi * mj * ei * ηj + K)    (hc : ¬ Even c) (hm : ¬ Even (mi * mj)) (hx : ∀ m, M.x m ∈ P.Lℓ) :    ηi * ηj ∈ Aring M ∧ 2 * (ηi * ηj) ∈ P.Lℓ ∧ ηi * ηj ∉ P.Lℓ ∧ ∀ z ∈ Aring M, z ∈ P.Nℓ (ηi * ηj) := by  obtain ⟨Ci, hCi⟩ := hi  obtain ⟨Cj, hCj⟩ := hj  obtain ⟨Ck, hCk⟩ := hk  have memL : ∀ g ∈ ({1, ηi, ηj, ηk} : Set O), g ∈ P.Lℓ := by    intro g hg    rw [← hset] at hg    obtain ⟨k, rfl⟩ := hg    exact P.mem_Lℓ k  have memN : ∀ g ∈ ({1, ηi, ηj, ηk} : Set O), g ∈ P.Nℓ (ηi * ηj) :=    fun g hg => P.Lℓ_le_N _ (memL g hg)  have odd := caseII_odd (P.Nℓ (ηi * ηj)) hCi hCj hCk hE (memN 1 (by simp)) (memN ηi (by simp))    (memN ηj (by simp)) (memN ηk (by simp)) (P.y_mem_N _)  have hodd : ∀ m, ∀ g ∈ Set.range P.vη ∪ {ηi * ηj},      (c : O) * (M.x m * g) ∈ P.Nℓ (ηi * ηj) := by    intro m g hg    have hg' : g ∈ ({1, ηi, ηj, ηk, ηi * ηj} : Set O) := by      rcases hg with hg | hg      · rw [hset] at hg        simp only [Set.mem_insert_iff, Set.mem_singleton_iff] at hg ⊢        tauto      · rw [Set.mem_singleton_iff.mp hg]; simp    suffices H : ∀ z ∈ P.Lℓ, (c : O) * (z * g) ∈ P.Nℓ (ηi * ηj) from H _ (hx m)    intro z hz    induction hz using Submodule.span_induction with    | mem z hz => rw [hset] at hz; exact odd z hz g hg'    | zero => simp    | add a b _ _ ha hb => rw [add_mul, mul_add]; exact add_mem ha hb    | smul n a _ ha => rw [smul_mul_assoc, mul_smul_comm]; exact Submodule.smul_mem _ n ha  have hyA : ηi * ηj ∈ Aring M := mul_mem hiA hjA  have hA := P.A_le_N hyA hc hodd  have sm := smul_mem' P.Lℓ  have h2L : 2 * (ηi * ηj) ∈ P.Lℓ := by    have h2A : (2 : O) ∈ Aring M := by simp    obtain ⟨K₂, hK₂⟩ := P.half_loc (mul_mem h2A hyA)    refine bezout_mem P.Lℓ hm K₂ ?_ (P.Mℓ_le_Lℓ hK₂)    rw [show ((mi * mj : ℤ) : O) * (2 * (ηi * ηj))        = (c : O) * ηk + (mi * mj * ej : ℤ) * ηi + (mi * mj * ei : ℤ) * ηj - (K : O) by      push_cast; linear_combination -hE]    exact sub_mem (add_mem (add_mem (sm c (memL ηk (by simp))) (sm _ (memL ηi (by simp))))      (sm _ (memL ηj (by simp)))) (by simpa using sm K P.one_Lℓ)  have hnL : ηi * ηj ∉ P.Lℓ := by    have hL : P.Lℓ = Submodule.span ℤ (Set.range ![1, ηi, ηj, ηk]) := by      rw [range_four, ← hset]    rw [hL]    exact caseII_notin hli hE hc  exact ⟨hyA, h2L, hnL, hA⟩ end Dat namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- Case II of prop:discA: all n_j even ⇒ A = L + ℤ η_iη_j for the pair with m_i, m_j odd. -/theorem caseII (h1 : 2 ∣ P.n₁) (h2 : 2 ∣ P.n₂) (h3 : 2 ∣ P.n₃) :    ∃ y ∈ Aring M, 2 * y ∈ P.Lℓ ∧ y ∉ P.Lℓ ∧ ∀ z ∈ Aring M, z ∈ P.Nℓ y := by  obtain ⟨a0, m₁, m₂, m₃, ha, hm₁, hm₂, hm₃, -, hcfg⟩ := P.toZD.even_case h1 h2 h3  change P.a = 2 * a0 at ha  change P.n₁ = 2 * m₁ at hm₁  change P.n₂ = 2 * m₂ at hm₂  change P.n₃ = 2 * m₃ at hm₃  have ot₁ := odd_of_cop_even ⟨P.u₁, P.v₁, P.bez₁⟩ h1  have ot₂ := odd_of_cop_even ⟨P.u₂, P.v₂, P.bez₂⟩ h2  have ot₃ := odd_of_cop_even ⟨P.u₃, P.v₃, P.bez₃⟩ h3  have hx := P.x_in_L h1 h2 h3  have sq₁ := eta_sq M P.two_η₁ P.hΔ₁  have sq₂ := eta_sq M P.two_η₂ P.hΔ₂  have sq₃ := eta_sq M P.two_η₃ P.hΔ₃  have hli := linIndep_of_D M P.D_vη_ne  have codd : ∀ {m t : ℤ}, Even m → ¬ Even t → ¬ Even (a0 * m - t) := by    intro m t hm ht h    rw [Int.even_sub, Int.even_mul] at h    tauto  have modd : ∀ {m m' : ℤ}, ¬ Even m → ¬ Even m' → ¬ Even (m * m') := by    intro m m' hm hm' h    rw [Int.even_mul] at h    tauto  rcases hcfg with ⟨o1, o2, e3, -⟩ | ⟨o1, e2, o3, -⟩ | ⟨e1, o2, o3, -⟩  · -- (i, j, k) = (1, 2, 3)    have hrel : ((m₁ : O) * m₂) * (P.ϑ₁ * P.ϑ₂) = ((a0 * m₃ - P.t₃ : ℤ) : O) * P.ϑ₃ := by      have := P.prod₃      rw [hm₁, hm₂, hm₃, ha] at this      push_cast at this ⊢      apply mul_left_cancel₀ (show (4 : O) ≠ 0 by norm_num)      linear_combination this    obtain ⟨K, hE⟩ := eta_rel M P.two_η₁ P.two_η₂ P.two_η₃ hrel    exact ⟨_, P.caseII_gen (by simp only [vη]; exact range_four _ _ _ _) hli P.η₁_mem P.η₂_mem      sq₁ sq₂ sq₃ hE (codd e3 ot₃) (modd o1 o2) hx⟩  · -- (i, j, k) = (1, 3, 2)    have hrel : ((m₁ : O) * m₃) * (P.ϑ₁ * P.ϑ₃) = ((a0 * m₂ - P.t₂ : ℤ) : O) * P.ϑ₂ := by      have := P.prod₂      rw [hm₁, hm₂, hm₃, ha] at this      push_cast at this ⊢      apply mul_left_cancel₀ (show (4 : O) ≠ 0 by norm_num)      linear_combination this    obtain ⟨K, hE⟩ := eta_rel M P.two_η₁ P.two_η₃ P.two_η₂ hrel    have hli' : LinearIndependent ℤ ![1, P.η₁, P.η₃, P.η₂] := by      have := hli.comp ![0, 1, 3, 2] (by decide)      convert this using 1      funext k; fin_cases k <;> rfl    exact ⟨_, P.caseII_gen (by        simp only [vη]; rw [range_four]; ext z; simp only [Set.mem_insert_iff,          Set.mem_singleton_iff]; tauto) hli' P.η₁_mem P.η₃_mem      sq₁ sq₃ sq₂ hE (codd e2 ot₂) (modd o1 o3) hx⟩  · -- (i, j, k) = (2, 3, 1)    have hrel : ((m₂ : O) * m₃) * (P.ϑ₂ * P.ϑ₃) = ((a0 * m₁ - P.t₁ : ℤ) : O) * P.ϑ₁ := by      have := P.prod₁      rw [hm₁, hm₂, hm₃, ha] at this      push_cast at this ⊢      apply mul_left_cancel₀ (show (4 : O) ≠ 0 by norm_num)      linear_combination this    obtain ⟨K, hE⟩ := eta_rel M P.two_η₂ P.two_η₃ P.two_η₁ hrel    have hli' : LinearIndependent ℤ ![1, P.η₂, P.η₃, P.η₁] := by      have := hli.comp ![0, 2, 3, 1] (by decide)      convert this using 1      funext k; fin_cases k <;> rfl    exact ⟨_, P.caseII_gen (by        simp only [vη]; rw [range_four]; ext z; simp only [Set.mem_insert_iff,          Set.mem_singleton_iff]; tauto) hli' P.η₂_mem P.η₃_mem      sq₂ sq₃ sq₁ hE (codd e1 ot₁) (modd o2 o3) hx⟩ /-- A = L + ℤy with 2y ∈ L, y ∉ L, in all cases (y = α or y = η_iη_j; proof of prop:discA). -/theorem exists_y : ∃ y ∈ Aring M, 2 * y ∈ P.Lℓ ∧ y ∉ P.Lℓ ∧ ∀ z ∈ Aring M, z ∈ P.Nℓ y := by  by_cases h : 2 ∣ P.n₁ ∧ 2 ∣ P.n₂ ∧ 2 ∣ P.n₃  · exact P.caseII h.1 h.2.1 h.2.2  · exact ⟨_, P.caseI (by simp only [even_iff_two_dvd]; tauto)⟩ /-- [A : L] = 2 in all cases (proof of prop:discA). -/theorem relIndex_L_A : P.Lℓ.toAddSubgroup.relIndex (Aring M).toAddSubgroup = 2 := by  obtain ⟨y, hyA, h2L, hnL, hA⟩ := P.exists_y  exact relIndex_two P.Lℓ hyA h2L hnL (fun z hz => P.decomp (hA z hz)) /-- **prop:discA, the index**: [A : ℤ[α]] · |Θ| = |Δ_α|. -/theorem index_mul_Θ : (Lat (vpow M)).relIndex (Aring M).toAddSubgroup * P.Θ.natAbs    = P.δ.natAbs := P.index_of_two P.relIndex_L_A /-- gcd(T, U, V, W) = |Θ| for the note's (n_j, t_j) (Lemma `lem:split`, cf. `gcd_TUVW`). -/theorem gcd_eq {t u v w : ℤ} (ht : T M.x = t) (hu : U M.x = u) (hv : V M.x = v)    (hw : W M.x = w) : Int.gcd (Int.gcd t u) (Int.gcd v w) = P.Θ.natAbs := by  have eu : u = P.Θ * (P.n₁ * P.n₂ * P.n₃) := by    have h : ((u : ℤ) : O) = ((P.Θ * (P.n₁ * P.n₂ * P.n₃) : ℤ) : O) := by      rw [← hu, U_eq, P.hr₁, P.hr₂, P.hr₃]; push_cast; rw [← P.hΘ]; ring    exact_mod_cast h  have ev : v = P.Θ * (P.n₁ * P.n₂ * P.t₃ + P.n₁ * P.t₂ * P.n₃ + P.t₁ * P.n₂ * P.n₃) := by    have h : ((v : ℤ) : O) = ((P.Θ * (P.n₁ * P.n₂ * P.t₃ + P.n₁ * P.t₂ * P.n₃        + P.t₁ * P.n₂ * P.n₃) : ℤ) : O) := by      rw [← hv, V_eq, P.hr₁, P.hr₂, P.hr₃, P.hq₁, P.hq₂, P.hq₃]; push_cast; rw [← P.hΘ]; ring    exact_mod_cast h  have ew : w = P.Θ * (P.n₁ * P.t₂ * P.t₃ + P.t₁ * P.n₂ * P.t₃ + P.t₁ * P.t₂ * P.n₃) := by    have h : ((w : ℤ) : O) = ((P.Θ * (P.n₁ * P.t₂ * P.t₃ + P.t₁ * P.n₂ * P.t₃        + P.t₁ * P.t₂ * P.n₃) : ℤ) : O) := by      rw [← hw, W_eq, P.hr₁, P.hr₂, P.hr₃, P.hq₁, P.hq₂, P.hq₃]; push_cast; rw [← P.hΘ]; ring    exact_mod_cast h  have et : t = P.Θ * (P.t₁ * P.t₂ * P.t₃) := by    have h : ((t : ℤ) : O) = ((P.Θ * (P.t₁ * P.t₂ * P.t₃) : ℤ) : O) := by      rw [← ht, T_eq, P.hq₁, P.hq₂, P.hq₃]; push_cast; rw [← P.hΘ]; ring    exact_mod_cast h  set G : ℤ := ((Int.gcd (Int.gcd t u) (Int.gcd v w) : ℕ) : ℤ) with hG  have hGtu : G ∣ ((Int.gcd t u : ℕ) : ℤ) := Int.gcd_dvd_left ..  have hGvw : G ∣ ((Int.gcd v w : ℕ) : ℤ) := Int.gcd_dvd_right ..  have hGθ : G ∣ P.Θ := gauss3 ⟨P.u₁, P.v₁, P.bez₁⟩ ⟨P.u₂, P.v₂, P.bez₂⟩ ⟨P.u₃, P.v₃, P.bez₃⟩    (eu ▸ hGtu.trans (Int.gcd_dvd_right t u)) (ev ▸ hGvw.trans (Int.gcd_dvd_left v w))    (ew ▸ hGvw.trans (Int.gcd_dvd_right v w)) (et ▸ hGtu.trans (Int.gcd_dvd_left t u))  have hθG : P.Θ ∣ G := Int.dvd_coe_gcd (Int.dvd_coe_gcd ⟨_, et⟩ ⟨_, eu⟩) (Int.dvd_coe_gcd ⟨_, ev⟩ ⟨_, ew⟩)  have e : G = |P.Θ| := Int.dvd_antisymm (Int.natCast_nonneg _) (abs_nonneg _)    ((dvd_abs G P.Θ).mpr hGθ) ((abs_dvd P.Θ G).mpr hθG)  have h2 : G = ((P.Θ.natAbs : ℕ) : ℤ) := by rw [Int.natCast_natAbs, e]  exact Int.natCast_inj.mp h2 end Dat /-! ## Part 9. Integer core of prop:smith (the invariant factors) -/ theorem cop_of_common {a b : ℤ} (h : ∀ g : ℤ, g ∣ a → g ∣ b → IsUnit g) : IsCoprime a b := by  rw [Int.isCoprime_iff_gcd_eq_one]  have hu := h (Int.gcd a b) (Int.gcd_dvd_left ..) (Int.gcd_dvd_right ..)  rcases Int.isUnit_iff.mp hu with h1 | h1  · exact_mod_cast h1  · exfalso; have := Int.natCast_nonneg (Int.gcd a b); omega theorem odd_cop_two {d : ℤ} (hd : ¬ Even d) : IsCoprime d 2 :=  (Int.isCoprime_two_left.mpr (Int.not_even_iff_odd.mp hd)).symm /-- d₁ = 1, Case I: (1 − dc) n_l = 2 d c_l for all l ⇒ d ∣ n_l for all l. -/theorem d1_caseI {d c c₁ c₂ c₃ n₁ n₂ n₃ : ℤ}    (h1 : (1 - d * c) * n₁ = 2 * d * c₁) (h2 : (1 - d * c) * n₂ = 2 * d * c₂)    (h3 : (1 - d * c) * n₃ = 2 * d * c₃) : d ∣ n₁ ∧ d ∣ n₂ ∧ d ∣ n₃ := by  have hc : IsCoprime d (1 - d * c) := ⟨c, 1, by ring⟩  refine ⟨hc.dvd_of_dvd_mul_left ⟨2 * c₁, by rw [h1]; ring⟩,    hc.dvd_of_dvd_mul_left ⟨2 * c₂, by rw [h2]; ring⟩,    hc.dvd_of_dvd_mul_left ⟨2 * c₃, by rw [h3]; ring⟩⟩ theorem odd_part {d x y : ℤ} (h : ¬ Even (d * (2 * x + y))) : ¬ Even y := by  intro hy; apply h; exact ((even_two_mul x).add hy).mul_left d theorem odd_right {c e : ℤ} (h : ¬ Even (c * e)) : ¬ Even e := fun he => h (he.mul_left c)theorem odd_left {c e : ℤ} (h : ¬ Even (c * e)) : ¬ Even c := fun hc => h (hc.mul_right e) /-- d₁ = 1, Case II: d(2c_i + c e_j) = 2m_i, d(2c_j + c e_i) = 2m_j with e_i + e_j odd,    m_i, m_j odd and gcd(m_i, m_j) = 1 force d to be a unit. -/theorem d1_caseII {d c ci cj ei ej mi mj : ℤ} (hi : d * (2 * ci + c * ej) = 2 * mi)    (hj : d * (2 * cj + c * ei) = 2 * mj) (he : ¬ Even (ei + ej)) (hm : IsCoprime mi mj)    (hmi : ¬ Even mi) (hmj : ¬ Even mj) : IsUnit d := by  have hd : ¬ Even d := by    rintro ⟨d', rfl⟩    have hi' : d' * (2 * ci + c * ej) = mi := by linarith    have hj' : d' * (2 * cj + c * ei) = mj := by linarith    have oj := odd_right (odd_part (show ¬ Even (d' * (2 * ci + c * ej)) by rw [hi']; exact hmi))    have oi := odd_right (odd_part (show ¬ Even (d' * (2 * cj + c * ei)) by rw [hj']; exact hmj))    exact he ((Int.not_even_iff_odd.mp oi).add_odd (Int.not_even_iff_odd.mp oj))  have h2 := odd_cop_two hd  have di : d ∣ mi := h2.dvd_of_dvd_mul_left ⟨2 * ci + c * ej, by rw [← hi]⟩  have dj : d ∣ mj := h2.dvd_of_dvd_mul_left ⟨2 * cj + c * ei, by rw [← hj]⟩  exact hm.isUnit_of_dvd' di dj /-- d₂ ∣ m₀, Case I: (P − dc) n_l − 2t_l = 2d c_l for all l, with some n_i odd. -/theorem d2_caseI {d c P c₁ c₂ c₃ n₁ n₂ n₃ t₁ t₂ t₃ : ℤ}    (h1 : P * n₁ - 2 * t₁ = 2 * d * c₁ + d * c * n₁)    (h2 : P * n₂ - 2 * t₂ = 2 * d * c₂ + d * c * n₂)    (h3 : P * n₃ - 2 * t₃ = 2 * d * c₃ + d * c * n₃)    (hodd : ¬ Even n₁ ∨ ¬ Even n₂ ∨ ¬ Even n₃) :    d ∣ n₁ * t₂ - n₂ * t₁ ∧ d ∣ n₁ * t₃ - n₃ * t₁ ∧ d ∣ n₂ * t₃ - n₃ * t₂ := by  -- P' = P − dc is even  have hP : Even (P - d * c) := by    rcases hodd with h | h | h    · have : (P - d * c) * n₁ = 2 * (t₁ + d * c₁) := by linarith      have e := congrArg Even this      simp only [even_two_mul, Int.even_mul] at e      tauto    · have : (P - d * c) * n₂ = 2 * (t₂ + d * c₂) := by linarith      have e := congrArg Even this      simp only [even_two_mul, Int.even_mul] at e      tauto    · have : (P - d * c) * n₃ = 2 * (t₃ + d * c₃) := by linarith      have e := congrArg Even this      simp only [even_two_mul, Int.even_mul] at e      tauto  obtain ⟨Q, hQ⟩ := hP  have q1 : Q * n₁ - t₁ = d * c₁ := by    apply mul_left_cancel₀ (two_ne_zero (α := ℤ)); linear_combination h1 - n₁ * hQ  have q2 : Q * n₂ - t₂ = d * c₂ := by    apply mul_left_cancel₀ (two_ne_zero (α := ℤ)); linear_combination h2 - n₂ * hQ  have q3 : Q * n₃ - t₃ = d * c₃ := by    apply mul_left_cancel₀ (two_ne_zero (α := ℤ)); linear_combination h3 - n₃ * hQ  refine ⟨⟨-(n₁ * c₂ - n₂ * c₁), ?_⟩, ⟨-(n₁ * c₃ - n₃ * c₁), ?_⟩, ⟨-(n₂ * c₃ - n₃ * c₂), ?_⟩⟩  · linear_combination (-n₁) * q2 + n₂ * q1  · linear_combination (-n₁) * q3 + n₃ * q1  · linear_combination (-n₂) * q3 + n₃ * q2 /-- d₂ ∣ m₀/2, Case II (configuration (i, j, k): m_i, m_j odd, m_k even). -/theorem d2_caseII {d c P ci cj ck c' ei ej mi mj mk ti tj tk : ℤ}    (hk : d * (2 * (mi * mj) * ck + c * c') = 2 * (mi * mj) * (P * mk - tk))    (hi : d * (2 * ci + c * ej) = 2 * (P * mi - ti))    (hj : d * (2 * cj + c * ei) = 2 * (P * mj - tj))    (hmi : ¬ Even mi) (hmj : ¬ Even mj) (hmk : Even mk) (hti : ¬ Even ti) (htj : ¬ Even tj)    (htk : ¬ Even tk) (he : ¬ Even (ei + ej))    (copi : ∀ g : ℤ, g ∣ mi → g ∣ ti → IsUnit g) (copj : ∀ g : ℤ, g ∣ mj → g ∣ tj → IsUnit g) :    d ∣ mi * tj - mj * ti ∧ d ∣ mi * tk - mk * ti ∧ d ∣ mj * tk - mk * tj := by  -- d is odd  have hd : ¬ Even d := by    rintro ⟨d', rfl⟩    have hk' : d' * (2 * (mi * mj * ck) + c * c') = mi * mj * (P * mk - tk) := by linarith    have hi' : d' * (2 * ci + c * ej) = P * mi - ti := by linarith    have hj' : d' * (2 * cj + c * ei) = P * mj - tj := by linarith    have ok : ¬ Even (mi * mj * (P * mk - tk)) := by      rw [Int.even_mul, Int.even_mul, Int.even_sub, Int.even_mul]; tauto    rw [← hk'] at ok    have od' : ¬ Even d' := odd_left ok    have oc : ¬ Even c := odd_left (odd_part ok)    have pi : Even ej ↔ ¬ Even P := by      have h := congrArg Even hi'      simp only [Int.even_mul, Int.even_add, Int.even_sub, even_two, true_or, od', false_or,        oc, hmi, hti, iff_false] at h      tauto    have pj : Even ei ↔ ¬ Even P := by      have h := congrArg Even hj'      simp only [Int.even_mul, Int.even_add, Int.even_sub, even_two, true_or, od', false_or,        oc, hmj, htj, iff_false] at h      tauto    rw [Int.even_add] at he    tauto   have h2 := odd_cop_two hd  have di : d ∣ P * mi - ti := h2.dvd_of_dvd_mul_left ⟨2 * ci + c * ej, by rw [← hi]⟩  have dj : d ∣ P * mj - tj := h2.dvd_of_dvd_mul_left ⟨2 * cj + c * ei, by rw [← hj]⟩  have ci' : IsCoprime d mi := cop_of_common fun g hgd hgm => copi g hgm (by    have : ti = P * mi - (P * mi - ti) := by ring    rw [this]; exact dvd_sub (dvd_mul_of_dvd_right hgm P) (hgd.trans di))  have cj' : IsCoprime d mj := cop_of_common fun g hgd hgm => copj g hgm (by    have : tj = P * mj - (P * mj - tj) := by ring    rw [this]; exact dvd_sub (dvd_mul_of_dvd_right hgm P) (hgd.trans dj))  have hc2 : IsCoprime d (2 * (mi * mj)) := h2.mul_right (ci'.mul_right cj')  have dk : d ∣ P * mk - tk := hc2.dvd_of_dvd_mul_left ⟨2 * (mi * mj) * ck + c * c', by rw [← hk]⟩  obtain ⟨xi, hxi⟩ := di  obtain ⟨xj, hxj⟩ := dj  obtain ⟨xk, hxk⟩ := dk  refine ⟨⟨mj * xi - mi * xj, ?_⟩, ⟨mk * xi - mi * xk, ?_⟩, ⟨mk * xj - mj * xk, ?_⟩⟩  · linear_combination (-mi) * hxj + mj * hxi  · linear_combination (-mi) * hxk + mk * hxi  · linear_combination (-mj) * hxk + mk * hxj /-! ## Part 10. Coordinates: A ↪ ℚ⁴ in the basis 1, α, α², α³ (bridge to `T1Completo`) -/ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- the ℤ-basis 1, α, α², α³ of ℤ[α]. -/noncomputable def bZ (M : V4Data O) : Basis (Fin 4) ℤ (Submodule.span ℤ (Set.range (vpow M))) :=  Basis.span (linIndep_of_D M (D_vpow_ne (M := M))) theorem pow_indep {g : Fin 4 → ℤ} (h : ∑ k, (g k : O) * M.x 0 ^ (k : ℕ) = 0) : g = 0 := by  have := Fintype.linearIndependent_iff.mp (linIndep_of_D M (D_vpow_ne (M := M))) g (by    simpa [vpow, zsmul_eq_mul] using h)  funext k; exact this k theorem δy_mem {y : O} (hy : y ∈ Aring M) :    (P.δ : O) * y ∈ Submodule.span ℤ (Set.range (vpow M)) := by  rw [← P.hδ]; exact cramer_mem M hy /-- integer coordinates of Δ·y. -/noncomputable def rc (y : (Aring M).toAddSubgroup) : Fin 4 → ℤ :=  fun k => (bZ M).repr ⟨P.δ * y, P.δy_mem y.2⟩ k theorem rc_sum (y : (Aring M).toAddSubgroup) :    ∑ k, (P.rc y k : O) * M.x 0 ^ (k : ℕ) = P.δ * y := by  have h := congrArg Subtype.val ((bZ M).sum_repr ⟨P.δ * y, P.δy_mem y.2⟩)  simp only [Submodule.coe_sum, Submodule.coe_smul, bZ, Basis.coe_span_apply, vpow,    zsmul_eq_mul] at h  exact h theorem rc_add (y z : (Aring M).toAddSubgroup) : P.rc (y + z) = P.rc y + P.rc z := by  have := pow_indep (M := M) (g := P.rc (y + z) - (P.rc y + P.rc z)) (by    simp only [Pi.sub_apply, Pi.add_apply, Int.cast_sub, Int.cast_add, sub_mul, add_mul,      Finset.sum_sub_distrib, Finset.sum_add_distrib, P.rc_sum]    push_cast; ring)  exact sub_eq_zero.mp this /-- φ(y) = (coordinates of y in 1, α, α², α³) ∈ ℚ⁴. -/noncomputable def φ : (Aring M).toAddSubgroup →+ (Fin 4 → ℚ) where  toFun y := fun k => (P.rc y k : ℚ) / P.δ  map_zero' := by    have h0 : P.rc 0 = 0 := by      have := P.rc_add 0 0      simp only [add_zero] at this      have h2 : P.rc 0 + P.rc 0 - P.rc 0 = P.rc 0 - P.rc 0 := by rw [← this]      simpa using h2    funext k; simp [h0]  map_add' y z := by    funext k; simp [P.rc_add, add_div] theorem φ_apply (y : (Aring M).toAddSubgroup) (k : Fin 4) : P.φ y k = (P.rc y k : ℚ) / P.δ := rfl /-- d·y = Σ c_k αᵏ ⟺ φ(y) = c/d (d ≠ 0). -/theorem φ_eq_iff (y : (Aring M).toAddSubgroup) {d : ℤ} (hd : d ≠ 0) (c : Fin 4 → ℤ) :    (d : O) * y = ∑ k, (c k : O) * M.x 0 ^ (k : ℕ) ↔ P.φ y = fun k => (c k : ℚ) / d := by  have hδ := P.δ_ne  have hδq : (P.δ : ℚ) ≠ 0 := by exact_mod_cast hδ  have hdq : (d : ℚ) ≠ 0 := by exact_mod_cast hd  constructor  · intro h    have hz := pow_indep (M := M) (g := fun k => d * P.rc y k - P.δ * c k) (by      simp only [Int.cast_sub, Int.cast_mul, sub_mul, Finset.sum_sub_distrib, mul_assoc,        ← Finset.mul_sum, P.rc_sum, ← h]      ring)    funext k    have e := congrFun hz k    simp only [Pi.zero_apply, sub_eq_zero] at e    rw [φ_apply, div_eq_div_iff hδq hdq]    exact_mod_cast (by linarith : P.rc y k * d = c k * P.δ)  · intro h    have e : ∀ k, d * P.rc y k = P.δ * c k := by      intro k      have := congrFun h k      rw [φ_apply, div_eq_div_iff hδq hdq] at this      have : (P.rc y k * d : ℤ) = c k * P.δ := by exact_mod_cast this      linarith    apply mul_left_cancel₀ (Int.cast_ne_zero.mpr hδ)    calc (P.δ : O) * (d * y) = d * (P.δ * y) := by ring      _ = d * ∑ k, (P.rc y k : O) * M.x 0 ^ (k : ℕ) := by rw [P.rc_sum]      _ = ∑ k, ((d * P.rc y k : ℤ) : O) * M.x 0 ^ (k : ℕ) := by        rw [Finset.mul_sum]; push_cast; simp only [mul_assoc]      _ = ∑ k, ((P.δ * c k : ℤ) : O) * M.x 0 ^ (k : ℕ) := by simp only [e]      _ = P.δ * ∑ k, (c k : O) * M.x 0 ^ (k : ℕ) := by        rw [Finset.mul_sum]; push_cast; simp only [mul_assoc] theorem φ_injective : Function.Injective P.φ := by  intro y z h  have e : P.rc y = P.rc z := by    funext k    have := congrFun h k    rw [φ_apply, φ_apply, div_left_inj' (by exact_mod_cast P.δ_ne)] at this    exact_mod_cast this  apply Subtype.ext  apply mul_left_cancel₀ (Int.cast_ne_zero.mpr P.δ_ne)  rw [← P.rc_sum, ← P.rc_sum, e] /-- the image of A in ℚ⁴. -/noncomputable def MQ : AddSubgroup (Fin 4 → ℚ) := P.φ.range theorem mem_Zα_iff (y : (Aring M).toAddSubgroup) :    (y : O) ∈ Lat (vpow M) ↔ P.φ y ∈ Louboutin.Z4 := by  constructor  · intro hy    obtain ⟨c, hc⟩ := (Submodule.mem_span_range_iff_exists_fun ℤ).mp hy    have h := (P.φ_eq_iff y one_ne_zero c).mp (by      rw [← hc]; simp [vpow, zsmul_eq_mul])    rw [h]    intro i    exact ⟨c i, by simp⟩  · intro hy    choose c hc using hy    have h : P.φ y = fun k => (c k : ℚ) / ((1 : ℤ) : ℚ) := by      funext k; rw [hc k]; simp    have := (P.φ_eq_iff y one_ne_zero c).mpr h    rw [Int.cast_one, one_mul] at this    rw [show (y : O) = ∑ k, c k • vpow M k by rw [this]; simp [vpow, zsmul_eq_mul]]    exact Submodule.sum_mem _ fun k _ => Submodule.smul_mem _ _ (Submodule.subset_span ⟨k, rfl⟩) /-- [φ(A) : ℤ⁴ ∩ φ(A)] = [A : ℤ[α]]. -/theorem index_MQ : (Louboutin.Z4.addSubgroupOf P.MQ).index    = (Lat (vpow M)).relIndex (Aring M).toAddSubgroup := by  have hs : Function.Surjective P.φ.rangeRestrict := P.φ.rangeRestrict_surjective  rw [← AddSubgroup.index_comap_of_surjective (Louboutin.Z4.addSubgroupOf P.MQ) hs]  rw [AddSubgroup.relIndex]  congr 1  ext y  simp only [AddSubgroup.mem_addSubgroupOf]  exact (P.mem_Zα_iff y).symm end Dat /-! ## Part 11. Coordinates on the ϑ-basis (comparison of coefficients) -/ theorem coeff4 {R : Type*} [CommRing R] {v₁ v₂ v₃ : R} (hli : LinearIndependent ℤ ![1, v₁, v₂, v₃])    {A0 A1 A2 A3 : ℤ} (h : (A0 : R) + A1 * v₁ + A2 * v₂ + A3 * v₃ = 0) :    A0 = 0 ∧ A1 = 0 ∧ A2 = 0 ∧ A3 = 0 := by  have := Fintype.linearIndependent_iff.mp hli ![A0, A1, A2, A3] (by    simp only [Fin.sum_univ_four, Matrix.cons_val_zero, Matrix.cons_val_one, Matrix.cons_val,      zsmul_eq_mul, mul_one]    exact h)  exact ⟨this 0, this 1, this 2, this 3⟩ section coeffs variable {R : Type*} [CommRing R] {ϑ₁ ϑ₂ ϑ₃ η₁ η₂ η₃ x0 w : R}  {a n₁ n₂ n₃ t₁ t₂ t₃ e₁ e₂ e₃ Δ₁ Δ₂ Δ₃ : ℤ} /-- Case I, d₁: d·w = p + α with w = c₀ + Σ c_l η_l + κα gives (1 − dκ) n_l = 2d c_l. -/theorem coeffI_d1 (hli : LinearIndependent ℤ ![1, ϑ₁, ϑ₂, ϑ₃])    (h1 : 2 * η₁ - e₁ = ϑ₁) (h2 : 2 * η₂ - e₂ = ϑ₂) (h3 : 2 * η₃ - e₃ = ϑ₃)    (hx : 4 * x0 = a + n₁ * ϑ₁ + n₂ * ϑ₂ + n₃ * ϑ₃)    {c0 c1 c2 c3 κ d p : ℤ} (hw : w = c0 + c1 * η₁ + c2 * η₂ + c3 * η₃ + κ * x0)    (hd : (d : R) * w = p + x0) :    (1 - d * κ) * n₁ = 2 * d * c1 ∧ (1 - d * κ) * n₂ = 2 * d * c2 ∧      (1 - d * κ) * n₃ = 2 * d * c3 := by  have key : ((4 * d * c0 + 2 * d * (c1 * e₁ + c2 * e₂ + c3 * e₃) + d * κ * a - 4 * p - a : ℤ) : R)      + ((2 * d * c1 + d * κ * n₁ - n₁ : ℤ) : R) * ϑ₁ + ((2 * d * c2 + d * κ * n₂ - n₂ : ℤ) : R) * ϑ₂      + ((2 * d * c3 + d * κ * n₃ - n₃ : ℤ) : R) * ϑ₃ = 0 := by    push_cast    linear_combination 4 * hd - 4 * (d : R) * hw - 2 * (d : R) * c1 * h1 - 2 * (d : R) * c2 * h2      - 2 * (d : R) * c3 * h3 - (d : R) * κ * hx + hx  obtain ⟨-, k1, k2, k3⟩ := coeff4 hli key  exact ⟨by linarith, by linarith, by linarith⟩ /-- Case I, d₂: d·w = q + pα + α² gives (a + p) n_l − 2t_l = 2d c_l + dκ n_l. -/theorem coeffI_d2 (hli : LinearIndependent ℤ ![1, ϑ₁, ϑ₂, ϑ₃])    (h1 : 2 * η₁ - e₁ = ϑ₁) (h2 : 2 * η₂ - e₂ = ϑ₂) (h3 : 2 * η₃ - e₃ = ϑ₃)    (hx : 4 * x0 = a + n₁ * ϑ₁ + n₂ * ϑ₂ + n₃ * ϑ₃)    (hΔ₁ : ϑ₁ * ϑ₁ = Δ₁) (hΔ₂ : ϑ₂ * ϑ₂ = Δ₂) (hΔ₃ : ϑ₃ * ϑ₃ = Δ₃)    (p₁ : (n₂ * n₃ : R) * (ϑ₂ * ϑ₃) = (a * n₁ - 4 * t₁ : R) * ϑ₁)    (p₂ : (n₁ * n₃ : R) * (ϑ₁ * ϑ₃) = (a * n₂ - 4 * t₂ : R) * ϑ₂)    (p₃ : (n₁ * n₂ : R) * (ϑ₁ * ϑ₂) = (a * n₃ - 4 * t₃ : R) * ϑ₃)    {c0 c1 c2 c3 κ d p q : ℤ} (hw : w = c0 + c1 * η₁ + c2 * η₂ + c3 * η₃ + κ * x0)    (hd : (d : R) * w = q + p * x0 + x0 ^ 2) :    (a + p) * n₁ - 2 * t₁ = 2 * d * c1 + d * κ * n₁ ∧      (a + p) * n₂ - 2 * t₂ = 2 * d * c2 + d * κ * n₂ ∧      (a + p) * n₃ - 2 * t₃ = 2 * d * c3 + d * κ * n₃ := by  have key : ((16 * d * c0 + 8 * d * (c1 * e₁ + c2 * e₂ + c3 * e₃) + 4 * d * κ * a        - (16 * q + 4 * p * a + a ^ 2 + n₁ ^ 2 * Δ₁ + n₂ ^ 2 * Δ₂ + n₃ ^ 2 * Δ₃) : ℤ) : R)      + ((8 * d * c1 + 4 * d * κ * n₁ - 4 * (p + a) * n₁ + 8 * t₁ : ℤ) : R) * ϑ₁      + ((8 * d * c2 + 4 * d * κ * n₂ - 4 * (p + a) * n₂ + 8 * t₂ : ℤ) : R) * ϑ₂      + ((8 * d * c3 + 4 * d * κ * n₃ - 4 * (p + a) * n₃ + 8 * t₃ : ℤ) : R) * ϑ₃ = 0 := by    push_cast    linear_combination 16 * hd - 16 * (d : R) * hw - 8 * (d : R) * c1 * h1      - 8 * (d : R) * c2 * h2 - 8 * (d : R) * c3 * h3 - 4 * (d : R) * κ * hx + 4 * (p : R) * hx      + (4 * x0 + a + n₁ * ϑ₁ + n₂ * ϑ₂ + n₃ * ϑ₃) * hx + (n₁ : R) ^ 2 * hΔ₁      + (n₂ : R) ^ 2 * hΔ₂ + (n₃ : R) ^ 2 * hΔ₃ + 2 * p₁ + 2 * p₂ + 2 * p₃  obtain ⟨-, k1, k2, k3⟩ := coeff4 hli key  exact ⟨by linarith, by linarith, by linarith⟩ end coeffs section coeffsII variable {R : Type*} [CommRing R] {ϑi ϑj ϑk ηi ηj ηk x0 w : R}  {a ni nj nk ti tj tk ei ej ek Δi Δj Δk mi mj c' : ℤ} /-- Case II, d₁ (configuration (i, j, k)): d·w = p + α with w = c₀ + Σ c_l η_l + κ η_iη_j. -/theorem coeffII_d1 (hli : LinearIndependent ℤ ![1, ϑi, ϑj, ϑk])    (hi : 2 * ηi - ei = ϑi) (hj : 2 * ηj - ej = ϑj) (hk : 2 * ηk - ek = ϑk)    (hx : 4 * x0 = a + ni * ϑi + nj * ϑj + nk * ϑk)    (hrel : (mi * mj : R) * (ϑi * ϑj) = c' * ϑk) (hm : mi * mj ≠ 0)    {c0 ci cj ck κ d p : ℤ} (hw : w = c0 + ci * ηi + cj * ηj + ck * ηk + κ * (ηi * ηj))    (hd : (d : R) * w = p + x0) :    d * (2 * ci + κ * ej) = ni ∧ d * (2 * cj + κ * ei) = nj := by  have key : ((4 * (mi * mj) * d * c0 + 2 * (mi * mj) * d * (ci * ei + cj * ej + ck * ek)        + (mi * mj) * d * κ * ei * ej - (mi * mj) * (4 * p + a) : ℤ) : R)      + ((2 * (mi * mj) * d * ci + (mi * mj) * d * κ * ej - (mi * mj) * ni : ℤ) : R) * ϑi      + ((2 * (mi * mj) * d * cj + (mi * mj) * d * κ * ei - (mi * mj) * nj : ℤ) : R) * ϑj      + ((2 * (mi * mj) * d * ck + d * κ * c' - (mi * mj) * nk : ℤ) : R) * ϑk = 0 := by    push_cast    linear_combination 4 * (mi : R) * mj * hd - 4 * (mi : R) * mj * d * hw      - 2 * (mi : R) * mj * d * ci * hi - 2 * (mi : R) * mj * d * cj * hj      - 2 * (mi : R) * mj * d * ck * hk      - (mi : R) * mj * d * κ * (2 * ηj) * hi - (mi : R) * mj * d * κ * (ϑi + ei) * hj      - (d : R) * κ * hrel + (mi : R) * mj * hx  obtain ⟨-, k1, k2, -⟩ := coeff4 hli key  constructor  · apply mul_left_cancel₀ hm; linarith  · apply mul_left_cancel₀ hm; linarith /-- Case II, d₂ (configuration (i, j, k)): d·w = q + pα + α². -/theorem coeffII_d2 (hli : LinearIndependent ℤ ![1, ϑi, ϑj, ϑk])    (hi : 2 * ηi - ei = ϑi) (hj : 2 * ηj - ej = ϑj) (hk : 2 * ηk - ek = ϑk)    (hx : 4 * x0 = a + ni * ϑi + nj * ϑj + nk * ϑk)    (hΔi : ϑi * ϑi = Δi) (hΔj : ϑj * ϑj = Δj) (hΔk : ϑk * ϑk = Δk)    (pij : (ni * nj : R) * (ϑi * ϑj) = (a * nk - 4 * tk : R) * ϑk)    (pik : (ni * nk : R) * (ϑi * ϑk) = (a * nj - 4 * tj : R) * ϑj)    (pjk : (nj * nk : R) * (ϑj * ϑk) = (a * ni - 4 * ti : R) * ϑi)    (hrel : (mi * mj : R) * (ϑi * ϑj) = c' * ϑk) (hm : mi * mj ≠ 0)    {c0 ci cj ck κ d p q : ℤ} (hw : w = c0 + ci * ηi + cj * ηj + ck * ηk + κ * (ηi * ηj))    (hd : (d : R) * w = q + p * x0 + x0 ^ 2) :    2 * (mi * mj) * d * ck + d * κ * c' = (mi * mj) * ((p + a) * nk - 2 * tk) ∧      d * (2 * ci + κ * ej) = (p + a) * ni - 2 * ti ∧      d * (2 * cj + κ * ei) = (p + a) * nj - 2 * tj := by  have key : ((16 * (mi * mj) * d * c0 + 8 * (mi * mj) * d * (ci * ei + cj * ej + ck * ek)        + 4 * (mi * mj) * d * κ * ei * ej        - (mi * mj) * (16 * q + 4 * p * a + a ^ 2 + ni ^ 2 * Δi + nj ^ 2 * Δj + nk ^ 2 * Δk) : ℤ) : R)      + ((8 * (mi * mj) * d * ci + 4 * (mi * mj) * d * κ * ej          - (mi * mj) * (4 * (p + a) * ni - 8 * ti) : ℤ) : R) * ϑi      + ((8 * (mi * mj) * d * cj + 4 * (mi * mj) * d * κ * ei          - (mi * mj) * (4 * (p + a) * nj - 8 * tj) : ℤ) : R) * ϑj      + ((8 * (mi * mj) * d * ck + 4 * d * κ * c'          - (mi * mj) * (4 * (p + a) * nk - 8 * tk) : ℤ) : R) * ϑk = 0 := by    push_cast    linear_combination 16 * (mi : R) * mj * hd - 16 * (mi : R) * mj * d * hw      - 8 * (mi : R) * mj * d * ci * hi - 8 * (mi : R) * mj * d * cj * hj      - 8 * (mi : R) * mj * d * ck * hk      - 4 * (mi : R) * mj * d * κ * (2 * ηj) * hi - 4 * (mi : R) * mj * d * κ * (ϑi + ei) * hj      - 4 * (d : R) * κ * hrel      + (mi : R) * mj * (4 * (p : R) * hx + (4 * x0 + a + ni * ϑi + nj * ϑj + nk * ϑk) * hx        + (ni : R) ^ 2 * hΔi + (nj : R) ^ 2 * hΔj + (nk : R) ^ 2 * hΔk + 2 * pij + 2 * pik        + 2 * pjk)  obtain ⟨-, k1, k2, k3⟩ := coeff4 hli key  refine ⟨by linarith, ?_, ?_⟩  · apply mul_left_cancel₀ (mul_ne_zero (show (4 : ℤ) ≠ 0 by norm_num) hm); linarith  · apply mul_left_cancel₀ (mul_ne_zero (show (4 : ℤ) ≠ 0 by norm_num) hm); linarith end coeffsII /-! ## Part 12. prop:smith in the model: d₁ = 1 and d₂ = m₀/h₀ for the triangular basis -/ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) theorem omega1_eq (d : ℕ) (p : ℤ) :    Louboutin.omega1 d p = fun k => ((![p, 1, 0, 0] : Fin 4 → ℤ) k : ℚ) / ((d : ℤ) : ℚ) := by  funext k; fin_cases k <;> simp [Louboutin.omega1] theorem omega2_eq (d : ℕ) (p0 p1 : ℤ) :    Louboutin.omega2 d p0 p1 = fun k => ((![p0, p1, 1, 0] : Fin 4 → ℤ) k : ℚ) / ((d : ℤ) : ℚ) := by  funext k; fin_cases k <;> simp [Louboutin.omega2] /-- The elements of A behind ω₁ and ω₂: d₁y₁ = p₁₀ + α and d₂y₂ = p₂₀ + p₂₁α + α². -/theorem tri_y1 (B : Louboutin.Triang P.MQ) :    ∃ y ∈ Aring M, (B.d₁ : O) * y = B.p₁₀ + M.x 0 := by  obtain ⟨y, hy⟩ := B.ω₁_mem  have hd : ((B.d₁ : ℤ)) ≠ 0 := by exact_mod_cast B.d₁_pos.ne'  have := (P.φ_eq_iff y hd _).mpr (by rw [hy, omega1_eq])  refine ⟨y, y.2, ?_⟩  rw [show ((B.d₁ : ℤ) : O) = (B.d₁ : O) by push_cast; rfl] at this  rw [this]; simp [Fin.sum_univ_four] theorem tri_y2 (B : Louboutin.Triang P.MQ) :    ∃ y ∈ Aring M, (B.d₂ : O) * y = B.p₂₀ + B.p₂₁ * M.x 0 + M.x 0 ^ 2 := by  obtain ⟨y, hy⟩ := B.ω₂_mem  have hd : ((B.d₂ : ℤ)) ≠ 0 := by exact_mod_cast B.d₂_pos.ne'  have := (P.φ_eq_iff y hd _).mpr (by rw [hy, omega2_eq])  refine ⟨y, y.2, ?_⟩  rw [show ((B.d₂ : ℤ) : O) = (B.d₂ : O) by push_cast; rfl] at this  rw [this]; simp [Fin.sum_univ_four] /-- elements of L + ℤy in coordinates. -/theorem N_form {y z : O} (hz : z ∈ P.Nℓ y) :    ∃ c0 c1 c2 c3 κ : ℤ, z = c0 + c1 * P.η₁ + c2 * P.η₂ + c3 * P.η₃ + κ * y := by  obtain ⟨l, hl, κ, rfl⟩ := P.decomp hz  obtain ⟨c, rfl⟩ := P.mem_Lℓ_iff.mp hl  exact ⟨c 0, c 1, c 2, c 3, κ, by rw [P.sum_vη, zsmul_eq_mul]⟩ /-- Case I of prop:smith: d₁ = 1 and d₂ divides d₁₂, d₁₃, d₂₃. -/theorem tierC_I (hodd : ¬ Even P.n₁ ∨ ¬ Even P.n₂ ∨ ¬ Even P.n₃) (B : Louboutin.Triang P.MQ) :    B.d₁ = 1 ∧ (B.d₂ : ℤ) ∣ P.toZD.d₁₂ ∧ (B.d₂ : ℤ) ∣ P.toZD.d₁₃ ∧ (B.d₂ : ℤ) ∣ P.toZD.d₂₃ := by  have hA := P.caseI_A_le hodd  have hli : LinearIndependent ℤ ![1, P.ϑ₁, P.ϑ₂, P.ϑ₃] := linIndep_of_D M P.D_vϑ_ne  constructor  · obtain ⟨y, hyA, hy⟩ := P.tri_y1 B    obtain ⟨c0, c1, c2, c3, κ, hw⟩ := P.N_form (hA y hyA)    have hy' : ((B.d₁ : ℤ) : O) * y = (B.p₁₀ : O) + M.x 0 := by exact_mod_cast hy    obtain ⟨e1, e2, e3⟩ := coeffI_d1 hli P.two_η₁ P.two_η₂ P.two_η₃ P.four_x0 hw hy'    obtain ⟨f1, f2, f3⟩ := d1_caseI e1 e2 e3    have h1 : P.toZD.h0 = 1 := P.toZD.h0_one_of_odd (by      simp only [toZD, ← even_iff_two_dvd]; exact hodd)    have hd : (B.d₁ : ℤ) ∣ (P.toZD.h0 : ℤ) := P.toZD.dvd_h0 f1 f2 f3    rw [h1] at hd    exact Nat.dvd_one.mp (by exact_mod_cast hd)  · obtain ⟨y, hyA, hy⟩ := P.tri_y2 B    obtain ⟨c0, c1, c2, c3, κ, hw⟩ := P.N_form (hA y hyA)    have hy' : ((B.d₂ : ℤ) : O) * y = (B.p₂₀ : O) + B.p₂₁ * M.x 0 + M.x 0 ^ 2 := by      exact_mod_cast hy    obtain ⟨e1, e2, e3⟩ := coeffI_d2 hli P.two_η₁ P.two_η₂ P.two_η₃ P.four_x0 P.hΔ₁ P.hΔ₂ P.hΔ₃      P.prod₁ P.prod₂ P.prod₃ hw hy'    exact d2_caseI e1 e2 e3 hodd /-- The element of A behind the lower bound: 2N α² = K₀ + K₁α + 2n₂d₁₃ x₁ + 2n₁d₂₃ x₂. -/theorem key_Y : ∃ K0 K1 : ℤ,    2 * ((P.n₁ * P.n₂ * P.n₃ : ℤ) : O) * M.x 0 ^ 2      = K0 + K1 * M.x 0 + 2 * ((P.n₂ * P.toZD.d₁₃ : ℤ) : O) * M.x 1        + 2 * ((P.n₁ * P.toZD.d₂₃ : ℤ) : O) * M.x 2 := by  have e1 : (P.n₁ : O) * (2 * (M.x 0 * M.x 1))      = P.n₁ * P.z₁ - P.a * P.t₁ + 2 * P.t₁ * (M.x 0 + M.x 1) := by    have h1 : 2 * (M.x 0 * M.x 1) = z1 M.x + dQ1 M.x := by simp only [z1, dQ1]; ring    have h2 : 2 * (M.x 0 + M.x 1) = ea M.x + r1 M.x := by simp only [ea, r1]; ring    rw [P.hz₁, P.hq₁] at h1; rw [P.ha, P.hr₁] at h2    linear_combination (P.n₁ : O) * h1 - (P.t₁ : O) * h2  have e2 : (P.n₂ : O) * (2 * (M.x 0 * M.x 2))      = P.n₂ * P.z₂ - P.a * P.t₂ + 2 * P.t₂ * (M.x 0 + M.x 2) := by    have h1 : 2 * (M.x 0 * M.x 2) = z2 M.x + dQ2 M.x := by simp only [z2, dQ2]; ring    have h2 : 2 * (M.x 0 + M.x 2) = ea M.x + r2 M.x := by simp only [ea, r2]; ring    rw [P.hz₂, P.hq₂] at h1; rw [P.ha, P.hr₂] at h2    linear_combination (P.n₂ : O) * h1 - (P.t₂ : O) * h2  have e3 : (P.n₃ : O) * (2 * (M.x 0 * M.x 3))      = P.n₃ * P.z₃ - P.a * P.t₃ + 2 * P.t₃ * (M.x 0 + M.x 3) := by    have h1 : 2 * (M.x 0 * M.x 3) = z3 M.x + dQ3 M.x := by simp only [z3, dQ3]; ring    have h2 : 2 * (M.x 0 + M.x 3) = ea M.x + r3 M.x := by simp only [ea, r3]; ring    rw [P.hz₃, P.hq₃] at h1; rw [P.ha, P.hr₃] at h2    linear_combination (P.n₃ : O) * h1 - (P.t₃ : O) * h2  have hs : M.x 0 ^ 2 = P.a * M.x 0 - M.x 0 * M.x 1 - M.x 0 * M.x 2 - M.x 0 * M.x 3 := by    rw [← P.ha]; simp only [ea]; ring  have h3 : M.x 3 = P.a - M.x 0 - M.x 1 - M.x 2 := by rw [← P.ha]; simp only [ea]; ring  refine ⟨-(P.n₂ * P.n₃ * (P.n₁ * P.z₁ - P.a * P.t₁) + P.n₁ * P.n₃ * (P.n₂ * P.z₂ - P.a * P.t₂)      + P.n₁ * P.n₂ * (P.n₃ * P.z₃ - P.a * P.t₃)) - 2 * P.n₁ * P.n₂ * P.t₃ * P.a,    2 * (P.n₁ * P.n₂ * P.n₃) * P.a - 2 * P.n₂ * P.n₃ * P.t₁ - 2 * P.n₁ * P.n₃ * P.t₂, ?_⟩  simp only [ZD.d₁₃, ZD.d₂₃, toZD]  push_cast  linear_combination 2 * (P.n₁ * P.n₂ * P.n₃ : O) * hs - (P.n₂ * P.n₃ : O) * e1    - (P.n₁ * P.n₃ : O) * e2 - (P.n₁ * P.n₂ : O) * e3 - 2 * (P.n₁ * P.n₂ * P.t₃ : O) * h3 end Dat /-- gcd(m_i, m_j) = 1 from m_i m_j X = (a₀m_i − t_i)(a₀m_j − t_j) (pair identity `P_k`). -/theorem cop_m {m m' t t' n n' a0 X : ℤ} (hn : n = 2 * m) (hn' : n' = 2 * m')    (c : IsCoprime n t) (c' : IsCoprime n' t') (hQ : m * m' * X = (a0 * m - t) * (a0 * m' - t')) :    IsCoprime m m' := by  rw [Int.isCoprime_iff_gcd_eq_one]  by_contra hg  obtain ⟨p, hp, hpg⟩ := Nat.exists_prime_and_dvd hg  have hpz : Prime (p : ℤ) := Nat.prime_iff_prime_int.mp hp  have hpm : (p : ℤ) ∣ m := (Int.natCast_dvd_natCast.mpr hpg).trans (Int.gcd_dvd_left ..)  have hpm' : (p : ℤ) ∣ m' := (Int.natCast_dvd_natCast.mpr hpg).trans (Int.gcd_dvd_right ..)  have hprod : (p : ℤ) ∣ (a0 * m - t) * (a0 * m' - t') := by    rw [← hQ]; exact dvd_mul_of_dvd_left (dvd_mul_of_dvd_left hpm _) _  rcases hpz.dvd_or_dvd hprod with h | h  · apply not_dvd_t hpz c (by rw [hn]; exact dvd_mul_of_dvd_right hpm 2)    have e : t = a0 * m - (a0 * m - t) := by ring    rw [e]; exact dvd_sub (dvd_mul_of_dvd_right hpm _) h  · apply not_dvd_t hpz c' (by rw [hn']; exact dvd_mul_of_dvd_right hpm' 2)    have e : t' = a0 * m' - (a0 * m' - t') := by ring    rw [e]; exact dvd_sub (dvd_mul_of_dvd_right hpm' _) h namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- Case II of prop:smith for one configuration (i, j, k): d₁ = 1 and d₂ divides the three    reduced minors m_i t_j − m_j t_i, … (= d_ij/2). -/theorem tierC_II_gen {ηi ηj ηk ϑi ϑj ϑk : O}    {ei ej ek ni nj nk ti tj tk Δi Δj Δk mi mj mk c' : ℤ}    (hset : Set.range P.vη = {1, ηi, ηj, ηk})    (hliη : LinearIndependent ℤ ![1, ηi, ηj, ηk])    (hli : LinearIndependent ℤ ![1, ϑi, ϑj, ϑk])    (hiA : ηi ∈ Aring M) (hjA : ηj ∈ Aring M)    (hi : 2 * ηi - ei = ϑi) (hj : 2 * ηj - ej = ϑj) (hk : 2 * ηk - ek = ϑk)    (hx : 4 * M.x 0 = P.a + ni * ϑi + nj * ϑj + nk * ϑk)    (hΔi : ϑi * ϑi = Δi) (hΔj : ϑj * ϑj = Δj) (hΔk : ϑk * ϑk = Δk)    (pij : (ni * nj : O) * (ϑi * ϑj) = (P.a * nk - 4 * tk : O) * ϑk)    (pik : (ni * nk : O) * (ϑi * ϑk) = (P.a * nj - 4 * tj : O) * ϑj)    (pjk : (nj * nk : O) * (ϑj * ϑk) = (P.a * ni - 4 * ti : O) * ϑi)    (hrel : (mi * mj : O) * (ϑi * ϑj) = c' * ϑk)    (hni : ni = 2 * mi) (hnj : nj = 2 * mj) (hnk : nk = 2 * mk)    (hmi : ¬ Even mi) (hmj : ¬ Even mj) (hmk : Even mk)    (hti : ¬ Even ti) (htj : ¬ Even tj) (htk : ¬ Even tk) (hc' : ¬ Even c')    (he : ¬ Even (ei + ej)) (hcop : IsCoprime mi mj) (copi : IsCoprime ni ti)    (copj : IsCoprime nj tj) (hxL : ∀ m, M.x m ∈ P.Lℓ) (B : Louboutin.Triang P.MQ) :    B.d₁ = 1 ∧ (B.d₂ : ℤ) ∣ mi * tj - mj * ti ∧ (B.d₂ : ℤ) ∣ mi * tk - mk * ti ∧      (B.d₂ : ℤ) ∣ mj * tk - mk * tj := by  obtain ⟨K, hE⟩ := eta_rel M hi hj hk hrel  have hm : ¬ Even (mi * mj) := by rw [Int.even_mul]; tauto  have hA := (P.caseII_gen hset hliη hiA hjA (eta_sq M hi hΔi) (eta_sq M hj hΔj)    (eta_sq M hk hΔk) hE hc' hm hxL).2.2.2  have hm0 : mi * mj ≠ 0 := by rintro h; rw [h] at hm; exact hm ⟨0, rfl⟩  have hL : P.Lℓ = Submodule.span ℤ (Set.range ![1, ηi, ηj, ηk]) := by rw [range_four, ← hset]  have form : ∀ z ∈ Aring M, ∃ c0 ci cj ck κ : ℤ,      z = c0 + ci * ηi + cj * ηj + ck * ηk + κ * (ηi * ηj) := by    intro z hz    obtain ⟨l, hl, κ, rfl⟩ := P.decomp (hA z hz)    rw [hL] at hl    obtain ⟨c, rfl⟩ := (Submodule.mem_span_range_iff_exists_fun ℤ).mp hl    refine ⟨c 0, c 1, c 2, c 3, κ, ?_⟩    simp [Fin.sum_univ_four, zsmul_eq_mul]  constructor  · obtain ⟨y, hyA, hy⟩ := P.tri_y1 B    obtain ⟨c0, ci, cj, ck, κ, hw⟩ := form y hyA    have hy' : ((B.d₁ : ℤ) : O) * y = (B.p₁₀ : O) + M.x 0 := by exact_mod_cast hy    obtain ⟨e1, e2⟩ := coeffII_d1 hli hi hj hk hx hrel hm0 hw hy'    rw [hni] at e1; rw [hnj] at e2    have hu := d1_caseII e1 e2 he hcop hmi hmj    rcases Int.isUnit_iff.mp hu with h | h    · exact_mod_cast h    · exfalso; have := B.d₁_pos; omega  · obtain ⟨y, hyA, hy⟩ := P.tri_y2 B    obtain ⟨c0, ci, cj, ck, κ, hw⟩ := form y hyA    have hy' : ((B.d₂ : ℤ) : O) * y = (B.p₂₀ : O) + B.p₂₁ * M.x 0 + M.x 0 ^ 2 := by      exact_mod_cast hy    obtain ⟨e1, e2, e3⟩ := coeffII_d2 hli hi hj hk hx hΔi hΔj hΔk pij pik pjk hrel hm0 hw hy'    rw [hnk] at e1; rw [hni] at e2; rw [hnj] at e3    exact d2_caseII (d := (B.d₂ : ℤ)) (P := B.p₂₁ + P.a) (c := κ) (c' := c') (ci := ci) (cj := cj)      (ck := ck) (ei := ei) (ej := ej) (mi := mi) (mj := mj) (mk := mk) (ti := ti) (tj := tj) (tk := tk)      (by linear_combination e1) (by linear_combination e2) (by linear_combination e3)      hmi hmj hmk hti htj htk he      (fun g hg ht => copi.isUnit_of_dvd' (by rw [hni]; exact dvd_mul_of_dvd_right hg 2) ht)      (fun g hg ht => copj.isUnit_of_dvd' (by rw [hnj]; exact dvd_mul_of_dvd_right hg 2) ht) end Dat namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- Case II of prop:smith (all n_j even): d₁ = 1 and 2d₂ divides d₁₂, d₁₃, d₂₃. -/theorem tierC_II (h1 : 2 ∣ P.n₁) (h2 : 2 ∣ P.n₂) (h3 : 2 ∣ P.n₃) (B : Louboutin.Triang P.MQ) :    B.d₁ = 1 ∧ 2 * (B.d₂ : ℤ) ∣ P.toZD.d₁₂ ∧ 2 * (B.d₂ : ℤ) ∣ P.toZD.d₁₃ ∧      2 * (B.d₂ : ℤ) ∣ P.toZD.d₂₃ := by  obtain ⟨a0, m₁, m₂, m₃, ha, hm₁, hm₂, hm₃, -, hcfg⟩ := P.toZD.even_case h1 h2 h3  change P.a = 2 * a0 at ha  change P.n₁ = 2 * m₁ at hm₁  change P.n₂ = 2 * m₂ at hm₂  change P.n₃ = 2 * m₃ at hm₃  have ot₁ := odd_of_cop_even ⟨P.u₁, P.v₁, P.bez₁⟩ h1  have ot₂ := odd_of_cop_even ⟨P.u₂, P.v₂, P.bez₂⟩ h2  have ot₃ := odd_of_cop_even ⟨P.u₃, P.v₃, P.bez₃⟩ h3  have c₁ : IsCoprime P.n₁ P.t₁ := ⟨P.u₁, P.v₁, P.bez₁⟩  have c₂ : IsCoprime P.n₂ P.t₂ := ⟨P.u₂, P.v₂, P.bez₂⟩  have c₃ : IsCoprime P.n₃ P.t₃ := ⟨P.u₃, P.v₃, P.bez₃⟩  have hxL := P.x_in_L h1 h2 h3  have hliη := linIndep_of_D M P.D_vη_ne  have hliϑ := linIndep_of_D M P.D_vϑ_ne  have Q₃ : m₁ * m₂ * (m₃ ^ 2 * P.Δ₃) = (a0 * m₁ - P.t₁) * (a0 * m₂ - P.t₂) := by    have := P.toZD.P₃    change P.n₁ * P.n₂ * P.n₃ ^ 2 * P.Δ₃ = (P.a * P.n₁ - 4 * P.t₁) * (P.a * P.n₂ - 4 * P.t₂) at this    rw [ha, hm₁, hm₂, hm₃] at this    apply mul_left_cancel₀ (show (16 : ℤ) ≠ 0 by norm_num)    linear_combination this  have Q₂ : m₁ * m₃ * (m₂ ^ 2 * P.Δ₂) = (a0 * m₁ - P.t₁) * (a0 * m₃ - P.t₃) := by    have := P.toZD.P₂    change P.n₁ * P.n₃ * P.n₂ ^ 2 * P.Δ₂ = (P.a * P.n₁ - 4 * P.t₁) * (P.a * P.n₃ - 4 * P.t₃) at this    rw [ha, hm₁, hm₂, hm₃] at this    apply mul_left_cancel₀ (show (16 : ℤ) ≠ 0 by norm_num)    linear_combination this  have Q₁ : m₂ * m₃ * (m₁ ^ 2 * P.Δ₁) = (a0 * m₂ - P.t₂) * (a0 * m₃ - P.t₃) := by    have := P.toZD.P₁    change P.n₂ * P.n₃ * P.n₁ ^ 2 * P.Δ₁ = (P.a * P.n₂ - 4 * P.t₂) * (P.a * P.n₃ - 4 * P.t₃) at this    rw [ha, hm₁, hm₂, hm₃] at this    apply mul_left_cancel₀ (show (16 : ℤ) ≠ 0 by norm_num)    linear_combination this  have codd : ∀ {m t : ℤ}, Even m → ¬ Even t → ¬ Even (a0 * m - t) := by    intro m t hm ht h    rw [Int.even_sub, Int.even_mul] at h    tauto  have two : ∀ {d x : ℤ}, d ∣ x → 2 * d ∣ 2 * x := fun h => mul_dvd_mul_left 2 h  have twoN : ∀ {d x : ℤ}, d ∣ x → 2 * d ∣ -(2 * x) := fun h => dvd_neg.mpr (mul_dvd_mul_left 2 h)  simp only [ZD.d₁₂, ZD.d₁₃, ZD.d₂₃, toZD]  rw [hm₁, hm₂, hm₃]  rcases hcfg with ⟨o1, o2, e3, he⟩ | ⟨o1, e2, o3, he⟩ | ⟨e1, o2, o3, he⟩  · -- (i, j, k) = (1, 2, 3)    have hrel : ((m₁ : O) * m₂) * (P.ϑ₁ * P.ϑ₂) = ((a0 * m₃ - P.t₃ : ℤ) : O) * P.ϑ₃ := by      have := P.prod₃      rw [hm₁, hm₂, hm₃, ha] at this      push_cast at this ⊢      apply mul_left_cancel₀ (show (4 : O) ≠ 0 by norm_num)      linear_combination this    obtain ⟨d1, f12, f13, f23⟩ := P.tierC_II_gen (by simp only [vη]; exact range_four _ _ _ _)      hliη hliϑ P.η₁_mem P.η₂_mem P.two_η₁ P.two_η₂ P.two_η₃ P.four_x0 P.hΔ₁ P.hΔ₂ P.hΔ₃      P.prod₃ P.prod₂ P.prod₁ hrel hm₁ hm₂ hm₃ o1 o2 e3 ot₁ ot₂ ot₃ (codd e3 ot₃) he      (cop_m hm₁ hm₂ c₁ c₂ Q₃) c₁ c₂ hxL B    refine ⟨d1, ?_, ?_, ?_⟩    · convert two f12 using 1; ring    · convert two f13 using 1; ring    · convert two f23 using 1; ring  · -- (i, j, k) = (1, 3, 2)    have hrel : ((m₁ : O) * m₃) * (P.ϑ₁ * P.ϑ₃) = ((a0 * m₂ - P.t₂ : ℤ) : O) * P.ϑ₂ := by      have := P.prod₂      rw [hm₁, hm₂, hm₃, ha] at this      push_cast at this ⊢      apply mul_left_cancel₀ (show (4 : O) ≠ 0 by norm_num)      linear_combination this    have hliη' : LinearIndependent ℤ ![1, P.η₁, P.η₃, P.η₂] := by      have := hliη.comp ![0, 1, 3, 2] (by decide)      convert this using 1      funext k; fin_cases k <;> rfl    have hliϑ' : LinearIndependent ℤ ![1, P.ϑ₁, P.ϑ₃, P.ϑ₂] := by      have := hliϑ.comp ![0, 1, 3, 2] (by decide)      convert this using 1      funext k; fin_cases k <;> rfl    obtain ⟨d1, f13, f12, f32⟩ := P.tierC_II_gen (by        simp only [vη]; rw [range_four]; ext z; simp only [Set.mem_insert_iff,          Set.mem_singleton_iff]; tauto)      hliη' hliϑ' P.η₁_mem P.η₃_mem P.two_η₁ P.two_η₃ P.two_η₂      (by linear_combination P.four_x0) P.hΔ₁ P.hΔ₃ P.hΔ₂      P.prod₂ P.prod₃ (by linear_combination P.prod₁) hrel hm₁ hm₃ hm₂ o1 o3 e2 ot₁ ot₃ ot₂      (codd e2 ot₂) he (cop_m hm₁ hm₃ c₁ c₃ Q₂) c₁ c₃ hxL B    refine ⟨d1, ?_, ?_, ?_⟩    · convert two f12 using 1; ring    · convert two f13 using 1; ring    · convert twoN (x := m₃ * P.t₂ - m₂ * P.t₃) f32 using 1; ring  · -- (i, j, k) = (2, 3, 1)    have hrel : ((m₂ : O) * m₃) * (P.ϑ₂ * P.ϑ₃) = ((a0 * m₁ - P.t₁ : ℤ) : O) * P.ϑ₁ := by      have := P.prod₁      rw [hm₁, hm₂, hm₃, ha] at this      push_cast at this ⊢      apply mul_left_cancel₀ (show (4 : O) ≠ 0 by norm_num)      linear_combination this    have hliη' : LinearIndependent ℤ ![1, P.η₂, P.η₃, P.η₁] := by      have := hliη.comp ![0, 2, 3, 1] (by decide)      convert this using 1      funext k; fin_cases k <;> rfl    have hliϑ' : LinearIndependent ℤ ![1, P.ϑ₂, P.ϑ₃, P.ϑ₁] := by      have := hliϑ.comp ![0, 2, 3, 1] (by decide)      convert this using 1      funext k; fin_cases k <;> rfl    obtain ⟨d1, f23, f21, f31⟩ := P.tierC_II_gen (by        simp only [vη]; rw [range_four]; ext z; simp only [Set.mem_insert_iff,          Set.mem_singleton_iff]; tauto)      hliη' hliϑ' P.η₂_mem P.η₃_mem P.two_η₂ P.two_η₃ P.two_η₁      (by linear_combination P.four_x0) P.hΔ₂ P.hΔ₃ P.hΔ₁      P.prod₁ (by linear_combination P.prod₃) (by linear_combination P.prod₂) hrel hm₂ hm₃ hm₁      o2 o3 e1 ot₂ ot₃ ot₁ (codd e1 ot₁) he (cop_m hm₂ hm₃ c₂ c₃ Q₁) c₂ c₃ hxL B    refine ⟨d1, ?_, ?_, ?_⟩    · convert twoN (x := m₂ * P.t₁ - m₁ * P.t₂) f21 using 1; ring    · convert twoN (x := m₃ * P.t₁ - m₁ * P.t₃) f31 using 1; ring    · convert two f23 using 1; ring end Dat namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) theorem e_pos : 0 < P.toZD.m0 / P.toZD.h0 :=  Nat.div_pos (Nat.le_of_dvd P.toZD.m0_pos P.toZD.h0_dvd_m0) (Nat.pos_of_ne_zero P.toZD.h0_ne_zero) /-- prop:smith, lower bound: m₀/h₀ ∣ d₂ (via the element (2Nα² − K₀ − K₁α)/(2e) ∈ A ∩ V₂). -/theorem e_dvd_d2 (B : Louboutin.Triang P.MQ) :    ((P.toZD.m0 / P.toZD.h0 : ℕ) : ℤ) ∣ (B.d₂ : ℤ) := by  set e : ℕ := P.toZD.m0 / P.toZD.h0 with he  have he0 : (e : ℤ) ≠ 0 := by exact_mod_cast P.e_pos.ne'  have hem : (e : ℤ) ∣ (P.toZD.m0 : ℤ) := Int.natCast_dvd_natCast.mpr (Nat.div_dvd_of_dvd P.toZD.h0_dvd_m0)  obtain ⟨f13, hf13⟩ := hem.trans P.toZD.m0_dvd₁₃  obtain ⟨f23, hf23⟩ := hem.trans P.toZD.m0_dvd₂₃  obtain ⟨K0, K1, hY⟩ := P.key_Y  set w : O := ((P.n₂ * f13 : ℤ) : O) * M.x 1 + ((P.n₁ * f23 : ℤ) : O) * M.x 2 with hwdef  have hwA : w ∈ Aring M :=    add_mem (mul_mem (intCast_mem _ _) (x_mem_A M 1)) (mul_mem (intCast_mem _ _) (x_mem_A M 2))  have h2e : (2 * e : ℤ) ≠ 0 := mul_ne_zero two_ne_zero he0  have hφ := (P.φ_eq_iff ⟨w, hwA⟩ h2e ![-K0, -K1, 2 * (P.n₁ * P.n₂ * P.n₃), 0]).mp (by    simp [Fin.sum_univ_four, hwdef]    rw [hf13, hf23] at hY    push_cast at hY ⊢    linear_combination -hY)  have hmem : P.φ ⟨w, hwA⟩ ∈ P.MQ := ⟨⟨w, hwA⟩, rfl⟩  have h3 : P.φ ⟨w, hwA⟩ 3 = 0 := by rw [hφ]; simp  obtain ⟨n, hn3, hcomb⟩ := B.inter_V2 hmem h3  have h2 := congrFun hcomb 2  rw [hφ, B.comb_apply_two, hn3] at h2  simp only [Matrix.cons_val, Int.cast_zero, zero_mul, zero_div, add_zero] at h2  have hd2 : (B.d₂ : ℚ) ≠ 0 := B.d₂_q  have h2eq : ((2 * e : ℤ) : ℚ) ≠ 0 := by exact_mod_cast h2e  rw [div_eq_div_iff h2eq hd2] at h2  have hz : (2 * (P.n₁ * P.n₂ * P.n₃)) * (B.d₂ : ℤ) = n 2 * (2 * e) := by exact_mod_cast h2  have hz' : (e : ℤ) ∣ (P.n₁ * P.n₂ * P.n₃) * B.d₂ :=    ⟨n 2, by linarith⟩  exact P.toZD.coprime_e_N.dvd_of_dvd_mul_left hz' /-- **prop:smith (core)**: for the triangular basis of A (prop:triang, cited), d₁ = 1 and    d₂ = m₀/h₀. -/theorem d1_d2 (B : Louboutin.Triang P.MQ) : B.d₁ = 1 ∧ B.d₂ = P.toZD.m0 / P.toZD.h0 := by  have hlow := P.e_dvd_d2 B  by_cases hev : 2 ∣ P.n₁ ∧ 2 ∣ P.n₂ ∧ 2 ∣ P.n₃  · obtain ⟨d1, f12, f13, f23⟩ := P.tierC_II hev.1 hev.2.1 hev.2.2 B    refine ⟨d1, ?_⟩    have hh := P.toZD.h0_two_of_even hev.1 hev.2.1 hev.2.2    have hm : 2 * (B.d₂ : ℤ) ∣ (P.toZD.m0 : ℤ) := P.toZD.dvd_m0 f12 f13 f23    have hm' : 2 * B.d₂ ∣ P.toZD.m0 := by exact_mod_cast hm    have hup : B.d₂ ∣ P.toZD.m0 / P.toZD.h0 := by      rw [hh]; exact Nat.dvd_div_of_mul_dvd hm'    exact Nat.dvd_antisymm hup (by exact_mod_cast hlow)  · have hodd : ¬ Even P.n₁ ∨ ¬ Even P.n₂ ∨ ¬ Even P.n₃ := by      simp only [even_iff_two_dvd]; tauto    obtain ⟨d1, f12, f13, f23⟩ := P.tierC_I hodd B    refine ⟨d1, ?_⟩    have hh := P.toZD.h0_one_of_odd (by simp only [toZD, ← even_iff_two_dvd]; exact hodd)    have hm : (B.d₂ : ℤ) ∣ (P.toZD.m0 : ℤ) := P.toZD.dvd_m0 f12 f13 f23    have hup : B.d₂ ∣ P.toZD.m0 / P.toZD.h0 := by rw [hh, Nat.div_one]; exact_mod_cast hm    exact Nat.dvd_antisymm hup (by exact_mod_cast hlow) end Dat /-! ## Part 13. Statements: prop:discA, prop:smith, thm:disc -/ namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- A ≃ φ(A) (additive), carrying ℤ[α] onto ℤ⁴. -/noncomputable def φequiv : (Aring M).toAddSubgroup ≃+ P.MQ :=  AddEquiv.ofBijective P.φ.rangeRestrict    ⟨AddMonoidHom.rangeRestrict_injective_iff.mpr P.φ_injective, P.φ.rangeRestrict_surjective⟩ theorem quot_equiv : Nonempty (((Aring M).toAddSubgroup ⧸    (Lat (vpow M)).addSubgroupOf (Aring M).toAddSubgroup) ≃+    (P.MQ ⧸ Louboutin.Z4.addSubgroupOf P.MQ)) := by  refine ⟨QuotientAddGroup.congr _ _ P.φequiv ?_⟩  ext z  simp only [AddSubgroup.mem_map, AddSubgroup.mem_addSubgroupOf]  constructor  · rintro ⟨y, hy, rfl⟩    exact (P.mem_Zα_iff y).mp hy  · intro hz    refine ⟨P.φequiv.symm z, ?_, by simp⟩    rw [P.mem_Zα_iff]    have : P.φ (P.φequiv.symm z) = (z : Fin 4 → ℚ) := by      have h := congrArg Subtype.val (P.φequiv.apply_symm_apply z)      exact h    rw [this]; exact hz /-- **prop:discA** (in the model): with g = gcd(T, U, V, W),    g ∣ Δ_α, [A : ℤ[α]] = |Δ_α|/g, g² = Δ₁Δ₂Δ₃, and D(b)² = g² for every family b spanning A    (disc A = D(b)², b a ℤ-basis of A). -/theorem prop_discA {t u v w : ℤ} (ht : T M.x = t) (hu : U M.x = u) (hv : V M.x = v)    (hw : W M.x = w) :    Int.gcd (Int.gcd t u) (Int.gcd v w) ∣ P.δ.natAbs ∧      (Lat (vpow M)).relIndex (Aring M).toAddSubgroup        = P.δ.natAbs / Int.gcd (Int.gcd t u) (Int.gcd v w) ∧      ((Int.gcd (Int.gcd t u) (Int.gcd v w) : ℕ) : ℤ) ^ 2 = P.Δ₁ * P.Δ₂ * P.Δ₃ ∧      ∀ b : Fin 4 → O, Lat b = (Aring M).toAddSubgroup →        D M b ^ 2 = ((Int.gcd (Int.gcd t u) (Int.gcd v w) : ℕ) : O) ^ 2 := by  have hg := P.gcd_eq ht hu hv hw  have hI := P.index_mul_Θ  have hΘ0 : P.Θ.natAbs ≠ 0 := Int.natAbs_ne_zero.mpr P.Θ_ne  rw [hg]  refine ⟨⟨_, by rw [← hI, mul_comm]⟩, ?_, ?_, ?_⟩  · rw [← hI, Nat.mul_div_cancel _ (Nat.pos_of_ne_zero hΘ0)]  · rw [Int.natCast_natAbs, sq_abs, sq, P.hΘ2]  · intro b hb    -- the powers of α lie in A = ⟨b⟩    have hmem : ∀ l, vpow M l ∈ Submodule.span ℤ (Set.range b) := by      intro l      have : vpow M l ∈ (Aring M).toAddSubgroup := pow_mem (x_mem_A M 0) _      rw [← hb] at this; exact this    -- D(b) ≠ 0 since D(powers) = Δ ≠ 0    choose c hc using fun l => (Submodule.mem_span_range_iff_exists_fun ℤ).mp (hmem l)    have hDc := D_comb M b (vpow M) (Matrix.of c) (fun l => by rw [← hc l]; simp [zsmul_eq_mul])    have hb0 : D M b ≠ 0 := by      intro h0; apply D_vpow_ne (M := M); rw [hDc, h0, zero_mul]    obtain ⟨C, hD, hidx⟩ := relIndex_D M hb0 (D_vpow_ne (M := M)) hmem    rw [hb] at hidx    rw [D_vpow, P.hδ] at hD    -- |det C| · |Θ| = |Δ|, and Δ = D(b) · det C    have h1 : C.det.natAbs * P.Θ.natAbs = P.δ.natAbs := by rw [← hidx]; exact hI    have h2 : (C.det * P.Θ).natAbs = P.δ.natAbs := by rw [Int.natAbs_mul]; exact h1    have hC0 : C.det ≠ 0 := by      intro h0; rw [h0, Int.cast_zero, mul_zero] at hD      exact P.δ_ne (by exact_mod_cast hD)    rcases Int.natAbs_eq_natAbs_iff.mp h2 with h | h    · have : D M b * C.det = (P.Θ : O) * C.det := by        rw [← hD, ← h]; push_cast; ring      have := mul_right_cancel₀ (Int.cast_ne_zero.mpr hC0) this      rw [this, show ((P.Θ.natAbs : ℕ) : O) = ((P.Θ.natAbs : ℤ) : O) from (Int.cast_natCast _).symm,        ← Int.cast_pow, ← Int.cast_pow, Int.natAbs_sq]    · have : D M b * C.det = (-P.Θ : O) * C.det := by        rw [← hD]; push_cast; rw [show (P.δ : O) = ((-(C.det * P.Θ) : ℤ) : O) by rw [h, neg_neg]]        push_cast; ring      have := mul_right_cancel₀ (Int.cast_ne_zero.mpr hC0) this      rw [this, neg_sq, show ((P.Θ.natAbs : ℕ) : O) = ((P.Θ.natAbs : ℤ) : O) from (Int.cast_natCast _).symm,        ← Int.cast_pow, ← Int.cast_pow, Int.natAbs_sq] /-- **prop:smith** (in the model), for the triangular basis B of A given by prop:triang (cited):    h₀ ∈ {1, 2}, h₀ ∣ m₀, d₁ = 1, d₂ = m₀/h₀, d₃ = I h₀/m₀ with I = [A : ℤ[α]],    A/ℤ[α] ≅ ℤ/(m₀/h₀) × ℤ/(I h₀/m₀), and A has a ℤ-basis {1, α, α², ω} iff m₀ = h₀. -/theorem prop_smith (B : Louboutin.Triang P.MQ) :    (P.toZD.h0 = 1 ∨ P.toZD.h0 = 2) ∧ P.toZD.h0 ∣ P.toZD.m0 ∧ B.d₁ = 1 ∧      B.d₂ = P.toZD.m0 / P.toZD.h0 ∧      B.d₃ = (Lat (vpow M)).relIndex (Aring M).toAddSubgroup * P.toZD.h0 / P.toZD.m0 ∧      Nonempty (((Aring M).toAddSubgroup ⧸ (Lat (vpow M)).addSubgroupOf (Aring M).toAddSubgroup)        ≃+ ZMod (P.toZD.m0 / P.toZD.h0) ×          ZMod ((Lat (vpow M)).relIndex (Aring M).toAddSubgroup * P.toZD.h0 / P.toZD.m0)) ∧      ((∃ ω, Louboutin.BasisWith P.MQ ω) ↔ P.toZD.m0 = P.toZD.h0) := by  obtain ⟨d1, d2⟩ := P.d1_d2 B  have hhm := P.toZD.h0_dvd_m0  have hh0 := Nat.pos_of_ne_zero P.toZD.h0_ne_zero  have hm0 := P.toZD.m0_pos  have hidx : (Lat (vpow M)).relIndex (Aring M).toAddSubgroup = B.d₂ * B.d₃ := by    rw [← P.index_MQ, B.index_eq, d1, one_mul]  have hd3 : B.d₃ = (Lat (vpow M)).relIndex (Aring M).toAddSubgroup * P.toZD.h0 / P.toZD.m0 := by    rw [hidx, d2]    obtain ⟨k, hk⟩ := hhm    rw [hk, Nat.mul_div_cancel_left _ hh0]    rw [show k * B.d₃ * P.toZD.h0 = B.d₃ * (P.toZD.h0 * k) by ring,      Nat.mul_div_cancel _ (by rw [← hk]; exact hm0)]  refine ⟨P.toZD.h0_one_or_two, hhm, d1, d2, hd3, ?_, ?_⟩  · obtain ⟨e⟩ := P.quot_equiv    obtain ⟨e'⟩ := B.quotient_equiv_T1 d1    rw [d2, hd3] at e'    exact ⟨e.trans e'⟩  · rw [B.T1_i_iff_ii, B.T1_ii_iff_iii, d2]    obtain ⟨k, hk⟩ := hhm    rw [hk, Nat.mul_div_cancel_left _ hh0]    constructor    · rintro rfl; ring    · intro h      have : P.toZD.h0 * k = P.toZD.h0 * 1 := by rw [mul_one]; exact h      exact Nat.eq_of_mul_eq_mul_left hh0 this end Dat namespace Dat open Matrix Module variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] {M : V4Data O} (P : Dat M) /-- From A = L + ℤy with 2y = λ₀ + (2q+1)g₁ + λ₂g₂ + λ₃g₃ ({g₁, g₂, g₃} = {η₁, η₂, η₃}):    the four elements 1, y − q g₁, g₂, g₃ span A. -/theorem basis_of_rel {y g₁ g₂ g₃ : O} {l0 q l2 l3 : ℤ}    (hset : Set.range P.vη = {1, g₁, g₂, g₃}) (hyA : y ∈ Aring M)    (hA : ∀ z ∈ Aring M, z ∈ P.Nℓ y)    (hrel : 2 * y = l0 + (2 * q + 1) * g₁ + l2 * g₂ + l3 * g₃) :    Lat ![1, y - q * g₁, g₂, g₃] = (Aring M).toAddSubgroup := by  have gA : ∀ g ∈ ({1, g₁, g₂, g₃} : Set O), g ∈ Aring M := by    intro g hg; rw [← hset] at hg; obtain ⟨k, rfl⟩ := hg; exact P.vη_mem_A k  have g1A := gA g₁ (by simp)  set S := Submodule.span ℤ (Set.range ![1, y - q * g₁, g₂, g₃]) with hS  have mem : ∀ k : Fin 4, ![1, y - q * g₁, g₂, g₃] k ∈ S := fun k => Submodule.subset_span ⟨k, rfl⟩  have m1 : (1 : O) ∈ S := by simpa using mem 0  have my : y - q * g₁ ∈ S := by simpa using mem 1  have m2 : g₂ ∈ S := by simpa using mem 2  have m3 : g₃ ∈ S := by simpa using mem 3  have sm := smul_mem' S  have mg1 : g₁ ∈ S := by    rw [show g₁ = (2 : ℤ) * (y - q * g₁) - (l0 : ℤ) * 1 - l2 * g₂ - l3 * g₃ by      push_cast; linear_combination -hrel]    exact sub_mem (sub_mem (sub_mem (sm 2 my) (sm l0 m1)) (sm l2 m2)) (sm l3 m3)  have mY : y ∈ S := by    rw [show y = (y - q * g₁) + (q : ℤ) * g₁ by ring]; exact add_mem my (sm q mg1)  have hNS : P.Nℓ y ≤ S := by    apply Submodule.span_le.mpr    rintro g (hg | hg)    · rw [hset] at hg      simp only [Set.mem_insert_iff, Set.mem_singleton_iff] at hg      rcases hg with rfl | rfl | rfl | rfl      exacts [m1, mg1, m2, m3]    · rw [Set.mem_singleton_iff.mp hg]; exact mY  ext z  constructor  · intro hz    refine span_le_A (v := ![1, y - q * g₁, g₂, g₃]) (fun k => ?_) hz    fin_cases k    · exact one_mem _    · exact sub_mem hyA (mul_mem (intCast_mem _ _) g1A)    · exact gA g₂ (by simp)    · exact gA g₃ (by simp)  · intro hz    exact hNS (hA z hz) include P in/-- A has a family of four elements spanning it (hence, by `prop_discA`, a ℤ-basis with    D(b)² = g² ≠ 0): the statement disc A = g² is not vacuous. -/theorem exists_basis : ∃ b : Fin 4 → O, Lat b = (Aring M).toAddSubgroup := by  obtain ⟨y, hyA, h2L, hnL, hA⟩ := P.exists_y  obtain ⟨c, hc⟩ := P.mem_Lℓ_iff.mp h2L  rw [P.sum_vη] at hc  -- some coordinate c₁, c₂, c₃ of 2y is odd  by_cases h1 : Even (c 1)  · by_cases h2 : Even (c 2)    · by_cases h3 : Even (c 3)      · exfalso        obtain ⟨μ1, hμ1⟩ := h1        obtain ⟨μ2, hμ2⟩ := h2        obtain ⟨μ3, hμ3⟩ := h3        obtain ⟨k, hk⟩ := half_int M (k := c 0) (w := y - (μ1 * P.η₁ + μ2 * P.η₂ + μ3 * P.η₃)) (by          rw [hμ1, hμ2, hμ3] at hc; push_cast at hc; linear_combination hc)        apply hnL        rw [show y = (k : O) + μ1 * P.η₁ + μ2 * P.η₂ + μ3 * P.η₃ by linear_combination hk]        have sm := smul_mem' P.Lℓ        exact add_mem (add_mem (add_mem (by simpa using sm k P.one_Lℓ) (sm _ P.η₁_Lℓ))          (sm _ P.η₂_Lℓ)) (sm _ P.η₃_Lℓ)      · obtain ⟨q, hq⟩ := Int.not_even_iff_odd.mp h3        exact ⟨_, P.basis_of_rel (g₁ := P.η₃) (g₂ := P.η₁) (g₃ := P.η₂) (q := q) (l0 := c 0) (l2 := c 1) (l3 := c 2)          (by simp only [vη]; rw [range_four]; ext z; simp only [Set.mem_insert_iff,            Set.mem_singleton_iff]; tauto) hyA hA          (by rw [hq] at hc; push_cast at hc; linear_combination -hc)⟩    · obtain ⟨q, hq⟩ := Int.not_even_iff_odd.mp h2      exact ⟨_, P.basis_of_rel (g₁ := P.η₂) (g₂ := P.η₁) (g₃ := P.η₃) (q := q) (l0 := c 0) (l2 := c 1) (l3 := c 3)        (by simp only [vη]; rw [range_four]; ext z; simp only [Set.mem_insert_iff,          Set.mem_singleton_iff]; tauto) hyA hA        (by rw [hq] at hc; push_cast at hc; linear_combination -hc)⟩  · obtain ⟨q, hq⟩ := Int.not_even_iff_odd.mp h1    exact ⟨_, P.basis_of_rel (g₁ := P.η₁) (g₂ := P.η₂) (g₃ := P.η₃) (q := q) (l0 := c 0) (l2 := c 2) (l3 := c 3)      (by simp only [vη]; exact range_four _ _ _ _) hyA hA      (by rw [hq] at hc; push_cast at hc; linear_combination -hc)⟩ end Dat /-! ## Part 14. Theorem B (thm:disc) in the V4 model -/ /-- the note's n_j = D_j/gcd(D_j, E_j) (or 0), D_j = a² − 4b + 4z_j, E_j = a z_j − 2c. -/def noteN (a b c z : ℤ) : ℤ := ntN (a ^ 2 - 4 * b + 4 * z) (a * z - 2 * c)/-- the note's t_j = E_j/gcd(D_j, E_j) (or 1). -/def noteT (a b c z : ℤ) : ℤ := ntT (a ^ 2 - 4 * b + 4 * z) (a * z - 2 * c) /-- the note's h₀ = gcd(n₁, n₂, n₃). -/def h0N (a b c z₁ z₂ z₃ : ℤ) : ℕ :=  Int.gcd (Int.gcd (noteN a b c z₁) (noteN a b c z₂)) (noteN a b c z₃) /-- the note's m₀ = gcd(d₁₂, d₁₃, d₂₃), d_ij = n_i t_j − n_j t_i. -/def m0N (a b c z₁ z₂ z₃ : ℤ) : ℕ :=  Int.gcd (Int.gcd (noteN a b c z₁ * noteT a b c z₂ - noteN a b c z₂ * noteT a b c z₁)    (noteN a b c z₁ * noteT a b c z₃ - noteN a b c z₃ * noteT a b c z₁))    (noteN a b c z₂ * noteT a b c z₃ - noteN a b c z₃ * noteT a b c z₂) variable {O : Type*} [CommRing O] [IsDomain O] [CharZero O] /-- **Theorem B (thm:disc)** in the abstract V4 model. Let a, b, c, d be the coefficients of    Π_α, z₁, z₂, z₃ the roots of the resolvent, Δ = Δ_α, T, U, V, W as in the note,    g = gcd(T, U, V, W), and (n_j, t_j), h₀, m₀ as in the note (`noteN`, `noteT`, `h0N`, `m0N`).    Then, for A = ℤ[x₁, …, x₄] and ℤ[α] = ℤ + ℤα + ℤα² + ℤα³ (`Lat (Dat.vpow M)`):    h₀ ∈ {1, 2}, h₀ ∣ m₀, g ∣ Δ, [A : ℤ[α]] = |Δ|/g, disc A = g² (D(b)² = g² for every family b    spanning A, and such b exist); and for the triangular basis of A (prop:triang, cited,    `Triang`, in the coordinates φ : A ↪ ℚ⁴ of `Dat.MQ`): d₁ = 1, d₂ = m₀/h₀,    d₃ = (|Δ|/g)h₀/m₀, A/ℤ[α] ≅ ℤ/(m₀/h₀) × ℤ/((|Δ|/g)h₀/m₀), and A has a ℤ-basis    {1, α, α², ω} iff m₀ = h₀. -/theorem thm_disc (M : V4Data O) {a b c d z₁ z₂ z₃ t u v w δ : ℤ}    (ha : ea M.x = a) (hb : eb M.x = b) (hc : ec M.x = c) (hd : ed M.x = d)    (hz₁ : z1 M.x = z₁) (hz₂ : z2 M.x = z₂) (hz₃ : z3 M.x = z₃) (hδ : Vand M.x = δ)    (ht : T M.x = t) (hu : U M.x = u) (hv : V M.x = v) (hw : W M.x = w) :    (h0N a b c z₁ z₂ z₃ = 1 ∨ h0N a b c z₁ z₂ z₃ = 2) ∧    h0N a b c z₁ z₂ z₃ ∣ m0N a b c z₁ z₂ z₃ ∧    Int.gcd (Int.gcd t u) (Int.gcd v w) ∣ δ.natAbs ∧    (Lat (Dat.vpow M)).relIndex (Aring M).toAddSubgroup = δ.natAbs / Int.gcd (Int.gcd t u) (Int.gcd v w) ∧    (∀ β : Fin 4 → O, Lat β = (Aring M).toAddSubgroup →      D M β ^ 2 = ((Int.gcd (Int.gcd t u) (Int.gcd v w) : ℕ) : O) ^ 2) ∧    (∃ β : Fin 4 → O, Lat β = (Aring M).toAddSubgroup) ∧    ∃ P : Dat M, P.n₁ = noteN a b c z₁ ∧ P.t₁ = noteT a b c z₁ ∧ P.n₂ = noteN a b c z₂ ∧      P.t₂ = noteT a b c z₂ ∧ P.n₃ = noteN a b c z₃ ∧ P.t₃ = noteT a b c z₃ ∧      ∀ B : Louboutin.Triang P.MQ,        B.d₁ = 1 ∧ B.d₂ = m0N a b c z₁ z₂ z₃ / h0N a b c z₁ z₂ z₃ ∧        B.d₃ = δ.natAbs / Int.gcd (Int.gcd t u) (Int.gcd v w) * h0N a b c z₁ z₂ z₃ /          m0N a b c z₁ z₂ z₃ ∧        Nonempty (((Aring M).toAddSubgroup ⧸ (Lat (Dat.vpow M)).addSubgroupOf (Aring M).toAddSubgroup)          ≃+ ZMod (m0N a b c z₁ z₂ z₃ / h0N a b c z₁ z₂ z₃) ×            ZMod (δ.natAbs / Int.gcd (Int.gcd t u) (Int.gcd v w) * h0N a b c z₁ z₂ z₃ /              m0N a b c z₁ z₂ z₃)) ∧        ((∃ ω, Louboutin.BasisWith P.MQ ω) ↔ m0N a b c z₁ z₂ z₃ = h0N a b c z₁ z₂ z₃) := by  obtain ⟨P, -, -, -, -, -, -, -, hPδ, hn₁, ht₁, hn₂, ht₂, hn₃, ht₃⟩ :=    Dat.exists_note M ha hb hc hd hz₁ hz₂ hz₃ hδ  have hh : P.toZD.h0 = h0N a b c z₁ z₂ z₃ := by    simp only [ZD.h0, Dat.toZD, h0N, noteN, hn₁, hn₂, hn₃]  have hm : P.toZD.m0 = m0N a b c z₁ z₂ z₃ := by    simp only [ZD.m0, ZD.d₁₂, ZD.d₁₃, ZD.d₂₃, Dat.toZD, m0N, noteN, noteT, hn₁, hn₂, hn₃,      ht₁, ht₂, ht₃]  obtain ⟨hg, hI, -, hdisc⟩ := P.prop_discA ht hu hv hw  rw [hPδ] at hg hI  refine ⟨hh ▸ P.toZD.h0_one_or_two, hh ▸ hm ▸ P.toZD.h0_dvd_m0, hg, hI, hdisc,    P.exists_basis, P, hn₁, ht₁, hn₂, ht₂, hn₃, ht₃, fun B => ?_⟩  obtain ⟨-, -, d1, d2, d3, iso, bas⟩ := P.prop_smith B  rw [hh, hm] at d2 d3 iso bas  rw [hI] at d3 iso  exact ⟨d1, d2, d3, iso, bas⟩ end Louboutin.DiscV4