gdp-wired 0.21.0 → 0.22.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.
@@ -0,0 +1,232 @@
1
+ const eps = 1e-9;
2
+ // A bound's tolerance: relative past 1 (values drift by a few ulps a pivot).
3
+ 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
+ const formMemo = new WeakMap();
6
+ function formOf(lin) {
7
+ let out = formMemo.get(lin);
8
+ if (out !== undefined)
9
+ return out;
10
+ const keys = Object.keys(lin).filter(key => key !== '1' && lin[key] !== 0).sort();
11
+ if (!keys.length)
12
+ out = null;
13
+ else if (keys.length === 1)
14
+ out = { single: keys[0], c: lin[keys[0]] };
15
+ else {
16
+ let g = 0;
17
+ for (const key of keys)
18
+ g = gcd(g, lin[key]);
19
+ if (lin[keys[0]] < 0)
20
+ g = -g;
21
+ const terms = keys.map(key => [key, lin[key] / g]);
22
+ out = { slack: `σ(${terms.map(([key, c]) => `${key}:${c}`).join(',')})`, g, terms };
23
+ }
24
+ formMemo.set(lin, out);
25
+ return out;
26
+ }
27
+ export class Simplex {
28
+ index = new Map();
29
+ lo = [];
30
+ hi = [];
31
+ loWhy = [];
32
+ hiWhy = [];
33
+ val = [];
34
+ // A basic variable's row: basic = Σ coef · nonbasic. rowOf[x] its row, or -1 for a nonbasic one; colOf[x] the rows
35
+ // a nonbasic x appears in.
36
+ rowOf = [];
37
+ rows = [];
38
+ colOf = [];
39
+ pivots = 0;
40
+ varOf(name) {
41
+ let j = this.index.get(name);
42
+ if (j !== undefined)
43
+ return j;
44
+ j = this.val.length;
45
+ this.index.set(name, j);
46
+ this.lo.push(-Infinity);
47
+ this.hi.push(Infinity);
48
+ this.loWhy.push(-1);
49
+ this.hiWhy.push(-1);
50
+ this.val.push(0);
51
+ this.rowOf.push(-1);
52
+ this.colOf.push(new Set());
53
+ return j;
54
+ }
55
+ // A slack for a form: basic, its row the form with each basic variable in it replaced by that one's row.
56
+ slackOf(f) {
57
+ const had = this.index.get(f.slack);
58
+ if (had !== undefined)
59
+ return had;
60
+ const coef = new Map();
61
+ const addTo = (j, c) => { const x = (coef.get(j) ?? 0) + c; if (Math.abs(x) < eps)
62
+ coef.delete(j);
63
+ else
64
+ coef.set(j, x); };
65
+ for (const [name, c] of f.terms) {
66
+ const j = this.varOf(name), r = this.rowOf[j];
67
+ if (r < 0)
68
+ addTo(j, c);
69
+ else
70
+ for (const [k, a] of this.rows[r].coef)
71
+ addTo(k, c * a);
72
+ }
73
+ const s = this.varOf(f.slack), r = this.rows.length;
74
+ this.rows.push({ basic: s, coef });
75
+ this.rowOf[s] = r;
76
+ for (const j of coef.keys())
77
+ this.colOf[j].add(r);
78
+ let value = 0;
79
+ for (const [j, c] of coef)
80
+ value += c * this.val[j];
81
+ this.val[s] = value;
82
+ return s;
83
+ }
84
+ // A node's rows as the bounds (a natural's own row x >= 0 among them). A pair of bounds that cross is a conflict at
85
+ // once.
86
+ load(rows) {
87
+ for (let j = 0; j < this.val.length; j++) {
88
+ this.lo[j] = -Infinity;
89
+ this.hi[j] = Infinity;
90
+ this.loWhy[j] = -1;
91
+ this.hiWhy[j] = -1;
92
+ }
93
+ const tighten = (j, upper, b, why) => {
94
+ if (upper) {
95
+ if (b < this.hi[j]) {
96
+ this.hi[j] = b;
97
+ this.hiWhy[j] = why;
98
+ }
99
+ }
100
+ else if (b > this.lo[j]) {
101
+ this.lo[j] = b;
102
+ this.loWhy[j] = why;
103
+ }
104
+ };
105
+ for (const [i, r] of rows.entries()) {
106
+ const f = formOf(r.lin), k = r.lin['1'] ?? 0;
107
+ if (f === null) {
108
+ if (k > 0)
109
+ return { kind: 'conflict', rows: [i] };
110
+ continue;
111
+ }
112
+ if ('single' in f) {
113
+ const j = this.varOf(f.single);
114
+ if (f.c > 0)
115
+ tighten(j, true, Math.floor(-k / f.c + eps), i);
116
+ else
117
+ tighten(j, false, Math.ceil(k / -f.c - eps), i);
118
+ continue;
119
+ }
120
+ const s = this.slackOf(f);
121
+ // g·form + k <= 0
122
+ if (f.g > 0)
123
+ tighten(s, true, Math.floor(-k / f.g + eps), i);
124
+ else
125
+ tighten(s, false, Math.ceil(k / -f.g - eps), i);
126
+ }
127
+ for (let j = 0; j < this.val.length; j++)
128
+ if (this.lo[j] > this.hi[j] + eps)
129
+ return { kind: 'conflict', rows: [this.loWhy[j], this.hiWhy[j]].filter(x => x >= 0) };
130
+ // Nonbasic variables into their bounds, then each basic one computed afresh from its row (no drift from the shifts
131
+ // of earlier nodes).
132
+ for (let j = 0; j < this.val.length; j++) {
133
+ if (this.rowOf[j] >= 0)
134
+ continue;
135
+ const x = this.val[j];
136
+ this.val[j] = x < this.lo[j] ? this.lo[j] : x > this.hi[j] ? this.hi[j] : x;
137
+ }
138
+ for (const row of this.rows) {
139
+ let value = 0;
140
+ for (const [j, c] of row.coef)
141
+ value += c * this.val[j];
142
+ this.val[row.basic] = value;
143
+ }
144
+ return { kind: 'sat' };
145
+ }
146
+ shift(j, d) {
147
+ this.val[j] += d;
148
+ for (const r of this.colOf[j]) {
149
+ const row = this.rows[r];
150
+ this.val[row.basic] += row.coef.get(j) * d;
151
+ }
152
+ }
153
+ check(maxPivots = 5_000) {
154
+ for (let n = 0; n < maxPivots; n++) {
155
+ // Bland: the violated basic variable of least index, then the nonbasic one of least index that can move.
156
+ let b = -1;
157
+ for (const row of this.rows) {
158
+ const j = row.basic, x = this.val[j];
159
+ if ((x < this.lo[j] - tol(this.lo[j]) || x > this.hi[j] + tol(this.hi[j])) && (b < 0 || j < b))
160
+ b = j;
161
+ }
162
+ if (b < 0)
163
+ return { kind: 'sat' };
164
+ const row = this.rows[this.rowOf[b]], below = this.val[b] < this.lo[b];
165
+ let e = -1;
166
+ for (const [j, a] of row.coef) {
167
+ if (e >= 0 && j > e)
168
+ continue;
169
+ const up = below === a > 0; // whether x_j must rise
170
+ if (up ? this.val[j] < this.hi[j] - eps : this.val[j] > this.lo[j] + eps)
171
+ e = j;
172
+ }
173
+ if (e < 0) {
174
+ const why = [below ? this.loWhy[b] : this.hiWhy[b]];
175
+ for (const [j, a] of row.coef)
176
+ why.push(below === a > 0 ? this.hiWhy[j] : this.loWhy[j]);
177
+ return { kind: 'conflict', rows: [...new Set(why)].filter(x => x >= 0) };
178
+ }
179
+ const target = below ? this.lo[b] : this.hi[b];
180
+ this.pivot(b, e, target);
181
+ this.pivots++;
182
+ }
183
+ return { kind: 'unknown' };
184
+ }
185
+ // x_b leaves the basis for x_e, x_b set to `target`.
186
+ pivot(b, e, target) {
187
+ const r = this.rowOf[b], row = this.rows[r], a = row.coef.get(e);
188
+ // x_e = (x_b − Σ_{j≠e} c_j x_j) / a
189
+ const theta = (target - this.val[b]) / a;
190
+ this.shift(e, theta); // moves x_b to target and every other basic variable with x_e
191
+ const ecoef = new Map();
192
+ for (const [j, c] of row.coef)
193
+ if (j !== e)
194
+ ecoef.set(j, -c / a);
195
+ ecoef.set(b, 1 / a);
196
+ for (const j of row.coef.keys())
197
+ this.colOf[j].delete(r);
198
+ row.basic = e;
199
+ row.coef = ecoef;
200
+ this.rowOf[e] = r;
201
+ this.rowOf[b] = -1;
202
+ for (const j of ecoef.keys())
203
+ this.colOf[j].add(r);
204
+ // Every other row reading x_e: substitute.
205
+ for (const q of [...this.colOf[e]]) {
206
+ if (q === r)
207
+ continue;
208
+ const other = this.rows[q], c = other.coef.get(e);
209
+ other.coef.delete(e);
210
+ this.colOf[e].delete(q);
211
+ for (const [j, d] of ecoef) {
212
+ const x = (other.coef.get(j) ?? 0) + c * d;
213
+ if (Math.abs(x) < eps) {
214
+ if (other.coef.delete(j))
215
+ this.colOf[j].delete(q);
216
+ }
217
+ else {
218
+ if (!other.coef.has(j))
219
+ this.colOf[j].add(q);
220
+ other.coef.set(j, x);
221
+ }
222
+ }
223
+ }
224
+ this.colOf[e].delete(r);
225
+ for (const j of ecoef.keys())
226
+ this.colOf[j].add(r);
227
+ }
228
+ get size() { return this.rows.length; }
229
+ get nnz() { let n = 0; for (const r of this.rows)
230
+ n += r.coef.size; return n; }
231
+ get vars() { return this.val.length; }
232
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdp-wired",
3
- "version": "0.21.0",
3
+ "version": "0.22.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",
@@ -113,7 +113,7 @@
113
113
  "build": "bun scripts/build.ts",
114
114
  "prepublishOnly": "bun scripts/build.ts",
115
115
  "smoke": "sh scripts/smoke.sh",
116
- "test": "bun scripts/gen-lean-library.ts --check && cd core && bun test solver.test.ts dsl.test.ts arith.test.ts conformance.test.ts compose.test.ts quantifiers.test.ts functions.test.ts keyterms.test.ts lists.test.ts liveness.test.ts sizes.test.ts values.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 data.test.ts seqs.test.ts search.test.ts invoices.test.ts folds.test.ts trees.test.ts pairs.test.ts chart.test.ts xstate.test.ts && cd ../kernel && bun test tamper.test.ts refusals.test.ts steplaw.test.ts sets.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/infer.ts && bun examples/arith.ts && bun examples/cyclic.ts && bun examples/cycles.ts && bun examples/data.ts && bun examples/scopes.ts && bun examples/lists.ts && bun examples/sizes.ts && bun examples/liveness.ts && bun examples/budget.ts && bun examples/gdp.ts && bun examples/steps.ts && bun examples/jev-fabric/run.ts"
116
+ "test": "bun scripts/gen-lean-library.ts --check && cd core && bun test solver.test.ts dsl.test.ts arith.test.ts conformance.test.ts compose.test.ts quantifiers.test.ts functions.test.ts keyterms.test.ts lists.test.ts liveness.test.ts sizes.test.ts values.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 data.test.ts seqs.test.ts search.test.ts invoices.test.ts folds.test.ts trees.test.ts pairs.test.ts chart.test.ts xstate.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/infer.ts && bun examples/arith.ts && bun examples/cyclic.ts && bun examples/cycles.ts && bun examples/data.ts && bun examples/scopes.ts && bun examples/lists.ts && bun examples/sizes.ts && bun examples/liveness.ts && bun examples/budget.ts && bun examples/gdp.ts && bun examples/steps.ts && bun examples/jev-fabric/run.ts"
117
117
  },
118
118
  "devDependencies": {
119
119
  "@gdp-ts/core": "0.1.0",