hclang 0.37.1 → 0.38.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/README.md CHANGED
@@ -93,21 +93,21 @@ deno task test
93
93
 
94
94
  The root `deno.json` provides several tasks:
95
95
 
96
- | Task | Description |
97
- | ---------------------- | ------------------------------------------------- |
98
- | `deno task hc` | Launch the HC REPL |
99
- | `deno task setup` | Install pre-commit hooks |
100
- | `deno task test` | Run all tests (setup, format, lint, and tests) |
101
- | `deno task test:setup` | Run all pre-commit hooks on all files |
102
- | `deno task test:cli` | Test CLI package |
103
- | `deno task test:lib` | Test library package |
104
- | `deno task test:maml` | Test MAML package |
105
- | `deno task test:web` | Test web package |
106
- | `deno task test:bs` | Test BitScheme documentation |
107
- | `deno task test:doc` | Test HC documentation examples |
108
- | `deno task build` | Build native, npm, and web artifacts |
109
- | `deno task build:npm` | Generate the Node.js CLI package in `dist/npm` |
110
- | `deno task test:npm` | Pack and smoke-test the CLI package through `npx` |
96
+ | Task | Description |
97
+ | --------------------- | ------------------------------------------------- |
98
+ | `deno task hc` | Launch the HC REPL |
99
+ | `deno task setup` | Install pre-commit hooks |
100
+ | `deno task test` | Check formatting and lint, then run all tests |
101
+ | `deno task fix` | Format sources and apply lint fixes |
102
+ | `deno task test:cli` | Test CLI package |
103
+ | `deno task test:lib` | Test library package |
104
+ | `deno task test:maml` | Test MAML package |
105
+ | `deno task test:web` | Test web package |
106
+ | `deno task test:bs` | Test BitScheme documentation |
107
+ | `deno task test:doc` | Test HC documentation examples |
108
+ | `deno task build` | Build native, npm, and web artifacts |
109
+ | `deno task build:npm` | Generate the Node.js CLI package in `dist/npm` |
110
+ | `deno task test:npm` | Pack and smoke-test the CLI package through `npx` |
111
111
 
112
112
  ### BitScheme
113
113
 
@@ -161,8 +161,7 @@ deno task hc
161
161
  # Other platforms: pip install pre-commit
162
162
  ```
163
163
 
164
- Pre-commit hooks are automatically installed when you run `deno task test` or
165
- manually with `deno task setup`.
164
+ Install pre-commit hooks explicitly with `deno task setup`.
166
165
 
167
166
  4. Run tests:
168
167
 
@@ -170,8 +169,11 @@ deno task hc
170
169
  deno task test
171
170
  ```
172
171
 
173
- This will automatically set up pre-commit hooks, format code, run linters,
174
- and execute the test suite.
172
+ This checks formatting and lint without fixing sources, then runs the test
173
+ suite. Use `deno task fix` to format and apply lint fixes.
174
+
175
+ `deno task bundle:check` checks bundled HC sources without rewriting them;
176
+ after editing `hc/*.hc`, regenerate them with `deno task bundle:embed`.
175
177
 
176
178
  ### Project Structure
177
179
 
