export const FORMAT_VERSION = 1 as const; export const SUPPORTED_LEAN = "v4.34.1" as const; export const STANDARD_AXIOMS = ["propext", "Classical.choice", "Quot.sound"] as const; export const COMPILER_AXIOMS = [ "Lean.ofReduceBool", "Lean.ofReduceNat", "Lean.trustCompiler", ] as const; export const LEMMA_NODES = 60; export const PROOF_BLOCKS = 40; export const BLOCK_LINES = 8; export const BLOCK_HYPS = 3; export const LINE_WIDTH = 64; export type GoalId = number; export type StepId = number; export type Place = { line: number; column: number }; export type Span = { line: number; column: number; endLine: number; endColumn: number }; export type DeclKind = | "theorem" | "axiom" | "def" | "opaque" | "inductive" | "constructor" | "recursor" | "quot"; export const DECL_KINDS: readonly DeclKind[] = [ "theorem", "axiom", "def", "opaque", "inductive", "constructor", "recursor", "quot", ]; export type SorryUse = { synthetic: boolean; at: Place | undefined; in?: string }; export type Decl = { name: string; userName: string; kind: DeclKind; visible: boolean; isUserDef: boolean; module: string; line: number | undefined; doc: string | undefined; statement: string; group: string[]; uses: string[]; sorries: SorryUse[]; }; export type Outside = { name: string; kind: DeclKind; home: string; axioms: string[]; }; export type Hyp = { name: string; type: string; value: string | undefined }; export type GoalFinal = "assigned" | "open" | "gone"; export type GoalClosedBy = "proof" | "sorry" | "error" | undefined; export type Goal = { id: GoalId; tag: string | undefined; hyps: number[]; target: string; final: GoalFinal; closedBy: GoalClosedBy; }; export type Step = { id: StepId; kind: string; text: string; span: Span; names: string[]; }; export type Event = { step: StepId; before: GoalId[]; after: GoalId[]; assigned: GoalId[]; declared: GoalId[]; reach: [GoalId, GoalId[]][]; children: Event[]; }; export type Root = { goal: GoalId; where: "body" | "term" | "termination" }; export type Script = { bodyText: string; roots: Root[]; goals: Goal[]; hyps: Hyp[]; steps: Step[]; events: Event[]; partial?: boolean; }; export type Message = { severity: "error" | "warning"; at: Place; text: string }; export type Facts = { format: 1; lean: string; file: string; module: string; package: string | undefined; target: string; targetSpan: Span; axioms: string[]; decls: Decl[]; outside: Outside[]; script: Script | undefined; messages: Message[]; }; export type Through = "proof" | "definition"; export type LemmaSorry = { in: string; synthetic: boolean; at: Place | undefined; }; export type Lemma = { decl: Decl; to: { name: string; through: Through }[]; library: string[]; axioms: string[]; sorries: LemmaSorry[]; }; export type LemmaGraph = { target: string; lemmas: Map; reducedEdges: number; }; export type DoubtKind = "sorry" | "error" | "axiom" | "native"; export type Doubt = | { kind: "sorry" | "error"; in: string; at: Place | undefined } | { kind: "axiom" | "native"; name: string }; export type Trust = { own: Map; below: Map; paths: { doubt: Doubt; path: string[] }[]; }; export type TrustStatus = "clean" | "sorry" | "error" | "axiom" | "native" | "restsOn"; export type BranchStatus = "clean" | "sorry" | "error" | "open" | "restsOn"; export type BudgetedLemmas = { graph: LemmaGraph; kept: Set; hidden: Map; total: number; cut: number; }; export type Close = { step: StepId; opens: GoalId[] }; export type ProofTree = { roots: Root[]; closes: Map; notes: string[] }; export type Mark = "sorry" | "error" | "open" | "restsOn"; export type Block = { entry: GoalId; steps: StepId[]; inlined: Map; errorLabel: string | undefined; last: GoalId; into: Block[]; marks: Set; restsOnKinds?: Set; }; export type RootBlock = { root: Root; block: Block }; export type FoldedEntry = { entry: GoalId; last: GoalId; tag: string | undefined; steps: StepId[]; inlined: Map; errorLabel: string | undefined; }; export type Drawn = Omit & { into: Drawn[]; times: number; tags: string[]; differentGoals: boolean; foldedEntries: FoldedEntry[]; hidden: { steps: number; cases: number } | undefined; }; export type RootDrawn = { root: Root; block: Drawn }; export type BudgetedBlocks = { roots: RootDrawn[]; totalBlocks: number; shownBlocks: number; hiddenBlocks: number; }; export type Verdict = { clean: boolean; reasons: string[]; }; export type LemmaViewInfo = { name: string; userName: string; shortName: string; kind: DeclKind; module: string; line: number | undefined; doc: string | undefined; statement: string; isTarget: boolean; status: TrustStatus; axioms: string[]; library: string[]; }; export type LemmaViewAnswer = | { hide: true } | { hide: false; label: string; group: string | undefined }; export type BranchViewInfo = { id: string; goal: string; newHyps: Hyp[]; tactics: string[]; lemmas: string[]; status: BranchStatus; caseTag: string | undefined; rawCaseTag: string | undefined; depth: number; isRoot: boolean; isLeaf: boolean; times: number; }; export type BranchViewAnswer = { label: string; detail: string | undefined; }; export type CaseLabelInfo = { id: string; rawTag: string; parentTag: string | undefined; goal: string; status: BranchStatus; isLeaf: boolean; }; export type LeanViewTitle = { lemmas: string | undefined; proof: string | undefined; }; export const leanViewChecked: unique symbol = Symbol("leanViewChecked"); export type CheckedLeanView = { readonly [leanViewChecked]: true; readonly title: LeanViewTitle | undefined; readonly lemma: ((info: LemmaViewInfo) => LemmaViewAnswer | undefined) | undefined; readonly group: ((info: LemmaViewInfo) => string | null | undefined) | undefined; readonly branch: ((info: BranchViewInfo) => BranchViewAnswer | undefined) | undefined; readonly caseLabel: ((tag: string, info: CaseLabelInfo) => string | undefined) | undefined; }; export type LemmaViewApplied = { graph: LemmaGraph; labels: Map; groups: Map; hiddenCount: number; }; export type LeanCompileOptions = { view?: CheckedLeanView; tactics?: boolean; }; export type FlowShape = "box" | "round" | "hexagon" | "circle" | "stub"; export type FlowNode = { id: string; lines: string[]; shape: FlowShape; classes: string[] }; export type FlowEdge = { from: string; to: string; label: string | undefined; dotted: boolean; thick: boolean; }; export type Flow = { title: string | undefined; nodes: FlowNode[]; edges: FlowEdge[]; groups: { title: string; nodes: string[] }[]; legend: { text: string; cls: string }[]; }; export type CompileOutput = { report: string; files: Map; exit: 0 | 1; }; const STANDARD_SET: ReadonlySet = new Set(STANDARD_AXIOMS); const COMPILER_SET: ReadonlySet = new Set(COMPILER_AXIOMS); const NATIVE_AXIOM_RE = /(?:^|\.)_native\.[^.]+\.ax(?:_\d+)?$/; const STANDARD_CASE_TAGS: Readonly> = { mp: "Forward", mpr: "Reverse", inl: "Left case", inr: "Right case", zero: "Base case", succ: "Inductive step", nil: "Empty list", cons: "Non-empty list", true: "When true", false: "When false", }; export const byText = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0); export const list = (items: readonly string[], join = "and"): string => items.length <= 2 ? items.join(` ${join} `) : `${items.slice(0, -1).join(", ")}, ${join} ${items[items.length - 1]}`; export const isStandardAxiom = (name: string): boolean => STANDARD_SET.has(name); export const isCompilerAxiom = (name: string): boolean => COMPILER_SET.has(name); export const isNativeAxiom = (name: string): boolean => COMPILER_SET.has(name) || NATIVE_AXIOM_RE.test(name); export const isSyntheticNativeAx = (name: string): boolean => NATIVE_AXIOM_RE.test(name); export const sortUnique = (items: Iterable): string[] => [...new Set(items)].sort(byText); export const must = (map: ReadonlyMap, key: K): V => { const value = map.get(key); if (value === undefined) throw new Error(`Missing key ${String(key)}.`); return value; }; export const append = (map: Map, key: K, value: V): void => { const listValue = map.get(key); if (listValue) listValue.push(value); else map.set(key, [value]); }; export const edgeKey = (edge: { from: string; to: string }): string => `${edge.from}\0${edge.to}`; export const shortName = (name: string): string => { const parts = name.split("."); return parts[parts.length - 1] ?? name; }; export const firstSentence = (doc: string): string => { const clean = doc.replace(/\s+/g, " ").trim(); const match = clean.match(/^(.+?[.!?])(?:\s|$)/); return match ? match[1] : clean; }; export const clipLine = (text: string, width: number = LINE_WIDTH): string => { const clean = text.replace(/\s+/g, " ").trim(); const points = Array.from(clean); if (points.length <= width) return clean; const maxContent = Math.max(1, width - 1); if (points[maxContent] === " ") { return `${points .slice(0, maxContent) .join("") .replace(/[,:;([-]+$/, "") .trimEnd()}…`; } const slice = points.slice(0, maxContent); const lastSpace = slice.lastIndexOf(" "); if (lastSpace > 0) { return `${slice .slice(0, lastSpace) .join("") .replace(/[,:;([-]+$/, "") .trimEnd()}…`; } return `${slice.join("")}…`; }; export const summarizeDoc = (doc: string, width: number = LINE_WIDTH): string => { const clean = firstSentence(doc).replaceAll("`", "").replace(/\s+/g, " ").trim(); const points = Array.from(clean); if (points.length <= width) return clean; const colonAt = clean.indexOf(": "); if (colonAt >= 0) { const prefix = clean.slice(0, colonAt).trimEnd(); const prefixLen = Array.from(prefix).length; if (prefixLen >= 12 && prefixLen <= width) return prefix; } return clipLine(clean, width); }; export const stripCasePrefix = (input: { rawTag: string; ancestorTags: readonly string[]; }): string => { let best = input.rawTag; let longest = -1; for (const anc of input.ancestorTags) { if (!anc) continue; const prefix = `${anc}.`; if (input.rawTag.startsWith(prefix) && prefix.length > longest) { longest = prefix.length; best = input.rawTag.slice(prefix.length); } } return best; }; export const humanCaseTag = (tag: string): string => tag .split(".") .map((segment) => STANDARD_CASE_TAGS[segment] ?? segment) .join(" / "); export const formatPlace = (at: Place | undefined): string => (at ? `${at.line}:${at.column}` : ""); export const placeLeq = (a: Place, b: Place): boolean => a.line < b.line || (a.line === b.line && a.column <= b.column); export const spanContainsPlace = (span: Span, at: Place): boolean => placeLeq({ line: span.line, column: span.column }, at) && placeLeq(at, { line: span.endLine, column: span.endColumn }); export const spanContainsSpan = (outer: Span, inner: Span): boolean => placeLeq( { line: outer.line, column: outer.column }, { line: inner.line, column: inner.column }, ) && placeLeq( { line: inner.endLine, column: inner.endColumn }, { line: outer.endLine, column: outer.endColumn }, ); export const spanStrictlyInside = (outer: Span, inner: Span): boolean => spanContainsSpan(outer, inner) && (outer.line !== inner.line || outer.column !== inner.column || outer.endLine !== inner.endLine || outer.endColumn !== inner.endColumn); export const spanSize = (span: Span): number => (span.endLine - span.line) * 10000 + (span.endColumn - span.column); export const hasSubtreeMark = ; into: readonly T[] }>( block: T, ): boolean => { if (block.marks.size > 0) return true; for (const child of block.into) { if (hasSubtreeMark(child)) return true; } return false; }; export const trustStatusOf = (input: { own: readonly Doubt[]; below: readonly Doubt[]; }): TrustStatus => { if (input.own.some((d) => d.kind === "error")) return "error"; if (input.own.some((d) => d.kind === "sorry")) return "sorry"; if (input.own.some((d) => d.kind === "native")) return "native"; if (input.own.some((d) => d.kind === "axiom")) return "axiom"; if (input.below.length > 0) return "restsOn"; return "clean"; }; export const branchStatusOf = (marks: ReadonlySet): BranchStatus => { if (marks.has("error")) return "error"; if (marks.has("sorry")) return "sorry"; if (marks.has("open")) return "open"; if (marks.has("restsOn")) return "restsOn"; return "clean"; }; export const doubtKey = (d: Doubt): string => { switch (d.kind) { case "sorry": case "error": return `${d.kind}\0${d.in}\0${d.at ? `${d.at.line}:${d.at.column}` : ""}`; case "axiom": case "native": return `${d.kind}\0${d.name}`; default: { const _exhaustive: never = d; return _exhaustive; } } }; export const formatDoubt = (d: Doubt): string => { switch (d.kind) { case "sorry": return d.at ? `unproven gap (sorry) in ${d.in} (${d.at.line}:${d.at.column})` : `unproven gap (sorry) in ${d.in}`; case "error": return d.at ? `failing step in ${d.in} (${d.at.line}:${d.at.column})` : `failing step in ${d.in}`; case "native": return `unchecked native code (${d.name})`; case "axiom": return `custom axiom ${d.name}`; default: { const _exhaustive: never = d; return _exhaustive; } } };