File size: 27,942 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import Mathlib
import OAI.AlgebraicGeometry.NumericalDimension.FactorialDivisors

namespace OAI

/-! Surface Intersection. -/

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne

lemma length_sup_span_eq_ord_quotient {R : Type*} [CommRing R]
    (I : Ideal R) (b : R) :
    Module.length R (R ⧸ (I ⊔ Ideal.span {b})) =
      Ring.ord (R ⧸ I) (Ideal.Quotient.mk I b) := by
  let J : Ideal (R ⧸ I) := (Ideal.span {b}).map (Ideal.Quotient.mk I)
  have ht : IsScalarTower R (R ⧸ I) ((R ⧸ I) ⧸ J) :=
    IsScalarTower.of_algebraMap_eq' rfl
  have he := (DoubleQuot.quotQuotEquivQuotSupₐ R I (Ideal.span {b})).toLinearEquiv.length_eq
  rw [← he, Module.length_eq_of_surjective (R := R ⧸ I) Ideal.Quotient.mk_surjective]
  rw [Ideal.map_span, Set.image_singleton]
  rfl

open AlgebraicGeometry CategoryTheory TopologicalSpace
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X]
  [StalkwiseNormal X] [CompactSpace X]

omit [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X] [CompactSpace X] in

lemma primeDivisor_effective (p : PrimeDivisor X) :
    ∀ q, 0 ≤ (Finsupp.single p (1 : ℤ)) q := by
  intro q
  classical
  simp only [Finsupp.single_apply]
  split_ifs <;> omega

lemma primeCartierStalkIdeal (p : PrimeDivisor X) (z : pointClosure p.1)
    [UniqueFactorizationMonoid (X.presheaf.stalk (pointClosureι p.1 z))]
    (hP : IsCartierDivisor (Finsupp.single p 1)) :
    effectiveCartierStalkIdeal hP (primeDivisor_effective p) (pointClosureι p.1 z) =
      generizationPrime (pointClosure_specializes p.1 z) := by
  classical
  let y := pointClosureι p.1 z
  let h := pointClosure_specializes p.1 z
  have hp := generizationPrime_height h p.2
  obtain ⟨a,ha⟩ := UniqueFactorizationMonoid.isPrincipal_of_height_eq_one hp
  have ha0 : a ≠ 0 := by
    intro he
    exact (generizationPrime h).ne_bot_of_height_eq_one hp
      (by simpa only [he, Ideal.span_singleton_zero] using ha)
  let f := algebraMap (X.presheaf.stalk y) X.functionField a
  have hf : f ≠ 0 := (map_ne_zero_iff _ (IsFractionRing.injective _ _)).mpr ha0
  have he (q : PrimeDivisor X) (hq : q.1 ⤳ y) :
      Finsupp.single p (1 : ℤ) q = X.ord f q.1 := by
    simpa only [Finsupp.single_apply, Subtype.ext_iff] using
      (order_stalk_generator h hq p.2 q.2 a ha).symm
  obtain ⟨U,_hU,hy,heq⟩ := affine_equation_of_generization_orders
    (Finsupp.single p 1) y f he
  let eqn : CartierStalkEquation (Finsupp.single p 1) y :=
    ⟨U,hy,f,hf,heq,a,rfl⟩
  exact (effectiveCartierStalkIdeal_eq hP (primeDivisor_effective p) eqn).trans ha.symm

theorem primeCurveMultiplicity_eq_ord (p : PrimeDivisor X) (z : pointClosure p.1)
    [UniqueFactorizationMonoid (X.presheaf.stalk (pointClosureι p.1 z))]
    (hP : IsCartierDivisor (Finsupp.single p 1))
    {D : WeilDivisor X} (hD : IsCartierDivisor D) (hDeff : ∀ q, 0 ≤ D q)
    (b : CartierStalkEquation D (pointClosureι p.1 z)) :
    effectiveCartierMultiplicity hP hD (primeDivisor_effective p) hDeff
        (pointClosureι p.1 z) =
      Ring.ord ((pointClosure p.1).presheaf.stalk z)
        ((pointClosureι p.1).stalkMap z b.regular) := by
  rw [effectiveCartierMultiplicity, primeCartierStalkIdeal p z hP,
    effectiveCartierStalkIdeal_eq hD hDeff b, length_sup_span_eq_ord_quotient]
  exact (ringOrder_equiv (pointClosure_stalkEquiv p.1 z) _).symm
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X]
  [StalkwiseNormal X] [CompactSpace X]

