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 { add, k, scale, sub, v } from "../core/solver.js";
1
+ import { add, costs, env, k, scale, sub, v } from "../core/solver.js";
2
2
  import { atomOf, canon, canonFlip, columnTerm, factorsOf, isCell, termColumn } from "./kernel.js";
3
3
  export const none = new Set();
4
4
  export const join = (a, b) => {
@@ -6,14 +6,26 @@ export const join = (a, b) => {
6
6
  return b;
7
7
  if (!b.size)
8
8
  return a;
9
+ // (one inside the other, often: a reason with the parts it already has; the same set, not a copy)
10
+ const [s, l] = a.size <= b.size ? [a, b] : [b, a];
11
+ let inside = true;
12
+ for (const x of s)
13
+ if (!l.has(x)) {
14
+ inside = false;
15
+ break;
16
+ }
17
+ if (inside)
18
+ return l;
9
19
  const out = new Set(a);
10
20
  for (const x of b)
11
21
  out.add(x);
12
22
  return out;
13
23
  };
14
- const R0 = { w: none, pos: -1, fin: true };
24
+ const R0 = { w: none, pos: -1, fin: true }, R1 = { w: none, pos: -1, fin: false };
25
+ const unfin = (r) => (r.fin ? { w: r.w, pos: r.pos, fin: false } : r);
15
26
  const both = (a, b) => ({ w: join(a.w, b.w), pos: Math.max(a.pos, b.pos), fin: a.fin && b.fin });
16
- function times(a, b) {
27
+ // The kernel's product of two linear forms (kernel.ts's `times`): each monomial its factors sorted, joined by '*'.
28
+ export function times(a, b) {
17
29
  const out = [];
18
30
  for (const [p, cp] of Object.entries(a))
19
31
  for (const [q, cq] of Object.entries(b)) {
@@ -37,7 +49,19 @@ function same(site, owner, a, b, c, make) {
37
49
  return out;
38
50
  }
39
51
  // GDP_READER_CLEAN=0: values with an open read found again at every node, as before (for comparing).
40
- const keepClean = globalThis.process?.env?.GDP_READER_CLEAN !== '0';
52
+ export const keepClean = env.GDP_READER_CLEAN !== '0';
53
+ // The key of a part of the live rows (`sync`): a hypothesis' facts (0, its index), a term column's rows (1, the column's
54
+ // name id), a pair of cells' (2, their name ids), a hypothesis' part's facts (3, its index and the part's).
55
+ class Seg {
56
+ kind;
57
+ i;
58
+ j;
59
+ constructor(kind, i, j = -1) {
60
+ this.kind = kind;
61
+ this.i = i;
62
+ this.j = j;
63
+ }
64
+ }
41
65
  export class Reader {
42
66
  goal;
43
67
  trail;
@@ -58,50 +82,70 @@ export class Reader {
58
82
  // read it; an atom's name → the keys of the values that read it. A clean key's value stands.
59
83
  up = new Map();
60
84
  byAtom = new Map();
85
+ // (each key's latest finding: a read is an edge to a finding, and one to a finding not its key's latest is no read
86
+ // any more, dropped when a change walks past it)
87
+ found = new Map();
88
+ lastUp = new WeakMap();
61
89
  clean = new Set();
62
- frame;
63
90
  factKeys = [];
64
91
  openKeys = [];
65
- lastUp = new WeakMap();
92
+ finding;
66
93
  read(key) {
67
- if (!this.frame || this.lastUp.get(key) === this.frame)
94
+ const f = this.finding;
95
+ if (!f || this.lastUp.get(key) === f)
68
96
  return;
69
- this.lastUp.set(key, this.frame);
97
+ this.lastUp.set(key, f);
70
98
  let s = this.up.get(key);
71
99
  if (!s)
72
- this.up.set(key, (s = new Set()));
73
- s.add(this.frame);
100
+ this.up.set(key, (s = new Map()));
101
+ s.set(f.key, f);
74
102
  }
75
103
  readAtom(name) {
76
- if (!this.frame)
104
+ const f = this.finding;
105
+ if (!f)
77
106
  return;
78
107
  let s = this.byAtom.get(name);
79
108
  if (!s)
80
- this.byAtom.set(name, (s = new Set()));
81
- s.add(this.frame);
109
+ this.byAtom.set(name, (s = new Map()));
110
+ s.set(f.key, f);
82
111
  }
83
112
  within(key, f) {
84
- const was = this.frame;
85
- this.frame = key;
113
+ const was = this.finding, now = { key };
114
+ costs.values++;
115
+ this.found.set(key, now);
116
+ this.finding = now;
86
117
  try {
87
118
  return f();
88
119
  }
89
120
  finally {
90
- this.frame = was;
121
+ this.finding = was;
91
122
  }
92
123
  }
93
124
  // The trail changed at this atom (decided, or taken back): every value that read it, and every reader of those, all
94
125
  // the way up (a value kept as final reads below it values that are not clean).
95
126
  touch(name) {
96
- const todo = [...(this.byAtom.get(name) ?? [])], seen = new Set();
127
+ const todo = [], seen = new Set(), found = this.found;
128
+ const walk = (edges) => {
129
+ if (edges) {
130
+ costs.edges += edges.size;
131
+ for (const [k, f] of edges) {
132
+ if (found.get(k) === f)
133
+ todo.push(k);
134
+ else
135
+ edges.delete(k);
136
+ }
137
+ }
138
+ };
139
+ walk(this.byAtom.get(name));
97
140
  while (todo.length) {
98
141
  const key = todo.pop();
99
142
  if (seen.has(key))
100
143
  continue;
101
144
  seen.add(key);
102
145
  this.clean.delete(key);
103
- for (const p of this.up.get(key) ?? [])
104
- todo.push(p);
146
+ if (key instanceof Seg && this.syncing)
147
+ this.dirty.add(key);
148
+ walk(this.up.get(key));
105
149
  }
106
150
  }
107
151
  ok(x, key) {
@@ -113,7 +157,6 @@ export class Reader {
113
157
  }
114
158
  return false;
115
159
  }
116
- kept(r) { return { pos: r.pos, stamp: r.pos < 0 ? 0 : this.trail.stamp(r.pos), node: this.node }; }
117
160
  units = new Map();
118
161
  unit(name) { let w = this.units.get(name); if (!w)
119
162
  this.units.set(name, (w = new Set([name]))); return w; }
@@ -126,7 +169,7 @@ export class Reader {
126
169
  break;
127
170
  }
128
171
  if (constant)
129
- return { ...R0, t: (lin['1'] ?? 0) <= 0 };
172
+ return { w: none, pos: -1, fin: true, t: (lin['1'] ?? 0) <= 0 };
130
173
  let names = namePair.get(lin);
131
174
  if (!names)
132
175
  namePair.set(lin, (names = [canon(lin), canonFlip(lin)]));
@@ -214,7 +257,7 @@ export class Reader {
214
257
  }
215
258
  }
216
259
  // (an open value's reason is what its decided parts need: only the column's own value is open)
217
- const out = { lin, w: r.w, fin: r.fin, ...this.kept(r) };
260
+ const out = { lin, w: r.w, fin: r.fin, pos: r.pos, stamp: r.pos < 0 ? 0 : this.trail.stamp(r.pos), node: this.node };
218
261
  this.ls.set(e, out);
219
262
  this.clean.add(e);
220
263
  return out;
@@ -232,7 +275,7 @@ export class Reader {
232
275
  return { when: [add(ab, k(1))], unless: [[ba]] };
233
276
  return { when: [ab, ba], unless: [[add(ab, k(1))], [add(ba, k(1))]] };
234
277
  });
235
- return { ...atoms, r };
278
+ return { when: atoms.when, unless: atoms.unless, r };
236
279
  }
237
280
  truth(c) {
238
281
  this.read(c);
@@ -247,7 +290,7 @@ export class Reader {
247
290
  case 'flag': throw new Error('flags are inlined before the kernel sees them');
248
291
  case 'all':
249
292
  t = undefined;
250
- r = { ...R0, fin: false };
293
+ r = R1;
251
294
  break;
252
295
  case 'not': {
253
296
  const x = this.truth(c.x);
@@ -273,7 +316,7 @@ export class Reader {
273
316
  }
274
317
  else {
275
318
  t = undefined;
276
- r = { ...R0, fin: false };
319
+ r = R1;
277
320
  }
278
321
  break;
279
322
  }
@@ -283,7 +326,7 @@ export class Reader {
283
326
  const cmp = this.atoms(c);
284
327
  if (!cmp) {
285
328
  t = undefined;
286
- r = { ...R0, fin: false };
329
+ r = R1;
287
330
  break;
288
331
  }
289
332
  const ds = cmp.when.map(lin => this.dec(lin));
@@ -293,7 +336,8 @@ export class Reader {
293
336
  best = d;
294
337
  if (best) {
295
338
  t = false;
296
- r = { ...both(cmp.r, best), fin: cmp.r.fin };
339
+ const x = both(cmp.r, best);
340
+ r = { w: x.w, pos: x.pos, fin: cmp.r.fin };
297
341
  }
298
342
  else if (ds.every(d => d.t === true)) {
299
343
  t = true;
@@ -301,14 +345,14 @@ export class Reader {
301
345
  }
302
346
  else {
303
347
  t = undefined;
304
- r = { ...cmp.r, fin: false };
348
+ r = unfin(cmp.r);
305
349
  }
306
350
  break;
307
351
  }
308
352
  }
309
353
  // Final: decided, and every term its reason reads final too (a term column's atom on the trail reads differently
310
354
  // once the column's choice is decided, so a value through an open term is found again at each node).
311
- const out = { t, w: r.w, fin: t !== undefined && r.fin, ...this.kept(r) };
355
+ const out = { t, w: r.w, fin: t !== undefined && r.fin, pos: r.pos, stamp: r.pos < 0 ? 0 : this.trail.stamp(r.pos), node: this.node };
312
356
  this.ts.set(c, out);
313
357
  this.clean.add(c);
314
358
  return out;
@@ -337,21 +381,22 @@ export class Reader {
337
381
  return closed;
338
382
  }
339
383
  if (open.length > 1)
340
- return { ...closed, fin: false };
384
+ return unfin(closed);
341
385
  const inner = [], r = this.factsOf(open[0], positive, inner);
342
386
  for (const row of inner)
343
387
  out.push({ lin: row.lin, w: join(row.w, closed.w), label: '' });
344
388
  // (the last open part's own value is a read: open while it is)
345
389
  const last = this.truth(open[0]);
346
390
  // (and the facts are kept only while the part's own value is: its reason is theirs, though not their rows')
347
- return { ...both(both(closed, r), last), fin: r.fin && last.t !== undefined && last.fin };
391
+ const x = both(both(closed, r), last);
392
+ return { w: x.w, pos: x.pos, fin: r.fin && last.t !== undefined && last.fin };
348
393
  }
349
394
  case '<':
350
395
  case '<=':
351
396
  case '=': {
352
397
  const cmp = this.atoms(c);
353
398
  if (!cmp)
354
- return { ...R0, fin: false };
399
+ return R1;
355
400
  if (positive) {
356
401
  for (const lin of cmp.when)
357
402
  out.push({ lin, w: cmp.r.w, label: '' });
@@ -378,27 +423,83 @@ export class Reader {
378
423
  return closed;
379
424
  }
380
425
  if (open.length > 1)
381
- return { ...closed, fin: false };
426
+ return unfin(closed);
382
427
  for (const lin of open[0])
383
428
  out.push({ lin, w: closed.w, label: '' });
384
- return { ...closed, fin: false };
429
+ return unfin(closed);
385
430
  }
386
431
  }
387
432
  }
433
+ // (a hypothesis' facts are its parts': each conjunct's, kept as a value is, so a change finds again only the parts
434
+ // that read it)
388
435
  facts(i) {
389
- const key = (this.factKeys[i] ??= {});
436
+ const key = (this.factKeys[i] ??= new Seg(0, i));
390
437
  this.read(key);
391
438
  const had = this.fs[i];
392
439
  if (had && this.ok(had, key))
393
440
  return had;
394
441
  return this.within(key, () => {
395
- const rows = [], r = this.factsOf(this.goal.hyps[i], true, rows);
396
- const out = { rows, fin: r.fin, ...this.kept(r) };
442
+ const parts = this.parts(i);
443
+ let rows = [], pos = -1, fin = true;
444
+ if (parts.length === 1) {
445
+ const f = this.part(parts[0]);
446
+ rows = f.rows;
447
+ pos = f.pos;
448
+ fin = f.fin;
449
+ }
450
+ else
451
+ for (const p of parts) {
452
+ const f = this.part(p);
453
+ for (const r of f.rows)
454
+ rows.push(r);
455
+ if (f.pos > pos)
456
+ pos = f.pos;
457
+ fin &&= f.fin;
458
+ }
459
+ const out = { rows, fin, pos, stamp: pos < 0 ? 0 : this.trail.stamp(pos), node: this.node };
397
460
  this.fs[i] = out;
398
461
  this.clean.add(key);
399
462
  return out;
400
463
  });
401
464
  }
465
+ // Whether a hypothesis' facts are final (each part's).
466
+ factsFin(i) { for (const p of this.parts(i))
467
+ if (!this.part(p).fin)
468
+ return false; return true; }
469
+ // A hypothesis' conjuncts (a conjunction's parts, a negated disjunction's, flattened), each with its sign.
470
+ partsOf = [];
471
+ ps = new Map();
472
+ parts(i) {
473
+ let out = this.partsOf[i];
474
+ if (out)
475
+ return out;
476
+ const mine = [];
477
+ const flat = (c, positive) => {
478
+ if (c.op === 'not')
479
+ return flat(c.x, !positive);
480
+ if ((c.op === 'and' && positive) || (c.op === 'or' && !positive)) {
481
+ for (const y of c.xs)
482
+ flat(y, positive);
483
+ return;
484
+ }
485
+ mine.push({ c, positive, key: new Seg(3, i, mine.length) });
486
+ };
487
+ flat(this.goal.hyps[i], true);
488
+ return (this.partsOf[i] = out = mine);
489
+ }
490
+ part(p) {
491
+ this.read(p.key);
492
+ const had = this.ps.get(p.key);
493
+ if (had && this.ok(had, p.key))
494
+ return had;
495
+ return this.within(p.key, () => {
496
+ const rows = [], r = this.factsOf(p.c, p.positive, rows);
497
+ const out = { rows, fin: r.fin, pos: r.pos, stamp: r.pos < 0 ? 0 : this.trail.stamp(r.pos), node: this.node };
498
+ this.ps.set(p.key, out);
499
+ this.clean.add(p.key);
500
+ return out;
501
+ });
502
+ }
402
503
  // kernel.ts's termRows, each row with its reason (the forms made again only when a part's form is a new object).
403
504
  termRows(name) {
404
505
  const e = columnTerm(name), col = same(4, e, 0, 0, 0, () => v(name)), out = [];
@@ -432,94 +533,450 @@ export class Reader {
432
533
  if (e.op !== 'sub' && e.op !== 'min' && e.op !== 'max')
433
534
  return true;
434
535
  const a = this.linear(e.a).lin, b = this.linear(e.b).lin;
435
- return !!a && !!b && this.dec(e.op === 'sub' ? sub(b, a) : sub(a, b)).t !== undefined;
536
+ return !!a && !!b && this.dec(same(1, e, a, b, 0, () => (e.op === 'sub' ? sub(b, a) : sub(a, b)))).t !== undefined;
436
537
  }
437
538
  // kernel.ts's leaf rows at the trail (leafRows), each with its reason: the trail's atoms, every hypothesis' facts,
438
539
  // equal cells at keys the trail proves equal, each term column's rows (and those of the columns they bring), and a
439
540
  // natural's row for every name.
440
541
  rows() {
441
- // One row per linear form, with the shortest reason any of its copies has.
442
- const out = [], at = new Map();
542
+ // One row per linear form, with the shortest reason any of its copies has (forms told apart by their ids, stamped).
543
+ const out = [], g = ++this.gen;
443
544
  for (const r of this.leafRows()) {
444
- const id = canon(r.lin), j = at.get(id);
445
- if (j === undefined) {
446
- at.set(id, out.length);
545
+ const id = this.form(r.lin).id;
546
+ if (this.formSeen[id] !== g) {
547
+ this.formSeen[id] = g;
548
+ this.formAt[id] = out.length;
447
549
  out.push(r);
448
550
  }
449
- else if (r.w.size < out[j].w.size)
450
- out[j] = r;
551
+ else {
552
+ const j = this.formAt[id];
553
+ if (r.w.size < out[j].w.size)
554
+ out[j] = r;
555
+ }
451
556
  }
452
557
  return out;
453
558
  }
559
+ // Each form's id (by its canonical text) and its names' ids, kept per reader; the stamps that tell a node's forms and
560
+ // names apart without hashing their texts at every node.
561
+ forms = new WeakMap();
562
+ formIds = new Map();
563
+ nameIds = new Map();
564
+ nameText = [];
565
+ nameKind = []; // 1: a term column, 2: a cell, 0: else
566
+ formSeen = [];
567
+ formAt = [];
568
+ nameSeen = [];
569
+ termSeen = [];
570
+ gen = 0;
571
+ form(lin) {
572
+ let f = this.forms.get(lin);
573
+ if (f)
574
+ return f;
575
+ const text = canon(lin);
576
+ let id = this.formIds.get(text);
577
+ if (id === undefined) {
578
+ id = this.formIds.size;
579
+ this.formIds.set(text, id);
580
+ this.formSeen.push(0);
581
+ this.formAt.push(0);
582
+ }
583
+ const names = namesOf(lin).map(n => {
584
+ let j = this.nameIds.get(n);
585
+ if (j === undefined) {
586
+ j = this.nameText.length;
587
+ this.nameIds.set(n, j);
588
+ this.nameText.push(n);
589
+ this.nameKind.push(n.startsWith('⟦') ? 1 : isCell(n) ? 2 : 0);
590
+ this.nameSeen.push(0);
591
+ this.termSeen.push(0);
592
+ }
593
+ return j;
594
+ });
595
+ this.forms.set(lin, (f = { id, names }));
596
+ return f;
597
+ }
598
+ // Whether a form reads a term column.
599
+ readsColumn(lin) { for (const j of this.form(lin).names)
600
+ if (this.nameKind[j] === 1)
601
+ return true; return false; }
602
+ // The trail's atom rows, by position, kept while the position's stamp is.
603
+ trailRows = [];
604
+ trailStamps = [];
605
+ pairs = new Map();
454
606
  // The rows in kernel.ts's order, copies kept (a leaf's derivation indexes these).
455
- leafRows() {
607
+ // With `src`, each row's source beside it: the hypothesis whose facts it is, the pair of cells it equates, else -1.
608
+ leafRows(src) {
456
609
  const rows = [];
457
610
  for (let i = 0; i < this.trail.size(); i++) {
458
- const a = this.trail.lin(i), key = `${a.name}=${a.holds}`;
459
- let r = this.atomRows.get(key);
460
- if (!r)
461
- this.atomRows.set(key, (r = { lin: atomOf(a.lin, a.holds).lin, w: new Set([a.name]), label: '' }));
611
+ const s = this.trail.stamp(i);
612
+ let r = this.trailStamps[i] === s ? this.trailRows[i] : undefined;
613
+ 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: '' }));
618
+ this.trailRows[i] = r;
619
+ this.trailStamps[i] = s;
620
+ }
462
621
  rows.push(r);
622
+ src?.push(-1);
463
623
  }
464
624
  for (let i = 0; i < this.goal.hyps.length; i++)
465
- for (const r of this.facts(i).rows)
625
+ for (const r of this.facts(i).rows) {
466
626
  rows.push(r);
467
- const names = new Set();
627
+ src?.push(i);
628
+ }
629
+ // (each name once, in the order first read, as a set of texts would keep them)
630
+ const g = ++this.gen, names = [], seen = this.nameSeen, kind = this.nameKind;
468
631
  for (const r of rows)
469
- for (const key of namesOf(r.lin))
470
- names.add(key);
471
- const cells = [...names].filter(isCell).map(cellOf);
632
+ for (const j of this.form(r.lin).names)
633
+ if (seen[j] !== g) {
634
+ seen[j] = g;
635
+ names.push(j);
636
+ }
637
+ const cells = names.filter(j => kind[j] === 2).map(j => cellOf(this.nameText[j]));
472
638
  for (const [i, p] of cells.entries())
473
639
  for (const q of cells.slice(i + 1)) {
474
640
  if (p.map !== q.map)
475
641
  continue;
476
- const d = sub(p.key, q.key), x = this.dec(d), y = this.dec(scale(d, -1));
642
+ const f = this.pair(p, q), x = this.dec(f.d), y = this.dec(f.neg);
477
643
  if (x.t === true && y.t === true) {
478
644
  const w = join(x.w, y.w);
479
- rows.push({ lin: sub(v(p.name), v(q.name)), w, label: '' }, { lin: sub(v(q.name), v(p.name)), w, label: '' });
645
+ rows.push({ lin: f.pq, w, label: '' }, { lin: f.qp, w, label: '' });
646
+ src?.push([p.name, q.name], [p.name, q.name]);
480
647
  }
481
648
  }
482
- const seen = new Set(), todo = [...names].filter(n => n.startsWith('⟦'));
649
+ const done = this.termSeen, todo = names.filter(j => kind[j] === 1);
483
650
  while (todo.length) {
484
- const name = todo.pop();
485
- if (seen.has(name))
651
+ const j = todo.pop();
652
+ if (done[j] === g)
486
653
  continue;
487
- seen.add(name);
488
- for (const r of this.termRows(name)) {
654
+ done[j] = g;
655
+ for (const r of this.termRows(this.nameText[j])) {
489
656
  rows.push(r);
490
- for (const key of namesOf(r.lin)) {
491
- names.add(key);
492
- if (key.startsWith('⟦') && !seen.has(key))
493
- todo.push(key);
657
+ src?.push(-1);
658
+ for (const n of this.form(r.lin).names) {
659
+ if (seen[n] !== g) {
660
+ seen[n] = g;
661
+ names.push(n);
662
+ }
663
+ if (kind[n] === 1 && done[n] !== g)
664
+ todo.push(n);
494
665
  }
495
666
  }
496
667
  }
497
- for (const name of names) {
668
+ for (const j of names) {
669
+ const name = this.nameText[j];
498
670
  let r = this.natRows.get(name);
499
671
  if (!r)
500
672
  this.natRows.set(name, (r = { lin: { [name]: -1 }, w: none, label: '' }));
501
673
  rows.push(r);
674
+ src?.push(-1);
502
675
  }
503
676
  return rows;
504
677
  }
505
- // prove.ts's open atoms of a hypothesis (openH), in its order; none for a hypothesis whose facts are final.
506
- open(i, out) {
507
- const key = (this.openKeys[i] ??= {}), had = this.os[i];
508
- if (had && ((keepClean && this.clean.has(key)) || had.node === this.node)) {
509
- for (const lin of had.lins)
510
- out.push(lin);
678
+ // --- The live rows: kept from one node to the next (kernel/cdcl.ts's search) ---------------------------------------
679
+ // The same rows as rows() gives at the trail, found from what changed since the last sync: the trail's atom rows by
680
+ // position, the facts of each hypothesis whose key a change touched (and of those new since), and the rows these bring
681
+ // as the names they read come and go: a natural's row a name, each term column's rows (a part kept like the facts, by
682
+ // a key of its own), each pair of cells' (the cells the trail's atoms and the facts read). `live` has one row a form,
683
+ // the first of its copies with the shortest reason, in no set order; `entered` and `left` the forms (by id) that came
684
+ // and went in the last sync.
685
+ live = [];
686
+ entered = [];
687
+ left = [];
688
+ syncing = false;
689
+ dirty = new Set();
690
+ slot = [];
691
+ copies = [];
692
+ c12 = []; // by name id: the copies from the trail and the facts that read it
693
+ cT = []; // and from term rows (a column's own rows not counted for it)
694
+ inTrail = [];
695
+ inParts = new Map();
696
+ hypsIn = 0;
697
+ // (the hypothesis each live copy of a fact came from)
698
+ src = new WeakMap();
699
+ // The hypotheses some rows came from, as the live rows have them.
700
+ hypsOf(rows) { const out = new Set(); for (const r of rows) {
701
+ const i = this.src.get(r);
702
+ if (i !== undefined)
703
+ out.add(i);
704
+ } return [...out]; }
705
+ inSeg = new Map();
706
+ cached = new Map();
707
+ termKeys = [];
708
+ pairKeys = new Map();
709
+ liveCells = new Map();
710
+ inNat = [];
711
+ syncN = 0;
712
+ markN = [];
713
+ wasLive = [];
714
+ changed = [];
715
+ // A live form's place in `live`, by its id; whether any cell is read.
716
+ slotOf(id) { return this.slot[id]; }
717
+ hasCells() { return this.cellsOn > 0; }
718
+ cellsOn = 0;
719
+ // The live cells' names by map (every cell a live row reads), and a count of their changes.
720
+ cells = new Map();
721
+ cellsChanged = 0;
722
+ sync() {
723
+ this.syncing = true;
724
+ this.syncN++;
725
+ this.changed = [];
726
+ 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];
729
+ if (r === had)
730
+ continue;
731
+ if (r)
732
+ this.addCopy(r, 1);
733
+ if (had)
734
+ this.removeCopy(had, 1);
735
+ this.inTrail[i] = r;
736
+ }
737
+ this.inTrail.length = n;
738
+ for (; this.hypsIn < this.goal.hyps.length; this.hypsIn++)
739
+ for (const p of this.parts(this.hypsIn))
740
+ this.dirty.add(p.key);
741
+ for (const key of [...this.dirty]) {
742
+ if (key.kind === 0)
743
+ continue;
744
+ if (key.kind === 3) {
745
+ const fv = this.part(this.partsOf[key.i][key.j]), had = this.inParts.get(key);
746
+ if (fv === had)
747
+ continue;
748
+ for (const r of fv.rows) {
749
+ this.src.set(r, key.i);
750
+ this.addCopy(r, 1);
751
+ }
752
+ if (had)
753
+ for (const r of had.rows)
754
+ this.removeCopy(r, 1);
755
+ this.inParts.set(key, fv);
756
+ }
757
+ else if (this.inSeg.has(key)) {
758
+ const had = this.inSeg.get(key), now = this.segRows(key);
759
+ if (now === had)
760
+ continue;
761
+ this.inSeg.set(key, now);
762
+ for (const r of now)
763
+ this.addCopy(r, key.kind === 1 ? 2 : 0, key.kind === 1 ? key.i : -1);
764
+ for (const r of had)
765
+ this.removeCopy(r, key.kind === 1 ? 2 : 0, key.kind === 1 ? key.i : -1);
766
+ }
767
+ }
768
+ this.dirty.clear();
769
+ this.entered = [];
770
+ this.left = [];
771
+ for (const id of this.changed) {
772
+ const now = this.copies[id].length > 0;
773
+ if (now && !this.wasLive[id])
774
+ this.entered.push(id);
775
+ else if (!now && this.wasLive[id])
776
+ this.left.push(id);
777
+ }
778
+ }
779
+ trailRow(i) {
780
+ const s = this.trail.stamp(i);
781
+ let r = this.trailStamps[i] === s ? this.trailRows[i] : undefined;
782
+ if (!r) {
783
+ const a = this.trail.lin(i), key = `${a.name}=${a.holds}`;
784
+ r = this.atomRows.get(key);
785
+ if (!r)
786
+ this.atomRows.set(key, (r = { lin: atomOf(a.lin, a.holds).lin, w: new Set([a.name]), label: '' }));
787
+ this.trailRows[i] = r;
788
+ this.trailStamps[i] = s;
789
+ }
790
+ return r;
791
+ }
792
+ // A part's rows: kept while its key is clean, else found again (its reads noted, as a value's).
793
+ segRows(key) {
794
+ const had = this.cached.get(key);
795
+ if (had && this.clean.has(key))
796
+ return had;
797
+ const rows = this.within(key, () => {
798
+ if (key.kind === 1)
799
+ return this.termRows(this.nameText[key.i]);
800
+ const p = cellOf(this.nameText[key.i]), q = cellOf(this.nameText[key.j]), f = this.pair(p, q);
801
+ const x = this.dec(f.d), y = this.dec(f.neg);
802
+ if (x.t !== true || y.t !== true)
803
+ return [];
804
+ const w = join(x.w, y.w);
805
+ return [{ lin: f.pq, w, label: '' }, { lin: f.qp, w, label: '' }];
806
+ });
807
+ this.cached.set(key, rows);
808
+ this.clean.add(key);
809
+ return rows;
810
+ }
811
+ // (each pair's forms made once: the same objects at every node, so what is kept per form is found again)
812
+ pair(p, q) {
813
+ const key = `${p.name}\u0000${q.name}`;
814
+ let f = this.pairs.get(key);
815
+ if (!f) {
816
+ if (this.pairs.size > 200_000)
817
+ this.pairs.clear();
818
+ const d = sub(p.key, q.key);
819
+ this.pairs.set(key, (f = { d, neg: scale(d, -1), pq: sub(v(p.name), v(q.name)), qp: sub(v(q.name), v(p.name)) }));
820
+ }
821
+ return f;
822
+ }
823
+ mark(id, live) {
824
+ if (this.markN[id] === this.syncN)
511
825
  return;
826
+ this.markN[id] = this.syncN;
827
+ this.wasLive[id] = live;
828
+ this.changed.push(id);
829
+ }
830
+ // A copy of a row in (kind 1: from the trail or the facts, 2: a column's term rows, its own name not counted; 0: a
831
+ // natural's or a pair of cells', whose names it doesn't count).
832
+ addCopy(r, kind, self = -1) {
833
+ const f = this.form(r.lin), id = f.id, cs = (this.copies[id] ??= []);
834
+ costs.copies++;
835
+ if (!cs.length) {
836
+ this.mark(id, false);
837
+ this.slot[id] = this.live.length;
838
+ this.live.push(r);
839
+ }
840
+ else {
841
+ const at = this.slot[id];
842
+ if (r.w.size < this.live[at].w.size)
843
+ this.live[at] = r;
512
844
  }
845
+ cs.push(r);
846
+ if (kind)
847
+ for (const j of f.names)
848
+ if (j !== self)
849
+ this.countUp(j, kind);
850
+ }
851
+ removeCopy(r, kind, self = -1) {
852
+ const f = this.form(r.lin), id = f.id, cs = this.copies[id];
853
+ costs.copies++;
854
+ cs.splice(cs.indexOf(r), 1);
855
+ const at = this.slot[id];
856
+ if (!cs.length) {
857
+ this.mark(id, true);
858
+ const last = this.live.pop();
859
+ if (at < this.live.length) {
860
+ this.live[at] = last;
861
+ this.slot[this.form(last.lin).id] = at;
862
+ }
863
+ this.slot[id] = -1;
864
+ }
865
+ else if (this.live[at] === r) {
866
+ let best = cs[0];
867
+ for (const c of cs)
868
+ if (c.w.size < best.w.size)
869
+ best = c;
870
+ this.live[at] = best;
871
+ }
872
+ if (kind)
873
+ for (const j of f.names)
874
+ if (j !== self)
875
+ this.countDown(j, kind);
876
+ }
877
+ countUp(j, kind) {
878
+ const before = (this.c12[j] ?? 0) + (this.cT[j] ?? 0);
879
+ if (kind === 1)
880
+ this.c12[j] = (this.c12[j] ?? 0) + 1;
881
+ else
882
+ this.cT[j] = (this.cT[j] ?? 0) + 1;
883
+ if (before === 0)
884
+ this.nameOn(j);
885
+ if (kind === 1 && this.c12[j] === 1 && this.nameKind[j] === 2)
886
+ this.cellOn(j);
887
+ }
888
+ countDown(j, kind) {
889
+ if (kind === 1)
890
+ this.c12[j]--;
891
+ else
892
+ this.cT[j]--;
893
+ if (kind === 1 && this.c12[j] === 0 && this.nameKind[j] === 2)
894
+ this.cellOff(j);
895
+ if ((this.c12[j] ?? 0) + (this.cT[j] ?? 0) === 0)
896
+ this.nameOff(j);
897
+ }
898
+ nameOn(j) {
899
+ const name = this.nameText[j];
900
+ let r = this.natRows.get(name);
901
+ if (!r)
902
+ this.natRows.set(name, (r = { lin: { [name]: -1 }, w: none, label: '' }));
903
+ this.inNat[j] = r;
904
+ this.addCopy(r, 0);
905
+ if (this.nameKind[j] === 2) {
906
+ this.cellsOn++;
907
+ this.cellsChanged++;
908
+ const c = cellOf(name);
909
+ let same = this.cells.get(c.map);
910
+ if (!same)
911
+ this.cells.set(c.map, (same = new Set()));
912
+ same.add(name);
913
+ }
914
+ if (this.nameKind[j] === 1)
915
+ this.segOn((this.termKeys[j] ??= new Seg(1, j)));
916
+ }
917
+ nameOff(j) {
918
+ this.removeCopy(this.inNat[j], 0);
919
+ this.inNat[j] = undefined;
920
+ if (this.nameKind[j] === 2) {
921
+ this.cellsOn--;
922
+ this.cellsChanged++;
923
+ this.cells.get(cellOf(this.nameText[j]).map).delete(this.nameText[j]);
924
+ }
925
+ if (this.nameKind[j] === 1)
926
+ this.segOff(this.termKeys[j]);
927
+ }
928
+ segOn(key) {
929
+ const rows = this.segRows(key);
930
+ this.inSeg.set(key, rows);
931
+ for (const r of rows)
932
+ this.addCopy(r, key.kind === 1 ? 2 : 0, key.kind === 1 ? key.i : -1);
933
+ }
934
+ segOff(key) {
935
+ const rows = this.inSeg.get(key);
936
+ this.inSeg.delete(key);
937
+ for (const r of rows)
938
+ this.removeCopy(r, key.kind === 1 ? 2 : 0, key.kind === 1 ? key.i : -1);
939
+ }
940
+ pairKey(a, b) {
941
+ const [p, q] = a < b ? [a, b] : [b, a];
942
+ let m = this.pairKeys.get(p);
943
+ if (!m)
944
+ this.pairKeys.set(p, (m = new Map()));
945
+ let key = m.get(q);
946
+ if (!key)
947
+ m.set(q, (key = new Seg(2, p, q)));
948
+ return key;
949
+ }
950
+ cellOn(j) {
951
+ const c = cellOf(this.nameText[j]);
952
+ let same = this.liveCells.get(c.map);
953
+ if (!same)
954
+ this.liveCells.set(c.map, (same = new Set()));
955
+ for (const q of same)
956
+ this.segOn(this.pairKey(j, q));
957
+ same.add(j);
958
+ }
959
+ cellOff(j) {
960
+ const same = this.liveCells.get(cellOf(this.nameText[j]).map);
961
+ same.delete(j);
962
+ for (const q of same)
963
+ this.segOff(this.pairKey(j, q));
964
+ }
965
+ // prove.ts's open atoms of a hypothesis (openH), in its order; none for a hypothesis whose facts are final.
966
+ // (the same array while it stands)
967
+ openList(i) {
968
+ const key = (this.openKeys[i] ??= {}), had = this.os[i];
969
+ if (had && ((keepClean && this.clean.has(key)) || had.node === this.node))
970
+ return had.lins;
513
971
  const lins = this.within(key, () => {
514
972
  const mine = [];
515
- if (!this.facts(i).fin)
973
+ if (!this.factsFin(i))
516
974
  this.openH(this.goal.hyps[i], mine, true);
517
975
  return mine;
518
976
  });
519
977
  this.os[i] = { lins, node: this.node };
520
978
  this.clean.add(key);
521
- for (const lin of lins)
522
- out.push(lin);
979
+ return lins;
523
980
  }
524
981
  openE(e, out) {
525
982
  switch (e.op) {