File size: 25,221 Bytes
2fca478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Geometry.PinnedDistances.CutGeometry

namespace OAI

universe u1 u2 u3 u4 u5 u6 u7 u8 u9 u10

section
noncomputable section
open NumberField
open scoped BigOperators
noncomputable section
open MeasureTheory
open scoped Topology
noncomputable section
open MeasureTheory
open scoped Topology ENNReal
noncomputable section
open MeasureTheory NumberField
open scoped BigOperators
noncomputable section
open scoped BigOperators
open MeasureTheory
noncomputable section
open Filter
open scoped Topology
noncomputable section
open scoped BigOperators
noncomputable section
open Filter
open scoped Topology
noncomputable section
open MeasureTheory
open scoped BigOperators
noncomputable section
noncomputable section
open scoped BigOperators

namespace WeakPinned.Hierarchy.Depth
open MeasureTheory Cuts
open scoped BigOperators
variable {P : Type u1} [Fintype P] [DecidableEq P]
variable (d : Depth P)

lemma cutLength_bound (f : Finset P → ℝ) (hfu : f Finset.univ=0)
    (hfs : ∀ x, f {x}=0) (C : Finset P) :
    |d.cutLength f C|≤2*d.radius*|f C| := by
  rw [← Real.norm_eq_abs,cutLength]
  apply (norm_integral_le_integral_norm _).trans
  exact integral_bound_supported d.radius_nonneg (d.integrable_cutAt f hfu hfs C).norm
    (d.cutAt_bound f C) (fun r hr => by rw [d.cutAt_vanish f hfu hfs C r hr,norm_zero])

lemma sum_cutLength_univ (f : Finset P → ℝ) (g : Finset P → ℝ) :
    (∑ C ∈ d.allCuts, d.cutLength f C*g C)=∑ C, d.cutLength f C*g C := by
  exact Finset.sum_subset (Finset.subset_univ _) (fun C _ hC => by rw [d.cutLength_outside f C hC,zero_mul])

lemma cutLength_mul (f g : Finset P → ℝ) (C : Finset P) :
    d.cutLength (fun B => f B*g B) C=d.cutLength f C*g C := by
  unfold cutLength
  rw [← integral_mul_const]
  congr 1
  funext r
  simp only [cutAt,ite_mul,zero_mul]

lemma cutLength_difference_le (f g : Finset P → ℝ)
    (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0)
    (hgu : g Finset.univ=0) (hgs : ∀ x, g {x}=0) (C : Finset P) :
    |d.cutLength f C-d.cutLength g C|≤d.cutLength (fun B => |f B-g B|) C := by
  rw [cutLength,cutLength,← integral_sub (d.integrable_cutAt f hfu hfs C) (d.integrable_cutAt g hgu hgs C)]
  rw [← Real.norm_eq_abs]
  apply (norm_integral_le_integral_norm _).trans_eq
  congr 1
  funext r
  simp only [cutAt,Real.norm_eq_abs]
  split_ifs <;> simp

lemma distance_cut_difference (f g : Finset P → ℝ)
    (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0)
    (hgu : g Finset.univ=0) (hgs : ∀ x, g {x}=0) (x y : P) :
    |distance d.allCuts (d.cutLength f) x y-distance d.allCuts (d.cutLength g) x y|≤
      ∑ C ∈ d.allCuts, d.cutLength (fun B => |f B-g B|) C := by
  unfold distance
  rw [← Finset.sum_sub_distrib]
  apply (Finset.abs_sum_le_sum_abs _ _).trans
  apply Finset.sum_le_sum
  intro C hC
  rw [← sub_mul,abs_mul,abs_abs]
  have hi : |ind C x-ind C y|≤1 := by unfold ind; split_ifs <;> norm_num
  have h1 := mul_le_mul_of_nonneg_right (d.cutLength_difference_le f g hfu hfs hgu hgs C) (abs_nonneg (ind C x-ind C y))
  exact h1.trans (by simpa using mul_le_mul_of_nonneg_left hi (d.cutLength_nonneg _ (fun B => abs_nonneg _) C))

