import { DECL_KINDS, type Decl, type DeclKind, type Event, type Facts, FORMAT_VERSION, type Goal, type GoalClosedBy, type GoalFinal, type Hyp, type Message, type Outside, type Place, type Root, type Script, type SorryUse, type Span, type Step, } from "../ir.ts"; const KIND_SET: ReadonlySet = new Set(DECL_KINDS); type RawRecord = { [key: string]: unknown }; const fail = (where: string, expected: string): never => { throw new Error(`Invalid facts JSON at ${where}: expected ${expected}.`); }; const isObj = (v: unknown): v is RawRecord => typeof v === "object" && v !== null && !Array.isArray(v); const obj = (v: unknown, where: string): RawRecord => (isObj(v) ? v : fail(where, "an object")); const str = (v: unknown, where: string): string => typeof v === "string" ? v : fail(where, "a string"); const optStr = (v: unknown, where: string): string | undefined => v === null || v === undefined ? undefined : str(v, where); const bool = (v: unknown, where: string): boolean => typeof v === "boolean" ? v : fail(where, "a boolean"); const int = (v: unknown, where: string): number => typeof v === "number" && Number.isInteger(v) ? v : fail(where, "an integer"); const optInt = (v: unknown, where: string): number | undefined => v === null || v === undefined ? undefined : int(v, where); const arr = (v: unknown, where: string): unknown[] => Array.isArray(v) ? v : fail(where, "an array"); const strArr = (v: unknown, where: string): string[] => arr(v, where).map((item, i) => str(item, `${where}[${i}]`)); const intArr = (v: unknown, where: string): number[] => arr(v, where).map((item, i) => int(item, `${where}[${i}]`)); const declKind = (v: unknown, where: string): DeclKind => { const s = str(v, where); if (!KIND_SET.has(s)) fail(where, `one of ${DECL_KINDS.join(", ")}`); switch (s) { case "theorem": case "axiom": case "def": case "opaque": case "inductive": case "constructor": case "recursor": case "quot": return s; default: return fail(where, `one of ${DECL_KINDS.join(", ")}`); } }; const place = (v: unknown, where: string): Place => { const o = obj(v, where); return { line: int(o.line, `${where}.line`), column: int(o.column, `${where}.column`), }; }; const optPlace = (v: unknown, where: string): Place | undefined => v === null || v === undefined ? undefined : place(v, where); const span = (v: unknown, where: string): Span => { const o = obj(v, where); return { line: int(o.line, `${where}.line`), column: int(o.column, `${where}.column`), endLine: int(o.endLine, `${where}.endLine`), endColumn: int(o.endColumn, `${where}.endColumn`), }; }; const sorryUse = (v: unknown, where: string): SorryUse => { const o = obj(v, where); const owner = optStr(o.in, `${where}.in`); return { synthetic: bool(o.synthetic, `${where}.synthetic`), at: optPlace(o.at, `${where}.at`), ...(owner !== undefined ? { in: owner } : {}), }; }; const decl = (v: unknown, where: string): Decl => { const o = obj(v, where); return { name: str(o.name, `${where}.name`), userName: str(o.userName, `${where}.userName`), kind: declKind(o.kind, `${where}.kind`), visible: bool(o.visible, `${where}.visible`), isUserDef: bool(o.isUserDef, `${where}.isUserDef`), module: str(o.module, `${where}.module`), line: optInt(o.line, `${where}.line`), doc: optStr(o.doc, `${where}.doc`), statement: str(o.statement, `${where}.statement`), group: strArr(o.group, `${where}.group`), uses: strArr(o.uses, `${where}.uses`), sorries: arr(o.sorries, `${where}.sorries`).map((item, i) => sorryUse(item, `${where}.sorries[${i}]`), ), }; }; const outside = (v: unknown, where: string): Outside => { const o = obj(v, where); return { name: str(o.name, `${where}.name`), kind: declKind(o.kind, `${where}.kind`), home: str(o.home, `${where}.home`), axioms: strArr(o.axioms, `${where}.axioms`), }; }; const hyp = (v: unknown, where: string): Hyp => { const o = obj(v, where); return { name: str(o.name, `${where}.name`), type: str(o.type, `${where}.type`), value: optStr(o.value, `${where}.value`), }; }; const goalFinal = (v: unknown, where: string): GoalFinal => { const s = str(v, where); if (s === "assigned" || s === "open" || s === "gone") return s; return fail(where, "assigned, open, or gone"); }; const goalClosedBy = (v: unknown, where: string): GoalClosedBy => { if (v === null || v === undefined) return undefined; const s = str(v, where); if (s === "proof" || s === "sorry" || s === "error") return s; return fail(where, "proof, sorry, error, or null"); }; const goal = (v: unknown, where: string): Goal => { const o = obj(v, where); return { id: int(o.id, `${where}.id`), tag: optStr(o.tag, `${where}.tag`), hyps: intArr(o.hyps, `${where}.hyps`), target: str(o.target, `${where}.target`), final: goalFinal(o.final, `${where}.final`), closedBy: goalClosedBy(o.closedBy, `${where}.closedBy`), }; }; const step = (v: unknown, where: string): Step => { const o = obj(v, where); return { id: int(o.id, `${where}.id`), kind: str(o.kind, `${where}.kind`), text: str(o.text, `${where}.text`), span: span(o.span, `${where}.span`), names: strArr(o.names, `${where}.names`), }; }; const reachEntry = (v: unknown, where: string): [number, number[]] => { const pair = arr(v, where); if (pair.length !== 2) fail(where, "a [goalId, goalId[]] pair"); return [int(pair[0], `${where}[0]`), intArr(pair[1], `${where}[1]`)]; }; const event = (v: unknown, where: string): Event => { const o = obj(v, where); return { step: int(o.step, `${where}.step`), before: intArr(o.before, `${where}.before`), after: intArr(o.after, `${where}.after`), assigned: intArr(o.assigned, `${where}.assigned`), declared: intArr(o.declared, `${where}.declared`), reach: arr(o.reach, `${where}.reach`).map((item, i) => reachEntry(item, `${where}.reach[${i}]`), ), children: arr(o.children, `${where}.children`).map((item, i) => event(item, `${where}.children[${i}]`), ), }; }; const root = (v: unknown, where: string): Root => { const o = obj(v, where); const w = str(o.where, `${where}.where`); if (w === "body" || w === "term" || w === "termination") { return { goal: int(o.goal, `${where}.goal`), where: w, }; } return fail(`${where}.where`, "body, term, or termination"); }; const script = (v: unknown, where: string): Script => { const o = obj(v, where); const partial = o.partial === undefined || o.partial === null ? false : bool(o.partial, `${where}.partial`); return { bodyText: str(o.bodyText, `${where}.bodyText`), roots: arr(o.roots, `${where}.roots`).map((item, i) => root(item, `${where}.roots[${i}]`)), goals: arr(o.goals, `${where}.goals`).map((item, i) => goal(item, `${where}.goals[${i}]`)), hyps: arr(o.hyps, `${where}.hyps`).map((item, i) => hyp(item, `${where}.hyps[${i}]`)), steps: arr(o.steps, `${where}.steps`).map((item, i) => step(item, `${where}.steps[${i}]`)), events: arr(o.events, `${where}.events`).map((item, i) => event(item, `${where}.events[${i}]`)), partial, }; }; const message = (v: unknown, where: string): Message => { const o = obj(v, where); const sev = str(o.severity, `${where}.severity`); if (sev === "error" || sev === "warning") { return { severity: sev, at: place(o.at, `${where}.at`), text: str(o.text, `${where}.text`), }; } return fail(`${where}.severity`, "error or warning"); }; export function readFacts(raw: unknown): Facts { const o = obj(raw, "root"); if (o.format !== FORMAT_VERSION) fail("format", `${FORMAT_VERSION}`); return { format: FORMAT_VERSION, lean: str(o.lean, "lean"), file: str(o.file, "file"), module: str(o.module, "module"), package: optStr(o.package, "package"), target: str(o.target, "target"), targetSpan: span(o.targetSpan, "targetSpan"), axioms: strArr(o.axioms, "axioms"), decls: arr(o.decls, "decls").map((item, i) => decl(item, `decls[${i}]`)), outside: arr(o.outside, "outside").map((item, i) => outside(item, `outside[${i}]`)), script: o.script === null || o.script === undefined ? undefined : script(o.script, "script"), messages: arr(o.messages, "messages").map((item, i) => message(item, `messages[${i}]`)), }; } export function parseFacts(text: string): Facts { let parsed: unknown; try { parsed = JSON.parse(text); } catch (error) { throw new Error( `Cannot parse facts JSON: ${error instanceof Error ? error.message : String(error)}`, { cause: error }, ); } return readFacts(parsed); }