hclang 0.43.0 → 0.44.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.
@@ -1483,7 +1483,9 @@ export const HC_SOURCES = new Map([
1483
1483
  "Import it to construct Demis directly: `Demis <- './hc/demi.hc'`, then",
1484
1484
  "`Demis.Demi [0, 0, 0]`.",
1485
1485
  "",
1486
- "**State (007.25c).** `Demi [C, L, R]` takes three Counts, any of them zero. `C`",
1486
+ "**State (007.25c).** `Demi [C, L, R]` takes three Counts, any of them zero",
1487
+ '(`Demi [C, loads]` takes a Count and a list of loads; see "Channels and',
1488
+ 'division" below). `C`',
1487
1489
  "is the full application Count the state has recorded, not its parity, so",
1488
1490
  "`Demi [2, 5, 1]` and `Demi [4, 5, 1]` are different states that agree in phase",
1489
1491
  "and in both loads. Anything else is refused with `$!!.invalid-demi`, a named",
@@ -1595,42 +1597,114 @@ export const HC_SOURCES = new Map([
1595
1597
  "**Text.** A Demi prints as what it is, an aggregate of fields and methods. As",
1596
1598
  "text it reads `Demi(C,L,R)`, with the Count and both loads.",
1597
1599
  "",
1600
+ "**Channels and division (007.27).** The two loads are the arity-two case of an",
1601
+ "ordered list of channel loads. `Demi [C, loads]` takes a Count and a nonempty",
1602
+ "list of Counts: two loads are this binary Demi, the same instance",
1603
+ "`Demi [C, L, R]` builds, and any other number is a channel Demi. Every Demi",
1604
+ "stores `count` and `loads`; only the binary one also has `left`, `right` and",
1605
+ "`phase`, which it writes together with `loads`. A channel Demi of arity k",
1606
+ "deposits into channel `C mod k`, which for k = 2 is the phase's channel. Its",
1607
+ "Size is the sum of its loads and its residual their alternating sum",
1608
+ "`e0 − e1 + e2 − …`, the list residual, which for two loads is `L − R`. Its",
1609
+ "fourth hash is intrinsic too: a fresh equal Demi of the same arity. Its `*`,",
1610
+ "`scale_` and ratios are as here. Its `+` and `-` follow the residual's signs:",
1611
+ "`-` deposits on the first even load, load 0, raising the residual, and `+` on",
1612
+ "the first odd load, load 1, lowering it, which for two loads are left and",
1613
+ "right; with one load `+` is `$!!.invalid-arity`. Text reads `Demi(C,[e0, e1,",
1614
+ "…])`.",
1615
+ "",
1616
+ "`d ./ n`, also spelled `d / n`, redistributes the Size: it keeps C and the",
1617
+ "total S and deals S Size units round-robin into n channels, so the first",
1618
+ "S mod n hold ⌈S/n⌉ and the rest ⌊S/n⌋. It is idempotent, `(d ./ n) ./ m` is",
1619
+ "`d ./ m`, and equal Count and Size give equal results, so `Demi(3,[5,2])` and",
1620
+ "`Demi(3,[4,3])` divide into the same `Demi(3,[3,2,2])`. It is not scalar",
1621
+ "division and not the ratios: no number is answered. The divisor is a positive",
1622
+ "Count, refused as a list refuses it (`$!.division-by-zero /`,",
1623
+ "`$!.division-domain /`), never read as a Size. `hc/division.hc` is its",
1624
+ "specification, with the list and RotorRouter divisions beside it.",
1625
+ "",
1598
1626
  "**Methods read their own state through `_^`.** A bare name inside a method is",
1599
1627
  "looked up on the argument first (spec 7), and a Demi is applied to arbitrary",
1600
1628
  "Frames, so a bare `left` could read the argument's field. `_^` is the receiver",
1601
1629
  "from a method's body, and `_^^` from a block one level in, because carets",
1602
1630
  "count lexically (spec a06). What a branch needs is bound as a local at method",
1603
- "level, where nested blocks read it.",
1604
- "```",
1605
- ".Demi {((_ ~ ([].<>)) ? {(__ .#) = 3}) ? {.c (__ .0); .l (__ .1); .r (__ .2);",
1606
- " (((c ~ (0.<>)) ? {l ~ (0.<>)}) ? {r ~ (0.<>)}) ? {.p (((c %% 2) = 0) ? {2} : {1}); [",
1631
+ "level, where nested blocks read it. The file's own names, the constructor",
1632
+ "`Demi` and the helpers `deposit-at` (a cursor that adds a Size at one channel",
1633
+ "index, applied by reduce) and `redistribution` (the round-robin loads of S",
1634
+ "units in n channels), are read the same way, through the scope a caret ladder",
1635
+ "reaches, `(_^^^ .Demi)` from a branch, so an argument's fields never stand in",
1636
+ "for them.",
1637
+ "```",
1638
+ ".deposit-at {.at (_ .0); .by (_ .1); [.index 0; .target at; .amount by; .out [];",
1639
+ " .apply_ {.v _; .i (_^ .index); .t (_^ .target); .a (_^ .amount); .o (_^ .out);",
1640
+ " @out (o ((i = t) ? {v + a} : {v})); @index (i + 1);}]};",
1641
+ ".redistribution {.s (_ .0); .ways (_ .1); .r (s %% ways); .q (((s - r) / ways).##);",
1642
+ " ((ways - r) [q]) | (r [(q + 1)])};",
1643
+ ".binary-demi {.c (_ .0); .l (_ .1); .r (_ .2); .p (((c %% 2) = 0) ? {2} : {1}); [",
1607
1644
  " .count c;",
1608
1645
  " .phase p;",
1609
1646
  " .left l;",
1610
1647
  " .right r;",
1648
+ " .loads [l, r];",
1611
1649
  " .# {_^.count};",
1612
1650
  " .## {_^.left + _^.right};",
1613
1651
  " .### {_^.left - _^.right};",
1614
1652
  " .apply_ {.s (_ .##); .c (_^ .count); .l (_^ .left); .r (_^ .right); .p (_^ .phase);",
1615
- " (s ~ (0.<>)) ? {(p = 2) ? {@left (l + s); @phase 1;} : {@right (r + s); @phase 2;}; @count (c + 1); _^^} : {s}};",
1653
+ " (s ~ (0.<>)) ? {(p = 2) ? {@left (l + s); @loads [(l + s), r]; @phase 1;} : {@right (r + s); @loads [l, (r + s)]; @phase 2;}; @count (c + 1); _^^} : {s}};",
1616
1654
  " .scale_ {.k _; .l (_^ .left); .r (_^ .right);",
1617
- " (k ~ (0.<>)) ? {@left (l * k); @right (r * k); _^^} : {$!!.invalid-scale}};",
1618
- " .deposit-right_ {.k _; .r (_^ .right);",
1619
- " (k ~ (0.<>)) ? {@right (r + k); _^^} : {$!!.invalid-demi}};",
1620
- " .deposit-left_ {.k _; .l (_^ .left);",
1621
- " (k ~ (0.<>)) ? {@left (l + k); _^^} : {$!!.invalid-demi}};",
1655
+ " (k ~ (0.<>)) ? {@left (l * k); @right (r * k); @loads [(l * k), (r * k)]; _^^} : {$!!.invalid-scale}};",
1656
+ " .deposit-right_ {.k _; .l (_^ .left); .r (_^ .right);",
1657
+ " (k ~ (0.<>)) ? {@right (r + k); @loads [l, (r + k)]; _^^} : {$!!.invalid-demi}};",
1658
+ " .deposit-left_ {.k _; .l (_^ .left); .r (_^ .right);",
1659
+ " (k ~ (0.<>)) ? {@left (l + k); @loads [(l + k), r]; _^^} : {$!!.invalid-demi}};",
1622
1660
  " .+ {.s (_ .##);",
1623
1661
  " (s ~ (0.<>)) ? {(_^^ .deposit-right_ s)} : {s}};",
1624
1662
  " .- {.s (_ .##);",
1625
1663
  " (s ~ (0.<>)) ? {(_^^ .deposit-left_ s)} : {s}};",
1626
1664
  " .* {.s (_ .##);",
1627
1665
  " (s ~ (0.<>)) ? {(_^^ .scale_ s)} : {s}};",
1666
+ " ./ {.ways _; .c (_^ .count); .s (_^ .##); .v ([] / ways);",
1667
+ " (v ~ ([].<>)) ? {(_^^^ .Demi) [c, ((_^^^ .redistribution) [s, ways])]} : {v}};",
1628
1668
  " .size-ratio {.c (_^ .count); .s (_^ .##); .u ((_ = ()) ? {1} : {0});",
1629
1669
  " (c .> 0) ? {(u = 1) ? {s / c} : {$!!.unary-observation}}};",
1630
1670
  " .residual-ratio {.c (_^ .count); .d (_^ .###); .u ((_ = ()) ? {1} : {0});",
1631
1671
  " (c .> 0) ? {(u = 1) ? {d / c} : {$!!.unary-observation}}};",
1632
1672
  " .toText {“Demi(” (_^ .count) “,” (_^ .left) “,” (_^ .right) “)”}",
1633
- " ]} : {$!!.invalid-demi}} : {$!!.invalid-demi}};",
1673
+ " ]};",
1674
+ ".channel-demi {.c (_ .0); .e (_ .1); [",
1675
+ " .count c;",
1676
+ " .loads e;",
1677
+ " .# {_^.count};",
1678
+ " .## {(_^ .loads).##};",
1679
+ " .### {(_^ .loads).###};",
1680
+ " .apply_ {.s (_ .##); .c (_^ .count); .e (_^ .loads);",
1681
+ " (s ~ (0.<>)) ? {@loads ((e | ((_^^^ .deposit-at) [(c %% (e.#)), s])) .out); @count (c + 1); _^^} : {s}};",
1682
+ " .scale_ {.k _; .e (_^ .loads);",
1683
+ " (k ~ (0.<>)) ? {@loads (e & {_ * k}); _^^} : {$!!.invalid-scale}};",
1684
+ " .deposit-right_ {.k _; .e (_^ .loads);",
1685
+ " ((k ~ (0.<>)) ? {(e.#) .> 1}) ? {@loads ((e | ((_^^^ .deposit-at) [1, k])) .out); _^^} : {(k ~ (0.<>)) ? {$!!.invalid-arity} : {$!!.invalid-demi}}};",
1686
+ " .deposit-left_ {.k _; .e (_^ .loads);",
1687
+ " (k ~ (0.<>)) ? {@loads ((e | ((_^^^ .deposit-at) [0, k])) .out); _^^} : {$!!.invalid-demi}};",
1688
+ " .+ {.s (_ .##);",
1689
+ " (s ~ (0.<>)) ? {(_^^ .deposit-right_ s)} : {s}};",
1690
+ " .- {.s (_ .##);",
1691
+ " (s ~ (0.<>)) ? {(_^^ .deposit-left_ s)} : {s}};",
1692
+ " .* {.s (_ .##);",
1693
+ " (s ~ (0.<>)) ? {(_^^ .scale_ s)} : {s}};",
1694
+ " ./ {.ways _; .c (_^ .count); .s (_^ .##); .v ([] / ways);",
1695
+ " (v ~ ([].<>)) ? {(_^^^ .Demi) [c, ((_^^^ .redistribution) [s, ways])]} : {v}};",
1696
+ " .size-ratio {.c (_^ .count); .s (_^ .##); .u ((_ = ()) ? {1} : {0});",
1697
+ " (c .> 0) ? {(u = 1) ? {s / c} : {$!!.unary-observation}}};",
1698
+ " .residual-ratio {.c (_^ .count); .d (_^ .###); .u ((_ = ()) ? {1} : {0});",
1699
+ " (c .> 0) ? {(u = 1) ? {d / c} : {$!!.unary-observation}}};",
1700
+ " .toText {“Demi(” (_^ .count) “,” (_^ .loads) “)”}",
1701
+ " ]};",
1702
+ ".Demi {((_ ~ ([].<>)) ? {(__ .#) ~ <2, 3>}) ? {.a __; .c (a .0);",
1703
+ " .boxed (((a.#) = 3) ? {[[a .1, a .2]]} : {[a .1]});",
1704
+ " ((c ~ (0.<>)) ? {(boxed .0) ~ ([].<>)}) ? {.e (boxed .0); .k (e.#);",
1705
+ " ((k .> 0) ? {(e &| {.x _; (x ~ (0.<>)) ? {[]} : {[x]}}).# = 0}) ? {",
1706
+ " (k = 2) ? {binary-demi [c, (e .0), (e .1)]} : {channel-demi [c, (e | [])]}",
1707
+ " } : {$!!.invalid-demi}} : {$!!.invalid-demi}} : {$!!.invalid-demi}};",
1634
1708
  "```",
1635
1709
  "## Construction",
1636
1710
  "",
@@ -2107,12 +2181,15 @@ export const HC_SOURCES = new Map([
2107
2181
  "",
2108
2182
  "A reading takes no divisor. It is applied to nil, and an operand is refused",
2109
2183
  "rather than read as the divisor, so it is neither `/` nor `%%`. The class",
2110
- "declares neither operator, so they answer nil as for any aggregate without one",
2184
+ "declares no `%%`, which answers nil as for any aggregate without one. Its `/`,",
2185
+ "also spelled `./`, is not a ratio either: it redistributes the Size into that",
2186
+ "many channels and keeps the Count (007.27, below), so `A / 4` is a Demi of four",
2187
+ "loads and no number",
2111
2188
  "```",
2112
2189
  "; [A.size-ratio 4, A.residual-ratio [], A.size-ratio 0, A.residual-ratio “abc”]",
2113
2190
  "# [$!!.unary-observation, $!!.unary-observation, $!!.unary-observation, $!!.unary-observation]",
2114
- "; [A / 4, A %% 4]",
2115
- "# [(), ()]",
2191
+ "; [“” (A / 4), A %% 4]",
2192
+ "# [“Demi(2,[2, 2, 1, 1])”, ()]",
2116
2193
  "```",
2117
2194
  "A reading at the wrong depth fails safe. Inside a branch `_^` is not the",
2118
2195
  "receiver, so a reading written that way answers `$!.name-missing` and no ratio",
@@ -2189,6 +2266,459 @@ export const HC_SOURCES = new Map([
2189
2266
  "",
2190
2267
  ].join("\n"),
2191
2268
  ],
2269
+ [
2270
+ "hc/division.hc",
2271
+ [
2272
+ "#!/usr/bin/env hc",
2273
+ "```",
2274
+ "Literal division (occurrence/figure 007.26, 007.27, 007.27c)",
2275
+ "",
2276
+ "Division by n, taken literally, is dealing a pile into n channels in turn: the",
2277
+ "first to channel 0, the next to channel 1, and so on round the n channels.",
2278
+ "Parity is the case n = 2. Nothing about it needs a host divisor, a quotient",
2279
+ "type or a fraction; it redistributes what is there and keeps its total.",
2280
+ "",
2281
+ "`x ./ n` is that dealing for three receivers. `./` is the dotted spelling of `/`",
2282
+ '(GRAMMAR "Mathematical Operators"), so `x / n` means the same, and a pair of',
2283
+ "numbers keeps its exact numeric division: `6 ./ 3` is 2 and `7 ./ 2` is 7/2.",
2284
+ "The divisor is always the channel Count itself: a positive Count, never a Size",
2285
+ "read from some other Frame.",
2286
+ "",
2287
+ "- **A list** (an array or a group) deals its immediate elements whole, in",
2288
+ " order, into n channels: the element at index i goes to channel i mod n.",
2289
+ " Elements are the same Frames, never copied, evaluated, measured or",
2290
+ " flattened, so identity, nesting and duplicates are kept. Properties are not",
2291
+ " elements and are not dealt. The answer is a `RotorRouter` of those n",
2292
+ " channels, read as `.channels`, not a bare list of lists (007.27c): a list",
2293
+ " whose children happen to be lists is a plain list, and a router keeps its",
2294
+ " channels as channels, so dividing the answer again re-deals the same Frames.",
2295
+ " The dealing is native, in `/`.",
2296
+ "- **A RotorRouter** (`hc/rotor-router.hc`) keeps the Frames it routed, so it",
2297
+ " re-deals them: it reconstructs the order they were routed in from its",
2298
+ " channels, which a round-robin history fixes, and routes that history through",
2299
+ " a fresh router of n channels. Its Count is unchanged and its next route goes",
2300
+ " to channel C mod n.",
2301
+ "- **A Demi** (`hc/demi.hc`) forgot its Frames and kept only their Sizes, so it",
2302
+ " deals Size units. `D ./ n` keeps the Count C and the total Size S and",
2303
+ " canonicalizes the loads to S units dealt round-robin into n channels, so the",
2304
+ " first S mod n channels hold ⌈S/n⌉ and the rest ⌊S/n⌋. Its next application",
2305
+ " deposits into channel C mod n.",
2306
+ "",
2307
+ "Router and list deal whole Frames, and a Demi deals Size units. Both conserve",
2308
+ "the summed Size, but their channel loads differ in general: children of Sizes",
2309
+ "5 and 2 dealt whole into two channels load them 5 and 2, while a Demi of Size 7",
2310
+ "normalizes to 4 and 3. No law makes them commute, and none is claimed here.",
2311
+ "",
2312
+ "Refusals are the native ones for all three: a divisor of zero is",
2313
+ "`$!.division-by-zero /`, as it is for numbers, and anything but a Count is",
2314
+ "`$!.division-domain /`, a negative or fractional number, a decimal, text, an",
2315
+ "aggregate or a Demi included. A divisor above both 1,000,000 and the element count is `$!.repetition-size 1000000`, checked before anything is built; up to the element count is always allowed, so the answer stays proportional to the list.",
2316
+ "```",
2317
+ "```",
2318
+ "## Lists",
2319
+ "",
2320
+ "Seven Frames dealt into three channels: the element at index i goes to channel",
2321
+ "i mod 3. Nested arrays, an aggregate with a property, a closure and a group are",
2322
+ "each one element, dealt whole. The answer is the router of those channels",
2323
+ "```",
2324
+ "; .A [1, [2]]; .B “b”; .C {5 + 3}; .D [.k 9; 4]; .E 5; .F (6, 7); .G 7;",
2325
+ "; .xs [A, B, C, D, E, F, G];",
2326
+ "; “” (xs ./ 3)",
2327
+ "# “RotorRouter([[[1, [2]], [.k 9; 4], 7], [“b”, 5], [{ 5 + 3 }, (6, 7)]])”",
2328
+ "; .t ((xs ./ 3).channels);",
2329
+ "; [t, t & {_.#}, (xs / 3) = (xs ./ 3), ((t .0) .1).k, (xs ./ 3).#]",
2330
+ "# [[[[1, [2]], [.k 9; 4], 7], [“b”, 5], [{ 5 + 3 }, (6, 7)]], [3, 2, 2], <>, 9, 7]",
2331
+ "```",
2332
+ "Each channel holds the same Frames, in their original order, each once. A",
2333
+ "Frame that occurs twice is dealt twice, and both are it",
2334
+ "```",
2335
+ "; [(t .0) &| {[_ === A]}, (t .2) &| {[_ === C]}, (t &| {_}).#, (t &| {_}) = [A, D, G, B, E, C, F]]",
2336
+ "# [[<>, (), ()], [<>, ()], 7, <>]",
2337
+ "; [([A, A, A] ./ 2).channels, ([A, A, A] ./ 2).channels &| {_} &| {[_ === A]}]",
2338
+ "# [[[[1, [2]], [1, [2]]], [[1, [2]]]], [<>, <>, <>]]",
2339
+ "```",
2340
+ "Two channels are the binary router, with its `left`, `right` and `phase`. One",
2341
+ "channel is the whole list, and more channels than elements leave the last ones",
2342
+ "empty. An empty list deals into n empty channels; empty elements are dealt",
2343
+ "like any other",
2344
+ "```",
2345
+ "; [“” ([1, 2, 3] ./ 2), ([1, 2, 3] ./ 2).left, ([1, 2, 3] ./ 2).phase]",
2346
+ "# [“RotorRouter([1, 3],[2])”, [1, 3], 1]",
2347
+ "; [(xs ./ 1).channels, ([1, 2] ./ 5).channels, ([] ./ 3).channels, ([[], []] ./ 2).channels]",
2348
+ "# [[[[1, [2]], “b”, { 5 + 3 }, [.k 9; 4], 5, (6, 7), 7]], [[1], [2], [], [], []], [[], [], []], [[[]], [[]]]]",
2349
+ "```",
2350
+ "Properties are not elements, so they are not dealt. A group deals its elements",
2351
+ "too, and its channels are arrays",
2352
+ "```",
2353
+ "; [([.k 1; 1, 2, 3] ./ 2).channels, ((1, 2, 3) ./ 2).channels]",
2354
+ "# [[[1, 3], [2]], [[1, 3], [2]]]",
2355
+ "```",
2356
+ "The channel Counts are the canonical dealing: for seven elements into three",
2357
+ "channels they are 3, 2, 2, whatever the elements are",
2358
+ "```",
2359
+ "; [(xs ./ 3).channels & {_.#}, ((7 [0]) ./ 3).channels & {_.#}, ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ./ 4).channels & {_.#}]",
2360
+ "# [[3, 2, 2], [3, 2, 2], [3, 3, 2, 2]]",
2361
+ "```",
2362
+ "## Dividing a division (007.27c)",
2363
+ "",
2364
+ "A division keeps its channels as channels, so dividing it again reads them",
2365
+ "breadth-first, the first of each channel, then the second of each, and deals",
2366
+ "that sequence: `(X ./ n) ./ m` is `X ./ m`, the same Frames in the same",
2367
+ "order, with no extra nesting. Dividing by the same n again changes nothing",
2368
+ "```",
2369
+ "; .X [A, 2, 3, 4, 5, 6];",
2370
+ "; [“” (X ./ 2), “” ((X ./ 2) ./ 2), ((X ./ 2) ./ 2) = (X ./ 2), ((X ./ 2) ./ 3) = (X ./ 3), ((X ./ 3) ./ 2) = (X ./ 2)]",
2371
+ "# [“RotorRouter([[1, [2]], 3, 5],[2, 4, 6])”, “RotorRouter([[1, [2]], 3, 5],[2, 4, 6])”, <>, <>, <>]",
2372
+ "; [((X ./ 2) ./ 3).channels, (((X ./ 2) ./ 3).channels .0 .0) === A]",
2373
+ "# [[[[1, [2]], 4], [2, 5], [3, 6]], <>]",
2374
+ "```",
2375
+ "A plain list whose children are lists is not a division: its two children are",
2376
+ "two whole Frames, dealt as such, even when they look like channels",
2377
+ "```",
2378
+ "; .N [[1, 3, 5], [2, 4, 6]];",
2379
+ "; [“” (N ./ 2), (N ./ 2).left .0 = [1, 3, 5], (N ./ 2).# , “” ((N ./ 2) ./ 2)]",
2380
+ "# [“RotorRouter([[1, 3, 5]],[[2, 4, 6]])”, <>, 2, “RotorRouter([[1, 3, 5]],[[2, 4, 6]])”]",
2381
+ "```",
2382
+ "Uneven channels read back in dealt order: seven Frames into three channels,",
2383
+ "then into one, give the seven in order; into two they are dealt again from",
2384
+ "that order",
2385
+ "```",
2386
+ "; .Y [1, 2, 3, 4, 5, 6, 7];",
2387
+ "; [“” (Y ./ 3), (((Y ./ 3) ./ 1).channels .0) = Y, “” ((Y ./ 3) ./ 2), ((Y ./ 3) ./ 2) = (Y ./ 2)]",
2388
+ "# [“RotorRouter([[1, 4, 7], [2, 5], [3, 6]])”, <>, “RotorRouter([1, 3, 5, 7],[2, 4, 6])”, <>]",
2389
+ "```",
2390
+ "Channels that no round-robin history left have no dealt order to read, so no",
2391
+ "router holds them and they are not divided: the constructor refuses them, and",
2392
+ "a plain list of such lists is a plain list",
2393
+ "```",
2394
+ "; Routers <- './hc/rotor-router.hc';",
2395
+ "; .RotorRouter Routers.RotorRouter;",
2396
+ "; [RotorRouter [[1], [2, 3]], ([[1], [2, 3]] ./ 2).channels]",
2397
+ "# [$!!.invalid-router, [[[1]], [[2, 3]]]]",
2398
+ "```",
2399
+ "## Refusals",
2400
+ "",
2401
+ "The divisor is a positive Count. Zero is division by zero, and anything else is",
2402
+ "outside the domain, whatever its Size: a negative or fractional number, a",
2403
+ "decimal, text, an aggregate, a Demi. A divisor that is already an error is",
2404
+ "answered as it is",
2405
+ "```",
2406
+ "; [[1, 2] ./ 0, [1, 2] ./ (3 - 1), [1, 2] ./ (0 - 2), [1, 2] ./ (1 / 2), [1, 2] ./ 1.5, [1, 2] ./ “two”, [1, 2] ./ [2]]",
2407
+ "# [$!.division-by-zero /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /]",
2408
+ "; [1, 2] ./ (1 / 0)",
2409
+ "# $!.division-by-zero /",
2410
+ "; [[1, 2] ./ (2 ** 30), [1, 2] ./ 1000001, ([1] ./ 1000).channels.#]",
2411
+ "# [$!.repetition-size 1000000, $!.repetition-size 1000000, 1000]",
2412
+ "```",
2413
+ "Numbers keep exact numeric division, and a Frame that is neither a number nor",
2414
+ "an ordered list still answers nil",
2415
+ "```",
2416
+ "; [6 ./ 3, 7 ./ 2, 7 / 2, “abcd” ./ 2, <1, 2> ./ 2]",
2417
+ "# [2, 7/2, 7/2, (), ()]",
2418
+ "```",
2419
+ "## Conservation and empty channels",
2420
+ "",
2421
+ "The summed Size of the dealt children is the list's Size: each child is in",
2422
+ "exactly one channel. Each channel's load is the sum of its children's Sizes,",
2423
+ "and an empty channel's load is 0. The router's `.##` is that sum. The `.##`",
2424
+ "of its channels taken as a plain list of lists is not, because an empty array",
2425
+ "has Size 1, so an empty channel adds one",
2426
+ "```",
2427
+ "; .load {.c _; (c.# = 0) ? {0} : {c.##}};",
2428
+ "; .loads {_ & {load _}};",
2429
+ "; [loads ([5, 2] ./ 2).channels, loads ([5, 2] ./ 3).channels, (loads ([5, 2] ./ 3).channels).##, [5, 2].##, ([5, 2] ./ 3).##, ([5, 2] ./ 3).channels.##]",
2430
+ "# [[5, 2], [5, 2, 0], 7, 7, 7, 8]",
2431
+ "```",
2432
+ "Zero-Size children take a slot, and empty children count their own Size 1",
2433
+ "```",
2434
+ "; [loads ([0, 0, 0] ./ 2).channels, loads ([[], [], 0] ./ 2).channels, [[], [], 0].##]",
2435
+ "# [[0, 0], [1, 1], 2]",
2436
+ "```",
2437
+ "Dealing measures nothing, so a child whose Size fails is dealt like any other.",
2438
+ "Its failure shows where the channel is measured",
2439
+ "```",
2440
+ "; .Failing [.## {[].nope}; 1];",
2441
+ "; ([Failing, 1] ./ 2).channels & {_.#}",
2442
+ "# [1, 1]",
2443
+ "; ([Failing, 1] ./ 2).##",
2444
+ "# $!.name-missing “...",
2445
+ "```",
2446
+ "## Demi",
2447
+ "",
2448
+ "A Demi of any arity is `Demi [C, loads]`, a Count and a nonempty list of",
2449
+ "Counts. Two loads are the binary Demi `Demi [C, L, R]` itself, the same",
2450
+ "instance, with its `left`, `right` and `phase`; any other arity is a channel",
2451
+ "Demi, which reads as `Demi(C,[…])`. Every Demi stores `count` and `loads`",
2452
+ "```",
2453
+ "; Demis <- './hc/demi.hc';",
2454
+ "; .Demi Demis.Demi;",
2455
+ "; [“” (Demi [3, [5, 2]]), (Demi [3, [5, 2]]) == (Demi [3, 5, 2]), (Demi [3, [5, 2]]).left, (Demi [3, 5, 2]).loads]",
2456
+ "# [“Demi(3,5,2)”, <>, 5, [5, 2]]",
2457
+ "; [“” (Demi [0, [0, 0, 0]]), “” (Demi [4, [7]]), “” (Demi [2, [1, 2, 3, 4]]), (Demi [2, [1, 2, 3]]).loads, (Demi [2, [1, 2, 3]]).count]",
2458
+ "# [“Demi(0,[0, 0, 0])”, “Demi(4,[7])”, “Demi(2,[1, 2, 3, 4])”, [1, 2, 3], 2]",
2459
+ "```",
2460
+ "No channels, a load that is not a Count, and any other shape are refused",
2461
+ "```",
2462
+ "; [Demi [2, []], Demi [2, [1, (0 - 1), 3]], Demi [2, [1, 1.5]], Demi [2, [1, [2]]], Demi [(0 - 2), [1]], Demi [2, 5], Demi [2, [1], 3], Demi [2, [1, 2], 3, 4]]",
2463
+ "# [$!!.invalid-demi, $!!.invalid-demi, $!!.invalid-demi, $!!.invalid-demi, $!!.invalid-demi, $!!.invalid-demi, $!!.invalid-demi, $!!.invalid-demi]",
2464
+ "```",
2465
+ "A channel Demi observes as its loads do: its Count is C, its Size the sum of",
2466
+ "its loads, and its residual their alternating sum, `e0 − e1 + e2 − …`, the",
2467
+ "list residual, which for two loads is `L − R`",
2468
+ "```",
2469
+ "; .d7 (Demi [3, 5, 2]); .q3 (d7 ./ 3);",
2470
+ "; [q3.#, q3.##, q3.###, q3.count, q3.loads]",
2471
+ "# [3, 7, 3, 3, [3, 2, 2]]",
2472
+ "```",
2473
+ "## Demi division redistributes Size units",
2474
+ "",
2475
+ "`D ./ n` keeps the Count and the Size and deals the Size units round-robin into",
2476
+ "n channels. For S = 7: into 2, 4 and 3; into 3, 3, 2 and 2; into 4, 2, 2, 2",
2477
+ "and 1. Into one channel it is the whole Size, and into more channels than units",
2478
+ "the last are empty",
2479
+ "```",
2480
+ "; [“” (d7 ./ 2), “” (d7 ./ 3), “” (d7 ./ 4), “” (d7 ./ 1), “” (d7 ./ 9)]",
2481
+ "# [“Demi(3,4,3)”, “Demi(3,[3, 2, 2])”, “Demi(3,[2, 2, 2, 1])”, “Demi(3,[7])”, “Demi(3,[1, 1, 1, 1, 1, 1, 1, 0, 0])”]",
2482
+ "```",
2483
+ "`Demi(3,[5,2]) ./ 3` is `Demi(3,[3,2,2])`, and so is the different state",
2484
+ "`Demi(3,[4,3]) ./ 3`. Normalization is many-to-one: equal Count and Size give",
2485
+ "the same result, and the original distribution cannot be recovered",
2486
+ "```",
2487
+ "; [q3 = (Demi [3, [3, 2, 2]]), ((Demi [3, 4, 3]) ./ 3) = q3, (Demi [3, 4, 3]) = d7, (d7 / 3) = q3, q3 = d7]",
2488
+ "# [<>, <>, (), <>, ()]",
2489
+ "```",
2490
+ "It is idempotent, and dividing again renormalizes from the same Count and Size,",
2491
+ "so the earlier division leaves no trace",
2492
+ "```",
2493
+ "; [(q3 ./ 3) = q3, (q3 ./ 2) = (d7 ./ 2), ((d7 ./ 4) ./ 3) = q3, “” (q3 ./ 2), ((q3 ./ 2) ./ 3) = q3]",
2494
+ "# [<>, <>, <>, “Demi(3,4,3)”, <>]",
2495
+ "```",
2496
+ "Zero Size, zero Count, one channel, and more channels than Size or Count",
2497
+ "```",
2498
+ "; [“” ((Demi [0, 0, 0]) ./ 3), “” ((Demi [0, 9, 0]) ./ 4), “” ((Demi [0, 9, 0]) ./ 1), “” ((Demi [2, 1, 1]) ./ 5), “” ((Demi [1, 0, 0]) ./ 2)]",
2499
+ "# [“Demi(0,[0, 0, 0])”, “Demi(0,[3, 2, 2, 2])”, “Demi(0,[9])”, “Demi(2,[1, 1, 0, 0, 0])”, “Demi(1,0,0)”]",
2500
+ "```",
2501
+ "The divisor is a channel Count, refused as a list refuses it, never coerced to",
2502
+ "its Size",
2503
+ "```",
2504
+ "; [d7 ./ 0, d7 ./ (3 - 1), d7 ./ 1.5, d7 ./ (1 / 2), d7 ./ [3], d7 ./ “three”, d7 ./ (Demi [1, 1, 1])]",
2505
+ "# [$!.division-by-zero /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /]",
2506
+ "```",
2507
+ "## Applying after division",
2508
+ "",
2509
+ "After `./ n` the next application deposits into channel C mod n and adds one to",
2510
+ "the Count, a Size of zero included. `q3` has Count 3, so it deposits into",
2511
+ "channels 0, 1, 2, 0 in turn",
2512
+ "```",
2513
+ "; [“” (q3 10), “” ((q3 10) 1), “” (((q3 10) 1) 1), “” ((((q3 10) 1) 1) 1)]",
2514
+ "# [“Demi(4,[13, 2, 2])”, “Demi(5,[13, 3, 2])”, “Demi(6,[13, 3, 3])”, “Demi(7,[14, 3, 3])”]",
2515
+ "; [“” ((Demi [0, [0, 0, 0]]) 4), “” (((Demi [0, [0, 0, 0]]) 4) 0), “” ((((Demi [0, [0, 0, 0]]) 4) 0) 6)]",
2516
+ "# [“Demi(1,[4, 0, 0])”, “Demi(2,[4, 0, 0])”, “Demi(3,[4, 0, 6])”]",
2517
+ "```",
2518
+ "A reduce applies it the same way, and through a mutable name it writes in",
2519
+ "place. One channel takes every deposit, and two channels are the binary Demi's",
2520
+ "parity",
2521
+ "```",
2522
+ "; “” ([5, 2, 7, 1, 0] | (Demi [0, [0, 0, 0]]))",
2523
+ "# “Demi(5,[6, 2, 7])”",
2524
+ "; .m_ (Demi [0, [0, 0, 0]]);",
2525
+ "; m_ 5;",
2526
+ "; m_ 6;",
2527
+ "; [“” m_, “” ((d7 ./ 1) 4), “” ((d7 ./ 2) 4), “” (d7 4)]",
2528
+ "# [“Demi(2,[5, 6, 0])”, “Demi(4,[11])”, “Demi(4,4,7)”, “Demi(4,5,6)”]",
2529
+ "```",
2530
+ "## Demi laws",
2531
+ "",
2532
+ "For every Count 0 to 3, every pair of loads 0 to 4 (100 states) and every n",
2533
+ "from 1 to 4: the Count and Size are kept, there are n loads, they are the",
2534
+ "dealing of S unit Frames into n channels, division is idempotent, and for every",
2535
+ "m from 1 to 4 dividing by n then m is dividing by m",
2536
+ "```",
2537
+ "; .cs [0, 1, 2, 3]; .ls [0, 1, 2, 3, 4]; .ns [1, 2, 3, 4];",
2538
+ "; .states (cs &| {.c _; ls &| {.l _; ls & {Demi [c, l, _]}}});",
2539
+ "; .law {.k _; ns &| {.n _; .q (k ./ n); [q.# = k.#, q.## = k.##, q.loads.# = n, q.loads = (((k.## [1]) ./ n).channels & {_.#}), (q ./ n) = q]}};",
2540
+ "; .renorm {.k _; ns &| {.n _; ns & {.m _; ((k ./ n) ./ m) = (k ./ m)}}};",
2541
+ "; .checks ((states &| law) | (states &| renorm));",
2542
+ "; [states.#, checks.#, checks = (checks & {<>})]",
2543
+ "# [100, 3600, <>]",
2544
+ "```",
2545
+ "## Channel Demi operations",
2546
+ "",
2547
+ "Its fourth hash is intrinsic, as for the binary Demi: a fresh equal channel Demi",
2548
+ "of the same arity, Count zero included. A record that only spells the fields is",
2549
+ "not one, and forged observers give the ordinary derived binary Demi",
2550
+ "```",
2551
+ "; [(q3.####) == q3, (q3.####) === q3, ((Demi [0, [0, 0, 0]]).####) = (Demi [0, [0, 0, 0]]), ([5, 2].#### ./ 3) = (Demi [2, [3, 2, 2]])]",
2552
+ "# [<>, (), <>, <>]",
2553
+ "; [[.count 3; .loads [3, 2, 2]].####, “” ([.count 3; .loads [3, 2, 2]; .# {3}; .## {7}; .### {3}].####)]",
2554
+ "# [(), “Demi(3,5,2)”]",
2555
+ "```",
2556
+ "`*` and `scale_` scale every load and keep the Count; the ratios read Size and",
2557
+ "residual over the Count, nil at Count zero",
2558
+ "```",
2559
+ "; [“” (q3 * 2), “” (q3.scale_ 3), q3.size-ratio (), q3.residual-ratio (), (Demi [0, [3, 2, 2]]).size-ratio ()]",
2560
+ "# [“Demi(3,[6, 4, 4])”, “Demi(3,[9, 6, 6])”, 7/3, 1, ()]",
2561
+ "```",
2562
+ "`+` and `-` follow the residual's signs. The residual adds the loads at even",
2563
+ "indices and subtracts those at odd ones, so `-` deposits `x.##` on the first",
2564
+ "even load, load 0, and raises the residual, and `+` deposits on the first odd",
2565
+ "load, load 1, and lowers it. For two loads these are the binary Demi's left and",
2566
+ "right. Both keep the Count, and both are `deposit-left_` and `deposit-right_`",
2567
+ "by the operand's Size. One load has no odd index, so there `+` is",
2568
+ "`$!!.invalid-arity`",
2569
+ "```",
2570
+ "; [“” (q3 + 1), “” (q3 - 1), q3.###, (q3 + 1).###, (q3 - 1).###, (q3 + 1).#]",
2571
+ "# [“Demi(3,[3, 3, 2])”, “Demi(3,[4, 2, 2])”, 3, 2, 4, 3]",
2572
+ "; [“” ((d7 ./ 4) + 1), “” ((d7 ./ 4) - 2.5), (d7 ./ 1) + 1, “” ((d7 ./ 1) - 1), “” (d7 + 1), “” (d7 - 1)]",
2573
+ "# [“Demi(3,[2, 3, 2, 1])”, “Demi(3,[4, 2, 2, 1])”, $!!.invalid-arity, “Demi(3,[8])”, “Demi(3,5,3)”, “Demi(3,6,2)”]",
2574
+ "; [((q3 + 4) - 2) = ((q3 - 2) + 4), ((q3 + 1) ./ 3) = ((q3 - 1) ./ 3), q3 + ([].nope), q3.deposit-right_ (0 - 1), q3.deposit-left_ 1.5, “” q3]",
2575
+ "# [<>, <>, $!.name-missing “..., $!!.invalid-demi, $!!.invalid-demi, “Demi(3,[3, 2, 2])”]",
2576
+ "```",
2577
+ "## RotorRouter",
2578
+ "",
2579
+ "`RotorRouter [ch0, …, chk−1]` takes k ≥ 1 channel arrays. Two are the binary",
2580
+ "router, with its `left`, `right` and `phase`; every router stores `channels`.",
2581
+ "A round-robin history from channel 0 fills the channels so that the first",
2582
+ "C mod k hold one more than the rest, so the constructor admits exactly the",
2583
+ "channels whose Counts are the dealing of C into k, and refuses every other",
2584
+ "```",
2585
+ "; [[[]], [[], []], [[], [], []], [[], [], [], []]] & {“” (RotorRouter _)}",
2586
+ "# [“RotorRouter([[]])”, “RotorRouter([],[])”, “RotorRouter([[], [], []])”, “RotorRouter([[], [], [], []])”]",
2587
+ "; [[[5]], [[5], [6]], [[5], [], []], [[5], [6], []], [[5, 8], [6], [7]], [[5, 9], [6], [7], [8]]] & {(RotorRouter _).#}",
2588
+ "# [1, 2, 1, 2, 4, 5]",
2589
+ "; [RotorRouter [[1], [2], [3, 4]], RotorRouter [[1], [], [2]], RotorRouter [[1, 2, 3], [4], [5]], RotorRouter [[], [1]], RotorRouter [[1, 2], [3], [4], [5], [6, 7]], RotorRouter []]",
2590
+ "# [$!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router]",
2591
+ "```",
2592
+ "Routing appends the Frame to channel C mod k, from Count 0 through 6 here, so",
2593
+ "every successor is admissible again. Routing nil routes nothing",
2594
+ "```",
2595
+ "; .s0 (RotorRouter [[], [], []]);",
2596
+ "; [0, 1, 2, 3, 4, 5, 6] & {.n _; ((n [0]) | s0).channels & {_.#}}",
2597
+ "# [[0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1], [2, 1, 1], [2, 2, 1], [2, 2, 2]]",
2598
+ "; [“” ((s0 A) B), (((s0 A) B) C).channels .2 &| {[_ === C]}, (s0 ()).#]",
2599
+ "# [“RotorRouter([[[1, [2]]], [“b”], []])”, [<>], 0]",
2600
+ "```",
2601
+ "## Router division re-deals the Frames",
2602
+ "",
2603
+ "Routing xs from the initial router, then dividing by 3, is the router of",
2604
+ "`xs ./ 3`: the router's history is reconstructed from its channels and dealt",
2605
+ "again. The Count and the summed Size are kept, every Frame is the same Frame,",
2606
+ "and dividing by one gives the history back in routed order",
2607
+ "```",
2608
+ "; .r0 (RotorRouter [[], []]);",
2609
+ "; .r7 (xs | r0); .t3 (r7 ./ 3);",
2610
+ "; [“” t3, t3.channels & {_.#}, t3.# = r7.#, t3.## = r7.##, t3 = (xs ./ 3)]",
2611
+ "# [“RotorRouter([[[1, [2]], [.k 9; 4], 7], [“b”, 5], [{ 5 + 3 }, (6, 7)]])”, [3, 2, 2], <>, <>, <>]",
2612
+ "; [(t3.channels .0) &| {[_ === A]}, (t3.channels .2) &| {[_ === C]}, ((t3.channels .0) .1).k, ((t3 ./ 1).channels .0) = xs]",
2613
+ "# [[<>, (), ()], [<>, ()], 9, <>]",
2614
+ "```",
2615
+ "Division is idempotent and renormalizes: dividing again re-deals the same",
2616
+ "history, so a router divided by 3 and then 2 is the binary router it came",
2617
+ "from. The empty router divides into empty channels",
2618
+ "```",
2619
+ "; [(t3 ./ 3) = t3, (t3 ./ 2) = r7, ((r7 ./ 4) ./ 3) = t3, ((t3 ./ 4) ./ 2) = r7, “” (r0 ./ 3), (r0 ./ 3).#, ((RotorRouter [[], [], []]) ./ 2) = r0]",
2620
+ "# [<>, <>, <>, <>, “RotorRouter([[], [], []])”, 0, <>]",
2621
+ "```",
2622
+ "After division the next route goes to channel C mod n: `t3` has Count 7, so 8,",
2623
+ "9 and 10 go to channels 1, 2 and 0. Divided back into two, it is the binary",
2624
+ "router that routed the same Frames",
2625
+ "```",
2626
+ "; [“” (t3 8), “” ((t3 8) 9), “” (((t3 8) 9) 10)]",
2627
+ "# [“RotorRouter([[[1, [2]], [.k 9; 4], 7], [“b”, 5, 8], [{ 5 + 3 }, (6, 7)]])”, “RotorRouter([[[1, [2]], [.k 9; 4], 7], [“b”, 5, 8], [{ 5 + 3 }, (6, 7), 9]])”, “RotorRouter([[[1, [2]], [.k 9; 4], 7, 10], [“b”, 5, 8], [{ 5 + 3 }, (6, 7), 9]])”]",
2628
+ "; [((t3 8) ./ 2) = (r7 8), (((t3 8) 9) ./ 1).channels .0 = ((xs 8) 9)]",
2629
+ "# [<>, <>]",
2630
+ "```",
2631
+ "Division routes and measures nothing. A Frame held through a mutable name is",
2632
+ "the same Frame after division, so a later write shows; a child whose Size",
2633
+ "fails is re-dealt and fails only where the router is measured; zero-Size",
2634
+ "children and empty arrays take slots, and nested arrays stay whole",
2635
+ "```",
2636
+ "; .kid_ [.n 1; .set_ {@n _;}];",
2637
+ "; .rk ((r0 kid_) ./ 3);",
2638
+ "; kid_.set_ 5;",
2639
+ "; [(rk.channels .0 .0) === kid_, rk.channels .0 .0 .n]",
2640
+ "# [<>, 5]",
2641
+ "; .rf (((r0 Failing) 0) ./ 3);",
2642
+ "; [rf.channels & {_.#}, ((r0 Failing) ./ 3) = (RotorRouter [[Failing], [], []])]",
2643
+ "# [[1, 1, 0], <>]",
2644
+ "; rf.##",
2645
+ "# $!.name-missing “...",
2646
+ "; .rz ((((r0 0) []) <>) ./ 2);",
2647
+ "; [rz.channels, rz.##, rz.###]",
2648
+ "# [[[0, <>], [[]]], 2, 0]",
2649
+ "; [((RotorRouter [[[1, 2]], [[3]]]) ./ 1).channels, ((RotorRouter [[[1, 2]], [[3]]]) ./ 1).## = (RotorRouter [[[1, 2]], [[3]]]).##]",
2650
+ "# [[[[1, 2], [3]]], <>]",
2651
+ "```",
2652
+ "A router's history is not bounded by the channel limit: its readout divides by",
2653
+ "its longest channel's length, which never exceeds the Frames it holds, so a",
2654
+ "one-channel router of 1,000,001 Frames divides into two (PR #482 review)",
2655
+ "```",
2656
+ "; .big ((500001 [1]) | (500000 [1]));",
2657
+ "; [big.#, ((RotorRouter [big]) ./ 2).channels & {_.#}]",
2658
+ "# [1000001, [500001, 500000]]",
2659
+ "```",
2660
+ "Routing appends to channel C mod k directly, without replaying the history, so",
2661
+ "routing grows linearly: three thousand Frames route into three channels in turn",
2662
+ "```",
2663
+ "; .grown ((3000 [1]) | (RotorRouter [[], [], []]));",
2664
+ "; [grown.#, grown.channels & {_.#}, (grown ./ 3) = grown, ((grown ./ 1).channels .0).# ]",
2665
+ "# [3000, [1000, 1000, 1000], <>, 3000]",
2666
+ "```",
2667
+ "The divisor is refused as for a list",
2668
+ "```",
2669
+ "; [r7 ./ 0, r7 ./ (3 - 1), r7 ./ 1.5, r7 ./ [2], r7 ./ (Demi [1, 1, 1]), r7 ./ “two”]",
2670
+ "# [$!.division-by-zero /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /, $!.division-domain /]",
2671
+ "```",
2672
+ "## A router's observers and fourth hash",
2673
+ "",
2674
+ "A router of any arity declares its Count C, its Size, the sum of its channel",
2675
+ "loads, and its residual, their alternating sum, as a channel Demi does. Its",
2676
+ "fourth hash is the Demi of the same arity: a router of k channels other than",
2677
+ "two is recognized intrinsically, as an instance of the shipped class, and",
2678
+ "answers `Demi [C, loads]`, one load per channel, so it routes to channel",
2679
+ "C mod k as its Demi deposits there. The binary router answers the binary Demi",
2680
+ "it always did. At Count zero there is no Demi, for any arity",
2681
+ "```",
2682
+ "; [t3.#, t3.##, t3.###, “” t3.####, t3.#### = (Demi [7, [14, 6, 22]]), t3.####.## = t3.##, t3.####.### = t3.###]",
2683
+ "# [7, 42, 30, “Demi(7,[14, 6, 22])”, <>, <>, <>]",
2684
+ "; [“” (r7.####), ([] ./ 3).####, (RotorRouter [[], [], []]).####, “” ((([] ./ 3) 5).####)]",
2685
+ "# [“Demi(7,24,18)”, (), (), “Demi(1,[5, 0, 0])”]",
2686
+ "```",
2687
+ "So the projection law `(r y).#### = r.#### y` holds for every arity: routing y",
2688
+ "to channel C mod k and observing is depositing `y.##` there",
2689
+ "```",
2690
+ "; .nine [1, 2, 3, 4, 5, 6, 7, 8, 9];",
2691
+ "; [1, 2, 3, 4, 5] & {.r (nine ./ _); ((r 10).####) = ((r.####) 10)}",
2692
+ "# [<>, <>, <>, <>, <>]",
2693
+ "; [“” ((t3 10).####), “” ((nine ./ 1).####), “” ((nine ./ 4).####)]",
2694
+ "# [“Demi(8,[14, 16, 22])”, “Demi(9,[45])”, “Demi(9,[15, 8, 10, 12])”]",
2695
+ "```",
2696
+ "Recognition reads nothing the router computes: a record that only spells",
2697
+ "`channels` is not a router, and a failed child Size is answered as it is",
2698
+ "```",
2699
+ "; [[.channels [[1], [], []]].####, ([Failing] ./ 3).####]",
2700
+ "# [(), $!.name-missing “...]",
2701
+ "```",
2702
+ "## Whole Frames and Size units",
2703
+ "",
2704
+ "Whole-Frame division and Size-unit division agree on the total and differ on",
2705
+ "the channels. Children of Sizes 5 and 2 dealt into two channels load them 5",
2706
+ "and 2, as list and router; the Demi of the same list, normalized, loads them 4",
2707
+ "and 3. They are different states with the same Size, and neither is wrong",
2708
+ "```",
2709
+ "; .two [5, 2];",
2710
+ "; [loads (two ./ 2).channels, ((two | r0) ./ 2).channels & {load _}, “” (two.#### ./ 2), ((two | r0) ./ 2).#### = (two.#### ./ 2), ((two | r0) ./ 2).####.## = (two.#### ./ 2).##]",
2711
+ "# [[5, 2], [5, 2], “Demi(2,4,3)”, (), <>]",
2712
+ "```",
2713
+ "They agree when every child has Size one, since then each Frame is one Size",
2714
+ "unit. With other Sizes they need not",
2715
+ "```",
2716
+ "; .ones [1, 1, 1, 1, 1, 1, 1];",
2717
+ "; [((ones | r0) ./ 3).channels & {load _}, (ones.#### ./ 3).loads, loads ([5, 2, 9, 1] ./ 3).channels, ([5, 2, 9, 1].#### ./ 3).loads]",
2718
+ "# [[3, 2, 2], [3, 2, 2], [6, 2, 9], [6, 6, 5]]",
2719
+ "",
2720
+ ].join("\n"),
2721
+ ],
2192
2722
  [
2193
2723
  "hc/figures.hc",
2194
2724
  [
@@ -3093,7 +3623,8 @@ export const HC_SOURCES = new Map([
3093
3623
  "array's properties are configuration, not routed Frames, so the constructor",
3094
3624
  "keeps the elements in a fresh array of its own, counts them there, and a",
3095
3625
  "channel argument that declares `apply` cannot change what appending means.",
3096
- "Anything that is not two arrays is refused the same way.",
3626
+ "Anything that is not a nonempty list of arrays is refused the same way; one",
3627
+ "array or three are routers of another arity (below).",
3097
3628
  "",
3098
3629
  "**Routing**, `router x`: append x as one member of the channel the parity of C",
3099
3630
  "selects; the Count grows by one and the phase flips with it. `router.phase` is",
@@ -3134,25 +3665,93 @@ export const HC_SOURCES = new Map([
3134
3665
  "`RotorRouter([…],[…])`, each channel as its spelling, the constructor's own",
3135
3666
  "shape.",
3136
3667
  "",
3668
+ "**Channels and division (007.27).** Two channels are the arity-two case of k",
3669
+ "ordered channels. `RotorRouter [ch0, …, chk−1]` takes k ≥ 1 arrays; two are",
3670
+ "this binary router, and any other number is a channel router. Every router",
3671
+ "stores `channels`; only the binary one also has `left`, `right` and `phase`,",
3672
+ "which it writes together with `channels`. A round-robin history from channel 0",
3673
+ "leaves the first C mod k channels one longer than the rest, so a channel",
3674
+ "router admits exactly the channels whose Counts are the dealing of C children",
3675
+ "into k, `round-robin [C, k]`, and refuses every other with",
3676
+ "`$!!.invalid-router`.",
3677
+ "Routing appends to channel C mod k, which for k = 2 is the phase's channel,",
3678
+ "directly, as the binary router does: a small cursor, `append-at`, rebuilds the",
3679
+ "list of channels with the Frame added to that one, so a route costs one pass",
3680
+ "over the k channels and never replays the history (PR #482 review).",
3681
+ "Its Size is the sum of its channel loads and its residual their alternating",
3682
+ "sum, as a channel Demi's are. Its fourth hash is the channel Demi of its",
3683
+ "Count and channel loads, `Demi [C, [load0, …]]`: the fourth hash recognizes an",
3684
+ "instance of this class with other than two channels intrinsically, as it",
3685
+ "recognizes a Demi, because the binary Demi the observers derive follows",
3686
+ "parity and not C mod k. So the projection law `(r y).#### = r.#### y` holds",
3687
+ "for every arity; at Count zero there is no Demi. Text reads `RotorRouter([ch0, ch1, …])`.",
3688
+ "",
3689
+ "`router ./ n`, also spelled `router / n`, re-deals the Frames it holds: it",
3690
+ "reconstructs the order they were routed in from its channels, which their",
3691
+ "admissibility fixes, and routes that history through a fresh router of n",
3692
+ "channels, that is, it is the history divided as a list, `history / n`, since",
3693
+ "list division answers the n-channel router of its channels (007.27c).",
3694
+ "Reconstructing the history is the breadth-first readout of 007.27c: the first",
3695
+ "Frame of each channel in channel order, then the second of each, and so on. Every Frame stays the",
3696
+ "same Frame, whole, in order, exactly once, and nothing is evaluated or",
3697
+ "measured; the Count is kept and the next route goes to channel C mod n. The",
3698
+ "history is the dealing inverted: the channels, the short ones padded with a",
3699
+ "sentinel to the longest, are concatenated and dealt into that many channels,",
3700
+ "which are the rounds, and the sentinels removed. One channel is its own",
3701
+ "history, and channels of equal length need no sentinel. The readout divides by",
3702
+ "the longest channel's length, which never exceeds the Frames held, so it is",
3703
+ "never refused by the channel limit. The divisor is refused as a list refuses",
3704
+ "it. `hc/division.hc` is its specification.",
3705
+ "",
3137
3706
  "**Methods read their own state through `_^`.** A router is applied to",
3138
3707
  "arbitrary Frames, and a bare name in a method is looked up on the argument",
3139
3708
  "first (spec 7), so a router reads its fields through the receiver, `_^` from",
3140
- "the method body. What a branch needs is bound as a local at method level.",
3709
+ "the method body. What a branch needs is bound as a local at method level. The",
3710
+ "file's own names, `RotorRouter` and `history`, are read through the scope a",
3711
+ "caret ladder reaches, `(_^^ .history)` from the method body, so an argument's",
3712
+ "fields never stand in for them.",
3141
3713
  "```",
3142
3714
  ".channel-load {.c _; (c.# = 0) ? {0} : {c.##}};",
3143
- ".RotorRouter {((_ ~ ([].<>)) ? {(__ .#) = 2}) ? {.l (__ .0); .r (__ .1);",
3144
- " ((l ~ ([].<>)) ? {r ~ ([].<>)}) ? {.lc (l | []); .rc (r | []); .a (lc.#); .b (rc.#);",
3145
- " ((a = b) : {a = (b + 1)}) ? {[",
3146
- " .phase ((a = b) ? {2} : {1});",
3715
+ ".history {.cs _; .k (cs.#); .w ((cs .0).#); .pad [];",
3716
+ " (k = 1) ? {cs .0} : {(w = 0) ? {[]} : {",
3717
+ " .even ((((cs &| {_}).#) = (k * w)) ? {1} : {0});",
3718
+ " .padded ((even = 1) ? {cs} : {cs & {.ch _; (ch.# = w) ? {ch} : {ch pad}}});",
3719
+ " .flat ((((padded &| {_}) / w) .channels) &| {_});",
3720
+ " (even = 1) ? {flat} : {flat &| {.x _; (x === (_^^^^ .pad)) ? {[]} : {[x]}}}}}};",
3721
+ ".append-at {.at (_ .0); .item (_ .1); [.index 0; .target at; .item item; .out [];",
3722
+ " .apply_ {.v _; .i (_^ .index); .t (_^ .target); .it (_^ .item); .o (_^ .out);",
3723
+ " @out (o ((i = t) ? {it | v} : {v})); @index (i + 1);}]};",
3724
+ ".round-robin {.s (_ .0); .ways (_ .1); .q (((s - (s %% ways)) / ways).##);",
3725
+ " [.low q; .short 0; .ok 1;",
3726
+ " .apply_ {.c _; .lo (_^ .low); .seen (_^ .short); .fine (_^ .ok);",
3727
+ " @ok ((((c = (lo + 1)) ? {seen = 0} : {c = lo}) ? {fine = 1}) ? {1} : {0}); @short ((c = lo) ? {1} : {seen});}]};",
3728
+ ".binary-router {.cs _; .lc (cs .0); .rc (cs .1); [",
3729
+ " .phase (((lc.#) = (rc.#)) ? {2} : {1});",
3147
3730
  " .left lc;",
3148
3731
  " .right rc;",
3732
+ " .channels [lc, rc];",
3149
3733
  " .apply_ {.x _; .l (_^ .left); .r (_^ .right); .n (l.# + r.#);",
3150
- " ((n %% 2) = 0) ? {@left (l x); @phase 1;} : {@right (r x); @phase 2;};};",
3734
+ " ((n %% 2) = 0) ? {.nl (l x); @left nl; @channels [nl, r]; @phase 1;} : {.nr (r x); @right nr; @channels [l, nr]; @phase 2;};};",
3151
3735
  " .# {.l (_^ .left); .r (_^ .right); l.# + r.#};",
3152
3736
  " .## {(channel-load (_^ .left)) + (channel-load (_^ .right))};",
3153
3737
  " .### {(channel-load (_^ .left)) - (channel-load (_^ .right))};",
3738
+ " ./ {.ways _; ((_^^ .history) (_^ .channels)) / ways};",
3154
3739
  " .toText {“RotorRouter(” (_^ .left) “,” (_^ .right) “)”}",
3155
- " ]} : {$!!.invalid-router}} : {$!!.invalid-router}} : {$!!.invalid-router}};",
3740
+ " ]};",
3741
+ ".channel-router {.cs _; [",
3742
+ " .channels cs;",
3743
+ " .apply_ {.x _; .cs (_^ .channels); .n ((cs & {_.#}).##);",
3744
+ " @channels ((cs | ((_^^ .append-at) [(n %% (cs.#)), [x]])) .out);};",
3745
+ " .# {((_^ .channels) &| {_}).#};",
3746
+ " .## {((_^ .channels) & {channel-load _}).##};",
3747
+ " .### {((_^ .channels) & {channel-load _}).###};",
3748
+ " ./ {.ways _; ((_^^ .history) (_^ .channels)) / ways};",
3749
+ " .toText {“RotorRouter(” (_^ .channels) “)”}",
3750
+ " ]};",
3751
+ ".RotorRouter {((_ ~ ([].<>)) ? {(__ .#) .> 0}) ? {.a __;",
3752
+ " ((a &| {.x _; (x ~ ([].<>)) ? {[]} : {[x]}}).# = 0) ? {.cs (a & {_ | []}); .k (cs.#); .n ((cs &| {_}).#);",
3753
+ " ((k .> 0) ? {(((cs & {_.#}) | (round-robin [n, k])) .ok) = 1}) ? {(k = 2) ? {binary-router cs} : {channel-router cs}} : {$!!.invalid-router}",
3754
+ " } : {$!!.invalid-router}} : {$!!.invalid-router}};",
3156
3755
  "```",
3157
3756
  "## Construction",
3158
3757
  "",
@@ -3179,9 +3778,10 @@ export const HC_SOURCES = new Map([
3179
3778
  "; [RotorRouter [2, [], []], RotorRouter [1, [5], []], RotorRouter [2, [5]], RotorRouter [1, []]]",
3180
3779
  "# [$!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router]",
3181
3780
  "```",
3182
- "Anything but two arrays is refused",
3781
+ "Anything but a nonempty list of arrays is refused. One channel or three are",
3782
+ "not refused: they are routers of another arity (007.27, `hc/division.hc`)",
3183
3783
  "```",
3184
- "; [RotorRouter [[], 5], RotorRouter [(1, 2), []], RotorRouter [[]], RotorRouter [[], [], []], RotorRouter 5, RotorRouter ()]",
3784
+ "; [RotorRouter [[], 5], RotorRouter [(1, 2), []], RotorRouter [], RotorRouter [[], [5], []], RotorRouter 5, RotorRouter ()]",
3185
3785
  "# [$!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router, $!!.invalid-router]",
3186
3786
  "```",
3187
3787
  "A channel is its elements. The router keeps them in an array of its own, so a",
@@ -3484,8 +4084,8 @@ export const HC_SOURCES = new Map([
3484
4084
  "printed form is matched here by its beginning",
3485
4085
  "```",
3486
4086
  "; RotorRouter [[5], []]",
3487
- "# [.phase 1; .left [5]; .right []; .apply_ {...",
3488
- "; (RotorRouter [[5], []]) = [.phase 1; .left [5]; .right []; .apply_ { .x _; .l (_^ .left); .r (_^ .right); .n (l.# + r.#); ((n %% 2) = 0) ? { @left (l x); @phase 1; } : { @right (r x); @phase 2; }; }; .# { .l (_^ .left); .r (_^ .right); l.# + r.# }; .## { (channel-load (_^ .left)) + (channel-load (_^ .right)) }; .### { (channel-load (_^ .left)) - (channel-load (_^ .right)) }; .toText { “RotorRouter(” (_^ .left) “,” (_^ .right) “)” };]",
4087
+ "# [.phase 1; .left [5]; .right []; .channels [[5], []]; .apply_ {...",
4088
+ "; (RotorRouter [[5], []]) = [.phase 1; .left [5]; .right []; .channels [[5], []]; .apply_ { .x _; .l (_^ .left); .r (_^ .right); .n (l.# + r.#); ((n %% 2) = 0) ? { .nl (l x); @left nl; @channels [nl, r]; @phase 1; } : { .nr (r x); @right nr; @channels [l, nr]; @phase 2; }; }; .# { .l (_^ .left); .r (_^ .right); l.# + r.# }; .## { (channel-load (_^ .left)) + (channel-load (_^ .right)) }; .### { (channel-load (_^ .left)) - (channel-load (_^ .right)) }; ./ { .ways _; ((_^^ .history) (_^ .channels)) / ways }; .toText { “RotorRouter(” (_^ .left) “,” (_^ .right) “)” };]",
3489
4089
  "# <>",
3490
4090
  "```",
3491
4091
  "## Importing",
@@ -73,19 +73,31 @@ export function demiClass() {
73
73
  return loaded;
74
74
  }
75
75
  /**
76
- * Constructs a Demi with the HC class: `Demi [count, left, right]`.
76
+ * Constructs a Demi of any arity with the HC class: `Demi [count, loads]`.
77
+ * Two loads give the binary Demi, the same instance `Demi [count, left,
78
+ * right]` builds; any other nonzero number gives the channel Demi (007.27).
77
79
  *
78
- * @param count The full application Count; the Demi derives its phase from it.
79
- * @param left The left load.
80
- * @param right The right load.
80
+ * @param count The full application Count.
81
+ * @param loads The ordered channel loads.
81
82
  * @returns The new instance, or the class's failure.
82
83
  */
83
- export function constructDemi(count, left, right) {
84
+ export function constructDemiLoads(count, loads) {
84
85
  const constructor = demiClass();
85
86
  if (constructor.isFailedResult() || constructor.observationError()) {
86
87
  return constructor;
87
88
  }
88
- const state = new FrameArray([count, left, right]);
89
+ const state = new FrameArray([count, new FrameArray([...loads])]);
89
90
  return FrameExpr.answeredValue(constructor.call(state)).referent();
90
91
  }
91
- installDemiConstructor(constructDemi);
92
+ /**
93
+ * Constructs a binary Demi with the HC class: `Demi [count, left, right]`.
94
+ *
95
+ * @param count The full application Count; the Demi derives its phase from it.
96
+ * @param left The left load.
97
+ * @param right The right load.
98
+ * @returns The new instance, or the class's failure.
99
+ */
100
+ export function constructDemi(count, left, right) {
101
+ return constructDemiLoads(count, [left, right]);
102
+ }
103
+ installDemiConstructor(constructDemiLoads);
@@ -1,6 +1,8 @@
1
1
  import { Frame, FrameCount, FrameString, FrameSymbol, } from "../frames.js";
2
2
  // Installs the Demi class that `.####` constructs (011.20).
3
3
  import "./demi-prologue.js";
4
+ // Installs the RotorRouter class that list division answers (007.27c).
5
+ import "./router-prologue.js";
4
6
  import { EvalPipe, makeSourcePipe } from "./eval-pipe.js";
5
7
  import { evaluateResourceSource } from "./resource-import.js";
6
8
  import { sigilizer } from "./sigilizer.js";
@@ -0,0 +1,153 @@
1
+ /**
2
+ * The RotorRouter class, loaded from HC for list division.
3
+ *
4
+ * Dividing a list, `xs ./ n`, deals its elements into n channels and answers
5
+ * them as a `RotorRouter`, the HC class in `hc/rotor-router.hc`, so that the
6
+ * channels stay channels and a further division reads them breadth-first
7
+ * (occurrence/figure 007.27c). The ops package deals natively but cannot run
8
+ * HC, so this module installs the constructor it calls
9
+ * (`installDivisionCarrier`).
10
+ *
11
+ * As with the Demi class (`demi-prologue.ts`), the class is read from the
12
+ * compiled-in bundle, evaluated once per process on first use, in a namespace
13
+ * of its own with no host bindings and no root, and its outcome is kept,
14
+ * failure included. A division requested while the file is still loading is
15
+ * `$!.router-prologue-reentrant`.
16
+ *
17
+ * @module
18
+ */
19
+ import { EvaluationScope, Frame, FrameArray, FrameExpr, installRouterRecognizer, } from "../frames.js";
20
+ import { installDivisionCarrier } from "../ops/math.js";
21
+ import { HC_SOURCES } from "../bundle/hc-sources.js";
22
+ import { evaluateSourceText } from "./resource-import.js";
23
+ /** The bundled file that defines the class. */
24
+ export const ROUTER_SOURCE = "hc/rotor-router.hc";
25
+ /** The name the file declares the constructor under. */
26
+ export const ROUTER_CLASS = "RotorRouter";
27
+ /**
28
+ * The builders the class file declares beside its constructor, which make an
29
+ * instance from channels already known to be admissible, without validating
30
+ * or copying them: `binary-router` for two channels, `channel-router` for any
31
+ * other number.
32
+ */
33
+ export const ROUTER_BUILDERS = ["binary-router", "channel-router"];
34
+ let loaded;
35
+ let builders;
36
+ let loading = false;
37
+ /**
38
+ * Evaluates class source in a namespace of its own, answering its
39
+ * `RotorRouter` or the failure. Uncached: `routerClass` keeps the one result.
40
+ *
41
+ * @param source The source text, or undefined when the bundle lacks it.
42
+ * @returns The constructor, or an error.
43
+ */
44
+ export function loadRouterClass(source) {
45
+ if (source === undefined) {
46
+ return Frame.error(`$!.router-prologue-missing ${ROUTER_SOURCE}`);
47
+ }
48
+ const namespace = new FrameArray([]);
49
+ const result = evaluateSourceText(source, EvaluationScope.root(namespace));
50
+ if (result.isFailedResult() || result.observationError())
51
+ return result;
52
+ const constructor = namespace.getOwn(ROUTER_CLASS);
53
+ if (constructor.is.missing) {
54
+ return Frame.error(`$!.router-prologue-missing ${ROUTER_CLASS}`);
55
+ }
56
+ const [binary, channel] = ROUTER_BUILDERS.map((key) => namespace.getOwn(key));
57
+ if (binary.is.missing || channel.is.missing) {
58
+ return Frame.error(`$!.router-prologue-missing ${ROUTER_BUILDERS}`);
59
+ }
60
+ builders = { binary, channel };
61
+ return constructor;
62
+ }
63
+ /**
64
+ * The `RotorRouter` constructor closure, evaluated from the bundle on first
65
+ * use.
66
+ *
67
+ * @returns The constructor, or the error that loading it answered.
68
+ */
69
+ export function routerClass() {
70
+ if (loaded)
71
+ return loaded;
72
+ if (loading)
73
+ return Frame.error("$!.router-prologue-reentrant");
74
+ loading = true;
75
+ try {
76
+ loaded = loadRouterClass(HC_SOURCES.get(ROUTER_SOURCE));
77
+ }
78
+ finally {
79
+ loading = false;
80
+ }
81
+ return loaded;
82
+ }
83
+ /**
84
+ * Constructs a router with the HC class: `RotorRouter [ch0, …, chk−1]`.
85
+ *
86
+ * @param channels The channel arrays, in order.
87
+ * @returns The new instance, or the class's failure.
88
+ */
89
+ export function constructRouter(channels) {
90
+ const constructor = routerClass();
91
+ if (constructor.isFailedResult() || constructor.observationError()) {
92
+ return constructor;
93
+ }
94
+ return FrameExpr.answeredValue(constructor.call(channels)).referent();
95
+ }
96
+ /**
97
+ * The channels of an instance of the shipped class with other than two
98
+ * channels, for `.####` (007.27); undefined for anything else.
99
+ *
100
+ * Recognition mirrors the Demi's (007.25e): the source's own stored
101
+ * `channels`, a list of arrays, must rebuild through the constructor into a
102
+ * router `==` to the source, fields and methods included. Nothing of the
103
+ * source runs, so a record that only spells `channels` is not a router.
104
+ *
105
+ * @param source The observed Frame.
106
+ * @returns The stored channel arrays, or undefined.
107
+ */
108
+ export function channelRouterChannels(source) {
109
+ const host = source.referent();
110
+ if (!host.ownsIdentity())
111
+ return undefined;
112
+ const stored = host.get_here("channels").referent();
113
+ if (!(stored instanceof FrameArray))
114
+ return undefined;
115
+ const channels = stored.elements().map((channel) => channel.referent());
116
+ if (channels.length === 0 || channels.length === 2)
117
+ return undefined;
118
+ if (!channels.every((channel) => channel instanceof FrameArray)) {
119
+ return undefined;
120
+ }
121
+ const candidate = constructRouter(new FrameArray(channels.map((channel) => new FrameArray([...channel.elements()]))));
122
+ if (candidate.isFailedResult() || candidate.observationError()) {
123
+ return undefined;
124
+ }
125
+ return candidate.typeEquals(host) === Frame.all ? channels : undefined;
126
+ }
127
+ /**
128
+ * The router a list division answers, from the channels it just dealt.
129
+ *
130
+ * Dealt channels are fresh arrays and a round-robin profile by construction,
131
+ * so they go straight to the class's own builder: validating and copying them
132
+ * through the public constructor costs a closure call per channel, which made
133
+ * a router's breadth-first readout (a division by its longest channel's
134
+ * length) dominate its division (PR #482 review). The instance is the one the
135
+ * constructor would build from the same channels.
136
+ *
137
+ * @param channels The dealt channel arrays, at least one.
138
+ * @returns The new router, or the class's failure.
139
+ */
140
+ export function dealtRouter(channels) {
141
+ const constructor = routerClass();
142
+ if (constructor.isFailedResult() || constructor.observationError()) {
143
+ return constructor;
144
+ }
145
+ if (!builders)
146
+ return Frame.error("$!.router-unavailable /");
147
+ const build = channels.elements().length === 2
148
+ ? builders.binary
149
+ : builders.channel;
150
+ return FrameExpr.answeredValue(build.call(channels)).referent();
151
+ }
152
+ installDivisionCarrier(dealtRouter);
153
+ installRouterRecognizer(channelRouterChannels);
@@ -12,21 +12,42 @@ let construct;
12
12
  export function installDemiConstructor(constructor) {
13
13
  construct = constructor;
14
14
  }
15
- /** The stored fields a Demi instance is reconstructed from, in order. */
16
- const DEMI_STATE = ["count", "left", "right"];
15
+ let recognizeRouter;
17
16
  /**
18
- * A stored field read as an exact nonnegative Count, or undefined.
17
+ * Installs how `.####` recognizes a channel router (007.27).
19
18
  *
20
- * Only the instance's own field is read (`get_here`), never a declared parent
21
- * or a method, so nothing runs.
19
+ * @param recognizer Answers a channel router's channels, or undefined.
22
20
  */
23
- function storedCount(source, key) {
24
- const value = source.get_here(key).referent();
21
+ export function installRouterRecognizer(recognizer) {
22
+ recognizeRouter = recognizer;
23
+ }
24
+ /** A Frame read as an exact nonnegative Count, or undefined. */
25
+ function exactCount(frame) {
26
+ const value = frame.referent();
25
27
  return value instanceof FrameCount && typeof value.data === "bigint" &&
26
28
  value.data >= 0n
27
29
  ? value
28
30
  : undefined;
29
31
  }
32
+ /**
33
+ * The stored loads of a Demi instance: its own `loads` field, a nonempty list
34
+ * whose elements are all exact Counts, or undefined.
35
+ *
36
+ * Only the instance's own field is read (`get_here`), never a declared parent
37
+ * or a method, and the list's elements are read as stored, so nothing runs.
38
+ */
39
+ function storedLoads(source) {
40
+ const value = source.get_here("loads").referent();
41
+ if (!value.ownsIdentity())
42
+ return undefined;
43
+ const elements = value.elements();
44
+ if (elements.length === 0)
45
+ return undefined;
46
+ const loads = elements.map(exactCount);
47
+ return loads.every((load) => load !== undefined)
48
+ ? loads
49
+ : undefined;
50
+ }
30
51
  /**
31
52
  * The intrinsic Demi path (007.25e). A constructed Demi is the one Frame whose
32
53
  * fourth hash the three observers cannot decide: `Demi [0, 0, 0]` reads 0, 0,
@@ -34,10 +55,12 @@ function storedCount(source, key) {
34
55
  * class declare its own `.####`, the fourth hash recognizes an instance of the
35
56
  * shipped class itself, and nothing else.
36
57
  *
37
- * A source is a Demi exactly when it has stored Counts `count`, `left` and
38
- * `right`, and it is `==` (same value and same type at every node, fields and
39
- * methods included) to the Demi the shipped constructor builds from those
40
- * three Counts. The answer is then that fresh reconstruction, never the source
58
+ * A source is a Demi exactly when it has a stored Count `count` and a stored
59
+ * nonempty list of Counts `loads`, and it is `==` (same value and same type at
60
+ * every node, fields and methods included) to the Demi the shipped constructor
61
+ * builds from them. A binary Demi stores `left` and `right` too, and the
62
+ * comparison covers them. The answer is then that fresh reconstruction, of
63
+ * the source's own arity, never the source
41
64
  * and never anything the source computed. No observer, method or closure of
42
65
  * the source runs, so a forged `#`, `##`, `###` or `####` cannot steer it, and
43
66
  * comparison is the language's own typed equality, not a spelling. Anything
@@ -54,15 +77,59 @@ function intrinsicDemi(source) {
54
77
  const host = source.referent();
55
78
  if (!host.ownsIdentity())
56
79
  return undefined;
57
- const [count, left, right] = DEMI_STATE.map((key) => storedCount(host, key));
58
- if (!count || !left || !right)
80
+ const count = exactCount(host.get_here("count"));
81
+ const loads = storedLoads(host);
82
+ if (!count || !loads)
59
83
  return undefined;
60
- const candidate = construct(new FrameCount(count.data), new FrameCount(left.data), new FrameCount(right.data));
84
+ const candidate = construct(new FrameCount(count.data), loads.map((load) => new FrameCount(load.data)));
61
85
  if (candidate.isFailedResult() || candidate.observationError()) {
62
86
  return undefined;
63
87
  }
64
88
  return candidate.typeEquals(host) === Frame.all ? candidate : undefined;
65
89
  }
90
+ /**
91
+ * The channel-router path (007.27). A router of k channels routes to channel
92
+ * C mod k, which only a Demi of k loads follows; the binary Demi the observers
93
+ * derive follows parity, so for k other than two it would not be the router's
94
+ * shadow. An instance of the shipped `RotorRouter` class with other than two
95
+ * channels therefore answers the channel Demi of its Count and channel loads,
96
+ * `Demi [C, [load0, …]]`, where a load is the summed Size of a channel's
97
+ * children and an empty channel's load is 0. The binary router keeps the
98
+ * derived path, which already answers `Demi(C, L, R)`.
99
+ *
100
+ * Its Count is the children it holds; at Count zero it has no Demi, as the
101
+ * empty binary router and the empty host have none. Each child's Size is read
102
+ * once, and a failed Size is answered as it is.
103
+ *
104
+ * @param source The observed Frame.
105
+ * @returns The router's channel Demi, nil, an error, or undefined when the
106
+ * source is not a channel router.
107
+ */
108
+ function channelRouterDemi(source) {
109
+ const channels = recognizeRouter?.(source);
110
+ if (!channels)
111
+ return undefined;
112
+ const count = channels.reduce((total, channel) => total + BigInt(channel.elements().length), 0n);
113
+ if (count === 0n)
114
+ return Frame.nil;
115
+ const loads = [];
116
+ for (const channel of channels) {
117
+ if (channel.elements().length === 0) {
118
+ loads.push(new FrameCount(0n));
119
+ continue;
120
+ }
121
+ const size = channel.sizeFor(Frame.kLEAVES);
122
+ if (size.observationError())
123
+ return size;
124
+ const load = exactCount(size);
125
+ if (!load)
126
+ return Frame.error("$!.demi-observations");
127
+ loads.push(new FrameCount(load.data));
128
+ }
129
+ if (!construct)
130
+ return Frame.error("$!.demi-unavailable");
131
+ return construct(new FrameCount(count), loads);
132
+ }
66
133
  /**
67
134
  * The fourth hash, `.####`. For a constructed Demi it is that Demi's own state
68
135
  * (`intrinsicDemi`), Count zero included. For every other Frame it is derived
@@ -92,6 +159,9 @@ export function observeDemi(source) {
92
159
  const intrinsic = intrinsicDemi(source);
93
160
  if (intrinsic)
94
161
  return intrinsic;
162
+ const routed = channelRouterDemi(source);
163
+ if (routed)
164
+ return routed;
95
165
  const n = source.sizeFor(Frame.kCOUNT);
96
166
  if (n.observationError())
97
167
  return n;
@@ -120,6 +190,9 @@ export function observeDemi(source) {
120
190
  }
121
191
  if (!construct)
122
192
  return Frame.error("$!.demi-unavailable");
123
- return construct(new FrameCount(n.data), new FrameCount(sum / 2n), new FrameCount(difference / 2n));
193
+ return construct(new FrameCount(n.data), [
194
+ new FrameCount(sum / 2n),
195
+ new FrameCount(difference / 2n),
196
+ ]);
124
197
  }
125
198
  Frame.demi = observeDemi;
@@ -292,6 +292,15 @@ export class FrameSymbol extends FrameAtom {
292
292
  if (this.is.address === true && Frame.SIZE_OPERATORS.includes(this.data)) {
293
293
  return context.referent().sizeFor(this.data);
294
294
  }
295
+ // A dotted arithmetic operator is the operator itself: `xs ./ 3` is
296
+ // `xs / 3` (GRAMMAR "Mathematical Operators"). Resolved as a name, it
297
+ // missed a named aggregate and was found in the scope the name was read
298
+ // in, so `xs .* 2` answered for that scope (007.27).
299
+ if (this.is.address === true &&
300
+ FrameSymbol.DOTTED_OPERATORS.includes(this.data) &&
301
+ !(context instanceof FrameSymbol && context.isPendingDeclaration())) {
302
+ return new FrameOperator(this.data).called_by(context);
303
+ }
295
304
  // An address applied by a receiver reads that member, `o .x`. Two
296
305
  // receivers take the name itself instead (#429). A pending declaration
297
306
  // stores it, so `.k .x` keeps the key and `o k` reads `o.x` later; the
@@ -420,6 +429,24 @@ Object.defineProperty(FrameSymbol, "symbols", {
420
429
  writable: true,
421
430
  value: {}
422
431
  });
432
+ /**
433
+ * The arithmetic operators a dot may prefix and still mean the operator,
434
+ * `.+ .- .* ./ .%% .**`. They dispatch as the bare operator does: the left
435
+ * operand's declared method, then the global operation.
436
+ */
437
+ Object.defineProperty(FrameSymbol, "DOTTED_OPERATORS", {
438
+ enumerable: true,
439
+ configurable: true,
440
+ writable: true,
441
+ value: [
442
+ "+",
443
+ "-",
444
+ "*",
445
+ "/",
446
+ "%%",
447
+ "**",
448
+ ]
449
+ });
423
450
  export class FrameOperator extends FrameSymbol {
424
451
  constructor(source, meta = NilContext) {
425
452
  super(source, meta);
package/esm/lib/frames.js CHANGED
@@ -27,7 +27,7 @@ export { FrameObservation } from "./frames/frame-observation.js";
27
27
  export { FrameNumeric, NUMERIC_RANK, } from "./frames/frame-numeric.js";
28
28
  export { FrameInt } from "./frames/frame-int.js";
29
29
  export { FrameCount } from "./frames/frame-count.js";
30
- export { installDemiConstructor, observeDemi, } from "./frames/fourth-hash.js";
30
+ export { installDemiConstructor, installRouterRecognizer, observeDemi, } from "./frames/fourth-hash.js";
31
31
  export { FrameDecimal } from "./frames/frame-decimal.js";
32
32
  export { FrameSequence } from "./frames/frame-sequence.js";
33
33
  export { FrameTypedNumber } from "./frames/frame-typed-number.js";
@@ -3,6 +3,8 @@ import { Frame } from "../frames/frame.js";
3
3
  import { FrameNumeric } from "../frames/frame-numeric.js";
4
4
  import { FrameArray } from "../frames/frame-array.js";
5
5
  import { FrameCount } from "../frames/frame-count.js";
6
+ import { FrameGroup } from "../frames/frame-group.js";
7
+ import { REPETITION_ELEMENT_LIMIT } from "../frames/frame-numeric.js";
6
8
  /** Applies a numeric operation, or answers nil for any other pair. */
7
9
  const numeric = (source, block, operation) => {
8
10
  return source instanceof FrameNumeric && block instanceof FrameNumeric
@@ -40,7 +42,51 @@ export const Multiply = (source, block) => {
40
42
  return FrameArray.starCount(left, block);
41
43
  return Frame.error("$!.star-domain *");
42
44
  };
43
- export const Divide = (source, block) => arithmetic(source, block, (left, right) => left.divide(right));
45
+ /**
46
+ * `/`, also spelled `./`. A numeric pair keeps the exact numeric ladder. An
47
+ * ordered aggregate, an array or a group, divided by a positive Count n is
48
+ * dealt into n channels (007.27): the element at index i goes to channel
49
+ * i mod n, so `[A, B, C, D, E, F, G] / 3` is the n-channel `RotorRouter`
50
+ * whose channels are `[[A, D, G], [B, E], [C, F]]`. Anything else answers nil,
51
+ * as before.
52
+ */
53
+ export const Divide = (source, block) => {
54
+ const left = source.referent();
55
+ if (left instanceof FrameArray || left instanceof FrameGroup) {
56
+ return failure(left) ?? failure(block) ?? deal(left, block);
57
+ }
58
+ return arithmetic(source, block, (left, right) => left.divide(right));
59
+ };
60
+ let carrier;
61
+ /**
62
+ * Installs what list division answers its channels with.
63
+ *
64
+ * @param construct The constructor, called once per division.
65
+ */
66
+ export function installDivisionCarrier(construct) {
67
+ carrier = construct;
68
+ }
69
+ const deal = (source, block) => {
70
+ const divisor = block.referent();
71
+ if (!(divisor instanceof FrameCount) || typeof divisor.data !== "bigint") {
72
+ return Frame.error("$!.division-domain /");
73
+ }
74
+ if (divisor.data === 0n)
75
+ return Frame.error("$!.division-by-zero /");
76
+ const elements = source.elements();
77
+ if (divisor.data > REPETITION_ELEMENT_LIMIT &&
78
+ divisor.data > BigInt(elements.length)) {
79
+ return Frame.error(`$!.repetition-size ${REPETITION_ELEMENT_LIMIT}`);
80
+ }
81
+ const n = Number(divisor.data);
82
+ const channels = Array.from({ length: n }, () => []);
83
+ elements.forEach((element, index) => {
84
+ channels[index % n].push(element);
85
+ });
86
+ if (!carrier)
87
+ return Frame.error("$!.router-unavailable /");
88
+ return carrier(new FrameArray(channels.map((channel) => new FrameArray(channel))));
89
+ };
44
90
  export const Modulo = (source, block) => arithmetic(source, block, (left, right) => left.modulo(right));
45
91
  export const Power = (source, block) => arithmetic(source, block, (left, right) => left.power(right));
46
92
  export const Equals = (source, block) => {
@@ -1 +1 @@
1
- export const VERSION = "0.43.0";
1
+ export const VERSION = "0.44.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hclang",
3
- "version": "0.43.0",
3
+ "version": "0.44.0",
4
4
  "description": "Homoiconic C command-line interpreter and REPL",
5
5
  "keywords": [
6
6
  "language",