File size: 48,978 Bytes
55a3af4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
import Mathlib

/-!
# Section 4, start: the Matrix lemma

The "Matrix lemma" of S. van Strien, "The Thurston algorithm for general entire maps on the
complex plane: existence and uniqueness" (research manuscript, version 8), lines 867-893 of the
text extraction, page 16 of the PDF:

> For a finite multicurve `Λ`, form `M_{αβ}` by summing reciprocal degrees of components of
> `f⁻¹(β)` in class `α`. If `f` has no Levy cycle, then `ρ(M) < 1`.

Lean has no curves, isotopy classes or coverings. The lemma is stated here for the combinatorial
data that the paper's proof uses, and the covering facts of lines 792-803 and 870-876 are
hypotheses. From these hypotheses the proof is complete: `matrix_lemma` gives `ρ(M) < 1`, and
`matrix_lemma_tendsto` gives that every entry of `M^k` tends to zero.

The data:

* `Λ` is the multicurve, a finite set of curve classes.
* `A` is the finite marked set and `f : A → A` is the map on the marks (`f(A) ⊆ A`).
* `L` is the set of closed lifts that enter the matrix: compact components of `f⁻¹(β)`, for a
  fixed representative of each `β ∈ Λ`, whose class is in `Λ`. The lift `l` is a component of
  `f⁻¹(tgt l)`, its class is `src l`, and it covers with degree `deg l`.
* `S α` is the set of finite labels in the bounded side of the class `α` (line 875).
* `thurstonMatrix src tgt deg` is `M`: its entry `(α, β)` is the sum of `1 / deg l` over the
  lifts `l` with `src l = α` and `tgt l = β`.

How the hypotheses of `matrix_lemma` match the paper:

* `[Fintype L]`: "the entries are finite sums" (line 871).
* `hdeg`: covering degrees are at least one.
* `hS`: the bounded side of an essential curve contains a finite label (it contains at least
  two, line 794; only one is used).
* `hmap`: "An arrow gives `f(S_α) ⊂ S_β`" (line 876). The disk bounded by a closed lift maps to
  the bounded target disk (lines 797-801), and the labels in that disk are `S_α` because the lift
  is in class `α`.
* `hdisj`: "For a fixed target representative the compact preimage disks are disjoint"
  (line 870; argued in lines 801-802). So distinct lifts of the same curve have disjoint label
  sets.
* `hLevy`: `f` has no Levy cycle among the classes of `Λ`. See `HasLevyCycle`.

The paper calls the lemma "the Berstein–Levy argument for the plane" and refers to [Hub,
Theorem 10.3.7] and [Sh, Proposition 1.1.16]. Nothing is taken from these references here: the
proof below is complete from the hypotheses listed above.

Also in this file, from the text before the lemma: the count of labels on the bounded side of an
essential circle (`bounded_side_card_of_essential`, lines 793-794), "an essential closed preimage
component has essential image" from its three cases (`essential_image_of_essential_lift`, lines
804-821), and the `p`-th power of the matrix of one cycle (`cycleMatrix_pow_self`, lines 887-890).

Differences from the paper's text:

* The paper decomposes the graph of arrows into strongly connected components, shows that each
  recurrent component is a single weighted cycle, computes its spectral radius
  `(∏ d_i)^{-1/p}`, and orders the components to make `M` block triangular. This file uses the
  same facts about periodic labels in a different arrangement, which avoids strongly connected
  components. The function `potential α` (the number of periodic labels in `S_α`, plus a count
  that separates the classes without periodic labels) does not decrease along an arrow. Among
  the lifts of a given `β` at most one keeps the potential (`level_lift_unique`): this is the
  paper's "Disjointness therefore allows just one, even counting multiple lifts in the same
  class". A cycle of such lifts with all degrees one would be a Levy cycle. From this a positive
  vector `u` and a number `θ < 1` with `u M ≤ θ u` are constructed, which gives `ρ(M) ≤ θ`.
* The value `(∏ d_i)^{-1/p}` of the spectral radius of a cycle block is not formalised. Only
  the bound below one is.
* The paper's Levy cycle has `f⁻¹(γ_{i+1})` with a degree-one component in class `γ_i`. Here the
  classes are numbered in the opposite direction (`δ_a = γ_{-a}`), which is the same notion.
* The spectral radius is that of the complexified matrix, so that complex eigenvalues count.
-/

open Finset

namespace Thurston.Conditional

/-! ## Linear algebra: a weighted column bound gives spectral radius below one -/

section LinearAlgebra

variable {n : Type*} [Fintype n] [DecidableEq n]

/-- If a nonnegative matrix `M` and a positive vector `u` satisfy `u M ≤ θ u` entrywise, then
every complex eigenvalue of `M` has modulus at most `θ`. -/
theorem norm_le_of_mem_spectrum_of_weighted_bound (M : Matrix n n ℝ) (hM : ∀ α β, 0 ≤ M α β)
    (u : n → ℝ) (hu : ∀ α, 0 < u α) (θ : ℝ) (hθ : ∀ β, ∑ α, u α * M α β ≤ θ * u β) (μ : ℂ)
    (hμ : μ ∈ spectrum ℂ (M.map (algebraMap ℝ ℂ))) : ‖μ‖ ≤ θ := by
  rw [spectrum.mem_iff] at hμ
  have hdet : (algebraMap ℂ (Matrix n n ℂ) μ - M.map (algebraMap ℝ ℂ)).det = 0 := by
    by_contra hne
    exact hμ ((Matrix.isUnit_iff_isUnit_det _).2 (isUnit_iff_ne_zero.2 hne))
  obtain ⟨x, hx0, hx⟩ := Matrix.exists_mulVec_eq_zero_iff.2 hdet
  have heig : ∀ α, μ * x α = ∑ β, (M α β : ℂ) * x β := by
    intro α
    have h := congrFun hx α
    rw [Algebra.algebraMap_eq_smul_one, Matrix.sub_mulVec, Matrix.smul_mulVec,
      Matrix.one_mulVec] at h
    simp only [Pi.sub_apply, Pi.smul_apply, smul_eq_mul, Pi.zero_apply, Matrix.mulVec,
      dotProduct, Matrix.map_apply, Complex.coe_algebraMap] at h
    exact sub_eq_zero.1 h
  have hrow : ∀ α, ‖μ‖ * ‖x α‖ ≤ ∑ β, M α β * ‖x β‖ := by
    intro α
    rw [← norm_mul, heig α]
    refine (norm_sum_le _ _).trans (le_of_eq ?_)
    refine Finset.sum_congr rfl fun β _ => ?_
    rw [norm_mul, Complex.norm_real, Real.norm_of_nonneg (hM α β)]
  have hN : 0 < ∑ α, u α * ‖x α‖ := by
    obtain ⟨α₀, hα₀⟩ := Function.ne_iff.1 hx0
    exact Finset.sum_pos' (fun α _ => mul_nonneg (hu α).le (norm_nonneg _))
      ⟨α₀, Finset.mem_univ _, mul_pos (hu α₀) (norm_pos_iff.2 hα₀)⟩
  have hmain : ‖μ‖ * ∑ α, u α * ‖x α‖ ≤ θ * ∑ α, u α * ‖x α‖ :=
    calc ‖μ‖ * ∑ α, u α * ‖x α‖ = ∑ α, u α * (‖μ‖ * ‖x α‖) := by
          rw [Finset.mul_sum]; exact Finset.sum_congr rfl fun α _ => by ring
      _ ≤ ∑ α, u α * ∑ β, M α β * ‖x β‖ :=
          Finset.sum_le_sum fun α _ => mul_le_mul_of_nonneg_left (hrow α) (hu α).le
      _ = ∑ β, (∑ α, u α * M α β) * ‖x β‖ := by
          simp_rw [Finset.mul_sum, Finset.sum_mul]
          rw [Finset.sum_comm]
          exact Finset.sum_congr rfl fun β _ => Finset.sum_congr rfl fun α _ => by ring
      _ ≤ ∑ β, θ * u β * ‖x β‖ :=
          Finset.sum_le_sum fun β _ => mul_le_mul_of_nonneg_right (hθ β) (norm_nonneg _)
      _ = θ * ∑ α, u α * ‖x α‖ := by
          rw [Finset.mul_sum]; exact Finset.sum_congr rfl fun α _ => by ring
  exact le_of_mul_le_mul_right hmain hN

