import { branchStatusOf, type BudgetedBlocks, type BudgetedLemmas, byText, type Drawn, type Facts, firstSentence, type FoldedEntry, formatDoubt, type Goal, type GoalId, humanCaseTag, isNativeAxiom, isStandardAxiom, type LemmaGraph, type ProofTree, type RootDrawn, type Script, spanContainsPlace, type Step, type StepId, stripCasePrefix, type Trust, type Verdict, } from "../ir.ts"; type BlockReportEntry = { id: string; block: Drawn; entry: FoldedEntry; caseTag: string | undefined; }; function collectAllEntries( block: Drawn, stableId: string, ancestorTags: readonly string[], out: BlockReportEntry[], ): void { const entries = block.foldedEntries.length > 0 ? block.foldedEntries : [ { entry: block.entry, last: block.last, tag: block.tags[0], steps: block.steps, inlined: block.inlined, errorLabel: block.errorLabel, }, ]; for (const [idx, entry] of entries.entries()) { const id = entries.length > 1 ? `${stableId}[${idx + 1}]` : stableId; const caseTag = entry.tag !== undefined ? humanCaseTag(stripCasePrefix({ rawTag: entry.tag, ancestorTags })) : undefined; out.push({ id, block, entry, caseTag }); } const primaryTag = block.tags[0]; const nextAncestors = primaryTag !== undefined ? [...ancestorTags, primaryTag] : ancestorTags; for (const [i, child] of block.into.entries()) { collectAllEntries(child, `${stableId}.${i}`, nextAncestors, out); } } export function buildReport(input: { facts: Facts; graph: LemmaGraph; budgetedLemmas: BudgetedLemmas; trust: Trust; verdict: Verdict; hiddenByView?: number; script?: Script; tree?: ProofTree; allBlocks?: RootDrawn[]; budgetedBlocks?: BudgetedBlocks; }): string { const { facts, graph, budgetedLemmas, trust, verdict, hiddenByView = 0, script, tree, allBlocks, budgetedBlocks, } = input; const lines: string[] = []; lines.push(`# ${facts.target}`); lines.push(""); const standardAxioms = facts.axioms.filter(isStandardAxiom).sort(byText); const targetMessages = facts.messages.filter((m) => spanContainsPlace(facts.targetSpan, m.at)); if (verdict.clean) { const axNote = standardAxioms.length > 0 ? `standard axioms at most (${standardAxioms.join(", ")})` : "no axioms"; lines.push(`Clean. Closed proof resting on ${axNote}.`); } else { lines.push(`Audit findings: ${verdict.reasons.join(", ")}.`); } lines.push(""); const metaParts: string[] = [ `File: \`${facts.file}\``, `Module: \`${facts.module}\``, `Lean: \`${facts.lean}\``, ]; if (facts.package) metaParts.splice(2, 0, `Package: \`${facts.package}\``); lines.push(metaParts.join(" · ")); const lemmaSummary: string[] = [ `Lemmas: ${budgetedLemmas.kept.size} drawn of ${budgetedLemmas.total}`, ]; if (hiddenByView > 0) { lemmaSummary.push( `view hides ${hiddenByView} supporting ${hiddenByView === 1 ? "lemma" : "lemmas"}`, ); } if (budgetedLemmas.cut > 0) lemmaSummary.push(`${budgetedLemmas.cut} cut by budget`); if (graph.reducedEdges > 0) lemmaSummary.push( `${graph.reducedEdges} transitive ${graph.reducedEdges === 1 ? "edge" : "edges"} reduced`, ); if (budgetedBlocks) { lemmaSummary.push( `Proof blocks: ${budgetedBlocks.shownBlocks} drawn of ${budgetedBlocks.totalBlocks}`, ); if (budgetedBlocks.hiddenBlocks > 0) lemmaSummary.push(`${budgetedBlocks.hiddenBlocks} summarized by budget`); } lines.push(`${lemmaSummary.join(" · ")}.`); const isPartial = script !== undefined && (script.partial === true || (budgetedBlocks !== undefined && budgetedBlocks.roots.some((r) => r.root.where === "term"))); if (isPartial) { lines.push( "Note: proof.mmd covers only the tactic sub-proofs; term-mode parts of the theorem are omitted from the proof tree.", ); } if (trust.paths.length > 0) { lines.push(""); lines.push("## Doubt paths"); for (const [i, entry] of trust.paths.entries()) { const chain = entry.path.map((n) => graph.lemmas.get(n)?.decl.userName ?? n).join(" -> "); lines.push(`${i + 1}. ${formatDoubt(entry.doubt)}: ${chain}`); } } lines.push(""); lines.push("## Axioms"); if (facts.axioms.length === 0) { lines.push("None."); } else { for (const ax of [...facts.axioms].sort(byText)) { const tag = ax === "sorryAx" ? "unproven gap (sorry)" : isStandardAxiom(ax) ? "standard" : isNativeAxiom(ax) ? "unchecked native code" : "custom axiom"; lines.push(`- \`${ax}\` (${tag})`); } } const reportRoots = allBlocks ?? budgetedBlocks?.roots; if (script && reportRoots && reportRoots.length > 0) { const goalById = new Map(); for (const g of script.goals) goalById.set(g.id, g); const stepById = new Map(); for (const s of script.steps) stepById.set(s.id, s); const entries: BlockReportEntry[] = []; for (const [i, r] of reportRoots.entries()) { const rootId = reportRoots.length === 1 ? "root" : `root${i}`; collectAllEntries(r.block, rootId, [], entries); } if (entries.length > 0) { lines.push(""); lines.push("## Goals"); for (const item of entries) { const g = goalById.get(item.entry.entry); if (!g) continue; const status = branchStatusOf(item.block.marks); const tagPart = item.caseTag ? ` (${item.caseTag})` : ""; lines.push( `- **${item.id}${tagPart}** (${status}, ${g.final}${g.closedBy ? `, ${g.closedBy}` : ""})`, ); for (const hid of g.hyps) { const h = script.hyps[hid]; if (!h) continue; const val = h.value !== undefined ? ` := ${h.value}` : ""; lines.push(` \`${h.name} : ${h.type}${val}\``); } lines.push(` \`⊢ ${g.target}\``); if (item.entry.steps.length > 0) { const stepTexts = item.entry.steps .map((sid) => { const s = stepById.get(sid); if (!s) return undefined; const inl = item.entry.inlined.get(sid)?.filter((t) => !s.text.includes(t)); return inl && inl.length > 0 ? `${s.text} [${inl.join("; ")}]` : s.text; }) .filter((t): t is string => t !== undefined); if (stepTexts.length > 0) { lines.push(` Tactics: ${stepTexts.map((t) => `\`${t}\``).join("; ")}`); } } if (item.entry.errorLabel !== undefined) { lines.push(` Failing step: \`${item.entry.errorLabel}\``); } } } } lines.push(""); lines.push("## Lemmas"); const lemmaNames = [ graph.target, ...[...graph.lemmas.keys()].filter((n) => n !== graph.target).sort(byText), ]; for (const name of lemmaNames) { const lemma = graph.lemmas.get(name); if (!lemma) continue; const loc = lemma.decl.line ? `:${lemma.decl.line}` : ""; const docPart = lemma.decl.doc ? ` — ${firstSentence(lemma.decl.doc)}` : ""; lines.push( `- \`${lemma.decl.userName}\` (${lemma.decl.kind}, ${lemma.decl.module}${loc})${docPart}`, ); if (lemma.decl.statement) lines.push(` \`${lemma.decl.statement.replace(/\s+/g, " ").trim()}\``); if (lemma.library.length > 0) lines.push(` Library: ${lemma.library.join(", ")}`); } if (tree && tree.notes.length > 0) { lines.push(""); lines.push("## Notes"); for (const note of tree.notes) lines.push(`- ${note}`); } if (targetMessages.length > 0) { lines.push(""); lines.push("## Messages"); for (const msg of targetMessages) { lines.push(`- ${msg.severity} at ${msg.at.line}:${msg.at.column}: ${msg.text}`); } } return lines.join("\n"); }