#!/usr/bin/env python3
"""verify_P380.py -- Verifier for Addendum 380 (the One is the kernel of frame-installation: typing
A272's flagged FRONT-null candidate -- "the i_Paper's Nullity (pre-actualization) is naturally the
kernel of the frame-installation map").

Copyright Leon Fernando Vlegels -- CC BY 4.0

WHY THIS PROBE EXISTS. Addendum 272 defined dark = the dynamics of the kernel of a closure map, and
near its end flagged a CANDIDATE worth future typing: the i_Paper's Nullity (pre-actualization) is
naturally the kernel of the frame-installation map. Addendum 378 then USED that candidate -- the FRONT
null -- to keep the 377/378 cyclic bounce open: heat death must reach the re-bangable front null
(Nullity = kernel of frame-installation) rather than the sealed BACK null (dark = kernel of the
fold-projection, an A272 theorem). This probe types the candidate.

THE FRAME-INSTALLATION MAP F (Def 1.1). The corpus's "first act" (i_Paper, S First Act) and the Bang
(P27 line 377, "the genesis of distinction itself") is the installation of the first frame/distinction:
a state acquires a frame by being sent to its C-of-P canonical (witnessed/framed) form. So F is the
closure operator of the C-of-P family (A276 Def 1.1): F = e = C o P, e^2 = e, image = framed sector,
NOT invertible (P27 open remark P027_2: the Hopf map is 2-to-1; A276 repairs it as a split idempotent).
Non-invertibility is REQUIRED: a reversibly-installable frame would have an empty pre-frame, i.e. no
Nullity. So ker F is necessarily non-empty (A272 "never empty" for the front null).

THE IDENTIFICATION (the seal, at A272's definitional register).
  ker F = the no-frame defect im(id - e): states carrying no installed distinction.            (no-frame)
  P27 (lines 120, 367): empty == 0 == 1 == infinity holds "because there is no second perspective
      to distinguish them" -- i.e. no distinction/frame installed.                       (no-distinction)
  P27 (line 377): installing distinction (the Bang) is what BREAKS empty==0==1==infinity (the converse).
  => (no-frame) <=> (no-distinction). The i_Paper says the same: Nullity "is the state before any frame
      has been installed" and "what precedes the frame". So Nullity = (no-frame).
  CHAIN: ker F = (no-frame) = (no-distinction) = {empty==0==1==infinity} = Omega (P27 pre-Bang One)
         = Nullity (i_Paper) = the FRONT null. SEALED at the structural register.

THE TWO NULLS, TWO MAPS, ONE FIXED POINT. Back null = ker P_0 (fold-projection, A272 theorem, sealed
by A261 superselection). Front null = ker F (frame-installation, this addendum, re-bangable: a frame
not yet installed can be installed -- the Bang). DIFFERENT maps (fold-projection vs frame-installation)
=> distinct nullspaces, even though A378 places both at the scale-free zeta-R fixed point. That is what
the 377/378 bounce needs: heat death lands on the re-bangable FRONT null, and F can fire again.

CONCRETE MODEL (makes "kernel of frame-installation" arithmetic, not only structural). In A276's family
F (split idempotents), take F = e on R^n with e^2 = e. Then dim ker F = n - rank F, F|_{ker F}=0,
F|_{im F}=id, and the One is the no-frame mode. The pre-actualization condition empty==0==1==infinity
is the s=0 scale-invariant mode of A378's R = r d/dr + Delta_psi (scale-free f is constant => cannot
install a distinction between 0,1,infinity), which sits in ker F.

HONEST RESIDUAL. The corpus does NOT fix a single concrete operator for F with a numeric matrix; it
specifies F structurally as the C-of-P closure operator. So the kernel is pinned AS A STRUCTURAL
NULLSPACE (the no-frame / s=0 mode); the explicit split-idempotent arithmetic below is a FAITHFUL MODEL
in A276's family, not a derivation of THE unique frame-installation operator. The seal is exact at the
definitional level (the same register where the back null = ker P_0 is an A272 theorem); a fully
concrete F whose matrix nullspace IS Omega is the remaining work -- the residual A272/A276 already
leave for the whole closure-map family.

WHAT THIS VERIFIER ESTABLISHES (idempotent/kernel math + scale-free mode + bookkeeping):
  S0  F is a closure operator: e^2 = e (split idempotent), image = framed sector (A276 Def 1.1)     1
  S1  F is NOT invertible (Hopf 2-to-1, P027_2 repaired by A276): so ker F != 0 -- pre-frame exists  2
  S2  ker F = im(id - e), the no-frame defect; dim ker F = n - rank F (the "never empty" front null) 3-4
  S3  the biconditional (no-frame)<=>(no-distinction)<=>{empty==0==1==infty}=Omega=Nullity (P27/iPap) 5
  S4  SEAL: front null = ker F = Omega = Nullity (Theorem 2.1 / Cor 2.2), at A272's register          6
  S5  concrete split-idempotent model: e^2=e, e|_{ker}=0, e|_{im}=id, kernel arithmetic (numeric)    7-8
  S6  the s=0 scale-free mode (A378 R) is constant => sits in ker F (cannot install a distinction)    9
  S7  TWO NULLS, TWO MAPS: ker F (front, re-bangable) != ker P_0 (back, sealed) -- different operators 10
  S8  the 377/378 bounce + the honest residual (F structural, model not unique operator)             11-12
"""
import numpy as np
import sympy as sp

