File size: 22,659 Bytes
7f5fab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import Mathlib

namespace OAI

/-! Normal Divisors. -/



namespace NumericalDimensionOne

variable {R K : Type*} [CommRing R] [IsDomain R]
  [Field K] [Algebra R K] [IsFractionRing R K]

def denominatorIdeal (R : Type*) [CommRing R] [Algebra R K] (x : K) : Ideal R :=
  (Algebra.linearMap R K).range.colon {x}

omit [IsDomain R] [IsFractionRing R K] in
lemma mem_denominatorIdeal (x : K) (r : R) :
    r ∈ denominatorIdeal R x ↔ ∃ a : R, algebraMap R K a = algebraMap R K r * x := by
  simp [denominatorIdeal, Submodule.mem_colon_singleton, Algebra.smul_def]

lemma denominatorIdeal_ne_bot (x : K) : denominatorIdeal R x ≠ ⊥ := by
  obtain ⟨⟨a, b⟩, hab⟩ := IsLocalization.surj (nonZeroDivisors R) x
  have hb : (b : R) ≠ 0 := mem_nonZeroDivisors_iff_ne_zero.mp b.2
  intro h
  have hbmem : (b : R) ∈ denominatorIdeal R x := by
    rw [mem_denominatorIdeal]
    exact ⟨a, by simpa [mul_comm] using hab.symm⟩
  rw [h, Ideal.mem_bot] at hbmem
  exact hb hbmem

omit [IsDomain R] [IsFractionRing R K] in
lemma denominatorIdeal_eq_top_iff (x : K) :
    denominatorIdeal R x = ⊤ ↔ ∃ a : R, algebraMap R K a = x := by
  rw [Ideal.eq_top_iff_one, mem_denominatorIdeal]
  simp

variable [IsNoetherianRing R] [IsIntegrallyClosed R]

theorem prime_denominator_height {x : K} (hp : (denominatorIdeal R x).IsPrime) :
    (denominatorIdeal R x).height = 1 := by
  let P := denominatorIdeal R x
  let M : Submodule R K := Submodule.map (Algebra.linearMap R K) P
  have hx : ¬ ∃ a : R, algebraMap R K a = x := by
    intro h
    exact hp.ne_top ((denominatorIdeal_eq_top_iff x).mpr h)
  have hM : M ≠ ⊥ := by
    intro heq
    apply denominatorIdeal_ne_bot (R := R) x
    apply le_antisymm _ bot_le
    intro a ha
    have ham : algebraMap R K a ∈ M := ⟨a, ha, rfl⟩
    rw [heq, Submodule.mem_bot] at ham
    exact (IsFractionRing.injective R K) (by simpa using ham)
  have hnotstable : ¬ ∀ y ∈ M, x * y ∈ M := by
    intro hstable
    have hi : IsIntegral R x := isIntegral_of_smul_mem_submodule M hM
      ((IsNoetherian.noetherian P).map (Algebra.linearMap R K)) x hstable
    exact hx (IsIntegrallyClosed.algebraMap_eq_of_integral hi)
  push Not at hnotstable
  obtain ⟨y, hyM, hxyM⟩ := hnotstable
  obtain ⟨a, haP, rfl⟩ := hyM
  obtain ⟨b, hb⟩ := (mem_denominatorIdeal (R := R) x a).mp haP
  have hbP : b ∉ P := by
    intro hbP
    apply hxyM
    exact ⟨b, hbP, by simpa [mul_comm] using hb⟩
  have hmin : P ∈ (Ideal.span {a}).minimalPrimes := by
    refine ⟨⟨hp, (Ideal.span_singleton_le_iff_mem P).mpr haP⟩, ?_⟩
    intro Q hQ hQP c hcP
    obtain ⟨d, hd⟩ := (mem_denominatorIdeal (R := R) x c).mp hcP
    have hbc : b * c = a * d := by
      apply IsFractionRing.injective R K
      rw [map_mul, map_mul, hb, hd]
      ring
    have hac : b * c ∈ Q := by
      rw [hbc]
      exact Q.mul_mem_right d (hQ.2 (Ideal.subset_span (Set.mem_singleton a)))
    exact (hQ.1.mem_or_mem hac).resolve_left fun hbQ => hbP (hQP hbQ)
  have hle : P.height ≤ 1 :=
    Ideal.height_le_one_of_isPrincipal_of_mem_minimalPrimes (Ideal.span {a}) P hmin
  have hne : P.height ≠ 0 := Ideal.height_eq_zero_iff_eq_bot.not.mpr
    (denominatorIdeal_ne_bot x)
  exact le_antisymm hle (Order.one_le_iff_ne_zero.mpr hne)

