gdp-wired 0.25.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.
Files changed (52) hide show
  1. package/README.md +14 -9
  2. package/dist/core/accel.d.ts +20 -1
  3. package/dist/core/accel.js +797 -220
  4. package/dist/core/bmc.d.ts +12 -1
  5. package/dist/core/bmc.js +275 -106
  6. package/dist/core/cycles.d.ts +2 -3
  7. package/dist/core/cycles.js +35 -131
  8. package/dist/core/cyclic.d.ts +4 -1
  9. package/dist/core/cyclic.js +103 -74
  10. package/dist/core/data.d.ts +1 -1
  11. package/dist/core/data.js +89 -118
  12. package/dist/core/discover.js +65 -27
  13. package/dist/core/dsl.d.ts +3 -2
  14. package/dist/core/dsl.js +29 -25
  15. package/dist/core/exists.js +7 -14
  16. package/dist/core/extract-check.d.ts +55 -4
  17. package/dist/core/extract-check.js +248 -122
  18. package/dist/core/extract-print.d.ts +1 -0
  19. package/dist/core/extract-print.js +26 -3
  20. package/dist/core/extract-read.d.ts +98 -4
  21. package/dist/core/extract-read.js +306 -43
  22. package/dist/core/extract-shapes.d.ts +14 -0
  23. package/dist/core/extract-shapes.js +609 -79
  24. package/dist/core/extract-xstate.d.ts +5 -1
  25. package/dist/core/extract-xstate.js +618 -184
  26. package/dist/core/extract.d.ts +1 -0
  27. package/dist/core/extract.js +81 -30
  28. package/dist/core/infer.d.ts +1 -0
  29. package/dist/core/infer.js +126 -64
  30. package/dist/core/machine.d.ts +6 -0
  31. package/dist/core/machine.js +35 -4
  32. package/dist/core/nonterm.d.ts +5 -1
  33. package/dist/core/nonterm.js +133 -45
  34. package/dist/core/q.d.ts +1 -0
  35. package/dist/core/q.js +2 -2
  36. package/dist/core/relation.d.ts +1 -0
  37. package/dist/core/relation.js +20 -36
  38. package/dist/core/solver.d.ts +33 -0
  39. package/dist/core/solver.js +84 -30
  40. package/dist/core/transfer.js +22 -13
  41. package/dist/core/translate.d.ts +1 -1
  42. package/dist/core/translate.js +88 -65
  43. package/dist/core/xstate.d.ts +6 -1
  44. package/dist/core/xstate.js +336 -55
  45. package/dist/kernel/cdcl.js +212 -79
  46. package/dist/kernel/prove.d.ts +36 -5
  47. package/dist/kernel/prove.js +212 -104
  48. package/dist/kernel/reader.d.ts +75 -5
  49. package/dist/kernel/reader.js +533 -76
  50. package/dist/kernel/simplex.d.ts +27 -2
  51. package/dist/kernel/simplex.js +160 -14
  52. package/package.json +3 -2
