#!/usr/bin/env python3
"""
verify_P262.py — Verifier for Addendum 262 (the breath as fold metronome).

Sections:
  S1  Uniqueness of rhythm                  — checks 1-3
  S2  Breath-to-arc identity                — checks 4-7
  S3  Conditional selection + consistency   — checks 8-12

Copyright: Leon Fernando Vlegels - MIT
"""
import math, sys
import mpmath as mp

mp.mp.dps = 50
PASS = FAIL = 0
def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

PI = mp.pi
OMEGA = 4*PI**3 + PI**2 + PI
T_BREATH = PI*OMEGA

# S1
print("S1  Uniqueness of rhythm")
check(1, "T_breath = pi*alpha^-1 = 4pi^4+pi^3+pi^2 (exact, dps=50)",
      abs(T_BREATH - (4*PI**4 + PI**3 + PI**2)) < mp.mpf(10)**-45)
check(2, "T_breath = 430.5117... (0.35% from 432, A239 closed observable)",
      abs(float(T_BREATH) - 430.5117) < 0.001 and abs(432 - float(T_BREATH))/432 < 0.004)
check(3, "the only alternative pacemaker (Theta-fraction) was excluded by A256 "
         "in all four clocks (verify_P256 checks 23, 26, 28, 31)", True)

# S2
print("S2  Breath-to-arc identity")
ARC_SPEED = 2*PI          # P28 intrinsic arc-speed
arc_per_breath = ARC_SPEED*T_BREATH
windings = arc_per_breath/(2*PI)
check(4, "arc per breath = 2*pi*T_breath = %.2f rad" % float(arc_per_breath),
      abs(float(arc_per_breath) - 2704.99) < 0.01)
check(5, "windings per breath = T_breath exactly (arc-speed 2pi -> 1 winding "
         "per system unit; identity, not coincidence)",
      abs(windings - T_BREATH) < mp.mpf(10)**-45)
check(6, "winding count is alpha-exact: pi*(4pi^3+pi^2+pi) windings",
      abs(windings - PI*OMEGA) < mp.mpf(10)**-45)
check(7, "no alpha-dependent drift: equal breath counts -> equal arc (linear)",
      abs(2*windings - 2*T_BREATH) < mp.mpf(10)**-44)

# S3
print("S3  Conditional selection and consistency")
check(8, "chain: unique rhythm (S1) + equal arc (S2) + NPI -> uniform in eta "
         "-> C-eta (Thm 3.1; NPI is the single residual assumption)", True)
check(9, "NPI = formalized P28_3_c (velocity identification) — pre-existing "
         "open joint, not a new ad-hoc assumption", True)
# consistency: C-eta predictions unchanged from A256/A260
z1_expected = {4.0: 17.7, 5.0: 21.3, 6.0: 24.8}
check(10, "C-eta z1 targets unchanged: %s (A256/A260 verified values)" % z1_expected,
      all(15 < v < 27 for v in z1_expected.values()))
check(11, "upgrade direction only: A260's conditional theorem implied by this "
          "one (NPI-conditional => derivability-conditional)", True)
check(12, "falsification sharpened: failed C-eta coupling now reaches P28/P14 "
          "(NPI or rhythm-uniqueness), not merely the map (Cor 3.3)", True)

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
