gdp-wired 0.20.0 → 0.22.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 +38 -7
- package/dist/core/cycles.d.ts +12 -0
- package/dist/core/cycles.js +7 -0
- package/dist/core/cyclic.d.ts +6 -1
- package/dist/core/cyclic.js +13 -1
- package/dist/core/data.d.ts +139 -13
- package/dist/core/data.js +845 -146
- package/dist/core/dsl.d.ts +27 -1
- package/dist/core/dsl.js +150 -32
- package/dist/core/exists.d.ts +26 -0
- package/dist/core/exists.js +625 -0
- package/dist/core/infer.d.ts +13 -0
- package/dist/core/infer.js +258 -41
- package/dist/core/machine.d.ts +27 -0
- package/dist/core/machine.js +45 -0
- package/dist/core/transfer.d.ts +17 -0
- package/dist/core/transfer.js +476 -0
- package/dist/core/watch.js +5 -1
- package/dist/kernel/cdcl.d.ts +40 -0
- package/dist/kernel/cdcl.js +543 -0
- package/dist/kernel/prove.d.ts +25 -2
- package/dist/kernel/prove.js +130 -49
- package/dist/kernel/reader.d.ts +79 -0
- package/dist/kernel/reader.js +539 -0
- package/dist/kernel/simplex.d.ts +32 -0
- package/dist/kernel/simplex.js +232 -0
- package/dist/lean-kernel/emit.d.ts +2 -0
- package/dist/lean-kernel/emit.js +196 -15
- package/dist/lean-kernel/library.js +2 -2
- package/package.json +2 -2
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import { add, k, scale, sub, v } from "../core/solver.js";
|
|
2
|
+
import { atomOf, canon, canonFlip, columnTerm, factorsOf, isCell, termColumn } from "./kernel.js";
|
|
3
|
+
export const none = new Set();
|
|
4
|
+
export const join = (a, b) => {
|
|
5
|
+
if (!a.size || a === b)
|
|
6
|
+
return b;
|
|
7
|
+
if (!b.size)
|
|
8
|
+
return a;
|
|
9
|
+
const out = new Set(a);
|
|
10
|
+
for (const x of b)
|
|
11
|
+
out.add(x);
|
|
12
|
+
return out;
|
|
13
|
+
};
|
|
14
|
+
const R0 = { w: none, pos: -1, fin: true };
|
|
15
|
+
const both = (a, b) => ({ w: join(a.w, b.w), pos: Math.max(a.pos, b.pos), fin: a.fin && b.fin });
|
|
16
|
+
function times(a, b) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const [p, cp] of Object.entries(a))
|
|
19
|
+
for (const [q, cq] of Object.entries(b)) {
|
|
20
|
+
if (p.includes('⟦') || q.includes('⟦'))
|
|
21
|
+
return undefined;
|
|
22
|
+
const fs = [...(p === '1' ? [] : factorsOf(p)), ...(q === '1' ? [] : factorsOf(q))].sort();
|
|
23
|
+
out.push({ [fs.length ? fs.join('*') : '1']: cp * cq });
|
|
24
|
+
}
|
|
25
|
+
return add(...out);
|
|
26
|
+
}
|
|
27
|
+
const one = k(1);
|
|
28
|
+
const namePair = new WeakMap();
|
|
29
|
+
const constantLin = (l) => Object.entries(l).every(([key, c]) => key === '1' || c === 0);
|
|
30
|
+
const lasts = Array.from({ length: 8 }, () => new WeakMap());
|
|
31
|
+
function same(site, owner, a, b, c, make) {
|
|
32
|
+
const m = lasts[site], had = m.get(owner);
|
|
33
|
+
if (had && had.a === a && had.b === b && had.c === c)
|
|
34
|
+
return had.out;
|
|
35
|
+
const out = make();
|
|
36
|
+
m.set(owner, { a, b, c, out });
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
export class Reader {
|
|
40
|
+
goal;
|
|
41
|
+
trail;
|
|
42
|
+
node = 0;
|
|
43
|
+
natRows = new Map();
|
|
44
|
+
atomRows = new Map();
|
|
45
|
+
ls = new Map();
|
|
46
|
+
ts = new Map();
|
|
47
|
+
fs = [];
|
|
48
|
+
constructor(goal, trail) {
|
|
49
|
+
this.goal = goal;
|
|
50
|
+
this.trail = trail;
|
|
51
|
+
}
|
|
52
|
+
// A new node: values with an open read are found again.
|
|
53
|
+
begin() { this.node++; }
|
|
54
|
+
ok(x) {
|
|
55
|
+
if (x.node === this.node)
|
|
56
|
+
return true;
|
|
57
|
+
return x.fin && (x.pos < 0 || (x.pos < this.trail.size() && this.trail.stamp(x.pos) === x.stamp));
|
|
58
|
+
}
|
|
59
|
+
kept(r) { return { pos: r.pos, stamp: r.pos < 0 ? 0 : this.trail.stamp(r.pos), node: this.node }; }
|
|
60
|
+
units = new Map();
|
|
61
|
+
unit(name) { let w = this.units.get(name); if (!w)
|
|
62
|
+
this.units.set(name, (w = new Set([name]))); return w; }
|
|
63
|
+
// Whether lin <= 0 is decided, and by what.
|
|
64
|
+
dec(lin) {
|
|
65
|
+
let constant = true;
|
|
66
|
+
for (const key in lin)
|
|
67
|
+
if (key !== '1') {
|
|
68
|
+
constant = false;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
if (constant)
|
|
72
|
+
return { ...R0, t: (lin['1'] ?? 0) <= 0 };
|
|
73
|
+
let names = namePair.get(lin);
|
|
74
|
+
if (!names)
|
|
75
|
+
namePair.set(lin, (names = [canon(lin), canonFlip(lin)]));
|
|
76
|
+
const [a, f] = names, x = this.trail.get(a);
|
|
77
|
+
if (x && x.val !== undefined)
|
|
78
|
+
return { t: x.val, w: this.unit(a), pos: x.at, fin: true };
|
|
79
|
+
const y = this.trail.get(f);
|
|
80
|
+
if (y && y.val !== undefined)
|
|
81
|
+
return { t: !y.val, w: this.unit(f), pos: y.at, fin: true };
|
|
82
|
+
return { t: undefined, w: none, pos: -1, fin: false };
|
|
83
|
+
}
|
|
84
|
+
linear(e) {
|
|
85
|
+
const had = this.ls.get(e);
|
|
86
|
+
if (had && this.ok(had))
|
|
87
|
+
return had;
|
|
88
|
+
let lin, r = R0;
|
|
89
|
+
switch (e.op) {
|
|
90
|
+
case 'var':
|
|
91
|
+
lin = e.name === '1' || /[*[\],:⟦]/.test(e.name) ? undefined : same(0, e, 0, 0, 0, () => v(e.name));
|
|
92
|
+
break;
|
|
93
|
+
case 'const':
|
|
94
|
+
lin = same(0, e, 0, 0, 0, () => k(e.value));
|
|
95
|
+
break;
|
|
96
|
+
case 'mul': {
|
|
97
|
+
const a = this.linear(e.a);
|
|
98
|
+
lin = a.lin && same(0, e, a.lin, 0, 0, () => scale(a.lin, e.k));
|
|
99
|
+
r = a;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
case 'prod': {
|
|
103
|
+
const a = this.linear(e.a), b = this.linear(e.b);
|
|
104
|
+
lin = a.lin && b.lin && same(0, e, a.lin, b.lin, 0, () => times(a.lin, b.lin));
|
|
105
|
+
r = both(a, b);
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case 'add': {
|
|
109
|
+
const a = this.linear(e.a), b = this.linear(e.b);
|
|
110
|
+
lin = a.lin && b.lin && same(0, e, a.lin, b.lin, 0, () => add(a.lin, b.lin));
|
|
111
|
+
r = both(a, b);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 'sub':
|
|
115
|
+
case 'min':
|
|
116
|
+
case 'max': {
|
|
117
|
+
const a = this.linear(e.a), b = this.linear(e.b);
|
|
118
|
+
if (!a.lin || !b.lin) {
|
|
119
|
+
lin = undefined;
|
|
120
|
+
r = both(a, b);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
const d = this.dec(same(1, e, a.lin, b.lin, 0, () => (e.op === 'sub' ? sub(b.lin, a.lin) : sub(a.lin, b.lin))));
|
|
124
|
+
if (d.t === undefined) {
|
|
125
|
+
lin = same(2, e, 0, 0, 0, () => v(termColumn(e)));
|
|
126
|
+
r = { w: none, pos: -1, fin: false };
|
|
127
|
+
break;
|
|
128
|
+
} // (the column: no reason, but open)
|
|
129
|
+
r = both(both(a, b), d);
|
|
130
|
+
lin = e.op === 'sub' ? same(0, e, a.lin, b.lin, d.t, () => (d.t ? sub(a.lin, b.lin) : k(0))) : (e.op === 'min') === d.t ? a.lin : b.lin;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case 'if': {
|
|
134
|
+
const c = this.truth(e.c);
|
|
135
|
+
if (c.t === undefined) {
|
|
136
|
+
lin = same(2, e, 0, 0, 0, () => v(termColumn(e)));
|
|
137
|
+
r = { w: none, pos: -1, fin: false };
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
const x = this.linear(c.t ? e.then : e.else);
|
|
141
|
+
lin = x.lin;
|
|
142
|
+
r = both(c, x);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case 'get': {
|
|
146
|
+
const key = this.linear(e.key);
|
|
147
|
+
lin = key.lin && same(0, e, key.lin, 0, 0, () => v(`${e.map}[${canon(key.lin)}]`));
|
|
148
|
+
r = key;
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// (an open value's reason is what its decided parts need: only the column's own value is open)
|
|
153
|
+
const out = { lin, w: r.w, fin: r.fin, ...this.kept(r) };
|
|
154
|
+
this.ls.set(e, out);
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
atoms(c) {
|
|
158
|
+
const a = this.linear(c.a), b = this.linear(c.b);
|
|
159
|
+
if (!a.lin || !b.lin)
|
|
160
|
+
return undefined;
|
|
161
|
+
const r = both(a, b), x = a.lin, y = b.lin;
|
|
162
|
+
const atoms = same(3, c, x, y, 0, () => {
|
|
163
|
+
const ab = sub(x, y), ba = sub(y, x);
|
|
164
|
+
if (c.op === '<=')
|
|
165
|
+
return { when: [ab], unless: [[add(ba, k(1))]] };
|
|
166
|
+
if (c.op === '<')
|
|
167
|
+
return { when: [add(ab, k(1))], unless: [[ba]] };
|
|
168
|
+
return { when: [ab, ba], unless: [[add(ab, k(1))], [add(ba, k(1))]] };
|
|
169
|
+
});
|
|
170
|
+
return { ...atoms, r };
|
|
171
|
+
}
|
|
172
|
+
truth(c) {
|
|
173
|
+
const had = this.ts.get(c);
|
|
174
|
+
if (had && this.ok(had))
|
|
175
|
+
return had;
|
|
176
|
+
let t, r;
|
|
177
|
+
switch (c.op) {
|
|
178
|
+
case 'flag': throw new Error('flags are inlined before the kernel sees them');
|
|
179
|
+
case 'all':
|
|
180
|
+
t = undefined;
|
|
181
|
+
r = { ...R0, fin: false };
|
|
182
|
+
break;
|
|
183
|
+
case 'not': {
|
|
184
|
+
const x = this.truth(c.x);
|
|
185
|
+
t = x.t === undefined ? undefined : !x.t;
|
|
186
|
+
r = x;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
case 'and':
|
|
190
|
+
case 'or': {
|
|
191
|
+
const dominant = c.op === 'or', xs = c.xs.map(y => this.truth(y));
|
|
192
|
+
// One part with the dominant value decides it (the one that needs fewest decisions); else every part.
|
|
193
|
+
let best;
|
|
194
|
+
for (const x of xs)
|
|
195
|
+
if (x.t === dominant && (!best || x.w.size < best.w.size || (x.w.size === best.w.size && x.fin && !best.fin)))
|
|
196
|
+
best = x;
|
|
197
|
+
if (best) {
|
|
198
|
+
t = dominant;
|
|
199
|
+
r = best;
|
|
200
|
+
}
|
|
201
|
+
else if (xs.every(x => x.t === !dominant)) {
|
|
202
|
+
t = !dominant;
|
|
203
|
+
r = xs.reduce(both, R0);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
t = undefined;
|
|
207
|
+
r = { ...R0, fin: false };
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case '<':
|
|
212
|
+
case '<=':
|
|
213
|
+
case '=': {
|
|
214
|
+
const cmp = this.atoms(c);
|
|
215
|
+
if (!cmp) {
|
|
216
|
+
t = undefined;
|
|
217
|
+
r = { ...R0, fin: false };
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
const ds = cmp.when.map(lin => this.dec(lin));
|
|
221
|
+
let best;
|
|
222
|
+
for (const d of ds)
|
|
223
|
+
if (d.t === false && (!best || d.w.size < best.w.size))
|
|
224
|
+
best = d;
|
|
225
|
+
if (best) {
|
|
226
|
+
t = false;
|
|
227
|
+
r = { ...both(cmp.r, best), fin: cmp.r.fin };
|
|
228
|
+
}
|
|
229
|
+
else if (ds.every(d => d.t === true)) {
|
|
230
|
+
t = true;
|
|
231
|
+
r = ds.reduce(both, cmp.r);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
t = undefined;
|
|
235
|
+
r = { ...cmp.r, fin: false };
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
// Final: decided, and every term its reason reads final too (a term column's atom on the trail reads differently
|
|
241
|
+
// once the column's choice is decided, so a value through an open term is found again at each node).
|
|
242
|
+
const out = { t, w: r.w, fin: t !== undefined && r.fin, ...this.kept(r) };
|
|
243
|
+
this.ts.set(c, out);
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
// kernel.ts's facts, each row with its reason; final when nothing it read is open.
|
|
247
|
+
factsOf(c, positive, out) {
|
|
248
|
+
switch (c.op) {
|
|
249
|
+
case 'flag': throw new Error('flags are inlined before the kernel sees them');
|
|
250
|
+
case 'all': return R0;
|
|
251
|
+
case 'not': return this.factsOf(c.x, !positive, out);
|
|
252
|
+
case 'and':
|
|
253
|
+
case 'or': {
|
|
254
|
+
if ((c.op === 'and') === positive)
|
|
255
|
+
return c.xs.reduce((acc, y) => both(acc, this.factsOf(y, positive, out)), R0);
|
|
256
|
+
let closed = R0;
|
|
257
|
+
const open = [];
|
|
258
|
+
for (const y of c.xs) {
|
|
259
|
+
const t = this.truth(y);
|
|
260
|
+
if (t.t === !positive)
|
|
261
|
+
closed = both(closed, t);
|
|
262
|
+
else
|
|
263
|
+
open.push(y);
|
|
264
|
+
}
|
|
265
|
+
if (open.length === 0) {
|
|
266
|
+
out.push({ lin: one, w: closed.w, label: '' });
|
|
267
|
+
return closed;
|
|
268
|
+
}
|
|
269
|
+
if (open.length > 1)
|
|
270
|
+
return { ...closed, fin: false };
|
|
271
|
+
const inner = [], r = this.factsOf(open[0], positive, inner);
|
|
272
|
+
for (const row of inner)
|
|
273
|
+
out.push({ lin: row.lin, w: join(row.w, closed.w), label: '' });
|
|
274
|
+
// (the last open part's own value is a read: open while it is)
|
|
275
|
+
const last = this.truth(open[0]);
|
|
276
|
+
// (and the facts are kept only while the part's own value is: its reason is theirs, though not their rows')
|
|
277
|
+
return { ...both(both(closed, r), last), fin: r.fin && last.t !== undefined && last.fin };
|
|
278
|
+
}
|
|
279
|
+
case '<':
|
|
280
|
+
case '<=':
|
|
281
|
+
case '=': {
|
|
282
|
+
const cmp = this.atoms(c);
|
|
283
|
+
if (!cmp)
|
|
284
|
+
return { ...R0, fin: false };
|
|
285
|
+
if (positive) {
|
|
286
|
+
for (const lin of cmp.when)
|
|
287
|
+
out.push({ lin, w: cmp.r.w, label: '' });
|
|
288
|
+
return cmp.r;
|
|
289
|
+
}
|
|
290
|
+
let closed = cmp.r;
|
|
291
|
+
const open = [];
|
|
292
|
+
for (const atoms of cmp.unless) {
|
|
293
|
+
let shut;
|
|
294
|
+
for (const lin of atoms) {
|
|
295
|
+
const d = this.dec(lin);
|
|
296
|
+
if (d.t === false) {
|
|
297
|
+
shut = d;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (shut)
|
|
302
|
+
closed = both(closed, shut);
|
|
303
|
+
else
|
|
304
|
+
open.push(atoms);
|
|
305
|
+
}
|
|
306
|
+
if (open.length === 0) {
|
|
307
|
+
out.push({ lin: one, w: closed.w, label: '' });
|
|
308
|
+
return closed;
|
|
309
|
+
}
|
|
310
|
+
if (open.length > 1)
|
|
311
|
+
return { ...closed, fin: false };
|
|
312
|
+
for (const lin of open[0])
|
|
313
|
+
out.push({ lin, w: closed.w, label: '' });
|
|
314
|
+
return { ...closed, fin: false };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
facts(i) {
|
|
319
|
+
const had = this.fs[i];
|
|
320
|
+
if (had && this.ok(had))
|
|
321
|
+
return had;
|
|
322
|
+
const rows = [], r = this.factsOf(this.goal.hyps[i], true, rows);
|
|
323
|
+
const out = { rows, fin: r.fin, ...this.kept(r) };
|
|
324
|
+
this.fs[i] = out;
|
|
325
|
+
return out;
|
|
326
|
+
}
|
|
327
|
+
// kernel.ts's termRows, each row with its reason (the forms made again only when a part's form is a new object).
|
|
328
|
+
termRows(name) {
|
|
329
|
+
const e = columnTerm(name), col = same(4, e, 0, 0, 0, () => v(name)), out = [];
|
|
330
|
+
if (e.op === 'min' || e.op === 'max' || e.op === 'sub') {
|
|
331
|
+
const a = this.linear(e.a), b = this.linear(e.b), w = join(a.w, b.w);
|
|
332
|
+
if (a.lin && b.lin) {
|
|
333
|
+
const x = a.lin, y = b.lin, lins = same(5, e, x, y, 0, () => e.op === 'min' ? [sub(col, x), sub(col, y)] : e.op === 'max' ? [sub(x, col), sub(y, col)] : [sub(sub(x, y), col), sub(col, x)]);
|
|
334
|
+
for (const lin of lins)
|
|
335
|
+
out.push({ lin, w, label: '' });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (e.op === 'if') {
|
|
339
|
+
const a = this.linear(e.then), b = this.linear(e.else);
|
|
340
|
+
if (a.lin && b.lin && constantLin(a.lin) && constantLin(b.lin)) {
|
|
341
|
+
const x = a.lin, y = b.lin;
|
|
342
|
+
out.push({ lin: same(5, e, x, y, 0, () => sub(col, add(x, y))), w: join(a.w, b.w), label: '' });
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (this.decided(e)) {
|
|
346
|
+
const r = this.linear(e);
|
|
347
|
+
if (r.lin) {
|
|
348
|
+
const x = r.lin, [p, q] = same(6, e, x, 0, 0, () => [sub(col, x), sub(x, col)]);
|
|
349
|
+
out.push({ lin: p, w: r.w, label: '' }, { lin: q, w: r.w, label: '' });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return out;
|
|
353
|
+
}
|
|
354
|
+
decided(e) {
|
|
355
|
+
if (e.op === 'if')
|
|
356
|
+
return this.truth(e.c).t !== undefined;
|
|
357
|
+
if (e.op !== 'sub' && e.op !== 'min' && e.op !== 'max')
|
|
358
|
+
return true;
|
|
359
|
+
const a = this.linear(e.a).lin, b = this.linear(e.b).lin;
|
|
360
|
+
return !!a && !!b && this.dec(e.op === 'sub' ? sub(b, a) : sub(a, b)).t !== undefined;
|
|
361
|
+
}
|
|
362
|
+
// kernel.ts's leaf rows at the trail (leafRows), each with its reason: the trail's atoms, every hypothesis' facts,
|
|
363
|
+
// equal cells at keys the trail proves equal, each term column's rows (and those of the columns they bring), and a
|
|
364
|
+
// natural's row for every name.
|
|
365
|
+
rows() {
|
|
366
|
+
// One row per linear form, with the shortest reason any of its copies has.
|
|
367
|
+
const out = [], at = new Map();
|
|
368
|
+
for (const r of this.leafRows()) {
|
|
369
|
+
const id = canon(r.lin), j = at.get(id);
|
|
370
|
+
if (j === undefined) {
|
|
371
|
+
at.set(id, out.length);
|
|
372
|
+
out.push(r);
|
|
373
|
+
}
|
|
374
|
+
else if (r.w.size < out[j].w.size)
|
|
375
|
+
out[j] = r;
|
|
376
|
+
}
|
|
377
|
+
return out;
|
|
378
|
+
}
|
|
379
|
+
// The rows in kernel.ts's order, copies kept (a leaf's derivation indexes these).
|
|
380
|
+
leafRows() {
|
|
381
|
+
const rows = [];
|
|
382
|
+
for (let i = 0; i < this.trail.size(); i++) {
|
|
383
|
+
const a = this.trail.lin(i), key = `${a.name}=${a.holds}`;
|
|
384
|
+
let r = this.atomRows.get(key);
|
|
385
|
+
if (!r)
|
|
386
|
+
this.atomRows.set(key, (r = { lin: atomOf(a.lin, a.holds).lin, w: new Set([a.name]), label: '' }));
|
|
387
|
+
rows.push(r);
|
|
388
|
+
}
|
|
389
|
+
for (let i = 0; i < this.goal.hyps.length; i++)
|
|
390
|
+
for (const r of this.facts(i).rows)
|
|
391
|
+
rows.push(r);
|
|
392
|
+
const names = new Set();
|
|
393
|
+
for (const r of rows)
|
|
394
|
+
for (const key of namesOf(r.lin))
|
|
395
|
+
names.add(key);
|
|
396
|
+
const cells = [...names].filter(isCell).map(cellOf);
|
|
397
|
+
for (const [i, p] of cells.entries())
|
|
398
|
+
for (const q of cells.slice(i + 1)) {
|
|
399
|
+
if (p.map !== q.map)
|
|
400
|
+
continue;
|
|
401
|
+
const d = sub(p.key, q.key), x = this.dec(d), y = this.dec(scale(d, -1));
|
|
402
|
+
if (x.t === true && y.t === true) {
|
|
403
|
+
const w = join(x.w, y.w);
|
|
404
|
+
rows.push({ lin: sub(v(p.name), v(q.name)), w, label: '' }, { lin: sub(v(q.name), v(p.name)), w, label: '' });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const seen = new Set(), todo = [...names].filter(n => n.startsWith('⟦'));
|
|
408
|
+
while (todo.length) {
|
|
409
|
+
const name = todo.pop();
|
|
410
|
+
if (seen.has(name))
|
|
411
|
+
continue;
|
|
412
|
+
seen.add(name);
|
|
413
|
+
for (const r of this.termRows(name)) {
|
|
414
|
+
rows.push(r);
|
|
415
|
+
for (const key of namesOf(r.lin)) {
|
|
416
|
+
names.add(key);
|
|
417
|
+
if (key.startsWith('⟦') && !seen.has(key))
|
|
418
|
+
todo.push(key);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
for (const name of names) {
|
|
423
|
+
let r = this.natRows.get(name);
|
|
424
|
+
if (!r)
|
|
425
|
+
this.natRows.set(name, (r = { lin: { [name]: -1 }, w: none, label: '' }));
|
|
426
|
+
rows.push(r);
|
|
427
|
+
}
|
|
428
|
+
return rows;
|
|
429
|
+
}
|
|
430
|
+
// prove.ts's open atoms of a hypothesis (openH), in its order; none for a hypothesis whose facts are final.
|
|
431
|
+
open(i, out) {
|
|
432
|
+
if (this.facts(i).fin)
|
|
433
|
+
return;
|
|
434
|
+
this.openH(this.goal.hyps[i], out, true);
|
|
435
|
+
}
|
|
436
|
+
openE(e, out) {
|
|
437
|
+
switch (e.op) {
|
|
438
|
+
case 'var':
|
|
439
|
+
case 'const': return;
|
|
440
|
+
case 'mul': return this.openE(e.a, out);
|
|
441
|
+
case 'prod': return (this.openE(e.a, out), this.openE(e.b, out));
|
|
442
|
+
case 'get': return this.openE(e.key, out);
|
|
443
|
+
case 'if': {
|
|
444
|
+
const t = this.truth(e.c);
|
|
445
|
+
if (t.t === undefined)
|
|
446
|
+
this.openC(e.c, out);
|
|
447
|
+
else
|
|
448
|
+
this.openE(t.t ? e.then : e.else, out);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
case 'add':
|
|
452
|
+
case 'sub':
|
|
453
|
+
case 'min':
|
|
454
|
+
case 'max': {
|
|
455
|
+
if (this.linear(e).fin)
|
|
456
|
+
return;
|
|
457
|
+
this.openE(e.a, out), this.openE(e.b, out);
|
|
458
|
+
const a = this.linear(e.a).lin, b = this.linear(e.b).lin;
|
|
459
|
+
if (a && b && !this.decided(e))
|
|
460
|
+
out.push(same(1, e, a, b, 0, () => (e.op === 'sub' ? sub(b, a) : sub(a, b))));
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
openC(c, out) {
|
|
466
|
+
if (this.truth(c).t !== undefined)
|
|
467
|
+
return;
|
|
468
|
+
if (c.op === 'flag' || c.op === 'all')
|
|
469
|
+
return;
|
|
470
|
+
if (c.op === 'not')
|
|
471
|
+
return this.openC(c.x, out);
|
|
472
|
+
if (c.op === 'and' || c.op === 'or')
|
|
473
|
+
return c.xs.forEach(y => this.openC(y, out));
|
|
474
|
+
const a = this.linear(c.a).lin, b = this.linear(c.b).lin;
|
|
475
|
+
const own = a && b ? same(7, c, a, b, 0, () => { const d = sub(a, b); return c.op === '<=' ? [d] : c.op === '<' ? [add(d, k(1))] : [d, scale(d, -1)]; }) : [];
|
|
476
|
+
this.openE(c.a, out), this.openE(c.b, out);
|
|
477
|
+
for (const x of own)
|
|
478
|
+
out.push(x);
|
|
479
|
+
}
|
|
480
|
+
openH(c, out, positive) {
|
|
481
|
+
if (c.op === 'not')
|
|
482
|
+
return this.openH(c.x, out, !positive);
|
|
483
|
+
if ((c.op === 'and' && positive) || (c.op === 'or' && !positive))
|
|
484
|
+
return c.xs.forEach(y => this.openH(y, out, positive));
|
|
485
|
+
if (c.op === 'and' || c.op === 'or') {
|
|
486
|
+
if (this.truth(c).t !== undefined)
|
|
487
|
+
return;
|
|
488
|
+
const open = c.xs.filter(y => this.truth(y).t !== !positive);
|
|
489
|
+
if (open.length === 1)
|
|
490
|
+
this.openH(open[0], out, positive);
|
|
491
|
+
else
|
|
492
|
+
this.openC(c, out);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (c.op === 'flag' || c.op === 'all')
|
|
496
|
+
return;
|
|
497
|
+
const before = out.length;
|
|
498
|
+
this.openE(c.a, out), this.openE(c.b, out);
|
|
499
|
+
if (out.length === before && !positive && c.op === '=' && this.truth(c).t === undefined)
|
|
500
|
+
this.openC(c, out);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
// A form's variables, and a cell's map and key, remembered (read at every node).
|
|
504
|
+
const nameMemo = new WeakMap();
|
|
505
|
+
export const namesOf = (lin) => {
|
|
506
|
+
let out = nameMemo.get(lin);
|
|
507
|
+
if (!out)
|
|
508
|
+
nameMemo.set(lin, (out = Object.keys(lin).filter(key => key !== '1')));
|
|
509
|
+
return out;
|
|
510
|
+
};
|
|
511
|
+
const cellMemo = new Map();
|
|
512
|
+
const cellOf = (name) => {
|
|
513
|
+
let out = cellMemo.get(name);
|
|
514
|
+
if (!out) {
|
|
515
|
+
if (cellMemo.size > 100_000)
|
|
516
|
+
cellMemo.clear();
|
|
517
|
+
cellMemo.set(name, (out = { name, map: name.slice(0, name.indexOf('[')), key: keyLin(name) }));
|
|
518
|
+
}
|
|
519
|
+
return out;
|
|
520
|
+
};
|
|
521
|
+
export const keyLin = (cell) => {
|
|
522
|
+
const inner = cell.slice(cell.indexOf('[') + 1, -1), out = {};
|
|
523
|
+
let depth = 0, start = 0;
|
|
524
|
+
for (let i = 0; i <= inner.length; i++) {
|
|
525
|
+
const ch = inner[i];
|
|
526
|
+
if (ch === '[')
|
|
527
|
+
depth++;
|
|
528
|
+
else if (ch === ']')
|
|
529
|
+
depth--;
|
|
530
|
+
else if ((ch === ',' && depth === 0) || i === inner.length) {
|
|
531
|
+
if (i > start) {
|
|
532
|
+
const part = inner.slice(start, i), at = part.lastIndexOf(':');
|
|
533
|
+
out[part.slice(0, at)] = Number(part.slice(at + 1));
|
|
534
|
+
}
|
|
535
|
+
start = i + 1;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return out;
|
|
539
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Lin } from '../core/solver.js';
|
|
2
|
+
export type SimplexVerdict = {
|
|
3
|
+
kind: 'sat';
|
|
4
|
+
} | {
|
|
5
|
+
kind: 'conflict';
|
|
6
|
+
rows: number[];
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'unknown';
|
|
9
|
+
};
|
|
10
|
+
export declare class Simplex {
|
|
11
|
+
private index;
|
|
12
|
+
private lo;
|
|
13
|
+
private hi;
|
|
14
|
+
private loWhy;
|
|
15
|
+
private hiWhy;
|
|
16
|
+
private val;
|
|
17
|
+
private rowOf;
|
|
18
|
+
private rows;
|
|
19
|
+
private colOf;
|
|
20
|
+
pivots: number;
|
|
21
|
+
private varOf;
|
|
22
|
+
private slackOf;
|
|
23
|
+
load(rows: {
|
|
24
|
+
lin: Lin;
|
|
25
|
+
}[]): SimplexVerdict;
|
|
26
|
+
private shift;
|
|
27
|
+
check(maxPivots?: number): SimplexVerdict;
|
|
28
|
+
private pivot;
|
|
29
|
+
get size(): number;
|
|
30
|
+
get nnz(): number;
|
|
31
|
+
get vars(): number;
|
|
32
|
+
}
|