@@ -1,4 +1,4 @@
1
- import type { Lin } from '../core/solver.js';
1
+ import { type Lin } from '../core/solver.js';
2
2
  export type SimplexVerdict = {
3
3
  kind: 'sat';
4
4
  } | {
@@ -9,7 +9,8 @@ export type SimplexVerdict = {
9
9
  };
10
10
  export declare class Simplex {
11
11
  readonly integral: boolean;
12
- constructor(integral?: boolean);
12
+ readonly boundsOnly: boolean;
13
+ constructor(integral?: boolean, boundsOnly?: boolean);
13
14
  private index;
14
15
  private lo;
15
16
  private hi;
@@ -20,12 +21,36 @@ export declare class Simplex {
20
21
  private rows;
21
22
  private colOf;
22
23
  pivots: number;
24
+ private nnzN;
25
+ private at;
26
+ private place;
23
27
  private varOf;
24
28
  private slackOf;
25
29
  load(rows: {
26
30
  lin: Lin;
27
31
  strict?: boolean;
28
32
  }[]): SimplexVerdict;
33
+ private ins;
34
+ private inLo;
35
+ private inHi;
36
+ private falseIn;
37
+ private crossing;
38
+ private moved;
39
+ private movedAt;
40
+ private settles;
41
+ assert(id: number, lin: Lin, strict?: boolean): void;
42
+ retract(id: number): void;
43
+ changes: number;
44
+ private move;
45
+ private vers;
46
+ version(j: number): number;
47
+ value(j: number): number;
48
+ settle(): SimplexVerdict;
49
+ indexOf(name: string): number | undefined;
50
+ low(j: number): number;
51
+ high(j: number): number;
52
+ lowWhy(j: number): number;
53
+ highWhy(j: number): number;
29
54
  private shift;
30
55
  check(maxPivots?: number): SimplexVerdict;
31
56
  private pivot;
@@ -1,7 +1,19 @@
1
+ // Untrusted: an incremental simplex over the rows a search reads at its nodes (each row lin <= 0), in floating point,
2
+ // as an SMT solver's arithmetic does it (Dutertre and de Moura's general form). A row of one variable is a bound on
3
+ // it; any other row is a bound on a slack, one per linear form up to a factor (each slack a basic variable of the
4
+ // tableau, its form over the nonbasic ones). `load` makes a node's rows the bounds (the tableau and the basis are kept
5
+ // from the node before, so a node that differs a little from the last costs a few pivots, not an elimination);
6
+ // `check` pivots until every basic variable is within its bounds (Bland's rule) or a row can't be: its explanation,
7
+ // the rows whose bounds it reads, is a rational refutation's support. Floating point only picks the rows: the caller
8
+ // refutes them exactly (core/solver.ts) and the kernel checks that; a verdict the floats misjudge costs a fallback.
9
+ // Bounds are rounded to integers (every variable is a natural, every form an integer combination): what a cut of the
10
+ // row by its coefficients' divisor gives, which the exact elimination makes as well. Over the rationals (`integral`
11
+ // false) nothing is rounded, and a strict row's bound is moved in by a hair (1e-7, relative): the floats only pick a
12
+ // conflict's rows, which the exact elimination then refutes, strictness and all, or doesn't.
13
+ import { costs, gcd } from "../core/solver.js";
1
14
  const eps = 1e-9;
2
15
  // A bound's tolerance: relative past 1 (values drift by a few ulps a pivot).
3
16
  const tol = (b) => 1e-7 * Math.max(1, Math.abs(b));
4
- const gcd = (a, b) => (b === 0 ? Math.abs(a) : gcd(b, a % b));
5
17
  const formMemo = new WeakMap();
6
18
  function formOf(lin) {
7
19
  let out = formMemo.get(lin);
@@ -26,8 +38,11 @@ function formOf(lin) {
26
38
  }
27
39
  export class Simplex {
28
40
  integral;
29
- constructor(integral = true) {
41
+ boundsOnly;
42
+ // `boundsOnly`: only the bounds are kept (assert, retract and their versions; no settle or check).
43
+ constructor(integral = true, boundsOnly = false) {
30
44
  this.integral = integral;
45
+ this.boundsOnly = boundsOnly;
31
46
  }
32
47
  index = new Map();
33
48
  lo = [];
@@ -41,6 +56,18 @@ export class Simplex {
41
56
  rows = [];
42
57
  colOf = [];
43
58
  pivots = 0;
59
+ nnzN = 0;
60
+ at = new WeakMap();
61
+ // (a row's variable and coefficient, kept per form)
62
+ place(lin) {
63
+ let at = this.at.get(lin);
64
+ if (at === undefined) {
65
+ const f = formOf(lin);
66
+ at = f === null ? null : 'single' in f ? { j: this.varOf(f.single), c: f.c } : { j: this.slackOf(f), c: f.g };
67
+ this.at.set(lin, at);
68
+ }
69
+ return at;
70
+ }
44
71
  varOf(name) {
45
72
  let j = this.index.get(name);
46
73
  if (j !== undefined)
@@ -76,6 +103,7 @@ export class Simplex {
76
103
  }
77
104
  const s = this.varOf(f.slack), r = this.rows.length;
78
105
  this.rows.push({ basic: s, coef });
106
+ this.nnzN += coef.size;
79
107
  this.rowOf[s] = r;
80
108
  for (const j of coef.keys())
81
109
  this.colOf[j].add(r);
@@ -124,19 +152,16 @@ export class Simplex {
124
152
  else
125
153
  tighten(j, false, b + d, i);
126
154
  };
127
- for (const [i, r] of rows.entries()) {
128
- const f = formOf(r.lin), k = r.lin['1'] ?? 0;
129
- if (f === null) {
155
+ for (let i = 0; i < rows.length; i++) {
156
+ const r = rows[i], k = r.lin['1'] ?? 0;
157
+ const at = this.place(r.lin);
158
+ if (at === null) {
130
159
  if (k > 0 || (!integral && r.strict && k >= 0))
131
160
  return { kind: 'conflict', rows: [i] };
132
161
  continue;
133
162
  }
134
- if ('single' in f) {
135
- bound(f.c, this.varOf(f.single), k, i, !integral && r.strict);
136
- continue;
137
- }
138
- // g·form + k <= 0
139
- bound(f.g, this.slackOf(f), k, i, !integral && r.strict);
163
+ // c·x + k <= 0 (a slack's: g·form + k <= 0)
164
+ bound(at.c, at.j, k, i, !integral && r.strict);
140
165
  }
141
166
  for (let j = 0; j < this.val.length; j++)
142
167
  if (this.lo[j] > this.hi[j] + eps)
@@ -157,6 +182,125 @@ export class Simplex {
157
182
  }
158
183
  return { kind: 'sat' };
159
184
  }
185
+ // Incremental use, a node's rows as the last node's with some in and some out (kernel/cdcl.ts's search): each row's
186
+ // bound asserted and retracted by an id, a variable's bounds the tightest of those in (the first asserted among
187
+ // equals); `settle` then finds a conflict among them (a constant row that is false, two bounds that cross: the least
188
+ // such variable) or moves each nonbasic value whose bounds changed into them, and `check` goes on as after `load`, a
189
+ // conflict's rows given by their ids. The basic values are computed afresh from their rows every 16 settles.
190
+ ins = new Map();
191
+ inLo = [];
192
+ inHi = [];
193
+ falseIn = new Set();
194
+ crossing = new Set();
195
+ moved = [];
196
+ movedAt = [];
197
+ settles = 0;
198
+ assert(id, lin, strict) {
199
+ const at = this.place(lin), k = lin['1'] ?? 0;
200
+ if (at === null) {
201
+ this.ins.set(id, null);
202
+ if (k > 0 || (!this.integral && strict && k >= 0))
203
+ this.falseIn.add(id);
204
+ return;
205
+ }
206
+ const { j, c } = at, b0 = c > 0 ? -k / c : k / -c, upper = c > 0, d = !this.integral && strict ? 1e-7 * Math.max(1, Math.abs(b0)) : 0;
207
+ const b = this.integral ? (upper ? Math.floor(b0 + eps) : Math.ceil(b0 - eps)) : upper ? b0 - d : b0 + d;
208
+ this.ins.set(id, { j, upper, b });
209
+ ((upper ? this.inHi : this.inLo)[j] ??= []).push([b, id]);
210
+ if (upper ? b < this.hi[j] : b > this.lo[j]) {
211
+ if (upper) {
212
+ this.hi[j] = b;
213
+ this.hiWhy[j] = id;
214
+ }
215
+ else {
216
+ this.lo[j] = b;
217
+ this.loWhy[j] = id;
218
+ }
219
+ this.move(j);
220
+ }
221
+ }
222
+ retract(id) {
223
+ const a = this.ins.get(id);
224
+ this.ins.delete(id);
225
+ if (a === undefined)
226
+ return;
227
+ if (a === null) {
228
+ this.falseIn.delete(id);
229
+ return;
230
+ }
231
+ const { j, upper } = a, list = (upper ? this.inHi : this.inLo)[j];
232
+ list.splice(list.findIndex(x => x[1] === id), 1);
233
+ if ((upper ? this.hiWhy : this.loWhy)[j] !== id)
234
+ return;
235
+ let b = upper ? Infinity : -Infinity, why = -1;
236
+ for (const [x, i] of list)
237
+ if (upper ? x < b : x > b) {
238
+ b = x;
239
+ why = i;
240
+ }
241
+ if (upper) {
242
+ this.hi[j] = b;
243
+ this.hiWhy[j] = why;
244
+ }
245
+ else {
246
+ this.lo[j] = b;
247
+ this.loWhy[j] = why;
248
+ }
249
+ this.move(j);
250
+ }
251
+ // (every change of any bound, counted)
252
+ changes = 0;
253
+ move(j) {
254
+ this.vers[j] = (this.vers[j] ?? 0) + 1;
255
+ this.changes++;
256
+ if (!this.boundsOnly && this.movedAt[j] !== this.settles + 1) {
257
+ this.movedAt[j] = this.settles + 1;
258
+ this.moved.push(j);
259
+ }
260
+ }
261
+ vers = [];
262
+ // How many times a variable's bounds have changed.
263
+ version(j) { return this.vers[j] ?? 0; }
264
+ value(j) { return this.val[j]; }
265
+ settle() {
266
+ for (const id of this.falseIn)
267
+ return { kind: 'conflict', rows: [id] };
268
+ for (const j of this.moved) {
269
+ if (this.lo[j] > this.hi[j] + eps)
270
+ this.crossing.add(j);
271
+ else
272
+ this.crossing.delete(j);
273
+ }
274
+ if (this.crossing.size) {
275
+ let j = Infinity;
276
+ for (const x of this.crossing)
277
+ if (x < j)
278
+ j = x;
279
+ return { kind: 'conflict', rows: [this.loWhy[j], this.hiWhy[j]].filter(x => x >= 0) };
280
+ }
281
+ for (const j of this.moved) {
282
+ if (this.rowOf[j] >= 0)
283
+ continue;
284
+ const x = this.val[j], to = x < this.lo[j] ? this.lo[j] : x > this.hi[j] ? this.hi[j] : x;
285
+ if (to !== x)
286
+ this.shift(j, to - x);
287
+ }
288
+ this.moved = [];
289
+ if (++this.settles % 16 === 0)
290
+ for (const row of this.rows) {
291
+ let value = 0;
292
+ for (const [j, c] of row.coef)
293
+ value += c * this.val[j];
294
+ this.val[row.basic] = value;
295
+ }
296
+ return { kind: 'sat' };
297
+ }
298
+ // A variable's index by name (undefined where no row has read it); its bounds and the ids that give them.
299
+ indexOf(name) { return this.index.get(name); }
300
+ low(j) { return this.lo[j]; }
301
+ high(j) { return this.hi[j]; }
302
+ lowWhy(j) { return this.loWhy[j]; }
303
+ highWhy(j) { return this.hiWhy[j]; }
160
304
  shift(j, d) {
161
305
  this.val[j] += d;
162
306
  for (const r of this.colOf[j]) {
@@ -209,6 +353,7 @@ export class Simplex {
209
353
  ecoef.set(b, 1 / a);
210
354
  for (const j of row.coef.keys())
211
355
  this.colOf[j].delete(r);
356
+ this.nnzN += ecoef.size - row.coef.size;
212
357
  row.basic = e;
213
358
  row.coef = ecoef;
214
359
  this.rowOf[e] = r;
@@ -216,10 +361,11 @@ export class Simplex {
216
361
  for (const j of ecoef.keys())
217
362
  this.colOf[j].add(r);
218
363
  // Every other row reading x_e: substitute.
364
+ costs.pivotRows += this.colOf[e].size;
219
365
  for (const q of [...this.colOf[e]]) {
220
366
  if (q === r)
221
367
  continue;
222
- const other = this.rows[q], c = other.coef.get(e);
368
+ const other = this.rows[q], c = other.coef.get(e), before = other.coef.size;
223
369
  other.coef.delete(e);
224
370
  this.colOf[e].delete(q);
225
371
  for (const [j, d] of ecoef) {
@@ -234,6 +380,7 @@ export class Simplex {
234
380
  other.coef.set(j, x);
235
381
  }
236
382
  }
383
+ this.nnzN += other.coef.size - before;
237
384
  }
238
385
  this.colOf[e].delete(r);
239
386
  for (const j of ecoef.keys())
@@ -253,7 +400,6 @@ export class Simplex {
253
400
  return x <= tol(x);
254
401
  }
255
402
  get size() { return this.rows.length; }
256
- get nnz() { let n = 0; for (const r of this.rows)
257
- n += r.coef.size; return n; }
403
+ get nnz() { return this.nnzN; }
258
404
  get vars() { return this.val.length; }
259
405
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdp-wired",
3
- "version": "0.25.0",
3
+ "version": "0.27.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",
@@ -135,6 +135,7 @@
135
135
  "oxlint": "1.88.0",
136
136
  "oxlint-tsgolint": "7.0.2004",
137
137
  "typescript": "5.9.3",
138
- "xstate": "5.33.2"
138
+ "xstate": "5.33.2",
139
+ "xstate4": "npm:xstate@4.38.3"
139
140
  }
140
141
  }