gdp-wired 0.23.0 → 0.25.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 (47) hide show
  1. package/README.md +12 -2
  2. package/dist/core/accel.d.ts +44 -0
  3. package/dist/core/accel.js +720 -0
  4. package/dist/core/bmc.d.ts +13 -2
  5. package/dist/core/bmc.js +390 -18
  6. package/dist/core/cycles.d.ts +23 -0
  7. package/dist/core/cycles.js +527 -146
  8. package/dist/core/data.d.ts +17 -0
  9. package/dist/core/data.js +1177 -213
  10. package/dist/core/discover.js +8 -3
  11. package/dist/core/dsl.d.ts +7 -2
  12. package/dist/core/dsl.js +47 -9
  13. package/dist/core/extract-check.d.ts +77 -0
  14. package/dist/core/extract-check.js +785 -0
  15. package/dist/core/extract-ir.d.ts +139 -0
  16. package/dist/core/extract-ir.js +198 -0
  17. package/dist/core/extract-print.d.ts +30 -0
  18. package/dist/core/extract-print.js +426 -0
  19. package/dist/core/extract-read.d.ts +298 -0
  20. package/dist/core/extract-read.js +1298 -0
  21. package/dist/core/extract-shapes.d.ts +13 -0
  22. package/dist/core/extract-shapes.js +993 -0
  23. package/dist/core/extract-xstate.d.ts +34 -0
  24. package/dist/core/extract-xstate.js +320 -0
  25. package/dist/core/extract.d.ts +31 -0
  26. package/dist/core/extract.js +249 -0
  27. package/dist/core/infer.d.ts +1 -0
  28. package/dist/core/infer.js +298 -28
  29. package/dist/core/kind.d.ts +30 -0
  30. package/dist/core/kind.js +81 -0
  31. package/dist/core/nonterm.d.ts +4 -1
  32. package/dist/core/nonterm.js +98 -2
  33. package/dist/core/relation.js +82 -7
  34. package/dist/core/transfer.js +1 -1
  35. package/dist/core/xstate.d.ts +6 -0
  36. package/dist/core/xstate.js +70 -12
  37. package/dist/kernel/cdcl.d.ts +15 -0
  38. package/dist/kernel/cdcl.js +331 -244
  39. package/dist/kernel/kernel.d.ts +15 -0
  40. package/dist/kernel/kernel.js +32 -0
  41. package/dist/kernel/prove.js +20 -7
  42. package/dist/kernel/reader.d.ts +14 -0
  43. package/dist/kernel/reader.js +102 -14
  44. package/dist/lean-kernel/emit.d.ts +28 -1
  45. package/dist/lean-kernel/emit.js +56 -15
  46. package/dist/lean-kernel/library.js +2 -1
  47. package/package.json +12 -3
