l3lab/ntp-mathlib huggingface dataset:
theorem norm_eq_zero_iff {d : ℤ} (hd : d < 0) (z : ℤ√d) : z.norm = 0 ↔ z = 0 := by
  constructor
  · intro h
    rw [ext, zero_re, zero_im]
    rw [norm_def, sub_eq_add_neg, mul_assoc] at h
    have left := mul_self_nonneg z.re
    have right := neg_nonneg.mpr (mul_nonpos_of_nonpos_of_nonneg hd.le (mul_self_nonneg z.im))
    obtain ⟨ha, hb⟩ := (add_eq_zero_iff' left right).mp h
    constructor <;> apply eq_zero_of_mul_self_eq_zero
    · exact ha
    · rw [neg_eq_zero, mul_eq_zero] at hb
      exact hb.resolve_left hd.ne
  · 

Proof state:
case mpr
d✝ d : ℤ
hd : d < 0
z : ℤ√d
⊢ z = 0 → norm z = 0

Next tactic:
rintro rfl
theorem norm_eq_zero_iff {d : ℤ} (hd : d < 0) (z : ℤ√d) : z.norm = 0 ↔ z = 0 := by
  constructor
  · intro h
    rw [ext, zero_re, zero_im]
    rw [norm_def, sub_eq_add_neg, mul_assoc] at h
    have left := mul_self_nonneg z.re
    have right := neg_nonneg.mpr (mul_nonpos_of_nonpos_of_nonneg hd.le (mul_self_nonneg z.im))
    obtain ⟨ha, hb⟩ := (add_eq_zero_iff' left right).mp h
    constructor <;> apply eq_zero_of_mul_self_eq_zero
    · exact ha
    · rw [neg_eq_zero, mul_eq_zero] at hb
      exact hb.resolve_left hd.ne
  · rintro rfl
    

Proof state:
case mpr
d✝ d : ℤ
hd : d < 0
⊢ norm 0 = 0

Next tactic:
exact norm_zero
theorem norm_eq_of_associated {d : ℤ} (hd : d ≤ 0) {x y : ℤ√d} (h : Associated x y) :
    x.norm = y.norm := by
  

Proof state:
d✝ d : ℤ
hd : d ≤ 0
x y : ℤ√d
h : Associated x y
⊢ norm x = norm y

Next tactic:
obtain ⟨u, rfl⟩ := h
theorem norm_eq_of_associated {d : ℤ} (hd : d ≤ 0) {x y : ℤ√d} (h : Associated x y) :
    x.norm = y.norm := by
  obtain ⟨u, rfl⟩ := h
  

Proof state:
case intro
d✝ d : ℤ
hd : d ≤ 0
x : ℤ√d
u : (ℤ√d)ˣ
⊢ norm x = norm (x * ↑u)

Next tactic:
rw [norm_mul, (norm_eq_one_iff' hd _).mpr u.isUnit, mul_one]
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  

Proof state:
d✝ c d : ℕ
a b : ℤ
⊢ Decidable (Nonnegg c d a b)

Next tactic:
cases a
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> 

Proof state:
case ofNat
d✝ c d : ℕ
b : ℤ
a✝ : ℕ
⊢ Decidable (Nonnegg c d (Int.ofNat a✝) b)

Next tactic:
cases b
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> 

Proof state:
case negSucc
d✝ c d : ℕ
b : ℤ
a✝ : ℕ
⊢ Decidable (Nonnegg c d (Int.negSucc a✝) b)

Next tactic:
cases b
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> 

Proof state:
case ofNat.ofNat
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable (Nonnegg c d (Int.ofNat a✝¹) (Int.ofNat a✝))

Next tactic:
unfold Nonnegg SqLe
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> 

Proof state:
case ofNat.negSucc
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable (Nonnegg c d (Int.ofNat a✝¹) (Int.negSucc a✝))

Next tactic:
unfold Nonnegg SqLe
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> 

Proof state:
case negSucc.ofNat
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable (Nonnegg c d (Int.negSucc a✝¹) (Int.ofNat a✝))

Next tactic:
unfold Nonnegg SqLe
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> 

Proof state:
case negSucc.negSucc
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable (Nonnegg c d (Int.negSucc a✝¹) (Int.negSucc a✝))

Next tactic:
unfold Nonnegg SqLe
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> unfold Nonnegg SqLe <;> 

Proof state:
case ofNat.ofNat
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable
    (match Int.ofNat a✝¹, Int.ofNat a✝ with
    | Int.ofNat a, Int.ofNat b => True
    | Int.ofNat a, Int.negSucc b => c * (b + 1) * (b + 1) ≤ d * a * a
    | Int.negSucc a, Int.ofNat b => d * (a + 1) * (a + 1) ≤ c * b * b
    | Int.negSucc a, Int.negSucc a_1 => False)

Next tactic:
infer_instance
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> unfold Nonnegg SqLe <;> 

Proof state:
case ofNat.negSucc
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable
    (match Int.ofNat a✝¹, Int.negSucc a✝ with
    | Int.ofNat a, Int.ofNat b => True
    | Int.ofNat a, Int.negSucc b => c * (b + 1) * (b + 1) ≤ d * a * a
    | Int.negSucc a, Int.ofNat b => d * (a + 1) * (a + 1) ≤ c * b * b
    | Int.negSucc a, Int.negSucc a_1 => False)

Next tactic:
infer_instance
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> unfold Nonnegg SqLe <;> 

Proof state:
case negSucc.ofNat
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable
    (match Int.negSucc a✝¹, Int.ofNat a✝ with
    | Int.ofNat a, Int.ofNat b => True
    | Int.ofNat a, Int.negSucc b => c * (b + 1) * (b + 1) ≤ d * a * a
    | Int.negSucc a, Int.ofNat b => d * (a + 1) * (a + 1) ≤ c * b * b
    | Int.negSucc a, Int.negSucc a_1 => False)

Next tactic:
infer_instance
instance decidableNonnegg (c d a b) : Decidable (Nonnegg c d a b) := by
  cases a <;> cases b <;> unfold Nonnegg SqLe <;> 

Proof state:
case negSucc.negSucc
d✝ c d a✝¹ a✝ : ℕ
⊢ Decidable
    (match Int.negSucc a✝¹, Int.negSucc a✝ with
    | Int.ofNat a, Int.ofNat b => True
    | Int.ofNat a, Int.negSucc b => c * (b + 1) * (b + 1) ≤ d * a * a
    | Int.negSucc a, Int.ofNat b => d * (a + 1) * (a + 1) ≤ c * b * b
    | Int.negSucc a, Int.negSucc a_1 => False)

Next tactic:
infer_instance
open Int in
theorem nonneg_add_lem {x y z w : ℕ} (xy : Nonneg (⟨x, -y⟩ : ℤ√d)) (zw : Nonneg (⟨-z, w⟩ : ℤ√d)) :
    Nonneg (⟨x, -y⟩ + ⟨-z, w⟩ : ℤ√d) := by
  

Proof state:
d x y z w : ℕ
xy : Nonneg { re := ↑x, im := -↑y }
zw : Nonneg { re := -↑z, im := ↑w }
⊢ Nonneg ({ re := ↑x, im := -↑y } + { re := -↑z, im := ↑w })

Next tactic:
have : Nonneg ⟨Int.subNatNat x z, Int.subNatNat w y⟩ :=
    Int.subNatNat_elim x z
      (fun m n i => SqLe y d m 1 → SqLe n 1 w d → Nonneg ⟨i, Int.subNatNat w y⟩)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d (k + j) 1 → SqLe k 1 m d → Nonneg ⟨Int.ofNat j, i⟩)
          (fun _ _ _ _ => trivial) fun m n xy zw => sqLe_cancel zw xy)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d k 1 → SqLe (k + j + 1) 1 m d → Nonneg ⟨-[j+1], i⟩)
          (fun m n xy zw => sqLe_cancel xy zw) fun m n xy zw =>
          let t := Nat.le_trans zw (sqLe_of_le (Nat.le_add_right n (m + 1)) le_rfl xy)
          have : k + j + 1 ≤ k :=
            Nat.mul_self_le_mul_self_iff.2 (by simpa [one_mul] using t)
          absurd this (not_le_of_gt <| Nat.succ_le_succ <| Nat.le_add_right _ _))
      (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw)
