gdp-wired 0.32.0 → 0.33.1
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 +13 -0
- package/dist/core/check.js +20 -6
- package/dist/core/cli.d.ts +13 -3
- package/dist/core/cli.js +77 -23
- package/dist/core/dsl.d.ts +2 -2
- package/dist/core/dsl.js +8 -4
- package/dist/core/exists.js +16 -4
- package/dist/core/infer.d.ts +4 -0
- package/dist/core/infer.js +164 -113
- package/dist/core/laws.d.ts +19 -2
- package/dist/core/laws.js +105 -4
- package/dist/core/machine.d.ts +2 -0
- package/dist/core/machine.js +87 -0
- package/dist/core/relational.d.ts +49 -0
- package/dist/core/relational.js +345 -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 +5 -0
- package/dist/kernel/epr.js +1152 -946
- package/dist/kernel/kernel.d.ts +45 -1
- package/dist/kernel/kernel.js +147 -18
- package/dist/kernel/prove.d.ts +1 -0
- package/dist/kernel/prove.js +4 -4
- package/dist/lean-kernel/emit.d.ts +16 -2
- package/dist/lean-kernel/emit.js +66 -3
- package/package.json +6 -2
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
import { canon, columnTerm, factorsOf, isCell, isMonomial, termColumn } from "../kernel/kernel.js";
|
|
2
|
+
export const FORMAT = 'gdp-wired-interchange', VERSION = '1.0';
|
|
3
|
+
export const KINDS = ['goals', 'laws', 'stepLaws', 'kInduction', 'progress', 'liveness', 'loop', 'recur', 'fold', 'cyclic', 'qgoals', 'qmachine', 'termination'];
|
|
4
|
+
// Why a value is outside the format: where, and what.
|
|
5
|
+
export class FormatError extends Error {
|
|
6
|
+
}
|
|
7
|
+
const fail = (at, what) => { throw new FormatError(`${at}: ${what}`); };
|
|
8
|
+
const isObj = (x) => typeof x === 'object' && x !== null && !Array.isArray(x);
|
|
9
|
+
const obj = (x, at, required, optional = []) => {
|
|
10
|
+
if (!isObj(x))
|
|
11
|
+
fail(at, 'an object');
|
|
12
|
+
const o = x;
|
|
13
|
+
for (const key of Object.keys(o))
|
|
14
|
+
if (!required.includes(key) && !optional.includes(key))
|
|
15
|
+
fail(at, `no field ${key}`);
|
|
16
|
+
for (const key of required)
|
|
17
|
+
if (!(key in o))
|
|
18
|
+
fail(at, `the field ${key}`);
|
|
19
|
+
return o;
|
|
20
|
+
};
|
|
21
|
+
const arr = (x, at) => (Array.isArray(x) ? x : fail(at, 'a list'));
|
|
22
|
+
const str = (x, at) => (typeof x === 'string' ? x : fail(at, 'a string'));
|
|
23
|
+
const strs = (x, at) => arr(x, at).map((y, i) => name(y, `${at}[${i}]`));
|
|
24
|
+
const bool = (x, at) => (typeof x === 'boolean' ? x : fail(at, 'true or false'));
|
|
25
|
+
// Every number the format holds is a safe integer (|n| ≤ 2^53 − 1), as the kernel computes exactly only there.
|
|
26
|
+
const int = (x, at) => (typeof x === 'number' && Number.isSafeInteger(x) ? (Object.is(x, -0) ? 0 : x) : fail(at, 'a safe integer'));
|
|
27
|
+
const nat = (x, at) => { const n = int(x, at); return n >= 0 ? n : fail(at, 'a natural'); };
|
|
28
|
+
// A name is a nonempty string that no JavaScript engine orders out of place: not a canonical array index (0, 7, 42),
|
|
29
|
+
// whose keys every engine lists first, so a record's order would not be its pairs' order.
|
|
30
|
+
const arrayIndex = /^(?:0|[1-9]\d*)$/;
|
|
31
|
+
const name = (x, at) => {
|
|
32
|
+
const s = str(x, at);
|
|
33
|
+
if (!s.length || s === '__proto__' || (arrayIndex.test(s) && Number(s) < 2 ** 32 - 1))
|
|
34
|
+
fail(at, `a name (nonempty, not __proto__, not a number like ${JSON.stringify(s)})`);
|
|
35
|
+
return s;
|
|
36
|
+
};
|
|
37
|
+
// A record as [name, value] pairs, in order; and back, each name once.
|
|
38
|
+
const pairs = (r, f, at) => Object.entries(r).map(([k, v]) => [name(k, at), f(v, `${at}.${k}`)]);
|
|
39
|
+
const record = (x, f, at) => {
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [i, p] of arr(x, at).entries()) {
|
|
42
|
+
const pr = arr(p, `${at}[${i}]`);
|
|
43
|
+
if (pr.length !== 2)
|
|
44
|
+
fail(`${at}[${i}]`, 'a pair [name, value]');
|
|
45
|
+
const k = name(pr[0], `${at}[${i}][0]`);
|
|
46
|
+
if (Object.hasOwn(out, k))
|
|
47
|
+
fail(`${at}[${i}]`, `${k} twice`);
|
|
48
|
+
out[k] = f(pr[1], `${at}.${k}`);
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
};
|
|
52
|
+
const opt = (o, key, f, at) => (key in o ? f(o[key], `${at}.${key}`) : undefined);
|
|
53
|
+
// An object with its optional fields set only where present (so a value read back equals the value written).
|
|
54
|
+
const clean = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
55
|
+
// --- terms and conditions (Lean's E and C: every constant and factor a natural) --------------------------------------------
|
|
56
|
+
export function encE(e, at = 'term') {
|
|
57
|
+
if (!isObj(e))
|
|
58
|
+
fail(at, 'a term');
|
|
59
|
+
switch (e.op) {
|
|
60
|
+
case 'var': return { op: e.op, name: name(e.name, `${at}.name`) };
|
|
61
|
+
case 'const': return { op: e.op, value: nat(e.value, `${at}.value`) };
|
|
62
|
+
case 'add':
|
|
63
|
+
case 'sub':
|
|
64
|
+
case 'min':
|
|
65
|
+
case 'max':
|
|
66
|
+
case 'prod': return { op: e.op, a: encE(e.a, `${at}.a`), b: encE(e.b, `${at}.b`) };
|
|
67
|
+
case 'mul': return { op: e.op, k: nat(e.k, `${at}.k`), a: encE(e.a, `${at}.a`) };
|
|
68
|
+
case 'if': return { op: e.op, c: encC(e.c, `${at}.c`), then: encE(e.then, `${at}.then`), else: encE(e.else, `${at}.else`) };
|
|
69
|
+
case 'get': return { op: e.op, map: name(e.map, `${at}.map`), key: encE(e.key, `${at}.key`) };
|
|
70
|
+
default: return fail(at, `no term op ${JSON.stringify(e.op)}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export function decE(x, at = 'term') {
|
|
74
|
+
if (!isObj(x))
|
|
75
|
+
fail(at, 'a term');
|
|
76
|
+
const o = x, op = typeof o.op === 'string' ? o.op : '';
|
|
77
|
+
switch (op) {
|
|
78
|
+
case 'var':
|
|
79
|
+
obj(o, at, ['op', 'name']);
|
|
80
|
+
return { op: 'var', name: name(o.name, `${at}.name`) };
|
|
81
|
+
case 'const':
|
|
82
|
+
obj(o, at, ['op', 'value']);
|
|
83
|
+
return { op: 'const', value: nat(o.value, `${at}.value`) };
|
|
84
|
+
case 'add':
|
|
85
|
+
case 'sub':
|
|
86
|
+
case 'min':
|
|
87
|
+
case 'max':
|
|
88
|
+
case 'prod':
|
|
89
|
+
obj(o, at, ['op', 'a', 'b']);
|
|
90
|
+
return { op, a: decE(o.a, `${at}.a`), b: decE(o.b, `${at}.b`) };
|
|
91
|
+
case 'mul':
|
|
92
|
+
obj(o, at, ['op', 'k', 'a']);
|
|
93
|
+
return { op: 'mul', k: nat(o.k, `${at}.k`), a: decE(o.a, `${at}.a`) };
|
|
94
|
+
case 'if':
|
|
95
|
+
obj(o, at, ['op', 'c', 'then', 'else']);
|
|
96
|
+
return { op: 'if', c: decC(o.c, `${at}.c`), then: decE(o.then, `${at}.then`), else: decE(o.else, `${at}.else`) };
|
|
97
|
+
case 'get':
|
|
98
|
+
obj(o, at, ['op', 'map', 'key']);
|
|
99
|
+
return { op: 'get', map: name(o.map, `${at}.map`), key: decE(o.key, `${at}.key`) };
|
|
100
|
+
default: return fail(at, `no term op ${JSON.stringify(o.op ?? null)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export function encC(c, at = 'condition') {
|
|
104
|
+
if (!isObj(c))
|
|
105
|
+
fail(at, 'a condition');
|
|
106
|
+
switch (c.op) {
|
|
107
|
+
case '<':
|
|
108
|
+
case '<=':
|
|
109
|
+
case '=': return { op: c.op, a: encE(c.a, `${at}.a`), b: encE(c.b, `${at}.b`) };
|
|
110
|
+
case 'and':
|
|
111
|
+
case 'or': return { op: c.op, xs: arr(c.xs, `${at}.xs`).map((y, i) => encC(y, `${at}.xs[${i}]`)) };
|
|
112
|
+
case 'not': return { op: c.op, x: encC(c.x, `${at}.x`) };
|
|
113
|
+
case 'flag': return { op: c.op, name: name(c.name, `${at}.name`) };
|
|
114
|
+
case 'all': return clean({ op: c.op, key: name(c.key, `${at}.key`), keys: c.keys === undefined ? undefined : strs(c.keys, `${at}.keys`), c: encC(c.c, `${at}.c`), witness: c.witness === undefined ? undefined : encE(c.witness, `${at}.witness`) });
|
|
115
|
+
default: return fail(at, `no condition op ${JSON.stringify(c.op)}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
export function decC(x, at = 'condition') {
|
|
119
|
+
if (!isObj(x))
|
|
120
|
+
fail(at, 'a condition');
|
|
121
|
+
const o = x, op = typeof o.op === 'string' ? o.op : '';
|
|
122
|
+
switch (op) {
|
|
123
|
+
case '<':
|
|
124
|
+
case '<=':
|
|
125
|
+
case '=':
|
|
126
|
+
obj(o, at, ['op', 'a', 'b']);
|
|
127
|
+
return { op, a: decE(o.a, `${at}.a`), b: decE(o.b, `${at}.b`) };
|
|
128
|
+
case 'and':
|
|
129
|
+
case 'or':
|
|
130
|
+
obj(o, at, ['op', 'xs']);
|
|
131
|
+
return { op, xs: arr(o.xs, `${at}.xs`).map((y, i) => decC(y, `${at}.xs[${i}]`)) };
|
|
132
|
+
case 'not':
|
|
133
|
+
obj(o, at, ['op', 'x']);
|
|
134
|
+
return { op: 'not', x: decC(o.x, `${at}.x`) };
|
|
135
|
+
case 'flag':
|
|
136
|
+
obj(o, at, ['op', 'name']);
|
|
137
|
+
return { op: 'flag', name: name(o.name, `${at}.name`) };
|
|
138
|
+
case 'all':
|
|
139
|
+
obj(o, at, ['op', 'key', 'c'], ['keys', 'witness']);
|
|
140
|
+
return clean({ op: 'all', key: name(o.key, `${at}.key`), keys: opt(o, 'keys', strs, at), c: decC(o.c, `${at}.c`), witness: opt(o, 'witness', decE, at) });
|
|
141
|
+
default: return fail(at, `no condition op ${JSON.stringify(o.op ?? null)}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// --- rationals (Rationals.lean's Q.E and Q.C) ----------------------------------------------------------------------------
|
|
145
|
+
// A rational is its numerator and denominator as decimal strings (any size; the denominator positive).
|
|
146
|
+
const encRat = (q, at) => (typeof q?.num === 'bigint' && typeof q.den === 'bigint' && q.den > 0n ? { num: q.num.toString(), den: q.den.toString() } : fail(at, 'a rational with a positive denominator'));
|
|
147
|
+
const decRat = (x, at) => {
|
|
148
|
+
const o = obj(x, at, ['num', 'den']), num = str(o.num, `${at}.num`), den = str(o.den, `${at}.den`);
|
|
149
|
+
if (!/^-?(?:0|[1-9]\d*)$/.test(num) || !/^[1-9]\d*$/.test(den))
|
|
150
|
+
fail(at, 'a numerator (an integer) and a denominator (a positive integer) in decimal');
|
|
151
|
+
return { num: BigInt(num), den: BigInt(den) };
|
|
152
|
+
};
|
|
153
|
+
export function encQE(e, at = 'term') {
|
|
154
|
+
if (!isObj(e))
|
|
155
|
+
fail(at, 'a rational term');
|
|
156
|
+
switch (e.op) {
|
|
157
|
+
case 'var': return { op: e.op, name: name(e.name, `${at}.name`) };
|
|
158
|
+
case 'const': return { op: e.op, q: encRat(e.q, `${at}.q`) };
|
|
159
|
+
case 'add':
|
|
160
|
+
case 'sub':
|
|
161
|
+
case 'min':
|
|
162
|
+
case 'max': return { op: e.op, a: encQE(e.a, `${at}.a`), b: encQE(e.b, `${at}.b`) };
|
|
163
|
+
case 'mul': return { op: e.op, k: encRat(e.k, `${at}.k`), a: encQE(e.a, `${at}.a`) };
|
|
164
|
+
case 'if': return { op: e.op, c: encQC(e.c, `${at}.c`), then: encQE(e.then, `${at}.then`), else: encQE(e.else, `${at}.else`) };
|
|
165
|
+
default: return fail(at, `no rational term op ${JSON.stringify(e.op)}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export function decQE(x, at = 'term') {
|
|
169
|
+
if (!isObj(x))
|
|
170
|
+
fail(at, 'a rational term');
|
|
171
|
+
const o = x, op = typeof o.op === 'string' ? o.op : '';
|
|
172
|
+
switch (op) {
|
|
173
|
+
case 'var':
|
|
174
|
+
obj(o, at, ['op', 'name']);
|
|
175
|
+
return { op: 'var', name: name(o.name, `${at}.name`) };
|
|
176
|
+
case 'const':
|
|
177
|
+
obj(o, at, ['op', 'q']);
|
|
178
|
+
return { op: 'const', q: decRat(o.q, `${at}.q`) };
|
|
179
|
+
case 'add':
|
|
180
|
+
case 'sub':
|
|
181
|
+
case 'min':
|
|
182
|
+
case 'max':
|
|
183
|
+
obj(o, at, ['op', 'a', 'b']);
|
|
184
|
+
return { op, a: decQE(o.a, `${at}.a`), b: decQE(o.b, `${at}.b`) };
|
|
185
|
+
case 'mul':
|
|
186
|
+
obj(o, at, ['op', 'k', 'a']);
|
|
187
|
+
return { op: 'mul', k: decRat(o.k, `${at}.k`), a: decQE(o.a, `${at}.a`) };
|
|
188
|
+
case 'if':
|
|
189
|
+
obj(o, at, ['op', 'c', 'then', 'else']);
|
|
190
|
+
return { op: 'if', c: decQC(o.c, `${at}.c`), then: decQE(o.then, `${at}.then`), else: decQE(o.else, `${at}.else`) };
|
|
191
|
+
default: return fail(at, `no rational term op ${JSON.stringify(o.op ?? null)}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export function encQC(c, at = 'condition') {
|
|
195
|
+
if (!isObj(c))
|
|
196
|
+
fail(at, 'a rational condition');
|
|
197
|
+
switch (c.op) {
|
|
198
|
+
case '<':
|
|
199
|
+
case '<=':
|
|
200
|
+
case '=': return { op: c.op, a: encQE(c.a, `${at}.a`), b: encQE(c.b, `${at}.b`) };
|
|
201
|
+
case 'and':
|
|
202
|
+
case 'or': return { op: c.op, a: encQC(c.a, `${at}.a`), b: encQC(c.b, `${at}.b`) };
|
|
203
|
+
case 'not': return { op: c.op, x: encQC(c.x, `${at}.x`) };
|
|
204
|
+
default: return fail(at, `no rational condition op ${JSON.stringify(c.op)}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export function decQC(x, at = 'condition') {
|
|
208
|
+
if (!isObj(x))
|
|
209
|
+
fail(at, 'a rational condition');
|
|
210
|
+
const o = x, op = typeof o.op === 'string' ? o.op : '';
|
|
211
|
+
switch (op) {
|
|
212
|
+
case '<':
|
|
213
|
+
case '<=':
|
|
214
|
+
case '=':
|
|
215
|
+
obj(o, at, ['op', 'a', 'b']);
|
|
216
|
+
return { op, a: decQE(o.a, `${at}.a`), b: decQE(o.b, `${at}.b`) };
|
|
217
|
+
case 'and':
|
|
218
|
+
case 'or':
|
|
219
|
+
obj(o, at, ['op', 'a', 'b']);
|
|
220
|
+
return { op, a: decQC(o.a, `${at}.a`), b: decQC(o.b, `${at}.b`) };
|
|
221
|
+
case 'not':
|
|
222
|
+
obj(o, at, ['op', 'x']);
|
|
223
|
+
return { op: 'not', x: decQC(o.x, `${at}.x`) };
|
|
224
|
+
default: return fail(at, `no rational condition op ${JSON.stringify(o.op ?? null)}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// A cell's key as the kernel writes it (canon: `name:coef` joined by ',', a name itself a cell or a monomial maybe).
|
|
228
|
+
const parseKey = (text) => {
|
|
229
|
+
const lin = {};
|
|
230
|
+
let depth = 0, start = 0;
|
|
231
|
+
for (let i = 0; i <= text.length; i++) {
|
|
232
|
+
const ch = text[i];
|
|
233
|
+
if (ch === '[')
|
|
234
|
+
depth++;
|
|
235
|
+
else if (ch === ']')
|
|
236
|
+
depth--;
|
|
237
|
+
else if ((ch === ',' && depth === 0) || i === text.length) {
|
|
238
|
+
if (i > start) {
|
|
239
|
+
const part = text.slice(start, i), j = part.lastIndexOf(':');
|
|
240
|
+
lin[part.slice(0, j)] = Number(part.slice(j + 1));
|
|
241
|
+
}
|
|
242
|
+
start = i + 1;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return lin;
|
|
246
|
+
};
|
|
247
|
+
// A variable's name has none of the characters the kernel builds cells, monomials and columns with.
|
|
248
|
+
const plain = /^[^*[\],:⟦⟧]+$/;
|
|
249
|
+
function encAtom(key, cols, at) {
|
|
250
|
+
if (!('terms' in cols))
|
|
251
|
+
throw new Error('writing reads no table');
|
|
252
|
+
const col = /^⟦\d+⟧$/.test(key) ? columnTerm(key) : undefined;
|
|
253
|
+
if (col) {
|
|
254
|
+
const term = encE(col, `${at} (a column's term)`), id = JSON.stringify(term);
|
|
255
|
+
let i = cols.index.get(id);
|
|
256
|
+
if (i === undefined) {
|
|
257
|
+
i = cols.terms.length;
|
|
258
|
+
cols.index.set(id, i);
|
|
259
|
+
cols.terms.push(term);
|
|
260
|
+
}
|
|
261
|
+
return { col: i };
|
|
262
|
+
}
|
|
263
|
+
if (isMonomial(key))
|
|
264
|
+
return { mono: factorsOf(key).map((f, i) => encAtom(f, cols, `${at}.mono[${i}]`)) };
|
|
265
|
+
if (isCell(key)) {
|
|
266
|
+
const map = key.slice(0, key.indexOf('[')), inner = key.slice(key.indexOf('[') + 1, -1);
|
|
267
|
+
// (a cell's name is the kernel's canon of its key: written back from the key read, it must be the same text)
|
|
268
|
+
const lin = parseKey(inner);
|
|
269
|
+
if (canon(lin) !== inner)
|
|
270
|
+
fail(at, `a cell whose key ${inner} isn't one the kernel writes`);
|
|
271
|
+
return { cell: name(map, at), key: encLin(lin, cols, `${at}.key`) };
|
|
272
|
+
}
|
|
273
|
+
if (key === '1' || !plain.test(key))
|
|
274
|
+
fail(at, `no atom ${JSON.stringify(key)}`);
|
|
275
|
+
return name(key, at);
|
|
276
|
+
}
|
|
277
|
+
function decAtom(x, cols, at) {
|
|
278
|
+
if (!('read' in cols))
|
|
279
|
+
throw new Error('reading writes no table');
|
|
280
|
+
if (typeof x === 'string')
|
|
281
|
+
return x !== '1' && plain.test(x) ? name(x, at) : fail(at, `a variable's name (not 1, none of * [ ] , : ⟦ ⟧): ${JSON.stringify(x)}`);
|
|
282
|
+
const o = isObj(x) ? x : fail(at, 'an atom: a name, {cell, key}, {mono} or {col}');
|
|
283
|
+
if ('col' in o) {
|
|
284
|
+
obj(o, at, ['col']);
|
|
285
|
+
const i = nat(o.col, `${at}.col`), t = cols.read[i];
|
|
286
|
+
return t ? termColumn(t) : fail(at, `column ${i} of ${cols.read.length}`);
|
|
287
|
+
}
|
|
288
|
+
if ('mono' in o) {
|
|
289
|
+
obj(o, at, ['mono']);
|
|
290
|
+
const fs = arr(o.mono, `${at}.mono`).map((f, i) => decAtom(f, cols, `${at}.mono[${i}]`));
|
|
291
|
+
if (fs.length < 2 || fs.some(f => !isCell(f) && !plain.test(f)))
|
|
292
|
+
fail(at, 'a monomial: two factors or more, each a variable or a cell');
|
|
293
|
+
// (the kernel's name for a product: its factors' names sorted, joined by *)
|
|
294
|
+
return [...fs].sort().join('*');
|
|
295
|
+
}
|
|
296
|
+
obj(o, at, ['cell', 'key']);
|
|
297
|
+
return `${name(o.cell, `${at}.cell`)}[${canon(decLin(o.key, cols, `${at}.key`))}]`;
|
|
298
|
+
}
|
|
299
|
+
export function encLin(lin, cols, at = 'row') {
|
|
300
|
+
if (!isObj(lin))
|
|
301
|
+
fail(at, 'a linear form');
|
|
302
|
+
const out = ['1' in lin ? int(lin['1'], `${at}[1]`) : null];
|
|
303
|
+
for (const key of Object.keys(lin))
|
|
304
|
+
if (key !== '1')
|
|
305
|
+
out.push([int(lin[key], `${at}[${key}]`), encAtom(key, cols, `${at}[${key}]`)]);
|
|
306
|
+
return out;
|
|
307
|
+
}
|
|
308
|
+
export function decLin(x, cols, at = 'row') {
|
|
309
|
+
const xs = arr(x, at);
|
|
310
|
+
if (!xs.length)
|
|
311
|
+
fail(at, 'a linear form: its constant (or null), then [coefficient, atom] pairs');
|
|
312
|
+
const out = {};
|
|
313
|
+
if (xs[0] !== null)
|
|
314
|
+
out['1'] = int(xs[0], `${at}[0]`);
|
|
315
|
+
for (const [i, t] of xs.slice(1).entries()) {
|
|
316
|
+
const p = arr(t, `${at}[${i + 1}]`);
|
|
317
|
+
if (p.length !== 2)
|
|
318
|
+
fail(`${at}[${i + 1}]`, 'a pair [coefficient, atom]');
|
|
319
|
+
const key = decAtom(p[1], cols, `${at}[${i + 1}][1]`);
|
|
320
|
+
if (Object.hasOwn(out, key))
|
|
321
|
+
fail(`${at}[${i + 1}]`, `the atom ${key} twice`);
|
|
322
|
+
out[key] = int(p[0], `${at}[${i + 1}][0]`);
|
|
323
|
+
}
|
|
324
|
+
return out;
|
|
325
|
+
}
|
|
326
|
+
// --- trees: splits on atoms, a derivation at each leaf --------------------------------------------------------------------
|
|
327
|
+
const encStep = (s, at) => {
|
|
328
|
+
switch (s?.kind) {
|
|
329
|
+
case 'input': return ['input', nat(s.index, `${at}.index`)];
|
|
330
|
+
case 'combine': return ['combine', int(s.a, `${at}.a`), int(s.b, `${at}.b`), int(s.ma, `${at}.ma`), int(s.mb, `${at}.mb`)];
|
|
331
|
+
case 'cut': return ['cut', int(s.of, `${at}.of`), int(s.g, `${at}.g`)];
|
|
332
|
+
default: return fail(at, 'a step: input, combine or cut');
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
const decStep = (x, at) => {
|
|
336
|
+
const s = arr(x, at);
|
|
337
|
+
if (s[0] === 'input' && s.length === 2)
|
|
338
|
+
return { kind: 'input', index: nat(s[1], `${at}[1]`) };
|
|
339
|
+
if (s[0] === 'combine' && s.length === 5)
|
|
340
|
+
return { kind: 'combine', a: int(s[1], `${at}[1]`), b: int(s[2], `${at}[2]`), ma: int(s[3], `${at}[3]`), mb: int(s[4], `${at}[4]`) };
|
|
341
|
+
if (s[0] === 'cut' && s.length === 3)
|
|
342
|
+
return { kind: 'cut', of: int(s[1], `${at}[1]`), g: int(s[2], `${at}[2]`) };
|
|
343
|
+
return fail(at, 'a step: ["input", i], ["combine", a, b, ma, mb] or ["cut", of, g]');
|
|
344
|
+
};
|
|
345
|
+
export function encTree(t, cols, at = 'tree') {
|
|
346
|
+
if (!isObj(t))
|
|
347
|
+
fail(at, 'a tree');
|
|
348
|
+
if ('split' in t) {
|
|
349
|
+
if (Object.keys(t).some(k => !['split', 'yes', 'no'].includes(k)))
|
|
350
|
+
fail(at, `a split has split, yes and no (not ${Object.keys(t).join(', ')})`);
|
|
351
|
+
return { split: encLin(t.split, cols, `${at}.split`), yes: encTree(t.yes, cols, `${at}.yes`), no: encTree(t.no, cols, `${at}.no`) };
|
|
352
|
+
}
|
|
353
|
+
if (Object.keys(t).some(k => !['leaf', 'products', 'squares'].includes(k)))
|
|
354
|
+
fail(at, `a leaf has leaf, products and squares (not ${Object.keys(t).join(', ')})`);
|
|
355
|
+
return clean({ leaf: arr(t.leaf, `${at}.leaf`).map((s, i) => encStep(s, `${at}.leaf[${i}]`)),
|
|
356
|
+
products: t.products === undefined ? undefined : t.products.map((p, i) => (Array.isArray(p) && p.length === 2 ? [nat(p[0], `${at}.products[${i}]`), nat(p[1], `${at}.products[${i}]`)] : fail(`${at}.products[${i}]`, 'a pair of rows'))),
|
|
357
|
+
squares: t.squares === undefined ? undefined : t.squares.map((l, i) => encLin(l, cols, `${at}.squares[${i}]`)) });
|
|
358
|
+
}
|
|
359
|
+
export function decTree(x, cols, at = 'tree') {
|
|
360
|
+
const o = isObj(x) ? x : fail(at, 'a tree');
|
|
361
|
+
if ('split' in o) {
|
|
362
|
+
obj(o, at, ['split', 'yes', 'no']);
|
|
363
|
+
return { split: decLin(o.split, cols, `${at}.split`), yes: decTree(o.yes, cols, `${at}.yes`), no: decTree(o.no, cols, `${at}.no`) };
|
|
364
|
+
}
|
|
365
|
+
obj(o, at, ['leaf'], ['products', 'squares']);
|
|
366
|
+
return clean({ leaf: arr(o.leaf, `${at}.leaf`).map((s, i) => decStep(s, `${at}.leaf[${i}]`)),
|
|
367
|
+
products: opt(o, 'products', (p, a) => arr(p, a).map((q, i) => { const r = arr(q, `${a}[${i}]`); return r.length === 2 ? [nat(r[0], `${a}[${i}][0]`), nat(r[1], `${a}[${i}][1]`)] : fail(`${a}[${i}]`, 'a pair of rows'); }), at),
|
|
368
|
+
squares: opt(o, 'squares', (s, a) => arr(s, a).map((l, i) => decLin(l, cols, `${a}[${i}]`)), at) });
|
|
369
|
+
}
|
|
370
|
+
const encTrees = (ts, cols, at) => (isObj(ts) ? Object.entries(ts).map(([n, t]) => [str(n, at), encTree(t, cols, `${at}.${n}`)]) : fail(at, 'trees by goal'));
|
|
371
|
+
// (goal names are any strings: the kernel writes them, `law @start`, `node: use 2, its hypothesis 0`)
|
|
372
|
+
const decTrees = (x, cols, at) => {
|
|
373
|
+
const out = {};
|
|
374
|
+
for (const [i, p] of arr(x, at).entries()) {
|
|
375
|
+
const pr = arr(p, `${at}[${i}]`);
|
|
376
|
+
if (pr.length !== 2)
|
|
377
|
+
fail(`${at}[${i}]`, 'a pair [goal, tree]');
|
|
378
|
+
const g = str(pr[0], `${at}[${i}][0]`);
|
|
379
|
+
if (Object.hasOwn(out, g) || g === '__proto__')
|
|
380
|
+
fail(`${at}[${i}]`, `the goal ${g} twice`);
|
|
381
|
+
out[g] = decTree(pr[1], cols, `${at}.${g}`);
|
|
382
|
+
}
|
|
383
|
+
return out;
|
|
384
|
+
};
|
|
385
|
+
// --- machines, laws and the rest of the subjects ----------------------------------------------------------------------------
|
|
386
|
+
const encMapUpdate = (w, at) => ('each' in w ? { each: name(w.each, `${at}.each`), value: encE(w.value, `${at}.value`) } : { key: encE(w.key, `${at}.key`), value: encE(w.value, `${at}.value`) });
|
|
387
|
+
const decMapUpdate = (x, at) => {
|
|
388
|
+
const o = isObj(x) ? x : fail(at, 'a write');
|
|
389
|
+
if ('each' in o) {
|
|
390
|
+
obj(o, at, ['each', 'value']);
|
|
391
|
+
return { each: name(o.each, `${at}.each`), value: decE(o.value, `${at}.value`) };
|
|
392
|
+
}
|
|
393
|
+
obj(o, at, ['key', 'value']);
|
|
394
|
+
return { key: decE(o.key, `${at}.key`), value: decE(o.value, `${at}.value`) };
|
|
395
|
+
};
|
|
396
|
+
const encHistory = (h, at) => clean({ keep: encC(h.keep, `${at}.keep`), record: strs(h.record, `${at}.record`), sums: pairs(h.sums, encE, `${at}.sums`), queries: pairs(h.queries, encE, `${at}.queries`),
|
|
397
|
+
relations: h.relations?.map((r, i) => ({ when: encC(r.when, `${at}.relations[${i}].when`), holds: encC(r.holds, `${at}.relations[${i}].holds`) })),
|
|
398
|
+
prune: h.prune && { order: name(h.prune.order, `${at}.prune.order`), after: encE(h.prune.after, `${at}.prune.after`) } });
|
|
399
|
+
const decHistory = (x, at) => {
|
|
400
|
+
const o = obj(x, at, ['keep', 'record', 'sums', 'queries'], ['relations', 'prune']);
|
|
401
|
+
return clean({ keep: decC(o.keep, `${at}.keep`), record: strs(o.record, `${at}.record`), sums: record(o.sums, decE, `${at}.sums`), queries: record(o.queries, decE, `${at}.queries`),
|
|
402
|
+
relations: opt(o, 'relations', (r, a) => arr(r, a).map((y, i) => { const q = obj(y, `${a}[${i}]`, ['when', 'holds']); return { when: decC(q.when, `${a}[${i}].when`), holds: decC(q.holds, `${a}[${i}].holds`) }; }), at),
|
|
403
|
+
prune: opt(o, 'prune', (p, a) => { const q = obj(p, a, ['order', 'after']); return { order: name(q.order, `${a}.order`), after: decE(q.after, `${a}.after`) }; }, at) });
|
|
404
|
+
};
|
|
405
|
+
const encLawLike = (l, at) => ({ name: str(l.name, `${at}.name`), holds: encC(l.holds, `${at}.holds`) });
|
|
406
|
+
const decLawLike = (x, at) => { const o = obj(x, at, ['name', 'holds']); return { name: str(o.name, `${at}.name`), holds: decC(o.holds, `${at}.holds`) }; };
|
|
407
|
+
const encRecurrence = (r, at) => clean({ args: strs(r.args, `${at}.args`), zero: encE(r.zero, `${at}.zero`), next: encE(r.next, `${at}.next`),
|
|
408
|
+
lemmas: r.lemmas.map((l, i) => encLawLike(l, `${at}.lemmas[${i}]`)), between: r.between.map((l, i) => encLawLike(l, `${at}.between[${i}]`)),
|
|
409
|
+
uses: pairs(r.uses, (u, a) => clean({ arity: nat(u.arity, `${a}.arity`), lean: u.lean === undefined ? undefined : str(u.lean, `${a}.lean`) }), `${at}.uses`),
|
|
410
|
+
proofs: r.proofs && clean({ lemmas: r.proofs.lemmas && encML(r.proofs.lemmas, `${at}.proofs.lemmas`), between: r.proofs.between && encML(r.proofs.between, `${at}.proofs.between`) }) });
|
|
411
|
+
const decRecurrence = (x, at) => {
|
|
412
|
+
const o = obj(x, at, ['args', 'zero', 'next', 'lemmas', 'between', 'uses'], ['proofs']);
|
|
413
|
+
return clean({ args: strs(o.args, `${at}.args`), zero: decE(o.zero, `${at}.zero`), next: decE(o.next, `${at}.next`),
|
|
414
|
+
lemmas: arr(o.lemmas, `${at}.lemmas`).map((l, i) => decLawLike(l, `${at}.lemmas[${i}]`)), between: arr(o.between, `${at}.between`).map((l, i) => decLawLike(l, `${at}.between[${i}]`)),
|
|
415
|
+
uses: record(o.uses, (u, a) => { const q = obj(u, a, ['arity'], ['lean']); return clean({ arity: nat(q.arity, `${a}.arity`), lean: opt(q, 'lean', str, a) }); }, `${at}.uses`),
|
|
416
|
+
proofs: opt(o, 'proofs', (p, a) => { const q = obj(p, a, [], ['lemmas', 'between']); return clean({ lemmas: opt(q, 'lemmas', decML, a), between: opt(q, 'between', decML, a) }); }, at) });
|
|
417
|
+
};
|
|
418
|
+
const encML = (x, at) => ({ machine: encMachine(x.machine, `${at}.machine`), laws: x.laws.map((l, i) => encLaw(l, `${at}.laws[${i}]`)) });
|
|
419
|
+
const decML = (x, at) => { const o = obj(x, at, ['machine', 'laws']); return { machine: decMachine(o.machine, `${at}.machine`), laws: decLaws(o.laws, `${at}.laws`) }; };
|
|
420
|
+
const encFn = (f, at) => clean({ js: str(f.js, `${at}.js`), lean: f.lean === undefined ? undefined : str(f.lean, `${at}.lean`), recur: f.recur && encRecurrence(f.recur, `${at}.recur`),
|
|
421
|
+
arity: f.arity === undefined ? undefined : nat(f.arity, `${at}.arity`), call: f.call === undefined ? undefined : str(f.call, `${at}.call`) });
|
|
422
|
+
const decFn = (x, at) => {
|
|
423
|
+
const o = obj(x, at, ['js'], ['lean', 'recur', 'arity', 'call']);
|
|
424
|
+
return clean({ js: str(o.js, `${at}.js`), lean: opt(o, 'lean', str, at), recur: opt(o, 'recur', decRecurrence, at), arity: opt(o, 'arity', nat, at), call: opt(o, 'call', str, at) });
|
|
425
|
+
};
|
|
426
|
+
export function encMachine(m, at = 'machine') {
|
|
427
|
+
if (!isObj(m))
|
|
428
|
+
fail(at, 'a machine');
|
|
429
|
+
for (const key of Object.keys(m))
|
|
430
|
+
if (!['name', 'params', 'inputs', 'state', 'maps', 'inputMaps', 'step', 'pre', 'assume', 'history', 'fns', 'facts'].includes(key) && m[key] !== undefined)
|
|
431
|
+
fail(at, `no field ${key}`);
|
|
432
|
+
const s = m.step;
|
|
433
|
+
if (!isObj(s) || Object.keys(s).some(k => !['flags', 'state', 'maps'].includes(k) && s[k] !== undefined))
|
|
434
|
+
fail(`${at}.step`, 'a step: flags, state and maps');
|
|
435
|
+
return clean({ name: str(m.name, `${at}.name`), params: strs(m.params, `${at}.params`), inputs: strs(m.inputs, `${at}.inputs`), state: pairs(m.state, encE, `${at}.state`),
|
|
436
|
+
maps: m.maps && pairs(m.maps, nat, `${at}.maps`), inputMaps: m.inputMaps && strs(m.inputMaps, `${at}.inputMaps`),
|
|
437
|
+
step: clean({ flags: s.flags && pairs(s.flags, encC, `${at}.step.flags`), state: s.state && pairs(s.state, encE, `${at}.step.state`), maps: s.maps && pairs(s.maps, encMapUpdate, `${at}.step.maps`) }),
|
|
438
|
+
pre: m.pre && encC(m.pre, `${at}.pre`), assume: m.assume && encC(m.assume, `${at}.assume`), history: m.history && encHistory(m.history, `${at}.history`),
|
|
439
|
+
fns: m.fns && pairs(m.fns, encFn, `${at}.fns`), facts: m.facts && m.facts.map((l, i) => encLaw(l, `${at}.facts[${i}]`)) });
|
|
440
|
+
}
|
|
441
|
+
export function decMachine(x, at = 'machine') {
|
|
442
|
+
const o = obj(x, at, ['name', 'params', 'inputs', 'state', 'step'], ['maps', 'inputMaps', 'pre', 'assume', 'history', 'fns', 'facts']);
|
|
443
|
+
const s = obj(o.step, `${at}.step`, [], ['flags', 'state', 'maps']);
|
|
444
|
+
return clean({ name: str(o.name, `${at}.name`), params: strs(o.params, `${at}.params`), inputs: strs(o.inputs, `${at}.inputs`), state: record(o.state, decE, `${at}.state`),
|
|
445
|
+
maps: opt(o, 'maps', (y, a) => record(y, nat, a), at), inputMaps: opt(o, 'inputMaps', strs, at),
|
|
446
|
+
step: clean({ flags: opt(s, 'flags', (y, a) => record(y, decC, a), `${at}.step`), state: opt(s, 'state', (y, a) => record(y, decE, a), `${at}.step`), maps: opt(s, 'maps', (y, a) => record(y, decMapUpdate, a), `${at}.step`) }),
|
|
447
|
+
pre: opt(o, 'pre', decC, at), assume: opt(o, 'assume', decC, at), history: opt(o, 'history', decHistory, at), fns: opt(o, 'fns', (y, a) => record(y, decFn, a), at), facts: opt(o, 'facts', decLaws, at) });
|
|
448
|
+
}
|
|
449
|
+
const encWitness = (w, at) => clean({ terms: w.terms && pairs(w.terms, encE, `${at}.terms`), ghost: w.ghost && pairs(w.ghost, (g, a) => ({ start: encE(g.start, `${a}.start`), step: encE(g.step, `${a}.step`) }), `${at}.ghost`) });
|
|
450
|
+
const decWitness = (x, at) => {
|
|
451
|
+
const o = obj(x, at, [], ['terms', 'ghost']);
|
|
452
|
+
return clean({ terms: opt(o, 'terms', (y, a) => record(y, decE, a), at), ghost: opt(o, 'ghost', (y, a) => record(y, (g, b) => { const q = obj(g, b, ['start', 'step']); return { start: decE(q.start, `${b}.start`), step: decE(q.step, `${b}.step`) }; }, a), at) });
|
|
453
|
+
};
|
|
454
|
+
const encEx = (e, at) => clean({ vars: strs(e.vars, `${at}.vars`), body: encC(e.body, `${at}.body`), sub: e.sub && pairs(e.sub, encE, `${at}.sub`), ghost: e.ghost === undefined ? undefined : e.ghost === true ? true : fail(`${at}.ghost`, 'true') });
|
|
455
|
+
const decEx = (x, at) => {
|
|
456
|
+
const o = obj(x, at, ['vars', 'body'], ['sub', 'ghost']);
|
|
457
|
+
return clean({ vars: strs(o.vars, `${at}.vars`), body: decC(o.body, `${at}.body`), sub: opt(o, 'sub', (y, a) => record(y, decE, a), at), ghost: opt(o, 'ghost', (y, a) => (bool(y, a) ? true : fail(a, 'true')), at) });
|
|
458
|
+
};
|
|
459
|
+
export function encLaw(l, at = 'law') {
|
|
460
|
+
if (!isObj(l))
|
|
461
|
+
fail(at, 'a law');
|
|
462
|
+
for (const key of Object.keys(l))
|
|
463
|
+
if (!['name', 'holds', 'forall', 'keys', 'stated', 'some', 'witness', 'ex'].includes(key) && l[key] !== undefined)
|
|
464
|
+
fail(at, `no field ${key}`);
|
|
465
|
+
return clean({ name: str(l.name, `${at}.name`), holds: encC(l.holds, `${at}.holds`), forall: l.forall === undefined ? undefined : name(l.forall, `${at}.forall`), keys: l.keys && strs(l.keys, `${at}.keys`),
|
|
466
|
+
stated: l.stated && encC(l.stated, `${at}.stated`), some: l.some && strs(l.some, `${at}.some`), witness: l.witness && encWitness(l.witness, `${at}.witness`), ex: l.ex && encEx(l.ex, `${at}.ex`) });
|
|
467
|
+
}
|
|
468
|
+
export function decLaw(x, at = 'law') {
|
|
469
|
+
const o = obj(x, at, ['name', 'holds'], ['forall', 'keys', 'stated', 'some', 'witness', 'ex']);
|
|
470
|
+
return clean({ name: str(o.name, `${at}.name`), holds: decC(o.holds, `${at}.holds`), forall: opt(o, 'forall', name, at), keys: opt(o, 'keys', strs, at), stated: opt(o, 'stated', decC, at),
|
|
471
|
+
some: opt(o, 'some', strs, at), witness: opt(o, 'witness', decWitness, at), ex: opt(o, 'ex', decEx, at) });
|
|
472
|
+
}
|
|
473
|
+
const encLaws = (ls, at) => arr(ls, at).map((l, i) => encLaw(l, `${at}[${i}]`));
|
|
474
|
+
const decLaws = (x, at) => arr(x, at).map((l, i) => decLaw(l, `${at}[${i}]`));
|
|
475
|
+
const encWhen = (w, at) => (Array.isArray(w) ? w.map((c, i) => encC(c, `${at}[${i}]`)) : encC(w, at));
|
|
476
|
+
const decWhen = (x, at) => (Array.isArray(x) ? x.map((c, i) => decC(c, `${at}[${i}]`)) : decC(x, at));
|
|
477
|
+
const encStepLaw = (s, at) => clean({ name: str(s.name, `${at}.name`), when: s.when === undefined ? undefined : encWhen(s.when, `${at}.when`), then: encC(s.then, `${at}.then`),
|
|
478
|
+
forall: s.forall === undefined ? undefined : name(s.forall, `${at}.forall`), keys: s.keys && strs(s.keys, `${at}.keys`), steps: s.steps === undefined ? undefined : nat(s.steps, `${at}.steps`) });
|
|
479
|
+
const decStepLaw = (x, at) => {
|
|
480
|
+
const o = obj(x, at, ['name', 'then'], ['when', 'forall', 'keys', 'steps']);
|
|
481
|
+
return clean({ name: str(o.name, `${at}.name`), when: opt(o, 'when', decWhen, at), then: decC(o.then, `${at}.then`), forall: opt(o, 'forall', name, at), keys: opt(o, 'keys', strs, at), steps: opt(o, 'steps', nat, at) });
|
|
482
|
+
};
|
|
483
|
+
const encRank = (r, at) => (Array.isArray(r) ? r.map((e, i) => encE(e, `${at}[${i}]`)) : encE(r, at));
|
|
484
|
+
const decRank = (x, at) => (Array.isArray(x) ? x.map((e, i) => decE(e, `${at}[${i}]`)) : decE(x, at));
|
|
485
|
+
const encHelpful = (h, at) => clean({ when: h.when && encC(h.when, `${at}.when`), request: pairs(h.request, encE, `${at}.request`) });
|
|
486
|
+
const decHelpful = (x, at) => { const o = obj(x, at, ['request'], ['when']); return clean({ when: opt(o, 'when', decC, at), request: record(o.request, decE, `${at}.request`) }); };
|
|
487
|
+
const encLiveness = (l, at) => clean({ name: str(l.name, `${at}.name`), from: encC(l.from, `${at}.from`), to: encC(l.to, `${at}.to`), rank: encRank(l.rank, `${at}.rank`),
|
|
488
|
+
request: l.request && pairs(l.request, encE, `${at}.request`), helpful: l.helpful && l.helpful.map((h, i) => encHelpful(h, `${at}.helpful[${i}]`)) });
|
|
489
|
+
const decLiveness = (x, at) => {
|
|
490
|
+
const o = obj(x, at, ['name', 'from', 'to', 'rank'], ['request', 'helpful']);
|
|
491
|
+
return clean({ name: str(o.name, `${at}.name`), from: decC(o.from, `${at}.from`), to: decC(o.to, `${at}.to`), rank: decRank(o.rank, `${at}.rank`),
|
|
492
|
+
request: opt(o, 'request', (y, a) => record(y, decE, a), at), helpful: opt(o, 'helpful', (y, a) => arr(y, a).map((h, i) => decHelpful(h, `${a}[${i}]`)), at) });
|
|
493
|
+
};
|
|
494
|
+
const encLoop = (l, at) => clean({ name: str(l.name, `${at}.name`), params: strs(l.params, `${at}.params`), inputs: strs(l.inputs, `${at}.inputs`), state: pairs(l.state, encE, `${at}.state`),
|
|
495
|
+
while: encC(l.while, `${at}.while`), body: pairs(l.body, encE, `${at}.body`), pre: l.pre && encC(l.pre, `${at}.pre`), assume: l.assume && encC(l.assume, `${at}.assume`) });
|
|
496
|
+
const decLoop = (x, at) => {
|
|
497
|
+
const o = obj(x, at, ['name', 'params', 'inputs', 'state', 'while', 'body'], ['pre', 'assume']);
|
|
498
|
+
return clean({ name: str(o.name, `${at}.name`), params: strs(o.params, `${at}.params`), inputs: strs(o.inputs, `${at}.inputs`), state: record(o.state, decE, `${at}.state`),
|
|
499
|
+
while: decC(o.while, `${at}.while`), body: record(o.body, decE, `${at}.body`), pre: opt(o, 'pre', decC, at), assume: opt(o, 'assume', decC, at) });
|
|
500
|
+
};
|
|
501
|
+
const encFold = (f, at) => clean({ name: str(f.name, `${at}.name`), params: strs(f.params, `${at}.params`), inputs: strs(f.inputs, `${at}.inputs`), leaf: pairs(f.leaf, encE, `${at}.leaf`),
|
|
502
|
+
node: pairs(f.node, encE, `${at}.node`), pre: f.pre && encC(f.pre, `${at}.pre`), assume: f.assume && encC(f.assume, `${at}.assume`) });
|
|
503
|
+
const decFold = (x, at) => {
|
|
504
|
+
const o = obj(x, at, ['name', 'params', 'inputs', 'leaf', 'node'], ['pre', 'assume']);
|
|
505
|
+
return clean({ name: str(o.name, `${at}.name`), params: strs(o.params, `${at}.params`), inputs: strs(o.inputs, `${at}.inputs`), leaf: record(o.leaf, decE, `${at}.leaf`),
|
|
506
|
+
node: record(o.node, decE, `${at}.node`), pre: opt(o, 'pre', decC, at), assume: opt(o, 'assume', decC, at) });
|
|
507
|
+
};
|
|
508
|
+
const encQMachine = (m, at) => clean({ name: str(m.name, `${at}.name`), params: strs(m.params, `${at}.params`), inputs: strs(m.inputs, `${at}.inputs`), state: pairs(m.state, encQE, `${at}.state`),
|
|
509
|
+
step: pairs(m.step, encQE, `${at}.step`), pre: m.pre && encQC(m.pre, `${at}.pre`), assume: m.assume && encQC(m.assume, `${at}.assume`) });
|
|
510
|
+
const decQMachine = (x, at) => {
|
|
511
|
+
const o = obj(x, at, ['name', 'params', 'inputs', 'state', 'step'], ['pre', 'assume']);
|
|
512
|
+
return clean({ name: str(o.name, `${at}.name`), params: strs(o.params, `${at}.params`), inputs: strs(o.inputs, `${at}.inputs`), state: record(o.state, decQE, `${at}.state`),
|
|
513
|
+
step: record(o.step, decQE, `${at}.step`), pre: opt(o, 'pre', decQC, at), assume: opt(o, 'assume', decQC, at) });
|
|
514
|
+
};
|
|
515
|
+
// --- proofs ---------------------------------------------------------------------------------------------------------------
|
|
516
|
+
const encInst = (i, at) => pairs(i, (es, a) => arr(es, a).map((e, j) => encE(e, `${a}[${j}]`)), at);
|
|
517
|
+
const decInst = (x, at) => record(x, (es, a) => arr(es, a).map((e, j) => decE(e, `${a}[${j}]`)), at);
|
|
518
|
+
const encLawsProof = (p, cols, at) => ({ instances: pairs(p.instances, encInst, `${at}.instances`), trees: encTrees(p.trees, cols, `${at}.trees`) });
|
|
519
|
+
const decLawsProof = (x, cols, at) => { const o = obj(x, at, ['instances', 'trees']); return { instances: record(o.instances, decInst, `${at}.instances`), trees: decTrees(o.trees, cols, `${at}.trees`) }; };
|
|
520
|
+
const encStepProof = (p, cols, at) => clean({ laws: encLawsProof(p.laws, cols, `${at}.laws`), instances: encInst(p.instances, `${at}.instances`),
|
|
521
|
+
perStep: p.perStep && pairs(p.perStep, encInst, `${at}.perStep`), trees: encTrees(p.trees, cols, `${at}.trees`) });
|
|
522
|
+
const decStepProof = (x, cols, at) => {
|
|
523
|
+
const o = obj(x, at, ['laws', 'instances', 'trees'], ['perStep']);
|
|
524
|
+
return clean({ laws: decLawsProof(o.laws, cols, `${at}.laws`), instances: decInst(o.instances, `${at}.instances`), perStep: opt(o, 'perStep', (y, a) => record(y, decInst, a), at), trees: decTrees(o.trees, cols, `${at}.trees`) });
|
|
525
|
+
};
|
|
526
|
+
const encCyclic = (p, cols, at) => ({
|
|
527
|
+
facts: p.facts.map((f, i) => ({ name: str(f.name, `${at}.facts[${i}].name`), keys: strs(f.keys, `${at}.facts[${i}].keys`), holds: encC(f.holds, `${at}.facts[${i}].holds`) })),
|
|
528
|
+
nodes: p.nodes.map((n, i) => {
|
|
529
|
+
const a = `${at}.nodes[${i}]`;
|
|
530
|
+
return { name: str(n.name, `${a}.name`), hyps: n.hyps.map((h, j) => encC(h, `${a}.hyps[${j}]`)), concl: encC(n.concl, `${a}.concl`), rank: n.rank.map((e, j) => encE(e, `${a}.rank[${j}]`)),
|
|
531
|
+
insts: n.insts.map((t, j) => ({ fact: nat(t.fact, `${a}.insts[${j}].fact`), at: t.at.map((e, l) => encE(e, `${a}.insts[${j}].at[${l}]`)) })),
|
|
532
|
+
uses: n.uses.map((u, j) => ({ node: nat(u.node, `${a}.uses[${j}].node`), subst: pairs(u.subst, encE, `${a}.uses[${j}].subst`), guard: encC(u.guard, `${a}.uses[${j}].guard`),
|
|
533
|
+
premises: u.premises.map((t, l) => encTree(t, cols, `${a}.uses[${j}].premises[${l}]`)), descent: encTree(u.descent, cols, `${a}.uses[${j}].descent`) })),
|
|
534
|
+
main: encTree(n.main, cols, `${a}.main`) };
|
|
535
|
+
}),
|
|
536
|
+
});
|
|
537
|
+
const decCyclic = (x, cols, at) => {
|
|
538
|
+
const o = obj(x, at, ['facts', 'nodes']);
|
|
539
|
+
return {
|
|
540
|
+
facts: arr(o.facts, `${at}.facts`).map((f, i) => { const a = `${at}.facts[${i}]`, q = obj(f, a, ['name', 'keys', 'holds']); return { name: str(q.name, `${a}.name`), keys: strs(q.keys, `${a}.keys`), holds: decC(q.holds, `${a}.holds`) }; }),
|
|
541
|
+
nodes: arr(o.nodes, `${at}.nodes`).map((n, i) => {
|
|
542
|
+
const a = `${at}.nodes[${i}]`, q = obj(n, a, ['name', 'hyps', 'concl', 'rank', 'insts', 'uses', 'main']);
|
|
543
|
+
return { name: str(q.name, `${a}.name`), hyps: arr(q.hyps, `${a}.hyps`).map((h, j) => decC(h, `${a}.hyps[${j}]`)), concl: decC(q.concl, `${a}.concl`), rank: arr(q.rank, `${a}.rank`).map((e, j) => decE(e, `${a}.rank[${j}]`)),
|
|
544
|
+
insts: arr(q.insts, `${a}.insts`).map((t, j) => { const b = `${a}.insts[${j}]`, r = obj(t, b, ['fact', 'at']); return { fact: nat(r.fact, `${b}.fact`), at: arr(r.at, `${b}.at`).map((e, l) => decE(e, `${b}.at[${l}]`)) }; }),
|
|
545
|
+
uses: arr(q.uses, `${a}.uses`).map((u, j) => {
|
|
546
|
+
const b = `${a}.uses[${j}]`, r = obj(u, b, ['node', 'subst', 'guard', 'premises', 'descent']);
|
|
547
|
+
return { node: nat(r.node, `${b}.node`), subst: record(r.subst, decE, `${b}.subst`), guard: decC(r.guard, `${b}.guard`), premises: arr(r.premises, `${b}.premises`).map((t, l) => decTree(t, cols, `${b}.premises[${l}]`)), descent: decTree(r.descent, cols, `${b}.descent`) };
|
|
548
|
+
}),
|
|
549
|
+
main: decTree(q.main, cols, `${a}.main`) };
|
|
550
|
+
}),
|
|
551
|
+
};
|
|
552
|
+
};
|
|
553
|
+
const encRecur = (r, cols, at) => ({ start: pairs(r.start, nat, `${at}.start`), stem: r.stem.map((t, i) => pairs(t, nat, `${at}.stem[${i}]`)), set: encC(r.set, `${at}.set`),
|
|
554
|
+
wit: pairs(r.wit, encE, `${at}.wit`), tree: encTree(r.tree, cols, `${at}.tree`) });
|
|
555
|
+
const decRecur = (x, cols, at) => {
|
|
556
|
+
const o = obj(x, at, ['start', 'stem', 'set', 'wit', 'tree']);
|
|
557
|
+
return { start: record(o.start, nat, `${at}.start`), stem: arr(o.stem, `${at}.stem`).map((t, i) => record(t, nat, `${at}.stem[${i}]`)), set: decC(o.set, `${at}.set`), wit: record(o.wit, decE, `${at}.wit`), tree: decTree(o.tree, cols, `${at}.tree`) };
|
|
558
|
+
};
|
|
559
|
+
const encMeasure = (m, at) => ({ comps: arr(m.comps, `${at}.comps`).map((c, i) => { const q = c; return { coef: arr(q.coef, `${at}.comps[${i}].coef`).map((k, j) => int(k, `${at}.comps[${i}].coef[${j}]`)), c: int(q.c, `${at}.comps[${i}].c`) }; }) });
|
|
560
|
+
const decMeasure = (x, at) => {
|
|
561
|
+
const o = obj(x, at, ['comps']);
|
|
562
|
+
return { comps: arr(o.comps, `${at}.comps`).map((c, i) => { const q = obj(c, `${at}.comps[${i}]`, ['coef', 'c']); return { coef: arr(q.coef, `${at}.comps[${i}].coef`).map((k, j) => int(k, `${at}.comps[${i}].coef[${j}]`)), c: int(q.c, `${at}.comps[${i}].c`) }; }) };
|
|
563
|
+
};
|
|
564
|
+
// --- documents ---------------------------------------------------------------------------------------------------------------
|
|
565
|
+
// The fields each kind has, in the order a document is written.
|
|
566
|
+
const FIELDS = {
|
|
567
|
+
goals: { required: ['goals'] }, laws: { required: ['machine', 'laws', 'proof'] }, stepLaws: { required: ['machine', 'laws', 'stepLaws', 'proof'] },
|
|
568
|
+
kInduction: { required: ['machine', 'laws', 'kInduction', 'proof'] }, progress: { required: ['machine', 'laws', 'progress', 'instances', 'trees'], optional: ['lawsProof'] },
|
|
569
|
+
liveness: { required: ['machine', 'laws', 'liveness', 'proof'] }, loop: { required: ['loop', 'laws', 'rank', 'trees'] }, recur: { required: ['loop', 'recur'] },
|
|
570
|
+
fold: { required: ['fold', 'laws', 'trees'] }, cyclic: { required: ['cyclic'] }, qgoals: { required: ['goals'] }, qmachine: { required: ['machine', 'laws', 'trees'] },
|
|
571
|
+
termination: { required: ['functions', 'text', 'measures', 'obligations'] },
|
|
572
|
+
};
|
|
573
|
+
// The certificate as a document. `meta` is anything the producer wants kept beside it (the checker reads none of it).
|
|
574
|
+
export function toInterchange(c, meta) {
|
|
575
|
+
const cols = { terms: [], index: new Map() };
|
|
576
|
+
const body = (() => {
|
|
577
|
+
switch (c.kind) {
|
|
578
|
+
case 'goals': return { goals: c.goals.map((g, i) => ({ name: str(g.goal.name, `goals[${i}].name`), hyps: g.goal.hyps.map((h, j) => encC(h, `goals[${i}].hyps[${j}]`)), tree: encTree(g.tree, cols, `goals[${i}].tree`) })) };
|
|
579
|
+
case 'laws': return { machine: encMachine(c.machine), laws: encLaws(c.laws, 'laws'), proof: encLawsProof(c.proof, cols, 'proof') };
|
|
580
|
+
case 'stepLaws': return { machine: encMachine(c.machine), laws: encLaws(c.laws, 'laws'), stepLaws: c.stepLaws.map((s, i) => encStepLaw(s, `stepLaws[${i}]`)), proof: encStepProof(c.proof, cols, 'proof') };
|
|
581
|
+
case 'kInduction': return { machine: encMachine(c.machine), laws: encLaws(c.laws, 'laws'), kInduction: { law: encLaw(c.kInduction.law, 'kInduction.law'), k: nat(c.kInduction.k, 'kInduction.k') }, proof: encStepProof(c.proof, cols, 'proof') };
|
|
582
|
+
case 'progress': return { machine: encMachine(c.machine), laws: encLaws(c.laws, 'laws'),
|
|
583
|
+
progress: { name: str(c.progress.name, 'progress.name'), when: encC(c.progress.when, 'progress.when'), then: encC(c.progress.then, 'progress.then'), request: pairs(c.progress.request, encE, 'progress.request') },
|
|
584
|
+
instances: encInst(c.instances, 'instances'), trees: encTrees(c.trees, cols, 'trees'), lawsProof: c.lawsProof && encLawsProof(c.lawsProof, cols, 'lawsProof') };
|
|
585
|
+
case 'liveness': return { machine: encMachine(c.machine), laws: encLaws(c.laws, 'laws'), liveness: encLiveness(c.liveness, 'liveness'), proof: encStepProof(c.proof, cols, 'proof') };
|
|
586
|
+
case 'loop': return { loop: encLoop(c.loop, 'loop'), laws: encLaws(c.laws, 'laws'), rank: encRank(c.rank, 'rank'), trees: encTrees(c.trees, cols, 'trees') };
|
|
587
|
+
case 'recur': return { loop: encLoop(c.loop, 'loop'), recur: encRecur(c.recur, cols, 'recur') };
|
|
588
|
+
case 'fold': return { fold: encFold(c.fold, 'fold'), laws: encLaws(c.laws, 'laws'), trees: encTrees(c.trees, cols, 'trees') };
|
|
589
|
+
case 'cyclic': return { cyclic: encCyclic(c.cyclic, cols, 'cyclic') };
|
|
590
|
+
case 'qgoals': return { goals: c.goals.map((g, i) => ({ name: str(g.goal.name, `goals[${i}].name`), hyps: g.goal.hyps.map((h, j) => encQC(h, `goals[${i}].hyps[${j}]`)), tree: encTree(g.tree, cols, `goals[${i}].tree`) })) };
|
|
591
|
+
case 'qmachine': return { machine: encQMachine(c.machine, 'machine'), laws: c.laws.map((l, i) => ({ name: str(l.name, `laws[${i}].name`), holds: encQC(l.holds, `laws[${i}].holds`) })),
|
|
592
|
+
trees: Object.entries(c.trees).map(([n, t]) => [str(n, 'trees'), { start: encTree(t.start, cols, `trees.${n}.start`), step: encTree(t.step, cols, `trees.${n}.step`) }]) };
|
|
593
|
+
case 'termination': return { functions: strs(c.functions, 'functions'), text: str(c.text, 'text'), measures: c.measures.map((m, i) => encMeasure(m, `measures[${i}]`)),
|
|
594
|
+
obligations: c.obligations.map((o, i) => ({ function: name(o.function, `obligations[${i}].function`), law: str(o.law, `obligations[${i}].law`), cyclic: encCyclic(o.cyclic, cols, `obligations[${i}].cyclic`) })) };
|
|
595
|
+
default: return fail('kind', `no kind ${JSON.stringify(c.kind)}`);
|
|
596
|
+
}
|
|
597
|
+
})();
|
|
598
|
+
return clean({ format: FORMAT, version: VERSION, kind: c.kind, meta, columns: 'terms' in cols && cols.terms.length ? cols.terms : undefined, ...body });
|
|
599
|
+
}
|
|
600
|
+
// A document read back into the kernel's objects, or a FormatError saying where it leaves the format.
|
|
601
|
+
export function fromInterchange(doc) {
|
|
602
|
+
const head = isObj(doc) ? doc : fail('document', 'an object');
|
|
603
|
+
if (head.format !== FORMAT)
|
|
604
|
+
fail('format', `${JSON.stringify(FORMAT)}`);
|
|
605
|
+
if (head.version !== VERSION)
|
|
606
|
+
fail('version', `${VERSION} (this reader's; a 1.0 reader reads 1.0 alone)`);
|
|
607
|
+
const kind = head.kind;
|
|
608
|
+
if (!KINDS.includes(kind))
|
|
609
|
+
fail('kind', `one of ${KINDS.join(', ')}`);
|
|
610
|
+
const o = obj(head, 'document', ['format', 'version', 'kind', ...FIELDS[kind].required], ['meta', 'columns', ...(FIELDS[kind].optional ?? [])]);
|
|
611
|
+
const cols = { read: opt(o, 'columns', (x, a) => arr(x, a).map((t, i) => decE(t, `${a}[${i}]`)), 'document') ?? [] };
|
|
612
|
+
switch (kind) {
|
|
613
|
+
case 'goals': return { kind, goals: arr(o.goals, 'goals').map((g, i) => { const q = obj(g, `goals[${i}]`, ['name', 'hyps', 'tree']); return { goal: { name: str(q.name, `goals[${i}].name`), hyps: arr(q.hyps, `goals[${i}].hyps`).map((h, j) => decC(h, `goals[${i}].hyps[${j}]`)) }, tree: decTree(q.tree, cols, `goals[${i}].tree`) }; }) };
|
|
614
|
+
case 'laws': return { kind, machine: decMachine(o.machine), laws: decLaws(o.laws, 'laws'), proof: decLawsProof(o.proof, cols, 'proof') };
|
|
615
|
+
case 'stepLaws': return { kind, machine: decMachine(o.machine), laws: decLaws(o.laws, 'laws'), stepLaws: arr(o.stepLaws, 'stepLaws').map((s, i) => decStepLaw(s, `stepLaws[${i}]`)), proof: decStepProof(o.proof, cols, 'proof') };
|
|
616
|
+
case 'kInduction': {
|
|
617
|
+
const q = obj(o.kInduction, 'kInduction', ['law', 'k']);
|
|
618
|
+
return { kind, machine: decMachine(o.machine), laws: decLaws(o.laws, 'laws'), kInduction: { law: decLaw(q.law, 'kInduction.law'), k: nat(q.k, 'kInduction.k') }, proof: decStepProof(o.proof, cols, 'proof') };
|
|
619
|
+
}
|
|
620
|
+
case 'progress': {
|
|
621
|
+
const q = obj(o.progress, 'progress', ['name', 'when', 'then', 'request']);
|
|
622
|
+
return clean({ kind, machine: decMachine(o.machine), laws: decLaws(o.laws, 'laws'), progress: { name: str(q.name, 'progress.name'), when: decC(q.when, 'progress.when'), then: decC(q.then, 'progress.then'), request: record(q.request, decE, 'progress.request') },
|
|
623
|
+
instances: decInst(o.instances, 'instances'), trees: decTrees(o.trees, cols, 'trees'), lawsProof: opt(o, 'lawsProof', (x, a) => decLawsProof(x, cols, a), 'document') });
|
|
624
|
+
}
|
|
625
|
+
case 'liveness': return { kind, machine: decMachine(o.machine), laws: decLaws(o.laws, 'laws'), liveness: decLiveness(o.liveness, 'liveness'), proof: decStepProof(o.proof, cols, 'proof') };
|
|
626
|
+
case 'loop': return { kind, loop: decLoop(o.loop, 'loop'), laws: decLaws(o.laws, 'laws'), rank: decRank(o.rank, 'rank'), trees: decTrees(o.trees, cols, 'trees') };
|
|
627
|
+
case 'recur': return { kind, loop: decLoop(o.loop, 'loop'), recur: decRecur(o.recur, cols, 'recur') };
|
|
628
|
+
case 'fold': return { kind, fold: decFold(o.fold, 'fold'), laws: decLaws(o.laws, 'laws'), trees: decTrees(o.trees, cols, 'trees') };
|
|
629
|
+
case 'cyclic': return { kind, cyclic: decCyclic(o.cyclic, cols, 'cyclic') };
|
|
630
|
+
case 'qgoals': return { kind, goals: arr(o.goals, 'goals').map((g, i) => { const q = obj(g, `goals[${i}]`, ['name', 'hyps', 'tree']); return { goal: { name: str(q.name, `goals[${i}].name`), hyps: arr(q.hyps, `goals[${i}].hyps`).map((h, j) => decQC(h, `goals[${i}].hyps[${j}]`)) }, tree: decTree(q.tree, cols, `goals[${i}].tree`) }; }) };
|
|
631
|
+
case 'qmachine': {
|
|
632
|
+
const trees = {};
|
|
633
|
+
for (const [i, p] of arr(o.trees, 'trees').entries()) {
|
|
634
|
+
const pr = arr(p, `trees[${i}]`);
|
|
635
|
+
if (pr.length !== 2)
|
|
636
|
+
fail(`trees[${i}]`, 'a pair [law, {start, step}]');
|
|
637
|
+
const n = str(pr[0], `trees[${i}][0]`), q = obj(pr[1], `trees.${n}`, ['start', 'step']);
|
|
638
|
+
if (Object.hasOwn(trees, n) || n === '__proto__')
|
|
639
|
+
fail(`trees[${i}]`, `the law ${n} twice`);
|
|
640
|
+
trees[n] = { start: decTree(q.start, cols, `trees.${n}.start`), step: decTree(q.step, cols, `trees.${n}.step`) };
|
|
641
|
+
}
|
|
642
|
+
return { kind, machine: decQMachine(o.machine, 'machine'), laws: arr(o.laws, 'laws').map((l, i) => { const q = obj(l, `laws[${i}]`, ['name', 'holds']); return { name: str(q.name, `laws[${i}].name`), holds: decQC(q.holds, `laws[${i}].holds`) }; }), trees };
|
|
643
|
+
}
|
|
644
|
+
case 'termination': return { kind, functions: strs(o.functions, 'functions'), text: str(o.text, 'text'), measures: arr(o.measures, 'measures').map((m, i) => decMeasure(m, `measures[${i}]`)),
|
|
645
|
+
obligations: arr(o.obligations, 'obligations').map((x, i) => { const q = obj(x, `obligations[${i}]`, ['function', 'law', 'cyclic']); return { function: name(q.function, `obligations[${i}].function`), law: str(q.law, `obligations[${i}].law`), cyclic: decCyclic(q.cyclic, cols, `obligations[${i}].cyclic`) }; }) };
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
// The document with each column reference replaced by its term: two documents mean the same certificate exactly when
|
|
649
|
+
// these agree (field order inside an object aside), whatever order their columns are numbered in.
|
|
650
|
+
export function resolved(doc) {
|
|
651
|
+
const cols = (doc.columns ?? []);
|
|
652
|
+
const walk = (x) => {
|
|
653
|
+
if (x === undefined)
|
|
654
|
+
return null;
|
|
655
|
+
if (Array.isArray(x))
|
|
656
|
+
return x.map(walk);
|
|
657
|
+
if (!isObj(x))
|
|
658
|
+
return x;
|
|
659
|
+
if (Object.keys(x).length === 1 && 'col' in x && typeof x.col === 'number')
|
|
660
|
+
return { col: cols[x.col] ?? null };
|
|
661
|
+
return Object.fromEntries(Object.entries(x).map(([k, v]) => [k, walk(v)]));
|
|
662
|
+
};
|
|
663
|
+
const { columns: _, ...rest } = doc;
|
|
664
|
+
void _;
|
|
665
|
+
return walk(rest);
|
|
666
|
+
}
|
|
667
|
+
// Structural equality of JSON values, object fields in any order.
|
|
668
|
+
export function sameJson(a, b) {
|
|
669
|
+
if (Array.isArray(a))
|
|
670
|
+
return Array.isArray(b) && a.length === b.length && a.every((x, i) => sameJson(x, b[i]));
|
|
671
|
+
if (isObj(a)) {
|
|
672
|
+
if (!isObj(b))
|
|
673
|
+
return false;
|
|
674
|
+
const ka = Object.keys(a).filter(k => a[k] !== undefined), kb = Object.keys(b).filter(k => b[k] !== undefined);
|
|
675
|
+
return ka.length === kb.length && ka.every(k => Object.hasOwn(b, k) && sameJson(a[k], b[k]));
|
|
676
|
+
}
|
|
677
|
+
return a === b;
|
|
678
|
+
}
|