File size: 28,615 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import OAI.Dynamics.SmoothObstruction.DensityBounds

namespace OAI

section

open MeasureTheory Filter Topology TopologicalSpace Set Finset
open scoped ENNReal NNReal BoundedContinuousFunction
open Finset MeasureTheory Filter Topology
open scoped BigOperators
open Finset Matrix
open scoped Matrix.Norms.L2Operator
open Matrix

namespace SmoothObstruction.GridMargin
open Set MeasureTheory

/-- The deleted street for one of N translated grids of the same mesh Nδ. -/
def street (δ : ℝ) (N : ℕ) (k : Fin N) : Set ℝ :=
  {x | ∃ j : ℤ, |x-δ*((N:ℝ)*j+k.val)|≤δ/4}

lemma street_measurable (δ : ℝ) (N : ℕ) (k : Fin N) : MeasurableSet (street δ N k) := by
  have he : street δ N k=⋃ j : ℤ,{x : ℝ | |x-δ*((N:ℝ)*j+k.val)|≤δ/4} := by
    ext x; simp [street]
  rw [he]
  exact MeasurableSet.iUnion fun _ => measurableSet_le (by fun_prop) measurable_const

lemma centers_lt_eq {a b : ℤ} {δ : ℝ} (hδ : 0<δ)
    (hab : |δ*((a:ℝ)-b)|≤δ/2) : a=b := by
  have hd : |(a:ℝ)-b|<1 := by
    rw [abs_mul,abs_of_pos hδ] at hab
    nlinarith [abs_nonneg ((a:ℝ)-b)]
  have hdi : |a-b|<1 := by exact_mod_cast hd
  have he : a-b=0 := by
    have hh := abs_lt.mp hdi
    omega
  omega

lemma street_disjoint {δ : ℝ} (hδ : 0<δ) (N : ℕ) :
    Pairwise (fun k l : Fin N => Disjoint (street δ N k) (street δ N l)) := by
  intro k l hkl
  apply Set.disjoint_left.mpr
  intro x hx hy
  obtain ⟨j,hj⟩ := hx
  obtain ⟨m,hm⟩ := hy
  have h := (abs_sub_le (δ*((N:ℝ)*j+k.val)) x (δ*((N:ℝ)*m+l.val)))
  rw [abs_sub_comm (δ*((N:ℝ)*j+k.val)) x] at h
  have hab : |δ*((((N:ℤ)*j+k.val : ℤ):ℝ)-(((N:ℤ)*m+l.val : ℤ):ℝ))|≤δ/2 := by
    push_cast
    calc
      _ = |δ*((N:ℝ)*j+k.val)-δ*((N:ℝ)*m+l.val)| := by congr 1; ring
      _ ≤ _ := h.trans (by linarith)
  have he := centers_lt_eq hδ hab
  have hmod := congrArg (fun z : ℤ => z%(N:ℤ)) he
  have hk : (k.val:ℤ)%(N:ℤ)=k.val := Int.emod_eq_of_lt (by omega) (by exact_mod_cast k.isLt)
  have hl : (l.val:ℤ)%(N:ℤ)=l.val := Int.emod_eq_of_lt (by omega) (by exact_mod_cast l.isLt)
  simp only [Int.add_emod,Int.mul_emod_right,zero_add,hk,hl] at hmod
  exact hkl (Fin.ext (by exact_mod_cast hmod))

/-- A translated regular grid with a quantitative margin for an arbitrary
probability law; no replacement of the law by a conditioned one. -/
theorem exists_small_street (μ : Measure ℝ) [IsProbabilityMeasure μ]
    {δ : ℝ} (hδ : 0<δ) {N : ℕ} (hN : 0<N) :
    ∃ k : Fin N,μ.real (street δ N k)≤(N:ℝ)⁻¹ := by
  let : Nonempty (Fin N) := ⟨⟨0,hN⟩⟩
  have hs : (∑ k : Fin N,μ.real (street δ N k))≤1 := by
    have h := sum_measureReal_le_measureReal_univ (μ:=μ) (s:=Finset.univ)
      (fun k _ => street_measurable δ N k)
      (fun k _ l _ hkl => street_disjoint hδ N hkl)
    simpa using h
  by_contra hh
  push Not at hh
  have hl := Finset.sum_lt_sum_of_nonempty (s:=Finset.univ) Finset.univ_nonempty
    (fun k _ => hh k)
  have hn : (N:ℝ)≠0 := by exact_mod_cast hN.ne'
  have hv : (∑ _k : Fin N,(N:ℝ)⁻¹)=1 := by simp [hn]
  rw [hv] at hl
  linarith

