import Mathlib.NumberTheory.NumberField.Basic import Mathlib.RingTheory.Localization.Integer import Mathlib.RingTheory.Localization.Away.Basic import Mathlib.RingTheory.Extension.Presentation.Core /-! # Algebra -/ namespace OAI.ArithmeticGeometry.Models.Algebra.Constructions.Foundations /-! Algebraic foundations for Foundations. This file imports no graph statements. -/ open scoped _root_.NumberField nonZeroDivisors TensorProduct universe u v variable (K : Type u) [Field K] [NumberField K] /-- A nonzero natural denominator defines the canonical map into the number field. -/ noncomputable def integersAwayToField (N : β„•) (hN : N β‰  0) : Localization.Away (N : π“ž K) β†’+* K := IsLocalization.Away.lift (N : π“ž K) (show IsUnit (algebraMap (π“ž K) K (N : π“ž K)) from by simpa using (isUnit_iff_ne_zero.mpr (Nat.cast_ne_zero.mpr hN) : IsUnit (N : K))) @[simp] theorem integersAwayToField_algebraMap (N : β„•) (hN : N β‰  0) (a : π“ž K) : integersAwayToField K N hN (algebraMap (π“ž K) (Localization.Away (N : π“ž K)) a) = algebraMap (π“ž K) K a := IsLocalization.Away.lift_eq _ _ _ theorem integersAwayToField_comp (N : β„•) (hN : N β‰  0) : (integersAwayToField K N hN).comp (algebraMap (π“ž K) (Localization.Away (N : π“ž K))) = algebraMap (π“ž K) K := by ext a exact integersAwayToField_algebraMap K N hN a /-- A common natural denominator can also contain prescribed prime factors. -/ theorem exists_common_natural_denominator {ΞΉ : Type v} [Finite ΞΉ] (x : ΞΉ β†’ K) (Nβ‚€ : β„•) (hNβ‚€ : Nβ‚€ β‰  0) : βˆƒ N : β„•, N β‰  0 ∧ Nβ‚€ ∣ N ∧ βˆ€ i, βˆƒ a : π“ž K, algebraMap (π“ž K) K a = (N : K) * x i := by obtain ⟨b, hb⟩ := IsLocalization.exist_integer_multiples_of_finite (Algebra.algebraMapSubmonoid (π“ž K) ℀⁰) x obtain ⟨m, hm, hmb⟩ := b.property have hm0 : m β‰  0 := mem_nonZeroDivisors_iff_ne_zero.mp hm let N := Nβ‚€ * m.natAbs have hN : N β‰  0 := mul_ne_zero hNβ‚€ (Int.natAbs_ne_zero.mpr hm0) refine ⟨N, hN, dvd_mul_right Nβ‚€ m.natAbs, ?_⟩ have hmN : m ∣ (N : β„€) := Int.dvd_natCast.mpr (dvd_mul_left m.natAbs Nβ‚€) obtain ⟨c, hc⟩ := hmN intro i obtain ⟨a, ha⟩ := hb i refine ⟨(c : π“ž K) * a, ?_⟩ have ha' : algebraMap (π“ž K) K a = (m : K) * x i := by simpa [Algebra.smul_def, ← hmb] using ha have hc' : (N : K) = (m : K) * (c : K) := by exact_mod_cast hc simp only [map_mul, map_intCast, ha', hc'] rw [← mul_assoc, mul_comm (c : K) (m : K)] /-- Every element with denominator `N` belongs to the canonical localization image. -/ theorem mem_range_integersAwayToField (N : β„•) (hN : N β‰  0) (x : K) (hx : βˆƒ a : π“ž K, algebraMap (π“ž K) K a = (N : K) * x) : x ∈ (integersAwayToField K N hN).range := by obtain ⟨a, ha⟩ := hx let L := Localization.Away (N : π“ž K) let j := integersAwayToField K N hN let z : L := IsLocalization.Away.invSelf (N : π“ž K) have hz : (N : K) * j z = 1 := by have H := congrArg j (IsLocalization.Away.mul_invSelf (S := L) (N : π“ž K)) simpa [j] using H refine ⟨algebraMap (π“ž K) L a * z, ?_⟩ change j (algebraMap (π“ž K) L a * z) = x rw [map_mul, integersAwayToField_algebraMap, ha] calc (N : K) * x * j z = x * ((N : K) * j z) := by ac_rfl _ = x := by rw [hz, mul_one] /-- A finite family of coefficients is contained in one localization of the integers. -/ theorem finite_coefficients_in_localized_integers {ΞΉ : Type v} [Finite ΞΉ] (x : ΞΉ β†’ K) (Nβ‚€ : β„•) (hNβ‚€ : Nβ‚€ β‰  0) : βˆƒ (N : β„•) (hN : N β‰  0), Nβ‚€ ∣ N ∧ βˆ€ i, x i ∈ (integersAwayToField K N hN).range := by obtain ⟨N, hN, hd, h⟩ := exists_common_natural_denominator K x Nβ‚€ hNβ‚€ exact ⟨N, hN, hd, fun i ↦ mem_range_integersAwayToField K N hN (x i) (h i)⟩ /-- A finitely presented algebra over a number field has an integral model, with the base-change isomorphism included in the conclusion. -/ theorem finitely_presented_algebra_has_integral_model (A : Type v) [CommRing A] [Algebra K A] [Algebra.FinitePresentation K A] (Nβ‚€ : β„•) (hNβ‚€ : Nβ‚€ β‰  0) : βˆƒ (N : β„•) (hN : N β‰  0), Nβ‚€ ∣ N ∧ let L := Localization.Away (N : π“ž K) letI : Algebra L K := (integersAwayToField K N hN).toAlgebra βˆƒ (Aβ‚€ : Type u) (_ : CommRing Aβ‚€) (_ : Algebra L Aβ‚€), Algebra.FinitePresentation L Aβ‚€ ∧ Nonempty (K βŠ—[L] Aβ‚€ ≃ₐ[K] A) := by classical let P := Algebra.Presentation.ofFinitePresentation K A let : Finite P.coeffs := P.finite_coeffs.to_subtype obtain ⟨N, hN, hd, hcoeff⟩ := finite_coefficients_in_localized_integers K (fun c : P.coeffs ↦ (c : K)) Nβ‚€ hNβ‚€ refine ⟨N, hN, hd, ?_⟩ let L := Localization.Away (N : π“ž K) let : Algebra L K := (integersAwayToField K N hN).toAlgebra let : Algebra L A := ((algebraMap K A).comp (integersAwayToField K N hN)).toAlgebra let : IsScalarTower L K A := IsScalarTower.of_algebraMap_eq' rfl let : P.HasCoeffs L := ⟨fun c hc ↦ hcoeff ⟨c, hc⟩⟩ exact ⟨P.ModelOfHasCoeffs L, inferInstance, inferInstance, inferInstance, ⟨P.tensorModelOfHasCoeffsEquiv L⟩⟩ end OAI.ArithmeticGeometry.Models.Algebra.Constructions.Foundations