@@ -0,0 +1,426 @@
1
+ // From a Model (core/extract-shapes.ts) to a rule's source: which numbers are naturals, and the builder calls that make
2
+ // the rule, as text a user can read and keep. The rule core/extract.ts hands out is this text evaluated, so the source
3
+ // and the rule checked against the code are the same thing.
4
+ //
5
+ // Numbers: a variable is a natural where the code keeps it one: a param or input the code refuses below 0 (or given
6
+ // as one), a state variable that starts at 0 or more and is never written below (each subtraction proven at most what it
7
+ // subtracts from, by the library's own linear arithmetic, solver.ts, under the branch's conditions and the code's
8
+ // refusals); every other is an integer (two naturals, Int). A natural's a − b is printed as `a.minus(b)` (truncated)
9
+ // only where a ≥ b is proven, so it is the code's own subtraction.
10
+ import * as I from "./extract-ir.js";
11
+ import { Refusal } from "./extract-read.js";
12
+ import { add, k as lk, le, lt, scale, solve, v as lv } from "./solver.js";
13
+ const MAX_CASES = 64;
14
+ let atomIds = 0;
15
+ const atomNames = new Map();
16
+ const atom = (x, prefix) => {
17
+ const key = I.keyOf(x);
18
+ let n = atomNames.get(key);
19
+ if (!n) {
20
+ n = `${prefix}$${++atomIds}`;
21
+ atomNames.set(key, n);
22
+ }
23
+ return n;
24
+ };
25
+ function cases(e, ctx) {
26
+ const cross = (xs, ys, f, extra = () => []) => {
27
+ if (!xs || !ys || xs.length * ys.length > MAX_CASES)
28
+ return undefined;
29
+ return xs.flatMap(a => ys.map(b => ({ lin: f(a.lin, b.lin), cons: [...a.cons, ...b.cons, ...extra(a.lin, b.lin)] })));
30
+ };
31
+ switch (e.op) {
32
+ case 'var': return [{ lin: lv(e.name), cons: ctx.sorts.get(e.name) === 'nat' ? le(lk(0), lv(e.name)) : [] }];
33
+ case 'const': return [{ lin: lk(e.v), cons: [] }];
34
+ case 'add': return cross(cases(e.a, ctx), cases(e.b, ctx), add);
35
+ case 'sub': return cross(cases(e.a, ctx), cases(e.b, ctx), (a, b) => add(a, scale(b, -1)));
36
+ case 'neg': return cases(e.a, ctx)?.map(c => ({ lin: scale(c.lin, -1), cons: c.cons }));
37
+ case 'mulk': return cases(e.a, ctx)?.map(c => ({ lin: scale(c.lin, e.k), cons: c.cons }));
38
+ case 'min':
39
+ case 'max': {
40
+ const lo = e.op === 'min';
41
+ const a = cross(cases(e.a, ctx), cases(e.b, ctx), x => x, (x, y) => (lo ? le(x, y) : le(y, x)));
42
+ const b = cross(cases(e.a, ctx), cases(e.b, ctx), (_, y) => y, (x, y) => (lo ? lt(y, x) : lt(x, y)));
43
+ return a && b && a.length + b.length <= MAX_CASES ? [...a, ...b] : undefined;
44
+ }
45
+ case 'ite': {
46
+ const ca = cases(e.a, ctx), cb = cases(e.b, ctx), pc = linCond(e.c, ctx), nc = linCond(I.not(e.c), ctx);
47
+ if (!ca || !cb || ca.length + cb.length > MAX_CASES)
48
+ return undefined;
49
+ return [...ca.map(c => ({ lin: c.lin, cons: [...c.cons, ...pc] })), ...cb.map(c => ({ lin: c.lin, cons: [...c.cons, ...nc] }))];
50
+ }
51
+ case 'divk': {
52
+ const t = atom(e, 'div');
53
+ return cases(e.a, ctx)?.map(c => ({ lin: lv(t), cons: [...c.cons, ...le(scale(lv(t), e.k), c.lin), ...le(c.lin, add(scale(lv(t), e.k), lk(e.k - 1)))] }));
54
+ }
55
+ case 'modk': {
56
+ const t = atom(e, 'mod');
57
+ return [{ lin: lv(t), cons: [...le(lk(0), lv(t)), ...le(lv(t), lk(e.k - 1))] }];
58
+ }
59
+ case 'mul': {
60
+ const t = atom(e, 'mul');
61
+ return [{ lin: lv(t), cons: natOK(e.a, ctx) && natOK(e.b, ctx) ? le(lk(0), lv(t)) : [] }];
62
+ }
63
+ case 'cell': {
64
+ const t = atom(e, 'cell');
65
+ return [{ lin: lv(t), cons: ctx.sorts.get(`${e.map}.${e.field}`) === 'nat' ? le(lk(0), lv(t)) : [] }];
66
+ }
67
+ }
68
+ }
69
+ // A condition's linear facts (none where it isn't a conjunction of comparisons of single cases).
70
+ function linCond(c, ctx) {
71
+ if (c.op === 'and')
72
+ return c.xs.flatMap(x => linCond(x, ctx));
73
+ if (c.op === 'not' && c.x.op === 'or')
74
+ return c.x.xs.flatMap(x => linCond(I.not(x), ctx));
75
+ if (c.op === 'not' && c.x.op === 'not')
76
+ return linCond(c.x.x, ctx);
77
+ if (c.op === 'cmp' || (c.op === 'not' && c.x.op === 'cmp')) {
78
+ const x = c.op === 'cmp' ? c : c.x, neg = c.op === 'not';
79
+ const a = cases(x.a, ctx), b = cases(x.b, ctx);
80
+ if (!a || !b || a.length !== 1 || b.length !== 1)
81
+ return [];
82
+ const [p, q] = [a[0], b[0]], base = [...p.cons, ...q.cons];
83
+ if (!neg)
84
+ return [...base, ...(x.rel === '<' ? lt(p.lin, q.lin) : x.rel === '<=' ? le(p.lin, q.lin) : [...le(p.lin, q.lin), ...le(q.lin, p.lin)])];
85
+ if (x.rel === '<')
86
+ return [...base, ...le(q.lin, p.lin)];
87
+ if (x.rel === '<=')
88
+ return [...base, ...lt(q.lin, p.lin)];
89
+ return base;
90
+ }
91
+ return [];
92
+ }
93
+ // The facts at a place: the branch's conditions, and what the code's refusals rule out there (a refusal whose other
94
+ // conditions all hold here can't hold, or the step would be refused).
95
+ function factsAt(ctx) {
96
+ const out = ctx.atoms.flatMap(a => linCond(a, ctx));
97
+ const known = (x) => ctx.atoms.some(a => I.same(a, x) || (a.op === 'and' && a.xs.some(y => I.same(y, x))));
98
+ const refusals = ctx.m.refuse.op === 'or' ? ctx.m.refuse.xs : [ctx.m.refuse];
99
+ for (const r of refusals) {
100
+ const parts = r.op === 'and' ? r.xs : [r], open = parts.filter(x => !known(x));
101
+ if (open.length === 1)
102
+ out.push(...linCond(I.not(open[0]), ctx));
103
+ }
104
+ out.push(...linCond(ctx.m.pre, ctx));
105
+ return out;
106
+ }
107
+ const refutes = (cons) => solve(cons).kind === 'refuted';
108
+ // A term linear over atoms: each part that isn't linear (min, max, by cases, a division, a product, a map's cell) one
109
+ // atom by its structure, so a fact about a part and the term that reads it name the same atom.
110
+ function atomic(e, ctx, side) {
111
+ switch (e.op) {
112
+ case 'var':
113
+ if (ctx.sorts.get(e.name) === 'nat')
114
+ side.push(...le(lk(0), lv(e.name)));
115
+ return lv(e.name);
116
+ case 'const': return lk(e.v);
117
+ case 'add': return add(atomic(e.a, ctx, side), atomic(e.b, ctx, side));
118
+ case 'sub': return add(atomic(e.a, ctx, side), scale(atomic(e.b, ctx, side), -1));
119
+ case 'neg': return scale(atomic(e.a, ctx, side), -1);
120
+ case 'mulk': return scale(atomic(e.a, ctx, side), e.k);
121
+ case 'min':
122
+ case 'max':
123
+ case 'ite':
124
+ case 'divk':
125
+ case 'modk':
126
+ case 'mul':
127
+ case 'cell': {
128
+ const t = atom(e, 'atom');
129
+ if (natOK(e, { ...ctx, atoms: [] }))
130
+ side.push(...le(lk(0), lv(t)));
131
+ return lv(t);
132
+ }
133
+ }
134
+ }
135
+ function atomicCond(c, ctx, side) {
136
+ if (c.op === 'and')
137
+ return c.xs.flatMap(x => atomicCond(x, ctx, side));
138
+ if (c.op === 'not' && c.x.op === 'or')
139
+ return c.x.xs.flatMap(x => atomicCond(I.not(x), ctx, side));
140
+ if (c.op === 'not' && c.x.op === 'not')
141
+ return atomicCond(c.x.x, ctx, side);
142
+ const x = c.op === 'cmp' ? c : c.op === 'not' && c.x.op === 'cmp' ? c.x : undefined;
143
+ if (!x)
144
+ return [];
145
+ const a = atomic(x.a, ctx, side), b = atomic(x.b, ctx, side);
146
+ if (c.op === 'cmp')
147
+ return x.rel === '<' ? lt(a, b) : x.rel === '<=' ? le(a, b) : [...le(a, b), ...le(b, a)];
148
+ return x.rel === '<' ? le(b, a) : x.rel === '<=' ? lt(b, a) : [];
149
+ }
150
+ function atomicFacts(ctx, side) {
151
+ const out = ctx.atoms.flatMap(a => atomicCond(a, ctx, side));
152
+ const known = (x) => ctx.atoms.some(a => I.same(a, x) || (a.op === 'and' && a.xs.some(y => I.same(y, x))));
153
+ for (const r of ctx.m.refuse.op === 'or' ? ctx.m.refuse.xs : [ctx.m.refuse]) {
154
+ const parts = r.op === 'and' ? r.xs : [r], open = parts.filter(x => !known(x));
155
+ if (open.length === 1)
156
+ out.push(...atomicCond(I.not(open[0]), ctx, side));
157
+ }
158
+ out.push(...atomicCond(ctx.m.pre, ctx, side));
159
+ return out;
160
+ }
161
+ export function nonneg(e, ctx) {
162
+ const side = [], lin = atomic(e, ctx, side), afacts = atomicFacts(ctx, side);
163
+ if (refutes([...side, ...afacts, ...lt(lin, lk(0))]))
164
+ return true;
165
+ const cs = cases(e, ctx);
166
+ if (!cs)
167
+ return false;
168
+ const facts = factsAt(ctx);
169
+ return cs.every(c => refutes([...facts, ...c.cons, ...lt(c.lin, lk(0))]));
170
+ }
171
+ const withAtom = (ctx, c) => ({ ...ctx, atoms: [...ctx.atoms, c] });
172
+ // A condition the place is already under (or under its negation): the branch it takes.
173
+ export const decided = (c, ctx) => (ctx.atoms.some(a => I.same(a, c)) ? true : ctx.atoms.some(a => I.same(a, I.not(c)) || I.same(I.not(a), c)) ? false : undefined);
174
+ // Whether a term is a natural at this place, every part of it too (so it prints over naturals).
175
+ export function natOK(e, ctx) {
176
+ switch (e.op) {
177
+ case 'var': return ctx.sorts.get(e.name) === 'nat';
178
+ case 'const': return e.v >= 0;
179
+ case 'add':
180
+ case 'mul':
181
+ case 'min': return natOK(e.a, ctx) && natOK(e.b, ctx);
182
+ case 'max': return natOK(e.a, ctx) || natOK(e.b, ctx);
183
+ case 'sub': return natOK(e.a, ctx) && natOK(e.b, ctx) && nonneg(e, ctx);
184
+ case 'neg': return false;
185
+ case 'mulk': return e.k >= 0 && natOK(e.a, ctx);
186
+ case 'divk': return natOK(e.a, ctx);
187
+ case 'modk': return true;
188
+ case 'ite': {
189
+ const d = decided(e.c, ctx);
190
+ if (d !== undefined)
191
+ return natOK(d ? e.a : e.b, ctx);
192
+ return natOK(e.a, withAtom(ctx, e.c)) && natOK(e.b, withAtom(ctx, I.not(e.c)));
193
+ }
194
+ case 'cell': return ctx.sorts.get(`${e.map}.${e.field}`) === 'nat';
195
+ }
196
+ }
197
+ // Every number's sort: given ones kept; a param or input a natural if the code refuses it below 0, the time a natural;
198
+ // a state variable or a map's numbers naturals until a start or a write may be below 0.
199
+ export function sortsOf(m, given = {}) {
200
+ const sorts = new Map(), notes = [];
201
+ const nums = (vs) => vs.filter(v => v.sort.k === 'num');
202
+ const ctx0 = () => ({ m, sorts, atoms: [] });
203
+ for (const v of [...nums(m.params), ...nums(m.inputs)]) {
204
+ if (given[v.name]) {
205
+ sorts.set(v.name, given[v.name]);
206
+ notes.push(`${v.name}: ${given[v.name] === 'nat' ? 'a natural' : 'an integer'}, as given`);
207
+ continue;
208
+ }
209
+ if (/^now(0)?(_\d+)?$/.test(v.name) && v.note?.startsWith('Date.now()')) {
210
+ sorts.set(v.name, 'nat');
211
+ continue;
212
+ }
213
+ // Refused below 0: no case of the step (or no params, for a param) takes it negative.
214
+ const facts = v.role === 'param' ? linCond(m.pre, ctx0()) : [];
215
+ const refused = v.role === 'param' ? refutes([...facts, ...lt(lv(v.name), lk(0))]) : refusedBelowZero(m, v.name, ctx0());
216
+ sorts.set(v.name, refused ? 'nat' : 'int');
217
+ notes.push(refused ? `${v.name}: a natural (the code refuses it below 0)` : `${v.name}: an integer (the code takes any whole number)`);
218
+ }
219
+ const cellNums = m.maps.flatMap(d => d.fields.filter(f => f.sort.k === 'num').map(f => `${d.name}.${f.name}`));
220
+ for (const v of nums(m.state))
221
+ sorts.set(v.name, given[v.name] ?? 'nat');
222
+ for (const c of cellNums)
223
+ sorts.set(c, given[c] ?? 'nat');
224
+ for (let changed = true; changed;) {
225
+ changed = false;
226
+ for (const v of nums(m.state)) {
227
+ if (sorts.get(v.name) === 'int' || given[v.name])
228
+ continue;
229
+ const init = m.init[v.name], w = m.writes[v.name];
230
+ if ((init && init.s === 'num' && !natOK(init.e, ctx0())) || (w && w.s === 'num' && !natOK(w.e, ctx0()))) {
231
+ sorts.set(v.name, 'int');
232
+ changed = true;
233
+ }
234
+ }
235
+ for (const d of m.maps)
236
+ for (const f of d.fields) {
237
+ const n = `${d.name}.${f.name}`, w = m.mapWrites[d.name]?.vals[f.name];
238
+ if (f.sort.k !== 'num' || sorts.get(n) === 'int' || given[n])
239
+ continue;
240
+ if (w && w.s === 'num' && !natOK(w.e, withAtom(ctx0(), m.mapWrites[d.name].has))) {
241
+ sorts.set(n, 'int');
242
+ changed = true;
243
+ }
244
+ }
245
+ }
246
+ for (const v of nums(m.state))
247
+ if (sorts.get(v.name) === 'int')
248
+ notes.push(`${v.name}: an integer (a step may take it below 0, or it starts there)`);
249
+ return { sorts, notes };
250
+ }
251
+ // x < 0 refused: for every event whose request brings x, a refusal of that event (its other conditions only the event's
252
+ // choice) whose condition holds wherever x < 0. So no step the rule takes reads a negative x.
253
+ function refusedBelowZero(m, x, ctx) {
254
+ const ds = m.refuse.op === 'or' ? m.refuse.xs : [m.refuse];
255
+ const mentions = (a) => (a.k === 'input' ? a.name === x : a.k === 'rec' ? a.fields.some(([, y]) => mentions(y)) : false);
256
+ const events = m.events.filter(e => e.args.some(mentions));
257
+ if (!events.length)
258
+ return false;
259
+ return events.every(e => ds.some(d => {
260
+ const parts = d.op === 'and' ? d.xs : [d];
261
+ const sel = parts.filter(p => p.op === 'is' && p.l.op === 'var'), own = parts.filter(p => !sel.includes(p));
262
+ if (!sel.every(p => p.op === 'is' && p.l.op === 'var' && e.select[p.l.name] === p.value) || own.length !== 1)
263
+ return false;
264
+ return refutes([...linCond(I.not(own[0]), ctx), ...lt(lv(x), lk(0))]);
265
+ }));
266
+ }
267
+ const q = (s) => `'${s.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
268
+ function pn(e, P, ctx, mode, recv = false) {
269
+ const h = P.hoisted.get(I.keyOf(e));
270
+ if (h)
271
+ return mode === 'int' && recv && natOK(e, { ...ctx, atoms: [] }) ? `Int.of(${h})` : h;
272
+ const nat = mode === 'nat';
273
+ const R = (x, c = ctx) => pn(x, P, c, nat ? 'nat' : 'int', true), A = (x, c = ctx) => pn(x, P, c, nat || natOK(x, c) ? 'nat' : 'int');
274
+ switch (e.op) {
275
+ case 'var': return !nat && recv && P.sorts.get(e.name) === 'nat' ? `Int.of(${e.name})` : e.name;
276
+ case 'const': return recv ? (nat ? `lit(${e.v})` : `Int.of(${e.v})`) : String(e.v);
277
+ case 'add': return e.a.op === 'const' ? `${R(e.b)}.plus(${A(e.a)})` : `${R(e.a)}.plus(${A(e.b)})`;
278
+ case 'sub': return `${R(e.a)}.minus(${A(e.b)})`;
279
+ case 'neg': return `${pn(e.a, P, ctx, 'int', true)}.negate()`;
280
+ case 'mulk': return `${R(e.a)}.times(${e.k})`;
281
+ case 'mul':
282
+ if (!(natOK(e.a, ctx) && natOK(e.b, ctx)))
283
+ throw new Refusal(P.m.file, 'a product of two numbers that may be negative (Int multiplies only by constants)');
284
+ return `${pn(e.a, P, ctx, 'nat', true)}.mul(${pn(e.b, P, ctx, 'nat')})`;
285
+ case 'min': return nat ? `min(${A(e.a)}, ${A(e.b)})` : `${R(e.a)}.min(${A(e.b)})`;
286
+ case 'max':
287
+ if (nat) {
288
+ const side = (x) => (natOK(x, ctx) ? pn(x, P, ctx, 'nat') : `${pn(x, P, ctx, 'int', true)}.clamped`);
289
+ // max(0, x) is x where x is at least 0: Int's clamped.
290
+ if (e.a.op === 'const' && e.a.v === 0)
291
+ return side(e.b);
292
+ if (e.b.op === 'const' && e.b.v === 0)
293
+ return side(e.a);
294
+ return `max(${side(e.a)}, ${side(e.b)})`;
295
+ }
296
+ return `${R(e.a)}.max(${A(e.b)})`;
297
+ case 'divk': return `${R(e.a)}.divBy(${e.k})`;
298
+ case 'modk':
299
+ // JavaScript's % takes the dividend's sign (-7 % 3 is -1); modBy is the remainder at least 0 (2): the same only
300
+ // where the dividend is at least 0.
301
+ if (!natOK(e.a, ctx) && !nonneg(e.a, ctx))
302
+ throw new Refusal(P.m.file, 'x % k where x may be negative (JavaScript\'s remainder takes the sign of x)');
303
+ return `${pn(e.a, P, ctx, natOK(e.a, ctx) ? 'nat' : 'int', true)}.modBy(${e.k})`;
304
+ case 'ite': {
305
+ const d = decided(e.c, ctx);
306
+ if (d !== undefined)
307
+ return pn(d ? e.a : e.b, P, ctx, mode, recv);
308
+ const ca = withAtom(ctx, e.c), cb = withAtom(ctx, I.not(e.c));
309
+ const side = (x, c) => pn(x, P, c, nat ? 'nat' : natOK(x, c) ? 'nat' : 'int');
310
+ return `when(${pb(e.c, P, ctx)}, ${side(e.a, ca)}, ${side(e.b, cb)})`;
311
+ }
312
+ case 'cell': {
313
+ const v = `${e.map}.at(${pk(e.key, P, ctx)}).${e.field}`;
314
+ return !nat && recv && P.sorts.get(`${e.map}.${e.field}`) === 'nat' ? `Int.of(${v})` : v;
315
+ }
316
+ }
317
+ }
318
+ // A number in its own mode at this place: a natural's terms where it is one, else Int's.
319
+ export const pnum = (e, P, ctx, recv = false) => pn(e, P, ctx, natOK(e, ctx) ? 'nat' : 'int', recv);
320
+ function pcmp(rel, a, b, P, ctx) {
321
+ const op = rel === '<' ? 'lt' : rel === '<=' ? 'atMost' : 'eq', flip = rel === '<' ? 'gt' : rel === '<=' ? 'atLeast' : 'eq';
322
+ const nat = natOK(a, ctx) && natOK(b, ctx);
323
+ const [x, y, o] = a.op === 'const' && b.op !== 'const' ? [b, a, flip] : [a, b, op];
324
+ return `${pn(x, P, ctx, nat ? 'nat' : 'int', true)}.${o}(${pn(y, P, ctx, nat || natOK(y, ctx) ? 'nat' : 'int')})`;
325
+ }
326
+ export function pb(c, P, ctx) {
327
+ const h = P.hoisted.get(I.keyOf(c));
328
+ if (h)
329
+ return h;
330
+ switch (c.op) {
331
+ case 'const': return c.v ? 'yes' : 'no';
332
+ case 'var': return c.name;
333
+ case 'cmp': return pcmp(c.rel, c.a, c.b, P, ctx);
334
+ case 'and': return `all(${c.xs.map(x => pb(x, P, ctx)).join(', ')})`;
335
+ case 'or': return `any(${c.xs.map(x => pb(x, P, ctx)).join(', ')})`;
336
+ case 'not':
337
+ if (c.x.op === 'cmp') {
338
+ const { rel, a, b } = c.x;
339
+ return rel === '<' ? pcmp('<=', b, a, P, ctx) : rel === '<=' ? pcmp('<', b, a, P, ctx) : `${pcmp('=', a, b, P, ctx)}.not()`;
340
+ }
341
+ if (c.x.op === 'and' && c.x.xs.length === 2)
342
+ return `${pb(c.x.xs[0], P, ctx)}.implies(${pb(I.not(c.x.xs[1]), P, ctx)})`;
343
+ if (c.x.op === 'or')
344
+ return `all(${c.x.xs.map(x => pb(I.not(x), P, ctx)).join(', ')})`;
345
+ return `not(${pb(c.x, P, ctx)})`;
346
+ case 'ite': {
347
+ const d = decided(c.c, ctx);
348
+ if (d !== undefined)
349
+ return pb(d ? c.a : c.b, P, ctx);
350
+ return `when(${pb(c.c, P, ctx)}, ${pb(c.a, P, withAtom(ctx, c.c))}, ${pb(c.b, P, withAtom(ctx, I.not(c.c)))})`;
351
+ }
352
+ case 'is': return `${pl(c.l, P, ctx)}.is(${q(c.value)})`;
353
+ case 'leq': return `${pl(c.a, P, ctx)}.eq(${pl(c.b, P, ctx)})`;
354
+ case 'keq': {
355
+ const s = (k) => (k.op === 'svar' ? k.name : k.op === 'slit' ? q(k.value) : pk(k, P, ctx));
356
+ return c.a.op === 'svar' ? `${c.a.name}.eq(${s(c.b)})` : c.b.op === 'svar' ? `${c.b.name}.eq(${s(c.a)})` : `${pk(c.a, P, ctx)}.eq(${pk(c.b, P, ctx)})`;
357
+ }
358
+ case 'cell': return `${c.map}.at(${pk(c.key, P, ctx)}).${c.field}`;
359
+ }
360
+ }
361
+ // A choice: a literal is written as the choice's own value where nothing else gives the choice (`status.of('open')`).
362
+ export function pl(l, P, ctx, bare = false) {
363
+ switch (l.op) {
364
+ case 'var': return l.name;
365
+ case 'lit': {
366
+ if (bare)
367
+ return q(l.value);
368
+ const w = P.choiceVars.get(l.values.join('\u0000'));
369
+ if (!w)
370
+ throw new Refusal(P.m.file, `a choice among ${l.values.join(', ')} with no variable of its own to name it by`);
371
+ return `${w}.of(${q(l.value)})`;
372
+ }
373
+ case 'ite': return `when(${pb(l.c, P, ctx)}, ${pl(l.a, P, ctx, l.b.op !== 'lit')}, ${pl(l.b, P, ctx, l.a.op !== 'lit' || l.b.op !== 'lit' ? l.a.op !== 'lit' : false)})`;
374
+ case 'cell': return `${l.map}.at(${pk(l.key, P, ctx)}).${l.field}`;
375
+ }
376
+ }
377
+ export function pk(k, P, ctx) {
378
+ switch (k.op) {
379
+ case 'num':
380
+ if (!natOK(k.e, ctx))
381
+ throw new Refusal(P.m.file, 'a map key that may be negative (a rule\'s map is keyed by naturals)');
382
+ return pn(k.e, P, ctx, 'nat');
383
+ case 'svar': return `${k.name}.id`;
384
+ case 'slit': return `intern(${q(k.value)})`;
385
+ case 'kite': return `when(${pb(k.c, P, ctx)}, ${pk(k.a, P, ctx)}, ${pk(k.b, P, ctx)})`;
386
+ }
387
+ }
388
+ export function pterm(t, P, ctx, target) {
389
+ if (t.s === 'num')
390
+ return target && P.sorts.get(target) === 'nat' ? pn(t.e, P, ctx, 'nat') : target ? pn(t.e, P, ctx, 'int') : pnum(t.e, P, ctx);
391
+ if (t.s === 'bool')
392
+ return pb(t.e, P, ctx);
393
+ if (t.s === 'str')
394
+ return pstr(t.e, P, ctx, true);
395
+ return pl(t.e, P, ctx, true);
396
+ }
397
+ // A string: a variable (a Str), a literal written whole, or one of two Strs by a condition.
398
+ function pstr(k, P, ctx, top = false) {
399
+ if (k.op === 'svar')
400
+ return k.name;
401
+ if (k.op === 'slit') {
402
+ if (top)
403
+ return q(k.value);
404
+ throw new Refusal(P.m.file, `the literal '${k.value}' chosen by a condition (a string by cases is of two strings the rule holds)`);
405
+ }
406
+ if (k.op === 'kite') {
407
+ const d = decided(k.c, ctx);
408
+ if (d !== undefined)
409
+ return pstr(d ? k.a : k.b, P, ctx, top);
410
+ // A literal beside a string value is written as itself (when reads it as that string); two literals have none.
411
+ const lits = (k.a.op === 'slit' ? 1 : 0) + (k.b.op === 'slit' ? 1 : 0);
412
+ return `when(${pb(k.c, P, ctx)}, ${pstr(k.a, P, withAtom(ctx, k.c), lits < 2)}, ${pstr(k.b, P, withAtom(ctx, I.not(k.c)), lits < 2)})`;
413
+ }
414
+ throw new Refusal(P.m.file, 'a number where a string is written');
415
+ }
416
+ // The kind of a variable, as the builders declare it.
417
+ export const kindOf = (v, sorts, at = v.name) => {
418
+ switch (v.sort.k) {
419
+ case 'num': return sorts.get(at) === 'nat' ? 'nat' : 'int';
420
+ case 'bool': return 'bool';
421
+ case 'choice': return `oneOf(${v.sort.values.map(q).join(', ')})`;
422
+ case 'str': return 'str()';
423
+ }
424
+ };
425
+ export const mapKind = (d, sorts) => `record({has: bool, ${d.fields.map(f => `${f.name}: ${kindOf(f, sorts, `${d.name}.${f.name}`)}`).join(', ')}})`;
426
+ export const mapEmpty = (d) => `{has: false, ${d.fields.map(f => `${f.name}: ${f.sort.k === 'num' ? '0' : f.sort.k === 'bool' ? 'false' : f.sort.k === 'choice' ? q(f.sort.values[0]) : "''"}`).join(', ')}}`;