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 / tools / exp445 / x445.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 | /* |
| 18 | * x445.c -- C helpers of the exp445 generation driver (omega#445, docs/one_layer/EXP445_PROTOCOL.md). |
| 19 | * No hidden data is read here: only the public requirement, the model's answer and the repo's own |
| 20 | * compiler / decoder. The same checks run in every arm; only the representation's own reader differs. |
| 21 | * |
| 22 | * x445 extract <content> <out> payload of a model answer: the first fenced block (``` lines), |
| 23 | * or the whole text if there is none. Prints fenced|unfenced|open. |
| 24 | * x445 hex <in.hex> <out.oscir> strict hex: pairs of [0-9a-fA-F], whitespace only between bytes. |
| 25 | * exit 0 "OK bytes=N"; exit 1 "REFUSED hex: ..." (nothing written). |
| 26 | * x445 tohex <in> lowercase hex, 32 bytes per line (the bytes-arm spelling) |
| 27 | * x445 check osc|oscir <file> <requirement> |
| 28 | * exit 0 "OK"; exit 1 one line "REFUSED <stage>: <reason>", stage compiler | decoder | |
| 29 | * signature | backend. Signature = the requirement's entry, params, ret. |
| 30 | * x445 wilson <k> <n> "p lo hi": Wilson score 95% interval (z = 1.959964) |
| 31 | * x445 mcnemar <b> <c> exact two-sided McNemar p on the discordant pairs (b, c) |
| 32 | */ |
| 33 | #include <math.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "osc_cg.h" |
| 39 | #include "osc_front.h" |
| 40 | #include "osc_ir.h" |
| 41 | |
| 42 | static unsigned char *slurp(const char *p, size_t *n) |
| 43 | { |
| 44 | FILE *f = fopen(p, "rb"); |
| 45 | if (!f) return NULL; |
| 46 | size_t cap = 1 << 16, len = 0; |
| 47 | unsigned char *b = malloc(cap + 1); |
| 48 | size_t r; |
| 49 | while (b && (r = fread(b + len, 1, cap - len, f)) > 0) { |
| 50 | len += r; |
| 51 | if (len == cap) { |
| 52 | if (cap >= (64u << 20)) { free(b); b = NULL; break; } |
| 53 | unsigned char *nb = realloc(b, (cap *= 2) + 1); |
| 54 | if (!nb) { free(b); b = NULL; break; } |
| 55 | b = nb; |
| 56 | } |
| 57 | } |
| 58 | fclose(f); |
| 59 | if (b) { b[len] = 0; *n = len; } |
| 60 | return b; |
| 61 | } |
| 62 | |
| 63 | static int put(const char *p, const void *b, size_t n) |
| 64 | { |
| 65 | FILE *f = fopen(p, "wb"); |
| 66 | if (!f) return -1; |
| 67 | int bad = fwrite(b, 1, n, f) != n; |
| 68 | return fclose(f) || bad ? -1 : 0; |
| 69 | } |
| 70 | |
| 71 | static int cmd_extract(const char *in, const char *out) |
| 72 | { |
| 73 | size_t n; |
| 74 | unsigned char *t = slurp(in, &n); |
| 75 | if (!t) { fprintf(stderr, "x445: cannot read %s\n", in); return 2; } |
| 76 | const char *s = (const char *)t, *open = NULL; |
| 77 | /* a fence is a line that starts with ``` (after optional spaces); its opening line may carry a tag */ |
| 78 | for (const char *l = s; *l; ) { |
| 79 | const char *e = strchr(l, '\n'), *q = l; |
| 80 | while (*q == ' ' || *q == '\t') q++; |
| 81 | if (!strncmp(q, "```", 3)) { open = e ? e + 1 : l + strlen(l); break; } |
| 82 | if (!e) break; |
| 83 | l = e + 1; |
| 84 | } |
| 85 | if (!open) { if (put(out, t, n)) return 2; printf("unfenced\n"); free(t); return 0; } |
| 86 | const char *end = NULL; |
| 87 | for (const char *l = open; *l; ) { |
| 88 | const char *e = strchr(l, '\n'), *q = l; |
| 89 | while (*q == ' ' || *q == '\t') q++; |
| 90 | if (!strncmp(q, "```", 3)) { end = l; break; } |
| 91 | if (!e) break; |
| 92 | l = e + 1; |
| 93 | } |
| 94 | size_t len = end ? (size_t)(end - open) : strlen(open); |
| 95 | if (put(out, open, len)) return 2; |
| 96 | printf("%s\n", end ? "fenced" : "open"); |
| 97 | free(t); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int hexv(int c) { return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1; } |
| 102 | static int ws(int c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } |
| 103 | |
| 104 | static int cmd_hex(const char *in, const char *out) |
| 105 | { |
| 106 | size_t n; |
| 107 | unsigned char *t = slurp(in, &n); |
| 108 | if (!t) { fprintf(stderr, "x445: cannot read %s\n", in); return 2; } |
| 109 | unsigned char *b = malloc(n / 2 + 1); |
| 110 | size_t k = 0; |
| 111 | for (size_t i = 0; i < n; ) { |
| 112 | if (ws(t[i])) { i++; continue; } |
| 113 | int hi = hexv(t[i]); |
| 114 | if (hi < 0) { printf("REFUSED hex: character 0x%02x at offset %zu is not a hex digit\n", t[i], i); return 1; } |
| 115 | if (i + 1 >= n) { printf("REFUSED hex: odd number of hex digits (offset %zu)\n", i); return 1; } |
| 116 | int lo = hexv(t[i + 1]); |
| 117 | if (lo < 0) { printf("REFUSED hex: byte at offset %zu is not two hex digits\n", i); return 1; } |
| 118 | b[k++] = (unsigned char)(hi << 4 | lo); |
| 119 | i += 2; |
| 120 | } |
| 121 | if (!k) { printf("REFUSED hex: no bytes\n"); return 1; } |
| 122 | if (put(out, b, k)) return 2; |
| 123 | printf("OK bytes=%zu\n", k); |
| 124 | free(t); free(b); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int cmd_tohex(const char *in) |
| 129 | { |
| 130 | size_t n; |
| 131 | unsigned char *t = slurp(in, &n); |
| 132 | if (!t) return 2; |
| 133 | for (size_t i = 0; i < n; i++) printf("%02x%s", t[i], (i % 32 == 31 || i + 1 == n) ? "\n" : ""); |
| 134 | free(t); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /* section 7 of spec/one-layer-format.md, in words */ |
| 139 | static const char *meaning(int rc) |
| 140 | { |
| 141 | switch (rc) { |
| 142 | case OSC_IR_REFUSE_MAGIC: return "the first 7 bytes are not the magic 4f 53 43 31 49 52 00"; |
| 143 | case OSC_IR_REFUSE_UNKNOWN_VERSION: return "the version byte is 0 or above 5"; |
| 144 | case OSC_IR_REFUSE_TRUNCATED: return "the input ends inside a field"; |
| 145 | case OSC_IR_REFUSE_OVERSIZED: return "a count or length is over the format's limits"; |
| 146 | case OSC_IR_REFUSE_TRAILING: return "bytes after the last function"; |
| 147 | case OSC_IR_REFUSE_RANGE: return "unknown opcode, type tag or ref kind, or a register/block/function field 0x8000..0xfffe"; |
| 148 | case OSC_IR_REFUSE_TEXT: return "a NUL byte inside a name or contract text"; |
| 149 | case OSC_IR_REFUSE_INVALID: return "well-formed, but the unit fails validation (types, registers, blocks, calls, definite assignment)"; |
| 150 | case OSC_IR_REFUSE_VERSION_MISMATCH: return "the version byte is not the one the content requires (1 for these programs)"; |
| 151 | case OSC_IR_REFUSE_NONCANONICAL: return "the unit re-encodes to different bytes"; |
| 152 | default: return "refused"; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static int kind_of(const char *s) |
| 157 | { |
| 158 | static const char *const nm[] = {"bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"}; |
| 159 | static const int k[] = {OSC_T_BOOL, OSC_T_U8, OSC_T_U16, OSC_T_U32, OSC_T_U64, OSC_T_I8, OSC_T_I16, OSC_T_I32, OSC_T_I64}; |
| 160 | for (int i = 0; i < 9; i++) if (!strcmp(s, nm[i])) return k[i]; |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | static const char *kname(int s) |
| 165 | { |
| 166 | static const char *const nm[] = {"bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"}; |
| 167 | static const int k[] = {OSC_T_BOOL, OSC_T_U8, OSC_T_U16, OSC_T_U32, OSC_T_U64, OSC_T_I8, OSC_T_I16, OSC_T_I32, OSC_T_I64}; |
| 168 | for (int i = 0; i < 9; i++) if (k[i] == s) return nm[i]; |
| 169 | return s == OSC_T_VOID ? "void" : "array/other"; |
| 170 | } |
| 171 | |
| 172 | static int cmd_check(const char *mode, const char *file, const char *reqf) |
| 173 | { |
| 174 | size_t n, rn; |
| 175 | unsigned char *src = slurp(file, &n), *req = slurp(reqf, &rn); |
| 176 | if (!src || !req) { fprintf(stderr, "x445: cannot read input\n"); return 2; } |
| 177 | char entry[64] = "", pk[6][8]; |
| 178 | int np = -1, ret = -1; |
| 179 | for (char *l = strtok((char *)req, "\n"); l; l = strtok(NULL, "\n")) { |
| 180 | char *h = strchr(l, '#'); |
| 181 | if (h) *h = 0; |
| 182 | char a[16], b[64]; |
| 183 | if (sscanf(l, "%15s %63s", a, b) < 1) continue; |
| 184 | if (!strcmp(a, "entry")) snprintf(entry, sizeof entry, "%s", b); |
| 185 | else if (!strcmp(a, "ret")) ret = kind_of(b); |
| 186 | else if (!strcmp(a, "params")) { |
| 187 | np = 0; |
| 188 | char *save = NULL; |
| 189 | strtok_r(l, " \t", &save); |
| 190 | for (char *t = strtok_r(NULL, " \t", &save); t && np < 6; t = strtok_r(NULL, " \t", &save)) snprintf(pk[np++], 8, "%s", t); |
| 191 | } |
| 192 | } |
| 193 | if (!entry[0] || np < 0 || ret < 0) { fprintf(stderr, "x445: requirement lacks entry/params/ret\n"); return 2; } |
| 194 | OscUnit *u = calloc(1, sizeof *u); |
| 195 | if (!u) return 2; |
| 196 | if (!strcmp(mode, "osc")) { |
| 197 | OscDiag d; |
| 198 | memset(&d, 0, sizeof d); |
| 199 | if (osc_compile((const char *)src, n, u, &d, NULL)) { |
| 200 | printf("REFUSED compiler: %s at line %u: %s\n", osc_diag_kind_name(d.kind), d.line, d.message); |
| 201 | return 1; |
| 202 | } |
| 203 | } else if (!strcmp(mode, "oscir")) { |
| 204 | int rc = osc_ir_decode(src, n, u); |
| 205 | if (rc) { printf("REFUSED decoder: %s (%s)\n", osc_ir_refusal_name(rc), meaning(rc)); return 1; } |
| 206 | } else { fprintf(stderr, "x445: mode osc|oscir\n"); return 2; } |
| 207 | int fi = -1; |
| 208 | for (unsigned i = 0; i < u->nfuncs; i++) if (!strcmp(u->funcs[i].name, entry)) { fi = (int)i; break; } |
| 209 | if (fi < 0) { printf("REFUSED signature: no function named %s\n", entry); return 1; } |
| 210 | const OscFunc *f = &u->funcs[fi]; |
| 211 | int ok = (int)f->nparams == np && (int)f->ret.s == ret && f->ret.ref == OSC_REF_NONE; |
| 212 | for (int i = 0; ok && i < np; i++) ok = (int)f->vtype[i].s == kind_of(pk[i]) && f->vtype[i].ref == OSC_REF_NONE; |
| 213 | if (!ok) { |
| 214 | char have[128] = "", want[128] = ""; |
| 215 | for (unsigned i = 0; i < f->nparams; i++) { strcat(have, i ? ", " : ""); strcat(have, f->vtype[i].ref ? "array" : kname(f->vtype[i].s)); } |
| 216 | for (int i = 0; i < np; i++) { strcat(want, i ? ", " : ""); strcat(want, pk[i]); } |
| 217 | printf("REFUSED signature: %s takes (%s) -> %s; the task needs (%s) -> %s\n", entry, have, |
| 218 | f->ret.ref ? "array" : kname(f->ret.s), want, kname(ret)); |
| 219 | return 1; |
| 220 | } |
| 221 | OscCode code; |
| 222 | memset(&code, 0, sizeof code); |
| 223 | char err[160] = ""; |
| 224 | if (osc_cg_compile(u, &code, err, sizeof err)) { printf("REFUSED backend: %s\n", err); return 1; } |
| 225 | osc_cg_free(&code); |
| 226 | printf("OK\n"); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int cmd_wilson(const char *ks, const char *ns) |
| 231 | { |
| 232 | char *e1, *e2; |
| 233 | long k = strtol(ks, &e1, 10), n = strtol(ns, &e2, 10); |
| 234 | if (*e1 || *e2 || n <= 0 || k < 0 || k > n) { fprintf(stderr, "x445: wilson k n with 0 <= k <= n, n > 0\n"); return 2; } |
| 235 | const double z = 1.959964, p = (double)k / (double)n, z2 = z * z, d = 1 + z2 / (double)n; |
| 236 | const double c = (p + z2 / (2.0 * (double)n)) / d, h = z * sqrt(p * (1 - p) / (double)n + z2 / (4.0 * (double)n * (double)n)) / d; |
| 237 | double lo = c - h, hi = c + h; |
| 238 | if (lo < 0) lo = 0; |
| 239 | if (hi > 1) hi = 1; |
| 240 | printf("%.4f %.4f %.4f\n", p, lo, hi); |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* exact two-sided McNemar test on the discordant pairs: b (only the first arm succeeds) and c (only the |
| 245 | * second): p = min(1, 2 * P(X <= min(b, c))), X ~ Binomial(b + c, 1/2); p = 1 when b + c = 0 */ |
| 246 | static int cmd_mcnemar(const char *bs, const char *cs) |
| 247 | { |
| 248 | char *e1, *e2; |
| 249 | long b = strtol(bs, &e1, 10), c = strtol(cs, &e2, 10); |
| 250 | if (*e1 || *e2 || !*bs || !*cs || b < 0 || c < 0 || b > 1000000 || c > 1000000 || b + c > 1000000) { fprintf(stderr, "x445: mcnemar b c with 0 <= b, c and b + c <= 1000000\n"); return 2; } |
| 251 | long n = b + c, m = b < c ? b : c; |
| 252 | double s = 0; |
| 253 | for (long i = 0; i <= m && n > 0; i++) |
| 254 | s += exp(lgamma((double)n + 1) - lgamma((double)i + 1) - lgamma((double)(n - i) + 1) - (double)n * log(2.0)); |
| 255 | double p = n == 0 ? 1.0 : 2 * s; |
| 256 | if (p > 1) p = 1; |
| 257 | printf("%.6f\n", p); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int main(int argc, char **argv) |
| 262 | { |
| 263 | if (argc == 4 && !strcmp(argv[1], "extract")) return cmd_extract(argv[2], argv[3]); |
| 264 | if (argc == 4 && !strcmp(argv[1], "hex")) return cmd_hex(argv[2], argv[3]); |
| 265 | if (argc == 3 && !strcmp(argv[1], "tohex")) return cmd_tohex(argv[2]); |
| 266 | if (argc == 5 && !strcmp(argv[1], "check")) return cmd_check(argv[2], argv[3], argv[4]); |
| 267 | if (argc == 4 && !strcmp(argv[1], "wilson")) return cmd_wilson(argv[2], argv[3]); |
| 268 | if (argc == 4 && !strcmp(argv[1], "mcnemar")) return cmd_mcnemar(argv[2], argv[3]); |
| 269 | fprintf(stderr, "usage: x445 extract <content> <out> | hex <in> <out> | tohex <in> | check osc|oscir <file> <requirement> | wilson <k> <n> | mcnemar <b> <c>\n"); |
| 270 | return 2; |
| 271 | } |