Exact witness geometry and the projection problem · Chapter 23 of 24

22. Reproducible finite checker

This checker is intentionally exponential on its small test inputs. That is its role as an independent comparison against the exact relation, not the proposed polynomial solver.

"""Independent finite checks; not a universal complexity proof."""
import json
from itertools import product
from random import Random
from projection_reference import evaluate, parity_chain, solve, xor_gate

checks = {"formulas": 0, "projected_memberships": 0,
          "recovery_results": 0, "xor_rows": 0,
          "parity_memberships": 0}


def raw_truth(clauses, bits):
    for clause in clauses:
        satisfied = False
        for literal in clause:
            value = bits[abs(literal) - 1]
            if (literal > 0 and value == 1) or (literal < 0 and value == 0):
                satisfied = True
        if not satisfied:
            return False
    return True


def check_formula(clauses, n):
    models = [bits for bits in product((0, 1), repeat=n)
              if raw_truth(clauses, bits)]
    model, levels, _ = solve(clauses, n)
    assert (model is None) == (len(models) == 0)
    if model is not None:
        assert model == min(models)
        assert raw_truth(clauses, model)
    checks["recovery_results"] += 1
    for t in range(n + 1):
        expected = {bits[:t] for bits in models}
        for prefix in product((0, 1), repeat=t):
            actual = evaluate(levels[t], dict(enumerate(prefix, 1)))
            assert actual == (prefix in expected)
            assert all(abs(lit) <= t for c in levels[t] for lit in c)
            checks["projected_memberships"] += 1
    checks["formulas"] += 1


universe = [tuple(sign * variable for variable, sign in enumerate(states, 1)
                  if sign != 0)
            for states in product((-1, 0, 1), repeat=2)]
for selected in product((0, 1), repeat=len(universe)):
    check_formula([c for c, used in zip(universe, selected) if used], 2)

rng = Random(20260914)
for n in range(3, 7):
    for _ in range(100):
        clauses = [tuple(rng.choice((-1, 1)) * rng.randint(1, n)
                         for _ in range(rng.randint(1, 3)))
                   for _ in range(rng.randint(0, 6 * n))]
        check_formula(clauses, n)

for clauses, n in [([], 0), ([()], 0), ([], 5),
                   ([(1,), (-1,)], 1), ([(1, -1)], 2),
                   ([(1, 2), (-1, 3), (-2, -3)], 3)]:
    check_formula(clauses, n)

for bits in product((0, 1), repeat=3):
    assert raw_truth(xor_gate(1, 2, 3), bits) == ((bits[0] ^ bits[1]) == bits[2])
    checks["xor_rows"] += 1

growth = []
for r in range(2, 11):
    for parity in (0, 1):
        formula, n = parity_chain(r, parity)
        model, levels, stats = solve(formula, n)
        assert len(levels[r]) == 2 ** (r - 1)
        assert all(len(c) == r for c in levels[r])
        assert raw_truth(formula, model)
        for bits in product((0, 1), repeat=r):
            assert evaluate(levels[r], dict(enumerate(bits, 1))) == (sum(bits) % 2 == parity)
            checks["parity_memberships"] += 1
        if r <= 6:
            check_formula(formula, n)
        if parity == 0:
            growth.append({"r": r, "variables": n,
                           "input_clauses": len(formula),
                           "projected_clauses": len(levels[r]),
                           "projected_literals": sum(map(len, levels[r])),
                           "max_raw_candidates": max(s["raw_candidates"] for s in stats)})

report = {"status": "passed", "seed": 20260914,
          "checks": checks, "parity_growth": growth}
print(json.dumps(report, indent=2))
with open("work/projection_verification.json", "w") as stream:
    json.dump(report, stream, indent=2)