Lean sources / SurfaceBlowup / Assembly / TypeEvents.lean

Type events and divisor counts

1329 lines · 17 public declarations · raw file · declaration index

1/-2Copyright (c) 2026 Asvin G, Claude (Anthropic), GPT-6-Astra (OpenAI). All rights reserved.3Released under Apache 2.0 license as described in the file LICENSE.4Authors: Asvin G, Claude (Anthropic), GPT-6-Astra (OpenAI)5-/6import SurfaceBlowup.Assembly.NullBridges7import SurfaceBlowup.Unramified.HomogeneousCells8 9/-!10# Type events and divisor counts11 12Type events of polynomials over a complete DVR `O` with finite residue field and the combinatorics13of exceptional divisors used to assemble the cell laws: unit scaling and the binary decomposition of14type events, type descent along unramified blocks, the array of a divisor and divisor counts, and15the law of a product of reduced forms.16 17## Main results18 19* `affine_type_event_unit_scaling`, `binary_type_event_decomposition`: elementary properties of20  type events.21* `descended_block_type_iff`: types of polynomials in a block cell.22* `divisor_array_exists_unique`, `divisor_count_of_point_set`,23  `divisor_count_eval_of_point_count`, `affine_divisor_count`: arrays and divisor counts.24* `monic_polynomial_divisor`: monic polynomials correspond to affine divisors.25* `form_product_type_event_law`: the law of a product of reduced forms.26 27Provenance: `Thm_SurfaceBlowupV2_{affine_type_event_unit_scaling, array_enumeration,28binary_type_event_decomposition, descended_block_type_iff, divisor_array_exists_unique,29divisor_count_of_point_set, affine_divisor_count, divisor_vector_properties, form_product_type_iff,30form_product_type_event_sets, form_product_type_event_law, monic_polynomial_divisor}`.31-/32 33namespace SurfaceBlowup34 35open scoped BigOperators ENNReal36 37private theorem directionDegree_mul_le_exceptionalDegree (k : Type) [Field k]38    (D : ExceptionalDivisor k) (Q : ClosedDirection k) :39    directionDegree k Q * D Q ≤ exceptionalDegree k D := by40  by_cases hQ : D Q = 041  · simp [hQ]42  · exact Finset.single_le_sum (f := fun Q => directionDegree k Q * D Q) (by intros; positivity)43      (Finsupp.mem_support_iff.mpr hQ)44 45/-! ### Affine and binary type events -/46 47/-- Affine type events are invariant under scaling by units; in particular, normalising a point of48a projective residue cell at a coordinate `μ` with `G μ = 1` does not change its type. -/49theorem affine_type_event_unit_scaling (O : Type) [CommRing O] [IsDomain O] [IsLocalRing O] (n : ℕ)50    (σ : FactorType) :51    (∀ (a : Fin (n+1) → O) (u : O), IsUnit u → (u • a ∈ affineTypeEvent O n σ ↔ a ∈ affineTypeEvent52      O n σ)) ∧ ∀ (G : Fin (n+1) → IsLocalRing.ResidueField O) (μ : Fin (n+1)), G μ = 1 → ∀ a ∈53      projectiveCell O n G, (insertOne μ (projectiveNormalize O n G μ a) ∈ affineTypeEvent O n σ ↔54      a ∈ affineTypeEvent O n σ) := by55  have key : ∀ (a : Fin (n+1) → O) (u : O), IsUnit u →56      (u • a ∈ affineTypeEvent O n σ ↔ a ∈ affineTypeEvent O n σ) := by57    intro a u hu58    have hu0 : u ≠ 0 := hu.ne_zero59    have hι : algebraMap O (BaseField O) u ≠ 0 := by60      intro h61      exact hu0 (IsFractionRing.injective O (BaseField O) (by simpa using h))62    have hP : affinePolynomial O (u • a) = Polynomial.C (algebraMap O (BaseField O) u) *63        affinePolynomial O a := by64      unfold affinePolynomial65      rw [Finset.mul_sum]66      refine Finset.sum_congr rfl fun i _ => ?_67      simp only [Pi.smul_apply, smul_eq_mul, map_mul, mul_assoc]68    have hlast : (u • a) (Fin.last n) ≠ 0 ↔ a (Fin.last n) ≠ 0 := by69      simp only [Pi.smul_apply, smul_eq_mul]70      rw [Ne, Ne, mul_eq_zero]; simp [hu0]71    have hn : Polynomial.C (algebraMap O (BaseField O) ((u • a) (Fin.last n)))⁻¹ *72          affinePolynomial O (u • a) =73        Polynomial.C (algebraMap O (BaseField O) (a (Fin.last n)))⁻¹ * affinePolynomial O a := by74      rw [hP]75      simp only [Pi.smul_apply, smul_eq_mul, map_mul, mul_inv]76      rw [← mul_assoc, mul_assoc (Polynomial.C (algebraMap O (BaseField O) u)⁻¹),77        mul_comm (Polynomial.C (algebraMap O (BaseField O) (a (Fin.last n)))⁻¹),78        ← mul_assoc, ← Polynomial.C_mul, inv_mul_cancel₀ hι]79      simp [mul_comm]80    exact ⟨fun ⟨h1, h2⟩ ↦ ⟨hlast.1 h1, by rwa [hn] at h2⟩, fun ⟨h1, h2⟩ ↦ ⟨hlast.2 h1, by rwa [hn]⟩⟩81  refine ⟨key, fun G μ hG a ha ↦ ?_⟩82  have ⟨_, c, hc, hres⟩ := ha83  have hres' : IsLocalRing.residue O (a μ) = c := by simpa [residueVector, hG] using congrFun hres μ84  have hunit : IsUnit (a μ) := by85    by_contra hnu86    exact hc (hres'.symm.trans ((IsLocalRing.residue_eq_zero_iff _).287      ((IsLocalRing.mem_maximalIdeal _).2 hnu)))88  have hpn : projectiveNormalize O n G μ a = dropCoord μ (normalizeAt μ a) := by89    unfold projectiveNormalize; rw [ite_eq_left ha]90  have hins : insertOne μ (projectiveNormalize O n G μ a) = normalizeAt μ a := by91    rw [hpn]92    unfold insertOne dropCoord93    have h1 : normalizeAt μ a μ = 1 := by94      simp only [normalizeAt, Pi.smul_apply, smul_eq_mul]95      exact Ring.inverse_mul_cancel _ hunit96    rw [← h1]97    exact Fin.insertNth_self_removeNth μ _98  rw [hins]99  unfold normalizeAt100  apply key a101  obtain ⟨v, hv⟩ := hunit102  rw [← hv, Ring.inverse_unit]103  exact Units.isUnit _104 105/-- If the divisor `D` of degree `n` has array `m`, the non-root children of the pattern `m` are106enumerated by the support of `D`, the child at `Q` being `(deg Q, single 1 (D Q))`. -/107theorem array_enumeration (k : Type) [Field k] (n : ℕ) (D : ExceptionalDivisor k)108    (hD : exceptionalDegree k D = n) (m : Pattern n) (hm : HasArray k n D m) :109    ∃ e : Fin (otherChildren 1 m).length ≃ D.support, ∀ j, (otherChildren 1 m).get j =110      (directionDegree k (e j), State.single 1 (D (e j))) := by111  set Ls := otherChildren 1 m with hLs112  let ℓ₁ : Fin Ls.length → ℕ × State := fun j => Ls.get j113  let ℓ₂ : D.support → ℕ × State := fun Q => (directionDegree k Q, State.single 1 (D Q))114  have hdeg := directionDegree_pos k115  have hle : ∀ Q ∈ D.support, directionDegree k Q * D Q ≤ n := fun Q _ ↦ hD ▸116      directionDegree_mul_le_exceptionalDegree k D Q117  have hcard : ∀ x, Fintype.card {j // ℓ₁ j = x} = Fintype.card {Q // ℓ₂ Q = x} := by118    intro x119    rw [← Nat.card_eq_fintype_card, ← Nat.card_eq_fintype_card]120    have h1 : Nat.card {j // ℓ₁ j = x} = Ls.count x := by121      convert list_get_fiber_card _ Ls x using 2122    rw [h1]123    by_cases hx : ∃ δ r, x = (δ, State.single 1 r)124    · obtain ⟨δ, r, rfl⟩ := hx125      rw [(other_children_count n m δ r).1]126      have hR : Nat.card {Q // ℓ₂ Q = (δ, State.single 1 r)} =127          Nat.card {Q : ClosedDirection k // directionDegree k Q = δ ∧ D Q = r ∧ 0 < r} := by128        apply Nat.card_congr129        exact130          { toFun := fun Q => ⟨Q.1.1, by131              have h := Q.2132              simp only [ℓ₂, Prod.mk.injEq, State.single.injEq, true_and] at h133              have hs := Finsupp.mem_support_iff.mp Q.1.2134              exact ⟨h.1, h.2, by omega⟩⟩135            invFun := fun Q => ⟨⟨Q.1, Finsupp.mem_support_iff.mpr (by omega)⟩, by136              simp only [ℓ₂, Prod.mk.injEq, State.single.injEq, true_and]137              exact ⟨Q.2.1, Q.2.2.1⟩⟩138            left_inv := fun Q => rfl139            right_inv := fun Q => rfl }140      rw [hR]141      split_ifs with h142      · rw [← hm (⟨δ - 1, by omega⟩, ⟨r - 1, by omega⟩)]143        apply Nat.card_congr144        apply Equiv.subtypeEquivRight145        intro Q146        simp only147        omega148      · symm; rw [Nat.card_eq_zero]149        left150        refine ⟨fun ⟨Q, hQ1, hQ2, hQ3⟩ => h ?_⟩151        have h1 := hdeg Q152        have hQs : Q ∈ D.support := Finsupp.mem_support_iff.mpr (by omega)153        have h2 := hle Q hQs154        rw [hQ1] at h1; rw [hQ1, hQ2] at h2155        refine ⟨h1, ?_, hQ3, ?_⟩156        · nlinarith157        · nlinarith158    · have hL : Ls.count x = 0 := by159        rw [List.count_eq_zero]160        intro hmem161        obtain ⟨ij, hij⟩ := (other_children_count n m 0 0).2.1 x hmem162        exact hx ⟨_, _, hij⟩163      rw [hL]164      symm165      rw [Nat.card_eq_zero]166      left167      exact ⟨fun ⟨Q, hQ⟩ => hx ⟨_, _, hQ.symm⟩⟩168  refine ⟨(Equiv.sigmaFiberEquiv ℓ₁).symm.trans169    ((Equiv.sigmaCongrRight fun x => Fintype.equivOfCardEq (hcard x)).trans170      (Equiv.sigmaFiberEquiv ℓ₂)), fun j => ?_⟩171  have := ((Fintype.equivOfCardEq (hcard (ℓ₁ j))) ⟨j, rfl⟩).2172  exact this.symm173 174section175 176open Polynomial177 178private lemma affinePolynomial_coeff (O : Type) [CommRing O] [IsDomain O] {n : ℕ}179    (a : Fin (n + 1) → O) (k : ℕ) :180    (affinePolynomial O a).coeff k = if h : k < n+1 then algebraMap O (BaseField O)181      (a ⟨k,h⟩) else 0 := by182  unfold affinePolynomial183  rw [finsetSum_coeff]184  simp only [coeff_C_mul, coeff_X_pow]185  split_ifs with h186  · rw [Finset.sum_eq_single ⟨k,h⟩]187    · simp188    · intro b _ hb; rw [ite_eq_right]; · simp189      intro e; apply hb; ext; simp [e]190    · simp191  · apply Finset.sum_eq_zero; intro i _; rw [ite_eq_right]; · simp192    omega193 194private lemma affinePolynomial_natDegree_le (O : Type) [CommRing O] [IsDomain O] {n : ℕ}195    (a : Fin (n + 1) → O) : (affinePolynomial O a).natDegree ≤ n := by196  rw [natDegree_le_iff_coeff_eq_zero]197  intro k hk; rw [affinePolynomial_coeff, dite_eq_right]; omega198 199private lemma affinePolynomial_natDegree_eq (O : Type) [CommRing O] [IsDomain O] {n : ℕ}200    (a : Fin (n + 1) → O) (h : a (Fin.last n) ≠ 0) :201    (affinePolynomial O a).natDegree = n ∧ (affinePolynomial O a).leadingCoeff = algebraMap O202      (BaseField O) (a (Fin.last n)) := by203  have hc : (affinePolynomial O a).coeff n = algebraMap O (BaseField O) (a (Fin.last n)) := by204    rw [affinePolynomial_coeff, dite_eq_left (by omega)]; rfl205  have hne : (affinePolynomial O a).coeff n ≠ 0 := by206    rw [hc]; exact (map_ne_zero_iff _ (IsFractionRing.injective O (BaseField O))).mpr h207  have hd := natDegree_eq_of_le_of_coeff_ne_zero (affinePolynomial_natDegree_le O a) hne208  exact ⟨hd, by rw [leadingCoeff, hd, hc]⟩209 210private lemma affinePolynomial_eq_init (O : Type) [CommRing O] [IsDomain O] {m : ℕ}211    (a : Fin (m + 2) → O) (h : a (Fin.last (m + 1)) = 0) :212    affinePolynomial O a = affinePolynomial O (Fin.init a) := by213  ext k; rw [affinePolynomial_coeff, affinePolynomial_coeff]214  by_cases hk : k < m + 1215  · rw [dite_eq_left (by omega), dite_eq_left hk]; rfl216  · rw [dite_eq_right hk]; split_ifs with h2217    · have : k = m+1 := by omega218      subst this219      have e : a ⟨m+1, h2⟩ = 0 := h220      rw [e, map_zero]221    · rfl222 223/-- Decomposition of the binary type event according to whether the top coefficient vanishes: if224it does not, it is the primitive affine type event; if it does, the type must contain a factor225`(1, 1)` at infinity and the remaining coefficients form an affine type event. -/226theorem binary_type_event_decomposition (O : Type) [CommRing O] [IsDomain O] (σ : FactorType) :227    (∀ n : ℕ, binaryTypeEvent O n σ ∩ {a | a (Fin.last n) ≠ 0} = affineTypeEvent O n σ ∩228      {a | primitive O a}) ∧ (binaryTypeEvent O 0 σ ∩ {a | a (Fin.last 0) = 0} = ∅) ∧ ∀ m : ℕ,229      binaryTypeEvent O (m+1) σ ∩ {a | a (Fin.last (m+1)) = 0} = {a | a (Fin.last (m+1)) = 0} ∩230      Fin.init ⁻¹' (if (1,1) ∈ σ then affineTypeEvent O m (σ.erase (1,1)) ∩ {b | primitive O b}231      else ∅) := by232  refine ⟨?_, ?_, ?_⟩233  · intro n; ext a234    simp only [binaryTypeEvent, affineTypeEvent, HasBinaryTypeF, Set.mem_inter_iff,235        Set.mem_ofPred_eq]236    constructor237    · rintro ⟨⟨hp, -, -, -, τ, hτ, hσ⟩, ha⟩238      obtain ⟨hd, hl⟩ := affinePolynomial_natDegree_eq O a ha239      rw [hd, Nat.sub_self, Multiset.replicate_zero, add_zero] at hσ240      subst hσ; rw [hl] at hτ; exact ⟨⟨ha, hτ⟩, hp⟩241    · rintro ⟨⟨ha, hτ⟩, hp⟩242      obtain ⟨hd, hl⟩ := affinePolynomial_natDegree_eq O a ha243      refine ⟨⟨hp, ?_, by omega, by omega, σ, by rwa [hl], by simp [hd]⟩, ha⟩244      intro h0; rw [h0, natDegree_zero] at hd; subst hd245      rw [h0] at hl; simp at hl246      exact ha ((map_eq_zero_iff _ (IsFractionRing.injective O (BaseField O))).mp hl.symm)247  · ext a248    simp only [binaryTypeEvent, primitive, Set.mem_inter_iff, Set.mem_ofPred_eq,249        Set.mem_empty_iff_false, iff_false]250    rintro ⟨⟨⟨i, hi⟩, -⟩, h0⟩251    obtain rfl : i = Fin.last 0 := Subsingleton.elim (α := Fin 1) _ _252    rw [h0] at hi; exact not_isUnit_zero hi253  · intro m; ext a254    simp only [Set.mem_inter_iff, Set.mem_ofPred_eq, Set.mem_preimage]255    have hprim : ∀ a : Fin (m+2) → O, a (Fin.last (m+1)) = 0 →256        (primitive O a ↔ primitive O (Fin.init a)) := by257      refine fun a h0 ↦ ⟨fun ⟨i, hi⟩ ↦ ?_, fun ⟨j, hj⟩ ↦ ⟨Fin.castSucc j, hj⟩⟩258      induction i using Fin.lastCases with259      | last => rw [h0] at hi; exact absurd hi not_isUnit_zero260      | cast j => exact ⟨j, hi⟩261    have hdeg : ∀ b : Fin (m+1) → O, affinePolynomial O b ≠ 0 →262        (affinePolynomial O b).natDegree = m → b (Fin.last m) ≠ 0 := fun b hne hd hb ↦ by263      refine hne (leadingCoeff_eq_zero.1 ?_)264      rw [leadingCoeff, hd, affinePolynomial_coeff, dite_eq_left (by omega)]265      exact (congrArg _ hb).trans (map_zero _)266    constructor267    · rintro ⟨⟨hp, hne, -, hle, τ, hτ, hσ⟩, h0⟩268      rw [affinePolynomial_eq_init O a h0] at hne hle hτ hσ269      have hd : (affinePolynomial O (Fin.init a)).natDegree = m := by270        have := affinePolynomial_natDegree_le O (Fin.init a); omega271      have hb := hdeg _ hne hd272      obtain ⟨-, hl⟩ := affinePolynomial_natDegree_eq O _ hb273      rw [hd, show m + 1 - m = 1 by omega, Multiset.replicate_one] at hσ274      have hmem : ((1:ℕ),(1:ℕ)) ∈ σ := by rw [hσ]; simp275      have her : σ.erase (1,1) = τ := by rw [hσ, add_comm, Multiset.singleton_add,276          Multiset.erase_cons_head]277      refine ⟨h0, ?_⟩278      rw [ite_eq_left hmem]279      refine ⟨⟨hb, ?_⟩, (hprim a h0).mp hp⟩280      rw [her, ← hl]; exact hτ281    · rintro ⟨h0, hs⟩282      split_ifs at hs with hmem283      · obtain ⟨⟨hb, hτ⟩, hp⟩ := hs284        obtain ⟨hd, hl⟩ := affinePolynomial_natDegree_eq O _ hb285        refine ⟨⟨(hprim a h0).mpr hp, ?_⟩, h0⟩286        unfold HasBinaryTypeF287        rw [affinePolynomial_eq_init O a h0]; intro F; simp only [F]288        rw [hd, show m + 1 - m = 1 by omega, Multiset.replicate_one, hl]289        refine ⟨?_, by omega, le_refl _, σ.erase (1,1), hτ, ?_⟩290        · intro hz; rw [hz, leadingCoeff_zero] at hl291          exact hb ((map_eq_zero_iff _ (IsFractionRing.injective O (BaseField O))).mp hl.symm)292        · rw [add_comm, Multiset.singleton_add, Multiset.cons_erase hmem]293      · exact hs.elim294 295end296 297/-- For an unramified lift `Φ` and a monic polynomial in the residue cell of `(Φ mod π)^r`, its298`O`-type is `scaleType (deg Φ) τ` exactly when the translated block polynomial over `O[X]/(Φ)` has299type `τ`. -/300theorem descended_block_type_iff (O : Type) [CommRing O] [IsDomain O] [IsDiscreteValuationRing O]301    [IsAdicComplete (IsLocalRing.maximalIdeal O) O] [Finite (IsLocalRing.ResidueField O)]302    (Φ : Polynomial O) (hΦ : IsUnramifiedLift O Φ) [IsDomain (AdjoinRoot Φ)] (r : ℕ)303    (a : Fin (Φ.natDegree * r) → O) (ha : a ∈ residueCell O (Φ.natDegree * r) (residuePoly O Φ ^ r))304    (σ : FactorType) :305    HasPolynomialTypeF O (monicPolynomial O a) σ ↔ ∃ τ : FactorType, HasPolynomialTypeF306      (AdjoinRoot Φ) (monicPolynomial (AdjoinRoot Φ) (pointChart O Φ r a)) τ ∧ σ =307      scaleType Φ.natDegree τ := by308  obtain ⟨_, _, hDVR, -, -, hComp, hFin, -⟩ := unramified_is_dvr O Φ hΦ309  have hδ : 0 < Φ.natDegree := by310    have h1 := Polynomial.natDegree_pos_iff_degree_pos.mpr311        (Polynomial.degree_pos_of_irreducible hΦ.2)312    rwa [Polynomial.Monic.natDegree_map hΦ.1] at h1313  obtain ⟨hbij, hdesc, -, hdiv⟩ := block_descent_bijection O Φ hΦ r314  set b := selectBlock O Φ r a with hb_def315  have hb : b ∈ blockCell O Φ r := hbij.mapsTo ha316  set B := monicPolyO b with hB_def317  have hBm : B.Monic := (hdiv a ha).1318  have hBdvd : B ∣ (monicPolyO a).map (AdjoinRoot.of Φ) := (hdiv a ha).2319  -- translation320  have hB' : monicPolyO (pointChart O Φ r a) = Polynomial.taylor (AdjoinRoot.root Φ) B := by321    have hrec := monic_poly_reconstruct (AdjoinRoot Φ) r b322    have hBn : B.natDegree = r := hrec.2.1323    have hTm : (Polynomial.taylor (AdjoinRoot.root Φ) B).Monic := by324      unfold Polynomial.Monic; rw [Polynomial.leadingCoeff_taylor]; exact hBm325    exact hrec.2.2.2 _ hTm ((Polynomial.natDegree_taylor B _).trans hBn)326  have htrans : ∀ τ, HasPolynomialTypeF (AdjoinRoot Φ)327      (B.map (algebraMap _ (BaseField (AdjoinRoot Φ)))) τ ↔328      HasPolynomialTypeF (AdjoinRoot Φ) (monicPolynomial (AdjoinRoot Φ) (pointChart O Φ r a)) τ :=329          by330    intro τ331    rw [← monicPolyO_map_fraction, hB']332    exact block_translation_type_iff (AdjoinRoot Φ) B hBm _ τ333  have hG : (monicPolyO a).map (algebraMap O (AdjoinRoot Φ)) = conjugateProduct O Φ B := by334    have := (descend_block_mem_cell O Φ hΦ r b hb).2335    rw [hdesc a ha] at this336    rw [AdjoinRoot.algebraMap_eq]; exact this337  have hback : ∀ τ, HasPolynomialTypeF (AdjoinRoot Φ)338      (B.map (algebraMap _ (BaseField (AdjoinRoot Φ)))) τ →339      HasPolynomialTypeF O (monicPolynomial O a) (scaleType Φ.natDegree τ) := by340    intro τ hτ341    rw [← monicPolyO_map_fraction]342    exact descended_polynomial_type O Φ hΦ B τ hτ (block_conjugates_coprime O Φ hΦ r b hb) _ hG343  constructor344  · intro hσ345    -- ascend the type to `Oδ` (closure-finiteness ascent), then pass to the divisor `B`346    obtain ⟨ι, hι, -⟩ := unramified_fraction_field_lift O Φ hΦ347    have hmap : (monicPolynomial O a).map ι =348        ((monicPolyO a).map (algebraMap O (AdjoinRoot Φ))).map349          (algebraMap (AdjoinRoot Φ) (BaseField (AdjoinRoot Φ))) := by350      rw [← monicPolyO_map_fraction, Polynomial.map_map, Polynomial.map_map]351      congr 1352      exact RingHom.ext hι353    have hdvd : B.map (algebraMap (AdjoinRoot Φ) (BaseField (AdjoinRoot Φ))) ∣354        ((monicPolyO a).map (algebraMap O (AdjoinRoot Φ))).map355          (algebraMap (AdjoinRoot Φ) (BaseField (AdjoinRoot Φ))) :=356      Polynomial.map_dvd _ (by rw [AdjoinRoot.algebraMap_eq]; exact hBdvd)357    obtain ⟨τ', hτ'⟩ := polynomial_type_map_unramified O Φ hΦ ι hι _ σ hσ358    rw [hmap] at hτ'359    obtain ⟨τ, hτ⟩ := polynomial_type_dvd (AdjoinRoot Φ) _ _ τ' hτ' (hBm.map _) hdvd360    refine ⟨τ, (htrans τ).1 hτ, ?_⟩361    exact polynomial_type_unique O _ σ _ hσ (hback τ hτ)362  · rintro ⟨τ, hτ, rfl⟩363    exact hback τ ((htrans τ).2 hτ)364 365/-! ### Divisors, arrays and counts -/366 367/-- Every exceptional divisor of degree `n` has a unique array, and that array is a residue368pattern. -/369theorem divisor_array_exists_unique (k : Type) [Field k] (n : ℕ) (D : ExceptionalDivisor k)370    (hD : exceptionalDegree k D = n) :371    (∃ m : Pattern n, HasArray k n D m ∧ m ∈ residuePatterns n) ∧ ∀ m m' : Pattern n,372      HasArray k n D m → HasArray k n D m' → m = m' := by373  have hdeg := directionDegree_pos k374  have hsum : exceptionalDegree k D = ∑ Q ∈ D.support, directionDegree k Q * D Q := rfl375  set S : Fin n × Fin n → Finset (ClosedDirection k) := fun ij =>376    D.support.filter (fun Q => directionDegree k Q = ij.1.val + 1 ∧ D Q = ij.2.val + 1) with hS377  have hcard : ∀ ij : Fin n × Fin n,378      Nat.card {Q : ClosedDirection k // directionDegree k Q = ij.1.val + 1 ∧ D Q = ij.2.val + 1}379        = (S ij).card := by380    intro ij381    rw [← Nat.card_eq_finsetCard]382    apply Nat.card_congr383    apply Equiv.subtypeEquivRight384    intro Q385    simp only [hS, Finset.mem_filter, Finsupp.mem_support_iff]386    constructor387    · intro h; exact ⟨by omega, h⟩388    · intro h; exact h.2389  have hconst : ∀ ij : Fin n × Fin n,390      ∑ Q ∈ S ij, directionDegree k Q * D Q = (S ij).card * ((ij.1.val + 1) * (ij.2.val + 1)) := by391    intro ij392    rw [Finset.sum_congr rfl (g := fun _ => (ij.1.val + 1) * (ij.2.val + 1))]393    · simp394    · intro Q hQ395      simp only [hS, Finset.mem_filter] at hQ396      rw [hQ.2.1, hQ.2.2]397  have hbound : ∀ ij : Fin n × Fin n, (S ij).card ≤ n := by398    intro ij399    have h1 : ∑ Q ∈ S ij, directionDegree k Q * D Q ≤ n := by400      rw [← hD, hsum]401      exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (by intros; positivity)402    rw [hconst] at h1403    have : 1 ≤ (ij.1.val + 1) * (ij.2.val + 1) := Nat.one_le_iff_ne_zero.mpr (by positivity)404    nlinarith405  refine ⟨⟨fun ij => ⟨(S ij).card, Nat.lt_succ_of_le (hbound ij)⟩, fun ij => hcard ij, ?_⟩, ?_⟩406  · rw [residuePatterns, Finset.mem_filter]407    refine ⟨Finset.mem_univ _, ?_⟩408    unfold patternWeight409    have step1 : ∀ ij : Fin n × Fin n, (ij.1.val + 1) * (ij.2.val + 1) * (S ij).card410        = ∑ Q ∈ D.support, if (directionDegree k Q = ij.1.val + 1 ∧ D Q = ij.2.val + 1)411            then directionDegree k Q * D Q else 0 := by412      intro ij413      rw [← Finset.sum_filter, hconst, mul_comm]414    simp only [step1]415    rw [Finset.sum_comm, ← hD, hsum]416    apply Finset.sum_congr rfl417    intro Q hQ418    have hle : directionDegree k Q * D Q ≤ n := hD ▸ directionDegree_mul_le_exceptionalDegree k D Q419    have hD1 : 1 ≤ D Q := Nat.one_le_iff_ne_zero.mpr (Finsupp.mem_support_iff.mp hQ)420    have hd1 := hdeg Q421    have hA : directionDegree k Q ≤ n := by nlinarith422    have hB : D Q ≤ n := by nlinarith423    rw [Finset.sum_eq_single_of_mem (⟨directionDegree k Q - 1, by omega⟩, ⟨D Q - 1, by omega⟩)424      (Finset.mem_univ _)]425    · rw [ite_eq_left]; simp only; constructor <;> omega426    · intro ij _ hne427      rw [ite_eq_right]428      rintro ⟨h1, h2⟩429      apply hne430      ext <;> simp <;> omega431  · intro m m' hm hm'432    funext ij433    exact Fin.ext ((hm ij).symm.trans (hm' ij))434 435private theorem finite_directions_of_degree (k : Type) [Field k] [Finite k]436    (p : ClosedDirection k → Prop) (n : ℕ) :437    Finite {Q : ClosedDirection k // p Q ∧ directionDegree k Q = n} := by438  let f : {Q : ClosedDirection k // p Q ∧ directionDegree k Q = n} → Option (Fin (n+1) → k) :=439    fun Q => Option.map (fun φ : AffineDirection k => fun i : Fin (n+1) => φ.val.coeff i) Q.1440  refine Finite.of_injective f ?_441  rintro ⟨Q, hQ⟩ ⟨Q', hQ'⟩ h442  apply Subtype.ext443  simp only [f] at h444  cases Q with445  | none =>446    cases Q' with447    | none => rfl448    | some _ => simp at h449  | some φ =>450    cases Q' with451    | none => simp at h452    | some ψ =>453      simp only [Option.map_some, Option.some.injEq] at h454      change some φ = some ψ455      congr 1456      apply Subtype.ext457      apply Polynomial.ext458      intro i459      by_cases hi : i < n + 1460      · exact congrFun h ⟨i, hi⟩461      · have h1 : φ.val.natDegree < i := by simp [directionDegree] at hQ; omega462        have h2 : ψ.val.natDegree < i := by simp [directionDegree] at hQ'; omega463        rw [Polynomial.coeff_eq_zero_of_natDegree_lt h1,464          Polynomial.coeff_eq_zero_of_natDegree_lt h2]465 466private theorem coloring_count (T : Type) [Finite T] (r : ℕ) (m : Fin r → ℕ) :467    Finite {f : T → Option (Fin r) // ∀ j : Fin r, Nat.card {x // f x = some j} = m j} ∧468      ((Nat.card {f : T → Option (Fin r) // ∀ j : Fin r, Nat.card {x // f x = some j} = m j} : ℕ) :469      ℚ) = ((List.range (∑ i : Fin r, m i)).map fun j => (Nat.card T : ℚ) - j).prod / ∏ i : Fin r,470      (Nat.factorial (m i) : ℚ) := by471  refine ⟨inferInstance, ?_⟩472  obtain e := Finite.equivFin T473  have key : {f : T → Option (Fin r) // ∀ j : Fin r, Nat.card {x // f x = some j} = m j} ≃474      {f : Fin (Nat.card T) → Option (Fin r) //475        ∀ i : Fin r, Nat.card {x : Fin (Nat.card T) // f x = some i} = m i} := by476    refine Equiv.subtypeEquiv (Equiv.arrowCongr e (Equiv.refl _)) ?_477    intro f478    refine forall_congr' fun j => ?_479    have : Nat.card {x // f x = some j} =480        Nat.card {x : Fin (Nat.card T) // (Equiv.arrowCongr e (Equiv.refl _) f) x = some j} := by481      apply Nat.card_congr482      refine e.subtypeEquiv ?_483      intro x484      simp [Equiv.arrowCongr_apply]485    rw [this]486  rw [Nat.card_congr key, colored_subset_count]487 488private theorem exceptionalDegree_eq_patternWeight (k : Type) [Field k] (n : ℕ)489    (D : ExceptionalDivisor k) (m : Pattern n)490    (hb : ∀ Q, D Q ≠ 0 → directionDegree k Q ≤ n ∧ D Q ≤ n) (hA : HasArray k n D m) :491    exceptionalDegree k D = patternWeight m := by492  have hsum : exceptionalDegree k D = ∑ Q ∈ D.support, directionDegree k Q * D Q := rfl493  set S : Fin n × Fin n → Finset (ClosedDirection k) := fun ij =>494    D.support.filter (fun Q => directionDegree k Q = ij.1.val + 1 ∧ D Q = ij.2.val + 1) with hS495  have hcard : ∀ ij : Fin n × Fin n, (m ij).val = (S ij).card := by496    intro ij497    rw [← hA ij, ← Nat.card_eq_finsetCard]498    apply Nat.card_congr499    apply Equiv.subtypeEquivRight500    intro Q501    simp only [hS, Finset.mem_filter, Finsupp.mem_support_iff]502    constructor503    · intro h; exact ⟨by omega, h⟩504    · intro h; exact h.2505  have hconst : ∀ ij : Fin n × Fin n,506      ∑ Q ∈ S ij, directionDegree k Q * D Q = (S ij).card * ((ij.1.val + 1) * (ij.2.val + 1)) := by507    intro ij508    rw [Finset.sum_congr rfl (g := fun _ => (ij.1.val + 1) * (ij.2.val + 1))]509    · simp510    · intro Q hQ511      simp only [hS, Finset.mem_filter] at hQ512      rw [hQ.2.1, hQ.2.2]513  unfold patternWeight514  have step1 : ∀ ij : Fin n × Fin n, (ij.1.val + 1) * (ij.2.val + 1) * (m ij).val515      = ∑ Q ∈ D.support, if (directionDegree k Q = ij.1.val + 1 ∧ D Q = ij.2.val + 1)516          then directionDegree k Q * D Q else 0 := by517    intro ij518    rw [← Finset.sum_filter, hcard, hconst, mul_comm]519  simp only [step1]520  rw [Finset.sum_comm, hsum]521  apply Finset.sum_congr rfl522  intro Q hQ523  have hD0 : D Q ≠ 0 := Finsupp.mem_support_iff.mp hQ524  have hd1 := directionDegree_pos k Q525  have hA' := (hb Q hD0).1526  have hB := (hb Q hD0).2527  rw [Finset.sum_eq_single_of_mem (⟨directionDegree k Q - 1, by omega⟩, ⟨D Q - 1, by omega⟩)528    (Finset.mem_univ _)]529  · rw [ite_eq_left]; simp only; constructor <;> omega530  · intro ij _ hne531    rw [ite_eq_right]532    rintro ⟨h1, h2⟩533    apply hne534    ext <;> simp <;> omega535 536section537variable (k : Type) [Field k] (S : Set (ClosedDirection k)) (n : ℕ)538 539private abbrev DegPoints (i : Fin n) : Type := {Q : ClosedDirection k // Q ∈ S ∧ directionDegree k540    Q =541    i.val + 1}542 543private def degreeFiberEquiv (D : ExceptionalDivisor k) (hS : ∀ Q, D Q ≠ 0 → Q ∈ S) (i j : Fin n) :544    {Q : ClosedDirection k // directionDegree k Q = i.val + 1 ∧ D Q = j.val + 1} ≃545      {q : DegPoints k S n i // D q.1 = j.val + 1} where546  toFun Q := ⟨⟨Q.1, hS Q.1 (by omega), Q.2.1⟩, Q.2.2⟩547  invFun q := ⟨q.1.1, q.1.2.2, q.2⟩548  left_inv _ := rfl549  right_inv _ := rfl550 551open scoped Classical in552private noncomputable def divisorOfColoringFun (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n))553    (Q : ClosedDirection k) : ℕ :=554  if h : ∃ i : Fin n, Q ∈ S ∧ directionDegree k Q = i.val + 1 then555    (g h.choose ⟨Q, h.choose_spec⟩).elim 0 (fun j => j.val + 1) else 0556 557private theorem divisorOfColoringFun_support_finite [Finite k]558    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n)) :559    (Function.support (divisorOfColoringFun k S n g)).Finite := by560  have : ∀ i, Finite (DegPoints k S n i) := fun i => finite_directions_of_degree k _ _561  refine Set.Finite.subset (Set.finite_iUnion fun i => Set.finite_range562      (Subtype.val : DegPoints k S n i → _)) ?_563  intro Q hQ564  simp only [Function.mem_support, divisorOfColoringFun] at hQ565  split_ifs at hQ with h566  · obtain ⟨i, hi⟩ := h567    exact Set.mem_iUnion.mpr ⟨i, ⟨Q, hi⟩, rfl⟩568  · exact absurd rfl hQ569 570private noncomputable def divisorOfColoring [Finite k]571    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n)) : ExceptionalDivisor k :=572  Finsupp.ofSupportFinite (divisorOfColoringFun k S n g)573      (divisorOfColoringFun_support_finite k S n g)574 575private theorem divisorOfColoring_apply [Finite k]576    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n)) (i : Fin n) (q : DegPoints k S n i) :577    divisorOfColoring k S n g q.1 = (g i q).elim 0 (fun j => j.val + 1) := by578  change divisorOfColoringFun k S n g q.1 = _579  have hex : ∃ i : Fin n, q.1 ∈ S ∧ directionDegree k q.1 = i.val + 1 := ⟨i, q.2⟩580  simp only [divisorOfColoringFun, dite_eq_left hex]581  have key : ∀ (i' : Fin n) (hi' : q.1 ∈ S ∧ directionDegree k q.1 = i'.val + 1), i' = i →582      (g i' ⟨q.1, hi'⟩).elim 0 (fun j => j.val + 1) = (g i q).elim 0 (fun j => j.val + 1) := by583    rintro _ _ rfl; rfl584  apply key585  apply Fin.ext586  have h1 := hex.choose_spec.2587  have h2 := q.2.2588  omega589 590private theorem divisorOfColoring_eq_zero [Finite k]591    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n)) (Q : ClosedDirection k)592    (h : ¬ (∃ i : Fin n, Q ∈ S ∧ directionDegree k Q = i.val + 1)) :593    divisorOfColoring k S n g Q = 0 := by594  change divisorOfColoringFun k S n g Q = 0595  simp only [divisorOfColoringFun, dite_eq_right h]596 597private theorem divisorOfColoring_supp [Finite k]598    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n)) :599    ∀ Q, divisorOfColoring k S n g Q ≠ 0 → Q ∈ S := by600  intro Q hQ601  by_contra hS602  exact hQ (divisorOfColoring_eq_zero k S n g Q (fun ⟨i, hi⟩ => hS hi.1))603 604private theorem divisorOfColoring_bound [Finite k]605    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n)) :606    ∀ Q, divisorOfColoring k S n g Q ≠ 0 → directionDegree k Q ≤ n ∧607      divisorOfColoring k S n g Q ≤ n := by608  intro Q hQ609  by_cases h : ∃ i : Fin n, Q ∈ S ∧ directionDegree k Q = i.val + 1610  · obtain ⟨i, hi⟩ := h611    have := divisorOfColoring_apply k S n g i ⟨Q, hi⟩612    simp only at this613    rw [this]614    refine ⟨by omega, ?_⟩615    cases g i ⟨Q, hi⟩ with616    | none => simp617    | some j => simp618  · exact absurd (divisorOfColoring_eq_zero k S n g Q h) hQ619 620private theorem divisorOfColoring_hasArray [Finite k] (m : Pattern n)621    (g : ∀ i : Fin n, DegPoints k S n i → Option (Fin n))622    (hg : ∀ i j, Nat.card {q // g i q = some j} = (m (i, j)).val) :623    HasArray k n (divisorOfColoring k S n g) m := by624  intro ij625  obtain ⟨i, j⟩ := ij626  rw [Nat.card_congr (degreeFiberEquiv k S n _ (divisorOfColoring_supp k S n _) i j), ← hg i j]627  apply Nat.card_congr (Equiv.subtypeEquivRight fun q => ?_)628  rw [divisorOfColoring_apply k S n g i q]629  cases g i q with630  | none => simp631  | some j' => simp [Fin.ext_iff]632 633private def coloringOfDivisor (D : ExceptionalDivisor k) (hb : ∀ Q, D Q ≤ n) (i : Fin n)634    (q : DegPoints k S n i) : Option (Fin n) :=635  if h : D q.1 = 0 then none else some ⟨D q.1 - 1, by have := hb q.1; omega⟩636 637private theorem coloringOfDivisor_eq_some (D : ExceptionalDivisor k) (hb : ∀ Q, D Q ≤ n)638    (i j : Fin n) (q : DegPoints k S n i) :639    coloringOfDivisor k S n D hb i q = some j ↔ D q.1 = j.val + 1 := by640  simp only [coloringOfDivisor]641  split_ifs with h642  · simp; omega643  · simp only [Option.some.injEq, Fin.ext_iff]; omega644 645private theorem le_of_exceptionalDegree_eq (D : ExceptionalDivisor k)646    (hD : exceptionalDegree k D = n) : ∀ Q, D Q ≤ n := by647  intro Q648  have := directionDegree_mul_le_exceptionalDegree k D Q649  have := directionDegree_pos k Q650  nlinarith651 652end653 654/-- Counting divisors of degree `n` with a given array `m` and support in a point set `S` that has655`c δ` points of degree `δ`: their number is the product of falling factorials over multinomial656factors. -/657theorem divisor_count_of_point_set (k : Type) [Field k] [Finite k] (S : Set (ClosedDirection k))658    (c : ℕ → ℕ)659    (hc : ∀ δ : ℕ, 0 < δ → Nat.card {Q : ClosedDirection k // Q ∈ S ∧ directionDegree k660      Q = δ} = c δ) (n : ℕ) (m : Pattern n) (hm : m ∈ residuePatterns n) :661    Finite {D : ExceptionalDivisor k // (∀ Q, D Q ≠ 0 → Q ∈ S) ∧ exceptionalDegree k D = n ∧662      HasArray k n D m} ∧663      ((Nat.card {D : ExceptionalDivisor k // (∀ Q, D Q ≠ 0 → Q ∈ S) ∧ exceptionalDegree k D = n ∧664      HasArray k n D m} : ℕ) : ℚ) = ∏ i : Fin n,665      (((List.range (∑ j : Fin n, (m (i, j)).val)).map fun t => ((c (i.val + 1) : ℕ) : ℚ) -666      (t : ℚ)).prod / ∏ j : Fin n, ((Nat.factorial (m (i, j)).val : ℕ) : ℚ)) := by667  have hw : patternWeight m = n := (Finset.mem_filter.mp hm).2668  have hPfin : ∀ i, Finite (DegPoints k S n i) := fun i => finite_directions_of_degree k _ _669  let X := {D : ExceptionalDivisor k // (∀ Q, D Q ≠ 0 → Q ∈ S) ∧ exceptionalDegree k D = n ∧670      HasArray k n D m}671  let Y := ∀ i : Fin n, {f : DegPoints k S n i → Option (Fin n) // ∀ j : Fin n, Nat.card672      {q // f q = some j} = (m (i, j)).val}673  have e : X ≃ Y :=674    { toFun := fun D i => ⟨coloringOfDivisor k S n D.1 (le_of_exceptionalDegree_eq k n D.1 D.2.2.1)675        i, fun j => by676        rw [← D.2.2.2 (i, j), Nat.card_congr (degreeFiberEquiv k S n D.1 D.2.1 i j)]677        exact Nat.card_congr (Equiv.subtypeEquivRight fun q => coloringOfDivisor_eq_some k S n _ _678            i j q)⟩679      invFun := fun g => ⟨divisorOfColoring k S n (fun i => (g i).1), divisorOfColoring_supp k S n680          _, by681        have hA := divisorOfColoring_hasArray k S n m (fun i => (g i).1) (fun i j => (g i).2 j)682        exact ⟨(exceptionalDegree_eq_patternWeight k n _ m (divisorOfColoring_bound k S n _)683            hA).trans hw, hA⟩⟩684      left_inv := by685        rintro ⟨D, hS, hD, hA⟩686        apply Subtype.ext687        ext Q688        by_cases h : ∃ i : Fin n, Q ∈ S ∧ directionDegree k Q = i.val + 1689        · obtain ⟨i, hi⟩ := h690          have := divisorOfColoring_apply k S n (fun i => coloringOfDivisor k S n D691              (le_of_exceptionalDegree_eq k n D hD) i) i ⟨Q, hi⟩692          simp only at this ⊢693          rw [this]694          simp only [coloringOfDivisor]695          split_ifs with h0696          · simp [h0]697          · simp; omega698        · simp only699          rw [divisorOfColoring_eq_zero k S n _ Q h]700          by_contra h0701          have h1 := directionDegree_mul_le_exceptionalDegree k D Q702          have h2 := directionDegree_pos k Q703          have h3 : directionDegree k Q ≤ n := by704            have : 1 ≤ D Q := by omega705            nlinarith706          exact h ⟨⟨directionDegree k Q - 1, by omega⟩, hS Q (Ne.symm h0), by simp; omega⟩707      right_inv := by708        intro g709        funext i710        apply Subtype.ext711        funext q712        have hb := divisorOfColoring_apply k S n (fun i => (g i).1) i q713        simp only [coloringOfDivisor]714        split_ifs with h0715        · rw [hb] at h0716          cases h : (g i).1 q with717          | none => rfl718          | some j => simp [h] at h0719        · cases h : (g i).1 q with720          | none => rw [hb, h] at h0; exact absurd rfl h0721          | some j =>722            simp only [Option.some.injEq]723            apply Fin.ext724            simp only725            rw [hb, h]726            simp }727  have hYfin : ∀ i, Finite {f : DegPoints k S n i → Option (Fin n) //728      ∀ j : Fin n, Nat.card {q // f q = some j} = (m (i, j)).val} :=729    fun i => (coloring_count _ _ _).1730  refine ⟨Finite.of_equiv _ e.symm, ?_⟩731  rw [Nat.card_congr e, Nat.card_pi, Nat.cast_prod]732  refine Finset.prod_congr rfl fun i _ => ?_733  rw [(coloring_count (DegPoints k S n i) n (fun j => (m (i, j)).val)).2, hc _ (Nat.succ_pos _)]734 735/-- If the points of `S` of each degree `δ > 0` are counted by `availablePoints offset δ` at736`q = |k|`, then the divisors of degree `n` supported on `S` with array `m` are counted by737`divisorCount offset m` at `q`. -/738theorem divisor_count_eval_of_point_count (k : Type) [Field k] [Finite k]739    (S : Set (ClosedDirection k)) (offset : ℤ)740    (hS : ∀ δ : ℕ, 0 < δ → ((Nat.card {Q : ClosedDirection k // Q ∈ S ∧ directionDegree k Q = δ} :741      ℕ) : ℚ) = (availablePoints offset δ).eval (Nat.card k))742    (n : ℕ) (m : Pattern n) (hm : m ∈ residuePatterns n) :743    Finite {D : ExceptionalDivisor k // (∀ Q, D Q ≠ 0 → Q ∈ S) ∧ exceptionalDegree k D = n ∧744      HasArray k n D m} ∧745      ((Nat.card {D : ExceptionalDivisor k // (∀ Q, D Q ≠ 0 → Q ∈ S) ∧ exceptionalDegree k D = n ∧746      HasArray k n D m} : ℕ) : ℚ) = (divisorCount offset m).eval (Nat.card k) := by747  obtain ⟨hfin, h⟩ := divisor_count_of_point_set k S _ (fun _ _ ↦ rfl) n m hm748  refine ⟨hfin, ?_⟩749  rw [h]750  unfold divisorCount751  rw [expression_prod_eval, List.map_map, ← Finset.prod_map_toList]752  congr 1753  apply List.map_congr_left754  intro i _755  simp only [Function.comp, Expr.eval, falling, expression_prod_eval, List.map_map,756      div_eq_mul_inv, one_mul]757  congr 1758  congr 1759  apply List.map_congr_left760  intro t _761  simp only [Function.comp, Expr.sub, Expr.neg, Expr.eval]762  rw [hS (i.val + 1) (Nat.succ_pos _)]763  ring_nf764 765/-- The number of affine divisors of degree `n` with array `m` is `divisorCount 0 m` evaluated at766the residue field size. -/767theorem affine_divisor_count (k : Type) [Field k] [Finite k] (n : ℕ) (m : Pattern n)768    (hm : m ∈ residuePatterns n) :769    Finite {D : ExceptionalDivisor k // AffineDivisor k n D ∧ HasArray k n D m} ∧770      ((Nat.card {D : ExceptionalDivisor k // AffineDivisor k n D ∧ HasArray k n D m} : ℕ) : ℚ) =771      (divisorCount 0 m).eval (Nat.card k) := by772  have hc : ∀ δ : ℕ, 0 < δ → ((Nat.card {Q : ClosedDirection k // Q ∈773      {Q : ClosedDirection k | Q ≠ none} ∧ directionDegree k Q = δ} : ℕ) : ℚ) =774      (availablePoints 0 δ).eval (Nat.card k) := by775    intro δ hδ776    have h915 := nonboundary_direction_count k (State.single 1 1) δ hδ777    simp only at h915778    rw [← h915]779    congr 1780  have h := divisor_count_eval_of_point_count k {Q : ClosedDirection k | Q ≠ none} 0 hc n m hm781  have e : ∀ D : ExceptionalDivisor k, ((∀ Q, D Q ≠ 0 → Q ∈ {Q : ClosedDirection k | Q ≠ none}) ∧782      exceptionalDegree k D = n ∧ HasArray k n D m) ↔ (AffineDivisor k n D ∧ HasArray k n D m) := by783    intro D784    simp only [AffineDivisor, infinityDirection, Set.mem_ofPred_eq]785    constructor786    · rintro ⟨h1, h2, h3⟩787      exact ⟨⟨by by_contra hh; exact h1 _ hh rfl, h2⟩, h3⟩788    · rintro ⟨⟨h1, h2⟩, h3⟩789      exact ⟨fun Q hQ hQn => hQ (hQn ▸ h1), h2, h3⟩790  have eq := Equiv.subtypeEquivRight e791  have := h.1792  exact ⟨Finite.of_equiv _ eq, by rw [← Nat.card_congr eq, h.2]⟩793 794/-- Basic properties of the divisor vector of a divisor `D` of degree `n`: the affine polynomial of795`D` is monic of degree `n - D ∞`, it is the form polynomial of the divisor vector, whose entry at796`n - D ∞` is `1` and whose higher entries vanish. -/797theorem divisor_vector_properties (k : Type) [Field k] (n : ℕ) (D : ExceptionalDivisor k)798    (hD : exceptionalDegree k D = n) :799    (divisorAffinePolynomial k D).Monic ∧ (divisorAffinePolynomial k D).natDegree = n - D800      (infinityDirection k) ∧ D (infinityDirection k) ≤ n ∧ formPoly (divisorVector k n D) =801      divisorAffinePolynomial k D ∧ divisorVector k n D (⟨n - D (infinityDirection k),802      Nat.lt_succ_of_le (Nat.sub_le _ _)⟩ :803      Fin (n+1)) = 1 ∧ (∀ i : Fin (n+1), n - D (infinityDirection k) < i.val → divisorVector k n D804      i = 0) ∧ divisorVector k n D ≠ 0 := by805  classical806  set g : ClosedDirection k → ℕ → Polynomial k :=807    fun P r => (match P with | none => 1 | some phi => phi.val ^ r) with hg808  have hmon : ∀ P r, (g P r).Monic := by809    intro P r810    cases P with811    | none => exact Polynomial.monic_one812    | some phi => exact phi.property.1.pow r813  have hM : (divisorAffinePolynomial k D).Monic := by814    unfold divisorAffinePolynomial815    exact Polynomial.monic_prod_of_monic _ _ (fun P _ => hmon P (D P))816  have hdeg : (divisorAffinePolynomial k D).natDegree = ∑ P ∈ D.support, (g P (D P)).natDegree := by817    unfold divisorAffinePolynomial818    exact Polynomial.natDegree_prod_of_monic _ _ (fun P _ => hmon P (D P))819  have hsum : exceptionalDegree k D = (divisorAffinePolynomial k D).natDegree + D none := by820    rw [hdeg]821    have : ∀ P ∈ D.support, directionDegree k P * D P = (g P (D P)).natDegree +822        (if none = P then D P else 0) := by823      intro P _824      cases P with825      | none => simp [directionDegree, hg]826      | some phi => simp [directionDegree, hg, Polynomial.natDegree_pow]; ring827    unfold exceptionalDegree Finsupp.sum828    rw [Finset.sum_congr rfl this, Finset.sum_add_distrib]829    congr 1830    by_cases h : none ∈ D.support831    · rw [Finset.sum_eq_single none]832      · simp833      · intro b _ hb; exact ite_eq_right (Ne.symm hb)834      · intro h'; exact absurd h h'835    · have : D none = 0 := by simpa using h836      simp [this]837  have hinf : infinityDirection k = none := rfl838  simp only [hinf]839  have h1 : (divisorAffinePolynomial k D).natDegree = n - D none := by omega840  have h2 : D none ≤ n := by omega841  have hle : (divisorAffinePolynomial k D).natDegree < n + 1 := by omega842  have hcoef : divisorVector k n D (⟨n - D none, by omega⟩ : Fin (n+1)) = 1 := by843    change (divisorAffinePolynomial k D).coeff (n - D none) = 1844    rw [← h1]; exact hM845  refine ⟨hM, h1, h2, ?_, hcoef, ?_, ?_⟩846  · unfold formPoly847    conv_rhs => rw [Polynomial.as_sum_range' _ (n+1) hle]848    rw [← Fin.sum_univ_eq_sum_range (fun i => Polynomial.monomial i849        ((divisorAffinePolynomial k D).coeff i))]850    refine Finset.sum_congr rfl (fun i _ => ?_)851    rw [Polynomial.C_mul_X_pow_eq_monomial]; rfl852  · intro i hi853    exact Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)854  · intro h0855    have := congrFun h0 ⟨n - D none, by omega⟩856    rw [hcoef] at this857    simp at this858 859section860open Polynomial861 862private lemma formPoly_coeff {A : Type} [CommRing A] {d : ℕ} (v : Fin (d + 1) → A)863    (i : Fin (d + 1)) :864    (formPoly v).coeff i = v i := by865  simp only [formPoly, finsetSum_coeff, coeff_C_mul, coeff_X_pow]866  rw [Finset.sum_eq_single i]867  · simp868  · intro b _ hb; rw [ite_eq_right (fun h => hb (Fin.ext h).symm), mul_zero]869  · simp870 871/-- The form polynomial of a vector of length `d + 1` has degree at most `d`. -/872theorem formPoly_natDegree_le {A : Type} [CommRing A] {d : ℕ} (v : Fin (d + 1) → A) :873    (formPoly v).natDegree ≤ d := by874  unfold formPoly875  refine natDegree_sum_le_of_forall_le _ _ (fun i _ => ?_)876  exact (natDegree_C_mul_X_pow_le _ _).trans (Nat.lt_succ_iff.mp i.2)877 878private lemma formPoly_map {A B : Type} [CommRing A] [CommRing B] (f : A →+* B) {d : ℕ}879    (v : Fin (d + 1) → A) : (formPoly v).map f = formPoly (fun i => f (v i)) := by880  simp [formPoly, Polynomial.map_sum]881 882private lemma formPoly_insertOne_last {A : Type} [CommRing A] {d : ℕ} (b : Fin d → A) :883    formPoly (insertOne (Fin.last d) b) = monicPolyO b := by884  simp [formPoly, insertOne, monicPolyO, Fin.sum_univ_castSucc, Fin.insertNth_last', add_comm]885 886private lemma formPoly_single_zero {A : Type} [CommRing A] {d : ℕ} :887    formPoly (Pi.single (0 : Fin (d+1)) (1 : A)) = 1 := by888  unfold formPoly889  rw [Fintype.sum_eq_single 0]890  · simp891  · intro i hi; simp [hi]892 893private lemma coeff_prod_sum_of_natDegree_le {A ι : Type} [CommRing A] (s : Finset ι) (p : ι → A[X])894    (d : ι → ℕ) (h : ∀ i ∈ s, (p i).natDegree ≤ d i) :895    (∏ i ∈ s, p i).coeff (∑ i ∈ s, d i) = ∏ i ∈ s, (p i).coeff (d i) := by896  classical897  induction s using Finset.induction_on with898  | empty => simp899  | insert a s ha ih =>900    rw [Finset.prod_insert ha, Finset.sum_insert ha, Finset.prod_insert ha,901      coeff_mul_add_eq_of_natDegree_le (h a (by simp)), ih (fun i hi => h i (by simp [hi]))]902    exact (natDegree_prod_le _ _).trans (Finset.sum_le_sum fun i hi => h i (by simp [hi]))903 904end905 906/-! ### Type events on products of cells -/907 908section ProductCells909 910variable {ι Y : Type} [Fintype ι] {X : ι → Type}911 912/-- If `f` maps the product of the cells `C j` into `E₀`, and a point `a` of that product has913`f a ∈ P` exactly when `a j ∈ Ev j (τ j)` for some `τ` with `σ = ∑ j, τ j`, then the trace of914`f ⁻¹' (E₀ ∩ P)` on the product is a finite union of products of events. -/915theorem preimage_inter_pi_eq_biUnion (f : (∀ j, X j) → Y) (C : ∀ j, Set (X j))916    (E₀ P : Set Y) (hf : Set.MapsTo f (Set.pi Set.univ C) E₀) (Ev : ∀ j, FactorType → Set (X j))917    (W : ι → Finset FactorType) (hW : ∀ j τ, (C j ∩ Ev j τ).Nonempty → τ ∈ W j)918    (σ : FactorType) (hP : ∀ a ∈ Set.pi Set.univ C,919      f a ∈ P ↔ ∃ τ : ι → FactorType, (∀ j, a j ∈ Ev j (τ j)) ∧ σ = ∑ j, τ j)920    (T : Finset (ι → FactorType)) (hT : ∀ τ, τ ∈ T ↔ (∀ j, τ j ∈ W j) ∧ σ = ∑ j, τ j) :921    f ⁻¹' (E₀ ∩ P) ∩ Set.pi Set.univ C = ⋃ τ ∈ T, Set.pi Set.univ fun j ↦ C j ∩ Ev j (τ j) := by922  ext a923  simp only [Set.mem_inter_iff, Set.mem_preimage, Set.mem_iUnion, hT, Set.mem_univ_pi,924    exists_prop, forall_and]925  constructor926  · rintro ⟨⟨-, haP⟩, haC⟩927    obtain ⟨τ, hτ, hσ⟩ := (hP a (Set.mem_univ_pi.2 haC)).1 haP928    exact ⟨τ, ⟨fun j ↦ hW j (τ j) ⟨a j, haC j, hτ j⟩, hσ⟩, haC, hτ⟩929  · rintro ⟨τ, ⟨-, hσ⟩, haC, hτ⟩930    have ha := Set.mem_univ_pi.2 haC931    exact ⟨⟨hf ha, (hP a ha).2 ⟨τ, hτ, hσ⟩⟩, haC⟩932 933omit [Fintype ι] in934/-- Products of events `Ev j (τ j)` are pairwise disjoint for distinct `τ` when each family935`Ev j` is pairwise disjoint. -/936theorem pairwiseDisjoint_pi_events (C : ∀ j, Set (X j)) (Ev : ∀ j, FactorType → Set (X j))937    (huniq : ∀ j τ τ' x, x ∈ Ev j τ → x ∈ Ev j τ' → τ = τ') (T : Set (ι → FactorType)) :938    T.PairwiseDisjoint fun τ ↦ Set.pi Set.univ fun j ↦ C j ∩ Ev j (τ j) :=939  fun _ _ _ _ hne ↦ Set.disjoint_left.2 fun _ ha ha' ↦ hne <| funext fun j ↦940    huniq j _ _ _ (ha j (Set.mem_univ _)).2 (ha' j (Set.mem_univ _)).2941 942/-- If `f` is continuous and bijective from a closed subset `C` of a compact space onto `E₀`, and943the trace on `C` of `f ⁻¹' (E₀ ∩ A)` is measurable, then `E₀ ∩ A` is measurable. -/944theorem measurableSet_inter_of_bijOn {Z : Type} [TopologicalSpace Z] [CompactSpace Z]945    [MeasurableSpace Z] [BorelSpace Z] [TopologicalSpace Y] [T2Space Y] [MeasurableSpace Y]946    [BorelSpace Y] {f : Z → Y} {C : Set Z} {E₀ A : Set Y} (hC : IsClosed C)947    (hf : ContinuousOn f C) (hbij : Set.BijOn f C E₀) (hA : MeasurableSet (f ⁻¹' (E₀ ∩ A) ∩ C)) :948    MeasurableSet (E₀ ∩ A) := by949  have := image_measurable_of_continuousOn_injOn_compact Z Y f C hC hf hbij.injOn _ hA950  rwa [Set.inter_assoc, Set.inter_self, Set.image_preimage_inter, hbij.image_eq,951    Set.inter_assoc, Set.inter_comm A, ← Set.inter_assoc, Set.inter_self] at this952 953open MeasureTheory in954/-- If `f` is measure preserving from `Measure.pi m` to `ν`, the product of the cells `C j` is955conull, and the trace on it of `f ⁻¹' E` is the finite union over `τ ∈ T` of products of the956measurable factor events `C j ∩ Ev j (τ j)` (each family `Ev j` being pairwise disjoint), then the957`ν`-mass of `E` is the sum over `T` of the products of the factor masses. -/958theorem measure_eq_sum_prod_of_preimage [∀ j, MeasurableSpace (X j)] [MeasurableSpace Y]959    (m : ∀ j, Measure (X j)) [∀ j, SigmaFinite (m j)] {ν : Measure Y} {f : (∀ j, X j) → Y}960    (hf : MeasurePreserving f (Measure.pi m) ν) (C : ∀ j, Set (X j))961    (hC : Measure.pi m (Set.pi Set.univ C)ᶜ = 0) (hm : ∀ j s, m j (C j ∩ s) = m j s)962    (Ev : ∀ j, FactorType → Set (X j)) (hEv : ∀ j τ, MeasurableSet (C j ∩ Ev j τ))963    (huniq : ∀ j τ τ' x, x ∈ Ev j τ → x ∈ Ev j τ' → τ = τ') {E : Set Y} (hE : MeasurableSet E)964    (T : Finset (ι → FactorType))965    (hpre : f ⁻¹' E ∩ Set.pi Set.univ C = ⋃ τ ∈ T, Set.pi Set.univ fun j ↦ C j ∩ Ev j (τ j)) :966    ν E = ∑ τ ∈ T, ∏ j, m j (Ev j (τ j)) := by967  rw [← hf.measure_preimage hE.nullMeasurableSet, ← measure_inter_conull hC, hpre,968    measure_biUnion_finset (pairwiseDisjoint_pi_events C Ev huniq _)969      fun τ _ ↦ .univ_pi fun j ↦ hEv j (τ j)]970  simp only [Measure.pi_pi, hm]971 972/-- The finite convolution of the factor densities over any finite sets of types `W j` containing973the supports of the laws `Dl j` is the density of the product law. -/974theorem sum_prod_densityValue_eq_lawProd {L : ℕ} (Dl : Fin L → Law) (q : ℕ)975    (hnn : ∀ j τ, 0 ≤ (lawCoeff (Dl j) τ).eval (q : ℚ)) (W : Fin L → Finset FactorType)976    (hW : ∀ j, ((Dl j).map Prod.fst).toFinset ⊆ W j) (σ : FactorType) :977    ∑ τ ∈ (Fintype.piFinset W).filter (fun τ ↦ σ = ∑ j, τ j),978      ∏ j, densityValue (lawCoeff (Dl j) (τ j)) q =979        densityValue (lawCoeff (lawProd (List.ofFn Dl)) σ) q := by980  have hy : ∀ j τ, (0 : ℝ) ≤ (((lawCoeff (Dl j) τ).eval (q : ℚ) : ℚ) : ℝ) := fun j τ ↦ by981    exact_mod_cast hnn j τ982  simp only [densityValue]983  rw [Finset.sum_congr rfl fun τ _ ↦ (ENNReal.ofReal_prod_of_nonneg fun j _ ↦ hy j (τ j)).symm,984    ← ENNReal.ofReal_sum_of_nonneg fun τ _ ↦ Finset.prod_nonneg fun j _ ↦ hy j (τ j)]985  obtain ⟨hform, hzero⟩ := law_coeff_prod_formula L Dl σ (q : ℚ)986  rw [hform, Finset.sum_filter]987  simp only [Rat.cast_sum, apply_ite (Rat.cast : ℚ → ℝ), Rat.cast_prod, Rat.cast_zero]988  refine congrArg _ (Finset.sum_subset (Fintype.piFinset_subset _ _ hW) fun τ _ hτ ↦ ?_).symm989  obtain ⟨j, hj⟩ := not_forall.1 (Fintype.mem_piFinset.not.1 hτ)990  rw [Finset.prod_eq_zero (Finset.mem_univ j) (by simp [hzero (Dl j) (τ j) hj]), ite_self]991 992end ProductCells993 994/-! ### Products of reduced forms -/995 996open Polynomial in997/-- A product of reduced forms has type `σ` iff all factor polynomials keep their top coefficient998and the product of the normalized factors has type `σ`. -/999private theorem form_product_mem_affineTypeEvent_iff (O : Type) [CommRing O] [IsDomain O]1000    [IsDiscreteValuationRing O]1001    [IsAdicComplete (IsLocalRing.maximalIdeal O) O] [Finite (IsLocalRing.ResidueField O)] (L : ℕ)1002    (d : Fin L → ℕ) (ν : ∀ j, Fin (d j + 1)) (g : ∀ j, Fin (d j + 1) → IsLocalRing.ResidueField O)1003    (μ : Fin ((∑ j, d j) + 1)) (hμ : formProd d g μ = 1) (a : ∀ j, Fin (d j) → O)1004    (ha : a ∈ Set.pi Set.univ (fun j => reducedFormCell O (d j) (ν j) (g j))) (σ : FactorType) :1005    insertOne μ (reducedFormProduct O d ν g μ a) ∈ affineTypeEvent O (∑ j, d j) σ ↔1006      (∀ j, (formPoly (insertOne (ν j) (a j))).coeff (d j) ≠ 0) ∧1007        HasPolynomialTypeF O (∏ j, C (algebraMap O (BaseField O)1008          ((formPoly (insertOne (ν j) (a j))).coeff (d j)))⁻¹ *1009          (formPoly (insertOne (ν j) (a j))).map (algebraMap O (BaseField O))) σ := by1010  have hcell : ∀ j, a j ∈ reducedFormCell O (d j) (ν j) (g j) := fun j ↦ ha j (Set.mem_univ _)1011  obtain ⟨-, -, hu, -⟩ := form_product_maps_to_and_preserves O (Fin L) d ν g μ hμ a a ha ha1012  set ι := algebraMap O (BaseField O)1013  have hι : ∀ {x : O}, x ≠ 0 → ι x ≠ 0 := (map_ne_zero_iff _ (IsFractionRing.injective O _)).21014  set p : Fin L → O[X] := fun j ↦ formPoly (insertOne (ν j) (a j))1015  set c : Fin L → O := fun j ↦ (p j).coeff (d j)1016  set F : Fin L → (BaseField O)[X] := fun j ↦ C (ι (c j))⁻¹ * (p j).map ι with hF1017  set Q := ∏ j, p j1018  change IsUnit (Q.coeff μ) at hu1019  set u := Ring.inverse (Q.coeff μ)1020  have hu0' : u ≠ 0 := left_ne_zero_of_mul_eq_one (Ring.inverse_mul_cancel _ hu)1021  have hu0 : ι u ≠ 0 := hι hu0'1022  have hQn : Q.coeff (∑ j, d j) = ∏ j, c j :=1023    coeff_prod_sum_of_natDegree_le _ _ _ fun j _ ↦ formPoly_natDegree_le _1024  have hred : insertOne μ (reducedFormProduct O d ν g μ a) =1025      fun i : Fin ((∑ j, d j) + 1) ↦ u * Q.coeff i := by1026    unfold reducedFormProduct1027    rw [ite_eq_left hcell]1028    have := Fin.insertNth_self_removeNth μ1029      (normalizeAt μ (formProd d fun j ↦ insertOne (ν j) (a j)))1030    rw [normalizeAt, Pi.smul_apply, smul_eq_mul, Ring.inverse_mul_cancel1031      (formProd d (fun j ↦ insertOne (ν j) (a j)) μ) hu] at this1032    exact this1033  have hAff : affinePolynomial O (fun i : Fin ((∑ j, d j) + 1) ↦ u * Q.coeff i) =1034      C (ι u) * Q.map ι := by1035    have hlt : (Q.map ι).natDegree < ∑ j, d j + 1 := Nat.lt_succ_of_le (natDegree_map_le.trans1036      ((natDegree_prod_le _ _).trans (Finset.sum_le_sum fun j _ ↦ formPoly_natDegree_le _)))1037    conv_rhs => rw [as_sum_range' (Q.map ι) _ hlt]1038    rw [affinePolynomial, Finset.mul_sum, ← Fin.sum_univ_eq_sum_range]1039    refine Finset.sum_congr rfl fun i _ ↦ ?_1040    rw [← C_mul_X_pow_eq_monomial, coeff_map, map_mul, C_mul, mul_assoc]1041  have hprod : C (ι (u * Q.coeff (∑ j, d j)))⁻¹ * (C (ι u) * Q.map ι) = ∏ j, F j := by1042    rw [hF, Finset.prod_mul_distrib, ← map_prod C, ← Polynomial.map_prod, hQn, map_mul, mul_inv,1043      C_mul, mul_mul_mul_comm, ← C_mul, inv_mul_cancel₀ hu0, C_1, one_mul, map_prod,1044      Finset.prod_inv_distrib]1045  have hι' : algebraMap O (BaseField O) = ι := rfl1046  simp only [affineTypeEvent, hred, Set.mem_ofPred_eq, hAff, Fin.val_last, hι', hprod]1047  rw [hQn, mul_ne_zero_iff, Finset.prod_ne_zero_iff]1048  simp [hu0', c, p]1049 1050open Polynomial in1051/-- A product of reduced forms has type `σ` iff its factors have types `τ j` (block types at the1052infinity factor) with `σ = ∑ j, τ j`. -/1053private theorem form_product_type_iff (O : Type) [CommRing O] [IsDomain O]1054    [IsDiscreteValuationRing O]1055    [IsAdicComplete (IsLocalRing.maximalIdeal O) O] [Finite (IsLocalRing.ResidueField O)] (L : ℕ)1056    (d : Fin L → ℕ) (ν : ∀ j, Fin (d j + 1)) (g : ∀ j, Fin (d j + 1) → IsLocalRing.ResidueField O)1057    (hc : FormsCoprime d g) (μ : Fin ((∑ j, d j) + 1))1058    (hμ : formProd d g μ = 1) (inf : Fin L → Bool)1059    (hclass : ∀ j, if inf j = true then (ν j = 0 ∧ g j = Pi.single 0 1) else ν j = Fin.last (d j))1060    (hone : ∀ i j, inf i = true → inf j = true → i = j) (a : ∀ j, Fin (d j) → O)1061    (ha : a ∈ Set.pi Set.univ (fun j => reducedFormCell O (d j) (ν j) (g j))) (σ : FactorType) :1062    insertOne μ (reducedFormProduct O d ν g μ a) ∈ affineTypeEvent O (∑ j, d j) σ ↔1063      ∃ τ : Fin L → FactorType, (∀ j, a j ∈1064        if inf j = true then infinityBlockEvent O (d j) (τ j) else typeEvent O (d j) (τ j)) ∧1065        σ = ∑ j, τ j := by1066  -- strategy: reduce to the normalized factors `F j`, which are pairwise coprime1067  rw [form_product_mem_affineTypeEvent_iff O L d ν g μ hμ a ha σ]1068  have hcell : ∀ j, a j ∈ reducedFormCell O (d j) (ν j) (g j) := fun j ↦ ha j (Set.mem_univ _)1069  set ι := algebraMap O (BaseField O)1070  have hι : ∀ {x : O}, x ≠ 0 → ι x ≠ 0 := (map_ne_zero_iff _ (IsFractionRing.injective O _)).21071  set p : Fin L → O[X] := fun j ↦ formPoly (insertOne (ν j) (a j))1072  set c : Fin L → O := fun j ↦ (p j).coeff (d j)1073  set F : Fin L → (BaseField O)[X] := fun j ↦ C (ι (c j))⁻¹ * (p j).map ι with hF1074  have hpred : ∀ j, (p j).map (IsLocalRing.residue O) = formPoly (g j) := fun j ↦ by1075    simp only [p, formPoly_map]; exact congrArg formPoly (hcell j).21076  have hpfin : ∀ j, inf j = false → p j = monicPolyO (a j) := fun j hj ↦ by1077    have hcl := hclass j1078    rw [ite_eq_right (by simp [hj])] at hcl1079    simp only [p, hcl, formPoly_insertOne_last]1080  have hpinf : ∀ j, inf j = true → (p j).map (IsLocalRing.residue O) = 1 := fun j hj ↦ by1081    have hcl := hclass j1082    rw [ite_eq_left hj] at hcl1083    rw [hpred, hcl.2, formPoly_single_zero]1084  have hmon : ∀ j, (monicPolyO (a j)).Monic := fun j ↦ (monic_poly_reconstruct O _ (a j)).11085  have hblock : ∀ j (t : FactorType),1086      (a j ∈ if inf j = true then infinityBlockEvent O (d j) t else typeEvent O (d j) t) ↔1087        c j ≠ 0 ∧ HasPolynomialTypeF O (F j) t := by1088    intro j t1089    by_cases hj : inf j = true1090    · have hcl := hclass j1091      rw [ite_eq_left hj] at hcl ⊢1092      simp only [infinityBlockEvent, Set.mem_ofPred_eq, F, c, p, hcl.1, ι]1093    · rw [ite_eq_right hj]1094      obtain ⟨hm, hd, -⟩ := monic_poly_reconstruct O _ (a j)1095      have hc1 : c j = 1 := by simp only [c, hpfin j (by simpa using hj), ← hd]; exact hm1096      simp [typeEvent, hF, hc1, hpfin j (by simpa using hj), ι, monicPolyO_map_fraction]1097  have hcop : (∀ j, c j ≠ 0) → ∀ i j, i ≠ j → IsCoprime (F i) (F j) := by1098    intro hall i j hij1099    have hU : ∀ k, IsUnit (C (ι (c k))⁻¹) := fun k ↦1100      isUnit_C.mpr (IsUnit.mk0 _ (inv_ne_zero (hι (hall k))))1101    rw [hF, isCoprime_mul_units_left (hU i) (hU j)]1102    rcases hi : inf i with _ | _ <;> rcases hj : inf j with _ | _1103    · rw [hpfin i hi, hpfin j hj]1104      refine coprime_of_coprime_residue_dvr O (BaseField O) _ _ (hmon i) (hmon j) ?_1105      rw [← hpfin i hi, ← hpfin j hj, hpred, hpred]1106      exact hc.1 i j hij1107    · rw [hpfin i hi]1108      exact coprime_of_monic_reduction_one O (BaseField O) _ _ (hmon i) (hpinf j hj)1109    · rw [hpfin j hj]1110      exact (coprime_of_monic_reduction_one O (BaseField O) _ _ (hmon j) (hpinf i hi)).symm1111    · exact absurd (hone i j hi hj) hij1112  simp only [hblock]1113  by_cases hall : ∀ j, c j ≠ 01114  · rw [coprime_family_type_iff O (Fin L) F (fun j ↦ ?_) (hcop hall) σ]1115    · exact ⟨fun ⟨_, τ, h1, h2⟩ ↦ ⟨τ, fun j ↦ ⟨hall j, h1 j⟩, h2⟩,1116        fun ⟨τ, h1, h2⟩ ↦ ⟨hall, τ, fun j ↦ (h1 j).2, h2⟩⟩1117    have hdeg : ((p j).map ι).natDegree = d j := natDegree_eq_of_le_of_coeff_ne_zero1118      (natDegree_map_le.trans (formPoly_natDegree_le _)) (by rw [coeff_map]; exact hι (hall j))1119    rw [Monic, hF, leadingCoeff_mul, leadingCoeff_C, leadingCoeff, hdeg, coeff_map,1120      inv_mul_cancel₀ (hι (hall j))]1121  · exact ⟨fun ⟨h, _⟩ ↦ absurd h hall, fun ⟨τ, h1, _⟩ ↦ absurd (fun j ↦ (h1 j).1) hall⟩1122 1123/-- A factorization type occurring in a block event of a cell of degree `d` has degree at most1124`d`, so it lies in a fixed finite set of types. -/1125private theorem type_mem_of_event_nonempty (O : Type) [CommRing O] [IsDomain O]1126    [IsDiscreteValuationRing O] [IsAdicComplete (IsLocalRing.maximalIdeal O) O]1127    [Finite (IsLocalRing.ResidueField O)] (d : ℕ) (ν : Fin (d + 1))1128    (g : Fin (d + 1) → IsLocalRing.ResidueField O) (b : Bool) (τ : FactorType)1129    (h : (reducedFormCell O d ν g ∩1130      (if b = true then infinityBlockEvent O d τ else typeEvent O d τ)).Nonempty) :1131    τ ∈ (Finset.range (d + 1)).biUnion fun n ↦ (finite_types_of_degree n).toFinset := by1132  obtain ⟨a, _, ha⟩ := h1133  obtain ⟨F, hF, hdeg⟩ : ∃ F : Polynomial (BaseField O),1134      HasPolynomialTypeF O F τ ∧ F.natDegree ≤ d := by1135    split_ifs at ha1136    · exact ⟨_, ha.2, (Polynomial.natDegree_C_mul_le _ _).trans1137        (Polynomial.natDegree_map_le.trans (formPoly_natDegree_le _))⟩1138    · exact ⟨_, ha, (monic_polynomial_degree O d a).2.le⟩1139  obtain ⟨hv, htd⟩ := polynomial_type_degree O F τ hF1140  simp only [Finset.mem_biUnion, Finset.mem_range, Set.Finite.mem_toFinset]1141  exact ⟨typeDegree τ, by omega, hv, rfl⟩1142 1143section1144 1145open MeasureTheory1146 1147/-- **Product law.** If the factor cells have laws `Dl j`, then the product cell has law1148`lawProd (List.ofFn Dl)`. -/1149theorem form_product_type_event_law (O : Type) [CommRing O] [IsDomain O] [IsDiscreteValuationRing O]1150    [IsAdicComplete (IsLocalRing.maximalIdeal O) O] [Finite (IsLocalRing.ResidueField O)]1151    [TopologicalSpace O] [IsTopologicalRing O] [CompactSpace O] [T2Space O]1152    [SecondCountableTopology O] [MeasurableSpace O] [BorelSpace O]1153    (htop : IsAdic (IsLocalRing.maximalIdeal O)) (L : ℕ) (d : Fin L → ℕ) (ν : ∀ j, Fin (d j + 1))1154    (g : ∀ j, Fin (d j + 1) → IsLocalRing.ResidueField O) (hgν : ∀ j, g j (ν j) = 1)1155    (hc : FormsCoprime d g) (μ : Fin ((∑ j, d j) + 1)) (hμ : formProd d g μ = 1)1156    (inf : Fin L → Bool)1157    (hclass : ∀ j, if inf j = true then (ν j = 0 ∧ g j = Pi.single 0 1) else ν j = Fin.last (d j))1158    (hone : ∀ i j, inf i = true → inf j = true → i = j) (Dl : Fin L → Law)1159    (hnn : ∀ (j : Fin L) (τ : FactorType), 0 ≤ (lawCoeff (Dl j) τ).eval1160      (Nat.card (IsLocalRing.ResidueField O)))1161    (hlaw : ∀ (j : Fin L) (τ : FactorType), MeasurableSet1162      (reducedFormCell O (d j) (ν j) (g j) ∩ (if inf j = true then infinityBlockEvent O (d j) τ1163      else typeEvent O (d j) τ)) ∧ cellMeasure O (d j) (reducedFormCell O (d j) (ν j) (g j))1164      (if inf j = true then infinityBlockEvent O (d j) τ else typeEvent O (d j) τ) = densityValue1165      (lawCoeff (Dl j) τ) (Nat.card (IsLocalRing.ResidueField O))) (σ : FactorType) :1166    MeasurableSet (reducedFormCell O (∑ j, d j) μ (formProd d g) ∩1167      (insertOne μ ⁻¹' affineTypeEvent O (∑ j, d j) σ)) ∧ cellMeasure O (∑ j, d j)1168      (reducedFormCell O (∑ j, d j) μ (formProd d g)) (insertOne μ ⁻¹' affineTypeEvent O (∑ j, d j)1169      σ) = densityValue (lawCoeff (lawProd (List.ofFn Dl)) σ)1170      (Nat.card (IsLocalRing.ResidueField O)) := by1171  have hcellj := fun j => normalized_form_cell_cylinder_mass O htop (d j) (ν j) (g j) (hgν j)1172  have hcellD := normalized_form_cell_cylinder_mass O htop (∑ j, d j) μ (formProd d g) hμ1173  have : ∀ j, IsProbabilityMeasure (cellMeasure O (d j) (reducedFormCell O (d j) (ν j) (g j))) :=1174    fun j => (hcellj j).2.2.2.2.2.11175  -- strategy: pull the event back along the form-product bijection to a finite disjoint union1176  -- of products of factor events, whose product measures multiply the factor laws1177  let W : Fin L → Finset FactorType := fun j => (Finset.range (d j + 1)).biUnion1178    (fun n => (finite_types_of_degree n).toFinset) ∪ ((Dl j).map Prod.fst).toFinset1179  have hW : ∀ j τ, (reducedFormCell O (d j) (ν j) (g j) ∩1180      (if inf j = true then infinityBlockEvent O (d j) τ else typeEvent O (d j) τ)).Nonempty → τ ∈1181      W j := fun j τ h ↦ Finset.mem_union_left _ (type_mem_of_event_nonempty O _ _ _ _ τ h)1182  have hmeas := fun j τ ↦ (hlaw j τ).11183  set T := (Fintype.piFinset W).filter (fun τ : Fin L → FactorType => σ = ∑ j, τ j) with hT1184  have hbij := form_product_bijection O (Fin L) d ν g hgν hc μ hμ1185  have hpre := preimage_inter_pi_eq_biUnion _ _ _ (insertOne μ ⁻¹' affineTypeEvent O (∑ j, d j) σ)1186    hbij.mapsTo (fun j τ ↦ if inf j = true then infinityBlockEvent O (d j) τ else1187      typeEvent O (d j) τ) W hW σ (form_product_type_iff O L d ν g hc μ hμ inf hclass hone · · σ) T1188    fun τ ↦ by rw [hT, Finset.mem_filter, Fintype.mem_piFinset]1189  have hE : MeasurableSet (reducedFormCell O (∑ j, d j) μ (formProd d g) ∩1190      (insertOne μ ⁻¹' affineTypeEvent O (∑ j, d j) σ)) := by1191    choose c hcc using fun j ↦ (hcellj j).2.2.11192    refine measurableSet_inter_of_bijOn ?_ ?_ hbij1193      (hpre ▸ Finset.measurableSet_biUnion _ fun τ _ ↦ .univ_pi fun j ↦ hmeas j (τ j))1194    · convert (coordinate_cylinder_clopen O (IsLocalRing.maximalIdeal O) htop 1).2.2 (Fin L) d c1195        using 3 with j1196      rw [pow_one]; exact (hcellj j).2.1 (c j) (hcc j)1197    · exact continuousOn_of_congruence_compatible O O (IsLocalRing.maximalIdeal O) htop1198        (IsLocalRing.maximalIdeal O) htop (Fin L) d (∑ j, d j) _ _ fun L' a ha a' ha' h ↦1199        (form_product_maps_to_and_preserves O (Fin L) d ν g μ hμ a a' ha ha').2.2.2 L' h1200  refine ⟨hE, ?_⟩1201  rw [← cellMeasure_cell_inter hcellD.2.2.2.1, measure_eq_sum_prod_of_preimage _1202    (form_cells_measure_preserving O htop (Fin L) d ν g hgν hc μ hμ) _1203    (form_cell_product_measure O htop (Fin L) d ν g hgν).2.11204    (fun j ↦ cellMeasure_cell_inter (hcellj j).2.2.2.1) _ hmeas (fun j τ τ' x h1 h2 ↦ by1205      split_ifs at h1 h21206      exacts [polynomial_type_unique O _ _ _ h1.2 h2.2, polynomial_type_unique O _ _ _ h1 h2])1207    hE T hpre, Finset.sum_congr rfl fun τ _ ↦ Finset.prod_congr rfl fun j _ ↦ (hlaw j (τ j)).2]1208  exact sum_prod_densityValue_eq_lawProd Dl _ hnn W (fun _ ↦ Finset.subset_union_right) σ1209 1210end1211 1212section1213 1214variable {k : Type} [Field k]1215 1216private noncomputable def directionPoly : ClosedDirection k → Polynomial k1217  | none => 11218  | some φ => φ.val1219 1220open scoped Classical in1221private noncomputable def directionOfPoly (p : Polynomial k) : ClosedDirection k :=1222  if hp : p.Monic ∧ Irreducible p then some ⟨p, hp⟩ else none1223 1224private lemma directionOfPoly_directionPoly (φ : AffineDirection k) :1225    directionOfPoly (directionPoly (some φ)) = some φ := by1226  simp [directionOfPoly, directionPoly, φ.2]1227 1228private lemma directionPoly_directionOfPoly (p : Polynomial k) (hp : p.Monic ∧ Irreducible p) :1229    directionPoly (directionOfPoly p) = p := by1230  simp [directionOfPoly, directionPoly, hp]1231 1232open scoped Classical in1233private lemma toFinsupp_prod_directionPoly (S : Multiset (ClosedDirection k)) :1234    (Multiset.toFinsupp S).prod (fun a r => directionPoly a ^ r) = (S.map directionPoly).prod := by1235  induction S using Multiset.induction with1236  | empty => simp1237  | cons a S ih =>1238    rw [← Multiset.singleton_add, Multiset.toFinsupp_add, Multiset.toFinsupp_singleton,1239        Finsupp.prod_add_index' (by simp) (by intros; rw [pow_add]),1240      ih]1241    simp1242 1243open scoped Classical in1244private lemma toFinsupp_sum_directionDegree (S : Multiset (ClosedDirection k)) :1245    (Multiset.toFinsupp S).sum (fun a r => directionDegree k a * r) =1246      (S.map (directionDegree k)).sum := by1247  induction S using Multiset.induction with1248  | empty => simp1249  | cons a S ih =>1250    rw [← Multiset.singleton_add, Multiset.toFinsupp_add, Multiset.toFinsupp_singleton,1251        Finsupp.sum_add_index' (by simp) (by intros; rw [mul_add]),1252      ih]1253    simp1254 1255private lemma divisorAffinePolynomial_eq_prod (D : ExceptionalDivisor k) :1256    divisorAffinePolynomial k D = D.prod (fun a r => directionPoly a ^ r) := by1257  unfold divisorAffinePolynomial1258  congr 11259  funext a r1260  cases a <;> simp [directionPoly]1261 1262end1263 1264/-- Every monic polynomial of degree `n` over `k` is the affine polynomial of a unique affine1265divisor of degree `n`. -/1266theorem monic_polynomial_divisor (k : Type) [Field k] (n : ℕ) (G : Polynomial k) (hG : G.Monic)1267    (hn : G.natDegree = n) :1268    ∃! D : ExceptionalDivisor k, AffineDivisor k n D ∧ divisorAffinePolynomial k D = G := by1269  classical1270  set N := UniqueFactorizationMonoid.normalizedFactors G with hN1271  have hG0 : G ≠ 0 := hG.ne_zero1272  have hNmi : ∀ p ∈ N, p.Monic ∧ Irreducible p := fun p hp =>1273    ⟨by rw [← UniqueFactorizationMonoid.normalize_normalized_factor p hp]; exact1274        Polynomial.monic_normalize (UniqueFactorizationMonoid.ne_zero_of_mem_normalizedFactors hp),1275     UniqueFactorizationMonoid.irreducible_of_normalized_factor p hp⟩1276  have hNprod : N.prod = G := by1277    have := Polynomial.eq_of_monic_of_associated1278      (Polynomial.monic_multiset_prod_of_monic N id (fun p hp => (hNmi p hp).1)) hG1279    simp only [Multiset.map_id] at this1280    apply this1281    exact UniqueFactorizationMonoid.prod_normalizedFactors hG01282  have hNg : (N.map directionOfPoly).map directionPoly = N := by1283    rw [Multiset.map_map]1284    conv_rhs => rw [← Multiset.map_id N]1285    exact Multiset.map_congr rfl (fun p hp => directionPoly_directionOfPoly p (hNmi p hp))1286  refine ⟨Multiset.toFinsupp (N.map directionOfPoly), ⟨⟨?_, ?_⟩, ?_⟩, ?_⟩1287  · change Multiset.toFinsupp (N.map directionOfPoly) none = 01288    rw [Multiset.toFinsupp_apply, Multiset.count_eq_zero, Multiset.mem_map]1289    rintro ⟨p, hp, hgp⟩1290    simp [directionOfPoly, hNmi p hp] at hgp1291  · unfold exceptionalDegree1292    rw [toFinsupp_sum_directionDegree, Multiset.map_map, ← hn, ← hNprod,1293      Polynomial.natDegree_multiset_prod_of_monic _ (fun p hp => (hNmi p hp).1)]1294    congr 11295    exact Multiset.map_congr rfl (fun p hp => by simp [directionOfPoly, hNmi p hp, directionDegree])1296  · rw [divisorAffinePolynomial_eq_prod, toFinsupp_prod_directionPoly, hNg, hNprod]1297  · rintro D ⟨⟨hinf, hdeg⟩, hprod⟩1298    set S := Finsupp.toMultiset D with hS1299    have hDS : D = Multiset.toFinsupp S := (Multiset.toFinsupp_eq_iff.2 rfl).symm1300    have hnone : none ∉ S := by1301      rw [hS, Finsupp.mem_toMultiset]1302      simpa [Finsupp.mem_support_iff, infinityDirection] using hinf1303    have hSmi : ∀ p ∈ S.map directionPoly, p.Monic ∧ Irreducible p := by1304      intro p hp1305      obtain ⟨a, ha, rfl⟩ := Multiset.mem_map.1 hp1306      cases a with1307      | none => exact absurd ha hnone1308      | some φ => exact φ.21309    have hSprod : (S.map directionPoly).prod = G := by1310      rw [← hprod, divisorAffinePolynomial_eq_prod, hDS, toFinsupp_prod_directionPoly]1311    have hrel : Multiset.Rel Associated (S.map directionPoly) N := by1312      apply UniqueFactorizationMonoid.factors_unique (fun p hp => (hSmi p hp).2)1313        (fun p hp => UniqueFactorizationMonoid.irreducible_of_normalized_factor p hp)1314      rw [hSprod, hNprod]1315      exact Associated.refl G1316    have hEq : S.map directionPoly = N := by1317      rw [← Multiset.rel_eq]1318      refine hrel.mono ?_1319      intro a ha b hb hab1320      exact Polynomial.eq_of_monic_of_associated (hSmi a ha).1 (hNmi b hb).1 hab1321    rw [hDS, ← hEq, Multiset.map_map]1322    congr 11323    conv_lhs => rw [← Multiset.map_id S]1324    refine Multiset.map_congr rfl (fun a ha => ?_)1325    cases a with1326    | none => exact absurd ha hnone1327    | some φ => exact (directionOfPoly_directionPoly φ).symm1328 1329end SurfaceBlowup