gdp-wired 0.26.0 → 0.28.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 (44) hide show
  1. package/dist/core/accel.d.ts +24 -0
  2. package/dist/core/accel.js +557 -207
  3. package/dist/core/bmc.d.ts +4 -1
  4. package/dist/core/bmc.js +18 -15
  5. package/dist/core/cycles.d.ts +2 -0
  6. package/dist/core/cycles.js +1 -1
  7. package/dist/core/cyclic.js +17 -10
  8. package/dist/core/data.d.ts +1 -0
  9. package/dist/core/data.js +73 -18
  10. package/dist/core/discover.js +64 -25
  11. package/dist/core/dsl.js +108 -18
  12. package/dist/core/extract-check.d.ts +52 -2
  13. package/dist/core/extract-check.js +155 -104
  14. package/dist/core/extract-read.d.ts +25 -0
  15. package/dist/core/extract-read.js +55 -14
  16. package/dist/core/extract-shapes.js +94 -18
  17. package/dist/core/extract-xstate.js +16 -3
  18. package/dist/core/extract.d.ts +1 -0
  19. package/dist/core/extract.js +16 -8
  20. package/dist/core/fresh.d.ts +9 -0
  21. package/dist/core/fresh.js +91 -0
  22. package/dist/core/infer.d.ts +1 -0
  23. package/dist/core/infer.js +64 -46
  24. package/dist/core/live.d.ts +15 -0
  25. package/dist/core/live.js +317 -0
  26. package/dist/core/machine.js +0 -1
  27. package/dist/core/nonterm.d.ts +3 -1
  28. package/dist/core/nonterm.js +103 -25
  29. package/dist/core/solver.d.ts +21 -0
  30. package/dist/core/solver.js +8 -0
  31. package/dist/core/translate.js +69 -19
  32. package/dist/kernel/cdcl.js +212 -78
  33. package/dist/kernel/kernel.d.ts +17 -1
  34. package/dist/kernel/kernel.js +158 -2
  35. package/dist/kernel/prove.d.ts +30 -4
  36. package/dist/kernel/prove.js +119 -25
  37. package/dist/kernel/reader.d.ts +74 -5
  38. package/dist/kernel/reader.js +531 -75
  39. package/dist/kernel/simplex.d.ts +26 -1
  40. package/dist/kernel/simplex.js +148 -14
  41. package/dist/lean-kernel/emit.d.ts +15 -3
  42. package/dist/lean-kernel/emit.js +144 -12
  43. package/dist/lean-kernel/library.js +1 -1
  44. package/package.json +2 -2