omit [CompactSpace X] in

lemma primeCurve_equation_ne_zero (p : PrimeDivisor X) (z : pointClosure p.1)
    {D : WeilDivisor X} (hDp : D p = 0)
    (b : CartierStalkEquation D (pointClosureι p.1 z)) :
    (pointClosureι p.1).stalkMap z b.regular ≠ 0 := by
  let h := pointClosure_specializes p.1 z
  let R := X.presheaf.stalk (pointClosureι p.1 z)
  let S := X.presheaf.stalk p.1
  let := stalkGenerizationAlgebra h
  have := functionField_stalkGenerization_tower h
  have : IsDiscreteValuationRing S := dvr_at_prime_divisor p
  let v := (X.presheaf.stalkSpecializes h).hom b.regular
  have hv : algebraMap S X.functionField v = b.function := by
    change algebraMap S X.functionField (algebraMap R S b.regular) = b.function
    rw [← IsScalarTower.algebraMap_apply R S X.functionField, b.regular_eq]
  have ho : X.ord b.function p.1 = 0 := by
    rw [← b.equation p (h.mem_open b.openSet.isOpen b.mem_openSet), hDp]
  have hu : IsUnit v := by
    rw [Ring.isUnit_iff_ordFrac_one_of_isDiscreteValuationRing (K := X.functionField), hv]
    exact (X.ord_eq_iff p.2 b.nonzero).mp ho
  have hn : b.regular ∉ generizationPrime h := IsLocalRing.notMem_maximalIdeal.mpr hu
  intro hb
  apply hn
  rw [← pointClosure_stalk_ker p.1 z]
  exact hb

theorem primeCurveMultiplicity_eq_normalizedBranches
    (sX : X ⟶ Spec (.of ℂ)) [LocallyOfFiniteType sX]
    (p : PrimeDivisor X) (z : pointClosure p.1)
    (hz : IsClosed ({z} : Set (pointClosure p.1)))
    (hdim : Order.coheight z ≤ 1)
    [UniqueFactorizationMonoid (X.presheaf.stalk (pointClosureι p.1 z))]
    (hP : IsCartierDivisor (Finsupp.single p 1))
    {D : WeilDivisor X} (hD : IsCartierDivisor D) (hDeff : ∀ q, 0 ≤ D q)
    (hDp : D p = 0) (b : CartierStalkEquation D (pointClosureι p.1 z)) :
    let a := (pointClosureι p.1).stalkMap z b.regular
    effectiveCartierMultiplicity hP hD (primeDivisor_effective p) hDeff
        (pointClosureι p.1 z) =
      ∑ᶠ q : NormalizedStalkBranches z a,
        Ring.ord (Localization.AtPrime (q.asIdeal.comap (Ideal.Quotient.mk
          (Ideal.span {algebraMap ((pointClosure p.1).presheaf.stalk z)
            (integralClosure ((pointClosure p.1).presheaf.stalk z)
              (pointClosure p.1).functionField) a}))))
          (algebraMap ((pointClosure p.1).presheaf.stalk z) _ a) := by
  dsimp only
  have : IsLocallyNoetherian (pointClosure p.1) :=
    LocallyOfFiniteType.isLocallyNoetherian (pointClosureι p.1)
  apply Eq.trans (primeCurveMultiplicity_eq_ord p z hP hD hDeff b)
  exact localOrder_eq_normalizedBranches (pointClosureι p.1 ≫ sX) z hz hdim
    ((pointClosureι p.1).stalkMap z b.regular) (primeCurve_equation_ne_zero p z hDp b)
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X]
variable (sX : X ⟶ Spec (.of ℂ)) [SmoothOfRelativeDimension 2 sX] [IsProper sX]

