#!/usr/bin/env python3
"""
PST Computation 8 — does the substrate furnish the chiral bimodule?
============================================================================
The Stage-6 (Computation 7) residual: chiral SU(2)_L follows IF A_F is represented
chirally (the quaternions H acting on left-handed states only). Track J's bare
ladder did not give that. Two candidate substrate mechanisms were flagged:
  (1) the gamma_M factor in the product Dirac operator
      D = D_M (x) 1 + gamma_M (x) D_F, correlating spacetime and internal
      chirality;
  (2) the directed modal threshold (paper §8: eps = T(C) - tau > 0 breaks the
      L<->R symmetry, the existing PST parity-violation argument).

This Track tests both, honestly.

Sections:
  P1  gamma_M does NOT furnish weak chirality: the physical chirality of an
      internal generator 1 (x) T is governed entirely by [T, gamma_F]; the
      gamma_M factor tensors along and cannot change it. (negative, located)
  P2  the substrate DOES offer SU(2)_L x SU(2)_R: quaternion left- and
      right-multiplications give two commuting su(2)'s (= so(4), the S^3
      isometry). Chirality selection is the choice of one factor, and the
      directed threshold is the physical principle that makes it.
  P3  the residual: the selected SU(2)_L must act on the gamma_F=+ block only
      (the bimodule support). Bridged to the threshold mechanism, not closed.

Run:
    python3 computation_08.py
"""
import numpy as np

SEP = "=" * 78
def hdr(s): print(f"\n{SEP}\n  {s}\n{SEP}")
def comm(A, B): return A @ B - B @ A
def anti(A, B): return A @ B + B @ A

print(SEP)
print("  PST Computation 8 — does the substrate furnish the chiral bimodule?")
print(SEP)

sx = np.array([[0,1],[1,0]], dtype=complex)
sy = np.array([[0,-1j],[1j,0]], dtype=complex)
sz = np.array([[1,0],[0,-1]], dtype=complex)
sp = np.array([[0,1],[0,0]], dtype=complex)   # raising
I2 = np.eye(2, dtype=complex)

# ─────────────────────────────────────────────────────────────────────
# §1. gamma_M does not furnish weak chirality
# ─────────────────────────────────────────────────────────────────────
hdr("§1 — the gamma_M cross-term does NOT fix weak chirality")
print("""
  In the product, the weak generators act only on the internal factor:
  T_a -> 1_M (x) T_a. The physical chirality is gamma = gamma_M (x) gamma_F.
  Then
      [1 (x) T, gamma_M (x) gamma_F] = gamma_M (x) [T, gamma_F],
  so whether the weak generator is chiral with respect to the PHYSICAL
  grading is governed entirely by [T, gamma_F] (the internal commutator);
  the gamma_M factor tensors along and cannot change it. We confirm this on
  the Track J ``mixing'' ladder.
""")
gM = sz                                   # spacetime chirality gamma_M
gF = sz                                   # an internal (weak) grading gamma_F
T  = sp                                   # a ladder weak generator (Track J)
gamma = np.kron(gM, gF)                   # physical chirality
Tprod = np.kron(I2, T)                    # 1 (x) T

lhs = comm(Tprod, gamma)
rhs = np.kron(gM, comm(T, gF))
print(f"  [1(x)T, gamma_M(x)gamma_F] = gamma_M(x)[T,gamma_F] : "
      f"{np.allclose(lhs, rhs)}")
print(f"  internal [T,gamma_F] = 0 (would be chiral-safe): "
      f"{np.allclose(comm(T,gF),0)}")
print(f"  so physical [1(x)T, gamma] = 0 : {np.allclose(lhs,0)}")
print("""
  Reading: the internal ladder has [T,gamma_F] != 0 (it mixes the internal
  grading), so 1(x)T mixes the PHYSICAL chirality too, regardless of
  gamma_M. The gamma_M cross-term cannot rescue weak chirality; the chiral
  property is an internal-representation fact. Mechanism (1) is ruled out as
  a stand-alone fix. (This sharpens the Computation 7 residual: it is specifically
  about the internal H-action, not about the spacetime grading.)
""")

