gdp-wired 0.31.1 → 0.33.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 +95 -6
- package/dist/core/c/reader.d.ts +86 -0
- package/dist/core/c/reader.js +860 -0
- package/dist/core/check.js +20 -6
- package/dist/core/cli.d.ts +76 -0
- package/dist/core/cli.js +636 -0
- package/dist/core/dsl.d.ts +6 -4
- package/dist/core/dsl.js +30 -16
- package/dist/core/effect.d.ts +6 -4
- package/dist/core/effect.js +48 -10
- package/dist/core/ex/harness.exs +177 -0
- package/dist/core/ex/json.exs +46 -0
- package/dist/core/ex/reader.exs +462 -0
- package/dist/core/exists.d.ts +3 -0
- package/dist/core/exists.js +19 -5
- package/dist/core/extract-c.d.ts +35 -0
- package/dist/core/extract-c.js +424 -0
- package/dist/core/extract-check.js +27 -6
- package/dist/core/extract-ex.d.ts +16 -0
- package/dist/core/extract-ex.js +33 -0
- package/dist/core/extract-go-check.d.ts +28 -0
- package/dist/core/extract-go-check.js +639 -0
- package/dist/core/extract-go.d.ts +231 -0
- package/dist/core/extract-go.js +1166 -0
- package/dist/core/extract-jvm.d.ts +23 -0
- package/dist/core/extract-jvm.js +220 -0
- package/dist/core/extract-print.js +17 -7
- package/dist/core/extract-py.d.ts +15 -0
- package/dist/core/extract-py.js +31 -0
- package/dist/core/extract-rb.d.ts +17 -0
- package/dist/core/extract-rb.js +31 -0
- package/dist/core/extract-read.d.ts +20 -0
- package/dist/core/extract-read.js +46 -6
- package/dist/core/extract-rs.d.ts +12 -0
- package/dist/core/extract-rs.js +306 -0
- package/dist/core/extract-shapes.d.ts +2 -0
- package/dist/core/extract-shapes.js +6 -5
- package/dist/core/extract-step.d.ts +806 -0
- package/dist/core/extract-step.js +4141 -0
- package/dist/core/extract-swift.d.ts +27 -0
- package/dist/core/extract-swift.js +484 -0
- package/dist/core/extract-xstate.d.ts +10 -3
- package/dist/core/extract-xstate.js +50 -4
- package/dist/core/extract.d.ts +22 -3
- package/dist/core/extract.js +107 -18
- package/dist/core/go/go.mod +3 -0
- package/dist/core/go/reader.go +901 -0
- package/dist/core/infer.d.ts +4 -0
- package/dist/core/infer.js +1838 -69
- package/dist/core/jvm/Harness.java +361 -0
- package/dist/core/jvm/JavaReader.java +825 -0
- package/dist/core/jvm/Jvm.java +332 -0
- package/dist/core/jvm/KtReader.java +960 -0
- package/dist/core/jvm/Rt.java +10 -0
- package/dist/core/laws.d.ts +83 -0
- package/dist/core/laws.js +623 -0
- package/dist/core/machine.d.ts +2 -0
- package/dist/core/machine.js +87 -0
- package/dist/core/py/harness.py +328 -0
- package/dist/core/py/reader.py +447 -0
- package/dist/core/rb/harness.rb +233 -0
- package/dist/core/rb/reader.rb +375 -0
- package/dist/core/relational.d.ts +49 -0
- package/dist/core/relational.js +345 -0
- package/dist/core/rs/Cargo.lock +108 -0
- package/dist/core/rs/Cargo.toml +14 -0
- package/dist/core/rs/src/main.rs +681 -0
- package/dist/core/swift/Harness.swift +124 -0
- package/dist/core/swift/Main.swift +34 -0
- package/dist/core/swift/Package.swift +13 -0
- package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
- package/dist/interchange/check.d.ts +16 -0
- package/dist/interchange/check.js +111 -0
- package/dist/interchange/format.d.ts +150 -0
- package/dist/interchange/format.js +678 -0
- package/dist/kernel/epr.d.ts +40 -0
- package/dist/kernel/epr.js +1882 -0
- package/dist/kernel/kernel.d.ts +45 -1
- package/dist/kernel/kernel.js +138 -13
- package/dist/kernel/prove.d.ts +45 -0
- package/dist/kernel/prove.js +658 -51
- package/dist/lean-kernel/emit.d.ts +17 -2
- package/dist/lean-kernel/emit.js +87 -7
- package/package.json +57 -2
|
@@ -0,0 +1,1882 @@
|
|
|
1
|
+
// Untrusted: a fast decision of EPR-shaped goals (research/protocols/epr/DESIGN.md). The hypotheses read as a
|
|
2
|
+
// propositional abstraction: a composed key (a·N² + b·N + c) the tuple of its parts, compared by the parts' order and
|
|
3
|
+
// equality, never by arithmetic; a few-valued cell a finite variable; an ordered value (an epoch) a term of a total order;
|
|
4
|
+
// anything else an opaque atom. Every clause beside the hypotheses holds in every model of the goal, and finite values are
|
|
5
|
+
// capped above every constant they meet, so unsat here is unsat there. It answers unsat with its core (the hypotheses
|
|
6
|
+
// its refutation read: prove.ts proves the goal over those and widens the tree to the whole, which kernel.ts checks), or
|
|
7
|
+
// a model read back as the kernel's witness and judged on every hypothesis, or nothing.
|
|
8
|
+
import { evalC } from "../core/machine.js";
|
|
9
|
+
import { canon, linear } from "./kernel.js";
|
|
10
|
+
import { env, konst, plus, solve, times, v as tv } from "../core/solver.js";
|
|
11
|
+
import { Simplex } from "./simplex.js";
|
|
12
|
+
// (a term's text, once per object: the decision asks it of the same terms many times)
|
|
13
|
+
const textsOf = new WeakMap();
|
|
14
|
+
const textOfE = (o) => { let s = textsOf.get(o); if (s === undefined)
|
|
15
|
+
textsOf.set(o, (s = JSON.stringify(o))); return s; };
|
|
16
|
+
// The patterns of a rule: of each map's reads in the step, the assumption, the flags and the laws (not the kinds' laws,
|
|
17
|
+
// whose key is a whole key), the one with the most parts. A read keyed by a write-at-every-key's own name is left out.
|
|
18
|
+
export function patternsOf(m, laws) {
|
|
19
|
+
const out = {};
|
|
20
|
+
const each = new Set(Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.each] : [])));
|
|
21
|
+
const scan = (o) => {
|
|
22
|
+
if (!o || typeof o !== 'object')
|
|
23
|
+
return;
|
|
24
|
+
const x = o;
|
|
25
|
+
if (x.op === 'get' && !x.args && !(x.key.op === 'var' && each.has(x.key.name)) && !(m.fns && x.map in m.fns)) {
|
|
26
|
+
const t = tupleOf(x.key);
|
|
27
|
+
if (t && t.coefs.length > (out[x.map]?.length ?? 0))
|
|
28
|
+
out[x.map] = t.coefs;
|
|
29
|
+
}
|
|
30
|
+
for (const y of Object.values(o))
|
|
31
|
+
scan(y);
|
|
32
|
+
};
|
|
33
|
+
scan(m.step);
|
|
34
|
+
scan(m.assume);
|
|
35
|
+
// (a function of one key: [0], its reads parts of the keys they're in, an uninterpreted function of a part)
|
|
36
|
+
for (const f of Object.keys(m.fns ?? {}))
|
|
37
|
+
out[f] = [0];
|
|
38
|
+
for (const l of laws)
|
|
39
|
+
if (!(l.name.startsWith('every ') && l.name.endsWith('of its kind')))
|
|
40
|
+
scan(l.holds);
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
const T = { k: 'c', b: true }, Fa = { k: 'c', b: false };
|
|
44
|
+
const and = (xs) => { const ys = xs.filter(x => !(x.k === 'c' && x.b)); return ys.some(x => x.k === 'c' && !x.b) ? Fa : ys.length === 0 ? T : ys.length === 1 ? ys[0] : { k: 'and', xs: ys }; };
|
|
45
|
+
const or = (xs) => { const ys = xs.filter(x => !(x.k === 'c' && !x.b)); return ys.some(x => x.k === 'c' && x.b) ? T : ys.length === 0 ? Fa : ys.length === 1 ? ys[0] : { k: 'or', xs: ys }; };
|
|
46
|
+
const not = (x) => (x.k === 'c' ? { k: 'c', b: !x.b } : x.k === 'not' ? x.x : { k: 'not', x });
|
|
47
|
+
// The functions of one key of the goal being decided (patterns [0]): a read f(x) of one at a part is a part, `f(x)`.
|
|
48
|
+
let fnMaps = new Set();
|
|
49
|
+
const partName = (e) => e.op === 'var' ? e.name
|
|
50
|
+
: e.op === 'get' && fnMaps.has(e.map) && !e.args ? (n => (n === undefined ? undefined : `${e.map}(${n})`))(partName(e.key)) : undefined;
|
|
51
|
+
// A key Σ c·x + d as its terms; undefined where it reads anything but parts under constant factors.
|
|
52
|
+
const linTerms = (e, k, out, d) => e.op === 'var' || e.op === 'get' ? (n => n !== undefined && (out.push([n, k]), true))(partName(e)) : e.op === 'const' ? ((d.c += k * e.value), true)
|
|
53
|
+
: e.op === 'mul' ? linTerms(e.a, k * e.k, out, d) : e.op === 'add' ? linTerms(e.a, k, out, d) && linTerms(e.b, k, out, d) : false;
|
|
54
|
+
const tupleMemo = new WeakMap();
|
|
55
|
+
function tupleOf(e) { let t = tupleMemo.get(e); if (t === undefined)
|
|
56
|
+
tupleMemo.set(e, (t = tupleOf0(e) ?? null)); return t ?? undefined; }
|
|
57
|
+
function tupleOf0(e) {
|
|
58
|
+
const ts = [], d = { c: 0 };
|
|
59
|
+
if (!linTerms(e, 1, ts, d) || d.c !== 0 || !ts.length)
|
|
60
|
+
return undefined;
|
|
61
|
+
ts.sort((a, b) => b[1] - a[1]);
|
|
62
|
+
const coefs = ts.map(t => t[1]);
|
|
63
|
+
if (coefs.at(-1) !== 1 || coefs.some((c, i) => i + 1 < coefs.length && (c % coefs[i + 1] !== 0 || c / coefs[i + 1] < 2)))
|
|
64
|
+
return undefined;
|
|
65
|
+
return { coefs, parts: ts.map(t => t[0]), bounds: coefs.map((c, i) => (i === 0 ? undefined : coefs[i - 1] / c)) };
|
|
66
|
+
}
|
|
67
|
+
const sameCoefs = (a, b) => a.length === b.length && a.every((c, i) => c === b[i]);
|
|
68
|
+
// `late`: true past the caller's end, checked between the steps and every 256 conflicts; then unknown.
|
|
69
|
+
// (a probe's hook: each decision with its goal, ms and outcome)
|
|
70
|
+
export const decideHook = {};
|
|
71
|
+
let lastShaped;
|
|
72
|
+
export function decide(goal, patterns, opts = {}) {
|
|
73
|
+
if (!decideHook.on)
|
|
74
|
+
return decide1(goal, patterns, opts);
|
|
75
|
+
const t0 = performance.now(), d = decide1(goal, patterns, opts);
|
|
76
|
+
decideHook.on(goal, d, performance.now() - t0, lastShaped);
|
|
77
|
+
return d;
|
|
78
|
+
}
|
|
79
|
+
function decide1(goal0, patterns, opts = {}) {
|
|
80
|
+
fnMaps = new Set(Object.entries(patterns).filter(([, c]) => c.length === 1 && c[0] === 0).map(([f]) => f));
|
|
81
|
+
const goal = goal0;
|
|
82
|
+
// (a model whose order leaves no room between two near constants: again with the constants between them, below)
|
|
83
|
+
// (a model the read-back breaks: once more without the other goals' theory lemmas, whose history chose that model)
|
|
84
|
+
try {
|
|
85
|
+
const d = decide0(goal, patterns, opts), d2 = 'unknown' in d && d.unknown === noRoom ? decide0(goal, patterns, { ...opts, fill: true }) : d;
|
|
86
|
+
return 'unknown' in d2 && d2.unknown.startsWith(breaks) && lemmaCache.size ? decide0(goal, patterns, { ...opts, fresh: true }) : d2;
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
if (e === pastEnd)
|
|
90
|
+
return { unknown: 'past the deadline' };
|
|
91
|
+
throw e;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
fnMaps = new Set();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// A read at a key that holds an if (a write at every key read at a ghost's key, M'[c ? w : y]) as the if of the reads at
|
|
98
|
+
// its two keys, M[c ? a : b] = c ? M[a] : M[b] (an if inside the key's sum first lifted to its top), so each is a cell
|
|
99
|
+
// at a tuple; equal terms, so a refutation and a model of one are of the other. At most 8 keys a read.
|
|
100
|
+
const liftMemo = new WeakMap();
|
|
101
|
+
const ifMemo = new WeakMap();
|
|
102
|
+
const hasIf = (o) => {
|
|
103
|
+
if (!o || typeof o !== 'object')
|
|
104
|
+
return false;
|
|
105
|
+
let b = ifMemo.get(o);
|
|
106
|
+
if (b === undefined)
|
|
107
|
+
ifMemo.set(o, (b = o.op === 'if' || Object.values(o).some(hasIf)));
|
|
108
|
+
return b;
|
|
109
|
+
};
|
|
110
|
+
const hoisted = (e) => {
|
|
111
|
+
if (e.op === 'add') {
|
|
112
|
+
const a = hoisted(e.a), b = hoisted(e.b);
|
|
113
|
+
return a.op === 'if' ? { op: 'if', c: a.c, then: hoisted({ ...e, a: a.then, b }), else: hoisted({ ...e, a: a.else, b }) } : b.op === 'if' ? { op: 'if', c: b.c, then: hoisted({ ...e, a, b: b.then }), else: hoisted({ ...e, a, b: b.else }) } : a === e.a && b === e.b ? e : { ...e, a, b };
|
|
114
|
+
}
|
|
115
|
+
if (e.op === 'mul') {
|
|
116
|
+
const a = hoisted(e.a);
|
|
117
|
+
return a.op === 'if' ? { op: 'if', c: a.c, then: hoisted({ ...e, a: a.then }), else: hoisted({ ...e, a: a.else }) } : a === e.a ? e : { ...e, a };
|
|
118
|
+
}
|
|
119
|
+
return e;
|
|
120
|
+
};
|
|
121
|
+
const keysOf = (e) => (e.op === 'if' ? keysOf(e.then) + keysOf(e.else) : 1);
|
|
122
|
+
const liftE = (e) => {
|
|
123
|
+
if (!hasIf(e))
|
|
124
|
+
return e;
|
|
125
|
+
if (e.op === 'get' && !e.args) {
|
|
126
|
+
const key = hoisted(liftE(e.key));
|
|
127
|
+
if (key.op !== 'if' || keysOf(key) > 8)
|
|
128
|
+
return key === e.key ? e : { ...e, key };
|
|
129
|
+
const at = (k) => (k.op === 'if' ? { op: 'if', c: k.c, then: at(k.then), else: at(k.else) } : { ...e, key: k });
|
|
130
|
+
return at(key);
|
|
131
|
+
}
|
|
132
|
+
const out = {};
|
|
133
|
+
let changed = false;
|
|
134
|
+
for (const [k, x] of Object.entries(e)) {
|
|
135
|
+
const y = x && typeof x === 'object' && 'op' in x ? (k === 'c' ? liftC(x) : liftE(x)) : x;
|
|
136
|
+
out[k] = y;
|
|
137
|
+
if (y !== x)
|
|
138
|
+
changed = true;
|
|
139
|
+
}
|
|
140
|
+
return changed ? out : e;
|
|
141
|
+
};
|
|
142
|
+
const liftC = (c) => {
|
|
143
|
+
if (!hasIf(c))
|
|
144
|
+
return c;
|
|
145
|
+
if (c.op === 'and' || c.op === 'or') {
|
|
146
|
+
const xs = c.xs.map(liftC);
|
|
147
|
+
return xs.every((x, i) => x === c.xs[i]) ? c : { ...c, xs };
|
|
148
|
+
}
|
|
149
|
+
if (c.op === 'not') {
|
|
150
|
+
const x = liftC(c.x);
|
|
151
|
+
return x === c.x ? c : { ...c, x };
|
|
152
|
+
}
|
|
153
|
+
if (c.op === 'all') {
|
|
154
|
+
const b = liftC(c.c);
|
|
155
|
+
return b === c.c ? c : { ...c, c: b };
|
|
156
|
+
}
|
|
157
|
+
if (c.op === 'flag')
|
|
158
|
+
return c;
|
|
159
|
+
const a = liftE(c.a), b = liftE(c.b);
|
|
160
|
+
return a === c.a && b === c.b ? c : { ...c, a, b };
|
|
161
|
+
};
|
|
162
|
+
const liftedKeys = (c) => { let x = liftMemo.get(c); if (!x)
|
|
163
|
+
liftMemo.set(c, (x = liftC(c))); return x; };
|
|
164
|
+
// (thrown by `late` checked every 4,096 clauses and hypotheses read: a goal's clauses are quadratic in its terms)
|
|
165
|
+
const pastEnd = new Error('past the deadline');
|
|
166
|
+
// The work the EPR path does, counted (each clause and hypothesis read, each literal the CDCL search propagates), and a
|
|
167
|
+
// cap on it a caller sets (core/infer.ts's engine, per goal): past the cap the decision is unknown, at the same place on
|
|
168
|
+
// any machine, where a wall-clock end stops it wherever the machine has got to. Untrusted: it only stops a search.
|
|
169
|
+
export const eprMeter = { n: 0 };
|
|
170
|
+
let eprCap = Infinity;
|
|
171
|
+
export const pastEprCap = () => eprMeter.n > eprCap;
|
|
172
|
+
export function withEprCap(units, f) {
|
|
173
|
+
const outer = eprCap;
|
|
174
|
+
eprCap = Math.min(outer, eprMeter.n + units);
|
|
175
|
+
try {
|
|
176
|
+
return f();
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
eprCap = outer;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// The theory's lemmas, by their atoms' names: each is arithmetic over the terms those names read (a part's value, a cell's
|
|
183
|
+
// at its key), so it holds in every goal that has those atoms, a round's other goals over the same set above all; each
|
|
184
|
+
// goal starts with those it has. At most 20,000, then begun again.
|
|
185
|
+
const lemmaCache = new Map();
|
|
186
|
+
const noRoom = 'no values for the model\'s order', breaks = 'the abstraction\'s model breaks';
|
|
187
|
+
const factsMemo = new Map();
|
|
188
|
+
function factsOf(h0, tick) {
|
|
189
|
+
tick();
|
|
190
|
+
const text = h0 ? JSON.stringify(h0) : '';
|
|
191
|
+
let f = factsMemo.get(text);
|
|
192
|
+
if (f)
|
|
193
|
+
return f;
|
|
194
|
+
const h = h0 && liftedKeys(h0), reads = [], comps = [], bounds = [], seen = new WeakSet();
|
|
195
|
+
const gather = (o) => {
|
|
196
|
+
if (!o || typeof o !== 'object' || seen.has(o))
|
|
197
|
+
return;
|
|
198
|
+
seen.add(o);
|
|
199
|
+
tick();
|
|
200
|
+
const x = o;
|
|
201
|
+
if (x.op === 'get')
|
|
202
|
+
reads.push(o);
|
|
203
|
+
else if ((x.op === '<' || x.op === '<=' || x.op === '=') && x.a)
|
|
204
|
+
comps.push(o);
|
|
205
|
+
for (const v of Object.values(o))
|
|
206
|
+
gather(v);
|
|
207
|
+
};
|
|
208
|
+
gather(h);
|
|
209
|
+
// exact: a scalar or a cell a hypothesis bounds by a constant (M[t] <= 1, a kind's instance)
|
|
210
|
+
const unitWalk = (c) => {
|
|
211
|
+
if (!c)
|
|
212
|
+
return;
|
|
213
|
+
if (c.op === 'and') {
|
|
214
|
+
c.xs.forEach(unitWalk);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if ((c.op === '<=' || c.op === '<') && c.b.op === 'const' && (c.a.op === 'get' || c.a.op === 'var')) {
|
|
218
|
+
const b = c.b.value - (c.op === '<' ? 1 : 0);
|
|
219
|
+
if (b >= 0)
|
|
220
|
+
bounds.push([textOfE(c.a), b]);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
unitWalk(h);
|
|
224
|
+
if (factsMemo.size > 50_000)
|
|
225
|
+
factsMemo.clear();
|
|
226
|
+
factsMemo.set(text, (f = { h, text, reads, comps, bounds }));
|
|
227
|
+
return f;
|
|
228
|
+
}
|
|
229
|
+
const leavesOf = (e, k, out) => {
|
|
230
|
+
if (e.op === 'add') {
|
|
231
|
+
leavesOf(e.a, k, out);
|
|
232
|
+
leavesOf(e.b, k, out);
|
|
233
|
+
}
|
|
234
|
+
else if (e.op === 'mul')
|
|
235
|
+
leavesOf(e.a, k * e.k, out);
|
|
236
|
+
else if (e.op === 'if') {
|
|
237
|
+
leavesOf(e.then, k, out);
|
|
238
|
+
leavesOf(e.else, k, out);
|
|
239
|
+
}
|
|
240
|
+
else if (e.op !== 'const')
|
|
241
|
+
out.push([e, k]);
|
|
242
|
+
};
|
|
243
|
+
const compMemo = new WeakMap();
|
|
244
|
+
const compInfo = (c) => {
|
|
245
|
+
let x = compMemo.get(c);
|
|
246
|
+
if (!x) {
|
|
247
|
+
const ts = [], consts = [];
|
|
248
|
+
leavesOf(c.a, 1, ts);
|
|
249
|
+
leavesOf(c.b, -1, ts);
|
|
250
|
+
const k = (e) => { if (e.op === 'const')
|
|
251
|
+
consts.push(Math.abs(e.value));
|
|
252
|
+
else if (e.op === 'add')
|
|
253
|
+
(k(e.a), k(e.b));
|
|
254
|
+
else if (e.op === 'mul')
|
|
255
|
+
k(e.a);
|
|
256
|
+
else if (e.op === 'if')
|
|
257
|
+
(k(e.then), k(e.else)); };
|
|
258
|
+
k(c.a);
|
|
259
|
+
k(c.b);
|
|
260
|
+
compMemo.set(c, (x = { ts, top: Math.max(1, ...consts), ta: tupleOf(c.a), tb: tupleOf(c.b) }));
|
|
261
|
+
}
|
|
262
|
+
return x;
|
|
263
|
+
};
|
|
264
|
+
function stateOf(fs, patterns, tick) {
|
|
265
|
+
const fits = (map, key) => { const t = tupleOf(key), p = patterns[map]; return t && p && sameCoefs(t.coefs, p) ? t : undefined; };
|
|
266
|
+
const parts = new Set(), fnParts = [], fnSeen = new Set(), reads = [];
|
|
267
|
+
for (const f of fs)
|
|
268
|
+
for (const x of f.reads) {
|
|
269
|
+
reads.push(x);
|
|
270
|
+
if (fnMaps.has(x.map)) {
|
|
271
|
+
const n = partName(x), a = partName(x.key);
|
|
272
|
+
if (n && a) {
|
|
273
|
+
if (!fnSeen.has(n))
|
|
274
|
+
(fnSeen.add(n), fnParts.push({ f: x.map, n, a }));
|
|
275
|
+
parts.add(n);
|
|
276
|
+
parts.add(a);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
else
|
|
280
|
+
fits(x.map, x.key)?.parts.forEach(p => parts.add(p));
|
|
281
|
+
}
|
|
282
|
+
// (and the names of a key equality between tuples of one shape: of several parts, or of one where the other side is a part)
|
|
283
|
+
const eqs = fs.flatMap(f => f.comps.flatMap(c => { if (c.op !== '=')
|
|
284
|
+
return []; const { ta, tb } = compInfo(c); return ta && tb && sameCoefs(ta.coefs, tb.coefs) ? [{ ta, tb }] : []; }));
|
|
285
|
+
for (let grew = true; grew;) {
|
|
286
|
+
grew = false;
|
|
287
|
+
for (const { ta, tb } of eqs)
|
|
288
|
+
if (ta.parts.length > 1 || [...ta.parts, ...tb.parts].some(x => parts.has(x)))
|
|
289
|
+
for (const x of [...ta.parts, ...tb.parts])
|
|
290
|
+
if (!parts.has(x)) {
|
|
291
|
+
parts.add(x);
|
|
292
|
+
grew = true;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const bounded = new Map();
|
|
296
|
+
for (const f of fs)
|
|
297
|
+
for (const [id, b] of f.bounds) {
|
|
298
|
+
const had = bounded.get(id);
|
|
299
|
+
if (had === undefined || b < had)
|
|
300
|
+
bounded.set(id, b);
|
|
301
|
+
}
|
|
302
|
+
const ordMaps = new Set(), ordScalars = new Set();
|
|
303
|
+
const exactOf = (e) => (e.op === 'get' ? (!ordMaps.has(e.map) ? bounded.get(textOfE(e)) : undefined) : e.op === 'var' && !parts.has(e.name) && !ordScalars.has(e.name) ? bounded.get(textOfE(e)) : undefined);
|
|
304
|
+
// --- caps: a finite term's values are 0 .. cap, cap above every constant a comparison it is in has (so a larger value
|
|
305
|
+
// decides every comparison the cap does: the terms are naturals with coefficients of one sign)
|
|
306
|
+
const caps = new Map();
|
|
307
|
+
const termKey = (e) => (e.op === 'var' && !parts.has(e.name) ? `s:${e.name}` : e.op === 'get' ? `c:${e.map}` : undefined);
|
|
308
|
+
const ofParts = (t) => t?.parts.every(x => parts.has(x));
|
|
309
|
+
const ordComps = [];
|
|
310
|
+
for (const f of fs)
|
|
311
|
+
for (const c of f.comps) {
|
|
312
|
+
const ci = compInfo(c);
|
|
313
|
+
let ex = 0;
|
|
314
|
+
for (const [e, k] of ci.ts) {
|
|
315
|
+
const x = exactOf(e);
|
|
316
|
+
if (x !== undefined)
|
|
317
|
+
ex += Math.abs(k) * x;
|
|
318
|
+
}
|
|
319
|
+
// (and past what the exact terms beside can add: x <= Voted[k] with x at its cap is false at every larger x)
|
|
320
|
+
const top = ci.top + ex + 1;
|
|
321
|
+
for (const [e] of ci.ts) {
|
|
322
|
+
const t = termKey(e);
|
|
323
|
+
if (t)
|
|
324
|
+
caps.set(t, Math.max(caps.get(t) ?? 1, top));
|
|
325
|
+
}
|
|
326
|
+
if (!(ofParts(ci.ta) && ofParts(ci.tb)))
|
|
327
|
+
ordComps.push(ci.ts);
|
|
328
|
+
}
|
|
329
|
+
// ordered: a map's cells, or a scalar, compared with another non-constant term (an epoch with a node's epoch: Ivy's
|
|
330
|
+
// total order, `le`), not only with constants (a yes/no, a choice, which stay finite)
|
|
331
|
+
let grew = true;
|
|
332
|
+
const ordWalk = (ts) => {
|
|
333
|
+
const hasPart = ts.some(([e]) => (n => n !== undefined && parts.has(n))(partName(e)));
|
|
334
|
+
const nonPart = ts.filter(([e]) => (hasPart || exactOf(e) === undefined) && ((e.op === 'get' && !fnMaps.has(e.map)) || (e.op === 'var' && !parts.has(e.name))));
|
|
335
|
+
// (and every term beside an ordered one in a comparison: one value of the sort, an if-then-else's branches)
|
|
336
|
+
const tn = ts.filter(([e]) => hasPart || exactOf(e) === undefined), mixed = nonPart.length && tn.length > 1 && tn.some(t => t[1] > 0) && tn.some(t => t[1] < 0);
|
|
337
|
+
const beside = nonPart.some(([e]) => (e.op === 'get' && ordMaps.has(e.map)) || (e.op === 'var' && ordScalars.has(e.name)));
|
|
338
|
+
if (mixed || beside)
|
|
339
|
+
for (const [e] of nonPart) {
|
|
340
|
+
if (e.op === 'get' && !ordMaps.has(e.map)) {
|
|
341
|
+
ordMaps.add(e.map);
|
|
342
|
+
grew = true;
|
|
343
|
+
}
|
|
344
|
+
else if (e.op === 'var' && !ordScalars.has(e.name)) {
|
|
345
|
+
ordScalars.add(e.name);
|
|
346
|
+
grew = true;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
while (grew) {
|
|
351
|
+
grew = false;
|
|
352
|
+
ordComps.forEach(ts => (tick(), ordWalk(ts)));
|
|
353
|
+
}
|
|
354
|
+
const sorted = (xs) => [...xs].map(x => String(x)).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
355
|
+
// (the bounded terms not in it: each finite variable a goal reads is checked against them in its encoder)
|
|
356
|
+
return { parts, fnParts, bounded, caps, ordMaps, ordScalars, reads, sig: JSON.stringify([JSON.stringify(patterns), sorted(parts), sorted(caps), sorted(ordMaps), sorted(ordScalars)]) };
|
|
357
|
+
}
|
|
358
|
+
// One encoder per sort of goal (`State.sig`): every hypothesis it was asked is encoded once, its clauses guarded by an
|
|
359
|
+
// activation literal (¬a ∨ clause), its atoms, cells and ordered terms kept, the axioms over them all (each holds in every
|
|
360
|
+
// model of any goal), and one CDCL search (`cdclSolver`) whose learned clauses and theory lemmas stay. A goal is its
|
|
361
|
+
// hypotheses' literals assumed: a refutation's origins are hypotheses of the goal (a clause of a hypothesis not assumed is
|
|
362
|
+
// satisfied by its guard, so never a reason), a model is read back over the goal's own terms. Unsat here is unsat there,
|
|
363
|
+
// as for one goal alone; a model is judged on every hypothesis. GDP_EPR_INC=0: an encoder per goal, as before.
|
|
364
|
+
const incOn = env.GDP_EPR_INC !== '0';
|
|
365
|
+
const pool = new Map(), other = 'another encoder\'s goal';
|
|
366
|
+
function decide0(goal, patterns, opts) {
|
|
367
|
+
let ticks = 0;
|
|
368
|
+
const tick = () => { eprMeter.n++; if ((++ticks & 4095) === 0 && opts.late?.())
|
|
369
|
+
throw pastEnd; };
|
|
370
|
+
const fs = goal.hyps.map(h => factsOf(h, tick));
|
|
371
|
+
// --- which hypotheses are EPR-shaped: every map read at a key of its map's pattern
|
|
372
|
+
const shapedOf = (f) => f.reads.every(x => (fnMaps.has(x.map) ? partName(x) !== undefined : (t => !!t && !!patterns[x.map] && sameCoefs(t.coefs, patterns[x.map]))(x.args ? undefined : tupleOf(x.key))));
|
|
373
|
+
if (decideHook.on)
|
|
374
|
+
lastShaped = fs.map(shapedOf);
|
|
375
|
+
// (the conclusion's reads at keys of any radix shape: one of the wrong sort, Leader[k] <= Msg[k] with Msg a relation of
|
|
376
|
+
// pairs, is a cell of its own, as in a hypothesis)
|
|
377
|
+
const last = fs.at(-1);
|
|
378
|
+
if (last && !shapedOf(last) && last.reads.some(x => !fnMaps.has(x.map) && (x.args || !tupleOf(x.key))))
|
|
379
|
+
return { unknown: 'the conclusion is not EPR-shaped' };
|
|
380
|
+
const st = stateOf(fs, patterns, tick);
|
|
381
|
+
if (!incOn || opts.fill || opts.fresh)
|
|
382
|
+
return encoder(st, patterns).decide(goal, fs, st, opts);
|
|
383
|
+
let enc = pool.get(st.sig);
|
|
384
|
+
if (enc)
|
|
385
|
+
pool.delete(st.sig);
|
|
386
|
+
else if (pool.size >= 4)
|
|
387
|
+
pool.delete(pool.keys().next().value);
|
|
388
|
+
let d = enc?.decide(goal, fs, st, opts);
|
|
389
|
+
if (!d || ('unknown' in d && d.unknown === other))
|
|
390
|
+
d = (enc = encoder(st, patterns)).decide(goal, fs, st, opts);
|
|
391
|
+
if (enc.size() < 20_000)
|
|
392
|
+
pool.set(st.sig, enc);
|
|
393
|
+
return d;
|
|
394
|
+
}
|
|
395
|
+
function encoder(st, patterns) {
|
|
396
|
+
const { parts, bounded, caps, ordMaps, ordScalars } = st;
|
|
397
|
+
const fits = (map, key) => { const t = tupleOf(key), p = patterns[map]; return t && p && sameCoefs(t.coefs, p) ? t : undefined; };
|
|
398
|
+
const exactOf = (e) => (e.op === 'get' ? (!ordMaps.has(e.map) ? bounded.get(textOfE(e)) : undefined) : e.op === 'var' && !parts.has(e.name) && !ordScalars.has(e.name) ? bounded.get(textOfE(e)) : undefined);
|
|
399
|
+
let tick = () => { eprMeter.n++; };
|
|
400
|
+
// (what the hypothesis being encoded reads: its ordered terms, finite terms, opaque atoms and successors)
|
|
401
|
+
let use = { ords: [], fins: [], opq: [], succ: [] };
|
|
402
|
+
// --- atoms
|
|
403
|
+
const ids = new Map(), names = [''], kept = new Set(), first = new Set();
|
|
404
|
+
const atom = (key) => { let v = ids.get(key); if (v === undefined) {
|
|
405
|
+
v = names.length;
|
|
406
|
+
ids.set(key, v);
|
|
407
|
+
names.push(key);
|
|
408
|
+
} return { k: 'v', v }; };
|
|
409
|
+
// (the order's and equality's atoms by their two terms, without building their text again: the axioms ask them
|
|
410
|
+
// quadratically and the transitivity cubically)
|
|
411
|
+
const pairAtoms = (tag) => { const byA = new Map(); return (a, b) => { let in2 = byA.get(a); if (!in2)
|
|
412
|
+
byA.set(a, (in2 = new Map())); let f = in2.get(b); if (!f) {
|
|
413
|
+
in2.set(b, (f = atom(`${tag} ${a} ${b}`)));
|
|
414
|
+
if (tag === 'le')
|
|
415
|
+
kept.add(f.v);
|
|
416
|
+
} return f; }; };
|
|
417
|
+
const eqAt = pairAtoms('eq'), leAt = pairAtoms('le');
|
|
418
|
+
const eq = (x, y) => (x === y ? T : x < y ? eqAt(x, y) : eqAt(y, x));
|
|
419
|
+
// the ordered terms (every part among them), `le a b` a ≤ b
|
|
420
|
+
const ords = new Set(['k:0', ...[...parts].map(x => `p:${x}`)]);
|
|
421
|
+
const touch = (id) => { ords.add(id); use.ords.push(id); };
|
|
422
|
+
const le = (a, b) => (a === b ? T : leAt(a, b));
|
|
423
|
+
// a part under a bound, x < c: ¬(c ≤ x) in the order (below), the constant one of its terms
|
|
424
|
+
const ltAt = (x, c) => { if (c <= 0)
|
|
425
|
+
return Fa; touch(`k:${c}`); return not(le(`k:${c}`, `p:${x}`)); };
|
|
426
|
+
// a finite variable at a value: a scalar s:x, or a cell c:M(parts) of map M
|
|
427
|
+
const fins = new Map(), finsList = [];
|
|
428
|
+
const finAt = (f, v) => atom(`val ${f.id} ${v}`);
|
|
429
|
+
const newFin = (id, cap, ex, more, bkey) => {
|
|
430
|
+
const f = { id, cap: ex ?? cap, bkey, ...(ex !== undefined ? { exact: true } : {}), ...more };
|
|
431
|
+
fins.set(id, f);
|
|
432
|
+
finsList.push(f);
|
|
433
|
+
const ls = Array.from({ length: f.cap + 1 }, (_, val) => v(finAt(f, val)));
|
|
434
|
+
first.add(ls[0]);
|
|
435
|
+
// one value per finite variable: of an exact one, under each hypothesis that bounds it
|
|
436
|
+
if (!f.exact)
|
|
437
|
+
add(ls, -1);
|
|
438
|
+
else {
|
|
439
|
+
exactFins.set(bkey, f);
|
|
440
|
+
for (const b of boundBy.get(bkey) ?? [])
|
|
441
|
+
if (b.b === f.cap)
|
|
442
|
+
add([-b.a, ...ls], b.gid);
|
|
443
|
+
}
|
|
444
|
+
for (let i = 0; i < ls.length; i++)
|
|
445
|
+
for (let j = i + 1; j < ls.length; j++)
|
|
446
|
+
add([-ls[i], -ls[j]], -1);
|
|
447
|
+
return f;
|
|
448
|
+
};
|
|
449
|
+
const finOf = (e) => {
|
|
450
|
+
const f = finOf0(e);
|
|
451
|
+
if (f)
|
|
452
|
+
use.fins.push(f);
|
|
453
|
+
return f;
|
|
454
|
+
};
|
|
455
|
+
const finOf0 = (e) => {
|
|
456
|
+
if ((e.op === 'get' && (ordMaps.has(e.map) || fnMaps.has(e.map))) || (e.op === 'var' && ordScalars.has(e.name)))
|
|
457
|
+
return undefined;
|
|
458
|
+
const ex = exactOf(e);
|
|
459
|
+
if (e.op === 'var' && !parts.has(e.name)) {
|
|
460
|
+
const id = `s:${e.name}`;
|
|
461
|
+
return fins.get(id) ?? newFin(id, caps.get(id) ?? 1, ex, {}, textOfE(e));
|
|
462
|
+
}
|
|
463
|
+
if (e.op === 'get' && !e.args && !fits(e.map, e.key)) {
|
|
464
|
+
const id = `c:${e.map}#${textOfE(e.key)}`;
|
|
465
|
+
return fins.get(id) ?? newFin(id, caps.get(`c:${e.map}`) ?? 1, ex, { map: e.map, key: e.key }, textOfE(e));
|
|
466
|
+
}
|
|
467
|
+
if (e.op === 'get') {
|
|
468
|
+
const t = tupleOf(e.key), id = `c:${e.map}(${t.parts.join(',')})`;
|
|
469
|
+
return fins.get(id) ?? newFin(id, caps.get(`c:${e.map}`) ?? 1, ex, { map: e.map, parts: t.parts }, textOfE(e));
|
|
470
|
+
}
|
|
471
|
+
return undefined;
|
|
472
|
+
};
|
|
473
|
+
// (each bound a hypothesis puts on a term, M[t] <= b: the exact finite variable's one value under that hypothesis)
|
|
474
|
+
const boundBy = new Map(), exactFins = new Map();
|
|
475
|
+
// An ordered term: a part, an ordered scalar, an ordered cell (a tuple's, or one of its own at a key of the wrong
|
|
476
|
+
// sort), a constant; `le a b` is a ≤ b.
|
|
477
|
+
const ordInfo = new Map();
|
|
478
|
+
const ordOf = (e) => {
|
|
479
|
+
if (e.op === 'const') {
|
|
480
|
+
const id = `k:${e.value}`;
|
|
481
|
+
touch(id);
|
|
482
|
+
return id;
|
|
483
|
+
}
|
|
484
|
+
const pn = partName(e);
|
|
485
|
+
if (pn !== undefined && parts.has(pn)) {
|
|
486
|
+
touch(`p:${pn}`);
|
|
487
|
+
return `p:${pn}`;
|
|
488
|
+
}
|
|
489
|
+
if (e.op === 'var') {
|
|
490
|
+
if (ordScalars.has(e.name)) {
|
|
491
|
+
touch(`s:${e.name}`);
|
|
492
|
+
return `s:${e.name}`;
|
|
493
|
+
}
|
|
494
|
+
return undefined;
|
|
495
|
+
}
|
|
496
|
+
// (a function read at a key of the wrong sort, idn(64·m + n): a term of the order too, its value the read-back's)
|
|
497
|
+
if (e.op === 'get' && fnMaps.has(e.map)) {
|
|
498
|
+
const id = `c:${e.map}#${textOfE(e.key)}`;
|
|
499
|
+
touch(id);
|
|
500
|
+
return id;
|
|
501
|
+
}
|
|
502
|
+
if (e.op === 'get' && ordMaps.has(e.map) && !e.args) {
|
|
503
|
+
const t = fits(e.map, e.key);
|
|
504
|
+
const id = t ? `c:${e.map}(${t.parts.join(',')})` : `c:${e.map}#${textOfE(e.key)}`;
|
|
505
|
+
if (!ordInfo.has(id))
|
|
506
|
+
ordInfo.set(id, t ? { map: e.map, parts: t.parts } : { map: e.map, key: e.key });
|
|
507
|
+
touch(id);
|
|
508
|
+
return id;
|
|
509
|
+
}
|
|
510
|
+
return undefined;
|
|
511
|
+
};
|
|
512
|
+
// (a value: an ordered term plus a constant, `o`: the ring's n + 1)
|
|
513
|
+
const ordCases = (e) => {
|
|
514
|
+
if (e.op === 'if') {
|
|
515
|
+
const c = lit(e.c), a = ordCases(e.then), b = ordCases(e.else);
|
|
516
|
+
return a && b ? [...a.map(x => ({ ...x, g: and([c, x.g]) })), ...b.map(x => ({ ...x, g: and([not(c), x.g]) }))] : undefined;
|
|
517
|
+
}
|
|
518
|
+
if (e.op === 'add' && e.b.op === 'const') {
|
|
519
|
+
const a = ordCases(e.a);
|
|
520
|
+
return a?.map(x => ({ ...x, o: x.o + e.b.value }));
|
|
521
|
+
}
|
|
522
|
+
if (e.op === 'add' && e.a.op === 'const') {
|
|
523
|
+
const b = ordCases(e.b);
|
|
524
|
+
return b?.map(x => ({ ...x, o: x.o + e.a.value }));
|
|
525
|
+
}
|
|
526
|
+
const t = ordOf(e);
|
|
527
|
+
return t ? [{ g: T, t, o: 0 }] : undefined;
|
|
528
|
+
};
|
|
529
|
+
// t1 − t2 + d ⋈ 0 as order atoms (naturals: t1 < t2 is t1 + 1 ≤ t2), or undefined
|
|
530
|
+
// (a constant side takes the offset: t + d ⋈ k:c is t ⋈ k:(c − d), nothing below 0; t1 = t2 + 1, the successor, is an atom
|
|
531
|
+
// that implies t2 < t1 with no term between, one way only: a gap no term names leaves it unknown)
|
|
532
|
+
const succs = [], succAt = new Map();
|
|
533
|
+
const ordAtom = (t1, t2, d, op) => {
|
|
534
|
+
const num = (x) => (x.startsWith('k:') ? Number(x.slice(2)) : undefined), n1 = num(t1), n2 = num(t2);
|
|
535
|
+
if (n1 !== undefined && n2 !== undefined) {
|
|
536
|
+
const x = n1 - n2 + d;
|
|
537
|
+
return { k: 'c', b: op === '<' ? x < 0 : op === '<=' ? x <= 0 : x === 0 };
|
|
538
|
+
}
|
|
539
|
+
if (n2 !== undefined && d !== 0) {
|
|
540
|
+
const c = n2 - d;
|
|
541
|
+
if (c < 0)
|
|
542
|
+
return Fa;
|
|
543
|
+
touch(`k:${c}`);
|
|
544
|
+
return ordAtom(t1, `k:${c}`, 0, op);
|
|
545
|
+
}
|
|
546
|
+
if (n1 !== undefined && d !== 0) {
|
|
547
|
+
const c = n1 + d;
|
|
548
|
+
if (c < 0)
|
|
549
|
+
return op === '=' ? Fa : T;
|
|
550
|
+
touch(`k:${c}`);
|
|
551
|
+
return ordAtom(`k:${c}`, t2, 0, op);
|
|
552
|
+
}
|
|
553
|
+
const D = op === '<' ? d + 1 : d;
|
|
554
|
+
if (op === '=') {
|
|
555
|
+
if (d === 0)
|
|
556
|
+
return and([le(t1, t2), le(t2, t1)]);
|
|
557
|
+
if (Math.abs(d) !== 1)
|
|
558
|
+
return undefined;
|
|
559
|
+
const [lo, hi] = d === -1 ? [t2, t1] : [t1, t2], s = atom(`succ ${lo} ${hi}`);
|
|
560
|
+
let si = succAt.get(s.v);
|
|
561
|
+
if (si === undefined) {
|
|
562
|
+
succAt.set(s.v, (si = succs.length));
|
|
563
|
+
succs.push({ s: s.v, lo, hi });
|
|
564
|
+
}
|
|
565
|
+
use.succ.push(si);
|
|
566
|
+
return s;
|
|
567
|
+
}
|
|
568
|
+
return D === 0 ? le(t1, t2) : D === 1 ? not(le(t2, t1)) : undefined;
|
|
569
|
+
};
|
|
570
|
+
const ordered = (ts, d, op) => {
|
|
571
|
+
let a, b, dd = d;
|
|
572
|
+
if (ts.length === 2 && ts[0][1] === 1 && ts[1][1] === -1)
|
|
573
|
+
[a, b] = [ts[0][0], ts[1][0]];
|
|
574
|
+
else if (ts.length === 2 && ts[0][1] === -1 && ts[1][1] === 1)
|
|
575
|
+
[a, b] = [ts[1][0], ts[0][0]];
|
|
576
|
+
else if (ts.length === 1 && ts[0][1] === 1) {
|
|
577
|
+
a = ts[0][0];
|
|
578
|
+
if (-d < 0)
|
|
579
|
+
return Fa;
|
|
580
|
+
b = { op: 'const', value: -d };
|
|
581
|
+
dd = 0;
|
|
582
|
+
}
|
|
583
|
+
else if (ts.length === 1 && ts[0][1] === -1) {
|
|
584
|
+
b = ts[0][0];
|
|
585
|
+
if (d < 0)
|
|
586
|
+
return op === '<' || op === '<=' ? T : Fa;
|
|
587
|
+
a = { op: 'const', value: d };
|
|
588
|
+
dd = 0;
|
|
589
|
+
}
|
|
590
|
+
else
|
|
591
|
+
return undefined;
|
|
592
|
+
const ca = ordCases(a), cb = ordCases(b);
|
|
593
|
+
if (!ca || !cb || ca.length * cb.length > 256)
|
|
594
|
+
return undefined;
|
|
595
|
+
const out = [];
|
|
596
|
+
for (const x of ca)
|
|
597
|
+
for (const y of cb) {
|
|
598
|
+
const at = ordAtom(x.t, y.t, dd + x.o - y.o, op);
|
|
599
|
+
if (!at)
|
|
600
|
+
return undefined;
|
|
601
|
+
out.push(and([x.g, y.g, at]));
|
|
602
|
+
}
|
|
603
|
+
return or(out);
|
|
604
|
+
};
|
|
605
|
+
const cases = (e) => {
|
|
606
|
+
if (e.op === 'const')
|
|
607
|
+
return [{ g: T, n: e.value }];
|
|
608
|
+
if (e.op === 'if') {
|
|
609
|
+
const c = lit(e.c), a = cases(e.then), b = cases(e.else);
|
|
610
|
+
return a && b ? [...a.map(x => ({ ...x, g: and([c, x.g]) })), ...b.map(x => ({ ...x, g: and([not(c), x.g]) }))] : undefined;
|
|
611
|
+
}
|
|
612
|
+
const f = finOf(e);
|
|
613
|
+
return f ? [{ g: T, f }] : undefined;
|
|
614
|
+
};
|
|
615
|
+
// The comparison's sides as Σ coef·term + const; finite where every term is finite and every coefficient one sign.
|
|
616
|
+
// (a ∸ b is a − b where every term of b is a term of a, at least as often: never truncated, as the terms are naturals)
|
|
617
|
+
const exactSub = (e) => {
|
|
618
|
+
const ta = [], tb = [], da = { c: 0 }, db = { c: 0 };
|
|
619
|
+
if (!linTerms(e.a, 1, ta, da) || !linTerms(e.b, 1, tb, db) || db.c > da.c)
|
|
620
|
+
return false;
|
|
621
|
+
const have = new Map();
|
|
622
|
+
for (const [x, c] of ta)
|
|
623
|
+
have.set(x, (have.get(x) ?? 0) + c);
|
|
624
|
+
return tb.every(([x, c]) => (have.get(x) ?? 0) >= c);
|
|
625
|
+
};
|
|
626
|
+
const sum = (e, k, ts, d) => e.op === 'const' ? ((d.c += k * e.value), true) : e.op === 'add' ? sum(e.a, k, ts, d) && sum(e.b, k, ts, d) : e.op === 'mul' ? sum(e.a, k * e.k, ts, d)
|
|
627
|
+
: e.op === 'sub' && exactSub(e) ? sum(e.a, k, ts, d) && sum(e.b, -k, ts, d) : (ts.push([e, k]), true);
|
|
628
|
+
// the theory: each opaque comparison of key parts, ordered terms and composed keys (a part against 64·e + n, an
|
|
629
|
+
// instance at a key of the wrong sort) as linear arithmetic over the ordered terms, which a model's order is checked
|
|
630
|
+
// against below (each a term of the order, so its axioms hold)
|
|
631
|
+
const tlin = (e) => {
|
|
632
|
+
if (e.op === 'const')
|
|
633
|
+
return konst(e.value);
|
|
634
|
+
if (e.op === 'add') {
|
|
635
|
+
const a = tlin(e.a), b = tlin(e.b);
|
|
636
|
+
return a && b ? plus(a, b) : undefined;
|
|
637
|
+
}
|
|
638
|
+
if (e.op === 'mul') {
|
|
639
|
+
const a = tlin(e.a);
|
|
640
|
+
return a && times(a, e.k);
|
|
641
|
+
}
|
|
642
|
+
const t = ordOf(e);
|
|
643
|
+
return t === undefined ? undefined : t.startsWith('k:') ? konst(Number(t.slice(2))) : tv(t);
|
|
644
|
+
};
|
|
645
|
+
const opqs = new Map();
|
|
646
|
+
const opaque = (c, more) => {
|
|
647
|
+
const a = atom(`op ${textOfE(c)}`);
|
|
648
|
+
let o = opqs.get(a.v);
|
|
649
|
+
if (!o) {
|
|
650
|
+
const outer = use;
|
|
651
|
+
use = { ords: [], fins: [], opq: [], succ: [] };
|
|
652
|
+
let keyOnly = true;
|
|
653
|
+
const wk = (q) => { if (!keyOnly || !q || typeof q !== 'object')
|
|
654
|
+
return; const x = q; if ((x.op === 'get' && !fnMaps.has(x.map) && !ordMaps.has(x.map)) || (x.op === 'var' && !parts.has(x.name) && !ordScalars.has(x.name))) {
|
|
655
|
+
keyOnly = false;
|
|
656
|
+
return;
|
|
657
|
+
} for (const y of Object.values(q))
|
|
658
|
+
wk(y); };
|
|
659
|
+
wk(c);
|
|
660
|
+
opqs.set(a.v, (o = { foreign: !keyOnly, tied: !!more, ords: [] }));
|
|
661
|
+
more?.(a.v);
|
|
662
|
+
if (keyOnly && (c.op === '<' || c.op === '<=' || c.op === '=')) {
|
|
663
|
+
const ta = tlin(c.a), tb = tlin(c.b);
|
|
664
|
+
if (ta && tb) {
|
|
665
|
+
o.row = { x: a.v, op: c.op, d: plus(ta, times(tb, -1)) };
|
|
666
|
+
kept.add(a.v);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
o.ords = use.ords;
|
|
670
|
+
outer.fins.push(...use.fins);
|
|
671
|
+
outer.succ.push(...use.succ);
|
|
672
|
+
use = outer;
|
|
673
|
+
}
|
|
674
|
+
use.opq.push(a.v);
|
|
675
|
+
use.ords.push(...o.ords);
|
|
676
|
+
return a;
|
|
677
|
+
};
|
|
678
|
+
// an interval of a prefix: equal prefixes give lo always and hi where X's low parts are under their bounds; lo and hi
|
|
679
|
+
// with every part of X and L but the top under its bound give equal prefixes (a pair of one X and one L)
|
|
680
|
+
const baseOf = (coefs, i) => coefs[i - 1] / coefs[i];
|
|
681
|
+
const pairs = new Map();
|
|
682
|
+
const comparison = (c) => {
|
|
683
|
+
// a key equality: two tuples of one shape
|
|
684
|
+
if (c.op === '=') {
|
|
685
|
+
const ta = tupleOf(c.a), tb = tupleOf(c.b);
|
|
686
|
+
if (ta && tb && sameCoefs(ta.coefs, tb.coefs) && [...ta.parts, ...tb.parts].every(p => parts.has(p))) {
|
|
687
|
+
// equal parts: equal keys, always; equal keys with every part but the top under its bound: equal parts
|
|
688
|
+
return opaque(c, pv => {
|
|
689
|
+
const es = ta.parts.map((x, i) => eq(x, tb.parts[i]));
|
|
690
|
+
add([...es.map(e => (e.k === 'v' ? -e.v : 0)).filter(Boolean), pv], -1);
|
|
691
|
+
const bd = ta.bounds.flatMap((b, i) => (b === undefined ? [] : [v(ltAt(ta.parts[i], b)), v(ltAt(tb.parts[i], b))]));
|
|
692
|
+
for (const e of es)
|
|
693
|
+
if (e.k === 'v')
|
|
694
|
+
add([-pv, ...bd.map(l => -l), e.v], -1);
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
const ts = [], d = { c: 0 };
|
|
699
|
+
sum(c.a, 1, ts, d);
|
|
700
|
+
sum(c.b, -1, ts, d);
|
|
701
|
+
// a part under a bound: x < k, x <= k
|
|
702
|
+
if (ts.length === 1 && ts[0][0].op === 'var' && parts.has(ts[0][0].name) && Math.abs(ts[0][1]) === 1 && c.op !== '=') {
|
|
703
|
+
const [[x, s]] = ts, k = -d.c * s; // s·x + d ⋈ 0
|
|
704
|
+
const strict = c.op === '<';
|
|
705
|
+
// s = 1: x + d < 0 → x < −d; x + d <= 0 → x < −d + 1. s = −1: −x + d < 0 → x > d → ¬(x < d + 1); <= : ¬(x < d)
|
|
706
|
+
return s === 1 ? ltAt(x.name, strict ? k : k + 1) : not(ltAt(x.name, strict ? k + 1 : k));
|
|
707
|
+
}
|
|
708
|
+
// a tuple against a prefix of its chain: L <= X (lo), X < L + c (hi), c the prefix's last coefficient
|
|
709
|
+
if (c.op !== '=' && ts.length && ts.every(([e]) => e.op === 'var' && parts.has(e.name))) {
|
|
710
|
+
const merged = new Map();
|
|
711
|
+
for (const [e, k] of ts)
|
|
712
|
+
merged.set(e.name, (merged.get(e.name) ?? 0) + k);
|
|
713
|
+
const pos = [...merged].filter(t => t[1] > 0).sort((a, b) => b[1] - a[1]), neg = [...merged].filter(t => t[1] < 0).map(([x, k]) => [x, -k]).sort((a, b) => b[1] - a[1]);
|
|
714
|
+
const k0 = d.c + (c.op === '<' ? 1 : 0); // the form Σ ≤ 0 over integers: P − Q + k0 ≤ 0
|
|
715
|
+
const chainOf = (ts2) => ts2.map(t => t[1]);
|
|
716
|
+
const isChain = (cs) => cs.length > 0 && cs.every((x, i) => i + 1 >= cs.length || (x % cs[i + 1] === 0 && x / cs[i + 1] >= 2));
|
|
717
|
+
const prefix = (full, pre) => pre.length < full.length && pre.every((x, i) => x === full[i]);
|
|
718
|
+
// lo: L − X ≤ 0, P = L (prefix), Q = X (tuple, ending in 1); hi: X − L − c + 1 ≤ 0, P = X, Q = L
|
|
719
|
+
const lo = k0 === 0 && chainOf(neg).at(-1) === 1 && isChain(chainOf(neg)) && prefix(chainOf(neg), chainOf(pos));
|
|
720
|
+
const hi = chainOf(pos).at(-1) === 1 && isChain(chainOf(pos)) && prefix(chainOf(pos), chainOf(neg)) && neg.length > 0 && k0 === 1 - neg.at(-1)[1];
|
|
721
|
+
if (lo || hi) {
|
|
722
|
+
const X = (lo ? neg : pos).map(t => t[0]), L = (lo ? pos : neg).map(t => t[0]), coefs = chainOf(lo ? neg : pos), kind = lo ? 'lo' : 'hi';
|
|
723
|
+
return opaque(c, pv => {
|
|
724
|
+
const pe = L.map((y, i) => eq(X[i], y));
|
|
725
|
+
const lowBounded = X.flatMap((x, i) => (i >= L.length ? [v(ltAt(x, baseOf(coefs, i)))] : []));
|
|
726
|
+
add([...pe.filter(e => e.k === 'v').map(e => -e.v), ...(kind === 'hi' ? lowBounded.map(l => -l) : []), pv], -1);
|
|
727
|
+
const key = `${X.join(',')}|${L.join(',')}`, pr = pairs.get(key) ?? { X, L, coefs };
|
|
728
|
+
const had = pr.lo !== undefined && pr.hi !== undefined;
|
|
729
|
+
pr[kind] = pv;
|
|
730
|
+
pairs.set(key, pr);
|
|
731
|
+
if (had || pr.lo === undefined || pr.hi === undefined)
|
|
732
|
+
return;
|
|
733
|
+
const bd = [...pr.X.flatMap((x, i) => (i ? [v(ltAt(x, baseOf(pr.coefs, i)))] : [])), ...pr.L.flatMap((y, i) => (i ? [v(ltAt(y, baseOf(pr.coefs, i)))] : []))];
|
|
734
|
+
for (const [i, y] of pr.L.entries()) {
|
|
735
|
+
const e = eq(pr.X[i], y);
|
|
736
|
+
if (e.k === 'v')
|
|
737
|
+
add([-pr.lo, -pr.hi, ...bd.map(l => -l), e.v], -1);
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
// ordered: a term against another, or a constant, one of them an ordered cell or scalar
|
|
743
|
+
{
|
|
744
|
+
const reads = (e) => e.op === 'get' ? ordMaps.has(e.map) || fnMaps.has(e.map) : e.op === 'var' ? ordScalars.has(e.name) || parts.has(e.name) : e.op === 'if' ? reads(e.then) || reads(e.else) : e.op === 'add' ? reads(e.a) || reads(e.b) : false;
|
|
745
|
+
if (ts.some(([e]) => reads(e))) {
|
|
746
|
+
const f = ordered(ts, d.c, c.op);
|
|
747
|
+
if (f)
|
|
748
|
+
return f;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
// finite: every term finite, coefficients of one sign
|
|
752
|
+
// (the terms not exact of one sign, the exact ones of any: a capped term at its cap decides it as any larger value)
|
|
753
|
+
const cs = ts.length ? ts.map(([e]) => cases(e)) : [], loose = ts.filter((_, i) => !cs[i]?.every(y => y.n !== undefined || y.f.exact));
|
|
754
|
+
if (ts.length && (loose.every(t => t[1] > 0) || loose.every(t => t[1] < 0))) {
|
|
755
|
+
if (cs.every(Boolean)) {
|
|
756
|
+
const holds = (v) => (c.op === '<' ? v < 0 : c.op === '<=' ? v <= 0 : v === 0);
|
|
757
|
+
const out = [];
|
|
758
|
+
let combos = 0;
|
|
759
|
+
const go = (i, acc, g) => {
|
|
760
|
+
if (++combos > 4096)
|
|
761
|
+
return false;
|
|
762
|
+
if (i === ts.length) {
|
|
763
|
+
if (holds(acc + d.c))
|
|
764
|
+
out.push(and(g));
|
|
765
|
+
return true;
|
|
766
|
+
}
|
|
767
|
+
for (const x of cs[i]) {
|
|
768
|
+
if (x.n !== undefined) {
|
|
769
|
+
if (!go(i + 1, acc + ts[i][1] * x.n, [...g, x.g]))
|
|
770
|
+
return false;
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
for (let v = 0; v <= x.f.cap; v++)
|
|
774
|
+
if (!go(i + 1, acc + ts[i][1] * v, [...g, x.g, finAt(x.f, v)]))
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
return true;
|
|
778
|
+
};
|
|
779
|
+
if (go(0, 0, []))
|
|
780
|
+
return or(out);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
if (!ts.length)
|
|
784
|
+
return { k: 'c', b: c.op === '<' ? d.c < 0 : c.op === '<=' ? d.c <= 0 : d.c === 0 };
|
|
785
|
+
return opaque(c);
|
|
786
|
+
};
|
|
787
|
+
const lit = (c) => {
|
|
788
|
+
switch (c.op) {
|
|
789
|
+
case 'and': return and(c.xs.map(lit));
|
|
790
|
+
case 'or': return or(c.xs.map(lit));
|
|
791
|
+
case 'not': return not(lit(c.x));
|
|
792
|
+
case '<':
|
|
793
|
+
case '<=':
|
|
794
|
+
case '=': return comparison(c);
|
|
795
|
+
case 'all':
|
|
796
|
+
case 'flag': return opaque(c);
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
// --- clauses (literals: +v / −v), each with the hypothesis it stands for, into the search at once
|
|
800
|
+
const solver = cdclSolver(first, kept);
|
|
801
|
+
const add = (lits, from) => {
|
|
802
|
+
tick();
|
|
803
|
+
// (a short clause, the order's axioms' above all, without a Set: the same literals in the same order)
|
|
804
|
+
if (lits.length <= 4) {
|
|
805
|
+
const out = [];
|
|
806
|
+
for (const l of lits) {
|
|
807
|
+
if (out.includes(-l))
|
|
808
|
+
return;
|
|
809
|
+
if (!out.includes(l))
|
|
810
|
+
out.push(l);
|
|
811
|
+
}
|
|
812
|
+
solver.add({ lits: out, from });
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
const s = new Set(lits);
|
|
816
|
+
for (const l of s)
|
|
817
|
+
if (s.has(-l))
|
|
818
|
+
return;
|
|
819
|
+
solver.add({ lits: [...s], from });
|
|
820
|
+
};
|
|
821
|
+
let fresh = 0;
|
|
822
|
+
// (a Tseitin variable's definition holds in every model: an axiom; the hypothesis' own clause is the guarded one)
|
|
823
|
+
const tseitin = (f) => {
|
|
824
|
+
if (f.k === 'v')
|
|
825
|
+
return f.v;
|
|
826
|
+
if (f.k === 'not')
|
|
827
|
+
return -tseitin(f.x);
|
|
828
|
+
if (f.k === 'c') {
|
|
829
|
+
const g = ++fresh + 1e6;
|
|
830
|
+
add([f.b ? g : -g], -1);
|
|
831
|
+
return g;
|
|
832
|
+
}
|
|
833
|
+
const ls = f.xs.map(x => tseitin(x)), g = ++fresh + 1e6;
|
|
834
|
+
if (f.k === 'and') {
|
|
835
|
+
for (const l of ls)
|
|
836
|
+
add([-g, l], -1);
|
|
837
|
+
add([g, ...ls.map(l => -l)], -1);
|
|
838
|
+
}
|
|
839
|
+
else {
|
|
840
|
+
add([-g, ...ls], -1);
|
|
841
|
+
for (const l of ls)
|
|
842
|
+
add([g, -l], -1);
|
|
843
|
+
}
|
|
844
|
+
return g;
|
|
845
|
+
};
|
|
846
|
+
const top = (f, from, ng) => {
|
|
847
|
+
if (f.k === 'and') {
|
|
848
|
+
for (const x of f.xs)
|
|
849
|
+
top(x, from, ng);
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
if (f.k === 'c') {
|
|
853
|
+
if (!f.b)
|
|
854
|
+
add([ng], from);
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
if (f.k === 'or' && f.xs.every(x => x.k === 'v' || (x.k === 'not' && x.x.k === 'v'))) {
|
|
858
|
+
add([ng, ...f.xs.map(x => (x.k === 'v' ? x.v : -x.x.v))], from);
|
|
859
|
+
return;
|
|
860
|
+
}
|
|
861
|
+
add([ng, tseitin(f)], from);
|
|
862
|
+
};
|
|
863
|
+
const v = (f) => (f.k === 'v' ? f.v : f.k === 'not' && f.x.k === 'v' ? -f.x.v : undefined);
|
|
864
|
+
// --- the hypotheses, each once by its text
|
|
865
|
+
const hyps = new Map();
|
|
866
|
+
const encode = (f) => {
|
|
867
|
+
let hp = hyps.get(f.text);
|
|
868
|
+
if (hp)
|
|
869
|
+
return hp;
|
|
870
|
+
const gid = hyps.size, a = ++fresh + 1e6;
|
|
871
|
+
use = { ords: [], fins: [], opq: [], succ: [] };
|
|
872
|
+
for (const [k, b] of f.bounds) {
|
|
873
|
+
let xs = boundBy.get(k);
|
|
874
|
+
if (!xs)
|
|
875
|
+
boundBy.set(k, (xs = []));
|
|
876
|
+
xs.push({ a, gid, b });
|
|
877
|
+
const ef = exactFins.get(k);
|
|
878
|
+
if (ef && ef.cap === b)
|
|
879
|
+
add([-a, ...Array.from({ length: b + 1 }, (_, val) => v(finAt(ef, val)))], gid);
|
|
880
|
+
}
|
|
881
|
+
tick();
|
|
882
|
+
if (f.h)
|
|
883
|
+
top(lit(f.h), gid, -a);
|
|
884
|
+
hp = { a, gid, use };
|
|
885
|
+
hyps.set(f.text, hp);
|
|
886
|
+
return hp;
|
|
887
|
+
};
|
|
888
|
+
// --- the axioms over every term so far, each once: congruence of cells; one value per finite variable (above); the
|
|
889
|
+
// order: total, transitive (lazily past 64 terms), discrete, the constants in their order, every term at least 0, a part's
|
|
890
|
+
// equality its order both ways, equal tuples equal ordered cells; a function's reads at equal parts are equal
|
|
891
|
+
// (Ackermann); a successor has nothing between it and its predecessor
|
|
892
|
+
const num = (x) => (x.startsWith('k:') ? Number(x.slice(2)) : undefined);
|
|
893
|
+
const os = [], cellsBy = new Map(), ordCellsBy = new Map(), fnAll = [], fnSeen = new Set(), discrete = new Set();
|
|
894
|
+
let nFins = 0, nSuccs = 0, nFn = 0, lazy = false;
|
|
895
|
+
const pairsOn = (xs, from, f) => { for (let j = from; j < xs.length; j++)
|
|
896
|
+
for (let i = 0; i < j; i++)
|
|
897
|
+
f(xs[i], xs[j]); };
|
|
898
|
+
const close = (gst) => {
|
|
899
|
+
for (; nFins < finsList.length; nFins++) {
|
|
900
|
+
const b = finsList[nFins];
|
|
901
|
+
if (!b.map || !b.parts)
|
|
902
|
+
continue;
|
|
903
|
+
let cells = cellsBy.get(b.map);
|
|
904
|
+
if (!cells)
|
|
905
|
+
cellsBy.set(b.map, (cells = []));
|
|
906
|
+
cells.push(b);
|
|
907
|
+
for (const a of cells.slice(0, -1)) {
|
|
908
|
+
const es = a.parts.map((x, k) => v(eq(x, b.parts[k]))), cap = Math.max(a.cap, b.cap);
|
|
909
|
+
const pre = es.filter((e) => e !== undefined).map(e => -e);
|
|
910
|
+
for (let val = 0; val <= cap; val++) {
|
|
911
|
+
const la = val <= a.cap ? v(finAt(a, val)) : undefined, lb = val <= b.cap ? v(finAt(b, val)) : undefined;
|
|
912
|
+
if (la !== undefined)
|
|
913
|
+
add([...pre, -la, ...(lb !== undefined ? [lb] : [])], -1);
|
|
914
|
+
if (lb !== undefined)
|
|
915
|
+
add([...pre, -lb, ...(la !== undefined ? [la] : [])], -1);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
const o0 = os.length, all0 = [...ords];
|
|
920
|
+
for (let i = o0; i < all0.length; i++)
|
|
921
|
+
os.push(all0[i]);
|
|
922
|
+
if (os.length > 64)
|
|
923
|
+
lazy = true;
|
|
924
|
+
pairsOn(os, o0, (a, b) => {
|
|
925
|
+
add([v(le(a, b)), v(le(b, a))], -1);
|
|
926
|
+
const na = num(a), nb = num(b);
|
|
927
|
+
if (na !== undefined && nb !== undefined) {
|
|
928
|
+
add([na <= nb ? v(le(a, b)) : -v(le(a, b))], -1);
|
|
929
|
+
add([nb <= na ? v(le(b, a)) : -v(le(b, a))], -1);
|
|
930
|
+
}
|
|
931
|
+
if (a.startsWith('p:') && b.startsWith('p:')) {
|
|
932
|
+
const e = v(eq(a.slice(2), b.slice(2))), x = v(le(a, b)), y = v(le(b, a));
|
|
933
|
+
add([-e, x], -1);
|
|
934
|
+
add([-e, y], -1);
|
|
935
|
+
add([e, -x, -y], -1);
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
for (const x of os.slice(o0))
|
|
939
|
+
if (x !== 'k:0')
|
|
940
|
+
add([v(le('k:0', x))], -1);
|
|
941
|
+
// (the naturals are discrete: nothing between c and c + 1)
|
|
942
|
+
for (const c of os.map(num).filter((n) => n !== undefined))
|
|
943
|
+
if (ords.has(`k:${c + 1}`))
|
|
944
|
+
for (const x of os)
|
|
945
|
+
if (!x.startsWith('k:') && !discrete.has(`${c}|${x}`)) {
|
|
946
|
+
discrete.add(`${c}|${x}`);
|
|
947
|
+
add([v(le(x, `k:${c}`)), v(le(`k:${c + 1}`, x))], -1);
|
|
948
|
+
}
|
|
949
|
+
if (!lazy)
|
|
950
|
+
for (let m = o0; m < os.length; m++)
|
|
951
|
+
for (let x = 0; x < m; x++)
|
|
952
|
+
for (let y = 0; y < m; y++)
|
|
953
|
+
if (x !== y) {
|
|
954
|
+
const t = os[m], p = os[x], q = os[y];
|
|
955
|
+
add([-v(le(t, p)), -v(le(p, q)), v(le(t, q))], -1);
|
|
956
|
+
add([-v(le(p, t)), -v(le(t, q)), v(le(p, q))], -1);
|
|
957
|
+
add([-v(le(p, q)), -v(le(q, t)), v(le(p, t))], -1);
|
|
958
|
+
}
|
|
959
|
+
for (const fp of gst.fnParts)
|
|
960
|
+
if (!fnSeen.has(fp.n)) {
|
|
961
|
+
fnSeen.add(fp.n);
|
|
962
|
+
fnAll.push(fp);
|
|
963
|
+
}
|
|
964
|
+
for (let j = nFn; j < fnAll.length; j++)
|
|
965
|
+
for (let i = 0; i < j; i++) {
|
|
966
|
+
const x = fnAll[i], y = fnAll[j];
|
|
967
|
+
if (x.f === y.f && x.n !== y.n) {
|
|
968
|
+
const a = v(eq(x.a, y.a)), b = v(eq(x.n, y.n));
|
|
969
|
+
if (a !== undefined && b !== undefined)
|
|
970
|
+
add([-a, b], -1);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
nFn = fnAll.length;
|
|
974
|
+
for (let k = 0; k < succs.length; k++) {
|
|
975
|
+
const { s, lo, hi } = succs[k];
|
|
976
|
+
if (k >= nSuccs)
|
|
977
|
+
add([-s, -v(le(hi, lo))], -1);
|
|
978
|
+
for (const z of k >= nSuccs ? os : os.slice(o0))
|
|
979
|
+
if (z !== lo && z !== hi)
|
|
980
|
+
add([-s, v(le(z, lo)), v(le(hi, z))], -1);
|
|
981
|
+
}
|
|
982
|
+
nSuccs = succs.length;
|
|
983
|
+
for (const x of os.slice(o0)) {
|
|
984
|
+
const inf = ordInfo.get(x);
|
|
985
|
+
if (inf?.parts) {
|
|
986
|
+
let cs = ordCellsBy.get(inf.map);
|
|
987
|
+
if (!cs)
|
|
988
|
+
ordCellsBy.set(inf.map, (cs = []));
|
|
989
|
+
cs.push(x);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
for (const [, cells] of ordCellsBy) {
|
|
993
|
+
const from = cells.findIndex(x => os.indexOf(x) >= o0);
|
|
994
|
+
if (from >= 0)
|
|
995
|
+
pairsOn(cells, from, (a, b) => {
|
|
996
|
+
const pa = ordInfo.get(a).parts, pb = ordInfo.get(b).parts, pre = pa.map((x, k) => v(eq(x, pb[k]))).filter((e) => e !== undefined).map(e => -e);
|
|
997
|
+
add([...pre, v(le(a, b))], -1);
|
|
998
|
+
add([...pre, v(le(b, a))], -1);
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
const lemmasIn = new Set();
|
|
1003
|
+
let lastGoal = '';
|
|
1004
|
+
const seedLemmas = () => { for (const [key, l] of lemmaCache) {
|
|
1005
|
+
if (lemmasIn.has(key))
|
|
1006
|
+
continue;
|
|
1007
|
+
const xs = l.map(([k, b]) => { const x = ids.get(k); return x === undefined ? undefined : b ? x : -x; });
|
|
1008
|
+
if (xs.every((x) => x !== undefined)) {
|
|
1009
|
+
lemmasIn.add(key);
|
|
1010
|
+
add(xs, -1);
|
|
1011
|
+
}
|
|
1012
|
+
} };
|
|
1013
|
+
const decideGoal = (goal, fs, gst, opts) => {
|
|
1014
|
+
tick = () => { eprMeter.n++; if ((++ticks & 4095) === 0 && opts.late?.())
|
|
1015
|
+
throw pastEnd; };
|
|
1016
|
+
let ticks = 0;
|
|
1017
|
+
const hs = fs.map(encode), gOrds = new Set(['k:0', ...[...parts].map(x => `p:${x}`)]), gFins = new Set(), gOpq = new Set(), gSuccs = new Set();
|
|
1018
|
+
for (const h of hs) {
|
|
1019
|
+
for (const x of h.use.ords)
|
|
1020
|
+
gOrds.add(x);
|
|
1021
|
+
for (const f of h.use.fins)
|
|
1022
|
+
gFins.add(f);
|
|
1023
|
+
for (const x of h.use.opq)
|
|
1024
|
+
gOpq.add(x);
|
|
1025
|
+
for (const x of h.use.succ)
|
|
1026
|
+
gSuccs.add(x);
|
|
1027
|
+
}
|
|
1028
|
+
// (`fill`: the constants between two near ones too, so that discreteness counts the room between them: k:0 < x < y < k:2
|
|
1029
|
+
// has none, which the read-back meets)
|
|
1030
|
+
if (opts.fill) {
|
|
1031
|
+
const cs = [...gOrds].filter(x => x.startsWith('k:')).map(x => Number(x.slice(2))).sort((a, b) => a - b);
|
|
1032
|
+
for (let i = 0; i + 1 < cs.length; i++)
|
|
1033
|
+
if (cs[i + 1] - cs[i] <= 4)
|
|
1034
|
+
for (let c = cs[i] + 1; c < cs[i + 1]; c++) {
|
|
1035
|
+
ords.add(`k:${c}`);
|
|
1036
|
+
gOrds.add(`k:${c}`);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
// (an order over many terms is a cubic number of transitivity clauses: past 64 terms they are added lazily, those a
|
|
1040
|
+
// model breaks, below; past 512, the totality's pairs alone are too many, and the goal keeps the usual path)
|
|
1041
|
+
// (a finite variable read as exact here and not there, or at another bound: this goal's sorts aren't the encoder's)
|
|
1042
|
+
for (const f of gFins)
|
|
1043
|
+
if (gst.bounded.get(f.bkey) !== (f.exact ? f.cap : undefined))
|
|
1044
|
+
return { unknown: other };
|
|
1045
|
+
if (gOrds.size > 512 || ords.size > 512) {
|
|
1046
|
+
over = true;
|
|
1047
|
+
return { unknown: `${gOrds.size} ordered terms` };
|
|
1048
|
+
}
|
|
1049
|
+
close(gst);
|
|
1050
|
+
const theory = [...gOpq].flatMap(x => { const o = opqs.get(x); return !o.foreign && o.row ? [o.row] : []; });
|
|
1051
|
+
if (theory.length && !opts.fresh)
|
|
1052
|
+
seedLemmas();
|
|
1053
|
+
// EPR-shaped: no comparison the abstraction can't read (a counter's arithmetic, a sum, a product) in any hypothesis
|
|
1054
|
+
if (opts.exact) {
|
|
1055
|
+
const loose = [...gOpq].filter(x => { const o = opqs.get(x); return o.foreign && !o.tied; }).length;
|
|
1056
|
+
if (loose)
|
|
1057
|
+
return { unknown: `${loose} comparisons outside the EPR shape` };
|
|
1058
|
+
}
|
|
1059
|
+
// --- the search, the goal's hypotheses assumed
|
|
1060
|
+
if (opts.late?.())
|
|
1061
|
+
return { unknown: 'past the deadline' };
|
|
1062
|
+
if (goal.name !== lastGoal)
|
|
1063
|
+
solver.fresh();
|
|
1064
|
+
lastGoal = goal.name;
|
|
1065
|
+
const assume = [...new Set(hs.map(h => h.a))], index = new Map();
|
|
1066
|
+
hs.forEach((h, i) => { if (!index.has(h.gid))
|
|
1067
|
+
index.set(h.gid, i); });
|
|
1068
|
+
const lemma = (lits) => add(lits, -1);
|
|
1069
|
+
const gos = [...gOrds];
|
|
1070
|
+
// (lazily: the transitivity a model breaks among the goal's terms, added and the search again, at most 4,096 a round
|
|
1071
|
+
// and 16 rounds; as each holds in every model, a refutation stays one)
|
|
1072
|
+
const search = () => {
|
|
1073
|
+
let o = solver.solve(opts.conflicts ?? 200_000, opts.late, assume);
|
|
1074
|
+
for (let round = 0; lazy && o?.sat; round++) {
|
|
1075
|
+
const m = o.model, at = (a, b) => a === b || m[v(le(a, b))] === true, broken = [];
|
|
1076
|
+
for (const a of gos)
|
|
1077
|
+
for (const b of gos)
|
|
1078
|
+
if (a !== b && at(a, b))
|
|
1079
|
+
for (const c of gos)
|
|
1080
|
+
if (c !== a && c !== b && at(b, c) && !at(a, c) && broken.length < 4096)
|
|
1081
|
+
broken.push([-v(le(a, b)), -v(le(b, c)), v(le(a, c))]);
|
|
1082
|
+
if (!broken.length)
|
|
1083
|
+
break;
|
|
1084
|
+
if (round === 16 || opts.late?.())
|
|
1085
|
+
return 'the order\'s transitivity, lazily, did not settle';
|
|
1086
|
+
for (const c of broken)
|
|
1087
|
+
lemma(c);
|
|
1088
|
+
o = solver.solve(opts.conflicts ?? 200_000, opts.late, assume);
|
|
1089
|
+
}
|
|
1090
|
+
return o;
|
|
1091
|
+
};
|
|
1092
|
+
// (the model's order, its groups of equal terms in order: the read-back's and the theory's)
|
|
1093
|
+
const groupsOf = (m) => {
|
|
1094
|
+
const leq = (a, b) => a === b || m[v(le(a, b))] === true;
|
|
1095
|
+
const os2 = [...gos].sort((a, b) => (leq(a, b) && !leq(b, a) ? -1 : leq(b, a) && !leq(a, b) ? 1 : 0)), groups = [];
|
|
1096
|
+
for (let i = 0; i < os2.length;) {
|
|
1097
|
+
let j = i;
|
|
1098
|
+
while (j < os2.length && leq(os2[j], os2[i]) && leq(os2[i], os2[j]))
|
|
1099
|
+
j++;
|
|
1100
|
+
groups.push(os2.slice(i, j));
|
|
1101
|
+
i = j;
|
|
1102
|
+
}
|
|
1103
|
+
return groups;
|
|
1104
|
+
};
|
|
1105
|
+
const succList = [...gSuccs].map(k => succs[k]);
|
|
1106
|
+
// The theory's check of a model: its order (each group's terms equal, each group below the next, a successor the next
|
|
1107
|
+
// value), every ordered term a natural, each theory comparison as the model has it, solved over the integers. Sat: the
|
|
1108
|
+
// values, the read-back's; refuted: the model's literals its refutation reads, whose negation holds in every model (a
|
|
1109
|
+
// theory lemma, DPLL(T)'s), added and the search again.
|
|
1110
|
+
// (first over the theory's own terms, each pair as the model orders it, so that a lemma names them alone; then every
|
|
1111
|
+
// term, the model's order as its groups, for the values)
|
|
1112
|
+
const theoryTerms = [...new Set(theory.flatMap(t => Object.keys(t.d).filter(x => x !== '1')))];
|
|
1113
|
+
// (each comparison's literals: its own and its terms' order, among them and with the constants)
|
|
1114
|
+
const feasible = [], locals = [];
|
|
1115
|
+
const localOf = (ti, t, ks) => {
|
|
1116
|
+
if (locals[ti])
|
|
1117
|
+
return locals[ti];
|
|
1118
|
+
const xs = Object.keys(t.d).filter(x => x !== '1'), out = [t.x];
|
|
1119
|
+
for (let i2 = 0; i2 < xs.length; i2++)
|
|
1120
|
+
for (const b of [...xs.slice(i2 + 1), ...ks])
|
|
1121
|
+
for (const [p, q] of [[xs[i2], b], [b, xs[i2]]]) {
|
|
1122
|
+
const x = v(le(p, q));
|
|
1123
|
+
if (x !== undefined)
|
|
1124
|
+
out.push(x);
|
|
1125
|
+
}
|
|
1126
|
+
return (locals[ti] = out);
|
|
1127
|
+
};
|
|
1128
|
+
const jointLp = new Simplex(true);
|
|
1129
|
+
// (each pair's order atoms and its four rows, a ≤ b, b < a, b ≤ a, a < b, made once: the same rows every round, which
|
|
1130
|
+
// the simplex knows by their objects)
|
|
1131
|
+
const pairs0 = new Map();
|
|
1132
|
+
const pairOf = (a, b) => {
|
|
1133
|
+
const key = `${a}|${b}`;
|
|
1134
|
+
let p = pairs0.get(key);
|
|
1135
|
+
if (!p) {
|
|
1136
|
+
const T = (t) => (t.startsWith('k:') ? konst(Number(t.slice(2))) : tv(t));
|
|
1137
|
+
p = { ab: v(le(a, b)), ba: v(le(b, a)), rows: [plus(T(a), times(T(b), -1)), plus(plus(T(b), konst(1)), times(T(a), -1)), plus(T(b), times(T(a), -1)), plus(plus(T(a), konst(1)), times(T(b), -1))] };
|
|
1138
|
+
pairs0.set(key, p);
|
|
1139
|
+
}
|
|
1140
|
+
return p;
|
|
1141
|
+
};
|
|
1142
|
+
const theoryCheck = (m) => {
|
|
1143
|
+
const T = (t) => (t.startsWith('k:') ? konst(Number(t.slice(2))) : tv(t));
|
|
1144
|
+
const theoryRows = (row) => {
|
|
1145
|
+
for (const t of theory) {
|
|
1146
|
+
const holds = m[t.x] === true;
|
|
1147
|
+
if (t.op === '=') {
|
|
1148
|
+
if (holds) {
|
|
1149
|
+
row(t.d, [t.x]);
|
|
1150
|
+
row(times(t.d, -1), [t.x]);
|
|
1151
|
+
}
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1154
|
+
// a − b < 0 is a − b + 1 ≤ 0; its negation b − a ≤ 0; a − b ≤ 0's negation b − a + 1 ≤ 0
|
|
1155
|
+
if (holds)
|
|
1156
|
+
row(t.op === '<' ? plus(t.d, konst(1)) : t.d, [t.x]);
|
|
1157
|
+
else
|
|
1158
|
+
row(t.op === '<' ? times(t.d, -1) : plus(times(t.d, -1), konst(1)), [-t.x]);
|
|
1159
|
+
}
|
|
1160
|
+
};
|
|
1161
|
+
const lemmaOf = (r, lits) => {
|
|
1162
|
+
if (r.kind !== 'refuted')
|
|
1163
|
+
return undefined;
|
|
1164
|
+
const used = new Set();
|
|
1165
|
+
for (const s of r.steps)
|
|
1166
|
+
if (s.kind === 'input')
|
|
1167
|
+
for (const l of lits[s.index])
|
|
1168
|
+
used.add(l);
|
|
1169
|
+
return used.size ? [...used].map(l => -l) : undefined;
|
|
1170
|
+
};
|
|
1171
|
+
const pairRows = (row, a, b) => {
|
|
1172
|
+
const p = pairOf(a, b);
|
|
1173
|
+
if (m[p.ab] === true)
|
|
1174
|
+
row(p.rows[0], [p.ab]);
|
|
1175
|
+
else
|
|
1176
|
+
row(p.rows[1], [-p.ab]);
|
|
1177
|
+
if (m[p.ba] === true)
|
|
1178
|
+
row(p.rows[2], [p.ba]);
|
|
1179
|
+
else
|
|
1180
|
+
row(p.rows[3], [-p.ba]);
|
|
1181
|
+
};
|
|
1182
|
+
// (each comparison alone first, with its terms' order and the constants: a lemma of a few literals for each the
|
|
1183
|
+
// model breaks, all of them in one round; one the model reads as an earlier model did, feasible then, not again)
|
|
1184
|
+
{
|
|
1185
|
+
const ks = gos.filter(t => t.startsWith('k:')), lemmas = [];
|
|
1186
|
+
for (const [ti, t] of theory.entries()) {
|
|
1187
|
+
const local = localOf(ti, t, ks), seen = (feasible[ti] ??= new Set()), key = local.map(l => (m[l] === true ? '1' : '0')).join('');
|
|
1188
|
+
if (seen.has(key))
|
|
1189
|
+
continue;
|
|
1190
|
+
const rows = [], lits = [], row = (lin, ls) => { rows.push({ lin, label: '' }); lits.push(ls); };
|
|
1191
|
+
const xs = Object.keys(t.d).filter(x => x !== '1');
|
|
1192
|
+
for (const x of xs)
|
|
1193
|
+
row(times(tv(x), -1), []);
|
|
1194
|
+
for (let i2 = 0; i2 < xs.length; i2++) {
|
|
1195
|
+
for (let j = i2 + 1; j < xs.length; j++)
|
|
1196
|
+
pairRows(row, xs[i2], xs[j]);
|
|
1197
|
+
for (const k2 of ks)
|
|
1198
|
+
pairRows(row, xs[i2], k2);
|
|
1199
|
+
}
|
|
1200
|
+
const holds = m[t.x] === true;
|
|
1201
|
+
if (t.op === '=') {
|
|
1202
|
+
if (holds) {
|
|
1203
|
+
row(t.d, [t.x]);
|
|
1204
|
+
row(times(t.d, -1), [t.x]);
|
|
1205
|
+
}
|
|
1206
|
+
else
|
|
1207
|
+
continue;
|
|
1208
|
+
}
|
|
1209
|
+
else if (holds)
|
|
1210
|
+
row(t.op === '<' ? plus(t.d, konst(1)) : t.d, [t.x]);
|
|
1211
|
+
else
|
|
1212
|
+
row(t.op === '<' ? times(t.d, -1) : plus(times(t.d, -1), konst(1)), [-t.x]);
|
|
1213
|
+
const l = lemmaOf(solve(rows), lits);
|
|
1214
|
+
if (l)
|
|
1215
|
+
lemmas.push(l);
|
|
1216
|
+
else
|
|
1217
|
+
seen.add(key);
|
|
1218
|
+
}
|
|
1219
|
+
if (lemmas.length)
|
|
1220
|
+
return { lemmas };
|
|
1221
|
+
}
|
|
1222
|
+
{
|
|
1223
|
+
const rows = [], lits = [], row = (lin, ls) => { rows.push({ lin, label: '' }); lits.push(ls); };
|
|
1224
|
+
// (the pairs of terms one comparison reads, and each with the constants)
|
|
1225
|
+
const ks = gos.filter(t => t.startsWith('k:')), pairs2 = new Map();
|
|
1226
|
+
for (const t of theory) {
|
|
1227
|
+
const xs = Object.keys(t.d).filter(x => x !== '1');
|
|
1228
|
+
for (const a of xs)
|
|
1229
|
+
for (const b of [...xs, ...ks])
|
|
1230
|
+
if (a !== b)
|
|
1231
|
+
pairs2.set(a < b ? `${a}|${b}` : `${b}|${a}`, a < b ? [a, b] : [b, a]);
|
|
1232
|
+
}
|
|
1233
|
+
// (with few ordered terms, every pair of them: a refutation through a term no comparison reads, a chain of the
|
|
1234
|
+
// order, is then a lemma of the pairs it needs, not of the whole order)
|
|
1235
|
+
const all = gos.length <= 48 ? gos : [];
|
|
1236
|
+
for (let i2 = 0; i2 < all.length; i2++)
|
|
1237
|
+
for (let j = i2 + 1; j < all.length; j++) {
|
|
1238
|
+
const a = all[i2], b = all[j];
|
|
1239
|
+
if (!(a.startsWith('k:') && b.startsWith('k:')))
|
|
1240
|
+
pairs2.set(a < b ? `${a}|${b}` : `${b}|${a}`, a < b ? [a, b] : [b, a]);
|
|
1241
|
+
}
|
|
1242
|
+
for (const t of new Set([...theoryTerms, ...all.filter(x => !x.startsWith('k:'))]))
|
|
1243
|
+
row(times(tv(t), -1), []);
|
|
1244
|
+
for (const [a, b] of pairs2.values())
|
|
1245
|
+
pairRows(row, a, b);
|
|
1246
|
+
// (and the successors the model holds among them, n1 = n + 1, both ways: the ring's next node)
|
|
1247
|
+
const tt = new Set([...theoryTerms, ...all]);
|
|
1248
|
+
for (const x of succList)
|
|
1249
|
+
if (m[x.s] && (tt.has(x.lo) || tt.has(x.hi))) {
|
|
1250
|
+
row(plus(T(x.hi), times(plus(T(x.lo), konst(1)), -1)), [x.s]);
|
|
1251
|
+
row(plus(plus(T(x.lo), konst(1)), times(T(x.hi), -1)), [x.s]);
|
|
1252
|
+
for (const y of [x.lo, x.hi])
|
|
1253
|
+
if (!tt.has(y) && !y.startsWith('k:'))
|
|
1254
|
+
row(times(tv(y), -1), []);
|
|
1255
|
+
}
|
|
1256
|
+
theoryRows(row);
|
|
1257
|
+
// (several lemmas a round: each refutation's theory rows set aside, the rest solved again, at most eight)
|
|
1258
|
+
// (each by the simplex first (kernel/simplex.ts, the CDCL search's, its tableau kept from round to round): its
|
|
1259
|
+
// conflict's rows refuted exactly (core/solver.ts), the lemma the literals that refutation reads; no answer, the
|
|
1260
|
+
// first time by elimination over them all, as before; feasible, or a conflict the floats misjudge, no more lemmas)
|
|
1261
|
+
const lemmas = [], off = new Set(), theoryLits = new Set(theory.flatMap(t => [t.x, -t.x]));
|
|
1262
|
+
for (let k = 0; k < 8; k++) {
|
|
1263
|
+
const keep = rows.map((_, i) => i).filter(i => !off.has(i)), sub = keep.map(i => rows[i]), subLits = keep.map(i => lits[i]);
|
|
1264
|
+
let r = jointLp.load(sub);
|
|
1265
|
+
if (r.kind === 'sat')
|
|
1266
|
+
r = jointLp.check();
|
|
1267
|
+
if (r.kind === 'sat')
|
|
1268
|
+
break;
|
|
1269
|
+
const l = r.kind === 'conflict' ? lemmaOf(solve(r.rows.map(i => sub[i])), r.rows.map(i => subLits[i])) : k ? undefined : lemmaOf(solve(sub), subLits);
|
|
1270
|
+
if (!l)
|
|
1271
|
+
break;
|
|
1272
|
+
lemmas.push(l);
|
|
1273
|
+
keep.forEach(i => { if (lits[i].some(x => theoryLits.has(x) && l.includes(-x)))
|
|
1274
|
+
off.add(i); });
|
|
1275
|
+
}
|
|
1276
|
+
if (lemmas.length)
|
|
1277
|
+
return { lemmas };
|
|
1278
|
+
}
|
|
1279
|
+
const rows = [], lits = [], row = (lin, ls) => { rows.push({ lin, label: '' }); lits.push(ls); };
|
|
1280
|
+
const groups = groupsOf(m);
|
|
1281
|
+
for (const t of gos)
|
|
1282
|
+
if (!t.startsWith('k:'))
|
|
1283
|
+
row(times(tv(t), -1), []);
|
|
1284
|
+
for (const [gi, g] of groups.entries()) {
|
|
1285
|
+
for (const t of g.slice(1)) {
|
|
1286
|
+
row(plus(T(t), times(T(g[0]), -1)), [v(le(t, g[0]))]);
|
|
1287
|
+
row(plus(T(g[0]), times(T(t), -1)), [v(le(g[0], t))]);
|
|
1288
|
+
}
|
|
1289
|
+
const h = groups[gi + 1];
|
|
1290
|
+
if (h)
|
|
1291
|
+
row(plus(plus(T(g[0]), konst(1)), times(T(h[0]), -1)), [-v(le(h[0], g[0]))]);
|
|
1292
|
+
}
|
|
1293
|
+
for (const x of succList)
|
|
1294
|
+
if (m[x.s])
|
|
1295
|
+
row(plus(T(x.hi), times(plus(T(x.lo), konst(1)), -1)), [x.s]);
|
|
1296
|
+
theoryRows(row);
|
|
1297
|
+
// (the simplex first, as the joint check: a conflict's rows refuted exactly; its point the values where every term is
|
|
1298
|
+
// a natural there and every row holds exactly, with elimination's (as before) for a read-back they break; else
|
|
1299
|
+
// elimination over them all)
|
|
1300
|
+
let q = jointLp.load(rows);
|
|
1301
|
+
if (q.kind === 'sat')
|
|
1302
|
+
q = jointLp.check();
|
|
1303
|
+
if (q.kind === 'conflict') {
|
|
1304
|
+
const l = lemmaOf(solve(q.rows.map(i => rows[i])), q.rows.map(i => lits[i]));
|
|
1305
|
+
if (l)
|
|
1306
|
+
return { lemmas: [l] };
|
|
1307
|
+
}
|
|
1308
|
+
const exact = () => {
|
|
1309
|
+
const r = solve(rows);
|
|
1310
|
+
if (r.kind === 'sat')
|
|
1311
|
+
return { val: new Map(gos.map(t => [t, t.startsWith('k:') ? Number(t.slice(2)) : r.witness[t] ?? 0])) };
|
|
1312
|
+
const l = lemmaOf(r, lits);
|
|
1313
|
+
return l && { lemmas: [l] };
|
|
1314
|
+
};
|
|
1315
|
+
if (q.kind === 'sat') {
|
|
1316
|
+
const at = (t) => { const j = jointLp.indexOf(t); return j === undefined ? 0 : jointLp.value(j); };
|
|
1317
|
+
const val = new Map(gos.map(t => [t, t.startsWith('k:') ? Number(t.slice(2)) : Math.round(at(t))]));
|
|
1318
|
+
const ok = gos.every(t => t.startsWith('k:') || (Math.abs(at(t) - val.get(t)) < 1e-6 && val.get(t) >= 0))
|
|
1319
|
+
&& rows.every(row => Object.entries(row.lin).reduce((x, [k, c]) => x + c * (k === '1' ? 1 : val.get(k) ?? 0), 0) <= 0);
|
|
1320
|
+
if (ok)
|
|
1321
|
+
return { val, exact };
|
|
1322
|
+
}
|
|
1323
|
+
return exact();
|
|
1324
|
+
};
|
|
1325
|
+
const outcome = (o) => (typeof o === 'string' ? { unknown: o } : o === undefined ? { unknown: 'the conflict budget ran out, or the deadline' }
|
|
1326
|
+
: !o.sat ? { sat: false, core: [...new Set([...o.core].flatMap(g => (index.has(g) ? [index.get(g)] : [])))].sort((a, b) => a - b) } : { model: o.model });
|
|
1327
|
+
// the goal's own terms, for the read-back: its finite variables, cells (of its maps' patterns, and at keys of the
|
|
1328
|
+
// wrong sort), ordered cells and functions' parts
|
|
1329
|
+
const gFinsL = finsList.filter(f => gFins.has(f)), cellsOf = new Map(), odd = [];
|
|
1330
|
+
for (const f of gFinsL)
|
|
1331
|
+
if (f.parts) {
|
|
1332
|
+
if (!cellsOf.has(f.map))
|
|
1333
|
+
cellsOf.set(f.map, []);
|
|
1334
|
+
cellsOf.get(f.map).push({ id: f.id, parts: f.parts });
|
|
1335
|
+
}
|
|
1336
|
+
else if (f.key)
|
|
1337
|
+
odd.push({ id: f.id, map: f.map, key: f.key });
|
|
1338
|
+
const ordCells = new Map(), ordOdd = [];
|
|
1339
|
+
for (const id of gos) {
|
|
1340
|
+
const inf = ordInfo.get(id);
|
|
1341
|
+
if (!inf)
|
|
1342
|
+
continue;
|
|
1343
|
+
if (inf.parts) {
|
|
1344
|
+
if (!ordCells.has(inf.map))
|
|
1345
|
+
ordCells.set(inf.map, []);
|
|
1346
|
+
ordCells.get(inf.map).push({ id, parts: inf.parts });
|
|
1347
|
+
}
|
|
1348
|
+
else
|
|
1349
|
+
ordOdd.push({ id, map: inf.map, key: inf.key });
|
|
1350
|
+
}
|
|
1351
|
+
const reads = gst.reads, fnParts = gst.fnParts;
|
|
1352
|
+
// --- a model, read back: the ordered terms (the parts among them) by the model's order, each composed key the radix
|
|
1353
|
+
// sum of its parts, each cell and scalar its value; then judged on every hypothesis
|
|
1354
|
+
// (the read-back with the theory's values, where it checked the model; where they break a hypothesis, the order's own
|
|
1355
|
+
// grouping, as without one)
|
|
1356
|
+
const finish = (model, theoryVal) => {
|
|
1357
|
+
const val = (l) => model[l] === true;
|
|
1358
|
+
const w = {};
|
|
1359
|
+
for (const f of gFinsL) {
|
|
1360
|
+
let x = 0;
|
|
1361
|
+
for (let val2 = 0; val2 <= f.cap; val2++)
|
|
1362
|
+
if (val(v(finAt(f, val2))))
|
|
1363
|
+
x = val2;
|
|
1364
|
+
if (f.id.startsWith('s:'))
|
|
1365
|
+
w[f.id.slice(2)] = x;
|
|
1366
|
+
}
|
|
1367
|
+
// each group of equal terms the next value, a constant's group its own; odd where there is room before the next constant
|
|
1368
|
+
// (a key of the wrong sort, 2·64·x + y, then meets no cell 64·x' + y), and far apart past every constant (composed keys
|
|
1369
|
+
// of parts above every bound then don't meet)
|
|
1370
|
+
// (where the theory checked the model, its values)
|
|
1371
|
+
const ordVal = theoryVal ?? new Map();
|
|
1372
|
+
if (!theoryVal) {
|
|
1373
|
+
const groups = groupsOf(model);
|
|
1374
|
+
const constOf = (g) => { const cs = [...new Set(g.filter(x => x.startsWith('k:')).map(x => Number(x.slice(2))))]; return cs.length > 1 ? NaN : cs[0]; };
|
|
1375
|
+
// (a successor the model holds, n1 = n + 1, makes its two groups consecutive values: such a chain is one block, its
|
|
1376
|
+
// first group's value the next free one, or its constant's less the steps to it)
|
|
1377
|
+
const groupOf = new Map();
|
|
1378
|
+
groups.forEach((g, i) => g.forEach(t => groupOf.set(t, i)));
|
|
1379
|
+
const tight = new Set(succList.filter(x => val(x.s)).map(x => `${groupOf.get(x.lo)}>${groupOf.get(x.hi)}`));
|
|
1380
|
+
const tied = (i) => tight.has(`${i}>${i + 1}`);
|
|
1381
|
+
let prev = -1, far = 0;
|
|
1382
|
+
for (const [gi, g] of groups.entries()) {
|
|
1383
|
+
const c = constOf(g);
|
|
1384
|
+
if (Number.isNaN(c))
|
|
1385
|
+
return { unknown: 'the model\'s order equates two constants' };
|
|
1386
|
+
let next = Infinity;
|
|
1387
|
+
for (const h of groups.slice(gi + 1)) {
|
|
1388
|
+
const d = constOf(h);
|
|
1389
|
+
if (d !== undefined) {
|
|
1390
|
+
next = d;
|
|
1391
|
+
break;
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
let x = c ?? (next === Infinity && prev > 0 ? (1 << 24) * ++far + 997 * far + prev + 13 : prev + 1);
|
|
1395
|
+
if (c === undefined && next !== Infinity && x % 2 === 0 && x + 1 < next)
|
|
1396
|
+
x++;
|
|
1397
|
+
if (c === undefined && gi > 0 && tied(gi - 1))
|
|
1398
|
+
x = prev + 1;
|
|
1399
|
+
else if (c === undefined) {
|
|
1400
|
+
let k = gi;
|
|
1401
|
+
while (tied(k) && constOf(groups[k + 1]) === undefined)
|
|
1402
|
+
k++;
|
|
1403
|
+
const d = tied(k) ? constOf(groups[k + 1]) : undefined;
|
|
1404
|
+
if (d !== undefined)
|
|
1405
|
+
x = d - (k + 1 - gi);
|
|
1406
|
+
}
|
|
1407
|
+
if (x <= prev || x >= next || (gi > 0 && tied(gi - 1) && x !== prev + 1))
|
|
1408
|
+
return { unknown: noRoom };
|
|
1409
|
+
for (const t of g)
|
|
1410
|
+
ordVal.set(t, x);
|
|
1411
|
+
prev = x;
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
for (const [t, x] of ordVal)
|
|
1415
|
+
if (t.startsWith('p:') || t.startsWith('s:'))
|
|
1416
|
+
w[t.slice(2)] = x;
|
|
1417
|
+
const maps = new Map();
|
|
1418
|
+
const cellVal = new Map(), cells2 = new Map([...cellsOf].map(([mp, cs]) => [mp, [...cs]])), odd2 = [...odd];
|
|
1419
|
+
for (const f of gFinsL)
|
|
1420
|
+
if (f.id.startsWith('c:')) {
|
|
1421
|
+
let x = 0;
|
|
1422
|
+
for (let val2 = 0; val2 <= f.cap; val2++)
|
|
1423
|
+
if (val(v(finAt(f, val2))))
|
|
1424
|
+
x = val2;
|
|
1425
|
+
cellVal.set(f.id, x);
|
|
1426
|
+
}
|
|
1427
|
+
for (const [mp, cs] of ordCells)
|
|
1428
|
+
for (const c of cs) {
|
|
1429
|
+
cellVal.set(c.id, ordVal.get(c.id) ?? 0);
|
|
1430
|
+
if (!cells2.has(mp))
|
|
1431
|
+
cells2.set(mp, []);
|
|
1432
|
+
cells2.get(mp).push(c);
|
|
1433
|
+
}
|
|
1434
|
+
for (const o of ordOdd) {
|
|
1435
|
+
cellVal.set(o.id, ordVal.get(o.id) ?? 0);
|
|
1436
|
+
odd2.push(o);
|
|
1437
|
+
}
|
|
1438
|
+
// every read of the goal, at its key's value under w: the abstraction's value where it has one, else 0
|
|
1439
|
+
const keyVal = (e) => { const ts = [], d = { c: 0 }; if (!linTerms(e, 1, ts, d))
|
|
1440
|
+
return undefined; let s = d.c; for (const [x, c] of ts) {
|
|
1441
|
+
if (w[x] === undefined)
|
|
1442
|
+
return undefined;
|
|
1443
|
+
s += c * w[x];
|
|
1444
|
+
} return s; };
|
|
1445
|
+
// (each function's table at its parts' values)
|
|
1446
|
+
for (const { f, n, a } of fnParts) {
|
|
1447
|
+
if (!maps.has(f))
|
|
1448
|
+
maps.set(f, new Map());
|
|
1449
|
+
maps.get(f).set(w[a], w[n]);
|
|
1450
|
+
}
|
|
1451
|
+
// (and at a key of the wrong sort, a value of its own past every value the model gives: an injective function's
|
|
1452
|
+
// instance there holds, and a key it is a part of meets no cell of the right sort)
|
|
1453
|
+
let fresh2 = Math.max(0, ...Object.values(w), ...[...maps.values()].flatMap(t => [...t.values()]));
|
|
1454
|
+
// (a key that reads such a read, idn(64·idn(64·m + n) + n): its value from the tables so far, inner reads first)
|
|
1455
|
+
const valueAt = (e) => {
|
|
1456
|
+
if (e.op === 'const')
|
|
1457
|
+
return e.value;
|
|
1458
|
+
if (e.op === 'var')
|
|
1459
|
+
return w[e.name] ?? 0;
|
|
1460
|
+
if (e.op === 'mul') {
|
|
1461
|
+
const a = valueAt(e.a);
|
|
1462
|
+
return a === undefined ? undefined : a * e.k;
|
|
1463
|
+
}
|
|
1464
|
+
if (e.op === 'add') {
|
|
1465
|
+
const a = valueAt(e.a), b = valueAt(e.b);
|
|
1466
|
+
return a === undefined || b === undefined ? undefined : a + b;
|
|
1467
|
+
}
|
|
1468
|
+
if (e.op === 'get' && fnMaps.has(e.map)) {
|
|
1469
|
+
const k = valueAt(e.key);
|
|
1470
|
+
return k === undefined ? undefined : maps.get(e.map)?.get(k);
|
|
1471
|
+
}
|
|
1472
|
+
return undefined;
|
|
1473
|
+
};
|
|
1474
|
+
// (the reads gathered, the inner ones first: each read is gathered before the reads in its key)
|
|
1475
|
+
const fnOdd = (x) => {
|
|
1476
|
+
tick();
|
|
1477
|
+
if (!fnMaps.has(x.map) || partName(x) !== undefined)
|
|
1478
|
+
return;
|
|
1479
|
+
const key = keyVal(x.key) ?? valueAt(x.key), t = maps.get(x.map) ?? new Map();
|
|
1480
|
+
if (key === undefined || t.has(key))
|
|
1481
|
+
return;
|
|
1482
|
+
maps.set(x.map, t.set(key, ordVal.get(`c:${x.map}#${textOfE(x.key)}`) ?? ++fresh2));
|
|
1483
|
+
};
|
|
1484
|
+
[...reads].reverse().forEach(fnOdd);
|
|
1485
|
+
for (const [mp, cs] of cells2)
|
|
1486
|
+
for (const c of cs) {
|
|
1487
|
+
const key = c.parts.reduce((s, p, i) => s + w[p] * (patterns[mp][i] ?? 1), 0);
|
|
1488
|
+
if (!maps.has(mp))
|
|
1489
|
+
maps.set(mp, new Map());
|
|
1490
|
+
maps.get(mp).set(key, cellVal.get(c.id));
|
|
1491
|
+
}
|
|
1492
|
+
// (a cell at a key of the wrong sort, where its key falls, unless a cell of the right sort is there: one cell, whose
|
|
1493
|
+
// value the hypotheses are judged with below)
|
|
1494
|
+
for (const o of odd2) {
|
|
1495
|
+
const key = keyVal(o.key) ?? valueAt(o.key);
|
|
1496
|
+
if (key === undefined)
|
|
1497
|
+
continue;
|
|
1498
|
+
if (!maps.has(o.map))
|
|
1499
|
+
maps.set(o.map, new Map());
|
|
1500
|
+
if (!maps.get(o.map).has(key))
|
|
1501
|
+
maps.get(o.map).set(key, cellVal.get(o.id));
|
|
1502
|
+
}
|
|
1503
|
+
const allMaps = new Set();
|
|
1504
|
+
for (const x of reads)
|
|
1505
|
+
allMaps.add(x.map);
|
|
1506
|
+
const st2 = { scalars: {}, maps: Object.fromEntries([...allMaps].map(mp => [mp, maps.get(mp) ?? new Map()])) };
|
|
1507
|
+
const m = { name: goal.name, params: [], inputs: [], state: {}, step: {}, maps: Object.fromEntries([...allMaps].map(mp => [mp, 0])) };
|
|
1508
|
+
const env = new Proxy({ ...w }, { get: (t, q) => t[q] ?? 0 });
|
|
1509
|
+
for (const [i, f] of fs.entries()) {
|
|
1510
|
+
tick();
|
|
1511
|
+
try {
|
|
1512
|
+
if (f.h && !evalC(f.h, env, st2, m)) {
|
|
1513
|
+
return { unknown: `the abstraction's model breaks hypothesis ${i}` };
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
catch {
|
|
1517
|
+
return { unknown: 'the model could not be read' };
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
// the kernel's witness: names, and each cell of the goal by its key's linear form
|
|
1521
|
+
const witness = { ...w };
|
|
1522
|
+
for (const x of reads) {
|
|
1523
|
+
tick();
|
|
1524
|
+
const l = linear(x.key, new Map()), kv = keyVal(x.key);
|
|
1525
|
+
if (l && kv !== undefined)
|
|
1526
|
+
witness[`${x.map}[${canon(l)}]`] = st2.maps[x.map]?.get(kv) ?? 0;
|
|
1527
|
+
}
|
|
1528
|
+
// the structure
|
|
1529
|
+
const values = [...new Set([...[...parts].map(x => w[x]), ...[...ordVal].filter(([t]) => !t.startsWith('k:') && !t.includes('#')).map(([, x]) => x)].filter((x) => x !== undefined))].sort((a, b) => a - b), elem = new Map(values.map((x, i) => [x, i]));
|
|
1530
|
+
const diagram = { elements: values.length, parts: {}, scalars: {}, cells: {}, fns: {} };
|
|
1531
|
+
for (const x of [...parts, ...ordScalars].sort())
|
|
1532
|
+
if (w[x] !== undefined)
|
|
1533
|
+
diagram.parts[x] = elem.get(w[x]);
|
|
1534
|
+
for (const f of gFinsL)
|
|
1535
|
+
if (f.id.startsWith('s:'))
|
|
1536
|
+
diagram.scalars[f.id.slice(2)] = w[f.id.slice(2)] ?? 0;
|
|
1537
|
+
for (const [mp, cs] of cells2) {
|
|
1538
|
+
const seen2 = new Set(), out = [];
|
|
1539
|
+
for (const c of cs) {
|
|
1540
|
+
const at = c.parts.map(x => elem.get(w[x])), id = at.join(','), value = cellVal.get(c.id);
|
|
1541
|
+
if (!seen2.has(id)) {
|
|
1542
|
+
seen2.add(id);
|
|
1543
|
+
out.push({ at, value: ordMaps.has(mp) ? elem.get(value) ?? value : value });
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
diagram.cells[mp] = out.sort((a, b) => { for (let i = 0; i < a.at.length; i++)
|
|
1547
|
+
if (a.at[i] !== b.at[i])
|
|
1548
|
+
return a.at[i] - b.at[i]; return 0; });
|
|
1549
|
+
}
|
|
1550
|
+
for (const { f, n, a } of fnParts) {
|
|
1551
|
+
const t = (diagram.fns[f] ??= []), x = elem.get(w[a]);
|
|
1552
|
+
if (!t.some(r => r[0] === x))
|
|
1553
|
+
t.push([x, elem.get(w[n])]);
|
|
1554
|
+
}
|
|
1555
|
+
for (const t of Object.values(diagram.fns))
|
|
1556
|
+
t.sort((a, b) => a[0] - b[0]);
|
|
1557
|
+
return { sat: true, witness, diagram };
|
|
1558
|
+
};
|
|
1559
|
+
// (the order's grouping first, as without a theory; where it breaks a hypothesis, the theory's rounds: lemmas the model
|
|
1560
|
+
// breaks, the search again, until the theory checks a model, its values the read-back's)
|
|
1561
|
+
let cur = outcome(search());
|
|
1562
|
+
if (!('model' in cur))
|
|
1563
|
+
return cur;
|
|
1564
|
+
let done = finish(cur.model, undefined);
|
|
1565
|
+
for (let round = 0; theory.length && 'unknown' in done && round < 256 && !opts.late?.(); round++) {
|
|
1566
|
+
let r = theoryCheck(cur.model);
|
|
1567
|
+
if (!r)
|
|
1568
|
+
break;
|
|
1569
|
+
if ('val' in r) {
|
|
1570
|
+
done = finish(cur.model, r.val);
|
|
1571
|
+
// (the simplex's values breaking it: elimination's)
|
|
1572
|
+
if ('unknown' in done && r.exact) {
|
|
1573
|
+
r = r.exact();
|
|
1574
|
+
if (r && 'val' in r)
|
|
1575
|
+
done = finish(cur.model, r.val);
|
|
1576
|
+
}
|
|
1577
|
+
if (r && !('val' in r)) {
|
|
1578
|
+
for (const l of r.lemmas)
|
|
1579
|
+
lemma(l);
|
|
1580
|
+
cur = outcome(search());
|
|
1581
|
+
if (!('model' in cur))
|
|
1582
|
+
return cur;
|
|
1583
|
+
done = finish(cur.model, undefined);
|
|
1584
|
+
continue;
|
|
1585
|
+
}
|
|
1586
|
+
if ('unknown' in done)
|
|
1587
|
+
done = finish(cur.model, undefined);
|
|
1588
|
+
break;
|
|
1589
|
+
}
|
|
1590
|
+
for (const l of r.lemmas) {
|
|
1591
|
+
lemma(l);
|
|
1592
|
+
const named = l.map((x) => [names[Math.abs(x)], x > 0]), key = named.map(([k, b]) => (b ? '' : '¬') + k).sort().join('|');
|
|
1593
|
+
if (lemmaCache.size > 20_000)
|
|
1594
|
+
lemmaCache.clear();
|
|
1595
|
+
lemmaCache.set(key, named);
|
|
1596
|
+
lemmasIn.add(key);
|
|
1597
|
+
}
|
|
1598
|
+
cur = outcome(search());
|
|
1599
|
+
if (!('model' in cur))
|
|
1600
|
+
return cur;
|
|
1601
|
+
done = finish(cur.model, undefined);
|
|
1602
|
+
}
|
|
1603
|
+
return done;
|
|
1604
|
+
};
|
|
1605
|
+
// (an encoder left mid-way by the deadline, or past 512 ordered terms, is not kept)
|
|
1606
|
+
let over = false;
|
|
1607
|
+
return { decide: (goal, fs, gst, opts) => { over = true; const d = decideGoal(goal, fs, gst, opts); if (!d || !('unknown' in d) || !d.unknown.endsWith('ordered terms'))
|
|
1608
|
+
over = false; return d; }, size: () => (over ? Infinity : hyps.size) };
|
|
1609
|
+
}
|
|
1610
|
+
function cdclSolver(trueFirst, keepPhase) {
|
|
1611
|
+
const idx = new Map(), back = [0];
|
|
1612
|
+
let cap = 0, val = new Int8Array(0), level = new Int32Array(0), act = new Float64Array(0), phase = new Int8Array(0), at = new Int32Array(0);
|
|
1613
|
+
const reason = [undefined];
|
|
1614
|
+
const heap = [];
|
|
1615
|
+
const above = (a, b) => act[a] > act[b] || (act[a] === act[b] && a < b);
|
|
1616
|
+
const up = (i) => { const x = heap[i]; while (i > 0) {
|
|
1617
|
+
const pi = (i - 1) >> 1, y = heap[pi];
|
|
1618
|
+
if (!above(x, y))
|
|
1619
|
+
break;
|
|
1620
|
+
heap[i] = y;
|
|
1621
|
+
at[y] = i;
|
|
1622
|
+
i = pi;
|
|
1623
|
+
} heap[i] = x; at[x] = i; };
|
|
1624
|
+
const down = (i) => { const x = heap[i]; for (;;) {
|
|
1625
|
+
let c = 2 * i + 1;
|
|
1626
|
+
if (c >= heap.length)
|
|
1627
|
+
break;
|
|
1628
|
+
if (c + 1 < heap.length && above(heap[c + 1], heap[c]))
|
|
1629
|
+
c++;
|
|
1630
|
+
if (!above(heap[c], x))
|
|
1631
|
+
break;
|
|
1632
|
+
heap[i] = heap[c];
|
|
1633
|
+
at[heap[i]] = i;
|
|
1634
|
+
i = c;
|
|
1635
|
+
} heap[i] = x; at[x] = i; };
|
|
1636
|
+
const push = (x) => { if (at[x] >= 0)
|
|
1637
|
+
return; heap.push(x); at[x] = heap.length - 1; up(heap.length - 1); };
|
|
1638
|
+
const pop = () => { const x = heap[0], y = heap.pop(); at[x] = -1; if (heap.length) {
|
|
1639
|
+
heap[0] = y;
|
|
1640
|
+
at[y] = 0;
|
|
1641
|
+
down(0);
|
|
1642
|
+
} return x; };
|
|
1643
|
+
const vid = (l) => {
|
|
1644
|
+
const a = Math.abs(l);
|
|
1645
|
+
let i = idx.get(a);
|
|
1646
|
+
if (i === undefined) {
|
|
1647
|
+
i = back.length;
|
|
1648
|
+
idx.set(a, i);
|
|
1649
|
+
back.push(a);
|
|
1650
|
+
reason.push(undefined);
|
|
1651
|
+
if (i >= cap) {
|
|
1652
|
+
const n = Math.max(1024, 2 * cap), grow = (x, mk) => { const y = mk(n); y.set(x); return y; };
|
|
1653
|
+
val = grow(val, k => new Int8Array(k));
|
|
1654
|
+
level = grow(level, k => new Int32Array(k));
|
|
1655
|
+
act = grow(act, k => new Float64Array(k));
|
|
1656
|
+
phase = grow(phase, k => new Int8Array(k));
|
|
1657
|
+
const at2 = new Int32Array(n).fill(-1);
|
|
1658
|
+
at2.set(at);
|
|
1659
|
+
at = at2;
|
|
1660
|
+
cap = n;
|
|
1661
|
+
}
|
|
1662
|
+
push(i);
|
|
1663
|
+
}
|
|
1664
|
+
return l > 0 ? i : -i;
|
|
1665
|
+
};
|
|
1666
|
+
const watch = new Map();
|
|
1667
|
+
const wl = (l) => { let w = watch.get(l); if (!w)
|
|
1668
|
+
watch.set(l, (w = [])); return w; };
|
|
1669
|
+
const valOf = (l) => (val[Math.abs(l)] === 0 ? 0 : (val[Math.abs(l)] > 0) === (l > 0) ? 1 : -1);
|
|
1670
|
+
const trail = [], lim = [];
|
|
1671
|
+
const assign = (l, r) => { val[Math.abs(l)] = l > 0 ? 1 : -1; level[Math.abs(l)] = lim.length; reason[Math.abs(l)] = r; trail.push(l); };
|
|
1672
|
+
let qhead = 0, fixed, seen = new Uint8Array(0);
|
|
1673
|
+
// (the origins of the reasons behind literals, through every level: a refutation's core)
|
|
1674
|
+
const coreOf = (lits, out) => {
|
|
1675
|
+
const seen = new Set(), todo = lits.map(Math.abs);
|
|
1676
|
+
while (todo.length) {
|
|
1677
|
+
const x = todo.pop();
|
|
1678
|
+
if (seen.has(x))
|
|
1679
|
+
continue;
|
|
1680
|
+
seen.add(x);
|
|
1681
|
+
const r = reason[x];
|
|
1682
|
+
if (r) {
|
|
1683
|
+
for (const o of r.from)
|
|
1684
|
+
out.add(o);
|
|
1685
|
+
for (const l of r.lits)
|
|
1686
|
+
todo.push(Math.abs(l));
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
return out;
|
|
1690
|
+
};
|
|
1691
|
+
const propagate = () => {
|
|
1692
|
+
while (qhead < trail.length) {
|
|
1693
|
+
const l = trail[qhead++], ws = wl(l);
|
|
1694
|
+
eprMeter.n++;
|
|
1695
|
+
for (let i = 0; i < ws.length; i++) {
|
|
1696
|
+
const c = ws[i];
|
|
1697
|
+
if (c.lits[0] === -l) {
|
|
1698
|
+
c.lits[0] = c.lits[1];
|
|
1699
|
+
c.lits[1] = -l;
|
|
1700
|
+
}
|
|
1701
|
+
if (valOf(c.lits[0]) > 0)
|
|
1702
|
+
continue;
|
|
1703
|
+
let moved = false;
|
|
1704
|
+
for (let k = 2; k < c.lits.length; k++)
|
|
1705
|
+
if (valOf(c.lits[k]) >= 0) {
|
|
1706
|
+
const t = c.lits[1];
|
|
1707
|
+
c.lits[1] = c.lits[k];
|
|
1708
|
+
c.lits[k] = t;
|
|
1709
|
+
wl(-c.lits[1]).push(c);
|
|
1710
|
+
ws.splice(i--, 1);
|
|
1711
|
+
moved = true;
|
|
1712
|
+
break;
|
|
1713
|
+
}
|
|
1714
|
+
if (moved)
|
|
1715
|
+
continue;
|
|
1716
|
+
if (valOf(c.lits[0]) < 0) {
|
|
1717
|
+
qhead = trail.length;
|
|
1718
|
+
return c;
|
|
1719
|
+
}
|
|
1720
|
+
assign(c.lits[0], c);
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
return undefined;
|
|
1724
|
+
};
|
|
1725
|
+
let inc = 1;
|
|
1726
|
+
const backjump = (bt) => { while (lim.length > bt) {
|
|
1727
|
+
const to = lim.pop();
|
|
1728
|
+
while (trail.length > to) {
|
|
1729
|
+
const x = Math.abs(trail.pop());
|
|
1730
|
+
val[x] = 0;
|
|
1731
|
+
reason[x] = undefined;
|
|
1732
|
+
push(x);
|
|
1733
|
+
}
|
|
1734
|
+
} qhead = Math.min(qhead, trail.length); };
|
|
1735
|
+
const add = (c) => {
|
|
1736
|
+
if (fixed)
|
|
1737
|
+
return;
|
|
1738
|
+
backjump(0);
|
|
1739
|
+
const cl = { lits: c.lits.map(vid), from: new Set([c.from]) };
|
|
1740
|
+
if (cl.lits.some(l => valOf(l) > 0))
|
|
1741
|
+
return;
|
|
1742
|
+
const open = cl.lits.filter(l => valOf(l) === 0);
|
|
1743
|
+
if (!open.length) {
|
|
1744
|
+
fixed = coreOf(cl.lits, new Set(cl.from));
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
cl.lits = [...open, ...cl.lits.filter(l => valOf(l) < 0)];
|
|
1748
|
+
if (open.length === 1)
|
|
1749
|
+
assign(open[0], cl);
|
|
1750
|
+
else {
|
|
1751
|
+
wl(-cl.lits[0]).push(cl);
|
|
1752
|
+
wl(-cl.lits[1]).push(cl);
|
|
1753
|
+
}
|
|
1754
|
+
};
|
|
1755
|
+
const solve = (budget, late, assume) => {
|
|
1756
|
+
if (fixed)
|
|
1757
|
+
return { sat: false, core: fixed };
|
|
1758
|
+
backjump(0);
|
|
1759
|
+
const as = assume.map(vid);
|
|
1760
|
+
let conflicts = 0;
|
|
1761
|
+
for (;;) {
|
|
1762
|
+
const confl = propagate();
|
|
1763
|
+
if (confl) {
|
|
1764
|
+
if (++conflicts > budget || (conflicts % 256 === 0 && late?.()))
|
|
1765
|
+
return undefined;
|
|
1766
|
+
if (!lim.length)
|
|
1767
|
+
return { sat: false, core: (fixed = coreOf(confl.lits, new Set(confl.from))) };
|
|
1768
|
+
// first UIP
|
|
1769
|
+
if (seen.length < back.length)
|
|
1770
|
+
seen = new Uint8Array(cap);
|
|
1771
|
+
const learnt = [], from = new Set(confl.from), marked = [];
|
|
1772
|
+
let p = 0, cnt = 0, c = confl, ti = trail.length - 1;
|
|
1773
|
+
do {
|
|
1774
|
+
for (const q of c.lits) {
|
|
1775
|
+
if (q === p)
|
|
1776
|
+
continue;
|
|
1777
|
+
const x = Math.abs(q);
|
|
1778
|
+
if (!seen[x] && level[x] > 0) {
|
|
1779
|
+
seen[x] = 1;
|
|
1780
|
+
marked.push(x);
|
|
1781
|
+
act[x] = act[x] + inc;
|
|
1782
|
+
if (at[x] >= 0)
|
|
1783
|
+
up(at[x]);
|
|
1784
|
+
if (level[x] === lim.length)
|
|
1785
|
+
cnt++;
|
|
1786
|
+
else
|
|
1787
|
+
learnt.push(q);
|
|
1788
|
+
}
|
|
1789
|
+
else if (!seen[x] && level[x] === 0 && reason[x]) {
|
|
1790
|
+
seen[x] = 1;
|
|
1791
|
+
marked.push(x);
|
|
1792
|
+
coreOf([q], from);
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
while (!seen[Math.abs(trail[ti])])
|
|
1796
|
+
ti--;
|
|
1797
|
+
p = trail[ti--];
|
|
1798
|
+
c = reason[Math.abs(p)];
|
|
1799
|
+
if (c)
|
|
1800
|
+
for (const o of c.from)
|
|
1801
|
+
from.add(o);
|
|
1802
|
+
cnt--;
|
|
1803
|
+
} while (cnt > 0);
|
|
1804
|
+
for (const x of marked)
|
|
1805
|
+
seen[x] = 0;
|
|
1806
|
+
learnt.unshift(-p);
|
|
1807
|
+
inc *= 1.05;
|
|
1808
|
+
let bt = 0, bi = 1;
|
|
1809
|
+
for (let i = 1; i < learnt.length; i++)
|
|
1810
|
+
if (level[Math.abs(learnt[i])] > bt) {
|
|
1811
|
+
bt = level[Math.abs(learnt[i])];
|
|
1812
|
+
bi = i;
|
|
1813
|
+
}
|
|
1814
|
+
if (learnt.length > 1) {
|
|
1815
|
+
const t = learnt[1];
|
|
1816
|
+
learnt[1] = learnt[bi];
|
|
1817
|
+
learnt[bi] = t;
|
|
1818
|
+
}
|
|
1819
|
+
backjump(bt);
|
|
1820
|
+
qhead = trail.length;
|
|
1821
|
+
const nc = { lits: learnt, from, learnt: true };
|
|
1822
|
+
if (learnt.length > 1) {
|
|
1823
|
+
wl(-learnt[0]).push(nc);
|
|
1824
|
+
wl(-learnt[1]).push(nc);
|
|
1825
|
+
}
|
|
1826
|
+
assign(learnt[0], nc);
|
|
1827
|
+
continue;
|
|
1828
|
+
}
|
|
1829
|
+
// the assumptions first, in order: one already false is the refutation
|
|
1830
|
+
if (lim.length < as.length) {
|
|
1831
|
+
const l = as[lim.length], s = valOf(l);
|
|
1832
|
+
if (s < 0)
|
|
1833
|
+
return { sat: false, core: coreOf([l], new Set()) };
|
|
1834
|
+
lim.push(trail.length);
|
|
1835
|
+
if (s === 0)
|
|
1836
|
+
assign(l);
|
|
1837
|
+
continue;
|
|
1838
|
+
}
|
|
1839
|
+
// decide: the most active unassigned variable, false first
|
|
1840
|
+
let best = 0;
|
|
1841
|
+
while (heap.length) {
|
|
1842
|
+
const x = pop();
|
|
1843
|
+
if (val[x] === 0) {
|
|
1844
|
+
best = x;
|
|
1845
|
+
break;
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
if (!best) {
|
|
1849
|
+
const model = [];
|
|
1850
|
+
for (let x = 1; x < back.length; x++) {
|
|
1851
|
+
model[back[x]] = val[x] > 0;
|
|
1852
|
+
if (keepPhase.has(back[x]))
|
|
1853
|
+
phase[x] = val[x];
|
|
1854
|
+
}
|
|
1855
|
+
return { sat: true, model };
|
|
1856
|
+
}
|
|
1857
|
+
lim.push(trail.length);
|
|
1858
|
+
assign(phase[best] ? phase[best] * best : trueFirst.has(back[best]) ? best : -best);
|
|
1859
|
+
}
|
|
1860
|
+
};
|
|
1861
|
+
// (another goal's search from its clauses alone: the learned clauses, activities and phases of the goals before it
|
|
1862
|
+
// dropped, so its refutation and its model are a search of its own, as with an encoder of its own; a goal asked again
|
|
1863
|
+
// with more instances keeps its own)
|
|
1864
|
+
const fresh = () => {
|
|
1865
|
+
if (fixed)
|
|
1866
|
+
return;
|
|
1867
|
+
backjump(0);
|
|
1868
|
+
for (const [l, ws] of watch) {
|
|
1869
|
+
const ks = ws.filter(c => !c.learnt);
|
|
1870
|
+
if (ks.length !== ws.length)
|
|
1871
|
+
watch.set(l, ks);
|
|
1872
|
+
}
|
|
1873
|
+
act.fill(0);
|
|
1874
|
+
phase.fill(0);
|
|
1875
|
+
inc = 1;
|
|
1876
|
+
heap.length = 0;
|
|
1877
|
+
at.fill(-1);
|
|
1878
|
+
for (let x = 1; x < back.length; x++)
|
|
1879
|
+
push(x);
|
|
1880
|
+
};
|
|
1881
|
+
return { solve, add, fresh };
|
|
1882
|
+
}
|