@@ -1,4 +1,4 @@
1
- import { add, env, 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,12 +6,23 @@ 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
27
  // The kernel's product of two linear forms (kernel.ts's `times`): each monomial its factors sorted, joined by '*'.
17
28
  export function times(a, b) {
@@ -38,7 +49,19 @@ function same(site, owner, a, b, c, make) {
38
49
  return out;
39
50
  }
40
51
  // GDP_READER_CLEAN=0: values with an open read found again at every node, as before (for comparing).
41
- const keepClean = 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
+ }
42
65
  export class Reader {
43
66
  goal;
44
67
  trail;
@@ -59,50 +82,70 @@ export class Reader {
59
82
  // read it; an atom's name → the keys of the values that read it. A clean key's value stands.
60
83
  up = new Map();
61
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();
62
89
  clean = new Set();
63
- frame;
64
90
  factKeys = [];
65
91
  openKeys = [];
66
- lastUp = new WeakMap();
92
+ finding;
67
93
  read(key) {
68
- if (!this.frame || this.lastUp.get(key) === this.frame)
94
+ const f = this.finding;
95
+ if (!f || this.lastUp.get(key) === f)
69
96
  return;
70
- this.lastUp.set(key, this.frame);
97
+ this.lastUp.set(key, f);
71
98
  let s = this.up.get(key);
72
99
  if (!s)
73
- this.up.set(key, (s = new Set()));
74
- s.add(this.frame);
100
+ this.up.set(key, (s = new Map()));
101
+ s.set(f.key, f);
75
102
  }
76
103
  readAtom(name) {
77
- if (!this.frame)
104
+ const f = this.finding;
105
+ if (!f)
78
106
  return;
79
107
  let s = this.byAtom.get(name);
80
108
  if (!s)
81
- this.byAtom.set(name, (s = new Set()));
82
- s.add(this.frame);
109
+ this.byAtom.set(name, (s = new Map()));
110
+ s.set(f.key, f);
83
111
  }
84
112
  within(key, f) {
85
- const was = this.frame;
86
- this.frame = key;
113
+ const was = this.finding, now = { key };
114
+ costs.values++;
115
+ this.found.set(key, now);
116
+ this.finding = now;
87
117
  try {
88
118
  return f();
89
119
  }
90
120
  finally {
91
- this.frame = was;
121
+ this.finding = was;
92
122
  }
93
123
  }
94
124
  // The trail changed at this atom (decided, or taken back): every value that read it, and every reader of those, all
95
125
  // the way up (a value kept as final reads below it values that are not clean).
96
126
  touch(name) {
97
- 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));
98
140
  while (todo.length) {
99
141
  const key = todo.pop();
100
142
  if (seen.has(key))
101
143
  continue;
102
144
  seen.add(key);
103
145
  this.clean.delete(key);
104
- for (const p of this.up.get(key) ?? [])
105
- todo.push(p);
146
+ if (key instanceof Seg && this.syncing)
147
+ this.dirty.add(key);
148
+ walk(this.up.get(key));
106
149
  }
107
150
  }
108
151
  ok(x, key) {
@@ -114,7 +157,6 @@ export class Reader {
114
157
  }
115
158
  return false;
116
159
  }
117
- kept(r) { return { pos: r.pos, stamp: r.pos < 0 ? 0 : this.trail.stamp(r.pos), node: this.node }; }
118
160
  units = new Map();
119
161
  unit(name) { let w = this.units.get(name); if (!w)
120
162
  this.units.set(name, (w = new Set([name]))); return w; }
@@ -127,7 +169,7 @@ export class Reader {
127
169
  break;
128
170
  }
129
171
  if (constant)
130
- return { ...R0, t: (lin['1'] ?? 0) <= 0 };
172
+ return { w: none, pos: -1, fin: true, t: (lin['1'] ?? 0) <= 0 };
131
173
  let names = namePair.get(lin);
132
174
  if (!names)
133
175
  namePair.set(lin, (names = [canon(lin), canonFlip(lin)]));
@@ -215,7 +257,7 @@ export class Reader {
215
257
  }
216
258
  }
217
259
  // (an open value's reason is what its decided parts need: only the column's own value is open)
218
- 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 };
219
261
  this.ls.set(e, out);
220
262
  this.clean.add(e);
221
263
  return out;
@@ -233,7 +275,7 @@ export class Reader {
233
275
  return { when: [add(ab, k(1))], unless: [[ba]] };
234
276
  return { when: [ab, ba], unless: [[add(ab, k(1))], [add(ba, k(1))]] };
235
277
  });
236
- return { ...atoms, r };
278
+ return { when: atoms.when, unless: atoms.unless, r };
237
279
  }
