gdp-wired 0.47.0 → 0.48.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 (170) hide show
  1. package/README.md +15 -4
  2. package/dist/core/carry.d.ts +15 -0
  3. package/dist/core/carry.js +651 -0
  4. package/dist/core/cli.d.ts +3 -1
  5. package/dist/core/cli.js +28 -8
  6. package/dist/core/count-cases.d.ts +24 -0
  7. package/dist/core/count-cases.js +320 -0
  8. package/dist/core/count-cert-grow.d.ts +4 -0
  9. package/dist/core/count-cert-grow.js +37 -0
  10. package/dist/core/count-cert-transport.d.ts +3 -0
  11. package/dist/core/count-cert-transport.js +58 -0
  12. package/dist/core/count-completion.d.ts +9 -0
  13. package/dist/core/count-completion.js +27 -0
  14. package/dist/core/count-cubes.d.ts +8 -0
  15. package/dist/core/count-cubes.js +69 -0
  16. package/dist/core/count-demand.d.ts +37 -0
  17. package/dist/core/count-demand.js +361 -0
  18. package/dist/core/count-facts.d.ts +52 -0
  19. package/dist/core/count-facts.js +247 -0
  20. package/dist/core/count-focus-proof.d.ts +8 -0
  21. package/dist/core/count-focus-proof.js +140 -0
  22. package/dist/core/count-focused.d.ts +22 -0
  23. package/dist/core/count-focused.js +676 -0
  24. package/dist/core/count-ranges.d.ts +5 -0
  25. package/dist/core/count-ranges.js +65 -0
  26. package/dist/core/count-recovery/action-guard.d.ts +5 -0
  27. package/dist/core/count-recovery/action-guard.js +51 -0
  28. package/dist/core/count-recovery/boolean-core.d.ts +16 -0
  29. package/dist/core/count-recovery/boolean-core.js +258 -0
  30. package/dist/core/count-recovery/carried-case.d.ts +2 -0
  31. package/dist/core/count-recovery/carried-case.js +24 -0
  32. package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
  33. package/dist/core/count-recovery/conditional-check-cache.js +130 -0
  34. package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
  35. package/dist/core/count-recovery/conditional-cuts.js +318 -0
  36. package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
  37. package/dist/core/count-recovery/conditional-proof.js +306 -0
  38. package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
  39. package/dist/core/count-recovery/derivability-closure.js +533 -0
  40. package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
  41. package/dist/core/count-recovery/empty-count-source.js +40 -0
  42. package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
  43. package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
  44. package/dist/core/count-recovery/field-case.d.ts +5 -0
  45. package/dist/core/count-recovery/field-case.js +89 -0
  46. package/dist/core/count-recovery/field-demand.d.ts +46 -0
  47. package/dist/core/count-recovery/field-demand.js +47 -0
  48. package/dist/core/count-recovery/field-only.d.ts +3 -0
  49. package/dist/core/count-recovery/field-only.js +18 -0
  50. package/dist/core/count-recovery/fold-path.d.ts +6 -0
  51. package/dist/core/count-recovery/fold-path.js +35 -0
  52. package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
  53. package/dist/core/count-recovery/frame-lemmas.js +120 -0
  54. package/dist/core/count-recovery/guard-cases.d.ts +12 -0
  55. package/dist/core/count-recovery/guard-cases.js +138 -0
  56. package/dist/core/count-recovery/key-closure.d.ts +14 -0
  57. package/dist/core/count-recovery/key-closure.js +62 -0
  58. package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
  59. package/dist/core/count-recovery/leaf-allowance.js +7 -0
  60. package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
  61. package/dist/core/count-recovery/lift-count-proxies.js +175 -0
  62. package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
  63. package/dist/core/count-recovery/live-dependencies.js +76 -0
  64. package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
  65. package/dist/core/count-recovery/member-inclusions.js +168 -0
  66. package/dist/core/count-recovery/member-premises.d.ts +29 -0
  67. package/dist/core/count-recovery/member-premises.js +114 -0
  68. package/dist/core/count-recovery/natural-cases.d.ts +6 -0
  69. package/dist/core/count-recovery/natural-cases.js +59 -0
  70. package/dist/core/count-recovery/ops.d.ts +6 -0
  71. package/dist/core/count-recovery/ops.js +8 -0
  72. package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
  73. package/dist/core/count-recovery/pre-state-source.js +18 -0
  74. package/dist/core/count-recovery/rebase-path.d.ts +9 -0
  75. package/dist/core/count-recovery/rebase-path.js +70 -0
  76. package/dist/core/count-recovery/rename-proof.d.ts +2 -0
  77. package/dist/core/count-recovery/rename-proof.js +102 -0
  78. package/dist/core/count-recovery/row-cases.d.ts +16 -0
  79. package/dist/core/count-recovery/row-cases.js +291 -0
  80. package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
  81. package/dist/core/count-recovery/scalar-guards.js +291 -0
  82. package/dist/core/count-recovery/source-memo.d.ts +12 -0
  83. package/dist/core/count-recovery/source-memo.js +67 -0
  84. package/dist/core/count-select.d.ts +16 -0
  85. package/dist/core/count-select.js +24 -0
  86. package/dist/core/count-support.d.ts +598 -0
  87. package/dist/core/count-support.js +2080 -0
  88. package/dist/core/count-unions.d.ts +2 -0
  89. package/dist/core/count-unions.js +139 -0
  90. package/dist/core/count-witness-flags.d.ts +9 -0
  91. package/dist/core/count-witness-flags.js +37 -0
  92. package/dist/core/count-write-frames.d.ts +12 -0
  93. package/dist/core/count-write-frames.js +206 -0
  94. package/dist/core/counts.d.ts +20 -2
  95. package/dist/core/counts.js +807 -156
  96. package/dist/core/dsl.d.ts +50 -6
  97. package/dist/core/dsl.js +307 -65
  98. package/dist/core/effect.js +8 -1
  99. package/dist/core/exists.d.ts +1 -1
  100. package/dist/core/exists.js +80 -15
  101. package/dist/core/extract-check.d.ts +7 -1
  102. package/dist/core/extract-check.js +130 -13
  103. package/dist/core/extract-read.d.ts +22 -8
  104. package/dist/core/extract-read.js +29 -11
  105. package/dist/core/extract-shapes.js +122 -41
  106. package/dist/core/extract-standalone.js +53 -9
  107. package/dist/core/extract.js +9 -3
  108. package/dist/core/field-laws.d.ts +2 -0
  109. package/dist/core/field-laws.js +100 -0
  110. package/dist/core/finite-frontiers.d.ts +5 -0
  111. package/dist/core/finite-frontiers.js +116 -0
  112. package/dist/core/finite-houdini.d.ts +52 -0
  113. package/dist/core/finite-houdini.js +460 -0
  114. package/dist/core/finite-model.d.ts +13 -0
  115. package/dist/core/finite-model.js +68 -0
  116. package/dist/core/finite-tiny.d.ts +72 -0
  117. package/dist/core/finite-tiny.js +1052 -0
  118. package/dist/core/infer.d.ts +1 -1
  119. package/dist/core/infer.js +32 -9
  120. package/dist/core/laws.d.ts +2 -1
  121. package/dist/core/laws.js +47 -14
  122. package/dist/core/lemma-cuts.d.ts +38 -0
  123. package/dist/core/lemma-cuts.js +365 -0
  124. package/dist/core/machine.d.ts +39 -2
  125. package/dist/core/machine.js +240 -35
  126. package/dist/core/mutate.d.ts +55 -0
  127. package/dist/core/mutate.js +210 -0
  128. package/dist/core/native-domain.d.ts +6 -0
  129. package/dist/core/native-domain.js +114 -0
  130. package/dist/core/native-law.d.ts +3 -0
  131. package/dist/core/native-law.js +174 -0
  132. package/dist/core/quorum-laws.d.ts +5 -0
  133. package/dist/core/quorum-laws.js +239 -0
  134. package/dist/core/relational.js +10 -2
  135. package/dist/core/relevance.d.ts +4 -0
  136. package/dist/core/relevance.js +79 -0
  137. package/dist/core/runtime-wall.d.ts +3 -0
  138. package/dist/core/runtime-wall.js +4 -0
  139. package/dist/core/sample-step.d.ts +16 -0
  140. package/dist/core/sample-step.js +28 -0
  141. package/dist/core/source-check.d.ts +44 -0
  142. package/dist/core/source-check.js +126 -0
  143. package/dist/core/source-diagnostic.d.ts +1 -0
  144. package/dist/core/source-diagnostic.js +34 -0
  145. package/dist/core/support-budget.d.ts +2 -0
  146. package/dist/core/support-budget.js +6 -0
  147. package/dist/core/system.js +2 -0
  148. package/dist/core/wall.d.ts +6 -0
  149. package/dist/core/wall.js +23 -0
  150. package/dist/core/watch.js +4 -0
  151. package/dist/kernel/cdcl.d.ts +4 -0
  152. package/dist/kernel/cdcl.js +20 -5
  153. package/dist/kernel/kernel.d.ts +10 -0
  154. package/dist/kernel/kernel.js +56 -3
  155. package/dist/kernel/model-sequence.d.ts +2 -2
  156. package/dist/kernel/model-sequence.js +4 -3
  157. package/dist/kernel/model-values.d.ts +2 -0
  158. package/dist/kernel/model-values.js +60 -0
  159. package/dist/kernel/prove.d.ts +10 -0
  160. package/dist/kernel/prove.js +56 -8
  161. package/dist/kernel/prover-rows.d.ts +29 -0
  162. package/dist/kernel/prover-rows.js +114 -0
  163. package/dist/lean-kernel/emit.d.ts +39 -2
  164. package/dist/lean-kernel/emit.js +136 -24
  165. package/dist/lean-kernel/lean-terms.d.ts +1 -0
  166. package/dist/lean-kernel/lean-terms.js +13 -5
  167. package/dist/lean-kernel/library.js +4 -0
  168. package/dist/lean-kernel/qalpha.d.ts +7 -0
  169. package/dist/lean-kernel/qalpha.js +40 -0
  170. package/package.json +5 -1
