"""Prototype of the partition/coverage preflight check. This is the ~20-line check that would have caught the 6502-refactor campaign's 77% shortfall before launch. See ../plan-grounding-and-closure-invariants.md. The principle generalises: when a phase decomposes an enumerable set into buckets, the buckets must be shown to cover the set exhaustively -- against the artifact, not against the prose that describes it. Run: F:\\storage-dev\\6502-python\\.venv\\Scripts\\python.exe coverage_preflight_demo.py """ from __future__ import annotations import sys from collections.abc import Iterable, Mapping def uncovered_items(universe: Iterable[str], buckets: Mapping[str, Iterable[str]]) -> list[str]: """Return items in the real universe that no bucket claims.""" covered = {item for names in buckets.values() for item in names} return sorted(set(universe) - covered) def report(universe: Iterable[str], buckets: Mapping[str, Iterable[str]]) -> int: universe = set(universe) missing = uncovered_items(universe, buckets) covered = len(universe) - len(missing) print(f"items in the artifact : {len(universe)}") print(f"items named by the buckets : {covered}") print(f"coverage : {100 * covered // max(len(universe), 1)}%") if missing: print(f"\nNOT ASSIGNED TO ANY BUCKET ({len(missing)}):\n " + ", ".join(missing)) return 1 if missing else 0 if __name__ == "__main__": # Replays the real 6502-refactor phase-3 taxonomy against the real opcode table. sys.path.insert(0, r"F:\storage-dev\6502-python\src") from sixfiveohtwo.core import OFFICIAL_OPCODES goal_families = { "flag set/clear": ["CLC", "CLD", "CLI", "CLV", "SEC", "SED", "SEI"], "NOP and JAM": ["NOP", "JAM"], "jumps and subroutine calls": ["JMP", "JSR", "RTS", "RTI", "BRK"], "branches": ["BCC", "BCS", "BEQ", "BMI", "BNE", "BPL", "BVC", "BVS"], "load/store/transfer": ["LDA", "LDX", "LDY", "STA", "STX", "STY", "TAX", "TAY", "TSX", "TXA", "TXS", "TYA"], "stack": ["PHA", "PHP", "PLA", "PLP"], "arithmetic": ["ADC", "SBC"], "logical and BIT": ["AND", "ORA", "EOR", "BIT"], "shift and rotate": ["ASL", "LSR", "ROL", "ROR"], "compare": ["CMP", "CPX", "CPY"], "increment/decrement": ["INC", "INX", "INY", "DEC", "DEX", "DEY"], # The spec's twelfth bucket read "the undocumented combined operations". # It reads as a catch-all; it is a proper subset. That is the whole bug. "undocumented combined operations": ["SAX", "LAX", "DCP", "ISC", "SLO", "RLA", "SRE", "RRA"], } raise SystemExit(report({d.mnemonic for d in OFFICIAL_OPCODES.values()}, goal_families))