import { type Block, type Drawn, type FoldedEntry, type Goal, type GoalId, hasSubtreeMark, type Hyp, type RootBlock, type RootDrawn, type Script, type Step, type StepId, } from "../ir.ts"; const hypSig = (h: Hyp): string => (h.value !== undefined ? `${h.type} := ${h.value}` : h.type); const isTypeBinder = (h: Hyp): boolean => h.type === "Type" || h.type.startsWith("Type ") || h.type === "Sort" || h.type.startsWith("Sort "); function branchGoalSig(input: { entryGoal: Goal | undefined; lastGoal: Goal | undefined; parentLastGoal: Goal | undefined; hyps: readonly Hyp[]; }): string { const { entryGoal, lastGoal, parentLastGoal, hyps } = input; if (!entryGoal) return ""; const added: string[] = []; if (parentLastGoal) { const parentMap = new Map(); for (const hid of parentLastGoal.hyps) { const h = hyps[hid]; if (h) parentMap.set(h.name, hypSig(h)); } for (const hid of entryGoal.hyps) { const h = hyps[hid]; if (!h || isTypeBinder(h)) continue; const sig = hypSig(h); if (parentMap.get(h.name) !== sig) added.push(`e:${h.name}:${sig}`); } } if (lastGoal && lastGoal.id !== entryGoal.id) { const entryMap = new Map(); for (const hid of entryGoal.hyps) { const h = hyps[hid]; if (h) entryMap.set(h.name, hypSig(h)); } for (const hid of lastGoal.hyps) { const h = hyps[hid]; if (!h || isTypeBinder(h)) continue; const sig = hypSig(h); if (entryMap.get(h.name) !== sig) added.push(`m:${h.name}:${sig}`); } } return `${entryGoal.target}\0${added.join("\n")}`; } export function foldSiblings(input: { roots: RootBlock[]; script: Script }): RootDrawn[] { const { roots, script } = 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 stepSignature = (block: Drawn): string => block.steps .map((sid) => { const text = stepById.get(sid)?.text ?? `#${sid}`; const inl = block.inlined.get(sid); return inl && inl.length > 0 ? `${text}[${inl.join(";")}]` : text; }) .join("\n"); const subtreeSignature = (block: Drawn): string => { const childSigs = block.into.map((c) => `${c.times}:${subtreeSignature(c)}`).join(","); return `${stepSignature(block)}|${block.errorLabel ?? ""}|(${childSigs})`; }; const subtreeGoalSig = (block: Drawn, parentLastGoal: Goal | undefined): string => { const own = branchGoalSig({ entryGoal: goalById.get(block.entry), lastGoal: goalById.get(block.last), parentLastGoal, hyps: script.hyps, }); const ownLast = goalById.get(block.last) ?? goalById.get(block.entry); const childSigs = block.into.map((c) => subtreeGoalSig(c, ownLast)).join(","); return `${own}|(${childSigs})`; }; const hasErrorInSubtree = (block: Drawn): boolean => block.errorLabel !== undefined || block.into.some(hasErrorInSubtree); const mergeChildSubtree = ( target: Drawn, extra: Drawn, targetParentLast: Goal | undefined, extraParentLast: Goal | undefined, ): void => { const targetSig = branchGoalSig({ entryGoal: goalById.get(target.entry), lastGoal: goalById.get(target.last), parentLastGoal: targetParentLast, hyps: script.hyps, }); const extraSig = branchGoalSig({ entryGoal: goalById.get(extra.entry), lastGoal: goalById.get(extra.last), parentLastGoal: extraParentLast, hyps: script.hyps, }); if (extra.differentGoals || targetSig !== extraSig) target.differentGoals = true; for (const tag of extra.tags) { if (!target.tags.includes(tag)) target.tags.push(tag); } for (const entry of extra.foldedEntries) target.foldedEntries.push(entry); const nextTargetLast = goalById.get(target.last) ?? goalById.get(target.entry); const nextExtraLast = goalById.get(extra.last) ?? goalById.get(extra.entry); for (let i = 0; i < target.into.length && i < extra.into.length; i++) { mergeChildSubtree(target.into[i], extra.into[i], nextTargetLast, nextExtraLast); } }; const mergeInto = (target: Drawn, extra: Drawn, differentGoals: boolean): void => { target.times += extra.times; if (differentGoals || extra.differentGoals) target.differentGoals = true; for (const tag of extra.tags) { if (!target.tags.includes(tag)) target.tags.push(tag); } for (const entry of extra.foldedEntries) target.foldedEntries.push(entry); const nextTargetLast = goalById.get(target.last) ?? goalById.get(target.entry); const nextExtraLast = goalById.get(extra.last) ?? goalById.get(extra.entry); for (let i = 0; i < target.into.length && i < extra.into.length; i++) { mergeChildSubtree(target.into[i], extra.into[i], nextTargetLast, nextExtraLast); } }; const convert = (block: Block): Drawn => { const parentLastGoal = goalById.get(block.last) ?? goalById.get(block.entry); const rawChildren = block.into.map(convert); type ChildItem = { drawn: Drawn; foldable: boolean; tacticSig: string; goalSig: string; }; const items: ChildItem[] = rawChildren.map((drawn) => { const foldable = !hasSubtreeMark(drawn) && !hasErrorInSubtree(drawn); return { drawn, foldable, tacticSig: foldable ? subtreeSignature(drawn) : "", goalSig: foldable ? subtreeGoalSig(drawn, parentLastGoal) : "", }; }); const exactMerged: ChildItem[] = []; const byExact = new Map(); for (const item of items) { if (!item.foldable) { exactMerged.push(item); continue; } const key = `${item.tacticSig}\u0001${item.goalSig}`; const existing = byExact.get(key); if (existing) { mergeInto(existing.drawn, item.drawn, false); } else { byExact.set(key, item); exactMerged.push(item); } } const tacticGroupCounts = new Map(); for (const item of exactMerged) { if (!item.foldable) continue; tacticGroupCounts.set( item.tacticSig, (tacticGroupCounts.get(item.tacticSig) ?? 0) + item.drawn.times, ); } const merged: Drawn[] = []; const byTactic = new Map(); for (const item of exactMerged) { if (!item.foldable || (tacticGroupCounts.get(item.tacticSig) ?? 0) < 3) { merged.push(item.drawn); continue; } const existing = byTactic.get(item.tacticSig); if (existing) { mergeInto(existing, item.drawn, true); } else { byTactic.set(item.tacticSig, item.drawn); merged.push(item.drawn); } } const tag = goalById.get(block.entry)?.tag; const ownEntry: FoldedEntry = { entry: block.entry, last: block.last, tag, steps: [...block.steps], inlined: new Map(block.inlined), errorLabel: block.errorLabel, }; return { ...block, into: merged, times: 1, tags: tag ? [tag] : [], differentGoals: false, foldedEntries: [ownEntry], hidden: undefined, }; }; return roots.map(({ root, block }) => ({ root, block: convert(block) })); }