noncomputable def surfaceCartierPrimeDegree (D : cartierDivisors (X := X))
    (p : PrimeDivisor X) : ℤ := by
  let C := (pointClosure p.1).fromSpecStalk (genericPoint (pointClosure p.1))
  have : IsIntegral C.normalization := integral_genericNormalization
  have : SmoothOfRelativeDimension 1
      (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) :=
    surface_prime_normalization_smooth sX p.1 p.2
  have : IsProper (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) :=
    surface_prime_normalization_proper sX p.1
  have : IsLocallyNoetherian C.normalization :=
    LocallyOfFiniteType.isLocallyNoetherian (C.fromNormalization ≫ pointClosureι p.1 ≫ sX)
  have : CompactSpace C.normalization :=
    QuasiCompact.compactSpace_of_compactSpace (C.fromNormalization ≫ pointClosureι p.1 ≫ sX)
  exact properCurveDegree (C.fromNormalization ≫ pointClosureι p.1) D

lemma surfaceCartierPrimeDegree_add (D E : cartierDivisors (X := X)) (p : PrimeDivisor X) :
    surfaceCartierPrimeDegree sX (D + E) p =
      surfaceCartierPrimeDegree sX D p + surfaceCartierPrimeDegree sX E p := by
  let C := (pointClosure p.1).fromSpecStalk (genericPoint (pointClosure p.1))
  have : IsIntegral C.normalization := integral_genericNormalization
  have : SmoothOfRelativeDimension 1
      (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) :=
    surface_prime_normalization_smooth sX p.1 p.2
  have : IsProper (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) :=
    surface_prime_normalization_proper sX p.1
  have : IsLocallyNoetherian C.normalization :=
    LocallyOfFiniteType.isLocallyNoetherian (C.fromNormalization ≫ pointClosureι p.1 ≫ sX)
  have : CompactSpace C.normalization :=
    QuasiCompact.compactSpace_of_compactSpace (C.fromNormalization ≫ pointClosureι p.1 ≫ sX)
  exact properCurveDegree_add (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) _ D E
lemma surfaceCartierPrimeDegree_zero (p : PrimeDivisor X) :
    surfaceCartierPrimeDegree sX 0 p = 0 := by
  let C := (pointClosure p.1).fromSpecStalk (genericPoint (pointClosure p.1))
  have : IsIntegral C.normalization := integral_genericNormalization
  have : SmoothOfRelativeDimension 1
      (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) :=
    surface_prime_normalization_smooth sX p.1 p.2
  have : IsProper (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) :=
    surface_prime_normalization_proper sX p.1
  have : IsLocallyNoetherian C.normalization :=
    LocallyOfFiniteType.isLocallyNoetherian (C.fromNormalization ≫ pointClosureι p.1 ≫ sX)
  have : CompactSpace C.normalization :=
    QuasiCompact.compactSpace_of_compactSpace (C.fromNormalization ≫ pointClosureι p.1 ≫ sX)
  exact properCurveDegree_zero (C.fromNormalization ≫ pointClosureι p.1 ≫ sX) _

noncomputable def surfaceIntersection (D E : cartierDivisors (X := X)) : ℤ :=
  D.1.sum fun p a => a * surfaceCartierPrimeDegree sX E p

lemma surfaceIntersection_add_left (D E F : cartierDivisors (X := X)) :
    surfaceIntersection sX (D + E) F =
      surfaceIntersection sX D F + surfaceIntersection sX E F := by
  classical
  exact Finsupp.sum_add_index (fun _ _ => zero_mul _) (fun _ _ a b => add_mul a b _)
lemma surfaceIntersection_add_right (D E F : cartierDivisors (X := X)) :
    surfaceIntersection sX D (E + F) =
      surfaceIntersection sX D E + surfaceIntersection sX D F := by
  classical
  simp only [surfaceIntersection, surfaceCartierPrimeDegree_add, mul_add,
    Finsupp.sum_add]