# ─────────────────────────────────────────────────────────────────────
# §2. The substrate offers SU(2)_L x SU(2)_R (= so(4)); pick one
# ─────────────────────────────────────────────────────────────────────
hdr("§2 — quaternion left/right multiplication: two commuting su(2)'s")
def q_conj(x): xc=-x.copy(); xc[0]=x[0]; return xc
def q_mul(x,y):
    n=len(x)
    if n==1: return np.array([x[0]*y[0]])
    h=n//2; a,b=x[:h],x[h:]; c,d=y[:h],y[h:]
    return np.concatenate([q_mul(a,c)-q_mul(q_conj(d),b),
                           q_mul(d,a)+q_mul(b,q_conj(c))])
def qe(i): v=np.zeros(4); v[i]=1.0; return v
# left-mult L_i: x -> e_i x ;  right-mult R_i: x -> x e_i
Lq = [np.column_stack([q_mul(qe(i), qe(k)) for k in range(4)]) for i in range(4)]
Rq = [np.column_stack([q_mul(qe(k), qe(i)) for k in range(4)]) for i in range(4)]

# both close as su(2)
Lclose = all(np.allclose(comm(Lq[i],Lq[j]), 2*Lq[k]) for (i,j,k) in [(1,2,3),(2,3,1),(3,1,2)])
Rclose = all(np.allclose(comm(Rq[i],Rq[j]), -2*Rq[k]) for (i,j,k) in [(1,2,3),(2,3,1),(3,1,2)])
# they commute (associativity: (e_i x) e_j = e_i (x e_j))
LRcomm = all(np.allclose(comm(Lq[i], Rq[j]), 0) for i in (1,2,3) for j in (1,2,3))
print(f"  su(2)_L closes  [L_i,L_j] = 2 eps L_k          : {Lclose}")
print(f"  su(2)_R closes  [R_i,R_j] = -2 eps R_k         : {Rclose}")
print(f"  [L_i, R_j] = 0  (left and right commute)       : {LRcomm}")
print(f"  => su(2)_L (+) su(2)_R = so(4), the isometry algebra of S^3.")
print("""
  The substrate thus supplies BOTH a left and a right weak SU(2). The
  Standard Model uses one of them, SU(2)_L. The selection is exactly the
  parity-violation mechanism PST already states (paper §8): the directed
  modal threshold eps = T(C) - tau > 0 is an oriented, irreversible crossing
  that breaks the L<->R (SU(2)_R) symmetry and selects a handedness. So the
  chirality CHOICE is furnished by an existing PST mechanism, not put in by
  hand: the threshold orientation picks su(2)_L out of so(4).
""")

# ─────────────────────────────────────────────────────────────────────
# §3. The remaining piece: bimodule support
# ─────────────────────────────────────────────────────────────────────
hdr("§3 — the residual: SU(2)_L must act on the gamma_F=+ block only")
print("""
  Selecting su(2)_L out of so(4) (§2) fixes WHICH SU(2) is the weak group.
  The CCM chiral bimodule additionally requires the selected su(2)_L to act
  on the left-handed block (gamma_F = +1) and to annihilate the right-handed
  singlets (Computation 7 §1). That support property is what makes order-one
  deliver the chiral Standard Model (Computation 7 §3). It is a property of HOW
  the selected su(2)_L sits relative to the chirality grading gamma_F.

  Status of chirality after Computation 8:
    - Mechanism (1), the gamma_M cross-term, is ruled out as a stand-alone
      fix (§1): physical weak chirality is governed by the internal
      [T, gamma_F], which gamma_M cannot change.
    - Mechanism (2), the directed threshold, is a genuine substrate
      principle that selects su(2)_L out of the substrate's su(2)_L (+)
      su(2)_R (§2). This BRIDGES the chirality residual to PST's existing
      parity-violation argument: the same oriented threshold that gives
      parity violation selects the chiral weak group.
    - What remains: a proof that the threshold-selected su(2)_L acts on the
      gamma_F=+ block only (the bimodule support). This is the sharp, single
      remaining matter-sector question. It is no longer ``does a chiral
      structure exist'' (Computation 7: yes) nor ``which SU(2)'' (Computation 8 §2: the
      threshold-selected one), but ``does the threshold orientation place the
      selected su(2)_L on the left block''.

  Honest verdict: chirality is reduced and now BRIDGED to PST's directed-
  threshold mechanism, not closed. The gamma_M route is dead; the threshold
  route is the live candidate and is the same mechanism PST already uses for
  parity violation, which is a genuine consolidation rather than a new posit.
""")