/-- Within a retained scalar cell, a perturbation less than the full margin
cannot change the integer cell index. -/
lemma floor_stable {r a x y ε : ℝ} (hr : 0<r)
    (hx : ∀ j : ℤ,ε < |x-(a+r*j)|) (hxy : |y-x|≤ε) :
    ⌊(y-a)/r⌋=⌊(x-a)/r⌋ := by
  let j : ℤ := ⌊(x-a)/r⌋
  have hj₁ := Int.floor_le ((x-a)/r)
  have hj₂ := Int.lt_floor_add_one ((x-a)/r)
  have hlo : a+r*j≤x := by nlinarith [(le_div_iff₀ hr).mp hj₁]
  have hhi : x<a+r*(j+1) := by nlinarith [(div_lt_iff₀ hr).mp hj₂]
  have hleft := hx j
  have hright := hx (j+1)
  rw [abs_of_nonneg (by linarith)] at hleft
  rw [abs_of_nonpos (by push_cast; linarith)] at hright
  have hy := abs_le.mp hxy
  apply Int.floor_eq_iff.mpr
  constructor
  · apply (le_div_iff₀ hr).mpr
    dsimp [j] at *
    nlinarith
  · apply (div_lt_iff₀ hr).mpr
    push_cast at hright
    dsimp [j] at *
    nlinarith
end SmoothObstruction.GridMargin

namespace SmoothObstruction.GridMargin
open Set MeasureTheory
variable {X ι : Type*} [MeasurableSpace X] [Fintype ι]

