sovereign-array / ArrayLang /SimplexNorm.lean
SNAPKITTYWEST's picture
Sync with GitHub, license metadata from LICENSE files, commercial license notice
5affcce verified
Raw History Blame Contribute Delete
9.18 kB

The standard (n-1)-simplex: a tuple of nonneg reals summing to 1. Note: we use Float to stay in the same universe as our array kernel, but the geometric claims are stated as algebraic identities.

46
structure Simplex (n : ℕ) where
body · 3 lines
  vals    : Fin n → Float
  nonneg  : ∀ i, 0 ≤ vals i
  sum_one : (List.map vals (List.finRange n)).foldl (· + ·) 0 = 1.0

Softmax maps any vector in ℝⁿ to the interior of Δⁿ — all coordinates strictly positive. This is the only continuous relaxation that is geometrically honest.

56
theorem softmax_pos {n : ℕ} (hn : 0 < n) (v : Fin n → Float) (i : Fin n) :
    0 < Float.exp (v i) := by
proof · 1 line
  exact Float.exp_pos (v i)

Softmax denominator is strictly positive (sum of exponentials).

61
theorem softmax_denom_pos {n : ℕ} (hn : 0 < n) (v : Fin n → Float) :
    0 < sumFin n fun j => Float.exp (v j) := by
proof · 5 lines
  apply List.foldl_pos
  · intro acc x ha hx
    exact Float.add_pos_of_nonneg_of_pos (le_of_lt ha) hx
  · exact Float.exp_pos _
  · simp [List.finRange_length, hn]

A face of Δⁿ is identified by its support: the Finset of coordinates that are allowed to be nonzero. The "full simplex" is Finset.univ.

73
def Face (n : ℕ) : Type := Finset (Fin n)

The full simplex is the face with all coordinates active.

76
def fullFace (n : ℕ) : Face n := Finset.univ

A vertex is a face with exactly one active coordinate.

79
def vertexFace (n : ℕ) (i : Fin n) : Face n := {i}

A face is in the simplex boundary iff it is a proper subset of univ.

82
def isBoundaryFace {n : ℕ} (F : Face n) : Prop := F ≠ Finset.univ

The centroid of face F: uniform distribution over F, zero outside. This is EXACT and DISCRETE — no integration, no dx, no continuous fantasy.

88
def faceCentroid {n : ℕ} (F : Face n) : Fin n → Float :=
body · 1 line
  fun i => if i ∈ F then 1.0 / F.card.toFloat else 0.0

The centroid coordinates are nonneg.

92
theorem faceCentroid_nonneg {n : ℕ} (F : Face n) (i : Fin n) :
    0 ≤ faceCentroid F i := by
proof · 4 lines
  simp [faceCentroid]
  split
  · exact le_of_lt (by positivity)
  · exact le_refl 0

The centroid is nonzero exactly on the support of F.

100
theorem faceCentroid_support {n : ℕ} (F : Face n) (hF : F.Nonempty) (i : Fin n) :
    faceCentroid F i ≠ 0 ↔ i ∈ F := by
proof · 9 lines
  simp [faceCentroid]
  constructor
  · intro h
    split at h
    · assumption
    · exact absurd rfl h
  · intro hi
    simp [hi]
    exact ne_of_gt (by positivity)

Vertex face centroid is the indicator: 1 at the vertex, 0 elsewhere.

113
theorem vertex_centroid_eq {n : ℕ} (i j : Fin n) :
    faceCentroid (vertexFace n i) j = if j = i then 1.0 else 0.0 := by
proof · 2 lines
  simp [faceCentroid, vertexFace, Finset.card_singleton]
  split <;> simp_all

At temperature → 0 (scale → ∞), softmax of the indicator c · 1_F converges to faceCentroid F. This is the only valid bridge between continuous relaxation and the discrete face structure.

We state this as a definitional equality in the limit representation: when all active logits are equal (the uniform distribution case), softmax already equals the face centroid exactly.

