gdp-wired 0.26.0 → 0.27.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.
@@ -9,7 +9,7 @@
9
9
  // - a hypothesis' rows and open atoms, and every term inside them, are reused from an earlier path that decided the
10
10
  // same atoms they read (kernel.ts's reusing), so a new decision reads again only what it touches.
11
11
  import { walkOnce } from "../core/machine.js";
12
- import { add, env, k, refuteFast, scale, solve, sub, v, work } from "../core/solver.js";
12
+ import { add, costs, env, k, refuteFast, scale, solve, sub, v, work } from "../core/solver.js";
13
13
  import { checkRefutation } from "../core/check.js";
14
14
  import { allowNode, allowTranslated, certCyclic, certOn, certSearch, closingInsts, crossCyclic, cyclicOn, cycLog, hasQuantified, loopNodes, machineNodes, proveCyclic, reqTS, searchedStepNodes, skMachine, timedKind, withWitness, translatedProof, uncovered, uncoveredWhy } from "../core/cyclic.js";
15
15
  import { proveSome, someLaws } from "../core/exists.js";
@@ -95,43 +95,68 @@ function openH(c, path, out, positive = true) {
95
95
  // keys a pairing builds ((a + b)² + b, keys whose difference has monomials) are left uncompared: that encoding states
96
96
  // their congruence as facts, and comparing their keys only brings products into leaves its linear rows decide.
97
97
  export const pairedKeys = { skip: false };
98
- export function openCells(rows, path) {
99
- const names = [...new Set(rows.flatMap(r => Object.keys(r.lin)))].filter(isCell);
98
+ // (each cell's map and key, and each pair's difference both ways, made once: the pairs are quadratic in the names, and
99
+ // the same forms at every node keep what is remembered per form)
100
+ const cellKeys = new Map(), cellPairs = new Map();
101
+ export const cellPair = (a, b) => {
102
+ const pk = `${a}\u0000${b}`;
103
+ let pair = cellPairs.get(pk);
104
+ if (!pair) {
105
+ if (cellPairs.size > 200_000)
106
+ cellPairs.clear();
107
+ const d = sub(cellKey(a).key, cellKey(b).key);
108
+ cellPairs.set(pk, (pair = { d, neg: scale(d, -1) }));
109
+ }
110
+ return pair;
111
+ };
112
+ const cellKey = (n) => { let c = cellKeys.get(n); if (!c) {
113
+ if (cellKeys.size > 100_000)
114
+ cellKeys.clear();
115
+ cellKeys.set(n, (c = { map: n.slice(0, n.indexOf('[')), key: parseKey(n.slice(n.indexOf('[') + 1, -1)) }));
116
+ } return c; };
117
+ function openCells(rows, path) {
118
+ const seen = new Set(), names = [];
119
+ for (const r of rows)
120
+ for (const n in r.lin)
121
+ if (!seen.has(n)) {
122
+ seen.add(n);
123
+ if (isCell(n))
124
+ names.push(n);
125
+ }
100
126
  const out = [];
101
- const keyText = (n) => n.slice(n.indexOf('[') + 1, -1);
102
- // (each name's map and key read once: the pairs are quadratic in the names)
103
- const maps = names.map(n => n.slice(0, n.indexOf('['))), keys = names.map(n => parseKey(keyText(n)));
127
+ const cs = names.map(cellKey);
128
+ const dec = (lin) => path.get(canon(lin))?.holds ?? (path.has(canonFlip(lin)) ? !path.get(canonFlip(lin)).holds : undefined);
104
129
  for (let i = 0; i < names.length; i++)
105
130
  for (let j = i + 1; j < names.length; j++) {
106
- if (maps[i] !== maps[j])
131
+ if (cs[i].map !== cs[j].map)
107
132
  continue;
108
- const d = sub(keys[i], keys[j]);
133
+ const pair = cellPair(names[i], names[j]), d = pair.d;
109
134
  if (pairedKeys.skip && Object.keys(d).some(isMonomial))
110
135
  continue;
111
- const dec = (lin) => path.get(canon(lin))?.holds ?? (path.has(canonFlip(lin)) ? !path.get(canonFlip(lin)).holds : undefined);
112
136
  // Two cells of a run's map (core/cyclic.ts: time u and key k at u + k·Ω) at keys k·Ω and k'·Ω: k = k' first, a
113
137
  // linear atom; the keys' own difference only where k = k' (its other side then refuted by (k − k')·Ω = 0, a product).
114
- const q0 = cellProducts.on ? quotient(d) : undefined;
138
+ const q0 = cellProducts.on ? ('q0' in pair ? pair.q0 : (pair.q0 = quotient(d))) : undefined;
115
139
  if (q0) {
116
- const qa = dec(q0), qb = dec(scale(q0, -1));
140
+ const q1 = (pair.q1 ??= scale(q0, -1));
141
+ const qa = dec(q0), qb = dec(q1);
117
142
  if (qa === undefined) {
118
143
  out.push(q0);
119
144
  continue;
120
145
  }
121
146
  if (qa === true && qb === undefined) {
122
- out.push(scale(q0, -1));
147
+ out.push(q1);
123
148
  continue;
124
149
  }
125
150
  if (!(qa && qb))
126
151
  continue;
127
152
  cellDiffs.set(canon(d), q0);
128
- cellDiffs.set(canon(scale(d, -1)), scale(q0, -1));
153
+ cellDiffs.set(canon(pair.neg), q1);
129
154
  }
130
- const a = dec(d), b = dec(scale(d, -1));
155
+ const a = dec(d), b = dec(pair.neg);
131
156
  if (a === undefined)
132
157
  out.push(d);
133
158
  else if (a === true && b === undefined)
134
- out.push(scale(d, -1));
159
+ out.push(pair.neg);
135
160
  }
136
161
  return out;
137
162
  }
@@ -204,8 +229,8 @@ const proveTrace = !!env.PROVE_TRACE;
204
229
  // `why`, when the budget ran out: which limit, at what size, and what usually helps.
205
230
  // Set while core/cyclic.ts grows its goals.
206
231
  export const cellProducts = { on: false };
207
- // The budget: 200,000 nodes and, by default, a second per hypothesis (at least 20 seconds) per goal; past either the goal
208
- // is left `unknown`. A goal with many instances (a law about two keys at its pairs) has that many more rows per leaf.
232
+ // The budget: 200,000 nodes and, by default, a second per hypothesis (at least 20 seconds) per goal, on the counted
233
+ // clock inside `counted` and in the cost model's work outside it (`withWork`); past either the goal is left `unknown`. A goal with many instances (a law about two keys at its pairs) has that many more rows per leaf.
209
234
  // A deadline every goal grown inside `withDeadline` keeps, beside its own budget: a search that proves many things (core/
210
235
  // infer.ts, core/discover.ts, the search proveRule falls back on) bounds them all; a goal past it is left `unknown`.
211
236
  let deadline = Infinity;
@@ -213,14 +238,16 @@ let deadline = Infinity;
213
238
  // time, or within `counted` the work the prover has done (the solver's rows read and made, each node's rows), `perMs`
214
239
  // units a millisecond, so that what a search's budget lets it try, and so its verdict, doesn't depend on the machine's
215
240
  // speed. A counted search inside another reads the outer's clock.
216
- let rate = 0;
217
- export const clock = () => (rate ? work.n / rate : performance.now());
218
- export function counted(perMs, f) {
241
+ // With `full`, the clock reads the cost model below in place of work.n.
242
+ let rate = 0, meter = () => work.n;
243
+ export const clock = () => (rate ? meter() / rate : performance.now());
244
+ export function counted(perMs, f, full = false) {
219
245
  if (rate)
220
246
  return f();
221
247
  const outer = deadline;
222
248
  rate = perMs;
223
249
  deadline = Infinity;
250
+ meter = full ? fullWork : () => work.n;
224
251
  try {
225
252
  return f();
226
253
  }
@@ -229,6 +256,33 @@ export function counted(perMs, f) {
229
256
  deadline = outer;
230
257
  }
231
258
  }
259
+ // The cost model: what a query does, by kind (core/solver.ts's costs), each weighed to the milliseconds a Modal 2-vCPU
260
+ // container takes for it, `fullRate` units a millisecond at the median query: elimination's pairs and the float
261
+ // simplex's cells, the plain search's rows a node (kernel.ts's reading, whole at each node), the values reader.ts finds
262
+ // again, the live rows' copies it keeps and the read edges a change walks, the candidates scanned, the pivots' rows,
263
+ // the root's rows for units, the unfolding's rows a leaf, the goal's nodes where a query starts. work.n counts the rows
264
+ // the solver and each node read, which the incremental search reads whole no more: on 7,027 root queries of 20 ms or
265
+ // more (research/query/fit.py on runs/query/cal2: the 332 step goals, CHC sat and unsat, ITS, the false step goals and
266
+ // the real-world corpus), work.n did 28 to 377 units a ms (p10, p90), the model 120 to 401.
267
+ const model = [['fm', 2.33], ['fcells', 0.0123], ['plainRows', 7.93], ['values', 0.672], ['copies', 0.735], ['edges', 0.0435],
268
+ ['scan', 0.0765], ['pivotRows', 0.599], ['unitRows', 15.6], ['unfoldRows', 0.735], ['goalNodes', 0.492], ['translateRows', 7.93]];
269
+ export const fullRate = 250;
270
+ export const fullWork = () => { let n = 0; for (const [k, w] of model)
271
+ n += w * costs[k]; return n; };
272
+ // A goal's own budget where its caller gives none, outside a counted search: the cost model's work, groundRate units a
273
+ // millisecond of the budget (below fullRate: the queries of 10 s or more did 163 to 271 units a ms at their median, by
274
+ // group of the run above, so a long goal's budget takes about the wall time it had), so a goal left unknown is left so
275
+ // on any machine; a deadline it is grown within stays.
276
+ const groundRate = 180;
277
+ let workCap = Infinity;
278
+ const withWork = (cap, f) => { const outer = workCap; workCap = Math.min(outer, cap); try {
279
+ return f();
280
+ }
281
+ finally {
282
+ workCap = outer;
283
+ } };
284
+ // Past a budget's end on the clock, or past the work a goal's own budget allows.
285
+ export const late = (until) => clock() > until || (workCap !== Infinity && fullWork() > workCap);
232
286
  // A budget's end, within the deadline (a translation's own bound, core/cyclic.ts's, kept by the export's GDP_CERT_MS).
233
287
  export const withinDeadline = (until) => Math.min(until, deadline);
234
288
  export function withDeadline(until, f) {
@@ -243,7 +297,13 @@ export function withDeadline(until, f) {
243
297
  }
244
298
  // `units: false`: the goal's own order, no units first (a caller after a model, not a proof: a run guided by the
245
299
  // prover's witnesses, say, whose models units would steer).
246
- export function grow(goal, path = new Map(), budget = { nodes: 0, until: clock() + Math.max(20_000, 1_000 * goal.hyps.length) }) {
300
+ export function grow(goal, path = new Map(), budget) {
301
+ if (!budget) {
302
+ const ms = Math.max(20_000, 1_000 * goal.hyps.length);
303
+ if (!rate)
304
+ return withWork(fullWork() + groundRate * ms, () => grow(goal, path, { nodes: 0 }));
305
+ budget = { nodes: 0, until: clock() + ms };
306
+ }
247
307
  if (deadline < (budget.until ?? Infinity))
248
308
  budget.until = deadline;
249
309
  if (budget.units === false && unitsNow) {
@@ -295,7 +355,27 @@ export function withTrees(f) {
295
355
  treeCache.trees.clear();
296
356
  }
297
357
  }
358
+ // Each root query as it ends, for research's logs (research/query/log.ts sets it; off otherwise): the kind, the goal,
359
+ // its wall time and counted work, its outcome, and how many queries it is inside.
360
+ export const queryHook = { depth: 0 };
361
+ export function logged(kind, goal, f) {
362
+ const t0 = performance.now(), w0 = work.n;
363
+ queryHook.depth++;
364
+ try {
365
+ const out = f();
366
+ queryHook.on?.({ kind, goal, ms: performance.now() - t0, work: work.n - w0, out, depth: queryHook.depth - 1 });
367
+ return out;
368
+ }
369
+ finally {
370
+ queryHook.depth--;
371
+ }
372
+ }
298
373
  function growKept(goal, path, budget) {
374
+ if (queryHook.on && !path.size && !budget.nodes && !queryHook.depth)
375
+ return logged('grow', goal, () => growKept0(goal, path, budget));
376
+ return growKept0(goal, path, budget);
377
+ }
378
+ function growKept0(goal, path, budget) {
299
379
  if (!profile.on || path.size || budget.nodes)
300
380
  return growRoot(goal, path, budget);
301
381
  const t = performance.now(), s0 = profile.solves, m0 = profile.solveMs, r0 = profile.rowsMs, n0 = profile.nodes;
@@ -330,8 +410,8 @@ function growRoot(goal, path, budget) {
330
410
  }
331
411
  if (root) {
332
412
  if (cdcl) {
333
- const now = clock(), end = withinDeadline(budget.until ?? Infinity);
334
- const out = growCdcl(goal, path, { nodes: 0, until: end === Infinity ? end : now + cdclShare * (end - now) });
413
+ const now = clock(), end = withinDeadline(budget.until ?? Infinity), w = fullWork();
414
+ const out = withWork(workCap === Infinity ? Infinity : w + cdclShare * (workCap - w), () => growCdcl(goal, path, { nodes: 0, until: end === Infinity ? end : now + cdclShare * (end - now) }));
335
415
  // (with `realModels.on`, a model whose products don't multiply out is no model: the CDCL search reads a product as
336
416
  // a name of its own, where the plain search below multiplies rows; a goal whose products only its cases bring,
337
417
  // core/nonterm.ts's x' := x·y)
@@ -355,12 +435,14 @@ function growPlain(goal, path, budget) {
355
435
  return { stuck: 'unknown', path, why: 'cap' };
356
436
  if (profile.on)
357
437
  profile.nodes++;
358
- if (++budget.nodes > 200_000 || (budget.until !== undefined && clock() > budget.until))
438
+ if (++budget.nodes > 200_000 || late(budget.until ?? Infinity))
359
439
  return { stuck: 'unknown', path, why: outOfBudget(goal, budget.nodes > 200_000 ? 'nodes' : 'time', path.size) };
360
440
  // Rows that read a term column only once a cut has put one on the path; before, the rows a choice-free search uses
361
441
  // (what it saw before term columns), renumbered to the whole leaf's when a leaf is found.
362
442
  const full = timed(() => rowsAt(goal, path), ms => { profile.rowsMs += ms; });
363
443
  work.n += 1 + full.length;
444
+ costs.plainNodes++;
445
+ costs.plainRows += full.length;
364
446
  const columns = [...path.values()].some(a => Object.keys(a.lin).some(key => key.startsWith('⟦')));
365
447
  const idx = full.flatMap((r, i) => (columns || !Object.keys(r.lin).some(key => key.startsWith('⟦')) ? [i] : []));
366
448
  const rows = idx.map(i => full[i]), at = (i) => (i < rows.length ? idx[i] : full.length + i - rows.length);
@@ -770,7 +852,7 @@ const nearSeen = new WeakMap();
770
852
  // (the names a term reads, its shared parts walked once: a witness read at each of its uses is one object)
771
853
  const namesOf = (t) => {
772
854
  const out = new Set();
773
- walkOnce(t, o => { for (const [key, v] of Object.entries(o))
855
+ walkOnce(t, o => { costs.goalNodes++; for (const [key, v] of Object.entries(o))
774
856
  if ((key === 'name' || key === 'map') && typeof v === 'string')
775
857
  out.add(v); });
776
858
  return out;
@@ -799,6 +881,7 @@ function unitsOf(goal) {
799
881
  // Forced by the root's rows without a solver: a row the atom is (or is the negation of), or the bounds the rows give
800
882
  // single variables (x <= c, x >= c) deciding it whichever way its variables go.
801
883
  const root = new Map(), rows = leafConstraints(goal, root), seen = new Set(), given = new Set();
884
+ costs.unitRows += rows.length;
802
885
  const lo = new Map(), hi = new Map();
803
886
  for (const r of rows) {
804
887
  given.add(canon(r.lin));
@@ -1281,8 +1364,19 @@ function triggered(m, laws, inst, build, focus = []) {
1281
1364
  // found that its leaves refute with, the proof grown again with those alone. The checked goal is then built from far
1282
1365
  // fewer hypotheses (a law about pairs of items with 900 instance hypotheses and 30 leaves using a few of them, say).
1283
1366
  export const minimal = { on: false };
1367
+ export const leafRowsOf = new WeakMap();
1284
1368
  // The hypotheses a tree's leaves read: each leaf's input rows, and the rows its products multiply.
1285
1369
  export function usedHyps(goal, tree, path = new Map(), out = new Set()) {
1370
+ const known = 'leaf' in tree && !tree.products && !tree.squares ? leafRowsOf.get(tree) : undefined;
1371
+ if (known && 'leaf' in tree) {
1372
+ for (const st of tree.leaf)
1373
+ if (st.kind === 'input') {
1374
+ const s = known.at.get(st.index)?.src;
1375
+ if (typeof s === 'number' && s >= 0)
1376
+ out.add(s);
1377
+ }
1378
+ return out;
1379
+ }
1286
1380
  if ('leaf' in tree) {
1287
1381
  const rows = rowsAt(goal, path), n = rows.length, sq = tree.squares?.length ?? 0;
1288
1382
  const mark = (i) => {
@@ -43,6 +43,7 @@ export type Row = {
43
43
  label: '';
44
44
  };
45
45
  export declare function times(a: Lin, b: Lin): Lin | undefined;
46
+ export declare const keepClean: boolean;
46
47
  export declare class Reader {
47
48
  readonly goal: Goal;
48
49
  readonly trail: Trail;
@@ -57,17 +58,17 @@ export declare class Reader {
57
58
  begin(): void;
58
59
  private up;
59
60
  private byAtom;
61
+ private found;
62
+ private lastUp;
60
63
  private clean;
61
- private frame;
62
64
  private factKeys;
63
65
  private openKeys;
64
- private lastUp;
66
+ private finding;
65
67
  private read;
66
68
  private readAtom;
67
69
  private within;
68
70
  touch(name: string): void;
69
71
  private ok;
70
- private kept;
71
72
  private units;
72
73
  private unit;
73
74
  dec(lin: Lin): R & {
@@ -80,11 +81,79 @@ export declare class Reader {
80
81
  private truth0;
81
82
  private factsOf;
82
83
  facts(i: number): FV;
84
+ private factsFin;
85
+ private partsOf;
86
+ private ps;
87
+ private parts;
88
+ private part;
83
89
  termRows(name: string): Row[];
84
90
  private decided;
85
91
  rows(): Row[];
86
- leafRows(): Row[];
87
- open(i: number, out: Lin[]): void;
92
+ private forms;
93
+ private formIds;
94
+ private nameIds;
95
+ private nameText;
96
+ private nameKind;
97
+ private formSeen;
98
+ private formAt;
99
+ private nameSeen;
100
+ private termSeen;
101
+ private gen;
102
+ form(lin: Lin): {
103
+ id: number;
104
+ names: number[];
105
+ };
106
+ readsColumn(lin: Lin): boolean;
107
+ private trailRows;
108
+ private trailStamps;
109
+ private pairs;
110
+ leafRows(src?: (number | [string, string])[]): Row[];
111
+ live: Row[];
112
+ entered: number[];
113
+ left: number[];
114
+ private syncing;
115
+ private dirty;
116
+ private slot;
117
+ private copies;
118
+ private c12;
119
+ private cT;
120
+ private inTrail;
121
+ private inParts;
122
+ private hypsIn;
123
+ private src;
124
+ hypsOf(rows: Row[]): number[];
125
+ private inSeg;
126
+ private cached;
127
+ private termKeys;
128
+ private pairKeys;
129
+ private liveCells;
130
+ private inNat;
131
+ private syncN;
132
+ private markN;
133
+ private wasLive;
134
+ private changed;
135
+ slotOf(id: number): number;
136
+ hasCells(): boolean;
137
+ private cellsOn;
138
+ readonly cells: Map<string, Set<string>>;
139
+ cellsChanged: number;
140
+ sync(): void;
141
+ private trailRow;
142
+ private segRows;
143
+ private pair;
144
+ private mark;
145
+ private addCopy;
146
+ private removeCopy;
147
+ private countUp;
148
+ private countDown;
149
+ private nameOn;
150
+ private nameOff;
151
+ private segOn;
152
+ private segOff;
153
+ private pairKey;
154
+ private cellOn;
155
+ private cellOff;
156
+ openList(i: number): Lin[];
88
157
  private openE;
89
158
  private openC;
90
159
  private openH;