hclang 0.47.0 → 0.49.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/esm/lib/bundle/hc-sources.js +501 -1227
- package/esm/lib/execute/denum-prologue.js +53 -67
- package/esm/lib/execute/hc-eval.js +1 -3
- package/esm/lib/execute/resource-import.js +1 -1
- package/esm/lib/execute/router-prologue.js +2 -2
- package/esm/lib/frames/fourth-hash.js +101 -40
- package/esm/lib/frames/frame-array.js +3 -3
- package/esm/lib/frames/frame-expr.js +3 -3
- package/esm/lib/frames/frame-lazy.js +2 -2
- package/esm/lib/frames/frame-list.js +1 -1
- package/esm/lib/frames/frame-name.js +10 -10
- package/esm/lib/frames/frame-symbol.js +1 -1
- package/esm/lib/frames/frame.js +27 -38
- package/esm/lib/frames.js +1 -3
- package/esm/lib/ops.js +0 -3
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
- package/esm/lib/execute/demi-prologue.js +0 -103
- package/esm/lib/frames/denum-hash.js +0 -180
- package/esm/lib/frames/denum.js +0 -601
- package/esm/lib/ops/denum.js +0 -238
package/esm/lib/frames/denum.js
DELETED
|
@@ -1,601 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Denum and its derived CD view (occurrence/figure 007.31, 007.32, 007.32b,
|
|
3
|
-
* 007.35, 007.35b, 007.36).
|
|
4
|
-
*
|
|
5
|
-
* This is the typed reference implementation. It depends on no Frame, so the
|
|
6
|
-
* HC classes in `hc/denum.hc` and the exhaustive tests can be checked against
|
|
7
|
-
* it. A Denum is a constructed state, not a conventional number:
|
|
8
|
-
*
|
|
9
|
-
* - a retained application **Count** `C ∈ ℕ₀`;
|
|
10
|
-
* - a nonempty ordered list of **loads**, each `W ∈ ℕ₀`, one per channel.
|
|
11
|
-
*
|
|
12
|
-
* There is no stored rank. The arity is `A = loads.length`, positive and
|
|
13
|
-
* independent of the Count, and the rank is the derived integer reading
|
|
14
|
-
* {@link rankOf}` = ⌊log₂ A⌋`.
|
|
15
|
-
*
|
|
16
|
-
* **One retained state, one derived view.** A {@link Denum} is the only
|
|
17
|
-
* retained state, of any positive arity. It carries the observers and the
|
|
18
|
-
* equal-arity componentwise {@link addDenum}, and nothing else. {@link CD} is
|
|
19
|
-
* not a second state: it is a *view* of a Denum, derived by {@link cdViewOf}
|
|
20
|
-
* (HC: `d.cd ()`), the one request for the Cayley–Dickson interpretation. A
|
|
21
|
-
* view holds its source Denum and reads `count` and `loads` from it, so it has
|
|
22
|
-
* no Count, loads or rank of its own that could disagree. The arity must be one
|
|
23
|
-
* of `1, 2, 4, 8, 16, 32`; the algebra is an interpretation imposed by the
|
|
24
|
-
* request, never a theorem inferred from the channel count, so a Denum with four
|
|
25
|
-
* loads is not a CD[2] until it is viewed as one. View identity is a private
|
|
26
|
-
* class brand (`#cd in x`), so a lookalike `{count, loads}` object, or a Denum
|
|
27
|
-
* with `2ⁿ` loads, is not a view. There is no public raw-payload view
|
|
28
|
-
* constructor: `new CD` refuses without a module-private token, and the
|
|
29
|
-
* module's `mintCD` is not exported.
|
|
30
|
-
*
|
|
31
|
-
* On a view, rank 0 has one unsigned channel `e_ε`. Rank `n ≥ 1` has `2ⁿ`
|
|
32
|
-
* channels, `±e_b` for every direction `b` of `n − 1` bits. Channels are
|
|
33
|
-
* ordered positives first, then negatives, each by direction value: channel
|
|
34
|
-
* `b` is `+e_b` and channel `2ⁿ⁻¹ + b` is `−e_b`. A minus names a *signature*;
|
|
35
|
-
* a load is never negative. The algebraic value is the signed observation
|
|
36
|
-
* {@link coordinates}, not the view.
|
|
37
|
-
*
|
|
38
|
-
* The product of two signed signatures is
|
|
39
|
-
* `(σ e_a)(ρ e_b) = σρ·τ(a, b) e_(a⊕b)`, where the twist `τ` is generated by
|
|
40
|
-
* the Cayley–Dickson recurrence `(a,b)(c,d) = (ac − d̄b, da + bc̄)` in
|
|
41
|
-
* {@link cdTwist}. No multiplication table is written by hand.
|
|
42
|
-
*
|
|
43
|
-
* Freshness: both are values. `loads` is frozen, and every operation here
|
|
44
|
-
* ({@link promote}, {@link opposite}, {@link conjugate}, {@link add},
|
|
45
|
-
* {@link multiply}, ...) answers a new view over a new source Denum with its
|
|
46
|
-
* own `loads` array and never the argument or an alias of its loads, even when
|
|
47
|
-
* the result equals the argument. Every result's retained Count and loads are
|
|
48
|
-
* recovered as a Denum by `view.source`.
|
|
49
|
-
*
|
|
50
|
-
* Count is carried, never derived from loads: `C(X+Y) = C(X)+C(Y)`,
|
|
51
|
-
* `C(X⊗Y) = C(X)·C(Y)`, `C(Opp X) = C(X)` and promotion keeps it. These are
|
|
52
|
-
* the explicit candidate laws of 007.32 §6, implemented for pure Denum
|
|
53
|
-
* arithmetic only. They are not the application phase `C mod arity`, and
|
|
54
|
-
* nothing here changes Demi.
|
|
55
|
-
*
|
|
56
|
-
* Refusals: `$!.denum-not-cd` (a CD operation given a value that is not a
|
|
57
|
-
* view), `$!.denum-mixed` (a Denum and a view in one binary operation),
|
|
58
|
-
* `$!.denum-not-generic` (a generic operation given two views),
|
|
59
|
-
* `$!.denum-arity-mismatch` (generic addition of different arities),
|
|
60
|
-
* `$!.denum-unsupported-arity` (a view or ranked embedding of a non-binary
|
|
61
|
-
* arity), `$!.denum-view-source` (a view given to the ranked embedding) and
|
|
62
|
-
* `$!.denum-view-forged` (`new CD` without the module-private token).
|
|
63
|
-
*
|
|
64
|
-
* @module
|
|
65
|
-
*/
|
|
66
|
-
var __classPrivateFieldIn = (this && this.__classPrivateFieldIn) || function(state, receiver) {
|
|
67
|
-
if (receiver === null || (typeof receiver !== "object" && typeof receiver !== "function")) throw new TypeError("Cannot use 'in' operator on non-object");
|
|
68
|
-
return typeof state === "function" ? receiver === state : state.has(receiver);
|
|
69
|
-
};
|
|
70
|
-
var _CD_cd;
|
|
71
|
-
/** The highest rung implemented: D5, the sedenions, 32 channels. */
|
|
72
|
-
export const MAX_RANK = 5;
|
|
73
|
-
/**
|
|
74
|
-
* A Cayley–Dickson view of a {@link Denum}. It is not retained state: it holds
|
|
75
|
-
* its source Denum and answers `count` and `loads` from it, so nothing can
|
|
76
|
-
* disagree with the source. Only {@link cdViewOf} (which validates a shipped
|
|
77
|
-
* arity) and the operations that answer a view make one, so `CD.is` proves the
|
|
78
|
-
* view was derived, not that an arity happened to be a power of two.
|
|
79
|
-
*/
|
|
80
|
-
export class CD {
|
|
81
|
-
/**
|
|
82
|
-
* Not callable from outside this module: the first argument must be the
|
|
83
|
-
* module-private mint token, so `new CD(...)` and `new view.constructor(...)`
|
|
84
|
-
* both refuse at runtime, whatever TypeScript is told.
|
|
85
|
-
*/
|
|
86
|
-
constructor(token, count, loads) {
|
|
87
|
-
_CD_cd.set(this, true);
|
|
88
|
-
/** The retained Count and loads: the one source of truth, frozen. */
|
|
89
|
-
Object.defineProperty(this, "source", {
|
|
90
|
-
enumerable: true,
|
|
91
|
-
configurable: true,
|
|
92
|
-
writable: true,
|
|
93
|
-
value: void 0
|
|
94
|
-
});
|
|
95
|
-
if (token !== MINT)
|
|
96
|
-
throw new DenumError("$!.denum-view-forged");
|
|
97
|
-
this.source = Object.freeze({
|
|
98
|
-
count,
|
|
99
|
-
loads: Object.freeze([...loads]),
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
get count() {
|
|
103
|
-
return this.source.count;
|
|
104
|
-
}
|
|
105
|
-
get loads() {
|
|
106
|
-
return this.source.loads;
|
|
107
|
-
}
|
|
108
|
-
/** True exactly for views built by this module: not for lookalikes. */
|
|
109
|
-
static is(value) {
|
|
110
|
-
return typeof value === "object" && value !== null && __classPrivateFieldIn(_CD_cd, value);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
_CD_cd = new WeakMap();
|
|
114
|
-
/** The module-private token that authorizes the {@link CD} constructor. */
|
|
115
|
-
const MINT = Symbol("CD.mint");
|
|
116
|
-
/**
|
|
117
|
-
* The only way to make a view: module-private, never exported. It re-validates
|
|
118
|
-
* Count, loads and shipped CD arity, so no operation can brand an invalid state
|
|
119
|
-
* even by mistake. Callers outside the module can only reach a view through
|
|
120
|
-
* {@link cdViewOf} or an operation that answers one.
|
|
121
|
-
*/
|
|
122
|
-
function mintCD(count, loads) {
|
|
123
|
-
const state = denum(count, loads);
|
|
124
|
-
if (!isCDArity(state.loads.length)) {
|
|
125
|
-
throw new DenumError("$!.denum-unsupported-arity", `${state.loads.length}`);
|
|
126
|
-
}
|
|
127
|
-
return new CD(MINT, state.count, state.loads);
|
|
128
|
-
}
|
|
129
|
-
/** A failure of the Denum contract, named like an HC error. */
|
|
130
|
-
export class DenumError extends Error {
|
|
131
|
-
constructor(kind, detail = "") {
|
|
132
|
-
super(detail ? `${kind} ${detail}` : kind);
|
|
133
|
-
Object.defineProperty(this, "kind", {
|
|
134
|
-
enumerable: true,
|
|
135
|
-
configurable: true,
|
|
136
|
-
writable: true,
|
|
137
|
-
value: kind
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/** The schema's display name, `CD[n]`. */
|
|
142
|
-
export function schemaName(rank) {
|
|
143
|
-
return `CD[${rank}]`;
|
|
144
|
-
}
|
|
145
|
-
/** Channel count of `CD[rank]`: 1 at rank 0, otherwise `2^rank`. */
|
|
146
|
-
export function arity(rank) {
|
|
147
|
-
checkRank(rank);
|
|
148
|
-
return rank === 0 ? 1 : 2 ** rank;
|
|
149
|
-
}
|
|
150
|
-
/** Number of direction bits of a rank: `rank − 1`, or 0 at rank 0. */
|
|
151
|
-
export function directionBits(rank) {
|
|
152
|
-
checkRank(rank);
|
|
153
|
-
return rank === 0 ? 0 : rank - 1;
|
|
154
|
-
}
|
|
155
|
-
function checkRank(rank) {
|
|
156
|
-
if (!Number.isInteger(rank) || rank < 0 || rank > MAX_RANK) {
|
|
157
|
-
throw new DenumError("$!.denum-rank", String(rank));
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* The derived rank of a channel arity: `⌊log₂ A⌋`, the greatest `n` with
|
|
162
|
-
* `2ⁿ ≤ A`, by integer arithmetic only (no floating-point logarithm). A is a
|
|
163
|
-
* positive integer: 1→0, 2,3→1, 4..7→2, 8..15→3, 32..63→5, 64→6.
|
|
164
|
-
*
|
|
165
|
-
* @throws DenumError `$!!.invalid-denum` for a non-positive or non-integer A.
|
|
166
|
-
*/
|
|
167
|
-
export function rankOfArity(channels) {
|
|
168
|
-
if (!Number.isInteger(channels) || channels < 1) {
|
|
169
|
-
throw new DenumError("$!!.invalid-denum", `arity ${channels}`);
|
|
170
|
-
}
|
|
171
|
-
let rank = 0;
|
|
172
|
-
let threshold = 2;
|
|
173
|
-
while (threshold <= channels) {
|
|
174
|
-
rank++;
|
|
175
|
-
threshold *= 2;
|
|
176
|
-
}
|
|
177
|
-
return rank;
|
|
178
|
-
}
|
|
179
|
-
/** The derived rank of a Denum, `⌊log₂ loads.length⌋`; never `count`. */
|
|
180
|
-
export function rankOf(source) {
|
|
181
|
-
return rankOfArity(source.loads.length);
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* The one CD capability predicate: the arity is a power of two whose rung is
|
|
185
|
-
* implemented, `A = 2ʳ` with `0 ≤ r ≤ MAX_RANK`. Rank alone is not enough:
|
|
186
|
-
* A = 3 has rank 1 and A = 6 rank 2, and neither is a CD rung.
|
|
187
|
-
*/
|
|
188
|
-
export function isCDArity(channels) {
|
|
189
|
-
const rank = Number.isInteger(channels) && channels >= 1
|
|
190
|
-
? rankOfArity(channels)
|
|
191
|
-
: -1;
|
|
192
|
-
return rank >= 0 && rank <= MAX_RANK && channels === 2 ** rank;
|
|
193
|
-
}
|
|
194
|
-
/** True when the value was derived as a CD view (see {@link cdViewOf}). */
|
|
195
|
-
export function isCD(source) {
|
|
196
|
-
return CD.is(source);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* The CD guard at every CD-only entry point: the value must be a CD view derived by
|
|
200
|
-
* {@link cdViewOf}. A generic Denum is refused whatever its arity.
|
|
201
|
-
*
|
|
202
|
-
* @throws DenumError `$!.denum-not-cd` for anything that is not a CD.
|
|
203
|
-
*/
|
|
204
|
-
export function requireCD(source) {
|
|
205
|
-
if (!CD.is(source))
|
|
206
|
-
throw new DenumError("$!.denum-not-cd");
|
|
207
|
-
return source;
|
|
208
|
-
}
|
|
209
|
-
/** The two operands of a binary CD operation, both required to be CD. */
|
|
210
|
-
function requireCDs(left, right) {
|
|
211
|
-
const l = CD.is(left);
|
|
212
|
-
const r = CD.is(right);
|
|
213
|
-
if (l && r)
|
|
214
|
-
return [left, right];
|
|
215
|
-
throw new DenumError(l !== r ? "$!.denum-mixed" : "$!.denum-not-cd");
|
|
216
|
-
}
|
|
217
|
-
/** The rank of a CD, the derived `⌊log₂ A⌋`; requires CD identity. */
|
|
218
|
-
export function cdRank(source) {
|
|
219
|
-
return rankOf(requireCD(source));
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* The schema description: `CD[n]` for a CD, otherwise `Arity[A]`, which never
|
|
223
|
-
* claims a Cayley–Dickson rung even for a power-of-two arity.
|
|
224
|
-
*/
|
|
225
|
-
export function schemaOf(source) {
|
|
226
|
-
return CD.is(source)
|
|
227
|
-
? schemaName(rankOf(source))
|
|
228
|
-
: `Arity[${source.loads.length}]`;
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* The Cayley–Dickson twist of two basis directions of `m` bits: the sign
|
|
232
|
-
* `τ(x, y) ∈ {+1, −1}` with `e_x · e_y = τ(x, y) e_(x⊕y)` in the real algebra
|
|
233
|
-
* of dimension `2^m`, generated by the doubling recurrence and conjugation
|
|
234
|
-
* rather than by a table.
|
|
235
|
-
*
|
|
236
|
-
* Write `x = (x₁ x′)` with the top bit first. By
|
|
237
|
-
* `(a,b)(c,d) = (ac − d̄b, da + bc̄)` and `conj(a,b) = (conj a, −b)`:
|
|
238
|
-
*
|
|
239
|
-
* - `(a,0)(c,0)`: `τ(x′, y′)`
|
|
240
|
-
* - `(a,0)(0,d)`: `τ(y′, x′)`
|
|
241
|
-
* - `(0,b)(c,0)`: `conj(y′)·τ(x′, y′)`
|
|
242
|
-
* - `(0,b)(0,d)`: `−conj(y′)·τ(y′, x′)`
|
|
243
|
-
*
|
|
244
|
-
* where `conj(e_k)` contributes `+1` only for the scalar direction `k = 0`.
|
|
245
|
-
*/
|
|
246
|
-
export function cdTwist(x, y, m) {
|
|
247
|
-
if (m === 0)
|
|
248
|
-
return 1;
|
|
249
|
-
const half = 2 ** (m - 1);
|
|
250
|
-
const xh = x >= half;
|
|
251
|
-
const yh = y >= half;
|
|
252
|
-
const x1 = xh ? x - half : x;
|
|
253
|
-
const y1 = yh ? y - half : y;
|
|
254
|
-
const conj = y1 === 0 ? 1 : -1;
|
|
255
|
-
if (!xh && !yh)
|
|
256
|
-
return cdTwist(x1, y1, m - 1);
|
|
257
|
-
if (!xh && yh)
|
|
258
|
-
return cdTwist(y1, x1, m - 1);
|
|
259
|
-
if (xh && !yh)
|
|
260
|
-
return (conj * cdTwist(x1, y1, m - 1));
|
|
261
|
-
return (-conj * cdTwist(y1, x1, m - 1));
|
|
262
|
-
}
|
|
263
|
-
/** The signature a channel index names at `rank`. */
|
|
264
|
-
export function signatureOf(rank, channel) {
|
|
265
|
-
const size = arity(rank);
|
|
266
|
-
if (!Number.isInteger(channel) || channel < 0 || channel >= size) {
|
|
267
|
-
throw new DenumError("$!.denum-channel", String(channel));
|
|
268
|
-
}
|
|
269
|
-
if (rank === 0)
|
|
270
|
-
return { negative: false, direction: 0 };
|
|
271
|
-
const half = size / 2;
|
|
272
|
-
return channel >= half
|
|
273
|
-
? { negative: true, direction: channel - half }
|
|
274
|
-
: { negative: false, direction: channel };
|
|
275
|
-
}
|
|
276
|
-
/** The channel index of a signature at `rank`. */
|
|
277
|
-
export function channelOf(rank, signature) {
|
|
278
|
-
const size = arity(rank);
|
|
279
|
-
if (rank === 0) {
|
|
280
|
-
if (signature.negative || signature.direction !== 0) {
|
|
281
|
-
throw new DenumError("$!.denum-signature", "D0 has only e_ε");
|
|
282
|
-
}
|
|
283
|
-
return 0;
|
|
284
|
-
}
|
|
285
|
-
const half = size / 2;
|
|
286
|
-
if (!Number.isInteger(signature.direction) || signature.direction < 0 ||
|
|
287
|
-
signature.direction >= half) {
|
|
288
|
-
throw new DenumError("$!.denum-signature", String(signature.direction));
|
|
289
|
-
}
|
|
290
|
-
return signature.negative ? half + signature.direction : signature.direction;
|
|
291
|
-
}
|
|
292
|
-
/** Native spelling of a signature: `e_ε` at D0, else `e_b` or `-e_b`. */
|
|
293
|
-
export function signatureName(rank, channel) {
|
|
294
|
-
const { negative, direction } = signatureOf(rank, channel);
|
|
295
|
-
const bits = directionBits(rank);
|
|
296
|
-
const label = bits === 0 ? "ε" : direction.toString(2).padStart(bits, "0");
|
|
297
|
-
return `${negative ? "-" : ""}e_${label}`;
|
|
298
|
-
}
|
|
299
|
-
/** Parses `e_b`, `-e_b` or `e_ε` at `rank`; anything else is an error. */
|
|
300
|
-
export function parseSignature(rank, name) {
|
|
301
|
-
const match = /^(-?)e_([01]+|ε)$/.exec(name);
|
|
302
|
-
if (!match)
|
|
303
|
-
throw new DenumError("$!.denum-signature", name);
|
|
304
|
-
const bits = directionBits(rank);
|
|
305
|
-
const label = match[2];
|
|
306
|
-
if (bits === 0) {
|
|
307
|
-
if (label !== "ε")
|
|
308
|
-
throw new DenumError("$!.denum-signature", name);
|
|
309
|
-
}
|
|
310
|
-
else if (label === "ε" || label.length !== bits) {
|
|
311
|
-
throw new DenumError("$!.denum-signature", name);
|
|
312
|
-
}
|
|
313
|
-
if (rank === 0 && match[1] === "-") {
|
|
314
|
-
throw new DenumError("$!.denum-signature", name);
|
|
315
|
-
}
|
|
316
|
-
return channelOf(rank, {
|
|
317
|
-
negative: match[1] === "-",
|
|
318
|
-
direction: bits === 0 ? 0 : parseInt(label, 2),
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* The signed product of two channels of the same rank: the destination channel
|
|
323
|
-
* and nothing else is computed from the recurrence, never looked up.
|
|
324
|
-
*/
|
|
325
|
-
export function multiplyChannels(rank, left, right) {
|
|
326
|
-
if (rank === 0)
|
|
327
|
-
return 0;
|
|
328
|
-
const a = signatureOf(rank, left);
|
|
329
|
-
const b = signatureOf(rank, right);
|
|
330
|
-
const twist = cdTwist(a.direction, b.direction, directionBits(rank));
|
|
331
|
-
const negative = (a.negative !== b.negative) !== (twist === -1);
|
|
332
|
-
return channelOf(rank, { negative, direction: a.direction ^ b.direction });
|
|
333
|
-
}
|
|
334
|
-
function nonnegative(value, what) {
|
|
335
|
-
if (typeof value !== "bigint" || value < 0n) {
|
|
336
|
-
throw new DenumError("$!!.invalid-denum", what);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Builds a Denum, refusing a negative Count or load or an empty load list.
|
|
341
|
-
* Any positive arity is accepted; the loads are copied.
|
|
342
|
-
*/
|
|
343
|
-
export function denum(count, loads) {
|
|
344
|
-
nonnegative(count, "count");
|
|
345
|
-
if (loads.length < 1) {
|
|
346
|
-
throw new DenumError("$!!.invalid-denum", "loads 0");
|
|
347
|
-
}
|
|
348
|
-
loads.forEach((load) => nonnegative(load, "load"));
|
|
349
|
-
return { count, loads: [...loads] };
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* The derived CD view of a Denum, `d.cd ()`: validates the source as
|
|
353
|
-
* {@link denum} does, then requires a shipped CD arity
|
|
354
|
-
* `A ∈ {1, 2, 4, 8, 16, 32}`. The source is not changed or reordered.
|
|
355
|
-
*
|
|
356
|
-
* @throws DenumError `$!!.invalid-denum` for a malformed state and
|
|
357
|
-
* `$!.denum-unsupported-arity` for any other arity (0 loads is malformed).
|
|
358
|
-
*/
|
|
359
|
-
export function cdViewOf(source) {
|
|
360
|
-
const state = denum(source.count, source.loads);
|
|
361
|
-
if (!isCDArity(state.loads.length)) {
|
|
362
|
-
throw new DenumError("$!.denum-unsupported-arity", `${state.loads.length}`);
|
|
363
|
-
}
|
|
364
|
-
return mintCD(state.count, state.loads);
|
|
365
|
-
}
|
|
366
|
-
/** The zero-load view of a rank with a Count: present, unlike a missing host. */
|
|
367
|
-
export function zero(rank, count = 0n) {
|
|
368
|
-
return cdViewOf(denum(count, Array(arity(rank)).fill(0n)));
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Generic addition: equal arity only, component-wise nonnegative addition of
|
|
372
|
-
* loads and Counts, no cancellation, no padding, truncation or alignment. The
|
|
373
|
-
* result is a fresh generic {@link Denum}, whatever the arity.
|
|
374
|
-
*
|
|
375
|
-
* @throws DenumError `$!.denum-arity-mismatch` for different arities,
|
|
376
|
-
* `$!.denum-mixed` when exactly one operand is a CD and `$!.denum-not-generic`
|
|
377
|
-
* when both are.
|
|
378
|
-
*/
|
|
379
|
-
export function addDenum(left, right) {
|
|
380
|
-
const l = CD.is(left);
|
|
381
|
-
const r = CD.is(right);
|
|
382
|
-
if (l || r) {
|
|
383
|
-
throw new DenumError(l && r ? "$!.denum-not-generic" : "$!.denum-mixed");
|
|
384
|
-
}
|
|
385
|
-
if (left.loads.length !== right.loads.length) {
|
|
386
|
-
throw new DenumError("$!.denum-arity-mismatch", `${left.loads.length} and ${right.loads.length}`);
|
|
387
|
-
}
|
|
388
|
-
return {
|
|
389
|
-
count: left.count + right.count,
|
|
390
|
-
loads: left.loads.map((load, channel) => load + right.loads[channel]),
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
/** The old-summand embedding of loads from rung `from` up to rung `rank`. */
|
|
394
|
-
function embed(loads, from, rank) {
|
|
395
|
-
let current = [...loads];
|
|
396
|
-
for (let at = from; at < rank; at++) {
|
|
397
|
-
const next = at + 1;
|
|
398
|
-
const widened = Array(arity(next)).fill(0n);
|
|
399
|
-
for (let channel = 0; channel < current.length; channel++) {
|
|
400
|
-
const { negative, direction } = signatureOf(at, channel);
|
|
401
|
-
// Leading zero bit leaves the direction value unchanged.
|
|
402
|
-
widened[channelOf(next, { negative, direction })] = current[channel];
|
|
403
|
-
}
|
|
404
|
-
current = widened;
|
|
405
|
-
}
|
|
406
|
-
return current;
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Canonical old-summand embedding `Dn → D(n+1)`: `e_ε ↦ +e_ε` at D0 → D1, and
|
|
410
|
-
* `±e_b ↦ ±e_(0b)` from D1 up, which is the same channel at the same sign with
|
|
411
|
-
* a zero bit placed in front of the direction. Count and every load are kept;
|
|
412
|
-
* the new channels start at zero.
|
|
413
|
-
*/
|
|
414
|
-
export function promote(source, rank) {
|
|
415
|
-
checkRank(rank);
|
|
416
|
-
const from = cdRank(source);
|
|
417
|
-
if (rank < from) {
|
|
418
|
-
throw new DenumError("$!.denum-demotion", `${from} to ${rank}`);
|
|
419
|
-
}
|
|
420
|
-
// Always fresh: a same-rank promotion is a copy, never the argument itself.
|
|
421
|
-
return mintCD(source.count, embed(source.loads, from, rank));
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* The ranked hash's promotion of a retained Denum (`d.#n`): the same
|
|
425
|
-
* load-preserving embedding, answering a generic {@link Denum}, never a view.
|
|
426
|
-
* It does not invoke the CD interpretation. The arity must be a shipped binary
|
|
427
|
-
* rung; any other is `$!.denum-unsupported-arity`, never truncated, padded or
|
|
428
|
-
* read as `2^rank`. A view is not a source.
|
|
429
|
-
*
|
|
430
|
-
* @throws DenumError `$!.denum-rank` above the top rung,
|
|
431
|
-
* `$!.denum-view-source` for a view, `$!.denum-unsupported-arity` for a
|
|
432
|
-
* non-binary arity and `$!.denum-demotion` below the source's own rank.
|
|
433
|
-
*/
|
|
434
|
-
export function promoteDenum(source, rank) {
|
|
435
|
-
checkRank(rank);
|
|
436
|
-
if (CD.is(source))
|
|
437
|
-
throw new DenumError("$!.denum-view-source");
|
|
438
|
-
const state = denum(source.count, source.loads);
|
|
439
|
-
if (!isCDArity(state.loads.length)) {
|
|
440
|
-
throw new DenumError("$!.denum-unsupported-arity", `${state.loads.length}`);
|
|
441
|
-
}
|
|
442
|
-
const from = rankOf(state);
|
|
443
|
-
if (rank < from) {
|
|
444
|
-
throw new DenumError("$!.denum-demotion", `${from} to ${rank}`);
|
|
445
|
-
}
|
|
446
|
-
return { count: state.count, loads: embed(state.loads, from, rank) };
|
|
447
|
-
}
|
|
448
|
-
/** Both operands at the larger rank, by promotion. */
|
|
449
|
-
function align(left, right) {
|
|
450
|
-
requireCDs(left, right);
|
|
451
|
-
const rank = Math.max(cdRank(left), cdRank(right));
|
|
452
|
-
return [promote(left, rank), promote(right, rank), rank];
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Addition: corresponding loads add, opposite channels stay separate, and the
|
|
456
|
-
* Counts add (C(X+Y) = C(X)+C(Y)).
|
|
457
|
-
*/
|
|
458
|
-
export function add(left, right) {
|
|
459
|
-
const [x, y] = align(left, right);
|
|
460
|
-
return mintCD(x.count + y.count, x.loads.map((load, channel) => load + y.loads[channel]));
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Opposite: swaps the loads of `+e_b` and `−e_b`. D0 has no negative channel,
|
|
464
|
-
* so it is promoted to D1 first. The Count is kept.
|
|
465
|
-
*/
|
|
466
|
-
export function opposite(source) {
|
|
467
|
-
const x = promote(source, Math.max(cdRank(source), 1));
|
|
468
|
-
const half = x.loads.length / 2;
|
|
469
|
-
return mintCD(x.count, x.loads.map((_, channel) => x.loads[channel < half ? channel + half : channel - half]));
|
|
470
|
-
}
|
|
471
|
-
/** Subtraction is addition of the opposite; it is observed, never erased. */
|
|
472
|
-
export function subtract(left, right) {
|
|
473
|
-
requireCDs(left, right);
|
|
474
|
-
return add(left, opposite(right));
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Cayley–Dickson conjugation: scalar signatures `±e_0…0` are fixed and every
|
|
478
|
-
* other signature goes to its opposite, `±e_b ↦ ∓e_b`. At D0 it is the
|
|
479
|
-
* identity. Counts and loads are kept, only moved between channels.
|
|
480
|
-
*/
|
|
481
|
-
export function conjugate(source) {
|
|
482
|
-
if (cdRank(source) === 0)
|
|
483
|
-
return mintCD(source.count, source.loads);
|
|
484
|
-
const half = source.loads.length / 2;
|
|
485
|
-
return mintCD(source.count, source.loads.map((load, channel) => {
|
|
486
|
-
const direction = channel % half;
|
|
487
|
-
if (direction === 0)
|
|
488
|
-
return load;
|
|
489
|
-
return source.loads[channel < half ? channel + half : channel - half];
|
|
490
|
-
}));
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Ordered multiplication: every ordered pair of weighted signatures
|
|
494
|
-
* contributes the product of its loads at the signed destination the
|
|
495
|
-
* recurrence gives, and only identical full signatures aggregate. Operand
|
|
496
|
-
* order and the caller's parenthesization are the caller's; nothing is
|
|
497
|
-
* regrouped. Counts multiply (C(X⊗Y) = C(X)·C(Y)).
|
|
498
|
-
*/
|
|
499
|
-
export function multiply(left, right) {
|
|
500
|
-
const [x, y, rank] = align(left, right);
|
|
501
|
-
const loads = Array(arity(rank)).fill(0n);
|
|
502
|
-
for (let i = 0; i < x.loads.length; i++) {
|
|
503
|
-
if (x.loads[i] === 0n)
|
|
504
|
-
continue;
|
|
505
|
-
for (let j = 0; j < y.loads.length; j++) {
|
|
506
|
-
if (y.loads[j] === 0n)
|
|
507
|
-
continue;
|
|
508
|
-
loads[multiplyChannels(rank, i, j)] += x.loads[i] * y.loads[j];
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
return mintCD(x.count * y.count, loads);
|
|
512
|
-
}
|
|
513
|
-
/**
|
|
514
|
-
* The Denum coordinates (occurrence/figure 007.32j, 007.33): an ordered list of
|
|
515
|
-
* integers, one per direction `b`, read from the preserved state without
|
|
516
|
-
* changing it. The one projection behind both HC spellings:
|
|
517
|
-
*
|
|
518
|
-
* - signed (`d.coordinates ()`): `S_b = W₍₊,b₎ − W₍₋,b₎`, the algebraic value;
|
|
519
|
-
* - unsigned (`d.coordinates <>`): `U_b = W₍₊,b₎ + W₍₋,b₎`, the direction load
|
|
520
|
-
* with the ± signature forgotten (so `U ≠ |S|`).
|
|
521
|
-
*
|
|
522
|
-
* Width is 1 at D0 and `2ⁿ⁻¹` at Dn, n ≥ 1, in the direction order of
|
|
523
|
-
* {@link signatureOf}. D0 has one unsigned channel `e_ε`, so both modes answer
|
|
524
|
-
* `[W_ε]`. Count is neither used nor returned. Given the rank, the pair (S, U)
|
|
525
|
-
* recovers the loads, `W₊ = (U+S)/2` and `W₋ = (U−S)/2`; Count, history and
|
|
526
|
-
* the Denum itself are not recovered. Always a fresh array.
|
|
527
|
-
*/
|
|
528
|
-
export function coordinates(source, unsigned = false) {
|
|
529
|
-
if (cdRank(source) === 0)
|
|
530
|
-
return [source.loads[0]];
|
|
531
|
-
const half = source.loads.length / 2;
|
|
532
|
-
return Array.from({ length: half }, (_, b) => unsigned
|
|
533
|
-
? source.loads[b] + source.loads[half + b]
|
|
534
|
-
: source.loads[b] - source.loads[half + b]);
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* The signed coordinates of v0.45.0.
|
|
538
|
-
*
|
|
539
|
-
* @deprecated Legacy compatibility alias of `coordinates(source)`, the signed
|
|
540
|
-
* mode; `d.observe ()` in HC is its deprecated spelling. Use {@link coordinates}.
|
|
541
|
-
*/
|
|
542
|
-
export function observe(source) {
|
|
543
|
-
return coordinates(source);
|
|
544
|
-
}
|
|
545
|
-
/**
|
|
546
|
-
* The declared residual of a Denum, the signed total `Σ W₊ − Σ W₋`. It is the
|
|
547
|
-
* sum of the observed coordinates, so it is the existing residual `L − R` at
|
|
548
|
-
* D1 and 0 at D0, which has no negative channel. A single integer, it forgets
|
|
549
|
-
* the directions; it is an observer of the state, not the state.
|
|
550
|
-
*/
|
|
551
|
-
export function residual(source) {
|
|
552
|
-
return cdRank(source) === 0
|
|
553
|
-
? 0n
|
|
554
|
-
: coordinates(source).reduce((a, b) => a + b, 0n);
|
|
555
|
-
}
|
|
556
|
-
/** True when every observed coordinate is zero. */
|
|
557
|
-
export function observesZero(source) {
|
|
558
|
-
return coordinates(source).every((coordinate) => coordinate === 0n);
|
|
559
|
-
}
|
|
560
|
-
/** True when no channel carries a load. */
|
|
561
|
-
export function isEmpty(source) {
|
|
562
|
-
return source.loads.every((load) => load === 0n);
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Host to Denum (the construction behind `X.#n`, distinct from promotion). It
|
|
566
|
-
* answers a generic {@link Denum}: the ranked hash never answers a CD view.
|
|
567
|
-
*
|
|
568
|
-
* A host answers only its Count `C`, Size `s` and residual `r`. Rank 0 keeps
|
|
569
|
-
* the one load `s`. Rank `n ≥ 1` places `(s+r)/2` on `+e_0…0` and `(s−r)/2` on
|
|
570
|
-
* `−e_0…0`, the two channels the existing fourth hash reads as left and right,
|
|
571
|
-
* and every other channel starts at zero. A host's applications carry no
|
|
572
|
-
* higher signature, so nothing is routed to them; `C mod arity` is the
|
|
573
|
-
* application phase, not a signature rule. Count zero is the caller's nil.
|
|
574
|
-
*
|
|
575
|
-
* @throws DenumError `$!.denum-coherence` when the loads are not whole counts.
|
|
576
|
-
*/
|
|
577
|
-
export function fromHost(rank, count, size, residual) {
|
|
578
|
-
checkRank(rank);
|
|
579
|
-
if (rank === 0)
|
|
580
|
-
return denum(count, [size]);
|
|
581
|
-
const sum = size + residual;
|
|
582
|
-
const difference = size - residual;
|
|
583
|
-
if (sum < 0n || difference < 0n || sum % 2n !== 0n || difference % 2n !== 0n) {
|
|
584
|
-
throw new DenumError("$!.denum-coherence");
|
|
585
|
-
}
|
|
586
|
-
const loads = Array(arity(rank)).fill(0n);
|
|
587
|
-
loads[0] = sum / 2n;
|
|
588
|
-
loads[arity(rank) / 2] = difference / 2n;
|
|
589
|
-
return denum(count, loads);
|
|
590
|
-
}
|
|
591
|
-
/**
|
|
592
|
-
* Text. A view reads `CD[2](C=7, W=[3, 2, 0, 1])`; a generic Denum reads
|
|
593
|
-
* `Denum(Arity[4], C=7, W=[3, 2, 0, 1])`, never claiming a CD rung. Loads are
|
|
594
|
-
* listed in channel order.
|
|
595
|
-
*/
|
|
596
|
-
export function denumText(source) {
|
|
597
|
-
const body = `C=${source.count}, W=[${source.loads.join(", ")}]`;
|
|
598
|
-
return CD.is(source)
|
|
599
|
-
? `${schemaOf(source)}(${body})`
|
|
600
|
-
: `Denum(${schemaOf(source)}, ${body})`;
|
|
601
|
-
}
|