omit [IsDomain R] [IsFractionRing R K] [IsNoetherianRing R] [IsIntegrallyClosed R] in

lemma quotient_annihilator_eq (x : K) :
    (⊥ : Submodule R (K ⧸ (Algebra.linearMap R K).range)).colon
      {Submodule.Quotient.mk x} = denominatorIdeal R x := by
  ext r
  rw [Submodule.mem_colon_singleton, Submodule.mem_bot,
    ← Submodule.Quotient.mk_smul, Submodule.Quotient.mk_eq_zero]
  exact (Submodule.mem_colon_singleton).symm

theorem normal_domain_height_one_criterion (x : K) :
    (∃ a : R, algebraMap R K a = x) ↔
      ∀ p : Ideal R, p.IsPrime → p.height = 1 →
        ∃ s : R, s ∉ p ∧ ∃ a : R,
          algebraMap R K s * x = algebraMap R K a := by
  constructor
  · rintro ⟨a, rfl⟩ p hp _
    refine ⟨1, ?_, a, by simp⟩
    exact fun h => hp.ne_top ((Ideal.eq_top_iff_one p).mpr h)
  · intro hregular
    by_contra hx
    let N : Submodule R K := (Algebra.linearMap R K).range
    have hxzero : Submodule.Quotient.mk x ≠ (0 : K ⧸ N) := by
      intro hzero
      exact hx ((LinearMap.mem_range).mp
        ((Submodule.Quotient.mk_eq_zero N).mp hzero))
    obtain ⟨P, hP, hxs⟩ :=
      exists_le_isAssociatedPrime_of_isNoetherianRing R
        (Submodule.Quotient.mk x : K ⧸ N) hxzero
    obtain ⟨hp, y, hy⟩ := isAssociatedPrime_iff.mp hP
    obtain ⟨w, rfl⟩ := Submodule.Quotient.mk_surjective N y
    have heq : P = denominatorIdeal R w := hy.trans (quotient_annihilator_eq w)
    have hheight : P.height = 1 := by
      rw [heq]
      exact prime_denominator_height (heq ▸ hp)
    obtain ⟨s, hs, a, ha⟩ := hregular P hp hheight
    apply hs
    apply hxs
    rw [quotient_annihilator_eq, mem_denominatorIdeal]
    exact ⟨a, ha.symm⟩

end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory

namespace NumericalDimensionOne

class StalkwiseNormal (X : Scheme) : Prop where
  integrallyClosed_stalk : ∀ x : X, IsIntegrallyClosed (X.presheaf.stalk x)

attribute [instance] StalkwiseNormal.integrallyClosed_stalk

abbrev PrimeDivisor (X : Scheme) := {x : X // Order.coheight x = 1}
abbrev WeilDivisor (X : Scheme) := PrimeDivisor X →₀ ℤ

instance nonempty_top_open (X : Scheme) [Nonempty X] : Nonempty (⊤ : X.Opens) :=
  ⟨⟨Classical.arbitrary X, trivial⟩⟩

variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X]

