#!/usr/bin/env python3
"""
PROVENANCE: PROOF

Computation 114 -- Bridge Premise (B), milestone M5:
                   is (Pi-total) true?  Vacuum-amplitude vs two-point
                   normalisation of the substrate->Higgs reduction
=========================================================================
STATUS (v26.13): M5 of the wave-function attack on Bridge Premise (B).
M4(a) (Comp 112) closed the value beta=2. M4(b) (Comp 113) reduced the
last residual to one property, (Pi-total): the substrate->Higgs
projection Pi is the TOTAL reduction, so the Higgs field-strength absorbs
the full substrate vacuum amplitude (1/2^D)Tr exp(-Delta/D) = e^(-1).
This computation tests (Pi-total) ab initio -- and it is built to FAIL
loudly if the bridge needs a normalisation the reduction does not supply.

THE SHARP QUESTION
==================
The bridge factor is e^(-1). Which normalisation of the substrate->Higgs
reduction is it?

  (A) the TEMPERED VACUUM AMPLITUDE -- the tempered partition function /
      embedding norm of the Higgs sector in the substrate Hilbert space:
          Z_A = E_mu[exp(-beta X_bar)] = (1/2^D) Tr exp(-Delta/D) -> e^(-1);
  (B) the CANONICAL TWO-POINT RESIDUE -- the standard EFT wave-function
      renormalisation, i.e. the connected two-point function of the order
      parameter that integrating out heavy modes corrects:
          Z_B = Var_mu(X_bar) = 1/(4D) -> 0.

These are DIFFERENT objects and they have DIFFERENT limits. The bridge
needs e^(-1), so it needs (A). Standard Wilsonian integrating-out of heavy
modes delivers (B) -- the two-point residue -- which here vanishes. So:

  - if the substrate->Higgs reduction normalises like a STANDARD EFT
    (route B), the bridge factor would be ~1/(4D) -> 0, NOT e^(-1):
    (B) is EXCLUDED by the data (the coupling is finite);
  - the bridge therefore requires the NON-STANDARD route (A): the
    reduction normalises by the full tempered vacuum amplitude.

(Pi-total) is precisely the claim that route (A) is correct: that Pi
folds the ENTIRE substrate measure into the single Higgs sector's
normalisation, rather than leaving the Higgs as one light field whose
2-point residue is corrected by heavy loops.

WHAT M5 CAN AND CANNOT DECIDE
=============================
M5 can: compute both normalisations exactly; show the bridge selects (A)
and that the standard reading (B) is excluded; and identify the precise
structural content of (A) -- the embedding-norm reading, valid because the
Higgs is an EMERGENT PROJECTED field phi = Pi(psi), not a fundamental
field, so its normalisation is its (tempered) norm inside the substrate
Hilbert space.

M5 cannot, from lattice combinatorics alone, DERIVE that the reduction is
total rather than standard-EFT. That distinction is the structural
content of Pi, which descends from P3 (modal sublimation collapses the
substrate onto the single symmetric order parameter). So M5 sharpens (B)
to a single binary structural question, shows one answer is excluded by
data, and shows the surviving answer is the embedding-norm reading
grounded in P3 -- but it does not eliminate the need for that P3-level
input.
=========================================================================
"""

import math
import itertools


def xbar(C):
    return sum(C) / len(C)


def all_configs(D):
    return itertools.product((0, 1), repeat=D)


def vacuum_amplitude(D, beta=2.0):
    """Route (A): tempered vacuum amplitude / embedding norm -> e^-1."""
    return ((1.0 + math.exp(-beta / D)) / 2.0) ** D


def twopoint_residue(D):
    """Route (B): canonical 2-pt residue Var_mu(X_bar) = 1/(4D)."""
    # exact under Bernoulli(1/2): Var(X_bar) = Var(|C|)/D^2 = (D/4)/D^2
    return 1.0 / (4.0 * D)


