gdp-wired 0.41.1 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -305,6 +305,10 @@ export function withWall(end, f) {
305
305
  wallEnd = outer;
306
306
  }
307
307
  }
308
+ // Past the wall or the deadline in force: a goal grown now is left unknown at once.
309
+ const ranOut = () => pastWall() || withinDeadline(Infinity) <= clock();
310
+ // (the wall's end in force, Infinity where none: a search inside that stops itself there)
311
+ export const wallAt = () => wallEnd;
308
312
  export const late = (until) => clock() > until || (workCap !== Infinity && fullWork() > workCap) || pastWall();
309
313
  // A budget's end, within the deadline (a translation's own bound, core/cyclic.ts's, kept by the export's GDP_CERT_MS).
310
314
  export const withinDeadline = (until) => Math.min(until, deadline);
@@ -1458,18 +1462,60 @@ const withReads = (laws, inst, keys, focus, byMap, fns = []) => {
1458
1462
  return { ...inst, ...Object.fromEntries(laws.filter(l => l.forall).map(l => {
1459
1463
  const had = inst[l.name] ?? [];
1460
1464
  if (l.keys?.length) {
1461
- const n = l.keys.length + 1, out = new Map(), sorted = byMap ? keySorted(l, byMap, fns) : undefined;
1465
+ const n = l.keys.length + 1, out = new Map();
1462
1466
  for (let i = 0; i + n <= had.length; i += n)
1463
1467
  out.set(JSON.stringify(had.slice(i, i + n)), had.slice(i, i + n));
1464
- for (const tuple of [...related(keys, focus, n, sorted, most), ...matched(l, keys, most)])
1465
- if (!sorted || sorted(tuple))
1468
+ // (a key the law's hypothesis fixes by an equation, x + c = t: the tuples of the others, x at t ∸ c in each)
1469
+ const { law: lr, back } = fixedOut(l), m2 = lr.keys?.length ?? 0, sorted = byMap ? keySorted(lr, byMap, fns) : undefined;
1470
+ const tuples = m2 ? [...related(keys, focus, m2 + 1, sorted, most), ...matched(lr, keys, most)] : related(keys, focus, 1, sorted, most);
1471
+ for (const t0 of tuples)
1472
+ if (!sorted || sorted(t0)) {
1473
+ const tuple = back(t0);
1466
1474
  out.set(JSON.stringify(tuple), tuple);
1475
+ }
1467
1476
  return [l.name, [...out.values()].flat()];
1468
1477
  }
1469
1478
  const seen = new Set(had.map(e => JSON.stringify(e)));
1470
1479
  return [l.name, [...had, ...keys.filter(e => { const id = JSON.stringify(e); return seen.has(id) ? false : (seen.add(id), true); })]];
1471
1480
  })) };
1472
1481
  };
