-- Adjoining a square root to a subfield of the reals: F(√d) as the set of -- a + b√d with a, b in F, a subfield when d ∈ F is nonnegative, equal to F -- when √d ∈ F, and with unique coefficients when √d ∉ F. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.Algebra.Field.QuadraticAdjoin import Grace.Cumulus.Algebra.Field.RealSubfield -- The definition and its membership -- The total realSqrt keeps the comprehension unconditional; the laws below -- take d nonnegative, where realSqrt(d) is √d. def adjoinSqrt (F d : Set) : Set := { z ∈ ℝ | ∃ a ∈ F, ∃ b ∈ F, z = (a:R) + (b:R) * (realSqrt(d):R) } theorem adjoinSqrt.mem_iff (F d z : Set) shows z ∈ adjoinSqrt(F, d) ↔ z ∈ ℝ ∧ ∃ a ∈ F, ∃ b ∈ F, z = (a:R) + (b:R) * (realSqrt(d):R) proof simp theorem adjoinSqrt.subset_reals (F d : Set) shows adjoinSqrt(F, d) ⊆ ℝ proof intros z member obtain ⟨inReals, _⟩ := adjoinSqrt.mem_iff.forward with member exact inReals theorem adjoinSqrt.intro (F : Set) (d a b : Real) requires field : RealSubfield(F) requires dNonneg : (0:R) ≤ d requires aIn : a ∈ F requires bIn : b ∈ F shows a + b * √d ∈ adjoinSqrt(F, d) proof have sumReal := realAddValue.real at a, b * √d apply adjoinSqrt.mem_iff.backward refine ⟨sumReal, a, aIn, b, bIn, ?_⟩ unfold sqrt rfl theorem adjoinSqrt.cases (F : Set) (d : Real) (r : Set) requires dNonneg : (0:R) ≤ d requires rIn : r ∈ adjoinSqrt(F, d) shows ∃ a ∈ F, ∃ b ∈ F, r = (a:R) + (b:R) * √d proof obtain ⟨_, a, aIn, b, bIn, same⟩ := adjoinSqrt.mem_iff.forward with rIn refine ⟨a, aIn, b, bIn, ?_⟩ unfold sqrt exact same -- The inclusions -- x = x + 0 · √d. theorem adjoinSqrt.contains (F : Set) (d : Real) requires field : RealSubfield(F) requires dNonneg : (0:R) ≤ d shows F ⊆ adjoinSqrt(F, d) proof intros x xIn have realX := RealSubfield.real with field, xIn have member := adjoinSqrt.intro at F, d, x, (0:R) with * have same : (x:R) + (0:R) * √d = x by ring rwa same at member -- √d = 0 + 1 · √d. theorem adjoinSqrt.sqrt_mem (F : Set) (d : Real) requires field : RealSubfield(F) requires dNonneg : (0:R) ≤ d shows √d ∈ adjoinSqrt(F, d) proof have member := adjoinSqrt.intro at F, d, (0:R), (1:R) with * have same : (0:R) + (1:R) * √d = √d by ring rwa same at member theorem adjoinSqrt.mono (F G d : Set) requires sub : F ⊆ G shows adjoinSqrt(F, d) ⊆ adjoinSqrt(G, d) proof intros z member obtain ⟨inReals, a, aIn, b, bIn, same⟩ := adjoinSqrt.mem_iff.forward with member have aInG := sub with a, aIn have bInG := sub with b, bIn apply adjoinSqrt.mem_iff.backward exact ⟨inReals, a, aInG, b, bInG, same⟩ -- Unique coefficients and the subfield -- If b ≠ 0 then √d = -a / b lies in F. theorem adjoinSqrt.coefficients_unique (F : Set) (d a b : Real) requires field : RealSubfield(F) requires dNonneg : (0:R) ≤ d requires rootOut : √d ∉ F requires aIn : a ∈ F requires bIn : b ∈ F requires zero : a + b * √d = 0 shows a = 0 ∧ b = 0 proof have bZero : b = 0 by_contra nonzero have unit := realInvValue.mul_right with nonzero have quotient : √d = (-a) * realInvValue(b) linear_combination (-√d) * unit + realInvValue(b) * zero have inverseIn := RealSubfield.inv_mem with field, bIn, nonzero have negIn := RealSubfield.neg_mem with field, aIn have productIn := RealSubfield.mul_mem with field, negIn, inverseIn rw ← quotient at productIn exact rootOut with productIn have aZero : a = 0 rw bZero at zero linear_combination zero exact ⟨aZero, bZero⟩ theorem adjoinSqrt.eq_of_sqrt_mem (F : Set) (d : Real) requires field : RealSubfield(F) requires dNonneg : (0:R) ≤ d requires rootIn : √d ∈ F shows adjoinSqrt(F, d) = F proof have includes := adjoinSqrt.contains with field, dNonneg have least : adjoinSqrt(F, d) ⊆ F intros z member obtain ⟨a, aIn, b, bIn, same⟩ := adjoinSqrt.cases with dNonneg, member have realA := RealSubfield.real with field, aIn have realB := RealSubfield.real with field, bIn rw same typecheck exact Subset.antisymmetric with least, includes -- The inverse of a + b√d is (a − b√d) / (a² − b²d). When √d ∈ F the -- element lies in F and its inverse with it; otherwise a² − b²d ≠ 0 by -- coefficients_unique applied to a − b√d. theorem adjoinSqrt.subfield (F : Set) (d : Real) requires field : RealSubfield(F) requires dIn : d ∈ F requires dNonneg : (0:R) ≤ d shows RealSubfield(adjoinSqrt(F, d)) proof have bounded := adjoinSqrt.subset_reals at F, d have includes := adjoinSqrt.contains with field, dNonneg have nonnegative := NonnegativeReal.of_le with dNonneg have square := sqrt.square at d with nonnegative have zeroIn := RealSubfield.zero_mem with field have oneIn := RealSubfield.one_mem with field have zeroMember := includes with (0:R), zeroIn have oneMember := includes with (1:R), oneIn have negatives : ∀ x ∈ adjoinSqrt(F, d), -(x:R) ∈ adjoinSqrt(F, d) intros x xIn obtain ⟨a, aIn, b, bIn, same⟩ := adjoinSqrt.cases with dNonneg, xIn have realA := RealSubfield.real with field, aIn have realB := RealSubfield.real with field, bIn have negA := RealSubfield.neg_mem with field, aIn have negB := RealSubfield.neg_mem with field, bIn have member := adjoinSqrt.intro with field, dNonneg, negA, negB have value : -(x:R) = -(a:R) + (-(b:R)) * √d rw same ring rwa value have sums : ∀ x y ∈ adjoinSqrt(F, d), (x:R) + (y:R) ∈ adjoinSqrt(F, d) intros x xIn y yIn obtain ⟨a, aIn, b, bIn, sameX⟩ := adjoinSqrt.cases with dNonneg, xIn obtain ⟨p, pIn, q, qIn, sameY⟩ := adjoinSqrt.cases with dNonneg, yIn have realA := RealSubfield.real with field, aIn have realB := RealSubfield.real with field, bIn have realP := RealSubfield.real with field, pIn have realQ := RealSubfield.real with field, qIn have firstIn := RealSubfield.add_mem with field, aIn, pIn have secondIn := RealSubfield.add_mem with field, bIn, qIn have member := adjoinSqrt.intro with field, dNonneg, firstIn, secondIn have value : (x:R) + (y:R) = ((a:R) + (p:R)) + ((b:R) + (q:R)) * √d rw [sameX, sameY] ring rwa value have products : ∀ x y ∈ adjoinSqrt(F, d), (x:R) * (y:R) ∈ adjoinSqrt(F, d) intros x xIn y yIn obtain ⟨a, aIn, b, bIn, sameX⟩ := adjoinSqrt.cases with dNonneg, xIn obtain ⟨p, pIn, q, qIn, sameY⟩ := adjoinSqrt.cases with dNonneg, yIn have realA := RealSubfield.real with field, aIn have realB := RealSubfield.real with field, bIn have realP := RealSubfield.real with field, pIn have realQ := RealSubfield.real with field, qIn have firstIn : (a:R) * (p:R) + (b:R) * (q:R) * d ∈ F by typecheck have secondIn : (a:R) * (q:R) + (p:R) * (b:R) ∈ F by typecheck have member := adjoinSqrt.intro with field, dNonneg, firstIn, secondIn have value : (x:R) * (y:R) = ((a:R) * (p:R) + (b:R) * (q:R) * d) + ((a:R) * (q:R) + (p:R) * (b:R)) * √d rw [sameX, sameY] linear_combination ((b:R) * (q:R)) * square rwa value have inverses : ∀ x ∈ adjoinSqrt(F, d), x ≠ (0:R) → realInvValue(x) ∈ adjoinSqrt(F, d) intros x xIn nonzero obtain ⟨a, aIn, b, bIn, same⟩ := adjoinSqrt.cases with dNonneg, xIn have realA := RealSubfield.real with field, aIn have realB := RealSubfield.real with field, bIn have realX := bounded with x, xIn by_cases rootIn : √d ∈ F . have xInF : x ∈ F rw same typecheck have inverseIn := RealSubfield.inv_mem with field, xInF, nonzero exact includes with realInvValue(x), inverseIn . -- The norm a² − b²d is nonzero: otherwise the conjugate a − b√d -- vanishes and coefficients_unique makes x = 0. have normIn : (a:R) * (a:R) - (b:R) * (b:R) * d ∈ F by typecheck have normNonzero : (a:R) * (a:R) - (b:R) * (b:R) * d ≠ 0 intro normZero have factored : ((a:R) + (b:R) * √d) * ((a:R) + (-(b:R)) * √d) = 0 linear_combination normZero - ((b:R) * (b:R)) * square rcases realMulValue.eq_zero_or with factored with xZero | conjugateZero . apply nonzero rw same exact xZero . have negB := RealSubfield.neg_mem with field, bIn obtain ⟨aZero, negZero⟩ := adjoinSqrt.coefficients_unique with field, dNonneg, rootIn, aIn, negB, conjugateZero apply nonzero rw same linear_combination aZero - √d * negZero have inverseIn := RealSubfield.inv_mem with field, normIn, normNonzero have unit := realInvValue.mul_right with normNonzero have firstIn : (a:R) * realInvValue((a:R) * (a:R) - (b:R) * (b:R) * d) ∈ F typecheck have secondIn : -(b:R) * realInvValue((a:R) * (a:R) - (b:R) * (b:R) * d) ∈ F typecheck have member := adjoinSqrt.intro with field, dNonneg, firstIn, secondIn have product : x * ((a:R) * realInvValue((a:R) * (a:R) - (b:R) * (b:R) * d) + -(b:R) * realInvValue((a:R) * (a:R) - (b:R) * (b:R) * d) * √d) = 1 rw same linear_combination unit - ((b:R) * (b:R) * realInvValue((a:R) * (a:R) - (b:R) * (b:R) * d)) * square have value := realInvValue.eq_of_mul_right with nonzero, product rw ← value exact member exact RealSubfield.mk with bounded, zeroMember, oneMember, negatives, sums, products, inverses