gdp-wired 0.21.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +16 -1
  2. package/dist/core/bmc.d.ts +38 -0
  3. package/dist/core/bmc.js +137 -0
  4. package/dist/core/check.d.ts +1 -1
  5. package/dist/core/check.js +19 -6
  6. package/dist/core/cycles.d.ts +15 -0
  7. package/dist/core/cycles.js +446 -8
  8. package/dist/core/cyclic.d.ts +6 -1
  9. package/dist/core/cyclic.js +13 -1
  10. package/dist/core/data.d.ts +17 -1
  11. package/dist/core/data.js +637 -97
  12. package/dist/core/dsl.d.ts +10 -1
  13. package/dist/core/dsl.js +47 -12
  14. package/dist/core/infer.js +518 -5
  15. package/dist/core/nonterm.d.ts +34 -0
  16. package/dist/core/nonterm.js +610 -0
  17. package/dist/core/q.d.ts +17 -0
  18. package/dist/core/q.js +52 -0
  19. package/dist/core/rationals.d.ts +112 -0
  20. package/dist/core/rationals.js +281 -0
  21. package/dist/core/relation.d.ts +28 -0
  22. package/dist/core/relation.js +480 -0
  23. package/dist/core/solver.d.ts +5 -2
  24. package/dist/core/solver.js +65 -20
  25. package/dist/core/transfer.d.ts +3 -3
  26. package/dist/core/transfer.js +77 -42
  27. package/dist/kernel/cdcl.d.ts +43 -0
  28. package/dist/kernel/cdcl.js +564 -0
  29. package/dist/kernel/kernel.d.ts +91 -0
  30. package/dist/kernel/kernel.js +310 -0
  31. package/dist/kernel/prove.d.ts +20 -1
  32. package/dist/kernel/prove.js +119 -44
  33. package/dist/kernel/qprove.d.ts +18 -0
  34. package/dist/kernel/qprove.js +157 -0
  35. package/dist/kernel/reader.d.ts +79 -0
  36. package/dist/kernel/reader.js +539 -0
  37. package/dist/kernel/simplex.d.ts +36 -0
  38. package/dist/kernel/simplex.js +259 -0
  39. package/dist/lean-kernel/emit.d.ts +19 -1
  40. package/dist/lean-kernel/emit.js +71 -3
  41. package/dist/lean-kernel/library.js +2 -0
  42. package/dist/lean-kernel/qlean.d.ts +26 -0
  43. package/dist/lean-kernel/qlean.js +105 -0
  44. package/package.json +6 -2
