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 / research / atemporal / at0 / evaluator / src / at0e_case.c
| 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 | /* Independent AT0_CASE_V1 codec: parse, validate (section 4 order), identities |
| 18 | * (section 5), canonical emission. Written from the contract text only. */ |
| 19 | #include "at0e.h" |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | static const char *const failure_codes_v1[] = { |
| 25 | "BOUND_KIND_INSUFFICIENT", "CONDITIONAL_UNDEFINED", "CONSTRAINT_RESIDUAL_EXCEEDED", |
| 26 | "NONFINITE_VALUE", "POVM_NORMALIZATION_EXCEEDED", "PRECISION_INSUFFICIENT", |
| 27 | "PROBABILITY_OUT_OF_RANGE", "PROBABILITY_SUM_EXCEEDED", "SCHRODINGER_DEVIATION_EXCEEDED", |
| 28 | "TRIVIAL_PHYSICAL_STATE", NULL }; |
| 29 | int failure_code_valid(const char *code) { for (int i = 0; failure_codes_v1[i]; i++) if (!strcmp(code, failure_codes_v1[i])) return 1; return 0; } |
| 30 | |
| 31 | const char *refusal_name(refusal r) { |
| 32 | switch (r) { |
| 33 | case REFUSE_NONE: return "NONE"; |
| 34 | case REFUSE_PARSE_ERROR: return "CASE_PARSE_ERROR"; |
| 35 | case REFUSE_NONCANONICAL: return "CASE_NONCANONICAL"; |
| 36 | case REFUSE_UNSUPPORTED_VERSION: return "CASE_UNSUPPORTED_VERSION"; |
| 37 | case REFUSE_INVALID_PARAMETER: return "CASE_INVALID_PARAMETER"; |
| 38 | case REFUSE_IRRATIONAL_SPECTRUM: return "CASE_IRRATIONAL_SPECTRUM"; |
| 39 | case REFUSE_ID_MISMATCH: return "CASE_ID_MISMATCH"; |
| 40 | } |
| 41 | return "?"; |
| 42 | } |
| 43 | |
| 44 | /* Parsing state. Shape problems are fatal at once (rule 1 comes first); value |
| 45 | * problems are recorded in order of the rules and reported after the shape pass. */ |
| 46 | typedef struct { |
| 47 | char **lines; size_t n, i; |
| 48 | const char *detail; |
| 49 | int noncanonical; const char *noncanon_detail; |
| 50 | int bad_version; const char *version_detail; |
| 51 | int bad_param; const char *param_detail; |
| 52 | } ps; |
| 53 | |
| 54 | static int expect_key(ps *p, const char *key, char **val) { |
| 55 | char *tok[8]; |
| 56 | if (p->i >= p->n) { p->detail = "unexpected end of file"; return 0; } |
| 57 | const char *line = p->lines[p->i]; |
| 58 | int n = split_tokens(line, tok, 8); |
| 59 | if (n != 2 || strcmp(tok[0], key) != 0) { p->detail = line; return 0; } |
| 60 | *val = tok[1]; |
| 61 | p->i++; |
| 62 | return 1; |
| 63 | } |
| 64 | static int expect_exact(ps *p, const char *line) { |
| 65 | if (p->i >= p->n) { p->detail = "unexpected end of file"; return 0; } |
| 66 | if (strcmp(p->lines[p->i], line) != 0) { p->detail = p->lines[p->i]; return 0; } |
| 67 | p->i++; |
| 68 | return 1; |
| 69 | } |
| 70 | static void mark_nc(ps *p, const char *d) { if (!p->noncanonical) { p->noncanonical = 1; p->noncanon_detail = d; } } |
| 71 | static void mark_param(ps *p, const char *d) { if (!p->bad_param) { p->bad_param = 1; p->param_detail = d; } } |
| 72 | |
| 73 | /* fixed-value key: a mismatch is INVALID_PARAMETER (rule 3), not a parse error */ |
| 74 | static int fixed_value(ps *p, const char *key, const char *want) { |
| 75 | char *v; |
| 76 | if (!expect_key(p, key, &v)) return 0; |
| 77 | if (strcmp(v, want) != 0) { p->detail = p->lines[p->i - 1]; return 0; } /* bare literal is line grammar: shape, CASE_PARSE_ERROR (Agent 0 ruling D11) */ |
| 78 | return 1; |
| 79 | } |
| 80 | static int parse_rat_list(ps *p, const char *val, rat *out, int max, int *count) { |
| 81 | char buf[AT0E_MAX_LINE]; snprintf(buf, sizeof buf, "%s", val); |
| 82 | size_t l = strlen(buf); |
| 83 | if (l == 0 || buf[0] == ',' || buf[l - 1] == ',' || strstr(buf, ",,")) return 0; |
| 84 | int n = 0; char *s = buf; |
| 85 | while (s) { |
| 86 | char *c = strchr(s, ','); if (c) *c = 0; |
| 87 | int canon = 1; rat tmp; |
| 88 | if (!rat_parse(s, &tmp, &canon)) return 0; |
| 89 | if (n >= 4096) return 0; |
| 90 | if (n < max) out[n] = tmp; else mark_param(p, p->lines[p->i - 1]); |
| 91 | if (canon < 0) mark_param(p, p->lines[p->i - 1]); else if (!canon) mark_nc(p, p->lines[p->i - 1]); |
| 92 | n++; |
| 93 | s = c ? c + 1 : NULL; |
| 94 | } |
| 95 | *count = n < max ? n : max; |
| 96 | return 1; |
| 97 | } |
| 98 | static int parse_code_list(const char *val, char codes[16][48], int *count) { |
| 99 | *count = 0; |
| 100 | if (!strcmp(val, "none")) return 1; |
| 101 | char buf[AT0E_MAX_LINE]; snprintf(buf, sizeof buf, "%s", val); |
| 102 | size_t l = strlen(buf); |
| 103 | if (l == 0 || buf[0] == ',' || buf[l - 1] == ',' || strstr(buf, ",,")) return 0; |
| 104 | char *s = buf; |
| 105 | while (s) { |
| 106 | char *c = strchr(s, ','); if (c) *c = 0; |
| 107 | if (*count >= 16 || strlen(s) >= 48 || strlen(s) == 0) return 0; |
| 108 | for (const char *q = s; *q; q++) if (!((*q >= 'A' && *q <= 'Z') || *q == '_')) return 0; |
| 109 | memcpy(codes[(*count)++], s, strlen(s) + 1); |
| 110 | s = c ? c + 1 : NULL; |
| 111 | } |
| 112 | return 1; |
| 113 | } |
| 114 | static int parse_scaled_field(ps *p, const char *key, scaled *out) { |
| 115 | char *v; int canon = 1; |
| 116 | if (!expect_key(p, key, &v)) return 0; |
| 117 | if (!scaled_parse(v, out, &canon)) { p->detail = p->lines[p->i - 1]; return 0; } |
| 118 | if (!canon) mark_nc(p, p->lines[p->i - 1]); |
| 119 | return 1; |
| 120 | } |
| 121 | static int parse_rat_field(ps *p, const char *key, rat *out) { |
| 122 | char *v; int canon = 1; |
| 123 | if (!expect_key(p, key, &v)) return 0; |
| 124 | if (!rat_parse(v, out, &canon)) { p->detail = p->lines[p->i - 1]; return 0; } |
| 125 | if (canon < 0) mark_param(p, p->lines[p->i - 1]); else if (!canon) mark_nc(p, p->lines[p->i - 1]); |
| 126 | return 1; |
| 127 | } |
| 128 | static int parse_int_field(ps *p, const char *key, long *out) { |
| 129 | char *v; |
| 130 | if (!expect_key(p, key, &v)) return 0; |
| 131 | /* canonical integer: shape-level parse, canonicity as NONCANONICAL */ |
| 132 | size_t l = strlen(v); int neg = v[0] == '-'; size_t s = neg ? 1 : 0; |
| 133 | if (l == s) { p->detail = p->lines[p->i - 1]; return 0; } |
| 134 | if (l - s > 12) { p->detail = p->lines[p->i - 1]; return 0; } |
| 135 | long val = 0; |
| 136 | for (size_t i = s; i < l; i++) { if (v[i] < '0' || v[i] > '9') { p->detail = p->lines[p->i - 1]; return 0; } val = val * 10 + (v[i] - '0'); } |
| 137 | if ((l - s > 1 && v[s] == '0') || (neg && val == 0)) mark_nc(p, p->lines[p->i - 1]); |
| 138 | *out = neg ? -val : val; |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | static char *join_lines(char **lines, size_t from, size_t to, size_t *len) { |
| 143 | size_t total = 0; |
| 144 | for (size_t i = from; i < to; i++) total += strlen(lines[i]) + 1; |
| 145 | char *b = malloc(total + 1); size_t o = 0; |
| 146 | for (size_t i = from; i < to; i++) { size_t l = strlen(lines[i]); memcpy(b + o, lines[i], l); o += l; b[o++] = '\n'; } |
| 147 | b[o] = 0; *len = o; |
| 148 | return b; |
| 149 | } |
| 150 | |
| 151 | refusal case_parse_blocks(char **lines, size_t n, size_t *consumed, at0_case *c, const char **detail, int in_result) { |
| 152 | ps p; memset(&p, 0, sizeof p); p.lines = lines; p.n = n; |
| 153 | memset(c, 0, sizeof *c); |
| 154 | char *v; long lv; |
| 155 | *detail = ""; |
| 156 | (void)in_result; |
| 157 | |
| 158 | if (!expect_key(&p, "case_name", &v)) goto parse_err; |
| 159 | if (!is_name(v)) goto parse_err_cur; |
| 160 | snprintf(c->case_name, sizeof c->case_name, "%s", v); |
| 161 | |
| 162 | size_t sem_start = p.i; |
| 163 | if (!expect_exact(&p, "begin semantic")) goto parse_err; |
| 164 | if (!fixed_value(&p, "model_family", "PAGE_WOOTTERS_FINITE_IDEAL")) goto parse_err; |
| 165 | if (!fixed_value(&p, "energy_unit", "DIMENSIONLESS_HBAR_1")) goto parse_err; |
| 166 | if (!parse_int_field(&p, "clock_dim", &lv)) goto parse_err; |
| 167 | if (lv < 2 || lv > AT0E_MAX_N) { mark_param(&p, p.lines[p.i - 1]); lv = lv < 2 ? 2 : AT0E_MAX_N; } |
| 168 | c->N = (int)lv; |
| 169 | { |
| 170 | int cnt = 0; |
| 171 | if (!expect_key(&p, "clock_energies", &v)) goto parse_err; |
| 172 | if (!parse_rat_list(&p, v, c->E, AT0E_MAX_N, &cnt)) goto parse_err_cur; |
| 173 | if (cnt != c->N) mark_param(&p, p.lines[p.i - 1]); |
| 174 | for (int i = 1; i < cnt; i++) if (rat_cmp(c->E[i - 1], c->E[i]) >= 0) { mark_param(&p, p.lines[p.i - 1]); break; } |
| 175 | if (cnt < c->N) c->N = cnt; |
| 176 | } |
| 177 | if (!fixed_value(&p, "system_dim", "2")) goto parse_err; |
| 178 | { |
| 179 | rat hs[4]; int cnt = 0; |
| 180 | if (!expect_key(&p, "system_hamiltonian_pauli", &v)) goto parse_err; |
| 181 | if (!parse_rat_list(&p, v, hs, 4, &cnt)) goto parse_err_cur; |
| 182 | if (cnt != 4) goto parse_err_cur; |
| 183 | c->h0 = hs[0]; c->hx = hs[1]; c->hy = hs[2]; c->hz = hs[3]; |
| 184 | } |
| 185 | if (!fixed_value(&p, "interaction", "NONE")) goto parse_err; |
| 186 | if (!fixed_value(&p, "constraint", "SUM_HC_HS")) goto parse_err; |
| 187 | if (!fixed_value(&p, "physical_state", "NULLSPACE_PROJECTION")) goto parse_err; |
| 188 | if (!expect_key(&p, "reference_clock_label", &v)) goto parse_err; |
| 189 | if (!is_label(v)) goto parse_err_cur; |
| 190 | snprintf(c->ref_label, sizeof c->ref_label, "%s", v); |
| 191 | { |
| 192 | if (!expect_key(&p, "reference_system_state", &v)) goto parse_err; |
| 193 | char buf[AT0E_MAX_LINE]; snprintf(buf, sizeof buf, "%s", v); |
| 194 | char *comma = strchr(buf, ','); |
| 195 | if (!comma || strchr(comma + 1, ',')) goto parse_err_cur; |
| 196 | *comma = 0; |
| 197 | int c1 = 1, c2 = 1; |
| 198 | if (!crat_parse(buf, &c->psi0[0], &c1) || !crat_parse(comma + 1, &c->psi0[1], &c2)) goto parse_err_cur; |
| 199 | if (c1 < 0 || c2 < 0) mark_param(&p, p.lines[p.i - 1]); else if (!c1 || !c2) mark_nc(&p, p.lines[p.i - 1]); |
| 200 | } |
| 201 | if (!fixed_value(&p, "clock_povm", "COVARIANT_DISCRETE")) goto parse_err; |
| 202 | if (!parse_rat_field(&p, "povm_tau_turns", &c->tau)) goto parse_err; |
| 203 | if (!parse_rat_field(&p, "povm_weight", &c->w)) goto parse_err; |
| 204 | if (!parse_int_field(&p, "clock_label_count", &lv)) goto parse_err; |
| 205 | if (lv < 1 || lv > AT0E_MAX_M) { mark_param(&p, p.lines[p.i - 1]); lv = lv < 1 ? 1 : AT0E_MAX_M; } |
| 206 | c->M = (int)lv; |
| 207 | for (int k = 0; k < c->M; k++) { |
| 208 | char *tok[8]; |
| 209 | if (p.i >= p.n) goto parse_err_eof; |
| 210 | int nt = split_tokens(p.lines[p.i], tok, 8); |
| 211 | if (nt != 3 || strcmp(tok[0], "clock_label") != 0) { |
| 212 | p.detail = p.lines[p.i]; goto parse_err; |
| 213 | } |
| 214 | long kk; |
| 215 | if (!parse_int_canonical(tok[1], &kk)) { p.detail = p.lines[p.i]; goto parse_err; } |
| 216 | if (kk != k) { p.detail = p.lines[p.i]; goto parse_err; } |
| 217 | if (!is_label(tok[2])) { p.detail = p.lines[p.i]; goto parse_err; } |
| 218 | snprintf(c->label[k], sizeof c->label[k], "%s", tok[2]); |
| 219 | p.i++; |
| 220 | } |
| 221 | if (!fixed_value(&p, "observables", "PAULI_X,PAULI_Y,PAULI_Z")) goto parse_err; |
| 222 | if (!expect_exact(&p, "end semantic")) goto parse_err; |
| 223 | size_t sem_end = p.i; |
| 224 | |
| 225 | size_t acc_start = p.i; |
| 226 | if (!expect_exact(&p, "begin acceptance")) goto parse_err; |
| 227 | if (!expect_key(&p, "control_kind", &v)) goto parse_err; |
| 228 | if (!strcmp(v, "POSITIVE")) c->control_positive = 1; else if (!strcmp(v, "NEGATIVE")) c->control_positive = 0; else { p.detail = p.lines[p.i - 1]; goto parse_err; } /* token set is line grammar: shape (Agent 0 ruling D3) */ |
| 229 | if (!expect_key(&p, "expected_outcome", &v)) goto parse_err; |
| 230 | if (!strcmp(v, "PASS")) c->expected_pass = 1; else if (!strcmp(v, "FAIL")) c->expected_pass = 0; else mark_param(&p, p.lines[p.i - 1]); |
| 231 | if (!expect_key(&p, "expected_failure_codes", &v)) goto parse_err; |
| 232 | if (!parse_code_list(v, c->expected_codes, &c->n_expected_codes)) goto parse_err_cur; |
| 233 | const char *codes_line = p.lines[p.i - 1]; |
| 234 | if (!expect_key(&p, "min_bound_kind", &v)) goto parse_err; |
| 235 | if (!strcmp(v, "RIGOROUS")) c->min_bound_kind = BK_RIGOROUS; else if (!strcmp(v, "ESTIMATED")) c->min_bound_kind = BK_ESTIMATED; else if (!strcmp(v, "NONE")) c->min_bound_kind = BK_NONE; else mark_param(&p, p.lines[p.i - 1]); |
| 236 | if (!parse_scaled_field(&p, "tol_constraint_residual", &c->tol_constraint)) goto parse_err; |
| 237 | if (!parse_scaled_field(&p, "tol_povm_residual", &c->tol_povm)) goto parse_err; |
| 238 | if (!parse_scaled_field(&p, "tol_probability", &c->tol_prob)) goto parse_err; |
| 239 | if (!parse_scaled_field(&p, "tol_zero_probability", &c->tol_zero)) goto parse_err; |
| 240 | if (!parse_scaled_field(&p, "tol_schrodinger", &c->tol_schro)) goto parse_err; |
| 241 | if (!expect_exact(&p, "end acceptance")) goto parse_err; |
| 242 | size_t acc_end = p.i; |
| 243 | |
| 244 | if (!expect_key(&p, "case_id", &v)) goto parse_err; |
| 245 | if (!is_digest(v)) goto parse_err_cur; |
| 246 | snprintf(c->case_id, sizeof c->case_id, "%s", v); |
| 247 | if (!expect_key(&p, "acceptance_id", &v)) goto parse_err; |
| 248 | if (!is_digest(v)) goto parse_err_cur; |
| 249 | snprintf(c->acceptance_id, sizeof c->acceptance_id, "%s", v); |
| 250 | *consumed = p.i; |
| 251 | |
| 252 | c->sem_block = join_lines(lines, sem_start, sem_end, &c->sem_len); |
| 253 | c->acc_block = join_lines(lines, acc_start, acc_end, &c->acc_len); |
| 254 | |
| 255 | /* rule 1 (value-level canonicity) */ |
| 256 | if (p.noncanonical) { *detail = p.noncanon_detail; return REFUSE_NONCANONICAL; } |
| 257 | /* rule 2 is checked by the caller for standalone files (header lines) */ |
| 258 | /* rule 3 */ |
| 259 | if (p.bad_param) { *detail = p.param_detail; return REFUSE_INVALID_PARAMETER; } |
| 260 | if (crat_is_zero(c->psi0[0]) && crat_is_zero(c->psi0[1])) { *detail = "reference_system_state is zero"; return REFUSE_INVALID_PARAMETER; } |
| 261 | if (rat_cmp(c->w, rat_make(0, 1)) <= 0) { *detail = "povm_weight <= 0"; return REFUSE_INVALID_PARAMETER; } |
| 262 | if (rat_cmp(c->tau, rat_make(0, 1)) <= 0) { *detail = "povm_tau_turns <= 0"; return REFUSE_INVALID_PARAMETER; } |
| 263 | c->ref_index = -1; |
| 264 | for (int k = 0; k < c->M; k++) { |
| 265 | for (int j = 0; j < k; j++) if (!strcmp(c->label[j], c->label[k])) { *detail = "duplicate clock label"; return REFUSE_INVALID_PARAMETER; } |
| 266 | if (!strcmp(c->label[k], c->ref_label)) c->ref_index = k; |
| 267 | } |
| 268 | if (c->ref_index < 0) { *detail = "reference_clock_label not among clock labels"; return REFUSE_INVALID_PARAMETER; } |
| 269 | /* rule 4 */ |
| 270 | { |
| 271 | rat sq = rat_add(rat_add(rat_mul(c->hx, c->hx), rat_mul(c->hy, c->hy)), rat_mul(c->hz, c->hz)); |
| 272 | if (!rat_sqrt(sq, &c->radius)) { *detail = "hx^2+hy^2+hz^2 is not a rational square"; return REFUSE_IRRATIONAL_SPECTRUM; } |
| 273 | } |
| 274 | /* rule 5 */ |
| 275 | { |
| 276 | char id[65]; |
| 277 | sha256_tagged_hex(AT0E_CASE_DOMAIN, (const uint8_t *)c->sem_block, c->sem_len, id); |
| 278 | if (strcmp(id, c->case_id) != 0) { *detail = "case_id does not match the semantic block"; return REFUSE_ID_MISMATCH; } |
| 279 | sha256_tagged_hex(AT0E_ACC_DOMAIN, (const uint8_t *)c->acc_block, c->acc_len, id); |
| 280 | if (strcmp(id, c->acceptance_id) != 0) { *detail = "acceptance_id does not match the acceptance block"; return REFUSE_ID_MISMATCH; } |
| 281 | } |
| 282 | /* rule 6 */ |
| 283 | if (c->expected_pass) { |
| 284 | if (c->n_expected_codes != 0) { *detail = codes_line; return REFUSE_INVALID_PARAMETER; } |
| 285 | } else { |
| 286 | if (c->n_expected_codes == 0) { *detail = codes_line; return REFUSE_INVALID_PARAMETER; } |
| 287 | for (int i = 0; i < c->n_expected_codes; i++) { |
| 288 | if (!failure_code_valid(c->expected_codes[i])) { *detail = codes_line; return REFUSE_INVALID_PARAMETER; } |
| 289 | if (i > 0 && strcmp(c->expected_codes[i - 1], c->expected_codes[i]) >= 0) { *detail = codes_line; return REFUSE_INVALID_PARAMETER; } |
| 290 | } |
| 291 | } |
| 292 | return REFUSE_NONE; |
| 293 | |
| 294 | parse_err_cur: |
| 295 | p.detail = p.lines[p.i - 1]; |
| 296 | goto parse_err; |
| 297 | parse_err_eof: |
| 298 | p.detail = "unexpected end of file"; |
| 299 | parse_err: |
| 300 | *detail = p.detail ? p.detail : "shape"; |
| 301 | *consumed = p.i; |
| 302 | return REFUSE_PARSE_ERROR; |
| 303 | } |
| 304 | |
| 305 | refusal case_parse_validate(const textfile *tf, at0_case *c, const char **detail) { |
| 306 | memset(c, 0, sizeof *c); |
| 307 | *detail = ""; |
| 308 | if (tf->n < 4) { *detail = "too few lines"; return REFUSE_PARSE_ERROR; } |
| 309 | char *tok[8]; |
| 310 | /* lines 0..2: shape now, content after the shape pass (rule 2 after rule 1) */ |
| 311 | int bad_version = 0; |
| 312 | if (split_tokens(tf->lines[0], tok, 8) != 2) { *detail = tf->lines[0]; return REFUSE_PARSE_ERROR; } |
| 313 | if (strcmp(tf->lines[0], "OMEGA-AT0-CASE v1") != 0) bad_version = 1; |
| 314 | if (split_tokens(tf->lines[1], tok, 8) != 2 || strcmp(tok[0], "domain") != 0) { *detail = tf->lines[1]; return REFUSE_PARSE_ERROR; } |
| 315 | if (strcmp(tok[1], AT0E_CASE_DOMAIN) != 0) bad_version = 1; |
| 316 | if (split_tokens(tf->lines[2], tok, 8) != 2 || strcmp(tok[0], "contract") != 0) { *detail = tf->lines[2]; return REFUSE_PARSE_ERROR; } |
| 317 | if (strcmp(tok[1], "AT0_CASE_V1") != 0) bad_version = 1; |
| 318 | |
| 319 | size_t consumed = 0; |
| 320 | refusal r = case_parse_blocks(tf->lines + 3, tf->n - 3, &consumed, c, detail, 0); |
| 321 | if (r == REFUSE_PARSE_ERROR) return r; |
| 322 | /* the trailing `end` line and nothing after it are shape */ |
| 323 | size_t idx = 3 + consumed; |
| 324 | if (idx >= tf->n || strcmp(tf->lines[idx], "end") != 0) { *detail = idx < tf->n ? tf->lines[idx] : "missing end"; return REFUSE_PARSE_ERROR; } |
| 325 | if (idx + 1 != tf->n) { *detail = tf->lines[idx + 1]; return REFUSE_PARSE_ERROR; } |
| 326 | if (r == REFUSE_NONCANONICAL) return r; |
| 327 | if (bad_version) { *detail = "header, domain or contract line"; return REFUSE_UNSUPPORTED_VERSION; } |
| 328 | if (r == REFUSE_NONE) sha256_hex(tf->bytes, tf->len, c->case_file_sha256); |
| 329 | return r; |
| 330 | } |
| 331 | |
| 332 | void case_free(at0_case *c) { free(c->sem_block); free(c->acc_block); c->sem_block = c->acc_block = NULL; } |
| 333 | |
| 334 | /* ---------------- emission ---------------- */ |
| 335 | static void scaled_format(scaled s, char *buf, size_t cap) { |
| 336 | char digits[64]; int n = 0; u128 v = s.n; |
| 337 | if (v == 0) { snprintf(buf, cap, "0@0"); return; } |
| 338 | while (v) { digits[n++] = (char)('0' + (int)(v % 10)); v /= 10; } |
| 339 | char num[64]; for (int i = 0; i < n; i++) num[i] = digits[n - 1 - i]; num[n] = 0; |
| 340 | snprintf(buf, cap, "%s@%d", num, s.k); |
| 341 | } |
| 342 | static void crat_format(crat z, char *buf, size_t cap) { |
| 343 | char a[64], b[64]; rat_format(z.re, a, sizeof a); rat_format(z.im, b, sizeof b); |
| 344 | size_t o = 0; |
| 345 | if (o + 1 < cap) buf[o++] = (char)40; |
| 346 | for (char *p = a; *p && o + 1 < cap; p++) buf[o++] = *p; |
| 347 | if (o + 1 < cap) buf[o++] = (char)59; |
| 348 | for (char *p = b; *p && o + 1 < cap; p++) buf[o++] = *p; |
| 349 | if (o + 1 < cap) buf[o++] = (char)41; |
| 350 | buf[o] = 0; |
| 351 | } |
| 352 | #define EMIT(...) do { int w_ = snprintf(buf + o, cap > o ? cap - o : 0, __VA_ARGS__); if (w_ < 0) return -1; o += (size_t)w_; } while (0) |
| 353 | long case_emit(const at0_case *c, char *buf, size_t cap) { |
| 354 | size_t o = 0; char t[8][80]; |
| 355 | EMIT("OMEGA-AT0-CASE v1\ndomain %s\ncontract AT0_CASE_V1\ncase_name %s\n", AT0E_CASE_DOMAIN, c->case_name); |
| 356 | size_t sem_start = o; |
| 357 | EMIT("begin semantic\nmodel_family PAGE_WOOTTERS_FINITE_IDEAL\nenergy_unit DIMENSIONLESS_HBAR_1\nclock_dim %d\nclock_energies ", c->N); |
| 358 | for (int i = 0; i < c->N; i++) { rat_format(c->E[i], t[0], sizeof t[0]); EMIT("%s%s", i ? "," : "", t[0]); } |
| 359 | rat_format(c->h0, t[0], 80); rat_format(c->hx, t[1], 80); rat_format(c->hy, t[2], 80); rat_format(c->hz, t[3], 80); |
| 360 | EMIT("\nsystem_dim 2\nsystem_hamiltonian_pauli %s,%s,%s,%s\ninteraction NONE\nconstraint SUM_HC_HS\nphysical_state NULLSPACE_PROJECTION\n", t[0], t[1], t[2], t[3]); |
| 361 | crat_format(c->psi0[0], t[0], 80); crat_format(c->psi0[1], t[1], 80); |
| 362 | EMIT("reference_clock_label %s\nreference_system_state %s,%s\nclock_povm COVARIANT_DISCRETE\n", c->ref_label, t[0], t[1]); |
| 363 | rat_format(c->tau, t[0], 80); rat_format(c->w, t[1], 80); |
| 364 | EMIT("povm_tau_turns %s\npovm_weight %s\nclock_label_count %d\n", t[0], t[1], c->M); |
| 365 | for (int k = 0; k < c->M; k++) EMIT("clock_label %d %s\n", k, c->label[k]); |
| 366 | EMIT("observables PAULI_X,PAULI_Y,PAULI_Z\nend semantic\n"); |
| 367 | size_t sem_end = o; |
| 368 | size_t acc_start = o; |
| 369 | EMIT("begin acceptance\ncontrol_kind %s\nexpected_outcome %s\nexpected_failure_codes ", c->control_positive ? "POSITIVE" : "NEGATIVE", c->expected_pass ? "PASS" : "FAIL"); |
| 370 | if (c->n_expected_codes == 0) EMIT("none"); |
| 371 | for (int i = 0; i < c->n_expected_codes; i++) EMIT("%s%s", i ? "," : "", c->expected_codes[i]); |
| 372 | EMIT("\nmin_bound_kind %s\n", c->min_bound_kind == BK_RIGOROUS ? "RIGOROUS" : c->min_bound_kind == BK_ESTIMATED ? "ESTIMATED" : "NONE"); |
| 373 | scaled_format(c->tol_constraint, t[0], 80); EMIT("tol_constraint_residual %s\n", t[0]); |
| 374 | scaled_format(c->tol_povm, t[0], 80); EMIT("tol_povm_residual %s\n", t[0]); |
| 375 | scaled_format(c->tol_prob, t[0], 80); EMIT("tol_probability %s\n", t[0]); |
| 376 | scaled_format(c->tol_zero, t[0], 80); EMIT("tol_zero_probability %s\n", t[0]); |
| 377 | scaled_format(c->tol_schro, t[0], 80); EMIT("tol_schrodinger %s\n", t[0]); |
| 378 | EMIT("end acceptance\n"); |
| 379 | size_t acc_end = o; |
| 380 | if (o >= cap) return -1; |
| 381 | char id[65]; |
| 382 | sha256_tagged_hex(AT0E_CASE_DOMAIN, (const uint8_t *)buf + sem_start, sem_end - sem_start, id); |
| 383 | EMIT("case_id %s\n", id); |
| 384 | sha256_tagged_hex(AT0E_ACC_DOMAIN, (const uint8_t *)buf + acc_start, acc_end - acc_start, id); |
| 385 | EMIT("acceptance_id %s\nend\n", id); |
| 386 | if (o >= cap) return -1; |
| 387 | return (long)o; |
| 388 | } |