/** * The library (pplus-library, through core's registry) as the site shows it: a routine opened in the Bridge, and * a summary of what its checks found. DOM-free; src/ui/library.ts draws it. * * A library routine opens like a catalog problem, under the id `lib:.` (#/bridge/p/lib:md.LennardJones): * its pseudo in the pseudo pane and its first case as the call, so it can be stepped through line by line, which * a program that only calls it never does. */ import { libraryModules, libraryRoutine, type LibraryRoutine } from '#core/library/registry.js'; import type { LibraryCheck } from '#core/library/types.js'; import { emptyState, type PlayState } from './state.js'; export const LIBRARY_PREFIX = 'lib:'; export function isLibraryId(id: string): boolean { return id.startsWith(LIBRARY_PREFIX); } /** The state a library routine opens in: its pseudo, and its first case as the call. */ export function stateForLibraryRoutine(r: LibraryRoutine): PlayState { const s = emptyState(); s.problem = `${LIBRARY_PREFIX}${r.id}`; s.title = `${r.id}, from the library`; s.sources = { pseudo: r.source }; s.lang = 'pseudo'; s.call = r.cases[0]?.call ?? null; s.view = 'trace'; return s; } /** The state for a `lib:` problem id, or null when the library has no such routine. */ export function libraryState(id: string): PlayState | null { if (!isLibraryId(id)) return null; const r = libraryRoutine(id.slice(LIBRARY_PREFIX.length)); return r ? stateForLibraryRoutine(r) : null; } /** Every check of a routine, named as the page lists them: its own checks, each reference's runs, the proof. */ export function checksOf(r: LibraryRoutine): Array<{ name: string; check: LibraryCheck }> { const out: Array<{ name: string; check: LibraryCheck }> = []; const LABEL: Record = { structure: 'One routine per file', pseudo: 'Cases run', prover: 'Prover', fuzz: 'Fuzzer', mojo: 'Generated Mojo' }; for (const k of ['structure', 'pseudo', 'prover', 'fuzz', 'mojo']) if (r.checks[k]) out.push({ name: LABEL[k], check: r.checks[k] }); for (const ref of r.references) for (const [how, c] of Object.entries(ref.checks)) out.push({ name: `${ref.lang} ${ref.level}, ${how}`, check: c }); if (r.proof) out.push({ name: `Proof (${r.proof.lang})`, check: r.proof.check }); return out; } /** How many checks of a routine ended each way. */ export function tally(r: LibraryRoutine): Record { const t: Record = { pass: 0, fail: 0, open: 0, skipped: 0, unverified: 0 }; for (const { check } of checksOf(r)) t[check.status]++; return t; } export { libraryModules, libraryRoutine }; export type { LibraryRoutine };