"""Check the two finite contradictions using only Python's standard library. Run python3 anc/verify.py from the extracted source directory, using ordinary Python without -O. Each sequence of numbered log files is an LRAT proof with only reverse-unit-propagation additions. """ from contextlib import ExitStack from itertools import chain from pathlib import Path import sys from system import build def require(test, msg): if not test: raise ValueError(msg) def check(clauses, proof): # A resolution proof remains valid with every deletion omitted. table = {i+1:frozenset(c) for i,c in enumerate(clauses)} additions = 0 with ExitStack() as stack: streams = [stack.enter_context(path.open(encoding='ascii')) for path in proof] for line in chain.from_iterable(streams): fields = line.split() if not fields or fields[1] == 'd': continue idx = int(fields[0]) data = list(map(int, fields[1:])) stop = data.index(0) clause = frozenset(data[:stop]) hints = data[stop+1:-1] require(idx not in table and data[-1] == 0 and bool(hints), 'bad LRAT line') require(all(h > 0 for h in hints), 'expected only RUP steps') assumptions = {-v for v in clause} require(all(-v not in assumptions for v in assumptions), 'tautological addition') for h in hints: parent = table[h] unrefuted = [lit for lit in parent if -lit not in assumptions] if not unrefuted: break require(len(unrefuted) == 1 and unrefuted[0] not in assumptions, f'failed unit propagation at addition {idx}, hint {h}') assumptions.add(unrefuted[0]) else: raise ValueError(f'no conflict at addition {idx}') table[idx] = clause additions += 1 if not clause: return additions raise ValueError('no empty clause') if __name__ == '__main__': require(__debug__, 'run Python without -O') here=Path(__file__).resolve().parent for case in (sys.argv[1:] or ('A','X')): primary, formula = build(case) steps = check(formula.clauses, sorted(here.glob(f'{case}_*.log'))) print(f'{case}: VERIFIED empty clause; {primary} primary variables, ' f'{formula.top} total variables, {len(formula.clauses)} axioms, {steps} RUP additions.')