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
This commit is contained in:
alexmandClaude committed 2026-10-05 12:06:41 +00:00
1 parent be0db2d74f
commit 7ddc628597
21 files changed
+2654 -20

No files matched your search

+56 -2
View File
@@ -39,6 +39,10 @@ export { GENERATED_HEADER };
export interface MojoOptions {
/** The call to run, such as COUNT_BASE('A', "GATTACA"); becomes the body of `main` when the pseudo has no lines of its own. */
call?: string | null;
/** Internal: write the definitions only, no imports and no main (a library routine). */
defsOnly?: boolean;
/** Internal: the library routines' Mojo, written before the program's own definitions. */
libraryBlocks?: Array<{ id: string; lines: string[]; imports: string[] }>;
}
export interface MojoResult { source: string; map: LineMap; notes: Diagnostic[] }
@@ -183,7 +187,7 @@ class Gen {
constructor(private program: any, private source: string, private raising: Set<string>, private opts: MojoOptions) {
for (const c of program.comments || []) this.commentsByLine.set(c.line, c);
this.srcLines = source.split('\n');
for (const sub of program.subs.values()) {
for (const sub of [...program.subs.values(), ...(program.libs?.values() ?? [])]) {
const changed = this.changedIn(sub.node.body);
const assigned = this.assignedIn(sub.node.body);
this.varParams.set(key(sub.name), sub.params.map((p: any) => p.mode !== 'BYREF' && ((changed.has(up(p.name)) && isCollection(p.type)) || assigned.has(up(p.name)))));
@@ -344,7 +348,15 @@ class Gen {
this.emit(0, '', null);
this.emit(0, '', null);
}
for (const b of this.opts.libraryBlocks || []) {
this.emit(0, `# From the Programming+ library: ${b.id} (verified in pplus-library; generated from its pseudo)`, null);
for (const l of b.lines) this.emit(0, l, null);
for (const n of b.imports) this.imports.add(n);
this.emit(0, '', null);
this.emit(0, '', null);
}
for (const sub of p.subs.values()) { this.subroutine(sub); this.emit(0, '', null); this.emit(0, '', null); }
if (this.opts.defsOnly) return this.assemble();
this.mainFunction();
this.leadingComments(Infinity, 0);
return this.assemble();
@@ -997,10 +1009,24 @@ export function pseudoToMojo(pseudo: string, opts: MojoOptions = {}): MojoResult
return { unsupported: errors.length ? errors : [{ lang: 'pseudo', severity: 'error', id: 'translate-no-program', line: 1, message: 'The pseudo does not read yet, so there is nothing to translate.' }] };
}
const program = st.program;
// the library routines the program uses, each once, the ones they use first
const libs = libraryOrder(program);
const libRaising = new Set<string>();
const libraryBlocks: Array<{ id: string; lines: string[]; imports: string[] }> = [];
for (const lib of libs) {
const raising = inferRaising(lib.program, libRaising);
const gen = new Gen(lib.program, lib.source, raising, { defsOnly: true });
gen.run();
for (const sub of lib.program.subs.values()) if (raising.has(up(sub.name))) libRaising.add(up(sub.name));
const lines = gen.out.map((l) => l.text);
while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
libraryBlocks.push({ id: lib.id, lines, imports: [...gen.imports] });
}
opts = { ...opts, libraryBlocks };
const callText = (opts.call || '').trim();
const callName = callText ? /^\s*([A-Za-z_][A-Za-z0-9_]*)/.exec(callText)?.[1] : null;
// Inference, then translation; a line that could not be translated becomes a raise, so its routine raises too, and the pass is run again.
const extra = new Set<string>();
const extra = new Set<string>(libRaising);
for (let pass = 0; ; pass++) {
const raising = inferRaising(program, extra);
if (bodyRaises(program.main.body, raising)) raising.add('MAIN');
@@ -1014,6 +1040,34 @@ export function pseudoToMojo(pseudo: string, opts: MojoOptions = {}): MojoResult
}
}
/** The names of every routine a program calls, anywhere in it. */
function calledNames(program: any): Set<string> {
const out = new Set<string>();
const seen = new Set<any>();
const visit = (x: any): void => {
if (!x || typeof x !== 'object' || seen.has(x)) return;
seen.add(x);
if (Array.isArray(x)) { for (const y of x) visit(y); return; }
if ((x.kind === 'CallExpr' || x.kind === 'Call') && x.name) out.add(up(x.name));
for (const [k, v] of Object.entries(x)) if (k !== 'sub' && k !== 'sym' && k !== 'type' && k !== 'resolved' && v && typeof v === 'object') visit(v);
};
visit(program.main?.body);
for (const s of program.subs.values()) { visit(s.node.body); visit(s.requires); visit(s.ensures); }
return out;
}
/** Every library routine a program calls, directly or through another routine, dependencies first, each once. */
function libraryOrder(program: any, out: Map<string, any> = new Map()): any[] {
const called = calledNames(program);
for (const sub of program.libs?.values() ?? []) {
if (!called.has(up(sub.name))) continue;
if (out.has(sub.lib.id)) continue;
libraryOrder(sub.lib.program, out);
out.set(sub.lib.id, sub.lib);
}
return [...out.values()];
}
/** The set of names a generated source may use from the standard library, for the grounding test. */
export function stdlibNamesUsed(): string[] { return Object.keys(USED_STDLIB); }
export const STD_INDEX = MOJO_STD_EXPORTS;