/-- Simultaneous product-coordinate margins for the original probability,
with no conditioning on a good set. Each coordinate gets one translation. -/
theorem exists_product_margins (μ : Measure X) [IsProbabilityMeasure μ]
    (c : X → ι → ℝ) (hc : ∀ i,Measurable (fun x =>  c x i))
    {δ : ℝ} (hδ : 0 < δ) {N : ℕ} (hN : 0 < N) :
    ∃ k : ι → Fin N,
      μ.real (⋃ i,(fun x =>  c x i) ⁻¹' street δ N (k i)) ≤ (Fintype.card ι:ℝ)/N := by
  have hchoice (i : ι) : ∃ k : Fin N,
      μ.real ((fun x =>  c x i) ⁻¹' street δ N k) ≤ (N:ℝ)⁻¹ := by
    let ν := μ.map (fun x =>  c x i)
    have : IsProbabilityMeasure ν := inferInstance
    obtain ⟨k,hk⟩ := exists_small_street ν hδ hN
    refine ⟨k,?_⟩
    simpa only [ν,Measure.real,Measure.map_apply (hc i) (street_measurable δ N k)] using hk
  choose k hk using hchoice
  refine ⟨k,?_⟩
  calc
    _  ≤  ∑ i,μ.real ((fun x =>  c x i) ⁻¹' street δ N (k i)) := measureReal_iUnion_fintype_le _
    _  ≤  ∑ _i : ι,(N:ℝ)⁻¹ := Finset.sum_le_sum fun i _ =>  hk i
    _ = _ := by simp [div_eq_mul_inv]

/-- The precise regular translated-grid label. -/
noncomputable def gridIndex (δ : ℝ) (N : ℕ) (k : Fin N) (x : ℝ) : ℤ :=
  ⌊(x-δ*k.val)/(δ*N)⌋

lemma measurable_gridIndex (δ : ℝ) (N : ℕ) (k : Fin N) :
    Measurable (gridIndex δ N k) := by unfold gridIndex; fun_prop

lemma gridIndex_stable {δ : ℝ} (hδ : 0 < δ) {N : ℕ} (hN : 0 < N)
    (k : Fin N) {x y : ℝ} (hx : x∉street δ N k) (hxy : |y-x| ≤ δ/4) :
    gridIndex δ N k y=gridIndex δ N k x := by
  apply floor_stable (mul_pos hδ (by exact_mod_cast hN)) _ hxy
  intro j
  have hj : ¬ |x-δ*((N:ℝ)*j+k.val)| ≤ δ/4 := fun h => hx ⟨j,h⟩
  have he : δ*k.val+(δ*N)*j=δ*((N:ℝ)*j+k.val) := by ring
  rw [he]
  exact lt_of_not_ge hj

/-- Bounded integer labels are encoded without enlarging the physical cells. -/
def boundedLabel (m : ℕ) (j : ℤ) : Fin (2*m+1) :=
  ⟨(max 0 (min (2*m : ℤ) (j+m))).toNat,by omega⟩

lemma boundedLabel_inj {m : ℕ} {i j : ℤ}
    (hi : -(m:ℤ) ≤ i  ∧  i ≤ m) (hj : -(m:ℤ) ≤ j  ∧  j ≤ m)
    (he : boundedLabel m i=boundedLabel m j) : i=j := by
  have hv := congrArg Fin.val he
  simp only [boundedLabel] at hv
  omega

lemma floor_bounds {x : ℝ} {m : ℕ} (hx : |x| ≤ (m:ℝ)) :
    -(m:ℤ) ≤ ⌊x⌋  ∧  ⌊x⌋ ≤ m := by
  constructor
  · apply Int.le_floor.mpr
    push_cast
    exact (abs_le.mp hx).1
  · exact_mod_cast (Int.floor_le x).trans (abs_le.mp hx).2

/-- Arbitrary bounded coordinates give a genuine finite measurable partition,
with exactly this finite alphabet, rather than an assumed cardinal bound. -/
noncomputable def productLabel (m : ℕ) (δ : ℝ) (N : ℕ) (k : ι → Fin N)
    (c : X → ι → ℝ) (x : X) : ι → Fin (2*m+1) :=
  fun i =>  boundedLabel m (gridIndex δ N (k i) (c x i))

omit [Fintype ι] in
lemma measurable_productLabel (m : ℕ) (δ : ℝ) (N : ℕ) (k : ι → Fin N)
    (c : X → ι → ℝ) (hc : ∀ i,Measurable (fun x =>  c x i)) :
    Measurable (productLabel m δ N k c) := by
  apply Measurable.of_eval
  intro i
  exact (measurable_of_countable (boundedLabel m)).comp ((measurable_gridIndex δ N (k i)).comp (hc i))

omit [MeasurableSpace X] [Fintype ι] in
lemma productLabel_stable (m : ℕ) {δ : ℝ} (hδ : 0 < δ) {N : ℕ} (hN : 0 < N)
    (k : ι → Fin N) (c : X → ι → ℝ) {x y : X}
    (hx : x∉⋃ i,(fun x =>  c x i) ⁻¹' street δ N (k i))
    (hxy : ∀ i,|c y i-c x i| ≤ δ/4) :
    productLabel m δ N k c y=productLabel m δ N k c x := by
  funext i
  apply congrArg (boundedLabel m)
  apply gridIndex_stable hδ hN _ _ (hxy i)
  exact fun h =>  hx (mem_iUnion.mpr ⟨i,h⟩)

lemma same_cell_coord {r a x y : ℝ} (hr : 0 < r)
    (he : ⌊(x-a)/r⌋=⌊(y-a)/r⌋) : |y-x| < r := by
  have hx₁ := Int.floor_le ((x-a)/r)
  have hx₂ := Int.lt_floor_add_one ((x-a)/r)
  have hy₁ := Int.floor_le ((y-a)/r)
  have hy₂ := Int.lt_floor_add_one ((y-a)/r)
  rw [←he] at hy₁ hy₂
  apply abs_lt.mpr
  constructor  <;>  nlinarith [(le_div_iff₀ hr).mp hx₁,(le_div_iff₀ hr).mp hy₁,
    (div_lt_iff₀ hr).mp hx₂,(div_lt_iff₀ hr).mp hy₂]

omit [MeasurableSpace X] [Fintype ι] in
lemma productLabel_diameter (m : ℕ) {δ : ℝ} (hδ : 0 < δ) {N : ℕ} (hN : 0 < N)
    (k : ι → Fin N) (c : X → ι → ℝ) {x y : X}
    (hx : ∀ i,|(c x i-δ*(k i).val)/(δ*N)| ≤ m)
    (hy : ∀ i,|(c y i-δ*(k i).val)/(δ*N)| ≤ m)
    (he : productLabel m δ N k c x=productLabel m δ N k c y) :
    ∀ i,|c y i-c x i| < δ*N := by
  intro i
  apply same_cell_coord (mul_pos hδ (by exact_mod_cast hN))
  apply boundedLabel_inj (floor_bounds (hx i)) (floor_bounds (hy i))
  exact congrFun he i

lemma productLabel_card (m : ℕ) :
    Nat.card (ι → Fin (2*m+1))=(2*m+1)^Fintype.card ι := by
  classical
  simp [Nat.card_eq_fintype_card]
end SmoothObstruction.GridMargin

namespace SmoothObstruction.GridMargin
open Set MeasureTheory

/-- Each finite original measure admits the same translated-street estimate,
scaled by its original total mass. This is used on partition fibres without
changing the final pair law. -/
theorem exists_small_street_finite (μ : Measure ℝ) [IsFiniteMeasure μ]
    {δ : ℝ} (hδ : 0<δ) {N : ℕ} (hN : 0<N) :
    ∃ k : Fin N,μ.real (street δ N k)≤μ.real univ/(N:ℝ) := by
  let : Nonempty (Fin N) := ⟨⟨0,hN⟩⟩
  have hs : (∑ k : Fin N,μ.real (street δ N k))≤μ.real univ :=
    sum_measureReal_le_measureReal_univ (μ:=μ) (s:=Finset.univ)
      (fun k _ => street_measurable δ N k)
      (fun k _ l _ hkl => street_disjoint hδ N hkl)
  by_contra hh
  push Not at hh
  have hl := Finset.sum_lt_sum_of_nonempty (s:=Finset.univ) Finset.univ_nonempty
    (fun k _ => hh k)
  have hn : (N:ℝ)≠0 := by exact_mod_cast hN.ne'
  have hv : (∑ _k : Fin N,μ.real univ/(N:ℝ))=μ.real univ := by
    simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul]
    field_simp
  rw [hv] at hl
  linarith

variable {X ι C : Type*} [MeasurableSpace X] [Fintype ι]

/-- Simultaneous margins for a finite, not necessarily probability, law. -/
theorem exists_product_margins_finite (μ : Measure X) [IsFiniteMeasure μ]
    (c : X → ι → ℝ) (hc : ∀ i,Measurable (fun x => c x i))
    {δ : ℝ} (hδ : 0<δ) {N : ℕ} (hN : 0<N) :
    ∃ k : ι → Fin N,
      μ.real (⋃ i,(fun x => c x i) ⁻¹' street δ N (k i)) ≤
        (Fintype.card ι:ℝ)*μ.real univ/N := by
  have hchoice (i : ι) : ∃ k : Fin N,
      μ.real ((fun x => c x i) ⁻¹' street δ N k) ≤ μ.real univ/(N:ℝ) := by
    obtain ⟨k,hk⟩ := exists_small_street_finite (μ.map (fun x => c x i)) hδ hN
    refine ⟨k,?_⟩
    simpa only [Measure.real,Measure.map_apply (hc i) (street_measurable δ N k),
      Measure.map_apply (hc i) MeasurableSet.univ,preimage_univ] using hk
  choose k hk using hchoice
  refine ⟨k,?_⟩
  calc
    _ ≤ ∑ i,μ.real ((fun x => c x i) ⁻¹' street δ N (k i)) := measureReal_iUnion_fintype_le _
    _ ≤ ∑ _i : ι,μ.real univ/(N:ℝ) := Finset.sum_le_sum fun i _ => hk i
    _ = _ := by simp [mul_div_assoc]

variable [Fintype C] [MeasurableSpace C] [MeasurableSingletonClass C]

/-- Independently translated grids in each fibre of a finite measurable
partition retain the same original total-mass budget. The number of fibres
does not multiply the loss. -/
theorem fibrewise_margins (μ : Measure X) [IsProbabilityMeasure μ]
    (P : X → C) (hP : Measurable P) (c : C → X → ι → ℝ)
    (hc : ∀ a i,Measurable (fun x => c a x i))
    {δ : ℝ} (hδ : 0<δ) {N : ℕ} (hN : 0<N) :
    ∃ k : C → ι → Fin N,
      μ.real (⋃ a,(P ⁻¹' {a}) ∩ ⋃ i,(fun x => c a x i) ⁻¹' street δ N (k a i)) ≤
        (Fintype.card ι:ℝ)/N := by
  have hchoice (a : C) := exists_product_margins_finite (μ.restrict (P ⁻¹' {a}))
    (c a) (hc a) hδ hN
  choose k hk using hchoice
  let D (a : C) := ⋃ i,(fun x => c a x i) ⁻¹' street δ N (k a i)
  have hD (a : C) : MeasurableSet (D a) :=
    MeasurableSet.iUnion (fun i => (hc a i) (street_measurable δ N _))
  have hQ (a : C) : MeasurableSet (P ⁻¹' {a}) := hP (measurableSet_singleton a)
  have hs : (∑ a,μ.real (P ⁻¹' {a}))=1 := by
    have hd : Pairwise (fun i j : C => Disjoint (P ⁻¹' {i}) (P ⁻¹' {j})) := by
      intro i j hij
      exact (Set.disjoint_singleton.mpr hij).preimage P
    rw [←measureReal_iUnion_fintype (μ:=μ) hd hQ]
    have he : (⋃ a,P ⁻¹' {a})=univ := by ext x; simp
    rw [he]
    simp
  refine ⟨k,?_⟩
  calc
    _ ≤ ∑ a,μ.real ((P ⁻¹' {a})∩D a) := measureReal_iUnion_fintype_le _
    _ ≤ ∑ a,(Fintype.card ι:ℝ)*μ.real (P ⁻¹' {a})/N := by
      apply Finset.sum_le_sum
      intro a _
      have h := hk a
      rw [Measure.real,Measure.restrict_apply (hD a),Measure.real,
        Measure.restrict_apply MeasurableSet.univ,univ_inter] at h
      simpa only [Measure.real,inter_comm] using h
    _ = (Fintype.card ι:ℝ)/N := by
      rw [←Finset.sum_div,←Finset.mul_sum,hs,mul_one]
end SmoothObstruction.GridMargin

namespace SmoothObstruction.GridRefinement
open Set MeasureTheory
open SmoothObstruction.GridMargin
variable {X C ι : Type*} [MeasurableSpace X] [Fintype C]
  [MeasurableSpace C] [MeasurableSingletonClass C] [Fintype ι] [DecidableEq ι]

noncomputable def label (P : X → C) (c : C → X → ι → ℝ)
    (m : ℕ) (δ : ℝ) (N : ℕ) (k : C → ι → Fin N) (x : X) :
    C × (ι → Fin (2*m+1)) :=
  (P x,productLabel m δ N (k (P x)) (c (P x)) x)

omit [Fintype ι] [DecidableEq ι] in
lemma measurable_label (P : X → C) (hP : Measurable P)
    (c : C → X → ι → ℝ) (hc : ∀ a i,Measurable (fun x => c a x i))
    (m : ℕ) (δ : ℝ) (N : ℕ) (k : C → ι → Fin N) :
    Measurable (label P c m δ N k) := by
  have h : Measurable (fun p : C × X => productLabel m δ N (k p.1) (c p.1) p.2) :=
    measurable_from_prod_countable_right (fun a => measurable_productLabel m δ N (k a) (c a) (hc a))
  change Measurable (fun x => (P x,productLabel m δ N (k (P x)) (c (P x)) x))
  have hg : Measurable (fun x : X => (P x,x)) := hP.prodMk measurable_id
  have hq := h.comp hg
  exact hP.prodMk hq

/-- A genuine finite coordinate-grid refinement, with original-measure loss
independent of the number of base fibres. Stability is quantitative and the
cardinality is the actual finite alphabet cardinality. -/
theorem exists_refinement (μ : Measure X) [IsProbabilityMeasure μ]
    (P : X → C) (hP : Measurable P) (c : C → X → ι → ℝ)
    (hc : ∀ a i,Measurable (fun x => c a x i))
    (m : ℕ) {δ : ℝ} (hδ : 0<δ) {N : ℕ} (hN : 0<N) :
    ∃ (k : C → ι → Fin N) (W : Set X),
      MeasurableSet W ∧ μ.real W≤(Fintype.card ι:ℝ)/N ∧
      Measurable (label P c m δ N k) ∧
      (∀ x y,x∉W → P y=P x →
        (∀ i,|c (P x) y i-c (P x) x i|≤δ/4) →
        label P c m δ N k y=label P c m δ N k x) ∧
      (∀ x y,label P c m δ N k x=label P c m δ N k y →
        (∀ i,|((c (P x) x i-δ*(k (P x) i).val)/(δ*N))|≤(m:ℝ)) →
        (∀ i,|((c (P x) y i-δ*(k (P x) i).val)/(δ*N))|≤(m:ℝ)) →
        ∀ i,gridIndex δ N (k (P x) i) (c (P x) x i)=
          gridIndex δ N (k (P x) i) (c (P x) y i)) ∧
      Fintype.card (C × (ι → Fin (2*m+1)))=
        Fintype.card C*(2*m+1)^Fintype.card ι := by
  classical
  obtain ⟨k,hk⟩ := fibrewise_margins μ P hP c hc hδ hN
  let W := ⋃ a,(P ⁻¹' {a}) ∩ ⋃ i,(fun x => c a x i) ⁻¹' street δ N (k a i)
  have hW : MeasurableSet W := MeasurableSet.iUnion (fun a =>
    (hP (measurableSet_singleton a)).inter
      (MeasurableSet.iUnion (fun i => (hc a i) (street_measurable δ N _))))
  refine ⟨k,W,hW,hk,measurable_label P hP c hc m δ N k,?_,?_,?_⟩
  · intro x y hx he hdist
    have hx' : x∉⋃ i,(fun u => c (P x) u i) ⁻¹' street δ N (k (P x) i) := by
      intro hh
      exact hx (mem_iUnion.mpr ⟨P x,by exact ⟨rfl,hh⟩⟩)
    dsimp [label]
    rw [he]
    exact Prod.ext rfl (productLabel_stable m hδ hN _ _ hx' hdist)
  · intro x y he hx hy i
    have hPxy := congrArg Prod.fst he
    have he' := congrArg (fun p : C × (ι → Fin (2*m+1)) => p.2 i) he
    change P x=P y at hPxy
    change boundedLabel m (gridIndex δ N (k (P x) i) (c (P x) x i))=
      boundedLabel m (gridIndex δ N (k (P y) i) (c (P y) y i)) at he'
    rw [←hPxy] at he'
    exact boundedLabel_inj (floor_bounds (hx i)) (floor_bounds (hy i)) he'
  · simp
end SmoothObstruction.GridRefinement

namespace SmoothObstruction.GridRefinement
open Set MeasureTheory
open SmoothObstruction.GridMargin
variable {X C ι : Type*} [MeasurableSpace X] [Fintype C]
  [MeasurableSpace C] [MeasurableSingletonClass C] [Fintype ι] [DecidableEq ι]

omit [DecidableEq ι] in
/-- The retained product margins survive a complementary coordinate mix.
Each output coordinate may follow either retained input, so this applies
simultaneously to both actual orbit joins. Mass is still the original law. -/
theorem exists_mixing_refinement (μ : Measure X) [IsProbabilityMeasure μ]
    (P : X → C) (hP : Measurable P) (c : C → X → ι → ℝ)
    (hc : ∀ a i,Measurable (fun x => c a x i))
    (m : ℕ) {δ : ℝ} (hδ : 0 < δ) {N : ℕ} (hN : 0 < N) :
    ∃ (k : C → ι → Fin N) (W : Set X),
      MeasurableSet W ∧ μ.real W ≤ (Fintype.card ι:ℝ)/N ∧
      Measurable (label P c m δ N k) ∧
      (∀ x y z,x∉W → y∉W →
        label P c m δ N k x=label P c m δ N k y → P z=P x →
        (∀ i,|c (P x) z i-c (P x) x i| ≤ δ/4 ∨
          |c (P x) z i-c (P x) y i| ≤ δ/4) →
        label P c m δ N k z=label P c m δ N k x) ∧
      (∀ x y,label P c m δ N k x=label P c m δ N k y →
        (∀ i,|(c (P x) x i-δ*(k (P x) i).val)/(δ*N)| ≤ (m:ℝ)) →
        (∀ i,|(c (P x) y i-δ*(k (P x) i).val)/(δ*N)| ≤ (m:ℝ)) →
        ∀ i,|c (P x) x i-c (P x) y i| < δ*N) := by
  classical
  obtain ⟨k,hk⟩ := fibrewise_margins μ P hP c hc hδ hN
  let W := ⋃ a,(P ⁻¹' {a}) ∩ ⋃ i,(fun x => c a x i) ⁻¹' street δ N (k a i)
  have hW : MeasurableSet W := MeasurableSet.iUnion (fun a =>
    (hP (measurableSet_singleton a)).inter
      (MeasurableSet.iUnion (fun i => (hc a i) (street_measurable δ N _))))
  have hout (x : X) (hx : x∉W) (i : ι) : c (P x) x i∉street δ N (k (P x) i) := by
    intro hi
    exact hx (mem_iUnion.mpr ⟨P x,rfl,mem_iUnion.mpr ⟨i,hi⟩⟩)
  refine ⟨k,W,hW,hk,measurable_label P hP c hc m δ N k,?_,?_⟩
  · intro x y z hx hy he hp hm
    have hxy : P x=P y := congrArg Prod.fst he
    apply Prod.ext hp
    change productLabel m δ N (k (P z)) (c (P z)) z=
      productLabel m δ N (k (P x)) (c (P x)) x
    rw [hp]
    funext i
    rcases hm i with hi | hi
    · exact congrArg (boundedLabel m) (gridIndex_stable hδ hN _ (hout x hx i) hi)
    · have hy' : c (P x) y i∉street δ N (k (P x) i) := by
        simpa only [hxy] using hout y hy i
      have he' := congrArg (fun p : C × (ι → Fin (2*m+1)) => p.2 i) he
      change boundedLabel m (gridIndex δ N (k (P x) i) (c (P x) x i))=
        boundedLabel m (gridIndex δ N (k (P y) i) (c (P y) y i)) at he'
      rw [←hxy] at he'
      exact (congrArg (boundedLabel m) (gridIndex_stable hδ hN _ hy' hi)).trans he'.symm
  · intro x y he hx hy i
    have hxy : P x=P y := congrArg Prod.fst he
    have he' := congrArg (fun p : C × (ι → Fin (2*m+1)) => p.2 i) he
    change boundedLabel m (gridIndex δ N (k (P x) i) (c (P x) x i))=
      boundedLabel m (gridIndex δ N (k (P y) i) (c (P y) y i)) at he'
    rw [←hxy] at he'
    have hidx := boundedLabel_inj (floor_bounds (hx i)) (floor_bounds (hy i)) he'
    exact same_cell_coord (mul_pos hδ (by exact_mod_cast hN)) hidx.symm
end SmoothObstruction.GridRefinement

namespace SmoothObstruction.ProductCells
open Set MeasureTheory
open SmoothObstruction.GridMargin SmoothObstruction.GridRefinement
variable {X C : Type*} [MeasurableSpace X] [Fintype C]
  [MeasurableSpace C] [MeasurableSingletonClass C] {d : ℕ}
local notation "E" => EuclideanSpace ℝ (Fin d)

lemma norm_le_coords {u : E} {h : ℝ} (hh : 0≤h) (hu : ∀ i,|u i|≤h) :
    ‖u‖≤((d:ℝ)+1)*h := by
  have hs : ‖u‖^2≤(d:ℝ)*h^2 := by
    rw [EuclideanSpace.real_norm_sq_eq]
    calc
      _ ≤ ∑ _ : Fin d,h^2 := Finset.sum_le_sum (fun i _ => by
        have hi := hu i
        nlinarith [sq_abs (u i),abs_nonneg (u i)])
      _ = _ := by simp
  have hd : 0≤(d:ℝ) := Nat.cast_nonneg _
  have hp : 0≤((d:ℝ)+1)*h := by positivity
  have hsq : (d:ℝ)*h^2≤(((d:ℝ)+1)*h)^2 := by
    nlinarith [sq_nonneg ((d:ℝ)*h),mul_nonneg hd (sq_nonneg h)]
  nlinarith [norm_nonneg u]

lemma grid_range {δ D H : ℝ} (hδ : 0<δ) (hD : 0≤D) (hH : 0≤H)
    {N : ℕ} (hN : 0<N) (k : Fin N) {v : ℝ} (hv : |v|≤H*D) :
    |(v-δ*k.val)/(δ*N)|≤(⌈H*D/(δ*N)+1⌉₊:ℝ) := by
  have hn : 0<(N:ℝ) := by exact_mod_cast hN
  have hk : (k.val:ℝ)≤N := by exact_mod_cast k.isLt.le
  have hi : 0≤δ*(k.val:ℝ) := by positivity
  have hab : |v-δ*k.val|≤H*D+δ*N := by
    calc
      _ ≤ |v|+|δ*k.val| := abs_sub _ _
      _ ≤ H*D+δ*N := by rw [abs_of_nonneg hi]; nlinarith
  rw [abs_div,abs_of_pos (mul_pos hδ hn)]
  calc
    _ ≤ (H*D+δ*N)/(δ*N) := div_le_div_of_nonneg_right hab (by positivity)
    _ = H*D/(δ*N)+1 := by field_simp
    _ ≤ _ := Nat.le_ceil _

/-- Finite product cells for arbitrary measurable geometric coordinates.
The original probability law loses at most d/N near product streets. The
number of cells is exactly the finite grid alphabet; no rank or orbit code
is counted. Stability is stated in the physical coordinates needed for joins. -/
theorem exists_cells (μ : Measure X) [IsProbabilityMeasure μ]
    (P : X → C) (hP : Measurable P) (φ : C → X → E)
    (hφ : ∀ c,Measurable (φ c)) (A : C → E ≃L[ℝ] E)
    {D H F δ : ℝ} (hD : 0≤D) (hH : 0≤H) (hF : 0≤F) (hδ : 0<δ)
    (hbound : ∀ x,‖φ (P x) x‖≤D)
    (hAi : ∀ c,‖(A c).symm.toContinuousLinearMap‖≤H)
    (hA : ∀ c,‖(A c).toContinuousLinearMap‖≤F)
    {N : ℕ} (hN : 0<N) :
    let m := ⌈H*D/(δ*N)+1⌉₊
    ∃ (L : X → C × (Fin d → Fin (2*m+1))) (W : Set X),
      Measurable L ∧ MeasurableSet W ∧ μ.real W≤(d:ℝ)/N ∧
      (∀ x,(L x).1=P x) ∧
      (∀ x y,L x=L y → ‖φ (P x) x-φ (P x) y‖≤F*((d:ℝ)+1)*(δ*N)) ∧
      (∀ x y z,L x=L y → x∉W → y∉W → P z=P x →
        (∀ i,|((A (P x)).symm (φ (P x) z)) i-
          ((A (P x)).symm (φ (P x) x)) i|≤δ/4 ∨
          |((A (P x)).symm (φ (P x) z)) i-
          ((A (P x)).symm (φ (P x) y)) i|≤δ/4) → L z=L x) ∧
      Fintype.card (C × (Fin d → Fin (2*m+1)))=Fintype.card C*(2*m+1)^d := by
  classical
  dsimp only
  let m := ⌈H*D/(δ*N)+1⌉₊
  let c (a : C) (x : X) (i : Fin d) := ((A a).symm (φ a x)) i
  have hc (a : C) (i : Fin d) : Measurable (fun x => c a x i) :=
    (show Measurable (fun u : E => u i) by fun_prop).comp
      ((A a).symm.continuous.measurable.comp (hφ a))
  obtain ⟨k,W,hW,hm,hL,hstable,hdiam⟩ :=
    exists_mixing_refinement μ P hP c hc m hδ hN
  let L := label P c m δ N k
  have hrange (x : X) (i : Fin d) :
      |(c (P x) x i-δ*(k (P x) i).val)/(δ*N)|≤(m:ℝ) := by
    apply grid_range hδ hD hH hN
    calc
      _ = ‖((A (P x)).symm (φ (P x) x)) i‖ := (Real.norm_eq_abs _).symm
      _ ≤ ‖(A (P x)).symm (φ (P x) x)‖ := PiLp.norm_apply_le _ _
      _ ≤ ‖(A (P x)).symm.toContinuousLinearMap‖*‖φ (P x) x‖ :=
        (A (P x)).symm.toContinuousLinearMap.le_opNorm _
      _ ≤ H*D := mul_le_mul (hAi _) (hbound x) (norm_nonneg _) hH
  refine ⟨L,W,hL,hW,by simpa using hm,fun _ => rfl,?_,?_,by simp⟩
  · intro x y he
    have hxy : P x=P y := congrArg Prod.fst he
    have hh := hdiam x y he (hrange x) (by simpa only [hxy] using hrange y)
    have hb : ‖(A (P x)).symm (φ (P x) x)-(A (P x)).symm (φ (P x) y)‖≤
        ((d:ℝ)+1)*(δ*N) := by
      apply norm_le_coords (by positivity)
      intro i
      exact (hh i).le
    calc
      _ = ‖A (P x) ((A (P x)).symm (φ (P x) x)-(A (P x)).symm (φ (P x) y))‖ := by
        rw [map_sub]; simp
      _ ≤ ‖(A (P x)).toContinuousLinearMap‖*
          ‖(A (P x)).symm (φ (P x) x)-(A (P x)).symm (φ (P x) y)‖ :=
        (A (P x)).toContinuousLinearMap.le_opNorm _
      _ ≤ F*(((d:ℝ)+1)*(δ*N)) := mul_le_mul (hA _) hb (norm_nonneg _) hF
      _ = _ := by ring
  · intro x y z he hx hy hp hh
    exact hstable x y z hx hy he hp hh
end SmoothObstruction.ProductCells

namespace SmoothObstruction.ChartDensity
open Set MeasureTheory
open scoped Manifold ContDiff
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [TopologicalSpace M] [T2Space M] [ChartedSpace H M]
    [MeasurableSpace M] [BorelSpace M]
    {ι : Type*} [Fintype ι]

omit [T2Space M] [MeasurableSpace M] [BorelSpace M] in
/-- A uniform selected-coordinate bound follows from the compact atlas,
without any regularity assumption on the selection itself. -/
theorem selected_coordinate_bound (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source)
    (q : M → ι) (hqK : ∀ x,x∈K (q x)) :
    ∃ D : ℝ,1≤D ∧ ∀ x,‖coordinates I (a (q x)) x‖≤D := by
  classical
  have hex (i : ι) : ∃ R : ℝ,∀ x∈K i,‖(extChartAt I (a i)) x‖≤R := by
    have hc := (hK i).image_of_continuousOn
      ((continuousOn_extChartAt (I:=I) (a i)).mono
        (by simpa only [extChartAt_source] using hKU i))
    obtain ⟨R,hR⟩ := hc.isBounded.exists_norm_le
    exact ⟨R,fun x hx => hR _ ⟨x,hx,rfl⟩⟩
  choose R hR using hex
  let D := 1+∑ i,|R i|
  have hp : 0≤∑ i,|R i| := Finset.sum_nonneg (fun i _ => abs_nonneg _)
  refine ⟨D,by dsimp [D]; linarith,fun x => ?_⟩
  rw [coordinates_eq I _ _ (by simpa only [extChartAt_source] using hKU _ (hqK x))]
  calc
    _ ≤ R (q x) := hR _ _ (hqK x)
    _ ≤ |R (q x)| := le_abs_self _
    _ ≤ ∑ i,|R i| := Finset.single_le_sum (fun i _ => abs_nonneg (R i)) (Finset.mem_univ _)
    _ ≤ D := by dsimp [D]; linarith

omit [T2Space M] in
/-- Genuine finite coarse bins in a compact atlas. Their translated margins
are estimated using the ORIGINAL measure, with no assumptions about its
interaction with the good gauge set. Only the selected starting chart is
part of the finite alphabet. -/
theorem exists_coarse_cells [MeasurableSpace ι] [MeasurableSingletonClass ι]
    (μ : Measure M) [IsProbabilityMeasure μ]
    (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source)
    (q : M → ι) (hq : Measurable q) (hqK : ∀ x,x∈K (q x)) :
    ∃ D : ℝ,1≤D ∧ ∀ {δ : ℝ},0<δ → ∀ {N : ℕ},0<N →
      let m := ⌈D/(δ*N)+1⌉₊
      ∃ (P : M → ι × (Fin d → Fin (2*m+1))) (W : Set M),
        Measurable P ∧ MeasurableSet W ∧ μ.real W≤(d:ℝ)/N ∧
        (∀ x,(P x).1=q x) ∧
        (∀ x y,P x=P y →
          ‖coordinates I (a (q x)) x-coordinates I (a (q x)) y‖≤((d:ℝ)+1)*(δ*N)) ∧
        (∀ x z,x∉W → q z=q x →
          ‖coordinates I (a (q x)) z-coordinates I (a (q x)) x‖≤δ/4 → P z=P x) ∧
        Fintype.card (ι × (Fin d → Fin (2*m+1)))=Fintype.card ι*(2*m+1)^d := by
  obtain ⟨D,hD,hbound⟩ := selected_coordinate_bound I a K hK hKU q hqK
  refine ⟨D,hD,?_⟩
  intro δ hδ N hN
  have hh := ProductCells.exists_cells μ q hq (fun i => coordinates I (a i))
      (fun i => measurable_coordinates I (a i))
      (fun _ => ContinuousLinearEquiv.refl ℝ (EuclideanSpace ℝ (Fin d)))
      (D:=D) (H:=1) (F:=1) (by linarith) (by norm_num) (by norm_num) hδ hbound
      (fun _ => ContinuousLinearMap.norm_id_le) (fun _ => ContinuousLinearMap.norm_id_le) hN
  dsimp only at hh
  rw [one_mul] at hh
  simp only [one_mul] at hh
  obtain ⟨P,W,hP,hW,hm,hqP,hd,hs,hcard⟩ := hh
  refine ⟨P,W,hP,hW,hm,hqP,hd,?_,hcard⟩
  intro x z hx hqz hz
  apply hs x x z rfl hx hx hqz
  intro i
  left
  exact (PiLp.norm_apply_le
    (coordinates I (a (q x)) z-coordinates I (a (q x)) x) i).trans hz
end SmoothObstruction.ChartDensity

end

end OAI