File size: 27,379 Bytes
f1f7953
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.Analysis.IrrationalRotation.Model.CircleSamplingStage
import OAI.Analysis.IrrationalRotation.Analysis.FrequencyTower
import OAI.Analysis.IrrationalRotation.Sampling.SamplingHorizon
import OAI.Analysis.IrrationalRotation.Analysis.LaurentNoncancellation
import OAI.Analysis.IrrationalRotation.Analysis.OrbitPartition
import OAI.Analysis.IrrationalRotation.Rotation.CircleParameterBlocks
import OAI.Analysis.IrrationalRotation.Sampling.PhaseRanges

namespace OAI

noncomputable section
open Filter MeasureTheory Set
open scoped BigOperators ENNReal Topology

namespace Invariant233.CircleParameterRecursion
open PrescribedPhaseRealization SamplingHorizon FiniteWindow

private structure State where
  K : ℕ
  G : ℕ
  d : ℕ
  q : ℕ → ℕ
  cost : ℝ
  previousL : ℝ
  previousN : ℝ
  K_pos : 0 < K
  G_pos : 0 < G
  q_zero : q 0 = 1
  q_pos : ∀ i ≤ d, 0 < q i
  q_mono : ∀ i < d, q i < q (i + 1)
  q_div : ∀ i < d, q i ∣ q (i + 1)

private structure Step (θ : ℝ) (height : ℕ → ℕ) (n : ℕ) (s : State) where
  m : ℕ
  Gnext : ℕ
  L : ℝ
  ε : ℝ
  epsilon_pos : 0 < ε
  L_one : 1 ≤ L
  L_previous : s.previousL ≤ L
  gain_one : 1 ≤ Gnext
  gain : L ≤ CircleParameterData.kappa (n + 2) * Real.sqrt (Gnext : ℝ)
  m_K : s.K ≤ m
  m_pow : 2 ^ (n + 1) ≤ m
  m_G : 4 * s.G ≤ m
  m_large : 200 ≤ m
  attenuation_m : L ≤ CircleParameterData.kappa (n + 1) *
    (m : ℝ) ^ (1 / 40 : ℝ)
  gap : (Gnext : ℝ) * (⌈(m : ℝ) ^ ((3 / 4 : ℝ) * m)⌉₊ : ℝ) <
    (m : ℝ) ^ ((4 / 5 : ℝ) * m)
  continuity : (height (n + 2) : ℝ) /
    (⌈(m : ℝ) ^ ((3 / 4 : ℝ) * m)⌉₊ : ℝ) ≤ (1 / 2 : ℝ) ^ (n + 1)
  double : 2 * s.K < ⌈(m : ℝ) ^ ((3 / 4 : ℝ) * m)⌉₊
  attenuation : ((2 * s.K + 1 : ℕ) : ℝ) * 3 * Real.exp (-L / 10000) ≤
    ε / ((n + 1 : ℕ) : ℝ)
  polynomial : ∀ A B : ℤ → ℂ,
    (∀ i, |i| ≤ (s.K : ℤ) → ‖A i‖ ≤ Real.exp (((s.K : ℝ) + 1) * s.cost)) →
    (∀ i, |i| ≤ (s.K : ℤ) → ‖B i‖ ≤ Real.exp (((s.K : ℝ) + 1) * s.cost)) →
    1 / ((n + 1 : ℕ) : ℝ) ≤ ‖A 0‖ → 1 / ((n + 1 : ℕ) : ℝ) ≤ ‖B 0‖ →
    ∃ i : ℕ, i ≤ 2 * s.K ∧ ε ≤ ‖convolution s.K A B i‖
  sampling : CircleSamplingStage θ (n + 1) s.K s.d (s.q s.d) m
    (max s.previousN ((m : ℝ) * s.K))