theorem dvr_at_prime_divisor (p : PrimeDivisor X) :
    IsDiscreteValuationRing (X.presheaf.stalk p.1) := by
  have hd : ringKrullDim (X.presheaf.stalk p.1) = 1 :=
    by rw [ringKrullDim_stalk_eq_coheight, p.2]; rfl
  have hnf : ¬ IsField (X.presheaf.stalk p.1) :=
    (ringKrullDim_eq_one_iff_of_isLocalRing_isDomain.mp hd).1
  have : Ring.KrullDimLE 1 (X.presheaf.stalk p.1) :=
    krullDimLE_of_coheight_le p.2.le
  have : Ring.DimensionLEOne (X.presheaf.stalk p.1) :=
    ⟨fun {I} hI hprime =>
      Ring.krullDimLE_one_iff_of_noZeroDivisors.mp inferInstance I hI hprime⟩
  have : IsDedekindDomain (X.presheaf.stalk p.1) := { }
  exact ((IsDiscreteValuationRing.TFAE (X.presheaf.stalk p.1) hnf).out 3 1).mp
    (show IsDedekindDomain (X.presheaf.stalk p.1) from inferInstance)

def IsDivisorSection (D : WeilDivisor X) (f : X.functionField) : Prop :=
  f = 0 ∨ ∀ p : PrimeDivisor X, 0 ≤ X.ord f p.1 + D p

theorem divisorSection_add {D : WeilDivisor X} {f g : X.functionField}
    (hf : IsDivisorSection D f) (hg : IsDivisorSection D g) :
    IsDivisorSection D (f + g) := by
  rcases hf with rfl | hf
  · simpa [IsDivisorSection] using hg
  rcases hg with rfl | hg
  · simpa [IsDivisorSection] using (Or.inr hf : IsDivisorSection D f)
  by_cases hfg : f + g = 0
  · exact Or.inl hfg
  right
  intro p
  have := dvr_at_prime_divisor p
  have h := X.ord_add (x := p.1) hfg
  have hm : -(D p) ≤ min (X.ord f p.1) (X.ord g p.1) :=
    le_min (by have := hf p; omega) (by have := hg p; omega)
  omega

omit [StalkwiseNormal X] in
theorem divisorSection_smul {D : WeilDivisor X} (a : Γ(X, ⊤))
    {f : X.functionField} (hf : IsDivisorSection D f) :
    IsDivisorSection D (a • f) := by
  by_cases ha : a = 0
  · left; simp [ha]
  rcases hf with rfl | hf
  · left; simp
  right
  intro p
  have h := X.ord_le_smul (x := p.1) (U := ⊤) (by trivial) ha f
  have := hf p
  omega

def divisorSections (D : WeilDivisor X) : Submodule Γ(X, ⊤) X.functionField where
  carrier := {f | IsDivisorSection D f}
  zero_mem' := Or.inl rfl
  add_mem' := divisorSection_add
  smul_mem' := divisorSection_smul

section PrincipalDivisor
variable [CompactSpace X]

omit [StalkwiseNormal X] in