238
280
  truth(c) {
239
281
  this.read(c);
@@ -248,7 +290,7 @@ export class Reader {
248
290
  case 'flag': throw new Error('flags are inlined before the kernel sees them');
249
291
  case 'all':
250
292
  t = undefined;
251
- r = { ...R0, fin: false };
293
+ r = R1;
252
294
  break;
253
295
  case 'not': {
254
296
  const x = this.truth(c.x);
@@ -274,7 +316,7 @@ export class Reader {
274
316
  }
275
317
  else {
276
318
  t = undefined;
277
- r = { ...R0, fin: false };
319
+ r = R1;
278
320
  }
279
321
  break;
280
322
  }
@@ -284,7 +326,7 @@ export class Reader {
284
326
  const cmp = this.atoms(c);
285
327
  if (!cmp) {
286
328
  t = undefined;
287
- r = { ...R0, fin: false };
329
+ r = R1;
288
330
  break;
289
331
  }
290
332
  const ds = cmp.when.map(lin => this.dec(lin));
@@ -294,7 +336,8 @@ export class Reader {
294
336
  best = d;
295
337
  if (best) {
296
338
  t = false;
297
- 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 };
298
341
  }
299
342
  else if (ds.every(d => d.t === true)) {
300
343
  t = true;
@@ -302,14 +345,14 @@ export class Reader {
302
345
  }
303
346
  else {
304
347
  t = undefined;
305
- r = { ...cmp.r, fin: false };
348
+ r = unfin(cmp.r);
306
349
  }
307
350
  break;
308
351
  }
309
352
  }
310
353
  // Final: decided, and every term its reason reads final too (a term column's atom on the trail reads differently
311
354
  // once the column's choice is decided, so a value through an open term is found again at each node).
312
- 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 };
313
356
  this.ts.set(c, out);
314
357
  this.clean.add(c);
315
358
  return out;
@@ -338,21 +381,22 @@ export class Reader {
338
381
  return closed;
339
382
  }
340
383
  if (open.length > 1)
341
- return { ...closed, fin: false };
384
+ return unfin(closed);
342
385
  const inner = [], r = this.factsOf(open[0], positive, inner);
343
386
  for (const row of inner)
344
387
  out.push({ lin: row.lin, w: join(row.w, closed.w), label: '' });
345
388
  // (the last open part's own value is a read: open while it is)
346
389
  const last = this.truth(open[0]);
347
390
  // (and the facts are kept only while the part's own value is: its reason is theirs, though not their rows')
348
- 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 };
349
393
  }
350
394
  case '<':
351
395
  case '<=':
352
396
  case '=': {
353
397
  const cmp = this.atoms(c);
354
398
  if (!cmp)
355
- return { ...R0, fin: false };
399
+ return R1;
356
400
  if (positive) {
357
401
  for (const lin of cmp.when)
358
402
  out.push({ lin, w: cmp.r.w, label: '' });
@@ -379,27 +423,83 @@ export class Reader {
379
423
  return closed;
380
424
  }
381
425
  if (open.length > 1)
382
- return { ...closed, fin: false };
426
+ return unfin(closed);
383
427
  for (const lin of open[0])
384
428
  out.push({ lin, w: closed.w, label: '' });
385
- return { ...closed, fin: false };
429
+ return unfin(closed);
386
430
  }
387
431
  }
388
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)
389
435
  facts(i) {
390
- const key = (this.factKeys[i] ??= {});
436
+ const key = (this.factKeys[i] ??= new Seg(0, i));
391
437
  this.read(key);
392
438
  const had = this.fs[i];
393
439
  if (had && this.ok(had, key))
394
440
  return had;
395
441
  return this.within(key, () => {
396
- const rows = [], r = this.factsOf(this.goal.hyps[i], true, rows);
397
- 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 };
398
460
  this.fs[i] = out;
399
461
  this.clean.add(key);
400
462
  return out;
401
463
  });
402
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
+ }
403
503
  // kernel.ts's termRows, each row with its reason (the forms made again only when a part's form is a new object).