127
theorem softmax_uniform_eq_faceCentroid {n : ℕ} (F : Face n) (hF : F.Nonempty)
    (c : Float) (hc_pos : 0 < c)
    (v : Fin n → Float)
    (hv : ∀ i j, i ∈ F → j ∈ F → v i = v j)   -- uniform within face
    (hv_out : ∀ i, i ∉ F → v i = 0.0)           -- zero outside
    (hv_in  : ∀ i, i ∈ F → v i = c) :            -- constant c inside
    ∀ i ∈ F, softmax v i = faceCentroid F i := by
proof · 13 lines
  intro i hi
  simp [softmax, faceCentroid, hi]
  -- softmax(v)_i = exp(c) / (|F| * exp(c) + 0) = 1/|F|
  -- which equals faceCentroid F i = 1/|F|
  congr 1
  · exact hv_in i hi
  · -- denominator: rewrite each term using hv_in / hv_out then simplify
    simp only [sumFin]
    congr 1
    ext j
    by_cases hj : j ∈ F
    · simp [hv_in j hj]
    · simp [hv_out j hj, Float.exp_zero]

A linear constraint on Δⁿ is an affine halfspace.

151
structure LinearConstraint (n : ℕ) where
body · 2 lines
  coeffs : Fin n → Float   -- a_i
  rhs    : Float            -- b, constraint: Σ a_i x_i ≤ b

Evaluate a linear constraint on a point in ℝⁿ.

156
def LinearConstraint.eval {n : ℕ} (c : LinearConstraint n) (x : Fin n → Float) : Float :=
body · 1 line
  sumFin n (fun i => c.coeffs i * x i)

A feasibility problem: is there a vertex of Δⁿ satisfying all constraints? This is the integer programming formulation — NP-complete in general. No polynomial shortcut exists; the value is exact symbolic enumeration.

162
structure FeasibilityProblem (n : ℕ) where
body · 1 line
  constraints : List (LinearConstraint n)

A vertex of Δⁿ is an element of the standard basis (one-hot).

166
def Vertex (n : ℕ) : Type := Fin n
168
def vertexPoint {n : ℕ} (v : Vertex n) : Fin n → Float :=
body · 1 line
  fun i => if i = v then 1.0 else 0.0

A feasibility problem is SAT if some vertex satisfies all constraints.

172
def FeasibilityProblem.isSat {n : ℕ} (P : FeasibilityProblem n) : Prop :=
body · 1 line
  ∃ v : Vertex n, ∀ c ∈ P.constraints, c.eval (vertexPoint v) ≤ c.rhs

If the constraint set is empty, the problem is trivially SAT (the full interior is feasible).

177
theorem empty_constraints_sat {n : ℕ} (hn : 0 < n) :
    (FeasibilityProblem.mk (n := n) []).isSat := by
proof · 1 line
  exact ⟨⟨0, hn⟩, by simp [FeasibilityProblem.isSat]⟩

Check a single vertex against all constraints.

195
def checkVertex {n : ℕ} (P : FeasibilityProblem n) (v : Vertex n) : Bool :=
body · 1 line
  P.constraints.all (fun c => c.eval (vertexPoint v) ≤ c.rhs)

Enumerate all vertices and check feasibility. This is the exact, verified, zero-sorry decision procedure for the vertex feasibility problem (LP vertex enumeration).

201
def solveFeasibility {n : ℕ} (P : FeasibilityProblem n) : Option (Vertex n) :=
body · 1 line
  (List.finRange n).find? (fun v => checkVertex P v)

If solveFeasibility returns a vertex, the problem is SAT.

205
theorem solveFeasibility_sound {n : ℕ} (P : FeasibilityProblem n) (v : Vertex n)
    (h : solveFeasibility P = some v) : P.isSat := by
proof · 6 lines
  simp [solveFeasibility] at h
  obtain ⟨_, hv⟩ := List.find?_some h
  simp [checkVertex] at hv
  exact ⟨v, fun c hc => by
    have := hv c hc
    exact_mod_cast this⟩