open Int in
theorem nonneg_add_lem {x y z w : ℕ} (xy : Nonneg (⟨x, -y⟩ : ℤ√d)) (zw : Nonneg (⟨-z, w⟩ : ℤ√d)) :
    Nonneg (⟨x, -y⟩ + ⟨-z, w⟩ : ℤ√d) := by
  have : Nonneg ⟨Int.subNatNat x z, Int.subNatNat w y⟩ :=
    Int.subNatNat_elim x z
      (fun m n i => SqLe y d m 1 → SqLe n 1 w d → Nonneg ⟨i, Int.subNatNat w y⟩)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d (k + j) 1 → SqLe k 1 m d → Nonneg ⟨Int.ofNat j, i⟩)
          (fun _ _ _ _ => trivial) fun m n xy zw => sqLe_cancel zw xy)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d k 1 → SqLe (k + j + 1) 1 m d → Nonneg ⟨-[j+1], i⟩)
          (fun m n xy zw => sqLe_cancel xy zw) fun m n xy zw =>
          let t := Nat.le_trans zw (sqLe_of_le (Nat.le_add_right n (m + 1)) le_rfl xy)
          have : k + j + 1 ≤ k :=
            Nat.mul_self_le_mul_self_iff.2 (by 

Proof state:
d x y z w : ℕ
xy✝ : Nonneg { re := ↑x, im := -↑y }
zw✝ : Nonneg { re := -↑z, im := ↑w }
j k m n : ℕ
xy : SqLe (n + m + 1) d k 1
zw : SqLe (k + j + 1) 1 n d
t : 1 * (k + j + 1) * (k + j + 1) ≤ 1 * k * k := Nat.le_trans zw (sqLe_of_le (Nat.le_add_right n (m + 1)) le_rfl xy)
⊢ (k + j + 1) * (k + j + 1) ≤ k * k

Next tactic:
simpa [one_mul] using t
open Int in
theorem nonneg_add_lem {x y z w : ℕ} (xy : Nonneg (⟨x, -y⟩ : ℤ√d)) (zw : Nonneg (⟨-z, w⟩ : ℤ√d)) :
    Nonneg (⟨x, -y⟩ + ⟨-z, w⟩ : ℤ√d) := by
  have : Nonneg ⟨Int.subNatNat x z, Int.subNatNat w y⟩ :=
    Int.subNatNat_elim x z
      (fun m n i => SqLe y d m 1 → SqLe n 1 w d → Nonneg ⟨i, Int.subNatNat w y⟩)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d (k + j) 1 → SqLe k 1 m d → Nonneg ⟨Int.ofNat j, i⟩)
          (fun _ _ _ _ => trivial) fun m n xy zw => sqLe_cancel zw xy)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d k 1 → SqLe (k + j + 1) 1 m d → Nonneg ⟨-[j+1], i⟩)
          (fun m n xy zw => sqLe_cancel xy zw) fun m n xy zw =>
          let t := Nat.le_trans zw (sqLe_of_le (Nat.le_add_right n (m + 1)) le_rfl xy)
          have : k + j + 1 ≤ k :=
            Nat.mul_self_le_mul_self_iff.2 (by simpa [one_mul] using t)
          absurd this (not_le_of_gt <| Nat.succ_le_succ <| Nat.le_add_right _ _))
      (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw)
  

