@mark1russell7/vex-compiler 0.12.0 → 0.13.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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/facts.ts","../src/loop.ts","../src/tiers.ts","../src/module.ts","../src/runtime.ts"],"mappings":";;;KAqCY,aAAa;;;;;KAMb,OAAO;;;;;UAYF;WACN,SAAS,SAAS,eAAe;WACjC,OAAO,SAAS,eAAe;;;UAIzB;WACN,mBAAmB;WACnB,MAAM,SAAS,eAAe;WAC9B,aAAa,SAAS,eAAe;WACrC,SAAS;;;;;KC9CR;;KAeA;;KA2BA;WACG;WAAqB;;WACrB;WAAqB;;WACrB;WAAmB;;WACnB;WAAoB;;WACpB;;WACA;WAAqB,gBAAgB;;WACrC;WAAoB,IAAI;WAAiB,eAAe;;WACxD;WAAoB,IAAI;WAAoB,eAAe;;WAK3D;WAAkB;WAAuB,eAAe;WAAkB,iBAAiB;;WAE3F;WAAoB;;WAEpB;WAAuB;WAAuB,gBAAgB,OAAO,OAAO;;;;;;;;;;;KAW/E;WACG;;WACA;WAAwB;WAAwB;aAAoB,MAAM;aAAgB,MAAM;;;WAChG;WAAqB;;;KAGxB;WAEG;WAAmB;WAAoB,OAAO;;WAE9C;WAAmB;WAAoB,OAAO;;WAE9C;WAAqB,IAAI;WAAgB;WAAyB,MAAM;;WAExE;WAAoB,MAAM;;WAC1B;WAAkB,MAAM;WAAgB,cAAc;WAAkB,aAAa;;WAKrF;WAAoB;WAAwB;WAAuB;WAAwB;WAAwB,MAAM;WAAgB,eAAe;;WAExJ;WAAqB;WAAwB,eAAe;;WAE5D;WAAsB;WAAwB,eAAe;;WAE7D;;WAEA;WAAoB;WAAoB,OAAO;;WAC/C;WAAsB,OAAO;;;UAG3B;WACN,eAAe;WACf;;WAEA;;;;;KCrHC,kBAAkB;WAA4B,SAAS;;;;;;KAUvD;WACG;WAAmB,SAAS;WAAs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,cAAc;EACjC,OAAO;EACP,KAAK;EACL,IAAI,0BAA0B;EAC9B,KAAK,yBAAyB;EAC9B,OAAO;EACP,IAAI,cAAc,eAAe,GAAG,OAAO,gBAAgB,eAAe;EAC1E,IAAI,MAAM,SAAS,sCAAsC;;;wBAI3C,cAAc,SAAS,iBAAiB"}
package/build/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { CELL_KIND, MAX_NESTING, addrExprs, axisTargets, children, compile, findMethod, fixAddr, fromDocument, isDocument, isFieldName, isListOp, isMove, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, rec, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
1
+ import { CELL_KIND, MAX_NESTING, addrExprs, app, axisTargets, children, compile, findMethod, fixAddr, fromDocument, isDocument, isFieldName, isListOp, isMove, isOpName, isStackOverflow, isVexList, liftsAt, listOf, lit, matchesKind, moveExpr, nestingOf, ok, rec, resolveAddr, safeIs, shapeError, vexList } from "@mark1russell7/vex";
2
2
  //#region src/facts.ts
3
3
  /**
4
4
  * The facts of the options that the printer uses (docs/COMPILER.md §13.2). For each op site, they give the domain
@@ -68,7 +68,7 @@ function returnsOf(domain, kind) {
68
68
  return kind === "any" ? void 0 : kind;
69
69
  }
70
70
  /** This function tells if an argument of a kind fits a parameter, or `undefined` if the kind of the argument is unknown. */
71
- function fits(domain, param, arg) {
71
+ function fits$1(domain, param, arg) {
72
72
  if (param === void 0 || param === "any") return true;
73
73
  if (arg === void 0) return void 0;
74
74
  return arg === (param === "domain" ? kindOfDomain(domain) : param);
@@ -100,7 +100,7 @@ function factsOf(options) {
100
100
  continue;
101
101
  }
102
102
  if (d.fromScalar !== void 0 && args.some((a, j) => a !== "string" && a !== "boolean" && liftsAt(e.spec.liftScalar, j))) return void 0;
103
- const ok = args.map((a, j) => fits(d, e.spec.params?.[j], a));
103
+ const ok = args.map((a, j) => fits$1(d, e.spec.params?.[j], a));
104
104
  if (ok.includes(void 0)) return void 0;
105
105
  return {
106
106
  t: "fn",
@@ -1961,8 +1961,9 @@ function keyOrderOf(d) {
1961
1961
  *
1962
1962
  * The printer takes a part of the language. It takes an array space or a tree space of records, and the fields and
1963
1963
  * the variables of the schema. It takes the axes `all`, `others` and `window`. A tree space also has the move `parent` and the
1964
- * axes of a tree (COMPILE.MODULE.TREE). The kinds of each op site must decide its op. Another construct gives a
1965
- * reason, not code.
1964
+ * axes of a tree (COMPILE.MODULE.TREE). A list is an array of pairs of a key and a value. The list ops, list literals
1965
+ * and lists as names of loops are in a module (COMPILE.MODULE.LIST). The kinds of each op site must decide its op.
1966
+ * Another construct gives a reason, not code.
1966
1967
  *
1967
1968
  * `emitSheetModule` prints the formula columns of a sheet as one module (COMPILE.MODULE.SHEET). Each column is an array
1968
1969
  * of results, and the run fills the arrays in the static order of `SheetRun.fill`. A read of a cell that is not finished
@@ -2011,6 +2012,45 @@ const HELPERS = {
2011
2012
  ].join("\n"),
2012
2013
  TK: "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
2013
2014
  NT: "function NT(l, i, s) { const o = V(l, s); return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F(); }",
2015
+ DR: "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
2016
+ AP: "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
2017
+ WO: [
2018
+ "function WO(l, o, s) {",
2019
+ " const a = V(l, s), g = new Map();",
2020
+ " for (const [k] of V(o, s)) g.set(`${k}`, 1);",
2021
+ " return a.filter((x) => !g.has(`${x[0]}`));",
2022
+ "}"
2023
+ ].join("\n"),
2024
+ FL: [
2025
+ "function FL(l, s) {",
2026
+ " const o = [];",
2027
+ " for (const [, v] of V(l, s)) for (const x of V(v, s)) o.push(x);",
2028
+ " return o;",
2029
+ "}"
2030
+ ].join("\n"),
2031
+ GT: [
2032
+ "function GT(l, k, s) {",
2033
+ " let x = l.ix;",
2034
+ " if (x === undefined) {",
2035
+ " x = new Map();",
2036
+ " for (const [q, v] of l) {",
2037
+ " const t = `${q}`;",
2038
+ " if (v === E) x.set(E, E);",
2039
+ " else if (!x.has(t)) x.set(t, v);",
2040
+ " }",
2041
+ " l.ix = x;",
2042
+ " }",
2043
+ " return (s && x.has(E)) || typeof k !== \"string\" || !x.has(k) ? F() : x.get(k);",
2044
+ "}"
2045
+ ].join("\n"),
2046
+ IX: [
2047
+ "function IX(l, x, s) {",
2048
+ " const o = V(l, s);",
2049
+ " if (typeof x !== \"number\" && typeof x !== \"string\" && typeof x !== \"boolean\") F();",
2050
+ " for (let i = 0; i < o.length; i++) if (o[i][1] === x) return i;",
2051
+ " return F();",
2052
+ "}"
2053
+ ].join("\n"),
2014
2054
  PT: [
2015
2055
  "function PT(p, n) {",
2016
2056
  " if (!Array.isArray(p) || p.length !== n) C(\"the parents are not an array with one parent for each record\");",
@@ -2334,6 +2374,12 @@ const NEEDS = {
2334
2374
  SB: ["V", "F"],
2335
2375
  TK: ["V", "F"],
2336
2376
  NT: ["V", "F"],
2377
+ DR: ["V", "F"],
2378
+ AP: ["V"],
2379
+ WO: ["V"],
2380
+ FL: ["V"],
2381
+ GT: ["F"],
2382
+ IX: ["V", "F"],
2337
2383
  PT: ["C"],
2338
2384
  UP: ["F"],
2339
2385
  SO: ["F"],
@@ -2387,15 +2433,82 @@ const BOOLEAN_LIST_OPS = /* @__PURE__ */ new Set([
2387
2433
  "all",
2388
2434
  "none"
2389
2435
  ]);
2390
- /** The list ops that build a list from lists (LIST.BUILD), `get` (LIST.GET) and `indexOf` (LIST.INDEXOF). A module gives a reason for them. */
2436
+ /** The list ops over lists of `#build`: append, without and flat (LIST.BUILD), get (LIST.GET) and indexOf (LIST.INDEXOF). */
2391
2437
  const BUILD_OPS = /* @__PURE__ */ new Set([
2392
- "drop",
2393
2438
  "append",
2394
- "flat",
2395
2439
  "without",
2440
+ "flat",
2396
2441
  "get",
2397
2442
  "indexOf"
2398
2443
  ]);
2444
+ /** The helper of each op of `#build`. */
2445
+ const BUILD_HELPERS = {
2446
+ append: "AP",
2447
+ without: "WO",
2448
+ flat: "FL",
2449
+ get: "GT",
2450
+ indexOf: "IX"
2451
+ };
2452
+ /** The list ops whose value is a list (LIST.SORT, LIST.TAKE and LIST.BUILD). */
2453
+ const LIST_RESULTS = /* @__PURE__ */ new Set([
2454
+ "sortBy",
2455
+ "take",
2456
+ "drop",
2457
+ "append",
2458
+ "without",
2459
+ "flat"
2460
+ ]);
2461
+ /**
2462
+ * This function tells if an expression gives a list. An each, a list literal and a list op of `LIST_RESULTS` give a
2463
+ * list. A name of a list, and a let or a loop whose result is a list, give a list too. `isName` tells if a name outside the expression holds a list.
2464
+ */
2465
+ function listy(e, isName) {
2466
+ switch (e.tag) {
2467
+ case "each": return true;
2468
+ case "lit": return isVexList(e.value);
2469
+ case "app": return LIST_RESULTS.has(e.op);
2470
+ case "var": return isName(e.name);
2471
+ case "let": return listy(e.body, namesIn(e.bind, isName));
2472
+ case "fold":
2473
+ case "iterate": return listy(e.result, namesIn(e.init, isName));
2474
+ case "call":
2475
+ case "ext":
2476
+ case "fn":
2477
+ case "rec":
2478
+ case "ref": return false;
2479
+ }
2480
+ }
2481
+ /** This function tells if a name holds a list in the scope of some bindings. A binding hides a name outside, and a function is not a list. */
2482
+ function namesIn(bind, isName) {
2483
+ const lists = new Map(Object.entries(bind).map(([name, x]) => [name, x.tag !== "fn" && listy(x, isName)]));
2484
+ return (name) => lists.get(name) ?? isName(name);
2485
+ }
2486
+ /** This function tells if an expression has a list literal with an item, so a key of the list can be a text (LIST.LITERAL). */
2487
+ function hasKeyedLiteral(e) {
2488
+ return e.tag === "lit" && isVexList(e.value) && e.value.items.length > 0 || children(e).some(hasKeyedLiteral);
2489
+ }
2490
+ /** This function gives the facts of the list of `append` from the facts of its two lists. An empty list fits each list. */
2491
+ function joinLists(a, b) {
2492
+ if (a.empty === true) return b;
2493
+ if (b.empty === true) return a;
2494
+ if (a.inner === void 0 !== (b.inner === void 0)) throw new NotStandalone("\"append\" of a list of lists and a list of values");
2495
+ const texts = a.texts === true || b.texts === true;
2496
+ return {
2497
+ item: join(a.item, b.item),
2498
+ inner: a.inner === void 0 ? void 0 : joinLists(a.inner, b.inner),
2499
+ texts
2500
+ };
2501
+ }
2502
+ /**
2503
+ * This function tells if a list of a step fits the list of a name of a loop (LOOP.LIST). The items of the step have the
2504
+ * kind of the items of the name, if the name has one. The name also has the text keys and the lists of lists of the step.
2505
+ */
2506
+ function fits(name, step) {
2507
+ if (step.empty === true) return true;
2508
+ if (name.item !== void 0 && step.item !== name.item) return false;
2509
+ if (step.texts === true && name.texts !== true) return false;
2510
+ return name.inner === void 0 ? step.inner === void 0 : step.inner !== void 0 && fits(name.inner, step.inner);
2511
+ }
2399
2512
  /** This function tells if the step of a list op throws for a value of the kind `kind`, which is the wrong kind. */
2400
2513
  const stepThrows = (op, kind) => NUMBER_STEPS.has(op) && kind !== "number" || BOOLEAN_LIST_OPS.has(op) && kind !== "boolean";
2401
2514
  /** The list ops whose step takes a number. */
@@ -2906,11 +3019,27 @@ var Printer = class {
2906
3019
  if (c.v.list === void 0) throw new NotStandalone("a list op on a value that is not a list");
2907
3020
  return c;
2908
3021
  }
2909
- /** This method tells if an expression gives a list at a place that takes one: an each, sortBy, take or a name of a list. */
3022
+ /** This method tells if an expression gives a list at a place that takes one (`listy`). */
2910
3023
  #isList(e, ctx) {
2911
- if (e.tag === "each") return true;
2912
- if (e.tag === "app") return e.op === "sortBy" || e.op === "take";
2913
- return e.tag === "var" && ctx.env.get(e.name)?.list !== void 0;
3024
+ return listy(e, (name) => ctx.env.get(name)?.list !== void 0);
3025
+ }
3026
+ /** This method prints a list literal (LIST.LITERAL) as an array of pairs of a key and a value. An error item holds the sentinel. */
3027
+ #listLiteral(l) {
3028
+ const values = l.items.map((it) => it.result.ok ? literal(it.result.value) : void 0);
3029
+ const kinds = values.flatMap((x) => x === void 0 ? [] : [x.kind]);
3030
+ const code = `[${l.items.map((it, i) => `[${JSON.stringify(it.key)}, ${values[i]?.code ?? "E"}]`).join(", ")}]`;
3031
+ const has = l.items.length > 0;
3032
+ return bare({
3033
+ code,
3034
+ kind: void 0,
3035
+ safe: true,
3036
+ deps: NO_DEPS,
3037
+ list: {
3038
+ item: kinds.reduce(join, kinds[0]),
3039
+ texts: has,
3040
+ empty: !has
3041
+ }
3042
+ });
2914
3043
  }
2915
3044
  /**
2916
3045
  * This method tells if a part of a loop over the targets is a column of the run. Its value depends only on the
@@ -2992,7 +3121,7 @@ var Printer = class {
2992
3121
  }
2993
3122
  #node(e, ctx) {
2994
3123
  switch (e.tag) {
2995
- case "lit": return bare(literal(e.value));
3124
+ case "lit": return isVexList(e.value) ? this.#listLiteral(e.value) : bare(literal(e.value));
2996
3125
  case "ref": return this.#ref(e, ctx);
2997
3126
  case "var": return bare(this.#var(e.name, ctx));
2998
3127
  case "let": return this.#let(e, ctx);
@@ -3207,7 +3336,7 @@ var Printer = class {
3207
3336
  * A binding is a variable. A binding with an error is an error only where the body reads its name, so the variable
3208
3337
  * can hold the sentinel. A binding that is a constant of the run is the variable of that constant.
3209
3338
  */
3210
- #let(e, ctx) {
3339
+ #let(e, ctx, wrap) {
3211
3340
  const env = new Map(ctx.env);
3212
3341
  const fns = new Map(ctx.fns);
3213
3342
  const s = [];
@@ -3248,8 +3377,10 @@ var Printer = class {
3248
3377
  root?.names.add(b);
3249
3378
  deps = /* @__PURE__ */ new Set([...deps, ...c.v.deps]);
3250
3379
  }
3251
- if (root !== void 0) root.root = e.body;
3252
- const body = this.expr(e.body, {
3380
+ const inner = wrap === void 0 ? e.body : wrap(e.body);
3381
+ if (this.#wantList.has(e)) this.#wantList.add(inner);
3382
+ if (root !== void 0) root.root = inner;
3383
+ const body = this.expr(inner, {
3253
3384
  ...ctx,
3254
3385
  env,
3255
3386
  fns
@@ -3396,10 +3527,12 @@ var Printer = class {
3396
3527
  if (op === "and" || op === "or") return this.#andOr(op === "and", args, ctx);
3397
3528
  if (op === "ifError") return this.#ifError(args, ctx);
3398
3529
  const [head] = args;
3399
- if (LIST_OPS.has(op) && head?.tag === "each") return this.#list(op, head, args.slice(1), ctx);
3400
- if (head !== void 0 && (op === "sortBy" || op === "take")) return this.#order(op, head, args.slice(1), ctx);
3401
- if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) throw new NotStandalone(`the list op "${op}" is not in a module yet`);
3402
- if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, args.slice(1), ctx);
3530
+ const rest = args.slice(1);
3531
+ if (LIST_OPS.has(op) && head?.tag === "each" && !this.#isList(head.body, ctx)) return this.#list(op, head, rest, ctx);
3532
+ if (LIST_OPS.has(op) && head?.tag === "let" && rest.every((x) => x.tag === "lit")) return this.#let(head, ctx, (body) => app(op, body, ...rest));
3533
+ if (head !== void 0 && (op === "sortBy" || op === "take" || op === "drop" && this.#isList(head, ctx))) return this.#order(op, head, rest, ctx);
3534
+ if (head !== void 0 && BUILD_OPS.has(op) && this.#isList(head, ctx)) return this.#build(op, head, rest, ctx);
3535
+ if (head !== void 0 && (LIST_OPS.has(op) || op === "nth") && this.#isList(head, ctx)) return this.#overList(op, head, rest, ctx);
3403
3536
  const raw = args.map((x) => this.expr(x, ctx));
3404
3537
  const deps = depsOf(...raw);
3405
3538
  const values = this.#inOrder(this.#settle(raw, deps, args), true);
@@ -3597,6 +3730,7 @@ var Printer = class {
3597
3730
  if (base.t === "list") {
3598
3731
  if (at.length > 0) throw new NotStandalone("the axis \"list\" with an address");
3599
3732
  const list = this.#hoist(this.#listOf(base.list, ctx), hasLoop(base.list));
3733
+ if (list.v.list.texts === true) throw new NotStandalone("the axis \"list\" over a list with the keys of a literal");
3600
3734
  const a = this.fresh("A");
3601
3735
  const q = this.fresh("q");
3602
3736
  const head = reverse ? `for (let ${q} = ${a}.length - 1; ${q} >= 0; ${q}--) {` : `for (let ${q} = 0; ${q} < ${a}.length; ${q}++) {`;
@@ -3723,7 +3857,7 @@ var Printer = class {
3723
3857
  const { level } = inner;
3724
3858
  const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
3725
3859
  const body = this.#hoist(this.expr(target.body, inner), hasLoop(target.body));
3726
- if (op === "count" && fixed && tests.length === 0 && body.v.safe) return bare(plainValue("n", "number"));
3860
+ if (op === "count" && fixed && tests.length === 0 && body.v.safe && !body.v.deps.has(CELLS)) return bare(plainValue("n", "number"));
3727
3861
  const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== level));
3728
3862
  const r = this.#reduction(op, body.v.kind);
3729
3863
  const { v } = r;
@@ -3851,6 +3985,7 @@ var Printer = class {
3851
3985
  const at = e.at ?? [];
3852
3986
  const { s, j, head, key, check, skip, start, startSafe, tests, inner } = this.#targetLoop(e.axis, at, ctx);
3853
3987
  const testCodes = tests.map((t) => this.#hoist(this.expr(t, inner), hasLoop(t)));
3988
+ if (this.#wantList.has(e)) this.#wantList.add(e.body);
3854
3989
  const body = this.#hoist(this.expr(e.body, inner), hasLoop(e.body));
3855
3990
  const deps = new Set([...start, ...[body, ...testCodes].flatMap((x) => [...x.v.deps])].filter((d) => d !== inner.level));
3856
3991
  const list = this.fresh("L");
@@ -3888,33 +4023,68 @@ var Printer = class {
3888
4023
  kind: void 0,
3889
4024
  safe: startSafe,
3890
4025
  deps,
3891
- list: { item: body.v.kind }
4026
+ list: {
4027
+ item: body.v.kind,
4028
+ inner: body.v.list
4029
+ }
3892
4030
  }
3893
4031
  };
3894
4032
  }
3895
4033
  /**
3896
- * This method prints sortBy and take (LIST.SORT, LIST.TAKE): a new array from the array of a list. An error item is
3897
- * not in the new list, and with strict it is the error of the op.
4034
+ * This method prints sortBy, take and drop (LIST.SORT, LIST.TAKE, LIST.DROP): a new array from the array of a list. An
4035
+ * error item is not in the new list, and with strict it is the error of the op.
3898
4036
  */
3899
4037
  #order(op, head, rest, ctx) {
3900
4038
  const list = this.#listOf(head, ctx);
3901
- const exprs = [head, ...op === "take" ? [rest[0] ?? lit(null)] : []];
4039
+ const exprs = [head, ...op === "sortBy" ? [] : [rest[0] ?? lit(null)]];
3902
4040
  const strict = this.#strict(op, rest.slice(exprs.length - 1));
3903
4041
  const raw = [list, ...exprs.slice(1).map((x) => this.expr(x, ctx))];
3904
4042
  const deps = depsOf(...raw);
3905
4043
  const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
3906
- const item = list.v.list.item;
4044
+ const helper = op === "sortBy" ? "SB" : op === "take" ? "TK" : "DR";
3907
4045
  return {
3908
4046
  s: stmtsOf(...parts),
3909
4047
  v: {
3910
- code: `${this.use(op === "sortBy" ? "SB" : "TK")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
4048
+ code: `${this.use(helper)}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
3911
4049
  kind: void 0,
3912
4050
  safe: false,
3913
4051
  deps,
3914
- list: { item }
4052
+ list: list.v.list
3915
4053
  }
3916
4054
  };
3917
4055
  }
4056
+ /**
4057
+ * This method prints append, without and flat (LIST.BUILD), get (LIST.GET) and indexOf (LIST.INDEXOF) over the array
4058
+ * of a list. Each list that the op reads follows LIST.LENIENT, as in the interpreter.
4059
+ */
4060
+ #build(op, head, rest, ctx) {
4061
+ const list = this.#listOf(head, ctx);
4062
+ const info = list.v.list;
4063
+ const two = op === "append" || op === "without";
4064
+ const exprs = [head, ...op === "flat" ? [] : [rest[0] ?? lit(null)]];
4065
+ const strict = this.#strict(op, rest.slice(exprs.length - 1));
4066
+ const raw = [list, ...exprs.slice(1).map((x) => two ? this.#listOf(x, ctx) : this.expr(x, ctx))];
4067
+ const deps = depsOf(...raw);
4068
+ const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
4069
+ const helper = BUILD_HELPERS[op];
4070
+ const value = (kind, facts) => ({
4071
+ s: stmtsOf(...parts),
4072
+ v: {
4073
+ code: `${this.use(helper)}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
4074
+ kind,
4075
+ safe: false,
4076
+ deps,
4077
+ list: facts
4078
+ }
4079
+ });
4080
+ if (op === "get") return value(info.item, info.inner);
4081
+ if (op === "indexOf") return value("number", void 0);
4082
+ if (op === "without") return value(void 0, info);
4083
+ if (op === "append") return value(void 0, joinLists(info, parts[1].v.list));
4084
+ if (info.empty === true) return value(void 0, info);
4085
+ if (info.inner === void 0) throw new NotStandalone("\"flat\" over a list whose items are not lists");
4086
+ return value(void 0, info.inner);
4087
+ }
3918
4088
  /** This method prints a list op over a list that is not an each: a loop over its array, and nth (LIST.NTH). */
3919
4089
  #overList(op, head, rest, ctx) {
3920
4090
  const list = this.#listOf(head, ctx);
@@ -3924,14 +4094,15 @@ var Printer = class {
3924
4094
  const deps = depsOf(...raw);
3925
4095
  const parts = this.#inOrder(this.#settle(raw, deps, exprs), true);
3926
4096
  const [l] = parts;
3927
- const item = list.v.list.item;
4097
+ const { item, inner } = list.v.list;
3928
4098
  if (op === "nth") return {
3929
4099
  s: stmtsOf(...parts),
3930
4100
  v: {
3931
4101
  code: `${this.use("NT")}(${[...parts.map((x) => x.v.code), String(strict)].join(", ")})`,
3932
4102
  kind: item,
3933
4103
  safe: false,
3934
- deps
4104
+ deps,
4105
+ list: inner
3935
4106
  }
3936
4107
  };
3937
4108
  const r = this.#reduction(op, item);
@@ -3949,7 +4120,8 @@ var Printer = class {
3949
4120
  code: r.result,
3950
4121
  kind: listKind(op, item),
3951
4122
  safe: false,
3952
- deps
4123
+ deps,
4124
+ list: op === "first" ? inner : void 0
3953
4125
  }
3954
4126
  };
3955
4127
  }
@@ -3958,21 +4130,30 @@ var Printer = class {
3958
4130
  * node. Inside the loop, each name has the level `loop`. Thus a part that reads a name stays in the loop. A loop that
3959
4131
  * reads no key of the run is a constant of the run.
3960
4132
  */
3961
- #loopNames(init, ctx, loop) {
4133
+ #loopNames(init, step, ctx, loop) {
3962
4134
  const s = [];
3963
4135
  const env = new Map(ctx.env);
3964
4136
  const vars = /* @__PURE__ */ new Map();
3965
4137
  const deps = /* @__PURE__ */ new Set();
4138
+ const keyed = Object.values(step).some(hasKeyedLiteral);
3966
4139
  for (const [name, x] of Object.entries(init)) {
4140
+ this.#wantList.add(x);
3967
4141
  const c = this.expr(x, ctx);
3968
- if (c.v.kind === void 0) throw new NotStandalone(`the first value of the name "${name}" of a loop has no kind`);
4142
+ const facts = c.v.list;
4143
+ if (c.v.kind === void 0 && facts === void 0) throw new NotStandalone(`the first value of the name "${name}" of a loop has no kind`);
3969
4144
  const v = this.fresh("l");
3970
4145
  s.push(...c.s, `let ${v} = ${c.v.code};`);
4146
+ const list = facts === void 0 ? void 0 : {
4147
+ item: facts.item,
4148
+ inner: facts.inner,
4149
+ texts: facts.texts === true || keyed
4150
+ };
3971
4151
  const read = {
3972
4152
  code: v,
3973
4153
  kind: c.v.kind,
3974
4154
  lazy: false,
3975
- deps: /* @__PURE__ */ new Set([loop])
4155
+ deps: /* @__PURE__ */ new Set([loop]),
4156
+ list
3976
4157
  };
3977
4158
  env.set(name, read);
3978
4159
  vars.set(name, read);
@@ -3991,9 +4172,11 @@ var Printer = class {
3991
4172
  const sets = [];
3992
4173
  const deps = /* @__PURE__ */ new Set();
3993
4174
  for (const [name, x] of Object.entries(step)) {
4175
+ this.#wantList.add(x);
3994
4176
  const c = this.#hoist(this.expr(x, inner), hasLoop(x));
3995
4177
  const v = vars.get(name);
3996
- if (c.v.kind !== v.kind) throw new NotStandalone(`a step of a loop changes the kind of the name "${name}"`);
4178
+ if (c.v.kind !== v.kind || c.v.list === void 0 !== (v.list === void 0)) throw new NotStandalone(`a step of a loop changes the kind of the name "${name}"`);
4179
+ if (v.list !== void 0 && !fits(v.list, c.v.list)) throw new NotStandalone(`a step of a loop gives the name "${name}" a list that does not fit its first list`);
3997
4180
  const t = this.fresh("t");
3998
4181
  s.push(...c.s, `const ${t} = ${c.v.code};`);
3999
4182
  sets.push(`${v.code} = ${t};`);
@@ -4010,11 +4193,12 @@ var Printer = class {
4010
4193
  */
4011
4194
  #iterate(e, ctx) {
4012
4195
  const loop = this.#loops++;
4013
- const names = this.#loopNames(e.init, ctx, loop);
4196
+ const names = this.#loopNames(e.init, e.step, ctx, loop);
4014
4197
  const inner = {
4015
4198
  ...ctx,
4016
4199
  env: names.env
4017
4200
  };
4201
+ if (this.#wantList.has(e)) this.#wantList.add(e.result);
4018
4202
  const until = e.until === void 0 ? void 0 : this.expr(e.until, inner);
4019
4203
  const step = this.#loopStep(e.step, inner, names.vars);
4020
4204
  const result = this.expr(e.result, inner);
@@ -4042,7 +4226,8 @@ var Printer = class {
4042
4226
  code: result.v.code,
4043
4227
  kind: result.v.kind,
4044
4228
  safe: false,
4045
- deps
4229
+ deps,
4230
+ list: result.v.list
4046
4231
  }
4047
4232
  };
4048
4233
  }
@@ -4052,7 +4237,8 @@ var Printer = class {
4052
4237
  */
4053
4238
  #fold(e, ctx) {
4054
4239
  const loop = this.#loops++;
4055
- const names = this.#loopNames(e.init, ctx, loop);
4240
+ const names = this.#loopNames(e.init, e.step, ctx, loop);
4241
+ if (this.#wantList.has(e)) this.#wantList.add(e.result);
4056
4242
  const t = this.#targetLoop(e.axis, e.at ?? [], ctx);
4057
4243
  const tests = t.tests.map((x) => this.#hoist(this.expr(x, t.inner), hasLoop(x)));
4058
4244
  if (tests.some((x) => x.v.deps.has(CELLS))) throw new NotStandalone("a test of the axis of a fold reads a cell");
@@ -4091,7 +4277,8 @@ var Printer = class {
4091
4277
  code: result.v.code,
4092
4278
  kind: result.v.kind,
4093
4279
  safe: false,
4094
- deps
4280
+ deps,
4281
+ list: result.v.list
4095
4282
  }
4096
4283
  };
4097
4284
  }