Rebuild lean-bench: pages in, verified indexed Lean out
Replaces the previous tree entirely. The pipeline is four separable pieces, each
testable on its own:
1. leanbench/llm.py provider-neutral OpenAI-compatible connector, stdlib only,
with retries, JSON coercion and a scripted client so the
rest of the code is testable without a network.
2. leanbench/detect.py per-page detection of formalizable mathematics. Case 1 is
mathematics asserted as fact (definition/theorem/lemma/...,
deliberately not sub-classified); case 2 is a problem left
open to the reader. Hits come back as spans -- start page
and index, end page and index -- so several per page is
normal. A hit cut off by the page end makes the detector
re-read that page together with the next, repeatedly, so an
item spread over three pages is found whole.
Offsets come from verbatim quotes resolved by
leanbench/anchors.py (exact -> whitespace-insensitive ->
fuzzy), because models quote well and count badly.
3. leanbench/formalize.py + leanbench/verify.py
each span becomes one Lean 4 declaration, judged by the real
compiler: full elaboration for case 1, statement legality
only for case 2, which has no proof to check. Lean's own
diagnostics feed back for a repair attempt. "verified" (Lean
accepted it) and "proved" (accepted with no sorry) are kept
strictly apart.
4. leanbench/db.py indexed SQLite plus JSONL export. Every item is indexed by
the types of its inputs, or by its own type when it is not a
proof, and records the source book (sha256) and the page it
starts on -- so `drop --source H --pages A-B` followed by a
re-ingest redoes exactly those pages and nothing else.
Infrastructure:
- lean/ Lake project pinned to Mathlib.
- docker/ image carrying Lean 4 and a prebuilt Mathlib. The Mathlib
revision and the toolchain are resolved against each other at
build time rather than both hardcoded, so the build does not rot.
docker/smoke.py runs the whole pipeline inside the image against
the real compiler, with the LLM scripted.
- .gitea/workflows ci (lint + tests in a venv, since the runner image's Python is
Debian-managed and refuses installs into itself), docker (build,
verify, optionally push), ingest (submit a book by hand).
The docker workflow is not a throwaway build: it leaves the image, the layer cache,
an idle `lean-bench` container and the `lean-bench-data` / `lean-bench-books`
volumes in place on the runner, and never prunes, so the environment is there to be
used afterwards. Pushing to the registry is optional and cannot fail the build --
Gitea's container registry does not accept the built-in Actions token, so that needs
a REGISTRY_TOKEN secret with write:package.
58 tests; they need neither network nor Lean.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ci5T3gWDhhmqDec3e1XtYZ
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
"""Step 3, second half: hand the generated Lean to the real compiler and believe it.
|
||||
|
||||
Two verification modes, matching the two kinds of item:
|
||||
|
||||
``full`` elaborate the declaration as written. If the model supplied a proof,
|
||||
this is a genuine proof check.
|
||||
``statement`` the item is an open problem, so there is no proof to check: we only
|
||||
ask whether the *statement* is legal Lean -- it elaborates with
|
||||
``sorry`` standing in for the proof.
|
||||
|
||||
``verified`` never means "proved". ``proved`` does, and it is only true when Lean
|
||||
accepted the declaration with no ``sorry`` anywhere in it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
MODE_FULL = "full"
|
||||
MODE_STATEMENT = "statement"
|
||||
|
||||
_ERROR = re.compile(r"^.*?:\d+:\d+: error:", re.MULTILINE)
|
||||
_SORRY_WARNING = re.compile(r"declaration uses 'sorry'")
|
||||
_SORRY_TOKEN = re.compile(r"(?<![A-Za-z0-9_'])sorry(?![A-Za-z0-9_'])")
|
||||
|
||||
|
||||
@dataclass
|
||||
class VerifyResult:
|
||||
ok: bool
|
||||
mode: str
|
||||
used_sorry: bool
|
||||
diagnostics: str
|
||||
skipped: bool = False
|
||||
|
||||
@property
|
||||
def proved(self) -> bool:
|
||||
return self.ok and not self.used_sorry and self.mode == MODE_FULL
|
||||
|
||||
def as_dict(self) -> dict:
|
||||
return {
|
||||
"ok": self.ok,
|
||||
"mode": self.mode,
|
||||
"used_sorry": self.used_sorry,
|
||||
"proved": self.proved,
|
||||
"skipped": self.skipped,
|
||||
"diagnostics": self.diagnostics[-4000:],
|
||||
}
|
||||
|
||||
|
||||
def contains_sorry(code: str) -> bool:
|
||||
return bool(_SORRY_TOKEN.search(code))
|
||||
|
||||
|
||||
@dataclass
|
||||
class LeanChecker:
|
||||
"""Runs ``lake env lean`` (or whatever you configure) on a scratch file."""
|
||||
|
||||
command: list[str]
|
||||
project: str | None = None
|
||||
timeout: float = 120.0
|
||||
header: str = "import Mathlib\n"
|
||||
|
||||
@classmethod
|
||||
def from_settings(cls, settings) -> LeanChecker: # noqa: ANN001 - LeanSettings
|
||||
return cls(
|
||||
command=list(settings.command),
|
||||
project=settings.project,
|
||||
timeout=settings.timeout,
|
||||
header=settings.header,
|
||||
)
|
||||
|
||||
def available(self) -> bool:
|
||||
if not self.command:
|
||||
return False
|
||||
if shutil.which(self.command[0]) is None:
|
||||
return False
|
||||
if self.project and not Path(self.project).is_dir():
|
||||
return False
|
||||
return True
|
||||
|
||||
def source_for(self, code: str, imports: list[str] | None = None) -> str:
|
||||
header = self.header
|
||||
for module in imports or []:
|
||||
module = module.strip()
|
||||
if not module:
|
||||
continue
|
||||
line = module if module.startswith("import ") else f"import {module}"
|
||||
if line not in header:
|
||||
header += line + "\n"
|
||||
return header.rstrip("\n") + "\n\n" + code.strip() + "\n"
|
||||
|
||||
def check(
|
||||
self,
|
||||
code: str,
|
||||
*,
|
||||
mode: str = MODE_FULL,
|
||||
imports: list[str] | None = None,
|
||||
) -> VerifyResult:
|
||||
if not self.available():
|
||||
return VerifyResult(
|
||||
ok=False,
|
||||
mode=mode,
|
||||
used_sorry=contains_sorry(code),
|
||||
diagnostics=(
|
||||
f"Lean not available: command {self.command!r}"
|
||||
+ (f", project {self.project!r}" if self.project else "")
|
||||
),
|
||||
skipped=True,
|
||||
)
|
||||
source = self.source_for(code, imports)
|
||||
workdir = self.project or os.getcwd()
|
||||
with tempfile.TemporaryDirectory(prefix="leanbench-", dir=workdir) as directory:
|
||||
path = Path(directory) / "LeanBenchCheck.lean"
|
||||
path.write_text(source, encoding="utf-8")
|
||||
try:
|
||||
completed = subprocess.run( # noqa: S603 - command is operator-supplied
|
||||
[*self.command, str(path)],
|
||||
cwd=workdir,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return VerifyResult(False, mode, contains_sorry(code),
|
||||
f"Lean timed out after {self.timeout}s")
|
||||
except FileNotFoundError as error:
|
||||
return VerifyResult(False, mode, contains_sorry(code),
|
||||
f"Lean not runnable: {error}", skipped=True)
|
||||
diagnostics = (completed.stdout or "") + (completed.stderr or "")
|
||||
ok = completed.returncode == 0 and not _ERROR.search(diagnostics)
|
||||
used_sorry = bool(_SORRY_WARNING.search(diagnostics)) or contains_sorry(code)
|
||||
return VerifyResult(ok, mode, used_sorry, diagnostics[-8000:])
|
||||
|
||||
|
||||
class NullChecker:
|
||||
"""Stands in when Lean is unavailable (unit tests, a machine without elan)."""
|
||||
|
||||
def available(self) -> bool:
|
||||
return False
|
||||
|
||||
def check(self, code: str, *, mode: str = MODE_FULL, imports=None) -> VerifyResult:
|
||||
return VerifyResult(False, mode, contains_sorry(code), "Lean checking disabled",
|
||||
skipped=True)
|
||||
Reference in New Issue
Block a user