/-
Milnor `K₂` of a field.

Shubhrajit Bhattacharya, *On Some Congruences of Zeta and L-values at Negative Odd
Integers* (UBC MSc thesis, April 2024), §4.3 begins "This question has a direct relation
with Milnor `K`-theory" and uses `K₀(𝒪_F) ≅ Cl F ⊕ ℤ`.  Algebraic `K`-theory is absent
from Mathlib, so this file builds the first definitional layer: `K₂^M F` as the
Matsumoto quotient `(Fˣ ⊗_ℤ Fˣ) / ⟨a ⊗ (1-a)⟩`, together with the symbol calculus that
is an immediate consequence of the definition.  Everything below is proved; nothing is
assumed and there is no `sorry`.
-/
import Mathlib

open scoped TensorProduct

universe u

namespace ThesisApp
namespace K2Milnor

variable {F : Type u} [Field F]

/-- `Fˣ` regarded as an additive group. -/
abbrev toA (a : Fˣ) : Additive Fˣ := Additive.ofMul a

/-- `1 - a` as a unit of `F`; the junk value at `a = 1` is `1`. -/
noncomputable def oneSub (a : Fˣ) : Fˣ := by
  classical
  exact if h : (1 : F) - (a : F) = 0 then 1 else Units.mk0 (1 - (a : F)) h

lemma coe_oneSub_of_ne (a : Fˣ) (h : (1 : F) - (a : F) ≠ 0) :
    ((oneSub a : Fˣ) : F) = 1 - (a : F) := by
  unfold oneSub
  rw [dif_neg h]
  rfl

/-- The Steinberg subgroup `⟨a ⊗ (1-a)⟩ ≤ Fˣ ⊗ Fˣ`. -/
noncomputable def steinberg : AddSubgroup (Additive Fˣ ⊗[ℤ] Additive Fˣ) :=
  AddSubgroup.closure (Set.range fun a : Fˣ => toA a ⊗ₜ[ℤ] toA (oneSub a))

/-- Milnor `K₂` of `F` in Matsumoto's presentation. -/
noncomputable def K2M : Type u :=
  (Additive Fˣ ⊗[ℤ] Additive Fˣ) ⧸ steinberg (F := F)

noncomputable instance : AddCommGroup (K2M (F := F)) :=
  QuotientAddGroup.Quotient.addCommGroup _

/-- The Steinberg symbol `{a, b} ∈ K₂^M F`. -/
noncomputable def k2 : Fˣ → Fˣ → K2M (F := F) :=
  fun a b => QuotientAddGroup.mk (toA a ⊗ₜ[ℤ] toA b)

lemma k2_add_left (a b c : Fˣ) : k2 (F := F) (a * b) c = k2 a c + k2 b c := by
  have h : toA (a * b) = toA a + toA b := rfl
  rw [k2, h, TensorProduct.add_tmul]
  rfl

lemma k2_add_right (a b c : Fˣ) : k2 (F := F) a (b * c) = k2 a b + k2 a c := by
  have h : toA (b * c) = toA b + toA c := rfl
  rw [k2, h, TensorProduct.tmul_add]
  rfl

lemma k2_one_left (b : Fˣ) : k2 (F := F) 1 b = 0 := by
  have h : toA (1 : Fˣ) = 0 := rfl
  rw [k2, h, TensorProduct.zero_tmul]
  rfl

lemma k2_one_right (a : Fˣ) : k2 (F := F) a 1 = 0 := by
  have h : toA (1 : Fˣ) = 0 := rfl
  rw [k2, h, TensorProduct.tmul_zero]
  rfl

lemma k2_inv_right (a b : Fˣ) : k2 (F := F) a b⁻¹ = -k2 a b := by
  have h : k2 (F := F) a (b⁻¹ * b) = k2 a b⁻¹ + k2 a b := k2_add_right _ _ _
  rw [inv_mul_cancel, k2_one_right] at h
  exact eq_neg_of_add_eq_zero_left h.symm

lemma k2_inv_left (a b : Fˣ) : k2 (F := F) a⁻¹ b = -k2 a b := by
  have h : k2 (F := F) (a⁻¹ * a) b = k2 a⁻¹ b + k2 a b := k2_add_left _ _ _
  rw [inv_mul_cancel, k2_one_left] at h
  exact eq_neg_of_add_eq_zero_left h.symm

/-- The **Steinberg relation** `{a, 1-a} = 0`. -/
theorem k2_steinberg (a : Fˣ) : k2 (F := F) a (oneSub a) = 0 :=
  (QuotientAddGroup.eq_zero_iff _).mpr
    (AddSubgroup.subset_closure (Set.mem_range_self a))

/-- The Steinberg relation with `1 - a` supplied as a unit. -/
theorem k2_steinberg' (a : Fˣ) (h : (1 : F) - (a : F) ≠ 0) :
    k2 (F := F) a (Units.mk0 (1 - (a : F)) h) = 0 := by
  rw [← k2_steinberg a]
  unfold oneSub
  rw [dif_neg h]

/-- `{a, b} = {a, b / (1-a)}` for `a ≠ 1`: the standard reduction of the theory, from
Steinberg plus bilinearity. -/
theorem k2_div_oneSub (a b : Fˣ) :
    k2 (F := F) a b = k2 a (b / oneSub a) := by
  nth_rewrite 1 [show b = b / oneSub a * oneSub a from (div_mul_cancel b (oneSub a)).symm]
  rw [k2_add_right, k2_steinberg, add_zero]

/-- `{-a, b} = {-1, b} + {a, b}`: the sign convention of the tame symbol. -/
theorem k2_neg_left (a b : Fˣ) : k2 (F := F) (-a) b = k2 (-1) b + k2 a b := by
  have h : -a = -1 * a := (neg_one_mul a).symm
  rw [h, k2_add_left]

/-- `{a, -a} = {a, -1} + {a, a}`.  Milnor's identity `{a,-a} = 0` is *not* a formal
consequence of Steinberg and is not claimed here; this is the shape in which the sign
question enters `K₂` computations, and it isolates exactly what is missing. -/
theorem k2_self_neg (a : Fˣ) : k2 (F := F) a (-a) = k2 a (-1) + k2 a a := by
  conv_lhs => rw [show -a = a * (-1) by rw [mul_neg, mul_one]]
  rw [k2_add_right, add_comm]

end K2Milnor
end ThesisApp