Proof state:
d x y z w : ℕ
xy : Nonneg { re := ↑x, im := -↑y }
zw : Nonneg { re := -↑z, im := ↑w }
this : Nonneg { re := subNatNat x z, im := subNatNat w y }
⊢ Nonneg ({ re := ↑x, im := -↑y } + { re := -↑z, im := ↑w })

Next tactic:
rw [add_def, neg_add_eq_sub]
open Int in
theorem nonneg_add_lem {x y z w : ℕ} (xy : Nonneg (⟨x, -y⟩ : ℤ√d)) (zw : Nonneg (⟨-z, w⟩ : ℤ√d)) :
    Nonneg (⟨x, -y⟩ + ⟨-z, w⟩ : ℤ√d) := by
  have : Nonneg ⟨Int.subNatNat x z, Int.subNatNat w y⟩ :=
    Int.subNatNat_elim x z
      (fun m n i => SqLe y d m 1 → SqLe n 1 w d → Nonneg ⟨i, Int.subNatNat w y⟩)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d (k + j) 1 → SqLe k 1 m d → Nonneg ⟨Int.ofNat j, i⟩)
          (fun _ _ _ _ => trivial) fun m n xy zw => sqLe_cancel zw xy)
      (fun j k =>
        Int.subNatNat_elim w y
          (fun m n i => SqLe n d k 1 → SqLe (k + j + 1) 1 m d → Nonneg ⟨-[j+1], i⟩)
          (fun m n xy zw => sqLe_cancel xy zw) fun m n xy zw =>
          let t := Nat.le_trans zw (sqLe_of_le (Nat.le_add_right n (m + 1)) le_rfl xy)
          have : k + j + 1 ≤ k :=
            Nat.mul_self_le_mul_self_iff.2 (by simpa [one_mul] using t)
          absurd this (not_le_of_gt <| Nat.succ_le_succ <| Nat.le_add_right _ _))
      (nonnegg_pos_neg.1 xy) (nonnegg_neg_pos.1 zw)
  rw [add_def, neg_add_eq_sub]
  