theorem finite_primeDivisors_in_closed {Z : Set X} (hZ : IsClosed Z)
    (hproper : Z ≠ Set.univ) :
    {p : PrimeDivisor X | p.1 ∈ Z}.Finite := by
  classical
  let : PartialOrder X := specializationOrder X
  let : AlgebraicGeometry.IsNoetherian X := ⟨⟩
  obtain ⟨S, hSfinite, hSclosed, hSirr, hZS⟩ :=
    TopologicalSpace.NoetherianSpace.exists_finite_set_isClosed_irreducible hZ
  let : Fintype S := hSfinite.fintype
  let g : S → X := fun T => (hSirr T.1 T.2).genericPoint
  have hgen : genericPoint X ∉ Z := by
    intro h
    apply hproper
    exact Set.eq_univ_of_univ_subset
      (((genericPoint_spec X).mem_closed_set_iff hZ).mp h)
  have hfin : (Set.range g).Finite := Set.finite_range g
  apply ((hfin.preimage (f := fun p : PrimeDivisor X => p.1)
    Subtype.val_injective.injOn)).subset
  intro p hp
  obtain ⟨T, hT, hpT⟩ := Set.mem_sUnion.mp (hZS ▸ hp)
  let q := g ⟨T, hT⟩
  have hq : IsGenericPoint q T := (hSirr T hT).isGenericPoint_genericPoint (hSclosed T hT)
  have hqp : p.1 ≤ q := hq.specializes hpT
  have hqg : q < genericPoint X := by
    refine lt_of_le_of_ne (genericPoint_specializes q) ?_
    intro heq
    apply hgen
    rw [← heq, hZS]
    exact Set.mem_sUnion.mpr ⟨T, hT, hq.mem⟩
  have hqone : (1 : ℕ∞) ≤ Order.coheight q := by
    exact le_trans (by simp) (Order.coheight_add_one_le hqg)
  have hpq : p.1 = q := by
    by_contra hne
    have h := Order.coheight_add_one_le (lt_of_le_of_ne hqp hne)
    rw [p.2] at h
    have htwo : (2 : ℕ∞) ≤ 1 := (add_le_add hqone (le_refl (1 : ℕ∞))).trans h
    norm_num at htwo
  exact ⟨⟨T, hT⟩, hpq.symm⟩

omit [StalkwiseNormal X] in

theorem ord_finite_support (f : X.functionField) :
    (Function.support (fun p : PrimeDivisor X => X.ord f p.1)).Finite := by
  by_cases hf : f = 0
  · simp [hf]
  obtain ⟨U, _, f', hU, heq, hunit⟩ := exists_isUnit_germ_eq X f hf
  have hproper : (U : Set X)ᶜ ≠ Set.univ := by
    intro h
    obtain ⟨x⟩ := hU
    have hx : x.1 ∈ (U : Set X)ᶜ := by rw [h]; trivial
    exact hx x.2
  apply (finite_primeDivisors_in_closed U.isOpen.isClosed_compl hproper).subset
  intro p hp hpU
  apply hp
  rw [← heq]
  exact X.ord_of_isUnit hunit hpU

noncomputable def principalWeilDivisor (f : X.functionField) : WeilDivisor X :=
  Finsupp.ofSupportFinite (fun p => X.ord f p.1) (ord_finite_support f)

omit [StalkwiseNormal X] in
@[simp] theorem principalWeilDivisor_apply (f : X.functionField) (p : PrimeDivisor X) :
    principalWeilDivisor f p = X.ord f p.1 := rfl

end PrincipalDivisor

theorem ord_nonnegative_iff_regular (p : PrimeDivisor X) (f : X.functionField)
    (hf : f ≠ 0) :
    0 ≤ X.ord f p.1 ↔ ∃ a : X.presheaf.stalk p.1,
      algebraMap (X.presheaf.stalk p.1) X.functionField a = f := by
  have := dvr_at_prime_divisor p
  rw [X.le_ord_iff p.2 hf]
  change 1 ≤ Ring.ordFrac (X.presheaf.stalk p.1) f ↔ _
  rw [Ring.ordFrac_eq_valuation_inv, one_le_inv₀
    (WithZero.pos_iff_ne_zero.mpr ((map_ne_zero _).mpr hf))]
  constructor
  · intro h
    obtain ⟨n, d, hnd⟩ :=
      IsDedekindDomain.HeightOneSpectrum.exists_primeCompl_mul_eq_of_integer
        (IsDiscreteValuationRing.maximalIdeal (X.presheaf.stalk p.1)) f h
    have hd : IsUnit (d : X.presheaf.stalk p.1) := by
      simpa [IsDiscreteValuationRing.maximalIdeal, IsLocalRing.mem_maximalIdeal,
        mem_nonunits_iff] using d.2
    obtain ⟨u, hu⟩ := hd
    refine ⟨n * ↑(u⁻¹), ?_⟩
    rw [map_mul, ← hnd, ← hu, mul_assoc, ← map_mul]
    simp
  · rintro ⟨a, rfl⟩
    exact (IsDiscreteValuationRing.maximalIdeal (X.presheaf.stalk p.1)).valuation_le_one a