lemma root_cut_difference (f g : Finset P → ℝ)
    (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0)
    (hgu : g Finset.univ=0) (hgs : ∀ x, g {x}=0) (y : P) :
    |rootDistance d.allCuts (d.cutLength f) y-rootDistance d.allCuts (d.cutLength g) y|≤
      ∑ C ∈ d.allCuts, d.cutLength (fun B => |f B-g B|) C := by
  unfold rootDistance
  rw [← Finset.sum_sub_distrib]
  apply (Finset.abs_sum_le_sum_abs _ _).trans
  apply Finset.sum_le_sum
  intro C hC
  rw [← sub_mul,abs_mul]
  have hi : |ind C y|≤1 := by unfold ind; split_ifs <;> norm_num
  have h1 := mul_le_mul_of_nonneg_right (d.cutLength_difference_le f g hfu hfs hgu hgs C) (abs_nonneg (ind C y))
  exact h1.trans (by simpa using mul_le_mul_of_nonneg_left hi (d.cutLength_nonneg _ (fun B => abs_nonneg _) C))

lemma cost_cut_formula (f : Finset P → ℝ) (p a : P → ℝ) :
    cost d.allCuts (d.cutLength f) p a =
      ∑ C ∈ d.allCuts, d.cutLength (fun B => f B*|Cuts.mass p B-Cuts.mass a B|) C := by
  simp only [cost,d.cutLength_mul]

variable {Ω : Type u2} [MeasurableSpace Ω] (D : Ω → Depth P)
    (hD : ∀ x y, Measurable (fun ω => (D ω).value x y))
include hD
lemma measurable_cutLength_param (f : Finset P → ℝ) (C : Finset P) :
    Measurable (fun ω => (D ω).cutLength f C) := by
  have hm : Measurable (fun p : Ω×ℝ => (D p.1).cutAt f C p.2) :=
    (measurable_of_finite (fun s : Finset (Finset P) => if C ∈ s then f C else 0)).comp
    (measurable_cells_param (P:=P) (Ω:=Ω×ℝ) (fun p : Ω×ℝ => D p.1)
      (fun x y => (hD x y).comp measurable_fst) Prod.snd measurable_snd)
  exact hm.stronglyMeasurable.integral_prod_right.measurable

lemma integrable_cutLength_param {μ : Measure Ω}
    (hi : ∀ x y, Integrable (fun ω => (D ω).value x y) μ)
    (f : Finset P → ℝ) (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0) (C : Finset P) :
    Integrable (fun ω => (D ω).cutLength f C) μ := by
  apply (((integrable_radius_param D hi).const_mul 2).mul_const |f C|).mono'
    (measurable_cutLength_param D hD f C).aestronglyMeasurable
  exact Filter.Eventually.of_forall (fun ω => (D ω).cutLength_bound f hfu hfs C)

lemma integrable_cutSum_param {μ : Measure Ω}
    (hi : ∀ x y, Integrable (fun ω => (D ω).value x y) μ)
    (f g : Finset P → ℝ) (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0) :
    Integrable (fun ω => ∑ C ∈ (D ω).allCuts, (D ω).cutLength f C*g C) μ := by
  simp only [sum_cutLength_univ]
  exact integrable_finsetSum _ (fun C _ => (integrable_cutLength_param D hD hi f hfu hfs C).mul_const (g C))

lemma integrable_cutDistance_param {μ : Measure Ω}
    (hi : ∀ x y, Integrable (fun ω => (D ω).value x y) μ)
    (f : Finset P → ℝ) (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0) (x y : P) :
    Integrable (fun ω => distance (D ω).allCuts ((D ω).cutLength f) x y) μ :=
  integrable_cutSum_param D hD hi f (fun B => |ind B x-ind B y|) hfu hfs

lemma integrable_rootDistance_param {μ : Measure Ω}
    (hi : ∀ x y, Integrable (fun ω => (D ω).value x y) μ)
    (f : Finset P → ℝ) (hfu : f Finset.univ=0) (hfs : ∀ x, f {x}=0) (y : P) :
    Integrable (fun ω => rootDistance (D ω).allCuts ((D ω).cutLength f) y) μ :=
  integrable_cutSum_param D hD hi f (fun B => ind B y) hfu hfs
end WeakPinned.Hierarchy.Depth

namespace WeakPinned.Cuts
lemma tent_lipschitzWith (δ : ℝ) : LipschitzWith 1 (tent δ) := by
  rw [lipschitzWith_iff_dist_le_mul]
  intro x y
  simpa only [Real.dist_eq,NNReal.coe_one,one_mul] using tent_lipschitz δ x y
end WeakPinned.Cuts