Proof state:
d x y z w : ℕ
xy : Nonneg { re := ↑x, im := -↑y }
zw : Nonneg { re := -↑z, im := ↑w }
this : Nonneg { re := subNatNat x z, im := subNatNat w y }
⊢ Nonneg { re := ↑x + -↑z, im := ↑w - ↑y }

Next tactic:
rwa [Int.subNatNat_eq_coe, Int.subNatNat_eq_coe] at this
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  

Proof state:
d : ℕ
a b : ℤ√↑d
ha : Nonneg a
hb : Nonneg b
⊢ Nonneg (a + b)

Next tactic:
rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    

Proof state:
case intro.intro.inl
d : ℕ
b : ℤ√↑d
hb : Nonneg b
x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
⊢ Nonneg ({ re := ↑x, im := ↑y } + b)

Next tactic:
rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    

Proof state:
case intro.intro.inr.inl
d : ℕ
b : ℤ√↑d
hb : Nonneg b
x y : ℕ
ha : Nonneg { re := ↑x, im := -↑y }
⊢ Nonneg ({ re := ↑x, im := -↑y } + b)

Next tactic:
rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    

Proof state:
case intro.intro.inr.inr
d : ℕ
b : ℤ√↑d
hb : Nonneg b
x y : ℕ
ha : Nonneg { re := -↑x, im := ↑y }
⊢ Nonneg ({ re := -↑x, im := ↑y } + b)

Next tactic:
rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · 

Proof state:
case intro.intro.inl.intro.intro.inl
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := ↑w }
⊢ Nonneg ({ re := ↑x, im := ↑y } + { re := ↑z, im := ↑w })

Next tactic:
trivial
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · 

Proof state:
case intro.intro.inl.intro.intro.inr.inl
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := -↑w }
⊢ Nonneg ({ re := ↑x, im := ↑y } + { re := ↑z, im := -↑w })

Next tactic:
refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · 

Proof state:
case intro.intro.inl.intro.intro.inr.inl.refine'_1
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := -↑w }
i : ℕ
h : { re := ↑x, im := ↑y }.im + { re := ↑z, im := -↑w }.im = -↑i
⊢ i ≤ w

Next tactic:
dsimp only at h
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      

Proof state:
case intro.intro.inl.intro.intro.inr.inl.refine'_1
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := -↑w }
i : ℕ
h : ↑y + -↑w = -↑i
⊢ i ≤ w

Next tactic:
exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by 

Proof state:
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := -↑w }
i : ℕ
h : ↑y + -↑w = -↑i
⊢ -↑w + ↑y = -↑i

Next tactic:
simp [add_comm, *]
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · 

Proof state:
case intro.intro.inl.intro.intro.inr.inl.refine'_2
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := -↑w }
i : ℕ
h : { re := ↑x, im := ↑y }.im + { re := ↑z, im := -↑w }.im = -↑i
⊢ z ≤ x + z

