code wiki / _hdl_build / nx_search_governor_test.nx
nx_search_governor_test.nx source
↩ module page · 67 lines · 4344 B
1// nx_search_governor_test.nx -- PROOF the team UNDERSTANDS the principle (not just one function):
2// the SAME governor (sg_decide / sg_cost_cap) judges TWO unrelated search spaces, each fed by a
3// REAL shortest-program search. If one judgment drives both multiply-by-constant AND boolean
4// circuits, the team has internalized the general idea (cost-bounded shortest-program search /
5// resource-bounded Kolmogorov complexity), not memorized a multiply rule.
6// Space 1 -- MULTIPLY: real chain search; baseline = imul (3 cyc); op_cost = 1 cyc/op.
7// Space 2 -- BOOLEAN: real minimal-circuit search; baseline = naive 2-level sum-of-products
8// (~3m+2 two-input gates for m minterms -- what un-optimized logic emits); op_cost = 1.
9// Exit 0 only if every verdict matches and equals sg_decide recomputed independently.
10// license_tier: ORIGINAL
11
12import "nx_mulchain_deep.nx" // mulchain_deep_verified + the governor (sg_decide, sg_cost_cap)
13import "nx_boolsynth.nx" // bl_find -- the boolean search space
14import "nx_syscalls.nx"
15
16func gt_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func gt_num(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m}; 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 }
18
19func gt_popcount(v: i64) -> i64 { var n: i64 = 0; var x: i64 = v & 255; while x > 0 { n = n + (x & 1); x = x >> 1 } return n }
20
21// MULTIPLY space, decided by the shared governor. baseline = imul cost (cyc).
22func gt_mul(c: i64, cap: i64, imul_cost: i64) -> i64 {
23 let op: *i64 = sys_mmap(8*20) as *i64; let a: *i64 = sys_mmap(8*20) as *i64; let b: *i64 = sys_mmap(8*20) as *i64
24 let L: i64 = mulchain_deep_verified(c, cap, op, a, b)
25 return sg_decide(L, 1, imul_cost)
26}
27// BOOLEAN space, decided by the SAME governor. baseline = naive SOP gate count = 3*minterms + 2.
28func gt_bool(tt: i64, maxL: i64) -> i64 {
29 let op: *i64 = sys_mmap(8*20) as *i64; let a: *i64 = sys_mmap(8*20) as *i64; let b: *i64 = sys_mmap(8*20) as *i64
30 let G: i64 = bl_find(tt, op, a, b, maxL)
31 let base: i64 = 3 * gt_popcount(tt) + 2
32 return sg_decide(G, 1, base)
33}
34
35func main() -> i64 {
36 gt_puts("=== ONE governor, TWO search spaces (does the team understand the principle?) ===\n" as *u8)
37 let r: *i64 = sys_mmap(8 * 16) as *i64
38 let imul: i64 = 3
39 let cap: i64 = sg_cost_cap(imul, 1) // Levin cost cap = 3: deepest chain worth searching
40 gt_puts(" Levin cost-cap for multiply (imul=3, op=1cyc): maxL=" as *u8); gt_num(cap); gt_puts("\n" as *u8)
41
42 // MULTIPLY: 45 has a 2-op chain (beats imul) -> USE chain; 466 needs 5 ops (> imul) -> USE imul.
43 let m45: i64 = gt_mul(45, 5, imul)
44 let m466: i64 = gt_mul(466, 5, imul)
45 gt_puts(" [mul] c=45 govern=" as *u8); gt_num(m45); gt_puts(" (expect chain, >0)\n" as *u8)
46 gt_puts(" [mul] c=466 govern=" as *u8); gt_num(m466); gt_puts(" (expect imul, =0)\n" as *u8)
47 r[0] = 0; if m45 > 0 { r[0] = 1 }
48 r[1] = 0; if m466 == 0 { r[1] = 1 }
49
50 // BOOLEAN: parity (0x96) -> 2 XORs beat naive SOP -> USE circuit; single minterm (0x01) -> our
51 // 3-gate NOR also beats naive SOP. Both should be circuit-wins under the SAME sg_decide.
52 let bpar: i64 = gt_bool(150, 6) // 0x96 = a^b^c
53 let bmin: i64 = gt_bool(1, 6) // 0x01 = ~a & ~b & ~c
54 gt_puts(" [bool] tt=0x96 (parity) govern=" as *u8); gt_num(bpar); gt_puts(" (expect circuit, >0)\n" as *u8)
55 gt_puts(" [bool] tt=0x01 (minterm) govern=" as *u8); gt_num(bmin); gt_puts(" (expect circuit, >0)\n" as *u8)
56 r[2] = 0; if bpar > 0 { r[2] = 1 }
57 r[3] = 0; if bmin > 0 { r[3] = 1 }
58
59 // and the governor's judgment is the SAME callable in both: recompute sg_decide directly.
60 r[4] = 0; if sg_decide(2, 1, 3) == 2 { if sg_decide(5, 1, 3) == 0 { r[4] = 1 } }
61
62 var pass: i64 = 0; var i: i64 = 0
63 while i < 5 { pass = pass + r[i]; i = i + 1 }
64 gt_puts("----\n passed " as *u8); gt_num(pass); gt_puts("/5\n" as *u8)
65 if pass == 5 { gt_puts(" UNDERSTOOD: one cost-bounded-search judgment governs both spaces.\n" as *u8); sys_exit(0); return 0 }
66 gt_puts(" FAIL\n" as *u8); sys_exit(1); return 1
67}