namespace WeakPinned.Global
open Hierarchy MeasureTheory Variance Cuts
open scoped BigOperators
variable {P : Type u3} [Fintype P] [DecidableEq P]
variable {K : Type u4} [Field K] [NumberField K]
variable (Z₁ Z₂ : P → K) (h₁ : Function.Injective Z₁) (h₂ : Function.Injective Z₂)
include h₁ h₂

lemma arch_cross_defect (v : NumberField.InfinitePlace K) (x y z : P)
    (hxy : x≠y) (hxz : x≠z) (hyz : y≠z) (t : K)
    (hy : (Z₁ x-Z₁ y)*(Z₂ x-Z₂ y)=t) (hz : (Z₁ x-Z₁ z)*(Z₂ x-Z₂ z)=t) :
    (fun ω => (arch Z₁ v ω).value y z-(arch Z₁ v ω).value x z-
      ((arch Z₂ v ω).value y z+(Real.log (v t)-Real.log (v (Z₁ x-Z₁ y))))) =ᵐ[Grid.fullLaw]
    (fun ω => Grid.error ω (v.embedding (Z₁ y)) (v.embedding (Z₁ z))-
      Grid.error ω (v.embedding (Z₁ x)) (v.embedding (Z₁ z))-
      Grid.error ω (v.embedding (Z₂ y)) (v.embedding (Z₂ z))) := by
  have he := Arithmetic.raw_cross_fiber Z₁ Z₂ h₁ h₂ v.val x y z hxy hxz hyz t hy hz
  filter_upwards [arch_value_ae Z₁ h₁ v hyz,arch_value_ae Z₁ h₁ v hxz,arch_value_ae Z₂ h₂ v hyz] with ω ha hb hc
  rw [ha,hb,hc]
  simp only [Grid.error,← map_sub,NumberField.InfinitePlace.norm_embedding_eq]
  change -Real.log (v (Z₂ y-Z₂ z)) = -Real.log (v (Z₁ y-Z₁ z)) -
    -Real.log (v (Z₁ x-Z₁ z)) - Real.log (v t) - -Real.log (v (Z₁ x-Z₁ y)) at he
  linarith

lemma arch_cross_defect_mean (v : NumberField.InfinitePlace K) (x y z : P)
    (hxy : x≠y) (hxz : x≠z) (hyz : y≠z) (t : K)
    (hy : (Z₁ x-Z₁ y)*(Z₂ x-Z₂ y)=t) (hz : (Z₁ x-Z₁ z)*(Z₂ x-Z₂ z)=t) :
    (∫ ω, |(arch Z₁ v ω).value y z-(arch Z₁ v ω).value x z-
      ((arch Z₂ v ω).value y z+(Real.log (v t)-Real.log (v (Z₁ x-Z₁ y))))| ∂Grid.fullLaw)≤12 := by
  have hEq := arch_cross_defect Z₁ Z₂ h₁ h₂ v x y z hxy hxz hyz t hy hz
  have heq := integral_congr_ae (hEq.fun_comp (abs : ℝ → ℝ))
  dsimp only [Function.comp_def] at heq
  rw [heq]
  have ha := Grid.error_integrable_full (embedding_ne Z₁ h₁ v hyz)
  have hb := Grid.error_integrable_full (embedding_ne Z₁ h₁ v hxz)
  have hc := Grid.error_integrable_full (embedding_ne Z₂ h₂ v hyz)
  have hh := integral_mono ((ha.sub hb).sub hc).abs ((ha.abs.add hb.abs).add hc.abs) (fun ω =>
    (abs_sub _ _).trans (add_le_add (abs_sub _ _) le_rfl))
  dsimp only [Pi.sub_apply,Pi.add_apply] at hh
  have hAdd := integral_add (ha.abs.add hb.abs) hc.abs
  have hAdd2 := integral_add ha.abs hb.abs
  dsimp only [Pi.add_apply] at hAdd hAdd2
  rw [hAdd,hAdd2] at hh
  have h1 := Grid.mean_abs_error_full (embedding_ne Z₁ h₁ v hyz)
  have h2 := Grid.mean_abs_error_full (embedding_ne Z₁ h₁ v hxz)
  have h3 := Grid.mean_abs_error_full (embedding_ne Z₂ h₂ v hyz)
  linarith