private theorem step_exists {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n : ℕ) (s : State) : Nonempty (Step θ height n s) := by
  have hn : (0 : ℝ) < ((n + 1 : ℕ) : ℝ) := by positivity
  obtain ⟨ε, hε, hpoly⟩ := uniform_convolution_lower_bound s.K
    (Real.exp (((s.K : ℝ) + 1) * s.cost)) (1 / ((n + 1 : ℕ) : ℝ)) (by positivity)
  obtain ⟨L, hL1, hLprev, hL⟩ := exists_attenuation
    (((2 * s.K + 1 : ℕ) : ℝ) * 3) s.previousL
    (ε / ((n + 1 : ℕ) : ℝ)) (div_pos hε hn)
  have hk : 0 < CircleParameterData.kappa (n + 2) := by
    unfold CircleParameterData.kappa
    positivity
  obtain ⟨G, hG, hgain⟩ := exists_next_gain (CircleParameterData.kappa (n + 2)) L hk
  have hnum := eventually_stage_size (n + 1) (by omega) s.K s.G G L
    (height (n + 2)) s.d
  have hsam := hs (n + 1) s.K s.d (s.q s.d) (by omega) s.K_pos (s.q_pos _ le_rfl)
  obtain ⟨m, hm, ht⟩ := (hnum.and hsam).exists
  obtain ⟨hmK, hmpow, hmG, hmlarge, _hcount, hmL, hgap, hheight, hdouble⟩ := hm
  obtain ⟨t⟩ := ht (max s.previousN ((m : ℝ) * s.K))
  exact ⟨{
    m := m, Gnext := G, L := L, ε := ε
    epsilon_pos := hε, L_one := hL1, L_previous := hLprev
    gain_one := hG, gain := hgain
    m_K := by exact_mod_cast hmK
    m_pow := by exact_mod_cast hmpow
    m_G := by exact_mod_cast hmG
    m_large := hmlarge
    attenuation_m := hmL, gap := hgap, continuity := hheight
    double := by exact_mod_cast hdouble
    attenuation := hL, polynomial := hpoly, sampling := t }⟩

private def chooseStep {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n : ℕ) (s : State) : Step θ height n s :=
  Classical.choice (step_exists hs height n s)

private def nextState {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n : ℕ) (s : State) : State := by
  let t := chooseStep hs height n s
  let q := spliceFrequency s.q t.sampling.q s.d
  have hb := t.sampling.q_zero
  exact {
    K := ⌈(t.m : ℝ) ^ ((3 / 4 : ℝ) * t.m)⌉₊
    G := t.Gnext
    d := s.d + t.m
    q := q
    cost := s.cost + max t.L ((s.K : ℝ) ^ 2) * t.m
    previousL := t.L
    previousN := (t.m : ℝ) * s.K
    K_pos := by have := t.double; omega
    G_pos := by have := t.gain_one; omega
    q_zero := (spliceFrequency_eq_old s.q t.sampling.q (Nat.zero_le _)).trans s.q_zero
    q_pos := fun i _ => spliceFrequency_pos s.q t.sampling.q s.d s.q_pos t.sampling.q_pos i
    q_mono := fun i _ => spliceFrequency_strictMono s.q t.sampling.q s.d hb
      s.q_mono t.sampling.q_strict (Nat.lt_succ_self i)
    q_div := fun i _ => spliceFrequency_divisible s.q t.sampling.q s.d hb
      s.q_div t.sampling.q_div i }

private def initial : State where
  K := 100
  G := 1
  d := 0
  q := fun _ => 1
  cost := 0
  previousL := 0
  previousN := 0
  K_pos := by norm_num
  G_pos := by norm_num
  q_zero := rfl
  q_pos := fun _ _ => by norm_num
  q_mono := fun _ h => by omega
  q_div := fun _ h => by omega

private def states {θ : ℝ} (hs : CircleStageSupply θ) (height : ℕ → ℕ) : ℕ → State :=
  Nat.rec initial (nextState hs height)

private def choices {θ : ℝ} (hs : CircleStageSupply θ) (height : ℕ → ℕ) (n : ℕ) :
    Step θ height n (states hs height n) := chooseStep hs height n (states hs height n)

private theorem states_d_succ {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n : ℕ) :
    (states hs height (n + 1)).d = (states hs height n).d + (choices hs height n).m := rfl

private theorem states_d_ge {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n : ℕ) : n ≤ (states hs height n).d := by
  induction n with
  | zero => exact Nat.zero_le _
  | succ n ih =>
    rw [states_d_succ]
    have := (choices hs height n).m_large
    omega

private theorem states_d_mono {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) : Monotone (fun n => (states hs height n).d) := by
  apply monotone_nat_of_le_succ
  intro n
  rw [states_d_succ]
  omega

