Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/checker2/c2_lrat.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / checker2 / c2_lrat.c` 298 lines ยท 10547 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 /* c2_lrat.c: DIMACS reader and RUP-only LRAT checker (see c2_lrat.h for the format and rules). */ 18 #include 19 #include 20 #include 21 22 #include "c2_lrat.h" 23 24 #define C2_LRAT_MAX_VARS `4000000` 25 #define C2_LRAT_MAX_ID 100000000L 26 27 /* ------------------------------------------------------------------ tokens */ 28 29 typedef struct { 30 const char *p; 31 size_t n, i; 32 long line; 33 } Lx; 34 35 static void skip_ws(Lx *x) 36 { 37 for (;;) { 38 while (x->i < x->n && (x->p[x->i] == ' ' || x->p[x->i] == '\t' || x->p[x->i] == '\r' || x->p[x->i] == '\n')) { 39 if (x->p[x->i] == '\n') x->line++; 40 x->i++; 41 } 42 if (x->i < x->n && x->p[x->i] == 'c') { /* comment to end of line */ 43 while (x->i < x->n && x->p[x->i] != '\n') x->i++; 44 continue; 45 } 46 return; 47 } 48 } 49 50 /* signed decimal integer; -1 on malformed/overflow, 1 at end of input, 0 ok */ 51 static int rd_int(Lx *x, long *out) 52 { 53 skip_ws(x); 54 if (x->i >= x->n) return 1; 55 int neg = 0; 56 if (x->p[x->i] == '-') { neg = 1; x->i++; } 57 if (x->i >= x->n || x->p[x->i] < '0' || x->p[x->i] > '9') return -1; 58 long v = 0; 59 while (x->i < x->n && x->p[x->i] >= '0' && x->p[x->i] <= '9') { 60 v = v * 10 + (x->p[x->i] - '0'); 61 if (v > C2_LRAT_MAX_ID) return -1; 62 x->i++; 63 } 64 if (x->i < x->n && !(x->p[x->i] == ' ' || x->p[x->i] == '\t' || x->p[x->i] == '\n' || x->p[x->i] == '\r')) return -1; 65 *out = neg ? -v : v; 66 return 0; 67 } 68 69 /* ------------------------------------------------------------------ CNF */ 70 71 void c2_cnf_free(C2Cnf *c) 72 { 73 if (!c) return; 74 free(c->lits); 75 free(c->start); 76 memset(c, 0, sizeof *c); 77 } 78 79 int c2_cnf_add(C2Cnf *c, const int32_t *lits, int n) 80 { 81 size_t need = c->nlits + (size_t)n + 1; 82 if (need > c->lcap) { 83 size_t nc = c->lcap ? c->lcap : 1024; 84 while (nc < need) nc *= 2; 85 int32_t *nl = realloc(c->lits, nc * sizeof *nl); 86 if (!nl) return -1; 87 c->lits = nl; 88 c->lcap = nc; 89 } 90 if ((size_t)c->nclauses + 1 > c->scap) { 91 size_t nc = c->scap ? c->scap * 2 : 1024; 92 size_t *ns = realloc(c->start, nc * sizeof *ns); 93 if (!ns) return -1; 94 c->start = ns; 95 c->scap = nc; 96 } 97 c->start[c->nclauses++] = c->nlits; 98 for (int k = 0; k < n; k++) { 99 int32_t l = lits[k]; 100 int v = l < 0 ? -l : l; 101 if (l == 0 || v > C2_LRAT_MAX_VARS) return -1; 102 if (v > c->nvars) c->nvars = v; 103 c->lits[c->nlits++] = l; 104 } 105 c->lits[c->nlits++] = 0; 106 return 0; 107 } 108 109 int c2_cnf_parse(const char *text, size_t len, C2Cnf *c, const char **why) 110 { 111 memset(c, 0, sizeof *c); 112 Lx x = {text, len, 0, 1}; 113 skip_ws(&x); 114 if (len - x.i < 6 || memcmp(text + x.i, "p cnf ", 6) != 0) { *why = "CNF-HEADER"; return -1; } 115 x.i += 6; 116 long nv, nc; 117 if (rd_int(&x, &nv) || rd_int(&x, &nc) || nv < 0 || nc < 0 || nv > C2_LRAT_MAX_VARS) { *why = "CNF-HEADER"; return -1; } 118 int32_t buf[4096]; 119 int n = 0; 120 for (;;) { 121 long l; 122 int r = rd_int(&x, &l); 123 if (r == 1) break; 124 if (r < 0 || l > nv || -l > nv) { *why = "CNF-LITERAL"; c2_cnf_free(c); return -1; } 125 if (l == 0) { 126 if (c2_cnf_add(c, buf, n)) { *why = "CNF-RESOURCE"; c2_cnf_free(c); return -1; } 127 n = 0; 128 continue; 129 } 130 if (n == 4096) { *why = "CNF-CLAUSE-TOO-LONG"; c2_cnf_free(c); return -1; } 131 buf[n++] = (int32_t)l; 132 } 133 if (n != 0 || c->nclauses != nc) { *why = "CNF-COUNT"; c2_cnf_free(c); return -1; } /* GUARD:cnf-count */ 134 c->nvars = (int)nv; 135 return 0; 136 } 137 138 char *c2_cnf_print(const C2Cnf *c, size_t *len) 139 { 140 size_t cap = 64 + c->nlits * 12, n = 0; 141 char *s = malloc(cap); 142 if (!s) return NULL; 143 n += (size_t)snprintf(s + n, cap - n, "p cnf %d %d\n", c->nvars, c->nclauses); 144 for (size_t i = 0; i < c->nlits; i++) 145 n += (size_t)snprintf(s + n, cap - n, c->lits[i] ? "%d " : "%d\n", c->lits[i]); 146 *len = n; 147 return s; 148 } 149 150 /* ------------------------------------------------------------------ LRAT */ 151 152 typedef struct { 153 int32_t **cl; /* by id; NULL = absent or deleted; each ends with 0 */ 154 long cap; 155 } Db; 156 157 static int db_put(Db *d, long id, int32_t *lits) 158 { 159 if (id >= d->cap) { 160 long nc = d->cap ? d->cap : 1024; 161 while (nc <= id) nc *= 2; 162 int32_t **n = realloc(d->cl, (size_t)nc * sizeof *n); 163 if (!n) return -1; 164 memset(n + d->cap, 0, (size_t)(nc - d->cap) * sizeof *n); 165 d->cl = n; 166 d->cap = nc; 167 } 168 d->cl[id] = lits; 169 return 0; 170 } 171 172 static int32_t *dup_clause(const int32_t *l, int n) 173 { 174 int32_t *c = malloc(((size_t)n + 1) * sizeof *c); 175 if (!c) return NULL; 176 memcpy(c, l, (size_t)n * sizeof *c); 177 c[n] = 0; 178 return c; 179 } 180 181 static int fail(C2LratResult *r, const char *rule, long line) 182 { 183 r->ok = 0; 184 r->rule = rule; 185 r->line = line; 186 return -1; 187 } 188 189 int c2_lrat_check(const C2Cnf *cnf, const char *proof, size_t len, C2LratResult *r) 190 { 191 memset(r, 0, sizeof *r); 192 r->rule = "LRAT-OK"; 193 Db d = {NULL, 0}; 194 int nv = cnf->nvars; 195 int8_t *val = calloc((size_t)nv + 1, 1); /* +1 true, -1 false, 0 unassigned (by variable) */ 196 int32_t *assigned = malloc(((size_t)nv + 1) * sizeof *assigned); 197 int32_t *lits = malloc(((size_t)nv + 2) * sizeof *lits); 198 int rc = -1; 199 if (!val || !assigned || !lits) { fail(r, "LRAT-RESOURCE", 0); goto out; } 200 for (int i = 0; i < cnf->nclauses; i++) { 201 const int32_t *c = cnf->lits + cnf->start[i]; 202 int n = 0; 203 while (c[n]) n++; 204 int32_t *k = dup_clause(c, n); 205 if (!k || db_put(&d, i + 1, k)) { free(k); fail(r, "LRAT-RESOURCE", 0); goto out; } 206 } 207 /* Clause ids are capped at (input clauses + proof lines): textual LRAT writes one step per line, 208 * so an honest proof with dense ids never exceeds it, and a huge id cannot make the clause table huge (the table 209 * is indexed by id). */ 210 long max_id = cnf->nclauses + 1; 211 for (size_t i = 0; i < len; i++) max_id += proof[i] == '\n'; 212 Lx x = {proof, len, 0, 1}; 213 long last_id = cnf->nclauses; 214 for (;;) { 215 long id; 216 int t = rd_int(&x, &id); 217 if (t == 1) break; 218 long line = x.line; 219 if (t < 0 || id < 1) { fail(r, "LRAT-SYNTAX", line); goto out; } 220 skip_ws(&x); 221 if (x.i < x.n && x.p[x.i] == 'd') { /* deletion */ 222 x.i++; 223 for (;;) { 224 long del; 225 if (rd_int(&x, &del)) { fail(r, "LRAT-SYNTAX", line); goto out; } 226 if (del == 0) break; 227 if (del < 0 || del >= d.cap || !d.cl[del]) { fail(r, "LRAT-DELETE-UNKNOWN", line); goto out; } /* GUARD:delete-unknown */ 228 free(d.cl[del]); 229 d.cl[del] = NULL; 230 r->deletions++; 231 } 232 continue; 233 } 234 if (id <= last_id) { fail(r, "LRAT-ID-ORDER", line); goto out; } /* GUARD:id-order */ 235 if (id > max_id) { fail(r, "LRAT-ID-RANGE", line); goto out; } /* GUARD:id-range */ 236 last_id = id; 237 /* clause literals */ 238 int n = 0, na = 0, ok = 0; 239 for (;;) { 240 long l; 241 if (rd_int(&x, &l)) { fail(r, "LRAT-SYNTAX", line); goto out; } 242 if (l == 0) break; 243 if (l > nv || -l > nv) { fail(r, "LRAT-LITERAL", line); goto out; } /* GUARD:lrat-literal */ 244 if (n > nv) { fail(r, "LRAT-SYNTAX", line); goto out; } 245 lits[n++] = (int32_t)l; 246 } 247 /* assign every literal of the new clause false */ 248 int taut = 0; 249 for (int k = 0; k < n; k++) { 250 int v = lits[k] < 0 ? -lits[k] : lits[k]; 251 int8_t want = lits[k] < 0 ? 1 : -1; 252 if (val[v] == -want) taut = 1; 253 if (!val[v]) { val[v] = want; assigned[na++] = v; } 254 } 255 ok = taut; /* a tautology is implied by anything */ 256 /* hints */ 257 for (;;) { 258 long h; 259 if (rd_int(&x, &h)) { fail(r, "LRAT-SYNTAX", line); goto undo_fail; } 260 if (h == 0) break; 261 if (ok) continue; /* trailing hints after the conflict are allowed and ignored */ 262 if (h < 0) { fail(r, "LRAT-RAT-UNSUPPORTED", line); goto undo_fail; } /* GUARD:rat-refused */ 263 if (h >= d.cap || !d.cl[h]) { fail(r, "LRAT-HINT-UNKNOWN", line); goto undo_fail; } /* GUARD:hint-unknown */ 264 const int32_t *c = d.cl[h]; 265 int32_t unit = 0; 266 int nfree = 0, sat = 0; 267 for (int k = 0; c[k]; k++) { 268 int v = c[k] < 0 ? -c[k] : c[k]; 269 int8_t s = val[v] == 0 ? 0 : ((c[k] > 0) == (val[v] > 0) ? 1 : -1); 270 if (s == 1) { sat = 1; break; } 271 if (s == 0) { nfree++; unit = c[k]; } 272 } 273 if (sat || nfree > 1) { fail(r, "LRAT-HINT-NOT-UNIT", line); goto undo_fail; } /* GUARD:hint-not-unit */ 274 if (nfree == 0) { ok = 1; continue; } /* falsified: the step is proved */ 275 int v = unit < 0 ? -unit : unit; 276 val[v] = unit > 0 ? 1 : -1; 277 assigned[na++] = v; 278 } 279 for (int k = 0; k < na; k++) val[assigned[k]] = 0; 280 if (!ok) { fail(r, "LRAT-NO-CONFLICT", line); goto out; } /* GUARD:no-conflict */ 281 int32_t *k = dup_clause(lits, n); 282 if (!k || db_put(&d, id, k)) { free(k); fail(r, "LRAT-RESOURCE", line); goto out; } 283 r->steps++; 284 if (n == 0) { r->ok = 1; rc = 0; goto out; } /* empty clause: unsatisfiable */ 285 continue; 286 undo_fail: 287 for (int k = 0; k < na; k++) val[assigned[k]] = 0; 288 goto out; 289 } 290 fail(r, "LRAT-NO-EMPTY-CLAUSE", x.line); /* GUARD:no-empty-clause */ 291 out: 292 for (long i = 0; i < d.cap; i++) free(d.cl[i]); 293 free(d.cl); 294 free(val); 295 free(assigned); 296 free(lits); 297 return rc; 298 }