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/omega_synthesis.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 / omega_synthesis.c` 456 lines ยท 16958 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 "omega_synthesis.h" 18 #include "omega_canonical.h" 19 #include "omega_exec.h" 20 #include "sha256.h" 21 #include "searchtrace/st_hook.h" 22 #include 23 #include 24 #include 25 26 static const uint64_t PROBE_INPUTS[SYNTH_PROBE_COUNT] = { 0, 1, 3, 7, 13, 42 }; 27 28 static const OmegaSynthPrimDef BASE_PRIM_DEFS[] = { 29 { "add_1", OP_ADD, 1 }, 30 { "add_2", OP_ADD, 2 }, 31 { "add_3", OP_ADD, 3 }, 32 { "add_5", OP_ADD, 5 }, 33 { "sub_1", OP_SUB, 1 }, 34 { "sub_2", OP_SUB, 2 }, 35 { "sub_3", OP_SUB, 3 }, 36 { "sub_5", OP_SUB, 5 }, 37 { "mul_2", OP_MUL, 2 }, 38 { "mul_3", OP_MUL, 3 }, 39 { "mul_4", OP_MUL, 4 }, 40 { "and_ff", OP_AND, 0xFF }, 41 { "and_0f", OP_AND, 0x0F }, 42 { "or_1", OP_OR, 1 }, 43 { "or_2", OP_OR, 2 } 44 }; 45 46 size_t omega_synth_base_prim_defs(const OmegaSynthPrimDef **out) { 47 if (out) *out = BASE_PRIM_DEFS; 48 return sizeof(BASE_PRIM_DEFS) / sizeof(BASE_PRIM_DEFS[0]); 49 } 50 51 int omega_synth_bank_init(SynthPrimitiveBank *bank) { 52 if (!bank) return -1; 53 memset(bank, 0, sizeof(SynthPrimitiveBank)); 54 55 const OmegaSynthPrimDef *prim_defs = NULL; 56 size_t num_defs = omega_synth_base_prim_defs(&prim_defs); 57 58 for (size_t i = 0; i < num_defs && i < SYNTH_MAX_PRIMITIVES; ++i) { 59 if (omega_program_build_unary_op(&bank->programs[i], prim_defs[i].name, 60 prim_defs[i].op, prim_defs[i].imm) == 0) { 61 bank->count++; 62 } 63 } 64 65 return 0; 66 } 67 68 void omega_synth_bank_destroy(SynthPrimitiveBank *bank) { 69 if (!bank) return; 70 for (size_t i = 0; i < bank->count; ++i) { 71 omega_program_destroy(&bank->programs[i]); 72 } 73 bank->count = 0; 74 } 75 76 void omega_synth_equiv_init(EquivTable *tbl) { 77 if (!tbl) return; 78 memset(tbl, 0, sizeof(EquivTable)); 79 } 80 81 bool omega_synth_equiv_contains_or_add(EquivTable *tbl, const uint8_t hash[32], uint32_t cost) { 82 if (!tbl) return false; 83 84 for (size_t i = 0; i < tbl->count; ++i) { 85 if (memcmp(tbl->entries[i].hash, hash, 32) == 0) { 86 if (tbl->entries[i].cost <= cost) { 87 return true; /* Prune: already seen with equal or lower cost */ 88 } else { 89 tbl->entries[i].cost = cost; 90 return false; /* Keep: current candidate is strictly lower cost */ 91 } 92 } 93 } 94 95 if (tbl->count < SYNTH_MAX_SIGNATURES) { 96 memcpy(tbl->entries[tbl->count].hash, hash, 32); 97 tbl->entries[tbl->count].cost = cost; 98 tbl->count++; 99 } 100 return false; 101 } 102 103 int omega_synth_compute_signature(const OmegaProgram *prog, uint8_t out_hash[32]) { 104 if (!prog || !out_hash || !prog->is_realized) return -1; 105 106 uint64_t outputs[SYNTH_PROBE_COUNT]; 107 for (size_t i = 0; i < SYNTH_PROBE_COUNT; ++i) { 108 uint64_t out_val = 0; 109 if (omega_program_exec(prog, PROBE_INPUTS[i], &out_val) != 0) { 110 return -1; 111 } 112 outputs[i] = out_val; 113 } 114 115 sha256_hash((const uint8_t*)outputs, sizeof(outputs), out_hash); 116 return 0; 117 } 118 119 /* ---- M23 search-trace recorder hook (src/searchtrace/st_hook.h) ---------- 120 * Off by default. With no hook installed every emission point below is a single 121 * null test, so search order, pruning, results and stats are unchanged. */ 122 static _Thread_local StHookFn st_synth_hook; 123 static _Thread_local void *st_synth_hook_ctx; 124 125 int omega_synth_set_trace_hook(StHookFn fn, void *ctx) { 126 if (!fn || st_synth_hook) return -1; 127 st_synth_hook = fn; 128 st_synth_hook_ctx = ctx; 129 return 0; 130 } 131 132 int omega_synth_clear_trace_hook(void *ctx) { 133 if (!st_synth_hook || st_synth_hook_ctx != ctx) return -1; 134 st_synth_hook = NULL; 135 st_synth_hook_ctx = NULL; 136 return 0; 137 } 138 139 static void st_synth_emit(uint32_t depth, size_t parent_index, size_t prim_index, 140 const OmegaProgram *parent, const OmegaProgram *prim, 141 const OmegaProgram *child, uint32_t prune, uint32_t verdict, 142 int eval_rc, int verify_rc, const uint8_t *sig) { 143 StEvent ev; 144 memset(&ev, 0, sizeof ev); 145 ev.kind = ST_EV_SYNTH_CANDIDATE; 146 ev.u.synth.depth = depth; 147 ev.u.synth.parent_index = parent ? (int32_t)parent_index : -1; 148 ev.u.synth.prim_index = (int32_t)prim_index; 149 ev.u.synth.parent = parent; 150 ev.u.synth.prim = prim; 151 ev.u.synth.child = child; 152 ev.u.synth.prune = prune; 153 ev.u.synth.verdict = verdict; 154 ev.u.synth.eval_rc = eval_rc; 155 ev.u.synth.verify_rc = verify_rc; 156 if (sig) { 157 ev.u.synth.has_signature = 1; 158 memcpy(ev.u.synth.signature, sig, 32); 159 } 160 st_synth_hook(st_synth_hook_ctx, &ev); 161 } 162 163 int omega_synthesize(const SynthesisTask *task, const SynthPrimitiveBank *bank, 164 const SynthesisConfig *config, SynthesisResult *result) { 165 if (!task || !bank || !config || !result) return -1; 166 memset(result, 0, sizeof(SynthesisResult)); 167 168 EquivTable equiv_tbl; 169 omega_synth_equiv_init(&equiv_tbl); 170 171 OmegaProgram depth1[SYNTH_MAX_PRIMITIVES]; 172 size_t depth1_count = 0; 173 174 OmegaProgram depth2[SYNTH_MAX_CANDIDATES]; 175 size_t depth2_count = 0; 176 177 /* ------------------------------------------------------------- 178 * DEPTH 1: Base primitives exploration 179 * ------------------------------------------------------------- */ 180 for (size_t i = 0; i < bank->count; ++i) { 181 const OmegaProgram *p = &bank->programs[i]; 182 result->stats.candidates_generated++; 183 uint8_t sig[32]; 184 bool has_sig = false; 185 186 if (config->deduplicate_equiv) { 187 if (omega_synth_compute_signature(p, sig) == 0) { 188 has_sig = true; 189 if (omega_synth_equiv_contains_or_add(&equiv_tbl, sig, p->cost.insn_count)) { 190 result->stats.candidates_pruned_equiv++; 191 if (st_synth_hook) 192 st_synth_emit(1, 0, i, NULL, p, p, ST_PRUNE_EQUIV, ST_VERDICT_NONE, 0, 0, sig); 193 continue; 194 } 195 } 196 } 197 198 bool solved = false; 199 int erc = omega_task_evaluate_candidate(task, p, &solved); 200 if (erc == 0 && solved) { 201 VerifyReport rep; 202 int vrc = omega_program_verify((OmegaProgram*)p, &rep); 203 if (st_synth_hook) 204 st_synth_emit(1, 0, i, NULL, p, p, ST_PRUNE_NONE, 205 vrc == 0 ? ST_VERDICT_SOLVED : ST_VERDICT_VERIFY_FAIL, erc, vrc, 206 has_sig ? sig : NULL); 207 if (vrc == 0) { 208 result->solved = true; 209 result->solution = *p; 210 result->verify_report = rep; 211 result->stats.solutions_found++; 212 return 0; 213 } 214 } else { 215 result->stats.candidates_failed_v1++; 216 if (st_synth_hook) 217 st_synth_emit(1, 0, i, NULL, p, p, ST_PRUNE_NONE, ST_VERDICT_REJECT, erc, 0, 218 has_sig ? sig : NULL); 219 } 220 221 if (depth1_count < SYNTH_MAX_PRIMITIVES) { 222 depth1[depth1_count++] = *p; 223 } 224 } 225 226 if (config->max_depth < 2) { 227 return 0; 228 } 229 230 /* ------------------------------------------------------------- 231 * DEPTH 2: Binary compositions C = A o B 232 * ------------------------------------------------------------- */ 233 for (size_t i = 0; i < depth1_count; ++i) { 234 for (size_t j = 0; j < bank->count; ++j) { 235 if (result->stats.candidates_generated >= config->max_candidates) { 236 if (st_synth_hook) 237 st_synth_emit(2, i, j, &depth1[i], &bank->programs[j], NULL, ST_PRUNE_BUDGET, 238 ST_VERDICT_NONE, 0, 0, NULL); 239 goto cleanup; 240 } 241 result->stats.candidates_generated++; 242 243 OmegaProgram cand; 244 char err[256]; 245 if (omega_program_compose(&depth1[i], &bank->programs[j], &cand, err, sizeof(err)) != 0) { 246 result->stats.candidates_pruned_type++; 247 if (st_synth_hook) 248 st_synth_emit(2, i, j, &depth1[i], &bank->programs[j], NULL, ST_PRUNE_TYPE, 249 ST_VERDICT_NONE, 0, 0, NULL); 250 continue; 251 } 252 253 if (cand.cost.insn_count > task->cost_budget.insn_count || 254 cand.cost.insn_count > config->max_cost) { 255 if (st_synth_hook) 256 st_synth_emit(2, i, j, &depth1[i], &bank->programs[j], &cand, ST_PRUNE_COST, 257 ST_VERDICT_NONE, 0, 0, NULL); 258 omega_program_destroy(&cand); 259 continue; 260 } 261 262 uint8_t sig[32]; 263 bool has_sig = false; 264 if (config->deduplicate_equiv) { 265 if (omega_synth_compute_signature(&cand, sig) == 0) { 266 has_sig = true; 267 if (omega_synth_equiv_contains_or_add(&equiv_tbl, sig, cand.cost.insn_count)) { 268 result->stats.candidates_pruned_equiv++; 269 if (st_synth_hook) 270 st_synth_emit(2, i, j, &depth1[i], &bank->programs[j], &cand, ST_PRUNE_EQUIV, 271 ST_VERDICT_NONE, 0, 0, sig); 272 omega_program_destroy(&cand); 273 continue; 274 } 275 } 276 } 277 278 bool solved = false; 279 int erc = omega_task_evaluate_candidate(task, &cand, &solved); 280 if (erc == 0 && solved) { 281 VerifyReport rep; 282 int vrc = omega_program_verify(&cand, &rep); 283 if (st_synth_hook) 284 st_synth_emit(2, i, j, &depth1[i], &bank->programs[j], &cand, ST_PRUNE_NONE, 285 vrc == 0 ? ST_VERDICT_SOLVED : ST_VERDICT_VERIFY_FAIL, erc, vrc, 286 has_sig ? sig : NULL); 287 if (vrc == 0) { 288 result->solved = true; 289 result->solution = cand; 290 result->verify_report = rep; 291 result->stats.solutions_found++; 292 goto cleanup; 293 } 294 } else { 295 result->stats.candidates_failed_v1++; 296 if (st_synth_hook) 297 st_synth_emit(2, i, j, &depth1[i], &bank->programs[j], &cand, ST_PRUNE_NONE, 298 ST_VERDICT_REJECT, erc, 0, has_sig ? sig : NULL); 299 } 300 301 if (depth2_count < SYNTH_MAX_CANDIDATES) { 302 depth2[depth2_count++] = cand; 303 } else { 304 omega_program_destroy(&cand); 305 } 306 } 307 } 308 309 if (config->max_depth < 3 || result->solved) { 310 goto cleanup; 311 } 312 313 /* ------------------------------------------------------------- 314 * DEPTH 3: Ternary compositions C = (A o B) o C 315 * ------------------------------------------------------------- */ 316 for (size_t i = 0; i < depth2_count; ++i) { 317 for (size_t j = 0; j < bank->count; ++j) { 318 if (result->stats.candidates_generated >= config->max_candidates) { 319 if (st_synth_hook) 320 st_synth_emit(3, i, j, &depth2[i], &bank->programs[j], NULL, ST_PRUNE_BUDGET, 321 ST_VERDICT_NONE, 0, 0, NULL); 322 goto cleanup; 323 } 324 result->stats.candidates_generated++; 325 326 OmegaProgram cand; 327 char err[256]; 328 if (omega_program_compose(&depth2[i], &bank->programs[j], &cand, err, sizeof(err)) != 0) { 329 result->stats.candidates_pruned_type++; 330 if (st_synth_hook) 331 st_synth_emit(3, i, j, &depth2[i], &bank->programs[j], NULL, ST_PRUNE_TYPE, 332 ST_VERDICT_NONE, 0, 0, NULL); 333 continue; 334 } 335 336 if (cand.cost.insn_count > task->cost_budget.insn_count || 337 cand.cost.insn_count > config->max_cost) { 338 if (st_synth_hook) 339 st_synth_emit(3, i, j, &depth2[i], &bank->programs[j], &cand, ST_PRUNE_COST, 340 ST_VERDICT_NONE, 0, 0, NULL); 341 omega_program_destroy(&cand); 342 continue; 343 } 344 345 uint8_t sig[32]; 346 bool has_sig = false; 347 if (config->deduplicate_equiv) { 348 if (omega_synth_compute_signature(&cand, sig) == 0) { 349 has_sig = true; 350 if (omega_synth_equiv_contains_or_add(&equiv_tbl, sig, cand.cost.insn_count)) { 351 result->stats.candidates_pruned_equiv++; 352 if (st_synth_hook) 353 st_synth_emit(3, i, j, &depth2[i], &bank->programs[j], &cand, ST_PRUNE_EQUIV, 354 ST_VERDICT_NONE, 0, 0, sig); 355 omega_program_destroy(&cand); 356 continue; 357 } 358 } 359 } 360 361 bool solved = false; 362 int erc = omega_task_evaluate_candidate(task, &cand, &solved); 363 if (erc == 0 && solved) { 364 VerifyReport rep; 365 int vrc = omega_program_verify(&cand, &rep); 366 if (st_synth_hook) 367 st_synth_emit(3, i, j, &depth2[i], &bank->programs[j], &cand, ST_PRUNE_NONE, 368 vrc == 0 ? ST_VERDICT_SOLVED : ST_VERDICT_VERIFY_FAIL, erc, vrc, 369 has_sig ? sig : NULL); 370 if (vrc == 0) { 371 result->solved = true; 372 result->solution = cand; 373 result->verify_report = rep; 374 result->stats.solutions_found++; 375 goto cleanup; 376 } 377 } else { 378 result->stats.candidates_failed_v1++; 379 if (st_synth_hook) 380 st_synth_emit(3, i, j, &depth2[i], &bank->programs[j], &cand, ST_PRUNE_NONE, 381 ST_VERDICT_REJECT, erc, 0, has_sig ? sig : NULL); 382 } 383 384 omega_program_destroy(&cand); 385 } 386 } 387 388 cleanup: 389 /* Clean up depth2 candidates that weren't selected as solution */ 390 for (size_t i = 0; i < depth2_count; ++i) { 391 if (!result->solved || 392 memcmp(depth2[i].program_id.bytes, result->solution.program_id.bytes, OMEGA_ID_BYTES) != 0) { 393 omega_program_destroy(&depth2[i]); 394 } 395 } 396 397 return 0; 398 } 399 400 int omega_synthesize_target_affine(SynthesisResult *result) { 401 if (!result) return -1; 402 403 SynthesisTask task; 404 static const uint64_t inputs[] = { 0, 1, 2, 3, 5, 10 }; 405 static const uint64_t outputs[] = { 1, 3, 5, 7, 11, 21 }; 406 size_t count = sizeof(inputs) / sizeof(inputs[0]); 407 408 if (omega_task_init(&task, "task_affine_2x_plus_1", 409 TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, 410 inputs, outputs, count) != 0) { 411 return -1; 412 } 413 414 SynthPrimitiveBank bank; 415 if (omega_synth_bank_init(&bank) != 0) return -1; 416 417 SynthesisConfig config = { 418 .max_depth = 2, 419 .max_cost = 10, 420 .max_candidates = 500, 421 .deduplicate_equiv = true 422 }; 423 424 int rc = omega_synthesize(&task, &bank, &config, result); 425 omega_synth_bank_destroy(&bank); 426 return rc; 427 } 428 429 int omega_synthesize_target_composed(SynthesisResult *result) { 430 if (!result) return -1; 431 432 SynthesisTask task; 433 static const uint64_t inputs[] = { 1, 2, 3, 4, 10 }; 434 static const uint64_t outputs[] = { 1, 4, 7, 10, 28 }; 435 size_t count = sizeof(inputs) / sizeof(inputs[0]); 436 437 if (omega_task_init(&task, "task_composed_3x_minus_2", 438 TYPE_UNSIGNED_INT, 64, TYPE_UNSIGNED_INT, 64, 439 inputs, outputs, count) != 0) { 440 return -1; 441 } 442 443 SynthPrimitiveBank bank; 444 if (omega_synth_bank_init(&bank) != 0) return -1; 445 446 SynthesisConfig config = { 447 .max_depth = 2, 448 .max_cost = 10, 449 .max_candidates = 500, 450 .deduplicate_equiv = true 451 }; 452 453 int rc = omega_synthesize(&task, &bank, &config, result); 454 omega_synth_bank_destroy(&bank); 455 return rc; 456 }