@mark1russell7/vex-compiler 0.17.2 → 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.
package/src/memo.ts CHANGED
@@ -10,7 +10,8 @@
10
10
  * A key reads the origin or a name from outside the node, and nothing that the node changes. Inside the node, the
11
11
  * value of the key replaces the key. Thus the value of the node depends only on the space and the values of the keys.
12
12
  */
13
- import { addrExprs, isListOp, listOf, type Addr, type Axis, type Expr } from "@mark1russell7/vex";
13
+ import { addrExprs, children, isListOp, listOf, type Addr, type Axis, type Expr } from "@mark1russell7/vex";
14
+ import { originalOf } from "./unshared.ts";
14
15
 
15
16
  /** A read of the focus of the expression, outside its own loops. */
16
17
  const FOCUS = 1;
@@ -55,104 +56,207 @@ function addressReads(at: Addr | undefined, top: boolean): number {
55
56
  return inLoop(found | start, top);
56
57
  }
57
58
 
58
- /**
59
- * This function gives the reads of `e`. `outer` has the names that the node binds outside `e`, and `inner` has the
60
- * names that `e` binds above this part. `top` is true outside the loops of `e`.
61
- */
62
- function reads(e: Expr, outer: ReadonlySet<string>, inner: ReadonlySet<string>, top: boolean): number {
63
- switch (e.tag) {
64
- // A missing case gives undefined, and a bitwise operation counts it as 0.
65
- // Stryker disable next-line StringLiteral: see the comment above
66
- case "lit":
67
- return 0;
68
- case "var":
69
- return inner.has(e.name) ? 0 : outer.has(e.name) ? INTERNAL : inLoop(FREE, top);
70
- case "ext":
71
- // A loop of names is not in the memo yet, so a node with one is not either. A call reads what its body reads, and a
72
- // function reads nothing where it is bound, but the memo does not follow them yet (FN.CALL).
73
- case "fold":
74
- case "iterate":
75
- // A let with a function and no call has the value of its body, and a call is opaque, so this case only keeps the
76
- // memo simple.
77
- // Stryker disable next-line StringLiteral: see the comment above
78
- case "fn":
79
- case "call":
80
- return OPAQUE;
81
- case "ref":
82
- // The expressions of computed moves evaluate at the position of the node (NAV.COMPUTED).
83
- return addrExprs(e.at).reduce((r, x) => r | reads(x, outer, inner, top), addressReads(e.at, top));
84
- case "app":
85
- return e.args.reduce((r, x) => r | reads(x, outer, inner, top), 0);
86
- case "rec":
87
- return Object.values(e.fields).reduce((r, x) => r | reads(x, outer, inner, top), 0);
88
- case "let": {
89
- const binds = Object.values(e.bind).reduce((r, x) => r | reads(x, outer, inner, top), 0);
90
- return binds | reads(e.body, outer, new Set([...inner, ...Object.keys(e.bind)]), top);
91
- }
92
- case "each": {
93
- const { base, tests } = parts(e.axis);
94
- // The targets of `all` do not depend on the focus, but its address can fail (V-051). Each other axis starts at its
95
- // address. The targets of the axis list are the items of a value, and the memo does not follow them yet (AXIS.LIST).
96
- const targets = base.t === "table" || listOf(e.axis) !== undefined ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top);
97
- const at = addrExprs(e.at).reduce((r, x) => r | reads(x, outer, inner, top), targets);
98
- return [e.body, ...tests].reduce((r, x) => r | reads(x, outer, inner, false), at);
99
- }
100
- }
59
+ /** The facts of an expression with no names around it. A set of names has one bit for each name. */
60
+ interface Facts {
61
+ /** The reads of the expression outside each loop around it, with no read of a name. */
62
+ readonly top: number;
63
+ /** The reads of the expression in a loop around it, with no read of a name. */
64
+ readonly inner: number;
65
+ /** The names that the expression reads and does not bind, outside its own loops. */
66
+ readonly near: bigint;
67
+ /** The names that the expression reads and does not bind, in its own loops. */
68
+ readonly far: bigint;
69
+ /** This value is true if the expression has a loop. A node with no loop is not a node of the memo. */
70
+ readonly loop: boolean;
101
71
  }
102
72
 