private theorem states_keep {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n i : ℕ) (hi : i ≤ (states hs height n).d) :
    (states hs height (n + 1)).q i = (states hs height n).q i :=
  spliceFrequency_eq_old _ _ hi

private theorem exists_global_frequency {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) :
    ∃ Q : ℕ → ℕ,
      (∀ n i, i ≤ (states hs height n).d → Q i = (states hs height n).q i) ∧
      (∀ i, 0 < Q i) ∧ StrictMono Q ∧ (∀ i, Q i ∣ Q (i + 1)) :=
  exists_frequency_of_prefix_tower (fun n => (states hs height n).d)
    (fun n => (states hs height n).q) (states_d_mono hs height)
    (states_d_ge hs height) (states_keep hs height)
    (fun n => (states hs height n).q_pos) (fun n => (states hs height n).q_mono)
    (fun n => (states hs height n).q_div)

private def globalFrequency {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) : ℕ → ℕ := Classical.choose (exists_global_frequency hs height)

private theorem global_agree {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n i : ℕ) (hi : i ≤ (states hs height n).d) :
    globalFrequency hs height i = (states hs height n).q i :=
  (Classical.choose_spec (exists_global_frequency hs height)).1 n i hi

private theorem global_block {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (n i : ℕ) (hi : i ≤ (choices hs height n).m) :
    globalFrequency hs height ((states hs height n).d + i) =
      (choices hs height n).sampling.q i := by
  rw [global_agree hs height (n + 1) _ (by rw [states_d_succ]; omega)]
  exact spliceFrequency_add _ _ _ _ (choices hs height n).sampling.q_zero

private def constructed {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) : CircleParameterData where
  m j := (choices hs height (j - 1)).m
  K j := (states hs height (j - 1)).K
  G j := (states hs height (j - 1)).G
  q := globalFrequency hs height
  L j := (choices hs height (j - 1)).L
  C j := max (choices hs height (j - 1)).L (((states hs height (j - 1)).K : ℝ) ^ 2)
  ε j := (choices hs height (j - 1)).ε
  height := height
  partition := φ
  samples j r := if hr : r < (states hs height (j - 1)).q (states hs height (j - 1)).d
    then (choices hs height (j - 1)).sampling.samples ⟨r, hr⟩ else ∅

private theorem blockEnd_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ) :
    (constructed hs height φ).blockEnd n = (states hs height n).d := by
  induction n with
  | zero => rfl
  | succ n ih =>
    rw [CircleParameterData.blockEnd_succ, ih, states_d_succ]
    simp [constructed]

private theorem cost_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ) :
    (states hs height n).cost =
      ∑ j ∈ Finset.Icc 1 n, (constructed hs height φ).C j * (constructed hs height φ).m j := by
  induction n with
  | zero => simp [states, initial]
  | succ n ih =>
    rw [Finset.sum_Icc_succ_top (by omega), ← ih]
    rfl

private theorem block_mem {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n i : ℕ) :
    i ∈ (constructed hs height φ).block (n + 1) ↔
      (states hs height n).d < i ∧ i ≤ (states hs height n).d + (choices hs height n).m := by
  rw [CircleParameterData.mem_block_iff]
  simp only [Nat.add_sub_cancel, blockEnd_eq, states_d_succ]

private theorem coefficientBound_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ) :
    (constructed hs height φ).coefficientBound (n + 1) =
      Real.exp ((((states hs height n).K : ℝ) + 1) * (states hs height n).cost) := by
  simp only [CircleParameterData.coefficientBound, Nat.add_sub_cancel]
  rw [← cost_eq hs height φ n]
  rfl

private theorem sample_index_bound {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n r : ℕ) :
    r < (constructed hs height φ).q ((constructed hs height φ).blockEnd n) ↔
      r < (states hs height n).q (states hs height n).d := by
  rw [blockEnd_eq]
  change r < globalFrequency hs height (states hs height n).d ↔ _
  rw [global_agree hs height n _ le_rfl]

private theorem samples_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n r : ℕ)
    (hr : r < (states hs height n).q (states hs height n).d) :
    (constructed hs height φ).samples (n + 1) r =
      (choices hs height n).sampling.samples ⟨r, hr⟩ := by
  simp [constructed, hr]

