#!/usr/bin/env python3
"""INDEPENDENT RE-VERIFICATION, Part I: alpha-ladder infeasibility theorem.
Re-derives: ladder count 10,800; per-ladder false-match p; expected false hits 7.2e-3;
x140 margin. Then BREAK attempts: window semantics, step heterogeneity, missed families
(Fano 3-folds/4-folds at dims 9/11, correction-bracket bit, target rescalings)."""
from math import comb, pi
import json

out = {}
alpha_inv = lambda k: (pi**1.5/24)*10*k*(k+3)/(k+4)*(1+7/(80*((k+4)**2-1)))
CODATA18 = 137.035999084   # what artifacts use
CODATA22 = 137.035999177   # current
a60 = alpha_inv(60)
step60 = alpha_inv(61)-alpha_inv(60)
d18 = abs(a60-CODATA18); d22 = abs(a60-CODATA22)
out["ladder_at_60"] = dict(alpha_inv_60=a60, step=step60,
    resid_vs_CODATA18_ppm=d18/CODATA18*1e6, resid_vs_CODATA22_ppm=d22/CODATA22*1e6)
# monotonicity + unique hit-capable k (independent re-check of E2 lemma)
mono = all(alpha_inv(k+1) > alpha_inv(k) for k in range(1, 5000))
n_1pct = sum(1 for k in range(1,5001) if abs(alpha_inv(k)/CODATA18-1)<0.01)
out["monotone_k1_5000"] = mono; out["n_within_1pct"] = n_1pct

# ---- per-ladder false p, three window readings ----
def p_ladder(halfwidth_ppm, step=step60):
    return 2*CODATA18*halfwidth_ppm*1e-6/step
p_obs   = p_ladder(d18/CODATA18*1e6)   # as-good-as-observed (E5's statistic)
p_falsi = p_ladder(0.1)                # corpus's own falsifier band |d|>0.1ppm (App AG)
p_subppm= p_ladder(1.0)                # literal 'sub-ppm'
out["p_per_ladder"] = dict(as_observed=p_obs, falsifier_band_0p1ppm=p_falsi, literal_subppm=p_subppm)

# ---- ladder count re-derivation (E5 attack-8 menu) ----
n_surf, n_rays, n_lens, n_dims, n_form = 10, 10, 12, 3, 3
divisors60 = [p for p in range(1,61) if 60 % p == 0]
assert len(divisors60) == 12
n_ladders = n_surf*n_rays*n_lens*n_dims*n_form
out["n_ladders_rederived"] = n_ladders          # 10,800
out["E5_consistency"] = dict(p_E5=6.645082440228894e-7, p_mine=p_obs,
    agree=abs(p_obs-6.645082440228894e-7)/p_obs < 1e-9)

# ---- expected false hits under each window ----
E = {w: n_ladders*p for w, p in
     [("as_observed", p_obs), ("falsifier_0p1ppm", p_falsi), ("literal_subppm", p_subppm)]}
out["expected_false"] = E
out["margin_x"] = {w: 1/(p*n_ladders)*n_ladders/n_ladders and (1/p)/n_ladders for w, p in
     [("as_observed", p_obs), ("falsifier_0p1ppm", p_falsi), ("literal_subppm", p_subppm)]}
# E6 hard-code check
out["E6_hardcode_check"] = dict(E6_p=6.6e-7, E6_expected=6.6e-7*10800,
    E5_computed_expected=p_obs*10800, E5_prose_4000x_error=dict(
    printed="~4000x the honesty-guard menu", true_ratio=(1/p_obs)/10800))

# ---- BREAK 1: step heterogeneity (small-prefactor quadratic variants -> denser rungs)
# ladder f(k)=c*g(k), g ~ linear slope 1 asymptotically -> step at crossing = c.
# worst grammar prefactor 1/8pi vs pi spans x(8pi^2)~79 in c -> densest variant p x~ (2.32/c_min)
c_base = (pi**1.5/24)*10
c_min = c_base/(8*pi**2)**0.5   # geometric worst-half; full worst = c_base/79
out["break1_step_heterogeneity"] = dict(
    note="p_ladder ~ window/step; step=c for asymptot. linear ladders; smallest natural "
         "prefactor inflates p by up to ~x79 for THAT ladder; menu-average x3-10",
    E_false_if_avg_x10=E["as_observed"]*10)

# ---- BREAK 2: missed families -- honest higher-dim counts with honest sparsity
# dim9: ~105 Fano 3-fold deformation families (Mori-Mukai), cubic HRR -> sparse rungs
# dim11: Fano 4-folds, bounded (KMM) but >~1000 known; quartic HRR -> sparser
def step_at_crossing(power, c=c_base):
    # f(k)=c*k^power/power! ; solve f(k*)=137, step = f'(k*)
    from math import factorial
    ks = (137.036*factorial(power)/c)**(1/power)
    return c*ks**(power-1)/factorial(power-1)
p_cubic  = 2*CODATA18*(d18/CODATA18)/step_at_crossing(3)
p_quartic= 2*CODATA18*(d18/CODATA18)/step_at_crossing(4)
n_dim9  = 105*n_rays*n_lens*n_form      # 37,800
n_dim11 = 1000*n_rays*n_lens*n_form     # 360,000 (generous)
E_infl = E["as_observed"] + n_dim9*p_cubic + n_dim11*p_quartic
out["break2_higher_dim_families"] = dict(p_cubic=p_cubic, p_quartic=p_quartic,
    n_dim9=n_dim9, n_dim11=n_dim11, E_false_inflated=E_infl,
    under_falsifier_band=E_infl*(0.1/(d18/CODATA18*1e6)),
    under_literal_subppm=E_infl*(1.0/(d18/CODATA18*1e6)))

# ---- BREAK 3: convention bits E5 did not charge to the alpha ladder
# 2-loop bracket on/off (x2, shifts 21ppm >> window so distinct ladder), target
# rescaling alpha vs 4pi*alpha (x2), level shift k vs k+h_vee (x2), operator (x2)
E_bits = E_infl*2*2*2*2
out["break3_bit_stack"] = dict(mult=16, E_false=E_bits,
    verdict_as_observed="still < 1" if E_bits < 1 else "BROKEN")

print(json.dumps(out, indent=1, default=str))
json.dump(out, open("verify3x/partI_results.json","w"), indent=1, default=str)