73
+ /** The facts of an expression that reads nothing. */
74
+ const NOTHING: Facts = { top: 0, inner: 0, near: 0n, far: 0n, loop: false };
75
+
76
+ /** The facts of an expression that the analysis does not follow. */
77
+ const OPAQUE_FACTS: Facts = { ...NOTHING, top: OPAQUE, inner: OPAQUE };
78
+
79
+ /** The keys of a part, in the order of the parts. A list of keys holds the lists of its parts, so a join does not copy them. */
80
+ type Keys = readonly (Expr | Keys)[];
81
+
103
82
  /** This function joins the keys of the parts of an expression. A part that cannot be in the memo makes the expression the same. */
104
- function all(xs: readonly (readonly Expr[] | undefined)[]): readonly Expr[] | undefined {
105
- // Stryker disable next-line ArrayDeclaration: after the check, each part has its keys, so no one reads the default list
106
- return xs.every((x) => x !== undefined) ? xs.flatMap((x) => x ?? []) : undefined;
83
+ function all(xs: readonly (Keys | undefined)[]): Keys | undefined {
84
+ const known = xs.filter((x) => x !== undefined);
85
+ if (known.length < xs.length) return undefined;
86
+ const some = known.filter((x) => x.length > 0);
87
+ return some.length === 1 ? some[0] : some;
88
+ }
89
+
90
+ /** This function adds each key of a list to `out`, in the order of the parts. */
91
+ function flatten(keys: Keys, out: Expr[]): readonly Expr[] {
92
+ for (const x of keys) {
93
+ if (Array.isArray(x)) flatten(x as Keys, out);
94
+ else out.push(x as Expr);
95
+ }
96
+ return out;
107
97
  }
108
98
 
109
99
  /**
110
- * This function gives the keys of the part `e` of a node, or `undefined` if the part cannot be in the memo. `top` is
111
- * true outside the loops of the node, and `outer` has the names that the node binds above `e`. The node itself (`root`)
112
- * is not a key of itself.
100
+ * The analysis of one program. Each node has one record of facts, so a read of a part does not walk that part again. A
101
+ * part that cannot be in the memo stays known, so the next node above it does not walk it again. The walk of the keys
102
+ * starts at each node of the memo. Thus the work on the key lists grows with the nodes of the memo times the levels of
103
+ * `origin` around them. The gate COMPILE.GROWTH holds this growth at the largest legal sizes.
113
104
  */
114
- function keysOf(e: Expr, top: boolean, outer: ReadonlySet<string>, pure: Set<Expr>, root = false): readonly Expr[] | undefined {
115
- const r = reads(e, outer, new Set(), true);
116
- // In a loop of the node, the focus is a target of the node, and the node can read it. A part that depends only on
117
- // the space has a memo of its own, if it has a loop.
118
- if ((r & (ORIGIN | FREE | OPAQUE | (top ? FOCUS : 0))) === 0) {
119
- if (!root) pure.add(e);
120
- return [];
105
+ class Analysis {
106
+ readonly #bits = new Map<string, bigint>();
107
+ readonly #facts = new Map<Expr, Facts>();
108
+ readonly #dead = new Map<Expr, Set<bigint>>();
109
+
110
+ /** This method gives the set of some names. */
111
+ namesOf(names: readonly string[]): bigint {
112
+ return names.reduce((set, name) => {
113
+ const bit = this.#bits.get(name) ?? 1n << BigInt(this.#bits.size);
114
+ this.#bits.set(name, bit);
115
+ return set | bit;
116
+ }, 0n);
121
117
  }
122
- // A key evaluates once, outside the loops of the node. A loop that reads the origin is a part of the node, not a key.
123
- if (!root && (r & (FOCUS | INTERNAL | OPAQUE | DEEP)) === 0) return [e];
124
- switch (e.tag) {
125
- case "app":
126
- return all(e.args.map((x) => keysOf(x, top, outer, pure)));
127
- case "rec":
128
- return all(Object.values(e.fields).map((x) => keysOf(x, top, outer, pure)));
129
- case "let": {
130
- const inner = new Set([...outer, ...Object.keys(e.bind)]);
131
- return all([...Object.values(e.bind).map((x) => keysOf(x, top, outer, pure)), keysOf(e.body, top, inner, pure)]);
118
+
119
+ /** This method gives the facts of an expression. */
120
+ factsOf(e: Expr): Facts {
121
+ const known = this.#facts.get(e);
122
+ if (known !== undefined) return known;
123
+ const facts = this.#read(e);
124
+ this.#facts.set(e, facts);
125
+ return facts;
126
+ }
127
+
128
+ /** This method joins the facts of some expressions. */
129
+ #joined(xs: readonly Expr[]): Facts {
130
+ return xs.reduce((a, x) => {
131
+ const b = this.factsOf(x);
132
+ return { top: a.top | b.top, inner: a.inner | b.inner, near: a.near | b.near, far: a.far | b.far, loop: a.loop || b.loop };
133
+ }, NOTHING);
134
+ }
135
+
136
+ /** This method gives the facts of an expression from the facts of its parts. */
137
+ #read(e: Expr): Facts {
138
+ switch (e.tag) {
139
+ case "lit":
140
+ return NOTHING;
141
+ case "var":
142
+ return { ...NOTHING, near: this.namesOf([e.name]) };
143
+ case "fold":
144
+ case "iterate":
145
+ case "fix":
146
+ return { ...OPAQUE_FACTS, loop: true };
147
+ case "ext":
148
+ // A loop of names is not in the memo yet, so a node with one is not either. A call reads what its body reads, and a
149
+ // function reads nothing where it is bound, but the memo does not follow them yet (FN.CALL).
150
+ case "last":
151
+ // A let with a function and no call has the value of its body, and a call is opaque, so this case only keeps the
152
+ // memo simple.
153
+ case "fn":
154
+ case "call":
155
+ return OPAQUE_FACTS;
156
+ case "ref": {
157
+ // The expressions of computed moves evaluate at the position of the node (NAV.COMPUTED).
158
+ const at = this.#joined(addrExprs(e.at));
159
+ return { ...at, top: at.top | addressReads(e.at, true), inner: at.inner | addressReads(e.at, false) };
160
+ }
161
+ case "app":
162
+ return this.#joined(e.args);
163
+ case "rec":
164
+ return this.#joined(Object.values(e.fields));
165
+ case "let": {
166
+ const binds = this.#joined(Object.values(e.bind));
167
+ const body = this.factsOf(e.body);
168
+ const free = ~this.namesOf(Object.keys(e.bind));
169
+ return { top: binds.top | body.top, inner: binds.inner | body.inner, near: binds.near | (body.near & free), far: binds.far | (body.far & free), loop: binds.loop || body.loop };
170
+ }
171
+ case "each": {
172
+ const { base, tests } = parts(e.axis);
173
+ // The targets of `all` do not depend on the focus, but its address can fail (V-051). Each other axis starts at its
174
+ // address. The targets of the axis list are the items of a value, and the memo does not follow them yet (AXIS.LIST).
175
+ const targets = (top: boolean): number => (base.t === "table" || listOf(e.axis) !== undefined ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top));
176
+ const at = this.#joined(addrExprs(e.at));
177
+ const body = this.#joined([e.body, ...tests]);
178
+ return { top: targets(true) | at.top | body.inner, inner: targets(false) | at.inner | body.inner, near: at.near, far: at.far | body.near | body.far, loop: true };
179
+ }
132
180
  }
133
- case "each": {
134
- const { base, tests } = parts(e.axis);
135
- // The targets of a loop must not depend on the origin: a set of targets is not a value of a key. The address of
136
- // `all` can fail, so it must not depend on the origin either. The targets of the axis list are the items of a
137
- // value, and the memo does not follow them yet (AXIS.LIST).
138
- if (base.t === "table" || listOf(e.axis) !== undefined || ((base.t !== "all" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0)) return undefined;
139
- // The expressions of computed moves give the targets too, so they must read nothing.
140
- if (addrExprs(e.at).some((x) => reads(x, outer, new Set(), top) !== 0)) return undefined;
141
- return all([e.body, ...tests].map((x) => keysOf(x, false, outer, pure)));
181
+ }
182
+
183
+ /**
184
+ * This method gives the reads of `e`. `outer` has the names that the node binds outside `e`. `top` is true outside the
185
+ * loops of the node.
186
+ */
187
+ #reads(e: Expr, outer: bigint, top: boolean): number {
188
+ const { near, far, top: atTop, inner } = this.factsOf(e);
189
+ const internal = ((near | far) & outer) === 0n ? 0 : INTERNAL;
190
+ const free = (near & ~outer) === 0n ? 0 : inLoop(FREE, top);
191
+ const deep = (far & ~outer) === 0n ? 0 : inLoop(FREE, false);
192
+ return (top ? atTop : inner) | internal | free | deep;
193
+ }
194
+
195
+ /**
196
+ * This method gives the keys of the part `e` of a node, or `undefined` if the part cannot be in the memo. `top` is
197
+ * true outside the loops of the node, and `outer` has the names that the node binds above `e`.
198
+ */
199
+ keysOf(e: Expr, top: boolean, outer: bigint, pure: Set<Expr>): Keys | undefined {
200
+ const mark = (outer << 1n) | (top ? 1n : 0n);
201
+ const dead = this.#dead.get(e);
202
+ if (dead?.has(mark) === true) return undefined;
203
+ const keys = this.#partsOf(e, top, outer, pure, false);
204
+ if (keys === undefined) this.#dead.set(e, (dead ?? new Set()).add(mark));
205
+ return keys;
206
+ }
207
+
208
+ /** This method gives the keys of a node of the program. The node itself is not a key of itself. */
209
+ keysOfNode(e: Expr, pure: Set<Expr>): readonly Expr[] | undefined {
210
+ const keys = this.#partsOf(e, true, 0n, pure, true);
211
+ return keys === undefined ? undefined : flatten(keys, []);
212
+ }
213
+
214
+ /** This method gives the keys of an expression from the keys of its parts. An undefined value means no memo. */
215
+ #partsOf(e: Expr, top: boolean, outer: bigint, pure: Set<Expr>, root: boolean): Keys | undefined {
216
+ const r = this.#reads(e, outer, true);
217
+ // In a loop of the node, the focus is a target of the node, and the node can read it. A part that depends only on
218
+ // the space has a memo of its own, if it has a loop.
219
+ if ((r & (ORIGIN | FREE | OPAQUE | (top ? FOCUS : 0))) === 0) {
220
+ if (!root) pure.add(e);
221
+ return [];
222
+ }
223
+ // A key evaluates once, outside the loops of the node. A loop that reads the origin is a part of the node, not a key.
224
+ if (!root && (r & (FOCUS | INTERNAL | OPAQUE | DEEP)) === 0) return [e];
225
+ switch (e.tag) {
226
+ case "app":
227
+ return all(e.args.map((x) => this.keysOf(x, top, outer, pure)));
228
+ case "rec":
229
+ return all(Object.values(e.fields).map((x) => this.keysOf(x, top, outer, pure)));
230
+ case "let": {
231
+ const inner = outer | this.namesOf(Object.keys(e.bind));
232
+ return all([...Object.values(e.bind).map((x) => this.keysOf(x, top, outer, pure)), this.keysOf(e.body, top, inner, pure)]);
233
+ }
234
+ case "each": {
235
+ const { base, tests } = parts(e.axis);
236
+ // The targets of a loop must not depend on the origin: a set of targets is not a value of a key. The address of
237
+ // `all` can fail, so it must not depend on the origin either. The targets of the axis list are the items of a
238
+ // value, and the memo does not follow them yet (AXIS.LIST).
239
+ if (base.t === "table" || listOf(e.axis) !== undefined || ((base.t !== "all" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0)) return undefined;
240
+ // The expressions of computed moves give the targets too, so they must read nothing.
241
+ if (addrExprs(e.at).some((x) => this.#reads(x, outer, top) !== 0)) return undefined;
242
+ return all([e.body, ...tests].map((x) => this.keysOf(x, false, outer, pure)));
243
+ }
244
+ // A leaf here reads the focus or a name of the node, so it is not a key, and the part cannot be in the memo. A
245
+ // missing case gives undefined too.
246
+ // Stryker disable StringLiteral,ConditionalExpression: see the comment above
247
+ case "lit":
248
+ case "ref":
249
+ case "var":
250
+ case "ext":
251
+ case "fold":
252
+ case "iterate":
253
+ case "fix":
254
+ case "last":
255
+ case "fn":
256
+ case "call":
257
+ return undefined;
258
+ // Stryker restore StringLiteral,ConditionalExpression
142
259
  }
143
- // A leaf here reads the focus or a name of the node, so it is not a key, and the part cannot be in the memo. A
144
- // missing case gives undefined too.
145
- // Stryker disable StringLiteral,ConditionalExpression: see the comment above
146
- case "lit":
147
- case "ref":
148
- case "var":
149
- case "ext":
150
- case "fold":
151
- case "iterate":
152
- case "fn":
153
- case "call":
154
- return undefined;
155
- // Stryker restore StringLiteral,ConditionalExpression
156
260
  }
157
261
  }
158
262
 
@@ -163,6 +267,7 @@ function loopOutside(e: Expr, keys: ReadonlySet<Expr>): boolean {
163
267
  case "each":
164
268
  case "fold":
165
269
  case "iterate":
270
+ case "fix":
166
271
  return true;
167
272
  case "app":
168
273
  return e.args.some((x) => loopOutside(x, keys));
@@ -174,6 +279,7 @@ function loopOutside(e: Expr, keys: ReadonlySet<Expr>): boolean {
174
279
  return addrExprs(e.at).some((x) => loopOutside(x, keys));
175
280
  // A missing case gives undefined, which is false too.
176
281
  // Stryker disable StringLiteral,ConditionalExpression: see the comment above
282
+ case "last":
177
283
  case "lit":
178
284
  case "var":
179
285
  case "ext":
@@ -199,19 +305,28 @@ function eachUnderLets(e: Expr | undefined): { readonly lets: readonly Extract<E
199
305
  /**
200
306
  * This function gives the nodes of `expr` that a program keeps in its memo, each with its keys. Each such node has a
201
307
  * loop outside its keys. A node inside such a node can be in the memo too. Thus the first evaluation of the outer
202
- * node evaluates the inner node once.
308
+ * node evaluates the inner node once. The function `loops` tells if a list op is one loop with an `each`. In a module,
309
+ * each list op is one. In tier 2, the ops of `isFusedOp` are.
310
+ *
311
+ * A node at two places of the program has one object in the source, and the compile gives each place a copy. A node of
312
+ * the memo at one place is a node of the memo at each place, so the second pass finds the other copies.
203
313
  */
204
- export function memoNodes(expr: Expr): ReadonlyMap<Expr, readonly Expr[]> {
314
+ export function memoNodes(expr: Expr, loops: (op: string) => boolean = isListOp): ReadonlyMap<Expr, readonly Expr[]> {
205
315
  const out = new Map<Expr, readonly Expr[]>();
206
- const visit = (e: Expr): void => {
316
+ const analysis = new Analysis();
317
+ const note = (e: Expr): void => {
318
+ if (!analysis.factsOf(e).loop) return;
207
319
  const pure = new Set<Expr>();
208
- const keys = keysOf(e, true, new Set(), pure, true);
320
+ const keys = analysis.keysOfNode(e, pure);
209
321
  if (keys !== undefined && loopOutside(e, new Set([...keys, ...pure]))) out.set(e, keys);
322
+ };
323
+ const visit = (e: Expr): void => {
324
+ note(e);
210
325
  if (e.tag === "app") {
211
326
  // A list op over an `each` under `let` expressions is one loop, so that `each` and those `let` expressions have
212
327
  // no value of their own. Their parts can be in the memo.
213
328
  const [first, ...rest] = e.args;
214
- const fused = isListOp(e.op) ? eachUnderLets(first) : undefined;
329
+ const fused = loops(e.op) ? eachUnderLets(first) : undefined;
215
330
  if (fused === undefined) {
216
331
  if (first !== undefined) visit(first);
217
332
  } else {
@@ -231,5 +346,11 @@ export function memoNodes(expr: Expr): ReadonlyMap<Expr, readonly Expr[]> {
231
346
  }
232
347
  };
233
348
  visit(expr);
349
+ const found = new Set([...out.keys()].map(originalOf));
350
+ const copies = (e: Expr): void => {
351
+ if (!out.has(e) && found.has(originalOf(e))) note(e);
352
+ children(e).forEach(copies);
353
+ };
354
+ copies(expr);
234
355
  return out;
235
356
  }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The entry `@mark1russell7/vex-compiler/module`: the printer of standalone modules (docs/COMPILER.md §4.4). This entry
3
+ * loads the parser of the copied functions (COMPILE.MODULE.TEXT). The root entry does not, so a program that only runs
4
+ * tiers does not load it.
5
+ */
6
+ export { functionText } from "./fn-text.ts";
7
+ export { emitModule, emitSheetModule, type Emitted, type ListKind, type ModuleOptions, type SheetModuleOptions } from "./module.ts";