private theorem alpha_block {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ)
    (i : Fin (choices hs height n).m) :
    (constructed hs height φ).alpha θ ((states hs height n).d + (i.val + 1)) =
      Int.fract (((choices hs height n).sampling.q (i.val + 1) : ℝ) * θ) := by
  unfold CircleParameterData.alpha
  change Int.fract ((globalFrequency hs height _ : ℝ) * θ) = _
  rw [global_block hs height n _ (by have := i.isLt; omega)]

private theorem constructed_phases {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n i : ℕ)
    (hi : i ∈ (constructed hs height φ).block (n + 1)) :
    1 / (constructed hs height φ).N (n + 1) < (constructed hs height φ).alpha θ i ∧
      (constructed hs height φ).alpha θ i < 2 / (constructed hs height φ).N (n + 1) := by
  have hmem := (block_mem hs height φ n i).mp hi
  let r : Fin (choices hs height n).m := ⟨i - (states hs height n).d - 1, by omega⟩
  have heq : (states hs height n).d + (r.val + 1) = i := by dsimp [r]; omega
  have hphase := (choices hs height n).sampling.phases r
  rw [← alpha_block hs height φ n r, heq] at hphase
  exact hphase

private theorem convolution_eq_supported (K : ℕ) (A B : ℤ → ℂ)
    (hB : Function.support B ⊆ Set.Icc (-(K : ℤ)) (K : ℤ)) (n : ℕ) :
    CircleParameterData.convolutionCoefficient K A B (-(n : ℤ)) =
      convolution K A B n := by
  classical
  unfold CircleParameterData.convolutionCoefficient FiniteWindow.convolution
  rw [Finset.sum_filter]
  apply Finset.sum_congr rfl
  intro i _
  by_cases hi : |(n : ℤ) + i| ≤ (K : ℤ)
  · simp [hi]
  · simp only [hi, ite_false]
    have hzero : B (-(n : ℤ) - i) = 0 := by
      by_contra hne
      have hmem := hB (Function.mem_support.mpr hne)
      have ha : |-(n : ℤ) - i| ≤ (K : ℤ) := abs_le.mpr hmem
      have heq : -(n : ℤ) - i = -((n : ℤ) + i) := by ring
      rw [heq, abs_neg] at ha
      exact hi ha
    simp [hzero]

private theorem constructed_ratios {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n i : ℕ)
    (hi : i ∈ (constructed hs height φ).block (n + 1)) :
    (constructed hs height φ).N (n + 1) ≤
      ((constructed hs height φ).q (i + 1) : ℝ) / (constructed hs height φ).q i := by
  have hmem := (block_mem hs height φ n i).mp hi
  by_cases hlt : i < (states hs height n).d + (choices hs height n).m
  · let r := i - (states hs height n).d
    have hr : r < (choices hs height n).m := by dsimp [r]; omega
    have heq : (states hs height n).d + r = i := by dsimp [r]; omega
    have hq0 := global_block hs height n r hr.le
    have hq1 := global_block hs height n (r + 1) (by omega)
    rw [heq] at hq0
    rw [← Nat.add_assoc, heq] at hq1
    change ((choices hs height n).m : ℝ) * (states hs height n).K ≤
      (globalFrequency hs height (i + 1) : ℝ) / globalFrequency hs height i
    rw [hq0, hq1]
    exact (le_max_right _ _).trans ((choices hs height n).sampling.q_ratio r hr)
  · have heq : i = (states hs height (n + 1)).d := by
      rw [states_d_succ]
      omega
    have hm : 0 < (choices hs height (n + 1)).m := by
      have := (choices hs height (n + 1)).m_large
      omega
    have hq0 := global_block hs height (n + 1) 0 (Nat.zero_le _)
    have hq1 := global_block hs height (n + 1) 1 hm
    simp only [Nat.add_zero, ← heq] at hq0 hq1
    change ((choices hs height n).m : ℝ) * (states hs height n).K ≤
      (globalFrequency hs height (i + 1) : ℝ) / globalFrequency hs height i
    rw [hq0, hq1]
    exact (le_max_left _ _).trans ((choices hs height (n + 1)).sampling.q_ratio 0 hm)

