Files
pplus-core/tests/library.test.mjs
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

86 lines
5.5 KiB
JavaScript

// The library (library/, pplus-library): the registry core builds from it is current, every routine checks and
// runs as its record says, and USE behaves as docs/library.md describes: names resolve, runs step over the
// routines, the prover uses their contracts, and the Mojo translator writes each routine once.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { spawnSync } from 'node:child_process';
import { libraryModules, libraryRoutine } from '../dist/library/registry.js';
import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js';
import { pseudoToMojo } from '../dist/sync/pseudo-to-mojo.js';
import { checkMojo } from '../dist/mojo/check.js';
import { runMojo } from '../dist/mojo/interp.js';
test('the registry is generated from the library checkout and is up to date', () => {
assert.ok(fs.existsSync('library/modules'), 'library/ is not checked out: git submodule update --init');
const r = spawnSync(process.execPath, ['scripts/library.mjs', 'emit', '--check'], { encoding: 'utf8' });
assert.equal(r.status, 0, r.stderr || r.stdout);
});
test('every routine: one routine per file, its cases run, and its record is current and has no failing check', () => {
const mods = libraryModules();
assert.ok(mods.length >= 6, `only ${mods.length} modules`);
let routines = 0;
for (const m of mods) {
for (const r of m.routines) {
routines++;
const st = studyOf(r.source);
assert.ok(st.ok, `${r.id}: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join('; ')}`);
const subs = [...st.program.program.subs.values()];
assert.deepEqual(subs.map((s) => s.name), [r.name], `${r.id} defines ${subs.map((s) => s.name)}`);
assert.ok(r.cases.length >= 2, `${r.id} has fewer than two cases`);
for (const c of r.cases) {
const tr = runPseudo(st, { call: c.call, maxSteps: 2000000 });
assert.equal(tr.error, null, `${r.id}: ${c.call}: ${tr.error?.message}`);
assert.ok(c.result !== undefined, `${r.id}: ${c.call} has no recorded result: run pixi run library-check`);
}
const all = [...Object.entries(r.checks), ...r.references.flatMap((x) => Object.entries(x.checks).map(([k, v]) => [`${x.lang}/${x.level} ${k}`, v])), ...(r.proof ? [['proof', r.proof.check]] : [])];
for (const [k, c] of all) assert.notEqual(c.status, 'fail', `${r.id} ${k}: ${c.detail}`);
for (const k of ['structure', 'pseudo', 'prover', 'fuzz']) assert.ok(r.checks[k], `${r.id} has no ${k} check recorded: run pixi run library-check`);
}
}
assert.ok(routines >= 16, `only ${routines} routines`);
});
test('USE: a module or one routine; unknown names are errors at the USE line', () => {
assert.ok(libraryRoutine('md.LennardJones'));
assert.ok(libraryRoutine('MD', 'lennardjones'), 'names ignore case');
const ids = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => `${d.id}@${d.line}`);
assert.deepEqual(ids('USE nosuchmodule\n'), ['unknown-library@1']);
assert.deepEqual(ids('USE md.NoSuchRoutine\n'), ['unknown-routine@1']);
assert.deepEqual(ids('USE md.MinimumImage\nFUNCTION MinimumImage(X : FLOAT) RETURNS FLOAT\n RETURN X\nENDFUNCTION\n'), ['library-clash@2']);
assert.deepEqual(ids('USE align.MatchScore\nOUTPUT LinearGapPenalty(2, -1)\n'), ['unknown-function@2'], 'USE of one routine brings in only that one');
assert.deepEqual(ids('IF TRUE THEN\n USE md\nENDIF\n'), ['use-place@2']);
});
test('a run steps over a library routine, and a broken REQUIRE is reported at the call', () => {
const st = studyOf('USE md\nOUTPUT MinimumImage(2.3, 1.0)\nOUTPUT MinimumImage(1.0, 0.0)\n');
const tr = runPseudo(st, {});
assert.equal(tr.output[0], '0.2999999999999998');
assert.ok(tr.steps.every((s) => s.line === null || s.line <= 3), 'a step carries a line of the library file');
assert.ok(tr.steps.some((s) => s.kind === 'call' && /from the library \(md\.MinimumImage\)/.test(s.text)));
assert.equal(tr.error?.kind, 'require');
assert.equal(tr.error.line, 3);
assert.match(tr.error.message, /In the library routine md\.MinimumImage/);
});
test('the prover uses a routine\'s REQUIRE at the call and its ENSURE afterwards, without proving it again', () => {
const r = provePseudo(studyOf('USE seq.HammingDistance\nFUNCTION F(S : STRING) RETURNS INTEGER\n ENSURE RESULT <= LENGTH(S)\n RETURN HammingDistance(S, S)\nENDFUNCTION\n'));
assert.deepEqual(r.units.map((u) => u.name), ['F'], 'library routines are not units of the program');
const obs = r.units[0].obligations;
assert.equal(obs.find((o) => o.kind === 'require').status, 'proved');
assert.equal(obs.find((o) => o.kind === 'ensure').status, 'proved', 'the ENSURE of HammingDistance is a fact after the call');
});
test('the Mojo made from a program that uses the library holds each routine once, dependencies first, and agrees', () => {
const src = 'USE align\nOUTPUT NeedlemanWunschScore("GATTACA", "GCATGCU", 1, -1, -1), " ", MatchScore(\'A\', \'A\', 2, -1)\n';
const mj = pseudoToMojo(src);
const defs = [...mj.source.matchAll(/^def (\w+)/gm)].map((m) => m[1]);
assert.deepEqual(defs, ['match_score', 'linear_gap_penalty', 'needleman_wunsch_score', 'main']);
assert.match(mj.source, /# From the Programming\+ library: align\.MatchScore/);
const ck = checkMojo(mj.source);
assert.deepEqual(ck.diagnostics.filter((d) => d.severity === 'error'), []);
const run = runMojo(ck.module, mj.source, { maxSteps: 4000000 });
assert.equal(run.stdout.trim(), runPseudo(studyOf(src), {}).output.join('\n'));
});