Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 24,550 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/-
Copyright (c) 2021 Alex Kontorovich and Heather Macbeth and Marc Masdeu. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Alex Kontorovich, Heather Macbeth, Marc Masdeu
-/

import analysis.complex.upper_half_plane.basic
import linear_algebra.general_linear_group
import analysis.matrix

/-!
# The action of the modular group SL(2, β„€) on the upper half-plane

We define the action of `SL(2,β„€)` on `ℍ` (via restriction of the `SL(2,ℝ)` action in
`analysis.complex.upper_half_plane`). We then define the standard fundamental domain
(`modular_group.fd`, `π’Ÿ`) for this action and show
(`modular_group.exists_smul_mem_fd`) that any point in `ℍ` can be
moved inside `π’Ÿ`.

## Main definitions

The standard (closed) fundamental domain of the action of `SL(2,β„€)` on `ℍ`, denoted `π’Ÿ`:
`fd := {z | 1 ≀ (z : β„‚).norm_sq ∧ |z.re| ≀ (1 : ℝ) / 2}`

The standard open fundamental domain of the action of `SL(2,β„€)` on `ℍ`, denoted `π’Ÿα΅’`:
`fdo := {z | 1 < (z : β„‚).norm_sq ∧ |z.re| < (1 : ℝ) / 2}`

These notations are localized in the `modular` locale and can be enabled via `open_locale modular`.

## Main results

Any `z : ℍ` can be moved to `π’Ÿ` by an element of `SL(2,β„€)`:
`exists_smul_mem_fd (z : ℍ) : βˆƒ g : SL(2,β„€), g β€’ z ∈ π’Ÿ`

If both `z` and `Ξ³ β€’ z` are in the open domain `π’Ÿα΅’` then `z = Ξ³ β€’ z`:
`eq_smul_self_of_mem_fdo_mem_fdo {z : ℍ} {g : SL(2,β„€)} (hz : z ∈ π’Ÿα΅’) (hg : g β€’ z ∈ π’Ÿα΅’) : z = g β€’ z`

# Discussion

Standard proofs make use of the identity

`g β€’ z = a / c - 1 / (c (cz + d))`

for `g = [[a, b], [c, d]]` in `SL(2)`, but this requires separate handling of whether `c = 0`.
Instead, our proof makes use of the following perhaps novel identity (see
`modular_group.smul_eq_lc_row0_add`):

`g β€’ z = (a c + b d) / (c^2 + d^2) + (d z - c) / ((c^2 + d^2) (c z + d))`

where there is no issue of division by zero.

Another feature is that we delay until the very end the consideration of special matrices
`T=[[1,1],[0,1]]` (see `modular_group.T`) and `S=[[0,-1],[1,0]]` (see `modular_group.S`), by
instead using abstract theory on the properness of certain maps (phrased in terms of the filters
`filter.cocompact`, `filter.cofinite`, etc) to deduce existence theorems, first to prove the
existence of `g` maximizing `(gβ€’z).im` (see `modular_group.exists_max_im`), and then among
those, to minimize `|(gβ€’z).re|` (see `modular_group.exists_row_one_eq_and_min_re`).
-/

/- Disable these instances as they are not the simp-normal form, and having them disabled ensures
we state lemmas in this file without spurious `coe_fn` terms. -/
local attribute [-instance] matrix.special_linear_group.has_coe_to_fun
local attribute [-instance] matrix.general_linear_group.has_coe_to_fun

open complex (hiding abs_one abs_two abs_mul abs_add)
open matrix (hiding mul_smul) matrix.special_linear_group upper_half_plane
noncomputable theory

local notation `SL(` n `, ` R `)`:= special_linear_group (fin n) R
local prefix `β†‘β‚˜`:1024 := @coe _ (matrix (fin 2) (fin 2) β„€) _

open_locale upper_half_plane complex_conjugate

local attribute [instance] fintype.card_fin_even

namespace modular_group

variables {g : SL(2, β„€)} (z : ℍ)

section bottom_row

/-- The two numbers `c`, `d` in the "bottom_row" of `g=[[*,*],[c,d]]` in `SL(2, β„€)` are coprime. -/
lemma bottom_row_coprime {R : Type*} [comm_ring R] (g : SL(2, R)) :
  is_coprime ((↑g : matrix (fin 2) (fin 2) R) 1 0) ((↑g : matrix (fin 2) (fin 2) R) 1 1) :=
begin
  use [- (↑g : matrix (fin 2) (fin 2) R) 0 1, (↑g : matrix (fin 2) (fin 2) R) 0 0],
  rw [add_comm, neg_mul, ←sub_eq_add_neg, ←det_fin_two],
  exact g.det_coe,
end