/-- Actual local cut projection under an arithmetic fiber probability. The
atom at y is retained and the source has zero mass. -/
lemma arch_fiber_cut (v : NumberField.InfinitePlace K) (p : P → ℝ)
    (hp0 : ∀ z, 0≤p z) (hp : ∑ z, p z=1) (x y : P) (hpx : p x=0) (hpy : p y≠0)
    (t : K) (ht : ∀ z, p z≠0 → (Z₁ x-Z₁ z)*(Z₂ x-Z₂ z)=t)
    (δ : ℝ) (hδ : 0≤δ) (hps : ∀ z, p z≤δ) :
    (∫ ω, |distance (arch Z₁ v ω).allCuts ((arch Z₁ v ω).cutLength (fun C => tent δ (mass p C))) x y-
      rootDistance (arch Z₂ v ω).allCuts ((arch Z₂ v ω).cutLength (fun C => tent δ (mass p C))) y| ∂Grid.fullLaw)≤12 := by
  have hxy : x≠y := fun hh => hpy (hh ▸ hpx)
  let f : Finset P → ℝ := fun C => tent δ (mass p C)
  have hfu : f Finset.univ=0 := by simp only [f,mass,hp]; exact tent_zero_right (by linarith)
  have hfs (z) : f {z}=0 := by simp only [f,mass,Finset.sum_singleton]; exact tent_zero_left (hps z)
  have hl := Depth.integrable_cutDistance_param (arch Z₁ v) (arch_measurable Z₁ h₁ v) (arch_integrable Z₁ h₁ v) f hfu hfs x y
  have hr := Depth.integrable_rootDistance_param (arch Z₂ v) (arch_measurable Z₂ h₂ v) (arch_integrable Z₂ h₂ v) f hfu hfs y
  let c := Real.log (v t)-Real.log (v (Z₁ x-Z₁ y))
  let E : Grid.FullSample → ℝ := fun ω => ∑ z, Tail.offTwo p x y z*
    |(arch Z₁ v ω).value y z-(arch Z₁ v ω).value x z-((arch Z₂ v ω).value y z+c)|
  have hEi (z : P) : Integrable (fun ω => Tail.offTwo p x y z*
    |(arch Z₁ v ω).value y z-(arch Z₁ v ω).value x z-((arch Z₂ v ω).value y z+c)|) Grid.fullLaw :=
    ((((arch_integrable Z₁ h₁ v y z).sub (arch_integrable Z₁ h₁ v x z)).sub
      ((arch_integrable Z₂ h₂ v y z).add (integrable_const c))).abs.const_mul _)
  have hi : Integrable E Grid.fullLaw := integrable_finsetSum _ (fun z _ => hEi z)
  have hh := integral_mono (hl.sub hr).abs hi (fun ω => by
    simpa only [f,E,Pi.sub_apply,NNReal.coe_one,one_mul] using Tail.fiber_cut_error
      (arch Z₁ v ω) (arch Z₂ v ω) p hp0 hp (tent δ) 1 (tent_lipschitzWith δ)
      (tent_zero_right (by linarith)) (fun z => tent_zero_left (hps z)) (tent_symm δ) x y hpx hxy c)
  apply hh.trans
  unfold E
  rw [integral_finsetSum Finset.univ (fun z _ => hEi z)]
  simp only [integral_const_mul]
  apply le_trans (Finset.sum_le_sum (fun z _ => show Tail.offTwo p x y z* _≤12*p z from ?_))
    (by rw [← Finset.mul_sum,hp,mul_one])
  by_cases hzx : z=x
  · subst z; simp [Tail.offTwo,hpx]
  by_cases hzy : z=y
  · subst z; simp only [Tail.offTwo,or_true,reduceIte,zero_mul]; exact mul_nonneg (by norm_num) (hp0 y)
  by_cases hpz : p z=0
  · simp [Tail.offTwo,hzx,hzy,hpz]
  · simp only [Tail.offTwo,hzx,hzy,or_self,reduceIte]
    have he := arch_cross_defect_mean Z₁ Z₂ h₁ h₂ v x y z hxy (Ne.symm hzx) (Ne.symm hzy) t (ht y hpy) (ht z hpz)
    dsimp [c]
    simpa [mul_comm] using mul_le_mul_of_nonneg_left he (hp0 z)

end WeakPinned.Global
noncomputable section

namespace WeakPinned.Global
open Hierarchy MeasureTheory Variance Cuts
open scoped BigOperators
variable {P : Type u5} [Fintype P] [DecidableEq P]
variable {K : Type u6} [Field K] [NumberField K]
variable (Z₁ Z₂ : P → K) (h₁ : Function.Injective Z₁) (h₂ : Function.Injective Z₂)
include h₁ h₂