@@ -0,0 +1,480 @@
1
+ import { solve } from "./solver.js";
2
+ class Refused extends Error {
3
+ }
4
+ const id = (t) => JSON.stringify(t);
5
+ const canon = (l) => id(Object.keys(l).sort().map(x => [x, l[x]]));
6
+ const plus = (a, b, f = 1) => { const o = { ...a }; for (const x in b) {
7
+ o[x] = (o[x] ?? 0) + f * b[x];
8
+ if (!o[x])
9
+ delete o[x];
10
+ } return o; };
11
+ const times = (a, f) => { const o = {}; if (f)
12
+ for (const x in a)
13
+ o[x] = a[x] * f; return o; };
14
+ const konst = (c) => (c ? { 1: c } : {});
15
+ const vars = (l) => Object.keys(l).filter(x => x !== '1');
16
+ const pinned = (l, pins) => {
17
+ let out;
18
+ for (const x in l)
19
+ if (x !== '1' && x in pins) {
20
+ out ??= { ...l };
21
+ delete out[x];
22
+ out['1'] = (out['1'] ?? 0) + l[x] * pins[x];
23
+ if (!out['1'])
24
+ delete out['1'];
25
+ }
26
+ return out ?? l;
27
+ };
28
+ // Every alternative of a term under a path (the path extended by each split), and the term's linear value there.
29
+ function alts(e, p, n) {
30
+ if (--n.left < 0)
31
+ throw new Refused('too many paths');
32
+ switch (e.op) {
33
+ case 'var': return [[p, e.name in p.pins ? konst(p.pins[e.name]) : { [e.name]: 1 }]];
34
+ case 'const': return [[p, konst(e.value)]];
35
+ case 'add': return alts(e.a, p, n).flatMap(([q, a]) => alts(e.b, q, n).map(([r, b]) => [r, plus(a, b)]));
36
+ case 'mul': return alts(e.a, p, n).map(([q, a]) => [q, times(a, e.k)]);
37
+ case 'sub':
38
+ case 'min':
39
+ case 'max':
40
+ return alts(e.a, p, n).flatMap(([q, a]) => alts(e.b, q, n).flatMap(([r, b]) => {
41
+ const d = plus(a, b, -1), c = vars(d).length ? undefined : (d['1'] ?? 0);
42
+ // a ≥ b: a − b, a's, b's; below: 0, b's, a's
43
+ const ge = e.op === 'sub' ? d : e.op === 'max' ? a : b, lt = e.op === 'sub' ? {} : e.op === 'max' ? b : a;
44
+ if (c !== undefined)
45
+ return [[r, c >= 0 ? ge : lt]];
46
+ return [...row(r, times(d, -1)).map((s) => [s, ge]), ...row(r, plus(d, konst(1))).map((s) => [s, lt])];
47
+ }));
48
+ case 'prod': return alts(e.a, p, n).flatMap(([q, a]) => alts(e.b, q, n).map(([r, b]) => {
49
+ if (!vars(a).length)
50
+ return [r, times(b, a['1'] ?? 0)];
51
+ if (!vars(b).length)
52
+ return [r, times(a, b['1'] ?? 0)];
53
+ return [r, { [`π(${[id(a), id(b)].sort().join('·')})`]: 1 }];
54
+ }));
55
+ case 'if': return chain(e, p, n, alts);
56
+ case 'get': throw new Refused('a map');
57
+ }
58
+ }
59
+ // A row lin <= 0 added (none where the pins decide it: kept or dropped); an equation of one variable pins it.
60
+ function row(p, l0) {
61
+ const l = pinned(l0, p.pins), xs = vars(l);
62
+ if (!xs.length)
63
+ return (l['1'] ?? 0) <= 0 ? [p] : [];
64
+ return [{ rows: [...p.rows, l], pins: p.pins }];
65
+ }
66
+ function equal(p, d0) {
67
+ const d = pinned(d0, p.pins), xs = vars(d);
68
+ if (!xs.length)
69
+ return (d['1'] ?? 0) === 0 ? [p] : [];
70
+ if (xs.length === 1 && Math.abs(d[xs[0]]) === 1) {
71
+ const value = -(d['1'] ?? 0) * d[xs[0]];
72
+ return value >= 0 ? [{ rows: p.rows, pins: { ...p.pins, [xs[0]]: value } }] : [];
73
+ }
74
+ return [{ rows: [...p.rows, d, times(d, -1)], pins: p.pins }];
75
+ }
76
+ // The paths where c holds (positive) or fails, each extending p.
77
+ function assert(c, p, positive, n) {
78
+ switch (c.op) {
79
+ case '<':
80
+ case '<=':
81
+ case '=':
82
+ return alts(c.a, p, n).flatMap(([q, a]) => alts(c.b, q, n).flatMap(([r, b]) => {
83
+ const d = plus(a, b, -1); // a − b
84
+ if (c.op === '=')
85
+ return positive ? equal(r, d) : [...row(r, plus(d, konst(1))), ...row(r, plus(times(d, -1), konst(1)))];
86
+ const strict = (c.op === '<') === positive; // positive <: d + 1 <= 0; not <=: b < a, −d + 1 <= 0
87
+ return positive ? row(r, strict ? plus(d, konst(1)) : d) : row(r, strict ? plus(times(d, -1), konst(1)) : times(d, -1));
88
+ }));
89
+ case 'and':
90
+ case 'or':
91
+ // a conjunction holding, or a disjunction failing: each part in turn; else any part
92
+ if ((c.op === 'and') === positive)
93
+ return c.xs.reduce((ps, x) => ps.flatMap(q => assert(x, q, positive, n)), [p]);
94
+ {
95
+ // (a part the pins decide true is the path itself: the others' paths are within it)
96
+ const out = [];
97
+ for (const x of c.xs) {
98
+ const ys = assert(x, p, positive, n);
99
+ if (ys.includes(p))
100
+ return [p];
101
+ out.push(...ys);
102
+ }
103
+ return [...new Map(out.map(q => [id([q.rows.map(canon), Object.entries(q.pins).sort((a, b) => (a[0] < b[0] ? -1 : 1))]), q])).values()];
104
+ }
105
+ case 'not': return assert(c.x, p, !positive, n);
106
+ case 'flag':
107
+ case 'all': throw new Refused('a flag');
108
+ }
109
+ }
110
+ // The body's value of a state variable on a path: its ifs decided or split, a truncated difference at the top kept
111
+ // whole (an integer's part, if it is one).
112
+ // An if and the ifs of its else branches, one after another (a long chain of rules is no deeper a recursion).
113
+ function chain(e, p, n, leaf) {
114
+ const out = [];
115
+ let cur = e, ps = [p];
116
+ while (cur.op === 'if' && ps.length) {
117
+ const c = cur.c, then = cur.then;
118
+ for (const q of ps)
119
+ for (const r of assert(c, q, true, n))
120
+ out.push(...leaf(then, r, n));
121
+ ps = ps.flatMap(q => assert(c, q, false, n));
122
+ cur = cur.else;
123
+ }
124
+ for (const q of ps)
125
+ out.push(...leaf(cur, q, n));
126
+ return out;
127
+ }
128
+ function valueOf(e, p, n) {
129
+ if (e.op === 'if')
130
+ return chain(e, p, n, valueOf);
131
+ if (e.op === 'sub')
132
+ return alts(e.a, p, n).flatMap(([q, a]) => alts(e.b, q, n).map(([r, b]) => [r, { sub: [a, b] }]));
133
+ return alts(e, p, n).map(([q, l]) => [q, { lin: l }]);
134
+ }
135
+ // The relation of one turn, or undefined where it isn't linear in this sense (a map, a flag, past `cap` alternatives).
136
+ export function relationOf(l, laws = [], cap = 4000) {
137
+ try {
138
+ return build(l, laws, cap);
139
+ }
140
+ catch (e) {
141
+ if (e instanceof Refused || e instanceof RangeError)
142
+ return undefined;
143
+ throw e;
144
+ }
145
+ }
146
+ function build(l, laws, cap) {
147
+ const n = { left: cap * 500 }, state = Object.keys(l.state), prime = (x) => `${x}′`;
148
+ let ps = [{ rows: [], pins: {} }];
149
+ for (const c of [l.while, ...(l.assume ? [l.assume] : []), ...laws.map(x => x.holds)])
150
+ ps = ps.flatMap(p => assert(c, p, true, n));
151
+ let raws = ps.map(p => ({ p, vals: {} }));
152
+ for (const x of state) {
153
+ const e = l.body[x] ?? { op: 'var', name: x };
154
+ raws = raws.flatMap(r => valueOf(e, r.p, n).map(([p, v]) => ({ p, vals: { ...r.vals, [x]: v } })));
155
+ if (raws.length > cap)
156
+ throw new Refused('too many paths');
157
+ }
158
+ const isSub = (v) => 'sub' in v;
159
+ const diff = (v) => plus(v.sub[0], v.sub[1], -1);
160
+ // Candidate integers among the state: two variables whose values are complementary differences, or both linear, on
161
+ // every path (and complementary on one at least).
162
+ const pairs = new Map(); // first part → second
163
+ const taken = new Set();
164
+ for (const [i, x] of state.entries())
165
+ for (const y of state.slice(i + 1)) {
166
+ if (taken.has(x) || taken.has(y))
167
+ continue;
168
+ let seen = false, ok = true;
169
+ for (const r of raws) {
170
+ const a = r.vals[x], b = r.vals[y];
171
+ if (isSub(a) !== isSub(b)) {
172
+ ok = false;
173
+ break;
174
+ }
175
+ if (isSub(a) && isSub(b)) {
176
+ if (canon(diff(a)) !== canon(times(diff(b), -1))) {
177
+ ok = false;
178
+ break;
179
+ }
180
+ seen = true;
181
+ }
182
+ }
183
+ if (ok && seen) {
184
+ pairs.set(x, y);
185
+ taken.add(x);
186
+ taken.add(y);
187
+ }
188
+ }
189
+ // The other truncated differences split (as `alts` splits them); `while` after the turn over primed names, pinned
190
+ // where the value after is a constant.
191
+ const splitAll = (rs) => rs.flatMap(r => {
192
+ let out = [{ p: r.p, vals: {} }];
193
+ for (const x of state) {
194
+ const v = r.vals[x];
195
+ if (!isSub(v) || taken.has(x)) {
196
+ out = out.map(o => ({ p: o.p, vals: { ...o.vals, [x]: v } }));
197
+ continue;
198
+ }
199
+ const d = diff(v);
200
+ out = out.flatMap(o => [...row(o.p, times(d, -1)).map(p => ({ p, vals: { ...o.vals, [x]: { lin: d } } })), ...row(o.p, plus(d, konst(1))).map(p => ({ p, vals: { ...o.vals, [x]: { lin: {} } } }))]);
201
+ }
202
+ if (out.length > cap)
203
+ throw new Refused('too many paths');
204
+ return out;
205
+ });
206
+ raws = splitAll(raws).flatMap(r => {
207
+ const pins = { ...r.p.pins };
208
+ for (const x of state) {
209
+ const v = r.vals[x];
210
+ if (!isSub(v)) {
211
+ const c = pinned(v.lin, r.p.pins);
212
+ if (!vars(c).length)
213
+ pins[prime(x)] = c['1'] ?? 0;
214
+ }
215
+ }
216
+ return assert(renameC(l.while, new Set(state), prime), { rows: r.p.rows, pins }, true, n).map(p => ({ p, vals: r.vals }));
217
+ });
218
+ if (raws.length > cap)
219
+ throw new Refused('too many paths');
220
+ // Which naturals every form reads only as a difference: each name's coefficients over the forms, a pair's opposite.
221
+ // A candidate pair that fails it has its differences split, and the forms are read again.
222
+ let vec = new Map();
223
+ for (;;) {
224
+ const forms = raws.flatMap(r => [...r.p.rows, ...state.map(x => { const v = r.vals[x]; return pinned(isSub(v) ? diff(v) : v.lin, r.p.pins); })]);
225
+ const lists = new Map();
226
+ for (const [i, f] of forms.entries())
227
+ for (const x of vars(f)) {
228
+ const list = lists.get(x) ?? [];
229
+ list.push(`${i}:${f[x]}`);
230
+ lists.set(x, list);
231
+ }
232
+ vec = new Map([...lists].map(([x, list]) => [x, list.join(',')]));
233
+ const neg = (x) => (lists.get(x) ?? []).map(s => { const [i, c] = s.split(':'); return `${i}:${-Number(c)}`; }).join(',');
234
+ const bad = [...pairs].filter(([x, y]) => (vec.get(y) ?? '') !== neg(x) || (vec.get(prime(y)) ?? '') !== neg(prime(x)));
235
+ if (!bad.length) {
236
+ vec = new Map([...vec, ...[...lists.keys()].map(x => [`-${x}`, neg(x)])]);
237
+ break;
238
+ }
239
+ for (const [x, y] of bad) {
240
+ pairs.delete(x);
241
+ taken.delete(x);
242
+ taken.delete(y);
243
+ }
244
+ raws = splitAll(raws);
245
+ }
246
+ // The params' and the inputs' integers: two names with opposite coefficients in every form, neither the state's.
247
+ const ints = new Map([...pairs].map(([x, y]) => [x, [x, y]]));
248
+ const stateNames = new Set([...state, ...state.map(prime)]);
249
+ const others = [...vec.keys()].filter(x => !x.startsWith('-') && !stateNames.has(x)).sort();
250
+ for (const x of others) {
251
+ if (taken.has(x))
252
+ continue;
253
+ const y = others.find(z => z !== x && !taken.has(z) && vec.get(z) === vec.get(`-${x}`));
254
+ if (y) {
255
+ ints.set(x, [x, y]);
256
+ taken.add(x);
257
+ taken.add(y);
258
+ }
259
+ }
260
+ const partOf = new Map();
261
+ for (const [name, [a, b]] of ints) {
262
+ partOf.set(a, [name, 1]);
263
+ partOf.set(b, [name, -1]);
264
+ if (stateNames.has(a)) {
265
+ partOf.set(prime(a), [prime(name), 1]);
266
+ partOf.set(prime(b), [prime(name), -1]);
267
+ }
268
+ }
269
+ // a form over the naturals as one over the integers (each read once, at its first part)
270
+ const conv = (f) => {
271
+ const o = {};
272
+ for (const x in f) {
273
+ const q = partOf.get(x);
274
+ if (!q)
275
+ o[x] = f[x];
276
+ else if (q[1] > 0)
277
+ o[q[0]] = f[x];
278
+ }
279
+ return o;
280
+ };
281
+ const seconds = new Set([...ints.values()].map(q => q[1]));
282
+ const stateVars = state.filter(x => !seconds.has(x));
283
+ const paths = [];
284
+ for (const r of raws) {
285
+ const post = {}, val = (x) => { const v = r.vals[x]; return pinned(isSub(v) ? diff(v) : v.lin, r.p.pins); };
286
+ for (const x of stateVars)
287
+ post[x] = conv(ints.has(x) ? (isSub(r.vals[x]) ? val(x) : plus(val(x), val(ints.get(x)[1]), -1)) : val(x));
288
+ // the primed names in the rows read through the values after the turn
289
+ const through = (f) => {
290
+ let o = {};
291
+ for (const x in f)
292
+ o = x.endsWith('′') && stateVars.includes(x.slice(0, -1)) ? plus(o, times(post[x.slice(0, -1)], f[x])) : plus(o, { [x]: f[x] });
293
+ return o;
294
+ };
295
+ paths.push({ rows: r.p.rows.map(f => through(conv(f))), pins: r.p.pins, post });
296
+ }
297
+ const readOf = (p) => [...p.rows.flatMap(vars), ...Object.values(p.post).flatMap(vars)];
298
+ const nats = [...new Set([...paths.flatMap(readOf), ...stateVars])].filter(x => !ints.has(x)).sort();
299
+ // the location: a state natural every path pins, its value after the turn a constant
300
+ const loc = stateVars.find(x => !ints.has(x) && paths.length > 0 && paths.every(p => x in p.pins && !vars(p.post[x]).length));
301
+ const kept = [], from = [], to = [];
302
+ for (const p of paths) {
303
+ // a path no integer point satisfies is dropped
304
+ const rows = [...p.rows, ...nats.filter(x => p.rows.some(f => x in f)).map((x) => ({ [x]: -1 }))];
305
+ if (p.rows.length && solve(rows.map(lin => ({ lin, label: '' })), 4000).kind === 'refuted')
306
+ continue;
307
+ if (loc) {
308
+ from.push(p.pins[loc]);
309
+ to.push(p.post[loc]['1'] ?? 0);
310
+ delete p.post[loc];
311
+ }
312
+ kept.push(p);
313
+ }
314
+ const read = new Set(kept.flatMap(readOf));
315
+ const nameOf = (x) => partOf.get(x)?.[0] ?? x;
316
+ return { paths: kept, ints, nats: nats.filter(x => x !== loc), state: stateVars.filter(x => x !== loc), params: [...new Set(l.params.map(nameOf))].filter(x => read.has(x)).sort(),
317
+ ...(loc ? { loc } : {}), from, to, exact: ![...read].some(x => x.startsWith('π(')) };
318
+ }
319
+ const renameE = (e, names, f) => {
320
+ switch (e.op) {
321
+ case 'var': return names.has(e.name) ? { op: 'var', name: f(e.name) } : e;
322
+ case 'const': return e;
323
+ case 'add':
324
+ case 'sub':
325
+ case 'min':
326
+ case 'max':
327
+ case 'prod': return { ...e, a: renameE(e.a, names, f), b: renameE(e.b, names, f) };
328
+ case 'mul': return { ...e, a: renameE(e.a, names, f) };
329
+ case 'if': return { op: 'if', c: renameC(e.c, names, f), then: renameE(e.then, names, f), else: renameE(e.else, names, f) };
330
+ case 'get': return { ...e, key: renameE(e.key, names, f) };
331
+ }
332
+ };
333
+ const renameC = (c, names, f) => {
334
+ switch (c.op) {
335
+ case '<':
336
+ case '<=':
337
+ case '=': return { op: c.op, a: renameE(c.a, names, f), b: renameE(c.b, names, f) };
338
+ case 'and':
339
+ case 'or': return { op: c.op, xs: c.xs.map(x => renameC(x, names, f)) };
340
+ case 'not': return { op: 'not', x: renameC(c.x, names, f) };
341
+ case 'flag':
342
+ case 'all': return c;
343
+ }
344
+ };
345
+ const bgcd = (a, b) => { if (a < 0n)
346
+ a = -a; if (b < 0n)
347
+ b = -b; while (b)
348
+ [a, b] = [b, a % b]; return a; };
349
+ const norm = (x) => { const g = x.reduce(bgcd, 0n); return g > 1n ? x.map(y => y / g) : x; };
350
+ const dot = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0n);
351
+ export function generators(rows, names, nats = [], cap = 600, until = Infinity) {
352
+ const n = names.length, at = new Map(names.map((x, i) => [x, i]));
353
+ const hs = [...rows.map(r => { const h = Array.from({ length: n + 1 }, () => 0n); for (const [x, c] of Object.entries(r)) {
354
+ if (x === '1')
355
+ h[n] = BigInt(c);
356
+ else
357
+ h[at.get(x)] = BigInt(c);
358
+ } return h; }),
359
+ ...nats.filter(x => at.has(x)).map(x => { const h = Array.from({ length: n + 1 }, () => 0n); h[at.get(x)] = -1n; return h; }),
360
+ Array.from({ length: n + 1 }, (_, i) => (i === n ? -1n : 0n))];
361
+ let lines = Array.from({ length: n + 1 }, (_, i) => Array.from({ length: n + 1 }, (_, j) => (i === j ? 1n : 0n)));
362
+ let rays = [];
363
+ for (const [hi, h] of hs.entries()) {
364
+ const li = lines.findIndex(l => dot(h, l) !== 0n);
365
+ if (li >= 0) {
366
+ let l = lines[li], s = dot(h, l);
367
+ if (s > 0n) {
368
+ l = l.map(x => -x);
369
+ s = -s;
370
+ }
371
+ lines = lines.filter((_, i) => i !== li).map(m => { const hm = dot(h, m); return hm === 0n ? m : norm(m.map((x, i) => s * x - hm * l[i])); });
372
+ rays = rays.map(r => { const hr = dot(h, r.v); return hr === 0n ? { v: r.v, zero: new Set([...r.zero, hi]) } : { v: norm(r.v.map((x, i) => -s * x + hr * l[i])), zero: new Set([...r.zero, hi]) }; });
373
+ rays.push({ v: norm(l), zero: new Set([...hs.keys()].filter(j => j < hi && dot(hs[j], l) === 0n)) });
374
+ continue;
375
+ }
376
+ const val = rays.map(r => dot(h, r.v));
377
+ const pos = rays.filter((_, i) => val[i] > 0n), neg = rays.filter((_, i) => val[i] < 0n), zero = rays.filter((_, i) => val[i] === 0n);
378
+ const next = [...zero.map(r => ({ v: r.v, zero: new Set([...r.zero, hi]) })), ...neg];
379
+ const vp = new Map(pos.map(r => [r, dot(h, r.v)])), vn = new Map(neg.map(r => [r, dot(h, r.v)]));
380
+ if (performance.now() > until)
381
+ return undefined;
382
+ for (const p of pos)
383
+ for (const q of neg) {
384
+ // adjacent: no other ray is zero wherever both are
385
+ const common = [...p.zero].filter(j => q.zero.has(j));
386
+ if (rays.some(r => r !== p && r !== q && common.every(j => r.zero.has(j))))
387
+ continue;
388
+ const a = vp.get(p), b = -vn.get(q);
389
+ next.push({ v: norm(p.v.map((x, i) => b * x + a * q.v[i])), zero: new Set([...common, hi]) });
390
+ if (next.length > cap)
391
+ return undefined;
392
+ }
393
+ rays = next;
394
+ if (rays.length > cap)
395
+ return undefined;
396
+ }
397
+ const safe = (x) => x.every(y => y <= BigInt(Number.MAX_SAFE_INTEGER) && -y <= BigInt(Number.MAX_SAFE_INTEGER));
398
+ if (![...rays.map(r => r.v), ...lines].every(safe))
399
+ return undefined;
400
+ const num = (x) => x.map(Number);
401
+ return { names, vertices: rays.filter(r => r.v[n] > 0n).map(r => num(r.v)), rays: rays.filter(r => r.v[n] === 0n).map(r => num(r.v)), lines: lines.map(num) };
402
+ }
403
+ // --- invariants: Houdini over the relation --------------------------------------------------------------------------
404
+ //
405
+ // Candidate atoms per location (each lin ≤ 0 over the relation's names), those that fail at a start dropped by the
406
+ // caller, then each path's points where its location's candidates hold (its generators) read through the turn: a
407
+ // candidate at the path's target that some point after the turn breaks is dropped, until none is. What is left is
408
+ // inductive over the relation (so over the loop, where the relation is exact); the kernel proves it again as laws.
409
+ export function houdini(rel, cands, locOf, until) {
410
+ const H = cands.map(cs => [...new Map(cs.map(c => [canon(c), c])).values()]);
411
+ // A path's rows with the candidates at its start: of the bounds on one name, only the tightest each way (the others
412
+ // add no vertex).
413
+ const tight = (cs) => {
414
+ const best = new Map(), rest = [];
415
+ for (const c of cs) {
416
+ const xs = vars(c);
417
+ if (xs.length !== 1) {
418
+ rest.push(c);
419
+ continue;
420
+ }
421
+ const x = xs[0], s = Math.sign(c[x]), key = `${x}${s}`, b = (c['1'] ?? 0) / Math.abs(c[x]), was = best.get(key);
422
+ if (!was || b > (was['1'] ?? 0) / Math.abs(was[x]))
423
+ best.set(key, c);
424
+ }
425
+ return [...best.values(), ...rest];
426
+ };
427
+ for (let round = 0, changed = true; changed && round < 50; round++) {
428
+ changed = false;
429
+ for (const [i, p] of rel.paths.entries()) {
430
+ if (performance.now() > until)
431
+ return undefined;
432
+ const [a, b] = locOf(i);
433
+ if (!H[b].length)
434
+ continue;
435
+ const rows = [...p.rows, ...tight(H[a])];
436
+ // (the names the candidates at the target read too: one the path reads nowhere is free there, a line)
437
+ const ns = [...new Set([...rows.flatMap(vars), ...Object.values(p.post).flatMap(vars), ...H[b].flatMap(vars)])].sort();
438
+ const g = generators(rows, ns, rel.nats, 600, until);
439
+ if (!g) {
440
+ // past the generators' cap: each candidate after the turn refuted by the integer solver, or dropped
441
+ const nat = rel.nats.filter(x => ns.includes(x)).map((x) => ({ [x]: -1 }));
442
+ const through = (c) => { let o = {}; for (const [y, k] of Object.entries(c))
443
+ o = plus(o, y !== '1' && p.post[y] ? times(p.post[y], k) : { [y]: k }); return o; };
444
+ const keep = H[b].filter(c => performance.now() < until && solve([...rows, ...nat, plus(konst(1), through(c), -1)].map(lin => ({ lin, label: '' })), 4000).kind === 'refuted');
445
+ if (keep.length < H[b].length) {
446
+ H[b] = keep;
447
+ changed = true;
448
+ }
449
+ continue;
450
+ }
451
+ if (!g.vertices.length)
452
+ continue; // no point: the path never runs where the candidates hold
453
+ const at = (x, t, c) => {
454
+ // c after the turn: each name through the path's value
455
+ let s = t * (c['1'] ?? 0);
456
+ for (const [y, k] of Object.entries(c)) {
457
+ if (y === '1')
458
+ continue;
459
+ const f = p.post[y];
460
+ if (!f) {
461
+ const j = ns.indexOf(y);
462
+ s += k * (j < 0 ? 0 : x[j]);
463
+ continue;
464
+ }
465
+ for (const [u, d] of Object.entries(f)) {
466
+ const j = ns.indexOf(u);
467
+ s += k * d * (u === '1' ? t : j < 0 ? 0 : x[j]);
468
+ }
469
+ }
470
+ return s;
471
+ };
472
+ const keep = H[b].filter(c => g.vertices.every(x => at(x, x[ns.length], c) <= 0) && g.rays.every(x => at(x, 0, c) <= 0) && g.lines.every(x => at(x, 0, c) === 0));
473
+ if (keep.length < H[b].length) {
474
+ H[b] = keep;
475
+ changed = true;
476
+ }
477
+ }
478
+ }
479
+ return H;
480
+ }
@@ -1,8 +1,10 @@
1
+ import { type Rat } from './q.js';
1
2
  export type Lin = Record<string, number>;
2
3
  export type Constraint = {
3
4
  lin: Lin;
4
5
  label: string;
5
6
  src?: RowSrc;
7
+ strict?: boolean;
6
8
  };
7
9
  export type RowSrc = {
8
10
  atom: number;
@@ -46,6 +48,7 @@ export type Verdict = {
46
48
  } | {
47
49
  kind: 'sat';
48
50
  witness: Record<string, number>;
51
+ rational?: Record<string, Rat>;
49
52
  } | {
50
53
  kind: 'unknown';
51
54
  point?: Record<string, number>;
@@ -53,5 +56,5 @@ export type Verdict = {
53
56
  export declare const work: {
54
57
  n: number;
55
58
  };
56
- export declare function solve(constraints: Constraint[], maxRows?: number): Verdict;
57
- export declare function refuteFast(constraints: Constraint[], maxRows?: number): Verdict | undefined;
59
+ export declare function solve(constraints: Constraint[], maxRows?: number, integral?: boolean): Verdict;
60
+ export declare function refuteFast(constraints: Constraint[], maxRows?: number, integral?: boolean): Verdict | undefined;