/-- Every pair `![c, d]` of coprime integers is the "bottom_row" of some element `g=[[*,*],[c,d]]`
of `SL(2,β„€)`. -/
lemma bottom_row_surj {R : Type*} [comm_ring R] :
  set.surj_on (Ξ» g : SL(2, R), @coe _ (matrix (fin 2) (fin 2) R) _ g 1) set.univ
    {cd | is_coprime (cd 0) (cd 1)} :=
begin
  rintros cd ⟨bβ‚€, a, gcd_eqn⟩,
  let A := of ![![a, -bβ‚€], cd],
  have det_A_1 : det A = 1,
  { convert gcd_eqn,
    simp [A, det_fin_two, (by ring : a * (cd 1) + bβ‚€ * (cd 0) = bβ‚€ * (cd 0) + a * (cd 1))] },
  refine ⟨⟨A, det_A_1⟩, set.mem_univ _, _⟩,
  ext; simp [A]
end

end bottom_row

section tendsto_lemmas

open filter continuous_linear_map
local attribute [instance] matrix.normed_add_comm_group matrix.normed_space
local attribute [simp] coe_smul

/-- The function `(c,d) β†’ |cz+d|^2` is proper, that is, preimages of bounded-above sets are finite.
-/
lemma tendsto_norm_sq_coprime_pair :
  filter.tendsto (Ξ» p : fin 2 β†’ β„€, ((p 0 : β„‚) * z + p 1).norm_sq)
  cofinite at_top :=
begin
  let Ο€β‚€ : (fin 2 β†’ ℝ) β†’β‚—[ℝ] ℝ := linear_map.proj 0,
  let π₁ : (fin 2 β†’ ℝ) β†’β‚—[ℝ] ℝ := linear_map.proj 1,
  let f : (fin 2 β†’ ℝ) β†’β‚—[ℝ] β„‚ := Ο€β‚€.smul_right (z:β„‚) + π₁.smul_right 1,
  have f_def : ⇑f = Ξ» (p : fin 2 β†’ ℝ), (p 0 : β„‚) * ↑z + p 1,
  { ext1,
    dsimp only [linear_map.coe_proj, real_smul,
      linear_map.coe_smul_right, linear_map.add_apply],
    rw mul_one, },
  have : (Ξ» (p : fin 2 β†’ β„€), norm_sq ((p 0 : β„‚) * ↑z + ↑(p 1)))
    = norm_sq ∘ f ∘ (Ξ» p : fin 2 β†’ β„€, (coe : β„€ β†’ ℝ) ∘ p),
  { ext1,
    rw f_def,
    dsimp only [function.comp],
    rw [of_real_int_cast, of_real_int_cast], },
  rw this,
  have hf : f.ker = βŠ₯,
  { let g : β„‚ β†’β‚—[ℝ] (fin 2 β†’ ℝ) :=
      linear_map.pi ![im_lm, im_lm.comp ((z:β„‚) β€’ (conj_ae  : β„‚ β†’β‚—[ℝ] β„‚))],
    suffices : ((z:β„‚).im⁻¹ β€’ g).comp f = linear_map.id,
    { exact linear_map.ker_eq_bot_of_inverse this },
    apply linear_map.ext,
    intros c,
    have hz : (z:β„‚).im β‰  0 := z.2.ne',
    rw [linear_map.comp_apply, linear_map.smul_apply, linear_map.id_apply],
    ext i,
    dsimp only [g, pi.smul_apply, linear_map.pi_apply, smul_eq_mul],
    fin_cases i,
    { show ((z : β„‚).im)⁻¹ * (f c).im = c 0,
      rw [f_def, add_im, of_real_mul_im, of_real_im, add_zero, mul_left_comm,
        inv_mul_cancel hz, mul_one], },
    { show ((z : β„‚).im)⁻¹ * ((z : β„‚) * conj (f c)).im = c 1,
      rw [f_def, ring_hom.map_add, ring_hom.map_mul, mul_add, mul_left_comm, mul_conj,
        conj_of_real, conj_of_real, ← of_real_mul, add_im, of_real_im, zero_add,
        inv_mul_eq_iff_eq_mulβ‚€ hz],
      simp only [of_real_im, of_real_re, mul_im, zero_add, mul_zero] } },
  have h₁ := (linear_equiv.closed_embedding_of_injective hf).tendsto_cocompact,
  have hβ‚‚ : tendsto (Ξ» p : fin 2 β†’ β„€, (coe : β„€ β†’ ℝ) ∘ p) cofinite (cocompact _),
  { convert tendsto.pi_map_Coprod (Ξ» i, int.tendsto_coe_cofinite),
    { rw Coprod_cofinite },
    { rw Coprod_cocompact } },
  exact tendsto_norm_sq_cocompact_at_top.comp (h₁.comp hβ‚‚)
