-- Mathlib/Topology/Instances/AddCircle.lean
theorem addOrderOf_eq_pos_iff {u : AddCircle p} {n : ℕ} (h : 0 < n) :
    addOrderOf u = n ↔ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = u

Proof state:
case refine_2.intro.refine_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
han : ↑(a % ↑n).toNat = a % ↑n
he : ↑(↑(a % ↑n).toNat / ↑n * p) = ↑k
this : (a % ↑n).toNat.gcd n * addOrderOf ↑k = addOrderOf ↑k
⊢ 0 < addOrderOf ↑k

Tactic:
rwa [hk]

Result:
no goals

Proof state:
case refine_2.intro
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k

Tactic:
have han : _ = a % n := Int.toNat_of_nonneg (Int.emod_nonneg _ <| mod_cast h.ne')

Result:
case refine_2.intro
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
han : ↑(a % ↑n).toNat = a % ↑n
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k

Proof state:
case h.e'_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
han : ↑(a % ↑n).toNat = a % ↑n
⊢ ↑(↑(a % ↑n).toNat / ↑n * p) = ↑(↑(a / ↑n) * p + (a % ↑n) • p / ↑n)

Tactic:
rw [coe_add, ← Int.cast_natCast, han, zsmul_eq_mul, mul_div_right_comm, eq_comm,
  add_left_eq_self, ← zsmul_eq_mul, coe_zsmul, coe_period, smul_zero]

Result:
no goals

Proof state:
case refine_2.intro.refine_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
han : ↑(a % ↑n).toNat = a % ↑n
he : ↑(↑(a % ↑n).toNat / ↑n * p) = ↑k
⊢ (a % ↑n).toNat.gcd n = 1

Tactic:
have := (gcd_mul_addOrderOf_div_eq p (Int.toNat (a % ↑n)) h).trans
  ((congr_arg addOrderOf he).trans hk).symm

Result:
case refine_2.intro.refine_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
han : ↑(a % ↑n).toNat = a % ↑n
he : ↑(↑(a % ↑n).toNat / ↑n * p) = ↑k
this : (a % ↑n).toNat.gcd n * addOrderOf ↑(↑(a % ↑n).toNat / ↑n * p) = addOrderOf ↑(↑(a % ↑n).toNat / ↑n * p)
⊢ (a % ↑n).toNat.gcd n = 1

Proof state:
case refine_2.intro
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : a • p = n • k
h0 : ↑n ≠ 0
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k

Tactic:
rw [nsmul_eq_mul, mul_comm, ← div_eq_iff h0, ← a.ediv_add_emod' n, add_smul, add_div,
  zsmul_eq_mul, Int.cast_mul, Int.cast_natCast, mul_assoc, ← mul_div, mul_comm _ p,
  mul_div_cancel_right₀ p h0] at ha

Result:
case refine_2.intro
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
a : ℤ
ha : ↑(a / ↑n) * p + (a % ↑n) • p / ↑n = k
h0 : ↑n ≠ 0
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k

Proof state:
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
⊢ addOrderOf u = n ↔ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = u

Tactic:
refine ⟨QuotientAddGroup.induction_on u fun k hk => ?_, ?_⟩

Result:
case refine_1
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
⊢ (∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = u) → addOrderOf u = n

case refine_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k

Proof state:
case refine_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k

Tactic:
have h0 := addOrderOf_nsmul_eq_zero (k : AddCircle p)

Result:
case refine_2
𝕜 : Type u_1
inst✝ : LinearOrderedField 𝕜
p : 𝕜
hp : Fact (0 < p)
u : AddCircle p
n : ℕ
h : 0 < n
k : 𝕜
hk : addOrderOf ↑k = n
h0 : addOrderOf ↑k • ↑k = 0
⊢ ∃ m < n, m.gcd n = 1 ∧ ↑(↑m / ↑n * p) = ↑k