lemma finite_fiber_cut (v : NumberField.FinitePlace K) (p : P → ℝ)
    (hp0 : ∀ z, 0≤p z) (hp : ∑ z, p z=1) (x y : P) (hpx : p x=0) (hpy : p y≠0)
    (t : K) (ht : ∀ z, p z≠0 → (Z₁ x-Z₁ z)*(Z₂ x-Z₂ z)=t)
    (δ : ℝ) (hδ : 0≤δ) (hps : ∀ z, p z≤δ) :
    |distance (finite Z₁ h₁ v).allCuts ((finite Z₁ h₁ v).cutLength (fun C => tent δ (mass p C))) x y-
      rootDistance (finite Z₂ h₂ v).allCuts ((finite Z₂ h₂ v).cutLength (fun C => tent δ (mass p C))) y|≤0 := by
  have hxy : x≠y := fun hh => hpy (hh ▸ hpx)
  let c := Real.log (v t)-Real.log (v (Z₁ x-Z₁ y))
  have hh := Tail.fiber_cut_error (finite Z₁ h₁ v) (finite Z₂ h₂ v) p hp0 hp (tent δ) 1
    (tent_lipschitzWith δ) (tent_zero_right (by linarith)) (fun z => tent_zero_left (hps z))
    (tent_symm δ) x y hpx hxy c
  simp only [NNReal.coe_one,one_mul] at hh
  apply hh.trans
  apply le_of_eq
  apply Finset.sum_eq_zero
  intro z _
  by_cases hxz : x=z
  · subst z; simp [Tail.offTwo]
  by_cases hyz : y=z
  · subst z; simp [Tail.offTwo]
  by_cases hpz : p z=0
  · simp [Tail.offTwo,hpz]
  · have he := Arithmetic.raw_cross_fiber Z₁ Z₂ h₁ h₂ v.val x y z hxy hxz hyz t (ht y hpy) (ht z hpz)
    change -Real.log (v (Z₂ y-Z₂ z)) = -Real.log (v (Z₁ y-Z₁ z)) -
      -Real.log (v (Z₁ x-Z₁ z)) - Real.log (v t) - -Real.log (v (Z₁ x-Z₁ y)) at he
    suffices (finite Z₁ h₁ v).value y z-(finite Z₁ h₁ v).value x z-
      ((finite Z₂ h₂ v).value y z+c)=0 by rw [this,abs_zero,mul_zero]
    simp only [finite,ite_eq_right hyz,ite_eq_right hxz,c]
    linarith

omit [NumberField K] in
lemma integrable_arch_cut_error (v : NumberField.InfinitePlace K) (p : P → ℝ)
    (δ : ℝ) (hpu : tent δ (mass p Finset.univ)=0) (hps : ∀ x, tent δ (mass p {x})=0)
    (x y : P) :
    Integrable (fun ω => |distance (arch Z₁ v ω).allCuts ((arch Z₁ v ω).cutLength (fun C => tent δ (mass p C))) x y-
      rootDistance (arch Z₂ v ω).allCuts ((arch Z₂ v ω).cutLength (fun C => tent δ (mass p C))) y|) Grid.fullLaw := by
  exact ((Depth.integrable_cutDistance_param (arch Z₁ v) (arch_measurable Z₁ h₁ v)
    (arch_integrable Z₁ h₁ v) _ hpu hps x y).sub
    (Depth.integrable_rootDistance_param (arch Z₂ v) (arch_measurable Z₂ h₂ v)
      (arch_integrable Z₂ h₂ v) _ hpu hps y)).abs

variable {I : Type u7} [Fintype I]
omit Z₁ Z₂ h₁ h₂ in
def fiberCutDiscrepancy (w : I → ℝ) (p : I → P → ℝ) (src : I → P) (δ : ℝ) (d d' : Depth P) : ℝ :=
  ∑ i, w i*∑ y, p i y*|distance d.allCuts (d.cutLength (fun C => tent δ (mass (p i) C))) (src i) y-
    rootDistance d'.allCuts (d'.cutLength (fun C => tent δ (mass (p i) C))) y|

