The following Python program is an exact finite solver using only the standard library. It contains no SAT oracle, HARD branch, or unimplemented compression call. It also has no polynomial worst-case runtime guarantee.
Save this block as work/projection_reference.py. Running it prints the worked example and the small parity growth table. The validation script in Section 22 is a separate block for work/check_projection.py and reproduces the verification record when run from the parent of work.
"""Exact Boolean projection by Davis-Putnam elimination.
Finite, deterministic, complete. No polynomial worst-case time claim.
The final Markdown document embeds this reference implementation.
"""
from itertools import product
def clause_key(clause):
return (len(clause), tuple(sorted(clause)))
def normalize(clauses):
"""Remove tautologies, duplicates, and subsumed clauses."""
unique = set()
for raw in clauses:
clause = frozenset(raw)
if any(-literal in clause for literal in clause):
continue
if not clause:
return (frozenset(),)
unique.add(clause)
kept = []
for clause in sorted(unique, key=clause_key):
if not any(other <= clause for other in kept):
kept.append(clause)
return tuple(kept)
def eliminate(cnf, variable):
"""Return a CNF equivalent to exists variable . cnf."""
positive, negative, untouched = [], [], []
for clause in cnf:
if variable in clause:
positive.append(clause - {variable})
elif -variable in clause:
negative.append(clause - {-variable})
else:
untouched.append(clause)
resolvents = [a | b for a in positive for b in negative]
output = normalize(untouched + resolvents)
statistics = {
"variable": variable,
"input_clauses": len(cnf),
"resolvent_pairs": len(positive) * len(negative),
"raw_candidates": len(untouched) + len(resolvents),
"output_clauses": len(output),
}
return output, statistics
def evaluate(cnf, assignment):
"""assignment maps every occurring variable to the integer 0 or 1."""
return all(
any(assignment[abs(literal)] == int(literal > 0)
for literal in clause)
for clause in cnf
)
def project_all(clauses, variable_count):
if not isinstance(variable_count, int) or variable_count < 0:
raise ValueError("variable_count must be a nonnegative integer")
clauses = tuple(tuple(clause) for clause in clauses)
for clause in clauses:
if any(not isinstance(lit, int) or lit == 0 or
abs(lit) > variable_count for lit in clause):
raise ValueError("literal outside the declared variable range")
levels = [None] * (variable_count + 1)
levels[variable_count] = normalize(clauses)
statistics = []
for variable in range(variable_count, 0, -1):
levels[variable - 1], stats = eliminate(levels[variable], variable)
statistics.append(stats)
return levels, statistics
def solve(clauses, variable_count):
"""Return (model or None, all projection levels, stage statistics)."""
levels, statistics = project_all(clauses, variable_count)
if not evaluate(levels[0], {}):
return None, levels, statistics
assignment = {}
for variable in range(1, variable_count + 1):
assignment[variable] = 0
if not evaluate(levels[variable], assignment):
assignment[variable] = 1
assert evaluate(levels[variable], assignment)
model = tuple(assignment[i] for i in range(1, variable_count + 1))
return model, levels, statistics
def xor_gate(a, b, c):
"""Four clauses expressing c = a XOR b."""
clauses = []
for av, bv, cv in product((0, 1), repeat=3):
if (av ^ bv) != cv:
clauses.append(frozenset(
-variable if value else variable
for variable, value in ((a, av), (b, bv), (c, cv))
))
return clauses
def parity_chain(r, parity=0):
"""An at-most-3-CNF whose projection onto 1..r is r-bit parity."""
if r < 2 or parity not in (0, 1):
raise ValueError("r >= 2 and parity in {0,1} are required")
clauses = []
previous = 1
for i in range(2, r + 1):
output = r + i - 1
clauses.extend(xor_gate(previous, i, output))
previous = output
clauses.append(frozenset({previous if parity else -previous}))
return tuple(clauses), 2 * r - 1
if __name__ == "__main__":
formula = ((1, 2), (-1, 3), (-2, -3))
model, levels, statistics = solve(formula, 3)
print("model:", model)
for stats in statistics:
print(stats)
for r in range(2, 9):
formula, n = parity_chain(r)
levels, _ = project_all(formula, n)
print("parity", r, "projected clauses", len(levels[r]))