hclang 0.21.2 → 0.23.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.
@@ -406,6 +406,8 @@ export const HC_SOURCES = new Map([
406
406
  "# 6",
407
407
  "; 2 “a”",
408
408
  "# “aa”",
409
+ "; 2 [1, 2]",
410
+ "# [1, 2, 1, 2]",
409
411
  "; [] 1",
410
412
  "# [1]",
411
413
  "; [1] 2",
@@ -559,6 +561,16 @@ export const HC_SOURCES = new Map([
559
561
  "; [1] 2",
560
562
  "# [1, 2]",
561
563
  "```",
564
+ "Integer repetition consults the same declaration. A count concatenates a plain",
565
+ "aggregate's elements, even where the scope binds `apply`, but repeats an",
566
+ "aggregate that declares `apply` by its rule: the first of the n steps answers",
567
+ "the receiver, and each later step applies the running answer to it, so here the",
568
+ "rule runs four times",
569
+ "```",
570
+ "; .Units {[.row _; .apply {Units (_^.row + 1)}]};",
571
+ "; [(5 (Units 0)).row, (5 [Units 0]).#]",
572
+ "# [4, 5]",
573
+ "```",
562
574
  "",
563
575
  "## An empty reduce answers its receiver; an empty map is empty",
564
576
  "",
@@ -984,6 +996,39 @@ export const HC_SOURCES = new Map([
984
996
  "; [(shelf.tick_ ()).count, shelf.count, (shelf.tick_ ()).item === shelf.item]",
985
997
  "# [1, 0, <>]",
986
998
  "",
999
+ "`A method reaches its receiver one caret out, because the scope a method is",
1000
+ "written in is its aggregate, and a call binds it to the aggregate it was found",
1001
+ "on. An inherited method therefore reads the descendant, and bare names, which",
1002
+ "read the argument first, are not needed to reach it.`",
1003
+ "; .Base [.x “base”; .get {_^.x}];",
1004
+ "; .Kid [.^ Base; .x “kid”];",
1005
+ "; [Base.get(), Kid.get()]",
1006
+ "# [“base”, “kid”]",
1007
+ "; .Pt [.n 1; .peek {[n, _^.n]}];",
1008
+ "; Pt.peek [.n 7]",
1009
+ "# [7, 1]",
1010
+ "",
1011
+ "`A functional copy is its own receiver, so its methods read the fields the copy",
1012
+ "holds rather than those of the value it was copied from.`",
1013
+ "; .Tally [.n 1; .read {_^.n}; .bump_ {@n (_^.n + 1);}];",
1014
+ "; .tally2 (Tally.bump_ ());",
1015
+ "; [tally2.read(), (tally2.bump_ ()).n, Tally.n]",
1016
+ "# [2, 3, 1]",
1017
+ "",
1018
+ "`The receiver keeps the authority its method was granted. A non-mutating method",
1019
+ "that calls a mutator through it gets a functional update, and a mutating method",
1020
+ "reached through an immutable handle writes only its own copy.`",
1021
+ "; .Ticker [.k 0; .bump_ {@k (k + 1);}; .sneak {(_^.bump_ ()).k}; .twice_ {_^.bump_ (); _^.bump_ ();}];",
1022
+ "; [Ticker.sneak(), (Ticker.twice_ ()).k, Ticker.k]",
1023
+ "# [1, 2, 0]",
1024
+ "",
1025
+ "`A block nested in a method counts its carets lexically, so the method's",
1026
+ "receiver is one caret further out.`",
1027
+ "; .Outer [.k 1; .ks {[1, 2] & {_^^.k}}];",
1028
+ "; .Inner [.^ Outer; .k 5];",
1029
+ "; [Outer.ks(), Inner.ks()]",
1030
+ "# [[1, 1], [5, 5]]",
1031
+ "",
987
1032
  ].join("\n"),
988
1033
  ],
989
1034
  [
@@ -1076,6 +1121,20 @@ export const HC_SOURCES = new Map([
1076
1121
  "`x.children.#` counts x's children. A Figure's own `.#` and `.##` are `0`,",
1077
1122
  "because its children are a property rather than its elements (#407, #411).",
1078
1123
  "",
1124
+ "Size, Multiplication and Tally multiplication count first and then construct.",
1125
+ "Each computes a count n natively, with Cells and the size operators, and builds",
1126
+ "`Figure (n [U])` or `Figure (n [y])`, with the guards for zero that their",
1127
+ "paragraphs give. Repeating an aggregate concatenates its elements and shares",
1128
+ "them, so `n [U]` is n units, each `U` itself. Before v0.22.0 Size joined its",
1129
+ "children's Sizes one unit at a time, so tallies added by joining. Tallies still",
1130
+ "are the natural numbers in unary, they still join, `ys | x`, and two of them",
1131
+ "add as the Size of a Figure holding both, `(Figure [x, y]).size()`. Size now",
1132
+ "trades the joining derivation for construction from a native count.",
1133
+ "Repetition holds at most 1,000,000 elements, so Size, Multiplication and Tally",
1134
+ "multiplication answer `$!.repetition-size 1000000` once their count passes",
1135
+ "that. The joining Size had no such ceiling, but its time grew quadratically",
1136
+ "with the cells.",
1137
+ "",
1079
1138
  "A method names its own Figure `_^`, because a bare name is looked up on the",
1080
1139
  "argument first (spec 7), and the argument is usually another Figure. For the",
1081
1140
  "same reason, a block inside a method reads only names the method bound",
@@ -1100,36 +1159,36 @@ export const HC_SOURCES = new Map([
1100
1159
  " .many {Figure (_^.children & {B})};",
1101
1160
  "```",
1102
1161
  "**Size**, `x.size()`: a tally with one unit cell `U` per cell. Any Figure shaped",
1103
- "like `U` is a cell; every other Figure adds up the Sizes of its children by",
1104
- "joining them into `Tally Z`. It keeps the cell count and forgets shape, nesting,",
1105
- "order, reuse and identity. The unit case comes first, because a unit's only",
1106
- "child is `Z`",
1162
+ "like `U` is a cell. Size counts them with Cells, below, and repeats `U` that",
1163
+ "many times, and a Figure with no cells answers `Z` itself. It keeps the cell",
1164
+ "count and forgets shape, nesting, order, reuse and identity",
1107
1165
  "```;",
1108
- " .size {.kids _^.children;",
1109
- " (kids = U.children) ? {Figure [U]} : {((kids & {_.size()}) | (Tally Z)).row}};",
1166
+ " .size {.n ((_^.cells()).##); (n = 0) ? {Z} : {Figure (n [U])}};",
1110
1167
  "```",
1111
1168
  "**Cells**, `x.cells()`: the bracket tree of x with `1` in place of each cell,",
1112
1169
  "so `(x.cells()).##` is the Size of x as a natural number, counted without",
1113
1170
  "joining a tally. A count is its own size, so the `1` of a unit is already its",
1114
1171
  "count. It keeps the cell count and where the cells are, and forgets identity.",
1115
- "The unit case comes first, as in Size",
1172
+ "The unit case comes first, because a unit's only child is `Z`",
1116
1173
  "```;",
1117
1174
  " .cells {.kids _^.children;",
1118
1175
  " (kids = U.children) ? {1} : {kids & {_.cells()}}};",
1119
1176
  "```",
1120
- "**Multiplication**, `x.mul y`: y once for every cell of x, and `Z` when y is",
1121
- "`Z`. It keeps y's identity in every position and forgets x's shape",
1177
+ "**Multiplication**, `x.mul y`: y once for every cell of x, counted as Size",
1178
+ "counts them, and `Z` when y is `Z`. Repetition shares what it repeats, so it",
1179
+ "keeps y's identity in every position, and it forgets x's shape",
1122
1180
  "```;",
1123
- " .mul {.y _; .units ((_^.size()).children);",
1124
- " (y = Z) ? {Z} : {Figure (units & {y})}};",
1181
+ " .mul {.y _; .n ((_^.cells()).##); (y = Z) ? {Z} : {Figure (n [y])}};",
1125
1182
  "```",
1126
1183
  "**Tally multiplication**, `x.size_mul y`: the Size of a Figure holding y once",
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",
1184
+ "per child of x. That is x's number of children times y's cells, so it is",
1185
+ "counted rather than built, and it is `Z` when the product is `0`, a y of `Z`",
1186
+ "included. Building the Figure would go wrong there, as Multiplication would",
1187
+ "without its guard: one row of nothing, `Figure [Z]`, has the form of `U` and",
1188
+ "would count as a cell. It keeps the cell count and forgets both shapes",
1130
1189
  "```;",
1131
- " .size_mul {.y _; .kids _^.children;",
1132
- " (y = Z) ? {Z} : {(Figure (kids & {y})).size()}};",
1190
+ " .size_mul {.n ((_^.children.#) * ((_.cells()).##));",
1191
+ " (n = 0) ? {Z} : {Figure (n [U])}};",
1133
1192
  "```",
1134
1193
  "**Size-equivalence**, `x.size_equivalent y`: `U` when the two Sizes agree and",
1135
1194
  "`Z` otherwise. It compares the two cell counts as natural numbers, which",
@@ -1172,12 +1231,6 @@ export const HC_SOURCES = new Map([
1172
1231
  " .toText {“” (_^.form())}",
1173
1232
  "]};",
1174
1233
  "```",
1175
- "**Tally** is a helper receiver, not a Figure. Its `row` starts at `Z`, and",
1176
- "applying a tally to it appends that tally's units, one at a time, through",
1177
- "`x y`, so a reduce into `Tally Z` adds counts",
1178
- "```",
1179
- ".Tally {[.row _; .apply {Tally (_.children | _^.row)}]};",
1180
- "```",
1181
1234
  "**The marks.** The observers answer `A`, the Figure with no children, for no",
1182
1235
  "and `B`, the Figure holding `A`, for yes. Size counts in the same two: `Z`",
1183
1236
  "reuses `A` as the additive zero and `U` reuses `B` as the unit cell. The class",
@@ -1217,6 +1270,13 @@ export const HC_SOURCES = new Map([
1217
1270
  "; ([A, B] & (Figure [C])) = [Figure [C, A], Figure [C, B]]",
1218
1271
  "# <>",
1219
1272
  "```",
1273
+ "Repetition applies a Figure rather than concatenating, because a Figure",
1274
+ "declares its application: `3 U` is `(U U) U`, and each child it appends is `U`",
1275
+ "itself",
1276
+ "```",
1277
+ "; [(3 U) = ((U U) U), ((3 U).children .0) === Z, ((3 U).children .2) === U]",
1278
+ "# [<>, <>, <>]",
1279
+ "```",
1220
1280
  "## Manyness and Size",
1221
1281
  "",
1222
1282
  "Manyness marks the immediate children and looks no deeper",
@@ -1225,9 +1285,9 @@ export const HC_SOURCES = new Map([
1225
1285
  "# [<>, <>, <>, ()]",
1226
1286
  "```",
1227
1287
  "Size is recursive cell volume. `Z` is `A` and `U` is `B`, the same mark",
1228
- "Manyness uses, so a count is itself a Figure of units. Size maps each child to its Size and joins them into",
1229
- "`Tally Z`; the unit case comes first because a unit's only child is `Z`, and it",
1230
- "tests form, so any Figure shaped like `U` is a cell",
1288
+ "Manyness uses, so a count is itself a Figure of units. Size counts the cells",
1289
+ "with Cells and repeats `U` that many times. Cells tests form, so any Figure",
1290
+ "shaped like `U` is a cell",
1231
1291
  "```",
1232
1292
  "; [Z === A, U === B]",
1233
1293
  "# [<>, <>]",
@@ -1239,14 +1299,33 @@ export const HC_SOURCES = new Map([
1239
1299
  "# [<>, <>, <>]",
1240
1300
  "; [(row4.many()) = (row4.size()), (deep4.many()) = (deep4.size()), (two_fours.many()) = row2]",
1241
1301
  "# [<>, (), <>]",
1242
- "; ((Tally Z).row) = Z",
1243
- "# <>",
1302
+ "```",
1303
+ "Tallies add without a helper: the Size of a Figure holding two tallies joins",
1304
+ "them, and two `Z`s add to `Z`. Add two at a time, because two children are",
1305
+ "never shaped like `U`. One child can be: `Figure [Z]` has the form of `U`, so",
1306
+ "its Size is one cell, the rule that makes Multiplication and Tally",
1307
+ "multiplication guard `Z`",
1308
+ "```",
1309
+ "; [((Figure [row2, row3]).size()) = (row3.children | row2), ((Figure [Z, Z]).size()) = Z, ((Figure [Z]).size()) = (Figure [U])]",
1310
+ "# [<>, <>, <>]",
1311
+ "```",
1312
+ "Size answers `Z` itself for a Figure with no cells, and every unit of a Size is",
1313
+ "`U` itself, because repetition shares what it repeats",
1314
+ "```",
1315
+ "; [(Z.size()) === Z, ((Figure [Z, Z]).size()) === Z, ((U.size()).children .0) === U]",
1316
+ "# [<>, <>, <>]",
1244
1317
  "```",
1245
1318
  "A tally is its own Size, so Size is idempotent",
1246
1319
  "```",
1247
1320
  "; ((two_fours.size()).size()) = (two_fours.size())",
1248
1321
  "# <>",
1249
1322
  "```",
1323
+ "Counting first keeps a large Size quick: 256 cells, built by Multiplication",
1324
+ "```",
1325
+ "; .row16 (row4.mul row4); .row256 (row16.mul row16);",
1326
+ "; [(row256.size()).children.#, ((row256.size()).children .255) === U]",
1327
+ "# [256, <>]",
1328
+ "```",
1250
1329
  "A tally reads as a natural number, because its children are an aggregate and",
1251
1330
  "`.#` counts them. A Figure's own `.#` and `.##` are `0`: its children are a",
1252
1331
  "property, not its elements, and `.##` counts only what is not an aggregate",
@@ -1406,7 +1485,8 @@ export const HC_SOURCES = new Map([
1406
1485
  "",
1407
1486
  "A bare name inside a block that a map applies to Figures reads each Figure's",
1408
1487
  "own members first (spec 7), so it cannot reach the method's. That is why a",
1409
- "method names its own Figure `_^` and binds `kids` before its blocks read it",
1488
+ "method names its own Figure `_^` and binds `kids` or `n` before its blocks read",
1489
+ "it",
1410
1490
  "```",
1411
1491
  "; (E.children & {children}) = [A.children, B.children]",
1412
1492
  "# <>",
@@ -1416,9 +1496,9 @@ export const HC_SOURCES = new Map([
1416
1496
  "the scope that built it. The Fano plane above already binds `f`, the name",
1417
1497
  "Withness uses inside its blocks; these bind the other locals",
1418
1498
  "```",
1419
- "; .kids 9; .y 9; .units 9; .cells 9; .found 9; .absent 9;",
1420
- "; [(E.withness [A, B]) = B, (row2.mul row3) = rectangle23, (E.size()) = (Figure [U])]",
1421
- "# [<>, <>, <>]",
1499
+ "; .kids 9; .y 9; .n 9; .cells 9; .found 9; .absent 9;",
1500
+ "; [(E.withness [A, B]) = B, (row2.mul row3) = rectangle23, (E.size()) = (Figure [U]), (row2.size_mul row3) = (rectangle23.size())]",
1501
+ "# [<>, <>, <>, <>]",
1422
1502
  "```",
1423
1503
  "A method's free names resolve where the method was written, never where its",
1424
1504
  "receiver was read (#400). So a scope that binds its own `B` and reads a Figure",
@@ -1671,8 +1751,8 @@ export const HC_SOURCES = new Map([
1671
1751
  "```",
1672
1752
  "Imported methods compose with Figures built in this file, each with",
1673
1753
  "`Figures.Figure`. Size counts the cells of a Figure sum and of a Figure product.",
1674
- "On tallies, joining is a reduce, `ys | x`. `size_mul` multiplies, and `Tally Z`",
1675
- "collects a reduce",
1754
+ "On tallies, joining is a reduce, `ys | x`, `size_mul` multiplies, and the Size",
1755
+ "of a Figure holding two tallies adds them",
1676
1756
  "```",
1677
1757
  "; .duo (Figures.Figure [Figures.U, Figures.U]);",
1678
1758
  "; .trio (Figures.Figure [Figures.U, Figures.U, Figures.U]);",
@@ -1681,7 +1761,7 @@ export const HC_SOURCES = new Map([
1681
1761
  "# [<>, <>, <>]",
1682
1762
  "; (trio.children | (Figures.Figure [Figures.U])) = (Figures.Figure [Figures.U, Figures.U, Figures.U, Figures.U])",
1683
1763
  "# <>",
1684
- "; (([Figures.Figure [Figures.U], Figures.Figure [Figures.U]] | (Figures.Tally Figures.Z)).row) = duo",
1764
+ "; ((Figures.Figure [Figures.Figure [Figures.U], Figures.Figure [Figures.U]]).size()) = duo",
1685
1765
  "# <>",
1686
1766
  "```",
1687
1767
  "Identity crosses the import boundary. Withness asks for immediate containment",
@@ -1801,6 +1881,25 @@ export const HC_SOURCES = new Map([
1801
1881
  "# “HelloHelloHello”",
1802
1882
  "",
1803
1883
  "```",
1884
+ "Repeating an aggregate concatenates its elements (v0.22.0). The answer is a",
1885
+ "fresh aggregate of the same kind holding the receiver's properties once and its",
1886
+ "elements n times, shared rather than copied, so the receiver is never answered",
1887
+ "or written and no element holds its own aggregate",
1888
+ "```",
1889
+ "; 3 [1, 2]",
1890
+ "# [1, 2, 1, 2, 1, 2]",
1891
+ "; 0 [1, 2]",
1892
+ "# []",
1893
+ "; 2 [.k 1; 4, 5]",
1894
+ "# [.k 1; 4, 5, 4, 5]",
1895
+ "; 2 (1, 2)",
1896
+ "# (1, 2, 1, 2)",
1897
+ "; .xs [1, 2];",
1898
+ "; [3 xs, xs]",
1899
+ "# [[1, 2, 1, 2, 1, 2], [1, 2]]",
1900
+ "; [(1 xs) = xs, (1 xs) === xs, ((3 [xs]) .2) === xs, (2 [[1, 2]]).##]",
1901
+ "# [<>, (), <>, 4]",
1902
+ "```",
1804
1903
  "Cross-rung promotion and integer-only modulo",
1805
1904
  "```",
1806
1905
  "; 1 + 2.5",
@@ -1838,6 +1937,20 @@ export const HC_SOURCES = new Map([
1838
1937
  "; 2 ** 1000000",
1839
1938
  "# $!.numeric-range ** FrameCount",
1840
1939
  "```",
1940
+ "An aggregate's repetition applies nothing, so the application bound leaves it",
1941
+ "alone. Its answer is bounded instead, at 1,000,000 elements, and refused with",
1942
+ "the error repeated text uses. A negative or fractional count fails as it does",
1943
+ "for text",
1944
+ "```",
1945
+ "; (65537 [1]).#",
1946
+ "# 65537",
1947
+ "; 1000001 [1]",
1948
+ "# $!.repetition-size 1000000",
1949
+ "; (0 - 1) [1]",
1950
+ "# $!.repetition-count -1",
1951
+ "; 3.14 [1]",
1952
+ "# $!.repetition-domain FrameDecimal",
1953
+ "```",
1841
1954
  "Counts (#407). Every nonnegative integer literal is a `FrameCount`, the root of",
1842
1955
  "the ladder, and prints as the same integer does. A sequence's refusal names the",
1843
1956
  "class of its other operand, which is how these examples observe the rung",
@@ -3177,7 +3290,9 @@ export const HC_SOURCES = new Map([
3177
3290
  "You can reach past the argument to the enclosing lexical scope (one level above)",
3178
3291
  "using the `_^` identifier. It is not a `super` reference: it names the scope the",
3179
3292
  "closure was written in, not a declared parent, and each caret is one more",
3180
- "lexical scope out regardless of how the closure was called.",
3293
+ "lexical scope out regardless of how the closure was called. A method is written",
3294
+ "in its aggregate, so in a method that scope is the receiver the call is bound",
3295
+ "to, which may be a copy or a descendant of the aggregate.",
3181
3296
  "```",
3182
3297
  "; .print-arg { var };",
3183
3298
  "; .print-parent { _^.var };",
@@ -236,10 +236,7 @@ export class EvaluationScope {
236
236
  const forwarded = receiverState !== undefined &&
237
237
  receiverState === enclosing?.receiverState;
238
238
  if (receiverState && !forwarded) {
239
- const receiverLookup = receiverState.lexicalContext
240
- ? new FrameHandle(receiverState.receiver, receiverState.mutable, receiverState.copyOnWrite, receiverState.lexicalContext)
241
- : receiverState.receiver;
242
- lookupFrames.push(receiverLookup);
239
+ lookupFrames.push(EvaluationScope.receiverLayer(receiverState));
243
240
  }
244
241
  if (closure)
245
242
  lookupFrames.push(closure);
@@ -388,11 +385,39 @@ export class EvaluationScope {
388
385
  lexicalAt(level) {
389
386
  if (level <= 0)
390
387
  return this.lexicalTarget;
391
- let scope = this.enclosing;
392
- for (let current = 1; current < level; current += 1) {
393
- scope = scope?.enclosing;
394
- }
395
- return scope?.lexicalTarget;
388
+ const inner = this.outward(level - 1);
389
+ // A method's body is written in its aggregate, and a call binds it to a
390
+ // receiver: the aggregate itself, an instance copy of it, or a declared
391
+ // descendant. The level that reaches the scope the method was written in
392
+ // reaches that receiver instead, so the caret count stays lexical while a
393
+ // copy or a descendant reads its own fields rather than its source's.
394
+ const receiverState = inner?.methodReceiverState();
395
+ if (receiverState)
396
+ return EvaluationScope.receiverLayer(receiverState);
397
+ return inner?.enclosing?.lexicalTarget;
398
+ }
399
+ /** The scope `steps` enclosing scopes out from this one. */
400
+ outward(steps) {
401
+ return steps <= 0 ? this : this.enclosing?.outward(steps - 1);
402
+ }
403
+ /**
404
+ * The receiver state this scope's call bound, when this scope is a method
405
+ * body rather than a block nested in one, which runs with the receiver state
406
+ * its method forwarded.
407
+ */
408
+ methodReceiverState() {
409
+ const state = this.receiverState;
410
+ return state && state !== this.enclosing?.receiverState ? state : undefined;
411
+ }
412
+ /**
413
+ * The receiver as a method call reads it: through a handle carrying the
414
+ * call's mutability and copy provenance when the read recorded a scope, so
415
+ * lookup through it reaches the receiver and its declared parents only.
416
+ */
417
+ static receiverLayer(state) {
418
+ return state.lexicalContext
419
+ ? new FrameHandle(state.receiver, state.mutable, state.copyOnWrite, state.lexicalContext)
420
+ : state.receiver;
396
421
  }
397
422
  /** Copies this scope's lookup layers with additions that are not present. */
398
423
  appended(...additions) {
@@ -59,6 +59,13 @@ export class FrameExpr extends FrameList {
59
59
  call(argument, parameter = Frame.nil) {
60
60
  return this.in(EvaluationScope.call(argument, parameter));
61
61
  }
62
+ /**
63
+ * An expression or a closure is called, not collected, so its terms are code
64
+ * rather than elements to concatenate: repetition applies it, as before.
65
+ */
66
+ repetitionFor(_count) {
67
+ return undefined;
68
+ }
62
69
  toStringDataArray() {
63
70
  const body = this.data
64
71
  .map((obj) => renderNested(obj, () => obj.toString()))
@@ -36,6 +36,14 @@ export class FrameGroup extends FrameList {
36
36
  this.data = this.data.map((frame) => frame.in(scope));
37
37
  return this;
38
38
  }
39
+ /**
40
+ * `()` is nil, so a repetition left with no term at all answers nil:
41
+ * `0 (1, 2)` is `()`, as the literal is. A group that holds properties keeps
42
+ * them, as every aggregate does, so `0 (.k 1; 4, 5)` holds `.k` alone.
43
+ */
44
+ finishRepetition() {
45
+ return this.size() === 0 ? Frame.nil : this;
46
+ }
39
47
  }
40
48
  Object.defineProperty(FrameGroup, "SIGIL_STARTS", {
41
49
  enumerable: true,
@@ -80,10 +80,16 @@ export class FrameInt extends FrameNumeric {
80
80
  ? count
81
81
  : Array.from({ length: count }, (_, index) => index);
82
82
  }
83
+ /** The refusal of a negative count, which every repetition answers first. */
84
+ negativeCount() {
85
+ return this.data < 0n
86
+ ? Frame.error(`$!.repetition-count ${this.spelling}`)
87
+ : undefined;
88
+ }
83
89
  repetitionCount() {
84
- if (this.data < 0n) {
85
- return Frame.error(`$!.repetition-count ${this.spelling}`);
86
- }
90
+ const negative = this.negativeCount();
91
+ if (negative)
92
+ return negative;
87
93
  if (this.data > REPETITION_LIMIT) {
88
94
  return Frame.error(`$!.repetition-limit ${REPETITION_LIMIT}`);
89
95
  }
@@ -170,6 +176,14 @@ export class FrameInt extends FrameNumeric {
170
176
  return this.data < value ? -1 : this.data > value ? 1 : 0;
171
177
  }
172
178
  repeat(argument, parameter) {
179
+ const negative = this.negativeCount();
180
+ if (negative)
181
+ return negative;
182
+ // An aggregate concatenates rather than applying itself (v0.22.0), and
183
+ // bounds the elements it answers rather than applications it performs.
184
+ const own = argument.referent().repetitionFor(this.data);
185
+ if (own !== undefined)
186
+ return own;
173
187
  const count = this.repetitionCount();
174
188
  if (count instanceof Frame)
175
189
  return count;
@@ -2,6 +2,7 @@ import { Frame } from "./frame.js";
2
2
  import { NilContext } from "./context.js";
3
3
  import { EvaluationScope } from "./evaluation-scope.js";
4
4
  import { FrameCount } from "./frame-count.js";
5
+ import { REPETITION_ELEMENT_LIMIT } from "./frame-numeric.js";
5
6
  import { renderNested } from "./stringify.js";
6
7
  /**
7
8
  * The declaration a term recorded, when it is a declaration echo.
@@ -104,7 +105,11 @@ export class FrameList extends Frame {
104
105
  * iterate.
105
106
  */
106
107
  elements() {
107
- return this.data.filter((item) => item.is.statement !== true && !this.holdsAsProperty(item));
108
+ return this.data.filter((item) => this.isElement(item));
109
+ }
110
+ /** Whether this term is one of the elements, as `elements()` reads them. */
111
+ isElement(term) {
112
+ return term.is.statement !== true && !this.holdsAsProperty(term);
108
113
  }
109
114
  /**
110
115
  * `.#` counts this aggregate's elements and `.##` the leaves below them
@@ -142,6 +147,55 @@ export class FrameList extends Frame {
142
147
  path.delete(this);
143
148
  return leaves;
144
149
  }
150
+ /**
151
+ * `n x` for an aggregate concatenates (v0.22.0). The answer is a fresh
152
+ * aggregate of x's kind holding the terms x keeps once, its properties and
153
+ * statements, followed by the terms it repeats, its elements, n times in
154
+ * order. The repeated terms are shared, as a collect shares what it
155
+ * collects, so each keeps its identity. x itself is never answered or
156
+ * written, so repeating a value builds no cycle. Properties stay once, as a
157
+ * reduce keeps only its receiver's: `[3, 4] | xs` concatenates into a copy
158
+ * of `xs`.
159
+ *
160
+ * An aggregate that declares its own application (#375) answers none, so
161
+ * repetition runs that application, as before. No application is performed
162
+ * here, so the answer is bounded by its size, at `REPETITION_ELEMENT_LIMIT`
163
+ * repeated terms, and refused with the error repeated text uses.
164
+ */
165
+ repetitionFor(count) {
166
+ if (this.applicationMethod() !== undefined)
167
+ return undefined;
168
+ const repeated = this.data.filter((term) => this.repeatsTerm(term));
169
+ if (BigInt(repeated.length) * count > REPETITION_ELEMENT_LIMIT) {
170
+ return Frame.error(`$!.repetition-size ${REPETITION_ELEMENT_LIMIT}`);
171
+ }
172
+ const result = this.copy();
173
+ const terms = result.data.filter((term) => !result.repeatsTerm(term));
174
+ // Within the limit, the loop runs at most that many times, and an
175
+ // aggregate with nothing to repeat skips it however large the count is.
176
+ const times = repeated.length === 0 ? 0 : Number(count);
177
+ for (let time = 0; time < times; time += 1) {
178
+ for (const term of repeated)
179
+ terms.push(term);
180
+ }
181
+ result.data = terms;
182
+ return result.finishRepetition();
183
+ }
184
+ /**
185
+ * Whether repetition repeats this term, rather than keeping it once. An
186
+ * aggregate repeats its elements and keeps its properties and statements
187
+ * once, as a reduce into it would.
188
+ */
189
+ repeatsTerm(term) {
190
+ return this.isElement(term);
191
+ }
192
+ /**
193
+ * What a repetition answers once its terms are in place: this aggregate,
194
+ * unless its kind has no value of that shape.
195
+ */
196
+ finishRepetition() {
197
+ return this;
198
+ }
145
199
  /**
146
200
  * The value a term answered, looking through a statement's wrapper.
147
201
  *
@@ -16,6 +16,8 @@ export const NUMERIC_RANK = {
16
16
  export const REPETITION_LIMIT = 65536n;
17
17
  /** The largest UTF-16 text result accepted by integer repetition. */
18
18
  export const REPETITION_TEXT_LIMIT = 1000000n;
19
+ /** The largest element count an aggregate's repetition may answer. */
20
+ export const REPETITION_ELEMENT_LIMIT = 1000000n;
19
21
  /** Maximum estimated bit width of an exact power result. */
20
22
  export const EXACT_POWER_BIT_LIMIT = 1000000n;
21
23
  /** Whether bigint exponentiation can stay within the exact-result budget. */
@@ -36,6 +36,32 @@ export class FrameSchema extends FrameList {
36
36
  const result = this.match(argument, parameter);
37
37
  return result.matched ? result.evidence : result.error;
38
38
  }
39
+ /**
40
+ * A schema's terms are all type terms, so repetition repeats every one, a
41
+ * statement too: `<.x; .y;>` and `<.x, .y>` are one type and repeat alike.
42
+ * A schema holds no properties, so only a held echo, were there one, would
43
+ * stay once.
44
+ */
45
+ repeatsTerm(term) {
46
+ return !this.holdsAsProperty(term);
47
+ }
48
+ /**
49
+ * A repeated schema matches what the literal with its terms written n times
50
+ * matches. With no term left that literal is `<>`, the total type. An
51
+ * enumeration matches by its terms, so it keeps its matcher. A compiled
52
+ * matcher was built from the terms, so it is rebuilt from the repeated ones;
53
+ * otherwise the schema would print and match as the one it repeated.
54
+ * `2 <.x, .y>` is `<.x, .y, .x, .y>`.
55
+ */
56
+ finishRepetition() {
57
+ if (this.size() === 0)
58
+ return Frame.all;
59
+ if (!(this.matcher instanceof EnumerationSchemaMatcher)) {
60
+ this.matcher = compileSchemaMatcher(this.data) ??
61
+ new EnumerationSchemaMatcher();
62
+ }
63
+ return this;
64
+ }
39
65
  match(value, origin = Frame.nil) {
40
66
  return this.matcher.match(this, value, origin);
41
67
  }
@@ -134,6 +134,14 @@ export class FrameSymbol extends FrameAtom {
134
134
  this.isReceiverPathValue(receiver, value, origin)) {
135
135
  return new BoundMethod(resolved, receiver, receiverState.mutable, methodEffect(this.data), receiverState.copyOnWrite, receiverState.lexicalContext);
136
136
  }
137
+ // A method read by dot from a value no name holds binds to that value
138
+ // as a name would, immutably: it may still be shared, so a mutating
139
+ // method answers a functional update rather than writing it.
140
+ if (resolved instanceof FrameLazy && this.is.address === true &&
141
+ !(context instanceof FrameHandle) && context.ownsIdentity() &&
142
+ this.isReceiverPathValue(context, value, origin)) {
143
+ return new BoundMethod(resolved, context, false, methodEffect(this.data));
144
+ }
137
145
  const copyOnWrite = context instanceof FrameHandle
138
146
  ? context.copyOnWriteScope() ?? receiverState?.copyOnWrite
139
147
  : receiverState?.copyOnWrite;
@@ -217,6 +217,16 @@ export class Frame extends MetaFrame {
217
217
  sizeFor(_key) {
218
218
  return Frame.nil;
219
219
  }
220
+ /**
221
+ * What integer repetition `n x` answers when this frame, x, has a rule of
222
+ * its own, or undefined when x is repeated by application: `FrameInt.repeat`
223
+ * then starts from nil and applies the running answer to x, n times. Only an
224
+ * aggregate has a rule (`FrameList`), so the base answers none. The caller
225
+ * has already refused a negative count.
226
+ */
227
+ repetitionFor(_count) {
228
+ return undefined;
229
+ }
220
230
  /**
221
231
  * Get a frame by key from this frame's own properties and its declared
222
232
  * parents only, never its lexical scope.
@@ -1 +1 @@
1
- export const VERSION = "0.21.2";
1
+ export const VERSION = "0.23.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.21.2",
3
+ "version": "0.23.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",