lemma surfaceIntersection_zero_left (D : cartierDivisors (X := X)) :
    surfaceIntersection sX 0 D = 0 := by
  rfl
lemma surfaceIntersection_zero_right (D : cartierDivisors (X := X)) :
    surfaceIntersection sX D 0 = 0 := by
  simp only [surfaceIntersection, surfaceCartierPrimeDegree_zero, mul_zero,
    Finsupp.sum, Finset.sum_const_zero]
noncomputable def surfaceIntersectionHom :
    cartierDivisors (X := X) →+ cartierDivisors (X := X) →+ ℤ where
  toFun D := { toFun := surfaceIntersection sX D
               map_zero' := surfaceIntersection_zero_right sX D
               map_add' := surfaceIntersection_add_right sX D }
  map_zero' := by ext D; exact surfaceIntersection_zero_left sX D
  map_add' D E := by ext F; exact surfaceIntersection_add_left sX D E F
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
lemma pointClosure_generic {X : Scheme} (p : X) :
    pointClosureι p (genericPoint (pointClosure p)) = p := by
  have hg := (genericPoint_spec (pointClosure p)).image (pointClosureι p).continuous
  rw [Set.image_univ, pointClosure_range, closure_closure] at hg
  exact hg.eq isGenericPoint_closure
lemma prime_point_outside_support {X : Scheme} (D : WeilDivisor X) (p : PrimeDivisor X)
    (hp : D p = 0) : p.1 ∉ divisorSupport D := by
  intro h
  obtain ⟨q,hq,hpq⟩ := Set.mem_iUnion₂.mp h
  have hsp := specializes_iff_mem_closure.mpr hpq
  have he : q = p := Subtype.ext
    (eq_of_specializes_of_equal_finite_coheight hsp q.2 p.2)
  subst q
  exact Finsupp.mem_support_iff.mp hq hp
variable {C Y : Scheme} [IsIntegral C] [IsIntegral Y]
  [IsLocallyNoetherian C] [IsLocallyNoetherian Y] [StalkwiseNormal Y]
  [CompactSpace C] [CompactSpace Y]
lemma finite_support_pulled_orders
    (sC : C ⟶ Spec (.of ℂ)) [SmoothOfRelativeDimension 1 sC]
    (f : C ⟶ Y) (D : cartierDivisors (X := Y))
    (hη : f (genericPoint C) ∉ divisorSupport D.1)
    (b : ∀ p : PrimeDivisor C, CartierStalkEquation D.1 (f p.1)) :
    Function.HasFiniteSupport (fun p : PrimeDivisor C =>
      ((Ring.ord (C.presheaf.stalk p.1) (f.stalkMap p.1 (b p).regular)).toNat : ℤ)) := by
  classical
  obtain ⟨E,hE⟩ := exists_pulledCartierRepresentative f D.1 D.2
  obtain ⟨t,_,ht⟩ := hE.exists_effective_local_orders f hη
  let F := principalWeilDivisor t + E
  have hF : ∀ p : PrimeDivisor C, F p =
      ((Ring.ord (C.presheaf.stalk p.1) (f.stalkMap p.1 (b p).regular)).toNat : ℤ) := by
    intro p
    exact (ht p (b p)).2.trans
      (NumericalDimensionOneCurveAux.scheme_order_of_stalk_element p.1 p.2 _ (ht p (b p)).1)
  exact (funext hF) ▸ F.hasFiniteSupport
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
lemma fiber_composite_stalk_element {C Y X : Scheme} (f : C ⟶ Y) (g : Y ⟶ X)
    (b : ∀ z : Y, X.presheaf.stalk (g z))
    (x : Y) (y : {y : C // f y = x}) :
    fiberStalkMap f x y (g.stalkMap x (b x)) =
      (f ≫ g).stalkMap y.1 (b (f y.1)) := by
  obtain ⟨y,rfl⟩ := y
  rw [fiberStalkMap_self, Scheme.Hom.stalkMap_comp]
  rfl
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X]
variable (sX : X ⟶ Spec (.of ℂ)) [SmoothOfRelativeDimension 2 sX] [IsProper sX]

theorem surfacePrimeDegree_eq_multiplicities
    (D : cartierDivisors (X := X)) (hDeff : ∀ q, 0 ≤ D.1 q)
    (p : PrimeDivisor X) (hDp : D.1 p = 0)
    (hP : IsCartierDivisor (Finsupp.single p 1)) :
    surfaceCartierPrimeDegree sX D p =
      ∑ᶠ z : ClosedCurvePoint (pointClosure p.1),
        ((effectiveCartierMultiplicity hP D.2 (primeDivisor_effective p) hDeff
          (pointClosureι p.1 z.1)).toNat : ℤ) := by
  classical
  let C := pointClosure p.1
  let ι := pointClosureι p.1
  let N := (C.fromSpecStalk (genericPoint C)).normalization
  let ν := (C.fromSpecStalk (genericPoint C)).fromNormalization
  have : IsIntegral N := integral_genericNormalization
  have : IsLocallyNoetherian C := LocallyOfFiniteType.isLocallyNoetherian ι
  have : CompactSpace X := QuasiCompact.compactSpace_of_compactSpace sX
  have : CompactSpace C := QuasiCompact.compactSpace_of_compactSpace ι
  have : AlgebraicGeometry.IsNoetherian C := ⟨⟩
  have : AlgebraicGeometry.IsFinite ν := finite_genericNormalization (ι ≫ sX)
  let sN := ν ≫ ι ≫ sX
  have : SmoothOfRelativeDimension 1 sN := surface_prime_normalization_smooth sX p.1 p.2
  have : IsProper sN := surface_prime_normalization_proper sX p.1
  have : IsLocallyNoetherian N := LocallyOfFiniteType.isLocallyNoetherian sN
  have : CompactSpace N := QuasiCompact.compactSpace_of_compactSpace sN
  let g := ν ≫ ι
  let b (z : C) := Classical.choice (exists_cartierStalkEquation D.2 hDeff (ι z))
  let bN (q : PrimeDivisor N) : CartierStalkEquation D.1 (g q.1) := b (ν q.1)
  have hη : g (genericPoint N) ∉ divisorSupport D.1 := by
    change ι (ν (genericPoint N)) ∉ divisorSupport D.1
    rw [dominant_genericPoint ν]
    rw [pointClosure_generic]
    exact prime_point_outside_support D.1 p hDp
  let w (q : PrimeDivisor N) : ℤ :=
    (Ring.ord (N.presheaf.stalk q.1) (g.stalkMap q.1 (bN q).regular)).toNat
  have hw : Function.HasFiniteSupport w := finite_support_pulled_orders sN g D hη bN
  change properCurveDegree g D = _
  rw [properCurveDegree_eq_local_orders sN g D hη bN]
  change (∑ᶠ q, w q) = _
  rw [← finsum_fiberwise (finiteCurvePrimeImage sN ν) w hw]
  apply finsum_congr
  intro z
  have : UniqueFactorizationMonoid (X.presheaf.stalk (ι z.1)) :=
    factorial_stalk_of_smooth_surface sX (ι z.1)
  rw [primeCurveMultiplicity_eq_ord p z.1 hP D.2 hDeff (b z.1)]
  rw [localOrder_int_eq_normalizationFiber (ι ≫ sX) z.1 z.2
    (surface_pointClosure_coheight sX p.1 p.2 z.1) _
    (primeCurve_equation_ne_zero p z.1 hDp (b z.1))]
  let e := finiteCurvePrimeFiberEquiv sN ν z
  rw [← finsum_comp_equiv e]
  apply finsum_congr
  intro q
  have he := fiber_composite_stalk_element ν ι (fun y => (b y).regular) z.1 (e q)
  exact congrArg (fun a : N.presheaf.stalk (e q).1 =>
    ((Ring.ord (N.presheaf.stalk (e q).1) a).toNat : ℤ)) he.symm
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
lemma finsum_comp_injective_supported {α β M : Type*} [AddCommMonoid M]
    (f : α → β) (hf : Function.Injective f) (w : β → M)
    (hs : Function.support w ⊆ Set.range f) :
    (∑ᶠ x, w (f x)) = ∑ᶠ y, w y := by
  classical
  rw [← finsum_mem_range hf, finsum_mem_def]
  apply finsum_congr
  intro y
  by_cases hy : y ∈ Set.range f
  · exact Set.indicator_of_mem hy _
  · rw [Set.indicator_of_notMem hy]
    exact (not_ne_iff.mp (fun h => hy (hs h))).symm
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X]
  [CompactSpace X]

lemma prime_multiplicity_support (hdim : ∀ x : X, Order.coheight x ≤ 2)
    (D : cartierDivisors (X := X)) (hDeff : ∀ q, 0 ≤ D.1 q)
    (p : PrimeDivisor X) (hDp : D.1 p = 0)
    (hP : IsCartierDivisor (Finsupp.single p 1)) :
    Function.support (fun x : X =>
      ((effectiveCartierMultiplicity hP D.2 (primeDivisor_effective p) hDeff x).toNat : ℤ)) ⊆
        Set.range (fun z : ClosedCurvePoint (pointClosure p.1) => pointClosureι p.1 z.1) := by
  classical
  intro x hx
  have hmem : x ∈ divisorSupport (Finsupp.single p 1) ∩ divisorSupport D.1 := by
    by_contra hn
    exact hx (congrArg (fun n : ℕ∞ => (n.toNat : ℤ))
      (effectiveCartierMultiplicity_eq_zero hP D.2 (primeDivisor_effective p) hDeff hn))
  have hxp : x ∈ closure ({p.1} : Set X) := by
    obtain ⟨q,hq,hxq⟩ := Set.mem_iUnion₂.mp hmem.1
    have hqp : q = p := by simpa only [Finsupp.support_single p one_ne_zero,
      Finset.mem_singleton] using hq
    exact hqp ▸ hxq
  obtain ⟨q,hq,hxq⟩ := Set.mem_iUnion₂.mp hmem.2
  have hpq : p ≠ q := by
    rintro rfl
    exact Finsupp.mem_support_iff.mp hq hDp
  have hc : IsClosed ({x} : Set X) := closed_point_of_distinct_prime_intersection
    hdim p q hpq x hxp hxq
  have hxr : x ∈ Set.range (pointClosureι p.1) := by rwa [pointClosure_range]
  obtain ⟨z,rfl⟩ := hxr
  have hz : IsClosed ({z} : Set (pointClosure p.1)) := by
    have he : (pointClosureι p.1) ⁻¹' {pointClosureι p.1 z} = {z} := by
      ext y
      exact (pointClosureι p.1).isClosedEmbedding.injective.eq_iff
    rw [← he]
    exact hc.preimage (pointClosureι p.1).continuous
  exact ⟨⟨z,hz⟩,rfl⟩
lemma prime_multiplicities_finsum (hdim : ∀ x : X, Order.coheight x ≤ 2)
    (D : cartierDivisors (X := X)) (hDeff : ∀ q, 0 ≤ D.1 q)
    (p : PrimeDivisor X) (hDp : D.1 p = 0)
    (hP : IsCartierDivisor (Finsupp.single p 1)) :
    (∑ᶠ z : ClosedCurvePoint (pointClosure p.1),
      ((effectiveCartierMultiplicity hP D.2 (primeDivisor_effective p) hDeff
        (pointClosureι p.1 z.1)).toNat : ℤ)) =
      ∑ᶠ x : X,
        ((effectiveCartierMultiplicity hP D.2 (primeDivisor_effective p) hDeff x).toNat : ℤ) := by
  apply finsum_comp_injective_supported
    (fun z : ClosedCurvePoint (pointClosure p.1) => pointClosureι p.1 z.1)
    (w := fun x : X =>
      ((effectiveCartierMultiplicity hP D.2 (primeDivisor_effective p) hDeff x).toNat : ℤ))
  · intro z w h
    exact Subtype.ext ((pointClosureι p.1).isClosedEmbedding.injective h)
  · exact prime_multiplicity_support hdim D hDeff p hDp hP
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X]
variable (sX : X ⟶ Spec (.of ℂ)) [SmoothOfRelativeDimension 2 sX] [IsProper sX]

