-- Towers of quadratic extensions of the rationals, as lists of -- discriminants with the top discriminant first: the field of a tower, -- the tower condition on every suffix, and the compositum of two towers. -- 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.QuadraticTower import Grace.Cumulus.Algebra.Field.QuadraticAdjoin import Grace.Cumulus.List.TakeDrop -- The definitions -- The head of the list is the last square root adjoined. def towerField (ds : List(ℝ)) → Set | List.nil => rationalReals | List.cons(d, rest) => adjoinSqrt(towerField(rest), d) -- A predicate cannot recurse, so the tower condition quantifies over the -- suffixes listDrop(k, ds): each discriminant is a nonnegative element of -- the field of the discriminants below it. predicate QuadraticTower (ds : List(ℝ)) := ∀ k < length(ds), ∃ d ∈ ℝ, ∃ rest, List(ℝ, rest) ∧ listDrop(k, ds) = d :: rest ∧ d ∈ towerField(rest) ∧ (0:R) ≤ d theorem towerField.nil shows towerField([]) = rationalReals proof rw towerField rfl theorem towerField.cons (ds : List(ℝ)) (d : Real) shows towerField(d :: ds) = adjoinSqrt(towerField(ds), d) proof rw towerField rfl -- The tower condition, built and taken apart theorem QuadraticTower.nil shows QuadraticTower([]) proof unfold QuadraticTower intros k kNat hk rw length.nil at hk exfalso arith -- listDrop.zero for k = 0 and listDrop.succ_cons for the rest. theorem QuadraticTower.cons (ds : List(ℝ)) (d : Real) requires tower : QuadraticTower(ds) requires dIn : d ∈ towerField(ds) requires dNonneg : (0:R) ≤ d shows QuadraticTower(d :: ds) proof have dReal : d ∈ ℝ by typecheck have whole : List(ℝ, d :: ds) by typecheck unfold QuadraticTower at tower unfold QuadraticTower intros k kNat hk rw length.cons at hk rcases natural.zero_or_successor at k with zero | successor . refine ⟨d, dReal, ds, ?_, ?_, dIn, dNonneg⟩ . typecheck . rw [zero, listDrop.zero] rfl . obtain ⟨j, jNat, equal⟩ := successor rw equal at hk have smaller : j < length{ℝ}(ds) by arith obtain ⟨e, eReal, rest, restList, dropped, eIn, eNonneg⟩ := tower with j, jNat, smaller refine ⟨e, eReal, rest, restList, ?_, eIn, eNonneg⟩ rw [equal, listDrop.succ_cons] exact dropped theorem QuadraticTower.head (ds : List(ℝ)) (d : Real) requires tower : QuadraticTower(d :: ds) shows d ∈ towerField(ds) ∧ (0:R) ≤ d proof have dReal : d ∈ ℝ by typecheck have whole : List(ℝ, d :: ds) by typecheck have positive := length.cons_positive at ℝ, d, ds with dReal unfold QuadraticTower at tower have zeroNat : (0:N) ∈ ℕ by typecheck obtain ⟨e, _, rest, _, equal, eIn, eNonneg⟩ := tower with (0:N), zeroNat, positive rw listDrop.zero at equal have sameHead := List.cons_head with equal have sameTail := List.cons_tail with equal rw [sameHead, sameTail] exact ⟨eIn, eNonneg⟩ theorem QuadraticTower.tail (ds : List(ℝ)) (d : Real) requires tower : QuadraticTower(d :: ds) shows QuadraticTower(ds) proof unfold QuadraticTower at tower unfold QuadraticTower intros k kNat hk have bound : succ(k) < length{ℝ}(d :: ds) rw length.cons arith have succNat : succ(k) ∈ ℕ by typecheck obtain ⟨e, eReal, rest, restList, equal, eIn, eNonneg⟩ := tower with succ(k), succNat, bound rw listDrop.succ_cons at equal exact ⟨e, eReal, rest, restList, equal, eIn, eNonneg⟩ -- The field of a tower theorem towerField.subset_reals (ds : List(ℝ)) shows towerField(ds) ⊆ ℝ proof induction ds | List.nil => rw towerField.nil intros x hx exact rationalReals.real with hx | List.cons(d, tail) using ih => rw towerField.cons exact adjoinSqrt.subset_reals at towerField(tail), d -- Induction on ds with adjoinSqrt.subfield. theorem towerField.subfield (ds : List(ℝ)) requires tower : QuadraticTower(ds) shows RealSubfield(towerField(ds)) proof induction ds | List.nil => intro tower rw towerField.nil exact RealSubfield.rationals | List.cons(d, tail) using ih => intro tower have below := QuadraticTower.tail with tower obtain ⟨dIn, dNonneg⟩ := QuadraticTower.head with tower have field := ih with below rw towerField.cons exact adjoinSqrt.subfield with field, dIn, dNonneg theorem towerField.rationals (ds : List(ℝ)) requires tower : QuadraticTower(ds) shows rationalReals ⊆ towerField(ds) proof have field := towerField.subfield with tower exact RealSubfield.rationals_subset with field -- Stacking two towers -- append(ds, es) is es followed by ds: the discriminants of es are -- stacked on top of the tower ds. The field of es sits in the field of -- the stack: induction on es with adjoinSqrt.mono, the base case being -- the rationals in the field of ds. theorem towerField.subset_append_right (ds es : List(ℝ)) requires towerD : QuadraticTower(ds) shows towerField(es) ⊆ towerField(append(ds, es)) proof induction es | List.nil => rw [append.nil, towerField.nil] exact towerField.rationals with towerD | List.cons(e, tail) using ih => have joined : List(ℝ, append(ds, tail)) by typecheck rw [append.cons, towerField.cons, towerField.cons] exact adjoinSqrt.mono with ih -- The stack is a tower whose field contains the field of ds. The two -- conclusions share one induction on es: the subfield of the stacked -- tower is what adjoinSqrt.contains needs for the inclusion. theorem QuadraticTower.append_subset (ds es : List(ℝ)) requires towerD : QuadraticTower(ds) requires towerE : QuadraticTower(es) shows QuadraticTower(append(ds, es)) ∧ towerField(ds) ⊆ towerField(append(ds, es)) proof induction es | List.nil => intro towerE rw append.nil have reflexive : towerField(ds) ⊆ towerField(ds) by simp [Subset] exact ⟨towerD, reflexive⟩ | List.cons(e, tail) using ih => intro towerE have joined : List(ℝ, append(ds, tail)) by typecheck have below := QuadraticTower.tail with towerE obtain ⟨eIn, eNonneg⟩ := QuadraticTower.head with towerE obtain ⟨stackTower, stackContains⟩ := ih with below have lifted := towerField.subset_append_right at ds, tail with towerD have eUp := lifted with e, eIn have tower := QuadraticTower.cons with stackTower, eUp, eNonneg have field := towerField.subfield with stackTower have contains := adjoinSqrt.contains with field, eNonneg rw [append.cons, towerField.cons] refine ⟨tower, ?_⟩ exact Subset.transitive with stackContains, contains theorem towerField.subset_append (ds es : List(ℝ)) requires towerD : QuadraticTower(ds) requires towerE : QuadraticTower(es) shows towerField(ds) ⊆ towerField(append(ds, es)) proof have both := QuadraticTower.append_subset with towerD, towerE exact both.right -- The compositum: each discriminant of es lies in the field of its suffix -- within es, hence in the field of that suffix stacked on ds. theorem QuadraticTower.append (ds es : List(ℝ)) requires towerD : QuadraticTower(ds) requires towerE : QuadraticTower(es) shows QuadraticTower(append(ds, es)) proof have both := QuadraticTower.append_subset with towerD, towerE exact both.left