@mark1russell7/vex-compiler 0.18.0 → 0.19.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.
@@ -1,4 +1,4 @@
1
- import { a as unsharedAll, c as factsOf, d as primitiveOf, n as originalOf, r as placesRead, t as memoNodes, u as kindOfDomain } from "./memo-08WGBHWI.js";
1
+ import { a as unsharedAll, c as factsOf, d as primitiveOf, n as originalOf, r as placesRead, t as memoNodes, u as kindOfDomain } from "./memo-J-Km2qc0.js";
2
2
  import { CELL_KIND, MAX_ITERATIONS, addrExprs, app, axisExprs, children, columnOrder, evaluate, fromDocument, isCellRef, isDocument, isFieldName, isListOp, isVexList, joinKind, limitAxis, listOf, lit, monotoneError, moveExpr, nestingError, nestingOf, readsOf, rec, shapeError, sizeError, space, splitLimit, stepError, vexList } from "@mark1russell7/vex";
3
3
  import { parseExpressionAt, tokTypes, tokenizer } from "acorn";
4
4
  import { GENERATOR, generate } from "astring";
@@ -66,10 +66,10 @@ const TEXT_TOKENS = /* @__PURE__ */ new Set([
66
66
  tokTypes.regexp
67
67
  ]);
68
68
  /** A text of the code starts with one of these characters. A comment starts with the slash too. */