omit [IsLocallyNoetherian X] in

theorem integrallyClosed_affine {U : X.Opens} (hU : IsAffineOpen U) [Nonempty U] :
    IsIntegrallyClosed Γ(X, U) := by
  let (x : PrimeSpectrum Γ(X, U)) : Algebra Γ(X, U) (X.presheaf.stalk (hU.fromSpec x)) :=
    TopCat.Presheaf.algebra_section_stalk X.presheaf ⟨hU.fromSpec x, (hU.isoSpec.inv x).2⟩
  have (P : Ideal Γ(X, U)) [hP : P.IsPrime] :
      IsLocalization.AtPrime (X.presheaf.stalk (hU.fromSpec ⟨P, hP⟩)) P :=
    hU.isLocalization_stalk' ⟨P, hP⟩ (hU.isoSpec.inv _).2
  exact IsIntegrallyClosed.of_isLocalization_maximal
    (fun P hP => X.presheaf.stalk (hU.fromSpec ⟨P, hP.isPrime⟩))
    (fun _ _ => inferInstance)

theorem affine_regular_iff_nonnegative_orders {U : X.Opens} (hU : IsAffineOpen U)
    [Nonempty U] (f : X.functionField) :
    (∃ a : Γ(X, U), X.germToFunctionField U a = f) ↔
      (f = 0 ∨ ∀ p : PrimeDivisor X, p.1 ∈ U → 0 ≤ X.ord f p.1) := by
  constructor
  · rintro ⟨a, rfl⟩
    by_cases hf : X.germToFunctionField U a = 0
    · exact Or.inl hf
    right
    intro p hpU
    apply (ord_nonnegative_iff_regular p _ hf).mpr
    exact ⟨X.presheaf.germ U p.1 hpU a,
      X.algebraMap_germ_eq_germToFunctionField hpU a⟩
  · rintro (rfl | h)
    · exact ⟨0, map_zero _⟩
    by_cases hf : f = 0
    · exact ⟨0, by simp [hf]⟩
    have := integrallyClosed_affine hU
    have := IsLocallyNoetherian.component_noetherian ⟨U, hU⟩
    have := functionField_isFractionRing_of_isAffineOpen X U hU
    change ∃ a : Γ(X, U), algebraMap Γ(X, U) X.functionField a = f
    apply (normal_domain_height_one_criterion (R := Γ(X, U)) f).mpr
    intro P hP hPh
    let y : PrimeSpectrum Γ(X, U) := ⟨P, hP⟩
    let x : U := ⟨hU.fromSpec y, (hU.isoSpec.inv y).2⟩
    let : Algebra Γ(X, U) (X.presheaf.stalk x.1) :=
      TopCat.Presheaf.algebra_section_stalk X.presheaf x
    have : IsLocalization.AtPrime (X.presheaf.stalk x.1) P :=
      hU.isLocalization_stalk' y x.2
    have := functionField_isScalarTower X U x
    have hx : Order.coheight x.1 = 1 := by
      have heq := IsLocalization.AtPrime.ringKrullDim_eq_height P
        (X.presheaf.stalk x.1)
      rw [ringKrullDim_stalk_eq_coheight, hPh] at heq
      exact_mod_cast heq
    obtain ⟨a, ha⟩ := (ord_nonnegative_iff_regular ⟨x.1, hx⟩ f hf).mp
      (h ⟨x.1, hx⟩ x.2)
    obtain ⟨⟨n, d⟩, hnd⟩ := IsLocalization.surj P.primeCompl a
    refine ⟨d, d.2, n, ?_⟩
    have hd : algebraMap (X.presheaf.stalk x.1) X.functionField
        (algebraMap Γ(X, U) (X.presheaf.stalk x.1) d) =
        algebraMap Γ(X, U) X.functionField d :=
      (IsScalarTower.algebraMap_apply Γ(X, U) (X.presheaf.stalk x.1) X.functionField d).symm
    have hn : algebraMap (X.presheaf.stalk x.1) X.functionField
        (algebraMap Γ(X, U) (X.presheaf.stalk x.1) n) =
        algebraMap Γ(X, U) X.functionField n :=
      (IsScalarTower.algebraMap_apply Γ(X, U) (X.presheaf.stalk x.1) X.functionField n).symm
    have hmap := congrArg (algebraMap (X.presheaf.stalk x.1) X.functionField) hnd
    rw [map_mul, ha, hd, hn] at hmap
    exact (mul_comm _ _).trans hmap