def embedding_norm_uniform_state(D, beta=2.0):
    """
    Route (A) as an embedding norm, computed directly: the norm of the
    uniform substrate state (constant Walsh mode chi_empty = 1) under the
    beta-tempered inner product <f|exp(-beta X_bar)|f>.  This is the
    Higgs-vacuum normalisation under the total-reduction reading.
    Returns E_mu[ |chi_empty|^2 exp(-beta X_bar) ] = E_mu[exp(-beta X_bar)].
    """
    Z = 0.0
    for C in all_configs(D):
        Z += math.exp(-beta * xbar(C))           # |chi_empty|^2 = 1
    return Z / (2 ** D)


def main():
    print("=" * 72)
    print("Computation 114: M5 -- is (Pi-total) true?")
    print("Vacuum-amplitude (A) vs two-point-residue (B) normalisation")
    print("=" * 72)
    print()

    e_inv = math.exp(-1.0)

    # ---- 1. the two normalisations have different limits ----
    print("1.  THE TWO CANDIDATE NORMALISATIONS DIVERGE")
    print("-" * 72)
    print(f"    {'D':>7} {'(A) vacuum ampl':>17} {'(B) 2-pt residue':>18}")
    for D in (10, 100, 1000, 10000):
        print(f"    {D:>7} {vacuum_amplitude(D):>17.6f} {twopoint_residue(D):>18.6e}")
    print(f"    {'limit':>7} {e_inv:>17.6f} {0.0:>18.6e}")
    print()
    print("    (A) -> e^-1 (finite); (B) -> 0. The bridge needs e^-1, so")
    print("    it selects (A). The STANDARD-EFT reading (B) -- the order")
    print("    parameter as a light field whose 2-pt residue is corrected")
    print("    by heavy-mode loops -- gives 1/(4D) -> 0 and is EXCLUDED by")
    print("    the data (lambda_SM is finite, not zero).")
    print()

    # ---- 2. route (A) IS the embedding norm of the Higgs vacuum ----
    print("2.  ROUTE (A) = TEMPERED EMBEDDING NORM OF THE HIGGS VACUUM")
    print("-" * 72)
    print("    The Higgs is an emergent PROJECTED field phi = Pi(psi), not")
    print("    fundamental. Its normalisation is its norm inside the")
    print("    substrate Hilbert space under the tempered (modal-sublimated)")
    print("    inner product. For the symmetric vacuum (uniform Walsh mode")
    print("    chi_empty = 1, selected by P3):")
    print()
    print(f"    {'D':>4} {'embedding norm':>16} {'vacuum amplitude':>18} {'match':>7}")
    for D in (2, 4, 6, 8):
        emb = embedding_norm_uniform_state(D)
        vac = vacuum_amplitude(D)
        ok = "yes" if abs(emb - vac) < 1e-12 else "NO"
        print(f"    {D:>4} {emb:>16.8f} {vac:>18.8f} {ok:>7}")
    print()
    print("    Exactly equal: the embedding norm of the symmetric vacuum IS")
    print("    the tempered vacuum amplitude. So route (A) is not an ad-hoc")
    print("    choice -- it is the norm of the Higgs vacuum as embedded in")
    print("    the substrate, which is the correct normalisation for an")
    print("    emergent projected field.")
    print()

    # ---- 3. the standard-EFT reading would need a heavy-light split ----
    print("3.  WHY THE STANDARD-EFT READING DOES NOT APPLY")
    print("-" * 72)
    print("    Standard Wilsonian integrating-out assumes a fundamental")
    print("    light field whose 2-pt function is corrected by heavy loops")
    print("    -> route (B). But in PST the Higgs is NOT a pre-existing")
    print("    light field: it is the single order parameter that the WHOLE")
    print("    substrate collapses onto past the LG threshold (P3). There")
    print("    is no heavy/light split internal to the Higgs sector -- the")
    print("    entire substrate measure normalises the one surviving mode.")
    print("    That is exactly (Pi-total), and it forces route (A).")
    print()
    print("    Consistency with Comp 98: all |S|>=2 Walsh shells are trivial")
    print("    fixed points (no residual light content), so nothing is left")
    print("    to play the role of an independent light Higgs field with its")
    print("    own 2-pt residue -- consistent with total reduction.")
    print()

    # ---- 4. honest assessment ----
    print("=" * 72)
    print("ASSESSMENT: does M5 close (Pi-total), hence (B)?")
    print("=" * 72)
    print()
    print("  ESTABLISHED:")
    print("   - The bridge factor is route (A), the tempered vacuum")
    print("     amplitude = e^-1, and equals the embedding norm of the")
    print("     symmetric Higgs vacuum (verified exactly, part 2).")
    print("   - The standard-EFT reading (B), the 2-pt residue, gives")
    print("     1/(4D) -> 0 and is EXCLUDED by data (part 1). So (B) is not")
    print("     a matter of taste: the only normalisation consistent with a")
    print("     finite lambda_SM is the vacuum-amplitude / embedding-norm one.")
    print()
    print("  NOT DERIVED FROM LATTICE COMBINATORICS ALONE:")
    print("   - That Pi is TOTAL rather than standard-EFT -- i.e. that the")
    print("     Higgs is the emergent projected collapse of the whole")
    print("     substrate (no internal heavy/light split), normalised by")
    print("     the full tempered measure. This is the structural content")
    print("     of P3 (modal sublimation onto the single symmetric order")
    print("     parameter), not a combinatorial identity.")
    print()
    print("  NET RESULT:")
    print("   M5 does NOT reduce (B) to zero assumptions, but it makes the")
    print("   remaining content a SHARP STRUCTURAL QUESTION with the standard")
    print("   field-theoretic answer excluded by data:")
    print()
    print("     Does the substrate->Higgs reduction normalise by the full")
    print("     tempered vacuum amplitude (route A, total reduction, B holds")
    print("     -> lambda_SM = b e^-1) or by a marginal 2-pt residue")
    print("     (route B, standard EFT, gives 0, EXCLUDED)?")
    print()
    print("   The standard-EFT reading (B) is dead -- it gives lambda_SM = 0,")
    print("   excluded outright. The surviving readings form a one-parameter")
    print("   FAMILY: a reduction folding k of the D substrate modes into the")
    print("   Higgs normalisation gives ((1+e^{-2/D})/2)^k, ranging from ~1")
    print("   (k small, weak collapse) down to e^-1 (k = D, total collapse).")
    print("   These are all finite, so finiteness alone does NOT pick e^-1;")
    print("   it only kills route (B). What selects k = D is:")
    print("     (i)  P3 + Comp 98: all |S|>=2 shells are trivial fixed points")
    print("          with no residual light content, so no partial reduction")
    print("          is available -- the collapse onto the single order")
    print("          parameter is total by construction; and")
    print("     (ii) the empirical 0.8% agreement of the total-collapse value")
    print("          e^-1/4 with observed lambda_SM -- evidence FOR k = D.")
    print()
    print("   HONEST NET: M5 does not prove (Pi-total) from combinatorics. It")
    print("   (a) kills the standard-EFT alternative (route B -> 0); (b) shows")
    print("   the surviving total-collapse reading is the embedding norm of")
    print("   the symmetric Higgs vacuum = e^-1, the correct normalisation")
    print("   for an emergent projected field; (c) grounds 'total' in")
    print("   P3 + Comp 98 (no residual light modes), with the 0.8% match as")
    print("   empirical support. (B) is thus reduced to a single structural")
    print("   premise -- P3's collapse is total -- internal to PST and")
    print("   corroborated by data, with the only competing field-theoretic")
    print("   reading excluded. A strong reduction, NOT a parameter-free proof.")


if __name__ == "__main__":
    main()