Next tactic:
apply Nat.le_add_left
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · 

Proof state:
case intro.intro.inl.intro.intro.inr.inr
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := -↑z, im := ↑w }
⊢ Nonneg ({ re := ↑x, im := ↑y } + { re := -↑z, im := ↑w })

Next tactic:
refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · 

Proof state:
case intro.intro.inl.intro.intro.inr.inr.refine'_1
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := -↑z, im := ↑w }
i : ℕ
h : { re := ↑x, im := ↑y }.re + { re := -↑z, im := ↑w }.re = -↑i
⊢ i ≤ z

Next tactic:
dsimp only at h
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      

Proof state:
case intro.intro.inl.intro.intro.inr.inr.refine'_1
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := -↑z, im := ↑w }
i : ℕ
h : ↑x + -↑z = -↑i
⊢ i ≤ z

Next tactic:
exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by 

Proof state:
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := -↑z, im := ↑w }
i : ℕ
h : ↑x + -↑z = -↑i
⊢ -↑z + ↑x = -↑i

Next tactic:
simp [add_comm, *]
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
    · 

Proof state:
case intro.intro.inl.intro.intro.inr.inr.refine'_2
d x y : ℕ
ha : Nonneg { re := ↑x, im := ↑y }
z w : ℕ
hb : Nonneg { re := -↑z, im := ↑w }
i : ℕ
h : { re := ↑x, im := ↑y }.re + { re := -↑z, im := ↑w }.re = -↑i
⊢ w ≤ y + w

Next tactic:
apply Nat.le_add_left
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · 

Proof state:
case intro.intro.inr.inl.intro.intro.inl
d x y : ℕ
ha : Nonneg { re := ↑x, im := -↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := ↑w }
⊢ Nonneg ({ re := ↑x, im := -↑y } + { re := ↑z, im := ↑w })

Next tactic:
refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 ha)
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 ha)
    · 

Proof state:
case intro.intro.inr.inl.intro.intro.inl.refine'_1
d x y : ℕ
ha : Nonneg { re := ↑x, im := -↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := ↑w }
i : ℕ
h : { re := ↑x, im := -↑y }.im + { re := ↑z, im := ↑w }.im = -↑i
⊢ i ≤ y

Next tactic:
dsimp only at h
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 ha)
    · dsimp only at h
      

Proof state:
case intro.intro.inr.inl.intro.intro.inl.refine'_1
d x y : ℕ
ha : Nonneg { re := ↑x, im := -↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := ↑w }
i : ℕ
h : -↑y + ↑w = -↑i
⊢ i ≤ y

Next tactic:
exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro w (by simp [*])))
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 ha)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro w (by 

Proof state:
d x y : ℕ
ha : Nonneg { re := ↑x, im := -↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := ↑w }
i : ℕ
h : -↑y + ↑w = -↑i
⊢ -↑y + ↑w = -↑i

Next tactic:
simp [*]
theorem Nonneg.add {a b : ℤ√d} (ha : Nonneg a) (hb : Nonneg b) : Nonneg (a + b) := by
  rcases nonneg_cases ha with ⟨x, y, rfl | rfl | rfl⟩ <;>
    rcases nonneg_cases hb with ⟨z, w, rfl | rfl | rfl⟩
  · trivial
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro y (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_left fun i h => sqLe_of_le _ _ (nonnegg_neg_pos.1 hb)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro x (by simp [add_comm, *])))
    · apply Nat.le_add_left
  · refine' nonnegg_cases_right fun i h => sqLe_of_le _ _ (nonnegg_pos_neg.1 ha)
    · dsimp only at h
      exact Int.ofNat_le.1 (le_of_neg_le_neg (Int.le.intro w (by simp [*])))
    · 

Proof state:
case intro.intro.inr.inl.intro.intro.inl.refine'_2
d x y : ℕ
ha : Nonneg { re := ↑x, im := -↑y }
z w : ℕ
hb : Nonneg { re := ↑z, im := ↑w }
i : ℕ
h : { re := ↑x, im := -↑y }.im + { re := ↑z, im := ↑w }.im = -↑i
⊢ x ≤ x + z

Next tactic:
apply Nat.le_add_right