code wiki / _hdl_build / nx_ncf_synth_gate.nx
nx_ncf_synth_gate.nx source
↩ module page · 227 lines · 11738 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_ncf_synth_gate.nx -- X-AUT-NCF-001 FIRST RUNG: SYNTHESIZE NOVEL CONTROL FLOW (a conditional branch),
4// NOT re-emit/lift-constants. The keystone X-AUT-NCF-001 was: "emitter-of-emitters that authors NEW control
5// flow ... transducer v0/v1 only re-emit verbatim / lift constants, author-share=0". This rung moves
6// author-share>0 by SEARCHING a grammar that INCLUDES branches, fitting a spec (input->output examples), and
7// proving the branch is NECESSARY via a negative control: a LINEAR-ONLY search PROVABLY FAILS on the same
8// spec (no op-list = a piecewise function). Then it MATERIALIZES the synthesized branch as a real .nx organ,
9// GOD-BUILDS it (nx_cc->nxasm, NO gcc) and runs it on HELD-OUT inputs to verify correctness. Builds ON the
10// god-cycle materialize->god-build->verify mechanism (composes nx_god_cycle_gate's pattern, does NOT rebuild
11// the self-scaffolder). HONEST SCOPE: synthesizes ONE control-flow form (a single conditional) from a fixed
12// grammar; loops/nested/multi-arg are further rungs. RACI: CAP-AUT-NCF-KEYSTONE (BUILDER), audited GREEN.
13// expect_exit: 0 license_tier: ORIGINAL
14import "nx_syscalls.nx"
15
16const NCF_ORGAN: *u8 = "runtime/_hdl_build/ncf_organ.nx"
17const NCF_NAME: *u8 = "ncf_organ"
18const NCF_RESULT: *u8 = "knowledge/status/ncf_out.bin"
19const NCF_RUNNER: *u8 = "_offc/nx_sov_build_run.elf"
20
21func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
22" as *u8); return ok }
23func 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 }
24func 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 }
25
26// ---- the grammar: linear ops (0=id 1=neg 2=+k 3=*k 4=*0) over an integer x ----
27func op_eval(op: i64, arg: i64, x: i64) -> i64 {
28 if op==0 { return x }
29 if op==1 { return 0 - x }
30 if op==2 { return x + arg }
31 if op==3 { return x * arg }
32 if op==4 { return x * 0 }
33 return x
34}
35// predicates: 0 = (x<0) 1 = (x<k) 2 = (x>k)
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}
42
43// LINEAR-only synthesis: try every length-1 op and length-2 composition; return 1 if ANY fits all examples.
44func search_linear(xs: *i64, ys: *i64, n: i64) -> i64 {
45 var op1: i64=0
46 while op1<5 {
47 var a1: i64=0-4
48 while a1<=4 {
49 var ok: i64=1; var i: i64=0
50 while i<n { if op_eval(op1,a1,xs[i])!=ys[i] { ok=0; i=n } else { i=i+1 } }
51 if ok==1 { return 1 }
52 a1=a1+1
53 }
54 op1=op1+1
55 }
56 var p: i64=0
57 while p<5 {
58 var qq: i64=0
59 while qq<5 {
60 var a: i64=0-3
61 while a<=3 {
62 var b: i64=0-3
63 while b<=3 {
64 var ok2: i64=1; var j: i64=0
65 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 } }
66 if ok2==1 { return 1 }
67 b=b+1
68 }
69 a=a+1
70 }
71 qq=qq+1
72 }
73 p=p+1
74 }
75 return 0
76}
77
78// BRANCH synthesis: ite(pred, then_op, else_op); fill form=[pred,k,then_op,then_arg,else_op,else_arg]; 1 if found.
79func search_branch(xs: *i64, ys: *i64, n: i64, form: *i64) -> i64 {
80 var pred: i64=0
81 while pred<3 {
82 var k: i64=0-3
83 while k<=3 {
84 var top: i64=0
85 while top<5 {
86 var ta: i64=0-3
87 while ta<=3 {
88 var eop: i64=0
89 while eop<5 {
90 var ea: i64=0-3
91 while ea<=3 {
92 var ok: i64=1; var i: i64=0
93 while i<n {
94 var r: i64=0
95 if pred_eval(pred,k,xs[i])==1 { r=op_eval(top,ta,xs[i]) } else { r=op_eval(eop,ea,xs[i]) }
96 if r!=ys[i] { ok=0; i=n } else { i=i+1 }
97 }
98 if ok==1 { form[0]=pred; form[1]=k; form[2]=top; form[3]=ta; form[4]=eop; form[5]=ea; return 1 }
99 ea=ea+1
100 }
101 eop=eop+1
102 }
103 ta=ta+1
104 }
105 top=top+1
106 }
107 k=k+1
108 }
109 pred=pred+1
110 }
111 return 0
112}
113
114// emit a `return <expr>` line for a single op acting on x, at the given indent
115func cat_ret(buf: *u8, off: i64, op: i64, arg: i64, ind: *u8) -> i64 {
116 var o: i64=off
117 o=gcat(buf,o,ind); o=gcat(buf,o,"return " as *u8)
118 if op==0 { o=gcat(buf,o,"x" as *u8) }
119 if op==1 { o=gcat(buf,o,"0 - x" as *u8) }
120 if op==2 { o=gcat(buf,o,"x + " as *u8); o=gcatn(buf,o,arg) }
121 if op==3 { o=gcat(buf,o,"x * " as *u8); o=gcatn(buf,o,arg) }
122 if op==4 { o=gcat(buf,o,"x * 0" as *u8) }
123 o=gcat(buf,o,"\n" as *u8)
124 return o
125}
126func cat_pred(buf: *u8, off: i64, pred: i64, k: i64) -> i64 {
127 var o: i64=off
128 if pred==0 { o=gcat(buf,o,"x < 0" as *u8) }
129 if pred==1 { o=gcat(buf,o,"x < " as *u8); o=gcatn(buf,o,k) }
130 if pred==2 { o=gcat(buf,o,"x > " as *u8); o=gcatn(buf,o,k) }
131 return o
132}
133// MATERIALIZE the synthesized branch as a real organ that tests f on HELD-OUT inputs {-7->7, 9->9}.
134func emit_branch(form: *i64) -> i64 {
135 let buf: *u8=sys_mmap(8192); var o: i64=0
136 o=gcat(buf,o,"// SYNTHESIZED CONTROL FLOW (a branch) + GOD-BUILT by nx_ncf_synth_gate. license_tier: ORIGINAL\n" as *u8)
137 o=gcat(buf,o,"import \"nx_syscalls.nx\"\nfunc f(x: i64) -> i64 {\n if " as *u8)
138 o=cat_pred(buf,o,form[0],form[1])
139 o=gcat(buf,o," {\n" as *u8)
140 o=cat_ret(buf,o,form[2],form[3]," " as *u8)
141 o=gcat(buf,o," }\n" as *u8)
142 o=cat_ret(buf,o,form[4],form[5]," " as *u8)
143 o=gcat(buf,o,"}\n" as *u8)
144 o=gcat(buf,o,"func main() -> i64 {\n let p: *i64 = sys_mmap(8) as *i64\n p[0] = (f(0 - 7) * 1000000) + f(9)\n let fd: i64 = sys_openat_wr(\"knowledge/status/ncf_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)
145 let fd: i64=sys_openat_wr(NCF_ORGAN,420)
146 if fd<0 { return 0-1 }
147 sys_write(fd,buf,o); sys_close(fd)
148 return 0
149}
150// GOD-BUILD: fork/exec the god-rooted sovereign runner to compile+run the synthesized organ.
151func god_build(name: *u8) -> i64 {
152 let pid: i64=sys_fork()
153 if pid==0 {
154 let dn: i64=sys_openat_wr("/dev/null\x00" as *u8,420)
155 if dn>=0 { sys_dup3(dn,1,0); sys_dup3(dn,2,0) }
156 let argv: *i64=sys_mmap(64) as *i64
157 argv[0]=NCF_RUNNER as i64; argv[1]=name as i64; argv[2]=0
158 let envp: *i64=sys_mmap(16) as *i64
159 envp[0]="PATH=/usr/bin:/bin" as *u8 as i64; envp[1]=0
160 sys_execve(NCF_RUNNER,argv,envp)
161 sys_exit(127)
162 }
163 let st: *i64=sys_mmap(16) as *i64
164 sys_wait4(pid,st,0)
165 return (st[0]>>8)&0xff
166}
167func read_i64(path: *u8) -> i64 {
168 let lenp: *i64=sys_mmap(16) as *i64
169 let buf: *u8=sys_read_file(path,lenp)
170 if (buf as i64)==0 { return 0-999999 }
171 if lenp[0]<8 { return 0-888888 }
172 let p: *i64=buf as *i64
173 return p[0]
174}
175
176func main() -> i64 {
177 gw("=== nx_ncf_synth: SYNTHESIZE NOVEL CONTROL FLOW (X-AUT-NCF-001 first rung) -- a branch linear synthesis CANNOT ===\n" as *u8)
178 var pass: i64=0; var total: i64=0
179
180 // SPEC A = abs(x): piecewise -> REQUIRES a branch. examples + HELD-OUT {-7->7, 9->9}.
181 let xs: *i64=sys_mmap(64) as *i64; let ys: *i64=sys_mmap(64) as *i64
182 xs[0]=0-3; xs[1]=0-1; xs[2]=0; xs[3]=2; xs[4]=5
183 ys[0]=3; ys[1]=1; ys[2]=0; ys[3]=2; ys[4]=5
184 let n: i64=5
185
186 let sp: *i64=sys_mmap(8) as *i64; sp[0]=0-1
187 let sfd: i64=sys_openat_wr(NCF_RESULT,420); if sfd>=0 { sys_write(sfd,sp as *u8,8); sys_close(sfd) }
188
189 // T1 NEG-CONTROL: linear-only synthesis FAILS (the branch is NECESSARY, not decorative).
190 let lin: i64=search_linear(xs,ys,n)
191 total=total+1; if lin==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
192 gw("T1 NEG-CONTROL: linear-only synthesis FAILS on abs (no op-list = piecewise) -> branch NECESSARY, lin_found=" as *u8); gn(lin); gw("\n" as *u8)
193
194 // T2 SYNTHESIS: the branch search authors the control flow.
195 let form: *i64=sys_mmap(64) as *i64
196 let br: i64=search_branch(xs,ys,n,form)
197 total=total+1; if br==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
198 gw("T2 SYNTHESIS: branch SEARCHED+FOUND ite(pred-form=" as *u8); gn(form[0]); gw(", then_op=" as *u8); gn(form[2]); gw(", else_op=" as *u8); gn(form[4]); gw(") for abs\n" as *u8)
199
200 // T3 MATERIALIZE -> GOD-BUILD -> run HELD-OUT -> verify (abs(-7)=7, abs(9)=9 -> 7000009).
201 var emitted: i64=0; if br==1 { if emit_branch(form)==0 { emitted=1 } }
202 var built: i64=0; if emitted==1 { if god_build(NCF_NAME)==0 { built=1 } }
203 let produced: i64=read_i64(NCF_RESULT)
204 total=total+1; if produced==7000009 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
205 gw("T3 MATERIALIZE+GOD-BUILD: synthesized branch compiled (nx_cc->nxasm, no gcc) + ran HELD-OUT -> produced=" as *u8); gn(produced); gw(" (expect 7000009)\n" as *u8)
206
207 // T4 AUTHOR-SHARE>0: linear PROVABLY can't (T1) yet the organ synthesized+materialized control flow that RUNS.
208 var ashare: i64=0; if lin==0 { if br==1 { if produced==7000009 { ashare=1 } } }
209 total=total+1; if ashare==1 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
210 gw("T4 AUTHOR-SHARE>0: organ SYNTHESIZED + MATERIALIZED novel control flow (a branch), not re-emit/lift-constants\n" as *u8)
211
212 // T5 GENERALITY: a 2nd branch-requiring spec g(x)=if x>2 then x*2 else x -> different branch, linear still fails.
213 let xs2: *i64=sys_mmap(64) as *i64; let ys2: *i64=sys_mmap(64) as *i64
214 xs2[0]=0; xs2[1]=1; xs2[2]=2; xs2[3]=3; xs2[4]=5
215 ys2[0]=0; ys2[1]=1; ys2[2]=2; ys2[3]=6; ys2[4]=10
216 let lin2: i64=search_linear(xs2,ys2,n)
217 let form2: *i64=sys_mmap(64) as *i64
218 let br2: i64=search_branch(xs2,ys2,n,form2)
219 var diff: i64=0; if form2[0]!=form[0] { diff=1 }
220 total=total+1; if lin2==0 { if br2==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) }
221 gw("T5 GENERALITY: 2nd spec needing a branch -> linear fails(" as *u8); gn(lin2); gw("), branch synthesized(" as *u8); gn(br2); gw(") with DIFFERENT pred-form=" as *u8); gn(form2[0]); gw(" vs abs=" as *u8); gn(form[0]); gw("\n" as *u8)
222
223 gw("\n HONEST SCOPE: synthesizes ONE control-flow form (a single conditional) from a fixed grammar; loops/nested/multi-arg branches are the next X-AUT-NCF-001 rungs. The GRAMMAR is the substrate; the PROGRAM (which branch) is organ-searched, verified, and materialized -- author-share for the control flow is the organ's.\n" as *u8)
224 gw("NCF-SYNTH verdict=" as *u8)
225 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- the team SYNTHESIZED + GOD-BUILT novel control flow (author-share>0): a real first rung of keystone X-AUT-NCF-001\n" as *u8); sys_exit(0); return 0 }
226 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
227}