hclang 0.21.0 → 0.21.2

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.
@@ -616,6 +616,21 @@ export const HC_SOURCES = new Map([
616
616
  "; [.a 1] & {_}",
617
617
  "# []",
618
618
  "```",
619
+ "`.#` and `.##` count the elements too, the immediate ones and the leaves below",
620
+ "them, so neither counts a property. Every aggregate counts this way, a group as",
621
+ "well as an array. A count is its own size, and any other value has none (#411)",
622
+ "```",
623
+ "; [.meta 1; 2, 3] .#",
624
+ "# 2",
625
+ "; [.meta [1, 2]; [3, []], 4] .##",
626
+ "# 2",
627
+ "; (1, [2, 3]) .##",
628
+ "# 3",
629
+ "; 5.#",
630
+ "# 5",
631
+ "; “abc”.#",
632
+ "# ()",
633
+ "```",
619
634
  "**Decision.** Canonical rendering puts the properties first, then the elements.",
620
635
  "This reverses the spelling this file carried before. It makes print order agree",
621
636
  "with the order the doubled operators iterate, and it leaves canonical output",
@@ -1054,8 +1069,12 @@ export const HC_SOURCES = new Map([
1054
1069
  "never affects `=`. Every observer answers a Figure, never a number or a",
1055
1070
  "Boolean: `A`, with no children, means no, and `B` means yes. So every answer can",
1056
1071
  "be a child, a receiver, or an argument, and the class is closed under its own",
1057
- "methods. Only Form and Text leave the class, on purpose: they answer a plain",
1058
- "bracket tree and its text, for reading a Figure rather than computing with it.",
1072
+ "methods. Only Cells, Form and Text leave the class, on purpose: Cells and Form",
1073
+ "answer a plain bracket tree and Text spells one, for reading a Figure rather",
1074
+ "than computing with it. A tally also reads as a natural number. Its children",
1075
+ "are an aggregate, so `(x.size()).children.#` is the FrameCount it spells and",
1076
+ "`x.children.#` counts x's children. A Figure's own `.#` and `.##` are `0`,",
1077
+ "because its children are a property rather than its elements (#407, #411).",
1059
1078
  "",
1060
1079
  "A method names its own Figure `_^`, because a bare name is looked up on the",
1061
1080
  "argument first (spec 7), and the argument is usually another Figure. For the",
@@ -1089,21 +1108,35 @@ export const HC_SOURCES = new Map([
1089
1108
  " .size {.kids _^.children;",
1090
1109
  " (kids = U.children) ? {Figure [U]} : {((kids & {_.size()}) | (Tally Z)).row}};",
1091
1110
  "```",
1111
+ "**Cells**, `x.cells()`: the bracket tree of x with `1` in place of each cell,",
1112
+ "so `(x.cells()).##` is the Size of x as a natural number, counted without",
1113
+ "joining a tally. A count is its own size, so the `1` of a unit is already its",
1114
+ "count. It keeps the cell count and where the cells are, and forgets identity.",
1115
+ "The unit case comes first, as in Size",
1116
+ "```;",
1117
+ " .cells {.kids _^.children;",
1118
+ " (kids = U.children) ? {1} : {kids & {_.cells()}}};",
1119
+ "```",
1092
1120
  "**Multiplication**, `x.mul y`: y once for every cell of x, and `Z` when y is",
1093
1121
  "`Z`. It keeps y's identity in every position and forgets x's shape",
1094
1122
  "```;",
1095
- " .mul {.y _; .cells ((_^.size()).children);",
1096
- " (y = Z) ? {Z} : {Figure (cells & {y})}};",
1123
+ " .mul {.y _; .units ((_^.size()).children);",
1124
+ " (y = Z) ? {Z} : {Figure (units & {y})}};",
1097
1125
  "```",
1098
1126
  "**Tally multiplication**, `x.size_mul y`: the Size of a Figure holding y once",
1099
- "per child of x. It keeps the cell count and forgets both shapes",
1127
+ "per child of x, and `Z` when y is `Z`, as in Multiplication: one row of",
1128
+ "nothing, `Figure [Z]`, has the form of `U` and would count as a cell. It keeps",
1129
+ "the cell count and forgets both shapes",
1100
1130
  "```;",
1101
- " .size_mul {.y _; (Figure (_^.children & {y})).size()};",
1131
+ " .size_mul {.y _; .kids _^.children;",
1132
+ " (y = Z) ? {Z} : {(Figure (kids & {y})).size()}};",
1102
1133
  "```",
1103
1134
  "**Size-equivalence**, `x.size_equivalent y`: `U` when the two Sizes agree and",
1104
- "`Z` otherwise. It keeps only whether the cell counts agree",
1135
+ "`Z` otherwise. It compares the two cell counts as natural numbers, which",
1136
+ "agree exactly when the two tallies do, so it joins no tally. It keeps only",
1137
+ "whether the cell counts agree",
1105
1138
  "```;",
1106
- " .size_equivalent {((_^.size()) = (_.size())) ? {U} : {Z}};",
1139
+ " .size_equivalent {(((_^.cells()).##) = ((_.cells()).##)) ? {U} : {Z}};",
1107
1140
  "```",
1108
1141
  "**Sameness of form**, `x.sameform y`: `B` when `x = y`. It compares child by",
1109
1142
  "child and forgets identity and reuse",
@@ -1214,6 +1247,21 @@ export const HC_SOURCES = new Map([
1214
1247
  "; ((two_fours.size()).size()) = (two_fours.size())",
1215
1248
  "# <>",
1216
1249
  "```",
1250
+ "A tally reads as a natural number, because its children are an aggregate and",
1251
+ "`.#` counts them. A Figure's own `.#` and `.##` are `0`: its children are a",
1252
+ "property, not its elements, and `.##` counts only what is not an aggregate",
1253
+ "```",
1254
+ "; [E.children.#, (E.many()).children.#, (two_fours.size()).children.#, E.#, E.##, E.children.##] = [2, 2, 8, 0, 0, 0]",
1255
+ "# <>",
1256
+ "```",
1257
+ "Cells marks where the cells are, and `.##` counts them as Size does. A count",
1258
+ "is its own size, as a tally is its own Size",
1259
+ "```",
1260
+ "; [(E.cells()) = [[], 1], (deep4.cells()) = [[[[1, 1, 1, 1]]]], (Z.cells()) = [], (U.cells()) = 1]",
1261
+ "# [<>, <>, <>, <>]",
1262
+ "; [(two_fours.cells()).##, (deep4.cells()).##, ((two_fours.cells()).##).##, (two_fours.size()).children.#] = [8, 4, 8, 8]",
1263
+ "# <>",
1264
+ "```",
1217
1265
  "## The semiring",
1218
1266
  "",
1219
1267
  "The carrier is not raw Figures. Let `F` be the finite, well-founded Figures,",
@@ -1241,6 +1289,11 @@ export const HC_SOURCES = new Map([
1241
1289
  "```",
1242
1290
  "; [((Figure [row2, row3]).size()) = (Figure [U, U, U, U, U]), (rectangle23.size()) = ((row2.size()).size_mul (row3.size()))]",
1243
1291
  "# [<>, <>]",
1292
+ "```",
1293
+ "At a zero factor both recipes answer `Z`, a one-cell factor included",
1294
+ "```",
1295
+ "; [(U.size_mul Z) = Z, ((Figure [U]).size_mul Z) = Z, ((U.mul Z).size()) = ((U.size()).size_mul (Z.size()))]",
1296
+ "# [<>, <>, <>]",
1244
1297
  "; .deep2 (Figure [Figure [row2]]); .deep3 (Figure [Figure [Figure [row3]]]);",
1245
1298
  "; [row2.size_equivalent deep2, row3.size_equivalent deep3, (Figure [row2, row3]).size_equivalent (Figure [deep2, deep3]), (row2.mul row3).size_equivalent (deep2.mul deep3)] = [U, U, U, U]",
1246
1299
  "# <>",
@@ -1255,6 +1308,12 @@ export const HC_SOURCES = new Map([
1255
1308
  "# [(), <>]",
1256
1309
  "; [(Figure [Z, row2]).size_equivalent row2, (Figure [row2, Z]).size_equivalent row2, (U.mul row2).size_equivalent row2, (row2.mul U).size_equivalent row2] = [U, U, U, U]",
1257
1310
  "# <>",
1311
+ "```",
1312
+ "Read as natural numbers, Size takes construction to `+` and multiplication to",
1313
+ "`*`, so the same laws hold of FrameCount arithmetic, where they are native",
1314
+ "```",
1315
+ "; [((Figure [row2, row3]).cells()).## = (((row2.cells()).##) + ((row3.cells()).##)), (rectangle23.cells()).## = (((row2.cells()).##) * ((row3.cells()).##))]",
1316
+ "# [<>, <>]",
1258
1317
  "; [(Figure [Figure [row2, row3], row4]).size_equivalent (Figure [row2, Figure [row3, row4]]), (Figure [row2, row3]).size_equivalent (Figure [row3, row2])] = [U, U]",
1259
1318
  "# <>",
1260
1319
  "; (((row2.mul row3).mul row4).size_equivalent (row2.mul (row3.mul row4))) = U",
@@ -1311,6 +1370,12 @@ export const HC_SOURCES = new Map([
1311
1370
  "; point_table = (originals.children & {.p _; originals.children & {(p === _) ? {Three} : {Figure [U]}}})",
1312
1371
  "# <>",
1313
1372
  "```",
1373
+ "Read as natural numbers, the table counts three on the diagonal and one",
1374
+ "everywhere else",
1375
+ "```",
1376
+ "; (point_table & {_ & {_.children.#}}) = (originals.children & {.p _; originals.children & {(p === _) ? {3} : {1}}})",
1377
+ "# <>",
1378
+ "```",
1314
1379
  "Dually, `meet` counts the originals two triples share: multiplying their two",
1315
1380
  "Withness marks leaves one cell exactly when both hold it. The two tables are",
1316
1381
  "equal, which is the plane's self-duality",
@@ -1351,7 +1416,7 @@ export const HC_SOURCES = new Map([
1351
1416
  "the scope that built it. The Fano plane above already binds `f`, the name",
1352
1417
  "Withness uses inside its blocks; these bind the other locals",
1353
1418
  "```",
1354
- "; .kids 9; .y 9; .cells 9; .found 9; .absent 9;",
1419
+ "; .kids 9; .y 9; .units 9; .cells 9; .found 9; .absent 9;",
1355
1420
  "; [(E.withness [A, B]) = B, (row2.mul row3) = rectangle23, (E.size()) = (Figure [U])]",
1356
1421
  "# [<>, <>, <>]",
1357
1422
  "```",
@@ -1,4 +1,3 @@
1
- import { Frame } from "./frame.js";
2
1
  /**
3
2
  * NilContext is an empty context.
4
3
  */
@@ -31,7 +30,9 @@ export function contextEqual(left, right) {
31
30
  */
32
31
  export function contextString(context) {
33
32
  return Object.entries(context).map(([key, value]) => {
34
- if (key === Frame.kOUT) {
33
+ // `Frame.kOUT`, spelled out so this module imports Frame only as a type:
34
+ // a value import would evaluate frame.ts, which needs NilContext, first.
35
+ if (key === ">>") {
35
36
  return `.${key} ${value.id};`;
36
37
  }
37
38
  else {
@@ -1,3 +1,6 @@
1
+ // Load the FrameList cycle from FrameLazy, where it closes, so FrameList and
2
+ // FrameExpr exist before any subclass, whichever module is imported first.
3
+ import "./frame-lazy.js";
1
4
  import { Frame } from "./frame.js";
2
5
  import { BoundMethod } from "./bound-method.js";
3
6
  import { methodEffect } from "./effect-marker.js";
@@ -30,6 +30,10 @@ export class FrameCount extends FrameInt {
30
30
  throw new RangeError(`count must not be negative: ${this.spelling}`);
31
31
  }
32
32
  }
33
+ /** A count is its own size, so `.#` and `.##` both answer it (#411). */
34
+ sizeFor(_key) {
35
+ return this;
36
+ }
33
37
  promoteOne() {
34
38
  return new FrameInt(this.data);
35
39
  }
@@ -1,3 +1,6 @@
1
+ // Load the FrameList cycle from FrameLazy, where it closes, so FrameList and
2
+ // FrameExpr exist before any subclass, whichever module is imported first.
3
+ import "./frame-lazy.js";
1
4
  import { Frame } from "./frame.js";
2
5
  import { FrameList } from "./frame-list.js";
3
6
  import { NilContext } from "./context.js";
@@ -1,6 +1,7 @@
1
1
  import { Frame } from "./frame.js";
2
2
  import { NilContext } from "./context.js";
3
3
  import { EvaluationScope } from "./evaluation-scope.js";
4
+ import { FrameCount } from "./frame-count.js";
4
5
  import { renderNested } from "./stringify.js";
5
6
  /**
6
7
  * The declaration a term recorded, when it is a declaration echo.
@@ -105,6 +106,42 @@ export class FrameList extends Frame {
105
106
  elements() {
106
107
  return this.data.filter((item) => item.is.statement !== true && !this.holdsAsProperty(item));
107
108
  }
109
+ /**
110
+ * `.#` counts this aggregate's elements and `.##` the leaves below them
111
+ * (#411), so neither counts a property. Every aggregate is a list, so each
112
+ * one answers here rather than in its own class.
113
+ */
114
+ sizeFor(key) {
115
+ if (key === Frame.kCOUNT) {
116
+ return new FrameCount(BigInt(this.elements().length));
117
+ }
118
+ const leaves = this.leafCount(new Set());
119
+ return leaves === undefined
120
+ ? Frame.error(`$!.cyclic-aggregate .${key}`)
121
+ : new FrameCount(leaves);
122
+ }
123
+ /**
124
+ * The leaves below this aggregate: each element that is not itself an
125
+ * aggregate counts once, at any depth, so an empty aggregate adds none.
126
+ * `path` holds only the aggregates from the root down to this one, so a
127
+ * branch reached twice counts twice, and one that contains itself answers
128
+ * undefined.
129
+ */
130
+ leafCount(path) {
131
+ if (path.has(this))
132
+ return undefined;
133
+ path.add(this);
134
+ let leaves = 0n;
135
+ for (const element of this.elements()) {
136
+ const value = element.referent();
137
+ const below = value instanceof FrameList ? value.leafCount(path) : 1n;
138
+ if (below === undefined)
139
+ return undefined;
140
+ leaves += below;
141
+ }
142
+ path.delete(this);
143
+ return leaves;
144
+ }
108
145
  /**
109
146
  * The value a term answered, looking through a statement's wrapper.
110
147
  *
@@ -23,6 +23,15 @@ const recognizeName = (symbol, source = "") => {
23
23
  if (source.endsWith("^")) {
24
24
  return { disposition: ScanDisposition.CompleteRedispatch };
25
25
  }
26
+ // `#` begins a comment except where it spells a size operator, `.#` or `.##`
27
+ // (#411), which nothing extends: `.foo#bar` still ends at its `#`, and `.###`
28
+ // is `.##` before a comment.
29
+ if ([Frame.kCOUNT, Frame.kLEAVES].includes(source + char)) {
30
+ return { disposition: ScanDisposition.Consume };
31
+ }
32
+ if (source.startsWith(Frame.kCOUNT)) {
33
+ return { disposition: ScanDisposition.CompleteRedispatch };
34
+ }
26
35
  // `.^` needs no rule of its own: `^` is an operator character, so it is
27
36
  // consumed and completed like any other name.
28
37
  if (!includes(char)) {
@@ -1,3 +1,6 @@
1
+ // Load the FrameList cycle from FrameLazy, where it closes, so FrameList and
2
+ // FrameExpr exist before any subclass, whichever module is imported first.
3
+ import "./frame-lazy.js";
1
4
  import { Frame } from "./frame.js";
2
5
  import { FrameList } from "./frame-list.js";
3
6
  import { FrameNote } from "./frame-note.js";
@@ -260,6 +260,13 @@ export class FrameSymbol extends FrameAtom {
260
260
  });
261
261
  }
262
262
  called_by(context) {
263
+ // A size operator answers for the receiver itself rather than looking a
264
+ // name up, so the scope a value was read from never answers for it (#411).
265
+ // Only an address is one, since the lexer looks `#` up as the comment.
266
+ if (this.is.address === true &&
267
+ (this.data === Frame.kCOUNT || this.data === Frame.kLEAVES)) {
268
+ return context.referent().sizeFor(this.data);
269
+ }
263
270
  return this.in([context]);
264
271
  }
265
272
  string_start() {
@@ -210,6 +210,13 @@ export class Frame extends MetaFrame {
210
210
  referent() {
211
211
  return this;
212
212
  }
213
+ /**
214
+ * What this frame answers for a size operator, `.#` or `.##` (#411). Only an
215
+ * aggregate counts and a count is its own size, so anything else is nil.
216
+ */
217
+ sizeFor(_key) {
218
+ return Frame.nil;
219
+ }
213
220
  /**
214
221
  * Get a frame by key from this frame's own properties and its declared
215
222
  * parents only, never its lexical scope.
@@ -504,6 +511,24 @@ Object.defineProperty(Frame, "kEND", {
504
511
  writable: true,
505
512
  value: "$$"
506
513
  });
514
+ /**
515
+ * kCOUNT is the size operator `.#`, an aggregate's element count (#411).
516
+ */
517
+ Object.defineProperty(Frame, "kCOUNT", {
518
+ enumerable: true,
519
+ configurable: true,
520
+ writable: true,
521
+ value: "#"
522
+ });
523
+ /**
524
+ * kLEAVES is the size operator `.##`, an aggregate's leaf count (#411).
525
+ */
526
+ Object.defineProperty(Frame, "kLEAVES", {
527
+ enumerable: true,
528
+ configurable: true,
529
+ writable: true,
530
+ value: "##"
531
+ });
507
532
  /**
508
533
  * BEGIN_EXPR is the symbol for the beginning of an expression.
509
534
  */
@@ -1,3 +1,6 @@
1
+ // Load the FrameList cycle from FrameLazy, where it closes, so FrameList and
2
+ // FrameExpr exist before any subclass, whichever module is imported first.
3
+ import "./frame-lazy.js";
1
4
  import { Frame } from "./frame.js";
2
5
  import { FrameList } from "./frame-list.js";
3
6
  import { isFrameMatcher, matchFailure, matchSuccess, } from "./frame-match.js";
@@ -1,4 +1,5 @@
1
- import { Frame } from "../frames.js";
1
+ // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
+ import { Frame } from "../frames/frame.js";
2
3
  export const IfThen = (source, block, receiverState) => {
3
4
  if (source !== Frame.nil) {
4
5
  return block.call(Frame.nil, Frame.nil, receiverState);
@@ -1,4 +1,7 @@
1
- import { Frame, FrameArray, FrameSymbol } from "../frames.js";
1
+ // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
+ import { Frame } from "../frames/frame.js";
3
+ import { FrameArray } from "../frames/frame-array.js";
4
+ import { FrameSymbol } from "../frames/frame-symbol.js";
2
5
  /**
3
6
  * Iteration is application, twice.
4
7
  *
@@ -1,4 +1,6 @@
1
- import { Frame, FrameNumeric } from "../frames.js";
1
+ // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
+ import { Frame } from "../frames/frame.js";
3
+ import { FrameNumeric } from "../frames/frame-numeric.js";
2
4
  const numeric = (source, block, operation) => {
3
5
  return source instanceof FrameNumeric && block instanceof FrameNumeric
4
6
  ? operation(source, block)
@@ -1,4 +1,8 @@
1
- import { Frame, FrameLazy, FrameLiteral, isFrameMatcher } from "../frames.js";
1
+ // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
+ import { Frame } from "../frames/frame.js";
3
+ import { FrameLazy } from "../frames/frame-lazy.js";
4
+ import { FrameLiteral } from "../frames/frame-symbol.js";
5
+ import { isFrameMatcher } from "../frames/frame-match.js";
2
6
  /** Test whether a value belongs to a first-class type or schema. */
3
7
  export const HasType = (source, type) => {
4
8
  if (type === Frame.all)
@@ -1 +1 @@
1
- export const VERSION = "0.21.0";
1
+ export const VERSION = "0.21.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.21.0",
3
+ "version": "0.21.2",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",