import Std

/-!
A finite rational-ray certificate for kissing configurations in five dimensions.
Run with Lean 4.19.0: lean Proof.lean

A nonzero integer vector u represents the real unit direction u / ‖u‖.
Outer unit spheres are centered at 2u / ‖u‖, with the central unit sphere at 0.
For unit directions a,b, the outer balls have disjoint interiors iff a·b ≤ 1/2.
For integer rays u,v this is equivalent to dot u v ≤ 0 or
4 (dot u v)^2 ≤ (dot u u)(dot v v), by squaring nonnegative quantities.
The Lean theorems below certify the integer conditions, including boundary contacts.
The normalization argument above is stated in prose. No trigonometry or rounding
is used in the checked predicates.
-/
namespace Sphere41

abbrev Ray := List Int

def dot : Ray → Ray → Int
  | x :: xs, y :: ys => x * y + dot xs ys
  | _, _ => 0

def validRay (u : Ray) : Bool :=
  (u.length == 5) && decide (0 < dot u u)

def separated (u v : Ray) : Bool :=
  let d := dot u v
  decide (d ≤ 0 ∨ 4 * d * d ≤ dot u u * dot v v)

def allSeparated : List Ray → Bool
  | [] => true
  | u :: us => us.all (separated u) && allSeparated us

def verifyRays (n : Nat) (xs : List Ray) : Bool :=
  (xs.length == n) && xs.all validRay && allSeparated xs

theorem allSeparated_iff (xs : List Ray) :
    allSeparated xs = true ↔ xs.Pairwise (fun u v => separated u v = true) := by
  induction xs with
  | nil => simp [allSeparated]
  | cons u us ih => simp [allSeparated, List.pairwise_cons, ih]

theorem verifyRays_iff (n : Nat) (xs : List Ray) :
    verifyRays n xs = true ↔
      (xs.length = n ∧ (∀ u ∈ xs, validRay u = true)) ∧
      xs.Pairwise (fun u v => separated u v = true) := by
  simp [verifyRays, allSeparated_iff]

set_option maxRecDepth 100000
set_option maxHeartbeats 100000000

/-- All signed pairs of distinct coordinate axes, the D5 root directions. -/
def d5 : List Ray := [
  [-1, -1, 0, 0, 0],
  [-1, 1, 0, 0, 0],
  [1, -1, 0, 0, 0],
  [1, 1, 0, 0, 0],
  [-1, 0, -1, 0, 0],
  [-1, 0, 1, 0, 0],
  [1, 0, -1, 0, 0],
  [1, 0, 1, 0, 0],
  [-1, 0, 0, -1, 0],
  [-1, 0, 0, 1, 0],
  [1, 0, 0, -1, 0],
  [1, 0, 0, 1, 0],
  [-1, 0, 0, 0, -1],
  [-1, 0, 0, 0, 1],
  [1, 0, 0, 0, -1],
  [1, 0, 0, 0, 1],
  [0, -1, -1, 0, 0],
  [0, -1, 1, 0, 0],
  [0, 1, -1, 0, 0],
  [0, 1, 1, 0, 0],
  [0, -1, 0, -1, 0],
  [0, -1, 0, 1, 0],
  [0, 1, 0, -1, 0],
  [0, 1, 0, 1, 0],
  [0, -1, 0, 0, -1],
  [0, -1, 0, 0, 1],
  [0, 1, 0, 0, -1],
  [0, 1, 0, 0, 1],
  [0, 0, -1, -1, 0],
  [0, 0, -1, 1, 0],
  [0, 0, 1, -1, 0],
  [0, 0, 1, 1, 0],
  [0, 0, -1, 0, -1],
  [0, 0, -1, 0, 1],
  [0, 0, 1, 0, -1],
  [0, 0, 1, 0, 1],
  [0, 0, 0, -1, -1],
  [0, 0, 0, -1, 1],
  [0, 0, 0, 1, -1],
  [0, 0, 0, 1, 1]]

/-- Kernel-checked exact 40-direction witness, testing all 780 pairs. -/
theorem d5_exact : verifyRays 40 d5 = true := by decide