omit [NumberField K] in
lemma fiber_cut_discrepancy_integrable (w : I → ℝ) (p : I → P → ℝ) (src : I → P)
    (hp : ∀ i, ∑ z, p i z=1) (δ : ℝ) (hδ : 0≤δ) (hps : ∀ i z, p i z≤δ) :
    AverageIntegrable Z₁ Z₂ (fiberCutDiscrepancy w p src δ) := by
  intro v
  apply integrable_finsetSum
  intro i _
  apply Integrable.const_mul
  apply integrable_finsetSum
  intro y _
  apply Integrable.const_mul
  exact integrable_arch_cut_error Z₁ Z₂ h₁ h₂ v (p i) δ
    (by simp only [mass,hp]; exact tent_zero_right (by linarith))
    (fun z => by simp only [mass,Finset.sum_singleton]; exact tent_zero_left (hps i z)) (src i) y

/-- A bounded projection estimate with exact fiber atoms and no restriction on
    degree, finite places or variation of number fields. -/
lemma arithmetic_fiber_cut (w : I → ℝ) (p : I → P → ℝ) (src : I → P) (t : I → K)
    (hw : ∀ i, 0≤w i) (hW : ∑ i, w i=1) (hp0 : ∀ i z, 0≤p i z)
    (hp : ∀ i, ∑ z, p i z=1) (hpx : ∀ i, p i (src i)=0)
    (ht : ∀ i z, p i z≠0 → (Z₁ (src i)-Z₁ z)*(Z₂ (src i)-Z₂ z)=t i)
    (δ : ℝ) (hδ : 0≤δ) (hps : ∀ i z, p i z≤δ) :
    average Z₁ Z₂ h₁ h₂ (fiberCutDiscrepancy w p src δ)≤12 := by
  classical
  have hi (v : NumberField.InfinitePlace K) (i : I) (y : P) :=
    integrable_arch_cut_error Z₁ Z₂ h₁ h₂ v (p i) δ
      (by simp only [mass,hp]; exact tent_zero_right (by linarith))
      (fun z => by simp only [mass,Finset.sum_singleton]; exact tent_zero_left (hps i z)) (src i) y
  have ha (v : NumberField.InfinitePlace K) :
      (∫ ω, fiberCutDiscrepancy w p src δ (arch Z₁ v ω) (arch Z₂ v ω) ∂Grid.fullLaw)≤12 := by
    unfold fiberCutDiscrepancy
    rw [integral_finsetSum _ (fun i _ => (integrable_finsetSum _ (fun y _ => (hi v i y).const_mul (p i y))).const_mul (w i))]
    simp only [integral_const_mul]
    have he (i : I) : (∫ ω, ∑ y, p i y*|distance (arch Z₁ v ω).allCuts
        ((arch Z₁ v ω).cutLength (fun C => tent δ (mass (p i) C))) (src i) y-
        rootDistance (arch Z₂ v ω).allCuts ((arch Z₂ v ω).cutLength (fun C => tent δ (mass (p i) C))) y| ∂Grid.fullLaw)≤12 := by
      rw [integral_finsetSum _ (fun y _ => (hi v i y).const_mul (p i y))]
      simp only [integral_const_mul]
      calc
        _ ≤ ∑ y, p i y*12 := Finset.sum_le_sum (fun y _ => by
          by_cases hy : p i y=0
          · simp [hy]
          · exact mul_le_mul_of_nonneg_left (arch_fiber_cut Z₁ Z₂ h₁ h₂ v (p i) (hp0 i) (hp i)
              (src i) y (hpx i) hy (t i) (ht i) δ hδ (hps i)) (hp0 i y))
        _ = 12 := by rw [← Finset.sum_mul,hp,one_mul]
    exact (Finset.sum_le_sum (fun i _ => mul_le_mul_of_nonneg_left (he i) (hw i))).trans_eq
      (by rw [← Finset.sum_mul,hW,one_mul])
  have hv (v : NumberField.FinitePlace K) :
      fiberCutDiscrepancy w p src δ (finite Z₁ h₁ v) (finite Z₂ h₂ v)≤0 := by
    apply Finset.sum_nonpos
    intro i _
    apply mul_nonpos_of_nonneg_of_nonpos (hw i)
    apply Finset.sum_nonpos
    intro y _
    by_cases hy : p i y=0
    · simp [hy]
    · exact mul_nonpos_of_nonneg_of_nonpos (hp0 i y) (finite_fiber_cut Z₁ Z₂ h₁ h₂ v
        (p i) (hp0 i) (hp i) (src i) y (hpx i) hy (t i) (ht i) δ hδ (hps i))
  change placeAverage (placeUnion Z₁ Z₂ h₁ h₂) _ _≤12
  have hh := placeAverage_mono (placeUnion Z₁ Z₂ h₁ h₂) ha (fun v _ => hv v)
  rwa [placeAverage_arch_const] at hh
