hclang 0.46.0 → 0.48.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,381 +0,0 @@
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 signed observation {@link coordinates}, 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 Denum coordinates (occurrence/figure 007.32j, 007.33): an ordered list of
303
- * integers, one per direction `b`, read from the preserved state without
304
- * changing it. The one projection behind both HC spellings:
305
- *
306
- * - signed (`d.coordinates ()`): `S_b = W₍₊,b₎ − W₍₋,b₎`, the algebraic value;
307
- * - unsigned (`d.coordinates <>`): `U_b = W₍₊,b₎ + W₍₋,b₎`, the direction load
308
- * with the ± signature forgotten (so `U ≠ |S|`).
309
- *
310
- * Width is 1 at D0 and `2ⁿ⁻¹` at Dn, n ≥ 1, in the direction order of
311
- * {@link signatureOf}. D0 has one unsigned channel `e_ε`, so both modes answer
312
- * `[W_ε]`. Count is neither used nor returned. Given the rank, the pair (S, U)
313
- * recovers the loads, `W₊ = (U+S)/2` and `W₋ = (U−S)/2`; Count, history and
314
- * the Denum itself are not recovered. Always a fresh array.
315
- */
316
- export function coordinates(source, unsigned = false) {
317
- if (source.rank === 0)
318
- return [source.loads[0]];
319
- const half = source.loads.length / 2;
320
- return Array.from({ length: half }, (_, b) => unsigned
321
- ? source.loads[b] + source.loads[half + b]
322
- : source.loads[b] - source.loads[half + b]);
323
- }
324
- /**
325
- * The signed coordinates of v0.45.0.
326
- *
327
- * @deprecated Legacy compatibility alias of `coordinates(source)`, the signed
328
- * mode; `d.observe ()` in HC is its deprecated spelling. Use {@link coordinates}.
329
- */
330
- export function observe(source) {
331
- return coordinates(source);
332
- }
333
- /**
334
- * The declared residual of a Denum, the signed total `Σ W₊ − Σ W₋`. It is the
335
- * sum of the observed coordinates, so it is the existing residual `L − R` at
336
- * D1 and 0 at D0, which has no negative channel. A single integer, it forgets
337
- * the directions; it is an observer of the state, not the state.
338
- */
339
- export function residual(source) {
340
- return source.rank === 0
341
- ? 0n
342
- : coordinates(source).reduce((a, b) => a + b, 0n);
343
- }
344
- /** True when every observed coordinate is zero. */
345
- export function observesZero(source) {
346
- return coordinates(source).every((coordinate) => coordinate === 0n);
347
- }
348
- /** True when no channel carries a load. */
349
- export function isEmpty(source) {
350
- return source.loads.every((load) => load === 0n);
351
- }
352
- /**
353
- * Host to Denum (the construction behind `X.#n`, distinct from promotion).
354
- *
355
- * A host answers only its Count `C`, Size `s` and residual `r`. Rank 0 keeps
356
- * the one load `s`. Rank `n ≥ 1` places `(s+r)/2` on `+e_0…0` and `(s−r)/2` on
357
- * `−e_0…0`, the two channels the existing fourth hash reads as left and right,
358
- * and every other channel starts at zero. A host's applications carry no
359
- * higher signature, so nothing is routed to them; `C mod arity` is the
360
- * application phase, not a signature rule. Count zero is the caller's nil.
361
- *
362
- * @throws DenumError `$!.denum-coherence` when the loads are not whole counts.
363
- */
364
- export function fromHost(rank, count, size, residual) {
365
- checkRank(rank);
366
- if (rank === 0)
367
- return denum(0, count, [size]);
368
- const sum = size + residual;
369
- const difference = size - residual;
370
- if (sum < 0n || difference < 0n || sum % 2n !== 0n || difference % 2n !== 0n) {
371
- throw new DenumError("$!.denum-coherence");
372
- }
373
- const loads = Array(arity(rank)).fill(0n);
374
- loads[0] = sum / 2n;
375
- loads[arity(rank) / 2] = difference / 2n;
376
- return denum(rank, count, loads);
377
- }
378
- /** Text: `Denum(CD[2], C=7, W=[3, 2, 0, 1])` lists loads in channel order. */
379
- export function denumText(source) {
380
- return `Denum(${schemaName(source.rank)}, C=${source.count}, W=[${source.loads.join(", ")}])`;
381
- }
@@ -1,131 +0,0 @@
1
- // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
- import { Frame } from "../frames/frame.js";
3
- import { FrameArray } from "../frames/frame-array.js";
4
- import { FrameCount } from "../frames/frame-count.js";
5
- import { FrameString } from "../frames/frame-string.js";
6
- import { add, arity, conjugate, coordinates, denum, DenumError, multiply, opposite, promote, residual, schemaName, signatureName, subtract, } from "../frames/denum.js";
7
- /**
8
- * The operations `denum-engine` performs, selected by the first element of its
9
- * argument. The argument is `[op, rank, count, loads, rank, count, loads]`: the
10
- * left Denum state, then the right one for the binary operations.
11
- */
12
- export const DENUM_OPERATIONS = {
13
- /** Validate a state and answer it as built. */
14
- build: 0,
15
- add: 1,
16
- subtract: 2,
17
- multiply: 3,
18
- opposite: 4,
19
- conjugate: 5,
20
- /** Promote the left state to the rank of the right state's first slot. */
21
- promote: 6,
22
- /**
23
- * The integer coordinate of each direction, an observation. The fifth slot
24
- * is the mode: 0 signed `W₊ − W₋`, 1 unsigned `W₊ + W₋`.
25
- */
26
- coordinates: 7,
27
- /** Legacy name of {@link DENUM_OPERATIONS.coordinates} (the signed mode). */
28
- observe: 7,
29
- /** The native signature names of the left state's channels, as text. */
30
- names: 8,
31
- /** The schema name `CD[n]` of the left state's rank, as text. */
32
- schema: 9,
33
- /** Positive minus negative load total: the declared residual, an integer. */
34
- residual: 10,
35
- };
36
- const counts = (frame) => {
37
- const value = frame.referent();
38
- return value instanceof FrameCount && typeof value.data === "bigint"
39
- ? value.data
40
- : undefined;
41
- };
42
- /** The state a triple of Frames holds, or the contract failure. */
43
- function readState(rank, count, loads) {
44
- const r = rank === undefined ? undefined : counts(rank);
45
- const c = count === undefined ? undefined : counts(count);
46
- // Base Frame.elements() answers [this] for any non-aggregate, so a bare Count
47
- // would read as a one-element list: require an aggregate, as the hash reader
48
- // `storedState` does.
49
- const list = loads?.referent();
50
- const elements = list?.ownsIdentity() ? list.elements() : undefined;
51
- const l = elements?.map(counts);
52
- if (r === undefined || c === undefined || !l || l.some((x) => x === undefined)) {
53
- throw new DenumError("$!!.invalid-denum");
54
- }
55
- if (r > 5n)
56
- throw new DenumError("$!!.invalid-denum", `rank ${r}`);
57
- return denum(Number(r), c, l);
58
- }
59
- const stateFrame = (state) => new FrameArray([
60
- Frame.count(BigInt(state.rank)),
61
- Frame.count(state.count),
62
- new FrameArray(state.loads.map((load) => Frame.count(load))),
63
- ]);
64
- /**
65
- * `denum-engine`, the one native seam under the HC class in `hc/denum.hc`.
66
- *
67
- * It is pure arithmetic over the typed reference in `lib/frames/denum.ts`: it
68
- * reads Counts and answers fresh data, touches no scope and holds no state,
69
- * and it refuses anything malformed with a named error rather than guessing.
70
- * The Denum itself is an HC class; this is only how that class reaches the
71
- * Cayley–Dickson recurrence without a hand-written table in HC.
72
- */
73
- export const DenumEngine = (_source, block) => {
74
- const failure = block.referent().observationError();
75
- if (failure)
76
- return failure;
77
- const args = block.referent().elements();
78
- const op = args[0] === undefined ? undefined : counts(args[0]);
79
- if (op === undefined)
80
- return Frame.error("$!!.invalid-denum");
81
- try {
82
- const left = readState(args[1], args[2], args[3]);
83
- switch (Number(op)) {
84
- case DENUM_OPERATIONS.build:
85
- return stateFrame(left);
86
- case DENUM_OPERATIONS.opposite:
87
- return stateFrame(opposite(left));
88
- case DENUM_OPERATIONS.conjugate:
89
- return stateFrame(conjugate(left));
90
- case DENUM_OPERATIONS.coordinates: {
91
- // Absent (the v0.45.0 call shape) is signed; otherwise exactly 0 or 1.
92
- const mode = args[4] === undefined ? 0n : counts(args[4]);
93
- if (mode !== 0n && mode !== 1n) {
94
- return Frame.error("$!!.invalid-denum");
95
- }
96
- return new FrameArray(coordinates(left, mode === 1n).map((c) => Frame.signed(c)));
97
- }
98
- case DENUM_OPERATIONS.names:
99
- return new FrameArray(Array.from({ length: arity(left.rank) }, (_, channel) => new FrameString(signatureName(left.rank, channel))));
100
- case DENUM_OPERATIONS.residual:
101
- return Frame.signed(residual(left));
102
- case DENUM_OPERATIONS.schema:
103
- return new FrameString(schemaName(left.rank));
104
- case DENUM_OPERATIONS.promote: {
105
- const target = args[4] === undefined ? undefined : counts(args[4]);
106
- if (target === undefined || target > 5n) {
107
- return Frame.error("$!.denum-rank");
108
- }
109
- return stateFrame(promote(left, Number(target)));
110
- }
111
- case DENUM_OPERATIONS.add:
112
- case DENUM_OPERATIONS.subtract:
113
- case DENUM_OPERATIONS.multiply: {
114
- const right = readState(args[4], args[5], args[6]);
115
- const operate = Number(op) === DENUM_OPERATIONS.add
116
- ? add
117
- : Number(op) === DENUM_OPERATIONS.subtract
118
- ? subtract
119
- : multiply;
120
- return stateFrame(operate(left, right));
121
- }
122
- default:
123
- return Frame.error("$!!.invalid-denum");
124
- }
125
- }
126
- catch (error) {
127
- if (error instanceof DenumError)
128
- return Frame.error(error.kind);
129
- throw error;
130
- }
131
- };