gdp-wired 0.46.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.
- package/README.md +18 -5
- package/dist/core/carry.d.ts +15 -0
- package/dist/core/carry.js +651 -0
- package/dist/core/cli.d.ts +4 -1
- package/dist/core/cli.js +31 -11
- package/dist/core/count-cases.d.ts +24 -0
- package/dist/core/count-cases.js +320 -0
- package/dist/core/count-cert-grow.d.ts +4 -0
- package/dist/core/count-cert-grow.js +37 -0
- package/dist/core/count-cert-transport.d.ts +3 -0
- package/dist/core/count-cert-transport.js +58 -0
- package/dist/core/count-completion.d.ts +9 -0
- package/dist/core/count-completion.js +27 -0
- package/dist/core/count-cubes.d.ts +8 -0
- package/dist/core/count-cubes.js +69 -0
- package/dist/core/count-demand.d.ts +37 -0
- package/dist/core/count-demand.js +361 -0
- package/dist/core/count-facts.d.ts +52 -0
- package/dist/core/count-facts.js +247 -0
- package/dist/core/count-focus-proof.d.ts +8 -0
- package/dist/core/count-focus-proof.js +140 -0
- package/dist/core/count-focused.d.ts +22 -0
- package/dist/core/count-focused.js +676 -0
- package/dist/core/count-ranges.d.ts +5 -0
- package/dist/core/count-ranges.js +65 -0
- package/dist/core/count-recovery/action-guard.d.ts +5 -0
- package/dist/core/count-recovery/action-guard.js +51 -0
- package/dist/core/count-recovery/boolean-core.d.ts +16 -0
- package/dist/core/count-recovery/boolean-core.js +258 -0
- package/dist/core/count-recovery/carried-case.d.ts +2 -0
- package/dist/core/count-recovery/carried-case.js +24 -0
- package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
- package/dist/core/count-recovery/conditional-check-cache.js +130 -0
- package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
- package/dist/core/count-recovery/conditional-cuts.js +318 -0
- package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
- package/dist/core/count-recovery/conditional-proof.js +306 -0
- package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
- package/dist/core/count-recovery/derivability-closure.js +533 -0
- package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
- package/dist/core/count-recovery/empty-count-source.js +40 -0
- package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
- package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
- package/dist/core/count-recovery/field-case.d.ts +5 -0
- package/dist/core/count-recovery/field-case.js +89 -0
- package/dist/core/count-recovery/field-demand.d.ts +46 -0
- package/dist/core/count-recovery/field-demand.js +47 -0
- package/dist/core/count-recovery/field-only.d.ts +3 -0
- package/dist/core/count-recovery/field-only.js +18 -0
- package/dist/core/count-recovery/fold-path.d.ts +6 -0
- package/dist/core/count-recovery/fold-path.js +35 -0
- package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
- package/dist/core/count-recovery/frame-lemmas.js +120 -0
- package/dist/core/count-recovery/guard-cases.d.ts +12 -0
- package/dist/core/count-recovery/guard-cases.js +138 -0
- package/dist/core/count-recovery/key-closure.d.ts +14 -0
- package/dist/core/count-recovery/key-closure.js +62 -0
- package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
- package/dist/core/count-recovery/leaf-allowance.js +7 -0
- package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
- package/dist/core/count-recovery/lift-count-proxies.js +175 -0
- package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
- package/dist/core/count-recovery/live-dependencies.js +76 -0
- package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
- package/dist/core/count-recovery/member-inclusions.js +168 -0
- package/dist/core/count-recovery/member-premises.d.ts +29 -0
- package/dist/core/count-recovery/member-premises.js +114 -0
- package/dist/core/count-recovery/natural-cases.d.ts +6 -0
- package/dist/core/count-recovery/natural-cases.js +59 -0
- package/dist/core/count-recovery/ops.d.ts +6 -0
- package/dist/core/count-recovery/ops.js +8 -0
- package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
- package/dist/core/count-recovery/pre-state-source.js +18 -0
- package/dist/core/count-recovery/rebase-path.d.ts +9 -0
- package/dist/core/count-recovery/rebase-path.js +70 -0
- package/dist/core/count-recovery/rename-proof.d.ts +2 -0
- package/dist/core/count-recovery/rename-proof.js +102 -0
- package/dist/core/count-recovery/row-cases.d.ts +16 -0
- package/dist/core/count-recovery/row-cases.js +291 -0
- package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
- package/dist/core/count-recovery/scalar-guards.js +291 -0
- package/dist/core/count-recovery/source-memo.d.ts +12 -0
- package/dist/core/count-recovery/source-memo.js +67 -0
- package/dist/core/count-select.d.ts +16 -0
- package/dist/core/count-select.js +24 -0
- package/dist/core/count-support.d.ts +598 -0
- package/dist/core/count-support.js +2080 -0
- package/dist/core/count-unions.d.ts +2 -0
- package/dist/core/count-unions.js +139 -0
- package/dist/core/count-witness-flags.d.ts +9 -0
- package/dist/core/count-witness-flags.js +37 -0
- package/dist/core/count-write-frames.d.ts +12 -0
- package/dist/core/count-write-frames.js +206 -0
- package/dist/core/counts.d.ts +20 -2
- package/dist/core/counts.js +807 -156
- package/dist/core/dsl.d.ts +50 -6
- package/dist/core/dsl.js +307 -65
- package/dist/core/effect-schema.d.ts +90 -0
- package/dist/core/effect-schema.js +500 -0
- package/dist/core/effect.d.ts +3 -7
- package/dist/core/effect.js +10 -65
- package/dist/core/exists.d.ts +1 -1
- package/dist/core/exists.js +80 -15
- package/dist/core/extract-check.d.ts +7 -1
- package/dist/core/extract-check.js +161 -19
- package/dist/core/extract-go-check.js +24 -4
- package/dist/core/extract-go.d.ts +7 -0
- package/dist/core/extract-go.js +29 -2
- package/dist/core/extract-read.d.ts +31 -9
- package/dist/core/extract-read.js +36 -11
- package/dist/core/extract-rs.js +58 -14
- package/dist/core/extract-shapes.d.ts +1 -0
- package/dist/core/extract-shapes.js +190 -27
- package/dist/core/extract-standalone.d.ts +3 -0
- package/dist/core/extract-standalone.js +265 -0
- package/dist/core/extract.d.ts +1 -0
- package/dist/core/extract.js +25 -5
- package/dist/core/field-laws.d.ts +2 -0
- package/dist/core/field-laws.js +100 -0
- package/dist/core/finite-frontiers.d.ts +5 -0
- package/dist/core/finite-frontiers.js +116 -0
- package/dist/core/finite-houdini.d.ts +52 -0
- package/dist/core/finite-houdini.js +460 -0
- package/dist/core/finite-model.d.ts +13 -0
- package/dist/core/finite-model.js +68 -0
- package/dist/core/finite-tiny.d.ts +72 -0
- package/dist/core/finite-tiny.js +1052 -0
- package/dist/core/infer.d.ts +1 -1
- package/dist/core/infer.js +32 -9
- package/dist/core/laws.d.ts +2 -1
- package/dist/core/laws.js +76 -19
- package/dist/core/lemma-cuts.d.ts +38 -0
- package/dist/core/lemma-cuts.js +365 -0
- package/dist/core/machine.d.ts +39 -2
- package/dist/core/machine.js +240 -35
- package/dist/core/mutate.d.ts +55 -0
- package/dist/core/mutate.js +210 -0
- package/dist/core/native-domain.d.ts +6 -0
- package/dist/core/native-domain.js +114 -0
- package/dist/core/native-law.d.ts +3 -0
- package/dist/core/native-law.js +174 -0
- package/dist/core/quorum-laws.d.ts +5 -0
- package/dist/core/quorum-laws.js +239 -0
- package/dist/core/relational.js +10 -2
- package/dist/core/relevance.d.ts +4 -0
- package/dist/core/relevance.js +79 -0
- package/dist/core/rs/Cargo.toml +1 -1
- package/dist/core/rs/src/main.rs +458 -9
- package/dist/core/runtime-wall.d.ts +3 -0
- package/dist/core/runtime-wall.js +4 -0
- package/dist/core/sample-step.d.ts +16 -0
- package/dist/core/sample-step.js +28 -0
- package/dist/core/source-check.d.ts +44 -0
- package/dist/core/source-check.js +126 -0
- package/dist/core/source-diagnostic.d.ts +1 -0
- package/dist/core/source-diagnostic.js +34 -0
- package/dist/core/support-budget.d.ts +2 -0
- package/dist/core/support-budget.js +6 -0
- package/dist/core/system.js +2 -0
- package/dist/core/wall.d.ts +6 -0
- package/dist/core/wall.js +23 -0
- package/dist/core/watch.js +4 -0
- package/dist/kernel/cdcl.d.ts +4 -0
- package/dist/kernel/cdcl.js +20 -5
- package/dist/kernel/kernel.d.ts +10 -0
- package/dist/kernel/kernel.js +56 -3
- package/dist/kernel/model-sequence.d.ts +2 -2
- package/dist/kernel/model-sequence.js +4 -3
- package/dist/kernel/model-values.d.ts +2 -0
- package/dist/kernel/model-values.js +60 -0
- package/dist/kernel/prove.d.ts +10 -0
- package/dist/kernel/prove.js +56 -8
- package/dist/kernel/prover-rows.d.ts +29 -0
- package/dist/kernel/prover-rows.js +114 -0
- package/dist/lean-kernel/emit.d.ts +39 -2
- package/dist/lean-kernel/emit.js +136 -24
- package/dist/lean-kernel/lean-terms.d.ts +1 -0
- package/dist/lean-kernel/lean-terms.js +13 -5
- package/dist/lean-kernel/library.js +4 -0
- package/dist/lean-kernel/qalpha.d.ts +7 -0
- package/dist/lean-kernel/qalpha.js +40 -0
- package/package.json +5 -1
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
// A count cell that is free in the abstraction can make MBQI miss a useful law
|
|
2
|
+
// instance. Bridge the goal's keys with request keys for unary/binary count laws.
|
|
3
|
+
// These are ordinary universal instances, never additional arithmetic facts.
|
|
4
|
+
export function countLawSeeds(m, laws, at, own, request, fromRequestOnly = false, options = {}) {
|
|
5
|
+
let out = at;
|
|
6
|
+
for (const law of laws) {
|
|
7
|
+
if (!law.forall || (law.keys?.length ?? 0) > 1)
|
|
8
|
+
continue;
|
|
9
|
+
let counted = false;
|
|
10
|
+
walkOnce(law.holds, (o) => { if (o.op === 'get' && m.counts?.[o.map])
|
|
11
|
+
counted = true; });
|
|
12
|
+
if (!counted)
|
|
13
|
+
continue;
|
|
14
|
+
const n = 1 + (law.keys?.length ?? 0), candidates = n === 1 ? [...own, ...request].map(x => [x]) : [...options.requestDiagonals ? request.map(b => [b, b]) : [], ...own.flatMap(a => request.flatMap(b => fromRequestOnly ? [[b, a]] : [[a, b], [b, a]]))];
|
|
15
|
+
const have = at[law.name] ?? [], known = new Set();
|
|
16
|
+
for (let i = 0; i < have.length; i += n)
|
|
17
|
+
known.add(JSON.stringify(have.slice(i, i + n)));
|
|
18
|
+
const extra = candidates.slice(0, 32).filter(t => { const k = JSON.stringify(t); if (known.has(k))
|
|
19
|
+
return false; known.add(k); return true; }).flat();
|
|
20
|
+
if (extra.length) {
|
|
21
|
+
if (out === at)
|
|
22
|
+
out = { ...at };
|
|
23
|
+
out[law.name] = [...have, ...extra];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
// Universal field guards need ordinary goal-row instances even when no
|
|
29
|
+
// count witness mentions that row. Do not expand carried-set predicates here.
|
|
30
|
+
export function fieldGuardSeeds(m, at, keys) {
|
|
31
|
+
let out = at;
|
|
32
|
+
for (const [name, q] of Object.entries(m.step.flags ?? {})) {
|
|
33
|
+
if (q.op !== 'all' || q.keys?.length)
|
|
34
|
+
continue;
|
|
35
|
+
const maps = new Set();
|
|
36
|
+
walkOnce(q.c, (o) => { if (o.op === 'get')
|
|
37
|
+
maps.add(o.map); });
|
|
38
|
+
if (!maps.size || [...maps].some(map => (m.inputMaps ?? []).includes(map) || map in (m.counts ?? {})))
|
|
39
|
+
continue;
|
|
40
|
+
const flag = `?${name}`, had = out[flag] ?? [], seen = new Set(had.map(t => JSON.stringify(t))), extra = [];
|
|
41
|
+
for (const key of keys.slice(0, 32)) {
|
|
42
|
+
const id = JSON.stringify(key);
|
|
43
|
+
if (!seen.has(id)) {
|
|
44
|
+
seen.add(id);
|
|
45
|
+
extra.push(key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (extra.length) {
|
|
49
|
+
if (out === at)
|
|
50
|
+
out = { ...at };
|
|
51
|
+
out[flag] = [...had, ...extra];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
// Coordinates of a count key are row parameters too. Treating only a bare key
|
|
57
|
+
// as a row loses the second row of a packed two-row count; arithmetic witnesses
|
|
58
|
+
// outside read keys are intentionally excluded.
|
|
59
|
+
export function rowKeyNames(m, law) {
|
|
60
|
+
const names = new Set();
|
|
61
|
+
walkOnce(law.holds, (o) => {
|
|
62
|
+
if (o.op !== 'get')
|
|
63
|
+
return;
|
|
64
|
+
if (o.key.op === 'var')
|
|
65
|
+
names.add(o.key.name);
|
|
66
|
+
if (m.counts?.[o.map])
|
|
67
|
+
walkOnce(o.key, (k) => { if (k.op === 'var')
|
|
68
|
+
names.add(k.name); });
|
|
69
|
+
});
|
|
70
|
+
return names;
|
|
71
|
+
}
|
|
72
|
+
// A copied row changes the row coordinates, not an invariant's arithmetic
|
|
73
|
+
// witnesses. Bridge the target instance at those coordinates without enumerating
|
|
74
|
+
// fresh half-cardinality tuples (MBQI proposes those separately).
|
|
75
|
+
export function countRowBridges(m, law, at, own, request) {
|
|
76
|
+
const names = [law.forall, ...law.keys ?? []];
|
|
77
|
+
if (!law.forall || own.length !== names.length)
|
|
78
|
+
return at;
|
|
79
|
+
let counted = false;
|
|
80
|
+
walkOnce(law.holds, (o) => { if (o.op === 'get' && m.counts?.[o.map])
|
|
81
|
+
counted = true; });
|
|
82
|
+
if (!counted)
|
|
83
|
+
return at;
|
|
84
|
+
const rows = rowKeyNames(m, law), arity = names.length, had = at[law.name] ?? [], seen = new Set();
|
|
85
|
+
for (let i = 0; i < had.length; i += arity)
|
|
86
|
+
seen.add(JSON.stringify(had.slice(i, i + arity)));
|
|
87
|
+
const out = [];
|
|
88
|
+
for (const [i, name] of names.entries())
|
|
89
|
+
if (name && rows.has(name))
|
|
90
|
+
for (const source of [...request, { op: 'const', value: 0 }]) {
|
|
91
|
+
const tuple = [...own];
|
|
92
|
+
tuple[i] = source;
|
|
93
|
+
const key = JSON.stringify(tuple);
|
|
94
|
+
if (!seen.has(key)) {
|
|
95
|
+
seen.add(key);
|
|
96
|
+
out.push(...tuple);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out.length ? { ...at, [law.name]: [...had, ...out] } : at;
|
|
100
|
+
}
|
|
101
|
+
// Count reads must not depend on a CInst witness (checkMC's freshness rule).
|
|
102
|
+
// Numeric comparison witnesses may depend on it; only coordinates occurring
|
|
103
|
+
// inside a count read's key are filtered. Existing instances are left intact.
|
|
104
|
+
export function independentCountInstances(m, laws, before, next, witnesses) {
|
|
105
|
+
const blocked = new Set(witnesses), out = { ...before };
|
|
106
|
+
let changed = false;
|
|
107
|
+
for (const [name, terms] of Object.entries(next)) {
|
|
108
|
+
const law = laws.find(l => l.name === name), had = before[name] ?? [];
|
|
109
|
+
if (terms.length <= had.length)
|
|
110
|
+
continue;
|
|
111
|
+
if (!law) {
|
|
112
|
+
out[name] = terms;
|
|
113
|
+
changed = true;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const names = [law.forall, ...law.keys ?? []], positions = new Set(), arity = names.length;
|
|
117
|
+
walkOnce(law.holds, (o) => { if (o.op === 'get' && m.counts?.[o.map])
|
|
118
|
+
walkOnce(o.key, (v) => { if (v.op === 'var') {
|
|
119
|
+
const i = names.indexOf(v.name);
|
|
120
|
+
if (i >= 0)
|
|
121
|
+
positions.add(i);
|
|
122
|
+
} }); });
|
|
123
|
+
const extra = [];
|
|
124
|
+
for (let i = had.length; i < terms.length; i += arity) {
|
|
125
|
+
const tuple = terms.slice(i, i + arity);
|
|
126
|
+
if (tuple.length !== arity)
|
|
127
|
+
continue;
|
|
128
|
+
let dependent = false;
|
|
129
|
+
for (const j of positions)
|
|
130
|
+
walkOnce(tuple[j], (o) => { if (o.op === 'var' && blocked.has(o.name))
|
|
131
|
+
dependent = true; });
|
|
132
|
+
if (!dependent)
|
|
133
|
+
extra.push(...tuple);
|
|
134
|
+
}
|
|
135
|
+
if (extra.length) {
|
|
136
|
+
out[name] = [...had, ...extra];
|
|
137
|
+
changed = true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return changed ? out : undefined;
|
|
141
|
+
}
|
|
142
|
+
// Keep a row law's column symbolic while bridging its row keys to request/source
|
|
143
|
+
// rows. Equal model values do not make the column variable the same term as a row.
|
|
144
|
+
export function fieldLawBridges(m, laws, at, own, request, column) {
|
|
145
|
+
let out = at;
|
|
146
|
+
const sources = [...request, { op: 'const', value: 0 }];
|
|
147
|
+
for (const law of laws) {
|
|
148
|
+
const arity = 1 + (law.keys?.length ?? 0);
|
|
149
|
+
if (!law.forall || arity > 3 || arity === 3 && !column)
|
|
150
|
+
continue;
|
|
151
|
+
let counted = false;
|
|
152
|
+
walkOnce(law.holds, (o) => { if (o.op === 'get' && m.counts?.[o.map])
|
|
153
|
+
counted = true; });
|
|
154
|
+
if (counted)
|
|
155
|
+
continue;
|
|
156
|
+
const tuples = arity === 1 ? [...own, ...sources].map(k => [k]) : own.flatMap(a => sources.flatMap(b => arity === 2 ? [[a, b], [b, a]] : [[a, b, column], [b, a, column]]));
|
|
157
|
+
const had = out[law.name] ?? [], seen = new Set();
|
|
158
|
+
for (let i = 0; i < had.length; i += arity)
|
|
159
|
+
seen.add(JSON.stringify(had.slice(i, i + arity)));
|
|
160
|
+
const extra = tuples.slice(0, 32).filter(t => { const k = JSON.stringify(t); if (seen.has(k))
|
|
161
|
+
return false; seen.add(k); return true; }).flat();
|
|
162
|
+
if (extra.length) {
|
|
163
|
+
if (out === at)
|
|
164
|
+
out = { ...at };
|
|
165
|
+
out[law.name] = [...had, ...extra];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
// A small first certificate, not an axiom: equivalent indicator counts and a
|
|
171
|
+
// carried set against a stored-row count. The full pool remains the fallback.
|
|
172
|
+
import { walkOnce, x } from "./machine.js";
|
|
173
|
+
import { substC, substE } from "../kernel/kernel.js";
|
|
174
|
+
import { countsForReads } from "./count-facts.js";
|
|
175
|
+
import { proposeCountDemand } from "./count-demand.js";
|
|
176
|
+
import { proposeWriteCountFrames } from "./count-write-frames.js";
|
|
177
|
+
import { activeWriteRow } from "./count-recovery/row-cases.js";
|
|
178
|
+
import { memberInclusions } from "./count-recovery/member-inclusions.js";
|
|
179
|
+
// An untrusted starting subset for row-copy cases. Keep pre-state partitions
|
|
180
|
+
// and query-local frames/copies, not every unrelated source tuple. A caller
|
|
181
|
+
// must retain its full pool for fallback and check the resulting certificate.
|
|
182
|
+
export function copyCountSubset(m, cert) {
|
|
183
|
+
const reads = new Map(cert.reads.map(r => [r[0], r])), variants = new Map(), sources = new Set();
|
|
184
|
+
for (const [map, write] of Object.entries(m.step.maps ?? {}))
|
|
185
|
+
if ('each' in write) {
|
|
186
|
+
const scan = (e) => {
|
|
187
|
+
if (e.op === 'if') {
|
|
188
|
+
scan(e.then);
|
|
189
|
+
scan(e.else);
|
|
190
|
+
}
|
|
191
|
+
else if (e.op === 'get' && e.map === map && e.key.op === 'var' && m.inputs.includes(e.key.name))
|
|
192
|
+
sources.add(e.key.name);
|
|
193
|
+
};
|
|
194
|
+
scan(write.value);
|
|
195
|
+
}
|
|
196
|
+
const keys = (id, key) => {
|
|
197
|
+
const prior = variants.get(id);
|
|
198
|
+
if (prior)
|
|
199
|
+
return prior;
|
|
200
|
+
const names = new Set();
|
|
201
|
+
walkOnce(key, (o) => { if (o.op === 'var' && !m.inputs.includes(o.name) && !m.params.includes(o.name) && !(o.name in m.state))
|
|
202
|
+
names.add(o.name); });
|
|
203
|
+
const vars = [...names], out = new Set([JSON.stringify(key)]);
|
|
204
|
+
if (vars.length <= 3)
|
|
205
|
+
for (const input of sources.size ? sources : m.inputs)
|
|
206
|
+
for (let mask = 1; mask < (1 << vars.length); mask++) {
|
|
207
|
+
const substitutions = Object.fromEntries(vars.filter((_, i) => mask & (1 << i)).map(name => [name, x(input)]));
|
|
208
|
+
out.add(JSON.stringify(substE(key, { vars: substitutions, maps: {}, flags: {} })));
|
|
209
|
+
}
|
|
210
|
+
variants.set(id, out);
|
|
211
|
+
return out;
|
|
212
|
+
};
|
|
213
|
+
return { ...cert, insts: cert.insts.filter(inst => {
|
|
214
|
+
if (inst.terms.length >= 3)
|
|
215
|
+
return inst.terms.length === 3 && inst.terms.every(([, id]) => !(reads.get(id)?.[1].endsWith('#') ?? true));
|
|
216
|
+
if (inst.terms.length === 1)
|
|
217
|
+
return reads.get(inst.terms[0][1])?.[1].endsWith('#') ?? false;
|
|
218
|
+
if (inst.terms.length !== 2 || !inst.terms.every(([coefficient]) => Math.abs(coefficient) === 1))
|
|
219
|
+
return true;
|
|
220
|
+
const a = reads.get(inst.terms[0][1]), b = reads.get(inst.terms[1][1]);
|
|
221
|
+
if (!a || !b)
|
|
222
|
+
return true;
|
|
223
|
+
const post = a[1].endsWith('#') ? a : b[1].endsWith('#') ? b : undefined, pre = post === a ? b : a;
|
|
224
|
+
return !!post && pre[1] === post[1].slice(0, -1) && keys(post[0], post[2]).has(JSON.stringify(pre[2]));
|
|
225
|
+
}) };
|
|
226
|
+
}
|
|
227
|
+
// Generate directly for the conclusion's reads and their pre-state twins.
|
|
228
|
+
// This is separate from certFor's unchanged legacy prefix. At most 128
|
|
229
|
+
// structured proposals survive, and every one keeps its witness alternative.
|
|
230
|
+
export function demandCountCert(m, decls, full, claim) {
|
|
231
|
+
const wanted = new Set(), reserved = new Set([...m.inputs, ...m.params, ...Object.keys(m.state), ...full.insts.map(q => q.wit)]);
|
|
232
|
+
walkOnce(claim, (o) => {
|
|
233
|
+
if (o.op === 'var') {
|
|
234
|
+
reserved.add(o.name);
|
|
235
|
+
if (full.reads.some(read => read[0] === o.name))
|
|
236
|
+
wanted.add(o.name);
|
|
237
|
+
}
|
|
238
|
+
if (o.op === 'get')
|
|
239
|
+
for (const read of full.reads)
|
|
240
|
+
if (read[1] === o.map && JSON.stringify(read[2]) === JSON.stringify(o.key))
|
|
241
|
+
wanted.add(read[0]);
|
|
242
|
+
});
|
|
243
|
+
if (wanted.size < 2)
|
|
244
|
+
return full;
|
|
245
|
+
const cs = countsForReads(decls, full.reads);
|
|
246
|
+
if (!cs)
|
|
247
|
+
return full;
|
|
248
|
+
const byId = new Map(cs.map(c => [c.v, c])), byCell = new Map(full.reads.map(r => [JSON.stringify([r[1], r[2]]), r[0]])), heads = new Set(wanted), projected = new Set(), actor = activeWriteRow(m, new Map());
|
|
249
|
+
for (const read of full.reads)
|
|
250
|
+
if (heads.has(read[0]) && read[1].endsWith('#')) {
|
|
251
|
+
const names = new Set();
|
|
252
|
+
walkOnce(read[2], (o) => { if (o.op === 'var' && !m.inputs.includes(o.name) && !m.params.includes(o.name))
|
|
253
|
+
names.add(o.name); });
|
|
254
|
+
const vars = [...names];
|
|
255
|
+
if (actor && vars.length <= 3)
|
|
256
|
+
for (let mask = 1; mask < (1 << vars.length); mask++) {
|
|
257
|
+
const key = substE(read[2], { vars: Object.fromEntries(vars.filter((_, i) => mask & (1 << i)).map(name => [name, x(actor)])), maps: {}, flags: {} }), id = byCell.get(JSON.stringify([read[1].slice(0, -1), key]));
|
|
258
|
+
if (id)
|
|
259
|
+
projected.add(id);
|
|
260
|
+
}
|
|
261
|
+
const twin = byCell.get(JSON.stringify([read[1].slice(0, -1), read[2]]));
|
|
262
|
+
if (twin)
|
|
263
|
+
heads.add(twin);
|
|
264
|
+
}
|
|
265
|
+
for (const id of projected)
|
|
266
|
+
wanted.add(id);
|
|
267
|
+
const pending = [...wanted], expanded = new Set(), primitiveMaps = new Set(), expandedFlags = new Set();
|
|
268
|
+
while (pending.length) {
|
|
269
|
+
const id = pending.shift();
|
|
270
|
+
if (expanded.has(id))
|
|
271
|
+
continue;
|
|
272
|
+
expanded.add(id);
|
|
273
|
+
const add = (id) => { if (id && !wanted.has(id)) {
|
|
274
|
+
wanted.add(id);
|
|
275
|
+
pending.push(id);
|
|
276
|
+
} };
|
|
277
|
+
const read = full.reads.find(r => r[0] === id);
|
|
278
|
+
if (read?.[1].endsWith('#'))
|
|
279
|
+
add(byCell.get(JSON.stringify([read[1].slice(0, -1), read[2]])));
|
|
280
|
+
const depend = (value) => {
|
|
281
|
+
walkOnce(value, (o) => {
|
|
282
|
+
if (o.op === 'get') {
|
|
283
|
+
primitiveMaps.add(o.map);
|
|
284
|
+
add(byCell.get(JSON.stringify([o.map, o.key])));
|
|
285
|
+
}
|
|
286
|
+
const name = o.op === 'flag' ? o.name : o.op === 'var' && o.name.startsWith('?') ? o.name.slice(1) : undefined;
|
|
287
|
+
if (name && !expandedFlags.has(name)) {
|
|
288
|
+
expandedFlags.add(name);
|
|
289
|
+
const flag = m.step.flags?.[name];
|
|
290
|
+
if (flag)
|
|
291
|
+
depend(flag);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
};
|
|
295
|
+
const body = byId.get(id)?.body;
|
|
296
|
+
if (body)
|
|
297
|
+
depend(body);
|
|
298
|
+
}
|
|
299
|
+
// Partition requests and positive-pair requests have separate fair shares.
|
|
300
|
+
// One early head cannot exhaust all slots before a later demanded head.
|
|
301
|
+
// Only proposal discovery uses symbolic post-state cells. Expanding a map
|
|
302
|
+
// write hides Boolean identities behind hundreds of guard atoms; all emitted
|
|
303
|
+
// witnesses are still proved against the original expanded declarations.
|
|
304
|
+
const preDecls = new Map(decls.filter(d => !d.map.endsWith('#')).map(d => [d.map, d]));
|
|
305
|
+
const postMaps = Object.fromEntries(Object.keys(m.maps ?? {}).map(map => [map, (key) => ({ op: 'get', map: `${map}#`, key })]));
|
|
306
|
+
const postVars = Object.fromEntries(Object.keys(m.state).map(name => [name, { op: 'var', name: `${name}#` }]));
|
|
307
|
+
const proposalDecls = decls.map(d => {
|
|
308
|
+
const pre = d.map.endsWith('#') ? preDecls.get(d.map.slice(0, -1)) : undefined;
|
|
309
|
+
return pre && pre.key === d.key && pre.j === d.j && pre.lo === d.lo && pre.base === d.base ? { ...d, body: substE(pre.body, { vars: postVars, maps: postMaps, flags: {} }) } : d;
|
|
310
|
+
});
|
|
311
|
+
const proposalCounts = countsForReads(proposalDecls, full.reads) ?? cs;
|
|
312
|
+
// Domain ceilings for requested Boolean counts share the existing 128 slots.
|
|
313
|
+
// They remain witness-backed proposals; no range is admitted syntactically.
|
|
314
|
+
const bounded = (body) => {
|
|
315
|
+
const todo = [body];
|
|
316
|
+
let seen = 0;
|
|
317
|
+
while (todo.length) {
|
|
318
|
+
const e = todo.pop();
|
|
319
|
+
if (++seen > 256)
|
|
320
|
+
return false;
|
|
321
|
+
if (e.op === 'const') {
|
|
322
|
+
if (e.value !== 0 && e.value !== 1)
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
else if (e.op === 'if')
|
|
326
|
+
todo.push(e.then, e.else);
|
|
327
|
+
else
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
return true;
|
|
331
|
+
};
|
|
332
|
+
walkOnce([m, decls, full], (o) => { if (o && typeof o === 'object')
|
|
333
|
+
for (const value of Object.values(o))
|
|
334
|
+
if (typeof value === 'string')
|
|
335
|
+
reserved.add(value); });
|
|
336
|
+
const ranges = [];
|
|
337
|
+
let serial = 0;
|
|
338
|
+
for (const count of cs)
|
|
339
|
+
if (wanted.has(count.v) && bounded(count.body) && ranges.length < 16) {
|
|
340
|
+
let wit;
|
|
341
|
+
do {
|
|
342
|
+
wit = `range$${serial++}`;
|
|
343
|
+
} while (reserved.has(wit));
|
|
344
|
+
reserved.add(wit);
|
|
345
|
+
ranges.push({ wit, below: count.below, terms: [[1, count.v]], unit: -1 });
|
|
346
|
+
const read = full.reads.find(r => r[0] === count.v), pre = read?.[1].endsWith('#') ? preDecls.get(read[1].slice(0, -1)) : undefined;
|
|
347
|
+
let fields = false;
|
|
348
|
+
if (pre)
|
|
349
|
+
walkOnce(pre.body, (o) => { if (o.op === '<' && o.a.op === 'get' && o.b.op === 'get')
|
|
350
|
+
fields = true; });
|
|
351
|
+
if (fields && ranges.length < 16) {
|
|
352
|
+
do {
|
|
353
|
+
wit = `range$${serial++}`;
|
|
354
|
+
} while (reserved.has(wit));
|
|
355
|
+
reserved.add(wit);
|
|
356
|
+
ranges.push({ wit, below: count.below, terms: [[1, count.v]], unit: 0 });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const proposed = [...ranges, ...proposeCountDemand(proposalCounts, wanted, { pairs: [], maxFacts: 64 - ranges.length, reserved })];
|
|
360
|
+
for (const q of proposed)
|
|
361
|
+
reserved.add(q.wit);
|
|
362
|
+
const carriedView = (e) => {
|
|
363
|
+
if (e.op === 'get')
|
|
364
|
+
return e;
|
|
365
|
+
if (e.op !== 'if' || e.then.op !== 'const' || e.then.value !== 1 || e.else.op !== 'const' || e.else.value !== 0)
|
|
366
|
+
return undefined;
|
|
367
|
+
const c = e.c;
|
|
368
|
+
if (c.op === '=') {
|
|
369
|
+
if (c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1)
|
|
370
|
+
return c.a;
|
|
371
|
+
if (c.b.op === 'get' && c.a.op === 'const' && c.a.value === 1)
|
|
372
|
+
return c.b;
|
|
373
|
+
}
|
|
374
|
+
if (c.op === '<=' && c.a.op === 'const' && c.a.value === 1 && c.b.op === 'get' || c.op === '<' && c.a.op === 'const' && c.a.value === 0 && c.b.op === 'get')
|
|
375
|
+
return c.b;
|
|
376
|
+
return undefined;
|
|
377
|
+
};
|
|
378
|
+
const carried = cs.filter(c => { const view = carriedView(c.body); return view?.op === 'get' && (wanted.has(c.v) || primitiveMaps.has(view.map)) && (m.inputMaps ?? []).includes(view.map); }), pairs = [...heads].flatMap(head => carried.filter(c => c.v !== head).map(c => [head, c.v]));
|
|
379
|
+
const share = Math.max(1, Math.floor((128 - proposed.length) / Math.max(1, pairs.length)));
|
|
380
|
+
for (const pair of pairs) {
|
|
381
|
+
if (proposed.length >= 128)
|
|
382
|
+
break;
|
|
383
|
+
const batch = proposeCountDemand(proposalCounts, new Set([...pair, ...projected]), { pairs: [pair], partitions: false, maxFacts: Math.min(share, 128 - proposed.length), reserved });
|
|
384
|
+
for (const q of batch) {
|
|
385
|
+
reserved.add(q.wit);
|
|
386
|
+
proposed.push(q);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (!proposed.length) {
|
|
390
|
+
const frames = proposeWriteCountFrames(m, decls, full, wanted);
|
|
391
|
+
return frames.length ? { reads: full.reads, insts: [...frames, ...full.insts] } : full;
|
|
392
|
+
}
|
|
393
|
+
const key = (q) => JSON.stringify({ below: q.below, unit: q.unit, terms: [...q.terms].sort((a, b) => a[1].localeCompare(b[1]) || a[0] - b[0]) }), seen = new Set(), structured = [];
|
|
394
|
+
for (const q of [...proposed, ...full.insts.filter(q => q.terms.length >= 3)]) {
|
|
395
|
+
const id = key(q);
|
|
396
|
+
if (seen.has(id))
|
|
397
|
+
continue;
|
|
398
|
+
seen.add(id);
|
|
399
|
+
structured.push(q);
|
|
400
|
+
if (structured.length === 128)
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
const merged = { reads: full.reads, insts: [...full.insts.filter(q => q.terms.length < 3), ...structured] }, frames = proposeWriteCountFrames(m, decls, merged, wanted);
|
|
404
|
+
return frames.length ? { reads: merged.reads, insts: [...frames, ...merged.insts] } : merged;
|
|
405
|
+
}
|
|
406
|
+
export function focusCountCert(m, decls, full, options = {}) {
|
|
407
|
+
const cs = countsForReads(decls, full.reads);
|
|
408
|
+
if (!cs)
|
|
409
|
+
return { reads: full.reads, insts: [] };
|
|
410
|
+
const inputs = new Set(m.inputMaps ?? []), text = (e) => JSON.stringify(e);
|
|
411
|
+
const bodies = new Map(cs.map(c => [c.v, substE(c.body, { vars: { [c.key]: x('$member') }, maps: {}, flags: {} })]));
|
|
412
|
+
const indicator = (e) => e.op === 'if' && e.then.op === 'const' && e.then.value === 1 && e.else.op === 'const' && e.else.value === 0 ? member(e.c) ?? e : e;
|
|
413
|
+
const predicate = (e) => e.op === 'if' && e.then.op === 'const' && e.then.value === 1 && e.else.op === 'const' && e.else.value === 0 ? e.c : e.op === 'get' ? { op: '=', a: e, b: { op: 'const', value: 1 } } : undefined;
|
|
414
|
+
const conjuncts = (c) => c.op === 'and' ? c.xs.flatMap(conjuncts) : [text(c)];
|
|
415
|
+
// A carried set's membership flag identifies the stored view it is a subset
|
|
416
|
+
// of. Do not compare it with every unrelated row merely because the bound fits.
|
|
417
|
+
const member = (c) => {
|
|
418
|
+
if (c.op === '=' && c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1)
|
|
419
|
+
return c.a;
|
|
420
|
+
if (c.op === '=' && c.b.op === 'get' && c.a.op === 'const' && c.a.value === 1)
|
|
421
|
+
return c.b;
|
|
422
|
+
if (c.op === '<' && c.a.op === 'const' && c.a.value === 0 && c.b.op === 'get')
|
|
423
|
+
return c.b;
|
|
424
|
+
if (c.op === '<=' && c.a.op === 'const' && c.a.value === 1 && c.b.op === 'get')
|
|
425
|
+
return c.b;
|
|
426
|
+
return undefined;
|
|
427
|
+
};
|
|
428
|
+
const views = new Map();
|
|
429
|
+
for (const q of Object.values(m.step.flags ?? {}))
|
|
430
|
+
if (q.op === 'all' && !q.keys?.length) {
|
|
431
|
+
const c = substC(q.c, { vars: { [q.key]: x('$member') }, maps: {}, flags: {} });
|
|
432
|
+
if (c.op !== 'or' || c.xs.length !== 2)
|
|
433
|
+
continue;
|
|
434
|
+
const a = c.xs[0].op === 'not' ? c.xs[0].x : undefined, b = c.xs[1];
|
|
435
|
+
const src = a && member(a), view = member(b);
|
|
436
|
+
if (src?.op !== 'get' || !inputs.has(src.map) || !view)
|
|
437
|
+
continue;
|
|
438
|
+
const got = views.get(src.map) ?? new Set();
|
|
439
|
+
got.add(text(view));
|
|
440
|
+
views.set(src.map, got);
|
|
441
|
+
}
|
|
442
|
+
const stored = new Set();
|
|
443
|
+
for (const [id, map] of full.reads) {
|
|
444
|
+
const d = decls.find(d => d.map === map);
|
|
445
|
+
if (!d || map.endsWith('#'))
|
|
446
|
+
continue;
|
|
447
|
+
let request = false;
|
|
448
|
+
walkOnce([d.below, d.body], (o) => { if (o.op === 'get' && inputs.has(o.map) || o.op === 'var' && m.inputs.includes(o.name) && ![d.j, d.lo, d.key].includes(o.name))
|
|
449
|
+
request = true; });
|
|
450
|
+
if (!request)
|
|
451
|
+
stored.add(id);
|
|
452
|
+
}
|
|
453
|
+
// Reserve existing slots for disjoint-union bounds: a prefix of partition
|
|
454
|
+
// identities must not starve the five-term family that uses those identities.
|
|
455
|
+
const algebraicSeen = new Set();
|
|
456
|
+
const eligible = options.algebraic ? full.insts.filter(q => {
|
|
457
|
+
if (q.unit !== 0 || q.terms.length < 3 || q.terms.length > 5 || !q.terms.every(([k]) => Math.abs(k) === 1))
|
|
458
|
+
return false;
|
|
459
|
+
const id = text({ below: q.below, unit: q.unit, terms: [...q.terms].sort((a, b) => a[1].localeCompare(b[1]) || a[0] - b[0]) });
|
|
460
|
+
if (algebraicSeen.has(id))
|
|
461
|
+
return false;
|
|
462
|
+
algebraicSeen.add(id);
|
|
463
|
+
return true;
|
|
464
|
+
}) : [];
|
|
465
|
+
const unionPriority = (q) => {
|
|
466
|
+
const positive = q.terms.filter(([k]) => k === 1).map(([, id]) => id);
|
|
467
|
+
const post = positive.some(id => full.reads.some(r => r[0] === id && r[1].endsWith('#')));
|
|
468
|
+
const carried = positive.some(id => { const b = bodies.get(id), e = b && indicator(b); return e?.op === 'get' && inputs.has(e.map) && (!options.carried || e.map === options.carried); });
|
|
469
|
+
return post && carried ? (q.terms.filter(([k]) => k === -1).every(([, id]) => stored.has(id)) ? 2 : 1) : 0;
|
|
470
|
+
};
|
|
471
|
+
const unions = eligible.filter(q => q.terms.length === 5).sort((a, b) => unionPriority(b) - unionPriority(a)).slice(0, 8);
|
|
472
|
+
const regions = new Set(unions.flatMap(q => q.terms.filter(([k]) => k === -1).map(([, id]) => id)));
|
|
473
|
+
const requestSizes = new Set([...bodies].filter(([, b]) => { const e = indicator(b); return e.op === 'get' && inputs.has(e.map); }).map(([id]) => id));
|
|
474
|
+
const overlap = (q) => q.terms.filter(([, id]) => regions.has(id)).length;
|
|
475
|
+
// Prioritize actual pointwise Boolean identities, not every triple that
|
|
476
|
+
// happens to mention an input size. This only ranks untrusted proposals.
|
|
477
|
+
const booleanIdentity = (q) => {
|
|
478
|
+
const ps = q.terms.map(([, id]) => { const b = bodies.get(id); return b && predicate(b); });
|
|
479
|
+
if (ps.some(p => !p))
|
|
480
|
+
return false;
|
|
481
|
+
const atoms = new Map(), atom = (c) => text(member(c) ?? c);
|
|
482
|
+
const visit = (c) => { if (c.op === 'and' || c.op === 'or')
|
|
483
|
+
c.xs.forEach(visit);
|
|
484
|
+
else if (c.op === 'not')
|
|
485
|
+
visit(c.x);
|
|
486
|
+
else if (!atoms.has(atom(c)))
|
|
487
|
+
atoms.set(atom(c), atoms.size); };
|
|
488
|
+
ps.forEach(p => visit(p));
|
|
489
|
+
if (atoms.size > 8)
|
|
490
|
+
return false;
|
|
491
|
+
const value = (c, bits) => c.op === 'and' ? c.xs.every(p => value(p, bits)) : c.op === 'or' ? c.xs.some(p => value(p, bits)) : c.op === 'not' ? !value(c.x, bits) : !!(bits & (1 << atoms.get(atom(c))));
|
|
492
|
+
for (let bits = 0; bits < 1 << atoms.size; bits++)
|
|
493
|
+
if (q.terms.reduce((sum, [k], i) => sum + k * (value(ps[i], bits) ? 1 : 0), 0) !== 0)
|
|
494
|
+
return false;
|
|
495
|
+
return true;
|
|
496
|
+
};
|
|
497
|
+
const identityPriority = (q) => (booleanIdentity(q) ? q.terms.length === 3 ? 200 : 100 : 0) + overlap(q) + (q.terms.some(([, id]) => requestSizes.has(id)) ? 10 : 0);
|
|
498
|
+
const identities = eligible.filter(q => q.terms.length !== 5).map(q => ({ q, rank: identityPriority(q) })).sort((a, b) => b.rank - a.rank).map(({ q }) => q);
|
|
499
|
+
const algebraic = new Set([...unions, ...identities.slice(0, 16 - unions.length)]);
|
|
500
|
+
const insts = full.insts.filter(q => {
|
|
501
|
+
if (options.frames && q.unit === 0 && q.terms.length === 3) {
|
|
502
|
+
const ps = q.terms.filter(([k]) => k === 1).map(([, v]) => v), ns = q.terms.filter(([k]) => k === -1).map(([, v]) => v);
|
|
503
|
+
const cover = ns.length === 1 ? bodies.get(ns[0]) : undefined, view = cover && indicator(cover);
|
|
504
|
+
if (ps.length === 2 && view?.op === 'get' && stored.has(ns[0]))
|
|
505
|
+
for (const id of ps) {
|
|
506
|
+
const post = full.reads.some(r => r[0] === id && r[1].endsWith('#')), other = bodies.get(ps.find(p => p !== id)), source = other && indicator(other);
|
|
507
|
+
if (!post || source?.op !== 'get' || !inputs.has(source.map))
|
|
508
|
+
continue;
|
|
509
|
+
let present = false;
|
|
510
|
+
walkOnce(bodies.get(id), (o) => { if (o.op === 'get' && text(o) === text(view))
|
|
511
|
+
present = true; });
|
|
512
|
+
if (present)
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (algebraic.has(q))
|
|
517
|
+
return true;
|
|
518
|
+
if (q.unit !== 0 || q.terms.length !== 2)
|
|
519
|
+
return false;
|
|
520
|
+
const positive = q.terms.find(([n]) => n === 1)?.[1], negative = q.terms.find(([n]) => n === -1)?.[1];
|
|
521
|
+
if (!positive || !negative)
|
|
522
|
+
return false;
|
|
523
|
+
const a = bodies.get(positive), b = bodies.get(negative);
|
|
524
|
+
if (!a || !b)
|
|
525
|
+
return false;
|
|
526
|
+
if (text(indicator(a)) === text(indicator(b)))
|
|
527
|
+
return true;
|
|
528
|
+
if (options.algebraic && text(indicator(a)) === text(indicator(b)))
|
|
529
|
+
return true;
|
|
530
|
+
const pa = predicate(a), pb = predicate(b);
|
|
531
|
+
if (pa && pb) {
|
|
532
|
+
const have = new Set(conjuncts(pa)), need = new Set(conjuncts(pb));
|
|
533
|
+
if (have.size === need.size && [...need].every(p => have.has(p)))
|
|
534
|
+
return true;
|
|
535
|
+
if ((options.structural !== false || options.frames && full.reads.some(r => r[0] === positive && r[1].endsWith('#')) && full.reads.some(r => r[0] === negative && r[1].endsWith('#'))) && [...need].every(p => have.has(p)))
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
if (options.frames && stored.has(negative) && full.reads.some(r => r[0] === positive && r[1].endsWith('#'))) {
|
|
539
|
+
const members = (c) => c.op === 'and' ? c.xs.flatMap(members) : member(c) ? [member(c)] : [];
|
|
540
|
+
const covers = pb ? members(pb) : [], direct = indicator(b);
|
|
541
|
+
if (direct.op === 'get')
|
|
542
|
+
covers.push(direct);
|
|
543
|
+
let present = false;
|
|
544
|
+
if (covers.length)
|
|
545
|
+
walkOnce(a, (o) => { if (o.op === 'get' && covers.some(c => text(o) === text(c)))
|
|
546
|
+
present = true; });
|
|
547
|
+
if (present)
|
|
548
|
+
return true;
|
|
549
|
+
}
|
|
550
|
+
const source = indicator(a);
|
|
551
|
+
if (source.op !== 'get' || !inputs.has(source.map) || options.carried && source.map !== options.carried)
|
|
552
|
+
return false;
|
|
553
|
+
if (options.targets?.length) {
|
|
554
|
+
const targetNames = new Set(options.targets.flatMap(t => t.op === 'var' ? [t.name] : []));
|
|
555
|
+
let relevant = false;
|
|
556
|
+
walkOnce(b, (o) => { if (o.op === 'var' && targetNames.has(o.name))
|
|
557
|
+
relevant = true; });
|
|
558
|
+
if (!relevant)
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
const view = views.get(source.map);
|
|
562
|
+
if (!view?.size)
|
|
563
|
+
return stored.has(negative);
|
|
564
|
+
let matches = false;
|
|
565
|
+
walkOnce(b, (o) => { if (o.op === 'get' && view.has(text(o)))
|
|
566
|
+
matches = true; });
|
|
567
|
+
return matches;
|
|
568
|
+
});
|
|
569
|
+
if (options.frames || options.requestFrames || options.copyFrames) {
|
|
570
|
+
const used = new Set([...m.inputs, ...m.params, ...Object.keys(m.state), ...full.insts.map(i => i.wit), ...full.reads.map(r => r[0])]);
|
|
571
|
+
walkOnce(cs, (o) => { if (o.op === 'var')
|
|
572
|
+
used.add(o.name); });
|
|
573
|
+
let serial = 0;
|
|
574
|
+
const add = (terms, below) => {
|
|
575
|
+
if (insts.some(i => i.unit === 0 && JSON.stringify(i.terms) === JSON.stringify(terms)))
|
|
576
|
+
return;
|
|
577
|
+
let wit;
|
|
578
|
+
do {
|
|
579
|
+
wit = `frame$${serial++}`;
|
|
580
|
+
} while (used.has(wit));
|
|
581
|
+
used.add(wit);
|
|
582
|
+
insts.push({ wit, below, terms, unit: 0 });
|
|
583
|
+
};
|
|
584
|
+
const byId = new Map(cs.map(c => [c.v, c])), afters = full.reads.filter(r => r[1].endsWith('#')).slice(0, 16);
|
|
585
|
+
if (options.copyFrames) {
|
|
586
|
+
// Share the existing cross-row cap between result counts. Prefer the
|
|
587
|
+
// machine's input keys over unrelated law keys and setup constants.
|
|
588
|
+
const rank = (key) => {
|
|
589
|
+
const names = new Set();
|
|
590
|
+
walkOnce(key, (o) => { if (o.op === 'var')
|
|
591
|
+
names.add(o.name); });
|
|
592
|
+
return names.size && [...names].every(name => m.inputs.includes(name)) ? Math.min(...[...names].map(name => m.inputs.indexOf(name))) : [...names].some(name => m.inputs.includes(name)) ? 100 : 200;
|
|
593
|
+
};
|
|
594
|
+
const pools = afters.flatMap(after => {
|
|
595
|
+
const c = byId.get(after[0]);
|
|
596
|
+
if (!c)
|
|
597
|
+
return [];
|
|
598
|
+
const before = full.reads.filter(old => old[1] === after[1].slice(0, -1) && text(old[2]) !== text(after[2]) && text(byId.get(old[0])?.below) === text(c.below)).sort((a, b) => rank(a[2]) - rank(b[2]));
|
|
599
|
+
return [{ after, c, before }];
|
|
600
|
+
});
|
|
601
|
+
let copies = 0;
|
|
602
|
+
for (let row = 0; copies < 16; row++) {
|
|
603
|
+
let found = false;
|
|
604
|
+
for (const pool of pools) {
|
|
605
|
+
const before = pool.before[row];
|
|
606
|
+
if (!before)
|
|
607
|
+
continue;
|
|
608
|
+
found = true;
|
|
609
|
+
add([[1, pool.after[0]], [-1, before[0]]], pool.c.below);
|
|
610
|
+
add([[1, before[0]], [-1, pool.after[0]]], pool.c.below);
|
|
611
|
+
if (++copies >= 16)
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
if (!found)
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
for (const after of afters) {
|
|
619
|
+
const c = byId.get(after[0]);
|
|
620
|
+
if (!c)
|
|
621
|
+
continue;
|
|
622
|
+
if (options.frames)
|
|
623
|
+
add([[1, after[0]]], c.below);
|
|
624
|
+
const before = full.reads.find(r => r[1] === after[1].slice(0, -1) && text(r[2]) === text(after[2]));
|
|
625
|
+
if (options.frames && before && text(byId.get(before[0])?.below) === text(c.below)) {
|
|
626
|
+
add([[1, after[0]], [-1, before[0]]], c.below);
|
|
627
|
+
add([[1, before[0]], [-1, after[0]]], c.below);
|
|
628
|
+
}
|
|
629
|
+
if (options.requestFrames) {
|
|
630
|
+
const shape = (v) => Array.isArray(v) ? v.map(shape) : v && typeof v === 'object' ? v.op === 'get' ? { op: 'cell' } : v.op === 'var' ? { op: 'key' } : Object.fromEntries(Object.entries(v).map(([k, v]) => [k, shape(v)])) : v;
|
|
631
|
+
const beforeDecl = decls.find(d => d.map === after[1].slice(0, -1));
|
|
632
|
+
if (beforeDecl)
|
|
633
|
+
for (const read of full.reads) {
|
|
634
|
+
if (read[1].endsWith('#'))
|
|
635
|
+
continue;
|
|
636
|
+
const d = decls.find(d => d.map === read[1]), count = byId.get(read[0]);
|
|
637
|
+
if (!d || !count || text(count.below) !== text(c.below))
|
|
638
|
+
continue;
|
|
639
|
+
let carried = false;
|
|
640
|
+
walkOnce(d.body, (o) => { if (o.op === 'get' && inputs.has(o.map) && (!options.carried || o.map === options.carried))
|
|
641
|
+
carried = true; });
|
|
642
|
+
if (!carried || text(shape(indicator(d.body))) !== text(shape(indicator(beforeDecl.body))))
|
|
643
|
+
continue;
|
|
644
|
+
add([[1, after[0]], [-1, read[0]]], c.below);
|
|
645
|
+
add([[1, read[0]], [-1, after[0]]], c.below);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
// Equality-consequent member facts extend the carried-view family. These
|
|
651
|
+
// remain ordinary pointwise obligations; guards are only source-scope hints.
|
|
652
|
+
const inferred = memberInclusions(m, decls, full.reads), reserved = new Set([...m.inputs, ...m.params, ...Object.keys(m.state), ...full.insts.map(q => q.wit), ...full.reads.map(r => r[0])]);
|
|
653
|
+
walkOnce(cs, (o) => { if (o.op === 'var')
|
|
654
|
+
reserved.add(o.name); });
|
|
655
|
+
let memberSerial = 0;
|
|
656
|
+
for (const p of inferred) {
|
|
657
|
+
if (options.carried && options.carried !== p.carried)
|
|
658
|
+
continue;
|
|
659
|
+
const terms = [[1, p.positive], [-1, p.negative]], signature = JSON.stringify(terms);
|
|
660
|
+
if (insts.some(q => q.unit === 0 && JSON.stringify(q.terms) === signature))
|
|
661
|
+
continue;
|
|
662
|
+
const prior = full.insts.find(q => q.unit === 0 && JSON.stringify(q.terms) === signature);
|
|
663
|
+
if (prior) {
|
|
664
|
+
insts.push(prior);
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
let wit;
|
|
668
|
+
do {
|
|
669
|
+
wit = `member$${memberSerial++}`;
|
|
670
|
+
} while (reserved.has(wit));
|
|
671
|
+
reserved.add(wit);
|
|
672
|
+
insts.push({ wit, below: p.below, terms, unit: 0 });
|
|
673
|
+
}
|
|
674
|
+
const focused = { reads: full.reads, insts };
|
|
675
|
+
return options.algebraic && options.claim ? demandCountCert(m, decls, focused, options.claim) : focused;
|
|
676
|
+
}
|