hclang 0.45.0 → 0.47.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 +640 -182
- package/esm/lib/execute/denum-prologue.js +62 -33
- package/esm/lib/frames/denum-hash.js +65 -51
- package/esm/lib/frames/denum.js +326 -82
- package/esm/lib/frames.js +1 -1
- package/esm/lib/ops/denum.js +151 -33
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
package/esm/lib/frames/denum.js
CHANGED
|
@@ -1,33 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Denum
|
|
3
|
-
*
|
|
2
|
+
* Denum and its derived CD view (occurrence/figure 007.31, 007.32, 007.32b,
|
|
3
|
+
* 007.35, 007.35b, 007.36).
|
|
4
4
|
*
|
|
5
5
|
* This is the typed reference implementation. It depends on no Frame, so the
|
|
6
|
-
* HC
|
|
7
|
-
* A Denum is a constructed state, not a conventional number:
|
|
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
8
|
*
|
|
9
|
-
* - a **rank** `n` in 0..MAX_RANK, the schema `CD[n]`;
|
|
10
9
|
* - a retained application **Count** `C ∈ ℕ₀`;
|
|
11
|
-
* - a
|
|
10
|
+
* - a nonempty ordered list of **loads**, each `W ∈ ℕ₀`, one per channel.
|
|
12
11
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
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.
|
|
19
37
|
*
|
|
20
38
|
* The product of two signed signatures is
|
|
21
39
|
* `(σ e_a)(ρ e_b) = σρ·τ(a, b) e_(a⊕b)`, where the twist `τ` is generated by
|
|
22
40
|
* the Cayley–Dickson recurrence `(a,b)(c,d) = (ac − d̄b, da + bc̄)` in
|
|
23
41
|
* {@link cdTwist}. No multiplication table is written by hand.
|
|
24
42
|
*
|
|
25
|
-
* Freshness:
|
|
26
|
-
*
|
|
27
|
-
* {@link
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
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`.
|
|
31
49
|
*
|
|
32
50
|
* Count is carried, never derived from loads: `C(X+Y) = C(X)+C(Y)`,
|
|
33
51
|
* `C(X⊗Y) = C(X)·C(Y)`, `C(Opp X) = C(X)` and promotion keeps it. These are
|
|
@@ -35,10 +53,79 @@
|
|
|
35
53
|
* arithmetic only. They are not the application phase `C mod arity`, and
|
|
36
54
|
* nothing here changes Demi.
|
|
37
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
|
+
*
|
|
38
64
|
* @module
|
|
39
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;
|
|
40
71
|
/** The highest rung implemented: D5, the sedenions, 32 channels. */
|
|
41
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
|
+
}
|
|
42
129
|
/** A failure of the Denum contract, named like an HC error. */
|
|
43
130
|
export class DenumError extends Error {
|
|
44
131
|
constructor(kind, detail = "") {
|
|
@@ -70,6 +157,76 @@ function checkRank(rank) {
|
|
|
70
157
|
throw new DenumError("$!.denum-rank", String(rank));
|
|
71
158
|
}
|
|
72
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
|
+
}
|
|
73
230
|
/**
|
|
74
231
|
* The Cayley–Dickson twist of two basis directions of `m` bits: the sign
|
|
75
232
|
* `τ(x, y) ∈ {+1, −1}` with `e_x · e_y = τ(x, y) e_(x⊕y)` in the real algebra
|
|
@@ -179,19 +336,74 @@ function nonnegative(value, what) {
|
|
|
179
336
|
throw new DenumError("$!!.invalid-denum", what);
|
|
180
337
|
}
|
|
181
338
|
}
|
|
182
|
-
/**
|
|
183
|
-
|
|
184
|
-
|
|
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) {
|
|
185
344
|
nonnegative(count, "count");
|
|
186
|
-
if (loads.length
|
|
187
|
-
throw new DenumError("$!!.invalid-denum",
|
|
345
|
+
if (loads.length < 1) {
|
|
346
|
+
throw new DenumError("$!!.invalid-denum", "loads 0");
|
|
188
347
|
}
|
|
189
348
|
loads.forEach((load) => nonnegative(load, "load"));
|
|
190
|
-
return {
|
|
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);
|
|
191
365
|
}
|
|
192
|
-
/** The zero-load
|
|
366
|
+
/** The zero-load view of a rank with a Count: present, unlike a missing host. */
|
|
193
367
|
export function zero(rank, count = 0n) {
|
|
194
|
-
return denum(
|
|
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;
|
|
195
407
|
}
|
|
196
408
|
/**
|
|
197
409
|
* Canonical old-summand embedding `Dn → D(n+1)`: `e_ε ↦ +e_ε` at D0 → D1, and
|
|
@@ -201,30 +413,42 @@ export function zero(rank, count = 0n) {
|
|
|
201
413
|
*/
|
|
202
414
|
export function promote(source, rank) {
|
|
203
415
|
checkRank(rank);
|
|
204
|
-
|
|
205
|
-
|
|
416
|
+
const from = cdRank(source);
|
|
417
|
+
if (rank < from) {
|
|
418
|
+
throw new DenumError("$!.denum-demotion", `${from} to ${rank}`);
|
|
206
419
|
}
|
|
207
420
|
// Always fresh: a same-rank promotion is a copy, never the argument itself.
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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}`);
|
|
222
441
|
}
|
|
223
|
-
|
|
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) };
|
|
224
447
|
}
|
|
225
448
|
/** Both operands at the larger rank, by promotion. */
|
|
226
449
|
function align(left, right) {
|
|
227
|
-
|
|
450
|
+
requireCDs(left, right);
|
|
451
|
+
const rank = Math.max(cdRank(left), cdRank(right));
|
|
228
452
|
return [promote(left, rank), promote(right, rank), rank];
|
|
229
453
|
}
|
|
230
454
|
/**
|
|
@@ -232,28 +456,21 @@ function align(left, right) {
|
|
|
232
456
|
* Counts add (C(X+Y) = C(X)+C(Y)).
|
|
233
457
|
*/
|
|
234
458
|
export function add(left, right) {
|
|
235
|
-
const [x, y
|
|
236
|
-
return
|
|
237
|
-
rank,
|
|
238
|
-
count: x.count + y.count,
|
|
239
|
-
loads: x.loads.map((load, channel) => load + y.loads[channel]),
|
|
240
|
-
};
|
|
459
|
+
const [x, y] = align(left, right);
|
|
460
|
+
return mintCD(x.count + y.count, x.loads.map((load, channel) => load + y.loads[channel]));
|
|
241
461
|
}
|
|
242
462
|
/**
|
|
243
463
|
* Opposite: swaps the loads of `+e_b` and `−e_b`. D0 has no negative channel,
|
|
244
464
|
* so it is promoted to D1 first. The Count is kept.
|
|
245
465
|
*/
|
|
246
466
|
export function opposite(source) {
|
|
247
|
-
const x = promote(source, Math.max(source
|
|
467
|
+
const x = promote(source, Math.max(cdRank(source), 1));
|
|
248
468
|
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
|
-
};
|
|
469
|
+
return mintCD(x.count, x.loads.map((_, channel) => x.loads[channel < half ? channel + half : channel - half]));
|
|
254
470
|
}
|
|
255
471
|
/** Subtraction is addition of the opposite; it is observed, never erased. */
|
|
256
472
|
export function subtract(left, right) {
|
|
473
|
+
requireCDs(left, right);
|
|
257
474
|
return add(left, opposite(right));
|
|
258
475
|
}
|
|
259
476
|
/**
|
|
@@ -262,20 +479,15 @@ export function subtract(left, right) {
|
|
|
262
479
|
* identity. Counts and loads are kept, only moved between channels.
|
|
263
480
|
*/
|
|
264
481
|
export function conjugate(source) {
|
|
265
|
-
if (source
|
|
266
|
-
return
|
|
267
|
-
}
|
|
482
|
+
if (cdRank(source) === 0)
|
|
483
|
+
return mintCD(source.count, source.loads);
|
|
268
484
|
const half = source.loads.length / 2;
|
|
269
|
-
return {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
return load;
|
|
276
|
-
return source.loads[channel < half ? channel + half : channel - half];
|
|
277
|
-
}),
|
|
278
|
-
};
|
|
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
|
+
}));
|
|
279
491
|
}
|
|
280
492
|
/**
|
|
281
493
|
* Ordered multiplication: every ordered pair of weighted signatures
|
|
@@ -296,17 +508,39 @@ export function multiply(left, right) {
|
|
|
296
508
|
loads[multiplyChannels(rank, i, j)] += x.loads[i] * y.loads[j];
|
|
297
509
|
}
|
|
298
510
|
}
|
|
299
|
-
return
|
|
511
|
+
return mintCD(x.count * y.count, loads);
|
|
300
512
|
}
|
|
301
513
|
/**
|
|
302
|
-
* The
|
|
303
|
-
*
|
|
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.
|
|
304
527
|
*/
|
|
305
|
-
export function
|
|
306
|
-
if (source
|
|
528
|
+
export function coordinates(source, unsigned = false) {
|
|
529
|
+
if (cdRank(source) === 0)
|
|
307
530
|
return [source.loads[0]];
|
|
308
531
|
const half = source.loads.length / 2;
|
|
309
|
-
return Array.from({ length: half }, (_, b) =>
|
|
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);
|
|
310
544
|
}
|
|
311
545
|
/**
|
|
312
546
|
* The declared residual of a Denum, the signed total `Σ W₊ − Σ W₋`. It is the
|
|
@@ -315,18 +549,21 @@ export function observe(source) {
|
|
|
315
549
|
* the directions; it is an observer of the state, not the state.
|
|
316
550
|
*/
|
|
317
551
|
export function residual(source) {
|
|
318
|
-
return source
|
|
552
|
+
return cdRank(source) === 0
|
|
553
|
+
? 0n
|
|
554
|
+
: coordinates(source).reduce((a, b) => a + b, 0n);
|
|
319
555
|
}
|
|
320
556
|
/** True when every observed coordinate is zero. */
|
|
321
557
|
export function observesZero(source) {
|
|
322
|
-
return
|
|
558
|
+
return coordinates(source).every((coordinate) => coordinate === 0n);
|
|
323
559
|
}
|
|
324
560
|
/** True when no channel carries a load. */
|
|
325
561
|
export function isEmpty(source) {
|
|
326
562
|
return source.loads.every((load) => load === 0n);
|
|
327
563
|
}
|
|
328
564
|
/**
|
|
329
|
-
* Host to Denum (the construction behind `X.#n`, distinct from promotion).
|
|
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.
|
|
330
567
|
*
|
|
331
568
|
* A host answers only its Count `C`, Size `s` and residual `r`. Rank 0 keeps
|
|
332
569
|
* the one load `s`. Rank `n ≥ 1` places `(s+r)/2` on `+e_0…0` and `(s−r)/2` on
|
|
@@ -340,7 +577,7 @@ export function isEmpty(source) {
|
|
|
340
577
|
export function fromHost(rank, count, size, residual) {
|
|
341
578
|
checkRank(rank);
|
|
342
579
|
if (rank === 0)
|
|
343
|
-
return denum(
|
|
580
|
+
return denum(count, [size]);
|
|
344
581
|
const sum = size + residual;
|
|
345
582
|
const difference = size - residual;
|
|
346
583
|
if (sum < 0n || difference < 0n || sum % 2n !== 0n || difference % 2n !== 0n) {
|
|
@@ -349,9 +586,16 @@ export function fromHost(rank, count, size, residual) {
|
|
|
349
586
|
const loads = Array(arity(rank)).fill(0n);
|
|
350
587
|
loads[0] = sum / 2n;
|
|
351
588
|
loads[arity(rank) / 2] = difference / 2n;
|
|
352
|
-
return denum(
|
|
589
|
+
return denum(count, loads);
|
|
353
590
|
}
|
|
354
|
-
/**
|
|
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
|
+
*/
|
|
355
596
|
export function denumText(source) {
|
|
356
|
-
|
|
597
|
+
const body = `C=${source.count}, W=[${source.loads.join(", ")}]`;
|
|
598
|
+
return CD.is(source)
|
|
599
|
+
? `${schemaOf(source)}(${body})`
|
|
600
|
+
: `Denum(${schemaOf(source)}, ${body})`;
|
|
357
601
|
}
|
package/esm/lib/frames.js
CHANGED
|
@@ -28,7 +28,7 @@ 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 {
|
|
31
|
+
export { installDenumConstructors, observeDenum, } from "./frames/denum-hash.js";
|
|
32
32
|
export * as denum from "./frames/denum.js";
|
|
33
33
|
export { FrameDecimal } from "./frames/frame-decimal.js";
|
|
34
34
|
export { FrameSequence } from "./frames/frame-sequence.js";
|