l3lab/ntp-mathlib huggingface dataset:
theorem neg_eq_self_iff {n : ℕ} (a : ZMod n) : -a = a ↔ a = 0 ∨ 2 * a.val = n := by
  rw [neg_eq_iff_add_eq_zero, ← two_mul]
  cases n
  · erw [@mul_eq_zero ℤ, @mul_eq_zero ℕ, val_eq_zero]
    exact
      ⟨fun h => h.elim (by simp) Or.inl, fun h =>
        Or.inr (h.elim id fun h => h.elim (by simp) id)⟩
  conv_lhs =>
    rw [← a.nat_cast_zmod_val, ← Nat.cast_two, ← Nat.cast_mul, nat_cast_zmod_eq_zero_iff_dvd]
  constructor
  · rintro ⟨m, he⟩
    cases' m with m
    · erw [mul_zero, mul_eq_zero] at he
      rcases he with (⟨⟨⟩⟩ | he)
      exact Or.inl (a.val_eq_zero.1 he)
    cases m
    · right
      rwa [show Nat.succ Nat.zero = 1 from rfl, mul_one] at he
    refine' (a.val_lt.not_le <| Nat.le_of_mul_le_mul_left _ zero_lt_two).elim
    rw [he, mul_comm]
    apply Nat.mul_le_mul_left
    erw [Nat.succ_le_succ_iff, Nat.succ_le_succ_iff]; simp
  · rintro (rfl | h)
    · 

Proof state:
case succ.mpr.inl
n✝ : ℕ
⊢ Nat.succ n✝ ∣ 2 * val 0

Next tactic:
rw [val_zero, mul_zero]
theorem neg_eq_self_iff {n : ℕ} (a : ZMod n) : -a = a ↔ a = 0 ∨ 2 * a.val = n := by
  rw [neg_eq_iff_add_eq_zero, ← two_mul]
  cases n
  · erw [@mul_eq_zero ℤ, @mul_eq_zero ℕ, val_eq_zero]
    exact
      ⟨fun h => h.elim (by simp) Or.inl, fun h =>
        Or.inr (h.elim id fun h => h.elim (by simp) id)⟩
  conv_lhs =>
    rw [← a.nat_cast_zmod_val, ← Nat.cast_two, ← Nat.cast_mul, nat_cast_zmod_eq_zero_iff_dvd]
  constructor
  · rintro ⟨m, he⟩
    cases' m with m
    · erw [mul_zero, mul_eq_zero] at he
      rcases he with (⟨⟨⟩⟩ | he)
      exact Or.inl (a.val_eq_zero.1 he)
    cases m
    · right
      rwa [show Nat.succ Nat.zero = 1 from rfl, mul_one] at he
    refine' (a.val_lt.not_le <| Nat.le_of_mul_le_mul_left _ zero_lt_two).elim
    rw [he, mul_comm]
    apply Nat.mul_le_mul_left
    erw [Nat.succ_le_succ_iff, Nat.succ_le_succ_iff]; simp
  · rintro (rfl | h)
    · rw [val_zero, mul_zero]
      

Proof state:
case succ.mpr.inl
n✝ : ℕ
⊢ Nat.succ n✝ ∣ 0

Next tactic:
apply dvd_zero
theorem neg_eq_self_iff {n : ℕ} (a : ZMod n) : -a = a ↔ a = 0 ∨ 2 * a.val = n := by
  rw [neg_eq_iff_add_eq_zero, ← two_mul]
  cases n
  · erw [@mul_eq_zero ℤ, @mul_eq_zero ℕ, val_eq_zero]
    exact
      ⟨fun h => h.elim (by simp) Or.inl, fun h =>
        Or.inr (h.elim id fun h => h.elim (by simp) id)⟩
  conv_lhs =>
    rw [← a.nat_cast_zmod_val, ← Nat.cast_two, ← Nat.cast_mul, nat_cast_zmod_eq_zero_iff_dvd]
  constructor
  · rintro ⟨m, he⟩
    cases' m with m
    · erw [mul_zero, mul_eq_zero] at he
      rcases he with (⟨⟨⟩⟩ | he)
      exact Or.inl (a.val_eq_zero.1 he)
    cases m
    · right
      rwa [show Nat.succ Nat.zero = 1 from rfl, mul_one] at he
    refine' (a.val_lt.not_le <| Nat.le_of_mul_le_mul_left _ zero_lt_two).elim
    rw [he, mul_comm]
    apply Nat.mul_le_mul_left
    erw [Nat.succ_le_succ_iff, Nat.succ_le_succ_iff]; simp
  · rintro (rfl | h)
    · rw [val_zero, mul_zero]
      apply dvd_zero
    · 

