hclang 0.46.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.
@@ -1,11 +1,15 @@
1
1
  /**
2
2
  * The Denum class, loaded from HC for the ranked hash.
3
3
  *
4
- * `x.#n` answers a Denum, and a Denum is an HC class, `Denum` in
5
- * `hc/denum.hc` (occurrence/figure 007.32). The frames package derives the
6
- * state natively but cannot run HC, so this module installs the constructor it
7
- * calls (`installDenumConstructor`), exactly as `demi-prologue.ts` does for
8
- * `.####`.
4
+ * `x.#n` answers a Denum, and the only public constructor is the HC class
5
+ * `Denum` in `hc/denum.hc` (occurrence/figure 007.32, 007.36). The CD algebra
6
+ * is a view derived by `d.cd ()`, not a second constructor. The frames package
7
+ * derives the state natively but cannot run HC, so this module installs what
8
+ * it calls (`installDenumConstructors`), exactly as `demi-prologue.ts` does for
9
+ * `.####`. The `Denum` constructor builds every answer; the internal view
10
+ * derivation `view-of` is installed only so a view source can be recognized
11
+ * and refused. It is not a constructor of retained state and is never
12
+ * exported.
9
13
  *
10
14
  * The class is read from the compiled-in bundle, not through a resource root,
11
15
  * so `.#n` works in a bare `evaluate`, which has no root and imports nothing.
@@ -19,70 +23,95 @@
19
23
  *
20
24
  * @module
21
25
  */
22
- import { EvaluationScope, Frame, FrameArray, FrameCount, FrameExpr, installDenumConstructor, } from "../frames.js";
26
+ import { EvaluationScope, Frame, FrameArray, FrameCount, FrameExpr, installDenumConstructors, } from "../frames.js";
23
27
  import { HC_SOURCES } from "../bundle/hc-sources.js";
24
28
  import { evaluateSourceText } from "./resource-import.js";
25
29
  /** The bundled file that defines the class. */
26
30
  export const DENUM_SOURCE = "hc/denum.hc";
27
- /** The name the file declares the constructor under. */
31
+ /** The name the file declares its one public constructor under. */
28
32
  export const DENUM_CLASS = "Denum";
33
+ /** The internal view derivation: `view-of d` is `d.cd ()`. Not a constructor. */
34
+ export const VIEW_FACTORY = "view-of";
29
35
  let loaded;
30
36
  let loading = false;
37
+ const failed = (frame) => frame.isFailedResult() || frame.observationError() !== undefined;
31
38
  /**
32
- * Evaluates class source in a namespace of its own, answering its `Denum` or
33
- * the failure. Uncached: `denumClass` keeps the one result the process uses.
39
+ * Evaluates class source in a namespace of its own, answering its `Denum`
40
+ * constructor and `view-of` derivation or the failure. Uncached: `denumClasses` keeps the one
41
+ * result the process uses.
34
42
  *
35
43
  * @param source The source text, or undefined when the bundle lacks it.
36
- * @returns The constructor, or an error.
44
+ * @returns The `Denum` constructor and the view derivation, or an error.
37
45
  */
