code wiki / _hdl_build / _type_search_authored.nx
_type_search_authored.nx source
↩ module page · 351 lines · 14958 B
1// AUTHORED BY THE NISHI BUILDER (nx_module_author type-search template) -- S3 rung-1 synthesizer.
2// Obs-equiv bottom-up search; tutoring: specs/2026-06-09-tutoring-code-synthesis-rung1.md PART B.
3import "nx_syscalls.nx"
4func _ts_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
5func _ts_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
6func _ts_fputs(fd2: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd2,s,n); return 0 }
7func _ts_fnum(fd2: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd2,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd2,bb,k); return 0 }
8func _ts_fsig(fd2: i64, v: i64) -> i64 { if v < 0 { _ts_fputs(fd2, "0 - " as *u8); _ts_fnum(fd2, 0 - v) } else { _ts_fnum(fd2, v) } return 0 }
9const TS_K: i64 = 8
10const TS_NCOMP: i64 = 12
11const TS_MAXTERM: i64 = 30000
12const TS_MAXLVL: i64 = 4
13const TS_HCAP: i64 = 131072
14const TS_BUDGET: i64 = 8000000
15func ts_arity(c: i64) -> i64 { if c == 12 { return 1 } return 2 }
16func ts_comm(c: i64) -> i64 { if c == 2 { return 1 } if c == 4 { return 1 } if c == 5 { return 1 } if c == 6 { return 1 } if c == 8 { return 1 } return 0 }
17func ts_apply(c: i64, a: i64, b: i64) -> i64 {
18 if c == 1 { if a <= b { return 1 } return 0 }
19 if c == 2 { return a & b }
20 if c == 3 { return a >> (b & 63) }
21 if c == 4 { if a < b { return a } return b }
22 if c == 5 { if a > b { return a } return b }
23 if c == 6 { return a + b }
24 if c == 7 { return a - b }
25 if c == 8 { return a * b }
26 if c == 9 { return a << (b & 63) }
27 if c == 10 { if b == 0 { return 0 } return a / b }
28 if c == 11 { if b == 0 { return 0 } return a % b }
29 if c == 12 { if a < 0 { return 0 - a } return a }
30 return 0
31}
32func ts_vech(vecs: *i64, ix: i64) -> i64 {
33 var h: i64 = 1469
34 var k: i64 = 0
35 while k < TS_K { h = h * 131 + vecs[ix*TS_K+k]; k = k + 1 }
36 if h < 0 { h = 0 - h }
37 return h
38}
39func ts_vec_eq(vecs: *i64, x: i64, y: i64) -> i64 {
40 var k: i64 = 0
41 while k < TS_K { if vecs[x*TS_K+k] != vecs[y*TS_K+k] { return 0 } k = k + 1 }
42 return 1
43}
44func ts_insert(vecs: *i64, hash: *i64, nterm: i64) -> i64 {
45 let h0: i64 = ts_vech(vecs, nterm) & (TS_HCAP - 1)
46 var s: i64 = h0
47 var go: i64 = 1
48 while go == 1 {
49 if hash[s] == 0 { hash[s] = nterm + 1; return 1 }
50 if ts_vec_eq(vecs, hash[s] - 1, nterm) == 1 { return 0 }
51 s = s + 1
52 if s >= TS_HCAP { s = 0 }
53 if s == h0 { return 0 }
54 }
55 return 0
56}
57func ts_is_target(vecs: *i64, ix: i64, kx: *i64) -> i64 {
58 var k: i64 = 0
59 while k < TS_K { if vecs[ix*TS_K+k] != kx[k] { return 0 } k = k + 1 }
60 return 1
61}
62func ts_search(ka: *i64, kb: *i64, kx: *i64, cons: *i64, ncon: i64, terms: *i64, vecs: *i64, hash: *i64, stats: *i64) -> i64 {
63 var s: i64 = 0
64 while s < TS_HCAP { hash[s] = 0; s = s + 1 }
65 var nt: i64 = 0
66 var cand: i64 = 0
67 var k: i64 = 0
68 k = 0
69 while k < TS_K { vecs[nt*TS_K+k] = ka[k]; k = k + 1 }
70 terms[nt*5+0] = 0
71 terms[nt*5+1] = 0
72 terms[nt*5+2] = 0
73 terms[nt*5+3] = 0
74 terms[nt*5+4] = 1
75 if ts_insert(vecs, hash, nt) == 1 { if ts_is_target(vecs, nt, kx) == 1 { stats[0]=cand; stats[1]=nt+1; stats[2]=1; return nt } nt = nt + 1 }
76 k = 0
77 while k < TS_K { vecs[nt*TS_K+k] = kb[k]; k = k + 1 }
78 terms[nt*5+0] = 0
79 terms[nt*5+1] = 1
80 terms[nt*5+2] = 0
81 terms[nt*5+3] = 0
82 terms[nt*5+4] = 1
83 if ts_insert(vecs, hash, nt) == 1 { if ts_is_target(vecs, nt, kx) == 1 { stats[0]=cand; stats[1]=nt+1; stats[2]=1; return nt } nt = nt + 1 }
84 var ci: i64 = 0
85 while ci < ncon {
86 k = 0
87 while k < TS_K { vecs[nt*TS_K+k] = cons[ci]; k = k + 1 }
88 terms[nt*5+0] = 1
89 terms[nt*5+1] = cons[ci]
90 terms[nt*5+2] = 0
91 terms[nt*5+3] = 0
92 terms[nt*5+4] = 1
93 if ts_insert(vecs, hash, nt) == 1 { if ts_is_target(vecs, nt, kx) == 1 { stats[0]=cand; stats[1]=nt+1; stats[2]=1; return nt } nt = nt + 1 }
94 ci = ci + 1
95 }
96 var lvl: i64 = 2
97 while lvl <= TS_MAXLVL {
98 let n0: i64 = nt
99 var c: i64 = 1
100 while c <= TS_NCOMP {
101 var x: i64 = 0
102 while x < n0 {
103 if ts_arity(c) == 1 {
104 if terms[x*5+4] == lvl - 1 {
105 if nt >= TS_MAXTERM { stats[0]=cand; stats[1]=nt; stats[2]=lvl; stats[3]=1; return 0 - 1 }
106 if cand > TS_BUDGET { stats[0]=cand; stats[1]=nt; stats[2]=lvl; stats[3]=1; return 0 - 1 }
107 cand = cand + 1
108 k = 0
109 while k < TS_K { vecs[nt*TS_K+k] = ts_apply(c, vecs[x*TS_K+k], 0); k = k + 1 }
110 if ts_insert(vecs, hash, nt) == 1 {
111 terms[nt*5+0] = 2
112 terms[nt*5+1] = c
113 terms[nt*5+2] = x
114 terms[nt*5+3] = 0
115 terms[nt*5+4] = lvl
116 if ts_is_target(vecs, nt, kx) == 1 { stats[0]=cand; stats[1]=nt+1; stats[2]=lvl; return nt }
117 nt = nt + 1
118 }
119 }
120 }
121 else {
122 var y: i64 = 0
123 while y < n0 {
124 var mx: i64 = terms[x*5+4]
125 if terms[y*5+4] > mx { mx = terms[y*5+4] }
126 if terms[x*5+0] == 1 { if terms[y*5+0] == 1 { mx = 0 - 1 } }
127 if ts_comm(c) == 1 { if x > y { mx = 0 - 1 } }
128 if mx == lvl - 1 {
129 if nt >= TS_MAXTERM { stats[0]=cand; stats[1]=nt; stats[2]=lvl; stats[3]=1; return 0 - 1 }
130 if cand > TS_BUDGET { stats[0]=cand; stats[1]=nt; stats[2]=lvl; stats[3]=1; return 0 - 1 }
131 cand = cand + 1
132 k = 0
133 while k < TS_K { vecs[nt*TS_K+k] = ts_apply(c, vecs[x*TS_K+k], vecs[y*TS_K+k]); k = k + 1 }
134 if ts_insert(vecs, hash, nt) == 1 {
135 terms[nt*5+0] = 2
136 terms[nt*5+1] = c
137 terms[nt*5+2] = x
138 terms[nt*5+3] = y
139 terms[nt*5+4] = lvl
140 if ts_is_target(vecs, nt, kx) == 1 { stats[0]=cand; stats[1]=nt+1; stats[2]=lvl; return nt }
141 nt = nt + 1
142 }
143 }
144 y = y + 1
145 }
146 }
147 x = x + 1
148 }
149 c = c + 1
150 }
151 lvl = lvl + 1
152 }
153 stats[0]=cand
154 stats[1]=nt
155 stats[2]=TS_MAXLVL
156 stats[3]=0
157 return 0 - 1
158}
159func ts_comp_name(c: i64) -> *u8 {
160 if c == 1 { return "_c_le" as *u8 }
161 if c == 2 { return "_c_and" as *u8 }
162 if c == 3 { return "_c_shr" as *u8 }
163 if c == 4 { return "_c_min" as *u8 }
164 if c == 5 { return "_c_max" as *u8 }
165 if c == 6 { return "_c_add" as *u8 }
166 if c == 7 { return "_c_sub" as *u8 }
167 if c == 8 { return "_c_mul" as *u8 }
168 if c == 9 { return "_c_shl" as *u8 }
169 if c == 10 { return "_c_div" as *u8 }
170 if c == 11 { return "_c_mod" as *u8 }
171 return "_c_abs" as *u8
172}
173func ts_emit_term(ofd: i64, terms: *i64, ix: i64) -> i64 {
174 let kind: i64 = terms[ix*5+0]
175 if kind == 0 { if terms[ix*5+1] == 0 { _ts_fputs(ofd, "a" as *u8) } else { _ts_fputs(ofd, "b" as *u8) } return 0 }
176 if kind == 1 { _ts_fsig(ofd, terms[ix*5+1]); return 0 }
177 _ts_fputs(ofd, ts_comp_name(terms[ix*5+1])); _ts_fputs(ofd, "(" as *u8)
178 ts_emit_term(ofd, terms, terms[ix*5+2])
179 if ts_arity(terms[ix*5+1]) == 2 { _ts_fputs(ofd, ", " as *u8); ts_emit_term(ofd, terms, terms[ix*5+3]) }
180 _ts_fputs(ofd, ")" as *u8)
181 return 0
182}
183func ts_emit_module(path: *u8, terms: *i64, ix: i64, ka: *i64, kb: *i64, kx: *i64) -> i64 {
184 let ofd: i64 = sys_openat_wr(path, 0x1a4)
185 if ofd < 0 { return 0 - 1 }
186 _ts_fputs(ofd, "// RE-SYNTHESIZED BY THE NISHI TEAM (_type_search_authored): derived from KATs by\n" as *u8)
187 _ts_fputs(ofd, "// observational-equivalence search over a primitive pool. NOT retrieved -- the pool\n" as *u8)
188 _ts_fputs(ofd, "// holds no team function bodies. Silent main: exit 0 iff every KAT passes.\n" as *u8)
189 _ts_fputs(ofd, "import \"nx_syscalls.nx\"\n" as *u8)
190 _ts_fputs(ofd, "func _c_le(a: i64, b: i64) -> i64 { if a <= b { return 1 } return 0 }\n" as *u8)
191 _ts_fputs(ofd, "func _c_and(a: i64, b: i64) -> i64 { return a & b }\n" as *u8)
192 _ts_fputs(ofd, "func _c_shr(a: i64, b: i64) -> i64 { return a >> (b & 63) }\n" as *u8)
193 _ts_fputs(ofd, "func _c_min(a: i64, b: i64) -> i64 { if a < b { return a } return b }\n" as *u8)
194 _ts_fputs(ofd, "func _c_max(a: i64, b: i64) -> i64 { if a > b { return a } return b }\n" as *u8)
195 _ts_fputs(ofd, "func _c_add(a: i64, b: i64) -> i64 { return a + b }\n" as *u8)
196 _ts_fputs(ofd, "func _c_sub(a: i64, b: i64) -> i64 { return a - b }\n" as *u8)
197 _ts_fputs(ofd, "func _c_mul(a: i64, b: i64) -> i64 { return a * b }\n" as *u8)
198 _ts_fputs(ofd, "func _c_shl(a: i64, b: i64) -> i64 { return a << (b & 63) }\n" as *u8)
199 _ts_fputs(ofd, "func _c_div(a: i64, b: i64) -> i64 { if b == 0 { return 0 } return a / b }\n" as *u8)
200 _ts_fputs(ofd, "func _c_mod(a: i64, b: i64) -> i64 { if b == 0 { return 0 } return a % b }\n" as *u8)
201 _ts_fputs(ofd, "func _c_abs(a: i64) -> i64 { if a < 0 { return 0 - a } return a }\n" as *u8)
202 _ts_fputs(ofd, "func resynth(a: i64, b: i64) -> i64 { return " as *u8)
203 ts_emit_term(ofd, terms, ix)
204 _ts_fputs(ofd, " }\n" as *u8)
205 _ts_fputs(ofd, "func main() -> i64 {\n" as *u8)
206 _ts_fputs(ofd, " let ka: *i64 = sys_mmap(64) as *i64\n" as *u8)
207 _ts_fputs(ofd, " let kb: *i64 = sys_mmap(64) as *i64\n" as *u8)
208 _ts_fputs(ofd, " let kx: *i64 = sys_mmap(64) as *i64\n" as *u8)
209 var k: i64 = 0
210 while k < TS_K {
211 _ts_fputs(ofd, " ka[" as *u8); _ts_fnum(ofd, k); _ts_fputs(ofd, "] = " as *u8); _ts_fsig(ofd, ka[k]); _ts_fputs(ofd, "\n" as *u8)
212 _ts_fputs(ofd, " kb[" as *u8); _ts_fnum(ofd, k); _ts_fputs(ofd, "] = " as *u8); _ts_fsig(ofd, kb[k]); _ts_fputs(ofd, "\n" as *u8)
213 _ts_fputs(ofd, " kx[" as *u8); _ts_fnum(ofd, k); _ts_fputs(ofd, "] = " as *u8); _ts_fsig(ofd, kx[k]); _ts_fputs(ofd, "\n" as *u8)
214 k = k + 1
215 }
216 _ts_fputs(ofd, " var i: i64 = 0\n" as *u8)
217 _ts_fputs(ofd, " while i < 8 {\n" as *u8)
218 _ts_fputs(ofd, " if resynth(ka[i], kb[i]) != kx[i] { sys_exit(1) }\n" as *u8)
219 _ts_fputs(ofd, " i = i + 1\n" as *u8)
220 _ts_fputs(ofd, " }\n" as *u8)
221 _ts_fputs(ofd, " sys_exit(0)\n return 0\n}\n" as *u8)
222 sys_close(ofd)
223 return 0
224}
225func ts_corpus_line(label: *u8, verdict: *u8, terms: *i64, ix: i64, kept: i64, cand: i64) -> i64 {
226 let cfd: i64 = sys_openat_append("knowledge/library/synth_corpus.log" as *u8, 0x1a4)
227 if cfd < 0 { return 0 - 1 }
228 _ts_fputs(cfd, "SYNTH engine=type-search spec=" as *u8); _ts_fputs(cfd, label)
229 _ts_fputs(cfd, " verdict=" as *u8); _ts_fputs(cfd, verdict)
230 _ts_fputs(cfd, " kept=" as *u8); _ts_fnum(cfd, kept)
231 _ts_fputs(cfd, " cand=" as *u8); _ts_fnum(cfd, cand)
232 _ts_fputs(cfd, " expr=" as *u8)
233 if ix >= 0 { ts_emit_term(cfd, terms, ix) } else { _ts_fputs(cfd, "none" as *u8) }
234 _ts_fputs(cfd, "\n" as *u8)
235 sys_close(cfd)
236 return 0
237}
238func ts_round(label: *u8, path: *u8, ka: *i64, kb: *i64, kx: *i64, cons: *i64, ncon: i64, terms: *i64, vecs: *i64, hash: *i64) -> i64 {
239 let stats: *i64 = sys_mmap(64) as *i64
240 stats[3] = 0
241 let r: i64 = ts_search(ka, kb, kx, cons, ncon, terms, vecs, hash, stats)
242 _ts_puts("TSRCH spec=" as *u8); _ts_puts(label)
243 _ts_puts(" cand=" as *u8); _ts_num(stats[0])
244 _ts_puts(" kept=" as *u8); _ts_num(stats[1])
245 _ts_puts(" lvl=" as *u8); _ts_num(stats[2])
246 if r < 0 {
247 if stats[3] == 1 { _ts_puts(" SEARCH-EXHAUSTED (budget/term-cap)\n" as *u8) } else { _ts_puts(" SEARCH-EXHAUSTED (depth)\n" as *u8) }
248 ts_corpus_line(label, "EXHAUSTED" as *u8, terms, 0 - 1, stats[1], stats[0])
249 return 0
250 }
251 _ts_puts(" FOUND -> " as *u8); _ts_puts(path); _ts_puts("\n" as *u8)
252 ts_emit_module(path, terms, r, ka, kb, kx)
253 ts_corpus_line(label, "FOUND" as *u8, terms, r, stats[1], stats[0])
254 return 1
255}
256func main() -> i64 {
257 _ts_puts("=== BUILDER-AUTHORED TYPE-SEARCH: obs-equiv synthesis, 3 held-out re-derivations ===\n" as *u8)
258 let terms: *i64 = sys_mmap(1310720) as *i64
259 let vecs: *i64 = sys_mmap(2097152) as *i64
260 let hash: *i64 = sys_mmap(1048576) as *i64
261 let cons: *i64 = sys_mmap(64) as *i64
262 cons[0] = 0
263 cons[1] = 1
264 cons[2] = 3
265 cons[3] = 7
266 cons[4] = 10
267 cons[5] = 99
268 let ka: *i64 = sys_mmap(64) as *i64
269 let kb: *i64 = sys_mmap(64) as *i64
270 let kx: *i64 = sys_mmap(64) as *i64
271 var won: i64 = 0
272 ka[0]=10
273 ka[1]=9
274 ka[2]=10
275 ka[3]=20
276 ka[4]=50
277 ka[5]=100
278 ka[6]=9
279 ka[7]=10
280 kb[0]=99
281 kb[1]=99
282 kb[2]=100
283 kb[3]=40
284 kb[4]=42
285 kb[5]=99
286 kb[6]=0
287 kb[7]=1000
288 kx[0]=1
289 kx[1]=0
290 kx[2]=0
291 kx[3]=1
292 kx[4]=1
293 kx[5]=1
294 kx[6]=0
295 kx[7]=0
296 won = won + ts_round("validator-core" as *u8, "runtime/_hdl_build/_resynth_validator.nx" as *u8, ka, kb, kx, cons, 6, terms, vecs, hash)
297 ka[0]=180
298 ka[1]=255
299 ka[2]=8
300 ka[3]=1023
301 ka[4]=64
302 ka[5]=7
303 ka[6]=256
304 ka[7]=5
305 kb[0]=2
306 kb[1]=0
307 kb[2]=3
308 kb[3]=7
309 kb[4]=6
310 kb[5]=0
311 kb[6]=8
312 kb[7]=1
313 kx[0]=5
314 kx[1]=7
315 kx[2]=1
316 kx[3]=7
317 kx[4]=1
318 kx[5]=7
319 kx[6]=1
320 kx[7]=2
321 won = won + ts_round("bitfield-extract" as *u8, "runtime/_hdl_build/_resynth_bitfield.nx" as *u8, ka, kb, kx, cons, 6, terms, vecs, hash)
322 ka[0]=5
323 ka[1]=0-3
324 ka[2]=15
325 ka[3]=0
326 ka[4]=0-100
327 ka[5]=7
328 ka[6]=100
329 ka[7]=0-1
330 kb[0]=10
331 kb[1]=10
332 kb[2]=10
333 kb[3]=0
334 kb[4]=50
335 kb[5]=7
336 kb[6]=3
337 kb[7]=0
338 kx[0]=5
339 kx[1]=0
340 kx[2]=10
341 kx[3]=0
342 kx[4]=0
343 kx[5]=7
344 kx[6]=3
345 kx[7]=0
346 won = won + ts_round("clamp" as *u8, "runtime/_hdl_build/_resynth_clamp.nx" as *u8, ka, kb, kx, cons, 6, terms, vecs, hash)
347 _ts_puts("TYPE-SEARCH GATE: " as *u8); _ts_num(won); _ts_puts("/3 re-synthesized (Engineer builds+runs the emitted modules next)\n" as *u8)
348 if won == 3 { sys_exit(0); return 0 }
349 sys_exit(1)
350 return 1
351}