omit [IsLocallyNoetherian X] [StalkwiseNormal X] in
lemma germToFunctionField_restrict {U V : X.Opens} [Nonempty U] [Nonempty V]
    (i : U ⟶ V) (a : Γ(X, V)) :
    X.germToFunctionField U (X.presheaf.map i.op a) = X.germToFunctionField V a := by
  exact ConcreteCategory.congr_hom (X.presheaf.germ_res i (genericPoint X)
    (((genericPoint_spec X).mem_open_set_iff U.isOpen).mpr (by simpa using ‹Nonempty U›))) a

theorem global_regular_iff_nonnegative_orders (f : X.functionField) :
    (∃ a : Γ(X, ⊤), X.germToFunctionField ⊤ a = f) ↔
      (f = 0 ∨ ∀ p : PrimeDivisor X, 0 ≤ X.ord f p.1) := by
  classical
  constructor
  · rintro ⟨a, rfl⟩
    by_cases hf : X.germToFunctionField ⊤ a = 0
    · exact Or.inl hf
    right
    intro p
    apply (ord_nonnegative_iff_regular p _ hf).mpr
    exact ⟨X.presheaf.germ ⊤ p.1 (by trivial) a,
      X.algebraMap_germ_eq_germToFunctionField (U := ⊤) (by trivial) a⟩
  · rintro (rfl | h)
    · exact ⟨0, map_zero _⟩
    let U : X → X.Opens := fun x => (X.affineCover.f (X.affineCover.idx x)).opensRange
    have hxU (x : X) : x ∈ U x := X.affineCover.covers x
    have hU (x : X) : IsAffineOpen (U x) := isAffineOpen_opensRange _
    let (x : X) : Nonempty (U x) := ⟨⟨x, hxU x⟩⟩
    have hgU (x : X) : genericPoint X ∈ U x :=
      ((genericPoint_spec X).mem_open_set_iff (U x).isOpen).mpr
        (by simpa using (inferInstance : Nonempty (U x)))
    have hlocal (x : X) : ∃ a : Γ(X, U x), X.germToFunctionField (U x) a = f :=
      (affine_regular_iff_nonnegative_orders (hU x) f).mpr (Or.inr fun p _ => h p)
    choose sf hsf using hlocal
    have hcompatible : TopCat.Presheaf.IsCompatible X.presheaf U sf := by
      intro x y
      let : Nonempty (U x ⊓ U y : X.Opens) := ⟨⟨genericPoint X, hgU x, hgU y⟩⟩
      apply X.germToFunctionField_injective (U x ⊓ U y)
      rw [germToFunctionField_restrict, germToFunctionField_restrict, hsf, hsf]
    have hcover : (⊤ : X.Opens) ≤ ⨆ x, U x := by
      intro x _
      exact TopologicalSpace.Opens.mem_iSup.mpr ⟨x, hxU x⟩
    obtain ⟨a, ha, _⟩ := X.sheaf.existsUnique_gluing' U ⊤
      (fun _ => homOfLE le_top) hcover sf hcompatible
    refine ⟨a, ?_⟩
    let x : X := genericPoint X
    have hax : X.presheaf.map (homOfLE (le_top : U x ≤ ⊤)).op a = sf x := ha x
    rw [← germToFunctionField_restrict (U := U x) (homOfLE le_top) a, hax, hsf x]

