import { byText, type CheckedLeanView, type Lemma, type LemmaGraph, type LemmaViewApplied, type LemmaViewInfo, shortName, sortUnique, type Through, type Trust, trustStatusOf, } from "../ir.ts"; function unhideableReason(input: { name: string; lemma: Lemma; target: string; trust: Trust; }): string | undefined { if (input.name === input.target) return "is the target theorem"; const own = input.trust.own.get(input.name) ?? []; if (own.some((d) => d.kind === "error")) return "has a failing step"; if (own.some((d) => d.kind === "sorry")) return "has an unproven gap (sorry)"; if (own.some((d) => d.kind === "native")) return "uses unchecked native code"; if (own.some((d) => d.kind === "axiom") || input.lemma.decl.kind === "axiom") { return "is or uses a custom axiom"; } const below = input.trust.below.get(input.name) ?? []; if (below.length > 0) return "depends on an unproven gap or axiom"; return undefined; } export function applyLemmaView(input: { graph: LemmaGraph; trust: Trust; view: CheckedLeanView | undefined; }): LemmaViewApplied { const { graph, trust, view } = input; const labels = new Map(); const groups = new Map(); if (!view || (!view.lemma && !view.group)) { return { graph, labels, groups, hiddenCount: 0 }; } const hidden = new Set(); const ordered = [ graph.target, ...[...graph.lemmas.keys()].filter((n) => n !== graph.target).sort(byText), ]; for (const name of ordered) { const lemma = graph.lemmas.get(name); if (!lemma) continue; const info: LemmaViewInfo = { name, userName: lemma.decl.userName, shortName: shortName(lemma.decl.userName), kind: lemma.decl.kind, module: lemma.decl.module, line: lemma.decl.line, doc: lemma.decl.doc, statement: lemma.decl.statement, isTarget: name === graph.target, status: trustStatusOf({ own: trust.own.get(name) ?? [], below: trust.below.get(name) ?? [], }), axioms: [...lemma.axioms], library: [...lemma.library], }; let groupFromLemma: string | undefined; if (view.lemma) { const answer = view.lemma(info); if (answer?.hide) { const reason = unhideableReason({ name, lemma, target: graph.target, trust }); if (reason !== undefined) { throw new Error( `The view tried to hide ${lemma.decl.userName}, which ${reason}. A view can hide only clean supporting lemmas.`, ); } hidden.add(name); continue; } if (answer && !answer.hide) { labels.set(name, answer.label); groupFromLemma = answer.group; } } if (groupFromLemma !== undefined) { groups.set(name, groupFromLemma); } else if (view.group) { const g = view.group(info); if (g !== undefined) groups.set(name, g); } } if (hidden.size === 0) { return { graph, labels, groups, hiddenCount: 0 }; } const nextLemmas = new Map(); for (const name of ordered) { if (hidden.has(name)) continue; const lemma = graph.lemmas.get(name); if (!lemma) continue; const to = new Map(); const axioms = new Set(lemma.axioms); const library = new Set(lemma.library); const seenHidden = new Map(); const work: { ref: string; through: Through }[] = lemma.to.map((e) => ({ ref: e.name, through: e.through, })); while (work.length > 0) { const item = work.shift()!; if (hidden.has(item.ref)) { const prev = seenHidden.get(item.ref); if (prev === "proof" || prev === item.through) continue; seenHidden.set(item.ref, item.through); const sub = graph.lemmas.get(item.ref); if (!sub) continue; for (const ax of sub.axioms) axioms.add(ax); for (const lib of sub.library) library.add(lib); for (const edge of sub.to) { const nextThrough: Through = item.through === "definition" || edge.through === "definition" ? "definition" : "proof"; work.push({ ref: edge.name, through: nextThrough }); } } else { if (item.ref === name) continue; const prev = to.get(item.ref); if (prev === undefined || item.through === "proof") to.set(item.ref, item.through); } } nextLemmas.set(name, { ...lemma, to: [...to.entries()] .sort(([a], [b]) => byText(a, b)) .map(([edgeName, through]) => ({ name: edgeName, through })), axioms: sortUnique(axioms), library: sortUnique(library), }); } return { graph: { target: graph.target, lemmas: nextLemmas, reducedEdges: graph.reducedEdges, }, labels, groups, hiddenCount: hidden.size, }; }