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,139 @@
|
|
|
1
|
+
// Untrusted set-cover proposals for Count.lean's existing single-witness CInst.
|
|
2
|
+
// Two positive counts may be disjoint inside one set or a union of three
|
|
3
|
+
// Venn regions. No disjointness/coverage fact is assumed here: a generated
|
|
4
|
+
// instance keeps its witness alternative, checked by the ordinary kernel.
|
|
5
|
+
import { walkOnce, x } from "./machine.js";
|
|
6
|
+
import { substE } from "../kernel/kernel.js";
|
|
7
|
+
const key = (x) => JSON.stringify(x);
|
|
8
|
+
const cond = (c, atoms) => {
|
|
9
|
+
if (c.op === 'flag' || c.op === 'all')
|
|
10
|
+
return undefined;
|
|
11
|
+
if (c.op === 'not') {
|
|
12
|
+
const b = cond(c.x, atoms);
|
|
13
|
+
return b === undefined ? undefined : { not: b };
|
|
14
|
+
}
|
|
15
|
+
if (c.op === 'and' || c.op === 'or') {
|
|
16
|
+
const bs = c.xs.map(c => cond(c, atoms));
|
|
17
|
+
return bs.some(b => b === undefined) ? undefined : { [c.op]: bs };
|
|
18
|
+
}
|
|
19
|
+
const atom = key(c);
|
|
20
|
+
atoms.add(atom);
|
|
21
|
+
return { atom };
|
|
22
|
+
};
|
|
23
|
+
const body = (e, atoms) => {
|
|
24
|
+
if (e.op === 'const')
|
|
25
|
+
return e.value === 0 ? false : e.value === 1 ? true : undefined;
|
|
26
|
+
if (e.op !== 'if')
|
|
27
|
+
return undefined;
|
|
28
|
+
const c = cond(e.c, atoms), a = body(e.then, atoms), b = body(e.else, atoms);
|
|
29
|
+
return c === undefined || a === undefined || b === undefined ? undefined : { or: [{ and: [c, a] }, { and: [{ not: c }, b] }] };
|
|
30
|
+
};
|
|
31
|
+
const evaluate = (b, at) => typeof b === 'boolean' ? b : 'atom' in b ? at.get(b.atom) : 'not' in b ? !evaluate(b.not, at) : 'and' in b ? b.and.every(c => evaluate(c, at)) : b.or.some(c => evaluate(c, at));
|
|
32
|
+
export function proposeCountUnions(counts, pairs, maxFacts = 128) {
|
|
33
|
+
const out = [], seen = new Set();
|
|
34
|
+
const add = (a, b, cover) => {
|
|
35
|
+
if (out.length >= maxFacts || cover.some(c => c.v === a.v || c.v === b.v))
|
|
36
|
+
return;
|
|
37
|
+
const terms = [[1, a.v], [1, b.v], ...cover.map(c => [-1, c.v])];
|
|
38
|
+
const signature = key(terms);
|
|
39
|
+
if (!seen.has(signature)) {
|
|
40
|
+
seen.add(signature);
|
|
41
|
+
out.push({ wit: `union$${out.length}`, below: a.below, terms, unit: 0 });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const groups = new Map();
|
|
45
|
+
for (const c of counts.slice(0, 128)) {
|
|
46
|
+
const k = key(c.below), cs = groups.get(k) ?? [];
|
|
47
|
+
cs.push(c);
|
|
48
|
+
groups.set(k, cs);
|
|
49
|
+
}
|
|
50
|
+
for (const cs of groups.values()) {
|
|
51
|
+
const byName = new Map(cs.map(c => [c.v, c])), regions = new Map();
|
|
52
|
+
for (const c of cs) {
|
|
53
|
+
const atoms = new Set(), f = body(substE(c.body, { vars: { [c.key]: x('union$key') }, maps: {}, flags: {} }), atoms);
|
|
54
|
+
if (f === undefined || atoms.size !== 2)
|
|
55
|
+
continue;
|
|
56
|
+
const ks = [...atoms].sort();
|
|
57
|
+
let mask = 0;
|
|
58
|
+
for (let bits = 0; bits < 4; bits++)
|
|
59
|
+
if (evaluate(f, new Map(ks.map((k, i) => [k, !!(bits & (1 << i))]))))
|
|
60
|
+
mask |= 1 << bits;
|
|
61
|
+
// One region of two primitive predicates; select a representative for duplicates.
|
|
62
|
+
if (![1, 2, 4, 8].includes(mask))
|
|
63
|
+
continue;
|
|
64
|
+
const id = key(ks), rs = regions.get(id) ?? new Map();
|
|
65
|
+
if (!rs.has(mask))
|
|
66
|
+
rs.set(mask, c);
|
|
67
|
+
regions.set(id, rs);
|
|
68
|
+
}
|
|
69
|
+
// An indicator is the disjoint union of the two regions where its atom
|
|
70
|
+
// holds. Raw map-valued counts are only *candidates* for indicators: the
|
|
71
|
+
// returned CInst retains its pointwise alternative, so 0/1 membership must
|
|
72
|
+
// still be established from the actual machine's hypotheses.
|
|
73
|
+
for (const c of cs) {
|
|
74
|
+
const e = substE(c.body, { vars: { [c.key]: x('union$key') }, maps: {}, flags: {} });
|
|
75
|
+
const atoms = new Set(), f = body(e, atoms);
|
|
76
|
+
const atom = e.op === 'get' ? key({ op: '=', a: e, b: { op: 'const', value: 1 } }) : atoms.size === 1 ? [...atoms][0] : undefined;
|
|
77
|
+
if (!atom)
|
|
78
|
+
continue;
|
|
79
|
+
const on = (v) => e.op === 'get' ? v : f !== undefined && evaluate(f, new Map([[atom, v]]));
|
|
80
|
+
for (const [id, rs] of regions) {
|
|
81
|
+
const ks = JSON.parse(id), i = ks.indexOf(atom);
|
|
82
|
+
if (i < 0)
|
|
83
|
+
continue;
|
|
84
|
+
const parts = [1, 2, 4, 8].filter((_, bits) => on(!!(bits & (1 << i)))).map(mask => rs.get(mask));
|
|
85
|
+
const [left, right] = parts;
|
|
86
|
+
if (parts.length !== 2 || !left || !right || out.length + 2 > maxFacts)
|
|
87
|
+
continue;
|
|
88
|
+
for (const sign of [1, -1]) {
|
|
89
|
+
const terms = [[sign, c.v], [-sign, left.v], [-sign, right.v]], signature = key(terms);
|
|
90
|
+
if (!seen.has(signature)) {
|
|
91
|
+
seen.add(signature);
|
|
92
|
+
out.push({ wit: `union$${out.length}`, below: c.below, terms, unit: 0 });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const triples = [...regions.values()].filter(rs => rs.has(2) && rs.has(4) && rs.has(8)).map(rs => [rs.get(2), rs.get(4), rs.get(8)]);
|
|
98
|
+
const positive = pairs.flatMap(([a, b]) => byName.has(a) && byName.has(b) && a !== b ? [[byName.get(a), byName.get(b)]] : []);
|
|
99
|
+
// Interleave useful singleton covers with relational unions per positive
|
|
100
|
+
// pair. A global five-term prefix can otherwise consume the whole budget
|
|
101
|
+
// before the actual carried set gets its three-term self-row cover.
|
|
102
|
+
const normalized = new Map(cs.map(c => [c.v, substE(c.body, { vars: { [c.key]: x('union$key') }, maps: {}, flags: {} })]));
|
|
103
|
+
const primitive = (e) => {
|
|
104
|
+
if (e.op === 'get')
|
|
105
|
+
return e;
|
|
106
|
+
if (e.op !== 'if' || e.then.op !== 'const' || e.then.value !== 1 || e.else.op !== 'const' || e.else.value !== 0)
|
|
107
|
+
return undefined;
|
|
108
|
+
const c = e.c;
|
|
109
|
+
if (c.op === '=' && c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1)
|
|
110
|
+
return c.a;
|
|
111
|
+
if (c.op === '=' && c.b.op === 'get' && c.a.op === 'const' && c.a.value === 1)
|
|
112
|
+
return c.b;
|
|
113
|
+
if (c.op === '<=' && c.a.op === 'const' && c.a.value === 1 && c.b.op === 'get')
|
|
114
|
+
return c.b;
|
|
115
|
+
if (c.op === '<' && c.a.op === 'const' && c.a.value === 0 && c.b.op === 'get')
|
|
116
|
+
return c.b;
|
|
117
|
+
return undefined;
|
|
118
|
+
};
|
|
119
|
+
for (const [a, b] of positive) {
|
|
120
|
+
if (out.length >= maxFacts)
|
|
121
|
+
break;
|
|
122
|
+
const members = new Set();
|
|
123
|
+
for (const c of [a, b])
|
|
124
|
+
walkOnce(normalized.get(c.v), (o) => { if (o.op === 'get')
|
|
125
|
+
members.add(key(o)); });
|
|
126
|
+
for (const c of cs) {
|
|
127
|
+
const view = primitive(normalized.get(c.v));
|
|
128
|
+
if (view && members.has(key(view)))
|
|
129
|
+
add(a, b, [c]);
|
|
130
|
+
}
|
|
131
|
+
for (const cover of triples)
|
|
132
|
+
add(a, b, cover);
|
|
133
|
+
}
|
|
134
|
+
for (const [a, b] of positive)
|
|
135
|
+
for (const c of cs)
|
|
136
|
+
add(a, b, [c]);
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type E, type Machine } from './machine.js';
|
|
2
|
+
import { type CountCert, type CountDecl, type Instances } from '../kernel/kernel.js';
|
|
3
|
+
export declare function seedCountWitnessFlags(m: Machine, at: Instances, packets: {
|
|
4
|
+
decls: CountDecl[];
|
|
5
|
+
cert: Pick<CountCert, 'reads' | 'insts'>;
|
|
6
|
+
}[], preferCarried?: boolean, options?: {
|
|
7
|
+
dependencies?: (body: E) => Set<string>;
|
|
8
|
+
carriedOnly?: boolean;
|
|
9
|
+
}): Instances;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Untrusted instances of the machine's universal flags at selected count witnesses.
|
|
2
|
+
// Count.lean only supplies a witness; the pointwise request predicates at that
|
|
3
|
+
// witness must still be instantiated and proved useful by the ordinary kernel.
|
|
4
|
+
import { walkOnce, x } from "./machine.js";
|
|
5
|
+
const mapsIn = (value) => {
|
|
6
|
+
const maps = new Set();
|
|
7
|
+
walkOnce(value, (o) => { if (o.op === 'get')
|
|
8
|
+
maps.add(o.map); });
|
|
9
|
+
return maps;
|
|
10
|
+
};
|
|
11
|
+
export function seedCountWitnessFlags(m, at, packets, preferCarried = false, options = {}) {
|
|
12
|
+
let out = at;
|
|
13
|
+
const flags = Object.entries(m.step.flags ?? {}).filter(([, q]) => q.op === 'all' && !q.keys?.length).map(([name, q]) => ({ name: `?${name}`, maps: mapsIn(q) }))
|
|
14
|
+
// A count read at its own CInst witness is outside checkMC's read-key scope.
|
|
15
|
+
.filter(q => ![...q.maps].some(mp => mp in (m.counts ?? {})));
|
|
16
|
+
for (const { decls, cert } of packets) {
|
|
17
|
+
const bodies = new Map(cert.reads.map(([id, map]) => { const d = decls.find(d => d.map === map); return [id, d ? (options.dependencies ?? mapsIn)(d.body) : new Set()]; }));
|
|
18
|
+
for (const inst of cert.insts) {
|
|
19
|
+
let used = new Set(inst.terms.flatMap(([, id]) => [...bodies.get(id) ?? []]));
|
|
20
|
+
if (preferCarried) {
|
|
21
|
+
const carried = new Set(inst.terms.filter(([coefficient]) => coefficient > 0).flatMap(([, id]) => [...bodies.get(id) ?? []]).filter(mp => m.inputMaps?.includes(mp)));
|
|
22
|
+
if (carried.size || options.carriedOnly)
|
|
23
|
+
used = carried;
|
|
24
|
+
}
|
|
25
|
+
for (const f of flags)
|
|
26
|
+
if (!f.maps.size || [...f.maps].some(mp => used.has(mp))) {
|
|
27
|
+
const term = x(inst.wit), had = out[f.name] ?? [];
|
|
28
|
+
if (had.some(e => e.op === 'var' && e.name === inst.wit))
|
|
29
|
+
continue;
|
|
30
|
+
if (out === at)
|
|
31
|
+
out = { ...at };
|
|
32
|
+
out[f.name] = [...had, term];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Machine } from './machine.js';
|
|
2
|
+
import { type CountCert, type CountDecl, type CountInst } from '../kernel/kernel.js';
|
|
3
|
+
/** Demand IDs are certificate read variables. Inspect PRE declarations at the
|
|
4
|
+
* same keys: expanded post bodies hide the small two-membership shape. Match
|
|
5
|
+
* only incoming counts joining a written map to its actual carried value source.
|
|
6
|
+
* Difference counts deliberately offer both incoming directions: which row is
|
|
7
|
+
* written/equal is a guarded proof obligation, not a syntactic equivalence.
|
|
8
|
+
* No reads/declarations are added, no input is mutated, and at most 32 ordinary
|
|
9
|
+
* unit-zero instances are returned (two-term frames or three-term compensated
|
|
10
|
+
* single-set/difference transports, in a deterministic prefix).
|
|
11
|
+
*/
|
|
12
|
+
export declare function proposeWriteCountFrames(m: Machine, ds: CountDecl[], cert: Pick<CountCert, 'reads' | 'insts'>, demanded: ReadonlySet<string>, maxFacts?: number): CountInst[];
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// UNTRUSTED written-row count proposals. No equality or frame is assumed here:
|
|
2
|
+
// each direction keeps Count.lean's counterexample-key alternative. Only the
|
|
3
|
+
// source solver/checker, under its actual row/scalar guards, can discharge it.
|
|
4
|
+
import { n } from "./machine.js";
|
|
5
|
+
import { countsForReads } from "./count-facts.js";
|
|
6
|
+
const stable = (value) => JSON.stringify(value, (_, v) => v && typeof v === 'object' && !Array.isArray(v)
|
|
7
|
+
? Object.fromEntries(Object.entries(v).sort(([a], [b]) => a.localeCompare(b))) : v);
|
|
8
|
+
// These are membership syntaxes, not arbitrary predicates that happen to have
|
|
9
|
+
// two atoms (e.g. older-field lexicographic comparisons).
|
|
10
|
+
function membership(c) {
|
|
11
|
+
if (c.op === '=') {
|
|
12
|
+
if (c.a.op === 'get' && c.b.op === 'const' && c.b.value === 1)
|
|
13
|
+
return c.a;
|
|
14
|
+
if (c.b.op === 'get' && c.a.op === 'const' && c.a.value === 1)
|
|
15
|
+
return c.b;
|
|
16
|
+
}
|
|
17
|
+
if ((c.op === '<=' && c.a.op === 'const' && c.a.value === 1
|
|
18
|
+
|| c.op === '<' && c.a.op === 'const' && c.a.value === 0) && c.b.op === 'get')
|
|
19
|
+
return c.b;
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
function literals(c, positive = true) {
|
|
23
|
+
if (c.op === 'not')
|
|
24
|
+
return literals(c.x, !positive);
|
|
25
|
+
if (c.op === 'and' && positive) {
|
|
26
|
+
const out = [];
|
|
27
|
+
for (const child of c.xs) {
|
|
28
|
+
const part = literals(child);
|
|
29
|
+
if (!part || out.length + part.length > 2)
|
|
30
|
+
return undefined;
|
|
31
|
+
out.push(...part);
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
const cell = membership(c);
|
|
36
|
+
return cell && [{ cell, positive }];
|
|
37
|
+
}
|
|
38
|
+
function shape(body) {
|
|
39
|
+
if (body.op !== 'if' || body.then.op !== 'const' || body.then.value !== 1
|
|
40
|
+
|| body.else.op !== 'const' || body.else.value !== 0)
|
|
41
|
+
return undefined;
|
|
42
|
+
const ls = literals(body.c);
|
|
43
|
+
if (ls?.length === 1)
|
|
44
|
+
return ls[0].positive ? { kind: 'single', literals: ls } : undefined;
|
|
45
|
+
if (!ls || ls.length !== 2 || stable(ls[0].cell) === stable(ls[1].cell))
|
|
46
|
+
return undefined;
|
|
47
|
+
const positives = ls.filter(l => l.positive).length;
|
|
48
|
+
return positives ? { kind: positives === 2 ? 'intersection' : 'difference', literals: ls } : undefined;
|
|
49
|
+
}
|
|
50
|
+
// Follow value branches, never general reads in a non-Boolean write guard.
|
|
51
|
+
// A Booleanized value can be an OR of guarded membership arms after lowering.
|
|
52
|
+
function booleanSources(c, inputs, out, positive = true) {
|
|
53
|
+
if (c.op === 'not') {
|
|
54
|
+
booleanSources(c.x, inputs, out, !positive);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (c.op === 'and' || c.op === 'or') {
|
|
58
|
+
for (const child of c.xs)
|
|
59
|
+
booleanSources(child, inputs, out, positive);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const cell = positive && membership(c);
|
|
63
|
+
if (cell && inputs.has(cell.map))
|
|
64
|
+
out.add(cell.map);
|
|
65
|
+
}
|
|
66
|
+
function copySources(value, inputs, out = new Set()) {
|
|
67
|
+
if (value.op === 'get' && inputs.has(value.map))
|
|
68
|
+
out.add(value.map);
|
|
69
|
+
if (value.op === 'if') {
|
|
70
|
+
if (value.then.op === 'const' && value.then.value === 1
|
|
71
|
+
&& value.else.op === 'const' && value.else.value === 0)
|
|
72
|
+
booleanSources(value.c, inputs, out);
|
|
73
|
+
copySources(value.then, inputs, out);
|
|
74
|
+
copySources(value.else, inputs, out);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
/** Demand IDs are certificate read variables. Inspect PRE declarations at the
|
|
79
|
+
* same keys: expanded post bodies hide the small two-membership shape. Match
|
|
80
|
+
* only incoming counts joining a written map to its actual carried value source.
|
|
81
|
+
* Difference counts deliberately offer both incoming directions: which row is
|
|
82
|
+
* written/equal is a guarded proof obligation, not a syntactic equivalence.
|
|
83
|
+
* No reads/declarations are added, no input is mutated, and at most 32 ordinary
|
|
84
|
+
* unit-zero instances are returned (two-term frames or three-term compensated
|
|
85
|
+
* single-set/difference transports, in a deterministic prefix).
|
|
86
|
+
*/
|
|
87
|
+
export function proposeWriteCountFrames(m, ds, cert, demanded, maxFacts = 32) {
|
|
88
|
+
const cap = Number.isSafeInteger(maxFacts) && maxFacts >= 0 ? Math.min(maxFacts, 32) : 0;
|
|
89
|
+
if (!cap || !demanded.size)
|
|
90
|
+
return [];
|
|
91
|
+
// Include ALL names, including unrelated declarations, flags, bindings, map
|
|
92
|
+
// keys and existing witness bounds. Iterative traversal also handles sharing.
|
|
93
|
+
const used = new Set(), seenObjects = new Set(), pending = [m, ds, cert, [...demanded]];
|
|
94
|
+
while (pending.length) {
|
|
95
|
+
const value = pending.pop();
|
|
96
|
+
if (typeof value === 'string')
|
|
97
|
+
used.add(value);
|
|
98
|
+
else if (value && typeof value === 'object' && !seenObjects.has(value)) {
|
|
99
|
+
seenObjects.add(value);
|
|
100
|
+
for (const [key, child] of Object.entries(value)) {
|
|
101
|
+
used.add(key);
|
|
102
|
+
pending.push(child);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
let serial = 0;
|
|
107
|
+
const fresh = () => {
|
|
108
|
+
while (used.has(`writeFrame$${serial}`))
|
|
109
|
+
serial++;
|
|
110
|
+
const name = `writeFrame$${serial++}`;
|
|
111
|
+
used.add(name);
|
|
112
|
+
return name;
|
|
113
|
+
};
|
|
114
|
+
const writers = new Map(), inputs = new Set(m.inputMaps ?? []);
|
|
115
|
+
for (const [map, write] of Object.entries(m.step.maps ?? {})) {
|
|
116
|
+
const sources = copySources(write.value, inputs);
|
|
117
|
+
if (sources.size)
|
|
118
|
+
writers.set(map, sources);
|
|
119
|
+
}
|
|
120
|
+
if (!writers.size)
|
|
121
|
+
return [];
|
|
122
|
+
const declarations = new Map();
|
|
123
|
+
for (const d of ds)
|
|
124
|
+
if (!declarations.has(d.map))
|
|
125
|
+
declarations.set(d.map, d);
|
|
126
|
+
const indexKey = (map, kind, below) => stable([map, kind, below]);
|
|
127
|
+
const incoming = new Map(), readIds = new Set();
|
|
128
|
+
for (const read of cert.reads) {
|
|
129
|
+
if (readIds.has(read[0]))
|
|
130
|
+
continue;
|
|
131
|
+
readIds.add(read[0]);
|
|
132
|
+
if (read[1].endsWith('#'))
|
|
133
|
+
continue;
|
|
134
|
+
const c = countsForReads(ds, [read])?.[0], s = c && shape(c.body);
|
|
135
|
+
if (!c || !s)
|
|
136
|
+
continue;
|
|
137
|
+
for (const [map, sources] of writers) {
|
|
138
|
+
if (!s.literals.some(l => sources.has(l.cell.map)) || s.kind !== 'single' && !s.literals.some(l => l.cell.map === map))
|
|
139
|
+
continue;
|
|
140
|
+
const key = indexKey(map, s.kind, c.below), group = incoming.get(key) ?? [];
|
|
141
|
+
group.push(c);
|
|
142
|
+
incoming.set(key, group);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Coefficient-vector dedupe ignores term order and combines repeated IDs.
|
|
146
|
+
// Each direction has its own signature; an existing reverse does not erase
|
|
147
|
+
// the still-missing forward direction.
|
|
148
|
+
const signature = (i) => {
|
|
149
|
+
const coefficients = new Map();
|
|
150
|
+
for (const [z, id] of i.terms)
|
|
151
|
+
coefficients.set(id, (coefficients.get(id) ?? 0) + z);
|
|
152
|
+
return stable([i.unit, [...coefficients].filter(([, z]) => z !== 0).sort(([a], [b]) => a.localeCompare(b))]);
|
|
153
|
+
};
|
|
154
|
+
const emitted = new Set(cert.insts.map(signature)), out = [];
|
|
155
|
+
readIds.clear();
|
|
156
|
+
for (const read of cert.reads) {
|
|
157
|
+
if (readIds.has(read[0]))
|
|
158
|
+
continue;
|
|
159
|
+
readIds.add(read[0]);
|
|
160
|
+
if (!demanded.has(read[0]) || !read[1].endsWith('#'))
|
|
161
|
+
continue;
|
|
162
|
+
const preRead = [read[0], read[1].slice(0, -1), read[2]];
|
|
163
|
+
const before = countsForReads(ds, [preRead])?.[0], s = before && shape(before.body);
|
|
164
|
+
const postDecl = declarations.get(read[1]);
|
|
165
|
+
if (!before || !s || !postDecl)
|
|
166
|
+
continue;
|
|
167
|
+
// Instantiate the real post bound without traversing the expanded body.
|
|
168
|
+
const post = countsForReads([{ ...postDecl, body: n(0) }], [read])?.[0];
|
|
169
|
+
if (!post || stable(post.below) !== stable(before.below))
|
|
170
|
+
continue;
|
|
171
|
+
for (const map of new Set(s.literals.map(l => l.cell.map))) {
|
|
172
|
+
const kinds = s.kind === 'intersection' && s.literals.every(l => l.cell.map === map) ? [s.kind, 'single'] : [s.kind];
|
|
173
|
+
for (const kind of kinds)
|
|
174
|
+
for (const c of incoming.get(indexKey(map, kind, post.below)) ?? []) {
|
|
175
|
+
if (c.v === read[0])
|
|
176
|
+
continue;
|
|
177
|
+
// Retaining the old count can compensate a changed single set or
|
|
178
|
+
// directed difference. These are only proposals: a writer may conjure
|
|
179
|
+
// members, and neither old+incoming nor either incoming orientation
|
|
180
|
+
// is trusted until checked under the actual write/row guards.
|
|
181
|
+
if (s.kind === 'difference' || s.kind === 'single') {
|
|
182
|
+
const previous = cert.reads.find(r => r[1] === preRead[1] && stable(r[2]) === stable(preRead[2]));
|
|
183
|
+
if (previous) {
|
|
184
|
+
const terms = [[1, read[0]], [-1, previous[0]], [-1, c.v]], key = signature({ terms, unit: 0 });
|
|
185
|
+
if (!emitted.has(key)) {
|
|
186
|
+
if (out.length >= cap)
|
|
187
|
+
return out;
|
|
188
|
+
emitted.add(key);
|
|
189
|
+
out.push({ wit: fresh(), below: post.below, terms, unit: 0 });
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
for (const sign of [1, -1]) {
|
|
194
|
+
if (out.length >= cap)
|
|
195
|
+
return out;
|
|
196
|
+
const terms = [[sign, read[0]], [-sign, c.v]], key = signature({ terms, unit: 0 });
|
|
197
|
+
if (emitted.has(key))
|
|
198
|
+
continue;
|
|
199
|
+
emitted.add(key);
|
|
200
|
+
out.push({ wit: fresh(), below: post.below, terms, unit: 0 });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
package/dist/core/counts.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type C, type E, type Law, type Machine } from './machine.js';
|
|
2
2
|
import { type CountCert, type CountInst, type Instances, type MapRead } from '../kernel/kernel.js';
|
|
3
3
|
import { inferLaws } from './infer.js';
|
|
4
|
+
export { attemptShare, supportBudget } from './support-budget.js';
|
|
4
5
|
export declare const cprof: Record<string, number>;
|
|
5
6
|
export declare const countsDump: {
|
|
6
7
|
on?: (goal: object) => void;
|
|
@@ -17,20 +18,28 @@ export declare function certFor(goal: {
|
|
|
17
18
|
j: string;
|
|
18
19
|
key: string;
|
|
19
20
|
body: E;
|
|
20
|
-
}[], wide?: boolean, most?: number): {
|
|
21
|
+
}[], wide?: boolean, most?: number, more?: () => boolean, structured?: boolean): {
|
|
21
22
|
reads: MapRead[];
|
|
22
23
|
insts: CountInst[];
|
|
23
24
|
wits: string[];
|
|
24
25
|
picks: [string, E[]][];
|
|
25
26
|
loose: boolean;
|
|
26
27
|
};
|
|
27
|
-
export declare function candidates(src: Parameters<typeof inferLaws>[0], m: Machine, caller: Law[], shapes: Law[], guarded: Law[], ms: number, until: number): Law[];
|
|
28
|
+
export declare function candidates(src: Parameters<typeof inferLaws>[0], m: Machine, caller: Law[], shapes: Law[], guarded: Law[], ms: number, until: number, support?: Set<Law>): Law[];
|
|
28
29
|
export type Tried<P> = {
|
|
29
30
|
proof: P;
|
|
30
31
|
} | {
|
|
31
32
|
stuck: string;
|
|
33
|
+
refuted?: boolean;
|
|
32
34
|
};
|
|
35
|
+
export declare function withDeferredCountRecovery<P>(until: number, attempt: (defer: (recover: () => P | undefined) => void) => Tried<P>, ready?: () => boolean): Tried<P>;
|
|
33
36
|
export declare function cone<P>(m: Machine, set: Law[], attempt: (law: Law, ls: Law[], ms: number) => Tried<P>, until: number, houdini?: boolean): Map<string, P>;
|
|
37
|
+
export declare const activeSupport: (set: Law[], support: Set<Law>) => boolean;
|
|
38
|
+
export declare function countSupportContext(m: Machine, caller: Law[], set: Law[], support: Set<Law>): {
|
|
39
|
+
laws: Law[];
|
|
40
|
+
support: Set<Law>;
|
|
41
|
+
};
|
|
42
|
+
export declare function supportOrder(set: Law[], caller: Law[], support: Set<Law>, forModels?: boolean): Law[];
|
|
34
43
|
export type Counted = {
|
|
35
44
|
proven: boolean;
|
|
36
45
|
laws: Law[];
|
|
@@ -42,3 +51,12 @@ export type Counted = {
|
|
|
42
51
|
why?: string;
|
|
43
52
|
};
|
|
44
53
|
export declare function searchCounted(m0: Machine, caller: Law[], ms?: number): Counted;
|
|
54
|
+
export declare function admitCountedFamily(m: Machine, caller: Law[], initial: Law[], support: Set<Law>, until: number): Counted;
|
|
55
|
+
export declare function countedAttempts(m: Machine, caller: Law[], support: Set<Law>, until: number, structured?: boolean): {
|
|
56
|
+
traced: (law: Law, ls: Law[], pass: 0 | 1, ms: number) => Tried<{
|
|
57
|
+
instances: Instances;
|
|
58
|
+
certs: Record<string, CountCert>;
|
|
59
|
+
}>;
|
|
60
|
+
readonly counted: boolean;
|
|
61
|
+
houdini: boolean;
|
|
62
|
+
};
|