Proof state:
case succ.mpr.inr
n✝ : ℕ
a : ZMod (Nat.succ n✝)
h : 2 * val a = Nat.succ n✝
⊢ Nat.succ n✝ ∣ 2 * val a

Next tactic:
rw [h]
theorem val_cast_of_lt {n : ℕ} {a : ℕ} (h : a < n) : (a : ZMod n).val = a := by
  

Proof state:
n a : ℕ
h : a < n
⊢ val ↑a = a

Next tactic:
rw [val_nat_cast, Nat.mod_eq_of_lt h]
theorem neg_val' {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = (n - a.val) % n :=
  calc
    (-a).val = val (-a) % n := by 

Proof state:
n : ℕ
inst✝ : NeZero n
a : ZMod n
⊢ val (-a) = val (-a) % n

Next tactic:
rw [Nat.mod_eq_of_lt (-a).val_lt]
theorem neg_val' {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = (n - a.val) % n :=
  calc
    (-a).val = val (-a) % n := by rw [Nat.mod_eq_of_lt (-a).val_lt]
    _ = (n - val a) % n :=
      Nat.ModEq.add_right_cancel' _
        (by
          

Proof state:
n : ℕ
inst✝ : NeZero n
a : ZMod n
⊢ val (-a) + ?m.552792 a ≡ n - val a + ?m.552792 a [MOD n]

Next tactic:
rw [Nat.ModEq, ← val_add, add_left_neg, tsub_add_cancel_of_le a.val_le, Nat.mod_self,
            val_zero]
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  

Proof state:
n : ℕ
inst✝ : NeZero n
a : ZMod n
⊢ val (-a) = if a = 0 then 0 else n - val a

Next tactic:
rw [neg_val']
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  

Proof state:
n : ℕ
inst✝ : NeZero n
a : ZMod n
⊢ (n - val a) % n = if a = 0 then 0 else n - val a

Next tactic:
by_cases h : a = 0
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  by_cases h : a = 0; · 

Proof state:
case pos
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : a = 0
⊢ (n - val a) % n = if a = 0 then 0 else n - val a

Next tactic:
rw [if_pos h, h, val_zero, tsub_zero, Nat.mod_self]
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  by_cases h : a = 0; · rw [if_pos h, h, val_zero, tsub_zero, Nat.mod_self]
  

Proof state:
case neg
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : ¬a = 0
⊢ (n - val a) % n = if a = 0 then 0 else n - val a

Next tactic:
rw [if_neg h]
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  by_cases h : a = 0; · rw [if_pos h, h, val_zero, tsub_zero, Nat.mod_self]
  rw [if_neg h]
  

Proof state:
case neg
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : ¬a = 0
⊢ (n - val a) % n = n - val a

Next tactic:
apply Nat.mod_eq_of_lt
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  by_cases h : a = 0; · rw [if_pos h, h, val_zero, tsub_zero, Nat.mod_self]
  rw [if_neg h]
  apply Nat.mod_eq_of_lt
  

Proof state:
case neg.h
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : ¬a = 0
⊢ n - val a < n

Next tactic:
apply Nat.sub_lt (NeZero.pos n)
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  by_cases h : a = 0; · rw [if_pos h, h, val_zero, tsub_zero, Nat.mod_self]
  rw [if_neg h]
  apply Nat.mod_eq_of_lt
  apply Nat.sub_lt (NeZero.pos n)
  

Proof state:
case neg.h
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : ¬a = 0
⊢ 0 < val a

Next tactic:
contrapose! h
theorem neg_val {n : ℕ} [NeZero n] (a : ZMod n) : (-a).val = if a = 0 then 0 else n - a.val := by
  rw [neg_val']
  by_cases h : a = 0; · rw [if_pos h, h, val_zero, tsub_zero, Nat.mod_self]
  rw [if_neg h]
  apply Nat.mod_eq_of_lt
  apply Nat.sub_lt (NeZero.pos n)
  contrapose! h
  

Proof state:
case neg.h
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : val a ≤ 0
⊢ a = 0

Next tactic:
rwa [le_zero_iff, val_eq_zero] at h
theorem val_neg_of_ne_zero {n : ℕ} [nz : NeZero n] (a : ZMod n) [na : NeZero a] :
    (- a).val = n - a.val := by 

Proof state:
n : ℕ
nz : NeZero n
a : ZMod n
na : NeZero a
⊢ val (-a) = n - val a

Next tactic:
simp_all [neg_val a, na.out]
theorem val_sub {n : ℕ} [NeZero n] {a b : ZMod n} (h : b.val ≤ a.val) :
    (a - b).val = a.val - b.val := by
  

Proof state:
n : ℕ
inst✝ : NeZero n
a b : ZMod n
h : val b ≤ val a
⊢ val (a - b) = val a - val b

Next tactic:
by_cases hb : b = 0
theorem val_sub {n : ℕ} [NeZero n] {a b : ZMod n} (h : b.val ≤ a.val) :
    (a - b).val = a.val - b.val := by
  by_cases hb : b = 0
  · 

Proof state:
case pos
n : ℕ
inst✝ : NeZero n
a b : ZMod n
h : val b ≤ val a
hb : b = 0
⊢ val (a - b) = val a - val b

Next tactic:
cases hb
theorem val_sub {n : ℕ} [NeZero n] {a b : ZMod n} (h : b.val ≤ a.val) :
    (a - b).val = a.val - b.val := by
  by_cases hb : b = 0
  · cases hb; 

Proof state:
case pos.refl
n : ℕ
inst✝ : NeZero n
a : ZMod n
h : val 0 ≤ val a
⊢ val (a - 0) = val a - val 0

Next tactic:
simp
theorem val_sub {n : ℕ} [NeZero n] {a b : ZMod n} (h : b.val ≤ a.val) :
    (a - b).val = a.val - b.val := by
  by_cases hb : b = 0
  · cases hb; simp
  · 

Proof state:
case neg
n : ℕ
inst✝ : NeZero n
a b : ZMod n
h : val b ≤ val a
hb : ¬b = 0
⊢ val (a - b) = val a - val b

Next tactic:
have : NeZero b := ⟨hb⟩
theorem val_sub {n : ℕ} [NeZero n] {a b : ZMod n} (h : b.val ≤ a.val) :
    (a - b).val = a.val - b.val := by
  by_cases hb : b = 0
  · cases hb; simp
  · have : NeZero b := ⟨hb⟩
    

Proof state:
case neg
n : ℕ
inst✝ : NeZero n
a b : ZMod n
h : val b ≤ val a
hb : ¬b = 0
this : NeZero b
⊢ val (a - b) = val a - val b

Next tactic:
rw [sub_eq_add_neg, val_add, val_neg_of_ne_zero, ← Nat.add_sub_assoc (le_of_lt (val_lt _)),
      add_comm, Nat.add_sub_assoc h, Nat.add_mod_left]
theorem val_sub {n : ℕ} [NeZero n] {a b : ZMod n} (h : b.val ≤ a.val) :
    (a - b).val = a.val - b.val := by
  by_cases hb : b = 0
  · cases hb; simp
  · have : NeZero b := ⟨hb⟩
    rw [sub_eq_add_neg, val_add, val_neg_of_ne_zero, ← Nat.add_sub_assoc (le_of_lt (val_lt _)),
      add_comm, Nat.add_sub_assoc h, Nat.add_mod_left]
    

Proof state:
case neg
n : ℕ
inst✝ : NeZero n
a b : ZMod n
h : val b ≤ val a
hb : ¬b = 0
this : NeZero b
⊢ (val a - val b) % n = val a - val b

Next tactic:
apply Nat.mod_eq_of_lt (tsub_lt_of_lt (val_lt _))
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  

Proof state:
m n : ℕ
nzm : NeZero m
a : ZMod m
h : val a < n
⊢ val ↑a = val a

Next tactic:
have nzn : NeZero n := by constructor; rintro rfl; simp at h
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by 

Proof state:
m n : ℕ
nzm : NeZero m
a : ZMod m
h : val a < n
⊢ NeZero n

Next tactic:
constructor
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; 

Proof state:
case out
m n : ℕ
nzm : NeZero m
a : ZMod m
h : val a < n
⊢ n ≠ 0

Next tactic:
rintro rfl
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; 

Proof state:
case out
m : ℕ
nzm : NeZero m
a : ZMod m
h : val a < 0
⊢ False

Next tactic:
simp at h
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  

Proof state:
m n : ℕ
nzm : NeZero m
a : ZMod m
h : val a < n
nzn : NeZero n
⊢ val ↑a = val a

Next tactic:
cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    cases n with
    | zero => cases nzn; simp_all
    | succ n => exact Fin.val_cast_of_lt h
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  

Proof state:
case zero
n : ℕ
nzn : NeZero n
nzm : NeZero Nat.zero
a : ZMod Nat.zero
h : val a < n
⊢ val ↑a = val a

Next tactic:
| zero => cases nzm; simp_all
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => 

Proof state:
case zero
n : ℕ
nzn : NeZero n
nzm : NeZero Nat.zero
a : ZMod Nat.zero
h : val a < n
⊢ val ↑a = val a

Next tactic:
cases nzm
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; 

Proof state:
case zero.mk
n : ℕ
nzn : NeZero n
a : ZMod Nat.zero
h : val a < n
out✝ : Nat.zero ≠ 0
⊢ val ↑a = val a

Next tactic:
simp_all
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  

Proof state:
case succ
n : ℕ
nzn : NeZero n
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
h : val a < n
⊢ val ↑a = val a

Next tactic:
| succ m =>
    cases n with
    | zero => cases nzn; simp_all
    | succ n => exact Fin.val_cast_of_lt h
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    

Proof state:
case succ
n : ℕ
nzn : NeZero n
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
h : val a < n
⊢ val ↑a = val a

Next tactic:
cases n with
    | zero => cases nzn; simp_all
    | succ n => exact Fin.val_cast_of_lt h
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    cases n with
    

Proof state:
case succ.zero
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
nzn : NeZero Nat.zero
h : val a < Nat.zero
⊢ val ↑a = val a

Next tactic:
| zero => cases nzn; simp_all
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    cases n with
    | zero => 

Proof state:
case succ.zero
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
nzn : NeZero Nat.zero
h : val a < Nat.zero
⊢ val ↑a = val a

Next tactic:
cases nzn
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    cases n with
    | zero => cases nzn; 

Proof state:
case succ.zero.mk
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
h : val a < Nat.zero
out✝ : Nat.zero ≠ 0
⊢ val ↑a = val a

Next tactic:
simp_all
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    cases n with
    | zero => cases nzn; simp_all
    

Proof state:
case succ.succ
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
n : ℕ
nzn : NeZero (Nat.succ n)
h : val a < Nat.succ n
⊢ val ↑a = val a

Next tactic:
| succ n => exact Fin.val_cast_of_lt h
theorem val_cast_eq_val_of_lt {m n : ℕ} [nzm : NeZero m] {a : ZMod m}
    (h : a.val < n) : (a.cast : ZMod n).val = a.val := by
  have nzn : NeZero n := by constructor; rintro rfl; simp at h
  cases m with
  | zero => cases nzm; simp_all
  | succ m =>
    cases n with
    | zero => cases nzn; simp_all
    | succ n => 

Proof state:
case succ.succ
m : ℕ
nzm : NeZero (Nat.succ m)
a : ZMod (Nat.succ m)
n : ℕ
nzn : NeZero (Nat.succ n)
h : val a < Nat.succ n
⊢ val ↑a = val a

Next tactic:
exact Fin.val_cast_of_lt h
theorem valMinAbs_def_pos {n : ℕ} [NeZero n] (x : ZMod n) :
    valMinAbs x = if x.val ≤ n / 2 then (x.val : ℤ) else x.val - n := by
  

Proof state:
n : ℕ
inst✝ : NeZero n
x : ZMod n
⊢ valMinAbs x = if val x ≤ n / 2 then ↑(val x) else ↑(val x) - ↑n

Next tactic:
cases n
theorem valMinAbs_def_pos {n : ℕ} [NeZero n] (x : ZMod n) :
    valMinAbs x = if x.val ≤ n / 2 then (x.val : ℤ) else x.val - n := by
  cases n
  · 

Proof state:
case zero
inst✝ : NeZero Nat.zero
x : ZMod Nat.zero
⊢ valMinAbs x = if val x ≤ Nat.zero / 2 then ↑(val x) else ↑(val x) - ↑Nat.zero

Next tactic:
cases NeZero.ne 0 rfl
theorem valMinAbs_def_pos {n : ℕ} [NeZero n] (x : ZMod n) :
    valMinAbs x = if x.val ≤ n / 2 then (x.val : ℤ) else x.val - n := by
  cases n
  · cases NeZero.ne 0 rfl
  · 

Proof state:
case succ
n✝ : ℕ
inst✝ : NeZero (Nat.succ n✝)
x : ZMod (Nat.succ n✝)
⊢ valMinAbs x = if val x ≤ Nat.succ n✝ / 2 then ↑(val x) else ↑(val x) - ↑(Nat.succ n✝)

Next tactic:
rfl
@[simp, norm_cast]
theorem coe_valMinAbs : ∀ {n : ℕ} (x : ZMod n), (x.valMinAbs : ZMod n) = x
  | 0, x => Int.cast_id
  | k@(n + 1), x => by
    

Proof state:
k n : ℕ
h✝ : k = n + 1
x : ZMod (namedPattern k (n + 1) h✝)
⊢ ↑(valMinAbs x) = x

Next tactic:
rw [valMinAbs_def_pos]
@[simp, norm_cast]
theorem coe_valMinAbs : ∀ {n : ℕ} (x : ZMod n), (x.valMinAbs : ZMod n) = x
  | 0, x => Int.cast_id
  | k@(n + 1), x => by
    rw [valMinAbs_def_pos]
    

Proof state:
k n : ℕ
h✝ : k = n + 1
x : ZMod (namedPattern k (n + 1) h✝)
⊢ ↑(if val x ≤ namedPattern k (n + 1) h✝ / 2 then ↑(val x) else ↑(val x) - ↑(namedPattern k (n + 1) h✝)) = x

Next tactic:
split_ifs
@[simp, norm_cast]
theorem coe_valMinAbs : ∀ {n : ℕ} (x : ZMod n), (x.valMinAbs : ZMod n) = x
  | 0, x => Int.cast_id
  | k@(n + 1), x => by
    rw [valMinAbs_def_pos]
    split_ifs
    · 

Proof state:
case pos
k n : ℕ
h✝¹ : k = n + 1
x : ZMod (namedPattern k (n + 1) h✝¹)
h✝ : val x ≤ namedPattern k (n + 1) h✝¹ / 2
⊢ ↑↑(val x) = x

Next tactic:
rw [Int.cast_ofNat, nat_cast_zmod_val]
@[simp, norm_cast]
theorem coe_valMinAbs : ∀ {n : ℕ} (x : ZMod n), (x.valMinAbs : ZMod n) = x
  | 0, x => Int.cast_id
  | k@(n + 1), x => by
    rw [valMinAbs_def_pos]
    split_ifs
    · rw [Int.cast_ofNat, nat_cast_zmod_val]
    · 

Proof state:
case neg
k n : ℕ
h✝¹ : k = n + 1
x : ZMod (namedPattern k (n + 1) h✝¹)
h✝ : ¬val x ≤ namedPattern k (n + 1) h✝¹ / 2
⊢ ↑(↑(val x) - ↑(namedPattern k (n + 1) h✝¹)) = x

Next tactic:
rw [Int.cast_sub, Int.cast_ofNat, nat_cast_zmod_val, Int.cast_ofNat, nat_cast_self, sub_zero]
theorem _root_.Nat.le_div_two_iff_mul_two_le {n m : ℕ} : m ≤ n / 2 ↔ (m : ℤ) * 2 ≤ n := by
  

Proof state:
n m : ℕ
⊢ m ≤ n / 2 ↔ ↑m * 2 ≤ ↑n

Next tactic:
rw [Nat.le_div_iff_mul_le zero_lt_two, ← Int.ofNat_le, Int.ofNat_mul, Nat.cast_two]
theorem valMinAbs_nonneg_iff {n : ℕ} [NeZero n] (x : ZMod n) : 0 ≤ x.valMinAbs ↔ x.val ≤ n / 2 := by
  

Proof state:
n : ℕ
inst✝ : NeZero n
x : ZMod n
⊢ 0 ≤ valMinAbs x ↔ val x ≤ n / 2

Next tactic:
rw [valMinAbs_def_pos]
theorem valMinAbs_nonneg_iff {n : ℕ} [NeZero n] (x : ZMod n) : 0 ≤ x.valMinAbs ↔ x.val ≤ n / 2 := by
  rw [valMinAbs_def_pos]; 

Proof state:
n : ℕ
inst✝ : NeZero n
x : ZMod n
⊢ (0 ≤ if val x ≤ n / 2 then ↑(val x) else ↑(val x) - ↑n) ↔ val x ≤ n / 2

Next tactic:
split_ifs with h
theorem valMinAbs_nonneg_iff {n : ℕ} [NeZero n] (x : ZMod n) : 0 ≤ x.valMinAbs ↔ x.val ≤ n / 2 := by
  rw [valMinAbs_def_pos]; split_ifs with h
  · 

Proof state:
case pos
n : ℕ
inst✝ : NeZero n
x : ZMod n
h : val x ≤ n / 2
⊢ 0 ≤ ↑(val x) ↔ val x ≤ n / 2

Next tactic:
exact iff_of_true (Nat.cast_nonneg _) h
theorem valMinAbs_nonneg_iff {n : ℕ} [NeZero n] (x : ZMod n) : 0 ≤ x.valMinAbs ↔ x.val ≤ n / 2 := by
  rw [valMinAbs_def_pos]; split_ifs with h
  · exact iff_of_true (Nat.cast_nonneg _) h
  · 

Proof state:
case neg
n : ℕ
inst✝ : NeZero n
x : ZMod n
h : ¬val x ≤ n / 2
⊢ 0 ≤ ↑(val x) - ↑n ↔ val x ≤ n / 2

Next tactic:
exact iff_of_false (sub_lt_zero.2 <| Int.ofNat_lt.2 x.val_lt).not_le h
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  

Proof state:
n : ℕ
a : ZMod n
⊢ valMinAbs a * 2 = ↑n ↔ 2 * val a = n

Next tactic:
cases' n with n
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · 

Proof state:
case zero
a : ZMod Nat.zero
⊢ valMinAbs a * 2 = ↑Nat.zero ↔ 2 * val a = Nat.zero

Next tactic:
simp
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  

Proof state:
case succ
n : ℕ
a : ZMod (Nat.succ n)
⊢ valMinAbs a * 2 = ↑(Nat.succ n) ↔ 2 * val a = Nat.succ n

Next tactic:
by_cases h : a.val ≤ n.succ / 2
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · 

Proof state:
case pos
n : ℕ
a : ZMod (Nat.succ n)
h : val a ≤ Nat.succ n / 2
⊢ valMinAbs a * 2 = ↑(Nat.succ n) ↔ 2 * val a = Nat.succ n

Next tactic:
dsimp [valMinAbs]
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    

Proof state:
case pos
n : ℕ
a : ZMod (Nat.succ n)
h : val a ≤ Nat.succ n / 2
⊢ (if val a ≤ Nat.succ n / 2 then ↑(val a) else ↑(val a) - ↑(Nat.succ n)) * 2 = ↑(Nat.succ n) ↔ 2 * val a = Nat.succ n

Next tactic:
rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
  

Proof state:
case neg
n : ℕ
a : ZMod (Nat.succ n)
h : ¬val a ≤ Nat.succ n / 2
⊢ valMinAbs a * 2 = ↑(Nat.succ n) ↔ 2 * val a = Nat.succ n

Next tactic:
apply iff_of_false _ (mt _ h)
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
  apply iff_of_false _ (mt _ h)
  · 

Proof state:
n : ℕ
a : ZMod (Nat.succ n)
h : ¬val a ≤ Nat.succ n / 2
⊢ ¬valMinAbs a * 2 = ↑(Nat.succ n)

Next tactic:
intro he
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
  apply iff_of_false _ (mt _ h)
  · intro he
    

Proof state:
n : ℕ
a : ZMod (Nat.succ n)
h : ¬val a ≤ Nat.succ n / 2
he : valMinAbs a * 2 = ↑(Nat.succ n)
⊢ False

Next tactic:
rw [← a.valMinAbs_nonneg_iff, ← mul_nonneg_iff_left_nonneg_of_pos, he] at h
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
  apply iff_of_false _ (mt _ h)
  · intro he
    rw [← a.valMinAbs_nonneg_iff, ← mul_nonneg_iff_left_nonneg_of_pos, he] at h
    

Proof state:
n : ℕ
a : ZMod (Nat.succ n)
h✝ : ¬0 ≤ valMinAbs a
h : ¬0 ≤ ↑(Nat.succ n)
he : valMinAbs a * 2 = ↑(Nat.succ n)
⊢ False
n : ℕ a : ZMod (Nat.succ n) h : ¬0 ≤ valMinAbs a he : valMinAbs a * 2 = ↑(Nat.succ n) ⊢ 0 < 2

Next tactic:
exacts [h (Nat.cast_nonneg _), zero_lt_two]
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
  apply iff_of_false _ (mt _ h)
  · intro he
    rw [← a.valMinAbs_nonneg_iff, ← mul_nonneg_iff_left_nonneg_of_pos, he] at h
    exacts [h (Nat.cast_nonneg _), zero_lt_two]
  · 

Proof state:
n : ℕ
a : ZMod (Nat.succ n)
h : ¬val a ≤ Nat.succ n / 2
⊢ 2 * val a = Nat.succ n → val a ≤ Nat.succ n / 2

Next tactic:
rw [mul_comm]
theorem valMinAbs_mul_two_eq_iff {n : ℕ} (a : ZMod n) : a.valMinAbs * 2 = n ↔ 2 * a.val = n := by
  cases' n with n
  · simp
  by_cases h : a.val ≤ n.succ / 2
  · dsimp [valMinAbs]
    rw [if_pos h, ← Int.coe_nat_inj', Nat.cast_mul, Nat.cast_two, mul_comm]
  apply iff_of_false _ (mt _ h)
  · intro he
    rw [← a.valMinAbs_nonneg_iff, ← mul_nonneg_iff_left_nonneg_of_pos, he] at h
    exacts [h (Nat.cast_nonneg _), zero_lt_two]
  · rw [mul_comm]
    

Proof state:
n : ℕ
a : ZMod (Nat.succ n)
h : ¬val a ≤ Nat.succ n / 2
⊢ val a * 2 = Nat.succ n → val a ≤ Nat.succ n / 2

Next tactic:
exact fun h => (Nat.le_div_iff_mul_le zero_lt_two).2 h.le
theorem valMinAbs_mem_Ioc {n : ℕ} [NeZero n] (x : ZMod n) :
    x.valMinAbs * 2 ∈ Set.Ioc (-n : ℤ) n := by
  

Proof state:
n : ℕ
inst✝ : NeZero n
x : ZMod n
⊢ valMinAbs x * 2 ∈ Set.Ioc (-↑n) ↑n

Next tactic:
simp_rw [valMinAbs_def_pos, Nat.le_div_two_iff_mul_two_le]
theorem valMinAbs_mem_Ioc {n : ℕ} [NeZero n] (x : ZMod n) :
    x.valMinAbs * 2 ∈ Set.Ioc (-n : ℤ) n := by
  simp_rw [valMinAbs_def_pos, Nat.le_div_two_iff_mul_two_le]; 

Proof state:
n : ℕ
inst✝ : NeZero n
x : ZMod n
⊢ (if ↑(val x) * 2 ≤ ↑n then ↑(val x) else ↑(val x) - ↑n) * 2 ∈ Set.Ioc (-↑n) ↑n

Next tactic:
split_ifs with h