38
- export function loadDenumClass(source) {
46
+ export function loadDenumClasses(source) {
39
47
  if (source === undefined) {
40
48
  return Frame.error(`$!.denum-prologue-missing ${DENUM_SOURCE}`);
41
49
  }
42
50
  const namespace = new FrameArray([]);
43
51
  const result = evaluateSourceText(source, EvaluationScope.root(namespace));
44
- if (result.isFailedResult() || result.observationError())
52
+ if (failed(result))
45
53
  return result;
46
- const constructor = namespace.getOwn(DENUM_CLASS);
47
- return constructor.is.missing
48
- ? Frame.error(`$!.denum-prologue-missing ${DENUM_CLASS}`)
49
- : constructor;
54
+ const denum = namespace.getOwn(DENUM_CLASS);
55
+ if (denum.is.missing) {
56
+ return Frame.error(`$!.denum-prologue-missing ${DENUM_CLASS}`);
57
+ }
58
+ const view = namespace.getOwn(VIEW_FACTORY);
59
+ if (view.is.missing) {
60
+ return Frame.error(`$!.denum-prologue-missing ${VIEW_FACTORY}`);
61
+ }
62
+ return { denum, view };
50
63
  }
51
64
  /**
52
- * The `Denum` constructor closure, evaluated from the bundle on first use.
65
+ * The `Denum` constructor and `view-of` closures, evaluated from the bundle on
66
+ * first use.
53
67
  *
54
- * @returns The constructor, or the error that loading it answered.
68
+ * @returns The closures, or the error that loading them answered.
55
69
  */
56
- export function denumClass() {
70
+ export function denumClasses() {
57
71
  if (loaded)
58
72
  return loaded;
59
73
  if (loading)
60
74
  return Frame.error("$!.denum-prologue-reentrant");
61
75
  loading = true;
62
76
  try {
63
- loaded = loadDenumClass(HC_SOURCES.get(DENUM_SOURCE));
77
+ loaded = loadDenumClasses(HC_SOURCES.get(DENUM_SOURCE));
64
78
  }
65
79
  finally {
66
80
  loading = false;
67
81
  }
68
82
  return loaded;
69
83
  }
84
+ const stateArgument = (state) => new FrameArray([
85
+ new FrameCount(state.count),
86
+ new FrameArray(state.loads.map((load) => new FrameCount(load))),
87
+ ]);
70
88
  /**
71
- * Constructs a Denum with the HC class: `Denum [rank, count, loads]`.
89
+ * Constructs a Denum with the HC class: `Denum [count, loads]`.
72
90
  *
73
- * @param state The rank, Count and loads, already admissible.
91
+ * @param state The Count and loads, already admissible.
74
92
  * @returns The new instance, or the class's failure.
75
93
  */
76
94
  export function constructDenum(state) {
77
- const constructor = denumClass();
78
- if (constructor.isFailedResult() || constructor.observationError()) {
79
- return constructor;
80
- }
81
- const argument = new FrameArray([
82
- new FrameCount(BigInt(state.rank)),
83
- new FrameCount(state.count),
84
- new FrameArray(state.loads.map((load) => new FrameCount(load))),
85
- ]);
86
- return FrameExpr.answeredValue(constructor.call(argument)).referent();
95
+ const classes = denumClasses();
96
+ if (classes instanceof Frame)
97
+ return classes;
98
+ return FrameExpr.answeredValue(classes.denum.call(stateArgument(state)))
99
+ .referent();
100
+ }
101
+ /**
102
+ * Derives the CD view of the Denum the state builds, as `d.cd ()` does. Only
103
+ * the ranked hash's recognition of a view source uses it.
104
+ *
105
+ * @param state The Count and loads, already admissible.
106
+ * @returns The view instance, or the class's failure.
107
+ */
108
+ function deriveView(state) {
109
+ const classes = denumClasses();
110
+ if (classes instanceof Frame)
111
+ return classes;
112
+ const source = constructDenum(state);
113
+ if (source.isFailedResult() || source.observationError())
114
+ return source;
115
+ return FrameExpr.answeredValue(classes.view.call(source)).referent();
87
116
  }
88
- installDenumConstructor(constructDenum);
117
+ installDenumConstructors({ denum: constructDenum, view: deriveView });
@@ -1,17 +1,19 @@
1
1
  import { Frame } from "./frame.js";
2
2
  import { FrameCount } from "./frame-count.js";
3
3
  import { FrameInt } from "./frame-int.js";
4
- import { DenumError, fromHost, MAX_RANK, promote, } from "./denum.js";
5
- let construct;
4
+ import { DenumError, fromHost, MAX_RANK, promoteDenum, } from "./denum.js";
5
+ let constructors;
6
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.
7
+ * Installs what `.#n` answers with and recognizes. The classes are HC, so the
8
+ * frames package, which cannot run HC, takes them from the evaluator
9
+ * (`lib/execute/denum-prologue.ts`), as `.####` takes its Demi. They are
10
+ * installed as a pair: the Denum constructor builds every answer, and the view
11
+ * derivation is used only to recognize a view source, which is refused.
10
12
  *
11
- * @param constructor The constructor, called once per present observation.
13
+ * @param installed Both, called once per present observation.
12
14
  */
13
- export function installDenumConstructor(constructor) {
14
- construct = constructor;
15
+ export function installDenumConstructors(installed) {
16
+ constructors = installed;
15
17
  }
16
18
  /** A Frame read as an exact nonnegative Count, or undefined. */
17
19
  function exactCount(frame) {
@@ -22,48 +24,55 @@ function exactCount(frame) {
22
24
  : undefined;
23
25
  }
24
26
  /**
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
+ * The stored state of a Denum instance: its own `count` and `loads` fields
28
+ * (there is no stored rank). Only the instance's own fields are read (`get_here`), never a
27
29
  * declared parent or a method, and the loads are read as stored, so nothing
28
30
  * runs. Undefined when any field is absent or not an exact Count of the right
29
31
  * shape.
30
32
  */
31
33
  function storedState(host) {
32
- const rank = exactCount(host.get_here("rank"));
33
34
  const count = exactCount(host.get_here("count"));
34
35
  const list = host.get_here("loads").referent();
35
- if (rank === undefined || count === undefined || rank > BigInt(MAX_RANK)) {
36
+ if (count === undefined)
36
37
  return undefined;
37
- }
38
38
  if (!list.ownsIdentity())
39
39
  return undefined;
40
40
  const loads = list.elements().map(exactCount);
41
- if (loads.some((load) => load === undefined))
41
+ if (loads.length === 0 || loads.some((load) => load === undefined)) {
42
42
  return undefined;
43
+ }
44
+ return { count, loads: loads };
45
+ }
46
+ /** True when `construct(state)` builds exactly `host` (same value and type). */
47
+ function rebuilds(construct, state, host) {
48
+ let candidate;
43
49
  try {
44
- return {
45
- rank: Number(rank),
46
- count,
47
- loads: loads,
48
- };
50
+ candidate = construct(state);
49
51
  }
50
- catch {
51
- return undefined;
52
+ catch (error) {
53
+ if (error instanceof DenumError)
54
+ return false;
55
+ throw error;
52
56
  }
57
+ if (candidate.isFailedResult() || candidate.observationError())
58
+ return false;
59
+ return candidate.typeEquals(host) === Frame.all;
53
60
  }
54
61
  /**
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
+ * The intrinsic path, as `.####` has for a Demi. A source is a Denum (or a CD
63
+ * view) exactly when its stored `count` and `loads` are a valid state and it is
64
+ * `==` (same value and same type at every node, fields and methods included) to
65
+ * the instance the shipped `Denum` constructor (or its `.cd ()` view) builds
66
+ * from them. The answer is then built from that reconstruction, never from the
67
+ * source: no observer, method or closure of the source runs, so a forged `#`,
68
+ * `##` or `###` cannot steer it. A zero Count is therefore present, unlike a
69
+ * host with no children. The arity decides nothing: a Denum with `2ⁿ` loads is
70
+ * not a view, and a view is not rebuilt by the Denum constructor.
62
71
  *
63
- * @returns The source's reconstruction as a state, or undefined.
72
+ * @returns Which construction rebuilds the source, and its state; or undefined.
64
73
  */
65
74
  function intrinsicDenum(source) {
66
- if (!construct)
75
+ if (!constructors)
67
76
  return undefined;
68
77
  const host = source.referent();
69
78
  if (!host.ownsIdentity())
@@ -71,26 +80,20 @@ function intrinsicDenum(source) {
71
80
  const state = storedState(host);
72
81
  if (!state)
73
82
  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;
83
+ if (rebuilds(constructors.denum, state, host)) {
84
+ return { kind: "denum", state };
85
85
  }
86
- return candidate.typeEquals(host) === Frame.all ? state : undefined;
86
+ if (rebuilds(constructors.view, state, host))
87
+ return { kind: "view", state };
88
+ return undefined;
87
89
  }
88
90
  /**
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.coordinates ()`.
91
+ * The ranked hash, `.#n` (occurrence/figure 007.32, 007.32b, 007.35b, 007.36):
92
+ * a generic Denum of rung n, n from 0 to 5. It never answers a CD view, and
93
+ * there is no `#n=`; the algebraic value is `d.coordinates ()` of a view
94
+ * derived by `d.cd ()`.
92
95
  *
93
- * Two different operations answer it, and they are not the same:
96
+ * Three different cases answer it, and they are not the same:
94
97
  *
95
98
  * - **Construction from a host.** A host answers its Count C, Size s and
96
99
  * residual r only, so the Denum is built from those three observers with
@@ -99,9 +102,17 @@ function intrinsicDenum(source) {
99
102
  * Demi; rank 0 reads no residual. Failures are answered as they are, and
100
103
  * unusable observations are `$!.denum-count`, `$!.denum-observations` and
101
104
  * `$!.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
+ * - **Promotion of a Denum.** An instance of the shipped `Denum` class with a
106
+ * shipped binary arity (1, 2, 4, 8, 16, 32) answers its own state under the
107
+ * load-preserving embedding at rung n, keeping Count and every load, and
108
+ * below its own derived rank `$!.denum-demotion`. The embedding is
109
+ * structural: it does not invoke the CD interpretation, and the answer is a
110
+ * Denum. A Denum of Count zero is present. Any other arity is
111
+ * `$!.denum-unsupported-arity`: nothing is truncated, padded or read as
112
+ * `2^rank`.
113
+ * - **Refusal of a CD view.** A view derived by `d.cd ()` is never a source:
114
+ * the answer is `$!.denum-view-source`, and it is not measured as a host from
115
+ * its Count and Size either. Ask the Denum it views, `v.denum ()`.
105
116
  *
106
117
  * A multi-channel `RotorRouter` is not special-cased here as it is under
107
118
  * `.####` (`channelRouterDemi`): `.#n` reads only Count, Size and residual, so
@@ -123,12 +134,15 @@ export function observeDenum(source, rank) {
123
134
  if (!Number.isInteger(rank) || rank < 0 || rank > MAX_RANK) {
124
135
  return Frame.error("$!.denum-rank");
125
136
  }
126
- if (!construct)
137
+ if (!constructors)
127
138
  return Frame.error("$!.denum-unavailable");
139
+ const construct = constructors.denum;
128
140
  try {
129
141
  const intrinsic = intrinsicDenum(source);
142
+ if (intrinsic?.kind === "view")
143
+ return Frame.error("$!.denum-view-source");
130
144
  if (intrinsic)
131
- return construct(promote(intrinsic, rank));
145
+ return construct(promoteDenum(intrinsic.state, rank));
132
146
  const n = source.sizeFor(Frame.kCOUNT);
133
147
  if (n.observationError())
134
148
  return n;