private theorem constructed_spread {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ) (k : ℤ)
    (hlo : (constructed hs height φ).N (n + 1) / 4 ≤ |(k : ℝ)|)
    (hhi : |(k : ℝ)| ≤ ((constructed hs height φ).m (n + 1) : ℝ) ^
      ((4 / 5 : ℝ) * (constructed hs height φ).m (n + 1))) :
    ((constructed hs height φ).m (n + 1) : ℝ) / 20 ≤
      (((constructed hs height φ).block (n + 1)).filter (fun i =>
        1 - Int.fract ((k : ℝ) * (constructed hs height φ).alpha θ i) ∈
          Set.Icc ((constructed hs height φ).b (n + 1))
            (1 - (constructed hs height φ).b (n + 1)))).card := by
  classical
  have hspread := (choices hs height n).sampling.spread k hlo
    (by simpa [constructed] using hhi)
  simp only [Finset.card_univ, Fintype.card_fin, ProductPhases.strictPhaseGoodCount] at hspread
  let f : Fin (choices hs height n).m → ℕ := fun r => (states hs height n).d + (r.val + 1)
  have hcard :
      (Finset.univ.filter (fun r : Fin (choices hs height n).m =>
        1 - Int.fract ((k : ℝ) * Int.fract
          (((choices hs height n).sampling.q (r.val + 1) : ℝ) * θ)) ∈
            Set.Ioo (((choices hs height n).m : ℝ) ^ (-19 / 20 : ℝ))
              (1 - ((choices hs height n).m : ℝ) ^ (-19 / 20 : ℝ)))).card ≤
      (((constructed hs height φ).block (n + 1)).filter (fun i =>
        1 - Int.fract ((k : ℝ) * (constructed hs height φ).alpha θ i) ∈
          Set.Icc ((constructed hs height φ).b (n + 1))
            (1 - (constructed hs height φ).b (n + 1)))).card := by
    apply Finset.card_le_card_of_injOn f
    · intro r hr
      obtain ⟨_, hr⟩ := Finset.mem_filter.mp hr
      apply Finset.mem_filter.mpr
      constructor
      · apply (block_mem hs height φ n (f r)).mpr
        dsimp [f]
        have := r.isLt
        omega
      · change 1 - Int.fract ((k : ℝ) * (constructed hs height φ).alpha θ
            ((states hs height n).d + (r.val + 1))) ∈ _
        rw [alpha_block]
        have hneg : (-19 / 20 : ℝ) = -(19 / 20 : ℝ) := by norm_num
        rw [hneg] at hr
        exact ⟨hr.1.le, hr.2.le⟩
    · intro r _ s _ hrs
      apply Fin.ext
      dsimp [f] at hrs
      omega
  exact hspread.trans (by exact_mod_cast hcard)

private theorem prod_constructed_block {A : Type*} [CommMonoid A]
    {θ : ℝ} (hs : CircleStageSupply θ) (height : ℕ → ℕ)
    (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ) (f : ℕ → A) :
    (∏ i ∈ (constructed hs height φ).block (n + 1), f i) =
      ∏ r : Fin (choices hs height n).m, f ((states hs height n).d + (r.val + 1)) := by
  classical
  symm
  apply Finset.prod_bij (fun r _ => (states hs height n).d + (r.val + 1))
  · intro r _
    apply (block_mem hs height φ n _).mpr
    have := r.isLt
    omega
  · intro r _ s _ hrs
    apply Fin.ext
    omega
  · intro i hi
    have hmem := (block_mem hs height φ n i).mp hi
    refine ⟨⟨i - (states hs height n).d - 1, by omega⟩, Finset.mem_univ _, ?_⟩
    simp only
    omega
  · intro r _
    rfl

private theorem samplingAverage_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n r : ℕ)
    (hr : r < (states hs height n).q (states hs height n).d)
    (endpoint : Bool) (x : Circle) :
    (constructed hs height φ).samplingAverage θ (n + 1) r endpoint x =
      TrialSampling.average (choices hs height n).m
        ((choices hs height n).sampling.samples ⟨r, hr⟩) endpoint
        (fun i : Fin (choices hs height n).m =>
          Int.fract (((choices hs height n).sampling.q (i.val + 1) : ℝ) * θ))
        (fun i : Fin (choices hs height n).m =>
          ((choices hs height n).sampling.q (i.val + 1) : ℤ) • x) := by
  unfold CircleParameterData.samplingAverage TrialSampling.average
  rw [samples_eq hs height φ n r hr, one_div]
  congr 1
  apply Finset.sum_congr rfl
  intro l _
  rw [prod_constructed_block]
  apply Finset.prod_congr rfl
  intro i _
  rw [alpha_block]
  congr 2
  unfold CircleParameterData.coordinate
  change CircleParameterData.rep (globalFrequency hs height
    ((states hs height n).d + (i.val + 1)) • x) = _
  rw [global_block hs height n _ (by have := i.isLt; omega)]
  simp