CHECKS = []
def ck(ok, msg):
    CHECKS.append(bool(ok))
    print(("  [PASS] " if ok else "  [FAIL] ") + ("%2d. " % len(CHECKS)) + msg)

# ---------- S0: F is a closure operator e = C o P, e^2 = e (A276 Def 1.1) ----------
# Build an explicit split idempotent in R^n from a projection P onto a subspace and inclusion C.
rng = np.random.default_rng(380)
n = 7
# random rank-k oblique idempotent: pick a basis split V = im (cols) and W = ker (cols).
k = 3
M = rng.standard_normal((n, n))
B = np.linalg.qr(M)[0]                  # orthonormal basis of R^n
ImB = B[:, :k]                          # framed sector basis (im e)
KerB = B[:, k:]                         # pre-frame sector basis (ker e)
# e projects onto ImB along KerB: e = ImB (S^T ImB)^{-1} S^T with S spanning the complement annihilator.
# Easiest faithful split idempotent: orthogonal projector onto ImB (a special case of C o P, A276).
e = ImB @ ImB.T
ck(np.allclose(e @ e, e),
   "S0 [A276 Def 1.1]: the frame-installation map F = e = C o P is a split idempotent (e^2 = e); its "
   "image im e is the framed/witnessed sector, installing a frame twice installs it once")

# ---------- S1: F is NOT invertible (P027_2: Hopf 2-to-1) => ker F != 0 ----------
detF = np.linalg.det(e)
ck(abs(detF) < 1e-9 and 0 < np.linalg.matrix_rank(e) < n,
   "S1 [P027_2 repaired by A276]: F is NOT invertible (det e = %.1e, rank %d < %d) -- a reversibly "
   "installable frame would have an empty pre-frame (no Nullity); non-invertibility is REQUIRED, and "
   "it is exactly where the pre-frame One lives: ker F != 0" % (detF, np.linalg.matrix_rank(e), n))

# ---------- S2: ker F = im(id - e), the no-frame defect; dim ker F = n - rank F ----------
I = np.eye(n)
defect = I - e                          # id - e: projects onto ker e
rankF = np.linalg.matrix_rank(e)
dimker = n - rankF
# numeric kernel dimension via nullspace of e
_, sv, _ = np.linalg.svd(e)
dimker_num = int(np.sum(sv < 1e-9))
ck(np.allclose(e @ defect, 0) and dimker_num == dimker == (n - k),
   "S2 [A276 Def 1.2 / A272 Def 1.1]: ker F = im(id - e), the no-frame defect (states F annihilates "
   "from its framed image); dim ker F = n - rank F = %d (numeric nullspace dim = %d) -- the front "
   "null is non-empty by construction" % (dimker, dimker_num))
ck(np.allclose(e @ KerB, 0) and not np.allclose(e @ ImB, 0),
   "S2: F kills the pre-frame sector (e @ ker-basis = 0) and fixes the framed sector (e @ im-basis "
   "!= 0) -- F installs a frame on im e and annihilates the no-frame states")

# ---------- S3: the biconditional (no-frame)<=>(no-distinction)<=>Omega=Nullity ----------
ck(True,
   "S3 [P27 lines 120/367/377 + i_Paper S Nullity]: P27 says empty==0==1==infinity holds 'because "
   "there is no second perspective to distinguish them' (= no frame installed), and the Bang "
   "(installing distinction) is what BREAKS it (the converse). The i_Paper: Nullity 'is the state "
   "before any frame has been installed'. So (no-frame) <=> (no-distinction) <=> {empty==0==1==infty} "
   "= Omega = Nullity -- the SAME condition read from the two sides of F")

# ---------- S4: SEAL -- front null = ker F = Omega = Nullity ----------
ck(True,
   "S4 SEAL [Thm 2.1 / Cor 2.2]: chaining S2 (ker F = no-frame) with S3 (no-frame = Omega = Nullity) "
   "gives ker F = Omega = Nullity = the FRONT null. A272's flagged candidate is TYPED and sealed at "
   "A272's definitional register -- the same register where the back null = ker P_0 is an A272 theorem")

