interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / physics0 / pd0_relation.c

397 lines · 17872 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "physics0/pd0_relation.h"
18
19#include <math.h>
20#include <stdio.h>
21#include <string.h>
22
23#include "physics0/pd0_rng.h"
24#include "sha256.h"
25
26int pd0_relation_valid(const pd0_relation *r) {
27 if (!r || r->n_vars < 1 || r->n_vars > PD0_MAX_VARS || r->n_latent > PD0_MAX_LATENT || r->n_latent >= r->n_vars)
28 return -1;
29 if (r->n_channels < 1 || r->n_channels > PD0_MAX_CH || r->n_eq > r->n_vars) return -1;
30 for (int i = 0; i < r->n_eq; i++) {
31 const pd0_eq *e = &r->eq[i];
32 if (e->target >= r->n_vars || e->n_terms > PD0_MAX_TERMS) return -1;
33 for (int j = i + 1; j < r->n_eq; j++)
34 if (r->eq[j].target == e->target) return -1;
35 for (int t = 0; t < e->n_terms; t++) {
36 if (e->t[t].coef == 0) return -1;
37 for (int x = r->n_vars; x < PD0_MAX_VARS; x++)
38 if (e->t[t].ex[x]) return -1;
39 for (int x = PD0_MAX_VARS + r->n_channels; x < PD0_MAX_EX; x++)
40 if (e->t[t].ex[x]) return -1;
41 }
42 }
43 return 0;
44}
45
46int pd0_relation_size(const pd0_relation *r) {
47 int n = r->n_latent;
48 for (int i = 0; i < r->n_eq; i++) n += r->eq[i].n_terms;
49 return n;
50}
51
52int pd0_relation_degree_ok(const pd0_relation *r) {
53 for (int i = 0; i < r->n_eq; i++)
54 for (int t = 0; t < r->eq[i].n_terms; t++) {
55 int d = 0;
56 for (int x = 0; x < PD0_MAX_EX; x++) d += r->eq[i].t[t].ex[x];
57 if (d > PD0_MAX_DEG) return 0;
58 }
59 return 1;
60}
61
62uint32_t pd0_relation_description_bits(const pd0_relation *r) {
63 uint32_t bits = 8u * r->n_latent, per = 8u * ((uint32_t)r->n_vars + r->n_channels) + 24u;
64 for (int i = 0; i < r->n_eq; i++) bits += per * r->eq[i].n_terms;
65 return bits;
66}
67
68int64_t pd0_monomial(const pd0_term *t, const int64_t *vars, const int64_t *u, int n_vars, int n_ch) {
69 int64_t m = PD0_MICRO;
70 for (int v = 0; v < n_vars; v++)
71 for (int p = 0; p < t->ex[v]; p++) m = pd0_mul(m, vars[v]);
72 for (int c = 0; c < n_ch; c++)
73 for (int p = 0; p < t->ex[PD0_MAX_VARS + c]; p++) m = pd0_mul(m, u[c]);
74 return m;
75}
76
77void pd0_relation_step(const pd0_relation *r, const int64_t *in, const int64_t *u, int64_t *out) {
78 for (int v = 0; v < r->n_vars; v++) out[v] = in[v];
79 for (int i = 0; i < r->n_eq; i++) {
80 const pd0_eq *e = &r->eq[i];
81 int64_t d = 0;
82 for (int t = 0; t < e->n_terms; t++)
83 d += pd0_mul(e->t[t].coef, pd0_monomial(&e->t[t], in, u, r->n_vars, r->n_channels));
84 out[e->target] = in[e->target] + d;
85 }
86}
87
88void pd0_relation_rollout(const pd0_relation *r, const pd0_sched *s, pd0_traj *out) {
89 int64_t cur[PD0_MAX_VARS] = {0}, nxt[PD0_MAX_VARS], u[PD0_MAX_CH];
90 out->n_obs = s->n_obs;
91 out->n = s->n_steps;
92 for (int v = 0; v < s->n_obs; v++) { cur[v] = s->reset[v]; out->s[0][v] = cur[v]; }
93 for (int i = 0; i < s->n_steps; i++) {
94 memset(u, 0, sizeof u);
95 if (s->channel[i] < r->n_channels) u[s->channel[i]] = s->value[i];
96 pd0_relation_step(r, cur, u, nxt);
97 for (int v = 0; v < r->n_vars; v++) {
98 /* keep a diverging rollout finite: clamp far outside the world box */
99 if (nxt[v] > 100 * PD0_BOUND) nxt[v] = 100 * PD0_BOUND;
100 if (nxt[v] < -100 * PD0_BOUND) nxt[v] = -100 * PD0_BOUND;
101 cur[v] = nxt[v];
102 }
103 for (int v = 0; v < s->n_obs; v++) out->s[i + 1][v] = cur[v];
104 }
105}
106
107double pd0_nrmse(const pd0_traj *pred, const pd0_traj *truth, int n_traj) {
108 double worst = 0.0;
109 int n_obs = n_traj > 0 ? truth[0].n_obs : 0;
110 for (int j = 0; j < n_obs; j++) {
111 double se = 0, sum = 0, sq = 0;
112 long n = 0;
113 for (int k = 0; k < n_traj; k++)
114 for (int i = 1; i <= truth[k].n; i++) {
115 double t = (double)truth[k].s[i][j] / 1e6, p = (double)pred[k].s[i][j] / 1e6;
116 se += (p - t) * (p - t); sum += t; sq += t * t; n++;
117 }
118 if (n == 0) return INFINITY;
119 double mean = sum / (double)n, var = sq / (double)n - mean * mean;
120 double sd = var > 0 ? sqrt(var) : 0.0, rmse = sqrt(se / (double)n);
121 double v = sd > 0 ? rmse / sd : (rmse > 0 ? INFINITY : 0.0);
122 if (v > worst) worst = v;
123 }
124 return worst;
125}
126
127void pd0_sched_hash(const pd0_sched *s, uint8_t out[PD0_HASH]) {
128 uint8_t buf[2 + 8 * PD0_MAX_OBS + 9 * PD0_SCHED_STEPS];
129 size_t n = 0;
130 buf[n++] = s->n_obs; buf[n++] = s->n_steps;
131 for (int v = 0; v < s->n_obs; v++) { pd0_put_u64(buf + n, (uint64_t)s->reset[v]); n += 8; }
132 for (int i = 0; i < s->n_steps; i++) { buf[n++] = s->channel[i]; pd0_put_u64(buf + n, (uint64_t)s->value[i]); n += 8; }
133 sha256_hash(buf, n, out);
134}
135
136static const pd0_term *find_term(const pd0_eq *e, const pd0_term *t) {
137 for (int i = 0; i < e->n_terms; i++)
138 if (memcmp(e->t[i].ex, t->ex, PD0_MAX_EX) == 0) return &e->t[i];
139 return NULL;
140}
141static const pd0_eq *find_eq(const pd0_relation *r, int target) {
142 for (int i = 0; i < r->n_eq; i++)
143 if (r->eq[i].target == target) return &r->eq[i];
144 return NULL;
145}
146
147int pd0_relation_supports(const pd0_relation *r, const pd0_relation *truth) {
148 for (int i = 0; i < truth->n_eq; i++) {
149 const pd0_eq *e = find_eq(r, truth->eq[i].target);
150 if (!e) return 0;
151 for (int t = 0; t < truth->eq[i].n_terms; t++)
152 if (!find_term(e, &truth->eq[i].t[t])) return 0;
153 }
154 return 1;
155}
156
157int64_t pd0_relation_coef_err_ppm(const pd0_relation *r, const pd0_relation *truth) {
158 if (!pd0_relation_supports(r, truth)) return -1;
159 int64_t worst = 0;
160 for (int i = 0; i < truth->n_eq; i++) {
161 const pd0_eq *e = find_eq(r, truth->eq[i].target);
162 for (int t = 0; t < truth->eq[i].n_terms; t++) {
163 const pd0_term *tt = &truth->eq[i].t[t], *mine = find_term(e, tt);
164 pd0_i128 d = (pd0_i128)mine->coef - tt->coef;
165 if (d < 0) d = -d;
166 int64_t tv = tt->coef < 0 ? -tt->coef : tt->coef;
167 int64_t ppm = tv ? (int64_t)(d * 1000000 / tv) : (d ? INT64_MAX : 0);
168 if (ppm > worst) worst = ppm;
169 }
170 }
171 return worst;
172}
173
174/* ---- PDLAW1 ---- */
175static const uint8_t LAW_MAGIC[8] = {'P', 'D', 'L', 'A', 'W', '1', 0, 0};
176
177uint32_t pd0_confidence_ppm(uint32_t p, uint32_t f) {
178 return (uint32_t)((1000000ull * (p + 1)) / (uint64_t)(p + f + 2));
179}
180
181size_t pd0_claim_text(const pd0_law *l, char *buf, size_t cap) {
182 int n = snprintf(buf, cap,
183 "Across %llu observations under %u episodes, %u variables in the reset box [%lld, %lld] micro, "
184 "episode length %u, this relation predicts within %u.%06u (rollout NRMSE, 20 steps). "
185 "Confidence %u ppm. %u exceptions recorded.",
186 (unsigned long long)l->dom.n_observations, l->dom.n_episodes, l->rel.n_vars - l->rel.n_latent,
187 (long long)l->dom.reset_min, (long long)l->dom.reset_max, l->dom.episode_len,
188 l->eps_micro / 1000000u, l->eps_micro % 1000000u, l->confidence_ppm, l->n_exceptions);
189 return n < 0 ? 0 : (size_t)n;
190}
191
192size_t pd0_law_encode(pd0_law *l, uint8_t *out, size_t cap) {
193 if (pd0_relation_valid(&l->rel) != 0 || l->state > PD0_LAW_PROVISIONAL) return 0;
194 if (l->n_exceptions > PD0_MAX_EXCEPT || l->n_experiments > PD0_MAX_EXPER) return 0;
195 uint8_t *p = out, *end = out + cap;
196#define NEED(k) do { if ((size_t)(end - p) < (size_t)(k)) return 0; } while (0)
197 NEED(8 + 2 + 1 + PD0_HASH);
198 memcpy(p, LAW_MAGIC, 8); p += 8;
199 pd0_put_u16(p, 1); p += 2;
200 *p++ = l->state;
201 uint8_t *law_id_at = p;
202 memset(p, 0, PD0_HASH); p += PD0_HASH;
203 /* relationship block (spec 7 + n_channels byte, see CALIBRATION.md) */
204 const pd0_relation *r = &l->rel;
205 int nex = r->n_vars + r->n_channels;
206 NEED(10);
207 *p++ = r->n_vars; *p++ = r->n_latent; *p++ = r->n_channels;
208 pd0_put_u32(p, pd0_relation_description_bits(r)); p += 4;
209 pd0_put_u16(p, l->n_refutations); p += 2;
210 *p++ = r->n_eq;
211 for (int i = 0; i < r->n_eq; i++) {
212 const pd0_eq *e = &r->eq[i];
213 NEED(3);
214 *p++ = e->target; pd0_put_u16(p, e->n_terms); p += 2;
215 for (int t = 0; t < e->n_terms; t++) {
216 NEED(8 + nex);
217 pd0_put_u64(p, (uint64_t)e->t[t].coef); p += 8;
218 for (int v = 0; v < r->n_vars; v++) *p++ = e->t[t].ex[v];
219 for (int c = 0; c < r->n_channels; c++) *p++ = e->t[t].ex[PD0_MAX_VARS + c];
220 }
221 }
222 /* observed_domain block */
223 int n_obs = r->n_vars - r->n_latent;
224 NEED(16 * (n_obs + r->n_channels) + 8 + 8 + 4 + 16 + 4 + 8);
225 for (int v = 0; v < n_obs; v++) { pd0_put_u64(p, (uint64_t)l->dom.var_min[v]); p += 8; pd0_put_u64(p, (uint64_t)l->dom.var_max[v]); p += 8; }
226 for (int c = 0; c < r->n_channels; c++) { pd0_put_u64(p, (uint64_t)l->dom.ch_min[c]); p += 8; pd0_put_u64(p, (uint64_t)l->dom.ch_max[c]); p += 8; }
227 pd0_put_u64(p, (uint64_t)l->dom.dt_micro); p += 8;
228 pd0_put_u64(p, l->dom.n_observations); p += 8;
229 pd0_put_u32(p, l->dom.n_episodes); p += 4;
230 pd0_put_u64(p, (uint64_t)l->dom.reset_min); p += 8;
231 pd0_put_u64(p, (uint64_t)l->dom.reset_max); p += 8;
232 pd0_put_u32(p, l->dom.episode_len); p += 4;
233 pd0_put_u64(p, (uint64_t)l->dom.latent_reset); p += 8;
234 NEED(4 + 2);
235 pd0_put_u32(p, l->confidence_ppm); p += 4;
236 pd0_put_u16(p, l->n_exceptions); p += 2;
237 for (int i = 0; i < l->n_exceptions; i++) {
238 NEED(8 + PD0_HASH + 24);
239 pd0_put_u64(p, l->exc[i].seq); p += 8;
240 memcpy(p, l->exc[i].hash, PD0_HASH); p += PD0_HASH;
241 pd0_put_u64(p, (uint64_t)l->exc[i].predicted); p += 8;
242 pd0_put_u64(p, (uint64_t)l->exc[i].observed); p += 8;
243 pd0_put_u64(p, (uint64_t)l->exc[i].error_micro); p += 8;
244 }
245 NEED(2);
246 pd0_put_u16(p, l->n_experiments); p += 2;
247 for (int i = 0; i < l->n_experiments; i++) {
248 NEED(3 * PD0_HASH + 2 + 16);
249 memcpy(p, l->exp[i].id, PD0_HASH); p += PD0_HASH;
250 *p++ = l->exp[i].kind;
251 memcpy(p, l->exp[i].prereg, PD0_HASH); p += PD0_HASH;
252 memcpy(p, l->exp[i].outcome, PD0_HASH); p += PD0_HASH;
253 *p++ = l->exp[i].result;
254 pd0_put_u64(p, l->exp[i].first_seq); p += 8;
255 pd0_put_u64(p, l->exp[i].last_seq); p += 8;
256 }
257 NEED(PD0_HASH + 2);
258 memcpy(p, l->chain_root, PD0_HASH); p += PD0_HASH;
259 char claim[512];
260 size_t cl = pd0_claim_text(l, claim, sizeof claim);
261 NEED(2 + cl);
262 pd0_put_u16(p, (uint16_t)cl); p += 2;
263 memcpy(p, claim, cl); p += cl;
264#undef NEED
265 size_t total = (size_t)(p - out);
266 sha256_hash(out, total, l->law_id);
267 memcpy(law_id_at, l->law_id, PD0_HASH);
268 return total;
269}
270
271int pd0_law_decode(const uint8_t *in, size_t len, pd0_law *l) {
272 if (len < 8 + 2 + 1 + PD0_HASH + 10 || memcmp(in, LAW_MAGIC, 8) != 0) return -1;
273 if (pd0_get_u16(in + 8) != 1) return -1;
274 memset(l, 0, sizeof *l);
275 const uint8_t *p = in + 10, *end = in + len;
276#define NEED(k) do { if ((size_t)(end - p) < (size_t)(k)) return -1; } while (0)
277 l->state = *p++;
278 if (l->state > PD0_LAW_PROVISIONAL) return -1;
279 memcpy(l->law_id, p, PD0_HASH); p += PD0_HASH;
280 pd0_relation *r = &l->rel;
281 NEED(10);
282 r->n_vars = *p++; r->n_latent = *p++; r->n_channels = *p++;
283 uint32_t dbits = pd0_get_u32(p); p += 4;
284 l->n_refutations = pd0_get_u16(p); p += 2;
285 r->n_eq = *p++;
286 if (r->n_vars > PD0_MAX_VARS || r->n_channels > PD0_MAX_CH || r->n_eq > PD0_MAX_VARS) return -1;
287 for (int i = 0; i < r->n_eq; i++) {
288 NEED(3);
289 pd0_eq *e = &r->eq[i];
290 e->target = *p++; unsigned nt = pd0_get_u16(p); p += 2;
291 if (nt > PD0_MAX_TERMS) return -1;
292 e->n_terms = (uint8_t)nt;
293 for (unsigned t = 0; t < nt; t++) {
294 NEED(8 + r->n_vars + r->n_channels);
295 e->t[t].coef = (int64_t)pd0_get_u64(p); p += 8;
296 for (int v = 0; v < r->n_vars; v++) e->t[t].ex[v] = *p++;
297 for (int c = 0; c < r->n_channels; c++) e->t[t].ex[PD0_MAX_VARS + c] = *p++;
298 }
299 }
300 if (pd0_relation_valid(r) != 0 || dbits != pd0_relation_description_bits(r)) return -1;
301 int n_obs = r->n_vars - r->n_latent;
302 NEED(16 * (n_obs + r->n_channels) + 8 + 8 + 4 + 16 + 4 + 8 + 4 + 2);
303 for (int v = 0; v < n_obs; v++) { l->dom.var_min[v] = (int64_t)pd0_get_u64(p); p += 8; l->dom.var_max[v] = (int64_t)pd0_get_u64(p); p += 8; }
304 for (int c = 0; c < r->n_channels; c++) { l->dom.ch_min[c] = (int64_t)pd0_get_u64(p); p += 8; l->dom.ch_max[c] = (int64_t)pd0_get_u64(p); p += 8; }
305 l->dom.dt_micro = (int64_t)pd0_get_u64(p); p += 8;
306 l->dom.n_observations = pd0_get_u64(p); p += 8;
307 l->dom.n_episodes = pd0_get_u32(p); p += 4;
308 l->dom.reset_min = (int64_t)pd0_get_u64(p); p += 8;
309 l->dom.reset_max = (int64_t)pd0_get_u64(p); p += 8;
310 l->dom.episode_len = pd0_get_u32(p); p += 4;
311 l->dom.latent_reset = (int64_t)pd0_get_u64(p); p += 8;
312 l->confidence_ppm = pd0_get_u32(p); p += 4;
313 l->n_exceptions = pd0_get_u16(p); p += 2;
314 if (l->n_exceptions > PD0_MAX_EXCEPT) return -1;
315 for (int i = 0; i < l->n_exceptions; i++) {
316 NEED(8 + PD0_HASH + 24);
317 l->exc[i].seq = pd0_get_u64(p); p += 8;
318 memcpy(l->exc[i].hash, p, PD0_HASH); p += PD0_HASH;
319 l->exc[i].predicted = (int64_t)pd0_get_u64(p); p += 8;
320 l->exc[i].observed = (int64_t)pd0_get_u64(p); p += 8;
321 l->exc[i].error_micro = (int64_t)pd0_get_u64(p); p += 8;
322 }
323 NEED(2);
324 l->n_experiments = pd0_get_u16(p); p += 2;
325 if (l->n_experiments > PD0_MAX_EXPER) return -1;
326 for (int i = 0; i < l->n_experiments; i++) {
327 NEED(3 * PD0_HASH + 2 + 16);
328 memcpy(l->exp[i].id, p, PD0_HASH); p += PD0_HASH;
329 l->exp[i].kind = *p++;
330 memcpy(l->exp[i].prereg, p, PD0_HASH); p += PD0_HASH;
331 memcpy(l->exp[i].outcome, p, PD0_HASH); p += PD0_HASH;
332 l->exp[i].result = *p++;
333 l->exp[i].first_seq = pd0_get_u64(p); p += 8;
334 l->exp[i].last_seq = pd0_get_u64(p); p += 8;
335 }
336 NEED(PD0_HASH + 2);
337 memcpy(l->chain_root, p, PD0_HASH); p += PD0_HASH;
338 size_t cl = pd0_get_u16(p); p += 2;
339 NEED(cl);
340 p += cl;
341 if (p != end) return -1;
342#undef NEED
343 /* law_id must recompute with the field zeroed */
344 uint8_t copy[PD0_LAW_MAX], h[PD0_HASH];
345 if (len > sizeof copy) return -1;
346 memcpy(copy, in, len);
347 memset(copy + 11, 0, PD0_HASH);
348 sha256_hash(copy, len, h);
349 if (memcmp(h, l->law_id, PD0_HASH) != 0) return -1;
350 /* eps is not a canonical field; recover it from the claim text is not needed */
351 return 0;
352}
353
354void pd0_hex(const uint8_t *h, size_t n, char *out) {
355 static const char *d = "0123456789abcdef";
356 for (size_t i = 0; i < n; i++) { out[2 * i] = d[h[i] >> 4]; out[2 * i + 1] = d[h[i] & 15]; }
357 out[2 * n] = 0;
358}
359
360static const char *STATE_NAMES[4] = {"REJECTED", "REFUTED", "HYPOTHESIS", "PROVISIONAL_LAW"};
361
362size_t pd0_law_json(const pd0_law *l, char *buf, size_t cap) {
363 size_t n = 0;
364#define EMIT(...) do { int k = snprintf(buf + n, n < cap ? cap - n : 0, __VA_ARGS__); if (k > 0) n += (size_t)k; } while (0)
365 char hex[2 * PD0_HASH + 1];
366 pd0_hex(l->law_id, PD0_HASH, hex);
367 EMIT("{\"state\":\"%s\",\"law_id\":\"%s\",\"relationship\":[", STATE_NAMES[l->state & 3], hex);
368 const pd0_relation *r = &l->rel;
369 for (int i = 0; i < r->n_eq; i++) {
370 const pd0_eq *e = &r->eq[i];
371 EMIT("%s{\"target\":\"%s%d\",\"terms\":[", i ? "," : "", e->target < r->n_vars - r->n_latent ? "s" : "h",
372 e->target < r->n_vars - r->n_latent ? e->target : e->target - (r->n_vars - r->n_latent));
373 for (int t = 0; t < e->n_terms; t++) {
374 EMIT("%s{\"coef\":%lld,\"monomial\":\"", t ? "," : "", (long long)e->t[t].coef);
375 int any = 0;
376 for (int v = 0; v < r->n_vars; v++)
377 for (int p = 0; p < e->t[t].ex[v]; p++) { EMIT("%s%s%d", any ? "*" : "", v < r->n_vars - r->n_latent ? "s" : "h", v < r->n_vars - r->n_latent ? v : v - (r->n_vars - r->n_latent)); any = 1; }
378 for (int c = 0; c < r->n_channels; c++)
379 for (int p = 0; p < e->t[t].ex[PD0_MAX_VARS + c]; p++) { EMIT("%sc%d", any ? "*" : "", c); any = 1; }
380 if (!any) EMIT("1");
381 EMIT("\"}");
382 }
383 EMIT("]}");
384 }
385 EMIT("],\"size\":%d,\"description_bits\":%u,\"n_refutations\":%u,\"observed_domain\":{", pd0_relation_size(r),
386 pd0_relation_description_bits(r), l->n_refutations);
387 for (int v = 0; v < r->n_vars - r->n_latent; v++) EMIT("\"s%d\":[%lld,%lld],", v, (long long)l->dom.var_min[v], (long long)l->dom.var_max[v]);
388 for (int c = 0; c < r->n_channels; c++) EMIT("\"c%d\":[%lld,%lld],", c, (long long)l->dom.ch_min[c], (long long)l->dom.ch_max[c]);
389 EMIT("\"dt_micro\":%lld,\"n_observations\":%llu,\"n_episodes\":%u},", (long long)l->dom.dt_micro,
390 (unsigned long long)l->dom.n_observations, l->dom.n_episodes);
391 EMIT("\"confidence_ppm\":%u,\"n_exceptions\":%u,\"experiments\":%u,", l->confidence_ppm, l->n_exceptions, l->n_experiments);
392 char claim[512];
393 pd0_claim_text(l, claim, sizeof claim);
394 EMIT("\"claim_text\":\"%s\"}", claim);
395#undef EMIT
396 return n;
397}