end


/-- Given `coprime_pair` `p=(c,d)`, the matrix `[[a,b],[*,*]]` is sent to `a*c+b*d`.
  This is the linear map version of this operation.
-/
def lc_row0 (p : fin 2 β†’ β„€) : (matrix (fin 2) (fin 2) ℝ) β†’β‚—[ℝ] ℝ :=
((p 0:ℝ) β€’ linear_map.proj 0 + (p 1:ℝ) β€’ linear_map.proj 1 : (fin 2 β†’ ℝ) β†’β‚—[ℝ] ℝ).comp
  (linear_map.proj 0)

@[simp] lemma lc_row0_apply (p : fin 2 β†’ β„€) (g : matrix (fin 2) (fin 2) ℝ) :
  lc_row0 p g = p 0 * g 0 0 + p 1 * g 0 1 :=
rfl

/-- Linear map sending the matrix [a, b; c, d] to the matrix [acβ‚€ + bdβ‚€, - adβ‚€ + bcβ‚€; c, d], for
some fixed `(cβ‚€, dβ‚€)`. -/
@[simps] def lc_row0_extend {cd : fin 2 β†’ β„€} (hcd : is_coprime (cd 0) (cd 1)) :
  (matrix (fin 2) (fin 2) ℝ) ≃ₗ[ℝ] matrix (fin 2) (fin 2) ℝ :=
linear_equiv.Pi_congr_right
![begin
    refine linear_map.general_linear_group.general_linear_equiv ℝ (fin 2 β†’ ℝ)
      (general_linear_group.to_linear (plane_conformal_matrix (cd 0 : ℝ) (-(cd 1 : ℝ)) _)),
    norm_cast,
    rw neg_sq,
    exact hcd.sq_add_sq_ne_zero
  end,
  linear_equiv.refl ℝ (fin 2 β†’ ℝ)]