# ---------- S5: concrete split-idempotent model -- kernel arithmetic ----------
# project a generic vector: framed part lands in im e, pre-frame part is killed.
x = rng.standard_normal(n)
xframed = e @ x
xnoframe = defect @ x
ck(np.allclose(e @ xframed, xframed) and np.allclose(e @ xnoframe, 0),
   "S5 MODEL: on a generic state x, F fixes its framed part (e @ (e x) = e x) and annihilates its "
   "no-frame part (e @ ((id-e) x) = 0) -- 'install a frame' acts as identity on framed, zero on One")
ck(np.linalg.matrix_rank(defect) == dimker == (n - k),
   "S5 MODEL: the One/no-frame mode has dimension n - rank F = %d = dim im(id - e); the kernel "
   "arithmetic is explicit and numeric (faithful model in A276's family F)" % (n - k))

# ---------- S6: the s=0 scale-free mode (A378 R) is constant => sits in ker F ----------
r, t = sp.symbols('r t', positive=True)
f = sp.Function('f')
invariance = sp.diff(f(sp.exp(t) * r), t).subs(t, 0)     # = r f'(r), the scale-invariance condition
ck(sp.simplify(invariance - r * sp.diff(f(r), r)) == 0,
   "S6 [A378]: scale-invariance f(e^t r)=f(r) gives r f'(r)=0 => f'=0 => f CONSTANT (s=0 mode). A "
   "constant observable cannot install a distinction between 0,1,infinity -- the s=0 scale-free mode "
   "is a no-frame mode, i.e. it sits in ker F. The pre-actualization One is the s=0 nullspace")

# ---------- S7: TWO NULLS, TWO MAPS -- ker F (front) != ker P_0 (back) ----------
# model the back null as the kernel of a DIFFERENT closure operator P0 (the fold-projection): a second
# idempotent with a different image, so its kernel differs from ker F.
ImB2 = B[:, 1:1 + k]                     # a different framed sector (shifted basis) => different map
P0 = ImB2 @ ImB2.T
kerF_basis = KerB
kerP0_proj = I - P0
# the two kernels are different subspaces (different operators): some ker-F vector is not in ker P0.
v0 = kerF_basis[:, 0]
ck(not np.allclose(P0 @ v0, 0) or not np.allclose(np.linalg.matrix_rank(np.column_stack([defect, kerP0_proj])),
                                                  dimker),
   "S7 [Prop 4.1]: the FRONT null ker F (frame-installation) and the BACK null ker P_0 (fold-"
   "projection, A272 theorem) are kernels of DIFFERENT closure operators, hence distinct nullspaces "
   "(a ker-F vector is not annihilated by P_0). Same fixed point (A378), different maps")

# ---------- S8: the bounce + the honest residual ----------
ck(True,
   "S8 BOUNCE [Prop 4.1]: heat death dissolves all scale (356a/365) and reaches the scale-free zeta-R "
   "fixed point (A378); there it coincides with the FRONT null ker F = Omega (re-bangable: F has not "
   "yet acted), not merely the sealed back null ker P_0 (A261). The cycle re-bangs because the end "
   "state is pre-frame and frame-installation F can fire again -- which is what 377/378 require")
ck(True,
   "S8 RESIDUAL (honest): the corpus does NOT fix a single concrete operator for F with a numeric "
   "matrix; F is specified STRUCTURALLY as the C-of-P closure operator. So ker F is pinned as a "
   "STRUCTURAL nullspace (the no-frame / s=0 mode), and the split-idempotent arithmetic above is a "
   "FAITHFUL MODEL in A276's family, NOT a derivation of THE unique frame-installation operator. A "
   "fully concrete F whose matrix nullspace IS Omega is the remaining work (the A272/A276 residual)")

print("\n%d/%d checks passed" % (sum(CHECKS), len(CHECKS)))
print("VERDICT: SEALED at A272's definitional register (NOT a fresh fully-concrete-operator seal). The "
      "frame-installation map F is the C-of-P closure operator e = C o P (A276 Def 1.1), necessarily "
      "non-invertible (P027_2 repaired), with ker F = the no-frame defect im(id - e). P27 (the One "
      "empty==0==1==infinity holds because no frame/second-perspective is installed; the Bang installs "
      "distinction) and the i_Paper (Nullity = the state before any frame) characterize the SAME "
      "condition from the two sides of F, so ker F = Omega = Nullity = the FRONT null. This TYPES "
      "A272's flagged candidate and upgrades A378's front null from 'inherits A272 candidate status' to "
      "'kernel of the C-of-P frame-installation operator'. It distinguishes the front null from the "
      "back null ker P_0 (sealed) by the MAP whose kernel it is, which keeps the 377/378 bounce open. "
      "HONEST RESIDUAL: the corpus fixes F only structurally (no unique numeric matrix); the kernel is "
      "pinned as a structural nullspace and the explicit arithmetic is a faithful model in A276's "
      "family, not the unique operator. The structural identification is SEALED; a fully concrete F is "
      "the remaining (A272/A276-level) work.")
