feat(v4): Phase 7 Task 4 -- v3's entropy, TOP-WORDS, WORD-ENTROPY and (- words (#189)

capsules/v4/physics.4th, blocks 8202-8204: ENTROPY@ ENTROPY! RESET-ENTROPY as kernel requests -35..-37 (a
word's heat by the address ' leaves, a data word's too; an error for what is not a word, as v3's);
TOP-WORDS and WORD-ENTROPY in StarForth, printing in v3's form what the kernel gives them through (HOT#)
(HOT) (XT-HEAT), -38..-40 (the i-th hottest in v3's stable order); (- in StarForth, skipping to the
matching close paren with nesting, as v3's code does. Tests first: test_mesh_boot.c 101 checks, 110 on, 98
without opcode physics. Listing: 403 live words. amd64 PASS, word_count=403. Not pushed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-14 01:31:31 +00:00
parent 855632b3c0
commit c64c70bdb7
9 changed files with 167 additions and 3 deletions
+41
View File
@@ -24,3 +24,44 @@ Block 8201
-34 KERNEL-WORD INFER-EARLY-EXIT@ ( -- flag )
( v3's DECAY_RATE_PER_US_Q16, its compile-time constant )
1 CONSTANT DECAY-RATE@
Block 8202
( v3's StarForth module: a word's execution heat, by the )
( address ' leaves; and the hottest -- the kernel's records )
-35 KERNEL-WORD ENTROPY@ ( addr -- n )
-36 KERNEL-WORD ENTROPY! ( n addr -- )
-37 KERNEL-WORD RESET-ENTROPY ( -- )
-38 KERNEL-WORD (HOT#) ( -- n )
-39 KERNEL-WORD (HOT) ( i -- xt heat )
-40 KERNEL-WORD (XT-HEAT) ( xt -- n )
( TOP-WORDS n: the n hottest, in v3's form )
: TOP-WORDS ( n -- )
DUP 1 < IF ." TOP-WORDS: invalid count " 0 .R CR EXIT THEN
." Top " DUP 0 .R ." most frequently used words:" CR
." ==================================" CR
(HOT#) MIN 0 ?DO I (HOT) SWAP I 1+ 0 .R ." . "
>NAME COUNT TYPE ." : " 0 .R CR LOOP ;
Block 8203
( WORD-ENTROPY: every word with heat, then the totals, as v3 )
VARIABLE (WE-T) VARIABLE (WE-N)
: WORD-ENTROPY ( -- )
." Word Usage Statistics (Execution Heat Counts):" CR
." =============================================" CR
0 (WE-T) ! 0 (WE-N) ! LATEST
BEGIN DUP WHILE 1 (WE-N) +!
DUP (XT-HEAT) ?DUP IF OVER >NAME COUNT TYPE ." : "
DUP 0 .R CR (WE-T) +! THEN >LINK @
REPEAT DROP
." -------------------------------------" CR
." Total executions: " (WE-T) @ 0 .R CR
." Total words: " (WE-N) @ 0 .R CR
(WE-T) @ IF ." Average executions per word: "
(WE-T) @ (WE-N) @ / 0 .R CR THEN ;
Block 8204
( (- skips its input to the matching close paren, nesting, )
( as v3's code does -- its documentation says to the line end )
: (- ( -- ) 1
BEGIN DUP 0= IF DROP EXIT THEN
>IN @ SOURCE SWAP DROP < 0= IF DROP EXIT THEN
SOURCE DROP >IN @ + C@ 1 >IN +!
DUP 40 = IF DROP 1+ ELSE 41 = IF 1- THEN THEN
AGAIN ;
+11
View File
@@ -7,10 +7,13 @@
#S ( ud -- 0 0 ) Pictured output: convert digits with # until the number is zero (at least one digit).
' ( -- addr ) ' xxx: the parameter field address of xxx; compiled as a literal when compiling.
( ( -- ) ( text): a comment, ended by the first ); immediate.
(- ( -- ) A comment: the input is skipped to the matching close paren, nesting, as v3's code does (its documentation says to the line's end).
(.") ( -- ) The run time of .": print the string compiled after the call, and go on after it.
(ABORT") ( flag -- ) The run time of ABORT": if flag is not zero, print the string compiled after the call and ABORT.
(BITS) ( -- n ) The number of bits in a cell, counted at run time (forth79.4th; used by U/MOD).
(CONSOLE) ( -- addr ) Variable: the node that what this node prints is sent to, as messages (core.f18).
(HOT#) ( -- n ) How many words have heat (TOP-WORDS's).
(HOT) ( i -- xt heat ) The i-th hottest word, as v3's TOP-WORDS orders them (ties in dictionary order); 0 0 past the last.
(LOG") ( level -- ) The run time of LOG-xxx": print the string compiled after the call if level is at or below LOG-LEVEL.
(LOST) ( -- addr ) Variable: a count of the messages this node has dropped -- nowhere to send them, or no room.
(ME) ( -- addr ) Variable: this node's number in the mesh.
@@ -21,8 +24,11 @@
(TELL-DEFINED) ( xt -- ) Tells the kernel that an entry has been made, with its code at xt (request -19 on port 0). The kernel puts it in (WORD-DEFINED) when it seals a node.
(TELL-FORGOTTEN) ( w -- ) Tells the kernel that every entry from word address w up has gone (request -20 on port 0). The kernel puts it in (WORD-FORGOTTEN) when it seals a node.
(UD) ( -- addr ) Variable: U/MOD's divisor while it divides (forth79.4th).
(WE-N) ( -- addr ) Variable: WORD-ENTROPY's count of the words.
(WE-T) ( -- addr ) Variable: WORD-ENTROPY's total of the heat.
(WORD-DEFINED) ( -- addr ) Variable: the hook told when an entry is made, with the xt its code will be at.
(WORD-FORGOTTEN) ( -- addr ) Variable: the hook told when entries are forgotten, with the word address they went from.
(XT-HEAT) ( xt -- n ) The heat of the word at xt; 0 for what is not a word.
* ( n1 n2 -- n3 ) Multiply: the low cell of the product.
*/ ( n1 n2 n3 -- n4 ) n1 times n2 divided by n3, through a double-cell intermediate product.
*/MOD ( n1 n2 n3 -- n4 n5 ) n1 times n2 divided by n3, through a double product: n4 the remainder, n5 the quotient.
@@ -162,6 +168,8 @@ EMPTY-BUFFERS ( -- ) Forget what is in the block buffers; nothing is written.
ENCLOSE ( baddr c -- baddr n1 n2 n3 ) Find a word delimited by c in the string at baddr: n1 its start, n2 its end, n3 the offset after the delimiter.
ENDCASE ( x -- ) Compile-only: end a CASE selection, dropping the value if no OF matched.
ENDOF ( -- ) Compile-only: end an OF branch; go on after ENDCASE.
ENTROPY! ( n addr -- ) The execution heat of the word at addr set to n; its decay counts from now. An error if addr is not a word's.
ENTROPY@ ( addr -- n ) The execution heat of the word at addr (what ' leaves), from the kernel's record of it, as v3's. An error if addr is not a word's.
ERASE ( baddr u -- ) Fill u bytes from baddr with zeros.
EXECUTE ( xt -- ) Run the word whose execution token is xt.
EXIT ( -- ) Compile-only: return from the definition at this point.
@@ -284,6 +292,7 @@ QUIT ( -- ) Empty the return stack, set execution mode and go back to the termin
R> ( -- x ) ( R: x -- ) Compile-only: move the top of the return stack to the data stack.
R@ ( -- x ) ( R: x -- x ) Compile-only: copy the top of the return stack.
REPEAT ( -- ) Compile-only: end a BEGIN ... WHILE ... REPEAT loop, jumping back to BEGIN.
RESET-ENTROPY ( -- ) Every word's heat and temperature back to 0, as v3's.
ROLL ( n -- ) Move the n-th value, not counting n itself, to the top: 2 ROLL is ROT.
ROT ( x1 x2 x3 -- x2 x3 x1 ) Rotate the third value to the top.
ROUTE ( node port -- ) The port, by its number, leads toward that node.
@@ -308,6 +317,7 @@ SWAP ( x1 x2 -- x2 x1 ) Exchange the top two values.
THEN ( -- ) Compile-only: end an IF ... THEN or IF ... ELSE ... THEN.
THRU ( first last -- ) LOAD each block from first to last.
TIB ( -- baddr ) The terminal input buffer.
TOP-WORDS ( n -- ) Print the n hottest words and their heat, in v3's form.
TRAVERSE ( baddr n -- baddr' ) Step across a name field: for n > 0 from its count byte to its last byte, for n < 0 back.
TRUE ( -- -1 ) Constant: the true flag, all bits set.
TYPE ( baddr u -- ) Print the u characters at baddr.
@@ -330,6 +340,7 @@ WARM ( -- ) Print the system's message and ABORT: both stacks emptied, the promp
WHILE ( flag -- ) Compile-only: in BEGIN ... WHILE ... REPEAT, leave the loop if flag is zero.
WITHIN ( n low high -- flag ) True if low <= n < high.
WORD ( c -- baddr ) Read the next word from the input stream, delimited by c, as a counted string.
WORD-ENTROPY ( -- ) Print every word with heat and its heat, newest first, then the totals, in v3's form.
WORDS ( -- ) List the names in the CONTEXT vocabulary, the newest first.
XOR ( x1 x2 -- x3 ) Bitwise exclusive or.
[ ( -- ) Stop compiling: interpret what follows; immediate.
Can't render this file because it contains an unexpected character in line 10 and column 3.
@@ -187,8 +187,14 @@ Phase 4).
code defined at run time (dictionary space), or is INTERPRET's or EXECUTE's dispatch; the system's own
calls among its words, none. `3 4 + .` counts 4, as v3. Test first (90 checks; 99 on; 87 without
opcode physics). amd64 PASS: promotions=136 (fewer: the interpreter's own calls are no longer words run).
- [ ] The entropy and top-words words (`starforth_words_test.c`): `ENTROPY!` `ENTROPY@` `RESET-ENTROPY`
`TOP-WORDS` `WORD-ENTROPY`.
- [x] v3's StarForth module's physics words, in `physics.4th` (blocks 8202-8204): `ENTROPY@` `ENTROPY!`
(a word's heat by the address ' leaves; an error for what is not a word, as v3's) and
`RESET-ENTROPY` -- requests -35..-37; `TOP-WORDS` and `WORD-ENTROPY` in StarForth, printing what the
kernel gives them (-38..-40: how many have heat, the i-th hottest in v3's stable order, a word's
heat), in v3's form; `(-` in StarForth, skipping to the matching close paren with nesting as v3's
code does. Test first (101 checks; 110 on; 98 without opcode physics). Listing: 403 live words.
**For Task 8:** WORD-ENTROPY prints "Total words", and v4's dictionary is not v3's size -- that line
cannot print v3's number; TOP-WORDS' hottest words are v4's system words, not v3's.
### Task 5: the StarForth words (Phase 3's: `init.4th`, `starforth.4th`), and their POST
### Task 6: the ACL (Phase 4's: `ACL.4th`, the word card, the countdown), and its POST
### Task 7: the block cases (Phase 5's part)
+1
View File
@@ -0,0 +1 @@
Development boot, not acceptance evidence (2026-11-14): amd64 with #189 Phase 7 Task 4 -- the entropy, TOP-WORDS, WORD-ENTROPY and (- words in physics.4th. RESULT: PASS; POST 539/539; word_count=403; CONSERVED.
Binary file not shown.
+7
View File
@@ -69,6 +69,13 @@
#define V4_REQ_INFER_VARIANCE (-32) /* ( -- q ) */
#define V4_REQ_INFER_FIT (-33) /* ( -- q ) */
#define V4_REQ_INFER_EARLY (-34) /* ( -- flag ) */
/* v3's StarForth module: a word's execution heat by the address ' leaves, and the hottest */
#define V4_REQ_ENTROPY_FETCH (-35) /* ( addr -- n ): an error if addr is not a word's */
#define V4_REQ_ENTROPY_STORE (-36) /* ( n addr -- ) */
#define V4_REQ_RESET_ENTROPY (-37) /* ( -- ): every word's heat and temperature to 0 */
#define V4_REQ_HOT_COUNT (-38) /* ( -- n ): words with heat */
#define V4_REQ_HOT_NTH (-39) /* ( i -- xt heat ): the i-th hottest, as v3's TOP-WORDS orders them; 0 0 past them */
#define V4_REQ_XT_HEAT (-40) /* ( xt -- n ): a word's heat, 0 for what is not one */
typedef struct {
v4_uheat_t calls; /* every call since the word was defined */
v4_uheat_t since; /* calls since the owner last charged them */
+69 -1
View File
@@ -888,13 +888,81 @@ static void push_q(v4_node *n, uint64_t q)
#endif
}
/* the record of the word at addr -- its xt, or for a data word the cell after (what ' leaves) -- or 0 */
static v4_mesh_account *record_at(unsigned place, const v4_node *n, v4_cell addr)
{
v4_mesh_account *r = (v4_mesh_account *)v4_mesh_account_at(the, place, addr);
if (!r && addr > 5 && (n->mem[addr - 1 - 3] & 4)) r = (v4_mesh_account *)v4_mesh_account_at(the, place, addr - 1);
return r;
}
/* the words with heat, newest first, then sorted hottest first as v3's TOP-WORDS (a stable sort: ties keep
* dictionary order) */
static uint32_t hot_list(unsigned place, const v4_node *n, v4_cell *xts, v4_uheat_t *heats, uint32_t max)
{
v4_cell xt;
uint32_t k = 0, i, j;
for (xt = n->mem[the->im->latest], i = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && i < V4_NODE_WORDS && k < max; xt = n->mem[xt - 1], i++) {
const v4_mesh_account *a = v4_mesh_account_at(the, place, xt);
if (a && a->heat > 0) { xts[k] = xt; heats[k] = a->heat; k++; }
}
for (i = 1; i < k; i++) { /* insertion: stable, as v3's bubble sort */
v4_cell x = xts[i]; v4_uheat_t h = heats[i];
for (j = i; j > 0 && heats[j - 1] < h; j--) { xts[j] = xts[j - 1]; heats[j] = heats[j - 1]; }
xts[j] = x; heats[j] = h;
}
return k;
}
static int entropy_serve(unsigned place, v4_node *n, v4_cell value)
{
static v4_cell xts[1000];
static v4_uheat_t heats[1000];
v4_mesh_account *r;
v4_cell a, v;
uint32_t k;
switch (value) {
case V4_REQ_ENTROPY_FETCH: case V4_REQ_ENTROPY_STORE:
a = v4_dstack_pop(&n->ds);
v = value == V4_REQ_ENTROPY_STORE ? v4_dstack_pop(&n->ds) : 0;
r = record_at(place, n, a);
if (!r) { v4_node_store(n, n->error_reg, 12); return 1; } /* not a word: v3's is an error too */
if (value == V4_REQ_ENTROPY_FETCH) v4_dstack_push(&n->ds, (v4_cell)r->heat);
else { r->heat = (v4_uheat_t)v; if (the->physics) r->last_decay_tick = the->physics[place].tick; }
return 1;
case V4_REQ_RESET_ENTROPY:
for (k = 0; k < V4_MESH_WORD_IDS; k++) {
v4_mesh_account *x = &accounts_of(place)[k];
if (x->xt && x->heat > 0) { x->heat = 0; x->temperature_q8 = 0; }
}
return 1;
case V4_REQ_HOT_COUNT:
v4_dstack_push(&n->ds, (v4_cell)hot_list(place, n, xts, heats, 1000));
return 1;
case V4_REQ_HOT_NTH:
a = v4_dstack_pop(&n->ds);
k = hot_list(place, n, xts, heats, 1000);
if (a >= 0 && (uint32_t)a < k) { v4_dstack_push(&n->ds, xts[a]); v4_dstack_push(&n->ds, (v4_cell)heats[a]); }
else { v4_dstack_push(&n->ds, 0); v4_dstack_push(&n->ds, 0); }
return 1;
default: /* V4_REQ_XT_HEAT */
a = v4_dstack_pop(&n->ds);
{
const v4_mesh_account *x = v4_mesh_account_at(the, place, a);
v4_dstack_push(&n->ds, x ? (v4_cell)x->heat : 0);
}
return 1;
}
}
static int physics_serve(unsigned place, v4_node *n, v4_cell value)
{
v4_mesh_physics *p = the->physics ? &the->physics[place] : 0;
v4_cell u, a, xt;
v4_mesh_account *r;
const uint64_t *d;
if (value > V4_REQ_FREEZE_WORD || value < V4_REQ_INFER_EARLY) return 0;
if (value > V4_REQ_FREEZE_WORD || value < V4_REQ_XT_HEAT) return 0;
if (value <= V4_REQ_ENTROPY_FETCH) return entropy_serve(place, n, value);
switch (value) {
case V4_REQ_FREEZE_WORD: case V4_REQ_UNFREEZE_WORD: case V4_REQ_FROZEN: case V4_REQ_HEAT_FETCH: case V4_REQ_HEAT_STORE:
u = v4_dstack_pop(&n->ds); a = v4_dstack_pop(&n->ds);
+27
View File
@@ -277,6 +277,33 @@ int main(void)
}
r = line(V4_MESH_HERA, "INFER-RUN INFER-WINDOW@ . INFER-DECAY@ Q.TO-INT . INFER-VARIANCE@ Q.TO-INT . INFER-FIT@ Q.TO-INT . INFER-EARLY-EXIT@ .", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "ERROR") == 0, "INFER-RUN and its five readings, with no error: \"%s\"", r);
/* v3's StarForth module: the entropy words (a word's execution heat, by the address ' leaves), TOP-WORDS,
* WORD-ENTROPY, (- */
r = line(V4_MESH_HERA, ": NEWWORD ; ' NEWWORD ENTROPY@ .", &how);
CHECK(strcmp(r, "0 ") == 0, "ENTROPY@ of an unused word: 0 (\"%s\")", r);
r = line(V4_MESH_HERA, ": ENTTEST 1 DROP ; : E3 ENTTEST ENTTEST ENTTEST ; E3 ' ENTTEST ENTROPY@ .", &how);
CHECK(strcmp(r, "3 ") == 0, "ENTROPY@ counts each call (\"%s\")", r);
r = line(V4_MESH_HERA, ": ENTSET ; 42 ' ENTSET ENTROPY! ' ENTSET ENTROPY@ . 10 ' ENTSET ENTROPY! 20 ' ENTSET ENTROPY! ' ENTSET ENTROPY@ .", &how);
CHECK(strcmp(r, "42 20 ") == 0, "ENTROPY! sets it, and again (\"%s\")", r);
r = line(V4_MESH_HERA, "0 ENTROPY@", &how);
CHECK(how == V4_TEXT_ERROR, "ENTROPY@ of what is not a word is an error, as v3's (%d \"%s\")", how, r);
r = line(V4_MESH_HERA, "42 0 ENTROPY!", &how);
CHECK(how == V4_TEXT_ERROR, "and ENTROPY! (%d)", how);
r = line(V4_MESH_HERA, "-5 TOP-WORDS", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "TOP-WORDS: invalid count -5\n") == 0, "TOP-WORDS of -5: v3's message (\"%s\")", r);
r = line(V4_MESH_HERA, "900000000 ' ENTSET ENTROPY! 899999999 ' ENTTEST ENTROPY! 2 TOP-WORDS", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "Top 2 most frequently used words:\n==================================\n1. ENTSET: 900000000\n2. ENTTEST: 899999999\n") == 0,
"TOP-WORDS 2: the two hottest, in v3's form (\"%s\")", r);
r = line(V4_MESH_HERA, "WORD-ENTROPY", &how);
CHECK(how == V4_TEXT_COMPLETED && strstr(r, "Word Usage Statistics (Execution Heat Counts):\n") == r
&& strstr(r, "ENTSET: 900000000\n") && strstr(r, "Total executions: ") && strstr(r, "Total words: "),
"WORD-ENTROPY: v3's report (\"%.120s...\")", r);
r = line(V4_MESH_HERA, "RESET-ENTROPY ' ENTSET ENTROPY@ . ' ENTTEST ENTROPY@ .", &how);
CHECK(strcmp(r, "0 0 ") == 0, "RESET-ENTROPY: every word's heat back to 0 (\"%s\")", r);
r = line(V4_MESH_HERA, "1 (- comment here ( nested ) still ) 2 + .", &how);
CHECK(strcmp(r, "3 ") == 0, "(- skips to its ), nesting as v3's code does (\"%s\")", r);
r = line(V4_MESH_HERA, "DEPTH (- comment ) DEPTH SWAP - .", &how);
CHECK(strcmp(r, "1 ") == 0, "and touches no stack: the second DEPTH sees only the first's (\"%s\")", r);
r = line(V4_MESH_HERA, "FORGET FT1", &how);
}