import { type BudgetedBlocks, type BudgetedLemmas, byText, type Drawn, hasSubtreeMark, LEMMA_NODES, type LemmaGraph, PROOF_BLOCKS, type RootDrawn, type Trust, } from "../ir.ts"; export function budgetLemmas(input: { graph: LemmaGraph; trust: Trust; limit?: number; }): BudgetedLemmas { const { graph, trust, limit = LEMMA_NODES } = input; const kept = new Set([graph.target]); for (const entry of trust.paths) { for (const node of entry.path) kept.add(node); } const parent = new Map([[graph.target, undefined]]); const bfsOrder: string[] = [graph.target]; for (let i = 0; i < bfsOrder.length; i++) { const cur = bfsOrder[i]; const lemma = graph.lemmas.get(cur); if (!lemma) continue; for (const next of lemma.to.map((e) => e.name).sort(byText)) { if (parent.has(next)) continue; parent.set(next, cur); bfsOrder.push(next); } } for (const [name, ownDoubts] of trust.own) { if (ownDoubts.length === 0 || !parent.has(name)) continue; let cur: string | undefined = name; while (cur !== undefined) { kept.add(cur); cur = parent.get(cur); } } let frontier = [graph.target]; const visited = new Set([graph.target]); while (frontier.length > 0 && kept.size < limit) { const nextLevel = new Set(); for (const node of frontier) { const lemma = graph.lemmas.get(node); if (!lemma) continue; for (const edge of lemma.to) { if (!visited.has(edge.name)) nextLevel.add(edge.name); } } const sorted = [...nextLevel].sort(byText); for (const node of sorted) { visited.add(node); if (kept.size < limit) kept.add(node); } frontier = sorted; } const hidden = new Map(); for (const node of kept) { const lemma = graph.lemmas.get(node); if (!lemma) continue; let cutChildren = 0; for (const edge of lemma.to) { if (!kept.has(edge.name)) cutChildren++; } if (cutChildren > 0) hidden.set(node, cutChildren); } const total = graph.lemmas.size; return { graph, kept, hidden, total, cut: Math.max(0, total - kept.size), }; } function countBlocks(node: Drawn): number { let total = 1; for (const child of node.into) total += countBlocks(child); return total; } function summarizeSubtree(node: Drawn): { steps: number; cases: number } { if (node.hidden) return { steps: node.hidden.steps, cases: node.hidden.cases }; if (node.into.length === 0) { return { steps: node.steps.length * node.times, cases: node.times }; } let steps = node.steps.length * node.times; let cases = 0; for (const child of node.into) { const sub = summarizeSubtree(child); steps += sub.steps * node.times; cases += sub.cases * node.times; } return { steps, cases }; } function cloneDrawn(node: Drawn): Drawn { return { ...node, steps: [...node.steps], inlined: new Map(node.inlined), marks: new Set(node.marks), restsOnKinds: node.restsOnKinds ? new Set(node.restsOnKinds) : undefined, tags: [...node.tags], differentGoals: node.differentGoals, foldedEntries: node.foldedEntries.map((e) => ({ ...e, steps: [...e.steps], inlined: new Map(e.inlined), })), hidden: node.hidden ? { ...node.hidden } : undefined, into: node.into.map(cloneDrawn), }; } type Candidate = { parent: Drawn; index: number; size: number; depth: number; }; function collectCandidates(node: Drawn, depth: number, out: Candidate[]): void { for (let i = 0; i < node.into.length; i++) { const child = node.into[i]; if (child.hidden !== undefined) continue; if (!hasSubtreeMark(child) && child.errorLabel === undefined) { const size = countBlocks(child); if (size > 1) { out.push({ parent: node, index: i, size, depth: depth + 1 }); } } collectCandidates(child, depth + 1, out); } } export function budgetBlocks(input: { roots: RootDrawn[]; limit?: number }): BudgetedBlocks { const { limit = PROOF_BLOCKS } = input; const roots = input.roots.map(({ root, block }) => ({ root, block: cloneDrawn(block) })); const totalBlocks = roots.reduce((sum, r) => sum + countBlocks(r.block), 0); let shownBlocks = totalBlocks; while (shownBlocks > limit) { const candidates: Candidate[] = []; for (const r of roots) collectCandidates(r.block, 0, candidates); if (candidates.length === 0) break; candidates.sort((a, b) => b.size - a.size || b.depth - a.depth || b.index - a.index); const pick = candidates[0]; const target = pick.parent.into[pick.index]; const summary = summarizeSubtree(target); pick.parent.into[pick.index] = { ...target, steps: [], inlined: new Map(), into: [], hidden: summary, }; shownBlocks -= pick.size - 1; } return { roots, totalBlocks, shownBlocks, hiddenBlocks: Math.max(0, totalBlocks - shownBlocks), }; }