package/esm/cli/hc.js CHANGED
@@ -117,6 +117,7 @@ export async function main(hcEval, options) {
117
117
  let evaluated = false;
118
118
  let sourceStarted = false;
119
119
  let lexicalComplete = true;
120
+ let runtimeFailed = false;
120
121
  let test;
121
122
  if (options.verbose) {
122
123
  console.error("options", options);
@@ -128,6 +129,7 @@ export async function main(hcEval, options) {
128
129
  }
129
130
  const finishSource = (evaluator) => {
130
131
  const complete = evaluator.finish();
132
+ runtimeFailed ||= evaluator.hasFailures();
131
133
  if (!complete) {
132
134
  const reason = evaluator.error() ?? "incomplete lexical input";
133
135
  console.error(`HCEval.finish.failed: ${reason}`);
@@ -168,7 +170,7 @@ export async function main(hcEval, options) {
168
170
  if (test && lexicalComplete) {
169
171
  test.finish();
170
172
  }
171
- return lexicalComplete ? test?.exitCode ?? 0 : 1;
173
+ return lexicalComplete ? test?.exitCode ?? (runtimeFailed ? 1 : 0) : 1;
172
174
  }
173
175
  if (globalThis[Symbol.for("import-meta-ponyfill-esmodule")](import.meta).main) {
174
176
  // One name at a time, through the dictionary. A wholesale read would hand the
@@ -226,7 +226,8 @@ export const HC_SOURCES = new Map([
226
226
  "",
227
227
  "=== Schema Retrieval and Captures (implemented, #310)",
228
228
  "",
229
- "The `<>` property retrieves the schema attached to the binding:",
229
+ "The `<^>` property retrieves the schema attached to the binding; `.<>` observes",
230
+ "the runtime type:",
230
231
  "```",
231
232
  "; enum123.<^>",
232
233
  "# <1, 2, 3>",
@@ -690,14 +691,17 @@ export const HC_SOURCES = new Map([
690
691
  "; [.a 1] & {_}",
691
692
  "# []",
692
693
  "```",
693
- "`.#` and `.##` count the elements too, the immediate ones and the terminals",
694
- "below them, so neither counts a property. A terminal is an element that is not",
695
- "an aggregate, or an aggregate with no elements: the empty frame is one terminal,",
696
- "at the root or nested. A nested count contributes its own Size, including zero",
697
- "(`[[3], 0].##` is 3); other atoms contribute one. Every aggregate counts this",
698
- "way, a group as well as an array. A count is its own size, and any other value",
699
- "has none (#411, issue",
700
- "occurrence/figure 010.02)",
694
+ "`.#` counts immediate elements or represented words; properties do not count.",
695
+ "A missing Count rule falls back to native Size, including numbers, text, blobs,",
696
+ "bytes and opaque atoms. Explicit list, schema and all Count rules take precedence; `()` has no Count. `.##` measures represented",
697
+ "quantity: lists sum complete child Sizes, with Size 1 for an empty list. Numeric",
698
+ "magnitude, Unicode text length, blob bits and payload bytes have their own native",
699
+ "Sizes; schemas retain their description/alternative quantities. Other opaque",
700
+ "values have Size 1. `.###` alternates child Sizes on non-schema lists, or returns",
701
+ "Count parity (`.# mod 2`) on every other value with a Count; schemas have nil",
702
+ "residual, and `<>` observes as `[]`. `.####` derives a Demi, an instance of",
703
+ "the class in `hc/demi.hc`, returning nil at zero Count or nil residual and",
704
+ "propagating failed observations.",
701
705
  "```",
702
706
  "; [.meta 1; 2, 3] .#",
703
707
  "# 2",
@@ -710,7 +714,7 @@ export const HC_SOURCES = new Map([
710
714
  "; 5.#",
711
715
  "# 5",
712
716
  "; “abc”.#",
713
- "# ()",
717
+ "# 3",
714
718
  "```",
715
719
  "**Decision.** Canonical rendering puts the properties first, then the elements.",
716
720
  "This reverses the spelling this file carried before. It makes print order agree",
@@ -1159,6 +1163,271 @@ export const HC_SOURCES = new Map([
1159
1163
  "",
1160
1164
  ].join("\n"),
1161
1165
  ],
1166
+ [
1167
+ "hc/demi.hc",
1168
+ [
1169
+ "#!/usr/bin/env hc",
1170
+ "```",
1171
+ "Demi (occurrence/figure 011.20)",
1172
+ "",
1173
+ "A Demi is a closed two-channel state: a phase and two loads, left and right.",
1174
+ "Applying a Demi to a Frame deposits that Frame's Size into the active load and",
1175
+ "flips the phase, so a Demi follows alternating construction one observation at",
1176
+ "a time. It is what the fourth hash answers: `x.####` is the Demi of x's Count,",
1177
+ "Size and residual, with left load (Size + residual)/2 and right load",
1178
+ "(Size − residual)/2.",
1179
+ "",
1180
+ "`Demi` is an ordinary HC class: a constructor closure answering a fresh",
1181
+ "aggregate whose fields are its state and whose methods are the class. Nothing",
1182
+ "about it is native. The fourth hash keeps only its derivation and builds its",
1183
+ "answer with this constructor, which the evaluator reads from this file, compiled",
1184
+ "in, the first time `.####` needs one. So this file is both the definition every",
1185
+ "evaluator uses and its specification, and it works without a resource root.",
1186
+ "Import it to construct Demis directly: `Demis <- './hc/demi.hc'`, then",
1187
+ "`Demis.Demi [2, 0, 0]`.",
1188
+ "",
1189
+ "**State.** `Demi [phase, left, right]` takes three Counts. The phase is the",
1190
+ "least positive Count of its parity (011.18g): 2 when even, left next, and 1",
1191
+ "when odd, right next. It is a Count rather than all or nil because a field",
1192
+ "cannot hold nil, and a Count keeps the odd phase present: a Demi in phase 1 is",
1193
+ "a value, unlike the nil `.####` answers for a Frame with no Demi. Anything else",
1194
+ "is refused with `invalid-demi`, a missing name, as Figures refuses an",
1195
+ "unsupported host. The fields are the whole state, so two Demis are `=` exactly",
1196
+ "when their fields are.",
1197
+ "",
1198
+ "**Observers.** A Demi declares its own Count, Size and residual (011.20): its",
1199
+ "phase, its total load L+R, and the signed difference L−R. An aggregate",
1200
+ "otherwise observes its elements, and a Demi has none, so without these it",
1201
+ "would observe as 0, 1, 0. Its fourth hash derives from them like any Frame's,",
1202
+ "so `d.####` rebuilds the same Demi.",
1203
+ "",
1204
+ "**Application**, `d x`: observe x's Size once, deposit it into the active load,",
1205
+ "flip the phase. A Size of zero still flips. If x has no Size, its failure is",
1206
+ "the answer rather than a deposit. It is a mutating method, `apply_`, so",
1207
+ "through a name, `d x` is a functional update: it answers a new Demi and leaves",
1208
+ "d unchanged, holding exactly the same three fields.",
1209
+ "",
1210
+ "**Scaling**, `d.scale_ k`: multiply both loads by a Count k, keeping the",
1211
+ "phase. It is the numerical shadow of Star: `(x * y).####` is",
1212
+ "`x.####.scale_ (y.##)` for a nonempty host x. Scaling is a method because a",
1213
+ "class cannot declare an operator, so `d * k` is ordinary Star, which refuses",
1214
+ "an aggregate with properties.",
1215
+ "",
1216
+ "**Text.** A Demi prints as what it is, an aggregate of fields and methods, and",
1217
+ "that reads back as the same Demi. As text it reads `Demi(<>,12,3)`, with the",
1218
+ "phase spelled as the orientation it stands for.",
1219
+ "",
1220
+ "**Methods read their own state through `_^`.** A bare name inside a method is",
1221
+ "looked up on the argument first (spec 7), and a Demi is applied to arbitrary",
1222
+ "Frames, so a bare `left` could read the argument's field. `_^` is the receiver",
1223
+ "from a method's body, and `_^^` from a block one level in, because carets",
1224
+ "count lexically (spec a06). What a branch needs is bound as a local at method",
1225
+ "level, where nested blocks read it.",
1226
+ "```",
1227
+ ".Demi {.p (_ .0); .l (_ .1); .r (_ .2);",
1228
+ " ((((p = 2) : {p = 1}) ? {p ~ (0.<>)}) ? {(l ~ (0.<>)) ? {r ~ (0.<>)}}) ? {[",
1229
+ " .phase p;",
1230
+ " .left l;",
1231
+ " .right r;",
1232
+ " .# {_^.phase};",
1233
+ " .## {_^.left + _^.right};",
1234
+ " .### {_^.left - _^.right};",
1235
+ " .apply_ {.s (_ .##); .l (_^ .left); .r (_^ .right);",
1236
+ " (s ~ (0.<>)) ? {((_^^ .phase) = 2) ? {@left (l + s); @phase 1;} : {@right (r + s); @phase 2;}; _^^} : {s}};",
1237
+ " .scale_ {.k _; .l (_^ .left); .r (_^ .right);",
1238
+ " (k ~ (0.<>)) ? {@left (l * k); @right (r * k); _^^} : {invalid-scale}};",
1239
+ " .toText {“Demi(” (((_^ .phase) = 2) ? {“<>”} : {“()”}) “,” (_^ .left) “,” (_^ .right) “)”}",
1240
+ " ]} : {invalid-demi}};",
1241
+ "```",
1242
+ "## Construction",
1243
+ "",
1244
+ "A Demi holds exactly its three fields. Each instance is its own: two built",
1245
+ "from the same constructor share nothing",
1246
+ "```",
1247
+ "; .d (Demi [2, 0, 0]);",
1248
+ "; [d.phase, d.left, d.right]",
1249
+ "# [2, 0, 0]",
1250
+ "; .a (Demi [2, 5, 3]); .b (Demi [1, 7, 0]);",
1251
+ "; [“” a, “” b, a.left, b.left]",
1252
+ "# [“Demi(<>,5,3)”, “Demi((),7,0)”, 5, 7]",
1253
+ "```",
1254
+ "Anything but three Counts with phase 1 or 2 is refused",
1255
+ "```",
1256
+ "; Demi [3, 0, 0]",
1257
+ "# $!.name-missing “$:Frame.0.invalid-demi”;",
1258
+ "; [Demi [2, (0 - 1), 0], Demi [2, (5 - 3), 0], Demi [2, 0], Demi 5, Demi [(5 - 3), 0, 0]]",
1259
+ "# [$!.name-missing “$:Frame.0.invalid-demi”;, $!.name-missing “$:Frame.0.invalid-demi”;, $!.name-missing “$:Frame.0.invalid-demi”;, $!.name-missing “$:Frame.0.invalid-demi”;, $!.name-missing “$:Frame.0.invalid-demi”;]",
1260
+ "```",
1261
+ "## Application",
1262
+ "",
1263
+ "Each application deposits the argument's Size into the active load and flips",
1264
+ "the phase. The receiver is left as it was",
1265
+ "```",
1266
+ "; “” (d [5, 3])",
1267
+ "# “Demi((),8,0)”",
1268
+ "; “” d",
1269
+ "# “Demi(<>,0,0)”",
1270
+ "; “” (((d 5) 3) 7)",
1271
+ "# “Demi((),12,3)”",
1272
+ "```",
1273
+ "A Size of zero deposits nothing and still flips, in both phases",
1274
+ "```",
1275
+ "; [(d 0).phase, ((d 0) 0).phase, (d 0).left, ((d 0) 0).right]",
1276
+ "# [1, 2, 0, 0]",
1277
+ "```",
1278
+ "Any Frame is applied by its Size, measured without running it: text, the",
1279
+ "empty aggregate and all (Size 1 each), nested aggregates, a closure (one per",
1280
+ "word, so `{5 + 3}` is 9), empty text, and loads too large for a machine integer",
1281
+ "```",
1282
+ "; “” ((((((d “hello”) []) <>) [[1, 2], [3]]) {5 + 3}) “”)",
1283
+ "# “Demi(<>,15,7)”",
1284
+ "; “” ((d 123456789012345678901234567890) 987654321098765432109876543210)",
1285
+ "# “Demi(<>,123456789012345678901234567890,987654321098765432109876543210)”",
1286
+ "```",
1287
+ "A Frame without a Size is not deposited: its failure is the answer",
1288
+ "```",
1289
+ "; d ([].nope)",
1290
+ "# $!.name-missing “...",
1291
+ "; “” d",
1292
+ "# “Demi(<>,0,0)”",
1293
+ "```",
1294
+ "Deposit reads the receiver through `_^`, so an argument that holds fields of",
1295
+ "the same names deposits by its Size and nothing else",
1296
+ "```",
1297
+ "; “” (d [.left 50; .phase 1; .s 100; 4])",
1298
+ "# “Demi((),4,0)”",
1299
+ "```",
1300
+ "A Demi captured by a closure, mapped, or reduced is applied the same way, and",
1301
+ "the captured one stays as it was",
1302
+ "```",
1303
+ "; .start (Demi [2, 0, 0]); .step {start _};",
1304
+ "; [“” (step 5), “” (step 3), “” start]",
1305
+ "# [“Demi((),5,0)”, “Demi((),3,0)”, “Demi(<>,0,0)”]",
1306
+ "; [d, a] & {“” (_ 5)}",
1307
+ "# [“Demi((),5,0)”, “Demi((),10,3)”]",
1308
+ "; “” ([5, 3, 7] | (Demi [2, 0, 0]))",
1309
+ "# “Demi((),12,3)”",
1310
+ "```",
1311
+ "Through a mutable name, application writes the Demi in place",
1312
+ "```",
1313
+ "; .m_ (Demi [2, 0, 0]);",
1314
+ "; m_ 5;",
1315
+ "; “” m_",
1316
+ "# “Demi((),5,0)”",
1317
+ "```",
1318
+ "## Observers",
1319
+ "",
1320
+ "A Demi's Count is its phase, its Size the total load and its residual the",
1321
+ "signed difference, including a present zero-load Demi in either phase",
1322
+ "```",
1323
+ "; .e (Demi [1, 12, 3]);",
1324
+ "; [e.#, e.##, e.###]",
1325
+ "# [1, 15, 9]",
1326
+ "; [d.#, d.##, d.###, (Demi [1, 0, 0]).#]",
1327
+ "# [2, 0, 0, 1]",
1328
+ "```",
1329
+ "So its fourth hash is the same Demi, however often it is taken. It is equal,",
1330
+ "not identical: each observation builds a new instance",
1331
+ "```",
1332
+ "; [e.#### = e, e.#### === e, e.#### == e, ((e.####).####).#### = e]",
1333
+ "# [<>, (), <>, <>]",
1334
+ "; [d.#### = d, (Demi [1, 0, 0]).#### = (Demi [1, 0, 0])]",
1335
+ "# [<>, <>]",
1336
+ "```",
1337
+ "A Demi inside an aggregate contributes its declared Size",
1338
+ "```",
1339
+ "; [[e, e].##, [e, d].###, [e].#### = (Demi [1, 15, 0])]",
1340
+ "# [30, 15, <>]",
1341
+ "```",
1342
+ "## The fourth hash",
1343
+ "",
1344
+ "`.####` answers an instance of this class, built from the same source, so it",
1345
+ "compares equal to one built here",
1346
+ "```",
1347
+ "; [[5, 3].#### = (Demi [2, 5, 3]), “hello”.#### = (Demi [1, 3, 2]), [0].#### = (Demi [1, 0, 0])]",
1348
+ "# [<>, <>, <>]",
1349
+ "; “” [5, 3, 7].####",
1350
+ "# “Demi((),12,3)”",
1351
+ "```",
1352
+ "It is nil where there is no Demi: an empty host, Count zero, no Count, or no",
1353
+ "residual. Properties are not elements, sharing counts each occurrence, and a",
1354
+ "group observes as its elements",
1355
+ "```",
1356
+ "; [[].####, 0.####, ().####, <2,3,5>.####]",
1357
+ "# [(), (), (), ()]",
1358
+ "; .shared [1, 2];",
1359
+ "; [[.a 100; 5, 3].#### = [5, 3].####, [shared, shared].#### = (Demi [2, 3, 3]), (5, 3).#### = (Demi [2, 5, 3])]",
1360
+ "# [<>, <>, <>]",
1361
+ "```",
1362
+ "## Observing commutes with applying",
1363
+ "",
1364
+ "For a nonempty host X, observing X Y is applying X's Demi to Y, and both equal",
1365
+ "the observation of the array X Y constructs",
1366
+ "```",
1367
+ "; .X [5, “ab”, [1, 2]];",
1368
+ "; [(X 7).#### = (X.#### 7), (X 7).#### = [5, “ab”, [1, 2], 7].####, (X {5 + 3}).#### = (X.#### {5 + 3})]",
1369
+ "# [<>, <>, <>]",
1370
+ "```",
1371
+ "The empty host is the boundary. `[].####` is nil, a choice of convention",
1372
+ "(011.15g) rather than a theorem, and applying nil answers the argument, so",
1373
+ "the equation as written fails there. Lifting the empty host to the zero-load",
1374
+ "Demi in phase 2 restores it: that is the initialized-router reading, and it is",
1375
+ "also what a reduce starts from",
1376
+ "```",
1377
+ "; [([] 7).#### = ([].#### 7), ([] 7).#### = ((Demi [2, 0, 0]) 7), ([] 7).#### = ([7] | (Demi [2, 0, 0]))]",
1378
+ "# [(), <>, <>]",
1379
+ "; [] | (Demi [2, 0, 0]) = (Demi [2, 0, 0])",
1380
+ "# <>",
1381
+ "```",
1382
+ "## Scaling and Star",
1383
+ "",
1384
+ "Scaling multiplies both loads and keeps the phase: zero annihilates, one is",
1385
+ "the identity, and scaling composes",
1386
+ "```",
1387
+ "; [“” (e.scale_ 2), “” (e.scale_ 0), (e.scale_ 1) = e, ((e.scale_ 2).scale_ 3) = (e.scale_ 6)]",
1388
+ "# [“Demi((),24,6)”, “Demi((),0,0)”, <>, <>]",
1389
+ "; e.scale_ (0 - 2)",
1390
+ "# $!.name-missing “$:Frame.0.invalid-scale”;",
1391
+ "```",
1392
+ "It is the shadow of Star on a nonempty host",
1393
+ "```",
1394
+ "; [([5, 3] * 3).#### = ([5, 3].####.scale_ 3), ([[[]], []] * [[], []]).#### = ([[[]], []].####.scale_ ([[], []].##))]",
1395
+ "# [<>, <>]",
1396
+ "```",
1397
+ "No operator is overloaded: `+` is ordinary addition, which a Demi is not a",
1398
+ "number for, and `*` is ordinary Star, which refuses an aggregate with fields",
1399
+ "```",
1400
+ "; [e + 3, e * 3]",
1401
+ "# [(), $!.star-metadata *]",
1402
+ "```",
1403
+ "## A precise regression: a load read at the wrong depth",
1404
+ "",
1405
+ "Carets count lexically, so inside a branch `_^` is the method's own scope, not",
1406
+ "the receiver. Reading a load there fails, `$!.name-missing`. Before 011.20,",
1407
+ "`+` turned that failure into nil, and writing nil leaves a field as it was, so",
1408
+ "the deposit silently did nothing while the phase still flipped. Now the",
1409
+ "failure reaches the field, where it shows",
1410
+ "```",
1411
+ "; .Wrong {[.phase 2; .left 0; .apply_ {.s (_ .##); (_^.phase = 2) ? {@left (_^.left + s); @phase 1;};}]};",
1412
+ "; .w ((Wrong ()) 5);",
1413
+ "; [w.phase, w.left]",
1414
+ "# [1, $!.name-missing “...",
1415
+ "; .Right {[.phase 2; .left 0; .apply_ {.s (_ .##); (_^.phase = 2) ? {@left (_^^.left + s); @phase 1;};}]};",
1416
+ "; .r ((Right ()) 5);",
1417
+ "; [r.phase, r.left]",
1418
+ "# [1, 5]",
1419
+ "```",
1420
+ "## Importing",
1421
+ "",
1422
+ "Imported, the file binds the same class. The import is an example line, so",
1423
+ "the evaluator, which reads this file without a root, never runs it",
1424
+ "```",
1425
+ "; Demis <- './hc/demi.hc';",
1426
+ "; [(Demis.Demi [2, 5, 3]) = [5, 3].####, “” ((Demis.Demi [2, 0, 0]) 4)]",
1427
+ "# [<>, “Demi((),4,0)”]",
1428
+ "",
1429
+ ].join("\n"),
1430
+ ],
1162
1431
  [
1163
1432
  "hc/figures.hc",
1164
1433
  [
@@ -1186,9 +1455,15 @@ export const HC_SOURCES = new Map([
1186
1455
  "Flat repetition is `x.## [y]`. A tally, when wanted, is an explicit",
1187
1456
  "construction, such as `x & {[[]]}` for one fresh mark per immediate element.",
1188
1457
  "Native `.##` uses each child's native Size, including numeric magnitude and",
1189
- "text length. Native `.###` also accepts mixed arrays and evaluated groups.",
1458
+ "text length. Native `.###` also accepts mixed arrays, groups, and non-schema list syntax/closures. Every other",
1459
+ "value with a Count answers its parity (`.# mod 2`, a FrameInt), including valid zero, so equal quantities",
1460
+ "observe alike whatever their kind or numeric type; schemas answer nil. `()` has no Count; `<>` observes as `[]`.",
1461
+ "Missing Count rules fall back to Size, including sized atoms. Explicit Counts take precedence.",
1462
+ "Stored errors and failed child quantities propagate rather than acquiring a load.",
1190
1463
  "Star keeps its narrower array/Count composition domain.",
1191
1464
  "",
1465
+ "For Counts, Gap and Spin now use parity too: `gap 7` is 1 and `spin 2 4` is 0.",
1466
+ "",
1192
1467
  "Only two derived mathematical abbreviations remain: Gap = |signed residual|",
1193
1468
  "and Spin = signed residual / positive Count. They define no Figure domain.",
1194
1469
  "Incidence is native `point ~~~ line`; collective incidence uses ordinary",
@@ -1300,7 +1575,7 @@ export const HC_SOURCES = new Map([
1300
1575
  "; [[[], [[]]], [[[], []], []], [[[[], [], []]]]].###",
1301
1576
  "# 2",
1302
1577
  "; [[1, []].###, [[], “ab”].###, [[], ([], [])].###, 5.###]",
1303
- "# [0, -1, -1, ()]",
1578
+ "# [0, -1, -1, 1]",
1304
1579
  "```",
1305
1580
  "Native Size also measures mixed values, independently of structural Star.",
1306
1581
  "Numeric magnitude truncates exactly; text length uses native Unicode code-point iteration,",
@@ -1315,7 +1590,7 @@ export const HC_SOURCES = new Map([
1315
1590
  "; [[5, “hello”].##, [5, “hello”].###, (5, 3).###, [5, 3, 2, 7].###]",
1316
1591
  "# [10, 0, 2, -3]",
1317
1592
  "; [([5, “hello”].### ? {<>} : {()}), (5.### ? {<>} : {()})]",
1318
- "# [<>, ()]",
1593
+ "# [<>, <>]",
1319
1594
  "; { .x <[].<>> _; x.### } [[], [“nested atom”]]",
1320
1595
  "# -10",
1321
1596
  "```",
@@ -1359,6 +1634,37 @@ export const HC_SOURCES = new Map([
1359
1634
  "; [.tag 1;] * [[]]",
1360
1635
  "# $!.star-metadata *",
1361
1636
  "```",
1637
+ "The fourth observer derives a Demi from Count, Size and residual.",
1638
+ "A zero Count or absent residual gives no Demi; errors propagate. A schema used",
1639
+ "as an explicit element contributes Size normally, even though its own Demi is nil.",
1640
+ "A present Demi has Size L+R and residual L−R. Its Count is the least positive",
1641
+ "representative of its stored parity: 2 for all/even and 1 for nil/odd. The same",
1642
+ "universal derivation therefore reconstructs its data value on repeated fourth",
1643
+ "observation, including present zero loads, without a special fourth-hash rule.",
1644
+ "A present Demi is an instance of the HC class in `hc/demi.hc` (011.20), and",
1645
+ "reads as text `Demi(phase,left,right)`. Applying a Demi to Y deposits Y's Size",
1646
+ "and flips the phase, so for a nonempty Figure host X, (X Y).#### is",
1647
+ "X.#### Y; for an empty host, start from the zero-load Demi in phase 2 instead,",
1648
+ "because [].#### is nil. Scaling is the method `scale_`, the shadow of Star:",
1649
+ "(X * Y).#### is X.####.scale_ (Y.##) for a nonempty host. Count is a terminal",
1650
+ "quantity: positive Counts agree with pure Figures of that terminal Size, but 0",
1651
+ "has Size 0 while [] has Size 1. Mixed values may have present zero-load Demis,",
1652
+ "including [0].####.",
1653
+ "```",
1654
+ "; [[].####, 0.####, “” “hello”.####, <2,3,5>.####]",
1655
+ "# [(), (), “Demi((),3,2)”, ()]",
1656
+ "; [(10 - 5).#### = 5.####, (0 - 5).#### = 5.####, (5.0).#### = 5.####, “hello”.#### = 5.####]",
1657
+ "# [<>, <>, <>, <>]",
1658
+ "; [<>.#, <>.##, <>.###, ().##]",
1659
+ "# [0, 1, 0, 1]",
1660
+ "; [1, 2, 3, 7] & {“” _.####}",
1661
+ "# [“Demi((),1,0)”, “Demi(<>,1,1)”, “Demi((),2,1)”, “Demi((),4,3)”]",
1662
+ "; [[0], [“hello”], [<2,3,5>,2], {5 + 3}] & {“” _.####}",
1663
+ "# [“Demi((),0,0)”, “Demi((),5,0)”, “Demi(<>,3,2)”, “Demi((),8,1)”]",
1664
+ "; [“” ([[],[]].#### 3), “” (7.####.scale_ 2), ([[],[]] 3).#### = ([[],[]].#### 3)]",
1665
+ "# [“Demi((),4,1)”, “Demi((),8,6)”, <>]",
1666
+ "",
1667
+ "```",
1362
1668
  "Importing skips the examples and ends on a native true value.",
1363
1669
  "```",
1364
1670
  "[] = []",
@@ -2197,8 +2503,11 @@ export const HC_SOURCES = new Map([
2197
2503
  "# $!.unimplemented .x 42",
2198
2504
  "; .mat <tensor<2x3xf32>> [[1,2,3]] # Tensor types",
2199
2505
  "# $!.unimplemented .mat...",
2200
- "; .f <(.x <i32>) -> <i32>> {x + 1} # Function signatures",
2201
- "# $!.unimplemented .f...",
2506
+ "```",
2507
+ "Function-signature notation such as \\`.f <(.x <i32>) -> <i32>> {x + 1}\\`",
2508
+ "remains a proposal and is not executable HC. Its arrow is not accepted by the",
2509
+ "current schema grammar.",
2510
+ "```",
2202
2511
  "",
2203
2512
  ].join("\n"),
2204
2513
  ],
@@ -0,0 +1,91 @@
1
+ /**
2
+ * The Demi class, loaded from HC for the fourth hash.
3
+ *
4
+ * `.####` answers a Demi, and a Demi is an HC class, `Demi` in `hc/demi.hc`,
5
+ * not a native one (occurrence/figure 011.20). The frames package derives the
6
+ * phase and loads natively but cannot run HC, so this module installs the
7
+ * constructor it calls (`installDemiConstructor`).
8
+ *
9
+ * The class is read from the compiled-in bundle, not through a resource root,
10
+ * so `.####` works in a bare `evaluate`, which has no root and imports nothing.
11
+ * The file is evaluated the first time a Demi is constructed, once per
12
+ * process, the way an import evaluates a testdoc: its example lines are
13
+ * dropped and the first failure fails the whole. It runs in a namespace of its
14
+ * own, with no host bindings and no root, so it can read nothing but the
15
+ * global operators, and nothing a program declares can reach it. Its outcome
16
+ * is kept, failure included, so it is never evaluated twice. A Demi requested
17
+ * while the file is still loading is `$!.demi-prologue-reentrant`, because
18
+ * the class cannot be defined by itself.
19
+ *
20
+ * What stays native is only what HC cannot say: reading `#`, `##` and `###`
21
+ * in a fixed order with their absence and error rules, and checking that the
22
+ * loads are whole. Everything a Demi does is in the HC source.
23
+ *
24
+ * @module
25
+ */
26
+ import { EvaluationScope, Frame, FrameArray, FrameExpr, installDemiConstructor, } from "../frames.js";
27
+ import { HC_SOURCES } from "../bundle/hc-sources.js";
28
+ import { evaluateSourceText } from "./resource-import.js";
29
+ /** The bundled file that defines the class. */
30
+ export const DEMI_SOURCE = "hc/demi.hc";
31
+ /** The name the file declares the constructor under. */
32
+ export const DEMI_CLASS = "Demi";
33
+ let loaded;
34
+ let loading = false;
35
+ /**
36
+ * Evaluates class source in a namespace of its own, with no host bindings and
37
+ * no root, answering its `Demi` or the failure. Uncached: `demiClass` keeps
38
+ * the one result the process uses.
39
+ *
40
+ * @param source The source text, or undefined when the bundle lacks it.
41
+ * @returns The constructor, or an error.
42
+ */
43
+ export function loadDemiClass(source) {
44
+ if (source === undefined) {
45
+ return Frame.error(`$!.demi-prologue-missing ${DEMI_SOURCE}`);
46
+ }
47
+ const namespace = new FrameArray([]);
48
+ const result = evaluateSourceText(source, EvaluationScope.root(namespace));
49
+ if (result.isFailedResult() || result.observationError())
50
+ return result;
51
+ const constructor = namespace.getOwn(DEMI_CLASS);
52
+ return constructor.is.missing
53
+ ? Frame.error(`$!.demi-prologue-missing ${DEMI_CLASS}`)
54
+ : constructor;
55
+ }
56
+ /**
57
+ * The `Demi` constructor closure, evaluated from the bundle on first use.
58
+ *
59
+ * @returns The constructor, or the error that loading it answered.
60
+ */
61
+ export function demiClass() {
62
+ if (loaded)
63
+ return loaded;
64
+ if (loading)
65
+ return Frame.error("$!.demi-prologue-reentrant");
66
+ loading = true;
67
+ try {
68
+ loaded = loadDemiClass(HC_SOURCES.get(DEMI_SOURCE));
69
+ }
70
+ finally {
71
+ loading = false;
72
+ }
73
+ return loaded;
74
+ }
75
+ /**
76
+ * Constructs a Demi with the HC class: `Demi [phase, left, right]`.
77
+ *
78
+ * @param phase 2 when even, 1 when odd.
79
+ * @param left The left load.
80
+ * @param right The right load.
81
+ * @returns The new instance, or the class's failure.
82
+ */
83
+ export function constructDemi(phase, left, right) {
84
+ const constructor = demiClass();
85
+ if (constructor.isFailedResult() || constructor.observationError()) {
86
+ return constructor;
87
+ }
88
+ const state = new FrameArray([phase, left, right]);
89
+ return FrameExpr.answeredValue(constructor.call(state)).referent();
90
+ }
91
+ installDemiConstructor(constructDemi);
@@ -1,4 +1,4 @@
1
- import { EvaluationScope, Frame, FrameGroup, NilContext, } from "../frames.js";
1
+ import { EvaluationScope, Frame, FrameGroup, FrameNote, NilContext, } from "../frames.js";
2
2
  import { LexPipe } from "./lex-pipe.js";
3
3
  import { ParsePipe } from "./parse-pipe.js";
4
4
  /** Builds the shared Lex/Parse/Eval pipeline for one HC source unit. */
@@ -60,6 +60,13 @@ export class EvalPipe extends Frame {
60
60
  writable: true,
61
61
  value: void 0
62
62
  });
63
+ /** Whether this source unit emitted an unhandled evaluation failure. */
64
+ Object.defineProperty(this, "failed", {
65
+ enumerable: true,
66
+ configurable: true,
67
+ writable: true,
68
+ value: false
69
+ });
63
70
  // Legacy lexical features follow kOUT to the file scope. Scoped pipes
64
71
  // replace it with a live view of every supplied lookup layer.
65
72
  this.set(Frame.kOUT, fileScope);
@@ -69,7 +76,17 @@ export class EvalPipe extends Frame {
69
76
  const scope = this.scope ??
70
77
  EvaluationScope.root(this.fileScope, this.hostNamespace);
71
78
  const result = expr.in(scope);
79
+ this.failed ||= outputFailed(result, new Set());
72
80
  this.out.collect(result, context);
73
81
  return result;
74
82
  }
75
83
  }
84
+ /** Inspect emitted values without evaluating them or following metadata links. */
85
+ function outputFailed(result, seen) {
86
+ if (seen.has(result))
87
+ return false;
88
+ seen.add(result);
89
+ return result.isFailedResult() ||
90
+ (result instanceof FrameNote && result.isDiagnosticFailure()) ||
91
+ result.asArray().some((item) => outputFailed(item, seen));
92
+ }