/-- The map `lc_row0` is proper, that is, preimages of cocompact sets are finite in
`[[* , *], [c, d]]`.-/
theorem tendsto_lc_row0 {cd : fin 2 β†’ β„€} (hcd : is_coprime (cd 0) (cd 1)) :
  tendsto (Ξ» g : {g : SL(2, β„€) // β†‘β‚˜g 1 = cd}, lc_row0 cd ↑(↑g : SL(2, ℝ)))
    cofinite (cocompact ℝ) :=
begin
  let mB : ℝ β†’ (matrix (fin 2) (fin 2) ℝ) := Ξ» t, of ![![t, (-(1:β„€):ℝ)], coe ∘ cd],
  have hmB : continuous mB,
  { refine continuous_matrix _,
    simp only [fin.forall_fin_two, mB, continuous_const, continuous_id', of_apply,
      cons_val_zero, cons_val_one, and_self ] },
  refine filter.tendsto.of_tendsto_comp _ (comap_cocompact_le hmB),
  let f₁ : SL(2, β„€) β†’ matrix (fin 2) (fin 2) ℝ :=
    Ξ» g, matrix.map (↑g : matrix _ _ β„€) (coe : β„€ β†’ ℝ),
  have cocompact_ℝ_to_cofinite_β„€_matrix :
    tendsto (Ξ» m : matrix (fin 2) (fin 2) β„€, matrix.map m (coe : β„€ β†’ ℝ)) cofinite (cocompact _),
  { simpa only [Coprod_cofinite, Coprod_cocompact]
      using tendsto.pi_map_Coprod (Ξ» i : fin 2, tendsto.pi_map_Coprod
        (Ξ» j : fin 2, int.tendsto_coe_cofinite)) },
  have hf₁ : tendsto f₁ cofinite (cocompact _) :=
    cocompact_ℝ_to_cofinite_β„€_matrix.comp subtype.coe_injective.tendsto_cofinite,
  have hfβ‚‚ : closed_embedding (lc_row0_extend hcd) :=
    (lc_row0_extend hcd).to_continuous_linear_equiv.to_homeomorph.closed_embedding,
  convert hfβ‚‚.tendsto_cocompact.comp (hf₁.comp subtype.coe_injective.tendsto_cofinite) using 1,
  ext ⟨g, rfl⟩ i j : 3,
  fin_cases i; [fin_cases j, skip],
  -- the following are proved by `simp`, but it is replaced by `simp only` to avoid timeouts.
  { simp only [mB, mul_vec, dot_product, fin.sum_univ_two, _root_.coe_coe, coe_matrix_coe,
      int.coe_cast_ring_hom, lc_row0_apply, function.comp_app, cons_val_zero, lc_row0_extend_apply,
      linear_map.general_linear_group.coe_fn_general_linear_equiv,
      general_linear_group.to_linear_apply, coe_plane_conformal_matrix, neg_neg, mul_vec_lin_apply,
      cons_val_one, head_cons, of_apply] },
  { convert congr_arg (Ξ» n : β„€, (-n:ℝ)) g.det_coe.symm using 1,
    simp only [f₁, mul_vec, dot_product, fin.sum_univ_two, matrix.det_fin_two, function.comp_app,
      subtype.coe_mk, lc_row0_extend_apply, cons_val_zero,
      linear_map.general_linear_group.coe_fn_general_linear_equiv,
      general_linear_group.to_linear_apply, coe_plane_conformal_matrix, mul_vec_lin_apply,
      cons_val_one, head_cons, map_apply, neg_mul, int.cast_sub, int.cast_mul, neg_sub, of_apply],
    ring },
  { refl }
end

/-- This replaces `(gβ€’z).re = a/c + *` in the standard theory with the following novel identity:
  `g β€’ z = (a c + b d) / (c^2 + d^2) + (d z - c) / ((c^2 + d^2) (c z + d))`
  which does not need to be decomposed depending on whether `c = 0`. -/
lemma smul_eq_lc_row0_add {p : fin 2 β†’ β„€} (hp : is_coprime (p 0) (p 1)) (hg : β†‘β‚˜g 1 = p) :
  ↑(g β€’ z) = ((lc_row0 p ↑(g : SL(2, ℝ))) : β„‚) / (p 0 ^ 2 + p 1 ^ 2)
    + ((p 1 : β„‚) * z - p 0) / ((p 0 ^ 2 + p 1 ^ 2) * (p 0 * z + p 1)) :=
begin
  have nonZ1 : (p 0 : β„‚) ^ 2 + (p 1) ^ 2 β‰  0 := by exact_mod_cast hp.sq_add_sq_ne_zero,
  have : (coe : β„€ β†’ ℝ) ∘ p β‰  0 := Ξ» h, hp.ne_zero (by ext i; simpa using congr_fun h i),
  have nonZ2 : (p 0 : β„‚) * z + p 1 β‰  0 := by simpa using linear_ne_zero _ z this,
  field_simp [nonZ1, nonZ2, denom_ne_zero, -upper_half_plane.denom, -denom_apply],
  rw (by simp : (p 1 : β„‚) * z - p 0 = ((p 1) * z - p 0) * ↑(det (↑g : matrix (fin 2) (fin 2) β„€))),
  rw [←hg, det_fin_two],
  simp only [int.coe_cast_ring_hom, coe_matrix_coe, int.cast_mul, of_real_int_cast, map_apply,
  denom, int.cast_sub, _root_.coe_coe,coe_GL_pos_coe_GL_coe_matrix],
  ring,
end

lemma tendsto_abs_re_smul {p : fin 2 β†’ β„€} (hp : is_coprime (p 0) (p 1)) :
  tendsto (Ξ» g : {g : SL(2, β„€) // β†‘β‚˜g 1 = p}, |((g : SL(2, β„€)) β€’ z).re|)
    cofinite at_top :=
begin
  suffices : tendsto (Ξ» g : (Ξ» g : SL(2, β„€), β†‘β‚˜g 1) ⁻¹' {p}, (((g : SL(2, β„€)) β€’ z).re))
    cofinite (cocompact ℝ),
  { exact tendsto_norm_cocompact_at_top.comp this },
  have : ((p 0 : ℝ) ^ 2 + p 1 ^ 2)⁻¹ β‰  0,
  { apply inv_ne_zero,
    exact_mod_cast hp.sq_add_sq_ne_zero },
  let f := homeomorph.mul_rightβ‚€ _ this,
  let ff := homeomorph.add_right (((p 1:β„‚)* z - p 0) / ((p 0 ^ 2 + p 1 ^ 2) * (p 0 * z + p 1))).re,
  convert ((f.trans ff).closed_embedding.tendsto_cocompact).comp (tendsto_lc_row0 hp),
  ext g,
  change ((g : SL(2, β„€)) β€’ z).re = (lc_row0 p ↑(↑g : SL(2, ℝ))) / (p 0 ^ 2 + p 1 ^ 2)
  + (((p 1:β„‚ )* z - p 0) / ((p 0 ^ 2 + p 1 ^ 2) * (p 0 * z + p 1))).re,
  exact_mod_cast (congr_arg complex.re (smul_eq_lc_row0_add z hp g.2))
end

end tendsto_lemmas

section fundamental_domain

local attribute [simp] coe_smul re_smul

/-- For `z : ℍ`, there is a `g : SL(2,β„€)` maximizing `(gβ€’z).im` -/
lemma exists_max_im :
  βˆƒ g : SL(2, β„€), βˆ€ g' : SL(2, β„€), (g' β€’ z).im ≀ (g β€’ z).im :=
begin
  classical,
  let s : set (fin 2 β†’ β„€) := {cd | is_coprime (cd 0) (cd 1)},
  have hs : s.nonempty := ⟨![1, 1], is_coprime_one_left⟩,
  obtain ⟨p, hp_coprime, hp⟩ :=
    filter.tendsto.exists_within_forall_le hs (tendsto_norm_sq_coprime_pair z),
  obtain ⟨g, -, hg⟩ := bottom_row_surj hp_coprime,
  refine ⟨g, λ g', _⟩,
  rw [special_linear_group.im_smul_eq_div_norm_sq, special_linear_group.im_smul_eq_div_norm_sq,
    div_le_div_left],
  { simpa [← hg] using hp (β†‘β‚˜g' 1) (bottom_row_coprime g') },
  { exact z.im_pos },
  { exact norm_sq_denom_pos g' z },
  { exact norm_sq_denom_pos g z },
end

/-- Given `z : ℍ` and a bottom row `(c,d)`, among the `g : SL(2,β„€)` with this bottom row, minimize
  `|(gβ€’z).re|`.  -/
lemma exists_row_one_eq_and_min_re {cd : fin 2 β†’ β„€} (hcd : is_coprime (cd 0) (cd 1)) :
  βˆƒ g : SL(2,β„€), β†‘β‚˜g 1 = cd ∧ (βˆ€ g' : SL(2,β„€), β†‘β‚˜g 1 = β†‘β‚˜g' 1 β†’
  |(g β€’ z).re| ≀ |(g' β€’ z).re|) :=
begin
  haveI : nonempty {g : SL(2, β„€) // β†‘β‚˜g 1 = cd} :=
    let ⟨x, hx⟩ := bottom_row_surj hcd in ⟨⟨x, hx.2⟩⟩,
  obtain ⟨g, hg⟩ := filter.tendsto.exists_forall_le (tendsto_abs_re_smul z hcd),
  refine ⟨g, g.2, _⟩,
  { intros g1 hg1,
    have : g1 ∈ ((Ξ» g : SL(2, β„€), β†‘β‚˜g 1) ⁻¹' {cd}),
    { rw [set.mem_preimage, set.mem_singleton_iff],
      exact eq.trans hg1.symm (set.mem_singleton_iff.mp (set.mem_preimage.mp g.2)) },
    exact hg ⟨g1, this⟩ },
end

/-- The matrix `T = [[1,1],[0,1]]` as an element of `SL(2,β„€)` -/
def T : SL(2,β„€) := ⟨!![1, 1; 0, 1], by norm_num [matrix.det_fin_two_of]⟩

/-- The matrix `S = [[0,-1],[1,0]]` as an element of `SL(2,β„€)` -/
def S : SL(2,β„€) := ⟨!![0, -1; 1, 0], by norm_num [matrix.det_fin_two_of]⟩

lemma coe_S : β†‘β‚˜S = !![0, -1; 1, 0] := rfl

lemma coe_T : β†‘β‚˜T = !![1, 1; 0, 1] := rfl

lemma coe_T_inv : β†‘β‚˜(T⁻¹) = !![1, -1; 0, 1] := by simp [coe_inv, coe_T, adjugate_fin_two]

lemma coe_T_zpow (n : β„€) : β†‘β‚˜(T ^ n) = !![1, n; 0, 1] :=
begin
  induction n using int.induction_on with n h n h,
  { rw [zpow_zero, coe_one, matrix.one_fin_two] },
  { simp_rw [zpow_add, zpow_one, coe_mul, h, coe_T, matrix.mul_fin_two],
    congrm !![_, _; _, _],
    rw [mul_one, mul_one, add_comm] },
  { simp_rw [zpow_sub, zpow_one, coe_mul, h, coe_T_inv, matrix.mul_fin_two],
    congrm !![_, _; _, _]; ring },
end

@[simp] lemma T_pow_mul_apply_one (n : β„€) (g : SL(2, β„€)) : β†‘β‚˜(T ^ n * g) 1 = β†‘β‚˜g 1 :=
by simp [coe_T_zpow, matrix.mul, matrix.dot_product, fin.sum_univ_succ]

@[simp] lemma T_mul_apply_one (g : SL(2, β„€)) : β†‘β‚˜(T * g) 1 = β†‘β‚˜g 1 :=
by simpa using T_pow_mul_apply_one 1 g

@[simp] lemma T_inv_mul_apply_one (g : SL(2, β„€)) : β†‘β‚˜(T⁻¹ * g) 1 = β†‘β‚˜g 1 :=
by simpa using T_pow_mul_apply_one (-1) g

lemma coe_T_zpow_smul_eq {n : β„€} : (↑((T^n) β€’ z) : β„‚) = z + n :=
by simp [coe_T_zpow]

lemma re_T_zpow_smul (n : β„€) : ((T^n) β€’ z).re = z.re + n :=
by rw [←coe_re, coe_T_zpow_smul_eq, add_re, int_cast_re, coe_re]

lemma im_T_zpow_smul (n : β„€) : ((T^n) β€’ z).im = z.im :=
by rw [←coe_im, coe_T_zpow_smul_eq, add_im, int_cast_im, add_zero, coe_im]

lemma re_T_smul : (T β€’ z).re = z.re + 1 := by simpa using re_T_zpow_smul z 1
lemma im_T_smul : (T β€’ z).im = z.im := by simpa using im_T_zpow_smul z 1
lemma re_T_inv_smul : (T⁻¹ β€’ z).re = z.re - 1 := by simpa using re_T_zpow_smul z (-1)
lemma im_T_inv_smul : (T⁻¹ β€’ z).im = z.im := by simpa using im_T_zpow_smul z (-1)

variables {z}

-- If instead we had `g` and `T` of type `PSL(2, β„€)`, then we could simply state `g = T^n`.
lemma exists_eq_T_zpow_of_c_eq_zero (hc : β†‘β‚˜g 1 0 = 0) :
  βˆƒ (n : β„€), βˆ€ (z : ℍ), g β€’ z = T^n β€’ z :=
begin
  have had := g.det_coe,
  replace had : β†‘β‚˜g 0 0 * β†‘β‚˜g 1 1 = 1, { rw [det_fin_two, hc] at had, linarith, },
  rcases int.eq_one_or_neg_one_of_mul_eq_one' had with ⟨ha, hd⟩ | ⟨ha, hd⟩,
  { use β†‘β‚˜g 0 1,
    suffices : g = T^(β†‘β‚˜g 0 1), { intros z, conv_lhs { rw this, }, },
    ext i j, fin_cases i; fin_cases j;
    simp [ha, hc, hd, coe_T_zpow], },
  { use -β†‘β‚˜g 0 1,
    suffices : g = -T^(-β†‘β‚˜g 0 1), { intros z, conv_lhs { rw [this, SL_neg_smul], }, },
    ext i j, fin_cases i; fin_cases j;
    simp [ha, hc, hd, coe_T_zpow], },
end

/- If `c = 1`, then `g` factorises into a product terms involving only `T` and `S`. -/
lemma g_eq_of_c_eq_one (hc : β†‘β‚˜g 1 0 = 1) :
  g = T^(β†‘β‚˜g 0 0) * S * T^(β†‘β‚˜g 1 1) :=
begin
  have hg := g.det_coe.symm,
  replace hg : β†‘β‚˜g 0 1 = β†‘β‚˜g 0 0 * β†‘β‚˜g 1 1 - 1, { rw [det_fin_two, hc] at hg, linarith, },
  refine subtype.ext _,
  conv_lhs { rw matrix.eta_fin_two β†‘β‚˜g },
  rw [hc, hg],
  simp only [coe_mul, coe_T_zpow, coe_S, mul_fin_two],
  congrm !![_, _; _, _]; ring
end

/-- If `1 < |z|`, then `|S β€’ z| < 1`. -/
lemma norm_sq_S_smul_lt_one (h: 1 < norm_sq z) : norm_sq ↑(S β€’ z) < 1 :=
by simpa [coe_S] using (inv_lt_inv z.norm_sq_pos zero_lt_one).mpr h

/-- If `|z| < 1`, then applying `S` strictly decreases `im`. -/
lemma im_lt_im_S_smul (h: norm_sq z < 1) : z.im < (S β€’ z).im :=
begin
  have : z.im < z.im / norm_sq (z:β„‚),
  { have imz : 0 < z.im := im_pos z,
    apply (lt_div_iff z.norm_sq_pos).mpr,
    nlinarith },
  convert this,
  simp only [special_linear_group.im_smul_eq_div_norm_sq],
  field_simp [norm_sq_denom_ne_zero, norm_sq_ne_zero, S]
end

/-- The standard (closed) fundamental domain of the action of `SL(2,β„€)` on `ℍ`. -/
def fd : set ℍ :=
{z | 1 ≀ (z : β„‚).norm_sq ∧ |z.re| ≀ (1 : ℝ) / 2}

/-- The standard open fundamental domain of the action of `SL(2,β„€)` on `ℍ`. -/
def fdo : set ℍ :=
{z | 1 < (z : β„‚).norm_sq ∧ |z.re| < (1 : ℝ) / 2}

localized "notation `π’Ÿ` := modular_group.fd" in modular

localized "notation `π’Ÿα΅’` := modular_group.fdo" in modular

lemma abs_two_mul_re_lt_one_of_mem_fdo (h : z ∈ π’Ÿα΅’) : |2 * z.re| < 1 :=
begin
  rw [abs_mul, abs_two, ← lt_div_iff' (@two_pos ℝ _ _)],
  exact h.2,
end

lemma three_lt_four_mul_im_sq_of_mem_fdo (h : z ∈ π’Ÿα΅’) : 3 < 4 * z.im^2 :=
begin
  have : 1 < z.re * z.re + z.im * z.im := by simpa [complex.norm_sq_apply] using h.1,
  have := h.2,
  cases abs_cases z.re;
  nlinarith,
end

/-- If `z ∈ π’Ÿα΅’`, and `n : β„€`, then `|z + n| > 1`. -/
lemma one_lt_norm_sq_T_zpow_smul (hz : z ∈ π’Ÿα΅’) (n : β„€) : 1 < norm_sq (((T^n) β€’ z) : ℍ) :=
begin
  have hz₁ : 1 < z.re * z.re + z.im * z.im := hz.1,
  have hzn := int.nneg_mul_add_sq_of_abs_le_one n (abs_two_mul_re_lt_one_of_mem_fdo hz).le,
  have : 1 < (z.re + ↑n) * (z.re + ↑n) + z.im * z.im, { linarith, },
  simpa [coe_T_zpow, norm_sq],
end

lemma eq_zero_of_mem_fdo_of_T_zpow_mem_fdo {n : β„€} (hz : z ∈ π’Ÿα΅’) (hg : (T^n) β€’ z ∈ π’Ÿα΅’) : n = 0 :=
begin
  suffices : |(n : ℝ)| < 1,
  { rwa [← int.cast_abs, ← int.cast_one, int.cast_lt, int.abs_lt_one_iff] at this, },
  have h₁ := hz.2,
  have hβ‚‚ := hg.2,
  rw [re_T_zpow_smul] at hβ‚‚,
  calc |(n : ℝ)| ≀ |z.re| + |z.re + (n : ℝ)| : abs_add' (n : ℝ) z.re
             ... < 1/2 + 1/2 : add_lt_add h₁ hβ‚‚
             ... = 1 : add_halves 1,
end

/-- Any `z : ℍ` can be moved to `π’Ÿ` by an element of `SL(2,β„€)`  -/
lemma exists_smul_mem_fd (z : ℍ) : βˆƒ g : SL(2,β„€), g β€’ z ∈ π’Ÿ :=
begin
  -- obtain a gβ‚€ which maximizes im (g β€’ z),
  obtain ⟨gβ‚€, hgβ‚€βŸ© := exists_max_im z,
  -- then among those, minimize re
  obtain ⟨g, hg, hg'⟩ := exists_row_one_eq_and_min_re z (bottom_row_coprime gβ‚€),
  refine ⟨g, _⟩,
  -- `g` has same max im property as `gβ‚€`
  have hgβ‚€' : βˆ€ (g' : SL(2,β„€)), (g' β€’ z).im ≀ (g β€’ z).im,
  { have hg'' : (g β€’ z).im = (gβ‚€ β€’ z).im,
    { rw [special_linear_group.im_smul_eq_div_norm_sq, special_linear_group.im_smul_eq_div_norm_sq,
      denom_apply, denom_apply, hg]},
    simpa only [hg''] using hgβ‚€ },
  split,
  { -- Claim: `1 ≀ ⇑norm_sq ↑(g β€’ z)`. If not, then `Sβ€’gβ€’z` has larger imaginary part
    contrapose! hgβ‚€',
    refine ⟨S * g, _⟩,
    rw mul_smul,
    exact im_lt_im_S_smul hgβ‚€' },
  { show |(g β€’ z).re| ≀ 1 / 2, -- if not, then either `T` or `T'` decrease |Re|.
    rw abs_le,
    split,
    { contrapose! hg',
      refine ⟨T * g, (T_mul_apply_one _).symm, _⟩,
      rw [mul_smul, re_T_smul],
      cases abs_cases ((g β€’ z).re + 1); cases abs_cases (g β€’ z).re; linarith },
    { contrapose! hg',
      refine ⟨T⁻¹ * g, (T_inv_mul_apply_one _).symm, _⟩,
      rw [mul_smul, re_T_inv_smul],
      cases abs_cases ((g β€’ z).re - 1); cases abs_cases (g β€’ z).re; linarith } }
end

section unique_representative

variables {z}

/-- An auxiliary result en route to `modular_group.c_eq_zero`. -/
lemma abs_c_le_one (hz : z ∈ π’Ÿα΅’) (hg : g β€’ z ∈ π’Ÿα΅’) : |β†‘β‚˜g 1 0| ≀ 1 :=
begin
  let c' : β„€ := β†‘β‚˜g 1 0,
  let c : ℝ := (c' : ℝ),
  suffices : 3 * c^2 < 4,
  { rw [← int.cast_pow, ← int.cast_three, ← int.cast_four, ← int.cast_mul, int.cast_lt] at this,
    replace this : c' ^ 2 ≀ 1 ^ 2, { linarith, },
    rwa [sq_le_sq, abs_one] at this },
  suffices : c β‰  0 β†’ 9 * c^4 < 16,
  { rcases eq_or_ne c 0 with hc | hc,
    { rw hc, norm_num, },
    { refine (abs_lt_of_sq_lt_sq' _ (by norm_num)).2,
      specialize this hc,
      linarith, }, },
  intros hc,
  replace hc : 0 < c^4, { rw pow_bit0_pos_iff; trivial, },
  have h₁ := mul_lt_mul_of_pos_right (mul_lt_mul'' (three_lt_four_mul_im_sq_of_mem_fdo hg)
      (three_lt_four_mul_im_sq_of_mem_fdo hz) (by linarith) (by linarith)) hc,
  have hβ‚‚ : (c * z.im) ^ 4 / norm_sq (denom ↑g z) ^ 2 ≀ 1 :=
    div_le_one_of_le (pow_four_le_pow_two_of_pow_two_le
      (upper_half_plane.c_mul_im_sq_le_norm_sq_denom z g)) (sq_nonneg _),
  let nsq := norm_sq (denom g z),
  calc 9 * c^4 < c^4 * z.im^2 * (g β€’ z).im^2 * 16 : by linarith
           ... = c^4 * z.im^4 / nsq^2 * 16 : by { rw [special_linear_group.im_smul_eq_div_norm_sq,
            div_pow], ring, }
           ... ≀ 16 : by { rw ← mul_pow, linarith, },
end

/-- An auxiliary result en route to `modular_group.eq_smul_self_of_mem_fdo_mem_fdo`. -/
lemma c_eq_zero (hz : z ∈ π’Ÿα΅’) (hg : g β€’ z ∈ π’Ÿα΅’) : β†‘β‚˜g 1 0 = 0 :=
begin
  have hp : βˆ€ {g' : SL(2, β„€)} (hg' : g' β€’ z ∈ π’Ÿα΅’), β†‘β‚˜g' 1 0 β‰  1,
  { intros,
    by_contra hc,
    let a := β†‘β‚˜g' 0 0,
    let d := β†‘β‚˜g' 1 1,
    have had : T^(-a) * g' = S * T^d, { rw g_eq_of_c_eq_one hc, group, },
    let w := T^(-a) β€’ (g' β€’ z),
    have h₁ : w = S β€’ (T^d β€’ z), { simp only [w, ← mul_smul, had], },
    replace h₁ : norm_sq w < 1 := h₁.symm β–Έ norm_sq_S_smul_lt_one (one_lt_norm_sq_T_zpow_smul hz d),
    have hβ‚‚ : 1 < norm_sq w := one_lt_norm_sq_T_zpow_smul hg' (-a),
    linarith, },
  have hn : β†‘β‚˜g 1 0 β‰  -1,
  { intros hc,
    replace hc : β†‘β‚˜(-g) 1 0 = 1, { simp [eq_neg_of_eq_neg hc], },
    replace hg : (-g) β€’ z ∈ π’Ÿα΅’ := (SL_neg_smul g z).symm β–Έ hg,
    exact hp hg hc, },
  specialize hp hg,
  rcases (int.abs_le_one_iff.mp $ abs_c_le_one hz hg);
  tauto,
end

/-- Second Main Fundamental Domain Lemma: if both `z` and `g β€’ z` are in the open domain `π’Ÿα΅’`,
where `z : ℍ` and `g : SL(2,β„€)`, then `z = g β€’ z`. -/
lemma eq_smul_self_of_mem_fdo_mem_fdo (hz : z ∈ π’Ÿα΅’) (hg : g β€’ z ∈ π’Ÿα΅’) : z = g β€’ z :=
begin
  obtain ⟨n, hn⟩ := exists_eq_T_zpow_of_c_eq_zero (c_eq_zero hz hg),
  rw hn at hg ⊒,
  simp [eq_zero_of_mem_fdo_of_T_zpow_mem_fdo hz hg, one_smul],
end

end unique_representative

end fundamental_domain

end modular_group