1482
+ // A law's keys that its hypothesis fixes by an equation (x + c = t, or t = x, t reading no fixed key): the law without
1483
+ // them (each read at t ∸ c, c ≤ t a further hypothesis: the same law over the naturals), and `back`, a tuple of its keys
1484
+ // made a tuple of the law's (each fixed key at its t). A law with five keys of which two a cell fixes (Table[a·N + k] =
1485
+ // v + 1) has its instances over three. Untrusted: it chooses instances only.
1486
+ const fixedMemo = new WeakMap();
1487
+ function fixedOut(l) {
1488
+ let out = fixedMemo.get(l);
1489
+ if (!out)
1490
+ fixedMemo.set(l, (out = fixedOut0(l)));
1491
+ return out;
1492
+ }
1493
+ function fixedOut0(l) {
1494
+ const all = [l.forall, ...(l.keys ?? [])], same = { law: l, back: (t) => t };
1495
+ const hyps = l.holds.op === 'or' ? l.holds.xs.flatMap(d => (d.op === 'not' ? (d.x.op === 'and' ? d.x.xs : [d.x]) : [])) : [];
1496
+ const named = (e, x) => JSON.stringify(e).includes(`"name":${JSON.stringify(x)}`);
1497
+ const offset = (e) => (e.op === 'var' && all.includes(e.name) ? [e.name, 0] : e.op === 'add' && e.a.op === 'var' && all.includes(e.a.name) && e.b.op === 'const' ? [e.a.name, e.b.value] : undefined);
1498
+ const fixed = new Map();
1499
+ for (const h of hyps) {
1500
+ if (h.op !== '=')
1501
+ continue;
1502
+ for (const [x, t] of [[h.a, h.b], [h.b, h.a]]) {
1503
+ const o = offset(x);
1504
+ if (!o || o[0] === l.forall || fixed.has(o[0]) || named(t, o[0]) || [...fixed.keys()].some(f => named(t, f)) || [...fixed.values()].some(f => named(f.t, o[0])))
1505
+ continue;
1506
+ fixed.set(o[0], { t, c: o[1] });
1507
+ break;
1508
+ }
1509
+ }
1510
+ if (!fixed.size)
1511
+ return same;
1512
+ const at = (x) => { const { t, c } = fixed.get(x); return c ? { op: 'sub', a: t, b: { op: 'const', value: c } } : t; };
1513
+ const sub0 = { vars: Object.fromEntries([...fixed.keys()].map(x => [x, at(x)])), maps: {}, flags: {} };
1514
+ const guards = [...fixed.values()].flatMap(({ t, c }) => (c ? [{ op: '<=', a: { op: 'const', value: c }, b: t }] : []));
1515
+ const kept = all.filter(x => !fixed.has(x)), holds = substC(guards.length ? { op: 'or', xs: [{ op: 'not', x: { op: 'and', xs: guards } }, l.holds] } : l.holds, sub0);
1516
+ const law = { ...l, holds, keys: kept.slice(1) };
1517
+ return { law, back: t => { const s2 = { vars: Object.fromEntries(kept.map((x, i) => [x, t[i]])), maps: {}, flags: {} }; return all.map(x => (fixed.has(x) ? substE(at(x), s2) : t[kept.indexOf(x)])); } };
1518
+ }
1473
1519
  // A tuple of the key's sort: each of a law's keys read at a map (M[x], M[x·N + y]) takes only the terms its read matches
1474
1520
  // against the keys the goals read that map at (the key's sort, as the goal holds it: Link[c1·N + s] against
1475
1521
  // Link[a·N + b] gives c1 a and s b); a key no read binds, or a read no goal key matches, takes any. A function's
