code wiki / _hdl_build / nx_eco_graph_ortho_gate.nx

nx_eco_graph_ortho_gate.nx source

↩ module page · 52 lines · 3291 B

1// nx_eco_graph_ortho_gate.nx -- R4-core: proves the ORTHOGONALITY metrics on the graph. A layered DAG has NO 2// import cycle; adding a back-edge (a tangle) is DETECTED (neg-control = the detector is real, not always-0). 3// + coupling-distribution stats (max Ca, hubs, leaves). license_tier: ORIGINAL expect_exit: 0 4import "nx_syscalls.nx" 5import "nx_eco_graph.nx" 6 7func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func pn(v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j: i64=0; while j<k{b[j]=t[k-1-j];j=j+1} sys_write(1,b,k); return 0 } 9func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 10func gi(g: *EcoGraph, s: *u8) -> i64 { return eg_intern(g, s, slen(s)) } 11 12func main() -> i64 { 13 hw("=== nx_eco_graph_ortho_gate -- orthogonality: cycle(tangle) detection + coupling stats ===\n" as *u8) 14 var fails: i64 = 0 15 16 // ---- DAG: god <- a,b <- top (imports flow toward god, no loop) ---- 17 let g: *EcoGraph = eg_new(64, 256, 4096, 128) 18 let god: i64 = gi(g, "nx_god.nx" as *u8) 19 let a: i64 = gi(g, "nx_a.nx" as *u8) 20 let b: i64 = gi(g, "nx_b.nx" as *u8) 21 let top: i64 = gi(g, "nx_top.nx" as *u8) 22 eg_add_edge(g, a, god); eg_add_edge(g, b, god); eg_add_edge(g, top, a); eg_add_edge(g, top, b) 23 eg_finalize(g) 24 25 // T1: a clean DAG has NO cycle 26 var t1: i64 = 0 27 if eg_has_cycle(g) == 0 { t1 = 1 } 28 if t1 == 1 { hw("T1 PASS clean layered DAG -> no import cycle (orthogonal layering)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL cycle in a DAG\n" as *u8) } 29 30 // T2: coupling stats on the DAG (god imported by a,b => Ca=2 max; leaves = top has Ca=0) 31 let cs: *i64 = sys_mmap(32) as *i64 32 eg_coupling_stats(g, 2, cs) 33 hw(" coupling: max_Ca=" as *u8); pn(cs[0]); hw(" hubs(Ca>=2)=" as *u8); pn(cs[1]); hw(" leaves(Ca==0)=" as *u8); pn(cs[2]); hw("\n" as *u8) 34 var t2: i64 = 0 35 if cs[0] == 2 { if cs[2] == 1 { t2 = 1 } } // max Ca = god's 2 ; exactly one Ca==0 node (top) 36 if t2 == 1 { hw("T2 PASS coupling stats: max_Ca=2 (god), 1 entry/leaf (top)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL stats\n" as *u8) } 37 38 // ---- NEG-CONTROL: add a back-edge god->top => a TANGLE (cycle) that MUST be detected ---- 39 let g2: *EcoGraph = eg_new(64, 256, 4096, 128) 40 let god2: i64 = gi(g2, "nx_god.nx" as *u8) 41 let a2: i64 = gi(g2, "nx_a.nx" as *u8) 42 let top2: i64 = gi(g2, "nx_top.nx" as *u8) 43 eg_add_edge(g2, top2, a2); eg_add_edge(g2, a2, god2); eg_add_edge(g2, god2, top2) // top->a->god->top = cycle 44 eg_finalize(g2) 45 var t3: i64 = 0 46 if eg_has_cycle(g2) == 1 { t3 = 1 } 47 if t3 == 1 { hw("T3 PASS neg-control: an injected import cycle (tangle) IS detected -> detector is load-bearing\n" as *u8) } else { fails=fails+1; hw("T3 FAIL cycle not detected\n" as *u8) } 48 49 if fails == 0 { hw("NX-ECO-ORTHO GREEN -- cycle(tangle) detection + coupling stats correct (orthogonality metric #1; SCC/redundancy-clusters/I-A-D layer next)\n" as *u8); sys_exit(0); return 0 } 50 hw("NX-ECO-ORTHO RED fails=" as *u8); pn(fails); hw("\n" as *u8) 51 sys_exit(1); return 1 52}