noncomputable def smoothSurfaceWeilCartier :
    WeilDivisor X →+ cartierDivisors (X := X) where
  toFun D := ⟨D, by
    have : CompactSpace X := QuasiCompact.compactSpace_of_compactSpace sX
    exact isCartierDivisor_smooth_surface sX D⟩
  map_zero' := rfl
  map_add' _ _ := rfl
noncomputable def smoothSurfacePrimeCartier (p : PrimeDivisor X) :
    cartierDivisors (X := X) := smoothSurfaceWeilCartier sX (Finsupp.single p 1)

theorem surfacePrimePrime_comm (p q : PrimeDivisor X) :
    surfaceCartierPrimeDegree sX (smoothSurfacePrimeCartier sX q) p =
      surfaceCartierPrimeDegree sX (smoothSurfacePrimeCartier sX p) q := by
  classical
  by_cases hpq : p = q
  · subst q
    rfl
  have : CompactSpace X := QuasiCompact.compactSpace_of_compactSpace sX
  let P := smoothSurfacePrimeCartier sX p
  let Q := smoothSurfacePrimeCartier sX q
  have hP : IsCartierDivisor (Finsupp.single p 1) := P.2
  have hQ : IsCartierDivisor (Finsupp.single q 1) := Q.2
  have hQp : Q.1 p = 0 := Finsupp.single_eq_of_ne hpq
  have hPq : P.1 q = 0 := Finsupp.single_eq_of_ne (Ne.symm hpq)
  have hd := coheight_le_of_smooth_dimension sX 2
  have h1 := (surfacePrimeDegree_eq_multiplicities sX Q
    (primeDivisor_effective q) p hQp hP).trans
    (prime_multiplicities_finsum hd Q (primeDivisor_effective q) p hQp hP)
  have h2 := (surfacePrimeDegree_eq_multiplicities sX P
    (primeDivisor_effective p) q hPq hQ).trans
    (prime_multiplicities_finsum hd P (primeDivisor_effective p) q hPq hQ)
  apply h1.trans
  apply Eq.trans _ h2.symm
  exact finsum_congr (fun x => congrArg (fun n : ℕ∞ => (n.toNat : ℤ))
    (effectiveCartierMultiplicity_comm hP hQ
      (primeDivisor_effective p) (primeDivisor_effective q) x))
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
lemma finsupp_bilinear_comm {α : Type*}
    (B : (α →₀ ℤ) →+ (α →₀ ℤ) →+ ℤ)
    (hs : ∀ p q, B (Finsupp.single p 1) (Finsupp.single q 1) =
      B (Finsupp.single q 1) (Finsupp.single p 1))
    (D E : α →₀ ℤ) : B D E = B E D := by
  classical
  induction D using Finsupp.induction_linear with
  | zero => simp
  | add D F hD hF => simp only [map_add, AddMonoidHom.add_apply, hD, hF]
  | single p n =>
    induction E using Finsupp.induction_linear with
    | zero => simp
    | add E F hE hF => simp only [map_add, AddMonoidHom.add_apply, hE, hF]
    | single q m =>
      have hn : Finsupp.single p n = n • Finsupp.single p (1 : ℤ) := by
        simp only [Finsupp.smul_single, zsmul_eq_mul, mul_one, Int.cast_id]
      have hm : Finsupp.single q m = m • Finsupp.single q (1 : ℤ) := by
        simp only [Finsupp.smul_single, zsmul_eq_mul, mul_one, Int.cast_id]
      rw [hn, hm]
      simp only [map_zsmul, AddMonoidHom.zsmul_apply, zsmul_eq_mul]
      rw [hs p q]
      ring
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {X : Scheme} [IsIntegral X] [IsLocallyNoetherian X] [StalkwiseNormal X]
variable (sX : X ⟶ Spec (.of ℂ)) [SmoothOfRelativeDimension 2 sX] [IsProper sX]
lemma surfaceIntersection_prime_left (p : PrimeDivisor X)
    (D : cartierDivisors (X := X)) :
    surfaceIntersection sX (smoothSurfacePrimeCartier sX p) D =
      surfaceCartierPrimeDegree sX D p := by
  change (Finsupp.single p (1 : ℤ)).sum
    (fun q n => n * surfaceCartierPrimeDegree sX D q) = _
  rw [Finsupp.sum_single_index (by simp only [zero_mul]), one_mul]

