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
omega-mv / src / physics0 / ladder / pd0_ladder.h
| 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 | /* PD-0 independent ladder checker (spec section 5, 7). The learner proposes; |
| 18 | * this replays the evidence ledger and derives the state itself. */ |
| 19 | #ifndef PD0_LADDER_H |
| 20 | #define PD0_LADDER_H |
| 21 | #include "pd0_fmt.h" |
| 22 | #include "pd0_codes.h" |
| 23 | |
| 24 | enum { LS_START = 0, LS_OBSERVATION, LS_CORRELATION, LS_CANDIDATE, LS_HYPOTHESIS, LS_PREDICTED, LS_INTERVENED, LS_REPLICATED }; |
| 25 | const char *pd0_ladder_state_name(int s); |
| 26 | |
| 27 | typedef struct { |
| 28 | uint8_t n_obs, n_channels, noisy; |
| 29 | int64_t chan_min[PD0_MAX_CHAN], chan_max[PD0_MAX_CHAN]; |
| 30 | int64_t reset_min, reset_max; uint32_t episode_len; int64_t dt_micro; |
| 31 | uint32_t size_bound; /* spec 6.2 S*+2 for the level */ |
| 32 | int64_t eps_bound_micro; /* spec 6.1 in-box bound (noisy levels: 0.05 cap) */ |
| 33 | uint32_t min_t1_records, min_t1_episodes, min_prereg, min_batches, min_batch_episodes, rollout_steps; |
| 34 | } pd0_ladder_params; |
| 35 | void pd0_ladder_params_default(pd0_ladder_params *P, uint8_t n_obs, uint8_t n_channels); |
| 36 | |
| 37 | typedef struct { |
| 38 | int state, code, transition, demoted; |
| 39 | int stall_code; /* first non-OK reason since the last successful transition */ |
| 40 | uint32_t n_refutations, p, f, confidence_ppm; |
| 41 | int have_candidate; pd0_rel candidate; uint8_t candidate_hash[PD0_HASH]; |
| 42 | int64_t eps_micro; |
| 43 | pd0_domain dom; |
| 44 | uint32_t n_exceptions; pd0_exception exc[PD0_MAX_EXC]; |
| 45 | uint32_t n_experiments; pd0_experiment exp[PD0_MAX_EXP]; |
| 46 | uint8_t chain_root[PD0_HASH]; |
| 47 | uint64_t n_entries, n_records; |
| 48 | int64_t last_nrmse_micro; /* diagnostics */ |
| 49 | uint32_t n_void_episodes, n_void_batches; /* spec rev 7: void TRIAL/REP episodes and batches (never in p or f) */ |
| 50 | double last_r, last_p; |
| 51 | } pd0_ladder_report; |
| 52 | |
| 53 | /* Replays the ledger. Returns the first fatal code (format/chain/dishonesty) or |
| 54 | * PD0V_OK; non-fatal "stay" conditions leave the state where it is and the |
| 55 | * reason in R->code. */ |
| 56 | int pd0_ladder_check(const uint8_t *ledger, size_t len, const pd0_ladder_params *P, pd0_ladder_report *R); |
| 57 | /* T8: verify a submitted PDLAW1 against the replayed state. */ |
| 58 | int pd0_ladder_verify_law(const pd0_ladder_report *R, const pd0_ladder_params *P, const uint8_t *law, size_t law_len); |
| 59 | /* Build the law record the evidence supports (used by tests and the harness, |
| 60 | * never by the learner). state is PROVISIONAL_LAW only when R->state == REPLICATED. */ |
| 61 | size_t pd0_ladder_emit_law(const pd0_ladder_report *R, const pd0_ladder_params *P, uint8_t *out, size_t cap); |
| 62 | /* generated claim text; returns length */ |
| 63 | uint32_t pd0_claim_text(const pd0_ladder_report *R, const pd0_ladder_params *P, char *out, size_t cap); |
| 64 | uint32_t pd0_confidence_ppm(uint32_t p, uint32_t f); |
| 65 | /* statistics exported for tests (the same code the checker uses) */ |
| 66 | double pd0_pearson(const double *x, const double *y, uint32_t n); |
| 67 | double pd0_perm_p(const double *x, const double *y, uint32_t n, uint32_t n_shuffles, uint64_t seed); |
| 68 | /* NRMSE over pooled steps: pred/obs are [n][n_obs] in micro; returns micro (1e6 = 1.0) */ |
| 69 | int64_t pd0_nrmse_micro(const int64_t *pred, const int64_t *obs, uint32_t n, uint8_t n_obs); |
| 70 | /* spec rev 6 (a): per-variable RMSE over the pooled FIT sd (micro); sd 0 falls back to the RMSE */ |
| 71 | int64_t pd0_nrmse_pooled_micro(const int64_t *pred, const int64_t *obs, uint32_t n, uint8_t n_obs, const int64_t *sd_micro); |
| 72 | #endif |