omit [StalkwiseNormal X] in
lemma order_one (x : X) : X.ord (1 : X.functionField) x = 0 := by
  have h := X.ord_mul (x := x) (f := (1 : X.functionField)) (g := 1) one_ne_zero one_ne_zero
  simp only [mul_one] at h
  omega

omit [StalkwiseNormal X] in
lemma order_inv (f : X.functionField) (hf : f ≠ 0) (x : X) :
    X.ord f⁻¹ x = - X.ord f x := by
  have h := X.ord_mul (x := x) hf (inv_ne_zero hf)
  rw [mul_inv_cancel₀ hf, order_one] at h
  omega

def IsCartierDivisor (D : WeilDivisor X) : Prop :=
  ∀ x : X, ∃ U : X.Opens, IsAffineOpen U ∧ x ∈ U ∧
    ∃ f : X.functionField, f ≠ 0 ∧ ∀ p : PrimeDivisor X,
      p.1 ∈ U → D p = X.ord f p.1

omit [StalkwiseNormal X] in
lemma isCartierDivisor_zero : IsCartierDivisor (0 : WeilDivisor X) := by
  intro x
  let U : X.Opens := (X.affineCover.f (X.affineCover.idx x)).opensRange
  refine ⟨U, isAffineOpen_opensRange _, X.affineCover.covers x, 1, one_ne_zero, ?_⟩
  intro p _
  simp [order_one]

omit [StalkwiseNormal X] in
lemma isCartierDivisor_neg {D : WeilDivisor X} (hD : IsCartierDivisor D) :
    IsCartierDivisor (-D) := by
  intro x
  obtain ⟨U, hU, hx, f, hf, heq⟩ := hD x
  refine ⟨U, hU, hx, f⁻¹, inv_ne_zero hf, ?_⟩
  intro p hp
  simp only [Finsupp.neg_apply, heq p hp, order_inv f hf]

omit [StalkwiseNormal X] in
lemma isCartierDivisor_add {D E : WeilDivisor X}
    (hD : IsCartierDivisor D) (hE : IsCartierDivisor E) :
    IsCartierDivisor (D + E) := by
  intro x
  obtain ⟨U, hU, hxU, f, hf, hDf⟩ := hD x
  obtain ⟨V, hV, hxV, g, hg, hEg⟩ := hE x
  obtain ⟨W, hW, hxW, hWU⟩ := exists_isAffineOpen_mem_and_subset
    (show x ∈ U ⊓ V from ⟨hxU, hxV⟩)
  refine ⟨W, hW, hxW, f * g, mul_ne_zero hf hg, ?_⟩
  intro p hp
  simp only [Finsupp.add_apply, hDf p (hWU hp).1, hEg p (hWU hp).2, X.ord_mul hf hg]

def cartierDivisors : AddSubgroup (WeilDivisor X) where
  carrier := {D | IsCartierDivisor D}
  zero_mem' := isCartierDivisor_zero
  add_mem' := isCartierDivisor_add
  neg_mem' := isCartierDivisor_neg

def IsDivisorSectionOn (D : WeilDivisor X) (U : X.Opens) (f : X.functionField) : Prop :=
  f = 0 ∨ ∀ p : PrimeDivisor X, p.1 ∈ U → 0 ≤ X.ord f p.1 + D p

theorem cartier_local_section_iff {D : WeilDivisor X} {U : X.Opens}
    (hU : IsAffineOpen U) [Nonempty U] {g : X.functionField} (hg : g ≠ 0)
    (heq : ∀ p : PrimeDivisor X, p.1 ∈ U → D p = X.ord g p.1)
    (f : X.functionField) :
    IsDivisorSectionOn D U f ↔ ∃ a : Γ(X, U), X.germToFunctionField U a = f * g := by
  rw [affine_regular_iff_nonnegative_orders hU]
  by_cases hf : f = 0
  · simp [IsDivisorSectionOn, hf]
  simp only [IsDivisorSectionOn, hf, false_or, mul_ne_zero hf hg,
    X.ord_mul hf hg]
  constructor <;> intro h p hp
  · simpa only [heq p hp] using h p hp
  · simpa only [heq p hp] using h p hp