theorem surfaceIntersection_comm (D E : cartierDivisors (X := X)) :
    surfaceIntersection sX D E = surfaceIntersection sX E D := by
  let H := smoothSurfaceWeilCartier sX
  let B := ((surfaceIntersectionHom sX).comp H).compl₂ H
  have hs (p q : PrimeDivisor X) :
      B (Finsupp.single p 1) (Finsupp.single q 1) =
        B (Finsupp.single q 1) (Finsupp.single p 1) := by
    change surfaceIntersection sX (smoothSurfacePrimeCartier sX p)
      (smoothSurfacePrimeCartier sX q) =
        surfaceIntersection sX (smoothSurfacePrimeCartier sX q)
          (smoothSurfacePrimeCartier sX p)
    rw [surfaceIntersection_prime_left, surfaceIntersection_prime_left]
    exact surfacePrimePrime_comm sX p q
  exact finsupp_bilinear_comm B hs D.1 E.1
end NumericalDimensionOne

open AlgebraicGeometry CategoryTheory
open scoped TensorProduct nonZeroDivisors
open scoped TensorProduct
open AlgebraicGeometry CategoryTheory TopologicalSpace
open CategoryTheory Opposite AlgebraicGeometry TopologicalSpace

namespace NumericalDimensionOne
open AlgebraicGeometry CategoryTheory
variable {C Y : Scheme} [IsIntegral C] [IsIntegral Y]
  [IsLocallyNoetherian C] [IsLocallyNoetherian Y] [StalkwiseNormal Y]
  [CompactSpace Y]