end WeakPinned.Global
noncomputable section
namespace WeakPinned.Cuts
open MeasureTheory Hierarchy Variance
open scoped BigOperators
variable {P : Type u8} [Fintype P] [DecidableEq P]

lemma mass_complement (p : P → ℝ) (hp : ∑ x, p x=1) (C : Finset P) : mass p Cᶜ=1-mass p C := by
  have hh := Finset.sum_compl_add_sum C p
  rw [hp] at hh
  exact eq_sub_of_add_eq hh

omit [Fintype P] [DecidableEq P] in
lemma mass_small (p : P → ℝ) (δ : ℝ) (hδ : 0≤δ) (hp : ∀ x, p x≤δ)
    (C : Finset P) (hC : C.card≤1) : mass p C≤δ := by
  have hh := Finset.sum_le_sum (s:=C) (fun x _ => hp x)
  simp only [Finset.sum_const,nsmul_eq_mul] at hh
  have hc : (C.card:ℝ)≤1 := by exact_mod_cast hC
  exact hh.trans (by nlinarith)

omit [Fintype P] [DecidableEq P] in
lemma cutError_small (p a : P → ℝ) (δ : ℝ) (hδ : 0≤δ)
    (hp : ∀ x, p x≤δ) (ha : ∀ x, a x≤δ) (C : Finset P) (hC : C.card≤1) :
    cutError δ (mass p C) (mass a C)=0 := by
  simp only [cutError,tent_zero_left (mass_small p δ hδ hp C hC),
    tent_zero_left (mass_small a δ hδ ha C hC),sub_self,abs_zero,zero_mul,add_zero]

lemma cutError_complement (p a : P → ℝ) (hp : ∑ x, p x=1) (ha : ∑ x, a x=1)
    (δ : ℝ) (C : Finset P) :
    cutError δ (mass p Cᶜ) (mass a Cᶜ)=cutError δ (mass p C) (mass a C) := by
  rw [mass_complement p hp,mass_complement a ha,cutError_symm]

def charge (d : Depth P) (δ : ℝ) (p a : P → ℝ) : ℝ :=
  ∑ C ∈ d.allCuts, d.cutLength (fun B => cutError δ (mass p B) (mass a B)) C

lemma charge_nonneg (d : Depth P) (δ : ℝ) (p a : P → ℝ) : 0≤charge d δ p a :=
  Finset.sum_nonneg (fun C _ => d.cutLength_nonneg _ (fun _ => cutError_nonneg _ _ _) C)

lemma charge_occurrence (d : Depth P) (hn : 2≤Fintype.card P) (δ : ℝ) (hδ : 0≤δ)
    (p a : P → ℝ) (hp : ∑ x, p x=1) (ha : ∑ x, a x=1)
    (hps : ∀ x, p x≤δ) (has : ∀ x, a x≤δ) :
    charge d δ p a=weightedIntegral d.mass (fun C => cutError δ (mass p C) (mass a C)) := by
  symm
  exact d.symmetric_integral_cut_sum hn _ (cutError_small p a δ hδ hps has)
    (cutError_complement p a hp ha δ)

omit [DecidableEq P] in
lemma tent_mass_univ (p : P → ℝ) (hp : ∑ x, p x=1) (δ : ℝ) (hδ : 0≤δ) :
    tent δ (mass p Finset.univ)=0 := by
  simp only [mass,hp]
  exact tent_zero_right (by linarith)
omit [Fintype P] [DecidableEq P] in
lemma tent_mass_singleton (p : P → ℝ) (δ : ℝ) (hp : ∀ x, p x≤δ) (x : P) :
    tent δ (mass p {x})=0 := by
  simp only [mass,Finset.sum_singleton]
  exact tent_zero_left (hp x)

end WeakPinned.Cuts
namespace WeakPinned.Hierarchy.Depth
open Cuts MeasureTheory
variable {P : Type u9} [Fintype P] [DecidableEq P]
lemma cutLength_mono (d : Depth P) (f g : Finset P → ℝ)
    (hf : f Finset.univ=0) (hfs : ∀ x, f {x}=0) (hg : g Finset.univ=0) (hgs : ∀ x, g {x}=0)
    (hfg : ∀ C, f C≤g C) (C : Finset P) : d.cutLength f C≤d.cutLength g C := by
  apply integral_mono (d.integrable_cutAt f hf hfs C) (d.integrable_cutAt g hg hgs C)
  intro r
  simp only [cutAt]
  split_ifs
  · exact hfg C
  · rfl
