hclang 0.44.0 → 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.
- package/esm/cli/hc.js +4 -1
- package/esm/lib/bundle/hc-sources.js +864 -0
- package/esm/lib/execute/denum-prologue.js +88 -0
- package/esm/lib/execute/hc-eval.js +2 -0
- package/esm/lib/frames/access-footprint.js +665 -0
- package/esm/lib/frames/bound-method.js +38 -0
- package/esm/lib/frames/denum-hash.js +166 -0
- package/esm/lib/frames/denum.js +357 -0
- package/esm/lib/frames/dispatch-host.js +118 -0
- package/esm/lib/frames/effect-registry.js +21 -2
- package/esm/lib/frames/event-dispatch.js +192 -0
- package/esm/lib/frames/frame-array.js +42 -11
- package/esm/lib/frames/frame-expr.js +40 -9
- package/esm/lib/frames/frame-lazy.js +37 -5
- package/esm/lib/frames/frame-list.js +10 -3
- package/esm/lib/frames/frame-name.js +8 -0
- package/esm/lib/frames/frame-symbol.js +1 -1
- package/esm/lib/frames/frame.js +75 -10
- package/esm/lib/frames/reference-tracking.js +275 -0
- package/esm/lib/frames.js +8 -0
- package/esm/lib/ops/denum.js +120 -0
- package/esm/lib/ops.js +3 -0
- package/esm/lib/version.js +1 -1
- package/package.json +1 -1
|
@@ -94,6 +94,30 @@ export class BoundMethod extends Frame {
|
|
|
94
94
|
isMutating() {
|
|
95
95
|
return this.effect.mutating;
|
|
96
96
|
}
|
|
97
|
+
/** The frame this method was bound to, before any functional-update copy. */
|
|
98
|
+
receiverFrame() {
|
|
99
|
+
return this.receiverTarget;
|
|
100
|
+
}
|
|
101
|
+
/** Whether the handle this method was reached through claims mutability. */
|
|
102
|
+
hasMutableReceiver() {
|
|
103
|
+
return this.mutable;
|
|
104
|
+
}
|
|
105
|
+
/** The shared closure this binding runs. */
|
|
106
|
+
methodBody() {
|
|
107
|
+
return this.method;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The scope a call would run the body in, with the receiver capability a
|
|
111
|
+
* call would select, without running the body (`access-footprint.ts`). The
|
|
112
|
+
* selection is the real one: a functional update's scope reads its instance
|
|
113
|
+
* copy as receiver, so a walk over it sees writes land on the copy.
|
|
114
|
+
*/
|
|
115
|
+
scopeForCall(argument) {
|
|
116
|
+
const state = this.receiverStateForCall();
|
|
117
|
+
if (state instanceof Frame)
|
|
118
|
+
return state;
|
|
119
|
+
return this.method.scopeForCall(argument, Frame.nil, state);
|
|
120
|
+
}
|
|
97
121
|
call(argument, _parameter = Frame.nil) {
|
|
98
122
|
const state = this.receiverStateForCall();
|
|
99
123
|
if (state instanceof Frame)
|
|
@@ -103,6 +127,20 @@ export class BoundMethod extends Frame {
|
|
|
103
127
|
// the lookup order, and a shared body's captured scope is whichever instance
|
|
104
128
|
// was built last, so a method would read another instance's fields.
|
|
105
129
|
const result = this.method.call(argument, Frame.nil, state);
|
|
130
|
+
return this.answer(result, state);
|
|
131
|
+
}
|
|
132
|
+
/** `call`, one statement at a time (`FrameLazy.steps`). */
|
|
133
|
+
*steps(argument, _parameter = Frame.nil) {
|
|
134
|
+
const state = this.receiverStateForCall();
|
|
135
|
+
if (state instanceof Frame) {
|
|
136
|
+
yield state;
|
|
137
|
+
return state;
|
|
138
|
+
}
|
|
139
|
+
const result = yield* this.method.steps(argument, Frame.nil, state);
|
|
140
|
+
return this.answer(result, state);
|
|
141
|
+
}
|
|
142
|
+
/** What a call answers once the body has: the receiver, or the value. */
|
|
143
|
+
answer(result, state) {
|
|
106
144
|
// A mutating method answers its receiver, unless it answered an error. A
|
|
107
145
|
// diagnostic failure such as `$!.name-missing` carries the same flag.
|
|
108
146
|
// Preserve referent checks for programmatic reference wrappers (011.20).
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Frame } from "./frame.js";
|
|
2
|
+
import { FrameArray } from "./frame-array.js";
|
|
3
|
+
import { FrameCount } from "./frame-count.js";
|
|
4
|
+
import { FrameLazy } from "./frame-lazy.js";
|
|
5
|
+
import { FrameString } from "./frame-string.js";
|
|
6
|
+
import { BoundMethod } from "./bound-method.js";
|
|
7
|
+
import { dispatch, } from "./event-dispatch.js";
|
|
8
|
+
/**
|
|
9
|
+
* The dispatcher as a host-namespace value, so an HC program can hand its
|
|
10
|
+
* own closures to `dispatch` and observe what each policy does to them
|
|
11
|
+
* (occurrence/figure 015.24j: the VACT witness has to reach the dispatcher
|
|
12
|
+
* under test from the testdoc that constructs its state).
|
|
13
|
+
*
|
|
14
|
+
* A harness that installs `dispatchNamespace()` under `DISPATCH_KEY` in its
|
|
15
|
+
* host namespace gives HC source four callables:
|
|
16
|
+
*
|
|
17
|
+
* $$.dispatch.sequential [e₁, e₂, …]
|
|
18
|
+
* $$.dispatch.certified-sequential [e₁, e₂, …]
|
|
19
|
+
* $$.dispatch.interleave [e₁, e₂, …]
|
|
20
|
+
* $$.dispatch.footprint [e₁, e₂, …]
|
|
21
|
+
*
|
|
22
|
+
* Each `eᵢ` is a bound closure or bound method, called with nil, or a pair
|
|
23
|
+
* `[callable, argument]`. The answer is one aggregate: its elements are the
|
|
24
|
+
* events' results in program order, and its properties the schedule the
|
|
25
|
+
* policy chose, `trace` as `“i:n …”` (the nth statement of event i, in the
|
|
26
|
+
* order they ran), `waited` as each event's indexes waited for, and
|
|
27
|
+
* `escapes` as the count of shared frames each event wrote outside its
|
|
28
|
+
* potential footprint, which the guard keeps at zero under `footprint`. The
|
|
29
|
+
* HC state the events touch is the program's own, so the program reads the
|
|
30
|
+
* final state itself, with its own observer, after the dispatch answers.
|
|
31
|
+
*
|
|
32
|
+
* This is a bridge, not a language feature: the policies are host values
|
|
33
|
+
* reached through `$$`, which a bare name never searches, and nothing here
|
|
34
|
+
* changes how HC evaluates, and it grants no authority: every event is a
|
|
35
|
+
* closure the program already holds, run with the authority it already has.
|
|
36
|
+
* It is installed in every program the CLI runs (`cli/hc.ts`), in every
|
|
37
|
+
* `HCLang` session and so the playground (`lib/execute/hc-lang.ts`), and by
|
|
38
|
+
* the companion Deno test; a host that does not install it has no
|
|
39
|
+
* `dispatch`, and `$$.dispatch` answers `$!.name-missing` there.
|
|
40
|
+
*
|
|
41
|
+
* @module
|
|
42
|
+
*/
|
|
43
|
+
/** The key the dispatch namespace is bound under in a host namespace. */
|
|
44
|
+
export const DISPATCH_KEY = "dispatch";
|
|
45
|
+
const POLICIES = [
|
|
46
|
+
"sequential",
|
|
47
|
+
"certified-sequential",
|
|
48
|
+
"interleave",
|
|
49
|
+
"footprint",
|
|
50
|
+
];
|
|
51
|
+
/** One policy as a callable host value. */
|
|
52
|
+
class DispatchPolicyFrame extends Frame {
|
|
53
|
+
constructor(policy) {
|
|
54
|
+
super();
|
|
55
|
+
Object.defineProperty(this, "policy", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: policy
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
toString() {
|
|
63
|
+
return `$$.${DISPATCH_KEY}.${this.policy}`;
|
|
64
|
+
}
|
|
65
|
+
/** A host value is itself; evaluating it performs no dispatch. */
|
|
66
|
+
in() {
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
call(argument) {
|
|
70
|
+
const events = eventsOf(argument);
|
|
71
|
+
if (!Array.isArray(events))
|
|
72
|
+
return events;
|
|
73
|
+
return reportFrame(dispatch(events, this.policy));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** The events an HC argument denotes, or the error that refuses it. */
|
|
77
|
+
function eventsOf(argument) {
|
|
78
|
+
const items = argument.referent();
|
|
79
|
+
if (!(items instanceof FrameArray)) {
|
|
80
|
+
return Frame.error(`$!.dispatch-events ${items.className()}`);
|
|
81
|
+
}
|
|
82
|
+
const events = [];
|
|
83
|
+
for (const [index, item] of items.asArray().entries()) {
|
|
84
|
+
const value = item.referent();
|
|
85
|
+
const label = String(index);
|
|
86
|
+
if (isCallable(value)) {
|
|
87
|
+
events.push({ label, target: value });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (value instanceof FrameArray) {
|
|
91
|
+
const [target, argument] = value.asArray();
|
|
92
|
+
const callee = target?.referent();
|
|
93
|
+
if (callee && isCallable(callee)) {
|
|
94
|
+
events.push({ label, target: callee, argument: argument ?? Frame.nil });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return Frame.error(`$!.dispatch-event ${label} ${value.className()}`);
|
|
99
|
+
}
|
|
100
|
+
return events;
|
|
101
|
+
}
|
|
102
|
+
const isCallable = (frame) => frame instanceof FrameLazy || frame instanceof BoundMethod;
|
|
103
|
+
/** The report as one aggregate: results as elements, schedule as properties. */
|
|
104
|
+
function reportFrame(report) {
|
|
105
|
+
const result = new FrameArray(report.outcomes.map((o) => o.result));
|
|
106
|
+
result.set("trace", new FrameString(report.trace.join(" ")));
|
|
107
|
+
result.set("waited", new FrameArray(report.outcomes.map((o) => new FrameArray(o.waitedFor.map((label) => FrameCount.for(label))))));
|
|
108
|
+
result.set("escapes", new FrameArray(report.outcomes.map((o) => FrameCount.for(String(o.escapes.length)))));
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
/** A fresh namespace holding the three policies, for a host to install. */
|
|
112
|
+
export function dispatchNamespace() {
|
|
113
|
+
const namespace = new Frame();
|
|
114
|
+
for (const policy of POLICIES) {
|
|
115
|
+
namespace.set(policy, new DispatchPolicyFrame(policy));
|
|
116
|
+
}
|
|
117
|
+
return namespace;
|
|
118
|
+
}
|