code wiki / _hdl_build / nx_eqsat_constfold_test.nx
nx_eqsat_constfold_test.nx
buildroot/runtime/_hdl_build/nx_eqsat_constfold_test.nx
about
nx_eqsat_constfold_test.nx -- the CONST-FOLD GATE: proves the egg-style e-class
ANALYSIS (constant folding / propagation) newly integrated into nx_eqsat.nx is
(a) SOUND BY EVALUATION (folds via the PROVEN nx_gsim_eval_cell oracle),
(b) CERTIFIED through membership-as-proof (cites NX_EQSAT_RULE_CONSTFOLD, admitted
to the proven-sound allow-list + lockstep), and
(c) LOAD-BEARING (a wrong fold / unmapped op / unproven rule is REFUSED).
FAIL LOUD known-answer; exit 0 iff every check holds.
STEPS (each self-asserts; mismatch => sys_exit(nonzero)):
STEP 0 LOCKSTEP: MP_RULE_CONSTFOLD == NX_EQSAT_RULE_CONSTFOLD (and the whole
id table agrees), so the live log the engine writes is never mislabeled.
Also: MP_RULE_CONSTFOLD must be in the proven-sound allow-list.
STEP 1 POSITIVE (add 2 3)->5, LIVE + CERTIFIED: enable prov + constfold, run the
REAL saturator, assert find(add23)==find(5), the log contains RULE_CONSTFOLD,
and mp_certify_live CERTIFIES (member && sound).
STEP 2 MORE OPS: (mul 6 7)->42 and (shl 1 4)->16 fold + certify (same engine path).
STEP 3 NEGATIVE -- WRONG FOLD REFUSED: (a) an unmapped op (DIV) never folds even
with const children (nx_eqsat_op_to_gate_kind(DIV) < 0) -- find stays
distinct from any const; (b) an UNPROVEN rule id (BOGUS) is NOT in the
allow-list so mp_log_all_sound would refuse it -- a back-door wrong fold
cannot certify; (c) folding is OFF by default (no enable => no fold => no
CONSTFOLD in the log) so the feature is opt-in by construction.
Known answer (FAIL LOUD): prints "5 42 16 1 1 1 " == folded(add 2 3), folded(mul 6 7),
folded(shl 1 4), cert_add, cert_mul, cert_shl. exit 0 iff all steps hold.
dependencies 1 imports · 0 importers
imports: nx_eqsat_membership_proof.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| none |
functions
| 30 | func _emit_num(v: i64) -> i64 called by 1: main |
| 38 | func _nl() -> i64 { let z: *u8 = sys_mmap(2); z[0] = 10; sys_write(1, z, 1); return 0 } called by 1: main |
| 41 | func _log_has(g: *NxEGraph, want: i64) -> i64 |
| 53 | func _fold_binop(op: i64, a: i64, b: i64, cert_out: *i64) -> i64 |
| 86 | func main() -> i64 |