import { type BudgetedLemmas, byText, type CheckedLeanView, clipLine, type Doubt, type DoubtKind, edgeKey, type Facts, type Flow, type FlowEdge, type FlowNode, type FlowShape, isSyntheticNativeAx, LINE_WIDTH, shortName, summarizeDoc, type Trust, } from "../ir.ts"; function displayNames( target: string, names: readonly string[], userNames: ReadonlyMap, ): Map { const targetUser = userNames.get(target) ?? target; const dot = targetUser.indexOf("."); const prefix = dot === -1 ? "" : targetUser.slice(0, dot + 1); const counts = new Map(); const stripped = new Map(); for (const name of names) { const u = userNames.get(name) ?? name; const s = prefix && u.startsWith(prefix) ? u.slice(prefix.length) : u; stripped.set(name, s); counts.set(s, (counts.get(s) ?? 0) + 1); } const out = new Map(); for (const name of names) { const s = stripped.get(name) ?? name; out.set(name, (counts.get(s) ?? 0) > 1 ? (userNames.get(name) ?? name) : s); } return out; } function doubtSummary(doubts: readonly Doubt[], ownerUserName: string): string | undefined { const tags: string[] = []; if (doubts.some((d) => d.kind === "error")) tags.push("Failing step"); const sorryDoubts = doubts.filter( (d): d is Extract => d.kind === "sorry", ); if (sorryDoubts.length > 0) { const otherOwners = [ ...new Set(sorryDoubts.map((d) => d.in).filter((o) => o !== ownerUserName)), ].sort(byText); if (otherOwners.length > 0 && sorryDoubts.every((d) => d.in !== ownerUserName)) { tags.push(clipLine(`Unproven gap (sorry in ${otherOwners.join(", ")})`, LINE_WIDTH)); } else { tags.push("Unproven gap (sorry)"); } } if (doubts.some((d) => d.kind === "native")) tags.push("Unchecked native code"); if (doubts.some((d) => d.kind === "axiom")) tags.push("Custom axiom"); return tags.length > 0 ? tags.join(", ") : undefined; } function doubtPathLegendLabel(kinds: ReadonlySet): string { const parts: string[] = []; if (kinds.has("sorry")) parts.push("unproven gap"); if (kinds.has("error")) parts.push("failing step"); if (kinds.has("axiom")) parts.push("custom axiom"); if (kinds.has("native")) parts.push("native_decide"); if (parts.length === 0) return "Depends on unproven gap"; return `Depends on ${parts.join(" / ")}`; } export function buildLemmaChart(input: { budgeted: BudgetedLemmas; trust: Trust; facts: Facts; hiddenByView?: number; view?: CheckedLeanView; viewLabels?: ReadonlyMap; viewGroups?: ReadonlyMap; }): Flow { const { budgeted, trust, facts, hiddenByView = 0, view, viewLabels, viewGroups } = input; const { graph, kept, hidden } = budgeted; const ordered = [graph.target, ...[...kept].filter((n) => n !== graph.target).sort(byText)]; const userNames = new Map(); for (const name of ordered) { const lemma = graph.lemmas.get(name); if (lemma) userNames.set(name, lemma.decl.userName); } const shortLabels = displayNames(graph.target, ordered, userNames); const doubtEdges = new Set(); for (const entry of trust.paths) { for (let i = 0; i + 1 < entry.path.length; i++) { doubtEdges.add(edgeKey({ from: entry.path[i], to: entry.path[i + 1] })); } } const ids = new Map(); let nextId = 0; for (const name of ordered) ids.set(name, `n${nextId++}`); const nodes: FlowNode[] = []; const edges: FlowEdge[] = []; const usedClasses = new Set(); const doubtPathKinds = new Set(); for (const name of ordered) { const lemma = graph.lemmas.get(name); if (!lemma) continue; const id = ids.get(name)!; const ownDoubts = trust.own.get(name) ?? []; const belowDoubts = trust.below.get(name) ?? []; const customLabel = viewLabels?.get(name); const shortDisplay = shortLabels.get(name) ?? shortName(lemma.decl.userName); const lines: string[] = []; if (customLabel !== undefined) { lines.push(clipLine(customLabel, 120)); } else if (isSyntheticNativeAx(lemma.decl.name)) { lines.push("native_decide check"); } else if (lemma.decl.doc) { lines.push(summarizeDoc(lemma.decl.doc, LINE_WIDTH)); lines.push(clipLine(shortDisplay, LINE_WIDTH)); } else { lines.push(clipLine(shortDisplay, LINE_WIDTH)); } const badge = doubtSummary(ownDoubts, lemma.decl.userName); if (badge) lines.push(badge); const classes: string[] = []; if (name === graph.target) classes.push("target"); if (ownDoubts.some((d) => d.kind === "error")) classes.push("error"); else if (ownDoubts.some((d) => d.kind === "sorry")) classes.push("sorry"); else if (ownDoubts.some((d) => d.kind === "native")) classes.push("native"); else if (ownDoubts.some((d) => d.kind === "axiom")) classes.push("axiom"); else if (belowDoubts.length > 0 && name !== graph.target) { classes.push("doubtPath"); for (const d of belowDoubts) doubtPathKinds.add(d.kind); } for (const cls of classes) usedClasses.add(cls); const shape: FlowShape = name === graph.target ? "round" : ownDoubts.length > 0 || lemma.decl.kind === "axiom" ? "hexagon" : "box"; nodes.push({ id, lines, shape, classes }); for (const edge of lemma.to) { if (!kept.has(edge.name)) continue; const toId = ids.get(edge.name)!; const thick = doubtEdges.has(edgeKey({ from: name, to: edge.name })); const dotted = edge.through === "definition"; edges.push({ from: id, to: toId, label: dotted ? "via def" : undefined, dotted, thick, }); } const cutCount = hidden.get(name) ?? 0; if (cutCount > 0) { const stubId = `n${nextId++}`; usedClasses.add("stub"); nodes.push({ id: stubId, lines: [`+${cutCount} ${cutCount === 1 ? "lemma" : "lemmas"}`], shape: "stub", classes: ["stub"], }); edges.push({ from: id, to: stubId, label: undefined, dotted: true, thick: false }); } } const groups: { title: string; nodes: string[] }[] = []; const assignedToGroup = new Set(); const hasCustomGroups = viewGroups !== undefined && viewGroups.size > 0; if (hasCustomGroups) { const byCustom = new Map(); for (const name of ordered) { const custom = viewGroups.get(name); if (custom === undefined) continue; assignedToGroup.add(name); if (custom === null) continue; const list = byCustom.get(custom); if (list) list.push(ids.get(name)!); else byCustom.set(custom, [ids.get(name)!]); } for (const [title, members] of byCustom) { if (members.length > 0) groups.push({ title, nodes: members }); } } const mutualSeen = new Set(); for (const name of ordered) { if (assignedToGroup.has(name)) continue; const lemma = graph.lemmas.get(name); if (!lemma || lemma.decl.group.length <= 1) continue; const key = [...lemma.decl.group].sort(byText).join("\0"); if (mutualSeen.has(key)) continue; mutualSeen.add(key); const members = lemma.decl.group .filter((m) => kept.has(m) && !assignedToGroup.has(m)) .map((m) => ids.get(m)!); if (members.length > 1) { for (const m of lemma.decl.group) if (kept.has(m)) assignedToGroup.add(m); groups.push({ title: "Mutual definitions", nodes: members }); } } if (!hasCustomGroups) { const byModule = new Map(); for (const name of ordered) { if (assignedToGroup.has(name)) continue; const lemma = graph.lemmas.get(name); if (!lemma) continue; const list = byModule.get(lemma.decl.module); if (list) list.push(ids.get(name)!); else byModule.set(lemma.decl.module, [ids.get(name)!]); } if (byModule.size > 1) { for (const mod of [...byModule.keys()].sort(byText)) { const members = byModule.get(mod)!; if (members.length > 0) groups.push({ title: mod, nodes: members }); } } } const legendSpecs: [string, string][] = [ ["Theorem", "target"], ["Unproven gap (sorry)", "sorry"], ["Failing step", "error"], ["Unchecked native code", "native"], ["Custom axiom", "axiom"], [doubtPathLegendLabel(doubtPathKinds), "doubtPath"], ["Hidden details", "stub"], ]; const legend = legendSpecs .filter(([, cls]) => usedClasses.has(cls)) .map(([text, cls]) => ({ text, cls })); if (edges.length > 0) { legend.push({ text: "Arrows mean uses", cls: "note" }); } if (hiddenByView > 0) { legend.push({ text: `View hides ${hiddenByView} supporting ${hiddenByView === 1 ? "lemma" : "lemmas"}`, cls: "note", }); } const defaultTitle = `${shortName(facts.target)} - lemmas`; return { title: view?.title?.lemmas ?? defaultTitle, nodes, edges, groups, legend, }; }