Download ArrayLang/SimplexNorm.lean from Snapkitty/sovereign-array: direct link, hf CLI and curl.
- Browser
- Download file 9.18 kB
-
https://huggingface.co/Snapkitty/sovereign-array/resolve/main/ArrayLang/SimplexNorm.lean
- Command line
-
hf download hf://Snapkitty/sovereign-array/ArrayLang/SimplexNorm.lean
-
curl -L -o SimplexNorm.lean https://huggingface.co/Snapkitty/sovereign-array/resolve/main/ArrayLang/SimplexNorm.lean
46The standard
(n-1)-simplex: a tuple of nonneg reals summing to 1. Note: we useFloatto stay in the same universe as our array kernel, but the geometric claims are stated as algebraic identities.
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.056Softmax maps any vector in
ℝⁿto the interior ofΔⁿ— all coordinates strictly positive. This is the only continuous relaxation that is geometrically honest.
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)61Softmax denominator is strictly positive (sum of exponentials).
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]73A face of
Δⁿis identified by its support: theFinsetof coordinates that are allowed to be nonzero. The "full simplex" isFinset.univ.
def Face (n : ℕ) : Type := Finset (Fin n) 76The full simplex is the face with all coordinates active.
def fullFace (n : ℕ) : Face n := Finset.univ 79A vertex is a face with exactly one active coordinate.
def vertexFace (n : ℕ) (i : Fin n) : Face n := {i} 82A face is in the simplex boundary iff it is a proper subset of
univ.
def isBoundaryFace {n : ℕ} (F : Face n) : Prop := F ≠ Finset.univ 88The centroid of face
F: uniform distribution overF, zero outside. This is EXACT and DISCRETE — no integration, nodx, no continuous fantasy.
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.092The centroid coordinates are nonneg.
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 0100The centroid is nonzero exactly on the support of
F.
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)113Vertex face centroid is the indicator: 1 at the vertex, 0 elsewhere.
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_all127At temperature → 0 (scale → ∞), softmax of the indicator
c · 1_Fconverges tofaceCentroid 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.
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]151A linear constraint on
Δⁿis an affine halfspace.
structure LinearConstraint (n : ℕ) where body · 2 lines
coeffs : Fin n → Float -- a_i
rhs : Float -- b, constraint: Σ a_i x_i ≤ b156Evaluate a linear constraint on a point in
ℝⁿ.
def LinearConstraint.eval {n : ℕ} (c : LinearConstraint n) (x : Fin n → Float) : Float := body · 1 line
sumFin n (fun i => c.coeffs i * x i)162A 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.
structure FeasibilityProblem (n : ℕ) where body · 1 line
constraints : List (LinearConstraint n)166A vertex of
Δⁿis an element of the standard basis (one-hot).
def Vertex (n : ℕ) : Type := Fin n def vertexPoint {n : ℕ} (v : Vertex n) : Fin n → Float := body · 1 line
fun i => if i = v then 1.0 else 0.0172A feasibility problem is SAT if some vertex satisfies all constraints.
def FeasibilityProblem.isSat {n : ℕ} (P : FeasibilityProblem n) : Prop := body · 1 line
∃ v : Vertex n, ∀ c ∈ P.constraints, c.eval (vertexPoint v) ≤ c.rhs177If the constraint set is empty, the problem is trivially SAT (the full interior is feasible).
theorem empty_constraints_sat {n : ℕ} (hn : 0 < n) :
(FeasibilityProblem.mk (n := n) []).isSat := by proof · 1 line
exact ⟨⟨0, hn⟩, by simp [FeasibilityProblem.isSat]⟩195Check a single vertex against all constraints.
def checkVertex {n : ℕ} (P : FeasibilityProblem n) (v : Vertex n) : Bool := body · 1 line
P.constraints.all (fun c => c.eval (vertexPoint v) ≤ c.rhs)201Enumerate all vertices and check feasibility. This is the exact, verified, zero-sorry decision procedure for the vertex feasibility problem (LP vertex enumeration).
def solveFeasibility {n : ℕ} (P : FeasibilityProblem n) : Option (Vertex n) := body · 1 line
(List.finRange n).find? (fun v => checkVertex P v)205If
solveFeasibilityreturns a vertex, the problem is SAT.
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⟩Outline
- structure
Simplex46 - theorem
softmax_pos56 - theorem
softmax_denom_pos61 - def
Face73 - def
fullFace76 - def
vertexFace79 - def
isBoundaryFace82 - def
faceCentroid88 - theorem
faceCentroid_nonneg92 - theorem
faceCentroid_support100 - theorem
vertex_centroid_eq113 - theorem
softmax_uniform_eq_faceCentroid127 - structure
LinearConstraint151 - def
LinearConstraint.eval156 - structure
FeasibilityProblem162 - def
Vertex166 - def
vertexPoint168 - def
FeasibilityProblem.isSat172 - theorem
empty_constraints_sat177 - def
checkVertex195 - def
solveFeasibility201 - theorem
solveFeasibility_sound205