hclang 0.33.0 → 0.36.1

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.
@@ -228,7 +228,7 @@ export const HC_SOURCES = new Map([
228
228
  "",
229
229
  "The `<>` property retrieves the schema attached to the binding:",
230
230
  "```",
231
- "; enum123.<>",
231
+ "; enum123.<^>",
232
232
  "# <1, 2, 3>",
233
233
  "```",
234
234
  "",
@@ -693,16 +693,18 @@ export const HC_SOURCES = new Map([
693
693
  "`.#` and `.##` count the elements too, the immediate ones and the terminals",
694
694
  "below them, so neither counts a property. A terminal is an element that is not",
695
695
  "an aggregate, or an aggregate with no elements: the empty frame is one terminal,",
696
- "at the root or nested. Every aggregate counts this way, a group as well as an",
697
- "array. A count is its own size, and any other value has none (#411, issue",
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",
698
700
  "occurrence/figure 010.02)",
699
701
  "```",
700
702
  "; [.meta 1; 2, 3] .#",
701
703
  "# 2",
702
704
  "; [.meta [1, 2]; [3, []], 4] .##",
703
- "# 3",
705
+ "# 8",
704
706
  "; (1, [2, 3]) .##",
705
- "# 3",
707
+ "# 6",
706
708
  "; [[].#, [].##, [[], [[], []]].#, [[], [[], []]].##]",
707
709
  "# [0, 1, 2, 3]",
708
710
  "; 5.#",
@@ -1164,75 +1166,67 @@ export const HC_SOURCES = new Map([
1164
1166
  "```",
1165
1167
  "Figures are native frames (#454; issue occurrence/figure 010.02)",
1166
1168
  "",
1167
- "A Figure is a finite, property-free nested array built from `[]`. Construction",
1168
- "is brackets, form is the value itself, immediate size is `.#`, terminal count",
1169
+ "A Figure is a finite nested array built from `[]`, read through its elements",
1170
+ "only: properties and declared parents are ignored by observations. Star keeps",
1171
+ "nonterminal metadata when rebuilding frames. Construction is brackets, form is the value itself, immediate size is `.#`, terminal count",
1169
1172
  "is `.##`, form equality is `=`, and identity is `===`. The empty frame is one",
1170
- "terminal: `[].#` is 0 and `[].##` is 1.",
1173
+ "terminal: `[].#` is 0 and `[].##` is 1. The signed residual `.###` is",
1174
+ "`x₁.## − x₂.## + x₃.## − …` over the immediate elements, a FrameInt that is 0",
1175
+ "for `[]` (issue occurrence/figure 010.04).",
1171
1176
  "",
1172
1177
  "There are no Figure constructors, marks, tally observers, count aliases or",
1173
- "comparison wrappers. Use native operations directly. Multiplication that places",
1174
- "y once per terminal of x is `x.## [y]`. A tally, when wanted, is an explicit",
1178
+ "comparison wrappers. Use native operations directly. Star `x * y` places y at each terminal of x, preserving nesting.",
1179
+ "Flat repetition is `x.## [y]`. A tally, when wanted, is an explicit",
1175
1180
  "construction, such as `x & {[[]]}` for one fresh mark per immediate element.",
1176
- "Native `.##` also counts atoms in mixed trees; this module's traversals stay",
1177
- "within the Figure domain so reconstruction cannot silently discard properties.",
1181
+ "Native `.##` weights nested counts by their Size and other atoms by one;",
1182
+ "native `.###` answers nil on",
1183
+ "anything that is not a Figure, so this module's traversals stay in the domain.",
1178
1184
  "",
1179
- "Only operations that add a traversal or calculation remain: `inlay`, `withness`, `gap` and `spin`, with `pure` and `need` for domain validation.",
1185
+ "Only observation operations remain: `withness`, `gap` and `spin`, with `pure` and `need` for domain validation.",
1180
1186
  "The traversals validate at their public boundary; their recursive steps use",
1181
1187
  "native operations directly. Withness returns a native boolean, `<>` or `()`.",
1182
1188
  "```",
1183
1189
  "```",
1184
- "Supported domain: `pure x` accepts property-free nested arrays and rejects",
1185
- "atoms, strings and property-bearing frames.",
1190
+ "Supported domain: `pure x` accepts nested arrays, ignoring their properties",
1191
+ "and declared parents at every level, and rejects atoms, strings, groups, schemas and closures anywhere in them. It is",
1192
+ "the native `.###` check: a FrameInt, even 0, is true, and nil is false.",
1186
1193
  "```",
1187
- ".pure {.x _;",
1188
- " .go {.n _;",
1189
- " (n = []) ? {<>} : {",
1190
- " .rebuilt (n & {.e _; e});",
1191
- " (rebuilt = n) ? {(n &| {(go _) ? {[]} : {[0]}}) = []} : {()}}};",
1192
- " go x};",
1194
+ ".pure {.x _; (x.###) ? {<>} : {()}};",
1193
1195
  "```",
1194
1196
  "`need x` returns a supported frame itself, or a native refusal.",
1195
1197
  "```",
1196
1198
  ".need {.x _; (pure x) ? {x} : {unsupported-figure}};",
1197
1199
  "```",
1198
- "`inlay x y` replaces every terminal of x with y, retaining nonterminal nesting.",
1199
- "The replacement is reused by identity; ancestors are freshly constructed.",
1200
- "The host is validated once, and the replacement can be any native value.",
1201
- "```",
1202
- ".inlay {.x _; .host x;",
1203
- " (pure host) ? {",
1204
- " {.y _;",
1205
- " .go {.n _; (n = []) ? {y} : {n & {go _}}};",
1206
- " go host}} : {",
1207
- " {.y _; need host}}};",
1208
- "```",
1200
+ "Native Star `x * y` replaces empty-array terminals with y and composes",
1201
+ "FrameCount leaves using the same rule as a root count, including zero.",
1202
+ "Replacements are shared by identity; ancestors preserve native metadata.",
1203
+ "Terminals carrying metadata or statements are refused to avoid losing them.",
1204
+ "Use `*` directly; the Figures module provides no composition wrapper.",
1205
+ "",
1209
1206
  "`withness x [f, …]` asks whether every requested frame is an immediate element",
1210
1207
  "of x by native identity. It returns a native boolean. No requested frames is true.",
1211
1208
  "```",
1212
1209
  ".withness {.x _; .host x;",
1213
1210
  " {.fs _;",
1214
- " .absent (fs &| {.f _; ((host &| {(_ === f) ? {[f]}}) = []) ? {[f]}});",
1211
+ " .absent (fs &| {.f _; (f ~~~ host) ? {[]} : {[f]}});",
1215
1212
  " absent = []}};",
1216
1213
  "```",
1217
- "`gap x` is the absolute alternating sum of its immediate elements' `.##`",
1218
- "counts. The empty frame has Gap 0. The validated host needs no per-child guard.",
1214
+ "`gap x` is the absolute value of native `x.###`, the alternating sum of its",
1215
+ "immediate elements' `.##` counts, always a nonnegative FrameInt (Owner decision",
1216
+ "for #455). The empty frame has Gap 0 as a FrameInt too.",
1219
1217
  "```",
1220
1218
  ".gap {.x _; (pure x) ? {",
1221
- " .tail {(_ && {(((_ .0) = .0) ? {[()]} : {[[__ .1]]}) .0}) &| {_}};",
1222
- " .alt {.cs _; (cs = []) ? {0} : {(cs .0).## - (alt (tail cs))}};",
1223
- " .signed (alt x);",
1219
+ " .signed (x.###);",
1224
1220
  " (signed .< 0) ? {0 - signed} : {signed}} : {need x}};",
1225
1221
  "```",
1226
- "`spin x N` keeps the sign of the alternating sum and divides by a positive",
1227
- "count N. Non-count N is a type error; zero is division-by-zero. The host is",
1228
- "validated before the resolution is checked.",
1222
+ "`spin x N` keeps the sign of `x.###` and divides by a positive count N.",
1223
+ "Non-count N is a type error; zero is division-by-zero. The host is validated",
1224
+ "before the resolution is checked.",
1229
1225
  "```",
1230
1226
  ".spin {.x _; .host x;",
1231
1227
  " (pure host) ? {",
1232
- " {.n <~~1> _;",
1233
- " .tail {(_ && {(((_ .0) = .0) ? {[()]} : {[[__ .1]]}) .0}) &| {_}};",
1234
- " .alt {.cs _; (cs = []) ? {0} : {(cs .0).## - (alt (tail cs))}};",
1235
- " .signed (alt host);",
1228
+ " {.n <(1).<>> _;",
1229
+ " .signed (host.###);",
1236
1230
  " (n .< 1) ? {signed / 0} : {signed / n}}} : {",
1237
1231
  " {.n _; need host}}};",
1238
1232
  "```",
@@ -1249,31 +1243,43 @@ export const HC_SOURCES = new Map([
1249
1243
  "Native terminal counting needs no enumeration or tally construction.",
1250
1244
  "```",
1251
1245
  "; [(pure []), (pure [[], [[]]]), (pure [[], 5]), (pure [.k 1; []])]",
1252
- "# [<>, <>, (), ()]",
1246
+ "# [<>, <>, (), <>]",
1253
1247
  "; [(need e) = e, (need e) === e]",
1254
1248
  "# [<>, <>]",
1255
- "; inlay [.k 1; []] [[]]",
1256
- "# $!.name-missing “$:Frame.0.unsupported-figure”;",
1257
- "; inlay 5 [[]]",
1258
- "# $!.name-missing “$:Frame.0.unsupported-figure”;",
1259
- "; gap [.k 1; [[], []]]",
1249
+ "; [[], “a”] * [[]]",
1250
+ "# $!.star-domain *",
1251
+ "; (0 - 5) * [[]]",
1252
+ "# $!.star-domain *",
1253
+ "; gap ([], [[], []])",
1260
1254
  "# $!.name-missing “$:Frame.0.unsupported-figure”;",
1261
1255
  "```",
1262
- "Inlay preserves nesting and empty replacements; native repetition constructs a",
1256
+ "Properties and declared parents never decide the observer domain or an observation.",
1257
+ "Star preserves metadata on rebuilt nonterminal ancestors and leaves its host",
1258
+ "unchanged.",
1259
+ "```",
1260
+ "; [(gap [.k 1; [[], []]]) = 2, [.k 1; [], [[], []]].### = -1]",
1261
+ "# [<>, <>]",
1262
+ "; ([.k 1; [], [.j 2; []]] * [[]]) = [.k 1; [[]], [.j 2; [[]]]]",
1263
+ "# <>",
1264
+ "; .p [.k 1; []]; .q (p * [[]]);",
1265
+ "; [q = [.k 1; [[]]], p.k, p = [.k 1; []]]",
1266
+ "# [<>, 1, <>]",
1267
+ "```",
1268
+ "Star preserves nesting and empty replacements; native repetition constructs a",
1263
1269
  "flat product. Both have the same terminal count on Figures.",
1264
1270
  "```",
1265
- "; [(inlay [] [[], []]) = [[], []], (inlay [[]] [[], []]) = [[[], []]]]",
1271
+ "; [([] * [[], []]) = [[], []], ([[]] * [[], []]) = [[[], []]]]",
1266
1272
  "# [<>, <>]",
1267
- "; [(inlay [[], []] []) = [[], []], (inlay [[]] []) = [[]]]",
1273
+ "; [([[], []] * []) = [[], []], ([[]] * []) = [[]]]",
1268
1274
  "# [<>, <>]",
1269
- "; (inlay [[], [], [[], []]] “x”) = [“x”, “x”, [“x”, “x”]]",
1275
+ "; ([[], [], [[], []]] * “x”) = [“x”, “x”, [“x”, “x”]]",
1270
1276
  "# <>",
1271
- "; .y [[], [], []]; .host [[], [[], []]]; .r (inlay host y);",
1277
+ "; .y [[], [], []]; .host [[], [[], []]]; .r (host * y);",
1272
1278
  "; [((r .0) === y), (((r .1) .0) === y), (((r .1) .1) === y), r === host]",
1273
1279
  "# [<>, <>, <>, ()]",
1274
- "; [(inlay [[], []] y).##, ([[], []].## [y]).##]",
1280
+ "; [([[], []] * y).##, ([[], []].## [y]).##]",
1275
1281
  "# [6, 6]",
1276
- "; [([[[], []]].## [[[]]]) = [[[]], [[]]], (inlay [[[], []]] [[]]) = [[[[]], [[]]]]]",
1282
+ "; [([[[], []]].## [[[]]]) = [[[]], [[]]], ([[[], []]] * [[]]) = [[[[]], [[]]]]]",
1277
1283
  "# [<>, <>]",
1278
1284
  "```",
1279
1285
  "Withness uses identity, and its answer is the native boolean.",
@@ -1284,7 +1290,22 @@ export const HC_SOURCES = new Map([
1284
1290
  "; [(withness [] [[[]]]), (withness [shared] [])]",
1285
1291
  "# [(), <>]",
1286
1292
  "```",
1287
- "Gap and Spin read native terminal counts, including shared branches at every",
1293
+ "Native `.###` folds the immediate elements' terminal counts with alternating",
1294
+ "signs, one sign per element, and answers a FrameInt even when it is 0 or",
1295
+ "positive. Anything that is not a Figure answers nil.",
1296
+ "```",
1297
+ "; [[].###, [[]].###, [[], []].###, [[], [[], []]].###, [[[], []], []].###]",
1298
+ "# [0, 1, 0, -1, 1]",
1299
+ "; [[], [[], []], [[], [], []]].###",
1300
+ "# 2",
1301
+ "; [([].###) == (0 - 0), ([[]].###) == (1 - 0), ([[]].###) == 1]",
1302
+ "# [<>, <>, ()]",
1303
+ "; [[[], [[]]], [[[], []], []], [[[[], [], []]]]].###",
1304
+ "# 2",
1305
+ "; [[1, []].###, [[], “ab”].###, [[], ([], [])].###, 5.###]",
1306
+ "# [(), (), (), ()]",
1307
+ "```",
1308
+ "Gap is `|x.###|` and Spin is `x.### / N`, including shared branches at every",
1288
1309
  "occurrence. Reversing two unequal branches reverses Spin's sign.",
1289
1310
  "```",
1290
1311
  "; [(gap []) = 0, (gap [[[], []]]) = 2, (gap [[[], []], [[], [], []]]) = 1]",
@@ -1295,10 +1316,25 @@ export const HC_SOURCES = new Map([
1295
1316
  "# [<>, <>]",
1296
1317
  "; [(spin [[[], []]] 1) = 2, (spin [] 4) = 0]",
1297
1318
  "# [<>, <>]",
1319
+ "; .z [[[], []], [[], [], [], [], []]];",
1320
+ "; [z.###, (gap z) = (0 - z.###), (spin z 4) = (z.### / 4)]",
1321
+ "# [-3, <>, <>]",
1298
1322
  "; spin [[[], []]] 2.5",
1299
- "# $!.type-error .n <~~1> 2.5",
1323
+ "# $!.type-error .n <(1).<>> 2.5",
1300
1324
  "; spin [[[], []]] 0",
1301
1325
  "# $!.division-by-zero /",
1326
+ "",
1327
+ "```",
1328
+ "Star uses native `*` dispatch; numeric pairs keep arithmetic.",
1329
+ "```",
1330
+ "; [2 * 3, 2 * [[], []], [[], []] * 3, [] * [[], []]]",
1331
+ "# [6, [[[], []], [[], []]], [3, 3], [[], []]]",
1332
+ "; [(2 * [[], [], []]).##, ([[], []] * 3).##, (0 * [[]]).##]",
1333
+ "# [6, 6, 0]",
1334
+ "; [[], [[], []]] * [[]]",
1335
+ "# [[[]], [[[]], [[]]]]",
1336
+ "; [.tag 1;] * [[]]",
1337
+ "# $!.star-metadata *",
1302
1338
  "```",
1303
1339
  "Importing skips the examples and ends on a native true value.",
1304
1340
  "```",
@@ -1463,6 +1499,60 @@ export const HC_SOURCES = new Map([
1463
1499
  "",
1464
1500
  ].join("\n"),
1465
1501
  ],
1502
+ [
1503
+ "hc/membership.hc",
1504
+ [
1505
+ "#!/usr/bin/env hc",
1506
+ "```",
1507
+ "On ordinary element hosts, immediate membership follows the equality ladder: ~ uses =, ~~ uses ==,",
1508
+ "and ~~~ uses ===. The host is on the right. No descendant search or metadata.",
1509
+ "On schema/runtime matcher hosts, ~ retains rich matcher acceptance.",
1510
+ "```",
1511
+ "; [] ~ [[]]",
1512
+ "# <>",
1513
+ "; [] ~~ [[]]",
1514
+ "# <>",
1515
+ "; [] ~~~ [[]]",
1516
+ "# ()",
1517
+ "; 2 ~ [5 - 3]",
1518
+ "# <>",
1519
+ "; 2 ~~ [5 - 3]",
1520
+ "# ()",
1521
+ "; [] ~ [[[]]]",
1522
+ "# ()",
1523
+ "; 1 ~ [.property 1;]",
1524
+ "# ()",
1525
+ "```",
1526
+ "Fano incidence uses shared point objects. Equal form alone is not incidence.",
1527
+ "```",
1528
+ ".point [];",
1529
+ ".other [];",
1530
+ ".line [point];",
1531
+ "; [other ~ line, other ~~ line, other ~~~ line, point ~~~ line]",
1532
+ "# [<>, <>, (), <>]",
1533
+ "; point ~~~ line",
1534
+ "# <>",
1535
+ "; other ~~~ line",
1536
+ "# ()",
1537
+ "```",
1538
+ "Runtime types use .<>; declared binding schemas use .<^>.",
1539
+ "Schema matching with ~ retains its nested contract.",
1540
+ "```",
1541
+ "; “Q” ~ “”.<>",
1542
+ "# <>",
1543
+ "; “Q” ~ 1.<>",
1544
+ "# ()",
1545
+ "; “”.<> “Q”",
1546
+ "# “Q”",
1547
+ "; {.f {missing}; “x” ~ f.<>} ()",
1548
+ "# ()",
1549
+ "; {.x <1, 2> 1; x.<^>} ()",
1550
+ "# <1, 2>",
1551
+ "; 0b101 ~ <3@Bit>",
1552
+ "# <>",
1553
+ "",
1554
+ ].join("\n"),
1555
+ ],
1466
1556
  [
1467
1557
  "hc/numerics.hc",
1468
1558
  [
@@ -1574,7 +1664,7 @@ export const HC_SOURCES = new Map([
1574
1664
  "; [3 xs, xs]",
1575
1665
  "# [[1, 2, 1, 2, 1, 2], [1, 2]]",
1576
1666
  "; [(1 xs) = xs, (1 xs) === xs, ((3 [xs]) .2) === xs, (2 [[1, 2]]).##]",
1577
- "# [<>, (), <>, 4]",
1667
+ "# [<>, (), <>, 6]",
1578
1668
  "```",
1579
1669
  "Cross-rung promotion and integer-only modulo",
1580
1670
  "```",
@@ -1673,7 +1763,7 @@ export const HC_SOURCES = new Map([
1673
1763
  "# -2",
1674
1764
  "```",
1675
1765
  "Comparison and `=` compare values across rungs, so a count and a FrameInt of the",
1676
- "same value are `=`. `==`, `===` and `~~` compare exact types, so the two are",
1766
+ "same value are `=`. `==`, `===` and runtime types from `.<>` compare exact types, so the two are",
1677
1767
  "not `==`, just as `2` and `2.0` are not",
1678
1768
  "```",
1679
1769
  "; 2 = (5 - 3)",
@@ -1688,10 +1778,10 @@ export const HC_SOURCES = new Map([
1688
1778
  "# ()",
1689
1779
  "; 2 == 2.0",
1690
1780
  "# ()",
1691
- "; ~~2 (5 - 3)",
1692
- "# $!.type-error ~~2 2",
1693
- "; ~~(5 - 3) 2",
1694
- "# $!.type-error ~~2 2",
1781
+ "; (2).<> (5 - 3)",
1782
+ "# $!.type-error (2).<> 2",
1783
+ "; (5 - 3).<> 2",
1784
+ "# $!.type-error (2).<> 2",
1695
1785
  "; (0 - 1).< 0",
1696
1786
  "# <>",
1697
1787
  "; 2.<= (5 - 3)",
@@ -1699,15 +1789,15 @@ export const HC_SOURCES = new Map([
1699
1789
  "; 2.< 2.5",
1700
1790
  "# <>",
1701
1791
  "```",
1702
- "Within one rung, `==` and `~~` hold as they always have",
1792
+ "Within one rung, `==` and type application hold as they always have",
1703
1793
  "```",
1704
1794
  "; 2 == (1 + 1)",
1705
1795
  "# <>",
1706
1796
  "; (5 - 3) == (4 - 2)",
1707
1797
  "# <>",
1708
- "; ~~2 3",
1798
+ "; (2).<> 3",
1709
1799
  "# 3",
1710
- "; ~~(0 - 1) (0 - 5)",
1800
+ "; (0 - 1).<> (0 - 5)",
1711
1801
  "# -5",
1712
1802
  "",
1713
1803
  ].join("\n"),
@@ -1976,9 +2066,9 @@ export const HC_SOURCES = new Map([
1976
2066
  "# <>",
1977
2067
  "; 2 ~ ()",
1978
2068
  "# ()",
1979
- "; “Q” ~ ~~“”",
2069
+ "; “Q” ~ (“”).<>",
1980
2070
  "# <>",
1981
- "; “Q” ~ ~~1",
2071
+ "; “Q” ~ (1).<>",
1982
2072
  "# ()",
1983
2073
  "## Closure signatures apply defaults, require bare fields, and allow extras",
1984
2074
  "; .join-name (.first “Jane”, .last) ^ {last “, ” first};",
@@ -3108,12 +3198,12 @@ export const HC_SOURCES = new Map([
3108
3198
  "",
3109
3199
  "#### Type Values",
3110
3200
  "",
3111
- "The simplest way to construct a type is to use the `~~` operator to extract the type",
3201
+ "The simplest way to construct a type is to use the `.<>` accessor to extract the runtime type",
3112
3202
  "of a known object:",
3113
3203
  "```",
3114
- "; “Q” ~ ~~“”",
3204
+ "; “Q” ~ (“”).<>",
3115
3205
  "# <>",
3116
- "; “Q” ~ ~~1",
3206
+ "; “Q” ~ (1).<>",
3117
3207
  "# ()",
3118
3208
  "```",
3119
3209
  "### Content",
@@ -5,6 +5,8 @@ import { Frame } from "./frame.js";
5
5
  import { BoundMethod } from "./bound-method.js";
6
6
  import { methodEffect } from "./effect-marker.js";
7
7
  import { FrameExpr } from "./frame-expr.js";
8
+ import { FrameCount } from "./frame-count.js";
9
+ import { FrameNumeric, REPETITION_LIMIT } from "./frame-numeric.js";
8
10
  import { FrameHandle } from "./frame-handle.js";
9
11
  import { FrameList } from "./frame-list.js";
10
12
  import { FrameNote } from "./frame-note.js";
@@ -13,6 +15,79 @@ export class FrameArray extends FrameList {
13
15
  constructor(data, meta = NilContext) {
14
16
  super(data, meta);
15
17
  }
18
+ /** Count leaves use the same Star rule at the root and inside an array. */
19
+ static starCount(count, replacement) {
20
+ const right = replacement.referent();
21
+ if (right instanceof FrameNumeric)
22
+ return count.multiply(right);
23
+ const refusal = FrameArray.starCountRefusal(count, replacement);
24
+ if (refusal)
25
+ return refusal;
26
+ if (count.data === 0n)
27
+ return count;
28
+ return new FrameArray(Array(Number(count.data)).fill(replacement));
29
+ }
30
+ /** Validate expansion before any ancestor is rebuilt or count is expanded. */
31
+ static starCountRefusal(count, replacement) {
32
+ if (replacement.referent() instanceof FrameNumeric)
33
+ return undefined;
34
+ if (count.meta_pairs().length || count.hasDeclaredParent()) {
35
+ return Frame.error("$!.star-metadata *");
36
+ }
37
+ if (count.data > REPETITION_LIMIT) {
38
+ return Frame.error(`$!.repetition-limit ${REPETITION_LIMIT}`);
39
+ }
40
+ return undefined;
41
+ }
42
+ /**
43
+ * Star: replace empty-array terminals and compose count leaves,
44
+ * sharing the replacement.
45
+ * Copy the same native metadata and declaration echoes as ordinary array
46
+ * reconstruction. A decorated terminal cannot be replaced without loss.
47
+ * Validate before rebuilding; neither operand is evaluated or mutated here.
48
+ */
49
+ star(replacement) {
50
+ const path = new Set();
51
+ const validate = (node) => {
52
+ const value = node.referent();
53
+ if (value instanceof FrameCount) {
54
+ return FrameArray.starCountRefusal(value, replacement);
55
+ }
56
+ if (!(value instanceof FrameArray) || path.has(value)) {
57
+ return Frame.error("$!.star-domain *");
58
+ }
59
+ const elements = value.elements();
60
+ if (!elements.length &&
61
+ (value.asArray().length || value.meta_pairs().length ||
62
+ value.hasDeclaredParent())) {
63
+ return Frame.error("$!.star-metadata *");
64
+ }
65
+ path.add(value);
66
+ for (const element of elements) {
67
+ const error = validate(element);
68
+ if (error)
69
+ return error;
70
+ }
71
+ path.delete(value);
72
+ return undefined;
73
+ };
74
+ const error = validate(this);
75
+ if (error)
76
+ return error;
77
+ const go = (node) => {
78
+ if (node instanceof FrameCount) {
79
+ return FrameArray.starCount(node, replacement);
80
+ }
81
+ if (!node.elements().length)
82
+ return replacement;
83
+ const result = node.copy();
84
+ result.data = node.itemsFor(result, (item) => node.repeatsTerm(item)
85
+ ? go(item.walked().referent())
86
+ : item);
87
+ return result;
88
+ };
89
+ return go(this);
90
+ }
16
91
  string_open() {
17
92
  return FrameArray.BEGIN_ARRAY;
18
93
  }
@@ -62,6 +137,13 @@ export class FrameArray extends FrameList {
62
137
  ownsIdentity() {
63
138
  return true;
64
139
  }
140
+ /**
141
+ * Every array is a Figure node, read through its elements; its properties
142
+ * and declared parent are ignored (010.04).
143
+ */
144
+ isFigureNode() {
145
+ return true;
146
+ }
65
147
  /**
66
148
  * An immutable handle on this aggregate, as a name reads it: applying it
67
149
  * collects onto a copy and its mutating methods answer functional updates.
@@ -30,9 +30,14 @@ 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;
33
+ /**
34
+ * A count is its own size, so `.#` and `.##` both answer it (#411). A count
35
+ * is not a Figure, so `.###` answers nil, as for any atom (010.04). The
36
+ * spelling is the literal `Frame.kRESIDUAL`, since this module imports
37
+ * `Frame` as a type only.
38
+ */
39
+ sizeFor(key) {
40
+ return key === "###" ? super.sizeFor(key) : this;
36
41
  }
37
42
  promoteOne() {
38
43
  return new FrameInt(this.data);
@@ -120,7 +120,7 @@ export class FrameExpr extends FrameList {
120
120
  }
121
121
  schemaRetrieval(scope) {
122
122
  if (this.data.length !== 2 || !(this.data[0] instanceof FrameSymbol) ||
123
- !(this.data[1] instanceof FrameName) || this.data[1].source !== "<>")
123
+ !(this.data[1] instanceof FrameName) || this.data[1].source !== "<^>")
124
124
  return undefined;
125
125
  return this.data[0].bindingSchema(resumedScope(this.data[0], scope));
126
126
  }
@@ -387,11 +387,10 @@ export class FrameScoped extends Frame {
387
387
  sizeFor(key) {
388
388
  return this.term.sizeFor(key);
389
389
  }
390
- /** Counts as its term walks, `.#` and `.##`; anything else is applied. */
390
+ /** Sizes as its term does, `.#`, `.##` and `.###`; anything else is applied. */
391
391
  call(argument, parameter = Frame.nil) {
392
392
  if (argument instanceof FrameSymbol && argument.is.address === true &&
393
- (argument.spelling() === Frame.kCOUNT ||
394
- argument.spelling() === Frame.kLEAVES)) {
393
+ Frame.SIZE_OPERATORS.includes(argument.spelling())) {
395
394
  return this.sizeFor(argument.spelling());
396
395
  }
397
396
  return this.live().call(argument, parameter);
@@ -1,4 +1,5 @@
1
1
  import { Frame } from "./frame.js";
2
+ import { FrameType } from "./frame-type.js";
2
3
  import { atomsOf, FrameExpr, noteEnclosing, sitesAround, } from "./frame-expr.js";
3
4
  import { FrameGroup } from "./frame-group.js";
4
5
  import { FrameSymbol } from "./frame-symbol.js";
@@ -94,14 +95,14 @@ export class FrameLazy extends FrameExpr {
94
95
  /**
95
96
  * A closure walks as the atoms of its body (#429), so `.#` counts them and
96
97
  * `.##` the terminals below them, as for any aggregate (#411); an empty
97
- * body is one terminal. The operator
98
+ * body is one terminal. A closure is not a Figure, so `.###` is nil
99
+ * (010.04). The operator
98
100
  * reaches a closure as its argument, where a body would otherwise run, so
99
101
  * it is answered here before the body is.
100
102
  */
101
103
  sizeOperator(argument) {
102
104
  if (argument instanceof FrameSymbol && argument.is.address === true &&
103
- (argument.spelling() === Frame.kCOUNT ||
104
- argument.spelling() === Frame.kLEAVES)) {
105
+ Frame.SIZE_OPERATORS.includes(argument.spelling())) {
105
106
  return this.sizeFor(argument.spelling());
106
107
  }
107
108
  return undefined;
@@ -124,6 +125,9 @@ export class FrameLazy extends FrameExpr {
124
125
  }
125
126
  /** Evaluates this closure with optional bound receiver capability. */
126
127
  call(argument, parameter = Frame.nil, receiverState) {
128
+ if (argument instanceof FrameSymbol && argument.is.address === true &&
129
+ argument.spelling() === "<>")
130
+ return FrameType.of(this);
127
131
  const size = this.sizeOperator(argument);
128
132
  if (size)
129
133
  return size;
@@ -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 { FrameInt } from "./frame-int.js";
5
6
  import { REPETITION_ELEMENT_LIMIT } from "./frame-numeric.js";
6
7
  import { renderNested } from "./stringify.js";
7
8
  /**
@@ -138,13 +139,15 @@ export class FrameList extends Frame {
138
139
  /**
139
140
  * `.#` counts this aggregate's elements (#411) and `.##` the terminals of
140
141
  * its structure (issue occurrence/figure 010.02), so neither counts a
141
- * property. Every aggregate is a list, so each one answers here rather than
142
- * in its own class.
142
+ * property. `.###` is a Figure's signed residual (010.04). Every aggregate
143
+ * is a list, so each one answers here rather than in its own class.
143
144
  */
144
145
  sizeFor(key) {
145
146
  if (key === Frame.kCOUNT) {
146
147
  return new FrameCount(BigInt(this.countedElements().length));
147
148
  }
149
+ if (key === Frame.kRESIDUAL)
150
+ return this.residual();
148
151
  const terminals = this.terminalCount(new Set());
149
152
  return terminals === undefined
150
153
  ? Frame.error(`$!.cyclic-aggregate .${key}`)
@@ -153,9 +156,9 @@ export class FrameList extends Frame {
153
156
  /**
154
157
  * The terminals of this aggregate's structure, the nodes with no elements
155
158
  * below them. An aggregate with no elements is itself one terminal, at the
156
- * root or nested, so `[]` counts 1 and `.##` never answers 0 for an
157
- * aggregate. A nonempty aggregate counts its elements' terminals: each
158
- * element that is not an aggregate is one terminal, and each aggregate
159
+ * root or nested, so `[]` counts 1. A nonempty aggregate counts its elements' terminals: each
160
+ * count element contributes its count (including zero); another atom is one
161
+ * terminal, and each aggregate
159
162
  * element counts its own, at any depth. Properties are not elements, so an
160
163
  * aggregate holding only properties is one terminal too.
161
164
  *
@@ -177,7 +180,11 @@ export class FrameList extends Frame {
177
180
  let terminals = 0n;
178
181
  for (const element of elements) {
179
182
  const value = element.referent();
180
- const below = value instanceof FrameList ? value.terminalCount(path) : 1n;
183
+ const below = value instanceof FrameList
184
+ ? value.terminalCount(path)
185
+ : value instanceof FrameCount
186
+ ? value.data
187
+ : 1n;
181
188
  if (below === undefined)
182
189
  return undefined;
183
190
  terminals += below;
@@ -185,6 +192,68 @@ export class FrameList extends Frame {
185
192
  path.delete(this);
186
193
  return terminals;
187
194
  }
195
+ /**
196
+ * Whether this aggregate can be a node of a Figure, a finite nested array
197
+ * (010.04). Only an array can, so groups, schemas and closures answer false
198
+ * here. Properties are not elements, so they never decide it.
199
+ */
200
+ isFigureNode() {
201
+ return false;
202
+ }
203
+ /**
204
+ * `.###`, the signed alternating sum of the terminal counts of a Figure's
205
+ * immediate elements, `T(x₁) − T(x₂) + T(x₃) − …`, always a FrameInt and 0
206
+ * for `[]` (issue occurrence/figure 010.04). A terminal is an empty frame,
207
+ * so on a Figure `T` is `.##`. Each element is folded with one sign; the
208
+ * signs do not alternate over the terminals below it.
209
+ *
210
+ * A Figure is read through its elements only, so properties and a declared
211
+ * parent are ignored at every level, as `.#` and `.##` ignore them. Anything
212
+ * else is not a Figure and answers nil, as `.#` does for a non-aggregate:
213
+ * an atom, a string, a group, a schema or a closure anywhere in the
214
+ * structure, or an aggregate that contains itself.
215
+ */
216
+ residual() {
217
+ if (!this.isFigureNode())
218
+ return Frame.nil;
219
+ const path = new Set([this]);
220
+ let signed = 0n;
221
+ let positive = true;
222
+ for (const element of this.countedElements()) {
223
+ const terminals = FrameList.figureTerminals(element.referent(), path);
224
+ if (terminals === undefined)
225
+ return Frame.nil;
226
+ signed += positive ? terminals : -terminals;
227
+ positive = !positive;
228
+ }
229
+ return new FrameInt(signed);
230
+ }
231
+ /**
232
+ * The terminal count of `value` as a Figure: 1 for an empty frame, else the
233
+ * sum over its elements. Answers undefined when any node is not a Figure
234
+ * node or contains itself. `path` holds the nodes from the root down, as
235
+ * for `terminalCount`, so a shared branch counts at every occurrence.
236
+ */
237
+ static figureTerminals(value, path) {
238
+ if (!(value instanceof FrameList) || !value.isFigureNode()) {
239
+ return undefined;
240
+ }
241
+ if (path.has(value))
242
+ return undefined;
243
+ const elements = value.countedElements();
244
+ if (elements.length === 0)
245
+ return 1n;
246
+ path.add(value);
247
+ let terminals = 0n;
248
+ for (const element of elements) {
249
+ const below = FrameList.figureTerminals(element.referent(), path);
250
+ if (below === undefined)
251
+ return undefined;
252
+ terminals += below;
253
+ }
254
+ path.delete(value);
255
+ return terminals;
256
+ }
188
257
  /**
189
258
  * `n x` for an aggregate concatenates (v0.22.0). The answer is a fresh
190
259
  * aggregate of x's kind holding the terms x keeps once, its properties and
@@ -20,13 +20,16 @@ const recognizeName = (symbol, source = "") => {
20
20
  if (source.startsWith(".")) {
21
21
  return { disposition: ScanDisposition.CompleteRedispatch };
22
22
  }
23
- if (source.endsWith("^")) {
23
+ // Reserve observation spellings and leave a following schema closer alone.
24
+ // The caret inside .<^> is not an effect suffix.
25
+ if (source === "<>" || source === "<^>" ||
26
+ (source.endsWith("^") && source !== "<^")) {
24
27
  return { disposition: ScanDisposition.CompleteRedispatch };
25
28
  }
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)) {
29
+ // `#` begins a comment except where it spells a size operator, `.#`, `.##`
30
+ // or `.###` (#411, 010.04), which nothing extends: `.foo#bar` still ends at
31
+ // its `#`, and `.####` is `.###` before a comment.
32
+ if (Frame.SIZE_OPERATORS.includes(source + char)) {
30
33
  return { disposition: ScanDisposition.Consume };
31
34
  }
32
35
  if (source.startsWith(Frame.kCOUNT)) {
@@ -280,11 +280,13 @@ export class FrameSymbol extends FrameAtom {
280
280
  });
281
281
  }
282
282
  called_by(context) {
283
+ if (this.is.address === true && this.data === "<>") {
284
+ return FrameType.of(context);
285
+ }
283
286
  // A size operator answers for the receiver itself rather than looking a
284
287
  // name up, so the scope a value was read from never answers for it (#411).
285
288
  // Only an address is one, since the lexer looks `#` up as the comment.
286
- if (this.is.address === true &&
287
- (this.data === Frame.kCOUNT || this.data === Frame.kLEAVES)) {
289
+ if (this.is.address === true && Frame.SIZE_OPERATORS.includes(this.data)) {
288
290
  return context.referent().sizeFor(this.data);
289
291
  }
290
292
  // An address applied by a receiver reads that member, `o .x`. Two
@@ -442,12 +444,6 @@ export class FrameOperator extends FrameSymbol {
442
444
  bound.receiverState = receiverState;
443
445
  return bound;
444
446
  }
445
- apply(argument, parameter) {
446
- if (this.operator === "~~") {
447
- return FrameType.of(argument);
448
- }
449
- return super.apply(argument, parameter);
450
- }
451
447
  called_by(context) {
452
448
  const value = FrameSymbol.for(this.data).called_by(context);
453
449
  return value instanceof FrameCurry && this.receiverState
@@ -18,13 +18,14 @@ export class FrameType extends Frame {
18
18
  });
19
19
  }
20
20
  static of(representative) {
21
+ representative = representative.referent();
21
22
  return new FrameType(representative.constructor, representative);
22
23
  }
23
24
  matches(value) {
24
25
  return this.match(value).matched;
25
26
  }
26
27
  match(value) {
27
- return value.constructor === this.FrameClass
28
+ return value.referent().constructor === this.FrameClass
28
29
  ? matchSuccess(value)
29
30
  : matchFailure(Frame.error(`$!.type-error ${this.toString()} ${value.toString()}`));
30
31
  }
@@ -33,7 +34,7 @@ export class FrameType extends Frame {
33
34
  return result.matched ? result.evidence : result.error;
34
35
  }
35
36
  toString() {
36
- return `~~${this.representative.toString()}`;
37
+ return `(${this.representative.toString()}).<>`;
37
38
  }
38
39
  dataString() {
39
40
  return this.toString();
@@ -244,8 +244,10 @@ export class Frame extends MetaFrame {
244
244
  return this;
245
245
  }
246
246
  /**
247
- * What this frame answers for a size operator, `.#` or `.##` (#411). Only an
248
- * aggregate counts and a count is its own size, so anything else is nil.
247
+ * What this frame answers for a size operator, `.#`, `.##` or `.###`
248
+ * (#411). Only an aggregate counts and a count is its own size, so anything
249
+ * else is nil. `.###` answers only on Figures, nested arrays (issue
250
+ * occurrence/figure 010.04).
249
251
  */
250
252
  sizeFor(_key) {
251
253
  return Frame.nil;
@@ -581,6 +583,31 @@ Object.defineProperty(Frame, "kLEAVES", {
581
583
  writable: true,
582
584
  value: "##"
583
585
  });
586
+ /**
587
+ * kRESIDUAL is the size operator `.###`, a Figure's signed alternating sum
588
+ * of its immediate elements' terminal counts, as a FrameInt (issue
589
+ * occurrence/figure 010.04).
590
+ */
591
+ Object.defineProperty(Frame, "kRESIDUAL", {
592
+ enumerable: true,
593
+ configurable: true,
594
+ writable: true,
595
+ value: "###"
596
+ });
597
+ /**
598
+ * The size operators, the only names spelled with `#` (#411). Each answers
599
+ * for its receiver through `sizeFor`, never through name lookup.
600
+ */
601
+ Object.defineProperty(Frame, "SIZE_OPERATORS", {
602
+ enumerable: true,
603
+ configurable: true,
604
+ writable: true,
605
+ value: [
606
+ Frame.kCOUNT,
607
+ Frame.kLEAVES,
608
+ Frame.kRESIDUAL,
609
+ ]
610
+ });
584
611
  /**
585
612
  * BEGIN_EXPR is the symbol for the beginning of an expression.
586
613
  */
@@ -44,7 +44,7 @@ export function closureFromAtoms(atoms, site) {
44
44
  }
45
45
  close(false);
46
46
  // Statements stay statements, as the parser holds them, so a statement's
47
- // own forms, such as `x .<>`, resume as they run.
47
+ // own forms, such as `x .<^>`, resume as they run.
48
48
  const closure = new FrameLazy(statements);
49
49
  if (sequence)
50
50
  closure.is.sequence = true;
@@ -1,6 +1,8 @@
1
1
  // Direct, not ../frames.ts, whose ../ops.ts needs this module loaded first.
2
2
  import { Frame } from "../frames/frame.js";
3
3
  import { FrameNumeric } from "../frames/frame-numeric.js";
4
+ import { FrameArray } from "../frames/frame-array.js";
5
+ import { FrameCount } from "../frames/frame-count.js";
4
6
  const numeric = (source, block, operation) => {
5
7
  return source instanceof FrameNumeric && block instanceof FrameNumeric
6
8
  ? operation(source, block)
@@ -8,7 +10,19 @@ const numeric = (source, block, operation) => {
8
10
  };
9
11
  export const Add = (source, block) => numeric(source, block, (left, right) => left.add(right));
10
12
  export const Subtract = (source, block) => numeric(source, block, (left, right) => left.subtract(right));
11
- export const Multiply = (source, block) => numeric(source, block, (left, right) => left.multiply(right));
13
+ /** Star constructs a frame; numeric pairs retain the existing arithmetic ladder. */
14
+ export const Multiply = (source, block) => {
15
+ const left = source.referent();
16
+ const right = block.referent();
17
+ if (left instanceof FrameNumeric && right instanceof FrameNumeric) {
18
+ return left.multiply(right);
19
+ }
20
+ if (left instanceof FrameArray)
21
+ return left.star(block);
22
+ if (left instanceof FrameCount)
23
+ return FrameArray.starCount(left, block);
24
+ return Frame.error("$!.star-domain *");
25
+ };
12
26
  export const Divide = (source, block) => numeric(source, block, (left, right) => left.divide(right));
13
27
  export const Modulo = (source, block) => numeric(source, block, (left, right) => left.modulo(right));
14
28
  export const Power = (source, block) => numeric(source, block, (left, right) => left.power(right));
@@ -0,0 +1,26 @@
1
+ // Enter the aggregate cycle at FrameLazy, as schema matchers do.
2
+ import "../frames/frame-lazy.js";
3
+ import { Frame } from "../frames/frame.js";
4
+ import { FrameList } from "../frames/frame-list.js";
5
+ import { isFrameMatcher } from "../frames/frame-match.js";
6
+ import { HasType } from "./type-operations.js";
7
+ /** Only aggregates expose immediate elements; scalar iteration is not membership. */
8
+ const member = (candidate, host, same) => {
9
+ const right = host.referent();
10
+ return right instanceof FrameList &&
11
+ right.elements().some((element) => same(candidate, element) === Frame.all)
12
+ ? Frame.all
13
+ : Frame.nil;
14
+ };
15
+ /** `~` preserves rich type/schema matching on matcher hosts. */
16
+ export const Member = (candidate, host) => {
17
+ const right = host.referent();
18
+ if (right === Frame.all || right === Frame.nil || isFrameMatcher(right)) {
19
+ return HasType(candidate.referent(), right);
20
+ }
21
+ return member(candidate, right, (left, element) => left.equals(element));
22
+ };
23
+ /** `~~` compares immediate elements by the existing typed equality. */
24
+ export const TypedMember = (candidate, host) => member(candidate, host, (left, element) => left.typeEquals(element));
25
+ /** `~~~` compares immediate elements by the existing identity rule. */
26
+ export const IdenticalMember = (candidate, host) => member(candidate, host, (left, element) => left.identical(element));
package/esm/lib/ops.js CHANGED
@@ -2,7 +2,8 @@ import { IfElse, IfThen } from "./ops/conditionals.js";
2
2
  import { FrameOps } from "./ops/frame-ops.js";
3
3
  import { MapElements, MapMembers, ReduceElements, ReduceMembers, SpliceElements, } from "./ops/iterators.js";
4
4
  import { Add, Divide, Equals, GreaterThan, GreaterThanOrEqual, Identical, LessThan, LessThanOrEqual, Modulo, Multiply, Power, Subtract, TypeEquals, } from "./ops/math.js";
5
- import { BindType, HasType } from "./ops/type-operations.js";
5
+ import { IdenticalMember, Member, TypedMember } from "./ops/membership.js";
6
+ import { BindType } from "./ops/type-operations.js";
6
7
  export { FrameCurry } from "./ops/frame-curry.js";
7
8
  export const Ops = new FrameOps({
8
9
  "&": MapElements,
@@ -27,6 +28,8 @@ export const Ops = new FrameOps({
27
28
  "<": LessThan,
28
29
  "<<": LessThan,
29
30
  "<=": LessThanOrEqual,
30
- "~": HasType,
31
+ "~": Member,
32
+ "~~": TypedMember,
33
+ "~~~": IdenticalMember,
31
34
  "^": BindType,
32
35
  });
@@ -1 +1 @@
1
- export const VERSION = "0.33.0";
1
+ export const VERSION = "0.36.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.33.0",
3
+ "version": "0.36.1",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",