import { buildLemmaChart } from "./backends/lemma-chart.ts"; import { renderFlow } from "./backends/mermaid.ts"; import { buildProofChart } from "./backends/proof-chart.ts"; import { buildReport } from "./backends/report.ts"; import { type BudgetedBlocks, type CompileOutput, type Drawn, type Facts, type LeanCompileOptions, type ProofTree, type RootDrawn, spanContainsPlace, type Verdict, } from "./ir.ts"; import { attribute } from "./passes/attribute.ts"; import { buildBlocks } from "./passes/blocks.ts"; import { budgetBlocks, budgetLemmas } from "./passes/budget.ts"; import { checkAxioms, checkTree } from "./passes/check.ts"; import { contract } from "./passes/contract.ts"; import { foldSiblings } from "./passes/fold.ts"; import { inlineBlocks } from "./passes/inline.ts"; import { analyzeProofMarks } from "./passes/mark.ts"; import { reduceLemmas } from "./passes/reduce.ts"; import { analyzeTrust } from "./passes/trust.ts"; import { applyLemmaView } from "./passes/view-lemmas.ts"; function hasMarkInBlock(block: Drawn, mark: "open" | "sorry" | "error" | "restsOn"): boolean { if (block.marks.has(mark)) return true; return block.into.some((child) => hasMarkInBlock(child, mark)); } export function compileLean(facts: Facts, options: LeanCompileOptions = {}): CompileOutput { const { view, tactics = false } = options; const contracted = contract(facts); checkAxioms({ facts, graph: contracted }); const initialTrust = analyzeTrust(contracted); const applied = applyLemmaView({ graph: contracted, trust: initialTrust, view }); if (applied.hiddenCount > 0) checkAxioms({ facts, graph: applied.graph }); const trust = applied.hiddenCount > 0 ? analyzeTrust(applied.graph) : initialTrust; const reduced = reduceLemmas({ graph: applied.graph, trust }); checkAxioms({ facts, graph: reduced }); const budgetedLemmas = budgetLemmas({ graph: reduced, trust }); const lemmaFlow = buildLemmaChart({ budgeted: budgetedLemmas, trust, facts, hiddenByView: applied.hiddenCount, view, viewLabels: applied.labels, viewGroups: applied.groups, }); const lemmasMmd = renderFlow(lemmaFlow); let tree: ProofTree | undefined; let allBlocks: RootDrawn[] | undefined; let budgetedProof: BudgetedBlocks | undefined; let proofMmd: string | undefined; let hasOpenGoals = false; let hasProofFindings = false; const hasErrors = facts.messages.some( (m) => m.severity === "error" && spanContainsPlace(facts.targetSpan, m.at), ); const targetHasDoubt = trust.paths.length > 0 || (trust.below.get(facts.target)?.length ?? 0) > 0; if (facts.script && facts.script.roots.length > 0 && facts.script.steps.length > 0) { tree = attribute(facts.script); checkTree({ script: facts.script, tree }); const rawBlocks = buildBlocks(tree); const { roots: marked, unattributedDoubt } = analyzeProofMarks({ roots: rawBlocks, script: facts.script, tree, facts, trust: initialTrust, }); const inlined = inlineBlocks({ roots: marked, script: facts.script }); allBlocks = foldSiblings({ roots: inlined, script: facts.script }); budgetedProof = budgetBlocks({ roots: allBlocks }); hasOpenGoals = budgetedProof.roots.some((r) => hasMarkInBlock(r.block, "open")); hasProofFindings = budgetedProof.roots.some( (r) => hasMarkInBlock(r.block, "open") || hasMarkInBlock(r.block, "sorry") || hasMarkInBlock(r.block, "error") || hasMarkInBlock(r.block, "restsOn"), ); const targetClean = !targetHasDoubt && !hasErrors && !hasOpenGoals && !hasProofFindings; const proofFlow = buildProofChart({ budgeted: budgetedProof, script: facts.script, facts, graph: contracted, targetClean, allowProven: !unattributedDoubt, tactics, view, }); proofMmd = renderFlow(proofFlow); } const reasons: string[] = []; if (trust.paths.length > 0) { reasons.push(`${trust.paths.length} unproven ${trust.paths.length === 1 ? "path" : "paths"}`); } if (hasOpenGoals) reasons.push("open goals"); if (hasErrors) reasons.push("failing steps"); if (hasProofFindings && reasons.length === 0) reasons.push("unproven proof steps"); const verdict: Verdict = { clean: !targetHasDoubt && !hasErrors && !hasOpenGoals && !hasProofFindings, reasons, }; const report = buildReport({ facts, graph: reduced, budgetedLemmas, trust, verdict, hiddenByView: applied.hiddenCount, script: facts.script, tree, allBlocks, budgetedBlocks: budgetedProof, }); const exit: 0 | 1 = verdict.clean ? 0 : 1; const files = new Map(); files.set("report.md", report); files.set("lemmas.mmd", lemmasMmd); if (proofMmd !== undefined) files.set("proof.mmd", proofMmd); files.set("facts.json", JSON.stringify(facts, null, 2)); return { report, files, exit }; }