ingot-lean-zig / tools /lean2zig_README.md
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
3.88 kB

lean2zig

Deterministic, rule-based Lean 4 -> Zig 0.17 converter for the integer-arithmetic subset of theorem statements, verified by differential testing. No model is involved; the same statement always gives the same Zig.

ZIG_BIN=<zig 0.17.0> python3 tools/lean2zig/run.py --tag full [--limit N] [--batch 150] [--jobs 2]
ZIG_BIN=<zig> python3 tools/lean2zig/selftest.py        # lean.zig helpers vs a Python bigint reference of Lean semantics

Files: lean2zig.py (lexer, Pratt parser, elaborator, emitter; translate(id, text)), lean.zig (Lean semantics and the vector source, imported by every emitted file), run.py (driver: dedup, translate, batched zig test, stats, twin check), selftest.py. Data goes to the gitignored out/lean2zig/; the ledger is results/cloud/lean2zig/stats.json.

What it accepts

Binders (x y : ℕ/ℤ), {..}, bare auto-bound names, hypotheses; prenex ∀ and → in the conclusion are hoisted to parameters/hypotheses; + - * / % ^ ∣ = ≠ ≤ < ≥ > ∧ ∨ ¬ ↔ → ∀ ∃, ≡ [ZMOD n], [MOD n], ∈ of range/Icc/Ico/Ioc/Ioo, Nat.choose, gcd/lcm, Int.natAbs, |x|, max/min, n !, fib, Nat.sqrt, Nat.totient, Nat.Prime, Even/Odd, Coprime, if then else, ∑/∏ over intervals and Nat.divisors, Finset.filter, Finset.card, casts. Anything else is rejected with a named reason (all reasons of a statement are recorded, the first is its primary).

Typing follows Lean's binop% rule: the leaves of an arithmetic tree are unified to one class (ℕ < ℤ, numerals polymorphic, untyped binders unify), so a - b is truncated subtraction exactly when its class resolves to ℕ.

Semantics (lean.zig)

Everything is i128. ℕ - truncates at 0; / and % are Int.ediv/emod (Euclidean) with x/0 = 0, x%0 = x; 0^0 = 1. Every helper checks overflow; an overflow or an inconclusive search (a truncated ∃/∀ range, a span over 4096, primality above 1e10) sets a flag and that vector is skipped, never counted as pass or fail. Finite ∀/∃ are walked when bounds are visible in the body (complete) and over a window otherwise (inconclusive unless a witness/counterexample is found).

Emitted unit

/// statement, pub fn <name>Hypotheses, <name>Lhs/Rhs (top-level =) or <name>Conclusion, and one test. The test draws vectors from lean.Source: an exhaustive small grid over the free variables (size depends on their number), then edge-weighted random draws. Bounds in hypotheses (p ≤ n, x > 2, x ∈ Icc a b) become draw ranges, v = e hypotheses define v, and equations linear in one variable (4*n+3 = 1351) are solved for it. tally.admit skips overflowed vectors, counts distinct admitted ones and fails on a false conclusion; tally.require(K) needs min(8, size of a literal-bounded domain) distinct admitted vectors. Failures are named: TestUnexpectedResult (semantic mismatch), TooFewVectors, NoVector, OnlySkipped.

Differential check

The theorems are proved, so the conclusion must hold on every admitted vector; a failure is a translation bug and the row is dropped as "semantic mismatch". Batches of 150 units go into one file; compile errors are mapped back to units by line, those units are dropped and the batch re-run; a timeout bisects (the zig process group is killed). Limits of the check: it cannot see bugs the proved statements never exercise (a canary that turned ℕ-subtraction into plain subtraction was caught on only 12 of 2268 units), which is why the helpers have their own conformance test.

Twin check

run.py reproduces the ID/N/T-normalised 5-gram Jaccard >= 0.90 test of results/cloud/lean-v2-review/shape_audit.py, exactly (inverted index, no family split). It is reported for whole units and for the theorem functions alone, because the test block is shared boilerplate.