/-- The exact quantized five-dimensional directions in candidate.json. -/
def candidate : List Ray := [
  [-24499155, 21160954, -61461300, 25506221, -67260291],
  [-3005597, 14282695, 76395771, -36784754, 50966058],
  [-3859273, -39423513, -885611, -54368951, 73987333],
  [55802799, 72368299, 15847213, 23299329, 29238401],
  [-58694048, -56004420, -10019097, 38870465, -42511390],
  [-56334086, -19800586, -26854797, -49943157, -56735425],
  [-19364624, -16627862, 75824802, -44012304, -40768149],
  [-71459505, -54507983, 27062229, -31500529, 14063124],
  [-30452618, 51683738, 21985063, -15620780, -75326473],
  [1935299, -85486396, 9791780, -30052335, -41101203],
  [58849289, 40029245, -14398887, 35645833, -58791729],
  [-33784124, -67326887, -61999880, -18499029, 11812029],
  [26979349, 25557193, 86834399, 30257943, -12774532],
  [57866506, -67223755, -33504173, -7682414, 30836426],
  [-23829053, 44193662, 15388619, 75367213, -39523032],
  [33572418, -53565122, -44077019, 38293369, -50936281],
  [8047081, -22434633, 39937278, 32771567, -82237278],
  [54131018, -35121391, 40423798, -64454283, 6919730],
  [63548022, 651025, -37869366, 62918180, 23841956],
  [37621521, 57169107, 46238289, -52704462, -20014364],
  [-91855720, 29945912, -11733694, 15105122, -17318338],
  [-65822778, 19090253, 10300210, -6477267, 71797457],
  [89313058, 17798748, -26835225, -31374141, 1384281],
  [-49834149, 37340270, 20824121, -75393486, 2107158],
  [-30073817, -60168034, -518100, 37641392, 63704242],
  [51902271, -13517410, 29537343, 18282098, 76920375],
  [21428405, 52902811, -14124303, -58668717, 55683048],
  [4793945, 7997991, -53794153, 12648292, 82820650],
  [42792711, -6131133, -2667408, -47050759, -76878502],
  [-30383146, -11187566, -61992224, 70067930, 14112125],
  [82696871, -33355489, 31707660, 25393051, -19961368],
  [9615866, -64547970, 72623344, 10801214, 18715725],
  [-50238813, 26228715, -67522162, -40142246, 24849023],
  [-17902802, 78196964, -47719456, 33437584, 13019445],
  [-11847563, 30408781, 13612263, 73725898, 57568564],
  [14990874, 64972922, -48207908, -45052561, -34641844],
  [9219756, -40681397, 21810271, 88086563, -5008955],
  [-34767064, 80527321, 45723911, 7477688, 12649507],
  [-60707079, -5008631, 65900174, 43252408, 8715525],
  [15651426, -18975145, -45649896, -85453804, -2951466],
  [27940605, -1434787, -95933397, -3613023, -996645]]

theorem candidate_has_41_valid_rays :
    candidate.length = 41 ∧ candidate.all validRay = true := by decide

/-- The saved candidate fails the full finite checker. -/
theorem candidate_rejected : verifyRays 41 candidate = false := by decide

/-- Points 3 and 22 in the exported one-based numbering collide. -/
theorem candidate_explicit_collision :
    separated (candidate[2]!) (candidate[21]!) = false := by decide

#print axioms d5_exact
#print axioms candidate_rejected
#print axioms candidate_explicit_collision
/-- Refined minimax search candidate, matching refined.json. -/
def refined : List Ray := [
  [-5066529, -56552505, 58233945, 26990347, 51540987],
  [47482346, 33217308, -38095475, 6766828, -71728531],
  [42417311, 39797633, -13028217, -59496738, 53919605],
  [30016329, 44397537, 57194703, 14004292, -60502268],
  [-62524908, 40072255, 37105119, -11808495, -54485042],
  [-75568773, -28609933, 42972169, -39320366, 8839332],
  [-27222403, 5019735, 29276024, -31108753, 86074985],
  [14101863, 36665181, -84086295, -37045422, -3732786],
  [-16273236, -92392906, 22892924, -12693268, -22661149],
  [-37485988, 27095443, 84071032, 17962055, 21680956],
  [-9836133, -27447945, 11563996, -3512314, -94888347],
  [74547646, 14748898, 23421261, -53914372, -27745334],
  [19661552, -43528040, -57975413, -47765593, 45563502],
  [48088729, 1505549, 33139175, 78148349, 21905207],
  [29690801, -76523010, -19763062, 51659394, 14262319],
  [70819633, -35547666, 20886912, 28205913, -49890945],
  [11188302, 50196702, -63196726, 57883827, 3278623],
  [-51981072, -43682543, -43507969, -52916627, -26394903],
  [-6306104, 28786803, -15514735, -71693679, -61244285],
  [-20320694, 96699187, -13401917, -5182978, -5464623],
  [14129006, -26460298, 85975098, -33569145, -24116775],
  [51329959, 17947439, 67557130, -8365203, 49083422],
  [-53674505, -64689097, -23764691, 4038455, 48510809],
  [42500680, -18163177, -10736344, 20743563, 85546635],
  [-47850976, 32675537, -57268349, 10685636, -56997779],
  [-3223831, 56945703, 54350483, -61571841, 1314267],
  [-32986073, -11911704, -18316666, 83626569, 37962177],
  [-87850268, 34159974, -16403, 19578135, 27057769],
  [63164834, -63411114, 23344152, -28456957, 25189016],
  [77353991, -9326549, -59951023, 16276684, 8385699],
  [70326448, 68551090, 1565598, 17328394, 7225953],
  [-28557324, 19504394, -62865935, 5103768, 69468580],
  [1828939, -27526891, 16851098, -94227427, 8726845],
  [-11843787, -31766683, 63861100, 57517543, -38263491],
  [-77687506, -38863693, -4718566, 33086366, -36569008],
  [-29095322, 52802149, 21702415, 73101113, -23465456],
  [43239034, -48735002, -40794591, -42124641, -48131087],
  [-3258749, 60602589, 14349653, 42298545, 65739085],
  [-15175965, -35801701, -89217237, 22564300, -4366376],
  [2880038, -18120915, -29174865, 75531377, -55742050],
  [-52005006, 36668498, -24942383, -68580863, 25008866]]