omit [StalkwiseNormal X] in

theorem divisorSection_mul {D E : WeilDivisor X} {f g : X.functionField}
    (hf : IsDivisorSection D f) (hg : IsDivisorSection E g) :
    IsDivisorSection (D + E) (f * g) := by
  rcases hf with rfl | hf
  · exact Or.inl (zero_mul _)
  rcases hg with rfl | hg
  · exact Or.inl (mul_zero _)
  by_cases hf0 : f = 0
  · exact Or.inl (by simp [hf0])
  by_cases hg0 : g = 0
  · exact Or.inl (by simp [hg0])
  right
  intro p
  rw [X.ord_mul hf0 hg0, Finsupp.add_apply]
  have := hf p
  have := hg p
  omega

theorem divisorSections_mono {D E : WeilDivisor X} (hDE : D ≤ E) :
    divisorSections D ≤ divisorSections E := by
  intro f hf
  rcases hf with rfl | hf
  · exact Or.inl rfl
  right
  intro p
  have := hf p
  have := hDE p
  omega

end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory

namespace NumericalDimensionOne
section DominantScalars
variable {X Y : Scheme} [IsIntegral X] [IsIntegral Y]

lemma dominant_genericPoint (f : X ⟶ Y) [IsDominant f] :
    f (genericPoint X) = genericPoint Y := by
  apply ((genericPoint_spec Y).eq _).symm
  simpa only [Set.image_univ, f.denseRange.closure_range] using
    (genericPoint_spec X).image f.continuous

noncomputable def dominantFunctionFieldMap (f : X ⟶ Y) [IsDominant f] :
    Y.functionField →+* X.functionField :=
  ((Y.presheaf.stalkCongr
    (Inseparable.of_eq (dominant_genericPoint f).symm)).hom ≫
      f.stalkMap (genericPoint X)).hom

lemma dominantFunctionFieldMap_algebraMap (f : X ⟶ Y) [IsDominant f]
    (x : X) (r : Y.presheaf.stalk (f x)) :
    dominantFunctionFieldMap f (algebraMap (Y.presheaf.stalk (f x)) Y.functionField r) =
      algebraMap (X.presheaf.stalk x) X.functionField (f.stalkMap x r) := by
  change f.stalkMap (genericPoint X)
    (Y.presheaf.stalkSpecializes
      (Inseparable.of_eq (dominant_genericPoint f).symm).specializes'
      (Y.presheaf.stalkSpecializes ((genericPoint_spec Y).specializes trivial) r)) =
      X.presheaf.stalkSpecializes _ (f.stalkMap x r)
  rw [Y.presheaf.stalkSpecializes_comp_apply]
  exact f.stalkSpecializes_stalkMap_apply (genericPoint X) x
    ((genericPoint_spec X).specializes (Set.mem_univ x)) r

end DominantScalars

section CartierPullbackDefinition
variable {X Y : Scheme} [IsIntegral X] [IsIntegral Y]
    [IsLocallyNoetherian X] [IsLocallyNoetherian Y]

def IsCartierPullback (f : X ⟶ Y) [IsDominant f]
    (D : WeilDivisor Y) (E : WeilDivisor X) : Prop :=
  ∀ x : X, ∃ U : Y.Opens, IsAffineOpen U ∧ f x ∈ U ∧
    ∃ g : Y.functionField, g ≠ 0 ∧
      (∀ p : PrimeDivisor Y, p.1 ∈ U → D p = Y.ord g p.1) ∧
      (∀ p : PrimeDivisor X, f p.1 ∈ U → E p = X.ord (dominantFunctionFieldMap f g) p.1)

end CartierPullbackDefinition
end NumericalDimensionOne

end OAI