| | @@ -0,0 +1,238 @@ |
| 1 | +"""Mirror of gridCheck75 (ErdosSwarm/Erdos530Gap75/GridCheck.lean) and a Lean emitter. |
| 2 | + |
| 3 | +Standard library only. |
| 4 | + |
| 5 | + python3 -IB emit_lean.py --tables print the packed constants of GridCheck.lean |
| 6 | + (kerBTable, kerP) as hexadecimal numbers |
| 7 | + python3 -IB emit_lean.py --check replay every block of row.json with the |
| 8 | + arithmetic of gridCheck75 (naive loops, natural |
| 9 | + numbers, truncated subtraction) and compare each |
| 10 | + block's numerator with verify.py's |
| 11 | + python3 -IB emit_lean.py --emit 3 [--blocks 0 40] [--name sample3] |
| 12 | + print a Lean theorem for the blocks of the |
| 13 | + allocation entry with this index (0-based) |
| 14 | + |
| 15 | +An emitted term is `gridCheck75 r last PL PR [(Bn, L), …] [(a, b, c, d, w), …]`: |
| 16 | +`r` is the radius of the entry (0 for a knot entry), `last` the last profile position (`hi` |
| 17 | +for a knot entry, `r` for a radius entry), `PL` and `PR` the decoded integer profiles of the |
| 18 | +12 signed bands in 16-bit digits at index `band * (last + 1) + position` (`PR = 0` for a knot |
| 19 | +entry), `Bn` the 65 binned masses of a witness in 16-bit digits, `L` its level, and `w` the |
| 20 | +index of the block's witness in the list of the witnesses that the emitted blocks use. |
| 21 | +""" |
| 22 | + |
| 23 | +import argparse |
| 24 | +import json |
| 25 | +import sys |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) # for python3 -I |
| 29 | +import verify as V # noqa: E402 |
| 30 | + |
| 31 | +HERE = Path(__file__).resolve().parent |
| 32 | +D = V.KERNEL_DEN # 282240000 |
| 33 | +A = V.COEFS # a_h = c_h D / (1600 h^2) |
| 34 | +TOP = 4294967295 |
| 35 | + |
| 36 | + |
| 37 | +def kerB(d): |
| 38 | + """D times the seven broad tents of W at grid distance d >= 0.""" |
| 39 | + return sum(a * max(0, 16*h - d) for h, a in enumerate(A, 1)) |
| 40 | + |
| 41 | + |
| 42 | +KERB_TABLE = [kerB(d) for d in range(112)] |
| 43 | +KER_P = sum(kerB(abs(t)) << (64 * (t + 111)) for t in range(-111, 112)) |
| 44 | + |
| 45 | + |
| 46 | +def pack(bits, values): |
| 47 | + return sum(v << (bits * k) for k, v in enumerate(values)) |
| 48 | + |
| 49 | + |
| 50 | +def digit(bits, x, j): |
| 51 | + return (x >> (bits * j)) & ((1 << bits) - 1) |
| 52 | + |
| 53 | + |
| 54 | +def sub(x, y): |
| 55 | + return max(0, x - y) |
| 56 | + |
| 57 | + |
| 58 | +def clip(last, x): |
| 59 | + return sub(x, sub(x, last)) |
| 60 | + |
| 61 | + |
| 62 | +def min_r(f, n): |
| 63 | + a = TOP |
| 64 | + for k in range(n): |
| 65 | + a = sub(a, sub(a, f(k))) |
| 66 | + return a |
| 67 | + |
| 68 | + |
| 69 | +def max_r(f, n): |
| 70 | + a = 0 |
| 71 | + for k in range(n): |
| 72 | + a = a + sub(f(k), a) |
| 73 | + return a |
| 74 | + |
| 75 | + |
| 76 | +def band75(lo, s): |
| 77 | + d = sub(s, lo) + sub(lo, s) |
| 78 | + k = sum(sub(1, sub(x, d)) for x in (12, 24, 40, 56, 80)) |
| 79 | + return 2*k + sub(1, sub(1, sub(s, lo))) |
| 80 | + |
| 81 | + |
| 82 | +def ker75(d): |
| 83 | + return digit(32, pack(32, KERB_TABLE), d) + 4*D*sub(1, d) |
| 84 | + |
| 85 | + |
| 86 | +def prepare75(Bn, L): |
| 87 | + """(X as a list, need) as in prepared75, or None if the level test fails.""" |
| 88 | + if not 0 < L < 131072: |
| 89 | + return None |
| 90 | + MP = pack(64, [digit(16, Bn, j // 8) for j in range(513)]) |
| 91 | + BR = MP * KER_P |
| 92 | + x = [sub(L, 16*digit(64, BR, j + 111) // D) for j in range(513)] |
| 93 | + norm = sum(digit(64, MP, j) * digit(64, BR, j + 111) for j in range(513)) |
| 94 | + pen = (1024*norm + (D-1)) // D + sum(v*v for v in x) |
| 95 | + need = sub(16001 * 1717986918400000 + 50000000*pen + 1, 812646137856000*L) |
| 96 | + return x, need |
| 97 | + |
| 98 | + |
| 99 | +def own75(r, last, pl, pr, a, b, c, d, band): |
| 100 | + def left(q): |
| 101 | + return pl[band][clip(last, a + q)] |
| 102 | + if r == 0: |
| 103 | + return min_r(left, b - a + 1) |
| 104 | + m1 = min_r(left, b - a + 1) if a < r else TOP |
| 105 | + m2 = min_r(lambda q: pr[band][clip(last, c + q)], d - c + 1) if c < r else TOP |
| 106 | + m3 = pl[band][last] if (r <= b and r <= d) else TOP |
| 107 | + return min(m1, m2, m3) |
| 108 | + |
| 109 | + |
| 110 | +def partner75(r, last, pl, pr, lo, a, b, c, d, band, s): |
| 111 | + lm = max_r(lambda q: pl[band][clip(last, sub(c + lo + q, s))], d - c + 1) |
| 112 | + if r == 0: |
| 113 | + return lm |
| 114 | + rm = max_r(lambda q: pr[band][clip(last, sub(a + s + q, lo))], b - a + 1) |
| 115 | + if r + s <= c + lo: |
| 116 | + p = rm |
| 117 | + elif a + s < r + lo: |
| 118 | + p = max(lm, rm) |
| 119 | + else: |
| 120 | + p = lm |
| 121 | + if r + s <= d + lo and r + lo <= b + s: |
| 122 | + p = max(p, pl[band][last]) |
| 123 | + return p |
| 124 | + |
| 125 | + |
| 126 | +def block75(r, last, pl, pr, prep, block): |
| 127 | + """(verdict, 500·numerator-side slack) of one block under gridCheck75's arithmetic.""" |
| 128 | + a, b, c, d, w = block |
| 129 | + if not (a <= b <= 512 and c <= d <= 512 and w < len(prep)): |
| 130 | + return False, None |
| 131 | + x, need = prep[w] |
| 132 | + lo = sub(a, sub(a, 111)) |
| 133 | + n = lo + sub(512 - b, sub(512 - b, 111)) + 1 |
| 134 | + y = [min_r(lambda q: x[a - lo + s + q], b - a + 1) for s in range(n)] |
| 135 | + own = [own75(r, last, pl, pr, a, b, c, d, band) for band in range(12)] |
| 136 | + saved = 0 |
| 137 | + for s in range(n): |
| 138 | + if d + lo < s: |
| 139 | + coef = 32768 |
| 140 | + else: |
| 141 | + band = band75(lo, s) |
| 142 | + coef = sub(16384 + own[band], partner75(r, last, pl, pr, lo, a, b, c, d, band, s)) |
| 143 | + z = 13994 * ker75(sub(s, lo) + sub(lo, s)) * coef // 282240000000 |
| 144 | + res = sub(y[s], z) |
| 145 | + saved += sub(y[s]*y[s], res*res) |
| 146 | + return need <= 50000000*saved, 50000000*saved - need |
| 147 | + |
| 148 | + |
| 149 | +def profiles(entry): |
| 150 | + """(r, last, left profiles, right profiles) as lists of 12 lists of integers.""" |
| 151 | + row = V.AllocationRow(entry) |
| 152 | + left = [p.values for p in row.left] |
| 153 | + if row.radius is None: |
| 154 | + return 0, row.hi, left, [[0]*(row.hi+1) for _ in left] |
| 155 | + return row.radius, row.radius, left, [p.values for p in row.right] |
| 156 | + |
| 157 | + |
| 158 | +def check(): |
| 159 | + data = json.loads((HERE / "row.json").read_text(encoding="ascii")) |
| 160 | + entries = json.loads(V.TABLE.read_text(encoding="ascii")) |
| 161 | + assert pack(32, KERB_TABLE) >> (32*112) == 0 and KERB_TABLE[111] > 0 and kerB(112) == 0 |
| 162 | + assert 16001 * 1717986918400000 == 27489508681318400000 == 16001 * V.BOUND_DEN |
| 163 | + assert 812646137856000 == 500 * 8 * V.MASS_NUM * V.POT_UNIT and 50000000 == 500 * V.MASS_DEN |
| 164 | + assert all(V.kernel_num(t) == ker75(abs(t)) for t in range(-111, 112)) |
| 165 | + assert all(V.band_index(t) == band75(111, t + 111) for t in range(-111, 112)) |
| 166 | + wit = [(pack(16, bins), level) for level, bins in data["witnesses"]] |
| 167 | + prep = [prepare75(*w) for w in wit] |
| 168 | + ref = [V.prepare(rec) for rec in data["witnesses"]] |
| 169 | + for (x, need), (base, xv) in zip(prep, ref): |
| 170 | + assert x == xv |
| 171 | + # verify.py: 500·(base + 10^5·saved) > 16001·BOUND_DEN |
| 172 | + assert need == max(0, 16001*V.BOUND_DEN + 1 - 500*base) |
| 173 | + blocks = worst = 0 |
| 174 | + least = None |
| 175 | + for rid, (entry, rows) in enumerate(zip(entries, data["rows"])): |
| 176 | + r, last, pl, pr = profiles(entry) |
| 177 | + row = V.AllocationRow(entry) |
| 178 | + for bid, block in enumerate(rows): |
| 179 | + a, b, c, d, w = block |
| 180 | + ok, slack = block75(r, last, pl, pr, prep, block) |
| 181 | + base, xv = ref[w] |
| 182 | + low, high = max(-V.RANGE, -a), min(V.RANGE, V.N - b) |
| 183 | + saved = 0 |
| 184 | + for t, coeff in row.coefficient_bounds(a, b, c, d, low, high): |
| 185 | + z = 2*6997*V.KERNEL[V.RANGE+t]*coeff // (1000*V.KERNEL_DEN) |
| 186 | + yv = min(xv[a+t:b+t+1]) |
| 187 | + res = max(0, yv - z) |
| 188 | + saved += yv*yv - res*res |
| 189 | + num = base + V.MASS_DEN*saved |
| 190 | + assert ok and slack == 500*num - 16001*V.BOUND_DEN - 1, (rid, bid) |
| 191 | + blocks += 1 |
| 192 | + if least is None or num < least[0]: |
| 193 | + least = num, rid, bid |
| 194 | + print(f"{len(prep)} witnesses and {blocks} blocks pass under gridCheck75's arithmetic;") |
| 195 | + print("every numerator equals verify.py's;") |
| 196 | + print(f"least numerator {least[0]} / {V.BOUND_DEN} = {least[0] / V.BOUND_DEN:.10f} " |
| 197 | + f"at entry {least[1]}, block {least[2]}") |
| 198 | + |
| 199 | + |
| 200 | +def emit(index, first, stop, name): |
| 201 | + data = json.loads((HERE / "row.json").read_text(encoding="ascii")) |
| 202 | + entries = json.loads(V.TABLE.read_text(encoding="ascii")) |
| 203 | + r, last, pl, pr = profiles(entries[index]) |
| 204 | + blocks = data["rows"][index][first:stop] |
| 205 | + used = sorted({blk[4] for blk in blocks}) |
| 206 | + local = {w: k for k, w in enumerate(used)} |
| 207 | + stride = last + 1 |
| 208 | + PL = pack(16, [v for row in pl for v in row[:stride]]) |
| 209 | + PR = pack(16, [v for row in pr for v in row[:stride]]) if r else 0 |
| 210 | + assert all(len(row) == stride for row in pl) |
| 211 | + ws = ", ".join(f"(0x{pack(16, data['witnesses'][w][1]):x}, {data['witnesses'][w][0]})" |
| 212 | + for w in used) |
| 213 | + bs = ", ".join(f"({a}, {b}, {c}, {d}, {local[w]})" for a, b, c, d, w in blocks) |
| 214 | + print(f"theorem {name} : gridCheck75 {r} {last}\n 0x{PL:x}\n 0x{PR:x}\n" |
| 215 | + f" [{ws}]\n [{bs}] = true := by\n decide +kernel") |
| 216 | + |
| 217 | + |
| 218 | +def main(): |
| 219 | + parser = argparse.ArgumentParser(description=__doc__, |
| 220 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 221 | + parser.add_argument("--tables", action="store_true") |
| 222 | + parser.add_argument("--check", action="store_true") |
| 223 | + parser.add_argument("--emit", type=int) |
| 224 | + parser.add_argument("--blocks", type=int, nargs=2) |
| 225 | + parser.add_argument("--name") |
| 226 | + args = parser.parse_args() |
| 227 | + if args.tables: |
| 228 | + print(f"kerBTable : 0x{pack(32, KERB_TABLE):x}") |
| 229 | + print(f"kerP : 0x{KER_P:x}") |
| 230 | + if args.check: |
| 231 | + check() |
| 232 | + if args.emit is not None: |
| 233 | + first, stop = args.blocks if args.blocks else (0, None) |
| 234 | + emit(args.emit, first, stop, args.name or f"sample{args.emit}") |
| 235 | + |
| 236 | + |
| 237 | +if __name__ == "__main__": |
| 238 | + main() |