omit [StalkwiseNormal Y] in
lemma isPulledCartierRepresentative_principal (f : C ⟶ Y)
    (a : Y.functionField) (ha : a ≠ 0) :
    IsPulledCartierRepresentative f (principalWeilDivisor a) 0 := by
  obtain ⟨U,hU,hyU,_⟩ := exists_isAffineOpen_mem_and_subset
    (show f (genericPoint C) ∈ (⊤ : Y.Opens) from trivial)
  refine ⟨U,hU,hyU,a,ha,fun _ _ => rfl,?_⟩
  intro x
  obtain ⟨V,hV,hxV,_⟩ := exists_isAffineOpen_mem_and_subset
    (show f x ∈ (⊤ : Y.Opens) from trivial)
  refine ⟨V,hV,hxV,a,ha,fun _ _ => rfl,1,isUnit_one,?_,?_⟩
  · simp only [map_one, div_self ha]
  · intro p _
    simp only [Finsupp.zero_apply, map_one, order_one]
lemma properCurveDegree_principal [CompactSpace C]
    (sC : C ⟶ Spec (.of ℂ)) [SmoothOfRelativeDimension 1 sC] [IsProper sC]
    (f : C ⟶ Y) (a : Y.functionField) (ha : a ≠ 0)
    (hD : IsCartierDivisor (principalWeilDivisor a)) :
    properCurveDegree f ⟨principalWeilDivisor a,hD⟩ = 0 := by
  rw [properCurveDegree_eq sC f _ 0 (isPulledCartierRepresentative_principal f a ha)]
  rfl
end NumericalDimensionOne

end OAI