Files
pplus-core/scripts/library_godbolt.py
T
alexmandClaude 7ddc628597 Add USE and pin pplus-library: routines verified once, used by name
Routines that many programs need (a mean, a gap penalty, the
Lennard-Jones potential, the minimum image convention) were rewritten
and re-verified each time, and there was no way to open one and see
exactly what it does. They now live in pplus-library, pinned here as
the submodule library/, and a program brings them in with USE.

- USE module / USE module.Routine (pseudo-study resolveUses): each
  routine is studied once in its own file (its own USE lines resolved
  the same way, cycles refused) and handed to the checker as a library
  routine; unknown-library, unknown-routine, library-clash, use-place.
- A run steps over a library routine (one step at the call, one at the
  return, none of its lines); a broken contract is reported at the call,
  naming the routine. The prover uses its REQUIRE and ENSURE at the
  call and does not prove it again; it is not a unit of the program.
- pseudo-to-mojo writes each library routine the program calls once,
  dependencies first, labeled as coming from the library.
- src/library: the registry USE reads, generated from library/
  (scripts/library.mjs emit), so the site needs no files at run time.
- scripts/library.mjs check: every routine at every level: structure,
  cases, prover, fuzzer, generated Mojo, each reference implementation
  (CPython; cc at -O0 and -O2; core's Mojo interpreter; the pinned
  compiler's stored answer) held to the pseudo's results, and the Dafny
  proof with the pinned Dafny. Writes library/status.json and STATUS.md.
  scripts/library_godbolt.py stores the compiler's answers (network).
- pixi tasks library-check, library-emit, library-verify-mojo,
  library-harness; CI checks out the submodule and runs library-check.
- Tests: tests/library.test.mjs, corpus 53-library; docs/library.md and
  a library section in trust.md.

Gate: 332 pass; library-check: no failing check (16 routines; the Mojo
references are unverified until library-verify-mojo runs on the network).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
2026-10-05 12:06:41 +00:00

59 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Ask the pinned Mojo compiler (on Compiler Explorer) about every Mojo reference implementation of the library,
and store each answer beside the routine, in golden/mojo-<level>.json. Network; standard library only.
node scripts/library.mjs mojo-sources | python3 scripts/library_godbolt.py [--force] [--dry-run]
Each program is the reference's source with a main that prints every case (scripts/library.mjs builds it). An
answer is keyed by the SHA-256 of that exact program and the compiler id, so editing a reference or a case makes
it unverified until it is asked again; an answer already stored for the same program is not asked again unless
--force. pixi run library-check then compares the stored output with the pseudo's results.
"""
from __future__ import annotations
import hashlib
import json
import sys
from datetime import date
from pathlib import Path
CORE = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(CORE / "scripts" / "mojo"))
from mojocorpus import godbolt # noqa: E402
def main(argv: list[str]) -> int:
force = "--force" in argv
dry = "--dry-run" in argv
pin = json.loads((CORE / "mojo" / "pin.json").read_text())
base, compiler = pin["godbolt"]["base"], pin["godbolt"]["compiler"]
timeout = int(pin["godbolt"].get("timeoutSeconds", 90))
items = json.load(sys.stdin)
asked = 0
for it in items:
src = it["source"]
sha = hashlib.sha256(src.encode("utf-8")).hexdigest()
out = Path(it["golden"])
if out.exists() and not force:
old = json.loads(out.read_text())
if old.get("source_sha256") == sha and old.get("compiler") == compiler:
print(f"[library] {it['id']}: stored answer is current")
continue
if dry:
print(f"[library] {it['id']}: would ask {base} ({compiler}), {len(src)} bytes")
continue
data = godbolt.post(base, compiler, godbolt.request_body(src, "run"), timeout=timeout)
result = godbolt.normalise("run", data)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps({"tool": "godbolt", "compiler": compiler, "mojoVersion": pin["mojo"]["version"],
"source_sha256": sha, "asked": date.today().isoformat(), "result": result},
indent=2, ensure_ascii=False) + "\n")
asked += 1
print(f"[library] {it['id']}: compiled={result['compiled']} exit={result['exitCode']}")
print(f"[library] asked {asked} program(s)")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))