theorem refined_has_41_valid_rays :
    refined.length = 41 ∧ refined.all validRay = true := by decide

theorem refined_rejected : verifyRays 41 refined = false := by decide

/-- Points 15 and 39 in the exported one-based numbering. -/
theorem refined_explicit_collision :
    separated (refined[14]!) (refined[38]!) = false := by decide

/-!
A symbolic fixed-D5 obstruction over the integers. After permuting axes and
changing their signs, a nonzero direction has coordinates a,b,c,d,e with
a maximal and b maximal among the rest. These symmetries preserve D5.
The symbolic theorem is for this explicitly specified coordinate chamber.
Its elementary identity and bound work over the reals by the same calculation.
-/
theorem frozenD5_identity (a b : Int) :
    5*((a+b)*(a+b)) - 4*(a*a+4*(b*b)) = (a-b)*(a+11*b) := by
  simp only [Int.add_mul, Int.mul_add, Int.sub_mul]
  rw [Int.mul_left_comm a 11 b, Int.mul_left_comm b 11 b, Int.mul_comm b a]
  omega
theorem frozenD5_bound (a b n : Int) (hb : 0 ≤ b) (hab : b ≤ a) (hn : n ≤ a*a+4*(b*b)) :
    4*n ≤ 5*((a+b)*(a+b)) := by
  have h := frozenD5_identity a b
  have hprod := Int.mul_nonneg (Int.sub_nonneg_of_le hab) (show 0 ≤ a+11*b by omega)
  omega
theorem frozenD5_chamber_collision (a b c d e : Int)
    (ha : 0 < a) (hab : b ≤ a) (hb : 0 ≤ b)
    (hc : 0 ≤ c) (hcb : c ≤ b)
    (hd : 0 ≤ d) (hdb : d ≤ b)
    (he : 0 ≤ e) (heb : e ≤ b) :
    separated [1,1,0,0,0] [a,b,c,d,e] = false := by
  have hc2 := Int.mul_self_le_mul_self hc hcb
  have hd2 := Int.mul_self_le_mul_self hd hdb
  have he2 := Int.mul_self_le_mul_self he heb
  have haa := Int.mul_pos ha ha
  have hbb := Int.mul_nonneg hb hb
  have hcc := Int.mul_nonneg hc hc
  have hdd := Int.mul_nonneg hd hd
  have hee := Int.mul_nonneg he he
  have h := frozenD5_bound a b (a*a + (b*b + (c*c + (d*d + e*e)))) hb hab (by omega)
  simp [separated, dot, Int.mul_assoc]
  omega


/-- The obstructing root is present in the exact 40-point family. -/
theorem frozenD5_root : [1,1,0,0,0] ∈ d5 := by decide

/-- The all-ones direction attains maximum squared *positive* cosine 2/5
with D5: every positive dot meets that bound, and an actual root attains it. -/
theorem frozenD5_equality :
    let u : Ray := [1,1,1,1,1]
    let r : Ray := [1,1,0,0,0]
    d5.all (fun v => decide (dot v u ≤ 0 ∨
        5 * dot v u * dot v u ≤ 2 * dot v v * dot u u)) = true ∧
      0 < dot r u ∧ 5 * dot r u * dot r u = 2 * dot r r * dot u u := by decide

#print axioms refined_rejected
#print axioms refined_explicit_collision
#print axioms frozenD5_chamber_collision
#print axioms frozenD5_equality

end Sphere41
