import { byText, type Decl, type Facts, type Lemma, type LemmaGraph, type LemmaSorry, type Outside, sortUnique, type Through, } from "../ir.ts"; function checkAcyclic(lemmas: ReadonlyMap, start: string): void { const state = new Map(); const stack: string[] = []; const visit = (name: string): void => { state.set(name, 1); stack.push(name); const lemma = lemmas.get(name); if (lemma) { for (const edge of lemma.to) { const mark = state.get(edge.name); if (mark === 1) { const at = stack.indexOf(edge.name); const cycle = [...stack.slice(at), edge.name].join(" -> "); throw new Error(`Cycle in visible lemma graph: ${cycle}.`); } if (mark === undefined) visit(edge.name); } } stack.pop(); state.set(name, 2); }; visit(start); } export function contract(facts: Facts): LemmaGraph { const decls = new Map(); for (const d of facts.decls) decls.set(d.name, d); const outside = new Map(); for (const o of facts.outside) outside.set(o.name, o); const rootDecl = decls.get(facts.target); if (!rootDecl) throw new Error(`Target declaration ${facts.target} is missing from decls.`); if (!rootDecl.visible) throw new Error(`Target declaration ${facts.target} must be visible in decls.`); const lemmas = new Map(); const queue: string[] = [facts.target]; const queued = new Set([facts.target]); while (queue.length > 0) { const name = queue.shift()!; const decl = decls.get(name); if (!decl) continue; const to = new Map(); const library = new Set(); const axioms = new Set(); const sorries: LemmaSorry[] = []; const seenSorries = new Set(); const pushSorry = (entry: LemmaSorry, mod: string): void => { const key = entry.at ? `${mod}\0${entry.in}\0${entry.synthetic}\0${entry.at.line}:${entry.at.column}` : `${mod}\0${entry.in}\0${entry.synthetic}\0`; if (seenSorries.has(key)) return; seenSorries.add(key); sorries.push(entry); }; for (const s of decl.sorries) { pushSorry( { in: s.in ?? decl.userName, synthetic: s.synthetic, at: s.at, }, decl.module, ); } if (decl.kind === "axiom") axioms.add(decl.name); const hiddenThrough = new Map(); const hiddenSorriesSeen = new Set(); const outsideSorriesSeen = new Set(); const work: { ref: string; through: Through }[] = decl.uses.map((ref) => ({ ref, through: "proof", })); while (work.length > 0) { const item = work.shift()!; const sub = decls.get(item.ref); if (sub) { if (sub.visible) { if (sub.name === decl.name) continue; const prev = to.get(sub.name); if (prev === undefined || item.through === "proof") to.set(sub.name, item.through); if (!queued.has(sub.name)) { queued.add(sub.name); queue.push(sub.name); } } else { const nextThrough: Through = item.through === "definition" || sub.isUserDef ? "definition" : "proof"; if (!hiddenSorriesSeen.has(sub.name)) { hiddenSorriesSeen.add(sub.name); for (const s of sub.sorries) { pushSorry( { in: s.in ?? (sub.isUserDef ? sub.userName : decl.userName), synthetic: s.synthetic, at: s.at, }, sub.module, ); } } const prev = hiddenThrough.get(sub.name); if (prev === "proof" || prev === nextThrough) continue; hiddenThrough.set(sub.name, nextThrough); for (const nextRef of sub.uses) work.push({ ref: nextRef, through: nextThrough }); } continue; } const ext = outside.get(item.ref); if (ext) { if (ext.kind === "theorem") library.add(ext.name); for (const ax of ext.axioms) axioms.add(ax); if ( ext.name !== "sorryAx" && ext.axioms.includes("sorryAx") && !outsideSorriesSeen.has(ext.name) ) { outsideSorriesSeen.add(ext.name); pushSorry( { in: `${ext.name} (${ext.home})`, synthetic: false, at: undefined, }, ext.home, ); } } } const edges = [...to.entries()] .sort(([a], [b]) => byText(a, b)) .map(([edgeName, through]) => ({ name: edgeName, through })); lemmas.set(name, { decl, to: edges, library: sortUnique(library), axioms: sortUnique(axioms), sorries, }); } checkAcyclic(lemmas, facts.target); return { target: facts.target, lemmas, reducedEdges: 0 }; }