69
- const TEXT_START = /["'`/]/;
69
+ const TEXT_START = () => /["'`/]/;
70
70
  /** This function gives a line of code with spaces at the place of each text. Each other character keeps its offset. A line with no text needs no tokens. */
71
71
  function codeOf(line) {
72
- if (!TEXT_START.test(line)) return line;
72
+ if (!TEXT_START().test(line)) return line;
73
73
  let out = "";
74
74
  let at = 0;
75
75
  for (const token of tokenizer(line, { ecmaVersion: "latest" })) {
@@ -103,9 +103,9 @@ function namesOfCode(code) {
103
103
  return names;
104
104
  }
105
105
  /** The words of a text. */
106
- const WORD = /[A-Za-z_$][\w$]*/g;
106
+ const WORD = () => /[A-Za-z_$][\w$]*/g;
107
107
  /** This function gives the words of a text. */
108
- const wordsOf = (text) => text.match(WORD) ?? [];
108
+ const wordsOf = (text) => text.match(WORD()) ?? [];
109
109
  /** This function gives the count of a word in some lines. A line without the text of the word needs no walk. */
110
110
  function countOf(lines, name) {
111
111
  let sum = 0;
@@ -129,23 +129,25 @@ function openerOf(lines, at) {
129
129
  return k;
130
130
  }
131
131
  /** The line of a function that a block opens. */
132
- const FUNCTION_HEAD = /=> \{$|\bfunction\b.*\{$/;
132
+ const FUNCTION_HEAD = () => /=> \{$|\bfunction\b.*\{$/;
133
133
  /** This function gives the line that opens the function of the line `at`, or -1 for the function of the run. */
134
134
  function functionOf(lines, at) {
135
+ const head = FUNCTION_HEAD();
135
136
  let k = openerOf(lines, at);
136
- while (k >= 0 && !FUNCTION_HEAD.test(lines[k])) k = openerOf(lines, k);
137
+ while (k >= 0 && !head.test(lines[k])) k = openerOf(lines, k);
137
138
  return k;
138
139
  }
139
140
  /** The key of an item of a list in a loop. The head of the loop must come just before it (`deadKeys`). */
140
- const KEY_READ = /^\s*const (j\d+) = ([A-Za-z]+\d*)\[(q\d+)\](\[0\])?;$/;
141
+ const KEY_READ = () => /^\s*const (j\d+) = ([A-Za-z]+\d*)\[(q\d+)\](\[0\])?;$/;
141
142
  /**
142
143
  * This function removes each read of a key that no code reads (COMPILE.MODULE.SIZE.KEY). The read must be the first line
143
144
  * of the loop of its position. A read of a pair must also have a loop over the length of its array. An item of a list
144
145
  * is a pair, so the read cannot throw.
145
146
  */
146
147
  function deadKeys(lines) {
148
+ const keyRead = KEY_READ();
147
149
  return lines.filter((line, i) => {
148
- const m = KEY_READ.exec(line);
150
+ const m = keyRead.exec(line);
149
151
  if (m === null) return true;
150
152
  const [, key, array, position, pair] = m;
151
153
  const head = lines[i - 1] ?? "";
@@ -160,11 +162,12 @@ function renamed(line, from, to) {
160
162
  return rewritten(line, new RegExp(`(?<![\\w$])(?<!(?<!\\.\\.)\\.)(?:(?<![{,]\\s*)${from}|${from}(?!\\s*:))(?![\\w$])`, "g"), () => to);
161
163
  }
162
164
  /** The writes to a name. The first group holds the name after `++` or `--`, and the second group the name before an operator. */
163
- const WRITE = /(?:\+\+|--)\s*([A-Za-z_$][\w$]*)|(?<![\w$.])(?<!\b(?:const|let) )([A-Za-z_$][\w$]*)\s*(?:\+\+|--|(?:[-+*/%&|^]|<<|>>>?|\*\*|&&|\|\||\?\?)?=(?![=>]))/g;
165
+ const WRITE = () => /(?:\+\+|--)\s*([A-Za-z_$][\w$]*)|(?<![\w$.])(?<!\b(?:const|let) )([A-Za-z_$][\w$]*)\s*(?:\+\+|--|(?:[-+*/%&|^]|<<|>>>?|\*\*|&&|\|\||\?\?)?=(?![=>]))/g;
164
166
  /** This function gives the lines that write to each name, without the line that declares the name. */
165
167
  function writesOf(lines) {
166
168
  const writes = /* @__PURE__ */ new Map();
167
- for (const [i, line] of lines.entries()) for (const m of line.matchAll(WRITE)) {
169
+ const write = WRITE();
170
+ for (const [i, line] of lines.entries()) for (const m of line.matchAll(write)) {
168
171
  const name = m[1] ?? m[2];
169
172
  writes.set(name, [...writes.get(name) ?? [], i]);
170
173
  }
@@ -177,7 +180,7 @@ function binds(line, name) {
177
180
  return new RegExp(`\\b(?:const|let)\\s+(?:[^;=]*,\\s*|\\[[^\\]]*)?${word}|\\([^()]*${word}[^()]*\\)\\s*=>|${word}\\s*=>|\\bfunction\\b[^(]*\\([^)]*${word}`).test(line);
178
181
  }
179
182
  /** A statement that gives a new name to a fresh name, or to `o`, the key of a run. */
180
- const ALIAS = /^\s*const ([A-Za-z]+\d+) = ([A-Za-z]+\d+|o);$/;
183
+ const ALIAS = () => /^\s*const ([A-Za-z]+\d+) = ([A-Za-z]+\d+|o);$/;
181
184
  /**
182
185
  * This function tells if the name `name` keeps its value from the line `at` to the line `end`. A name with no write
183
186
  * keeps its value. Else no line in that part can write to it. That part must also have no function, because a function
@@ -199,8 +202,9 @@ function aliases(lines) {
199
202
  const out = [...lines];
200
203
  const writes = writesOf(lines);
201
204
  const gone = /* @__PURE__ */ new Set();
205
+ const statement = ALIAS();
202
206
  for (let i = 0; i < out.length; i++) {
203
- const m = ALIAS.exec(out[i]);
207
+ const m = statement.exec(out[i]);
204
208
  if (m === null) continue;
205
209
  const [, alias, name] = m;
206
210
  const end = blockEnd(out, i);
@@ -215,20 +219,22 @@ function aliases(lines) {
215
219
  return out.filter((_, i) => !gone.has(i));
216
220
  }
217
221
  /** The first line of a lazy cell of the run. */
218
- const CELL = /^ {2}const (c\d+) = L\(\(\) => \{$/;
222
+ const CELL = () => /^ {2}const (c\d+) = L\(\(\) => \{$/;
219
223
  /** The first line of a lazy cell at each place. */
220
- const ANY_CELL = /^\s*const \w+ = L\(\(\) => \{$/;
224
+ const ANY_CELL = () => /^\s*const \w+ = L\(\(\) => \{$/;
221
225
  /** The blocks that run each statement at most once, for one run of the block around them. */
222
- const ONCE_BLOCK = /^\s*(?:try \{|\} catch \{|\} else \{|(?:\} else )?if \(.*\) \{)$/;
226
+ const ONCE_BLOCK = () => /^\s*(?:try \{|\} catch \{|\} else \{|(?:\} else )?if \(.*\) \{)$/;
223
227
  /** The statements that can run a part of their line more than once. */
224
- const LOOP_LINE = /^\s*(?:for|while|do)\b|=>/;
228
+ const LOOP_LINE = () => /^\s*(?:for|while|do)\b|=>/;
225
229
  /** This function tells if the statement at the line `at` runs at most once in a run. */
226
230
  function runsOnce(lines, at) {
227
- if (LOOP_LINE.test(lines[at])) return false;
231
+ if (LOOP_LINE().test(lines[at])) return false;
232
+ const cell = ANY_CELL();
233
+ const once = ONCE_BLOCK();
228
234
  for (let k = openerOf(lines, at); k >= 0; k = openerOf(lines, k)) {
229
235
  const line = lines[k];
230
- if (ANY_CELL.test(line)) return true;
231
- if (!ONCE_BLOCK.test(line)) return false;
236
+ if (cell.test(line)) return true;
237
+ if (!once.test(line)) return false;
232
238
  }
233
239
  return true;
234
240
  }
@@ -239,8 +245,9 @@ function runsOnce(lines, at) {
239
245
  */
240
246
  function onceCells(lines) {
241
247
  let out = lines;
248
+ const cell = CELL();
242
249
  for (let i = 0; i + 2 < out.length; i++) {
243
- const name = CELL.exec(out[i])?.[1];
250
+ const name = cell.exec(out[i])?.[1];
244
251
  const value = /^ {4}return (.*);$/.exec(out[i + 1])?.[1];
245
252
  if (name === void 0 || value === void 0 || out[i + 2] !== " });" || countOf(out, name) !== 2) continue;
246
253
  const read = new RegExp(`(?<![\\w$.])G\\(${name}\\(\\)\\)`, "g");
@@ -253,37 +260,37 @@ function onceCells(lines) {
253
260
  return out;
254
261
  }
255
262
  /** A constant of the run on one line, with its name. */
256
- const CONSTANT = /^ {2}const (c\d+) = (?:T\(\(\) => .*\)|\[\]);$/;
263
+ const CONSTANT = () => /^ {2}const (c\d+) = (?:T\(\(\) => .*\)|\[\]);$/;
257
264
  /** The first line of a constant of the run with a `try`, with its name. */
258
- const HELD = /^ {2}let (c\d+);$/;
265
+ const HELD = () => /^ {2}let (c\d+);$/;
259
266
  /** The first line of a column at each depth, with its indent and its name. A column does not run until a read calls it. */
260
- const COLUMN = /^( *)const (col\d+) = Q\(\(\w+\) => \{$/;
267
+ const COLUMN = () => /^( *)const (col\d+) = Q\(\(\w+\) => \{$/;
261
268
  /** The variable of the pack of a column at each depth, with its name. */
262
- const PACK = /^ *let (PK\d+);$/;
269
+ const PACK = () => /^ *let (PK\d+);$/;
263
270
  /** This function gives the name of the declaration at the line `at` and its last line, or `undefined` for another line. */
264
271
  function constantAt(lines, at) {
265
272
  const line = lines[at];
266
- const one = CONSTANT.exec(line)?.[1];
273
+ const one = CONSTANT().exec(line)?.[1];
267
274
  if (one !== void 0) return {
268
275
  name: one,
269
276
  last: at
270
277
  };
271
- const lazy = CELL.exec(line)?.[1];
278
+ const lazy = CELL().exec(line)?.[1];
272
279
  if (lazy !== void 0) return {
273
280
  name: lazy,
274
281
  last: lines.indexOf(" });", at)
275
282
  };
276
- const column = COLUMN.exec(line);
283
+ const column = COLUMN().exec(line);
277
284
  if (column !== null) return {
278
285
  name: column[2],
279
286
  last: lines.indexOf(`${column[1]}});`, at)
280
287
  };
281
- const pack = PACK.exec(line)?.[1];
288
+ const pack = PACK().exec(line)?.[1];
282
289
  if (pack !== void 0) return {
283
290
  name: pack,
284
291
  last: at
285
292
  };
286
- const held = HELD.exec(line)?.[1];
293
+ const held = HELD().exec(line)?.[1];
287
294
  return held === void 0 || lines[at + 1] !== " try {" ? void 0 : {
288
295
  name: held,
289
296
  last: lines.indexOf(" }", at)
@@ -328,7 +335,7 @@ function liveDecls(decls, roots) {
328
335
  return new Map([...decls].filter(([name]) => live.has(name)));
329
336
  }
330
337
  /** The tokens of the body of a function of an op. A body with another token has no inline form. */
331
- const BODY_TOKEN = /\s+|"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'|\.{3}|\.\d\w*|\d[\w.]*|\.[A-Za-z_$][\w$]*|[A-Za-z_$][\w$]*|===|!==|\?\?|&&|\|\||\*\*|<<|>>>|>>|==|!=|<=|>=|\+\+|--|=>|\?\.|[-+*/%<>!?:,&|^~()[\]{}]/y;
338
+ const BODY_TOKEN = () => /\s+|"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'|\.{3}|\.\d\w*|\d[\w.]*|\.[A-Za-z_$][\w$]*|[A-Za-z_$][\w$]*|===|!==|\?\?|&&|\|\||\*\*|<<|>>>|>>|==|!=|<=|>=|\+\+|--|=>|\?\.|[-+*/%<>!?:,&|^~()[\]{}]/y;
332
339
  /** The tokens that change a value or start a function. */
333
340
  const NOT_INLINE = /* @__PURE__ */ new Set([
334
341
  "++",
@@ -350,9 +357,9 @@ const NOT_VALUES$1 = /* @__PURE__ */ new Set(["typeof", "void"]);
350
357
  /** This function gives the tokens of a body, or `undefined` for a body with another token. */
351
358
  function tokensOf(body) {
352
359
  const out = [];
353
- BODY_TOKEN.lastIndex = 0;
354
- while (BODY_TOKEN.lastIndex < body.length) {
355
- const m = BODY_TOKEN.exec(body);
360
+ const token = BODY_TOKEN();
361
+ while (token.lastIndex < body.length) {
362
+ const m = token.exec(body);
356
363
  if (m === null || NOT_INLINE.has(m[0])) return void 0;
357
364
  out.push(m[0]);
358
365
  }
@@ -386,7 +393,7 @@ function bodyParts(params, body, globals) {
386
393
  if (t === ")" || t === "]" || t === "}") open.pop();
387
394
  const at = params.indexOf(t);
388
395
  const key = open.at(-1) === "{" && (last === "{" || last === ",");
389
- if (!isWord(t) || key && next === ":") parts.push(t);
396
+ if (!isWord(t) || last === "?." || key && next === ":") parts.push(t);
390
397
  else if (key && next !== "," && next !== "}") return void 0;
391
398
  else if (at >= 0) parts.push(...key ? [`${t}:`, at] : [at]);
392
399
  else if (PLAIN_WORDS.has(t) || globals.has(t)) parts.push(t);
@@ -396,10 +403,10 @@ function bodyParts(params, body, globals) {
396
403
  return parts;
397
404
  }
398
405
  /** The declaration of a function of an op or of a check of a domain, as an arrow function. */
399
- const OP_DECL = /^const ((?:op|v)_\w+) = (?:\(([\w$,\s]*)\)|([A-Za-z_$][\w$]*))\s*=>([\s\S]*);$/;
406
+ const OP_DECL = () => /^const ((?:op|v)_\w+) = (?:\(([\w$,\s]*)\)|([A-Za-z_$][\w$]*))\s*=>([\s\S]*);$/;
400
407
  /** This function gives the form of a declaration, or `undefined` for a declaration without one. */
401
408
  function opForm(text, globals) {
402
- const m = OP_DECL.exec(text);
409
+ const m = OP_DECL().exec(text);
403
410
  if (m === null) return void 0;
404
411
  const params = (m[2] ?? m[3] ?? "").split(",").map((x) => x.trim()).filter((x) => x !== "");
405
412
  const parts = bodyParts(params, m[4], globals);
@@ -957,568 +964,598 @@ const depsOf = (...xs) => new Set(xs.flatMap((x) => [...x.v.deps]));
957
964
  const stmtsOf = (...xs) => xs.flatMap((x) => x.s);
958
965
  /** This function indents some statements by one level. */
959
966
  const ind = (xs) => xs.map((x) => ` ${x}`);
960
- /** The helpers of the generated code. A module has only the helpers that it uses. */
967
+ /** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
968
+ function helperTexts1() {
969
+ return {
970
+ F: () => "function F() { throw E; }",
971
+ C: () => "function C(m) { throw new VexContractError(m); }",
972
+ T: () => "function T(f) { try { return f(); } catch { return E; } }",
973
+ L: () => "function L(f) { let done = false, v; return () => { if (!done) { done = true; try { v = f(); } catch { v = E; } } return v; }; }",
974
+ Q: () => "function Q(f) { const m = []; return (j) => { let v = m[j]; if (v === undefined) { try { v = f(j); } catch { v = E; } m[j] = v; } return v; }; }",
975
+ G: () => "function G(x) { return x === E ? F() : x; }",
976
+ QA: () => "function QA(n, f) { const a = []; for (let j = 0; j < n; j++) a.push(f(j)); return a; }",
977
+ PF: () => [
978
+ "function PF(n, init, key, test, step, result) {",
979
+ " let ts, js, a0, bad, mono, pa;",
980
+ " return (q) => {",
981
+ " if (ts === undefined) {",
982
+ " ts = [];",
983
+ " js = [];",
984
+ " bad = false;",
985
+ " try { a0 = init(); } catch { a0 = E; }",
986
+ " for (let j = 0; j < n; j++) {",
987
+ " let t;",
988
+ " try { t = key(j); } catch { bad = true; break; }",
989
+ " if (t !== t) continue;",
990
+ " ts.push(t);",
991
+ " js.push(j);",
992
+ " }",
993
+ " mono = true;",
994
+ " for (let i = 1; i < ts.length; i++) if (!(ts[i - 1] <= ts[i])) mono = false;",
995
+ " pa = [a0];",
996
+ " }",
997
+ " if (a0 === E || bad || q === E) F();",
998
+ " if (!mono) {",
999
+ " let a = a0;",
1000
+ " for (let i = 0; i < ts.length; i++) if (test(ts[i], q)) a = step(a, js[i]);",
1001
+ " return result(a);",
1002
+ " }",
1003
+ " let lo = 0, hi = ts.length;",
1004
+ " while (lo < hi) {",
1005
+ " const h = (lo + hi) >> 1;",
1006
+ " if (test(ts[h], q)) lo = h + 1;",
1007
+ " else hi = h;",
1008
+ " }",
1009
+ " for (let i = pa.length - 1; i < lo; i++) pa.push(step(pa[i], js[i]));",
1010
+ " return result(pa[lo]);",
1011
+ " };",
1012
+ "}"
1013
+ ].join("\n"),
1014
+ B: () => "function B(x) { return typeof x === \"boolean\" ? x : F(); }",
1015
+ U: () => "function U(x) { return typeof x === \"number\" ? x : F(); }",
1016
+ X: () => "function X(t, n) { return t >= 0 && t < n ? t : F(); }",
1017
+ I: () => "function I(t, n) { return Number.isInteger(t) && t >= 0 && t < n ? t : F(); }",
1018
+ K: () => "function K(k, n) { return typeof k === \"string\" && /^(0|[1-9]\\d*)$/.test(k) && +k < n ? +k : F(); }",
1019
+ V: () => "function V(l, s) { if (!l.some((x) => x[1] === E)) return l; if (s) F(); return l.filter((x) => x[1] !== E); }",
1020
+ SB: () => [
1021
+ "function SB(l, s) {",
1022
+ " const o = V(l, s);",
1023
+ " const t = o.length > 0 ? typeof o[0][1] : \"number\";",
1024
+ " for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1025
+ " return o.slice().sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
1026
+ "}"
1027
+ ].join("\n"),
1028
+ SK: () => [
1029
+ "function SK(l, s, f) {",
1030
+ " const o = V(l, s).map((x) => [x, f(x[1])]);",
1031
+ " const t = o.length > 0 ? typeof o[0][1] : \"number\";",
1032
+ " for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1033
+ " return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0)).map((x) => x[0]);",
1034
+ "}"
1035
+ ].join("\n"),
1036
+ TK: () => "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
1037
+ VW: () => [
1038
+ "function VW(l) {",
1039
+ " let o = l.vw;",
1040
+ " if (o === undefined) {",
1041
+ " o = l.some((x) => x[1] === E) ? l.filter((x) => x[1] !== E) : l;",
1042
+ " l.vw = o;",
1043
+ " }",
1044
+ " return o;",
1045
+ "}"
1046
+ ].join("\n"),
1047
+ NT: () => [
1048
+ "function NT(l, i, s) {",
1049
+ " const o = VW(l);",
1050
+ " if (s && o.length < l.length) F();",
1051
+ " return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F();",
1052
+ "}"
1053
+ ].join("\n"),
1054
+ CT: () => "function CT(l, s) { const o = VW(l); return s && o.length < l.length ? F() : o.length; }",
1055
+ FS: () => "function FS(l, s) { const o = VW(l); return (s && o.length < l.length) || o.length === 0 ? F() : o[0][1]; }",
1056
+ SN: () => [
1057
+ "function SN(l, i, v, s) {",
1058
+ " const x = V(l, s);",
1059
+ " if (!(Number.isSafeInteger(i) && i >= 0 && i < x.length && v !== undefined)) F();",
1060
+ " const o = x.slice();",
1061
+ " o[i] = [x[i][0], v];",
1062
+ " return o;",
1063
+ "}"
1064
+ ].join("\n"),
1065
+ FI: () => [
1066
+ "function FI(n, v) {",
1067
+ " if (!(Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined)) F();",
1068
+ " const o = [];",
1069
+ " for (let i = 0; i < n; i++) o.push([i, v]);",
1070
+ " return o;",
1071
+ "}"
1072
+ ].join("\n"),
1073
+ FN: () => "function FN(n, v) { return Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined ? n + 0 : F(); }",
1074
+ TN: () => "function TN(n, k) { return Number.isSafeInteger(k) && k >= 0 ? Math.min(n, k + 0) : F(); }"
1075
+ };
1076
+ }
1077
+ /** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
1078
+ function helperTexts2() {
1079
+ return {
1080
+ LI: () => [
1081
+ "function LI(x, h, t) {",
1082
+ " if (typeof x !== \"object\" || x === null || x.kind !== \"vex.list\" || !Array.isArray(x.items)) return undefined;",
1083
+ " const o = [];",
1084
+ " for (let i = 0; i < x.items.length; i++) {",
1085
+ " const it = x.items[i];",
1086
+ " const r = typeof it === \"object\" && it !== null ? it.result : undefined;",
1087
+ " if (typeof r !== \"object\" || r === null || typeof it.key !== \"string\") return undefined;",
1088
+ " if (r.ok === true) {",
1089
+ " const v = h > 0 ? LI(r.value, h - 1, t) : r.value !== undefined && t(r.value) ? r.value : undefined;",
1090
+ " if (v === undefined) return undefined;",
1091
+ " o.push([it.key, v]);",
1092
+ " } else {",
1093
+ " const e = r.error;",
1094
+ " if (r.ok !== false || typeof e !== \"object\" || e === null || typeof e.code !== \"string\" || typeof e.kind !== \"string\" || typeof e.message !== \"string\" || !Array.isArray(e.path) || e.kind === \"depth\") return undefined;",
1095
+ " o.push([it.key, E]);",
1096
+ " }",
1097
+ " }",
1098
+ " return o;",
1099
+ "}"
1100
+ ].join("\n"),
1101
+ LA: () => [
1102
+ "function LA(x, h, t) {",
1103
+ " if (!Array.isArray(x)) return undefined;",
1104
+ " const o = [];",
1105
+ " for (let i = 0; i < x.length; i++) {",
1106
+ " const y = x[i];",
1107
+ " if (y === VEX_ERROR) {",
1108
+ " o.push([i, E]);",
1109
+ " continue;",
1110
+ " }",
1111
+ " const v = h > 0 ? LA(y, h - 1, t) : y !== undefined && t(y) ? y : undefined;",
1112
+ " if (v === undefined) return undefined;",
1113
+ " o.push([i, v]);",
1114
+ " }",
1115
+ " return o;",
1116
+ "}"
1117
+ ].join("\n"),
1118
+ DR: () => "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
1119
+ AP: () => "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
1120
+ AO: () => "function AO(l, o, s, w) { if (l !== w) return AP(l, o, s); const a = V(o, s), n = a.length; for (let i = 0; i < n; i++) l.push(a[i]); return l; }",
1121
+ AW: () => "function AW(l, s, w) { if (l === w) return l; const a = V(l, s), o = []; for (let i = 0; i < a.length; i++) o.push(a[i]); return o; }",
1122
+ AI: () => "function AI(l, x, s, w) { const o = AW(l, s, w); o.push(x); return o; }",
1123
+ WO: () => [
1124
+ "function WO(l, o, s) {",
1125
+ " const a = V(l, s), g = new Map();",
1126
+ " for (const [k] of V(o, s)) g.set(`${k}`, 1);",
1127
+ " return a.filter((x) => !g.has(`${x[0]}`));",
1128
+ "}"
1129
+ ].join("\n"),
1130
+ FL: () => [
1131
+ "function FL(l, s) {",
1132
+ " const o = [];",
1133
+ " for (const [, v] of V(l, s)) for (const x of V(v, s)) o.push(x);",
1134
+ " return o;",
1135
+ "}"
1136
+ ].join("\n"),
1137
+ GT: () => [
1138
+ "function GT(l, k, s) {",
1139
+ " let x = l.ix;",
1140
+ " if (x === undefined) {",
1141
+ " x = new Map();",
1142
+ " x.n = 0;",
1143
+ " l.ix = x;",
1144
+ " }",
1145
+ " if (x.n !== l.length) {",
1146
+ " for (let i = x.n; i < l.length; i++) {",
1147
+ " const [q, v] = l[i], t = `${q}`;",
1148
+ " if (v === E) x.set(E, E);",
1149
+ " else if (!x.has(t)) x.set(t, v);",
1150
+ " }",
1151
+ " x.n = l.length;",
1152
+ " }",
1153
+ " return (s && x.has(E)) || typeof k !== \"string\" || !x.has(k) ? F() : x.get(k);",
1154
+ "}"
1155
+ ].join("\n"),
1156
+ IX: () => [
1157
+ "function IX(l, x, s) {",
1158
+ " const o = V(l, s);",
1159
+ " if (typeof x !== \"number\" && typeof x !== \"string\" && typeof x !== \"boolean\") F();",
1160
+ " for (let i = 0; i < o.length; i++) if (o[i][1] === x) return i;",
1161
+ " return F();",
1162
+ "}"
1163
+ ].join("\n"),
1164
+ KC: () => [
1165
+ "function KC(a, n) {",
1166
+ " if (!Array.isArray(a) || a.length !== n) C(\"the keys are not an array with one key for each record\");",
1167
+ " const m = new Map();",
1168
+ " for (let i = 0; i < n; i++) {",
1169
+ " const k = a[i];",
1170
+ " if (typeof k !== \"string\" || m.has(k)) C(`the key ${i} is not a text of its own`);",
1171
+ " m.set(k, i);",
1172
+ " }",
1173
+ " return m;",
1174
+ "}"
1175
+ ].join("\n"),
1176
+ KP: () => "function KP(m, k) { const i = m.get(k); return i === undefined ? F() : i; }"
1177
+ };
1178
+ }
1179
+ /** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
1180
+ function helperTexts3() {
1181
+ return {
1182
+ PT: () => [
1183
+ "function PT(p, n) {",
1184
+ " if (!Array.isArray(p) || p.length !== n) C(\"the parents are not an array with one parent for each record\");",
1185
+ " const P = new Int32Array(n), s = new Uint8Array(n);",
1186
+ " for (let i = 0; i < n; i++) {",
1187
+ " const x = p[i];",
1188
+ " if (x !== -1 && !(Number.isInteger(x) && x >= 0 && x < n)) C(`the parent of the record ${i} is not -1 or the position of a record`);",
1189
+ " P[i] = x;",
1190
+ " }",
1191
+ " for (let i = 0; i < n; i++) {",
1192
+ " let k = i;",
1193
+ " while (k >= 0 && s[k] === 0) {",
1194
+ " s[k] = 1;",
1195
+ " k = P[k];",
1196
+ " }",
1197
+ " if (k >= 0 && s[k] === 1) C(`the parents of the record ${i} make a cycle`);",
1198
+ " for (k = i; k >= 0 && s[k] === 1; k = P[k]) s[k] = 2;",
1199
+ " }",
1200
+ " return P;",
1201
+ "}"
1202
+ ].join("\n"),
1203
+ CH: () => [
1204
+ "function CH(P, n) {",
1205
+ " const h = [], r = [];",
1206
+ " for (let i = 0; i < n; i++) h.push([]);",
1207
+ " for (let i = 0; i < n; i++) (P[i] < 0 ? r : h[P[i]]).push(i);",
1208
+ " return [h, r];",
1209
+ "}"
1210
+ ].join("\n"),
1211
+ SG: () => [
1212
+ "function SG(P, h, r, n) {",
1213
+ " const g = [], s = new Int32Array(n);",
1214
+ " for (let i = 0; i < n; i++) g.push(P[i] < 0 ? r : h[P[i]]);",
1215
+ " for (const x of [r, ...h]) for (let q = 0; q < x.length; q++) s[x[q]] = q;",
1216
+ " return [g, s];",
1217
+ "}"
1218
+ ].join("\n"),
1219
+ PR: () => [
1220
+ "function PR(P, n) {",
1221
+ " const f = new Int32Array(n).fill(-1), x = new Int32Array(n), o = new Int32Array(n), at = new Int32Array(n), z = new Int32Array(n);",
1222
+ " let r = -1;",
1223
+ " for (let i = n - 1; i >= 0; i--) {",
1224
+ " const p = P[i];",
1225
+ " if (p < 0) {",
1226
+ " x[i] = r;",
1227
+ " r = i;",
1228
+ " } else {",
1229
+ " x[i] = f[p];",
1230
+ " f[p] = i;",
1231
+ " }",
1232
+ " }",
1233
+ " let m = 0;",
1234
+ " for (let k = r; k >= 0; ) {",
1235
+ " at[k] = m;",
1236
+ " o[m++] = k;",
1237
+ " if (f[k] >= 0) k = f[k];",
1238
+ " else {",
1239
+ " while (k >= 0 && x[k] < 0) k = P[k];",
1240
+ " if (k >= 0) k = x[k];",
1241
+ " }",
1242
+ " }",
1243
+ " for (let q = n - 1; q >= 0; q--) {",
1244
+ " const k = o[q], p = P[k];",
1245
+ " z[k]++;",
1246
+ " if (p >= 0) z[p] += z[k];",
1247
+ " }",
1248
+ " return [o, at, z];",
1249
+ "}"
1250
+ ].join("\n"),
1251
+ UP: () => "function UP(P, k) { const p = P[k]; return p < 0 ? F() : p; }",
1252
+ SO: () => "function SO(g, s, k, d) { const t = g[k][s[k] + d]; return t === undefined ? F() : t; }",
1253
+ AN: () => "function AN(P, k) { const o = []; for (let p = P[k]; p >= 0; p = P[p]) o.push(p); return o; }",
1254
+ BF: () => "function BF(h, k) { const o = [k]; for (let i = 0; i < o.length; i++) for (const c of h[o[i]]) o.push(c); return o; }",
1255
+ GR: () => [
1256
+ "function GR(e, n) {",
1257
+ " if (!Array.isArray(e) || e.length !== n) C(\"the edges are not an array with the ends of the edges of each record\");",
1258
+ " const O = [], s = new Int32Array(n);",
1259
+ " for (let i = 0; i < n; i++) {",
1260
+ " const x = e[i];",
1261
+ " if (!Array.isArray(x)) C(`the edges of the record ${i} are not an array`);",
1262
+ " const o = [];",
1263
+ " for (const t of x) {",
1264
+ " if (!(Number.isInteger(t) && t >= 0 && t < n)) C(`an edge of the record ${i} does not go to the position of a record`);",
1265
+ " if (s[t] !== i + 1) {",
1266
+ " s[t] = i + 1;",
1267
+ " o.push(t);",
1268
+ " }",
1269
+ " }",
1270
+ " O.push(o);",
1271
+ " }",
1272
+ " return O;",
1273
+ "}"
1274
+ ].join("\n"),
1275
+ GI: () => "function GI(O, n) { const I = []; for (let i = 0; i < n; i++) I.push([]); for (let i = 0; i < n; i++) for (const t of O[i]) I[t].push(i); return I; }",
1276
+ RB: () => "function RB(O, k, n) { const s = new Uint8Array(n), o = [k]; s[k] = 1; for (let i = 0; i < o.length; i++) for (const c of O[o[i]]) if (s[c] === 0) { s[c] = 1; o.push(c); } return o; }",
1277
+ DO: () => "function DO(d) { for (let k = 0; k < d.length; k++) if (d[k] === E) d[k] = VEX_ERROR; return d; }",
1278
+ LO: () => "function LO(l, h) { return { kind: \"vex.list\", items: l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LO(v, h - 1) : v } })) }; }",
1279
+ LS: () => "function LS(l, h) { return Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LS(v, h - 1) : v } }))) }); }",
1280
+ LP: () => "function LP(l, h) { return l.map((x) => (x[1] === E ? VEX_ERROR : h > 0 ? LP(x[1], h - 1) : x[1])); }",
1281
+ LV: () => [
1282
+ "function LV(l, h) {",
1283
+ " let o = l.vl;",
1284
+ " if (o === undefined || o.items.length !== l.length) {",
1285
+ " o = Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LV(v, h - 1) : v } }))) });",
1286
+ " l.vl = o;",
1287
+ " }",
1288
+ " return o;",
1289
+ "}"
1290
+ ].join("\n"),
1291
+ SBC: () => [
1292
+ "function SBC(l) {",
1293
+ " const t = l.length > 0 ? typeof l[0][1] : \"number\";",
1294
+ " for (const [, v] of l) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1295
+ " return l.slice().sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
1296
+ "}"
1297
+ ].join("\n")
1298
+ };
1299
+ }
1300
+ /** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
1301
+ function helperTexts4() {
1302
+ return {
1303
+ SKC: () => [
1304
+ "function SKC(l, f) {",
1305
+ " const o = l.map((x) => [x, f(x[1])]);",
1306
+ " const t = o.length > 0 ? typeof o[0][1] : \"number\";",
1307
+ " for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1308
+ " return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0)).map((x) => x[0]);",
1309
+ "}"
1310
+ ].join("\n"),
1311
+ SBI: () => [
1312
+ "function SBI(l) {",
1313
+ " const n = l.length, t = n > 0 ? typeof l[0][1] : \"number\";",
1314
+ " for (let i = 0; i < n; i++) { const v = l[i][1]; if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F(); }",
1315
+ " if (n > 64) l.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
1316
+ " else for (let i = 1; i < n; i++) {",
1317
+ " const x = l[i], v = x[1];",
1318
+ " let j = i - 1;",
1319
+ " for (; j >= 0 && l[j][1] > v; j--) l[j + 1] = l[j];",
1320
+ " l[j + 1] = x;",
1321
+ " }",
1322
+ " return l;",
1323
+ "}"
1324
+ ].join("\n"),
1325
+ SKI: () => [
1326
+ "function SKI(l, f) {",
1327
+ " const n = l.length, k = new Array(n);",
1328
+ " for (let i = 0; i < n; i++) k[i] = f(l[i][1]);",
1329
+ " const t = n > 0 ? typeof k[0] : \"number\";",
1330
+ " for (const v of k) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1331
+ " if (n > 64) {",
1332
+ " const o = new Array(n), c = l.slice();",
1333
+ " for (let i = 0; i < n; i++) o[i] = i;",
1334
+ " o.sort((a, b) => (k[a] < k[b] ? -1 : k[a] > k[b] ? 1 : 0));",
1335
+ " for (let i = 0; i < n; i++) l[i] = c[o[i]];",
1336
+ " } else for (let i = 1; i < n; i++) {",
1337
+ " const v = k[i], x = l[i];",
1338
+ " let j = i - 1;",
1339
+ " for (; j >= 0 && k[j] > v; j--) { k[j + 1] = k[j]; l[j + 1] = l[j]; }",
1340
+ " k[j + 1] = v; l[j + 1] = x;",
1341
+ " }",
1342
+ " return l;",
1343
+ "}"
1344
+ ].join("\n"),
1345
+ TKC: () => "function TKC(l, n) { return Number.isSafeInteger(n) && n >= 0 ? l.slice(0, n) : F(); }",
1346
+ DRC: () => "function DRC(l, n) { return Number.isSafeInteger(n) && n >= 0 ? l.slice(n) : F(); }",
1347
+ AWC: () => "function AWC(l, w) { if (l === w) return l; const o = []; for (let i = 0; i < l.length; i++) o.push(l[i]); return o; }",
1348
+ AIC: () => "function AIC(l, x, w) { if (l !== w) { const o = []; for (let i = 0; i < l.length; i++) o.push(l[i]); l = o; } l.push(x); return l; }",
1349
+ KS: () => "function KS(l, n) { l.length = n; if (l.ix !== undefined) l.ix = undefined; if (l.vl !== undefined) l.vl = undefined; }",
1350
+ AOC: () => "function AOC(l, o, w) { if (l !== w) return l.concat(o); const n = o.length; for (let i = 0; i < n; i++) l.push(o[i]); return l; }",
1351
+ WOC: () => [
1352
+ "function WOC(l, o) {",
1353
+ " const g = new Map();",
1354
+ " for (const [k] of o) g.set(`${k}`, 1);",
1355
+ " return l.filter((x) => !g.has(`${x[0]}`));",
1356
+ "}"
1357
+ ].join("\n"),
1358
+ FLC: () => "function FLC(l) { const o = []; for (const [, v] of l) for (const x of v) o.push(x); return o; }",
1359
+ IXC: () => [
1360
+ "function IXC(l, x) {",
1361
+ " if (typeof x !== \"number\" && typeof x !== \"string\" && typeof x !== \"boolean\") F();",
1362
+ " for (let i = 0; i < l.length; i++) if (l[i][1] === x) return i;",
1363
+ " return F();",
1364
+ "}"
1365
+ ].join("\n"),
1366
+ NTC: () => "function NTC(l, i) { return Number.isSafeInteger(i) && i >= 0 && i < l.length ? l[i][1] : F(); }",
1367
+ SNC: () => [
1368
+ "function SNC(l, i, v) {",
1369
+ " if (!(Number.isSafeInteger(i) && i >= 0 && i < l.length && v !== undefined)) F();",
1370
+ " const o = l.slice();",
1371
+ " o[i] = [l[i][0], v];",
1372
+ " return o;",
1373
+ "}"
1374
+ ].join("\n"),
1375
+ FSC: () => "function FSC(l) { return l.length === 0 ? F() : l[0][1]; }",
1376
+ LOC: () => "function LOC(l, h) { return { kind: \"vex.list\", items: l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LOC(v, h - 1) : v } })) }; }",
1377
+ LSC: () => "function LSC(l, h) { return Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LSC(v, h - 1) : v } }))) }); }",
1378
+ LPC: () => "function LPC(l, h) { return l.map(h > 0 ? (x) => LPC(x[1], h - 1) : (x) => x[1]); }",
1379
+ LVC: () => [
1380
+ "function LVC(l, h) {",
1381
+ " let o = l.vl;",
1382
+ " if (o === undefined || o.items.length !== l.length) {",
1383
+ " o = Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LVC(v, h - 1) : v } }))) });",
1384
+ " l.vl = o;",
1385
+ " }",
1386
+ " return o;",
1387
+ "}"
1388
+ ].join("\n"),
1389
+ LC: () => "function LC(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LO(d[k], h); return d; }",
1390
+ LCP: () => "function LCP(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LP(d[k], h); return d; }"
1391
+ };
1392
+ }
1393
+ /** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
1394
+ function helperTexts5() {
1395
+ return {
1396
+ SH: () => [
1397
+ "function SH(n, cols, own, budget) {",
1398
+ " const pg = (c, k) => {",
1399
+ " const o = own[c];",
1400
+ " const g = o === undefined ? undefined : o[k];",
1401
+ " return g === undefined ? cols[c] : g;",
1402
+ " };",
1403
+ " const D = [];",
1404
+ " for (let c = 0; c < cols.length; c++) {",
1405
+ " const d = new Array(n);",
1406
+ " for (let k = 0; k < n; k++) if (pg(c, k) === undefined) d[k] = E;",
1407
+ " D.push(d);",
1408
+ " }",
1409
+ " const seen = new Map(), stack = [], FILL = {};",
1410
+ " let top = FILL, missed = false, next = 0, used = 0;",
1411
+ " const start = (c, k) => {",
1412
+ " const i = next++;",
1413
+ " const v = { id: c * n + k, c, k, i, low: i, re: false, r: E, p: false, need: undefined };",
1414
+ " seen.set(v.id, v);",
1415
+ " stack.push(v);",
1416
+ " return v;",
1417
+ " };",
1418
+ " const ev = (v, g) => {",
1419
+ " const caller = top;",
1420
+ " v.p = false;",
1421
+ " v.need = undefined;",
1422
+ " top = v;",
1423
+ " used += g[1];",
1424
+ " const x = g[0](v.k);",
1425
+ " used -= g[1];",
1426
+ " top = caller;",
1427
+ " if (!v.p) v.r = x;",
1428
+ " };",
1429
+ " const end = (v, caller) => {",
1430
+ " if (v.low === v.i) {",
1431
+ " const ms = stack.splice(stack.lastIndexOf(v));",
1432
+ " const cycle = ms.length > 1 || v.re;",
1433
+ " for (const m of ms) {",
1434
+ " seen.delete(m.id);",
1435
+ " D[m.c][m.k] = cycle ? E : m.r;",
1436
+ " }",
1437
+ " } else if (caller !== undefined) caller.low = Math.min(caller.low, v.low);",
1438
+ " };",
1439
+ " const r = (c, k) => {",
1440
+ " const d = D[c][k];",
1441
+ " if (d !== undefined) return d;",
1442
+ " const caller = top;",
1443
+ " if (caller === FILL) {",
1444
+ " missed = true;",
1445
+ " return E;",
1446
+ " }",
1447
+ " if (caller.p) return E;",
1448
+ " const s = seen.get(c * n + k);",
1449
+ " if (s !== undefined) {",
1450
+ " caller.low = Math.min(caller.low, s.i);",
1451
+ " s.re = true;",
1452
+ " return E;",
1453
+ " }",
1454
+ " const g = pg(c, k);",
1455
+ " const v = start(c, k);",
1456
+ " if (used + g[1] > budget) {",
1457
+ " caller.p = true;",
1458
+ " caller.need = v;",
1459
+ " return E;",
1460
+ " }",
1461
+ " ev(v, g);",
1462
+ " if (v.p) {",
1463
+ " caller.p = true;",
1464
+ " caller.need = v;",
1465
+ " return E;",
1466
+ " }",
1467
+ " end(v, caller);",
1468
+ " const x = D[c][k];",
1469
+ " return x === undefined ? E : x;",
1470
+ " };",
1471
+ " const drive = (c, k) => {",
1472
+ " const work = [start(c, k)];",
1473
+ " for (let v = work[0]; v !== undefined; v = work[work.length - 1]) {",
1474
+ " ev(v, pg(v.c, v.k));",
1475
+ " if (v.p) {",
1476
+ " for (let q = v.need; q !== undefined; q = q.need) work.push(q);",
1477
+ " continue;",
1478
+ " }",
1479
+ " work.pop();",
1480
+ " end(v, work[work.length - 1]);",
1481
+ " }",
1482
+ " };",
1483
+ " const f = (c, k) => {",
1484
+ " if (D[c][k] !== undefined) return;",
1485
+ " missed = false;",
1486
+ " const x = pg(c, k)[0](k);",
1487
+ " if (missed) drive(c, k);",
1488
+ " else D[c][k] = x;",
1489
+ " };",
1490
+ " const o = (c) => {",
1491
+ " const d = D[c], out = new Array(n);",
1492
+ " for (let k = 0; k < n; k++) out[k] = d[k] === E ? VEX_ERROR : d[k];",
1493
+ " return out;",
1494
+ " };",
1495
+ " return { r, f, o };",
1496
+ "}"
1497
+ ].join("\n"),
1498
+ KO: () => [
1499
+ "function KO(h, r, post, back) {",
1500
+ " const o = [], st = [];",
1501
+ " const push = (x) => {",
1502
+ " if (back) for (let q = 0; q < x.length; q++) st.push(x[q]);",
1503
+ " else for (let q = x.length - 1; q >= 0; q--) st.push(x[q]);",
1504
+ " };",
1505
+ " push(r);",
1506
+ " while (st.length > 0) {",
1507
+ " const k = st.pop();",
1508
+ " o.push(k);",
1509
+ " push(h[k]);",
1510
+ " }",
1511
+ " return post ? o.reverse() : o;",
1512
+ "}"
1513
+ ].join("\n"),
1514
+ M1: () => [
1515
+ "function M1(f) {",
1516
+ " const m = new Map();",
1517
+ " return (k) => {",
1518
+ " const s = Object.is(k, -0) ? Z : k;",
1519
+ " let r = m.get(s);",
1520
+ " if (r === undefined) {",
1521
+ " try { r = f(k); } catch { r = E; }",
1522
+ " m.set(s, r);",
1523
+ " }",
1524
+ " return G(r);",
1525
+ " };",
1526
+ "}"
1527
+ ].join("\n"),
1528
+ M: () => [
1529
+ "function M(f) {",
1530
+ " const root = new Map();",
1531
+ " return (...k) => {",
1532
+ " let m = root;",
1533
+ " for (const x of k.slice(0, -1)) {",
1534
+ " const s = Object.is(x, -0) ? Z : x;",
1535
+ " let c = m.get(s);",
1536
+ " if (c === undefined) m.set(s, (c = new Map()));",
1537
+ " m = c;",
1538
+ " }",
1539
+ " const last = k.at(-1);",
1540
+ " const s = Object.is(last, -0) ? Z : last;",
1541
+ " let r = m.get(s);",
1542
+ " if (r === undefined) {",
1543
+ " try { r = f(...k); } catch { r = E; }",
1544
+ " m.set(s, r);",
1545
+ " }",
1546
+ " return G(r);",
1547
+ " };",
1548
+ "}"
1549
+ ].join("\n")
1550
+ };
1551
+ }
1552
+ /** The helpers of the generated code. A module has only the helpers that it uses. Each entry is a function, so the mutation lane gives the text of a helper to the tests that emit it, not to all tests. */
961
1553
  const HELPERS = {
962
- F: "function F() { throw E; }",
963
- C: "function C(m) { throw new VexContractError(m); }",
964
- T: "function T(f) { try { return f(); } catch { return E; } }",
965
- L: "function L(f) { let done = false, v; return () => { if (!done) { done = true; try { v = f(); } catch { v = E; } } return v; }; }",
966
- Q: "function Q(f) { const m = []; return (j) => { let v = m[j]; if (v === undefined) { try { v = f(j); } catch { v = E; } m[j] = v; } return v; }; }",
967
- G: "function G(x) { return x === E ? F() : x; }",
968
- QA: "function QA(n, f) { const a = []; for (let j = 0; j < n; j++) a.push(f(j)); return a; }",
969
- PF: [
970
- "function PF(n, init, key, test, step, result) {",
971
- " let ts, js, a0, bad, mono, pa;",
972
- " return (q) => {",
973
- " if (ts === undefined) {",
974
- " ts = [];",
975
- " js = [];",
976
- " bad = false;",
977
- " try { a0 = init(); } catch { a0 = E; }",
978
- " for (let j = 0; j < n; j++) {",
979
- " let t;",
980
- " try { t = key(j); } catch { bad = true; break; }",
981
- " if (t !== t) continue;",
982
- " ts.push(t);",
983
- " js.push(j);",
984
- " }",
985
- " mono = true;",
986
- " for (let i = 1; i < ts.length; i++) if (!(ts[i - 1] <= ts[i])) mono = false;",
987
- " pa = [a0];",
988
- " }",
989
- " if (a0 === E || bad || q === E) F();",
990
- " if (!mono) {",
991
- " let a = a0;",
992
- " for (let i = 0; i < ts.length; i++) if (test(ts[i], q)) a = step(a, js[i]);",
993
- " return result(a);",
994
- " }",
995
- " let lo = 0, hi = ts.length;",
996
- " while (lo < hi) {",
997
- " const h = (lo + hi) >> 1;",
998
- " if (test(ts[h], q)) lo = h + 1;",
999
- " else hi = h;",
1000
- " }",
1001
- " for (let i = pa.length - 1; i < lo; i++) pa.push(step(pa[i], js[i]));",
1002
- " return result(pa[lo]);",
1003
- " };",
1004
- "}"
1005
- ].join("\n"),
1006
- B: "function B(x) { return typeof x === \"boolean\" ? x : F(); }",
1007
- U: "function U(x) { return typeof x === \"number\" ? x : F(); }",
1008
- X: "function X(t, n) { return t >= 0 && t < n ? t : F(); }",
1009
- I: "function I(t, n) { return Number.isInteger(t) && t >= 0 && t < n ? t : F(); }",
1010
- K: "function K(k, n) { return typeof k === \"string\" && /^(0|[1-9]\\d*)$/.test(k) && +k < n ? +k : F(); }",
1011
- V: "function V(l, s) { if (!l.some((x) => x[1] === E)) return l; if (s) F(); return l.filter((x) => x[1] !== E); }",
1012
- SB: [
1013
- "function SB(l, s) {",
1014
- " const o = V(l, s);",
1015
- " const t = o.length > 0 ? typeof o[0][1] : \"number\";",
1016
- " for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1017
- " return o.slice().sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
1018
- "}"
1019
- ].join("\n"),
1020
- SK: [
1021
- "function SK(l, s, f) {",
1022
- " const o = V(l, s).map((x) => [x, f(x[1])]);",
1023
- " const t = o.length > 0 ? typeof o[0][1] : \"number\";",
1024
- " for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1025
- " return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0)).map((x) => x[0]);",
1026
- "}"
1027
- ].join("\n"),
1028
- TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
1029
- VW: [
1030
- "function VW(l) {",
1031
- " let o = l.vw;",
1032
- " if (o === undefined) {",
1033
- " o = l.some((x) => x[1] === E) ? l.filter((x) => x[1] !== E) : l;",
1034
- " l.vw = o;",
1035
- " }",
1036
- " return o;",
1037
- "}"
1038
- ].join("\n"),
1039
- NT: [
1040
- "function NT(l, i, s) {",
1041
- " const o = VW(l);",
1042
- " if (s && o.length < l.length) F();",
1043
- " return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F();",
1044
- "}"
1045
- ].join("\n"),
1046
- CT: "function CT(l, s) { const o = VW(l); return s && o.length < l.length ? F() : o.length; }",
1047
- FS: "function FS(l, s) { const o = VW(l); return (s && o.length < l.length) || o.length === 0 ? F() : o[0][1]; }",
1048
- SN: [
1049
- "function SN(l, i, v, s) {",
1050
- " const x = V(l, s);",
1051
- " if (!(Number.isSafeInteger(i) && i >= 0 && i < x.length && v !== undefined)) F();",
1052
- " const o = x.slice();",
1053
- " o[i] = [x[i][0], v];",
1054
- " return o;",
1055
- "}"
1056
- ].join("\n"),
1057
- FI: [
1058
- "function FI(n, v) {",
1059
- " if (!(Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined)) F();",
1060
- " const o = [];",
1061
- " for (let i = 0; i < n; i++) o.push([i, v]);",
1062
- " return o;",
1063
- "}"
1064
- ].join("\n"),
1065
- FN: "function FN(n, v) { return Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined ? n + 0 : F(); }",
1066
- TN: "function TN(n, k) { return Number.isSafeInteger(k) && k >= 0 ? Math.min(n, k + 0) : F(); }",
1067
- LI: [
1068
- "function LI(x, h, t) {",
1069
- " if (typeof x !== \"object\" || x === null || x.kind !== \"vex.list\" || !Array.isArray(x.items)) return undefined;",
1070
- " const o = [];",
1071
- " for (let i = 0; i < x.items.length; i++) {",
1072
- " const it = x.items[i];",
1073
- " const r = typeof it === \"object\" && it !== null ? it.result : undefined;",
1074
- " if (typeof r !== \"object\" || r === null || typeof it.key !== \"string\") return undefined;",
1075
- " if (r.ok === true) {",
1076
- " const v = h > 0 ? LI(r.value, h - 1, t) : r.value !== undefined && t(r.value) ? r.value : undefined;",
1077
- " if (v === undefined) return undefined;",
1078
- " o.push([it.key, v]);",
1079
- " } else {",
1080
- " const e = r.error;",
1081
- " if (r.ok !== false || typeof e !== \"object\" || e === null || typeof e.code !== \"string\" || typeof e.kind !== \"string\" || typeof e.message !== \"string\" || !Array.isArray(e.path) || e.kind === \"depth\") return undefined;",
1082
- " o.push([it.key, E]);",
1083
- " }",
1084
- " }",
1085
- " return o;",
1086
- "}"
1087
- ].join("\n"),
1088
- LA: [
1089
- "function LA(x, h, t) {",
1090
- " if (!Array.isArray(x)) return undefined;",
1091
- " const o = [];",
1092
- " for (let i = 0; i < x.length; i++) {",
1093
- " const y = x[i];",
1094
- " if (y === VEX_ERROR) {",
1095
- " o.push([i, E]);",
1096
- " continue;",
1097
- " }",
1098
- " const v = h > 0 ? LA(y, h - 1, t) : y !== undefined && t(y) ? y : undefined;",
1099
- " if (v === undefined) return undefined;",
1100
- " o.push([i, v]);",
1101
- " }",
1102
- " return o;",
1103
- "}"
1104
- ].join("\n"),
1105
- DR: "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
1106
- AP: "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
1107
- AO: "function AO(l, o, s, w) { if (l !== w) return AP(l, o, s); const a = V(o, s), n = a.length; for (let i = 0; i < n; i++) l.push(a[i]); return l; }",
1108
- AW: "function AW(l, s, w) { if (l === w) return l; const a = V(l, s), o = []; for (let i = 0; i < a.length; i++) o.push(a[i]); return o; }",
1109
- AI: "function AI(l, x, s, w) { const o = AW(l, s, w); o.push(x); return o; }",
1110
- WO: [
1111
- "function WO(l, o, s) {",
1112
- " const a = V(l, s), g = new Map();",
1113
- " for (const [k] of V(o, s)) g.set(`${k}`, 1);",
1114
- " return a.filter((x) => !g.has(`${x[0]}`));",
1115
- "}"
1116
- ].join("\n"),
1117
- FL: [
1118
- "function FL(l, s) {",
1119
- " const o = [];",
1120
- " for (const [, v] of V(l, s)) for (const x of V(v, s)) o.push(x);",
1121
- " return o;",
1122
- "}"
1123
- ].join("\n"),
1124
- GT: [
1125
- "function GT(l, k, s) {",
1126
- " let x = l.ix;",
1127
- " if (x === undefined) {",
1128
- " x = new Map();",
1129
- " x.n = 0;",
1130
- " l.ix = x;",
1131
- " }",
1132
- " if (x.n !== l.length) {",
1133
- " for (let i = x.n; i < l.length; i++) {",
1134
- " const [q, v] = l[i], t = `${q}`;",
1135
- " if (v === E) x.set(E, E);",
1136
- " else if (!x.has(t)) x.set(t, v);",
1137
- " }",
1138
- " x.n = l.length;",
1139
- " }",
1140
- " return (s && x.has(E)) || typeof k !== \"string\" || !x.has(k) ? F() : x.get(k);",
1141
- "}"
1142
- ].join("\n"),
1143
- IX: [
1144
- "function IX(l, x, s) {",
1145
- " const o = V(l, s);",
1146
- " if (typeof x !== \"number\" && typeof x !== \"string\" && typeof x !== \"boolean\") F();",
1147
- " for (let i = 0; i < o.length; i++) if (o[i][1] === x) return i;",
1148
- " return F();",
1149
- "}"
1150
- ].join("\n"),
1151
- KC: [
1152
- "function KC(a, n) {",
1153
- " if (!Array.isArray(a) || a.length !== n) C(\"the keys are not an array with one key for each record\");",
1154
- " const m = new Map();",
1155
- " for (let i = 0; i < n; i++) {",
1156
- " const k = a[i];",
1157
- " if (typeof k !== \"string\" || m.has(k)) C(`the key ${i} is not a text of its own`);",
1158
- " m.set(k, i);",
1159
- " }",
1160
- " return m;",
1161
- "}"
1162
- ].join("\n"),
1163
- KP: "function KP(m, k) { const i = m.get(k); return i === undefined ? F() : i; }",
1164
- PT: [
1165
- "function PT(p, n) {",
1166
- " if (!Array.isArray(p) || p.length !== n) C(\"the parents are not an array with one parent for each record\");",
1167
- " const P = new Int32Array(n), s = new Uint8Array(n);",
1168
- " for (let i = 0; i < n; i++) {",
1169
- " const x = p[i];",
1170
- " if (x !== -1 && !(Number.isInteger(x) && x >= 0 && x < n)) C(`the parent of the record ${i} is not -1 or the position of a record`);",
1171
- " P[i] = x;",
1172
- " }",
1173
- " for (let i = 0; i < n; i++) {",
1174
- " let k = i;",
1175
- " while (k >= 0 && s[k] === 0) {",
1176
- " s[k] = 1;",
1177
- " k = P[k];",
1178
- " }",
1179
- " if (k >= 0 && s[k] === 1) C(`the parents of the record ${i} make a cycle`);",
1180
- " for (k = i; k >= 0 && s[k] === 1; k = P[k]) s[k] = 2;",
1181
- " }",
1182
- " return P;",
1183
- "}"
1184
- ].join("\n"),
1185
- CH: [
1186
- "function CH(P, n) {",
1187
- " const h = [], r = [];",
1188
- " for (let i = 0; i < n; i++) h.push([]);",
1189
- " for (let i = 0; i < n; i++) (P[i] < 0 ? r : h[P[i]]).push(i);",
1190
- " return [h, r];",
1191
- "}"
1192
- ].join("\n"),
1193
- SG: [
1194
- "function SG(P, h, r, n) {",
1195
- " const g = [], s = new Int32Array(n);",
1196
- " for (let i = 0; i < n; i++) g.push(P[i] < 0 ? r : h[P[i]]);",
1197
- " for (const x of [r, ...h]) for (let q = 0; q < x.length; q++) s[x[q]] = q;",
1198
- " return [g, s];",
1199
- "}"
1200
- ].join("\n"),
1201
- PR: [
1202
- "function PR(P, n) {",
1203
- " const f = new Int32Array(n).fill(-1), x = new Int32Array(n), o = new Int32Array(n), at = new Int32Array(n), z = new Int32Array(n);",
1204
- " let r = -1;",
1205
- " for (let i = n - 1; i >= 0; i--) {",
1206
- " const p = P[i];",
1207
- " if (p < 0) {",
1208
- " x[i] = r;",
1209
- " r = i;",
1210
- " } else {",
1211
- " x[i] = f[p];",
1212
- " f[p] = i;",
1213
- " }",
1214
- " }",
1215
- " let m = 0;",
1216
- " for (let k = r; k >= 0; ) {",
1217
- " at[k] = m;",
1218
- " o[m++] = k;",
1219
- " if (f[k] >= 0) k = f[k];",
1220
- " else {",
1221
- " while (k >= 0 && x[k] < 0) k = P[k];",
1222
- " if (k >= 0) k = x[k];",
1223
- " }",
1224
- " }",
1225
- " for (let q = n - 1; q >= 0; q--) {",
1226
- " const k = o[q], p = P[k];",
1227
- " z[k]++;",
1228
- " if (p >= 0) z[p] += z[k];",
1229
- " }",
1230
- " return [o, at, z];",
1231
- "}"
1232
- ].join("\n"),
1233
- UP: "function UP(P, k) { const p = P[k]; return p < 0 ? F() : p; }",
1234
- SO: "function SO(g, s, k, d) { const t = g[k][s[k] + d]; return t === undefined ? F() : t; }",
1235
- AN: "function AN(P, k) { const o = []; for (let p = P[k]; p >= 0; p = P[p]) o.push(p); return o; }",
1236
- BF: "function BF(h, k) { const o = [k]; for (let i = 0; i < o.length; i++) for (const c of h[o[i]]) o.push(c); return o; }",
1237
- GR: [
1238
- "function GR(e, n) {",
1239
- " if (!Array.isArray(e) || e.length !== n) C(\"the edges are not an array with the ends of the edges of each record\");",
1240
- " const O = [], s = new Int32Array(n);",
1241
- " for (let i = 0; i < n; i++) {",
1242
- " const x = e[i];",
1243
- " if (!Array.isArray(x)) C(`the edges of the record ${i} are not an array`);",
1244
- " const o = [];",
1245
- " for (const t of x) {",
1246
- " if (!(Number.isInteger(t) && t >= 0 && t < n)) C(`an edge of the record ${i} does not go to the position of a record`);",
1247
- " if (s[t] !== i + 1) {",
1248
- " s[t] = i + 1;",
1249
- " o.push(t);",
1250
- " }",
1251
- " }",
1252
- " O.push(o);",
1253
- " }",
1254
- " return O;",
1255
- "}"
1256
- ].join("\n"),
1257
- GI: "function GI(O, n) { const I = []; for (let i = 0; i < n; i++) I.push([]); for (let i = 0; i < n; i++) for (const t of O[i]) I[t].push(i); return I; }",
1258
- RB: "function RB(O, k, n) { const s = new Uint8Array(n), o = [k]; s[k] = 1; for (let i = 0; i < o.length; i++) for (const c of O[o[i]]) if (s[c] === 0) { s[c] = 1; o.push(c); } return o; }",
1259
- DO: "function DO(d) { for (let k = 0; k < d.length; k++) if (d[k] === E) d[k] = VEX_ERROR; return d; }",
1260
- LO: "function LO(l, h) { return { kind: \"vex.list\", items: l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LO(v, h - 1) : v } })) }; }",
1261
- LS: "function LS(l, h) { return Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LS(v, h - 1) : v } }))) }); }",
1262
- LP: "function LP(l, h) { return l.map((x) => (x[1] === E ? VEX_ERROR : h > 0 ? LP(x[1], h - 1) : x[1])); }",
1263
- LV: [
1264
- "function LV(l, h) {",
1265
- " let o = l.vl;",
1266
- " if (o === undefined || o.items.length !== l.length) {",
1267
- " o = Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LV(v, h - 1) : v } }))) });",
1268
- " l.vl = o;",
1269
- " }",
1270
- " return o;",
1271
- "}"
1272
- ].join("\n"),
1273
- SBC: [
1274
- "function SBC(l) {",
1275
- " const t = l.length > 0 ? typeof l[0][1] : \"number\";",
1276
- " for (const [, v] of l) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1277
- " return l.slice().sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
1278
- "}"
1279
- ].join("\n"),
1280
- SKC: [
1281
- "function SKC(l, f) {",
1282
- " const o = l.map((x) => [x, f(x[1])]);",
1283
- " const t = o.length > 0 ? typeof o[0][1] : \"number\";",
1284
- " for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1285
- " return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0)).map((x) => x[0]);",
1286
- "}"
1287
- ].join("\n"),
1288
- SBI: [
1289
- "function SBI(l) {",
1290
- " const n = l.length, t = n > 0 ? typeof l[0][1] : \"number\";",
1291
- " for (let i = 0; i < n; i++) { const v = l[i][1]; if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F(); }",
1292
- " if (n > 64) l.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
1293
- " else for (let i = 1; i < n; i++) {",
1294
- " const x = l[i], v = x[1];",
1295
- " let j = i - 1;",
1296
- " for (; j >= 0 && l[j][1] > v; j--) l[j + 1] = l[j];",
1297
- " l[j + 1] = x;",
1298
- " }",
1299
- " return l;",
1300
- "}"
1301
- ].join("\n"),
1302
- SKI: [
1303
- "function SKI(l, f) {",
1304
- " const n = l.length, k = new Array(n);",
1305
- " for (let i = 0; i < n; i++) k[i] = f(l[i][1]);",
1306
- " const t = n > 0 ? typeof k[0] : \"number\";",
1307
- " for (const v of k) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
1308
- " if (n > 64) {",
1309
- " const o = new Array(n), c = l.slice();",
1310
- " for (let i = 0; i < n; i++) o[i] = i;",
1311
- " o.sort((a, b) => (k[a] < k[b] ? -1 : k[a] > k[b] ? 1 : 0));",
1312
- " for (let i = 0; i < n; i++) l[i] = c[o[i]];",
1313
- " } else for (let i = 1; i < n; i++) {",
1314
- " const v = k[i], x = l[i];",
1315
- " let j = i - 1;",
1316
- " for (; j >= 0 && k[j] > v; j--) { k[j + 1] = k[j]; l[j + 1] = l[j]; }",
1317
- " k[j + 1] = v; l[j + 1] = x;",
1318
- " }",
1319
- " return l;",
1320
- "}"
1321
- ].join("\n"),
1322
- TKC: "function TKC(l, n) { return Number.isSafeInteger(n) && n >= 0 ? l.slice(0, n) : F(); }",
1323
- DRC: "function DRC(l, n) { return Number.isSafeInteger(n) && n >= 0 ? l.slice(n) : F(); }",
1324
- AWC: "function AWC(l, w) { if (l === w) return l; const o = []; for (let i = 0; i < l.length; i++) o.push(l[i]); return o; }",
1325
- AIC: "function AIC(l, x, w) { if (l !== w) { const o = []; for (let i = 0; i < l.length; i++) o.push(l[i]); l = o; } l.push(x); return l; }",
1326
- KS: "function KS(l, n) { l.length = n; if (l.ix !== undefined) l.ix = undefined; if (l.vl !== undefined) l.vl = undefined; }",
1327
- AOC: "function AOC(l, o, w) { if (l !== w) return l.concat(o); const n = o.length; for (let i = 0; i < n; i++) l.push(o[i]); return l; }",
1328
- WOC: [
1329
- "function WOC(l, o) {",
1330
- " const g = new Map();",
1331
- " for (const [k] of o) g.set(`${k}`, 1);",
1332
- " return l.filter((x) => !g.has(`${x[0]}`));",
1333
- "}"
1334
- ].join("\n"),
1335
- FLC: "function FLC(l) { const o = []; for (const [, v] of l) for (const x of v) o.push(x); return o; }",
1336
- IXC: [
1337
- "function IXC(l, x) {",
1338
- " if (typeof x !== \"number\" && typeof x !== \"string\" && typeof x !== \"boolean\") F();",
1339
- " for (let i = 0; i < l.length; i++) if (l[i][1] === x) return i;",
1340
- " return F();",
1341
- "}"
1342
- ].join("\n"),
1343
- NTC: "function NTC(l, i) { return Number.isSafeInteger(i) && i >= 0 && i < l.length ? l[i][1] : F(); }",
1344
- SNC: [
1345
- "function SNC(l, i, v) {",
1346
- " if (!(Number.isSafeInteger(i) && i >= 0 && i < l.length && v !== undefined)) F();",
1347
- " const o = l.slice();",
1348
- " o[i] = [l[i][0], v];",
1349
- " return o;",
1350
- "}"
1351
- ].join("\n"),
1352
- FSC: "function FSC(l) { return l.length === 0 ? F() : l[0][1]; }",
1353
- LOC: "function LOC(l, h) { return { kind: \"vex.list\", items: l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LOC(v, h - 1) : v } })) }; }",
1354
- LSC: "function LSC(l, h) { return Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LSC(v, h - 1) : v } }))) }); }",
1355
- LPC: "function LPC(l, h) { return l.map(h > 0 ? (x) => LPC(x[1], h - 1) : (x) => x[1]); }",
1356
- LVC: [
1357
- "function LVC(l, h) {",
1358
- " let o = l.vl;",
1359
- " if (o === undefined || o.items.length !== l.length) {",
1360
- " o = Object.freeze({ kind: \"vex.list\", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LVC(v, h - 1) : v } }))) });",
1361
- " l.vl = o;",
1362
- " }",
1363
- " return o;",
1364
- "}"
1365
- ].join("\n"),
1366
- LC: "function LC(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LO(d[k], h); return d; }",
1367
- LCP: "function LCP(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LP(d[k], h); return d; }",
1368
- SH: [
1369
- "function SH(n, cols, own, budget) {",
1370
- " const pg = (c, k) => {",
1371
- " const o = own[c];",
1372
- " const g = o === undefined ? undefined : o[k];",
1373
- " return g === undefined ? cols[c] : g;",
1374
- " };",
1375
- " const D = [];",
1376
- " for (let c = 0; c < cols.length; c++) {",
1377
- " const d = new Array(n);",
1378
- " for (let k = 0; k < n; k++) if (pg(c, k) === undefined) d[k] = E;",
1379
- " D.push(d);",
1380
- " }",
1381
- " const seen = new Map(), stack = [], FILL = {};",
1382
- " let top = FILL, missed = false, next = 0, used = 0;",
1383
- " const start = (c, k) => {",
1384
- " const i = next++;",
1385
- " const v = { id: c * n + k, c, k, i, low: i, re: false, r: E, p: false, need: undefined };",
1386
- " seen.set(v.id, v);",
1387
- " stack.push(v);",
1388
- " return v;",
1389
- " };",
1390
- " const ev = (v, g) => {",
1391
- " const caller = top;",
1392
- " v.p = false;",
1393
- " v.need = undefined;",
1394
- " top = v;",
1395
- " used += g[1];",
1396
- " const x = g[0](v.k);",
1397
- " used -= g[1];",
1398
- " top = caller;",
1399
- " if (!v.p) v.r = x;",
1400
- " };",
1401
- " const end = (v, caller) => {",
1402
- " if (v.low === v.i) {",
1403
- " const ms = stack.splice(stack.lastIndexOf(v));",
1404
- " const cycle = ms.length > 1 || v.re;",
1405
- " for (const m of ms) {",
1406
- " seen.delete(m.id);",
1407
- " D[m.c][m.k] = cycle ? E : m.r;",
1408
- " }",
1409
- " } else if (caller !== undefined) caller.low = Math.min(caller.low, v.low);",
1410
- " };",
1411
- " const r = (c, k) => {",
1412
- " const d = D[c][k];",
1413
- " if (d !== undefined) return d;",
1414
- " const caller = top;",
1415
- " if (caller === FILL) {",
1416
- " missed = true;",
1417
- " return E;",
1418
- " }",
1419
- " if (caller.p) return E;",
1420
- " const s = seen.get(c * n + k);",
1421
- " if (s !== undefined) {",
1422
- " caller.low = Math.min(caller.low, s.i);",
1423
- " s.re = true;",
1424
- " return E;",
1425
- " }",
1426
- " const g = pg(c, k);",
1427
- " const v = start(c, k);",
1428
- " if (used + g[1] > budget) {",
1429
- " caller.p = true;",
1430
- " caller.need = v;",
1431
- " return E;",
1432
- " }",
1433
- " ev(v, g);",
1434
- " if (v.p) {",
1435
- " caller.p = true;",
1436
- " caller.need = v;",
1437
- " return E;",
1438
- " }",
1439
- " end(v, caller);",
1440
- " const x = D[c][k];",
1441
- " return x === undefined ? E : x;",
1442
- " };",
1443
- " const drive = (c, k) => {",
1444
- " const work = [start(c, k)];",
1445
- " for (let v = work[0]; v !== undefined; v = work[work.length - 1]) {",
1446
- " ev(v, pg(v.c, v.k));",
1447
- " if (v.p) {",
1448
- " for (let q = v.need; q !== undefined; q = q.need) work.push(q);",
1449
- " continue;",
1450
- " }",
1451
- " work.pop();",
1452
- " end(v, work[work.length - 1]);",
1453
- " }",
1454
- " };",
1455
- " const f = (c, k) => {",
1456
- " if (D[c][k] !== undefined) return;",
1457
- " missed = false;",
1458
- " const x = pg(c, k)[0](k);",
1459
- " if (missed) drive(c, k);",
1460
- " else D[c][k] = x;",
1461
- " };",
1462
- " const o = (c) => {",
1463
- " const d = D[c], out = new Array(n);",
1464
- " for (let k = 0; k < n; k++) out[k] = d[k] === E ? VEX_ERROR : d[k];",
1465
- " return out;",
1466
- " };",
1467
- " return { r, f, o };",
1468
- "}"
1469
- ].join("\n"),
1470
- KO: [
1471
- "function KO(h, r, post, back) {",
1472
- " const o = [], st = [];",
1473
- " const push = (x) => {",
1474
- " if (back) for (let q = 0; q < x.length; q++) st.push(x[q]);",
1475
- " else for (let q = x.length - 1; q >= 0; q--) st.push(x[q]);",
1476
- " };",
1477
- " push(r);",
1478
- " while (st.length > 0) {",
1479
- " const k = st.pop();",
1480
- " o.push(k);",
1481
- " push(h[k]);",
1482
- " }",
1483
- " return post ? o.reverse() : o;",
1484
- "}"
1485
- ].join("\n"),
1486
- M1: [
1487
- "function M1(f) {",
1488
- " const m = new Map();",
1489
- " return (k) => {",
1490
- " const s = Object.is(k, -0) ? Z : k;",
1491
- " let r = m.get(s);",
1492
- " if (r === undefined) {",
1493
- " try { r = f(k); } catch { r = E; }",
1494
- " m.set(s, r);",
1495
- " }",
1496
- " return G(r);",
1497
- " };",
1498
- "}"
1499
- ].join("\n"),
1500
- M: [
1501
- "function M(f) {",
1502
- " const root = new Map();",
1503
- " return (...k) => {",
1504
- " let m = root;",
1505
- " for (const x of k.slice(0, -1)) {",
1506
- " const s = Object.is(x, -0) ? Z : x;",
1507
- " let c = m.get(s);",
1508
- " if (c === undefined) m.set(s, (c = new Map()));",
1509
- " m = c;",
1510
- " }",
1511
- " const last = k.at(-1);",
1512
- " const s = Object.is(last, -0) ? Z : last;",
1513
- " let r = m.get(s);",
1514
- " if (r === undefined) {",
1515
- " try { r = f(...k); } catch { r = E; }",
1516
- " m.set(s, r);",
1517
- " }",
1518
- " return G(r);",
1519
- " };",
1520
- "}"
1521
- ].join("\n")
1554
+ ...helperTexts1(),
1555
+ ...helperTexts2(),
1556
+ ...helperTexts3(),
1557
+ ...helperTexts4(),
1558
+ ...helperTexts5()
1522
1559
  };
1523
1560
  /**
1524
1561
  * This list has the globals of JavaScript that the code of a module reads. This code is the helpers, the run and the
@@ -1698,8 +1735,8 @@ const NEEDS = {
1698
1735
  * value takes the place of the recursion, and the helper has no argument `h` (COMPILE.MODULE.SIZE.FLAT).
1699
1736
  */
1700
1737
  const FLAT_LIST_HELPERS = {
1701
- LI: HELPERS.LI.replace("LI(x, h, t)", "LI(x, t)").replace("h > 0 ? LI(r.value, h - 1, t) : ", ""),
1702
- LA: HELPERS.LA.replace("LA(x, h, t)", "LA(x, t)").replace("h > 0 ? LA(y, h - 1, t) : ", "")
1738
+ LI: () => HELPERS.LI().replace("LI(x, h, t)", "LI(x, t)").replace("h > 0 ? LI(r.value, h - 1, t) : ", ""),
1739
+ LA: () => HELPERS.LA().replace("LA(x, h, t)", "LA(x, t)").replace("h > 0 ? LA(y, h - 1, t) : ", "")
1703
1740
  };
1704
1741
  /** The clean form of each helper that gives a list of the code to the outside (COMPILE.MODULE.FUSE.CLEAN). */
1705
1742
  const CLEAN_FORMS = {
@@ -1993,7 +2030,7 @@ function cleanForm(e, fact) {
1993
2030
  }
1994
2031
  }
1995
2032
  /** This function gives the count of the reads of a part that a loop or a function can evaluate again. */
1996
- const again = (n) => n > 0 ? Infinity : 0;
2033
+ const again = (n) => n > 0 ? 1e400 : 0;
1997
2034
  /** This function adds the counts of the reads of the parts `xs`. */
1998
2035
  const refsIn = (xs, name) => xs.reduce((n, x) => n + refsOf(x, name), 0);
1999
2036
  /**
@@ -2066,17 +2103,17 @@ function readCounts(root) {
2066
2103
  const fn = fns.get(e.fn);
2067
2104
  e.args.forEach((x, i) => {
2068
2105
  const param = fn?.params[i];
2069
- visit(x, param === void 0 ? Infinity : refsOf(fn.body, param), fns);
2106
+ visit(x, param === void 0 ? 1e400 : refsOf(fn.body, param), fns);
2070
2107
  });
2071
2108
  return;
2072
2109
  }
2073
2110
  case "fix":
2074
- all(children(e), Infinity);
2111
+ all(children(e), 1e400);
2075
2112
  return;
2076
2113
  case "fold":
2077
2114
  case "iterate": {
2078
2115
  const names = /* @__PURE__ */ new Set([...Object.values(e.init), ...Object.values(e.step)]);
2079
- for (const x of children(e)) visit(x, x === e.result ? reads : names.has(x) ? Infinity : 1, fns);
2116
+ for (const x of children(e)) visit(x, x === e.result ? reads : names.has(x) ? 1e400 : 1, fns);
2080
2117
  return;
2081
2118
  }
2082
2119
  case "each":
@@ -2741,10 +2778,10 @@ var Printer = class {
2741
2778
  const fact = this.#shared.has(e) ? {
2742
2779
  ...f,
2743
2780
  fresh: false,
2744
- reads: Infinity
2781
+ reads: 1e400
2745
2782
  } : {
2746
2783
  ...f,
2747
- reads: this.#reads.get(e) ?? Infinity
2784
+ reads: this.#reads.get(e) ?? 1e400
2748
2785
  };
2749
2786
  const old = out.get(originalOf(e));
2750
2787
  out.set(originalOf(e), old === void 0 ? fact : bothPlaces(old, fact));
@@ -6586,7 +6623,7 @@ function printModule(p, options, run, body) {
6586
6623
  ok: false,
6587
6624
  reason: bad
6588
6625
  };
6589
- const textOf = (h) => h === "S" ? helperS(p.features) : flat && (h === "LI" || h === "LA") ? FLAT_LIST_HELPERS[h] : HELPERS[h];
6626
+ const textOf = (h) => h === "S" ? helperS(p.features) : flat && (h === "LI" || h === "LA") ? FLAT_LIST_HELPERS[h]() : HELPERS[h]();
6590
6627
  const params = `(records, vars${tree ? ", parents" : graph ? ", edges" : ""}${p.runKeys ? ", keys" : ""}) {`;
6591
6628
  const start = [
6592
6629
  " if (!Array.isArray(records)) C(\"the records are not an array\");",