diff --git a/builder/machine.mjs b/builder/machine.mjs index c622065..5db85f4 100644 --- a/builder/machine.mjs +++ b/builder/machine.mjs @@ -59,29 +59,69 @@ function missCondition(n, c) { } } -/** A table of this many rules or more is factored by a choice field when one shortens every chain. */ -export const FACTOR_AT = 11; +/** + * The deepest `if` nesting the property proofs split. The fixed + * `linear_arithmetic` script unfolds the whole decision into one goal and runs + * `repeat' split`; Lean's `split` simplifies the goal at every step and gives up + * (simp's step limit) once the `if`s nest past a dozen, measured on the locked + * SDK's Lean (13 nested `if`s split, 14 do not). A decision deeper than this is + * factored by choice fields until no path is. + */ +export const MAX_DEPTH = 12; + +/** A rule with the conditions on `fixed` fields dropped: inside that branch they hold. */ +const specialize = (r, fixed) => ({ ...r, when: r.when.filter((c) => !fixed.has(c.field)), whole: r.whole ?? r }); + +/** The rules a chain tests: up to the first that always fires there, which answers the rest. */ +function truncate(rules) { + const at = rules.findIndex((r) => r.when.length === 0); + return at < 0 ? { tests: rules, last: null } : { tests: rules.slice(0, at), last: rules[at] }; +} /** - * The choice field that factors a long decision table best: the one whose - * longest chain (a value's rules, or the rules past every value) is shortest, - * if that is shorter than the whole table. A first-match chain asks the prover - * to split one `if` per rule, and Lean's `split` gives up near a dozen. + * The choice field that factors a too-deep decision best: the one whose deepest + * branch (the tests to reach a value's chain plus that chain, or past every + * value) is shallowest, if that is shallower than the chain itself. */ -export function factorField(spec) { - if (spec.rules.length < FACTOR_AT) return null; +function bestField(spec, rules, fixed) { let best = null; - for (const f of spec.fields.filter((g) => g.kind === 'choice')) { - const longest = Math.max(spec.rules.filter((r) => canFire(r, f.name, null)).length, ...f.values.map((v) => spec.rules.filter((r) => canFire(r, f.name, v)).length)); - if (longest < spec.rules.length && (!best || longest < best.longest)) best = { state: f.name, longest }; + for (const f of spec.fields.filter((g) => g.kind === 'choice' && !fixed.has(g.name))) { + const sub = (v) => truncate(rules.filter((r) => canFire(r, f.name, v)).map((r) => specialize(r, new Set([f.name])))).tests.length; + const depth = Math.max(f.values.length + sub(null), ...f.values.map((v, i) => i + 1 + sub(v))); + if (!best || depth < best.depth) best = { field: f.name, depth }; + } + return best && best.depth < truncate(rules).tests.length ? best.field : null; +} + +/** Kept for callers that ask which field a table is factored by first. */ +export function factorField(spec) { + const field = truncate(spec.rules).tests.length > MAX_DEPTH ? bestField(spec, spec.rules, new Set()) : null; + return field ? { state: field } : null; +} + +/** A condition that holds, as an atom (the rule's own name is used when it is unchanged). */ +function holdCondition(n, c) { + const field = n.fields[c.field]; + const left = `(project request ${field.id})`; + const right = c.op === 'is' || c.op === 'is not' ? `(${field.choices[c.value]})` : typeof c.value === 'object' ? `(project request ${n.fields[c.value.field].id})` : String(c.value); + switch (c.op) { + case 'is': + case '=': + return `(${left} =? ${right})`; + case '<': return `(${left} ': return `(${right} =': return `(${right} <=? ${left})`; + default: throw new Error(`no atom holds for ${c.op}`); } - return best ? { state: best.state } : null; } /** - * The decision as it is proved: a state machine's owned by its states, a long - * table's factored by its best choice field, and every rule that would need a - * classical negation tested by its miss first. The answer is prism-code's + * The decision as it is proved: a state machine's owned by its states, a deep + * table's factored by choice fields until no path nests past MAX_DEPTH, and + * every rule that would need a classical negation tested by its miss first. + * Inside a branch the conditions on the fields it fixed are dropped, and a rule + * left with none answers the rest of its chain. The answer is prism-code's * first-match answer for every request. */ export function machineDeclarations(spec, machine, n = names(spec)) { @@ -101,24 +141,46 @@ export function machineDeclarations(spec, machine, n = names(spec)) { const rule = decls.find((d) => d.name === n.rules[r.id]); return { ...rule, name: misses.get(r.id), body: r.when.map((c) => missCondition(n, c)).reduce((acc, t) => (acc ? `(${acc} || ${t})` : t), '') }; }); - const chain = (rules) => - rules.reduceRight( - (rest, r) => (misses.has(r.id) ? `(if (${misses.get(r.id)} request) then ${rest} else (${n.answers[r.then]}))` : `(if (${n.rules[r.id]} request) then (${n.answers[r.then]}) else ${rest})`), - `(${n.answers[spec.otherwise]})`, - ); - const owner = machine ?? factorField(spec); + const or = (terms) => terms.reduce((acc, t) => (acc ? `(${acc} || ${t})` : t), ''); + const chain = (rules) => { + const { tests, last } = truncate(rules); + return tests.reduceRight((rest, r) => { + const whole = r.whole ?? r; + const answer = `(${n.answers[r.then]})`; + if (r.when.length === whole.when.length) return misses.has(r.id) ? `(if (${misses.get(r.id)} request) then ${rest} else ${answer})` : `(if (${n.rules[r.id]} request) then ${answer} else ${rest})`; + return missable(r) ? `(if ${or(r.when.map((c) => missCondition(n, c)))} then ${rest} else ${answer})` : `(if ${holdCondition(n, r.when[0])} then ${answer} else ${rest})`; + }, `(${n.answers[last ? last.then : spec.otherwise]})`); + }; + const stepDecls = []; + // One branch per value of the field, its own chain or factored again; the chain past every value holds the rules that name no value with `is`. + const factor = (rules, fixed, prefix, field, budget) => { + const f = spec.fields.find((g) => g.name === field); + const inner = new Set([...fixed, field]); + const steps = f.values.map((value, i) => { + const name = claim(`step${prefix}${pascal(value)}`); + const sub = rules.filter((r) => canFire(r, field, value)).map((r) => specialize(r, inner)); + stepDecls.push({ ...judge, name, body: build(sub, inner, `${prefix}${pascal(value)}`, budget - i - 1) }); + return { value, name, rules: rules.filter((r) => canFire(r, field, value)).map((r) => r.whole ?? r) }; + }); + const rest = rules.filter((r) => canFire(r, field, null)); + const stateOf = `(project request ${n.fields[field].id})`; + const body = steps.reduceRight((tail, s) => `(if (${stateOf} =? (${n.fields[field].choices[s.value]})) then (${s.name} request) else ${tail})`, chain(rest)); + return { body, steps, rest }; + }; + const build = (rules, fixed, prefix, budget) => { + if (truncate(rules).tests.length <= budget) return chain(rules); + const field = bestField(spec, rules, fixed); + return field ? factor(rules, fixed, prefix, field, budget).body : chain(rules); + }; + const owner = machine?.state ?? factorField(spec)?.state; if (!owner) { - return { decls: [...decls.slice(0, at), ...missDecls, { ...judge, body: chain(spec.rules) }, ...decls.slice(at + 1)], steps: [], misses: [...misses.values()], rest: spec.rules.map((r) => r.id), n }; + return { decls: [...decls.slice(0, at), ...missDecls, { ...judge, body: chain(spec.rules) }, ...decls.slice(at + 1)], steps: [], stepNames: [], misses: [...misses.values()], rest: spec.rules.map((r) => r.id), n }; } - const field = spec.fields.find((f) => f.name === owner.state); - const steps = field.values.map((value) => ({ value, name: claim(`step${pascal(value)}`), rules: spec.rules.filter((r) => canFire(r, field.name, value)) })); - const stateOf = `(project request ${n.fields[field.name].id})`; - const rest = spec.rules.filter((r) => canFire(r, field.name, null)); - const body = steps.reduceRight((tail, s) => `(if (${stateOf} =? (${n.fields[field.name].choices[s.value]})) then (${s.name} request) else ${tail})`, chain(rest)); - const stepDecls = steps.map((s) => ({ ...judge, name: s.name, body: chain(s.rules) })); + const { body, steps, rest } = factor(spec.rules, new Set(), '', owner, MAX_DEPTH); return { decls: [...decls.slice(0, at), ...missDecls, ...stepDecls, { ...judge, body }, ...decls.slice(at + 1)], steps: steps.map((s) => ({ value: s.value, name: s.name, rules: s.rules.map((r) => r.id) })), + stepNames: stepDecls.map((d) => d.name), misses: [...misses.values()], rest: rest.map((r) => r.id), n, @@ -127,9 +189,9 @@ export function machineDeclarations(spec, machine, n = names(spec)) { /** The proofs module's declarations: each theorem also unfolds the states' chains and the rules' misses. */ export function machineProofDeclarations(spec, machine, n = names(spec)) { - const { steps, misses } = machineDeclarations(spec, machine, n); + const { stepNames, misses } = machineDeclarations(spec, machine, n); const q = (name) => `${n.module}::${name}`; - return proofDeclarations(spec, n).map((d) => (d.kind === 'theorem' ? { ...d, unfold: [...new Set([...d.unfold, ...steps.map((s) => q(s.name)), ...misses.map(q)])].sort() } : d)); + return proofDeclarations(spec, n).map((d) => (d.kind === 'theorem' ? { ...d, unfold: [...new Set([...d.unfold, ...stepNames.map(q), ...misses.map(q)])].sort() } : d)); } // The module text exactly as prism-code's renderer lays it out. diff --git a/builder/set.mjs b/builder/set.mjs index a96e95f..66ce4ce 100644 --- a/builder/set.mjs +++ b/builder/set.mjs @@ -78,8 +78,9 @@ export function readSet(set) { /** * Claim ids: the initials of every word of the app's name. A prefix a row of - * another suite already holds (`taken`) is lengthened by the next letters of - * the name's last word until it is free. Refused when two apps would share one. + * another suite already holds (`taken`), or an earlier app of the set holds, is + * lengthened by the next letters of the name's last word until it is free. + * Refused when two apps would still share one. */ function idPrefixes(models, taken = new Set()) { const out = new Map(); @@ -88,7 +89,9 @@ function idPrefixes(models, taken = new Set()) { const p = words.map((w) => w[0]).join('').toUpperCase(); let prefix = p === 'CM' ? 'CN' : p; const last = words.at(-1).toUpperCase(); - for (let k = 1; taken.has(prefix) && k < last.length; k++) prefix = `${p}${last.slice(1, k + 1)}`; + // A prefix an earlier app of the set holds is lengthened the same way, so the first app keeps it. + const held = (x) => taken.has(x) || [...out.values()].includes(x); + for (let k = 1; held(prefix) && k < last.length; k++) prefix = `${p}${last.slice(1, k + 1)}`; if (taken.has(prefix)) throw new SpecError(`Every id prefix of ${m.spec.name} is held by another suite's rows; rename the app.`, 'prompts'); if ([...out.values()].includes(prefix)) throw new SpecError(`Two apps of the set would both claim ${prefix}-xx; rename one.`, 'prompts'); out.set(m, prefix); diff --git a/builder/test.mjs b/builder/test.mjs index 0cc4754..7f03254 100644 --- a/builder/test.mjs +++ b/builder/test.mjs @@ -14,7 +14,7 @@ import { fileURLToPath } from 'node:url'; import { buildApp } from './build.mjs'; import { buildSet, routerRespond } from './set.mjs'; import { WIDTH } from './vendor/prism-code/src/journey/render.mjs'; -import { machineDeclarations } from './machine.mjs'; +import { MAX_DEPTH, machineDeclarations } from './machine.mjs'; import { deriveModel } from './model.mjs'; import { render as renderAssets } from './pack.mjs'; import { DIATAXIS, EXAMPLES, JOURNEY, OVERVIEW, PATTERN_TEXT, cliCommands, examplePage, groupOf, guidePath, guideTitle, page, prismSource, verifyScript, workflowStep, writeSite } from './site.mjs'; @@ -126,6 +126,45 @@ const CASES = { wide.app.examples = [{ request: `closed push 0 ${'v0 '.repeat(5).trim()}`, response: 'open' }]; const wm = deriveModel(wide).machine; ok(!wm.exhaustive && wm.unreachable.length === 0 && wm.derived.length === 1, 'a partial sample claims only the final state'); + // A table nested past MAX_DEPTH is factored by choice fields until no path is, and answers as the flat first-match chain. + const tall = rule30(); + tall.app.fields = [{ name: 'a', kind: 'choice', values: ['a0', 'a1', 'a2', 'a3', 'a4'], means: 'a' }, { name: 'b', kind: 'choice', values: ['b0', 'b1', 'b2', 'b3'], means: 'b' }, { name: 'c', kind: 'choice', values: ['c0', 'c1', 'c2', 'c3'], means: 'c' }, { name: 'd', kind: 'choice', values: ['d0', 'd1'], means: 'd' }]; + const at = (f, v, op = 'is') => ({ field: f, op, value: v }); + const tallRules = [[at('d', 'd1')], [at('a', 'a3')], [at('a', 'a4')], [at('c', 'c3')], [at('b', 'b3')], [at('b', 'b2'), at('a', 'a2')], [at('b', 'b2')], [at('a', 'a2'), at('b', 'b1')], [at('a', 'a2')], [at('c', 'c1'), at('b', 'b0')], [at('c', 'c1')], [at('a', 'a0'), at('b', 'b0')], [at('a', 'a0')], [at('b', 'b1')], ...['c0', 'c1', 'c2'].flatMap((c) => ['b0', 'b1', 'b2'].map((b) => [at('c', c), at('b', b)]))]; + tall.app.outcomes = ['on', 'off']; + tall.app.rules = tallRules.map((when, i) => ({ id: `REQ-${String(i + 1).padStart(3, '0')}`, when, then: i % 2 ? 'on' : 'off', says: `rule ${i + 1}`, why: 'a tall table' })); + tall.app.otherwise = 'on'; + tall.app.properties = [{ id: 'INV-001', kind: 'always', when: [at('d', 'd1')], then: 'off', says: 'd1 is off', why: 'first rule' }]; + tall.app.examples = [{ request: 'a0 b0 c0 d1', response: 'off' }]; + delete tall.journeys; + delete tall.app.journeys; + const tm = deriveModel(tall); + const tallOwned = machineDeclarations(tm.spec, null); + const defs = Object.fromEntries(tallOwned.decls.map((d) => [d.name, d])); + const code = (x) => +defs[x].body; + const term = (text) => { + const ts = text.match(/\(|\)|=\?|<=\?|<\?|\|\||[A-Za-z_][A-Za-z0-9_]*|\d+/g); + let i = 0; + const read = () => (ts[i] === '(' ? (i++, (() => { const xs = []; while (ts[i] !== ')') xs.push(read()); i++; return xs; })()) : ts[i++]); + return read(); + }; + const evalTerm = (x, req) => { + if (typeof x === 'string') return /^\d+$/.test(x) ? +x : code(x); + if (x.length === 1) return evalTerm(x[0], req); + if (x[0] === 'if') return evalTerm(x[1], req) ? evalTerm(x[3], req) : evalTerm(x[5], req); + if (x[0] === 'project') return req[x[2]]; + if (x[1] === 'request') return evalTerm(term(defs[x[0]].body), req); + const [l, r] = [evalTerm(x[0], req), evalTerm(x[2], req)]; + return { '=?': l === r, ' (typeof x === 'string' ? 0 : x[0] === 'if' ? 1 + Math.max(nest(x[3]), nest(x[5])) : x[1] === 'request' && tallOwned.stepNames.includes(x[0]) ? nest(term(defs[x[0]].body)) : Math.max(0, ...x.map(nest))); + const tn = tm.names; + const codeOf = (f, v) => code(tn.fields[f].choices[v]); + const answerOf = Object.fromEntries(Object.entries(tn.answers).map(([o, d]) => [code(d), o])); + const everyTall = tall.app.fields.reduce((reqs, f) => reqs.flatMap((r) => f.values.map((v) => ({ ...r, [f.name]: v }))), [{}]); + ok(tallOwned.stepNames.length > tallOwned.steps.length, 'a branch still too deep is factored again'); + ok(nest(term(defs.judge.body)) <= MAX_DEPTH, `no path of the decision nests past ${MAX_DEPTH} ifs`); + ok(everyTall.every((r) => answerOf[evalTerm(term(defs.judge.body), Object.fromEntries(Object.entries(r).map(([f, v]) => [tn.fields[f].id, codeOf(f, v)])))] === judge(tm.spec, r).outcome), 'the factored decision answers as the first-match chain, for every request'); // A request takes up to sixteen fields (the prism-code-wide-requests patch, UOR-Foundation/PrismPM#679). const pad = (p, n) => ({ ...p, app: { ...p.app, fields: [...p.app.fields, ...Array.from({ length: n - p.app.fields.length }, (_, i) => ({ name: `pad${i}`, kind: 'choice', values: ['a', 'b'] }))], examples: p.app.examples.map((e) => ({ ...e, request: `${e.request}${' a'.repeat(n - p.app.fields.length)}` })) } }); ok(deriveModel(pad(rule30(), 16)).spec.fields.length === 16, 'a decision of sixteen fields is built'); @@ -330,6 +369,11 @@ const CASES = { const hostedIds = hosted.claims.map((c) => c.id); ok(hostedIds.includes('PCLI-01') && hostedIds.filter((id) => id === 'PCL-01').length === 0 && hosted.files['model/ids.toml'].split('id = "PCL-01"').length === 2, "a prefix another suite's rows hold is lengthened from the app's last word"); await refused(() => buildSet({ ...set, root: { module: 'host' } }, prompts, { lastModified: AT }), /root\.module/, 'a root is a module name'); + // Two apps of one set with the same initials: the first keeps them, the later one is lengthened from its last word. + const twin = readPrompt({ ...JSON.parse(readFileSync(join(examples, `${names[1]}.prompt.json`), 'utf8')), app: { ...JSON.parse(readFileSync(join(examples, `${names[1]}.prompt.json`), 'utf8')).app, name: 'prismpm-container-life' } }); + const twins = await buildSet({ ...set, prompts: [...set.prompts, 'twin.prompt.json'] }, [...prompts, twin], { lastModified: AT }); + const twinIds = twins.claims.map((c) => c.id); + ok(twinIds.includes('PCL-01') && twinIds.includes('PCLI-01') && new Set(twinIds).size === twinIds.length, "a prefix an earlier app of the set holds is lengthened from the later app's last word"); }, async 'DR-07'() { const good = rule30();