ingot-lean-zig / tools /lean2zig_selftest.py
ego0op's picture
Add workbook config: 1,208 proved Lean-Workbook integer statements converted by the rule-based lean2zig converter, each re-verified with zig test
2789d27 verified
Raw History Blame Contribute Delete
2.94 kB
#!/usr/bin/env python3
"""Conformance of lean.zig helpers against a Python bigint reference of Lean 4 semantics (Int.ediv/emod, Nat.sub, x/0 = 0, x%0 = x...).
Run: ZIG_BIN=<zig> python3 tools/lean2zig/selftest.py (writes out/lean2zig/selftest/, prints PASS or the first mismatches)"""
import os, subprocess, shutil, itertools, math
HERE = os.path.dirname(os.path.abspath(__file__))
OUT = os.path.join(HERE, '..', '..', 'out', 'lean2zig', 'selftest')
os.makedirs(OUT, exist_ok=True)
shutil.copy(os.path.join(HERE, 'lean.zig'), OUT)
def emod(a, b): return a if b == 0 else a % abs(b)
def ediv(a, b): return 0 if b == 0 else (a - emod(a, b)) // b
def monus(a, b): return max(a - b, 0)
def fib(n):
a, b = 0, 1
for _ in range(n): a, b = b, a + b
return a
def prime(n): return n >= 2 and all(n % d for d in range(2, math.isqrt(n) + 1))
def tot(n): return sum(1 for i in range(1, n + 1) if math.gcd(i, n) == 1) if n > 0 else 0
V = [-13, -7, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 97, 100, 1000]
N = [v for v in V if v >= 0]
cases = []
for a, b in itertools.product(V, V):
cases += [('add', a, b, a + b), ('sub', a, b, a - b), ('mul', a, b, a * b), ('div', a, b, ediv(a, b)), ('mod', a, b, emod(a, b)),
('gcd', a, b, math.gcd(a, b)), ('lcm', a, b, abs(a * b) // math.gcd(a, b) if a and b else 0)]
for a, b in itertools.product(N, N):
cases += [('monus', a, b, monus(a, b)), ('choose', a, b, math.comb(a, b) if b <= a else 0)]
if b <= 12: cases += [('pow', a, b, a ** b)]
for a in V: cases += [('neg', a, 0, -a), ('abs', a, 0, abs(a))]
for a in N:
if a <= 30: cases += [('factorial', a, 0, math.factorial(a)), ('fib', a, 0, fib(a))]
cases += [('isqrt', a, 0, math.isqrt(a)), ('totient', a, 0, tot(a))]
lines = ['const std = @import("std");', 'const lean = @import("lean.zig");', '']
for i, (f, a, b, r) in enumerate(cases):
call = 'lean.%s(%d)' % (f, a) if f in ('neg', 'abs', 'factorial', 'fib', 'isqrt', 'totient') else 'lean.%s(%d, %d)' % (f, a, b)
if abs(r) >= 2 ** 127: # beyond i128: the helper must flag overflow instead of returning a value
lines.append('test "%s_%d_overflow" { lean.begin(); _ = %s; try std.testing.expect(lean.skipped()); }' % (f, i, call))
continue
lines.append('test "%s_%d" { lean.begin(); try std.testing.expectEqual(@as(i128, %d), %s); try std.testing.expect(!lean.skipped()); }' % (f, i, r, call))
for n in range(0, 200): lines.append('test "prime_%d" { try std.testing.expectEqual(%s, lean.isPrime(%d)); }' % (n, 'true' if prime(n) else 'false', n))
open(os.path.join(OUT, 'selftest.zig'), 'w').write('\n'.join(lines) + '\n')
p = subprocess.run([os.environ.get('ZIG_BIN', 'zig'), 'test', 'selftest.zig'], cwd=OUT, capture_output=True, text=True)
bad = [l for l in (p.stdout + p.stderr).splitlines() if 'FAIL' in l]
print('cases', len(cases) + 200, 'PASS' if p.returncode == 0 and not bad else 'FAIL'); print('\n'.join(bad[:10]))