@@ -0,0 +1,69 @@
1
+ // Width-two zero/nonzero cubes over fresh lifted one-key counts. These are
2
+ // proposals only: the normal reachable filter and checked induction still apply.
3
+ import { all, any, atMost, carriedView, compileE, evalE, get, less, n, not, same, walk, x } from "./machine.js";
4
+ import { checkWall } from "./wall.js";
5
+ export function liftedOneKeyCounts(source, m) {
6
+ return Object.entries(m.counts ?? {}).filter(([name, d]) => {
7
+ if (name in (source.counts ?? {}) || d.lo || d.body.op !== 'if')
8
+ return false;
9
+ const c = d.body.c;
10
+ // A stored row's cardinality is one membership read. Filtered quorum
11
+ // bodies are already separate candidate families, not extra row fields.
12
+ return (c.op === '=' && c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1) || (c.op === '<=' && c.a.op === 'const' && c.a.value === 1 && c.b.op === 'get');
13
+ }).map(([name]) => name);
14
+ }
15
+ export function countCubes(source, m, samples, only) {
16
+ const fresh = liftedOneKeyCounts(source, m).filter(name => !only || only.has(name));
17
+ if (!fresh.length)
18
+ return [];
19
+ const taken = new Set([...m.params, ...m.inputs, ...Object.keys(m.state)]);
20
+ walk(m, o => { if (o.op === 'var')
21
+ taken.add(o.name); });
22
+ let key = 'x$countCube';
23
+ while (taken.has(key))
24
+ key += '$';
25
+ const direct = new Set();
26
+ walk([m.assume, m.step], o => { if (o.op === 'get' && o.map in (m.maps ?? {}) && o.key.op === 'var')
27
+ direct.add(o.map); });
28
+ const groups = new Map();
29
+ for (const name of fresh) {
30
+ const id = JSON.stringify(m.counts[name].below);
31
+ groups.set(id, [...groups.get(id) ?? [], name]);
32
+ }
33
+ const out = [];
34
+ for (const names of groups.values()) {
35
+ const below = m.counts[names[0]].below;
36
+ const terms = [...Object.keys(m.state).map(name => ({ name, term: x(name), lifted: false })), ...[...direct].sort().map(name => ({ name: `${name}[x]`, term: get(name, x(key)), lifted: false })), ...names.map(name => ({ name: `${name}[x]`, term: get(name, x(key)), lifted: true }))];
37
+ const compiled = terms.map(t => compileE(t.term, m)), rows = [];
38
+ for (const { p, sts } of samples)
39
+ for (const st of sts) {
40
+ checkWall('count cubes');
41
+ const bound = evalE(below, { ...p, ...st.scalars }, st, m);
42
+ const view = carriedView(m, st, Object.fromEntries((m.inputMaps ?? []).map(k => [k, new Map()])), undefined, p);
43
+ for (let k = 0; k < bound; k++) {
44
+ if ((k & 255) === 0)
45
+ checkWall('count cube rows');
46
+ rows.push(compiled.flatMap(f => { const value = f({ ...p, ...st.scalars, [key]: k }, view); return [value === 0, value >= 1]; }));
47
+ }
48
+ }
49
+ if (!rows.length)
50
+ continue;
51
+ const lits = terms.flatMap((t, i) => [{ name: `${t.name} is 0`, c: same(t.term, n(0)), atom: i, lifted: t.lifted }, { name: `1 <= ${t.name}`, c: atMost(n(1), t.term), atom: i, lifted: t.lifted }]);
52
+ const seen = lits.map((_, i) => rows.some(r => r[i]));
53
+ const add = (indices) => out.push({ name: `count cubes: every x with x < ${JSON.stringify(below)}: never ${indices.map(i => lits[i].name).join(' and ')}`, forall: key, holds: any(not(less(x(key), below)), not(all(...indices.map(i => lits[i].c)))) });
54
+ for (let i = 0; i < lits.length; i++) {
55
+ checkWall('count cube enumeration');
56
+ if (!seen[i] && lits[i].lifted)
57
+ add([i]);
58
+ if (!seen[i])
59
+ continue;
60
+ for (let j = i + 1; j < lits.length; j++) {
61
+ if (!seen[j] || lits[i].atom === lits[j].atom || !(lits[i].lifted || lits[j].lifted))
62
+ continue;
63
+ if (!rows.some(r => r[i] && r[j]))
64
+ add([i, j]);
65
+ }
66
+ }
67
+ }
68
+ return out;
69
+ }
@@ -0,0 +1,37 @@
1
+ import { type Count, type CountInst } from '../kernel/kernel.js';
2
+ export type CountDemandOptions = {
3
+ /** Restrict positive pairs to these IDs; at least one ID must be demanded.
4
+ * Omit to visit demanded/demanded pairs, then raw carried indicators,
5
+ * then other partners sharing a primitive read with the demanded body. */
6
+ pairs?: readonly (readonly [string, string])[];
7
+ /** Output limit, clamped to 128 (default 128). */
8
+ maxFacts?: number;
9
+ /** Disable partition emission when this call is only a bounded pair request. */
10
+ partitions?: boolean;
11
+ /** Shared scan/AST/search budget, clamped to 1,000,000 (default 200,000).
12
+ * Exhaustion returns a sound but possibly incomplete prefix, never a fallback pool. */
13
+ maxWork?: number;
14
+ /** Names already used by goal hypotheses or other certificate witnesses. */
15
+ reserved?: Iterable<string>;
16
+ };
17
+ /** Demand is a set of Count.v IDs read by the goal or a numeric refuted core.
18
+ * This function neither extracts the core nor asserts any returned inequality.
19
+ * Pass its ordinary CountInsts through countedGoal/mcGoal and checkCounted/checkMC.
20
+ * Every instance retains the SAME pointwise witness alternative, including raw
21
+ * map-valued "indicators" and positive pairs that are not actually disjoint.
22
+ *
23
+ * Only same-bound groups containing a demanded ID are normalized. Within them,
24
+ * two-atom truth tables index declared singleton regions (no synthetic counts).
25
+ * Requested indicators get both partition directions; requested regions also
26
+ * request identities containing that region. Positive pairs use only singleton
27
+ * memberships they read, and three-region families selected by demanded regions
28
+ * or a primitive map read shared with the positive bodies. There is no exhaustive
29
+ * union pool, no Cartesian product of all positive pairs, and no final slice.
30
+ *
31
+ * Work includes every input/AST scan and each candidate visit. Individual input
32
+ * ASTs are limited to 4096 nodes and depth 128 before recursive normalization;
33
+ * an incomplete name scan returns [] so witnesses cannot capture unseen names.
34
+ * Runtime is bounded by maxWork times these fixed local bounds; memory is O(maxWork).
35
+ * No input-position cap: late demanded families are not discarded at count 128.
36
+ */
37
+ export declare function proposeCountDemand(counts: Count[], demanded: ReadonlySet<string>, options?: CountDemandOptions): CountInst[];
@@ -0,0 +1,361 @@
1
+ // Untrusted, demand-directed proposals for the existing single-witness count rule.
2
+ import { x } from "./machine.js";
3
+ import { substE } from "../kernel/kernel.js";
4
+ const stable = (v) => JSON.stringify(v, (_, a) => a && typeof a === 'object' && !Array.isArray(a)
5
+ ? Object.fromEntries(Object.entries(a).sort(([a], [b]) => a.localeCompare(b))) : a);
6
+ const limit = (n, fallback, cap) => n === undefined ? fallback
7
+ : Number.isSafeInteger(n) && n >= 0 ? Math.min(n, cap) : 0;
8
+ const evaluate = (b, values) => typeof b === 'boolean' ? b
9
+ : 'atom' in b ? values.get(b.atom) : 'not' in b ? !evaluate(b.not, values)
10
+ : 'and' in b ? b.and.every(c => evaluate(c, values)) : b.or.some(c => evaluate(c, values));
11
+ // Equality is symmetric; no arithmetic predicate is identified with membership
12
+ // merely because it agrees on 0/1 values. Raw get bodies remain candidates only.
13
+ const atomKey = (c) => c.op === '=' && stable(c.a) > stable(c.b)
14
+ ? stable({ ...c, a: c.b, b: c.a }) : stable(c);
15
+ /** Demand is a set of Count.v IDs read by the goal or a numeric refuted core.
16
+ * This function neither extracts the core nor asserts any returned inequality.
17
+ * Pass its ordinary CountInsts through countedGoal/mcGoal and checkCounted/checkMC.
18
+ * Every instance retains the SAME pointwise witness alternative, including raw
19
+ * map-valued "indicators" and positive pairs that are not actually disjoint.
20
+ *
21
+ * Only same-bound groups containing a demanded ID are normalized. Within them,
22
+ * two-atom truth tables index declared singleton regions (no synthetic counts).
23
+ * Requested indicators get both partition directions; requested regions also
24
+ * request identities containing that region. Positive pairs use only singleton
25
+ * memberships they read, and three-region families selected by demanded regions
26
+ * or a primitive map read shared with the positive bodies. There is no exhaustive
27
+ * union pool, no Cartesian product of all positive pairs, and no final slice.
28
+ *
29
+ * Work includes every input/AST scan and each candidate visit. Individual input
30
+ * ASTs are limited to 4096 nodes and depth 128 before recursive normalization;
31
+ * an incomplete name scan returns [] so witnesses cannot capture unseen names.
32
+ * Runtime is bounded by maxWork times these fixed local bounds; memory is O(maxWork).
33
+ * No input-position cap: late demanded families are not discarded at count 128.
34
+ */
35
+ export function proposeCountDemand(counts, demanded, options = {}) {
36
+ const cap = limit(options.maxFacts, 128, 128);
37
+ let work = limit(options.maxWork, 200_000, 1_000_000);
38
+ const take = () => work > 0 && (--work >= 0);
39
+ if (!cap || !work || !demanded.size)
40
+ return [];
41
+ const used = new Set(), byId = new Map();
42
+ // Freshness must cover even counts in unrelated families, not only emitted terms.
43
+ const inspect = (value) => {
44
+ const stack = [[value, 0]];
45
+ let nodes = 0;
46
+ while (stack.length) {
47
+ if (!take() || ++nodes > 4096)
48
+ return false;
49
+ const [v, depth] = stack.pop();
50
+ if (depth > 128)
51
+ return false;
52
+ if (typeof v === 'string')
53
+ used.add(v);
54
+ else if (v && typeof v === 'object') {
55
+ for (const child of Object.values(v)) {
56
+ if (!take())
57
+ return false;
58
+ stack.push([child, depth + 1]);
59
+ }
60
+ }
61
+ }
62
+ return true;
63
+ };
64
+ for (const c of counts) {
65
+ if (!take() || !inspect(c))
66
+ return [];
67
+ if (!byId.has(c.v))
68
+ byId.set(c.v, c); // matches kernel findCount
69
+ }
70
+ for (const name of options.reserved ?? []) {
71
+ if (!take())
72
+ return [];
73
+ used.add(name);
74
+ }
75
+ let serial = 0;
76
+ const fresh = () => {
77
+ while (take()) {
78
+ const name = `demand$${serial++}`;
79
+ if (!used.has(name)) {
80
+ used.add(name);
81
+ return name;
82
+ }
83
+ }
84
+ return undefined;
85
+ };
86
+ const member = fresh();
87
+ if (!member)
88
+ return [];
89
+ const requested = [], bounds = new Set();
90
+ for (const id of demanded) {
91
+ if (!take())
92
+ return [];
93
+ const c = byId.get(id);
94
+ if (c) {
95
+ requested.push(c);
96
+ bounds.add(stable(c.below));
97
+ }
98
+ }
99
+ if (!requested.length)
100
+ return [];
101
+ const groups = new Map();
102
+ // Demand first also chooses the demanded representative of duplicate regions.
103
+ for (const c of requested) {
104
+ const key = stable(c.below), group = groups.get(key) ?? [];
105
+ group.push(c);
106
+ groups.set(key, group);
107
+ }
108
+ for (const c of byId.values()) {
109
+ if (!take())
110
+ return [];
111
+ if (demanded.has(c.v))
112
+ continue;
113
+ const key = stable(c.below);
114
+ if (bounds.has(key))
115
+ groups.get(key).push(c);
116
+ }
117
+ const out = [], emitted = new Set();
118
+ const emit = (below, terms) => {
119
+ if (out.length >= cap || !take())
120
+ return;
121
+ // Do not emit identities with self-cancellation or repeated cover IDs.
122
+ if (new Set(terms.map(([, id]) => id)).size !== terms.length)
123
+ return;
124
+ const signature = stable([below, [...terms].sort((a, b) => a[1].localeCompare(b[1]))]);
125
+ if (emitted.has(signature))
126
+ return;
127
+ const wit = fresh();
128
+ if (!wit)
129
+ return;
130
+ emitted.add(signature);
131
+ out.push({ wit, below, terms, unit: 0 });
132
+ };
133
+ for (const cs of groups.values()) {
134
+ if (!work || out.length >= cap)
135
+ break;
136
+ const views = new Map(), families = new Map();
137
+ const indicators = new Map(), memberships = new Map();
138
+ for (const count of cs) {
139
+ if (!take())
140
+ break;
141
+ const e = substE(count.body, { vars: { [count.key]: x(member) }, maps: {}, flags: {} });
142
+ const members = new Set(), atoms = new Set();
143
+ const scan = (v) => {
144
+ if (!take() || !v || typeof v !== 'object')
145
+ return;
146
+ if ('op' in v && v.op === 'get')
147
+ members.add(stable(v));
148
+ for (const child of Object.values(v))
149
+ scan(child);
150
+ };
151
+ scan(e);
152
+ const cond = (c) => {
153
+ if (!take() || c.op === 'flag' || c.op === 'all')
154
+ return undefined;
155
+ if (c.op === 'not') {
156
+ const b = cond(c.x);
157
+ return b === undefined ? undefined : { not: b };
158
+ }
159
+ if (c.op === 'and' || c.op === 'or') {
160
+ const bs = c.xs.map(cond);
161
+ return bs.some(b => b === undefined) ? undefined : { [c.op]: bs };
162
+ }
163
+ const a = atomKey(c);
164
+ atoms.add(a);
165
+ return atoms.size > 2 ? undefined : { atom: a };
166
+ };
167
+ const body = (v) => {
168
+ if (!take())
169
+ return undefined;
170
+ if (v.op === 'const')
171
+ return v.value === 0 ? false : v.value === 1 ? true : undefined;
172
+ if (v.op !== 'if')
173
+ return undefined;
174
+ const c = cond(v.c), a = body(v.then), b = body(v.else);
175
+ return c === undefined || a === undefined || b === undefined ? undefined
176
+ : { or: [{ and: [c, a] }, { and: [{ not: c }, b] }] };
177
+ };
178
+ const f = body(e), keys = [...atoms].sort();
179
+ const view = { count, atoms: keys, members };
180
+ if (f !== undefined && keys.length <= 2) {
181
+ view.mask = 0;
182
+ for (let bits = 0; bits < 2 ** keys.length; bits++) {
183
+ if (evaluate(f, new Map(keys.map((k, i) => [k, !!(bits & (1 << i))]))))
184
+ view.mask |= 1 << bits;
185
+ }
186
+ }
187
+ if (!work)
188
+ break; // never use partial metadata
189
+ if (e.op === 'get')
190
+ view.indicator = atomKey({ op: '=', a: e, b: { op: 'const', value: 1 } });
191
+ else if (keys.length === 1 && (view.mask === 1 || view.mask === 2))
192
+ view.indicator = keys[0];
193
+ views.set(count.v, view);
194
+ if (view.indicator) {
195
+ const list = indicators.get(view.indicator) ?? [];
196
+ list.push(view);
197
+ indicators.set(view.indicator, list);
198
+ for (const m of members) {
199
+ const vs = memberships.get(m) ?? [];
200
+ vs.push(view);
201
+ memberships.set(m, vs);
202
+ }
203
+ }
204
+ if (keys.length !== 2 || ![1, 2, 4, 8].includes(view.mask ?? 0))
205
+ continue;
206
+ const id = stable(keys), family = families.get(id) ?? { atoms: keys, regions: new Map(), members: new Set() };
207
+ if (!family.regions.has(view.mask))
208
+ family.regions.set(view.mask, view);
209
+ for (const m of members)
210
+ family.members.add(m);
211
+ families.set(id, family);
212
+ }
213
+ const byAtom = new Map(), byMember = new Map(), demandFamilies = new Set();
214
+ for (const family of families.values()) {
215
+ if (!take())
216
+ break;
217
+ for (const atom of family.atoms) {
218
+ const fs = byAtom.get(atom) ?? [];
219
+ fs.push(family);
220
+ byAtom.set(atom, fs);
221
+ }
222
+ for (const m of family.members) {
223
+ const fs = byMember.get(m) ?? [];
224
+ fs.push(family);
225
+ byMember.set(m, fs);
226
+ }
227
+ if ([...family.regions.values()].some(v => demanded.has(v.count.v)))
228
+ demandFamilies.add(family);
229
+ }
230
+ const partition = (v, family, required) => {
231
+ if (!take() || out.length + 2 > cap)
232
+ return;
233
+ let mask;
234
+ if (v.indicator) {
235
+ const index = family.atoms.indexOf(v.indicator);
236
+ if (index < 0)
237
+ return;
238
+ const on = v.mask !== 1;
239
+ mask = [0, 1, 2, 3].reduce((m, bits) => !!(bits & (1 << index)) === on ? m | (1 << bits) : m, 0);
240
+ }
241
+ else if (stable(v.atoms) === stable(family.atoms) && v.mask !== undefined)
242
+ mask = v.mask;
243
+ else
244
+ return;
245
+ const parts = [];
246
+ for (const bit of [1, 2, 4, 8])
247
+ if (mask & bit) {
248
+ const part = family.regions.get(bit);
249
+ if (!part)
250
+ return;
251
+ parts.push(part);
252
+ }
253
+ if (parts.length < 2 || (required && !parts.some(p => p.count.v === required)))
254
+ return;
255
+ for (const sign of [1, -1])
256
+ emit(v.count.below, [[sign, v.count.v], ...parts.map(p => [-sign, p.count.v])]);
257
+ };
258
+ if (options.partitions !== false)
259
+ for (const c of requested) {
260
+ if (!take() || out.length >= cap)
261
+ break;
262
+ const v = views.get(c.v);
263
+ if (!v)
264
+ continue;
265
+ if (v.indicator)
266
+ for (const f of byAtom.get(v.indicator) ?? []) {
267
+ if (!work)
268
+ break;
269
+ partition(v, f);
270
+ }
271
+ else {
272
+ const f = families.get(stable(v.atoms));
273
+ if (!f)
274
+ continue;
275
+ partition(v, f);
276
+ for (const atom of f.atoms)
277
+ for (const indicator of indicators.get(atom) ?? []) {
278
+ if (!work)
279
+ break;
280
+ partition(indicator, f, c.v);
281
+ }
282
+ }
283
+ }
284
+ const seenPairs = new Set();
285
+ const pair = (aId, bId) => {
286
+ if (!take() || out.length >= cap || aId === bId || (!demanded.has(aId) && !demanded.has(bId)))
287
+ return;
288
+ const a = views.get(aId), b = views.get(bId);
289
+ if (!a || !b)
290
+ return;
291
+ const key = stable([aId, bId].sort());
292
+ if (seenPairs.has(key))
293
+ return;
294
+ seenPairs.add(key);
295
+ const selected = new Set(), covers = new Set();
296
+ for (const f of demandFamilies) {
297
+ if (!take())
298
+ return;
299
+ selected.add(f);
300
+ }
301
+ for (const m of new Set([...a.members, ...b.members])) {
302
+ if (!take())
303
+ return;
304
+ for (const f of byMember.get(m) ?? []) {
305
+ if (!take())
306
+ return;
307
+ selected.add(f);
308
+ }
309
+ for (const v of memberships.get(m) ?? []) {
310
+ if (!take())
311
+ return;
312
+ covers.add(v);
313
+ }
314
+ }
315
+ const add = (cover) => emit(a.count.below, [[1, aId], [1, bId], ...cover.map(v => [-1, v.count.v])]);
316
+ // A cover is considered only here, at the requested pair, never pre-expanded.
317
+ for (const f of selected) {
318
+ if (!take() || out.length >= cap)
319
+ return;
320
+ const cover = [2, 4, 8].map(bit => f.regions.get(bit));
321
+ if (cover.every((v) => v !== undefined))
322
+ add(cover);
323
+ }
324
+ for (const v of covers) {
325
+ if (!take() || out.length >= cap)
326
+ return;
327
+ add([v]);
328
+ }
329
+ };
330
+ if (options.pairs) {
331
+ for (const [a, b] of options.pairs) {
332
+ if (!work || out.length >= cap)
333
+ break;
334
+ pair(a, b);
335
+ }
336
+ }
337
+ else {
338
+ const ds = cs.filter(c => demanded.has(c.v));
339
+ for (let i = 0; i < ds.length && work && out.length < cap; i++) {
340
+ for (let j = i + 1; j < ds.length && work && out.length < cap; j++)
341
+ pair(ds[i].v, ds[j].v);
342
+ }
343
+ // Raw carried indicators are useful partners even when absent from the
344
+ // goal/core. Visit them before region columns used primarily as covers.
345
+ // This is an AST-shape choice, independent of map names or read indices.
346
+ for (const raw of [true, false])
347
+ for (const a of ds)
348
+ for (const b of cs) {
349
+ if (!take() || out.length >= cap)
350
+ break;
351
+ if (demanded.has(b.v) || (b.body.op === 'get') !== raw)
352
+ continue;
353
+ const left = views.get(a.v), right = views.get(b.v);
354
+ if (!raw && (!left || !right || ![...left.members].some(m => right.members.has(m))))
355
+ continue;
356
+ pair(a.v, b.v);
357
+ }
358
+ }
359
+ }
360
+ return out;
361
+ }
@@ -0,0 +1,52 @@
1
+ import { type C, type E } from './machine.js';
2
+ import { type Count, type CountDecl, type CountInst, type MapRead } from '../kernel/kernel.js';
3
+ export type CountFactOptions = {
4
+ maxAtoms?: number;
5
+ maxCounts?: number;
6
+ maxFacts?: number;
7
+ reserved?: Iterable<string>;
8
+ };
9
+ export type CountRegions = {
10
+ below: E;
11
+ atoms: C[];
12
+ counts: string[];
13
+ rows: number[][];
14
+ };
15
+ export type CountFacts = {
16
+ insts: CountInst[];
17
+ regions: CountRegions[];
18
+ skipped: {
19
+ count: string;
20
+ reason: string;
21
+ }[];
22
+ };
23
+ /** Propose Boolean identities, subset bounds and quorum intersection instances.
24
+ * At most 2^maxAtoms rows per same-bound group. Non-Boolean counts and over-wide
25
+ * groups are reported, never silently treated as sets. Atom valuations deliberately
26
+ * overapproximate arithmetic: the kernel, not this table, decides their feasibility.
27
+ */
28
+ export declare function proposeCountFacts(counts: Count[], context?: unknown, options?: CountFactOptions): CountFacts;
29
+ export type CountPickAudit = {
30
+ count: string;
31
+ below: number;
32
+ abstract: number;
33
+ actual?: number;
34
+ reason?: string;
35
+ };
36
+ /** Ask for the existing distinct-key lower-bound rule at concrete member keys.
37
+ * The evaluator is supplied by the caller's model. It is only a heuristic: even
38
+ * a lying evaluator cannot bypass checkMC's pickHyp, which re-reads each body.
39
+ * Both scanning and certificate size are bounded; large universes are reported.
40
+ */
41
+ export declare function proposeCountPicks(counts: Count[], valueOf: (e: E) => number, options?: {
42
+ maxBelow?: number;
43
+ maxKeys?: number;
44
+ }): {
45
+ picks: [string, E[]][];
46
+ audit: CountPickAudit[];
47
+ };
48
+ /** Instantiate existing machine reads, including packed two-parameter keys.
49
+ * Mirrors the untrusted side of toCount; mcGoal/checkMC remain the authority.
50
+ * Returns undefined rather than guessing a decomposition the checker won't use.
51
+ */
52
+ export declare function countsForReads(decls: CountDecl[], reads: MapRead[]): Count[] | undefined;