#!/usr/bin/env python3
"""verify_P024.py -- Paper 24: Riemann Hypothesis Hilbert-Polya framework."""

from __future__ import annotations

import sys
from pathlib import Path

PASS = FAIL = 0
_N = 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}")
    return ok


class Verifier:
    """Target-style output adapter; check/record numerics are unchanged."""

    def __init__(self, name):
        print(name)

    def check(self, label, computed, claimed, *, rel=1e-3, abs_tol=None, detail=""):
        if abs_tol is not None:
            ok = abs(computed - claimed) <= abs_tol
            err_detail = f"abs err={abs(computed - claimed):.6g}, tol={abs_tol:.6g}"
        else:
            if claimed == 0:
                ok = abs(computed) <= (rel or 1e-12)
                err_detail = f"abs value={abs(computed):.6g}, tol={rel:.6g}"
            else:
                err = (computed - claimed) / abs(claimed)
                ok = abs(err) <= (rel or 0)
                err_detail = f"rel err={100 * err:+.6g}%, tol={100 * (rel or 0):.6g}%"
        return self.record(label, ok, computed, claimed,
                           err_detail + (f"; {detail}" if detail else ""))

    def record(self, label, ok, computed="", claimed="", detail=""):
        global _N
        ok = bool(ok)
        desc, extra = label, detail
        if not ok and detail:
            desc, extra = f"{label} -- {detail}", ""
        _N += 1
        check(_N, desc, ok)
        if computed != "" or claimed != "":
            print(f"       computed: {computed}")
            print(f"       claimed : {claimed}")
        if extra:
            print(f"       {extra}")
        return ok

    def summary(self):
        print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
        return 1 if FAIL else 0


v = Verifier("P024 -- Riemann Hypothesis")
ROOT = Path(__file__).resolve().parents[2]
TEX = (ROOT / "toe" / "24_Paper_RiemannHypothesis.tex").read_text()

v.record("TeX source is present", "Riemann Hypothesis" in TEX)
v.record("functional equation symmetry on critical line", True, computed="xi(1/2+iγ)=xi(1/2-iγ)", claimed="s <-> 1-s")
v.record("multiplication by a real variable is self-adjoint on its natural L2 domain", True)
v.record("Weil trace-formula gap is acknowledged", "critical open step" in TEX and "falls short of a proof" in TEX)
v.record("distributional eigenfunction caveat is acknowledged", "not elements of" in TEX and "rigged Hilbert" in TEX)
v.record("Riemann-von Mangoldt density gives convergent sum for h=O(t^-2-eps)", True)

v.record(
    "Mellin transform sign/normalization is consistent",
    False,
    computed="with M[f](s)=∫f x^(s-1)dx, x d/dx maps to -s, so -i(-s+1/2) at s=1/2+iγ equals -γ, not +γ; the displayed dx/x transform also shifts the exponent",
    claimed="H maps to multiplication by +γ",
    detail="Expected sign/normalization fail.",
)
v.record(
    "zero-supported odd part is a nonzero L2 subspace",
    False,
    computed="the zero set is countable/measure zero, so any L2 function supported on it is zero a.e.; delta masses are distributions, not L2 vectors",
    claimed="H_xi = L2_even plus H_odd,Z",
    detail="Expected Hilbert-space fail.",
)
v.record(
    "constraint creates point spectrum at zeta zeros inside L2",
    False,
    computed="after quotienting by a.e. equality, the constraint just enforces evenness off a null set; it cannot add L2 eigenvectors at points",
    claimed="zeros open doors for localized spectral weight",
    detail="Expected spectral fail.",
)
v.record(
    "self-adjointness of H_xi proves RH",
    False,
    computed="self-adjoint multiplication has real spectrum regardless of zeta; the construction only references zeros already written as 1/2+iγ",
    claimed="self-adjoint spectrum real implies RH",
    detail="Expected circularity fail.",
)
v.record(
    "off-critical zeros are represented",
    False,
    computed="Z is defined as {γ real: xi(1/2+iγ)=0}; zeros with Re(s)!=1/2 are omitted rather than ruled out",
    claimed="Weil formula sums all non-trivial zeros and spectrum=zeros",
    detail="Expected RH-circularity fail.",
)
v.record(
    "h(H_xi) is trace class on continuous-spectrum part",
    False,
    computed="multiplication by h(γ) on L2(R) is generally not trace class on the continuous part merely because h decays",
    claimed="Weil class subset trace class for h(H_xi)",
    detail="Expected trace-class fail.",
)
v.record(
    "Weil explicit formula is identified with this operator trace",
    False,
    computed="the paper explicitly says the kernel/trace identification with the prime sum is not established",
    claimed="Weil = Trace Formula is rigorous",
    detail="Expected unresolved-gap fail.",
)
v.record(
    "resolvent has poles at real zeros",
    False,
    computed="the resolvent of multiplication by γ has continuous-spectrum singularities on the real axis, not isolated poles, unless an actual point-spectrum subspace is constructed",
    claimed="simple poles exactly at γ_n",
    detail="Expected resolvent fail.",
)

sys.exit(v.summary())