end WeakPinned.Hierarchy.Depth

namespace WeakPinned.Cuts
open MeasureTheory Hierarchy Variance
variable {P : Type u10} [Fintype P] [DecidableEq P]
lemma distance_change_charge (d : Depth P) (δ : ℝ) (hδ : 0≤δ) (p a : P → ℝ)
    (hp : ∑ x, p x=1) (ha : ∑ x, a x=1) (hps : ∀ x, p x≤δ) (has : ∀ x, a x≤δ) (x y : P) :
    |distance d.allCuts (d.cutLength (fun C => tent δ (mass p C))) x y-
      distance d.allCuts (d.cutLength (fun C => tent δ (mass a C))) x y|≤charge d δ p a := by
  apply (d.distance_cut_difference _ _ (tent_mass_univ p hp δ hδ) (tent_mass_singleton p δ hps)
    (tent_mass_univ a ha δ hδ) (tent_mass_singleton a δ has) x y).trans
  apply Finset.sum_le_sum
  intro C _
  apply d.cutLength_mono
  · simp only [tent_mass_univ p hp δ hδ,tent_mass_univ a ha δ hδ,sub_self,abs_zero]
  · intro z; simp only [tent_mass_singleton p δ hps,tent_mass_singleton a δ has,sub_self,abs_zero]
  · simp only [cutError,tent_mass_univ p hp δ hδ,tent_mass_univ a ha δ hδ,sub_self,abs_zero,zero_mul,add_zero]
  · intro z; exact cutError_small p a δ hδ hps has _ (by simp)
  · intro C; exact le_add_of_nonneg_right (mul_nonneg (tent_nonneg _ _) (abs_nonneg _))

lemma root_change_charge (d : Depth P) (δ : ℝ) (hδ : 0≤δ) (p a : P → ℝ)
    (hp : ∑ x, p x=1) (ha : ∑ x, a x=1) (hps : ∀ x, p x≤δ) (has : ∀ x, a x≤δ) (y : P) :
    |rootDistance d.allCuts (d.cutLength (fun C => tent δ (mass p C))) y-
      rootDistance d.allCuts (d.cutLength (fun C => tent δ (mass a C))) y|≤charge d δ p a := by
  apply (d.root_cut_difference _ _ (tent_mass_univ p hp δ hδ) (tent_mass_singleton p δ hps)
    (tent_mass_univ a ha δ hδ) (tent_mass_singleton a δ has) y).trans
  apply Finset.sum_le_sum
  intro C _
  apply d.cutLength_mono
  · simp only [tent_mass_univ p hp δ hδ,tent_mass_univ a ha δ hδ,sub_self,abs_zero]
  · intro z; simp only [tent_mass_singleton p δ hps,tent_mass_singleton a δ has,sub_self,abs_zero]
  · simp only [cutError,tent_mass_univ p hp δ hδ,tent_mass_univ a ha δ hδ,sub_self,abs_zero,zero_mul,add_zero]
  · intro z; exact cutError_small p a δ hδ hps has _ (by simp)
  · intro C; exact le_add_of_nonneg_right (mul_nonneg (tent_nonneg _ _) (abs_nonneg _))

lemma cost_le_charge (d : Depth P) (δ : ℝ) (hδ : 0≤δ) (p a : P → ℝ)
    (hp : ∑ x, p x=1) (ha : ∑ x, a x=1) (hps : ∀ x, p x≤δ) (has : ∀ x, a x≤δ) :
    cost d.allCuts (d.cutLength (fun C => tent δ (mass a C))) p a≤charge d δ p a := by
  rw [d.cost_cut_formula]
  apply Finset.sum_le_sum
  intro C _
  apply d.cutLength_mono
  · simp only [tent_mass_univ a ha δ hδ,zero_mul]
  · intro z; simp only [tent_mass_singleton a δ has,zero_mul]
  · simp only [cutError,tent_mass_univ p hp δ hδ,tent_mass_univ a ha δ hδ,sub_self,abs_zero,zero_mul,add_zero]
  · intro z; exact cutError_small p a δ hδ hps has _ (by simp)
  · intro C; exact le_add_of_nonneg_left (abs_nonneg _)
end WeakPinned.Cuts
noncomputable section

end
end
end
end
end
end
end
end
end
end
end
end
end
end
end

end OAI