gdp-wired 0.28.0 → 0.30.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.
@@ -68,6 +68,7 @@ export class Reader {
68
68
  node = 0;
69
69
  natRows = new Map();
70
70
  atomRows = new Map();
71
+ leafAtomRows = new WeakMap();
71
72
  ls = new Map();
72
73
  ts = new Map();
73
74
  fs = [];
@@ -608,13 +609,15 @@ export class Reader {
608
609
  leafRows(src) {
609
610
  const rows = [];
610
611
  for (let i = 0; i < this.trail.size(); i++) {
612
+ if (this.trail.quiet?.(i))
613
+ continue;
611
614
  const s = this.trail.stamp(i);
612
615
  let r = this.trailStamps[i] === s ? this.trailRows[i] : undefined;
613
616
  if (!r) {
614
- const a = this.trail.lin(i), key = `${a.name}=${a.holds}`;
615
- r = this.atomRows.get(key);
616
- if (!r)
617
- this.atomRows.set(key, (r = { lin: atomOf(a.lin, a.holds).lin, w: new Set([a.name]), label: '' }));
617
+ // (by the atom's own form, not its name: kernel.ts reads the names in a form's own order, which two forms of
618
+ // one atom may not share, and the names' order is the order of the rows past the facts)
619
+ const a = this.trail.lin(i), own = this.leafAtomRows.get(a.lin) ?? this.leafAtomRows.set(a.lin, []).get(a.lin), k = a.holds ? 1 : 0;
620
+ r = own[k] ?? (own[k] = { lin: atomOf(a.lin, a.holds).lin, w: new Set([a.name]), label: '' });
618
621
  this.trailRows[i] = r;
619
622
  this.trailStamps[i] = s;
620
623
  }
@@ -702,6 +705,8 @@ export class Reader {
702
705
  if (i !== undefined)
703
706
  out.add(i);
704
707
  } return [...out]; }
708
+ // The hypothesis a live row came from (undefined for the trail's atoms, the naturals and the term columns' rows).
709
+ srcOf(r) { return this.src.get(r); }
705
710
  inSeg = new Map();
706
711
  cached = new Map();
707
712
  termKeys = [];
@@ -719,13 +724,14 @@ export class Reader {
719
724
  // The live cells' names by map (every cell a live row reads), and a count of their changes.
720
725
  cells = new Map();
721
726
  cellsChanged = 0;
722
- sync() {
727
+ // (`from`: the trail's first position that may have changed since the last sync, where the caller knows it)
728
+ sync(from = 0) {
723
729
  this.syncing = true;
724
730
  this.syncN++;
725
731
  this.changed = [];
726
732
  const n = this.trail.size();
727
- for (let i = 0; i < Math.max(n, this.inTrail.length); i++) {
728
- const r = i < n ? this.trailRow(i) : undefined, had = this.inTrail[i];
733
+ for (let i = from; i < Math.max(n, this.inTrail.length); i++) {
734
+ const r = i < n && !this.trail.quiet?.(i) ? this.trailRow(i) : undefined, had = this.inTrail[i];
729
735
  if (r === had)
730
736
  continue;
731
737
  if (r)
@@ -1082,3 +1088,332 @@ export const keyLin = (cell) => {
1082
1088
  }
1083
1089
  return out;
1084
1090
  };
1091
+ const notLit = (l) => ({ lin: l.lin, holds: !l.holds });
1092
+ const litIds = new WeakMap();
1093
+ // A literal's variable (the smaller of its atom's texts, as kernel/cdcl.ts names it) and value.
1094
+ export const litVar = (l) => {
1095
+ let x = litIds.get(l.lin);
1096
+ if (!x) {
1097
+ const a = canon(l.lin), f = canonFlip(l.lin);
1098
+ litIds.set(l.lin, (x = f < a ? [f, false] : [a, true]));
1099
+ }
1100
+ return [x[0], x[1] === l.holds];
1101
+ };
1102
+ const isConst = (lin) => { for (const key in lin)
1103
+ if (key !== '1' && lin[key] !== 0)
1104
+ return false; return true; };
1105
+ class Unread extends Error {
1106
+ }
1107
+ export class Clausal {
1108
+ sideOf;
1109
+ maxCases;
1110
+ // The clauses made since the caller last took them (each a disjunction, with the hypothesis it is for; `own`: a gate's,
1111
+ // its first literal the gate's, so the clause is needed only where that literal is false; `out`: its last literal a
1112
+ // case's atom, which the clause's other literals false imply, so a search need never decide it for this clause).
1113
+ made = [];
1114
+ n = 0;
1115
+ hyp = 0;
1116
+ gm = new Map();
1117
+ cm = new Map();
1118
+ constructor(sideOf = () => 0, maxCases = 16) {
1119
+ this.sideOf = sideOf;
1120
+ this.maxCases = maxCases;
1121
+ }
1122
+ add(c, hyp) {
1123
+ this.hyp = hyp;
1124
+ try {
1125
+ this.top(c, true);
1126
+ return true;
1127
+ }
1128
+ catch (e) {
1129
+ if (e instanceof Unread)
1130
+ return false;
1131
+ throw e;
1132
+ }
1133
+ }
1134
+ memo(m) {
1135
+ const s = this.sideOf(this.hyp);
1136
+ let out = m.get(s);
1137
+ if (!out)
1138
+ m.set(s, (out = new Map()));
1139
+ return out;
1140
+ }
1141
+ fresh(kind) { return `${kind}${this.hyp}_${this.n++}`; }
1142
+ clause(lits, own = false, last = false) {
1143
+ const seen = new Map(), out = [];
1144
+ for (const l of lits) {
1145
+ const [x, val] = litVar(l), had = seen.get(x);
1146
+ if (had === undefined) {
1147
+ seen.set(x, val);
1148
+ out.push(l);
1149
+ }
1150
+ else if (had !== val)
1151
+ return;
1152
+ }
1153
+ this.made.push({ lits: out, hyp: this.hyp, own, out: last && out.at(-1) === lits.at(-1) });
1154
+ }
1155
+ top(c, positive) {
1156
+ switch (c.op) {
1157
+ case 'not': return this.top(c.x, !positive);
1158
+ case 'and':
1159
+ case 'or': {
1160
+ if ((c.op === 'and') === positive) {
1161
+ for (const y of c.xs)
1162
+ this.top(y, positive);
1163
+ return;
1164
+ }
1165
+ const lits = [];
1166
+ for (const y of c.xs) {
1167
+ const l = this.lit(y, positive ? 1 : 2), m = typeof l === 'boolean' ? l === positive : positive ? l : notLit(l);
1168
+ if (m === true)
1169
+ return;
1170
+ if (m !== false)
1171
+ lits.push(m);
1172
+ }
1173
+ return this.clause(lits);
1174
+ }
1175
+ case '<':
1176
+ case '<=':
1177
+ case '=':
1178
+ for (const cs of this.cmpCases(c)) {
1179
+ const not = cs.g.map(notLit);
1180
+ if (positive) {
1181
+ if (cs.atoms === false)
1182
+ this.clause(not);
1183
+ else if (cs.atoms !== true)
1184
+ for (const a of cs.atoms)
1185
+ this.clause([...not, a], false, true);
1186
+ }
1187
+ else if (cs.atoms === true)
1188
+ this.clause(not);
1189
+ else if (cs.atoms !== false)
1190
+ this.clause([...not, ...cs.atoms.map(notLit)]);
1191
+ }
1192
+ return;
1193
+ case 'flag':
1194
+ case 'all': throw new Unread();
1195
+ }
1196
+ }
1197
+ // A condition's literal (or its constant truth), its gate written for `pol` (1: read true, 2: read false, 3: both).
1198
+ lit(c, pol) {
1199
+ switch (c.op) {
1200
+ case 'not': {
1201
+ const x = this.lit(c.x, ((pol & 1) << 1) | ((pol & 2) >> 1));
1202
+ return typeof x === 'boolean' ? !x : notLit(x);
1203
+ }
1204
+ case 'and':
1205
+ case 'or':
1206
+ case '<':
1207
+ case '<=':
1208
+ case '=': break;
1209
+ case 'flag':
1210
+ case 'all': throw new Unread();
1211
+ }
1212
+ const m = this.memo(this.gm), had = m.get(c);
1213
+ if (had && (typeof had.l === 'boolean' || !(pol & ~had.done)))
1214
+ return had.l;
1215
+ const need = had ? pol & ~had.done : pol;
1216
+ let g = had?.l, out;
1217
+ const gate = () => (g ??= { lin: v(this.fresh('τ')), holds: true });
1218
+ if (c.op === 'and' || c.op === 'or') {
1219
+ const dominant = c.op === 'or', kids = [];
1220
+ let fold;
1221
+ for (const y of c.xs) {
1222
+ const l = this.lit(y, need);
1223
+ if (typeof l === 'boolean') {
1224
+ if (l === dominant) {
1225
+ fold = dominant;
1226
+ break;
1227
+ }
1228
+ }
1229
+ else
1230
+ kids.push(l);
1231
+ }
1232
+ if (fold !== undefined || !kids.length)
1233
+ out = fold ?? !dominant;
1234
+ else if (kids.length === 1)
1235
+ out = kids[0];
1236
+ else {
1237
+ const t = gate();
1238
+ if (need & 1) {
1239
+ if (dominant)
1240
+ this.clause([notLit(t), ...kids], true);
1241
+ else
1242
+ for (const x of kids)
1243
+ this.clause([notLit(t), x], true);
1244
+ }
1245
+ if (need & 2) {
1246
+ if (dominant)
1247
+ for (const x of kids)
1248
+ this.clause([t, notLit(x)], true);
1249
+ else
1250
+ this.clause([t, ...kids.map(notLit)], true);
1251
+ }
1252
+ out = t;
1253
+ }
1254
+ }
1255
+ else {
1256
+ const cases = this.cmpCases(c);
1257
+ if (cases.length === 1 && !cases[0].g.length && (typeof cases[0].atoms === 'boolean' || cases[0].atoms.length === 1))
1258
+ out = typeof cases[0].atoms === 'boolean' ? cases[0].atoms : cases[0].atoms[0];
1259
+ else {
1260
+ const t = gate();
1261
+ for (const cs of cases) {
1262
+ const not = cs.g.map(notLit);
1263
+ if (need & 1) {
1264
+ if (cs.atoms === false)
1265
+ this.clause([notLit(t), ...not], true);
1266
+ else if (cs.atoms !== true)
1267
+ for (const a of cs.atoms)
1268
+ this.clause([notLit(t), ...not, a], true, true);
1269
+ }
1270
+ if (need & 2) {
1271
+ if (cs.atoms === true)
1272
+ this.clause([t, ...not], true);
1273
+ else if (cs.atoms !== false)
1274
+ this.clause([t, ...not, ...cs.atoms.map(notLit)], true);
1275
+ }
1276
+ }
1277
+ out = t;
1278
+ }
1279
+ }
1280
+ m.set(c, { l: out, done: (had?.done ?? 0) | need });
1281
+ return out;
1282
+ }
1283
+ // A comparison's cases: each its conditions and its atoms (true or false where they are constant).
1284
+ cmpCases(c) {
1285
+ return this.cross(this.cases(c.a), this.cases(c.b), sub).map(({ g, lin: d }) => {
1286
+ const lins = c.op === '<=' ? [d] : c.op === '<' ? [add(d, k(1))] : [d, scale(d, -1)], atoms = [];
1287
+ for (const lin of lins) {
1288
+ if (!isConst(lin))
1289
+ atoms.push({ lin, holds: true });
1290
+ else if ((lin['1'] ?? 0) > 0)
1291
+ return { g, atoms: false };
1292
+ }
1293
+ return { g, atoms: atoms.length ? atoms : true };
1294
+ });
1295
+ }
1296
+ // A guard joined to a case's: undefined where it contradicts it.
1297
+ join(g, more) {
1298
+ if (!more.length)
1299
+ return g;
1300
+ if (!g.length)
1301
+ return more;
1302
+ const out = [...g], ids = new Map(g.map(l => litVar(l)));
1303
+ for (const l of more) {
1304
+ const [x, val] = litVar(l), had = ids.get(x);
1305
+ if (had === undefined) {
1306
+ ids.set(x, val);
1307
+ out.push(l);
1308
+ }
1309
+ else if (had !== val)
1310
+ return undefined;
1311
+ }
1312
+ return out;
1313
+ }
1314
+ cross(xs, ys, f) {
1315
+ const out = [];
1316
+ for (const x of xs)
1317
+ for (const y of ys) {
1318
+ const g = this.join(x.g, y.g);
1319
+ if (g)
1320
+ out.push({ g, lin: f(x.lin, y.lin) });
1321
+ }
1322
+ return out;
1323
+ }
1324
+ // A case split on a literal: `yes` where it holds, `no` where it doesn't.
1325
+ split(c, l, yes, no) {
1326
+ if (typeof l === 'boolean')
1327
+ return [{ g: c.g, lin: l ? yes : no }];
1328
+ const out = [];
1329
+ for (const [m, lin] of [[l, yes], [notLit(l), no]]) {
1330
+ const g = this.join(c.g, [m]);
1331
+ if (g)
1332
+ out.push({ g, lin });
1333
+ }
1334
+ return out;
1335
+ }
1336
+ cases(e) {
1337
+ const m = this.memo(this.cm), had = m.get(e);
1338
+ if (had)
1339
+ return had;
1340
+ let out;
1341
+ switch (e.op) {
1342
+ case 'var':
1343
+ if (e.name === '1' || /[*[\],:⟦]/.test(e.name))
1344
+ throw new Unread();
1345
+ out = [{ g: [], lin: v(e.name) }];
1346
+ break;
1347
+ case 'const':
1348
+ out = [{ g: [], lin: k(e.value) }];
1349
+ break;
1350
+ case 'add':
1351
+ out = this.cross(this.cases(e.a), this.cases(e.b), add);
1352
+ break;
1353
+ case 'mul':
1354
+ out = this.cases(e.a).map(c => ({ g: c.g, lin: scale(c.lin, e.k) }));
1355
+ break;
1356
+ case 'sub':
1357
+ case 'min':
1358
+ case 'max': {
1359
+ // (∸: a − b where b <= a, else 0; min: a where a <= b, else b; max the other way)
1360
+ out = [];
1361
+ for (const x of this.cases(e.a))
1362
+ for (const y of this.cases(e.b)) {
1363
+ const g = this.join(x.g, y.g);
1364
+ if (!g)
1365
+ continue;
1366
+ const a = x.lin, b = y.lin, d = e.op === 'sub' ? sub(b, a) : sub(a, b);
1367
+ out.push(...this.split({ g, lin: a }, isConst(d) ? (d['1'] ?? 0) <= 0 : { lin: d, holds: true }, e.op === 'sub' ? sub(a, b) : e.op === 'min' ? a : b, e.op === 'sub' ? {} : e.op === 'min' ? b : a));
1368
+ }
1369
+ break;
1370
+ }
1371
+ case 'get': {
1372
+ // (a division or remainder by a constant, core/machine.ts's div$c and mod$c: a fresh natural q per case of its
1373
+ // key x with c·q <= x <= c·q + c − 1, the remainder x − c·q)
1374
+ const f = /^(div|mod)\$(\d+)$/.exec(e.map);
1375
+ if (!f)
1376
+ throw new Unread();
1377
+ const c = Number(f[2]);
1378
+ out = [];
1379
+ for (const x of this.cases(e.key)) {
1380
+ const q = v(this.fresh('ϰ')), cq = scale(q, c), not = x.g.map(notLit);
1381
+ for (const lin of [sub(cq, x.lin), add(sub(x.lin, cq), k(1 - c))])
1382
+ this.clause([...not, { lin, holds: true }]);
1383
+ out.push({ g: x.g, lin: f[1] === 'div' ? q : sub(x.lin, cq) });
1384
+ }
1385
+ break;
1386
+ }
1387
+ case 'if': {
1388
+ const l = this.lit(e.c, 3);
1389
+ if (typeof l === 'boolean') {
1390
+ out = this.cases(l ? e.then : e.else);
1391
+ break;
1392
+ }
1393
+ out = [];
1394
+ for (const [m2, x] of [[l, e.then], [notLit(l), e.else]])
1395
+ for (const c of this.cases(x)) {
1396
+ const g = this.join(c.g, [m2]);
1397
+ if (g)
1398
+ out.push({ g, lin: c.lin });
1399
+ }
1400
+ break;
1401
+ }
1402
+ case 'prod': throw new Unread();
1403
+ }
1404
+ if (out.length > this.maxCases) {
1405
+ const col = v(this.fresh('ϰ'));
1406
+ for (const c of out) {
1407
+ const not = c.g.map(notLit);
1408
+ for (const lin of [sub(col, c.lin), sub(c.lin, col)])
1409
+ if (!isConst(lin))
1410
+ this.clause([...not, { lin, holds: true }], false, true);
1411
+ else if ((lin['1'] ?? 0) > 0)
1412
+ this.clause(not);
1413
+ }
1414
+ out = [{ g: [], lin: col }];
1415
+ }
1416
+ m.set(e, out);
1417
+ return out;
1418
+ }
1419
+ }
@@ -52,7 +52,10 @@ export declare class Simplex {
52
52
  lowWhy(j: number): number;
53
53
  highWhy(j: number): number;
54
54
  private shift;
55
+ greedy: number;
55
56
  check(maxPivots?: number): SimplexVerdict;
57
+ private checkGreedy;
58
+ private conflict;
56
59
  private pivot;
57
60
  holds(lin: Lin): boolean | undefined;
58
61
  get size(): number;
@@ -308,7 +308,12 @@ export class Simplex {
308
308
  this.val[row.basic] += row.coef.get(j) * d;
309
309
  }
310
310
  }
311
+ // `greedy` (kernel/cdcl.ts's clausal mode): the most violated basic variable first and the sparsest nonbasic one that
312
+ // can move, for the first `greedy` pivots of a check (`checkGreedy`), Bland's rule after (which can't cycle).
313
+ greedy = 0;
311
314
  check(maxPivots = 5_000) {
315
+ if (this.greedy)
316
+ return this.checkGreedy(maxPivots);
312
317
  for (let n = 0; n < maxPivots; n++) {
313
318
  // Bland: the violated basic variable of least index, then the nonbasic one of least index that can move.
314
319
  let b = -1;
@@ -328,18 +333,59 @@ export class Simplex {
328
333
  if (up ? this.val[j] < this.hi[j] - eps : this.val[j] > this.lo[j] + eps)
329
334
  e = j;
330
335
  }
331
- if (e < 0) {
332
- const why = [below ? this.loWhy[b] : this.hiWhy[b]];
333
- for (const [j, a] of row.coef)
334
- why.push(below === a > 0 ? this.hiWhy[j] : this.loWhy[j]);
335
- return { kind: 'conflict', rows: [...new Set(why)].filter(x => x >= 0) };
336
- }
336
+ if (e < 0)
337
+ return this.conflict(b, row, below);
337
338
  const target = below ? this.lo[b] : this.hi[b];
338
339
  this.pivot(b, e, target);
339
340
  this.pivots++;
340
341
  }
341
342
  return { kind: 'unknown' };
342
343
  }
344
+ checkGreedy(maxPivots) {
345
+ for (let n = 0; n < maxPivots; n++) {
346
+ const g = n < this.greedy;
347
+ let b = -1, worst = 0;
348
+ for (const row of this.rows) {
349
+ const j = row.basic, x = this.val[j], lo = this.lo[j], hi = this.hi[j];
350
+ if (x < lo - tol(lo) || x > hi + tol(hi)) {
351
+ if (g) {
352
+ const d = x < lo ? lo - x : x - hi;
353
+ if (d > worst) {
354
+ worst = d;
355
+ b = j;
356
+ }
357
+ }
358
+ else if (b < 0 || j < b)
359
+ b = j;
360
+ }
361
+ }
362
+ if (b < 0)
363
+ return { kind: 'sat' };
364
+ const row = this.rows[this.rowOf[b]], below = this.val[b] < this.lo[b];
365
+ let e = -1;
366
+ for (const [j, a] of row.coef) {
367
+ if (!g && e >= 0 && j > e)
368
+ continue;
369
+ const up = below === a > 0;
370
+ if (up ? this.val[j] < this.hi[j] - eps : this.val[j] > this.lo[j] + eps) {
371
+ if (!g || e < 0 || this.colOf[j].size < this.colOf[e].size || (this.colOf[j].size === this.colOf[e].size && j < e))
372
+ e = j;
373
+ }
374
+ }
375
+ if (e < 0)
376
+ return this.conflict(b, row, below);
377
+ this.pivot(b, e, below ? this.lo[b] : this.hi[b]);
378
+ this.pivots++;
379
+ }
380
+ return { kind: 'unknown' };
381
+ }
382
+ // A row whose basic variable can't reach its bound: the rows whose bounds it reads.
383
+ conflict(b, row, below) {
384
+ const why = [below ? this.loWhy[b] : this.hiWhy[b]];
385
+ for (const [j, a] of row.coef)
386
+ why.push(below === a > 0 ? this.hiWhy[j] : this.loWhy[j]);
387
+ return { kind: 'conflict', rows: [...new Set(why)].filter(x => x >= 0) };
388
+ }
343
389
  // x_b leaves the basis for x_e, x_b set to `target`.
344
390
  pivot(b, e, target) {
345
391
  const r = this.rowOf[b], row = this.rows[r], a = row.coef.get(e);
@@ -137,6 +137,18 @@ export declare function smachineToLean(id: string, given: Machine, laws: Law[],
137
137
  };
138
138
  noFacts?: boolean;
139
139
  kind?: KindLean;
140
+ sumTampers?: {
141
+ what: string;
142
+ m: Machine;
143
+ covers?: Record<string, E[]>;
144
+ expect: boolean;
145
+ }[];
146
+ coverTrees?: Record<string, Tree>;
147
+ liveTampers?: {
148
+ what: string;
149
+ l: Liveness;
150
+ expect: boolean;
151
+ }[];
140
152
  }): string;
141
153
  export declare const seedStepProof: (m: Machine, laws: Law[], stepLaws: StepLaw[], r: ReturnType<typeof proveStepLaws>) => void;
142
154
  export declare const cyclicCerts: () => boolean;