private theorem shift_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n r : ℕ) :
    (constructed hs height φ).shift (n + 1) r =
      (r : ℝ) / (states hs height n).q (states hs height n).d := by
  unfold CircleParameterData.shift
  simp only [Nat.add_sub_cancel, blockEnd_eq]
  change (r : ℝ) / globalFrequency hs height (states hs height n).d = _
  rw [global_agree hs height n _ le_rfl]

private theorem ell_eq {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ)) (n : ℕ) :
    (constructed hs height φ).ell (n + 1) =
      1 / (((n + 2 : ℕ) : ℝ) ^ 2 * (1 + ((states hs height n).d : ℝ)) *
        (states hs height n).q (states hs height n).d) := by
  unfold CircleParameterData.ell
  simp only [Nat.add_sub_cancel, blockEnd_eq]
  change 1 / (((n + 2 : ℕ) : ℝ) ^ 2 * (1 + ((states hs height n).d : ℝ)) *
    globalFrequency hs height (states hs height n).d) = _
  rw [global_agree hs height n _ le_rfl]

private theorem constructed_laws {θ : ℝ} (hs : CircleStageSupply θ)
    (height : ℕ → ℕ) (φ : ℕ → ℕ → C(Circle, ℝ))
    (hφnonneg : ∀ j n x, 0 ≤ φ j n x)
    (hφsum : ∀ j x, ∑ n ∈ Finset.range (height j + 1), φ j n x = 1)
    (hφsupport : ∀ j, 1 ≤ j → ∀ n, ∀ x ∈ tsupport (φ j n),
      dist (x + (((n : ℝ) * θ : ℝ) : Circle)) 0 < 1 / (10 * (j : ℝ))) :
    CircleParameterLaws θ (constructed hs height φ) := by
  have hmpos (n : ℕ) : 0 < (choices hs height n).m := by
    have := (choices hs height n).m_large
    omega
  have hLmono : Monotone (fun n => (choices hs height n).L) := by
    apply monotone_nat_of_le_succ
    intro n
    exact (choices hs height (n + 1)).L_previous
  have globalSpec := Classical.choose_spec (exists_global_frequency hs height)
  refine {
    m_pos := ?_
    K_pos := ?_
    G_pos := ?_
    q_pos := globalSpec.2.1
    L_pos := ?_
    C_pos := ?_
    epsilon_pos := ?_
    partition_nonneg := fun j _ n _ x => hφnonneg j n x
    partition_sum := fun j _ x => hφsum j x
    partition_support := fun j hj n _ x hx => hφsupport j hj n x hx
    frequency_zero := ?_
    frequency_increasing := globalSpec.2.2.1
    frequency_divisible := globalSpec.2.2.2
    phase_bounds := ?_
    frequency_ratios := ?_
    spread_high := ?_
    spread_low := ?_
    initial_K := rfl
    initial_G := rfl
    L_ge_one := ?_
    L_monotone := ?_
    C_eq := fun _ _ => rfl
    m_ge_K := ?_
    m_ge_pow := ?_
    m_ge_G := ?_
    m_ge_200 := ?_
    next_K := ?_
    K_double := ?_
    next_G_gap := ?_
    attenuation_m := ?_
    attenuation_G := ?_
    continuity_height := ?_
    polynomial_lower := ?_
    polynomial_attenuation := ?_
    samples_nonempty := ?_
    samples_positive := ?_
    samples_range := ?_
    samples_phase := ?_
    sampling_L2 := ?_ }
  · intro j _
    exact hmpos (j - 1)
  · intro j _
    exact (states hs height (j - 1)).K_pos
  · intro j _
    exact (states hs height (j - 1)).G_pos
  · intro j _
    exact (by norm_num : (0 : ℝ) < 1).trans_le (choices hs height (j - 1)).L_one
  · intro j _
    exact ((by norm_num : (0 : ℝ) < 1).trans_le
      (choices hs height (j - 1)).L_one).trans_le (le_max_left _ _)
  · intro j _
    exact (choices hs height (j - 1)).epsilon_pos
  · exact (global_agree hs height 0 0 (Nat.zero_le _)).trans rfl
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact constructed_phases hs height φ n
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact constructed_ratios hs height φ n
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact constructed_spread hs height φ n
  · intro j hj k hlo hhi i hi
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    have ha := constructed_phases hs height φ n i hi
    exact phase_small_window (by exact_mod_cast hmpos n)
      (by exact_mod_cast (states hs height n).K_pos)
      (by exact_mod_cast (states hs height n).G_pos) ha.1.le ha.2.le hlo hhi
  · intro j _
    exact (choices hs height (j - 1)).L_one
  · intro j k _ hjk
    exact hLmono (Nat.sub_le_sub_right hjk 1)
  · intro j _
    exact (choices hs height (j - 1)).m_K
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).m_pow
  · intro j _
    exact (choices hs height (j - 1)).m_G
  · intro j _
    exact (choices hs height (j - 1)).m_large
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    rfl
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).double
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).gap
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).attenuation_m
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).gain
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).continuity
  · intro j hj A B _hAs hBs hA hB hA0 hB0
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    rw [coefficientBound_eq] at hA hB
    obtain ⟨r, hr, hcoef⟩ := (choices hs height n).polynomial A B
      (fun i _ => hA i) (fun i _ => hB i) hA0 hB0
    refine ⟨r, Finset.mem_Icc.mpr ⟨Nat.zero_le _, hr⟩, ?_⟩
    rw [convolution_eq_supported _ A B hBs]
    exact hcoef
  · intro j hj
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    exact (choices hs height n).attenuation
  · intro j hj r hr
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    have hr' := (sample_index_bound hs height φ n r).mp hr
    rw [samples_eq hs height φ n r hr']
    exact (choices hs height n).sampling.samples_nonempty ⟨r, hr'⟩
  · intro j hj r hr l hl
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    have hr' := (sample_index_bound hs height φ n r).mp hr
    rw [samples_eq hs height φ n r hr'] at hl
    exact (choices hs height n).sampling.samples_positive ⟨r, hr'⟩ l hl
  · intro j hj r hr l hl
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    have hr' := (sample_index_bound hs height φ n r).mp hr
    rw [samples_eq hs height φ n r hr'] at hl
    exact (choices hs height n).sampling.samples_range ⟨r, hr'⟩ l hl
  · intro j hj r hr l hl
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    have hr' := (sample_index_bound hs height φ n r).mp hr
    rw [samples_eq hs height φ n r hr'] at hl
    rw [shift_eq, ell_eq]
    exact (choices hs height n).sampling.samples_phase ⟨r, hr'⟩ l hl
  · intro j hj r hr endpoint
    obtain ⟨n, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (by omega : j ≠ 0)
    have hr' := (sample_index_bound hs height φ n r).mp hr
    have heq : (fun x : Circle => (constructed hs height φ).samplingAverage θ
        (n + 1) r endpoint x - 1) =
      (fun x : Circle => TrialSampling.average (choices hs height n).m
        ((choices hs height n).sampling.samples ⟨r, hr'⟩) endpoint
        (fun i : Fin (choices hs height n).m =>
          Int.fract (((choices hs height n).sampling.q (i.val + 1) : ℝ) * θ))
        (fun i : Fin (choices hs height n).m =>
          ((choices hs height n).sampling.q (i.val + 1) : ℤ) • x) - 1) := by
      funext x
      rw [samplingAverage_eq hs height φ n r hr']
    rw [heq]
    exact (choices hs height n).sampling.sampling_L2 ⟨r, hr'⟩ endpoint

theorem exists_parameters_of_stage_supply {θ : ℝ} (hθ : Irrational θ)
    (hs : CircleStageSupply θ) : ∃ p : CircleParameterData, CircleParameterLaws θ p := by
  obtain ⟨height, φ, hnonneg, hsum, hsupport, _⟩ := exists_preparatory_partitions hθ
  exact ⟨constructed hs height φ, constructed_laws hs height φ hnonneg hsum hsupport⟩

end Invariant233.CircleParameterRecursion

end

end OAI