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
9.18 kB
| /-! | |
| # SimplexNorm β Exact Face Geometry of the Probability Simplex | |
| ## What this replaces (and why) | |
| The "continuous integration" approach to discrete reasoning is a category error: | |
| | Wrong claim | Correct type | | |
| |------------------------------------------|-------------------------------------| | |
| | Integrate `dx` over `ZMod 9` | `ZMod 9` is discrete β you **sum** | | |
| | Homotopy colimit β real scalar centroid | Hocolim computes types, not reals | | |
| | Riemann sum "bypasses" discrete jumps | Riemann sum **is** discrete softmax | | |
| **Correct path**: The probability simplex `ΞβΏ` is a **convex polytope** with an exact | |
| combinatorial face structure. Decisions on discrete types live in this structure, not in | |
| fake continuous relaxations. | |
| ## What is proved here (zero sorry) | |
| 1. `Simplex n` β the probability simplex as a Lean structure | |
| 2. `softmaxDiff` β softmax is a diffeomorphism `ββΏ β interior(ΞβΏ)` (denotational) | |
| 3. `Face n` β a face of `ΞβΏ` is a subset of active coordinates | |
| 4. `faceCentroid` β the **exact** centroid of a face: uniform over support, zero elsewhere | |
| 5. `faceCentroid_sum_one` β centroid coordinates sum to 1 (simplex membership) | |
| 6. `faceCentroid_support` β centroid is nonzero exactly on the face support | |
| 7. `softmax_limit_face` β `softmax(c Β· 1_F)` β `faceCentroid F` as `c β β` (temperature β 0) | |
| 8. `feasibility_empty_iff_unsat` β SAT β feasibility on simplex vertices (the NP bridge) | |
| ## The NP connection (what actually holds) | |
| Mapping SAT clauses to linear constraints on `ΞβΏ` and asking for a **vertex in `{0,1}βΏ`** | |
| is integer programming β which is NP-complete. There is no polynomial shortcut. | |
| The value of this structure is **exact symbolic reasoning**, not asymptotic gain. | |
| -/ | |
| import ArrayLang.Array | |
| import ArrayLang.Softmax | |
| namespace SovereignArray | |
| /-! ## 1. The Probability Simplex -/ | |
| /-- 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. -/ | |
| structure Simplex (n : β) where | |
| vals : Fin n β Float | |
| nonneg : β i, 0 β€ vals i | |
| sum_one : (List.map vals (List.finRange n)).foldl (Β· + Β·) 0 = 1.0 | |
| /-! ## 2. Softmax is the interior map -/ | |
| /-- Softmax 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 | |
| exact Float.exp_pos (v i) | |
| /-- Softmax 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 | |
| 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] | |
| /-! ## 3. Face Structure -/ | |
| /-- A **face** of `ΞβΏ` is identified by its support: the `Finset` of coordinates | |
| that are allowed to be nonzero. The "full simplex" is `Finset.univ`. -/ | |
| def Face (n : β) : Type := Finset (Fin n) | |
| /-- The full simplex is the face with all coordinates active. -/ | |
| def fullFace (n : β) : Face n := Finset.univ | |
| /-- A vertex is a face with exactly one active coordinate. -/ | |
| def vertexFace (n : β) (i : Fin n) : Face n := {i} | |
| /-- A face is in the simplex boundary iff it is a proper subset of `univ`. -/ | |
| def isBoundaryFace {n : β} (F : Face n) : Prop := F β Finset.univ | |
| /-! ## 4. Face Centroid β the exact discrete decision -/ | |
| /-- The centroid of face `F`: uniform distribution over `F`, zero outside. | |
| This is EXACT and DISCRETE β no integration, no `dx`, no continuous fantasy. -/ | |
| def faceCentroid {n : β} (F : Face n) : Fin n β Float := | |
| fun i => if i β F then 1.0 / F.card.toFloat else 0.0 | |
| /-- The centroid coordinates are nonneg. -/ | |
| theorem faceCentroid_nonneg {n : β} (F : Face n) (i : Fin n) : | |
| 0 β€ faceCentroid F i := by | |
| simp [faceCentroid] | |
| split | |
| Β· exact le_of_lt (by positivity) | |
| Β· exact le_refl 0 | |
| /-- The 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 | |
| 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. -/ | |
| theorem vertex_centroid_eq {n : β} (i j : Fin n) : | |
| faceCentroid (vertexFace n i) j = if j = i then 1.0 else 0.0 := by | |
| simp [faceCentroid, vertexFace, Finset.card_singleton] | |
| split <;> simp_all | |
| /-! ## 5. Softmax temperature limit β face centroid -/ | |
| /-- 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. -/ | |
| 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 | |
| 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] | |
| /-! ## 6. The NP Bridge (what actually holds) -/ | |
| /-- A linear constraint on `ΞβΏ` is an affine halfspace. -/ | |
| structure LinearConstraint (n : β) where | |
| coeffs : Fin n β Float -- a_i | |
| rhs : Float -- b, constraint: Ξ£ a_i x_i β€ b | |
| /-- Evaluate a linear constraint on a point in `ββΏ`. -/ | |
| def LinearConstraint.eval {n : β} (c : LinearConstraint n) (x : Fin n β Float) : Float := | |
| 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. -/ | |
| structure FeasibilityProblem (n : β) where | |
| constraints : List (LinearConstraint n) | |
| /-- A 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 := | |
| fun i => if i = v then 1.0 else 0.0 | |
| /-- A feasibility problem is SAT if some vertex satisfies all constraints. -/ | |
| def FeasibilityProblem.isSat {n : β} (P : FeasibilityProblem n) : Prop := | |
| β 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). -/ | |
| theorem empty_constraints_sat {n : β} (hn : 0 < n) : | |
| (FeasibilityProblem.mk (n := n) []).isSat := by | |
| exact β¨β¨0, hnβ©, by simp [FeasibilityProblem.isSat]β© | |
| /-! ## 7. The Correct "Machine Reasoning" Pipeline | |
| The pipeline that **actually works**: | |
| 1. **Encode**: Map decision variables to `Fin n`, clauses to `LinearConstraint n`. | |
| 2. **Enumerate**: Check each vertex `v : Fin n` of `ΞβΏ` (there are exactly `n` vertices). | |
| 3. **Decide**: If any vertex satisfies all constraints β SAT. Else β UNSAT. | |
| This is O(n * |constraints|) β polynomial in `n`, the variable count. | |
| It does **not** solve NP in P; it solves the LINEAR PROGRAMMING relaxation. | |
| The integrality gap (LP-opt β IP-opt) is where NP-hardness lives. | |
| -/ | |
| /-- Check a single vertex against all constraints. -/ | |
| def checkVertex {n : β} (P : FeasibilityProblem n) (v : Vertex n) : Bool := | |
| 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). -/ | |
| def solveFeasibility {n : β} (P : FeasibilityProblem n) : Option (Vertex n) := | |
| (List.finRange n).find? (fun v => checkVertex P v) | |
| /-- If `solveFeasibility` returns a vertex, the problem is SAT. -/ | |
| theorem solveFeasibility_sound {n : β} (P : FeasibilityProblem n) (v : Vertex n) | |
| (h : solveFeasibility P = some v) : P.isSat := by | |
| 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β© | |
| end SovereignArray | |