@@ -2185,8 +2231,21 @@ export function proveLaw(m, laws, i) {
2185
2231
  function proveOne(m, laws, i) {
2186
2232
  const law = laws[i], pm = plainOf(m), inst = lawInstances(m, laws, i);
2187
2233
  const few = triggered(pm, laws, inst, i2 => obligations(m, laws, { [law.name]: i2 }, i), law.forall ? [`${law.forall}#`] : []);
2188
- if (!sameInstances(few, inst) && obligations(m, laws, { [law.name]: few }, i).every(goal => 'tree' in grow(goal)))
2189
- return { proven: true };
2234
+ if (!sameInstances(few, inst)) {
2235
+ let proven = true;
2236
+ for (const goal of obligations(m, laws, { [law.name]: few }, i)) {
2237
+ const o = grow(goal);
2238
+ if ('tree' in o)
2239
+ continue;
2240
+ proven = false;
2241
+ // (a goal that ran out past the deadline: the next instances can't be tried, and it is the verdict's goal)
2242
+ if (o.stuck === 'unknown' && ranOut())
2243
+ return { proven: false, goal: goal.name, stuck: 'unknown' };
2244
+ break;
2245
+ }
2246
+ if (proven)
2247
+ return { proven: true };
2248
+ }
2190
2249
  // (a model breaking a law at a tuple of the goal's keys: those added, the law asked again)
2191
2250
  const { result: a } = withModels(m, laws, inst, i2 => obligations(m, laws, { [law.name]: i2 }, i), growAll, grownModel, grownSettled);
2192
2251
  return a.out ? { proven: false, goal: a.goal.name, stuck: a.out.stuck, ...(a.out.witness ? { witness: a.out.witness } : {}), ...(a.out.diagram ? { diagram: a.out.diagram } : {}) } : { proven: true };
@@ -2482,15 +2541,17 @@ function thinned(m, laws, instances) {
2482
2541
  const trees = {}, fulls = { ...instances };
2483
2542
  let nodes = 0, leaves = 0;
2484
2543
  for (const [i, law] of laws.entries()) {
2485
- const full = instances[law.name];
2486
- const few = triggered(plainOf(m), laws, full, inst => obligations(m, laws, { ...instances, [law.name]: inst }, i), law.forall ? [`${law.forall}#`] : []);
2544
+ const full = instances[law.name], focus = law.forall ? [`${law.forall}#`] : [];
2545
+ const few = triggered(plainOf(m), laws, full, inst => obligations(m, laws, { ...instances, [law.name]: inst }, i), focus);
2487
2546
  // (thinned instances first, then the full ones; each asked again at the tuples its models ask for)
2488
2547
  for (const inst of sameInstances(few, full) ? [full] : [few, full]) {
2489
2548
  const { result: a, inst: at } = withModels(m, laws, inst, i2 => obligations(m, laws, { ...instances, [law.name]: i2 }, i), growAll, grownModel, grownSettled);
2490
2549
  instances[law.name] = at;
2491
2550
  if (inst === full)
2492
2551
  fulls[law.name] = at;
2493
- if (a.out && inst !== full)
2552
+ // (one that ran out past the wall: the next instances can't be tried, and the goal that ran out is the verdict's, not
2553
+ // the next one's first goal)
2554
+ if (a.out && inst !== full && !(a.out.stuck === 'unknown' && ranOut()))
2494
2555
  continue;
2495
2556
  if (a.out)
2496
2557
  return stuckAt(a.goal, a.out, nodes, leaves);
@@ -3,7 +3,7 @@ import { add, k, scale, sub } from "../core/solver.js";
3
3
  import { countedGoal, countedObligations, countHyps, mcGoal, postName, boundOf, caseGoals, check, checkRecur, checkRecurCases, coverGoal, recurGoal, canon, decided, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, truth, historyMachine, sumMachine, sumCovers, sumParts, foldObligations, kInductionStart, kInductionSteps, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessLevels, livenessRanks, strongly, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
4
4
  import { chooseLevels, clock, grow, minimal, proveBuilt, proveLaws, proveSetFast, proveStepLaws, withDeadline } from "../kernel/prove.js";
5
5
  import { refinement, refinementSides } from "../core/compose.js";
6
- import { leanSurface, leanTerms } from "./lean-terms.js";
6
+ import { exactNat, leanSurface, leanTerms } from "./lean-terms.js";
7
7
  import { descentGoal, mainGoal, premiseGoal } from "../kernel/cyclic.js";
8
8
  import { allowNode, certMs, closingInsts, reqTS, withWitness, foldNodes, certMode, quietly, uncoveredWhy, hasQuantified, kWhens, machineNodes, proveCyclic, routeOf, searchedStepNodes, skMachine, stepLawNodes, tBack, tSub } from "../core/cyclic.js";
9
9
  import { leanLibrary } from "./library.js";
@@ -908,7 +908,7 @@ export function mmachineToLean(id, m, laws, opts = {}) {
908
908
  const ix = (name) => vars.indexOf(name);
909
909
  const lit = (mm) => `⟨⟨${list(mm.inputs.map(name => String(ix(name))))},
910
910
  ${list(Object.entries(mm.state).map(([name, e]) => `(${ix(name)}, ${any0.e(e)})`))},
911
- ${list(Object.entries(mm.maps ?? {}).map(([name, c2]) => `(${mapNames.indexOf(name)}, ${c2})`))},
911
+ ${list(Object.entries(mm.maps ?? {}).map(([name, c2]) => `(${mapNames.indexOf(name)}, ${exactNat(c2)})`))},
912
912
  ${list(Object.entries(mm.step.state ?? {}).map(([name, e]) => `(${ix(name)}, ${any0.e(e)})`))},
913
913
  ${list(Object.entries(mm.step.maps ?? {}).map(([name, w]) => { const pw = pointWrite(w, 'Lean (for now)'); return `(${mapNames.indexOf(name)}, ${any0.e(pw.key)}, ${any0.e(pw.value)})`; }))},
914
914
  ${mm.pre ? any0.c(mm.pre) : TRUE},
@@ -1004,7 +1004,7 @@ export function machineLit(mm, t, ix, mx) {
1004
1004
  const fl = flagsOfM(mm), fs = Object.entries(mm.step.flags ?? {}), ws = Object.entries(mm.step.maps ?? {});
1005
1005
  return `⟨⟨⟨${list(mm.inputs.map(name => String(ix(name))))},
1006
1006
  ${list(Object.entries(mm.state).map(([name, e2]) => `(${ix(name)}, ${e(e2)})`))},
1007
- ${list(Object.entries(mm.maps ?? {}).map(([name, c2]) => `(${mx(name)}, ${c2})`))},
1007
+ ${list(Object.entries(mm.maps ?? {}).map(([name, c2]) => `(${mx(name)}, ${exactNat(c2)})`))},
1008
1008
  ${list(Object.entries(mm.step.state ?? {}).map(([name, e2]) => `(${ix(name)}, ${e(e2)})`))},
1009
1009
  ${list(ws.flatMap(([name, w]) => ('each' in w ? [] : [`(${mx(name)}, ${e(w.key)}, ${e(w.value)})`])))},
1010
1010
  ${mm.pre ? c(mm.pre) : TRUE},
@@ -1,4 +1,5 @@
1
1
  import { type C, type E } from '../core/machine.js';
2
+ export declare const exactNat: (x: number) => string;
2
3
  export declare function leanTerms(vars: readonly string[], maps: readonly string[], where: string): {
3
4
  e: (t: E) => string;
4
5
  c: (t: C) => string;
@@ -3,6 +3,10 @@
3
3
  // numbered by their place in `vars` and `maps`; a flag is the variable ?flag read as `= 1` (Progress.lean inlines it).
4
4
  // A name with no number fails here, never as a malformed index in the Lean text.
5
5
  import { unreachable } from "../core/machine.js";
6
+ // A number as Lean reads it: the exact integer JavaScript holds. (Past 2^53 a JavaScript number prints as the shortest
7
+ // decimal that reads back to it, 10^18 + 128 as 1000000000000000100: Lean would read another number than the kernel and
8
+ // the runtimes do. terra's review of gen/laneQ.)
9
+ export const exactNat = (x) => (Number.isSafeInteger(x) ? String(x) : BigInt(x).toString());
6
10
  export function leanTerms(vars, maps, where) {
7
11
  const ix = (name) => { const j = vars.indexOf(name); if (j < 0)
8
12
  throw new Error(`${where}: no variable for ${name}`); return j; };
@@ -11,8 +15,8 @@ export function leanTerms(vars, maps, where) {
11
15
  const e = (t) => {
12
16
  switch (t.op) {
13
17
  case 'var': return `.var ${ix(t.name)}`;
14
- case 'const': return `.const ${t.value}`;
15
- case 'mul': return `.mul ${t.k} (${e(t.a)})`;
18
+ case 'const': return `.const ${exactNat(t.value)}`;
19
+ case 'mul': return `.mul ${exactNat(t.k)} (${e(t.a)})`;
16
20
  case 'add':
17
21
  case 'sub':
18
22
  case 'min':
@@ -50,8 +54,8 @@ export function leanSurface(names, where, get) {
50
54
  const e = (t) => {
51
55
  switch (t.op) {
52
56
  case 'var': return name(t.name);
53
- case 'const': return String(t.value);
54
- case 'mul': return `(${t.k} * ${e(t.a)})`;
57
+ case 'const': return exactNat(t.value);
58
+ case 'mul': return `(${exactNat(t.k)} * ${e(t.a)})`;
55
59
  case 'add': return `(${e(t.a)} + ${e(t.b)})`;
56
60
  case 'sub': return `(${e(t.a)} - ${e(t.b)})`;
57
61
  case 'prod': return `(${e(t.a)} * ${e(t.b)})`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdp-wired",
3
- "version": "0.41.1",
3
+ "version": "0.43.0",
4
4
  "description": "An automatic prover for stateful rules (budgets, caps, windows, workflows, policies): laws proven with no proof to write, checked again in Lean, counterexamples found on the GPU, the rule shipped as code",
5
5
  "keywords": [
6
6
  "verification",
@@ -185,7 +185,7 @@
185
185
  "prepublishOnly": "bun scripts/build.ts",
186
186
  "smoke": "sh scripts/smoke.sh",
187
187
  "bar": "bun gates/bar.ts",
188
- "test": "bun scripts/gen-lean-library.ts --check && cd core && bun test exports.test.ts cli.test.ts solver.test.ts dsl.test.ts sums.test.ts arith.test.ts conformance.test.ts compose.test.ts relational.test.ts quantifiers.test.ts functions.test.ts keyterms.test.ts lists.test.ts liveness.test.ts sizes.test.ts values.test.ts rationals.test.ts builders.test.ts gdp.test.ts datatypes.test.ts lemmas.test.ts discover.test.ts infer.test.ts cyclic.test.ts cycles.test.ts nonterm.test.ts data.test.ts hypothesis.test.ts seqs.test.ts abstract.test.ts search.test.ts invoices.test.ts folds.test.ts trees.test.ts pairs.test.ts chart.test.ts xstate.test.ts extract.test.ts effect.test.ts && cd ../kernel && bun test tamper.test.ts refusals.test.ts steplaw.test.ts sets.test.ts cdcl.test.ts && cd ../examples && bun test examples.test.ts && cd .. && bun examples/readme.ts && bun examples/guide.ts && bun examples/composition.ts && bun examples/sets.ts && bun examples/quantifiers.ts && bun examples/functions.ts && bun examples/datatypes.ts && bun examples/lemmas.ts && bun examples/discover.ts && bun examples/invoices.ts && bun examples/folds.ts && bun examples/trees.ts && bun examples/pairs.ts && bun examples/groups.ts && bun examples/charts.ts && bun examples/xstate.ts && bun examples/effect.ts && bun examples/infer.ts && bun examples/arith.ts && bun examples/cyclic.ts && bun examples/cycles.ts && bun examples/data.ts && bun examples/abstract.ts && bun test compare/tip/translate.test.ts && bun examples/scopes.ts && bun examples/lists.ts && bun examples/sizes.ts && bun examples/liveness.ts && bun examples/sums.ts && bun examples/nonterm.ts && bun examples/rationals.ts && bun examples/budget.ts && bun examples/gdp.ts && bun examples/steps.ts && bun examples/jev-fabric/run.ts"
188
+ "test": "bun scripts/gen-lean-library.ts --check && cd core && bun test exports.test.ts cli.test.ts solver.test.ts dsl.test.ts sums.test.ts arith.test.ts conformance.test.ts compose.test.ts relational.test.ts quantifiers.test.ts functions.test.ts keyterms.test.ts lists.test.ts liveness.test.ts sizes.test.ts values.test.ts rationals.test.ts builders.test.ts gdp.test.ts datatypes.test.ts lemmas.test.ts discover.test.ts infer.test.ts cyclic.test.ts cycles.test.ts nonterm.test.ts data.test.ts hypothesis.test.ts seqs.test.ts abstract.test.ts search.test.ts invoices.test.ts folds.test.ts trees.test.ts pairs.test.ts chart.test.ts xstate.test.ts extract.test.ts effect.test.ts effect-schedule.test.ts effect-stm.test.ts && cd ../kernel && bun test tamper.test.ts refusals.test.ts steplaw.test.ts sets.test.ts cdcl.test.ts && cd ../examples && bun test examples.test.ts && cd .. && bun examples/readme.ts && bun examples/guide.ts && bun examples/composition.ts && bun examples/sets.ts && bun examples/quantifiers.ts && bun examples/functions.ts && bun examples/datatypes.ts && bun examples/lemmas.ts && bun examples/discover.ts && bun examples/invoices.ts && bun examples/folds.ts && bun examples/trees.ts && bun examples/pairs.ts && bun examples/groups.ts && bun examples/charts.ts && bun examples/xstate.ts && bun examples/effect.ts && bun examples/effect-retry.ts && bun examples/effect-stm.ts && bun examples/infer.ts && bun examples/arith.ts && bun examples/cyclic.ts && bun examples/cycles.ts && bun examples/data.ts && bun examples/abstract.ts && bun test compare/tip/translate.test.ts && bun examples/scopes.ts && bun examples/lists.ts && bun examples/sizes.ts && bun examples/liveness.ts && bun examples/sums.ts && bun examples/nonterm.ts && bun examples/rationals.ts && bun examples/budget.ts && bun examples/gdp.ts && bun examples/steps.ts && bun examples/jev-fabric/run.ts"
189
189
  },
190
190
  "devDependencies": {
191
191
  "@gdp-ts/core": "0.1.0",