diff --git a/src/engines/pseudo/vcgen.ts b/src/engines/pseudo/vcgen.ts new file mode 100644 index 0000000..21b8c29 --- /dev/null +++ b/src/engines/pseudo/vcgen.ts @@ -0,0 +1,1081 @@ +/** + * pseudo/vcgen.js + * + * Proof obligations: every place where the program depends on something + * being true, stated as a fact to prove from what is known at that point. + * + * verify(program, options) -> { + * units: [{ name, kind, line, obligations, inferred, notes, partial }], + * counts: { proved, unknown, total }, + * } + * + * An obligation group is one place in the source (a line and a kind), with + * one instance per path that reaches it: + * { kind, line, col, label, goal, status, instances, shown } + * + * Kinds: 'index', 'divide', 'key', 'slice', 'argument', 'require', 'ensure', + * 'invariant-init', 'invariant-keep', 'decreases-bound', 'decreases-step', + * 'terminates', 'assert'. + * + * The method is symbolic execution. Each path keeps what it knows as a list + * of facts about symbols. A loop is cut at its invariant: at the top of the + * loop every variable the body changes becomes a fresh symbol that the + * invariants describe. Invariants come from the student (INVARIANT lines) + * and from the checker itself, which tries a few simple shapes + * (0 <= I, Total <= I, LENGTH(Hits) >= 0, ...) and keeps the ones it can + * prove hold on entry and survive every pass. + */ + +import { + sym, bv, num, lit, TRUE, FALSE, and, or, not, cmp, ite, app, at, at2, length, member, + arith, add, sub, neg, mkq, key, substSyms, show, symsOf, sortOf, numeric, +} from './logic.js'; +import type { Term, Sort, TypeLike, CmpOp } from './logic.js'; +import { prove } from './solver.js'; +import { sourceOf } from './parse.js'; +import { walkStatements, rootName, statementExprs, walkExpr } from './analyze.js'; + +/* ------------------------------------------------------------------ */ +/* Types */ +/* ------------------------------------------------------------------ */ + +/** + * A node of the parsed program, as parse.js and check.js build it: a `kind` + * ('For', 'Binary', 'CallExpr', ...), a position, and the fields of that kind. + * The parser is still JavaScript, so the fields are not typed here. + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type PNode = { kind: string; line?: number; col?: number; [field: string]: any }; +/** A subroutine as check.js records it (params, requires, ensures, decreases, node). */ +export type Routine = PNode; +/** The checked program: subs, main, constants, defines. */ +export type Program = PNode; + +/** The verifier's unit of work: one subroutine, or the main program. */ +export interface Unit { + name: string; + kind: string; + node: PNode | null; + body: PNode[]; + params: PNode[]; + symbols: PNode[]; + requires: PNode[]; + ensures: PNode[]; + sub?: Routine; +} + +export interface VerifyOptions { budgetMs?: number; maxPaths?: number; infer?: boolean; debug?: boolean } + +/** One obligation group as the report gives it: one place in the source, one kind. */ +export interface Obligation { + kind: string; + line: number | null; + col: number; + label: string; + status: 'proved' | 'unknown'; + paths: number; + target?: number; + measure?: string; + needsMeasure?: boolean; + recursive?: 'self' | 'mutual'; + shown: { facts: string[]; goal: string; trail: string[]; reason?: string; witness?: string[] } | null; +} +export interface Note { line: number; text: string; kind?: string } +export interface UnitReport { + name: string; + kind: string; + line: number; + obligations: Obligation[]; + inferred: Array<{ line: number; facts: string[] }>; + notes: Note[]; + partial: boolean; +} +export interface Report { units: UnitReport[]; counts: { proved: number; unknown: number; total: number } } + +/** A value in the symbolic store: a term, or the mark of a variable declared but not yet set. */ +const UNSET = { k: 'unset' } as const; +type Val = Term | typeof UNSET; +type Env = Map; + +/** What an expression is translated in: inside a spec, under quantifiers, at a call. */ +interface Ctx { + spec?: boolean; + bound?: Map; + result?: Term | null; + paramEnv?: Env; + oldEnv?: Env; + breaks?: State[]; + continues?: State[]; +} +/** One obligation as recorded, before grouping and proving. */ +interface RawOb { + kind: string; line: number | null; col: number; label: string; hyps: Term[]; goal: Term | null; trail: string[]; + target?: number; measure?: string; needsMeasure?: boolean; recursive?: 'self' | 'mutual'; + status?: string; reason?: string; witness?: Term[]; +} +/** A candidate invariant: its text, and the formula it makes over a store. */ +interface Cand { text: string; at: (env: Env) => Term } +/** How a loop kind lets the inference build its head and take a pass. */ +interface InferHooks { + entryEnv: (env: Env) => Env; + makeHead: (extra: (h: State) => Term[]) => { H: State; i?: Term }; + guardOf: ((i: Term) => Term) | null; + whileGuard?: (H: State) => Term; + advance: (st: State, i: Term) => State; + counter?: string; + start?: Term; + step?: number; +} + +const up = (s: unknown): string => String(s).toUpperCase(); +/** A stored value as a term: an unset variable reads as a fresh unknown. */ +const isSet = (v: Val | undefined): v is Term => !!v && v !== UNSET; +const asTerm = (v: Val | undefined, name: string, sort: Sort): Term => (isSet(v) ? v : sym(name, sort)); +/** A store value the code knows is a term (a loop counter, a variable the loop head made fresh). */ +const valOf = (env: Env, k: string): Term => env.get(k) as Term; + +/** The subroutines a subroutine's body calls directly. */ +const calleeCache = new WeakMap>(); +function calleesOf(routine: Routine): Set { + const hit = calleeCache.get(routine); + if (hit) return hit; + const out = new Set(); + const body = routine.node?.body || []; + const look = (s: PNode): void => { + if (s.kind === 'Call' && s.sub) out.add(s.sub); + for (const e of statementExprs(s)) walkExpr(e, (x: PNode) => { if (x.kind === 'CallExpr' && x.sub && !x.isOld) out.add(x.sub); }); + }; + walkStatements(body, look); + for (const c of [...(routine.requires || []), ...(routine.ensures || [])]) look(c); + calleeCache.set(routine, out); + return out; +} + +/** Whether calling `from` can lead, through any chain of calls, to `to` (from === to counts). */ +function callsReach(program: Program, from: Routine, to: Routine): boolean { + const seen = new Set(); + const stack: Routine[] = [from]; + while (stack.length) { + const r = stack.pop()!; + if (r === to) return true; + if (seen.has(r)) continue; + seen.add(r); + for (const c of calleesOf(r)) stack.push(c); + } + return false; +} +let arrayIds = 0; + +/* ------------------------------------------------------------------ */ +/* State */ +/* ------------------------------------------------------------------ */ + +class State { + env: Env; + facts: Term[]; + trail: string[]; + constructor(env: Env = new Map(), facts: Term[] = [], trail: string[] = []) { this.env = env; this.facts = facts; this.trail = trail; } + clone(): State { return new State(new Map(this.env), this.facts.slice(), this.trail.slice()); } + assume(f: Term | null | undefined, why?: string): State { + const s = this.clone(); + if (f && !(f.k === 'lit' && f.v === true)) s.facts.push(f); + if (why) s.trail.push(why); + return s; + } +} + +/** Every variable a list of statements can change. */ +function modifiedIn(body: PNode[]): Set { + const out = new Set(); + walkStatements(body, (x: PNode) => { + if (x.kind === 'Assign' || x.kind === 'Input') { const r = rootName(x.target); if (r) out.add(up(r)); } + if (x.kind === 'BuiltinCall' && x.args[0]) { const r = rootName(x.args[0]); if (r) out.add(up(r)); } + if (x.kind === 'Call') x.args.forEach((a: PNode, i: number) => { if (x.sub?.params?.[i]?.mode === 'BYREF' && a.kind === 'Name') out.add(up(a.name)); }); + if (x.kind === 'For' || x.kind === 'ForEach') out.add(up(x.var)); + if (x.kind === 'Declare') for (const n of x.names) out.add(up(n.name)); + }); + return out; +} + +/* ------------------------------------------------------------------ */ +/* The executor */ +/* ------------------------------------------------------------------ */ + +class Exec { + program: Program; + unit: Unit; + opts: Required> & VerifyOptions; + recording: boolean; + raw: RawOb[]; + notes: Note[]; + inferred: Array<{ line: number; facts: string[] }>; + partial: boolean; + types: Map; + consts: Map; + defines: Map; + depth: number; + quiet = 0; + deadline = 0; + entryEnv: Env = new Map(); + intArith: unknown = false; + floatArith = false; + constructor(program: Program, unit: Unit, options: VerifyOptions) { + this.program = program; + this.unit = unit; + this.opts = { maxPaths: 48, infer: true, budgetMs: 4000, ...options }; + this.recording = true; + this.raw = []; + this.notes = []; + this.inferred = []; + this.partial = false; + this.types = new Map(); + for (const s of unit.symbols || []) this.types.set(up(s.name), s.type); + for (const p of unit.params || []) this.types.set(up(p.name), p.type); + this.consts = new Map(); + for (const c of program.constants.values()) this.consts.set(up(c.name), c); + this.defines = new Map([...program.defines.values()].map((d: PNode): [string, PNode] => [up(d.name), d])); + this.depth = 0; + } + + sortOfName(name: string): Sort { return sortOf(this.types.get(up(name))); } + + obligation(kind: string, node: PNode | null | undefined, label: string, st: State, guards: Term[], goal: Term | null, extra: Partial = {}): void { + if (!this.recording || this.quiet) return; + this.raw.push({ kind, line: node?.line ?? null, col: node?.col ?? 1, label, hyps: [...st.facts, ...guards], goal, trail: st.trail.slice(), ...extra }); + } + + fresh(name: string, sort: Sort): Term { return sym(name, sort); } + + /* ---------- expressions ---------- */ + tr(e: PNode, st: State, g: Term[] = [], ctx: Ctx = {}): Term { + switch (e.kind) { + case 'Num': return num(e.value, e.float ? 'float' : 'int'); + case 'Str': return lit(e.value, 'string'); + case 'Char': return lit(e.value, 'char'); + case 'Bool': return e.value ? TRUE : FALSE; + case 'Blank': case 'Error': return this.fresh('?', 'any'); + case 'Name': return this.name(e, st, ctx); + case 'Unary': { + const a = this.tr(e.arg, st, g, ctx); + return e.op === 'NOT' ? not(a) : neg(a); + } + case 'Binary': return this.binary(e, st, g, ctx); + case 'Chain': { + const parts: Term[] = []; + let prev: Term | null = this.tr(e.operands[0], st, g, ctx); + for (let i = 0; i < e.ops.length; i++) { + const nextNode = e.operands[i + 1]; + if (e.ops[i] === 'IN') { parts.push(this.membership(prev ?? this.fresh('?', 'any'), nextNode, st, [...g, ...parts], ctx)); prev = null; continue; } + const nx = this.tr(nextNode, st, [...g, ...parts], ctx); + parts.push(cmp(e.ops[i] as CmpOp, prev ?? this.fresh('?', 'any'), nx)); + prev = nx; + } + return and(parts); + } + case 'Index': return this.index(e, st, g, ctx); + case 'Field': { + const r = this.tr(e.target, st, g, ctx); + return fieldRead(r, e.field, sortOf(e.type)); + } + case 'CallExpr': return this.call(e, st, g, ctx); + case 'ListLit': return app('LIST', e.items.map((x: PNode) => this.tr(x, st, g, ctx)), 'list'); + case 'SetLit': return app('SET', e.items.map((x: PNode) => this.tr(x, st, g, ctx)), 'set'); + case 'MapLit': return e.entries.reduce((m: Term, x: PNode) => app('MAPPUT', [m, this.tr(x.key, st, g, ctx), this.tr(x.value, st, g, ctx)], 'map'), app('EMPTY', [], 'map')); + case 'Range': return app('RANGE', [this.tr(e.lo, st, g, ctx), this.tr(e.hi, st, g, ctx)], 'range'); + case 'SetBuilder': case 'Quant': return this.binder(e, st, g, ctx); + default: return this.fresh('?', 'any'); + } + } + + name(e: PNode, st: State, ctx: Ctx): Term { + const k = up(e.name); + if (ctx.bound && ctx.bound.has(k)) return bv(ctx.bound.get(k)!, sortOf(e.type)); + if (ctx.result && k === 'RESULT' && e.sym?.kind === 'result') return ctx.result; + if (ctx.paramEnv && ctx.paramEnv.has(k)) return asTerm(ctx.paramEnv.get(k), e.name, sortOf(e.type)); + if (st.env.has(k)) { + const v = st.env.get(k); + if (!isSet(v)) return this.fresh(e.name, sortOf(e.type)); + return v; + } + const c = this.consts.get(k); + if (c) return this.tr(c.node.value, new State(), [], {}); + const d = this.defines.get(k); + if (d) return app('SET', d.node.values.map((v: PNode) => this.tr(v, new State(), [], {})), 'set'); + return this.fresh(e.name, sortOf(e.type)); + } + + binary(e: PNode, st: State, g: Term[], ctx: Ctx): Term { + const op = e.op; + if (op === 'AND') { const l = this.tr(e.left, st, g, ctx); return and(l, this.tr(e.right, st, [...g, l], ctx)); } + if (op === 'OR') { const l = this.tr(e.left, st, g, ctx); return or(l, this.tr(e.right, st, [...g, not(l)], ctx)); } + if (op === 'IN') return this.membership(this.tr(e.left, st, g, ctx), e.right, st, g, ctx); + const l = this.tr(e.left, st, g, ctx); + const r = this.tr(e.right, st, g, ctx); + if (['/', 'DIV', 'MOD'].includes(op) && !ctx.spec) { + this.obligation('divide', e.right, `${sourceOf(e.right)} is not 0`, st, g, cmp('!=', r, num(0, r.t))); + } + if (['==', '!=', '<', '<=', '>', '>='].includes(op)) return cmp(op as CmpOp, l, r); + // FLOAT arithmetic rounds at every step, so 0.2 + 0.1 - 0.2 is not 0.1 + // and X + 1.0 can equal X. The checker knows nothing about a rounded + // result except that the same operation on the same values gives it + // again; comparisons of FLOAT values stay exact. + if (['+', '-', '*', '/', '^'].includes(op) && (op === '/' || sortOf(e.type) === 'float' || e.left.type?.k === 'FLOAT' || e.right.type?.k === 'FLOAT')) { + if (!ctx.spec) this.floatArith = true; + return app(`FLOAT${op}`, [l, r], 'float'); + } + if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int') this.intArith = e.line ?? e.left?.line ?? true; + return arith(op, l, r, sortOf(e.type)); + } + + membership(x: Term, rightNode: PNode, st: State, g: Term[], ctx: Ctx): Term { + if (rightNode.kind === 'Range') { + const lo = this.tr(rightNode.lo, st, g, ctx); const hi = this.tr(rightNode.hi, st, g, ctx); + return and(cmp('<=', lo, x), cmp('<', x, hi)); + } + return member(x, this.tr(rightNode, st, g, ctx)); + } + + index(e: PNode, st: State, g: Term[], ctx: Ctx): Term { + const c = this.tr(e.target, st, g, ctx); + const idx: Term[] = e.indices.map((i: PNode) => this.tr(i, st, g, ctx)); + const tk = e.target.type?.k; + const s = sortOf(e.type); + const quiet = ctx.spec || ctx.bound; + if (tk === 'HASHMAP') { + if (!quiet) this.obligation('key', e, `${sourceOf(e.indices[0])} is a key of ${sourceOf(e.target)}`, st, g, member(idx[0], c)); + return at(c, idx[0], s); + } + if (tk === 'ARRAY' && idx.length === 2) { + if (!quiet) { + this.obligation('index', e, `row ${sourceOf(e.indices[0])} and column ${sourceOf(e.indices[1])} of ${sourceOf(e.target)} exist`, st, g, + and(cmp('<=', num(0), idx[0]), cmp('<', idx[0], length(c)), cmp('<=', num(0), idx[1]), cmp('<', idx[1], columns(c)))); + } + return at2(c, idx[0], idx[1], s); + } + if (!quiet) { + this.obligation('index', e, `position ${sourceOf(e.indices[0])} of ${sourceOf(e.target)} exists`, st, g, + and(cmp('<=', num(0), idx[0]), cmp('<', idx[0], length(c)))); + } + return at(c, idx[0], s); + } + + binder(e: PNode, st: State, g: Term[], ctx: Ctx): Term { + const bound = new Map(ctx.bound || []); + const name = `${e.var}`; + bound.set(up(e.var), name); + const inner: Ctx = { ...ctx, bound, spec: true }; + const q = e.kind === 'SetBuilder' ? 'SETOF' : e.q; + const t: Sort = e.kind === 'SetBuilder' ? 'set' : e.q === 'SUM' ? sortOf(e.type) : 'bool'; + if (e.domain?.kind === 'Range') { + const lo = this.tr(e.domain.lo, st, g, ctx); const hi = this.tr(e.domain.hi, st, g, ctx); + const body = this.tr(e.body, st, [], inner); + return mkq(q, name, lo, hi, body, t); + } + const dom = this.tr(e.domain, st, g, ctx); + return { k: 'qc', q, v: name, dom, body: this.tr(e.body, st, [], inner), t }; + } + + call(e: PNode, st: State, g: Term[], ctx: Ctx): Term { + if (e.isOld) { + const n = e.args[0]; + if (ctx.oldEnv && n.kind === 'Name' && ctx.oldEnv.has(up(n.name))) return asTerm(ctx.oldEnv.get(up(n.name)), n.name, sortOf(n.type)); + return this.tr(n, st, g, ctx); + } + const args: Term[] = e.args.map((a: PNode) => this.tr(a, st, g, ctx)); + const s = sortOf(e.type); + if (e.builtin) { + const [x, y, z] = args; + switch (e.builtin) { + case 'LENGTH': return length(x); + case 'ROWS': return length(x); + case 'COLUMNS': return columns(x); + case 'MIN': return args.length === 2 ? ite(cmp('<=', x, y), x, y) : app('MIN', args, s); + case 'MAX': return args.length === 2 ? ite(cmp('>=', x, y), x, y) : app('MAX', args, s); + case 'ABS': return ite(cmp('>=', x, num(0, x.t)), x, neg(x)); + case 'SLICE': + if (!ctx.spec) this.obligation('slice', e, `SLICE(${e.args.map(sourceOf).join(', ')}) stays inside ${sourceOf(e.args[0])}`, st, g, and(cmp('<=', num(0), y), cmp('<=', y, z), cmp('<=', z, length(x)))); + return app('SLICE', [x, y, z], s); + case 'SORTED': + if (x.k === 'q' && x.q === 'SETOF') return { ...x, q: 'SORTED', t: 'list' }; + if (x.k === 'app' && x.f === 'EMPTY') return app('LIST', [], 'list'); + return app('SORTEDOF', [x], 'list'); + // A fact pushed here holds for every value the built-in can return, or + // it is guarded by what the call needs: an unguarded fact that a run can + // break leaves the checker holding a contradiction, and then it proves + // anything (tests/prover/rand-*.pseudo). Where the interpreter stops the + // program on a bad argument, that is an obligation (kind 'argument'). + case 'FILL': { + if (!ctx.spec) this.obligation('argument', e, `FILL(${e.args.map(sourceOf).join(', ')}) asks for 0 or more copies`, st, g, cmp('<=', num(0), x)); + const r = app('FILL', args, s); + st.facts.push(or(cmp('<', x, num(0)), cmp('==', length(r), x))); + return r; + } + case 'INDEX_OF': { const r = app('INDEX_OF', args, 'int'); st.facts.push(and(cmp('<=', num(-1), r), cmp('<', r, length(x)))); return r; } + case 'RAND': { + // RAND(X) is a random number in [0, 1) times X: [0, X) for X > 0, (X, 0] for X < 0, 0 for X = 0. + const r = this.fresh('RAND', 'float'); + const zero = num(0, 'float'); + st.facts.push(or( + and(cmp('>', x, zero), cmp('<=', zero, r), cmp('<', r, x)), + and(cmp('==', x, zero), cmp('==', r, zero)), + and(cmp('<', x, zero), cmp('<', x, r), cmp('<=', r, zero)), + )); + return r; + } + case 'RANDINT': { + if (!ctx.spec) this.obligation('argument', e, `RANDINT(${e.args.map(sourceOf).join(', ')}) picks from a range that is not empty`, st, g, cmp('<', x, y)); + const r = this.fresh('RANDINT', 'int'); + st.facts.push(or(cmp('>=', x, y), and(cmp('<=', x, r), cmp('<', r, y)))); + return r; + } + default: return app(e.builtin, args, s); + } + } + const sub = e.sub; + if (!sub) return this.fresh(e.name, s); + const result = app(`FN:${sub.name}`, args, s); + const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]])); + if (!ctx.spec) { + for (const r of sub.requires) { + const goal = this.tr(r.expr, st, [], { paramEnv, spec: true }); + this.obligation('require', e, `this call to ${sub.name} meets its REQUIRE, ${sourceOf(r.expr)}`, st, g, goal, { target: r.line }); + } + this.recursion(sub, e, args, st, g); + } + for (const en of sub.ensures) { + st.facts.push(this.tr(en.expr, st, [], { paramEnv, result, spec: true, oldEnv: paramEnv })); + } + return result; + } + + /** A recursive call has to make the DECREASES measure smaller, and keep it at 0 or above. */ + recursion(sub: Routine, at: PNode, args: Term[], st: State, g: Term[]): void { + const cur = this.unit.sub; + if (!cur || !callsReach(this.program, sub, cur)) return; + // Only a subroutine calling itself, with a DECREASES, has a measure to + // check. Any other cycle of calls stays open: nothing shows it ends. + if (sub !== cur || !sub.decreases) { + this.obligation('terminates', at, sub === cur ? `the recursive calls to ${sub.name} end` : `the calls from ${cur.name} to ${sub.name} and back end`, st, g, null, { needsMeasure: true, recursive: sub === cur ? 'self' : 'mutual' }); + return; + } + const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]])); + const now = this.tr(sub.decreases.expr, st, [], { spec: true }); + const then = this.tr(sub.decreases.expr, st, [], { paramEnv, spec: true }); + this.obligation('decreases-step', at, `the recursive call makes ${sourceOf(sub.decreases.expr)} smaller`, st, g, and(cmp('<', then, now), cmp('>=', then, num(0)))); + } + + /* ---------- statements ---------- */ + run(body: PNode[], states: State[], ctx: Ctx): State[] { + let cur = states; + for (const s of body) { + if (!cur.length) break; + const next: State[] = []; + for (const st of cur) next.push(...this.stmt(s, st, ctx)); + if (next.length > this.opts.maxPaths) { this.partial = true; cur = next.slice(0, this.opts.maxPaths); } else cur = next; + if (this.deadline && Date.now() > this.deadline) { this.partial = true; return []; } + } + return cur; + } + + stmt(s: PNode, st: State, ctx: Ctx): State[] { + switch (s.kind) { + case 'Declare': { + const n = st.clone(); + for (const nm of s.names) { + const k = s.resolved?.k; + let v: Val = UNSET; + if (k === 'LIST') v = app('LIST', [], 'list'); + else if (k === 'HASHMAP' || k === 'SET') v = app('EMPTY', [], k === 'SET' ? 'set' : 'map'); + else if (k === 'ARRAY') { + const dims: Term[] = (s.resolved.dimExprs || []).map((d: PNode) => this.tr(d, st, [])); + v = app('NEWARRAY', [num(++arrayIds), ...dims], 'array'); + if (dims.length) n.facts.push(cmp('>=', dims[0], num(0))); + } else if (k === 'RECORD') v = this.fresh(nm.name, 'record'); + n.env.set(up(nm.name), v); + } + return [n]; + } + case 'Assign': { + const v = this.tr(s.value, st, []); + const n = st.clone(); + this.store(s.target, v, n, st); + return [n]; + } + case 'Input': { + const n = st.clone(); + this.store(s.target, this.fresh(rootName(s.target) || 'input', sortOf(s.target.type)), n, st); + return [n]; + } + case 'Output': case 'ExprStmt': { + for (const v of s.values || [s.expr]) this.tr(v, st, []); + return [st]; + } + case 'If': return this.ifStmt(s, st, ctx); + case 'Case': return this.caseStmt(s, st, ctx); + case 'For': return this.forStmt(s, st, ctx); + case 'ForEach': return this.forEach(s, st, ctx); + case 'While': return this.whileStmt(s, st, ctx); + case 'Repeat': return this.repeatStmt(s, st, ctx); + case 'Break': ctx.breaks?.push(st); return []; + case 'Continue': ctx.continues?.push(st); return []; + case 'Return': { + const v = s.value ? this.tr(s.value, st, []) : null; + this.ensure(s, st, v); + return []; + } + case 'Call': return [this.procCall(s, st)]; + case 'BuiltinCall': return [this.builtin(s, st)]; + case 'Assert': { + const goal = this.tr(s.expr, st, [], { spec: true }); + this.obligation('assert', s, `ASSERT ${sourceOf(s.expr)}`, st, [], goal); + return [st.assume(goal)]; + } + default: return [st]; + } + } + + /** Write a value into a target, updating the symbolic store. */ + store(target: PNode, v: Term, n: State, st: State): void { + if (target.kind === 'Name') { n.env.set(up(target.name), v); return; } + const root = rootName(target); + const build = (t: PNode, value: Term): Term => { + if (t.kind === 'Name') return value; + if (t.kind === 'Index') { + const container = this.tr(t.target, n, []); + const idx: Term[] = t.indices.map((i: PNode) => this.tr(i, st, [])); + const tk = t.target.type?.k; + if (tk === 'HASHMAP') return build(t.target, app('MAPPUT', [container, idx[0], value], 'map')); + if (idx.length === 2) { + this.obligation('index', t, `row ${sourceOf(t.indices[0])} and column ${sourceOf(t.indices[1])} of ${sourceOf(t.target)} exist`, st, [], + and(cmp('<=', num(0), idx[0]), cmp('<', idx[0], length(container)), cmp('<=', num(0), idx[1]), cmp('<', idx[1], columns(container)))); + return build(t.target, app('STORE2', [container, idx[0], idx[1], value], 'array')); + } + this.obligation('index', t, `position ${sourceOf(t.indices[0])} of ${sourceOf(t.target)} exists`, st, [], and(cmp('<=', num(0), idx[0]), cmp('<', idx[0], length(container)))); + return build(t.target, app('STORE', [container, idx[0], value], container.t)); + } + if (t.kind === 'Field') { + const r = this.tr(t.target, n, []); + return build(t.target, app(`FSTORE.${up(t.field)}`, [r, value], 'record')); + } + return value; + }; + n.env.set(up(root), build(target, v)); + } + + builtin(s: PNode, st: State): State { + const [first, ...rest]: PNode[] = s.args; + const vals: Term[] = rest.map((a) => this.tr(a, st, [])); + const coll = this.tr(first, st, []); + const n = st.clone(); + let nv: Term; + switch (s.name) { + case 'APPEND': nv = app('APPENDED', [coll, vals[0]], 'list'); break; + case 'ADD': nv = app('SETADD', [coll, vals[0]], 'set'); break; + case 'DELETE': nv = app(coll.t === 'map' ? 'MAPDEL' : 'SETDEL', [coll, vals[0]], coll.t); break; + case 'INSERT': { + this.obligation('index', s, `INSERT's position ${sourceOf(rest[0])} lies in [0, LENGTH(${sourceOf(first)})]`, st, [], and(cmp('<=', num(0), vals[0]), cmp('<=', vals[0], length(coll)))); + nv = this.fresh(sourceOf(first), 'list'); + n.facts.push(cmp('==', length(nv), add(length(coll), num(1)))); + break; + } + case 'REMOVE': { + this.obligation('index', s, `position ${sourceOf(rest[0])} of ${sourceOf(first)} exists`, st, [], and(cmp('<=', num(0), vals[0]), cmp('<', vals[0], length(coll)))); + nv = this.fresh(sourceOf(first), 'list'); + n.facts.push(cmp('==', length(nv), sub(length(coll), num(1)))); + break; + } + case 'SORT': nv = app('SORTLIST', [coll], coll.t); break; + default: nv = this.fresh(sourceOf(first), coll.t); + } + this.store(first, nv, n, st); + return n; + } + + procCall(s: PNode, st: State): State { + const sub = s.sub; + if (!sub) return st; + const args: Term[] = s.args.map((a: PNode) => this.tr(a, st, [])); + const paramEnv: Env = new Map(sub.params.map((p: PNode, i: number): [string, Val] => [up(p.name), args[i]])); + for (const r of sub.requires) { + this.obligation('require', s, `this call to ${sub.name} meets its REQUIRE, ${sourceOf(r.expr)}`, st, [], this.tr(r.expr, st, [], { paramEnv, spec: true }), { target: r.line }); + } + this.recursion(sub, s, args, st, []); + const n = st.clone(); + const after = new Map(paramEnv); + sub.params.forEach((p: PNode, i: number) => { + if (p.mode === 'BYREF' && s.args[i].kind === 'Name') { + const fresh = this.fresh(s.args[i].name, sortOf(p.type)); + n.env.set(up(s.args[i].name), fresh); + after.set(up(p.name), fresh); + } + }); + for (const en of sub.ensures) n.facts.push(this.tr(en.expr, n, [], { paramEnv: after, oldEnv: paramEnv, spec: true })); + return n; + } + + ensure(at: PNode, st: State, value: Term | null): void { + const u = this.unit; + if (!u.ensures?.length) return; + const paramEnv: Env = new Map(); + for (const p of u.params || []) { + const k = up(p.name); + paramEnv.set(k, (p.mode === 'BYREF' ? st.env.get(k) : this.entryEnv.get(k)) ?? UNSET); + } + for (const en of u.ensures) { + const goal = this.tr(en.expr, st, [], { paramEnv, result: value, oldEnv: this.entryEnv, spec: true }); + this.obligation('ensure', at, `${u.name} keeps its ENSURE, ${sourceOf(en.expr)}`, st, [], goal, { target: en.line }); + } + } + + ifStmt(s: PNode, st: State, ctx: Ctx): State[] { + const out: State[] = []; + let rest = st; + for (let i = 0; i < s.branches.length; i++) { + const b = s.branches[i]; + const c = this.tr(b.cond, rest, []); + out.push(...this.run(b.body, [rest.assume(c, `line ${b.line}: ${sourceOf(b.cond)}`)], ctx)); + rest = rest.assume(not(c), `line ${b.line}: NOT (${sourceOf(b.cond)})`); + } + if (s.elseBody) out.push(...this.run(s.elseBody, [rest], ctx)); + else out.push(rest); + return out; + } + + caseStmt(s: PNode, st: State, ctx: Ctx): State[] { + const v = s.subject ? this.tr(s.subject, st, []) : this.fresh('case', 'any'); + const out: State[] = []; + let rest = st; + for (const arm of s.arms) { + const c = or(arm.labels.map((l: PNode) => (l.value ? cmp('==', v, this.tr(l.value, st, [])) : and(cmp('<=', this.tr(l.low, st, []), v), cmp('<', v, this.tr(l.high, st, [])))))); + out.push(...this.run(arm.body, [rest.assume(c, `line ${arm.line}`)], ctx)); + rest = rest.assume(not(c)); + } + if (s.otherwise) out.push(...this.run(s.otherwise, [rest], ctx)); + else out.push(rest); + return out; + } + + /* ---------- loops ---------- */ + + /** The state at the top of a loop: changed variables become fresh symbols. */ + havoc(st: State, modified: Set, loopLine: number | undefined): State { + const n = st.clone(); + for (const k of modified) { + const old = st.env.get(k); + const name = this.types.get(k) ? [...(this.unit.symbols || []), ...(this.unit.params || [])].find((x) => up(x.name) === k)?.name || k : k; + const known = isSet(old) ? old : null; + const fresh = this.fresh(name, known && known.t ? known.t : this.sortOfName(k)); + n.env.set(k, fresh); + // an ARRAY keeps its dimensions, however its contents change + if (known && known.t === 'array') n.facts.push(cmp('==', length(fresh), length(known)), cmp('==', columns(fresh), columns(known))); + } + n.trail.push(`the top of the loop on line ${loopLine}`); + return n; + } + + userInvariants(s: PNode, st: State, env?: Env): Array<{ node: PNode; term: Term }> { + const view = new State(env || st.env, st.facts, st.trail); + return (s.invariants || []).map((inv: PNode) => ({ node: inv, term: this.tr(inv.expr, view, [], { spec: true }) })); + } + + forStmt(s: PNode, E: State, ctx: Ctx): State[] { + const a = this.tr(s.start, E, []); + const b = this.tr(s.stop, E, []); + const step: number | null = s.step ? (s.step.kind === 'Num' ? s.step.value : null) : 1; + const I = up(s.var); + const modified = modifiedIn(s.body); + modified.delete(I); + // entry: the invariant with the counter at its start + const entryEnv = new Map(E.env); entryEnv.set(I, a); + for (const inv of this.userInvariants(s, E, entryEnv)) { + this.obligation('invariant-init', inv.node, `the INVARIANT holds before the first pass`, E, [], inv.term); + } + const counterFacts = (i: Term): Term => { + if (step === null) return TRUE; + // The counter never passes the stop by a whole step or more, unless the + // loop never ran at all and it still holds its start. + if (step > 0) return and(cmp('<=', a, i), or(cmp('<', i, add(b, num(step))), cmp('==', i, a))); + return and(cmp('>=', a, i), or(cmp('>', i, add(b, num(step))), cmp('==', i, a))); + }; + const guardOf = (i: Term): Term => (step === null ? this.fresh('guard', 'bool') : step > 0 ? cmp('<', i, b) : cmp('>', i, b)); + const makeHead = (extra: (h: State) => Term[]) => { + const H = this.havoc(E, modified, s.line); + const i = this.fresh(s.var, 'int'); + H.env.set(I, i); + H.facts.push(counterFacts(i)); + if (step !== null && Math.abs(step) > 1) { + // the counter moves in whole steps from its start: I == start + STEP * Passes + const passes = this.fresh('passes', 'int'); + H.facts.push(cmp('==', i, add(a, arith('*', num(step), passes))), cmp('>=', passes, num(0))); + } + for (const f of extra(H)) H.facts.push(f); + for (const inv of this.userInvariants(s, H)) H.facts.push(inv.term); + return { H, i }; + }; + const advance = (st: State, _i: Term): State => { const n = st.clone(); n.env.set(I, add(asTerm(n.env.get(I), s.var, 'int'), num(step ?? 1))); return n; }; + const cands = this.opts.infer ? this.infer(s, E, modified, { + entryEnv: (env: Env) => { const m = new Map(env); m.set(I, a); return m; }, + makeHead, guardOf, advance, counter: I, start: a, step: step ?? 1, + }) : []; + const { H, i } = makeHead((h) => cands.map((c) => c.at(h.env))); + const g = guardOf(i); + const inner: { breaks: State[]; continues: State[] } = { breaks: [], continues: [] }; + const ends = this.run(s.body, [H.assume(g, `line ${s.line}: ${s.var} < ${sourceOf(s.stop)}`)], inner); + for (const st of [...ends, ...inner.continues]) { + const n = advance(st, i); + for (const inv of this.userInvariants(s, n)) this.obligation('invariant-keep', inv.node, `each pass keeps the INVARIANT`, n, [], inv.term); + } + this.decreases(s, H, g, [...ends, ...inner.continues].map((st) => advance(st, i))); + const exit = H.assume(not(g), `line ${s.line}: the loop has ended`); + const after = [exit, ...inner.breaks].map((st) => { + const n = st.clone(); + if (s.declaredCounter) n.env.set(I, UNSET); else n.env.delete(I); + return n; + }); + return after; + } + + forEach(s: PNode, E: State, ctx: Ctx): State[] { + const coll = this.tr(s.iterable, E, []); + const X = up(s.var); + const modified = modifiedIn(s.body); + modified.delete(X); + for (const inv of this.userInvariants(s, E)) this.obligation('invariant-init', inv.node, `the INVARIANT holds before the first pass`, E, [], inv.term); + const H = this.havoc(E, modified, s.line); + const x = this.fresh(s.var, sortOf(s.eachSym?.type)); + H.env.set(X, x); + for (const inv of this.userInvariants(s, H)) H.facts.push(inv.term); + // X is in the collection only inside a pass. The loop also ends without + // any pass when the collection is empty, so the way out must not know it: + // for an empty list that fact is FALSE, and the checker would then prove + // anything after the loop (tests/prover/foreach-empty.pseudo). + let inPass = H; + if (coll.k === 'app' && coll.f === 'RANGE') inPass = H.assume(and(cmp('<=', coll.args[0], x), cmp('<', x, coll.args[1]))); + else if (coll.t === 'list' || coll.t === 'set' || coll.t === 'map') inPass = H.assume(member(x, coll)); + const inner: { breaks: State[]; continues: State[] } = { breaks: [], continues: [] }; + const ends = this.run(s.body, [inPass], inner); + for (const st of [...ends, ...inner.continues]) { + for (const inv of this.userInvariants(s, st)) this.obligation('invariant-keep', inv.node, `each pass keeps the INVARIANT`, st, [], inv.term); + } + const out = [H, ...inner.breaks].map((st) => { const n = st.clone(); n.env.delete(X); return n; }); + return out; + } + + whileStmt(s: PNode, E: State, ctx: Ctx): State[] { + const modified = modifiedIn(s.body); + for (const inv of this.userInvariants(s, E)) this.obligation('invariant-init', inv.node, `the INVARIANT holds before the first pass`, E, [], inv.term); + const makeHead = (extra: (h: State) => Term[]) => { + const H = this.havoc(E, modified, s.line); + for (const f of extra(H)) H.facts.push(f); + for (const inv of this.userInvariants(s, H)) H.facts.push(inv.term); + return { H }; + }; + const guardOf = (H: State): Term => this.withQuiet(() => this.tr(s.cond, H, [])); + const cands = this.opts.infer ? this.infer(s, E, modified, { + entryEnv: (env: Env) => env, makeHead, guardOf: null, whileGuard: guardOf, advance: (st: State) => st, + }) : []; + const { H } = makeHead((h) => cands.map((c) => c.at(h.env))); + const g = this.tr(s.cond, H, []); + const inner: { breaks: State[]; continues: State[] } = { breaks: [], continues: [] }; + const ends = this.run(s.body, [H.assume(g, `line ${s.line}: ${sourceOf(s.cond)}`)], inner); + const back = [...ends, ...inner.continues]; + for (const st of back) { + for (const inv of this.userInvariants(s, st)) this.obligation('invariant-keep', inv.node, `each pass keeps the INVARIANT`, st, [], inv.term); + } + this.decreases(s, H, g, back, true, cands); + return [H.assume(not(g), `line ${s.line}: NOT (${sourceOf(s.cond)})`), ...inner.breaks]; + } + + repeatStmt(s: PNode, E: State, ctx: Ctx): State[] { + const modified = modifiedIn(s.body); + for (const inv of this.userInvariants(s, E)) this.obligation('invariant-init', inv.node, `the INVARIANT holds before the first pass`, E, [], inv.term); + const H = this.havoc(E, modified, s.line); + for (const inv of this.userInvariants(s, H)) H.facts.push(inv.term); + const inner: { breaks: State[]; continues: State[] } = { breaks: [], continues: [] }; + const ends = this.run(s.body, [H], inner); + const exits: State[] = []; + const again: State[] = []; + for (const st of [...ends, ...inner.continues]) { + const c = s.cond ? this.tr(s.cond, st, []) : TRUE; + exits.push(st.assume(c, `line ${s.endLine}: UNTIL ${sourceOf(s.cond)}`)); + const n = st.assume(not(c)); + again.push(n); + for (const inv of this.userInvariants(s, n)) this.obligation('invariant-keep', inv.node, `each pass keeps the INVARIANT`, n, [], inv.term); + } + this.decreases(s, H, TRUE, again, s.kind !== 'For'); + return [...exits, ...inner.breaks]; + } + + withQuiet(fn: () => T): T { this.quiet = (this.quiet || 0) + 1; try { return fn(); } finally { this.quiet--; } } + + /** + * Termination. A DECREASES line gives two obligations: the measure is at + * least 0 whenever the loop continues, and every pass makes it smaller. + * Without one, a WHILE loop gets a guess from its condition (N - I for + * I < N) that is kept only if both obligations prove. + */ + decreases(s: PNode, H: State, guard: Term, backStates: State[], needed = false, _cands: Cand[] = []): void { + if (s.decreases) { + const d0 = this.tr(s.decreases.expr, H, [], { spec: true }); + this.obligation('decreases-bound', s.decreases, `${sourceOf(s.decreases.expr)} is at least 0 while the loop continues`, H, [guard], cmp('>=', d0, num(0))); + for (const st of backStates) { + const d1 = this.tr(s.decreases.expr, st, [], { spec: true }); + this.obligation('decreases-step', s.decreases, `each pass makes ${sourceOf(s.decreases.expr)} smaller`, st, [], cmp('<', d1, d0)); + } + return; + } + if (!needed || s.kind === 'For' || !this.recording) return; + // guess a measure from the condition + const guesses: PNode[] = []; + const conds = s.cond ? conjuncts(s.cond) : []; + for (const c of conds) { + if (c.kind !== 'Binary') continue; + const [a, b] = [c.left, c.right]; + if (c.op === '<') guesses.push({ kind: 'Binary', op: '-', left: b, right: a, type: a.type }); + if (c.op === '<=') guesses.push({ kind: 'Binary', op: '+', left: { kind: 'Binary', op: '-', left: b, right: a, type: a.type }, right: { kind: 'Num', value: 1 }, type: a.type }); + if (c.op === '>') guesses.push({ kind: 'Binary', op: '-', left: a, right: b, type: a.type }); + if (c.op === '>=') guesses.push({ kind: 'Binary', op: '+', left: { kind: 'Binary', op: '-', left: a, right: b, type: a.type }, right: { kind: 'Num', value: 1 }, type: a.type }); + } + for (const gs of guesses) { + if (gs.left?.type?.k !== 'INTEGER' && gs.type?.k !== 'INTEGER') continue; + const d0 = this.withQuiet(() => this.tr(gs, H, [], { spec: true })); + const okBound = prove([...H.facts, guard], cmp('>=', d0, num(0))).status === 'proved'; + const okStep = okBound && backStates.every((st) => prove(st.facts, cmp('<', this.withQuiet(() => this.tr(gs, st, [], { spec: true })), d0)).status === 'proved'); + if (okBound && okStep) { + this.notes.push({ line: s.line as number, text: `The ${s.kind === 'While' ? 'WHILE' : 'REPEAT'} loop on line ${s.line} ends: ${sourceOf(gs)} is at least 0 whenever the loop continues, and every pass makes it smaller. The checker found this measure itself; writing it as DECREASES ${sourceOf(gs)} states it for a reader.` }); + this.obligation('terminates', s, `the ${s.kind === 'While' ? 'WHILE' : 'REPEAT'} loop on line ${s.line} ends`, H, [], TRUE, { measure: sourceOf(gs) }); + return; + } + } + this.obligation('terminates', s, `the ${s.kind === 'While' ? 'WHILE' : 'REPEAT'} loop on line ${s.line} ends`, H, [], null, { needsMeasure: true }); + } + + /* ---------- invariant inference ---------- */ + + /** + * Houdini: start from a list of simple candidate invariants, and throw out + * every one that fails on entry or fails to survive a pass, until what is + * left survives. What is left is proved, and the loop's real analysis + * assumes it. + */ + infer(s: PNode, E: State, modified: Set, h: InferHooks): Cand[] { + if (this.depth > 2) return []; + const cands: Cand[] = []; + const entry = h.entryEnv(E.env); + const counter = h.counter; + const startTerm = h.start ?? num(0); + const loopLine = s.line; + for (const k of modified) { + const v0 = E.env.get(k); + const nm = [...(this.unit.symbols || [])].find((x) => up(x.name) === k)?.name || k; + if (!v0 || v0 === UNSET) continue; + if (!isSet(v0)) continue; + const t = v0.t; + if (numeric(t)) { + cands.push({ text: `${nm} >= ${show(v0)}`, at: (env) => cmp('>=', valOf(env, k), v0) }); + cands.push({ text: `${nm} <= ${show(v0)}`, at: (env) => cmp('<=', valOf(env, k), v0) }); + if (counter) cands.push({ text: `${nm} <= ${show(v0)} + ${counter} - ${show(startTerm)}`, at: (env) => cmp('<=', valOf(env, k), add(v0, sub(valOf(env, counter), startTerm))) }); + } + if (t === 'list' || t === 'string') { + cands.push({ text: `LENGTH(${nm}) >= LENGTH(before the loop)`, at: (env) => cmp('>=', length(valOf(env, k)), length(v0)) }); + if (counter) { + const passes = (env: Env): Term => ((h.step ?? 1) < 0 ? sub(startTerm, valOf(env, counter)) : sub(valOf(env, counter), startTerm)); + cands.push({ text: `LENGTH(${nm}) <= LENGTH(before) + passes`, at: (env) => cmp('<=', length(valOf(env, k)), add(length(v0), passes(env))) }); + cands.push({ text: `LENGTH(${nm}) == LENGTH(before) + passes`, at: (env) => cmp('==', length(valOf(env, k)), add(length(v0), passes(env))) }); + } + } + } + // from a WHILE condition: I < N suggests I <= N + if (s.kind === 'While') { + for (const c of conjuncts(s.cond)) { + if (c.kind !== 'Binary' || !['<', '<=', '>', '>='].includes(c.op)) continue; + const l = c.left; const r = c.right; + if (l.kind === 'Name' && modified.has(up(l.name)) && l.type?.k === 'INTEGER') { + const op = c.op === '<' ? '<=' : c.op === '>' ? '>=' : c.op; + const extra = c.op === '<=' ? 1 : c.op === '>=' ? -1 : 0; + cands.push({ text: `${l.name} ${op} ${sourceOf(r)}${extra ? (extra > 0 ? ' + 1' : ' - 1') : ''}`, at: (env) => { const st = new State(env, []); const R = this.withQuiet(() => this.tr(r, st, [], { spec: true })); return cmp(op as CmpOp, valOf(env, up(l.name)), extra ? add(R, num(extra)) : R); } }); + } + } + } + if (!cands.length) return []; + let lastHead: State | null = null; + let live = cands.filter((c) => { + try { return prove(E.facts, c.at(entry)).status === 'proved'; } catch { return false; } + }); + const saved = this.recording; + this.recording = false; + this.depth++; + // Only a set that survives a whole pass unchanged is inductive. Running + // out of rounds while candidates are still being dropped proves nothing + // about the ones left, so then nothing is kept (tests/prover/houdini-cap). + let stable = live.length === 0; + try { + for (let round = 0; round < 8 && live.length; round++) { + const { H, i } = h.makeHead((hh) => live.map((c) => c.at(hh.env))); + lastHead = H; + let start = H; + if (h.guardOf) start = H.assume(h.guardOf(i as Term)); + else if (h.whileGuard) start = H.assume(h.whileGuard(H)); + const inner = { breaks: [], continues: [] }; + const ends = this.run(s.body, [start], inner).concat(inner.continues).map((st) => h.advance(st, i as Term)); + const keep = live.filter((c) => ends.every((st) => { + try { return prove(st.facts, c.at(st.env)).status === 'proved'; } catch { return false; } + })); + if (keep.length === live.length) { stable = true; break; } + live = keep; + if (!live.length) stable = true; + } + } finally { + this.recording = saved; + this.depth--; + } + if (!stable) live = []; + if (saved && live.length) { + const env = lastHead ? lastHead.env : entry; + const texts = live.filter((c) => { try { return prove([], c.at(env)).status !== 'proved'; } catch { return true; } }) + .map((c) => { try { return show(c.at(env)); } catch { return c.text; } }); + this.inferred.push({ line: loopLine as number, facts: [...new Set(texts)] }); + } + return live; + } + + /* ---------- a whole unit ---------- */ + runUnit(): void { + const u = this.unit; + const st = new State(); + this.entryEnv = new Map(); + for (const p of u.params || []) { + const v = this.fresh(p.name, sortOf(p.type)); + st.env.set(up(p.name), v); + this.entryEnv.set(up(p.name), v); + } + for (const r of u.requires || []) st.facts.push(this.tr(r.expr, st, [], { spec: true })); + const ends = this.run(u.body, [st], { breaks: [], continues: [] }); + if (u.kind === 'Procedure') for (const e of ends) this.ensure({ kind: 'End', line: u.node!.endLine, col: 1 }, e, null); + } +} + +function conjuncts(e: PNode | null | undefined): PNode[] { + if (!e) return []; + if (e.kind === 'Binary' && e.op === 'AND') return [...conjuncts(e.left), ...conjuncts(e.right)]; + return [e]; +} + +function fieldRead(r: Term, field: string, s: Sort): Term { + if (r.k === 'app' && r.f.startsWith('FSTORE.')) return r.f === `FSTORE.${up(field)}` ? r.args[1] : fieldRead(r.args[0], field, s); + return app(`FIELD.${field}`, [r], s); +} + +function columns(x: Term): Term { + if (x.k === 'app' && x.f === 'NEWARRAY') return x.args[2] || num(0); + if (x.k === 'app' && x.f === 'STORE2') return columns(x.args[0]); + return app('COLUMNS', [x], 'int'); +} + +/* ------------------------------------------------------------------ */ +/* Grouping and proving */ +/* ------------------------------------------------------------------ */ + +/** The facts that share a symbol with the goal, directly or through another fact. */ +function relevantFacts(hyps: Term[], goal: Term, limit = 14): Term[] { + const want = new Set(symsOf(goal).keys()); + const chosen = new Set(); + for (let round = 0; round < 3; round++) { + let grew = false; + hyps.forEach((h, i) => { + if (chosen.has(i)) return; + const ks = [...symsOf(h).keys()]; + if (ks.some((k) => want.has(k))) { chosen.add(i); for (const k of ks) if (!want.has(k)) { want.add(k); grew = true; } } + }); + if (!grew) break; + } + return [...chosen].sort((a, b) => a - b).slice(0, limit).map((i) => hyps[i]); +} + +/** Display names: the newest version of I prints as I, older ones as I0, I1, ... */ +function namesFor(terms: Term[]): Map { + const all = new Map(); + for (const t of terms) symsOf(t, all); + const byName = new Map>(); + for (const s of all.values()) { let l = byName.get(s.name); if (!l) { l = []; byName.set(s.name, l); } l.push(s); } + const names = new Map(); + for (const [n, list] of byName) { + list.sort((a, b) => a.id - b.id); + list.forEach((s, i) => names.set(key(s), i === list.length - 1 ? n : `${n}${'₀₁₂₃₄₅₆₇₈₉'[i] || i}`)); + } + return names; +} + +export function verifyUnit(program: Program, unit: Unit, options: VerifyOptions = {}): UnitReport { + const ex = new Exec(program, unit, options); + ex.deadline = Date.now() + (options.budgetMs ?? 4000); + try { ex.runUnit(); } catch (err) { ex.partial = true; ex.notes.push({ line: unit.node?.line ?? 1, text: `The checker stopped early: ${(err as Error).message}` }); } + // The number model behind "proved": INTEGER is a whole number of any size. + // Neither the lab's limit (a run stops past 2^53 - 1) nor Mojo's 64-bit Int + // (which wraps past 2^63 - 1, probes/int-overflow-int) is part of the proof. + if (ex.floatArith) ex.notes.push({ line: unit.node?.line ?? 1, kind: 'float-model', text: 'The proofs here treat a FLOAT as a finite number and assume nothing about a rounded result except that the same calculation gives it again. A run in this lab stops when a FLOAT passes about 1.8 × 10^308; in the Mojo made from this program, a Float64 division by zero gives inf or nan and the run goes on (probes/float-special).' }); + if (ex.intArith) ex.notes.push({ line: unit.node?.line ?? 1, kind: 'int-model', text: 'The proofs here treat INTEGER as a whole number of any size. A run in this lab stops when a whole number passes 9,007,199,254,740,991, and the Mojo made from this program uses a 64-bit Int, which wraps around past 9,223,372,036,854,775,807. Neither limit is part of the proof.' }); + type Group = { kind: string; line: number | null; col: number; label: string; instances: RawOb[]; target?: number; measure?: string; needsMeasure?: boolean; recursive?: 'self' | 'mutual' }; + const groups = new Map(); + for (const o of ex.raw) { + const k = `${o.kind}@${o.line}:${o.col}@${o.label}`; + if (!groups.has(k)) groups.set(k, { kind: o.kind, line: o.line, col: o.col, label: o.label, instances: [], target: o.target, measure: o.measure, needsMeasure: o.needsMeasure, recursive: o.recursive }); + groups.get(k)!.instances.push(o); + } + const obligations: Obligation[] = []; + for (const gr of groups.values()) { + let status: 'proved' | 'unknown' = 'proved'; + let failing: RawOb | null = null; + if (gr.needsMeasure) { status = 'unknown'; failing = gr.instances[0]; } + else { + for (const inst of gr.instances) { + if (Date.now() > ex.deadline + 2000) { status = 'unknown'; failing = inst; inst.reason = 'the checker ran out of time'; break; } + let r: import('./solver.js').ProveResult; + try { r = inst.goal ? prove(inst.hyps, inst.goal) : { status: 'unknown', reason: 'there is no goal to prove' }; } catch (err) { r = { status: 'unknown', reason: (err as Error).message }; } + inst.status = r.status; + if (r.status !== 'proved') { status = 'unknown'; failing = inst; inst.reason = r.reason; inst.witness = r.witness; break; } + } + } + const shownInst = failing || gr.instances[0]; + let shown: Obligation['shown'] = null; + if (shownInst && shownInst.goal) { + const facts = relevantFacts(shownInst.hyps, shownInst.goal); + const names = namesFor([...facts, shownInst.goal]); + shown = { facts: facts.map((f) => show(f, names)), goal: show(shownInst.goal, names), trail: shownInst.trail, reason: shownInst.reason }; + const goal = shownInst.goal; + if (options.debug && shownInst.witness) shown.witness = shownInst.witness.map((l) => show(l, namesFor([...shownInst.hyps, goal]))); + } + const { instances, ...place } = gr; + obligations.push({ ...place, status, paths: instances.length, shown, instances: undefined } as Obligation); + } + obligations.sort((a, b) => (a.line ?? 0) - (b.line ?? 0) || KIND_ORDER.indexOf(a.kind) - KIND_ORDER.indexOf(b.kind)); + return { name: unit.name, kind: unit.kind, line: unit.node?.line ?? 1, obligations, inferred: ex.inferred, notes: ex.notes, partial: ex.partial }; +} + +const KIND_ORDER = ['require', 'index', 'key', 'slice', 'divide', 'argument', 'invariant-init', 'invariant-keep', 'decreases-bound', 'decreases-step', 'terminates', 'assert', 'ensure']; + +export function verify(program: Program, options: VerifyOptions = {}): Report { + const units: UnitReport[] = []; + for (const sub of program.subs.values()) { + units.push(verifyUnit(program, { name: sub.name, kind: sub.kind, node: sub.node, body: sub.node.body, params: sub.params, symbols: sub.symbols || [], requires: sub.requires, ensures: sub.ensures, sub }, options)); + } + if (program.main.body.length) units.push(verifyUnit(program, { name: 'main program', kind: 'main', node: null, body: program.main.body, params: [], symbols: program.main.symbols, requires: [], ensures: [] }, options)); + const all = units.flatMap((u) => u.obligations); + return { units, counts: { proved: all.filter((o) => o.status === 'proved').length, unknown: all.filter((o) => o.status !== 'proved').length, total: all.length } }; +} + +export const OBLIGATION_WORDS: Record = { + index: 'Position exists', + key: 'Key present', + slice: 'Slice in range', + divide: 'No division by zero', + argument: 'Argument allowed', + require: 'Precondition met', + ensure: 'Postcondition holds', + 'invariant-init': 'Invariant on entry', + 'invariant-keep': 'Invariant kept', + 'decreases-bound': 'Measure stays at 0 or above', + 'decreases-step': 'Measure shrinks', + terminates: 'Loop or recursion ends', + assert: 'Assertion holds', +};