feat(v4): Phase 7 Task 4 -- the loops' words: v3's physics-freeze and inference words, F18 asking the kernel (#189)

capsules/v4/physics.4th, a kernel capsule on Hera (blocks 8200-8201): FREEZE-WORD UNFREEZE-WORD FROZEN?
HEAT! HEAT@ DECAY-RATE@ Q.VARIANCE INFER-DECAY-SLOPE INFER-WINDOW-WIDTH INFER-RUN INFER-WINDOW@ INFER-DECAY@
INFER-VARIANCE@ INFER-FIT@ INFER-EARLY-EXIT@ -- each a KERNEL-WORD, requests -21..-34 (v4/mesh.h), served by
the kernel on any node: "The compudynamics is stadium/fabric" (Captain Bob, 2026-11-14). A word is named as
v3's, the newest visible entry of CONTEXT then FORTH (the image gains CONTEXT's address); the arithmetic is
v4/system/inference.c, v3's to the bit. Q48.16 results are v4's doubles (low cell, then high). HEAT! starts
the word's decay from now. Test first with v3's POST cases in v4's address units: test_mesh_boot.c 86
checks, 95 on, 83 without opcode physics. docs/reference/v4-dictionary.tsv: the 15 words (392 live).
amd64 PASS, word_count=392. Not pushed (held to the end of Phase 7).

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 00:40:27 +00:00
parent e1b731f444
commit 28ffed8f57
12 changed files with 214 additions and 5 deletions
+3
View File
@@ -76,6 +76,9 @@ typedef struct {
* the dictionary. The kernel fills it, when its build has ENABLE_HOTWORDS_CACHE; otherwise it is empty. */
v4_cell hot;
/* CONTEXT: the variable that holds the address of the vocabulary names are looked up in first (dict.f18) */
v4_cell context;
/* the block window: the word address of the slots the kernel copies
* blocks into (v4/blocks.h) */
v4_cell block_window;
+16
View File
@@ -53,6 +53,22 @@
#define V4_MESH_WORD_IDS 1024u /* word IDs a record is kept for: v3's DICTIONARY_SIZE, the Stadium's bound */
#define V4_REQ_WORD_DEFINED (-19)
#define V4_REQ_WORD_FORGOTTEN (-20)
/* the loops' words (capsules/v4/physics.4th; #189 Phase 7, Task 4): a word named by (baddr u), as v3's, or cells
* of the node's memory (addr u) */
#define V4_REQ_FREEZE_WORD (-21) /* ( baddr u -- ) */
#define V4_REQ_UNFREEZE_WORD (-22) /* ( baddr u -- ) */
#define V4_REQ_FROZEN (-23) /* ( baddr u -- flag ) */
#define V4_REQ_HEAT_STORE (-24) /* ( n baddr u -- ) */
#define V4_REQ_HEAT_FETCH (-25) /* ( baddr u -- n ) */
#define V4_REQ_Q_VARIANCE (-26) /* ( addr u -- q ) */
#define V4_REQ_INFER_SLOPE (-27) /* ( addr u -- q ) */
#define V4_REQ_INFER_WIDTH (-28) /* ( addr u -- n ) */
#define V4_REQ_INFER_RUN (-29) /* ( -- ) */
#define V4_REQ_INFER_WINDOW (-30) /* ( -- u ) */
#define V4_REQ_INFER_DECAY (-31) /* ( -- q ) */
#define V4_REQ_INFER_VARIANCE (-32) /* ( -- q ) */
#define V4_REQ_INFER_FIT (-33) /* ( -- q ) */
#define V4_REQ_INFER_EARLY (-34) /* ( -- flag ) */
typedef struct {
v4_uheat_t calls; /* every call since the word was defined */
v4_uheat_t since; /* calls since the owner last charged them */
+102
View File
@@ -819,6 +819,107 @@ void v4_mesh_charge(v4_mesh *ms, unsigned place, void (*each)(void *ctx, v4_cell
}
}
/* ---- the loops' words (capsules/v4/physics.4th) ---- */
static unsigned byte_at(const v4_node *n, v4_cell baddr) /* four to a cell, low byte first (D-1) */
{
v4_cell w = baddr >> 2;
if (baddr < 0 || w >= (v4_cell)V4_NODE_WORDS) return 0;
return (unsigned)(((v4_ucell)n->mem[w] >> (8u * (unsigned)(baddr & 3))) & 0xFFu);
}
/* the newest visible entry named by (baddr u) -- CONTEXT's vocabulary, then FORTH, as (LOOKUP) -- or 0 */
static v4_cell word_named(const v4_node *n, v4_cell baddr, v4_cell u)
{
v4_cell heads[2], xt;
unsigned h, k, len = u > 31 ? 31u : (unsigned)u;
if (u <= 0) return 0;
heads[0] = n->mem[n->mem[the->im->context]];
heads[1] = n->mem[the->im->latest];
for (h = 0; h < 2; h++) {
for (xt = heads[h], k = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && k < V4_NODE_WORDS; xt = n->mem[xt - 1], k++) {
v4_cell nfa = n->mem[xt - 2];
unsigned i, ok = 1;
if (n->mem[xt - 3] & 2) continue; /* hidden */
if (byte_at(n, nfa * 4) != len) continue;
for (i = 0; i < len && ok; i++) if (byte_at(n, nfa * 4 + 1 + (v4_cell)i) != byte_at(n, baddr + (v4_cell)i)) ok = 0;
if (ok) return xt;
}
}
return 0;
}
/* u cells of the node's memory at addr, as v3's array of 64-bit heats (0 if out of range) */
static const uint64_t *cells_at(const v4_node *n, v4_cell addr, v4_cell u)
{
static uint64_t buf[V4_MESH_WINDOW];
v4_cell i;
if (u <= 0 || u > (v4_cell)V4_MESH_WINDOW || addr < 0 || addr + u > (v4_cell)V4_NODE_WORDS) return 0;
for (i = 0; i < u; i++) buf[i] = (uint64_t)(v4_ucell)n->mem[addr + i];
return buf;
}
/* a Q48.16 value as v4's: a double, low cell then high (qmath.f18: Q.1 is 65536 0), at either cell width */
static void push_q(v4_node *n, uint64_t q)
{
#if V4_CELL_BITS >= 64
v4_dstack_push(&n->ds, (v4_cell)q);
v4_dstack_push(&n->ds, 0);
#else
v4_dstack_push(&n->ds, (v4_cell)(v4_ucell)(q & 0xFFFFFFFFu));
v4_dstack_push(&n->ds, (v4_cell)(v4_ucell)(q >> 32));
#endif
}
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;
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);
xt = word_named(n, a, u);
r = xt ? (v4_mesh_account *)v4_mesh_account_at(the, place, xt) : 0;
if (value == V4_REQ_FREEZE_WORD) { if (r) r->state_flags |= V4_MESH_STATE_FROZEN; }
else if (value == V4_REQ_UNFREEZE_WORD) { if (r) r->state_flags &= (uint8_t)~V4_MESH_STATE_FROZEN; }
else if (value == V4_REQ_FROZEN) v4_dstack_push(&n->ds, r && (r->state_flags & V4_MESH_STATE_FROZEN) ? -1 : 0);
else if (value == V4_REQ_HEAT_FETCH) v4_dstack_push(&n->ds, r ? (v4_cell)r->heat : 0);
else { /* the heat stored is its heat now: decay counts from here */
v4_cell heat = v4_dstack_pop(&n->ds);
if (r) { r->heat = (v4_uheat_t)heat; if (p) r->last_decay_tick = p->tick; }
}
return 1;
case V4_REQ_Q_VARIANCE: case V4_REQ_INFER_SLOPE: case V4_REQ_INFER_WIDTH:
u = v4_dstack_pop(&n->ds); a = v4_dstack_pop(&n->ds);
d = cells_at(n, a, u);
if (value == V4_REQ_Q_VARIANCE) push_q(n, d ? v4_inference_variance(d, (uint64_t)u) : 0);
else if (value == V4_REQ_INFER_SLOPE) push_q(n, d ? v4_inference_decay_slope(d, (uint64_t)u) : 0);
else v4_dstack_push(&n->ds, d ? (v4_cell)v4_inference_window_width(d, (uint64_t)u) : 0);
return 1;
case V4_REQ_INFER_RUN: /* v3's: the engine run now on the window, outputs kept */
if (p) {
uint64_t have = p->win.total_executions < V4_MESH_WINDOW ? p->win.total_executions : V4_MESH_WINDOW;
uint64_t span = p->win.effective_window_size < have ? p->win.effective_window_size : have, i;
for (i = 0; i < span; i++) {
uint32_t id = p->win.history[(p->win.window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW];
trajectory[i] = (id < V4_MESH_WORD_IDS && accounts_of(place)[id].xt) ? (uint64_t)accounts_of(place)[id].heat : 0;
}
v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->inference);
}
return 1;
default:
if (value == V4_REQ_INFER_DECAY) push_q(n, p ? p->inference.adaptive_decay_slope : 0);
else if (value == V4_REQ_INFER_VARIANCE) push_q(n, p ? p->inference.window_variance_q48 : 0);
else if (value == V4_REQ_INFER_FIT) push_q(n, p ? p->inference.slope_fit_quality_q48 : 0);
else if (value == V4_REQ_INFER_WINDOW) v4_dstack_push(&n->ds, p ? (v4_cell)p->inference.adaptive_window_width : 0);
else v4_dstack_push(&n->ds, p ? (v4_cell)p->inference.early_exited : 0);
return 1;
}
}
static int kernel_take(void *self, v4_cell value) /* a request */
{
unsigned place = place_of_device(self);
@@ -827,6 +928,7 @@ static int kernel_take(void *self, v4_cell value) /* a request */
if (v4_blocks_serve(&the->blocks[place], &x->n, value)) return 1;
if (value == V4_REQ_WORD_DEFINED) { word_made(place, &x->n, v4_dstack_pop(&x->n.ds)); return 1; }
if (value == V4_REQ_WORD_FORGOTTEN) { words_gone(place, &x->n, v4_dstack_pop(&x->n.ds)); return 1; }
if (physics_serve(place, &x->n, value)) return 1;
if (place == the->hera_place && the->serve && value >= 1 && value <= (v4_cell)the->word_count) {
the->serve(&x->n, (unsigned)value);
return 1;
+34 -2
View File
@@ -81,7 +81,7 @@ static int key(void) { return V4_BOOT_KEY_END; }
static const char *const words[] = { "BYE" };
static unsigned served;
static void serve(v4_node *n, unsigned request) { (void)n; if (request == 1) served++; }
static const char *const capsules[] = { "v4:centre.4th", "v4:hera.4th" };
static const char *const capsules[] = { "v4:centre.4th", "v4:hera.4th", "v4:physics.4th" };
static unsigned char ram[(size_t)BLK_RAM_BLOCKS * BLK_FORTH_SIZE];
extern void sf_time_init(void);
@@ -110,7 +110,7 @@ int main(void)
ms.reg = reg; ms.blocks = blocks; ms.nucleus = &nucleus; ms.hera = &hera; ms.kernel = kernel;
ms.room = room; ms.unroom = unroom; ms.born = born; ms.gone = gone; ms.parity = parity;
ms.out = out; ms.key = key; ms.words = words; ms.word_count = 1; ms.serve = serve;
ms.capsules = capsules; ms.capsule_count = 2; ms.im = &im; ms.self = 0;
ms.capsules = capsules; ms.capsule_count = 3; ms.im = &im; ms.self = 0;
ms.accounts = accounts; ms.is_word = is_word; ms.forgot = forgot;
ms.physics = physics; ms.alloc = alloc; ms.release = release; ms.heartrate = heartrate;
@@ -218,6 +218,38 @@ int main(void)
r = line(V4_MESH_HERA, "FORGET L8T", &how);
}
/* ---- the loops' words (#189 Phase 7, Task 4): v3's physics-freeze and inference words, F18 asking the kernel,
* which keeps the records and does the arithmetic (Captain Bob, 2026-11-14: "The compudynamics is
* stadium/fabric"). v3's POST cases, with v4's address units (a cell is one) ---- */
{
r = line(V4_MESH_HERA, ": FT1 ; S\" FT1\" FREEZE-WORD S\" FT1\" FROZEN? .", &how);
CHECK(how == V4_TEXT_COMPLETED && strcmp(r, "-1 ") == 0, "FREEZE-WORD, then FROZEN?: -1 (\"%s\")", r);
r = line(V4_MESH_HERA, "S\" FT1\" UNFREEZE-WORD S\" FT1\" FROZEN? .", &how);
CHECK(strcmp(r, "0 ") == 0, "UNFREEZE-WORD: 0 (\"%s\")", r);
r = line(V4_MESH_HERA, ": FT2 ; S\" FT2\" FROZEN? . S\" __nonexistent_xyz__\" FROZEN? .", &how);
CHECK(strcmp(r, "0 0 ") == 0, "a fresh word is not frozen, and nor is one not found (\"%s\")", r);
r = line(V4_MESH_HERA, ": HT1 ; 999 S\" HT1\" HEAT! S\" HT1\" HEAT@ . : HT2 ; S\" HT2\" HEAT@ .", &how);
CHECK(strcmp(r, "999 0 ") == 0, "HEAT! 999 then HEAT@: 999; a fresh word's: 0 (\"%s\")", r);
r = line(V4_MESH_HERA, "DECAY-RATE@ .", &how);
CHECK(strcmp(r, "1 ") == 0, "DECAY-RATE@: v3's DECAY_RATE_PER_US_Q16, 1 (\"%s\")", r);
{
uint64_t d3[3] = { 100, 200, 300 }, u3[3] = { 300, 200, 100 };
char want[64];
r = line(V4_MESH_HERA, "100 HERE ! 200 HERE 1 + ! 300 HERE 2 + ! HERE 3 Q.VARIANCE Q.TO-INT .", &how);
snprintf(want, sizeof want, "%lu ", (unsigned long)(v4_inference_variance(d3, 3) >> 16));
CHECK(strcmp(r, want) == 0, "Q.VARIANCE of 100 200 300: \"%s\" (v3's engine: \"%s\")", r, want);
r = line(V4_MESH_HERA, "300 HERE ! 200 HERE 1 + ! 100 HERE 2 + ! HERE 3 INFER-DECAY-SLOPE Q.TO-INT .", &how);
snprintf(want, sizeof want, "%lu ", (unsigned long)(v4_inference_decay_slope(u3, 3) >> 16));
CHECK(strcmp(r, want) == 0, "INFER-DECAY-SLOPE of 300 200 100: \"%s\" (\"%s\")", r, want);
r = line(V4_MESH_HERA, "100 HERE ! 200 HERE 1 + ! 300 HERE 2 + ! HERE 3 INFER-WINDOW-WIDTH .", &how);
snprintf(want, sizeof want, "%u ", v4_inference_window_width(d3, 3));
CHECK(strcmp(r, want) == 0, "INFER-WINDOW-WIDTH of 100 200 300: \"%s\" (\"%s\")", r, want);
}
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);
r = line(V4_MESH_HERA, "FORGET FT1", &how);
}
/* ---- the opcodes (#189 Phase 7, ruling 7: every loop on the 32 F18 opcodes too; revert: OPCODE_PHYSICS 0) ---- */
{
v4_mesh_physics *p = &ms.physics[0];
+2 -2
View File
@@ -53,10 +53,10 @@ static int image_description(void)
printf("#include \"v4/image.h\"\n\nconst v4_image v4_map_image = {\n");
printf(" %uu, %uu, %uu, %uu,\n", (unsigned)V4_CELL_BITS, (unsigned)V4_NODE_WORDS, (unsigned)V4_DATA_RING, (unsigned)V4_RET_RING);
printf(" -1,\n -1, %ld,\n -1, -1,\n", (long)PORT);
printf(" %ld, %ld, %ld,\n %ld, %ld,\n %ld,\n %ld, %ld,\n %ld, %ld,\n %ld, %ld,\n %ld, %ld,\n %ld, %ld,\n %ld,\n %ld,\n",
printf(" %ld, %ld, %ld,\n %ld, %ld,\n %ld,\n %ld, %ld,\n %ld, %ld,\n %ld, %ld,\n %ld, %ld,\n %ld, %ld,\n %ld,\n %ld,\n %ld,\n",
(long)CONSOLE_TX, (long)CONSOLE_RX, (long)CONSOLE_ST, (long)DSTACK_REG, (long)RSTACK_REG,
(long)NODE_ERROR, (long)HEAT_REG, (long)ME, (long)DP, (long)LATEST, (long)BOOT_CELLS, (long)FENCE,
(long)NEXT_ID, (long)BOOT_ID, (long)WORD_DEFINED, (long)WORD_FORGOTTEN, (long)HOTVARS, (long)BUF0_W);
(long)NEXT_ID, (long)BOOT_ID, (long)WORD_DEFINED, (long)WORD_FORGOTTEN, (long)HOTVARS, (long)CONTEXT, (long)BUF0_W);
printf(" %ld, %ld, %ld\n};\n", (long)ROM_IMG, (long)ROM_FAILED, (long)ROM_ERRLINE);
return 0;
}