hclang 0.44.1 → 0.45.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.
@@ -0,0 +1,166 @@
1
+ import { Frame } from "./frame.js";
2
+ import { FrameCount } from "./frame-count.js";
3
+ import { FrameInt } from "./frame-int.js";
4
+ import { DenumError, fromHost, MAX_RANK, promote, } from "./denum.js";
5
+ let construct;
6
+ /**
7
+ * Installs what `.#n` answers a present Denum with. The Denum is an HC class,
8
+ * so the frames package, which cannot run HC, takes its constructor from the
9
+ * evaluator (`lib/execute/denum-prologue.ts`), as `.####` takes its Demi.
10
+ *
11
+ * @param constructor The constructor, called once per present observation.
12
+ */
13
+ export function installDenumConstructor(constructor) {
14
+ construct = constructor;
15
+ }
16
+ /** A Frame read as an exact nonnegative Count, or undefined. */
17
+ function exactCount(frame) {
18
+ const value = frame.referent();
19
+ return value instanceof FrameCount && typeof value.data === "bigint" &&
20
+ value.data >= 0n
21
+ ? value.data
22
+ : undefined;
23
+ }
24
+ /**
25
+ * The stored state of a Denum instance: its own `rank`, `count` and `loads`
26
+ * fields. Only the instance's own fields are read (`get_here`), never a
27
+ * declared parent or a method, and the loads are read as stored, so nothing
28
+ * runs. Undefined when any field is absent or not an exact Count of the right
29
+ * shape.
30
+ */
31
+ function storedState(host) {
32
+ const rank = exactCount(host.get_here("rank"));
33
+ const count = exactCount(host.get_here("count"));
34
+ const list = host.get_here("loads").referent();
35
+ if (rank === undefined || count === undefined || rank > BigInt(MAX_RANK)) {
36
+ return undefined;
37
+ }
38
+ if (!list.ownsIdentity())
39
+ return undefined;
40
+ const loads = list.elements().map(exactCount);
41
+ if (loads.some((load) => load === undefined))
42
+ return undefined;
43
+ try {
44
+ return {
45
+ rank: Number(rank),
46
+ count,
47
+ loads: loads,
48
+ };
49
+ }
50
+ catch {
51
+ return undefined;
52
+ }
53
+ }
54
+ /**
55
+ * The intrinsic path, as `.####` has for a Demi. A source is a Denum exactly
56
+ * when its stored `rank`, `count` and `loads` are a valid state and it is `==`
57
+ * (same value and same type at every node, fields and methods included) to the
58
+ * Denum the shipped constructor builds from them. The answer is then built
59
+ * from that reconstruction, never from the source: no observer, method or
60
+ * closure of the source runs, so a forged `#`, `##` or `###` cannot steer it.
61
+ * A Denum of Count zero is therefore present, unlike a host with no children.
62
+ *
63
+ * @returns The source's reconstruction as a state, or undefined.
64
+ */
65
+ function intrinsicDenum(source) {
66
+ if (!construct)
67
+ return undefined;
68
+ const host = source.referent();
69
+ if (!host.ownsIdentity())
70
+ return undefined;
71
+ const state = storedState(host);
72
+ if (!state)
73
+ return undefined;
74
+ let candidate;
75
+ try {
76
+ candidate = construct(state);
77
+ }
78
+ catch (error) {
79
+ if (error instanceof DenumError)
80
+ return undefined;
81
+ throw error;
82
+ }
83
+ if (candidate.isFailedResult() || candidate.observationError()) {
84
+ return undefined;
85
+ }
86
+ return candidate.typeEquals(host) === Frame.all ? state : undefined;
87
+ }
88
+ /**
89
+ * The ranked hash, `.#n` (occurrence/figure 007.32, 007.32b): the Denum of
90
+ * rung n, n from 0 to 5. There is no `#n=`, and a Denum is not a scalar
91
+ * observation; the algebraic value is `d.observe ()`.
92
+ *
93
+ * Two different operations answer it, and they are not the same:
94
+ *
95
+ * - **Construction from a host.** A host answers its Count C, Size s and
96
+ * residual r only, so the Denum is built from those three observers with
97
+ * the derivation of `.####` (`observeDemi`), at rank n (`fromHost`). A host
98
+ * with no Count, or Count zero, has no Denum and answers nil, as it has no
99
+ * Demi; rank 0 reads no residual. Failures are answered as they are, and
100
+ * unusable observations are `$!.denum-count`, `$!.denum-observations` and
101
+ * `$!.denum-coherence`.
102
+ * - **Promotion of a Denum.** An instance of the shipped class answers its own
103
+ * state promoted to rank n, keeping Count and every load, and below its own
104
+ * rank `$!.denum-demotion`. A Denum of Count zero is present.
105
+ *
106
+ * A multi-channel `RotorRouter` is not special-cased here as it is under
107
+ * `.####` (`channelRouterDemi`): `.#n` reads only Count, Size and residual, so
108
+ * `x.#1` equals `x.####` for ordinary and binary hosts, and for a router of
109
+ * more than two channels is the binary projection while `.####` keeps its
110
+ * channel Demi (pinned in `hc/denum.hc`).
111
+ *
112
+ * Nothing folds, executes or caches the source, and each observer is read at
113
+ * most once. A rank above 5 is `$!.denum-rank`.
114
+ *
115
+ * @param source The observed Frame.
116
+ * @param rank The rung n of `.#n`.
117
+ * @returns A Denum, nil, or an error.
118
+ */
119
+ export function observeDenum(source, rank) {
120
+ const failure = source.observationError();
121
+ if (failure)
122
+ return failure;
123
+ if (!Number.isInteger(rank) || rank < 0 || rank > MAX_RANK) {
124
+ return Frame.error("$!.denum-rank");
125
+ }
126
+ if (!construct)
127
+ return Frame.error("$!.denum-unavailable");
128
+ try {
129
+ const intrinsic = intrinsicDenum(source);
130
+ if (intrinsic)
131
+ return construct(promote(intrinsic, rank));
132
+ const n = source.sizeFor(Frame.kCOUNT);
133
+ if (n.observationError())
134
+ return n;
135
+ if (n === Frame.nil)
136
+ return Frame.nil; // no Count, no Denum
137
+ if (!(n instanceof FrameCount) || typeof n.data !== "bigint" || n.data < 0n) {
138
+ return Frame.error("$!.denum-count");
139
+ }
140
+ if (n.data === 0n)
141
+ return Frame.nil;
142
+ const s = source.sizeFor(Frame.kLEAVES);
143
+ if (s.observationError())
144
+ return s;
145
+ if (!(s instanceof FrameCount) || typeof s.data !== "bigint") {
146
+ return Frame.error("$!.denum-observations");
147
+ }
148
+ if (rank === 0)
149
+ return construct(fromHost(0, n.data, s.data, 0n));
150
+ const r = source.sizeFor(Frame.kRESIDUAL);
151
+ if (r.observationError())
152
+ return r;
153
+ if (r === Frame.nil)
154
+ return Frame.nil;
155
+ if (!(r instanceof FrameInt) || typeof r.data !== "bigint") {
156
+ return Frame.error("$!.denum-observations");
157
+ }
158
+ return construct(fromHost(rank, n.data, s.data, r.data));
159
+ }
160
+ catch (error) {
161
+ if (error instanceof DenumError)
162
+ return Frame.error(error.kind);
163
+ throw error;
164
+ }
165
+ }
166
+ Frame.denum = observeDenum;
@@ -0,0 +1,357 @@
1
+ /**
2
+ * Denum: the signed-load carrier of the binary Cayley–Dickson step ladder
3
+ * (occurrence/figure 007.31, 007.32, 007.32b).
4
+ *
5
+ * This is the typed reference implementation. It depends on no Frame, so the
6
+ * HC class in `hc/denum.hc` and the exhaustive tests can be checked against it.
7
+ * A Denum is a constructed state, not a conventional number:
8
+ *
9
+ * - a **rank** `n` in 0..MAX_RANK, the schema `CD[n]`;
10
+ * - a retained application **Count** `C ∈ ℕ₀`;
11
+ * - a nonnegative **load** `W ∈ ℕ₀` on every signature channel.
12
+ *
13
+ * Rank 0 has one unsigned channel `e_ε`. Rank `n ≥ 1` has `2ⁿ` channels, `±e_b`
14
+ * for every direction `b` of `n − 1` bits. Channels are ordered positives
15
+ * first, then negatives, each by direction value: channel `b` is `+e_b` and
16
+ * channel `2ⁿ⁻¹ + b` is `−e_b`. A minus names a *signature*; a load is never
17
+ * negative. The algebraic value is the observation {@link observe}, not the
18
+ * Denum.
19
+ *
20
+ * The product of two signed signatures is
21
+ * `(σ e_a)(ρ e_b) = σρ·τ(a, b) e_(a⊕b)`, where the twist `τ` is generated by
22
+ * the Cayley–Dickson recurrence `(a,b)(c,d) = (ac − d̄b, da + bc̄)` in
23
+ * {@link cdTwist}. No multiplication table is written by hand.
24
+ *
25
+ * Freshness: a Denum is a value. `loads` is only `readonly` in the type, not
26
+ * frozen at runtime, so every operation here ({@link promote},
27
+ * {@link opposite}, {@link conjugate}, {@link add}, {@link multiply}, ...)
28
+ * answers a new object with its own `loads` array and never the argument or
29
+ * an alias of its loads, even when the result equals the argument (same-rank
30
+ * promotion, D0 conjugation). Callers must not mutate a Denum they are given.
31
+ *
32
+ * Count is carried, never derived from loads: `C(X+Y) = C(X)+C(Y)`,
33
+ * `C(X⊗Y) = C(X)·C(Y)`, `C(Opp X) = C(X)` and promotion keeps it. These are
34
+ * the explicit candidate laws of 007.32 §6, implemented for pure Denum
35
+ * arithmetic only. They are not the application phase `C mod arity`, and
36
+ * nothing here changes Demi.
37
+ *
38
+ * @module
39
+ */
40
+ /** The highest rung implemented: D5, the sedenions, 32 channels. */
41
+ export const MAX_RANK = 5;
42
+ /** A failure of the Denum contract, named like an HC error. */
43
+ export class DenumError extends Error {
44
+ constructor(kind, detail = "") {
45
+ super(detail ? `${kind} ${detail}` : kind);
46
+ Object.defineProperty(this, "kind", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: kind
51
+ });
52
+ }
53
+ }
54
+ /** The schema's display name, `CD[n]`. */
55
+ export function schemaName(rank) {
56
+ return `CD[${rank}]`;
57
+ }
58
+ /** Channel count of `CD[rank]`: 1 at rank 0, otherwise `2^rank`. */
59
+ export function arity(rank) {
60
+ checkRank(rank);
61
+ return rank === 0 ? 1 : 2 ** rank;
62
+ }
63
+ /** Number of direction bits of a rank: `rank − 1`, or 0 at rank 0. */
64
+ export function directionBits(rank) {
65
+ checkRank(rank);
66
+ return rank === 0 ? 0 : rank - 1;
67
+ }
68
+ function checkRank(rank) {
69
+ if (!Number.isInteger(rank) || rank < 0 || rank > MAX_RANK) {
70
+ throw new DenumError("$!.denum-rank", String(rank));
71
+ }
72
+ }
73
+ /**
74
+ * The Cayley–Dickson twist of two basis directions of `m` bits: the sign
75
+ * `τ(x, y) ∈ {+1, −1}` with `e_x · e_y = τ(x, y) e_(x⊕y)` in the real algebra
76
+ * of dimension `2^m`, generated by the doubling recurrence and conjugation
77
+ * rather than by a table.
78
+ *
79
+ * Write `x = (x₁ x′)` with the top bit first. By
80
+ * `(a,b)(c,d) = (ac − d̄b, da + bc̄)` and `conj(a,b) = (conj a, −b)`:
81
+ *
82
+ * - `(a,0)(c,0)`: `τ(x′, y′)`
83
+ * - `(a,0)(0,d)`: `τ(y′, x′)`
84
+ * - `(0,b)(c,0)`: `conj(y′)·τ(x′, y′)`
85
+ * - `(0,b)(0,d)`: `−conj(y′)·τ(y′, x′)`
86
+ *
87
+ * where `conj(e_k)` contributes `+1` only for the scalar direction `k = 0`.
88
+ */
89
+ export function cdTwist(x, y, m) {
90
+ if (m === 0)
91
+ return 1;
92
+ const half = 2 ** (m - 1);
93
+ const xh = x >= half;
94
+ const yh = y >= half;
95
+ const x1 = xh ? x - half : x;
96
+ const y1 = yh ? y - half : y;
97
+ const conj = y1 === 0 ? 1 : -1;
98
+ if (!xh && !yh)
99
+ return cdTwist(x1, y1, m - 1);
100
+ if (!xh && yh)
101
+ return cdTwist(y1, x1, m - 1);
102
+ if (xh && !yh)
103
+ return (conj * cdTwist(x1, y1, m - 1));
104
+ return (-conj * cdTwist(y1, x1, m - 1));
105
+ }
106
+ /** The signature a channel index names at `rank`. */
107
+ export function signatureOf(rank, channel) {
108
+ const size = arity(rank);
109
+ if (!Number.isInteger(channel) || channel < 0 || channel >= size) {
110
+ throw new DenumError("$!.denum-channel", String(channel));
111
+ }
112
+ if (rank === 0)
113
+ return { negative: false, direction: 0 };
114
+ const half = size / 2;
115
+ return channel >= half
116
+ ? { negative: true, direction: channel - half }
117
+ : { negative: false, direction: channel };
118
+ }
119
+ /** The channel index of a signature at `rank`. */
120
+ export function channelOf(rank, signature) {
121
+ const size = arity(rank);
122
+ if (rank === 0) {
123
+ if (signature.negative || signature.direction !== 0) {
124
+ throw new DenumError("$!.denum-signature", "D0 has only e_ε");
125
+ }
126
+ return 0;
127
+ }
128
+ const half = size / 2;
129
+ if (!Number.isInteger(signature.direction) || signature.direction < 0 ||
130
+ signature.direction >= half) {
131
+ throw new DenumError("$!.denum-signature", String(signature.direction));
132
+ }
133
+ return signature.negative ? half + signature.direction : signature.direction;
134
+ }
135
+ /** Native spelling of a signature: `e_ε` at D0, else `e_b` or `-e_b`. */
136
+ export function signatureName(rank, channel) {
137
+ const { negative, direction } = signatureOf(rank, channel);
138
+ const bits = directionBits(rank);
139
+ const label = bits === 0 ? "ε" : direction.toString(2).padStart(bits, "0");
140
+ return `${negative ? "-" : ""}e_${label}`;
141
+ }
142
+ /** Parses `e_b`, `-e_b` or `e_ε` at `rank`; anything else is an error. */
143
+ export function parseSignature(rank, name) {
144
+ const match = /^(-?)e_([01]+|ε)$/.exec(name);
145
+ if (!match)
146
+ throw new DenumError("$!.denum-signature", name);
147
+ const bits = directionBits(rank);
148
+ const label = match[2];
149
+ if (bits === 0) {
150
+ if (label !== "ε")
151
+ throw new DenumError("$!.denum-signature", name);
152
+ }
153
+ else if (label === "ε" || label.length !== bits) {
154
+ throw new DenumError("$!.denum-signature", name);
155
+ }
156
+ if (rank === 0 && match[1] === "-") {
157
+ throw new DenumError("$!.denum-signature", name);
158
+ }
159
+ return channelOf(rank, {
160
+ negative: match[1] === "-",
161
+ direction: bits === 0 ? 0 : parseInt(label, 2),
162
+ });
163
+ }
164
+ /**
165
+ * The signed product of two channels of the same rank: the destination channel
166
+ * and nothing else is computed from the recurrence, never looked up.
167
+ */
168
+ export function multiplyChannels(rank, left, right) {
169
+ if (rank === 0)
170
+ return 0;
171
+ const a = signatureOf(rank, left);
172
+ const b = signatureOf(rank, right);
173
+ const twist = cdTwist(a.direction, b.direction, directionBits(rank));
174
+ const negative = (a.negative !== b.negative) !== (twist === -1);
175
+ return channelOf(rank, { negative, direction: a.direction ^ b.direction });
176
+ }
177
+ function nonnegative(value, what) {
178
+ if (typeof value !== "bigint" || value < 0n) {
179
+ throw new DenumError("$!!.invalid-denum", what);
180
+ }
181
+ }
182
+ /** Builds a Denum, refusing a bad rank, a negative Count or load, or a bad width. */
183
+ export function denum(rank, count, loads) {
184
+ checkRank(rank);
185
+ nonnegative(count, "count");
186
+ if (loads.length !== arity(rank)) {
187
+ throw new DenumError("$!!.invalid-denum", `loads ${loads.length}`);
188
+ }
189
+ loads.forEach((load) => nonnegative(load, "load"));
190
+ return { rank, count, loads: [...loads] };
191
+ }
192
+ /** The zero-load Denum of a rank with a Count: present, unlike a missing host. */
193
+ export function zero(rank, count = 0n) {
194
+ return denum(rank, count, Array(arity(rank)).fill(0n));
195
+ }
196
+ /**
197
+ * Canonical old-summand embedding `Dn → D(n+1)`: `e_ε ↦ +e_ε` at D0 → D1, and
198
+ * `±e_b ↦ ±e_(0b)` from D1 up, which is the same channel at the same sign with
199
+ * a zero bit placed in front of the direction. Count and every load are kept;
200
+ * the new channels start at zero.
201
+ */
202
+ export function promote(source, rank) {
203
+ checkRank(rank);
204
+ if (rank < source.rank) {
205
+ throw new DenumError("$!.denum-demotion", `${source.rank} to ${rank}`);
206
+ }
207
+ // Always fresh: a same-rank promotion is a copy, never the argument itself.
208
+ let current = {
209
+ rank: source.rank,
210
+ count: source.count,
211
+ loads: [...source.loads],
212
+ };
213
+ while (current.rank < rank) {
214
+ const next = current.rank + 1;
215
+ const loads = Array(arity(next)).fill(0n);
216
+ for (let channel = 0; channel < current.loads.length; channel++) {
217
+ const { negative, direction } = signatureOf(current.rank, channel);
218
+ // Leading zero bit leaves the direction value unchanged.
219
+ loads[channelOf(next, { negative, direction })] = current.loads[channel];
220
+ }
221
+ current = { rank: next, count: current.count, loads };
222
+ }
223
+ return current;
224
+ }
225
+ /** Both operands at the larger rank, by promotion. */
226
+ function align(left, right) {
227
+ const rank = Math.max(left.rank, right.rank);
228
+ return [promote(left, rank), promote(right, rank), rank];
229
+ }
230
+ /**
231
+ * Addition: corresponding loads add, opposite channels stay separate, and the
232
+ * Counts add (C(X+Y) = C(X)+C(Y)).
233
+ */
234
+ export function add(left, right) {
235
+ const [x, y, rank] = align(left, right);
236
+ return {
237
+ rank,
238
+ count: x.count + y.count,
239
+ loads: x.loads.map((load, channel) => load + y.loads[channel]),
240
+ };
241
+ }
242
+ /**
243
+ * Opposite: swaps the loads of `+e_b` and `−e_b`. D0 has no negative channel,
244
+ * so it is promoted to D1 first. The Count is kept.
245
+ */
246
+ export function opposite(source) {
247
+ const x = promote(source, Math.max(source.rank, 1));
248
+ const half = x.loads.length / 2;
249
+ return {
250
+ rank: x.rank,
251
+ count: x.count,
252
+ loads: x.loads.map((_, channel) => x.loads[channel < half ? channel + half : channel - half]),
253
+ };
254
+ }
255
+ /** Subtraction is addition of the opposite; it is observed, never erased. */
256
+ export function subtract(left, right) {
257
+ return add(left, opposite(right));
258
+ }
259
+ /**
260
+ * Cayley–Dickson conjugation: scalar signatures `±e_0…0` are fixed and every
261
+ * other signature goes to its opposite, `±e_b ↦ ∓e_b`. At D0 it is the
262
+ * identity. Counts and loads are kept, only moved between channels.
263
+ */
264
+ export function conjugate(source) {
265
+ if (source.rank === 0) {
266
+ return { rank: 0, count: source.count, loads: [...source.loads] };
267
+ }
268
+ const half = source.loads.length / 2;
269
+ return {
270
+ rank: source.rank,
271
+ count: source.count,
272
+ loads: source.loads.map((load, channel) => {
273
+ const direction = channel % half;
274
+ if (direction === 0)
275
+ return load;
276
+ return source.loads[channel < half ? channel + half : channel - half];
277
+ }),
278
+ };
279
+ }
280
+ /**
281
+ * Ordered multiplication: every ordered pair of weighted signatures
282
+ * contributes the product of its loads at the signed destination the
283
+ * recurrence gives, and only identical full signatures aggregate. Operand
284
+ * order and the caller's parenthesization are the caller's; nothing is
285
+ * regrouped. Counts multiply (C(X⊗Y) = C(X)·C(Y)).
286
+ */
287
+ export function multiply(left, right) {
288
+ const [x, y, rank] = align(left, right);
289
+ const loads = Array(arity(rank)).fill(0n);
290
+ for (let i = 0; i < x.loads.length; i++) {
291
+ if (x.loads[i] === 0n)
292
+ continue;
293
+ for (let j = 0; j < y.loads.length; j++) {
294
+ if (y.loads[j] === 0n)
295
+ continue;
296
+ loads[multiplyChannels(rank, i, j)] += x.loads[i] * y.loads[j];
297
+ }
298
+ }
299
+ return { rank, count: x.count * y.count, loads };
300
+ }
301
+ /**
302
+ * The algebraic observation Φ: the signed integer coordinate of each
303
+ * direction, `W₍₊,b₎ − W₍₋,b₎`, in direction order. At D0 it is the one Size.
304
+ */
305
+ export function observe(source) {
306
+ if (source.rank === 0)
307
+ return [source.loads[0]];
308
+ const half = source.loads.length / 2;
309
+ return Array.from({ length: half }, (_, b) => source.loads[b] - source.loads[half + b]);
310
+ }
311
+ /**
312
+ * The declared residual of a Denum, the signed total `Σ W₊ − Σ W₋`. It is the
313
+ * sum of the observed coordinates, so it is the existing residual `L − R` at
314
+ * D1 and 0 at D0, which has no negative channel. A single integer, it forgets
315
+ * the directions; it is an observer of the state, not the state.
316
+ */
317
+ export function residual(source) {
318
+ return source.rank === 0 ? 0n : observe(source).reduce((a, b) => a + b, 0n);
319
+ }
320
+ /** True when every observed coordinate is zero. */
321
+ export function observesZero(source) {
322
+ return observe(source).every((coordinate) => coordinate === 0n);
323
+ }
324
+ /** True when no channel carries a load. */
325
+ export function isEmpty(source) {
326
+ return source.loads.every((load) => load === 0n);
327
+ }
328
+ /**
329
+ * Host to Denum (the construction behind `X.#n`, distinct from promotion).
330
+ *
331
+ * A host answers only its Count `C`, Size `s` and residual `r`. Rank 0 keeps
332
+ * the one load `s`. Rank `n ≥ 1` places `(s+r)/2` on `+e_0…0` and `(s−r)/2` on
333
+ * `−e_0…0`, the two channels the existing fourth hash reads as left and right,
334
+ * and every other channel starts at zero. A host's applications carry no
335
+ * higher signature, so nothing is routed to them; `C mod arity` is the
336
+ * application phase, not a signature rule. Count zero is the caller's nil.
337
+ *
338
+ * @throws DenumError `$!.denum-coherence` when the loads are not whole counts.
339
+ */
340
+ export function fromHost(rank, count, size, residual) {
341
+ checkRank(rank);
342
+ if (rank === 0)
343
+ return denum(0, count, [size]);
344
+ const sum = size + residual;
345
+ const difference = size - residual;
346
+ if (sum < 0n || difference < 0n || sum % 2n !== 0n || difference % 2n !== 0n) {
347
+ throw new DenumError("$!.denum-coherence");
348
+ }
349
+ const loads = Array(arity(rank)).fill(0n);
350
+ loads[0] = sum / 2n;
351
+ loads[arity(rank) / 2] = difference / 2n;
352
+ return denum(rank, count, loads);
353
+ }
354
+ /** Text: `Denum(CD[2], C=7, W=[3, 2, 0, 1])` lists loads in channel order. */
355
+ export function denumText(source) {
356
+ return `Denum(${schemaName(source.rank)}, C=${source.count}, W=[${source.loads.join(", ")}])`;
357
+ }
@@ -373,7 +373,7 @@ export class FrameArray extends FrameList {
373
373
  /** A declared Count, Size or residual first; the list's own rule otherwise. */
374
374
  sizeFor(key) {
375
375
  const failure = this.observationError();
376
- if (failure && Frame.SIZE_OPERATORS.includes(key))
376
+ if (failure && Frame.isSizeOperator(key))
377
377
  return failure;
378
378
  return this.declaredObservation(key) ?? super.sizeFor(key);
379
379
  }
@@ -177,7 +177,7 @@ export class FrameExpr extends FrameList {
177
177
  continue;
178
178
  }
179
179
  if (index === 0 && value.isSymbol() && value.is.address &&
180
- Frame.SIZE_OPERATORS.includes(value.spelling())) {
180
+ Frame.isSizeOperator(value.spelling())) {
181
181
  sum = value;
182
182
  continue;
183
183
  }
@@ -185,7 +185,7 @@ export class FrameExpr extends FrameList {
185
185
  // is not a receiver, so a leading .## remains an address (#411).
186
186
  if (index > 0 && sum.is.void && value.isSymbol() &&
187
187
  value.is.address &&
188
- Frame.SIZE_OPERATORS.includes(value.spelling())) {
188
+ Frame.isSizeOperator(value.spelling())) {
189
189
  sum = value.called_by(sum);
190
190
  continue;
191
191
  }
@@ -553,7 +553,7 @@ export class FrameScoped extends Frame {
553
553
  /** Sizes as its term does, `.#`, `.##` and `.###`; anything else is applied. */
554
554
  call(argument, parameter = Frame.nil) {
555
555
  if (argument.isSymbol() && argument.is.address === true &&
556
- Frame.SIZE_OPERATORS.includes(argument.spelling())) {
556
+ Frame.isSizeOperator(argument.spelling())) {
557
557
  return this.sizeFor(argument.spelling());
558
558
  }
559
559
  return this.live().call(argument, parameter);
@@ -159,7 +159,7 @@ export class FrameLazy extends FrameExpr {
159
159
  */
160
160
  sizeOperator(argument) {
161
161
  if (argument instanceof FrameSymbol && argument.is.address === true &&
162
- Frame.SIZE_OPERATORS.includes(argument.spelling())) {
162
+ Frame.isSizeOperator(argument.spelling())) {
163
163
  return this.sizeFor(argument.spelling());
164
164
  }
165
165
  return undefined;
@@ -145,7 +145,7 @@ export class FrameList extends Frame {
145
145
  /** Immediate count is unchanged; Size and residual share native child sizes. */
146
146
  sizeFor(key) {
147
147
  const failure = this.observationError();
148
- if (failure && Frame.SIZE_OPERATORS.includes(key))
148
+ if (failure && Frame.isSizeOperator(key))
149
149
  return failure;
150
150
  if (key === Frame.kCOUNT) {
151
151
  const elements = this.sizeElements();
@@ -32,6 +32,14 @@ const recognizeName = (symbol, source = "") => {
32
32
  if (Frame.SIZE_OPERATORS.includes(source + char)) {
33
33
  return { disposition: ScanDisposition.Consume };
34
34
  }
35
+ // A single hash followed by decimal digits is the ranked hash `.#n`
36
+ // (occurrence/figure 007.32). Only the lone `#` takes digits: `.##2` is
37
+ // `.##` and then the count 2, as before, and `.#2x` is `.#2` then `x`.
38
+ // The spelling is canonical decimal, one name per rung: a zero ends the name,
39
+ // so `.#05` is `.#0` and then the number 5.
40
+ if (/^#(|[1-9][0-9]*)$/.test(source) && /^[0-9]$/.test(char)) {
41
+ return { disposition: ScanDisposition.Consume };
42
+ }
35
43
  if (source.startsWith(Frame.kCOUNT)) {
36
44
  return { disposition: ScanDisposition.CompleteRedispatch };
37
45
  }
@@ -289,7 +289,7 @@ export class FrameSymbol extends FrameAtom {
289
289
  // A size operator answers for the receiver itself rather than looking a
290
290
  // name up, so the scope a value was read from never answers for it (#411).
291
291
  // Only an address is one, since the lexer looks `#` up as the comment.
292
- if (this.is.address === true && Frame.SIZE_OPERATORS.includes(this.data)) {
292
+ if (this.is.address === true && Frame.isSizeOperator(this.data)) {
293
293
  return context.referent().sizeFor(this.data);
294
294
  }
295
295
  // A dotted arithmetic operator is the operator itself: `xs ./ 3` is
@@ -452,6 +452,17 @@ export function inspectFlags(flags) {
452
452
  * manipulating and interacting with frames.
453
453
  */
454
454
  export class Frame extends MetaFrame {
455
+ /**
456
+ * Whether `key` names an observer a Frame answers for itself: one of the four
457
+ * fixed hashes `#`, `##`, `###`, `####`, or a ranked hash `#n`.
458
+ */
459
+ static isSizeOperator(key) {
460
+ return Frame.SIZE_OPERATORS.includes(key) || Frame.RANKED_HASH.test(key);
461
+ }
462
+ /** The rung n of a ranked hash `#n`, or undefined for any other key. */
463
+ static hashRank(key) {
464
+ return Frame.RANKED_HASH.test(key) ? Number(key.slice(1)) : undefined;
465
+ }
455
466
  /** Creates a runtime error value without coupling MetaFrame to FrameNote. */
456
467
  static error(source) {
457
468
  const error = new (class extends Frame {
@@ -724,7 +735,7 @@ export class Frame extends MetaFrame {
724
735
  * Nil has no Count, so no residual or Demi.
725
736
  */
726
737
  sizeFor(key) {
727
- if (!Frame.SIZE_OPERATORS.includes(key))
738
+ if (!Frame.isSizeOperator(key))
728
739
  return Frame.nil;
729
740
  const failure = this.observationError();
730
741
  if (failure)
@@ -751,8 +762,10 @@ export class Frame extends MetaFrame {
751
762
  }
752
763
  case Frame.kDEMI:
753
764
  return Frame.demi(this);
754
- default:
755
- return Frame.nil;
765
+ default: {
766
+ const rank = Frame.hashRank(key);
767
+ return rank === undefined ? Frame.nil : Frame.denum(this, rank);
768
+ }
756
769
  }
757
770
  }
758
771
  /** Nonnegative represented quantity; path tracks recursive containment only. */
@@ -1070,7 +1083,7 @@ export class Frame extends MetaFrame {
1070
1083
  return undefined;
1071
1084
  }
1072
1085
  const key = argument.spelling();
1073
- return Frame.SIZE_OPERATORS.includes(key) ? key : undefined;
1086
+ return Frame.isSizeOperator(key) ? key : undefined;
1074
1087
  }
1075
1088
  /**
1076
1089
  * Negation is decided by the symbol's name, never by how it prints.
@@ -1155,6 +1168,19 @@ Object.defineProperty(Frame, "SIZE_OPERATORS", {
1155
1168
  Frame.kDEMI,
1156
1169
  ]
1157
1170
  });
1171
+ /**
1172
+ * The ranked hash `.#n` (occurrence/figure 007.32): `#` and decimal digits,
1173
+ * as `.#2`, in canonical decimal (no leading zero, so each rung has one
1174
+ * spelling). Its n is the Cayley–Dickson rung and not a hash count, so it is
1175
+ * not one of {@link SIZE_OPERATORS}, whose spellings are the four fixed
1176
+ * hashes. Bare `.#` stays the Count.
1177
+ */
1178
+ Object.defineProperty(Frame, "RANKED_HASH", {
1179
+ enumerable: true,
1180
+ configurable: true,
1181
+ writable: true,
1182
+ value: /^#(0|[1-9][0-9]*)$/
1183
+ });
1158
1184
  /**
1159
1185
  * BEGIN_EXPR is the symbol for the beginning of an expression.
1160
1186
  */
package/esm/lib/frames.js CHANGED
@@ -28,6 +28,8 @@ export { FrameNumeric, NUMERIC_RANK, } from "./frames/frame-numeric.js";
28
28
  export { FrameInt } from "./frames/frame-int.js";
29
29
  export { FrameCount } from "./frames/frame-count.js";
30
30
  export { installDemiConstructor, installRouterRecognizer, observeDemi, } from "./frames/fourth-hash.js";
31
+ export { installDenumConstructor, observeDenum, } from "./frames/denum-hash.js";
32
+ export * as denum from "./frames/denum.js";
31
33
  export { FrameDecimal } from "./frames/frame-decimal.js";
32
34
  export { FrameSequence } from "./frames/frame-sequence.js";
33
35
  export { FrameTypedNumber } from "./frames/frame-typed-number.js";