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:
@@ -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
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user