import { type Block, type Decl, type Doubt, doubtKey, type DoubtKind, type Event, type Facts, type Goal, type GoalId, hasSubtreeMark, isNativeAxiom, isStandardAxiom, type Outside, type ProofTree, type RootBlock, type Script, spanContainsPlace, spanSize, type Step, type StepId, type Trust, } from "../ir.ts"; function innermostStepAt( steps: readonly Step[], at: { line: number; column: number }, ): StepId | undefined { let best: Step | undefined; let bestSize = Number.MAX_SAFE_INTEGER; for (const s of steps) { if (!spanContainsPlace(s.span, at)) continue; const sz = spanSize(s.span); if (sz < bestSize) { best = s; bestSize = sz; } } return best?.id; } function collectEvents(events: readonly Event[], out: Event[]): void { for (const ev of events) { out.push(ev); collectEvents(ev.children, out); } } type TaintEntry = { kinds: Set; keys: Set; }; function addTaintEntry( map: Map, name: string, kinds: Iterable, keys: Iterable, ): void { let entry = map.get(name); if (!entry) { entry = { kinds: new Set(), keys: new Set() }; map.set(name, entry); } for (const k of kinds) entry.kinds.add(k); for (const key of keys) entry.keys.add(key); } function buildAllTaint( facts: Facts, trust: Trust, targetDoubtKinds: ReadonlySet, targetDoubtKeys: ReadonlySet, ): Map { const declByName = new Map(); for (const d of facts.decls) declByName.set(d.name, d); const outsideByName = new Map(); for (const o of facts.outside) outsideByName.set(o.name, o); const targetUserName = declByName.get(facts.target)?.userName ?? facts.target; const taint = new Map(); for (const [name, doubts] of trust.below) { if (doubts.length === 0) continue; addTaintEntry( taint, name, doubts.map((d) => d.kind), doubts.map((d) => doubtKey(d)), ); } for (const o of facts.outside) { const kinds = new Set(); const keys = new Set(); for (const ax of o.axioms) { if (ax === "sorryAx") { kinds.add("sorry"); if (o.name !== "sorryAx") { keys.add(doubtKey({ kind: "sorry", in: `${o.name} (${o.home})`, at: undefined })); } } else if (isStandardAxiom(ax)) continue; else if (isNativeAxiom(ax)) { kinds.add("native"); keys.add(doubtKey({ kind: "native", name: ax })); } else { kinds.add("axiom"); keys.add(doubtKey({ kind: "axiom", name: ax })); } } if (kinds.size > 0) addTaintEntry(taint, o.name, kinds, keys); } const directDeclTaint = (d: Decl): { kinds: Set; keys: Set } => { const kinds = new Set(); const keys = new Set(); const ownerFallback = d.visible || d.isUserDef ? d.userName : targetUserName; for (const s of d.sorries) { const kind: "error" | "sorry" = s.synthetic ? "error" : "sorry"; kinds.add(kind); keys.add(doubtKey({ kind, in: s.in ?? ownerFallback, at: s.at })); } if (d.kind === "axiom") { if (d.name === "sorryAx") { kinds.add("sorry"); } else if (!isStandardAxiom(d.name)) { const kind: "native" | "axiom" = isNativeAxiom(d.name) ? "native" : "axiom"; kinds.add(kind); keys.add(doubtKey({ kind, name: d.name })); } } return { kinds, keys }; }; for (const d of facts.decls) { if (d.name === facts.target) continue; const { kinds, keys } = directDeclTaint(d); const seen = new Set([d.name]); const queue: { ref: string; fromHasSorries: boolean }[] = d.uses.map((ref) => ({ ref, fromHasSorries: d.sorries.length > 0, })); while (queue.length > 0) { const item = queue.shift()!; const ref = item.ref; if (ref === "sorryAx" && item.fromHasSorries) continue; if (seen.has(ref) || ref === facts.target) continue; seen.add(ref); const ext = outsideByName.get(ref); if (ext) { const extTaint = taint.get(ext.name); if (extTaint) { for (const k of extTaint.kinds) kinds.add(k); for (const key of extTaint.keys) keys.add(key); } continue; } const sub = declByName.get(ref); if (sub) { const subTaint = directDeclTaint(sub); for (const k of subTaint.kinds) kinds.add(k); for (const key of subTaint.keys) keys.add(key); const subHasSorries = sub.sorries.length > 0; for (const next of sub.uses) { if (!seen.has(next)) queue.push({ ref: next, fromHasSorries: subHasSorries }); } } } if (kinds.size > 0) addTaintEntry(taint, d.name, kinds, keys); } if (targetDoubtKinds.size > 0) { const targetDecl = declByName.get(facts.target); if (targetDecl) { for (const member of targetDecl.group) { if (member !== facts.target && declByName.has(member)) continue; addTaintEntry(taint, member, targetDoubtKinds, targetDoubtKeys); } } } return taint; } function findMatchingErrorStep(input: { goalId: GoalId; allEvents: readonly Event[]; stepById: ReadonlyMap; facts: Facts; }): string | undefined { const errorMsgs = input.facts.messages.filter( (m) => m.severity === "error" && spanContainsPlace(input.facts.targetSpan, m.at), ); let best: { text: string; size: number } | undefined; for (const ev of input.allEvents) { if (ev.assigned.length > 0) continue; if (!ev.before.includes(input.goalId)) continue; const step = input.stepById.get(ev.step); if (!step) continue; const matchesError = errorMsgs.some((msg) => spanContainsPlace(step.span, msg.at)); if (!matchesError) continue; const sz = spanSize(step.span); if (!best || sz <= best.size) { best = { text: step.text, size: sz }; } } return best?.text; } function findFailingStep(input: { goalId: GoalId; allEvents: readonly Event[]; stepById: ReadonlyMap; facts: Facts; }): string | undefined { const matched = findMatchingErrorStep(input); if (matched !== undefined) return matched; const errorMsgs = input.facts.messages.filter( (m) => m.severity === "error" && spanContainsPlace(input.facts.targetSpan, m.at), ); let best: { text: string; size: number } | undefined; for (const ev of input.allEvents) { if (ev.assigned.length > 0) continue; if (ev.before[0] !== input.goalId) continue; const step = input.stepById.get(ev.step); if (!step) continue; const sz = spanSize(step.span); if (!best || sz <= best.size) { best = { text: step.text, size: sz }; } } if (best) return best.text; if (errorMsgs.length > 0) { const firstLine = errorMsgs[0].text.split(/\r?\n/)[0]?.trim(); if (firstLine) return firstLine; } return undefined; } export function analyzeProofMarks(input: { roots: RootBlock[]; script: Script; tree: ProofTree; facts: Facts; trust: Trust; }): { roots: RootBlock[]; unattributedDoubt: boolean } { const { roots, script, tree, facts, trust } = input; 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 targetErrorMsgs = facts.messages.filter( (m) => m.severity === "error" && spanContainsPlace(facts.targetSpan, m.at), ); const targetDoubts = trust.below.get(facts.target) ?? []; const targetOwn = trust.own.get(facts.target) ?? []; const targetDoubtKinds = new Set(); const targetDoubtKeys = new Set(); const requiredDoubts: Doubt[] = []; for (const d of targetDoubts) { targetDoubtKinds.add(d.kind); targetDoubtKeys.add(doubtKey(d)); requiredDoubts.push(d); } for (const p of trust.paths) { targetDoubtKinds.add(p.doubt.kind); targetDoubtKeys.add(doubtKey(p.doubt)); requiredDoubts.push(p.doubt); } if (targetErrorMsgs.length > 0) targetDoubtKinds.add("error"); const allTaint = buildAllTaint(facts, trust, targetDoubtKinds, targetDoubtKeys); const allEvents: Event[] = []; collectEvents(script.events, allEvents); const sorrySteps = new Set(); const errorSteps = new Set(); const stepOwnDoubtKeys = new Map>(); const recordStepOwnKey = (sid: StepId, key: string): void => { const prev = stepOwnDoubtKeys.get(sid); if (prev) prev.add(key); else stepOwnDoubtKeys.set(sid, new Set([key])); }; const targetDecl = facts.decls.find((d) => d.name === facts.target); if (targetDecl) { for (const s of targetDecl.sorries) { if (!s.at) continue; const sid = innermostStepAt(script.steps, s.at); if (sid === undefined) continue; if (s.synthetic) errorSteps.add(sid); else sorrySteps.add(sid); recordStepOwnKey( sid, doubtKey({ kind: s.synthetic ? "error" : "sorry", in: s.in ?? targetDecl.userName, at: s.at, }), ); } } for (const d of targetOwn) { if ((d.kind === "sorry" || d.kind === "error") && d.at) { const sid = innermostStepAt(script.steps, d.at); if (sid !== undefined) recordStepOwnKey(sid, doubtKey(d)); } } for (const msg of targetErrorMsgs) { const sid = innermostStepAt(script.steps, msg.at); if (sid !== undefined) errorSteps.add(sid); } const directCoveredKeys = new Set(); const visit = (block: Block): Block => { const marks = new Set(block.marks); const restsOnKinds = new Set(block.restsOnKinds); let errorLabel = block.errorLabel; const visitedInto = block.into.map(visit); const childHasError = visitedInto.some((c) => c.marks.has("error")); for (const sid of block.steps) { if (sorrySteps.has(sid)) marks.add("sorry"); if (errorSteps.has(sid) && !childHasError) { marks.add("error"); if (errorLabel === undefined) { const step = stepById.get(sid); if (step) errorLabel = step.text; } } const ownKeys = stepOwnDoubtKeys.get(sid); if (ownKeys) for (const k of ownKeys) directCoveredKeys.add(k); } const lastGoal = goalById.get(block.last); if (block.into.length === 0 && lastGoal) { const closedByStep = tree.closes.has(block.last); if (!closedByStep) { const matchedError = findMatchingErrorStep({ goalId: lastGoal.id, allEvents, stepById, facts, }); if (matchedError !== undefined) { marks.add("error"); errorLabel ??= matchedError; } else if (lastGoal.closedBy === "error") { marks.add("error"); errorLabel ??= findFailingStep({ goalId: lastGoal.id, allEvents, stepById, facts, }); } else if (lastGoal.final === "open") { marks.add("open"); } else if (lastGoal.closedBy === "sorry") { marks.add("sorry"); } } else { if (lastGoal.closedBy === "sorry") marks.add("sorry"); else if (lastGoal.closedBy === "error") marks.add("error"); } } const hasOwnSorryOrError = marks.has("sorry") || marks.has("error"); for (const sid of block.steps) { const step = stepById.get(sid); if (!step) continue; for (const name of step.names) { if (name === "sorryAx" && hasOwnSorryOrError) continue; const entry = allTaint.get(name); if (entry && entry.kinds.size > 0) { marks.add("restsOn"); for (const k of entry.kinds) restsOnKinds.add(k); for (const key of entry.keys) directCoveredKeys.add(key); } } } return { ...block, errorLabel, marks, restsOnKinds, into: visitedInto, }; }; const directMarked = roots.map(({ root, block }) => ({ root, block: visit(block) })); const targetHasDoubt = trust.paths.length > 0 || targetDoubts.length > 0; const targetHasError = targetErrorMsgs.length > 0; const unattributedErrors = targetErrorMsgs.filter( (m) => innermostStepAt(script.steps, m.at) === undefined, ); const anyDirectMark = directMarked.some((r) => hasSubtreeMark(r.block)); const missingDoubt = requiredDoubts.some((d) => !directCoveredKeys.has(doubtKey(d))); const unattributedDoubt = unattributedErrors.length > 0 || ((targetHasDoubt || targetHasError) && !anyDirectMark) || ((targetHasDoubt || targetHasError) && script.partial === true) || missingDoubt; if ((targetHasDoubt || targetHasError) && !anyDirectMark) { const firstError = unattributedErrors[0]?.text.split(/\r?\n/)[0]?.trim() ?? targetErrorMsgs[0]?.text.split(/\r?\n/)[0]?.trim(); const outRoots = directMarked.map(({ root, block }) => { const marks = new Set(block.marks); const restsOnKinds = new Set(block.restsOnKinds); let errorLabel = block.errorLabel; if (targetHasError || targetOwn.some((d) => d.kind === "error")) { marks.add("error"); errorLabel ??= firstError; } else if (targetOwn.some((d) => d.kind === "sorry")) { marks.add("sorry"); } else { marks.add("restsOn"); for (const d of targetDoubts) restsOnKinds.add(d.kind); if (restsOnKinds.size === 0) { for (const p of trust.paths) restsOnKinds.add(p.doubt.kind); } } return { root, block: { ...block, marks, restsOnKinds, errorLabel, }, }; }); return { roots: outRoots, unattributedDoubt }; } return { roots: directMarked, unattributedDoubt }; } export function markBlocks(input: { roots: RootBlock[]; script: Script; tree: ProofTree; facts: Facts; trust: Trust; }): RootBlock[] { return analyzeProofMarks(input).roots; }