code wiki / _hdl_build / nx_ncf_loop_gate.nx

nx_ncf_loop_gate.nx source

↩ module page · 244 lines · 11794 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_ncf_loop_gate.nx -- X-AUT-NCF-001 RUNG 2: SYNTHESIZE A BOUNDED LOOP (iteration) -- a strictly stronger 4// control-flow construct than rung 1's conditional. Escalation discipline: rung 1 proved branch>linear; this 5// proves LOOP>(branch AND linear). Target 2^x is EXPONENTIAL, so the negative controls are decisive: 6// T1 linear-only synthesis PROVABLY FAILS (no op-list = exponential) 7// T2 single-conditional (rung-1 branch) PROVABLY FAILS (2 affine regions cannot fit 5 exponential points) 8// -> only a LOOP fits. T3 the organ SEARCHES a bounded-loop grammar + finds acc=1; repeat x: acc*=2. 9// T4 MATERIALIZE the synthesized loop -> GOD-BUILD (nx_cc->nxasm, no gcc) -> run HELD-OUT {5->32,6->64}. 10// T5 GENERALITY: a 2nd loop spec (sum 1..x) synthesizes a DIFFERENT loop body (uses the index i). 11// Builds ON the god-cycle materialize->god-build->verify mechanism. NOTE: search_linear/search_branch are 12// re-included as the gate's self-contained negative controls (ecosystem gate style); the clean DRY follow-up 13// is to extract nx_ncf_synth.nx (shared grammar+searchers+materialize) for all rungs to import. 14// expect_exit: 0 license_tier: ORIGINAL 15import "nx_syscalls.nx" 16 17const NCL_ORGAN: *u8 = "runtime/_hdl_build/ncf_loop_organ.nx" 18const NCL_NAME: *u8 = "ncf_loop_organ" 19const NCL_RESULT: *u8 = "knowledge/status/ncf_loop_out.bin" 20const NCL_RUNNER: *u8 = "_offc/nx_sov_build_run.elf" 21 22func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 23" as *u8); return ok } 24func gcat(buf: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){ buf[o]=s[i]; o=o+1; i=i+1 } return o } 25func gcatn(buf: *u8, off: i64, v: i64) -> i64 { var o: i64=off; if v==0 { buf[o]=48 as u8; return o+1 } var m: i64=v; if m<0 { buf[o]=45 as u8; o=o+1; m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var i: i64=0; while i<k { buf[o]=t[k-1-i]; o=o+1; i=i+1 } return o } 26 27// ---- negative-control grammar (rung 1): affine ops + a single conditional ---- 28func op_eval(op: i64, arg: i64, x: i64) -> i64 { 29 if op==0 { return x } 30 if op==1 { return 0 - x } 31 if op==2 { return x + arg } 32 if op==3 { return x * arg } 33 if op==4 { return x * 0 } 34 return x 35} 36func pred_eval(pred: i64, k: i64, x: i64) -> i64 { 37 if pred==0 { if x<0 { return 1 } return 0 } 38 if pred==1 { if x<k { return 1 } return 0 } 39 if pred==2 { if x>k { return 1 } return 0 } 40 return 0 41} 42func search_linear(xs: *i64, ys: *i64, n: i64) -> i64 { 43 var op1: i64=0 44 while op1<5 { 45 var a1: i64=0-4 46 while a1<=4 { 47 var ok: i64=1; var i: i64=0 48 while i<n { if op_eval(op1,a1,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 49 if ok==1 { return 1 } 50 a1=a1+1 51 } 52 op1=op1+1 53 } 54 var p: i64=0 55 while p<5 { 56 var qq: i64=0 57 while qq<5 { 58 var a: i64=0-3 59 while a<=3 { 60 var b: i64=0-3 61 while b<=3 { 62 var ok2: i64=1; var j: i64=0 63 while j<n { let r1: i64=op_eval(p,a,xs[j]); if op_eval(qq,b,r1)!=ys[j] { ok2=0; j=n } else { j=j+1 } } 64 if ok2==1 { return 1 } 65 b=b+1 66 } 67 a=a+1 68 } 69 qq=qq+1 70 } 71 p=p+1 72 } 73 return 0 74} 75func search_branch(xs: *i64, ys: *i64, n: i64) -> i64 { 76 var pred: i64=0 77 while pred<3 { 78 var k: i64=0-3 79 while k<=3 { 80 var top: i64=0 81 while top<5 { 82 var ta: i64=0-3 83 while ta<=3 { 84 var eop: i64=0 85 while eop<5 { 86 var ea: i64=0-3 87 while ea<=3 { 88 var ok: i64=1; var i: i64=0 89 while i<n { 90 var r: i64=0 91 if pred_eval(pred,k,xs[i])==1 { r=op_eval(top,ta,xs[i]) } else { r=op_eval(eop,ea,xs[i]) } 92 if r!=ys[i] { ok=0; i=n } else { i=i+1 } 93 } 94 if ok==1 { return 1 } 95 ea=ea+1 96 } 97 eop=eop+1 98 } 99 ta=ta+1 100 } 101 top=top+1 102 } 103 k=k+1 104 } 105 pred=pred+1 106 } 107 return 0 108} 109 110// ---- the NEW construct: a bounded loop acc=init; for i in 1..=x { acc = body(acc,i,c) } ---- 111// body ops: 0 = acc+c 1 = acc*c 2 = acc+i 3 = acc*i 112func loop_eval(init: i64, bop: i64, c: i64, x: i64) -> i64 { 113 var acc: i64=init 114 var i: i64=1 115 while i<=x { 116 if bop==0 { acc=acc+c } 117 if bop==1 { acc=acc*c } 118 if bop==2 { acc=acc+i } 119 if bop==3 { acc=acc*i } 120 i=i+1 121 } 122 return acc 123} 124// search the loop grammar; fill form=[init,bop,c]; return 1 if a loop fits all examples. 125func search_loop(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 { 126 var init: i64=0 127 while init<3 { 128 var bop: i64=0 129 while bop<4 { 130 var c: i64=0-2 131 while c<=3 { 132 var ok: i64=1; var i: i64=0 133 while i<n { if loop_eval(init,bop,c,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } } 134 if ok==1 { form[0]=init; form[1]=bop; form[2]=c; return 1 } 135 c=c+1 136 } 137 bop=bop+1 138 } 139 init=init+1 140 } 141 return 0 142} 143 144func cat_body(buf: *u8, off: i64, bop: i64, c: i64) -> i64 { 145 var o: i64=off 146 if bop==0 { o=gcat(buf,o," acc = acc + " as *u8); o=gcatn(buf,o,c) } 147 if bop==1 { o=gcat(buf,o," acc = acc * " as *u8); o=gcatn(buf,o,c) } 148 if bop==2 { o=gcat(buf,o," acc = acc + i" as *u8) } 149 if bop==3 { o=gcat(buf,o," acc = acc * i" as *u8) } 150 o=gcat(buf,o,"\n" as *u8) 151 return o 152} 153// MATERIALIZE the synthesized loop as a real organ testing HELD-OUT {5->32, 6->64} -> (f(5)*1000)+f(6). 154func emit_loop(form: *i64) -> i64 { 155 let buf: *u8=sys_mmap(8192); var o: i64=0 156 o=gcat(buf,o,"// SYNTHESIZED CONTROL FLOW (a bounded loop) + GOD-BUILT by nx_ncf_loop_gate. license_tier: ORIGINAL\n" as *u8) 157 o=gcat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n var acc: i64 = " as *u8) 158 o=gcatn(buf,o,form[0]) 159 o=gcat(buf,o,"\n var i: i64 = 1\n while i <= x {\n" as *u8) 160 o=cat_body(buf,o,form[1],form[2]) 161 o=gcat(buf,o," i = i + 1\n }\n return acc\n}\n" as *u8) 162 o=gcat(buf,o,"func main() -> i64 {\n let p: *i64 = sys_mmap(8) as *i64\n p[0] = (f(5) * 1000) + f(6)\n let fd: i64 = sys_openat_wr(\"knowledge/status/ncf_loop_out.bin\" as *u8, 420)\n if fd >= 0 { sys_write(fd, p as *u8, 8); sys_close(fd) }\n return 0\n}\n" as *u8) 163 let fd: i64=sys_openat_wr(NCL_ORGAN,420) 164 if fd<0 { return 0-1 } 165 sys_write(fd,buf,o); sys_close(fd) 166 return 0 167} 168func god_build(name: *u8) -> i64 { 169 let pid: i64=sys_fork() 170 if pid==0 { 171 let dn: i64=sys_openat_wr("/dev/null\x00" as *u8,420) 172 if dn>=0 { sys_dup3(dn,1,0); sys_dup3(dn,2,0) } 173 let argv: *i64=sys_mmap(64) as *i64 174 argv[0]=NCL_RUNNER as i64; argv[1]=name as i64; argv[2]=0 175 let envp: *i64=sys_mmap(16) as *i64 176 envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0 177 sys_execve(NCL_RUNNER,argv,envp) 178 sys_exit(127) 179 } 180 let st: *i64=sys_mmap(16) as *i64 181 sys_wait4(pid,st,0) 182 return (st[0]>>8)&0xff 183} 184func read_i64(path: *u8) -> i64 { 185 let lenp: *i64=sys_mmap(16) as *i64 186 let buf: *u8=sys_read_file(path,lenp) 187 if (buf as i64)==0 { return 0-999999 } 188 if lenp[0]<8 { return 0-888888 } 189 let p: *i64=buf as *i64 190 return p[0] 191} 192 193func main() -> i64 { 194 gw("=== nx_ncf_loop: SYNTHESIZE A BOUNDED LOOP (X-AUT-NCF-001 rung 2) -- LOOP > (branch AND linear) ===\n" as *u8) 195 var pass: i64=0; var total: i64=0 196 197 // SPEC = 2^x: exponential -> needs a LOOP. examples {0..4}, HELD-OUT {5->32, 6->64}. 198 let xs: *i64=sys_mmap(64) as *i64; let ys: *i64=sys_mmap(64) as *i64 199 xs[0]=0; xs[1]=1; xs[2]=2; xs[3]=3; xs[4]=4 200 ys[0]=1; ys[1]=2; ys[2]=4; ys[3]=8; ys[4]=16 201 let n: i64=5 202 203 let sp: *i64=sys_mmap(8) as *i64; sp[0]=0-1 204 let sfd: i64=sys_openat_wr(NCL_RESULT,420); if sfd>=0 { sys_write(sfd,sp as *u8,8); sys_close(sfd) } 205 206 // T1 NEG-CONTROL A: linear-only FAILS (exponential is not affine). 207 let lin: i64=search_linear(xs,ys,n) 208 total=total+1; if lin==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 209 gw("T1 NEG-CONTROL: linear-only synthesis FAILS on 2^x, lin_found=" as *u8); gn(lin); gw("\n" as *u8) 210 211 // T2 NEG-CONTROL B (the key escalation): a single CONDITIONAL also FAILS -> a LOOP is necessary. 212 let br: i64=search_branch(xs,ys,n) 213 total=total+1; if br==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 214 gw("T2 NEG-CONTROL: single-conditional (rung 1) FAILS on 2^x (2 affine regions != exponential) -> LOOP necessary, branch_found=" as *u8); gn(br); gw("\n" as *u8) 215 216 // T3 SYNTHESIS: the organ searches the bounded-loop grammar + authors the loop. 217 let form: *i64=sys_mmap(64) as *i64 218 let lp: i64=search_loop(xs,ys,n,form) 219 total=total+1; if lp==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 220 gw("T3 SYNTHESIS: loop SEARCHED+FOUND acc=" as *u8); gn(form[0]); gw("; repeat x { body=" as *u8); gn(form[1]); gw(" c=" as *u8); gn(form[2]); gw(" } for 2^x\n" as *u8) 221 222 // T4 MATERIALIZE -> GOD-BUILD -> run HELD-OUT -> verify (2^5=32, 2^6=64 -> 32064). 223 var emitted: i64=0; if lp==1 { if emit_loop(form)==0 { emitted=1 } } 224 var built: i64=0; if emitted==1 { if god_build(NCL_NAME)==0 { built=1 } } 225 let produced: i64=read_i64(NCL_RESULT) 226 total=total+1; if produced==32064 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 227 gw("T4 MATERIALIZE+GOD-BUILD: synthesized loop compiled (nx_cc->nxasm, no gcc) + ran HELD-OUT -> produced=" as *u8); gn(produced); gw(" (expect 32064 = 2^5*1000+2^6)\n" as *u8) 228 229 // T5 GENERALITY: sum 1..x needs a DIFFERENT loop body (uses index i); linear still fails. 230 let xs2: *i64=sys_mmap(64) as *i64; let ys2: *i64=sys_mmap(64) as *i64 231 xs2[0]=0; xs2[1]=1; xs2[2]=2; xs2[3]=3; xs2[4]=4 232 ys2[0]=0; ys2[1]=1; ys2[2]=3; ys2[3]=6; ys2[4]=10 233 let lin2: i64=search_linear(xs2,ys2,n) 234 let form2: *i64=sys_mmap(64) as *i64 235 let lp2: i64=search_loop(xs2,ys2,n,form2) 236 var diff: i64=0; if form2[1]!=form[1] { diff=1 } 237 total=total+1; if lin2==0 { if lp2==1 { if diff==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 238 gw("T5 GENERALITY: sum(1..x) -> linear fails(" as *u8); gn(lin2); gw("), loop synthesized(" as *u8); gn(lp2); gw(") with DIFFERENT body=" as *u8); gn(form2[1]); gw(" (uses index i) vs 2^x body=" as *u8); gn(form[1]); gw("\n" as *u8) 239 240 gw("\n HONEST SCOPE: synthesizes a single bounded loop (one accumulator, index-or-const body) from a fixed grammar; nested loops + loop+branch composition + multi-arg are further rungs. Construct ladder now: linear < conditional < BOUNDED LOOP, each with its own proving negative control.\n" as *u8) 241 gw("NCF-LOOP verdict=" as *u8) 242 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- the team SYNTHESIZED + GOD-BUILT a bounded loop (author-share>0): X-AUT-NCF-001 rung 2\n" as *u8); sys_exit(0); return 0 } 243 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1 244}