404
504
  termRows(name) {
405
505
  const e = columnTerm(name), col = same(4, e, 0, 0, 0, () => v(name)), out = [];
@@ -433,94 +533,450 @@ export class Reader {
433
533
  if (e.op !== 'sub' && e.op !== 'min' && e.op !== 'max')
434
534
  return true;
435
535
  const a = this.linear(e.a).lin, b = this.linear(e.b).lin;
436
- 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;
437
537
  }
438
538
  // kernel.ts's leaf rows at the trail (leafRows), each with its reason: the trail's atoms, every hypothesis' facts,
439
539
  // equal cells at keys the trail proves equal, each term column's rows (and those of the columns they bring), and a
440
540
  // natural's row for every name.
441
541
  rows() {
442
- // One row per linear form, with the shortest reason any of its copies has.
443
- 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;
444
544
  for (const r of this.leafRows()) {
445
- const id = canon(r.lin), j = at.get(id);
446
- if (j === undefined) {
447
- 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;
448
549
  out.push(r);
449
550
  }
450
- else if (r.w.size < out[j].w.size)
451
- 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
+ }
452
556
  }
453
557
  return out;
454
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();
455
606
  // The rows in kernel.ts's order, copies kept (a leaf's derivation indexes these).
456
- 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) {
457
609
  const rows = [];
458
610
  for (let i = 0; i < this.trail.size(); i++) {
459
- const a = this.trail.lin(i), key = `${a.name}=${a.holds}`;
460
- let r = this.atomRows.get(key);
461
- if (!r)
462
- 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
+ }
463
621
  rows.push(r);
622
+ src?.push(-1);
464
623
  }
465
624
  for (let i = 0; i < this.goal.hyps.length; i++)
466
- for (const r of this.facts(i).rows)
625
+ for (const r of this.facts(i).rows) {
467
626
  rows.push(r);
468
- 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;
469
631
  for (const r of rows)
470
- for (const key of namesOf(r.lin))
471
- names.add(key);
472
- 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]));
473
638
  for (const [i, p] of cells.entries())
474
639
  for (const q of cells.slice(i + 1)) {
475
640
  if (p.map !== q.map)
476
641
  continue;
477
- 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);
478
643
  if (x.t === true && y.t === true) {
479
644
  const w = join(x.w, y.w);
480
- 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]);
481
647
  }
482
648
  }
483
- const seen = new Set(), todo = [...names].filter(n => n.startsWith('⟦'));
649
+ const done = this.termSeen, todo = names.filter(j => kind[j] === 1);
484
650
  while (todo.length) {
485
- const name = todo.pop();
486
- if (seen.has(name))
651
+ const j = todo.pop();
652
+ if (done[j] === g)
487
653
  continue;
488
- seen.add(name);
489
- for (const r of this.termRows(name)) {
654
+ done[j] = g;
655
+ for (const r of this.termRows(this.nameText[j])) {
490
656
  rows.push(r);
491
- for (const key of namesOf(r.lin)) {
492
- names.add(key);
493
- if (key.startsWith('⟦') && !seen.has(key))
494
- 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);
495
665
  }
496
666
  }
497
667
  }
498
- for (const name of names) {
668
+ for (const j of names) {
669
+ const name = this.nameText[j];
499
670
  let r = this.natRows.get(name);
500
671
  if (!r)
501
672
  this.natRows.set(name, (r = { lin: { [name]: -1 }, w: none, label: '' }));
502
673
  rows.push(r);
674
+ src?.push(-1);
503
675
  }
504
676
  return rows;
505
677
  }
506
- // prove.ts's open atoms of a hypothesis (openH), in its order; none for a hypothesis whose facts are final.
507
- open(i, out) {
508
- const key = (this.openKeys[i] ??= {}), had = this.os[i];
509
- if (had && ((keepClean && this.clean.has(key)) || had.node === this.node)) {
510
- for (const lin of had.lins)
511
- 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)
512
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;
513
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;
514
971
  const lins = this.within(key, () => {
515
972
  const mine = [];
516
- if (!this.facts(i).fin)
973
+ if (!this.factsFin(i))
517
974
  this.openH(this.goal.hyps[i], mine, true);
518
975
  return mine;
519
976
  });
520
977
  this.os[i] = { lins, node: this.node };
521
978
  this.clean.add(key);
522
- for (const lin of lins)
523
- out.push(lin);
979
+ return lins;
524
980
  }
525
981
  openE(e, out) {
526
982
  switch (e.op) {