/-- The same hypothesis with `θ < 1` gives spectral radius below one. -/
theorem spectralRadius_lt_one_of_weighted_bound (M : Matrix n n ℝ) (hM : ∀ α β, 0 ≤ M α β)
    (u : n → ℝ) (hu : ∀ α, 0 < u α) (θ : ℝ) (hθ1 : θ < 1)
    (hθ : ∀ β, ∑ α, u α * M α β ≤ θ * u β) :
    spectralRadius ℂ (M.map (algebraMap ℝ ℂ)) < 1 := by
  refine lt_of_le_of_lt (iSup₂_le fun k hk => ?_) (ENNReal.ofReal_lt_one.2 hθ1)
  rcases mem_quasispectrum_iff.1 hk with rfl | hk'
  · simp
  · rw [← enorm_eq_nnnorm, ← ofReal_norm]
    exact ENNReal.ofReal_le_ofReal
      (norm_le_of_mem_spectrum_of_weighted_bound M hM u hu θ hθ k hk')

theorem pow_apply_nonneg (M : Matrix n n ℝ) (hM : ∀ α β, 0 ≤ M α β) (k : ℕ) :
    ∀ α β, 0 ≤ (M ^ k) α β := by
  induction k with
  | zero => intro α β; rw [pow_zero, Matrix.one_apply]; split_ifs <;> norm_num
  | succ k ih =>
    intro α β
    rw [pow_succ, Matrix.mul_apply]
    exact Finset.sum_nonneg fun γ _ => mul_nonneg (ih α γ) (hM γ β)

theorem weighted_pow_le (M : Matrix n n ℝ) (hM : ∀ α β, 0 ≤ M α β) (u : n → ℝ) (θ : ℝ)
    (hθ0 : 0 ≤ θ) (hθ : ∀ β, ∑ α, u α * M α β ≤ θ * u β) (k : ℕ) :
    ∀ β, ∑ α, u α * (M ^ k) α β ≤ θ ^ k * u β := by
  induction k with
  | zero => intro β; simp [Matrix.one_apply]
  | succ k ih =>
    intro β
    calc ∑ α, u α * (M ^ (k + 1)) α β = ∑ γ, (∑ α, u α * (M ^ k) α γ) * M γ β := by
          simp_rw [pow_succ, Matrix.mul_apply, Finset.mul_sum, Finset.sum_mul]
          rw [Finset.sum_comm]
          exact Finset.sum_congr rfl fun γ _ => Finset.sum_congr rfl fun α _ => by ring
      _ ≤ ∑ γ, θ ^ k * u γ * M γ β :=
          Finset.sum_le_sum fun γ _ => mul_le_mul_of_nonneg_right (ih γ) (hM γ β)
      _ = θ ^ k * ∑ γ, u γ * M γ β := by
          rw [Finset.mul_sum]; exact Finset.sum_congr rfl fun γ _ => by ring
      _ ≤ θ ^ k * (θ * u β) := mul_le_mul_of_nonneg_left (hθ β) (pow_nonneg hθ0 k)
      _ = θ ^ (k + 1) * u β := by ring

/-- "In finite dimension, spectral radius below one implies that all entries of its powers tend
to zero" (lines 857-858), here directly from the weighted bound. -/
theorem tendsto_pow_apply_zero_of_weighted_bound (M : Matrix n n ℝ) (hM : ∀ α β, 0 ≤ M α β)
    (u : n → ℝ) (hu : ∀ α, 0 < u α) (θ : ℝ) (hθ0 : 0 ≤ θ) (hθ1 : θ < 1)
    (hθ : ∀ β, ∑ α, u α * M α β ≤ θ * u β) (α β : n) :
    Filter.Tendsto (fun k : ℕ => (M ^ k) α β) Filter.atTop (nhds 0) := by
  have hle : ∀ k, (M ^ k) α β ≤ θ ^ k * (u β / u α) := by
    intro k
    have h1 := weighted_pow_le M hM u θ hθ0 hθ k β
    have h2 : u α * (M ^ k) α β ≤ ∑ α', u α' * (M ^ k) α' β :=
      Finset.single_le_sum (f := fun α' => u α' * (M ^ k) α' β)
        (fun α' _ => mul_nonneg (hu α').le (pow_apply_nonneg M hM k α' β)) (Finset.mem_univ α)
    rw [← mul_div_assoc, le_div_iff₀ (hu α)]
    linarith
  have hlim : Filter.Tendsto (fun k : ℕ => θ ^ k * (u β / u α)) Filter.atTop (nhds 0) := by
    simpa using (tendsto_pow_atTop_nhds_zero_of_lt_one hθ0 hθ1).mul_const (u β / u α)
  exact squeeze_zero (fun k => pow_apply_nonneg M hM k α β) hle hlim

end LinearAlgebra

/-! ## A weighted map without a cycle of weight one -/

section Chain

variable {n : Type*} [Fintype n]

/-- Along `card n + 1` steps of the chain `β, lam β, lam (lam β), …` some point repeats, so the
chain runs through a cycle, and the product of the weights is at most `1/2`. -/
theorem prod_chain_le_half (lam : n → n) (w : n → ℝ) (hw0 : ∀ β, 0 ≤ w β) (hw1 : ∀ β, w β ≤ 1)
    (hcyc : ∀ γ p, 0 < p → lam^[p] γ = γ → ∃ a < p, w (lam^[a] γ) ≤ 1 / 2) (β : n) :
    ∏ a ∈ range (Fintype.card n + 1), w (lam^[a] β) ≤ 1 / 2 := by
  obtain ⟨i, j, hij, heq⟩ := Fintype.exists_ne_map_eq_of_card_lt
    (fun a : Fin (Fintype.card n + 1) => lam^[a] β) (by simp)
  wlog hlt : (i : ℕ) < j generalizing i j
  · have hne : (i : ℕ) ≠ j := fun h => hij (Fin.ext h)
    exact this j i hij.symm heq.symm (by omega)
  have heq' : lam^[(i : ℕ)] β = lam^[(j : ℕ)] β := heq
  have hper : lam^[(j : ℕ) - i] (lam^[(i : ℕ)] β) = lam^[(i : ℕ)] β := by
    rw [← Function.iterate_add_apply, Nat.sub_add_cancel hlt.le, ← heq']
  obtain ⟨a, ha, hwa⟩ := hcyc _ _ (by omega) hper
  rw [← Function.iterate_add_apply] at hwa
  have hmem : a + i ∈ range (Fintype.card n + 1) := by
    rw [Finset.mem_range]; have := j.2; omega
  calc ∏ b ∈ range (Fintype.card n + 1), w (lam^[b] β)
      = w (lam^[a + i] β) * ∏ b ∈ (range (Fintype.card n + 1)).erase (a + i), w (lam^[b] β) :=
        (Finset.mul_prod_erase _ _ hmem).symm
    _ ≤ 1 / 2 * 1 := mul_le_mul hwa
        (Finset.prod_le_one₀ (fun b _ => hw0 _) (fun b _ => hw1 _))
        (Finset.prod_nonneg fun b _ => hw0 _) (by norm_num)
    _ = 1 / 2 := by norm_num

/-- A map `lam` with weights `w` in `[0, 1]`, such that every cycle of `lam` has a weight at most
`1/2`, has a vector `v ≥ 1` and a number `θ < 1` with `v (lam β) w β ≤ θ v β` for every `β`. -/
theorem exists_weight_of_no_unit_cycle (lam : n → n) (w : n → ℝ) (hw0 : ∀ β, 0 ≤ w β)
    (hw1 : ∀ β, w β ≤ 1)
    (hcyc : ∀ γ p, 0 < p → lam^[p] γ = γ → ∃ a < p, w (lam^[a] γ) ≤ 1 / 2) :
    ∃ (θ : ℝ) (v : n → ℝ), 0 ≤ θ ∧ θ < 1 ∧ (∀ β, 1 ≤ v β) ∧ ∀ β, v (lam β) * w β ≤ θ * v β := by
  set N := Fintype.card n with hN
  set θ : ℝ := 1 - 1 / (2 * ((N : ℝ) + 1)) with hθ
  have hN0 : (0 : ℝ) ≤ N := Nat.cast_nonneg N
  have hsmall : 1 / (2 * ((N : ℝ) + 1)) ≤ 1 / 2 := by
    rw [div_le_div_iff₀ (by positivity) (by norm_num)]; linarith
  have hsmall0 : 0 < 1 / (2 * ((N : ℝ) + 1)) := by positivity
  have hθ0 : 0 < θ := by linarith
  have hθ1 : θ < 1 := by linarith
  have hbern : 1 / 2 ≤ θ ^ (N + 1) := by
    have h := one_add_mul_le_pow (a := -(1 / (2 * ((N : ℝ) + 1)))) (by linarith) (N + 1)
    have e : 1 + ((N + 1 : ℕ) : ℝ) * -(1 / (2 * ((N : ℝ) + 1))) = 1 / 2 := by
      push_cast; field_simp; ring
    have e2 : 1 + -(1 / (2 * ((N : ℝ) + 1))) = θ := by rw [hθ]; ring
    rw [e, e2] at h
    exact h
  have hinv : θ * θ⁻¹ = 1 := mul_inv_cancel₀ hθ0.ne'
  have hinv0 : 0 < θ⁻¹ := inv_pos.2 hθ0
  let W : ℕ → n → ℝ := fun j β => ∏ a ∈ range j, w (lam^[a] β)
  have hWsucc : ∀ j β, W (j + 1) β = w β * W j (lam β) := by
    intro j β
    show ∏ a ∈ range (j + 1), w (lam^[a] β) = w β * ∏ a ∈ range j, w (lam^[a] (lam β))
    rw [Finset.prod_range_succ', mul_comm]
    rfl
  have hW0 : ∀ β, W 0 β = 1 := fun β => Finset.prod_range_zero _
  have hWnn : ∀ j β, 0 ≤ W j β := fun j β => Finset.prod_nonneg fun a _ => hw0 _
  have hWhalf : ∀ β, W (N + 1) β ≤ 1 / 2 := fun β => prod_chain_le_half lam w hw0 hw1 hcyc β
  refine ⟨θ, fun β => ∑ j ∈ range (N + 1), θ⁻¹ ^ j * W j β, hθ0.le, hθ1, fun β => ?_,
    fun β => ?_⟩
  · show 1 ≤ ∑ j ∈ range (N + 1), θ⁻¹ ^ j * W j β
    rw [Finset.sum_range_succ', pow_zero, hW0, mul_one]
    have h : 0 ≤ ∑ j ∈ range N, θ⁻¹ ^ (j + 1) * W (j + 1) β :=
      Finset.sum_nonneg fun j _ => mul_nonneg (pow_nonneg hinv0.le _) (hWnn _ _)
    linarith
  · have hL : (∑ j ∈ range (N + 1), θ⁻¹ ^ j * W j (lam β)) * w β
        = ∑ j ∈ range (N + 1), θ⁻¹ ^ j * W (j + 1) β := by
      rw [Finset.sum_mul]
      refine Finset.sum_congr rfl fun j _ => ?_
      rw [hWsucc]; ring
    have hR : θ * ∑ j ∈ range (N + 1), θ⁻¹ ^ j * W j β
        = ∑ j ∈ range N, θ⁻¹ ^ j * W (j + 1) β + θ := by
      rw [Finset.sum_range_succ', pow_zero, hW0, mul_one, mul_add, mul_one, Finset.mul_sum]
      congr 1
      refine Finset.sum_congr rfl fun j _ => ?_
      rw [pow_succ]
      linear_combination (θ⁻¹ ^ j * W (j + 1) β) * hinv
    have hlast : θ⁻¹ ^ N * W (N + 1) β ≤ θ :=
      calc θ⁻¹ ^ N * W (N + 1) β ≤ θ⁻¹ ^ N * θ ^ (N + 1) :=
            mul_le_mul_of_nonneg_left ((hWhalf β).trans hbern) (pow_nonneg hinv0.le _)
        _ = θ := by
            rw [pow_succ, ← mul_assoc, ← mul_pow, inv_mul_cancel₀ hθ0.ne', one_pow, one_mul]
    show (∑ j ∈ range (N + 1), θ⁻¹ ^ j * W j (lam β)) * w β
      ≤ θ * ∑ j ∈ range (N + 1), θ⁻¹ ^ j * W j β
    rw [hL, hR, Finset.sum_range_succ]
    linarith

end Chain

/-! ## From levels to a weighted bound -/

section Levels

variable {Λ L : Type*} [Fintype Λ] [DecidableEq Λ] [Fintype L]

/-- Lifts with weights in `[0, 1]`, and a function `Φ` that does not decrease along a lift.
Suppose that the lifts that keep `Φ` are given by a map `lam` with weights `w` (at most one such
lift over each `β`), and that `v` and `θ < 1` are as in `exists_weight_of_no_unit_cycle`. Then
there is a positive vector `u` with `∑ u (src l) wt l ≤ (1 + θ)/2 · u β`, the sum over the lifts
`l` of `β`. -/
theorem exists_weighted_bound_of_levels (src tgt : L → Λ) (wt : L → ℝ)
    (hwt1 : ∀ l, wt l ≤ 1) (Φ : Λ → ℕ) (hΦ : ∀ l, Φ (src l) ≤ Φ (tgt l))
    (lam : Λ → Λ) (w : Λ → ℝ) (hw0 : ∀ β, 0 ≤ w β)
    (hlevel : ∀ l, Φ (src l) = Φ (tgt l) → src l = lam (tgt l) ∧ wt l = w (tgt l))
    (huniq : ∀ l l', Φ (src l) = Φ (tgt l) → Φ (src l') = Φ (tgt l') → tgt l = tgt l' → l = l')
    (θ : ℝ) (hθ1 : θ < 1) (v : Λ → ℝ) (hv : ∀ β, 1 ≤ v β)
    (hvw : ∀ β, v (lam β) * w β ≤ θ * v β) :
    ∃ u : Λ → ℝ, (∀ α, 0 < u α) ∧
      ∀ β, ∑ l ∈ univ.filter (fun l => tgt l = β), u (src l) * wt l ≤ (1 + θ) / 2 * u β := by
  set V : ℝ := ∑ β, v β with hV
  have hv0 : ∀ β, 0 ≤ v β := fun β => by linarith [hv β]
  have hvV : ∀ β, v β ≤ V := fun β =>
    Finset.single_le_sum (f := v) (fun γ _ => hv0 γ) (Finset.mem_univ β)
  have hV0 : 0 ≤ V := Finset.sum_nonneg fun γ _ => hv0 γ
  have h1θ : 0 < 1 - θ := by linarith
  have hc0 : (0 : ℝ) ≤ Fintype.card L := Nat.cast_nonneg _
  set R : ℝ := 2 * (Fintype.card L) * V / (1 - θ) + 1 with hR
  have hR1 : 1 ≤ R := by
    have : 0 ≤ 2 * (Fintype.card L : ℝ) * V / (1 - θ) := by positivity
    linarith
  have hR0 : 0 < R := by linarith
  have hRV : (Fintype.card L : ℝ) * V ≤ (1 - θ) / 2 * R := by
    have e : (1 - θ) / 2 * (2 * (Fintype.card L) * V / (1 - θ)) = (Fintype.card L : ℝ) * V := by
      field_simp
    rw [hR, mul_add, e]
    linarith
  refine ⟨fun α => R ^ Φ α * v α, fun α => mul_pos (pow_pos hR0 _) (by linarith [hv α]),
    fun β => ?_⟩
  have hpowβ : 0 < R ^ Φ β := pow_pos hR0 _
  set Lβ : Finset L := univ.filter (fun l => tgt l = β) with hLβ
  rw [← Finset.sum_filter_add_sum_filter_not Lβ (fun l => Φ (src l) = Φ (tgt l))]
  -- The lifts of `β` that keep `Φ`: at most one, and it is the one given by `lam` and `w`.
  have hlev : ∑ l ∈ Lβ.filter (fun l => Φ (src l) = Φ (tgt l)), R ^ Φ (src l) * v (src l) * wt l
      ≤ θ * (R ^ Φ β * v β) := by
    have hterm : ∀ l ∈ Lβ.filter (fun l => Φ (src l) = Φ (tgt l)),
        R ^ Φ (src l) * v (src l) * wt l = R ^ Φ β * (v (lam β) * w β) := by
      intro l hl
      simp only [hLβ, Finset.mem_filter, Finset.mem_univ, true_and] at hl
      obtain ⟨hs, hwl⟩ := hlevel l hl.2
      rw [hl.2, hs, hwl, hl.1]; ring
    rw [Finset.sum_congr rfl hterm, Finset.sum_const, nsmul_eq_mul]
    have hcard : ((Lβ.filter (fun l => Φ (src l) = Φ (tgt l))).card : ℝ) ≤ 1 := by
      have h : (Lβ.filter (fun l => Φ (src l) = Φ (tgt l))).card ≤ 1 := by
        refine Finset.card_le_one.2 fun l hl l' hl' => ?_
        simp only [hLβ, Finset.mem_filter, Finset.mem_univ, true_and] at hl hl'
        exact huniq l l' hl.2 hl'.2 (hl.1.trans hl'.1.symm)
      exact_mod_cast h
    have hnn : 0 ≤ R ^ Φ β * (v (lam β) * w β) :=
      mul_nonneg hpowβ.le (mul_nonneg (hv0 _) (hw0 β))
    have h2 := mul_le_mul_of_nonneg_left (hvw β) hpowβ.le
    nlinarith
  -- The other lifts of `β` come from a lower value of `Φ`.
  have hnon : ∑ l ∈ Lβ.filter (fun l => ¬ Φ (src l) = Φ (tgt l)),
      R ^ Φ (src l) * v (src l) * wt l ≤ (1 - θ) / 2 * (R ^ Φ β * v β) := by
    have hterm : ∀ l ∈ Lβ.filter (fun l => ¬ Φ (src l) = Φ (tgt l)),
        R ^ Φ (src l) * v (src l) * wt l ≤ R ^ Φ β / R * V := by
      intro l hl
      simp only [hLβ, Finset.mem_filter, Finset.mem_univ, true_and] at hl
      have hlt : Φ (src l) + 1 ≤ Φ β := by
        have h := hΦ l
        rw [hl.1] at h
        have hne := hl.2
        rw [hl.1] at hne
        omega
      have hpow : R ^ Φ (src l) ≤ R ^ Φ β / R := by
        rw [le_div_iff₀ hR0, ← pow_succ]
        exact pow_le_pow_right₀ hR1 hlt
      have hnn : 0 ≤ R ^ Φ (src l) * v (src l) := mul_nonneg (pow_pos hR0 _).le (hv0 _)
      calc R ^ Φ (src l) * v (src l) * wt l ≤ R ^ Φ (src l) * v (src l) * 1 :=
            mul_le_mul_of_nonneg_left (hwt1 l) hnn
        _ = R ^ Φ (src l) * v (src l) := mul_one _
        _ ≤ R ^ Φ β / R * V :=
            mul_le_mul hpow (hvV _) (hv0 _) (div_nonneg hpowβ.le hR0.le)
    have hcard : ((Lβ.filter (fun l => ¬ Φ (src l) = Φ (tgt l))).card : ℝ) ≤ Fintype.card L := by
      exact_mod_cast Finset.card_le_univ _
    have hq : 0 ≤ R ^ Φ β / R := div_nonneg hpowβ.le hR0.le
    calc ∑ l ∈ Lβ.filter (fun l => ¬ Φ (src l) = Φ (tgt l)), R ^ Φ (src l) * v (src l) * wt l
        ≤ ∑ _l ∈ Lβ.filter (fun l => ¬ Φ (src l) = Φ (tgt l)), R ^ Φ β / R * V :=
          Finset.sum_le_sum hterm
      _ = (Lβ.filter (fun l => ¬ Φ (src l) = Φ (tgt l))).card * (R ^ Φ β / R * V) := by
          rw [Finset.sum_const, nsmul_eq_mul]
      _ ≤ (Fintype.card L : ℝ) * (R ^ Φ β / R * V) :=
          mul_le_mul_of_nonneg_right hcard (mul_nonneg hq hV0)
      _ = R ^ Φ β / R * ((Fintype.card L : ℝ) * V) := by ring
      _ ≤ R ^ Φ β / R * ((1 - θ) / 2 * R) := mul_le_mul_of_nonneg_left hRV hq
      _ = (1 - θ) / 2 * R ^ Φ β := by field_simp
      _ ≤ (1 - θ) / 2 * (R ^ Φ β * v β) := by
          have h := hv β
          have h3 : R ^ Φ β ≤ R ^ Φ β * v β := by nlinarith
          exact mul_le_mul_of_nonneg_left h3 (by linarith)
  linarith

end Levels

/-! ## Covering facts at the start of Section 4: the counting part -/

section Covering

/-- "Since `∞` is marked, the bounded disk of an essential plane circle contains between `2` and
`s - 1` finite labels" (lines 793-794). `D` is the set of finite labels in the bounded disk. The
unbounded side contains the other finite labels and `∞`. Essential means at least two marks on
each side. -/
theorem bounded_side_card_of_essential {A : Type*} [Fintype A] [DecidableEq A] (D : Finset A)
    (hin : 2 ≤ D.card) (hout : 2 ≤ Dᶜ.card + 1) :
    2 ≤ D.card ∧ D.card ≤ Fintype.card A - 1 := by
  have h := D.card_compl
  have h' := D.card_le_univ
  omega

/-- "It follows that an essential closed preimage component has essential image" (line 821).
`D` is the set of finite labels in the disk bounded by the closed lift, and `Δ` is the set of
finite labels in the bounded target disk. The three cases of lines 804-820 are the hypotheses:

* `hmap`: the disk of the lift maps to the target disk, and marks map to marks. This gives "if
  the target disk contains no finite mark, its proper preimage disks contain none".
* `hone`: "if it contains just one mark […] at most one source mark lies in the disk".
* `hall`: for a transcendental map there is no closed lift when the target disk contains all
  finite marks. -/
theorem essential_image_of_essential_lift {A : Type*} [Fintype A] (f : A → A)
    (D Δ : Finset A) (hmap : ∀ a ∈ D, f a ∈ Δ) (hone : Δ.card = 1 → D.card ≤ 1)
    (hall : Δ ≠ univ) (hD : 2 ≤ D.card) : 2 ≤ Δ.card ∧ Δ.card ≤ Fintype.card A - 1 := by
  have hne : Δ.card ≠ 0 := by
    obtain ⟨a, ha⟩ := Finset.card_pos.1 (by omega : 0 < D.card)
    exact (Finset.card_pos.2 ⟨f a, hmap a ha⟩).ne'
  have h1 : Δ.card ≠ 1 := fun h => by have := hone h; omega
  have hlt : Δ.card < Fintype.card A := by
    rw [← Finset.card_univ]
    exact Finset.card_lt_card (Finset.ssubset_univ_iff.2 hall)
  omega

/-- "For a fixed target representative the compact preimage disks are disjoint. Each essential
one contains at least two finite marks, so the entries are finite sums" (lines 870-871): the set
of lifts is finite. -/
theorem finite_lifts {Λ A L : Type*} [Finite Λ] [Finite A] (src tgt : L → Λ)
    (S : Λ → Finset A) (hS : ∀ α, (S α).Nonempty)
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l'))) :
    Finite L := by
  have hinj : Function.Injective fun l : L => (tgt l, S (src l)) := by
    intro l l' h
    by_contra hne
    have h1 : tgt l = tgt l' := congrArg Prod.fst h
    have h2 : S (src l) = S (src l') := congrArg Prod.snd h
    have hd := hdisj l l' hne h1
    rw [h2] at hd
    obtain ⟨a, ha⟩ := hS (src l')
    exact Finset.disjoint_left.1 hd ha ha
  exact Finite.of_injective _ hinj

/-- The hypotheses of `essential_image_of_essential_lift` can hold together (the example
`z² - 1` of the paper, with labels `0, 1, 2` for the marks `-1, 0, a`). -/
example : ∃ (f : Fin 3 → Fin 3) (D Δ : Finset (Fin 3)), (∀ a ∈ D, f a ∈ Δ) ∧
    (Δ.card = 1 → D.card ≤ 1) ∧ Δ ≠ univ ∧ 2 ≤ D.card :=
  ⟨![1, 0, 2], {0, 1}, {0, 1}, by decide, by decide, by decide, by decide⟩

end Covering

/-! ## One cycle: the recurrent block of the paper -/

section CycleBlock

/-- The matrix of a single weighted cycle on `p` classes: the entry `(i, i + 1)` is `w i`. -/
def cycleMatrix (p : ℕ) (w : ZMod p → ℝ) : Matrix (ZMod p) (ZMod p) ℝ :=
  Matrix.of fun i j => if j = i + 1 then w i else 0

theorem cycleMatrix_pow_apply (p : ℕ) [NeZero p] (w : ZMod p → ℝ) (k : ℕ) (i j : ZMod p) :
    (cycleMatrix p w ^ k) i j = if j = i + k then ∏ a ∈ range k, w (i + a) else 0 := by
  induction k generalizing j with
  | zero => simp [Matrix.one_apply, eq_comm]
  | succ k ih =>
    rw [pow_succ, Matrix.mul_apply]
    simp only [ih]
    simp only [cycleMatrix, Matrix.of_apply, ite_mul, zero_mul, Finset.sum_ite_eq',
      Finset.mem_univ, ↓reduceIte]
    have e : i + ((k + 1 : ℕ) : ZMod p) = i + (k : ZMod p) + 1 := by push_cast; ring
    rw [e, Finset.prod_range_succ]
    split_ifs <;> simp

theorem prod_range_natCast_zmod (p : ℕ) [NeZero p] (g : ZMod p → ℝ) :
    ∏ a ∈ range p, g (a : ZMod p) = ∏ x : ZMod p, g x := by
  refine Finset.prod_bij (fun a _ => (a : ZMod p)) (fun _ _ => Finset.mem_univ _) ?_ ?_
    (fun _ _ => rfl)
  · intro a ha b hb hab
    rw [Finset.mem_range] at ha hb
    have h := (ZMod.natCast_eq_natCast_iff' a b p).1 hab
    rwa [Nat.mod_eq_of_lt ha, Nat.mod_eq_of_lt hb] at h
  · intro x _
    exact ⟨x.val, Finset.mem_range.2 (ZMod.val_lt x), ZMod.natCast_zmod_val x⟩

/-- "The component is a single cycle with weights `1/d_1, …, 1/d_p`. The `p`th power of its
matrix is `(∏ d_i)⁻¹` times the identity" (lines 887-888). Here `w i = 1 / d_i`. -/
theorem cycleMatrix_pow_self (p : ℕ) [NeZero p] (w : ZMod p → ℝ) :
    cycleMatrix p w ^ p = (∏ i, w i) • (1 : Matrix (ZMod p) (ZMod p) ℝ) := by
  ext i j
  rw [cycleMatrix_pow_apply, Matrix.smul_apply, Matrix.one_apply, ZMod.natCast_self, add_zero,
    prod_range_natCast_zmod p (fun x => w (i + x)), smul_eq_mul]
  have hshift : ∏ x : ZMod p, w (i + x) = ∏ x : ZMod p, w x :=
    Fintype.prod_equiv (Equiv.addLeft i) _ _ (fun _ => rfl)
  rw [hshift]
  by_cases h : j = i
  · simp [h]
  · have h' : ¬ i = j := fun h'' => h h''.symm
    simp [h, h']

/-- Every complex eigenvalue `μ` of the cycle matrix has `μ^p = ∏ w_i`. -/
theorem cycleMatrix_eigenvalue_pow (p : ℕ) [NeZero p] (w : ZMod p → ℝ) (μ : ℂ)
    (hμ : μ ∈ spectrum ℂ ((cycleMatrix p w).map (algebraMap ℝ ℂ))) :
    μ ^ p = ((∏ i, w i : ℝ) : ℂ) := by
  have h1 := spectrum.pow_mem_pow _ p hμ
  have h3 : ((cycleMatrix p w).map (algebraMap ℝ ℂ)) ^ p
      = (cycleMatrix p w ^ p).map (algebraMap ℝ ℂ) := by
    rw [← RingHom.mapMatrix_apply, ← RingHom.mapMatrix_apply, map_pow]
  have h2 : ((cycleMatrix p w).map (algebraMap ℝ ℂ)) ^ p
      = algebraMap ℂ (Matrix (ZMod p) (ZMod p) ℂ) ((∏ i, w i : ℝ) : ℂ) := by
    rw [h3, cycleMatrix_pow_self, Algebra.algebraMap_eq_smul_one]
    ext i j
    by_cases h : i = j <;> simp [h]
  rw [h2, spectrum.scalar_eq] at h1
  exact h1

/-- "so the spectral radius of this block is `(∏ d_i)^{-1/p}`" (lines 888-890): every complex
eigenvalue `μ` of the cycle matrix with weights `w_i = 1 / d_i` has `|μ|^p = ∏ w_i`. -/
theorem cycleMatrix_eigenvalue_norm_pow (p : ℕ) [NeZero p] (w : ZMod p → ℝ)
    (hw : ∀ i, 0 ≤ w i) (μ : ℂ)
    (hμ : μ ∈ spectrum ℂ ((cycleMatrix p w).map (algebraMap ℝ ℂ))) :
    ‖μ‖ ^ p = ∏ i, w i := by
  rw [← norm_pow, cycleMatrix_eigenvalue_pow p w μ hμ, Complex.norm_real,
    Real.norm_of_nonneg (Finset.prod_nonneg fun i _ => hw i)]

end CycleBlock

/-! ## The multicurve, its lifts, and the Matrix lemma -/

section MatrixLemma

open Function

-- Each statement below lists only the hypotheses it uses. Some finiteness instances of the
-- section are not needed in every lemma.
set_option linter.unusedSectionVars false

variable {Λ A L : Type*} [Fintype Λ] [DecidableEq Λ] [Fintype A] [Fintype L]
  (src tgt : L → Λ) (deg : L → ℕ) (S : Λ → Finset A) (f : A → A)

/-- The matrix `M` of the Matrix lemma: "form `M_{αβ}` by summing reciprocal degrees of
components of `f⁻¹(β)` in class `α`" (line 867). -/
noncomputable def thurstonMatrix (src tgt : L → Λ) (deg : L → ℕ) : Matrix Λ Λ ℝ :=
  Matrix.of fun α β => ∑ l ∈ univ.filter (fun l => src l = α ∧ tgt l = β), (1 : ℝ) / deg l

/-- A Levy cycle among the classes of the multicurve: pairwise distinct classes
`δ_0, …, δ_{p-1}` and lifts `ℓ_0, …, ℓ_{p-1}` of degree one, where `ℓ_a` is a component of
`f⁻¹(δ_a)` in the class `δ_{a+1}`, indices modulo `p` (Section 1, lines 107-111, with the
classes numbered in the opposite direction). -/
def HasLevyCycle (src tgt : L → Λ) (deg : L → ℕ) : Prop :=
  ∃ (p : ℕ) (δ : ℕ → Λ) (ℓ : ℕ → L), 0 < p ∧ (∀ a < p, ∀ b < p, δ a = δ b → a = b) ∧
    ∀ a < p, tgt (ℓ a) = δ a ∧ src (ℓ a) = δ ((a + 1) % p) ∧ deg (ℓ a) = 1

theorem thurstonMatrix_nonneg (α β : Λ) : 0 ≤ thurstonMatrix src tgt deg α β := by
  unfold thurstonMatrix
  rw [Matrix.of_apply]
  exact Finset.sum_nonneg fun l _ => by positivity

/-- A weighted column sum of `M` is a sum over the lifts of `β`. -/
theorem sum_mul_thurstonMatrix (u : Λ → ℝ) (β : Λ) :
    ∑ α, u α * thurstonMatrix src tgt deg α β
      = ∑ l ∈ univ.filter (fun l => tgt l = β), u (src l) * (1 / deg l) := by
  simp only [thurstonMatrix, Matrix.of_apply, Finset.mul_sum, Finset.sum_filter]
  rw [Finset.sum_comm]
  refine Finset.sum_congr rfl fun l _ => ?_
  by_cases h : tgt l = β
  · simp [h]
  · simp [h]

/-- Two distinct lifts of the same curve are in different classes: their disks are disjoint, and
the disks of two lifts in one class contain the same labels. -/
theorem src_ne_of_ne (hS : ∀ α, (S α).Nonempty)
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    {l l' : L} (hne : l ≠ l') (ht : tgt l = tgt l') : src l ≠ src l' := by
  intro h
  have hd := hdisj l l' hne ht
  rw [h] at hd
  obtain ⟨a, ha⟩ := hS (src l')
  exact Finset.disjoint_left.1 hd ha ha

/-- So each entry of `M` is a sum with at most one term: it is zero or one reciprocal degree. -/
theorem card_lifts_le_one (hS : ∀ α, (S α).Nonempty)
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    (α β : Λ) : (univ.filter (fun l => src l = α ∧ tgt l = β)).card ≤ 1 := by
  refine Finset.card_le_one.2 fun l hl l' hl' => ?_
  simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hl hl'
  by_contra hne
  exact src_ne_of_ne src tgt S hS hdisj hne (hl.2.trans hl'.2.symm) (hl.1.trans hl'.1.symm)

open Classical in
/-- `P_α`: "the periodic labels among them" (line 875). -/
noncomputable def perLabels (S : Λ → Finset A) (f : A → A) (α : Λ) : Finset A :=
  (S α).filter fun a => a ∈ periodicPts f

theorem mem_perLabels {α : Λ} {a : A} :
    a ∈ perLabels S f α ↔ a ∈ S α ∧ a ∈ periodicPts f := by
  simp [perLabels]

/-- "Thus along internal arrows the cardinality of `P_α` cannot decrease" (line 880). Here this
holds along every arrow: `f` maps `P_α` into `P_β`, and `f` is injective on periodic labels. -/
theorem perLabels_card_le (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l)) (l : L) :
    (perLabels S f (src l)).card ≤ (perLabels S f (tgt l)).card := by
  refine Finset.card_le_card_of_injOn f ?_ ?_
  · intro a ha
    have ha' := (mem_perLabels S f).1 ha
    exact (mem_perLabels S f).2 ⟨hmap l a ha'.1, (bijOn_periodicPts f).mapsTo ha'.2⟩
  · intro a ha b hb hab
    exact (bijOn_periodicPts f).injOn ((mem_perLabels S f).1 ha).2
      ((mem_perLabels S f).1 hb).2 hab

/-- "In particular `f(P_α) = P_β` along each internal arrow" (line 881): when the two sets have
the same number of elements. -/
theorem image_perLabels_eq [DecidableEq A] (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l)) (l : L)
    (hcard : (perLabels S f (src l)).card = (perLabels S f (tgt l)).card) :
    (perLabels S f (src l)).image f = perLabels S f (tgt l) := by
  refine Finset.eq_of_subset_of_card_le ?_ ?_
  · intro b hb
    obtain ⟨a, ha, rfl⟩ := Finset.mem_image.1 hb
    have ha' := (mem_perLabels S f).1 ha
    exact (mem_perLabels S f).2 ⟨hmap l a ha'.1, (bijOn_periodicPts f).mapsTo ha'.2⟩
  · rw [Finset.card_image_of_injOn, hcard]
    intro a ha b hb hab
    exact (bijOn_periodicPts f).injOn ((mem_perLabels S f).1 ha).2
      ((mem_perLabels S f).1 hb).2 hab

/-- Every label is periodic after `|A|` steps. -/
theorem iterate_card_mem_periodicPts (b : A) : f^[Fintype.card A] b ∈ periodicPts f := by
  obtain ⟨i, j, hij, heq⟩ := Fintype.exists_ne_map_eq_of_card_lt
    (fun a : Fin (Fintype.card A + 1) => f^[a] b) (by simp)
  wlog hlt : (i : ℕ) < j generalizing i j
  · have hne : (i : ℕ) ≠ j := fun h => hij (Fin.ext h)
    exact this j i hij.symm heq.symm (by omega)
  have heq' : f^[(i : ℕ)] b = f^[(j : ℕ)] b := heq
  have hper : IsPeriodicPt f ((j : ℕ) - i) (f^[(i : ℕ)] b) := by
    show f^[(j : ℕ) - i] (f^[(i : ℕ)] b) = f^[(i : ℕ)] b
    rw [← Function.iterate_add_apply, Nat.sub_add_cancel hlt.le, ← heq']
  have hmem : f^[(i : ℕ)] b ∈ periodicPts f := mk_mem_periodicPts (by omega) hper
  have hi : (i : ℕ) ≤ Fintype.card A := by have := j.2; omega
  have hsplit : f^[Fintype.card A] b = f^[Fintype.card A - i] (f^[(i : ℕ)] b) := by
    rw [← Function.iterate_add_apply, Nat.sub_add_cancel hi]
  rw [hsplit]
  exact ((bijOn_periodicPts f).mapsTo.iterate _) hmem

/-- "If a cycle through `α` has length `p`, then `f^p(S_α) ⊂ S_α`. This nonempty finite set
contains a periodic label by iterating `f^p`" (lines 877-879). The proof of `matrix_lemma` does
not use this statement; it uses `depth_le` in its place. -/
theorem exists_periodic_of_iterate_mapsTo (T : Finset A) (hT : T.Nonempty) (p : ℕ) (hp : 0 < p)
    (h : ∀ a ∈ T, f^[p] a ∈ T) : ∃ a ∈ T, a ∈ periodicPts f := by
  obtain ⟨a, ha⟩ := hT
  have hiter : ∀ k, (f^[p])^[k] a ∈ T := by
    intro k
    induction k with
    | zero => exact ha
    | succ k ih => rw [Function.iterate_succ_apply']; exact h _ ih
  refine ⟨_, hiter (Fintype.card A), ?_⟩
  obtain ⟨n, hn, hx⟩ := mem_periodicPts.1 (iterate_card_mem_periodicPts (f^[p]) a)
  refine mk_mem_periodicPts (Nat.mul_pos hp hn) ?_
  show f^[p * n] _ = _
  rw [Function.iterate_mul]
  exact hx

/-- `S α` contains a label of the form `f^j(b)`. -/
def MeetsImage (S : Λ → Finset A) (f : A → A) (α : Λ) (j : ℕ) : Prop :=
  ∃ a ∈ S α, ∃ b, f^[j] b = a

open Classical in
/-- The largest `j ≤ |A|` such that `S α` contains a label of the form `f^j(b)`. It equals `|A|`
exactly when `S α` contains a periodic label. -/
noncomputable def depth (S : Λ → Finset A) (f : A → A) (α : Λ) : ℕ :=
  Nat.findGreatest (MeetsImage S f α) (Fintype.card A)

/-- The potential: it does not decrease along an arrow (`potential_le`). -/
noncomputable def potential (S : Λ → Finset A) (f : A → A) (α : Λ) : ℕ :=
  depth S f α + (perLabels S f α).card

theorem meetsImage_depth (hS : ∀ α, (S α).Nonempty) (α : Λ) :
    MeetsImage S f α (depth S f α) := by
  classical
  unfold depth
  obtain ⟨a, ha⟩ := hS α
  exact Nat.findGreatest_spec (P := MeetsImage S f α) (Nat.zero_le _) ⟨a, ha, a, rfl⟩

theorem depth_le_card (α : Λ) : depth S f α ≤ Fintype.card A := by
  classical
  unfold depth
  exact Nat.findGreatest_le _

theorem le_depth {α : Λ} {j : ℕ} (hj : j ≤ Fintype.card A) (h : MeetsImage S f α j) :
    j ≤ depth S f α := by
  classical
  unfold depth
  exact Nat.le_findGreatest hj h

/-- Along an arrow the depth does not decrease. If it stays the same, the source class contains a
periodic label. -/
theorem depth_le (hS : ∀ α, (S α).Nonempty) (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (l : L) : depth S f (src l) ≤ depth S f (tgt l) ∧
      (depth S f (src l) = depth S f (tgt l) → (perLabels S f (src l)).Nonempty) := by
  have hspec := meetsImage_depth S f hS (src l)
  have hle := depth_le_card S f (src l)
  have hnext : MeetsImage S f (tgt l) (depth S f (src l) + 1) := by
    obtain ⟨a, ha, b, rfl⟩ := hspec
    exact ⟨f (f^[depth S f (src l)] b), hmap l _ ha, b, Function.iterate_succ_apply' f _ b⟩
  rcases Nat.lt_or_ge (depth S f (src l)) (Fintype.card A) with hlt | hge
  · have h1 : depth S f (src l) + 1 ≤ depth S f (tgt l) := le_depth S f hlt hnext
    exact ⟨by omega, fun h => by omega⟩
  · have hK : depth S f (src l) = Fintype.card A := le_antisymm hle hge
    have hprev : MeetsImage S f (tgt l) (depth S f (src l)) := by
      obtain ⟨a, ha, b, hb⟩ := hnext
      exact ⟨a, ha, f b, by rw [← hb, Function.iterate_succ_apply]⟩
    refine ⟨le_depth S f hle hprev, fun _ => ?_⟩
    rw [hK] at hspec
    obtain ⟨a, ha, b, rfl⟩ := hspec
    exact ⟨_, (mem_perLabels S f).2 ⟨ha, iterate_card_mem_periodicPts f b⟩⟩

theorem potential_le (hS : ∀ α, (S α).Nonempty) (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (l : L) : potential S f (src l) ≤ potential S f (tgt l) := by
  have h1 := (depth_le src tgt S f hS hmap l).1
  have h2 := perLabels_card_le src tgt S f hmap l
  unfold potential
  omega

/-- A lift that keeps the potential: `P_α` is not empty and `f(P_α) = P_β`. In the paper these
are the internal arrows of a recurrent component (lines 877-881). -/
theorem level_facts [DecidableEq A] (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l)) (l : L)
    (hl : potential S f (src l) = potential S f (tgt l)) :
    (perLabels S f (src l)).Nonempty ∧
      (perLabels S f (src l)).image f = perLabels S f (tgt l) := by
  have h1 := depth_le src tgt S f hS hmap l
  have h2 := perLabels_card_le src tgt S f hmap l
  unfold potential at hl
  exact ⟨h1.2 (by omega), image_perLabels_eq src tgt S f hmap l (by omega)⟩

/-- "For one fixed target representative `β`, every internal predecessor disk must contain the
entire set `(f|𝒫)⁻¹(P_β)` […]. Disjointness therefore allows just one, even counting multiple
lifts in the same class" (lines 882-885). -/
theorem level_lift_unique (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    (l l' : L) (hl : potential S f (src l) = potential S f (tgt l))
    (hl' : potential S f (src l') = potential S f (tgt l')) (ht : tgt l = tgt l') : l = l' := by
  classical
  by_contra hne
  obtain ⟨⟨a, ha⟩, him⟩ := level_facts src tgt S f hS hmap l hl
  obtain ⟨-, him'⟩ := level_facts src tgt S f hS hmap l' hl'
  have hfa : f a ∈ perLabels S f (tgt l') := by
    rw [← ht, ← him]; exact Finset.mem_image_of_mem f ha
  rw [← him'] at hfa
  obtain ⟨a', ha', haa'⟩ := Finset.mem_image.1 hfa
  have heq : a' = a := (bijOn_periodicPts f).injOn ((mem_perLabels S f).1 ha').2
    ((mem_perLabels S f).1 ha).2 haa'
  subst heq
  exact Finset.disjoint_left.1 (hdisj l l' hne ht) ((mem_perLabels S f).1 ha).1
    ((mem_perLabels S f).1 ha').1

/-- The lift over `β` that keeps the potential, as a map `lam` on classes with a weight `w`.
Where there is no such lift, `w β = 0`. -/
theorem exists_level_map (hdeg : ∀ l, 1 ≤ deg l) (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l'))) :
    ∃ (lam : Λ → Λ) (w : Λ → ℝ), (∀ β, 0 ≤ w β) ∧ (∀ β, w β ≤ 1) ∧
      (∀ l, potential S f (src l) = potential S f (tgt l) →
        src l = lam (tgt l) ∧ (1 : ℝ) / deg l = w (tgt l)) ∧
      ∀ β, w β ≠ 0 → ∃ l, tgt l = β ∧ src l = lam β ∧ w β = 1 / deg l := by
  classical
  have hdeg1 : ∀ l, (1 : ℝ) / deg l ≤ 1 := fun l => by
    have h : (1 : ℝ) ≤ deg l := by exact_mod_cast hdeg l
    rw [div_le_one (by linarith)]; exact h
  refine ⟨fun β => if h : ∃ l, tgt l = β ∧ potential S f (src l) = potential S f β
      then src h.choose else β,
    fun β => if h : ∃ l, tgt l = β ∧ potential S f (src l) = potential S f β
      then 1 / (deg h.choose : ℝ) else 0, fun β => ?_, fun β => ?_, fun l hl => ?_, fun β hβ => ?_⟩
  · dsimp only
    split_ifs
    · positivity
    · exact le_rfl
  · dsimp only
    split_ifs
    · exact hdeg1 _
    · norm_num
  · have h : ∃ l', tgt l' = tgt l ∧ potential S f (src l') = potential S f (tgt l) := ⟨l, rfl, hl⟩
    have hc : h.choose = l := by
      obtain ⟨h1, h2⟩ := h.choose_spec
      exact level_lift_unique src tgt S f hS hmap hdisj _ _ (by rw [h1]; exact h2) hl h1
    dsimp only
    split_ifs
    rw [hc]
    exact ⟨rfl, rfl⟩
  · dsimp only at hβ ⊢
    split_ifs at hβ ⊢ with h
    · exact ⟨h.choose, h.choose_spec.1, rfl, rfl⟩
    · exact absurd rfl hβ

/-- The key bound: without a Levy cycle there are a positive vector `u` and a number `θ < 1`
with `u M ≤ θ u`. -/
theorem exists_weighted_bound (hdeg : ∀ l, 1 ≤ deg l) (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    (hLevy : ¬ HasLevyCycle src tgt deg) :
    ∃ (u : Λ → ℝ) (θ : ℝ), (∀ α, 0 < u α) ∧ 0 ≤ θ ∧ θ < 1 ∧
      ∀ β, ∑ α, u α * thurstonMatrix src tgt deg α β ≤ θ * u β := by
  obtain ⟨lam, w, hw0, hw1, hlevel, hwne⟩ := exists_level_map src tgt deg S f hdeg hS hmap hdisj
  -- A cycle of `lam` with all weights above `1/2` would be a Levy cycle.
  have hcyc : ∀ γ p, 0 < p → lam^[p] γ = γ → ∃ a < p, w (lam^[a] γ) ≤ 1 / 2 := by
    intro γ p hp hper
    by_contra hcon
    push Not at hcon
    have hlift : ∀ a, a < p → ∃ l, tgt l = lam^[a] γ ∧ src l = lam^[a + 1] γ ∧ deg l = 1 := by
      intro a ha
      have hwa := hcon a ha
      obtain ⟨l, hl1, hl2, hl3⟩ := hwne (lam^[a] γ) (by linarith)
      refine ⟨l, hl1, by rw [hl2, Function.iterate_succ_apply'], ?_⟩
      rw [hl3] at hwa
      have hd : (1 : ℝ) ≤ deg l := by exact_mod_cast hdeg l
      have hd2 : (deg l : ℝ) < 2 := by
        rw [lt_div_iff₀ (by linarith)] at hwa; linarith
      have hd3 : deg l < 2 := by exact_mod_cast hd2
      have := hdeg l
      omega
    obtain ⟨l₀, -⟩ := hlift 0 hp
    have hlift' : ∀ a, ∃ l, a < p → tgt l = lam^[a] γ ∧ src l = lam^[a + 1] γ ∧ deg l = 1 := by
      intro a
      by_cases ha : a < p
      · obtain ⟨l, hl⟩ := hlift a ha
        exact ⟨l, fun _ => hl⟩
      · exact ⟨l₀, fun h => absurd h ha⟩
    choose ℓ hℓ using hlift'
    have hperpt : IsPeriodicPt lam p γ := hper
    have hmin_pos : 0 < minimalPeriod lam γ := hperpt.minimalPeriod_pos hp
    have hmin_le : minimalPeriod lam γ ≤ p := hperpt.minimalPeriod_le hp
    refine hLevy ⟨minimalPeriod lam γ, fun a => lam^[a] γ, ℓ, hmin_pos, fun a ha b hb hab => ?_,
      fun a ha => ?_⟩
    · exact iterate_injOn_Iio_minimalPeriod (Set.mem_Iio.2 ha) (Set.mem_Iio.2 hb) hab
    · obtain ⟨h1, h2, h3⟩ := hℓ a (by omega)
      exact ⟨h1, by rw [h2]; exact (iterate_mod_minimalPeriod_eq (f := lam) (x := γ)).symm, h3⟩
  obtain ⟨θ, v, hθ0, hθ1, hv, hvw⟩ := exists_weight_of_no_unit_cycle lam w hw0 hw1 hcyc
  have hdeg1 : ∀ l, (1 : ℝ) / deg l ≤ 1 := fun l => by
    have h : (1 : ℝ) ≤ deg l := by exact_mod_cast hdeg l
    rw [div_le_one (by linarith)]; exact h
  obtain ⟨u, hu, hbound⟩ := exists_weighted_bound_of_levels src tgt (fun l => (1 : ℝ) / deg l)
    hdeg1 (potential S f) (potential_le src tgt S f hS hmap) lam w hw0 hlevel
    (level_lift_unique src tgt S f hS hmap hdisj) θ hθ1 v hv hvw
  refine ⟨u, (1 + θ) / 2, hu, by linarith, by linarith, fun β => ?_⟩
  rw [sum_mul_thurstonMatrix]
  exact hbound β

/-- **Matrix lemma** (lines 867-868): "For a finite multicurve `Λ`, form `M_{αβ}` by summing
reciprocal degrees of components of `f⁻¹(β)` in class `α`. If `f` has no Levy cycle, then
`ρ(M) < 1`." The header of this file says how the hypotheses match the paper. -/
theorem matrix_lemma (hdeg : ∀ l, 1 ≤ deg l) (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    (hLevy : ¬ HasLevyCycle src tgt deg) :
    spectralRadius ℂ ((thurstonMatrix src tgt deg).map (algebraMap ℝ ℂ)) < 1 := by
  obtain ⟨u, θ, hu, -, hθ1, hθ⟩ := exists_weighted_bound src tgt deg S f hdeg hS hmap hdisj hLevy
  exact spectralRadius_lt_one_of_weighted_bound _ (thurstonMatrix_nonneg src tgt deg) u hu θ
    hθ1 hθ

/-- The Matrix lemma for single eigenvalues: every complex eigenvalue of `M` has modulus below
one. -/
theorem matrix_lemma_eigenvalue (hdeg : ∀ l, 1 ≤ deg l) (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    (hLevy : ¬ HasLevyCycle src tgt deg) (μ : ℂ)
    (hμ : μ ∈ spectrum ℂ ((thurstonMatrix src tgt deg).map (algebraMap ℝ ℂ))) : ‖μ‖ < 1 := by
  obtain ⟨u, θ, hu, -, hθ1, hθ⟩ := exists_weighted_bound src tgt deg S f hdeg hS hmap hdisj hLevy
  exact lt_of_le_of_lt (norm_le_of_mem_spectrum_of_weighted_bound _
    (thurstonMatrix_nonneg src tgt deg) u hu θ hθ μ hμ) hθ1

/-- The form in which the Matrix lemma is used (Figure 1, `(M_Λ^k)_{γγ} → 0`): every entry of
`M^k` tends to zero as `k → ∞`. -/
theorem matrix_lemma_tendsto (hdeg : ∀ l, 1 ≤ deg l) (hS : ∀ α, (S α).Nonempty)
    (hmap : ∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l))
    (hdisj : ∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l')))
    (hLevy : ¬ HasLevyCycle src tgt deg) (α β : Λ) :
    Filter.Tendsto (fun k : ℕ => (thurstonMatrix src tgt deg ^ k) α β) Filter.atTop (nhds 0) := by
  obtain ⟨u, θ, hu, hθ0, hθ1, hθ⟩ :=
    exists_weighted_bound src tgt deg S f hdeg hS hmap hdisj hLevy
  exact tendsto_pow_apply_zero_of_weighted_bound _ (thurstonMatrix_nonneg src tgt deg) u hu θ
    hθ0 hθ1 hθ α β

/-- The hypotheses of `matrix_lemma` can hold together: the paper's example `z² - 1` (lines
853-855). The labels `0, 1, 2` stand for the marks `-1, 0, a`; the map exchanges `-1` and `0`
and fixes `a`. There is one curve, with `-1` and `0` on its bounded side, and one lift, of
degree two. The matrix is `[1/2]`. -/
example : ∃ (src tgt : Unit → Unit) (deg : Unit → ℕ) (S : Unit → Finset (Fin 3))
    (f : Fin 3 → Fin 3), (∀ l, 1 ≤ deg l) ∧ (∀ α, (S α).Nonempty) ∧
    (∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l)) ∧
    (∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l'))) ∧
    ¬ HasLevyCycle src tgt deg ∧ thurstonMatrix src tgt deg () () = 1 / 2 := by
  refine ⟨id, id, fun _ => 2, fun _ => {0, 1}, ![1, 0, 2], fun _ => by norm_num,
    fun _ => ⟨0, by simp⟩, by decide, fun l l' h => absurd (Subsingleton.elim l l') h, ?_, ?_⟩
  · rintro ⟨p, δ, ℓ, hp, -, h⟩
    have h1 := (h 0 hp).2.2
    norm_num at h1
  · simp [thurstonMatrix]

/-- The hypothesis `hLevy` of `matrix_lemma` cannot be dropped. In the same model with a lift of
degree one, the other hypotheses hold and the matrix is `[1]`, with spectral radius one. -/
example : ∃ (src tgt : Unit → Unit) (deg : Unit → ℕ) (S : Unit → Finset (Fin 3))
    (f : Fin 3 → Fin 3), (∀ l, 1 ≤ deg l) ∧ (∀ α, (S α).Nonempty) ∧
    (∀ l, ∀ a ∈ S (src l), f a ∈ S (tgt l)) ∧
    (∀ l l', l ≠ l' → tgt l = tgt l' → Disjoint (S (src l)) (S (src l'))) ∧
    thurstonMatrix src tgt deg () () = 1 := by
  refine ⟨id, id, fun _ => 1, fun _ => {0, 1}, ![1, 0, 2], fun _ => le_rfl,
    fun _ => ⟨0, by simp⟩, by decide, fun l l' h => absurd (Subsingleton.elim l l') h, ?_⟩
  simp [thurstonMatrix]

end MatrixLemma

end Thurston.Conditional