"""Symbolic certificate for the three-move game T(a,b) in a linear regime (2m-1)a <= b <= 2ma, m >= 2. data/three_symbolic_m{m}.json (made by experiments/three_symbolic_gen.py) holds, for consecutive closed ranges of b/a covering [2m-1, 2m], a symbolic table: breakpoints i*alpha + j*beta (alpha = b-(2m-1)a, beta = 2ma-b) on each axis and one value per cell. This script PROVES, with the SMT solver Z3 (linear integer arithmetic), that for ALL integers alpha, beta >= 0 (a >= 1) in each range the table function D satisfies every hypothesis of the Lean theorem `ThreeMove.certifiedD` with a = alpha+beta, b = 2m*alpha+(2m-1)*beta, p = 2ma+b, r = 2mb+a, X = 3p+r+a+b, Y = max(2p+2r+2b, 6mb+a+1), Y1 = 4mb: hwin the mex rule at every (x, y) in [0,X) x [0,Y) h1 D(x+p, y+r) = D(x, y) for x < a+b, x <= y <= Y1 h2 D(x+p, y+r) = D(x, y) for a+b <= x < 2p+r+a+b, x <= y < x+(b-a) h3 D(x+q, y+q) = D(x, y) for p <= x < 2p+b, x <= y < x+(r-p), q = p+r (hp, hr, hX, hY, hsat of `certifiedD` are elementary and checked here as well, and the ranges are checked to cover [2m-1, 2m].) Consequence: statements (A) and (B) hold in T(a,b) for every a >= 1 and (2m-1)a <= b <= 2ma. Method: a cell is a product of two intervals between consecutive breakpoints. For a cell and a shift, the cells that the shifted point can fall into are found by feasibility queries; for each combination of target cells that would violate the required property, the joint constraint system must be infeasible for all parameters. Usage: python scripts/check_symbolic.py m [--corrupt] """ import sys, json, itertools, time from multiprocessing import Pool import z3 M = int(sys.argv[1]) DATA = json.load(open(f'data/three_symbolic_m{M}.json'))['ranges'] RANGES = [(d['lo'][0], d['lo'][1], d['hi'][0], d['hi'][1]) for d in DATA] CORRUPT = '--corrupt' in sys.argv NONE, OUT = -1, -2 # shifted coordinate below 0 / at or beyond the window _ctx = {} def ctx(k): """Z3 objects for range k (built once per process).""" if k in _ctx: return _ctx[k] al, be, x, y = z3.Ints('al be x y') a = al + be; b = 2 * M * al + (2 * M - 1) * be p = 2 * M * a + b; r = 2 * M * b + a; q = p + r X = 3 * p + r + a + b; Y1 = 4 * M * b Y = 2 * p + 2 * r + 2 * b if M <= 3 else 6 * M * b + a + 1 ln, ld, hn, hd = RANGES[k] d = DATA[k] cone = [al >= 0, be >= 0, a >= 1, ln * a <= ld * b, hd * b <= hn * a] # closed range, end points included if d.get('open_hi'): cone[-1] = hd * b < hn * a # ... except where the next range starts at hi bx = [i * al + j * be for i, j in d['xforms']] + [X] by = [i * al + j * be for i, j in d['yforms']] + [Y] T = [row[:] for row in d['table']] if CORRUPT and k == 0: T[len(T) // 2][len(T[0]) // 2] ^= 1 s = z3.Solver(); s.add(*cone) c = dict(al=al, be=be, x=x, y=y, a=a, b=b, p=p, r=r, q=q, X=X, Y=Y, Y1=Y1, cone=cone, bx=bx, by=by, T=T, s=s, nx=len(bx) - 1, ny=len(by) - 1) _ctx[k] = c return c def feasible(c, cons): s = c['s']; s.push(); s.add(*cons); res = s.check(); s.pop() assert res != z3.unknown return res == z3.sat def inside(B, i, v): """constraint: v lies in interval i of the breakpoint list B""" return [B[i] <= v, v < B[i + 1]] def target(B, t, v): if t == NONE: return [v < 0] if t == OUT: return [v >= B[-1]] return inside(B, t, v) def cands(k, axis, i): """for interval i on the axis: for each shift, the list of intervals (or NONE/OUT) the shifted coordinate can reach""" c = ctx(k); B = c['bx'] if axis == 0 else c['by']; v = c['x'] if axis == 0 else c['y'] n = len(B) - 1 shifts = {'-a': -c['a'], '-b': -c['b'], '-s': -(c['a'] + c['b']), '+p': c['p'], '+r': c['r'], '+q': c['q']} out = {} base = inside(B, i, v) if not feasible(c, base): return (k, axis, i, None) # interval empty for every parameter value for name, d in shifts.items(): lst = [] rng = range(0, i + 1) if name[0] == '-' else range(i, n) for t in rng: if feasible(c, base + inside(B, t, v + d)): lst.append(t) if name[0] == '-' and feasible(c, base + [v + d < 0]): lst.append(NONE) if name[0] == '+' and feasible(c, base + [v + d >= B[-1]]): lst.append(OUT) out[name] = lst return (k, axis, i, out) def mex(vals): g = 0 while g in vals: g += 1 return g CX = {}; CY = {} def init(cx, cy): global CX, CY CX, CY = cx, cy def check_row(arg): """all checks for the cells (i, j), j = 0..ny-1, of range k; returns a list of failure descriptions""" k, i = arg c = ctx(k); T = c['T']; x, y = c['x'], c['y']; bx, by = c['bx'], c['by'] a, b, p, r, q, Y1 = c['a'], c['b'], c['p'], c['r'], c['q'], c['Y1'] fails = []; nq = 0 cx = CX[(k, i)] if cx is None: return fails, 0 for j in range(c['ny']): cy = CY[(k, j)] if cy is None: continue cell = inside(bx, i, x) + inside(by, j, y) if not feasible(c, cell): continue v = T[i][j] # --- mex rule --- for ia, jb, ib, ja, is_, js in itertools.product(cx['-a'], cy['-b'], cx['-b'], cy['-a'], cx['-s'], cy['-s']): opts = [] if ia != NONE and jb != NONE: opts.append(T[ia][jb]) if ib != NONE and ja != NONE: opts.append(T[ib][ja]) if is_ != NONE and js != NONE: opts.append(T[is_][js]) if mex(opts) == v: continue nq += 1 cons = (cell + target(bx, ia, x - a) + target(by, jb, y - b) + target(bx, ib, x - b) + target(by, ja, y - a) + target(bx, is_, x - (a + b)) + target(by, js, y - (a + b))) if feasible(c, cons): m = c['s']; fails.append(f'range {k} cell ({i},{j}) value {v}: mex rule can fail (targets {ia},{jb},{ib},{ja},{is_},{js})') # --- h1, h2: translation by (p, r) --- regions = [('h1', [x < a + b, x <= y, y <= Y1], '+p', '+r', p, r), ('h2', [a + b <= x, x < 2 * p + r + a + b, x <= y, y < x + (b - a)], '+p', '+r', p, r), ('h3', [p <= x, x < 2 * p + b, x <= y, y < x + (r - p)], '+q', '+q', q, q)] for name, reg, sx, sy, dx, dy in regions: if not feasible(c, cell + reg): continue for ti, tj in itertools.product(cx[sx], cy[sy]): if ti >= 0 and tj >= 0 and T[ti][tj] == v: continue nq += 1 if feasible(c, cell + reg + target(bx, ti, x + dx) + target(by, tj, y + dy)): fails.append(f'range {k} cell ({i},{j}) value {v}: {name} can fail (target {ti},{tj})') return fails, nq def pre(k): """elementary hypotheses and well-formedness of the symbolic table, for all parameters in the range""" c = ctx(k); bad = [] a, b, p, r, X, Y, Y1 = c['a'], c['b'], c['p'], c['r'], c['X'], c['Y'], c['Y1'] for B, nm in ((c['bx'], 'x'), (c['by'], 'y')): for t in range(len(B) - 1): if feasible(c, [B[t] > B[t + 1]]): bad.append(f'range {k}: {nm}-breakpoints {t},{t+1} not ordered') if feasible(c, [B[0] != 0]): bad.append(f'range {k}: first {nm}-breakpoint not 0') hyp = z3.And(a + b <= p, p + 2 * (b - a) <= r, 3 * p + r + (a + b) <= X, Y1 + r + 1 <= Y, 2 * p + 2 * r + (a + b) + (b - a) <= Y, a + b <= Y1) if feasible(c, [z3.Not(hyp)]): bad.append(f'range {k}: size hypotheses hp/hr/hX/hY fail') # hsat: for x < a+b: b*(x/a) <= Y1 = 4mb and b*((x+p)/a) <= Y1 + r = 6mb + a. # It suffices that x/a <= 2m and (x+p)/a <= 6m, i.e. x < (2m+1)a and x + p < (6m+1)a. x = c['x'] if feasible(c, [x >= 0, x < a + b, x >= (2 * M + 1) * a]): bad.append(f'range {k}: hsat part 1') if feasible(c, [x >= 0, x < a + b, x + p >= (6 * M + 1) * a]): bad.append(f'range {k}: hsat part 2') return bad def run_range(k, pool): t0 = time.time() c = ctx(k); nx, ny = c['nx'], c['ny'] bad = pre(k) res = pool.starmap(cands, [(k, 0, i) for i in range(nx)] + [(k, 1, j) for j in range(ny)], chunksize=1) cx = {(kk, i): o for kk, ax, i, o in res if ax == 0}; cy = {(kk, i): o for kk, ax, i, o in res if ax == 1} mx = max(len(l) for o in list(cx.values()) + list(cy.values()) if o for l in o.values()) return bad, cx, cy, mx, time.time() - t0 if __name__ == '__main__': from fractions import Fraction allbad = [] ends = [Fraction(2 * M - 1)] for (ln, ld, hn, hd) in RANGES: if Fraction(ln, ld) != ends[-1]: allbad.append(f'ranges not contiguous at {ln}/{ld}') ends.append(Fraction(hn, hd)) if ends[-1] != 2 * M: allbad.append('ranges do not reach 2m') tot = 0 for k in range(len(RANGES)): with Pool(100) as pool: bad, cx, cy, mx, t1 = run_range(k, pool) t0 = time.time() with Pool(100, initializer=init, initargs=(cx, cy)) as pool: out = pool.map(check_row, [(k, i) for i in range(ctx(k)['nx'])], chunksize=1) fails = bad + [f for fl, _ in out for f in fl] nq = sum(n for _, n in out); tot += ctx(k)['nx'] * ctx(k)['ny'] ln, ld, hn, hd = RANGES[k] print(f'range {k} [{Fraction(ln, ld)}, {Fraction(hn, hd)}{")" if DATA[k].get("open_hi") else "]"}: {ctx(k)["nx"]} x {ctx(k)["ny"]} cells; max targets per shift {mx}; ' f'{nq} infeasibility queries; failures {len(fails)}; {t1:.0f}s + {time.time()-t0:.0f}s', flush=True) for f in fails[:6]: print(' ', f) allbad += fails print(f'RESULT m={M}:', f'ALL HYPOTHESES VERIFIED for every a >= 1, {2*M-1}a <= b <= {2*M}a ({len(RANGES)} ranges, {tot} cells)' if not allbad else f'{len(allbad)} FAILURES') for f in allbad[:3]: print(' ', f)